4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25 * Copyright (c) 2018, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright 2013 Saso Kiselkov. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
29 * Copyright 2016 Toomas Soome <tsoome@me.com>
30 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
31 * Copyright 2018 Joyent, Inc.
32 * Copyright (c) 2017, 2019, Datto Inc. All rights reserved.
33 * Copyright 2017 Joyent, Inc.
34 * Copyright (c) 2017, Intel Corporation.
35 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
36 * Copyright (c) 2023 Hewlett Packard Enterprise Development LP.
40 * SPA: Storage Pool Allocator
42 * This file contains all the routines used when modifying on-disk SPA state.
43 * This includes opening, importing, destroying, exporting a pool, and syncing a
47 #include <sys/zfs_context.h>
48 #include <sys/fm/fs/zfs.h>
49 #include <sys/spa_impl.h>
51 #include <sys/zio_checksum.h>
53 #include <sys/dmu_tx.h>
58 #include <sys/vdev_impl.h>
59 #include <sys/vdev_removal.h>
60 #include <sys/vdev_indirect_mapping.h>
61 #include <sys/vdev_indirect_births.h>
62 #include <sys/vdev_initialize.h>
63 #include <sys/vdev_rebuild.h>
64 #include <sys/vdev_trim.h>
65 #include <sys/vdev_disk.h>
66 #include <sys/vdev_raidz.h>
67 #include <sys/vdev_draid.h>
68 #include <sys/metaslab.h>
69 #include <sys/metaslab_impl.h>
71 #include <sys/uberblock_impl.h>
74 #include <sys/bpobj.h>
75 #include <sys/dmu_traverse.h>
76 #include <sys/dmu_objset.h>
77 #include <sys/unique.h>
78 #include <sys/dsl_pool.h>
79 #include <sys/dsl_dataset.h>
80 #include <sys/dsl_dir.h>
81 #include <sys/dsl_prop.h>
82 #include <sys/dsl_synctask.h>
83 #include <sys/fs/zfs.h>
85 #include <sys/callb.h>
86 #include <sys/systeminfo.h>
87 #include <sys/zfs_ioctl.h>
88 #include <sys/dsl_scan.h>
89 #include <sys/zfeature.h>
90 #include <sys/dsl_destroy.h>
94 #include <sys/fm/protocol.h>
95 #include <sys/fm/util.h>
96 #include <sys/callb.h>
98 #include <sys/vmsystm.h>
101 #include "zfs_prop.h"
102 #include "zfs_comutil.h"
103 #include <cityhash.h>
106 * spa_thread() existed on Illumos as a parent thread for the various worker
107 * threads that actually run the pool, as a way to both reference the entire
108 * pool work as a single object, and to share properties like scheduling
109 * options. It has not yet been adapted to Linux or FreeBSD. This define is
110 * used to mark related parts of the code to make things easier for the reader,
111 * and to compile this code out. It can be removed when someone implements it,
112 * moves it to some Illumos-specific place, or removes it entirely.
114 #undef HAVE_SPA_THREAD
117 * The "System Duty Cycle" scheduling class is an Illumos feature to help
118 * prevent CPU-intensive kernel threads from affecting latency on interactive
119 * threads. It doesn't exist on Linux or FreeBSD, so the supporting code is
120 * gated behind a define. On Illumos SDC depends on spa_thread(), but
121 * spa_thread() also has other uses, so this is a separate define.
126 * The interval, in seconds, at which failed configuration cache file writes
129 int zfs_ccw_retry_interval
= 300;
131 typedef enum zti_modes
{
132 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
133 ZTI_MODE_SCALE
, /* Taskqs scale with CPUs. */
134 ZTI_MODE_SYNC
, /* sync thread assigned */
135 ZTI_MODE_NULL
, /* don't create a taskq */
139 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
140 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
141 #define ZTI_SCALE { ZTI_MODE_SCALE, 0, 1 }
142 #define ZTI_SYNC { ZTI_MODE_SYNC, 0, 1 }
143 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
145 #define ZTI_N(n) ZTI_P(n, 1)
146 #define ZTI_ONE ZTI_N(1)
148 typedef struct zio_taskq_info
{
149 zti_modes_t zti_mode
;
154 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
155 "iss", "iss_h", "int", "int_h"
159 * This table defines the taskq settings for each ZFS I/O type. When
160 * initializing a pool, we use this table to create an appropriately sized
161 * taskq. Some operations are low volume and therefore have a small, static
162 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
163 * macros. Other operations process a large amount of data; the ZTI_SCALE
164 * macro causes us to create a taskq oriented for throughput. Some operations
165 * are so high frequency and short-lived that the taskq itself can become a
166 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
167 * additional degree of parallelism specified by the number of threads per-
168 * taskq and the number of taskqs; when dispatching an event in this case, the
169 * particular taskq is chosen at random. ZTI_SCALE uses a number of taskqs
170 * that scales with the number of CPUs.
172 * The different taskq priorities are to handle the different contexts (issue
173 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
174 * need to be handled with minimum delay.
176 static zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
177 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
178 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
179 { ZTI_N(8), ZTI_NULL
, ZTI_SCALE
, ZTI_NULL
}, /* READ */
180 { ZTI_SYNC
, ZTI_N(5), ZTI_SCALE
, ZTI_N(5) }, /* WRITE */
181 { ZTI_SCALE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
182 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
183 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
184 { ZTI_N(4), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* TRIM */
187 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
188 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
189 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
190 static int spa_load_impl(spa_t
*spa
, spa_import_type_t type
,
191 const char **ereport
);
192 static void spa_vdev_resilver_done(spa_t
*spa
);
195 * Percentage of all CPUs that can be used by the metaslab preload taskq.
197 static uint_t metaslab_preload_pct
= 50;
199 static uint_t zio_taskq_batch_pct
= 80; /* 1 thread per cpu in pset */
200 static uint_t zio_taskq_batch_tpq
; /* threads per taskq */
203 static const boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
204 static const uint_t zio_taskq_basedc
= 80; /* base duty cycle */
207 #ifdef HAVE_SPA_THREAD
208 static const boolean_t spa_create_process
= B_TRUE
; /* no process => no sysdc */
211 static uint_t zio_taskq_wr_iss_ncpus
= 0;
214 * Report any spa_load_verify errors found, but do not fail spa_load.
215 * This is used by zdb to analyze non-idle pools.
217 boolean_t spa_load_verify_dryrun
= B_FALSE
;
220 * Allow read spacemaps in case of readonly import (spa_mode == SPA_MODE_READ).
221 * This is used by zdb for spacemaps verification.
223 boolean_t spa_mode_readable_spacemaps
= B_FALSE
;
226 * This (illegal) pool name is used when temporarily importing a spa_t in order
227 * to get the vdev stats associated with the imported devices.
229 #define TRYIMPORT_NAME "$import"
232 * For debugging purposes: print out vdev tree during pool import.
234 static int spa_load_print_vdev_tree
= B_FALSE
;
237 * A non-zero value for zfs_max_missing_tvds means that we allow importing
238 * pools with missing top-level vdevs. This is strictly intended for advanced
239 * pool recovery cases since missing data is almost inevitable. Pools with
240 * missing devices can only be imported read-only for safety reasons, and their
241 * fail-mode will be automatically set to "continue".
243 * With 1 missing vdev we should be able to import the pool and mount all
244 * datasets. User data that was not modified after the missing device has been
245 * added should be recoverable. This means that snapshots created prior to the
246 * addition of that device should be completely intact.
248 * With 2 missing vdevs, some datasets may fail to mount since there are
249 * dataset statistics that are stored as regular metadata. Some data might be
250 * recoverable if those vdevs were added recently.
252 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
253 * may be missing entirely. Chances of data recovery are very low. Note that
254 * there are also risks of performing an inadvertent rewind as we might be
255 * missing all the vdevs with the latest uberblocks.
257 uint64_t zfs_max_missing_tvds
= 0;
260 * The parameters below are similar to zfs_max_missing_tvds but are only
261 * intended for a preliminary open of the pool with an untrusted config which
262 * might be incomplete or out-dated.
264 * We are more tolerant for pools opened from a cachefile since we could have
265 * an out-dated cachefile where a device removal was not registered.
266 * We could have set the limit arbitrarily high but in the case where devices
267 * are really missing we would want to return the proper error codes; we chose
268 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
269 * and we get a chance to retrieve the trusted config.
271 uint64_t zfs_max_missing_tvds_cachefile
= SPA_DVAS_PER_BP
- 1;
274 * In the case where config was assembled by scanning device paths (/dev/dsks
275 * by default) we are less tolerant since all the existing devices should have
276 * been detected and we want spa_load to return the right error codes.
278 uint64_t zfs_max_missing_tvds_scan
= 0;
281 * Debugging aid that pauses spa_sync() towards the end.
283 static const boolean_t zfs_pause_spa_sync
= B_FALSE
;
286 * Variables to indicate the livelist condense zthr func should wait at certain
287 * points for the livelist to be removed - used to test condense/destroy races
289 static int zfs_livelist_condense_zthr_pause
= 0;
290 static int zfs_livelist_condense_sync_pause
= 0;
293 * Variables to track whether or not condense cancellation has been
294 * triggered in testing.
296 static int zfs_livelist_condense_sync_cancel
= 0;
297 static int zfs_livelist_condense_zthr_cancel
= 0;
300 * Variable to track whether or not extra ALLOC blkptrs were added to a
301 * livelist entry while it was being condensed (caused by the way we track
302 * remapped blkptrs in dbuf_remap_impl)
304 static int zfs_livelist_condense_new_alloc
= 0;
307 * ==========================================================================
308 * SPA properties routines
309 * ==========================================================================
313 * Add a (source=src, propname=propval) list to an nvlist.
316 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, const char *strval
,
317 uint64_t intval
, zprop_source_t src
)
319 const char *propname
= zpool_prop_to_name(prop
);
322 propval
= fnvlist_alloc();
323 fnvlist_add_uint64(propval
, ZPROP_SOURCE
, src
);
326 fnvlist_add_string(propval
, ZPROP_VALUE
, strval
);
328 fnvlist_add_uint64(propval
, ZPROP_VALUE
, intval
);
330 fnvlist_add_nvlist(nvl
, propname
, propval
);
331 nvlist_free(propval
);
335 * Add a user property (source=src, propname=propval) to an nvlist.
338 spa_prop_add_user(nvlist_t
*nvl
, const char *propname
, char *strval
,
343 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
344 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
345 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
346 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
347 nvlist_free(propval
);
351 * Get property values from the spa configuration.
354 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
356 vdev_t
*rvd
= spa
->spa_root_vdev
;
357 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
358 uint64_t size
, alloc
, cap
, version
;
359 const zprop_source_t src
= ZPROP_SRC_NONE
;
360 spa_config_dirent_t
*dp
;
361 metaslab_class_t
*mc
= spa_normal_class(spa
);
363 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
366 alloc
= metaslab_class_get_alloc(mc
);
367 alloc
+= metaslab_class_get_alloc(spa_special_class(spa
));
368 alloc
+= metaslab_class_get_alloc(spa_dedup_class(spa
));
369 alloc
+= metaslab_class_get_alloc(spa_embedded_log_class(spa
));
371 size
= metaslab_class_get_space(mc
);
372 size
+= metaslab_class_get_space(spa_special_class(spa
));
373 size
+= metaslab_class_get_space(spa_dedup_class(spa
));
374 size
+= metaslab_class_get_space(spa_embedded_log_class(spa
));
376 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
377 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
378 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
379 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
381 spa_prop_add_list(*nvp
, ZPOOL_PROP_CHECKPOINT
, NULL
,
382 spa
->spa_checkpoint_info
.sci_dspace
, src
);
384 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
385 metaslab_class_fragmentation(mc
), src
);
386 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
387 metaslab_class_expandable_space(mc
), src
);
388 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
389 (spa_mode(spa
) == SPA_MODE_READ
), src
);
391 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
392 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
394 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
395 ddt_get_pool_dedup_ratio(spa
), src
);
396 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONEUSED
, NULL
,
397 brt_get_used(spa
), src
);
398 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONESAVED
, NULL
,
399 brt_get_saved(spa
), src
);
400 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONERATIO
, NULL
,
401 brt_get_ratio(spa
), src
);
403 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
404 rvd
->vdev_state
, src
);
406 version
= spa_version(spa
);
407 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
408 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
409 version
, ZPROP_SRC_DEFAULT
);
411 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
412 version
, ZPROP_SRC_LOCAL
);
414 spa_prop_add_list(*nvp
, ZPOOL_PROP_LOAD_GUID
,
415 NULL
, spa_load_guid(spa
), src
);
420 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
421 * when opening pools before this version freedir will be NULL.
423 if (pool
->dp_free_dir
!= NULL
) {
424 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
425 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
428 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
432 if (pool
->dp_leak_dir
!= NULL
) {
433 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
434 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
437 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
442 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
444 if (spa
->spa_comment
!= NULL
) {
445 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
449 if (spa
->spa_compatibility
!= NULL
) {
450 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMPATIBILITY
,
451 spa
->spa_compatibility
, 0, ZPROP_SRC_LOCAL
);
454 if (spa
->spa_root
!= NULL
)
455 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
458 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
459 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
460 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
462 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
463 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
466 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
467 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
468 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
470 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
471 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
474 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
475 if (dp
->scd_path
== NULL
) {
476 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
477 "none", 0, ZPROP_SRC_LOCAL
);
478 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
479 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
480 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
486 * Get zpool property values.
489 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
491 objset_t
*mos
= spa
->spa_meta_objset
;
497 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
501 dp
= spa_get_dsl(spa
);
502 dsl_pool_config_enter(dp
, FTAG
);
503 mutex_enter(&spa
->spa_props_lock
);
506 * Get properties from the spa config.
508 spa_prop_get_config(spa
, nvp
);
510 /* If no pool property object, no more prop to get. */
511 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0)
515 * Get properties from the MOS pool property object.
517 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
518 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
519 zap_cursor_advance(&zc
)) {
522 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
525 if ((prop
= zpool_name_to_prop(za
.za_name
)) ==
526 ZPOOL_PROP_INVAL
&& !zfs_prop_user(za
.za_name
))
529 switch (za
.za_integer_length
) {
531 /* integer property */
532 if (za
.za_first_integer
!=
533 zpool_prop_default_numeric(prop
))
534 src
= ZPROP_SRC_LOCAL
;
536 if (prop
== ZPOOL_PROP_BOOTFS
) {
537 dsl_dataset_t
*ds
= NULL
;
539 err
= dsl_dataset_hold_obj(dp
,
540 za
.za_first_integer
, FTAG
, &ds
);
544 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
546 dsl_dataset_name(ds
, strval
);
547 dsl_dataset_rele(ds
, FTAG
);
550 intval
= za
.za_first_integer
;
553 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
556 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
561 /* string property */
562 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
563 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
564 za
.za_name
, 1, za
.za_num_integers
, strval
);
566 kmem_free(strval
, za
.za_num_integers
);
569 if (prop
!= ZPOOL_PROP_INVAL
) {
570 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
572 src
= ZPROP_SRC_LOCAL
;
573 spa_prop_add_user(*nvp
, za
.za_name
, strval
,
576 kmem_free(strval
, za
.za_num_integers
);
583 zap_cursor_fini(&zc
);
585 mutex_exit(&spa
->spa_props_lock
);
586 dsl_pool_config_exit(dp
, FTAG
);
587 if (err
&& err
!= ENOENT
) {
597 * Validate the given pool properties nvlist and modify the list
598 * for the property values to be set.
601 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
604 int error
= 0, reset_bootfs
= 0;
606 boolean_t has_feature
= B_FALSE
;
609 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
611 const char *strval
, *slash
, *check
, *fname
;
612 const char *propname
= nvpair_name(elem
);
613 zpool_prop_t prop
= zpool_name_to_prop(propname
);
616 case ZPOOL_PROP_INVAL
:
618 * Sanitize the input.
620 if (zfs_prop_user(propname
)) {
621 if (strlen(propname
) >= ZAP_MAXNAMELEN
) {
622 error
= SET_ERROR(ENAMETOOLONG
);
626 if (strlen(fnvpair_value_string(elem
)) >=
628 error
= SET_ERROR(E2BIG
);
631 } else if (zpool_prop_feature(propname
)) {
632 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
633 error
= SET_ERROR(EINVAL
);
637 if (nvpair_value_uint64(elem
, &intval
) != 0) {
638 error
= SET_ERROR(EINVAL
);
643 error
= SET_ERROR(EINVAL
);
647 fname
= strchr(propname
, '@') + 1;
648 if (zfeature_lookup_name(fname
, NULL
) != 0) {
649 error
= SET_ERROR(EINVAL
);
653 has_feature
= B_TRUE
;
655 error
= SET_ERROR(EINVAL
);
660 case ZPOOL_PROP_VERSION
:
661 error
= nvpair_value_uint64(elem
, &intval
);
663 (intval
< spa_version(spa
) ||
664 intval
> SPA_VERSION_BEFORE_FEATURES
||
666 error
= SET_ERROR(EINVAL
);
669 case ZPOOL_PROP_DELEGATION
:
670 case ZPOOL_PROP_AUTOREPLACE
:
671 case ZPOOL_PROP_LISTSNAPS
:
672 case ZPOOL_PROP_AUTOEXPAND
:
673 case ZPOOL_PROP_AUTOTRIM
:
674 error
= nvpair_value_uint64(elem
, &intval
);
675 if (!error
&& intval
> 1)
676 error
= SET_ERROR(EINVAL
);
679 case ZPOOL_PROP_MULTIHOST
:
680 error
= nvpair_value_uint64(elem
, &intval
);
681 if (!error
&& intval
> 1)
682 error
= SET_ERROR(EINVAL
);
685 uint32_t hostid
= zone_get_hostid(NULL
);
687 spa
->spa_hostid
= hostid
;
689 error
= SET_ERROR(ENOTSUP
);
694 case ZPOOL_PROP_BOOTFS
:
696 * If the pool version is less than SPA_VERSION_BOOTFS,
697 * or the pool is still being created (version == 0),
698 * the bootfs property cannot be set.
700 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
701 error
= SET_ERROR(ENOTSUP
);
706 * Make sure the vdev config is bootable
708 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
709 error
= SET_ERROR(ENOTSUP
);
715 error
= nvpair_value_string(elem
, &strval
);
720 if (strval
== NULL
|| strval
[0] == '\0') {
721 objnum
= zpool_prop_default_numeric(
726 error
= dmu_objset_hold(strval
, FTAG
, &os
);
731 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
732 error
= SET_ERROR(ENOTSUP
);
734 objnum
= dmu_objset_id(os
);
736 dmu_objset_rele(os
, FTAG
);
740 case ZPOOL_PROP_FAILUREMODE
:
741 error
= nvpair_value_uint64(elem
, &intval
);
742 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
743 error
= SET_ERROR(EINVAL
);
746 * This is a special case which only occurs when
747 * the pool has completely failed. This allows
748 * the user to change the in-core failmode property
749 * without syncing it out to disk (I/Os might
750 * currently be blocked). We do this by returning
751 * EIO to the caller (spa_prop_set) to trick it
752 * into thinking we encountered a property validation
755 if (!error
&& spa_suspended(spa
)) {
756 spa
->spa_failmode
= intval
;
757 error
= SET_ERROR(EIO
);
761 case ZPOOL_PROP_CACHEFILE
:
762 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
765 if (strval
[0] == '\0')
768 if (strcmp(strval
, "none") == 0)
771 if (strval
[0] != '/') {
772 error
= SET_ERROR(EINVAL
);
776 slash
= strrchr(strval
, '/');
777 ASSERT(slash
!= NULL
);
779 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
780 strcmp(slash
, "/..") == 0)
781 error
= SET_ERROR(EINVAL
);
784 case ZPOOL_PROP_COMMENT
:
785 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
787 for (check
= strval
; *check
!= '\0'; check
++) {
788 if (!isprint(*check
)) {
789 error
= SET_ERROR(EINVAL
);
793 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
794 error
= SET_ERROR(E2BIG
);
805 (void) nvlist_remove_all(props
,
806 zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO
));
808 if (!error
&& reset_bootfs
) {
809 error
= nvlist_remove(props
,
810 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
813 error
= nvlist_add_uint64(props
,
814 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
822 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
824 const char *cachefile
;
825 spa_config_dirent_t
*dp
;
827 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
831 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
834 if (cachefile
[0] == '\0')
835 dp
->scd_path
= spa_strdup(spa_config_path
);
836 else if (strcmp(cachefile
, "none") == 0)
839 dp
->scd_path
= spa_strdup(cachefile
);
841 list_insert_head(&spa
->spa_config_list
, dp
);
843 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
847 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
850 nvpair_t
*elem
= NULL
;
851 boolean_t need_sync
= B_FALSE
;
853 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
856 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
857 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
859 if (prop
== ZPOOL_PROP_CACHEFILE
||
860 prop
== ZPOOL_PROP_ALTROOT
||
861 prop
== ZPOOL_PROP_READONLY
)
864 if (prop
== ZPOOL_PROP_INVAL
&&
865 zfs_prop_user(nvpair_name(elem
))) {
870 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
873 if (prop
== ZPOOL_PROP_VERSION
) {
874 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
876 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
877 ver
= SPA_VERSION_FEATURES
;
881 /* Save time if the version is already set. */
882 if (ver
== spa_version(spa
))
886 * In addition to the pool directory object, we might
887 * create the pool properties object, the features for
888 * read object, the features for write object, or the
889 * feature descriptions object.
891 error
= dsl_sync_task(spa
->spa_name
, NULL
,
892 spa_sync_version
, &ver
,
893 6, ZFS_SPACE_CHECK_RESERVED
);
904 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
905 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
912 * If the bootfs property value is dsobj, clear it.
915 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
917 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
918 VERIFY(zap_remove(spa
->spa_meta_objset
,
919 spa
->spa_pool_props_object
,
920 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
926 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
928 uint64_t *newguid __maybe_unused
= arg
;
929 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
930 vdev_t
*rvd
= spa
->spa_root_vdev
;
933 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
934 int error
= (spa_has_checkpoint(spa
)) ?
935 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
936 return (SET_ERROR(error
));
939 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
940 vdev_state
= rvd
->vdev_state
;
941 spa_config_exit(spa
, SCL_STATE
, FTAG
);
943 if (vdev_state
!= VDEV_STATE_HEALTHY
)
944 return (SET_ERROR(ENXIO
));
946 ASSERT3U(spa_guid(spa
), !=, *newguid
);
952 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
954 uint64_t *newguid
= arg
;
955 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
957 vdev_t
*rvd
= spa
->spa_root_vdev
;
959 oldguid
= spa_guid(spa
);
961 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
962 rvd
->vdev_guid
= *newguid
;
963 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
964 vdev_config_dirty(rvd
);
965 spa_config_exit(spa
, SCL_STATE
, FTAG
);
967 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
968 (u_longlong_t
)oldguid
, (u_longlong_t
)*newguid
);
972 * Change the GUID for the pool. This is done so that we can later
973 * re-import a pool built from a clone of our own vdevs. We will modify
974 * the root vdev's guid, our own pool guid, and then mark all of our
975 * vdevs dirty. Note that we must make sure that all our vdevs are
976 * online when we do this, or else any vdevs that weren't present
977 * would be orphaned from our pool. We are also going to issue a
978 * sysevent to update any watchers.
981 spa_change_guid(spa_t
*spa
)
986 mutex_enter(&spa
->spa_vdev_top_lock
);
987 mutex_enter(&spa_namespace_lock
);
988 guid
= spa_generate_guid(NULL
);
990 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
991 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
995 * Clear the kobj flag from all the vdevs to allow
996 * vdev_cache_process_kobj_evt() to post events to all the
997 * vdevs since GUID is updated.
999 vdev_clear_kobj_evt(spa
->spa_root_vdev
);
1000 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
1001 vdev_clear_kobj_evt(spa
->spa_l2cache
.sav_vdevs
[i
]);
1003 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
1004 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
1007 mutex_exit(&spa_namespace_lock
);
1008 mutex_exit(&spa
->spa_vdev_top_lock
);
1014 * ==========================================================================
1015 * SPA state manipulation (open/create/destroy/import/export)
1016 * ==========================================================================
1020 spa_error_entry_compare(const void *a
, const void *b
)
1022 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
1023 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
1026 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
1027 sizeof (zbookmark_phys_t
));
1029 return (TREE_ISIGN(ret
));
1033 * Utility function which retrieves copies of the current logs and
1034 * re-initializes them in the process.
1037 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
1039 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
1041 memcpy(last
, &spa
->spa_errlist_last
, sizeof (avl_tree_t
));
1042 memcpy(scrub
, &spa
->spa_errlist_scrub
, sizeof (avl_tree_t
));
1044 avl_create(&spa
->spa_errlist_scrub
,
1045 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1046 offsetof(spa_error_entry_t
, se_avl
));
1047 avl_create(&spa
->spa_errlist_last
,
1048 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1049 offsetof(spa_error_entry_t
, se_avl
));
1053 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1055 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
1056 enum zti_modes mode
= ztip
->zti_mode
;
1057 uint_t value
= ztip
->zti_value
;
1058 uint_t count
= ztip
->zti_count
;
1059 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1060 uint_t cpus
, flags
= TASKQ_DYNAMIC
;
1063 case ZTI_MODE_FIXED
:
1064 ASSERT3U(value
, >, 0);
1070 * Create one wr_iss taskq for every 'zio_taskq_wr_iss_ncpus',
1071 * not to exceed the number of spa allocators.
1073 if (zio_taskq_wr_iss_ncpus
== 0) {
1074 count
= MAX(boot_ncpus
/ spa
->spa_alloc_count
, 1);
1077 boot_ncpus
/ MAX(1, zio_taskq_wr_iss_ncpus
));
1079 count
= MAX(count
, (zio_taskq_batch_pct
+ 99) / 100);
1080 count
= MIN(count
, spa
->spa_alloc_count
);
1083 * zio_taskq_batch_pct is unbounded and may exceed 100%, but no
1084 * single taskq may have more threads than 100% of online cpus.
1086 value
= (zio_taskq_batch_pct
+ count
/ 2) / count
;
1087 value
= MIN(value
, 100);
1088 flags
|= TASKQ_THREADS_CPU_PCT
;
1091 case ZTI_MODE_SCALE
:
1092 flags
|= TASKQ_THREADS_CPU_PCT
;
1094 * We want more taskqs to reduce lock contention, but we want
1095 * less for better request ordering and CPU utilization.
1097 cpus
= MAX(1, boot_ncpus
* zio_taskq_batch_pct
/ 100);
1098 if (zio_taskq_batch_tpq
> 0) {
1099 count
= MAX(1, (cpus
+ zio_taskq_batch_tpq
/ 2) /
1100 zio_taskq_batch_tpq
);
1103 * Prefer 6 threads per taskq, but no more taskqs
1104 * than threads in them on large systems. For 80%:
1107 * cpus taskqs percent threads threads
1108 * ------- ------- ------- ------- -------
1119 count
= 1 + cpus
/ 6;
1120 while (count
* count
> cpus
)
1123 /* Limit each taskq within 100% to not trigger assertion. */
1124 count
= MAX(count
, (zio_taskq_batch_pct
+ 99) / 100);
1125 value
= (zio_taskq_batch_pct
+ count
/ 2) / count
;
1129 tqs
->stqs_count
= 0;
1130 tqs
->stqs_taskq
= NULL
;
1134 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
1135 "spa_taskqs_init()",
1136 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
1140 ASSERT3U(count
, >, 0);
1141 tqs
->stqs_count
= count
;
1142 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
1144 for (uint_t i
= 0; i
< count
; i
++) {
1149 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
1150 zio_type_name
[t
], zio_taskq_types
[q
], i
);
1152 (void) snprintf(name
, sizeof (name
), "%s_%s",
1153 zio_type_name
[t
], zio_taskq_types
[q
]);
1156 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
1157 (void) zio_taskq_basedc
;
1158 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
1159 spa
->spa_proc
, zio_taskq_basedc
, flags
);
1162 pri_t pri
= maxclsyspri
;
1164 * The write issue taskq can be extremely CPU
1165 * intensive. Run it at slightly less important
1166 * priority than the other taskqs.
1168 * Under Linux and FreeBSD this means incrementing
1169 * the priority value as opposed to platforms like
1170 * illumos where it should be decremented.
1172 * On FreeBSD, if priorities divided by four (RQ_PPQ)
1173 * are equal then a difference between them is
1176 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
) {
1177 #if defined(__linux__)
1179 #elif defined(__FreeBSD__)
1185 tq
= taskq_create_proc(name
, value
, pri
, 50,
1186 INT_MAX
, spa
->spa_proc
, flags
);
1191 tqs
->stqs_taskq
[i
] = tq
;
1196 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1198 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1200 if (tqs
->stqs_taskq
== NULL
) {
1201 ASSERT3U(tqs
->stqs_count
, ==, 0);
1205 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
1206 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
1207 taskq_destroy(tqs
->stqs_taskq
[i
]);
1210 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
1211 tqs
->stqs_taskq
= NULL
;
1216 * The READ and WRITE rows of zio_taskqs are configurable at module load time
1217 * by setting zio_taskq_read or zio_taskq_write.
1219 * Example (the defaults for READ and WRITE)
1220 * zio_taskq_read='fixed,1,8 null scale null'
1221 * zio_taskq_write='sync fixed,1,5 scale fixed,1,5'
1223 * Each sets the entire row at a time.
1225 * 'fixed' is parameterised: fixed,Q,T where Q is number of taskqs, T is number
1226 * of threads per taskq.
1228 * 'null' can only be set on the high-priority queues (queue selection for
1229 * high-priority queues will fall back to the regular queue if the high-pri
1232 static const char *const modes
[ZTI_NMODES
] = {
1233 "fixed", "scale", "sync", "null"
1236 /* Parse the incoming config string. Modifies cfg */
1238 spa_taskq_param_set(zio_type_t t
, char *cfg
)
1242 zio_taskq_info_t row
[ZIO_TASKQ_TYPES
] = {{0}};
1244 char *next
= cfg
, *tok
, *c
;
1247 * Parse out each element from the string and fill `row`. The entire
1248 * row has to be set at once, so any errors are flagged by just
1249 * breaking out of this loop early.
1252 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1253 /* `next` is the start of the config */
1257 /* Eat up leading space */
1258 while (isspace(*next
))
1263 /* Mode ends at space or end of string */
1265 next
= strchr(tok
, ' ');
1266 if (next
!= NULL
) *next
++ = '\0';
1268 /* Parameters start after a comma */
1269 c
= strchr(tok
, ',');
1270 if (c
!= NULL
) *c
++ = '\0';
1272 /* Match mode string */
1274 for (mode
= 0; mode
< ZTI_NMODES
; mode
++)
1275 if (strcmp(tok
, modes
[mode
]) == 0)
1277 if (mode
== ZTI_NMODES
)
1280 /* Invalid canary */
1281 row
[q
].zti_mode
= ZTI_NMODES
;
1283 /* Per-mode setup */
1287 * FIXED is parameterised: number of queues, and number of
1288 * threads per queue.
1290 case ZTI_MODE_FIXED
: {
1291 /* No parameters? */
1292 if (c
== NULL
|| *c
== '\0')
1295 /* Find next parameter */
1297 c
= strchr(tok
, ',');
1301 /* Take digits and convert */
1302 unsigned long long nq
;
1303 if (!(isdigit(*tok
)))
1305 err
= ddi_strtoull(tok
, &tok
, 10, &nq
);
1306 /* Must succeed and also end at the next param sep */
1307 if (err
!= 0 || tok
!= c
)
1310 /* Move past the comma */
1312 /* Need another number */
1313 if (!(isdigit(*tok
)))
1315 /* Remember start to make sure we moved */
1319 unsigned long long ntpq
;
1320 err
= ddi_strtoull(tok
, &tok
, 10, &ntpq
);
1321 /* Must succeed, and moved forward */
1322 if (err
!= 0 || tok
== c
|| *tok
!= '\0')
1326 * sanity; zero queues/threads make no sense, and
1327 * 16K is almost certainly more than anyone will ever
1328 * need and avoids silly numbers like UINT32_MAX
1330 if (nq
== 0 || nq
>= 16384 ||
1331 ntpq
== 0 || ntpq
>= 16384)
1334 const zio_taskq_info_t zti
= ZTI_P(ntpq
, nq
);
1339 case ZTI_MODE_SCALE
: {
1340 const zio_taskq_info_t zti
= ZTI_SCALE
;
1345 case ZTI_MODE_SYNC
: {
1346 const zio_taskq_info_t zti
= ZTI_SYNC
;
1351 case ZTI_MODE_NULL
: {
1353 * Can only null the high-priority queues; the general-
1354 * purpose ones have to exist.
1356 if (q
!= ZIO_TASKQ_ISSUE_HIGH
&&
1357 q
!= ZIO_TASKQ_INTERRUPT_HIGH
)
1360 const zio_taskq_info_t zti
= ZTI_NULL
;
1369 /* Ensure we set a mode */
1370 if (row
[q
].zti_mode
== ZTI_NMODES
)
1374 /* Didn't get a full row, fail */
1375 if (q
< ZIO_TASKQ_TYPES
)
1376 return (SET_ERROR(EINVAL
));
1378 /* Eat trailing space */
1380 while (isspace(*next
))
1383 /* If there's anything left over then fail */
1384 if (next
!= NULL
&& *next
!= '\0')
1385 return (SET_ERROR(EINVAL
));
1387 /* Success! Copy it into the real config */
1388 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++)
1389 zio_taskqs
[t
][q
] = row
[q
];
1395 spa_taskq_param_get(zio_type_t t
, char *buf
)
1399 /* Build paramater string from live config */
1400 const char *sep
= "";
1401 for (uint_t q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1402 const zio_taskq_info_t
*zti
= &zio_taskqs
[t
][q
];
1403 if (zti
->zti_mode
== ZTI_MODE_FIXED
)
1404 pos
+= sprintf(&buf
[pos
], "%s%s,%u,%u", sep
,
1405 modes
[zti
->zti_mode
], zti
->zti_count
,
1408 pos
+= sprintf(&buf
[pos
], "%s%s", sep
,
1409 modes
[zti
->zti_mode
]);
1421 spa_taskq_read_param_set(const char *val
, zfs_kernel_param_t
*kp
)
1423 char *cfg
= kmem_strdup(val
);
1424 int err
= spa_taskq_param_set(ZIO_TYPE_READ
, cfg
);
1425 kmem_free(cfg
, strlen(val
)+1);
1429 spa_taskq_read_param_get(char *buf
, zfs_kernel_param_t
*kp
)
1431 return (spa_taskq_param_get(ZIO_TYPE_READ
, buf
));
1435 spa_taskq_write_param_set(const char *val
, zfs_kernel_param_t
*kp
)
1437 char *cfg
= kmem_strdup(val
);
1438 int err
= spa_taskq_param_set(ZIO_TYPE_WRITE
, cfg
);
1439 kmem_free(cfg
, strlen(val
)+1);
1443 spa_taskq_write_param_get(char *buf
, zfs_kernel_param_t
*kp
)
1445 return (spa_taskq_param_get(ZIO_TYPE_WRITE
, buf
));
1449 * On FreeBSD load-time parameters can be set up before malloc() is available,
1450 * so we have to do all the parsing work on the stack.
1452 #define SPA_TASKQ_PARAM_MAX (128)
1455 spa_taskq_read_param(ZFS_MODULE_PARAM_ARGS
)
1457 char buf
[SPA_TASKQ_PARAM_MAX
];
1460 (void) spa_taskq_param_get(ZIO_TYPE_READ
, buf
);
1461 err
= sysctl_handle_string(oidp
, buf
, sizeof (buf
), req
);
1462 if (err
|| req
->newptr
== NULL
)
1464 return (spa_taskq_param_set(ZIO_TYPE_READ
, buf
));
1468 spa_taskq_write_param(ZFS_MODULE_PARAM_ARGS
)
1470 char buf
[SPA_TASKQ_PARAM_MAX
];
1473 (void) spa_taskq_param_get(ZIO_TYPE_WRITE
, buf
);
1474 err
= sysctl_handle_string(oidp
, buf
, sizeof (buf
), req
);
1475 if (err
|| req
->newptr
== NULL
)
1477 return (spa_taskq_param_set(ZIO_TYPE_WRITE
, buf
));
1480 #endif /* _KERNEL */
1483 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1484 * Note that a type may have multiple discrete taskqs to avoid lock contention
1485 * on the taskq itself.
1488 spa_taskq_dispatch_select(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1491 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1494 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1495 ASSERT3U(tqs
->stqs_count
, !=, 0);
1497 if ((t
== ZIO_TYPE_WRITE
) && (q
== ZIO_TASKQ_ISSUE
) &&
1498 (zio
!= NULL
) && (zio
->io_wr_iss_tq
!= NULL
)) {
1499 /* dispatch to assigned write issue taskq */
1500 tq
= zio
->io_wr_iss_tq
;
1504 if (tqs
->stqs_count
== 1) {
1505 tq
= tqs
->stqs_taskq
[0];
1507 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1513 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1514 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
,
1517 taskq_t
*tq
= spa_taskq_dispatch_select(spa
, t
, q
, zio
);
1518 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
1522 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
1525 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1526 task_func_t
*func
, void *arg
, uint_t flags
)
1528 taskq_t
*tq
= spa_taskq_dispatch_select(spa
, t
, q
, NULL
);
1529 taskqid_t id
= taskq_dispatch(tq
, func
, arg
, flags
);
1531 taskq_wait_id(tq
, id
);
1535 spa_create_zio_taskqs(spa_t
*spa
)
1537 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1538 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1539 spa_taskqs_init(spa
, t
, q
);
1544 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1546 spa_thread(void *arg
)
1548 psetid_t zio_taskq_psrset_bind
= PS_NONE
;
1549 callb_cpr_t cprinfo
;
1552 user_t
*pu
= PTOU(curproc
);
1554 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1557 ASSERT(curproc
!= &p0
);
1558 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1559 "zpool-%s", spa
->spa_name
);
1560 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1562 /* bind this thread to the requested psrset */
1563 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1565 mutex_enter(&cpu_lock
);
1566 mutex_enter(&pidlock
);
1567 mutex_enter(&curproc
->p_lock
);
1569 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1570 0, NULL
, NULL
) == 0) {
1571 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1574 "Couldn't bind process for zfs pool \"%s\" to "
1575 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1578 mutex_exit(&curproc
->p_lock
);
1579 mutex_exit(&pidlock
);
1580 mutex_exit(&cpu_lock
);
1585 if (zio_taskq_sysdc
) {
1586 sysdc_thread_enter(curthread
, 100, 0);
1590 spa
->spa_proc
= curproc
;
1591 spa
->spa_did
= curthread
->t_did
;
1593 spa_create_zio_taskqs(spa
);
1595 mutex_enter(&spa
->spa_proc_lock
);
1596 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1598 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1599 cv_broadcast(&spa
->spa_proc_cv
);
1601 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1602 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1603 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1604 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1606 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1607 spa
->spa_proc_state
= SPA_PROC_GONE
;
1608 spa
->spa_proc
= &p0
;
1609 cv_broadcast(&spa
->spa_proc_cv
);
1610 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1612 mutex_enter(&curproc
->p_lock
);
1617 extern metaslab_ops_t
*metaslab_allocator(spa_t
*spa
);
1620 * Activate an uninitialized pool.
1623 spa_activate(spa_t
*spa
, spa_mode_t mode
)
1625 metaslab_ops_t
*msp
= metaslab_allocator(spa
);
1626 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1628 spa
->spa_state
= POOL_STATE_ACTIVE
;
1629 spa
->spa_mode
= mode
;
1630 spa
->spa_read_spacemaps
= spa_mode_readable_spacemaps
;
1632 spa
->spa_normal_class
= metaslab_class_create(spa
, msp
);
1633 spa
->spa_log_class
= metaslab_class_create(spa
, msp
);
1634 spa
->spa_embedded_log_class
= metaslab_class_create(spa
, msp
);
1635 spa
->spa_special_class
= metaslab_class_create(spa
, msp
);
1636 spa
->spa_dedup_class
= metaslab_class_create(spa
, msp
);
1638 /* Try to create a covering process */
1639 mutex_enter(&spa
->spa_proc_lock
);
1640 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1641 ASSERT(spa
->spa_proc
== &p0
);
1644 #ifdef HAVE_SPA_THREAD
1645 /* Only create a process if we're going to be around a while. */
1646 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1647 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1649 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1650 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1651 cv_wait(&spa
->spa_proc_cv
,
1652 &spa
->spa_proc_lock
);
1654 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1655 ASSERT(spa
->spa_proc
!= &p0
);
1656 ASSERT(spa
->spa_did
!= 0);
1660 "Couldn't create process for zfs pool \"%s\"\n",
1665 #endif /* HAVE_SPA_THREAD */
1666 mutex_exit(&spa
->spa_proc_lock
);
1668 /* If we didn't create a process, we need to create our taskqs. */
1669 if (spa
->spa_proc
== &p0
) {
1670 spa_create_zio_taskqs(spa
);
1673 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1674 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
,
1678 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1679 offsetof(vdev_t
, vdev_config_dirty_node
));
1680 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1681 offsetof(objset_t
, os_evicting_node
));
1682 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1683 offsetof(vdev_t
, vdev_state_dirty_node
));
1685 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1686 offsetof(struct vdev
, vdev_txg_node
));
1688 avl_create(&spa
->spa_errlist_scrub
,
1689 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1690 offsetof(spa_error_entry_t
, se_avl
));
1691 avl_create(&spa
->spa_errlist_last
,
1692 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1693 offsetof(spa_error_entry_t
, se_avl
));
1694 avl_create(&spa
->spa_errlist_healed
,
1695 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1696 offsetof(spa_error_entry_t
, se_avl
));
1698 spa_activate_os(spa
);
1700 spa_keystore_init(&spa
->spa_keystore
);
1703 * This taskq is used to perform zvol-minor-related tasks
1704 * asynchronously. This has several advantages, including easy
1705 * resolution of various deadlocks.
1707 * The taskq must be single threaded to ensure tasks are always
1708 * processed in the order in which they were dispatched.
1710 * A taskq per pool allows one to keep the pools independent.
1711 * This way if one pool is suspended, it will not impact another.
1713 * The preferred location to dispatch a zvol minor task is a sync
1714 * task. In this context, there is easy access to the spa_t and minimal
1715 * error handling is required because the sync task must succeed.
1717 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1721 * The taskq to preload metaslabs.
1723 spa
->spa_metaslab_taskq
= taskq_create("z_metaslab",
1724 metaslab_preload_pct
, maxclsyspri
, 1, INT_MAX
,
1725 TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1728 * Taskq dedicated to prefetcher threads: this is used to prevent the
1729 * pool traverse code from monopolizing the global (and limited)
1730 * system_taskq by inappropriately scheduling long running tasks on it.
1732 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", 100,
1733 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1736 * The taskq to upgrade datasets in this pool. Currently used by
1737 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1739 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", 100,
1740 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1744 * Opposite of spa_activate().
1747 spa_deactivate(spa_t
*spa
)
1749 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1750 ASSERT(spa
->spa_dsl_pool
== NULL
);
1751 ASSERT(spa
->spa_root_vdev
== NULL
);
1752 ASSERT(spa
->spa_async_zio_root
== NULL
);
1753 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1755 spa_evicting_os_wait(spa
);
1757 if (spa
->spa_zvol_taskq
) {
1758 taskq_destroy(spa
->spa_zvol_taskq
);
1759 spa
->spa_zvol_taskq
= NULL
;
1762 if (spa
->spa_metaslab_taskq
) {
1763 taskq_destroy(spa
->spa_metaslab_taskq
);
1764 spa
->spa_metaslab_taskq
= NULL
;
1767 if (spa
->spa_prefetch_taskq
) {
1768 taskq_destroy(spa
->spa_prefetch_taskq
);
1769 spa
->spa_prefetch_taskq
= NULL
;
1772 if (spa
->spa_upgrade_taskq
) {
1773 taskq_destroy(spa
->spa_upgrade_taskq
);
1774 spa
->spa_upgrade_taskq
= NULL
;
1777 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1779 list_destroy(&spa
->spa_config_dirty_list
);
1780 list_destroy(&spa
->spa_evicting_os_list
);
1781 list_destroy(&spa
->spa_state_dirty_list
);
1783 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1785 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1786 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1787 spa_taskqs_fini(spa
, t
, q
);
1791 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1792 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1793 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1794 spa
->spa_txg_zio
[i
] = NULL
;
1797 metaslab_class_destroy(spa
->spa_normal_class
);
1798 spa
->spa_normal_class
= NULL
;
1800 metaslab_class_destroy(spa
->spa_log_class
);
1801 spa
->spa_log_class
= NULL
;
1803 metaslab_class_destroy(spa
->spa_embedded_log_class
);
1804 spa
->spa_embedded_log_class
= NULL
;
1806 metaslab_class_destroy(spa
->spa_special_class
);
1807 spa
->spa_special_class
= NULL
;
1809 metaslab_class_destroy(spa
->spa_dedup_class
);
1810 spa
->spa_dedup_class
= NULL
;
1813 * If this was part of an import or the open otherwise failed, we may
1814 * still have errors left in the queues. Empty them just in case.
1816 spa_errlog_drain(spa
);
1817 avl_destroy(&spa
->spa_errlist_scrub
);
1818 avl_destroy(&spa
->spa_errlist_last
);
1819 avl_destroy(&spa
->spa_errlist_healed
);
1821 spa_keystore_fini(&spa
->spa_keystore
);
1823 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1825 mutex_enter(&spa
->spa_proc_lock
);
1826 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1827 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1828 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1829 cv_broadcast(&spa
->spa_proc_cv
);
1830 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1831 ASSERT(spa
->spa_proc
!= &p0
);
1832 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1834 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1835 spa
->spa_proc_state
= SPA_PROC_NONE
;
1837 ASSERT(spa
->spa_proc
== &p0
);
1838 mutex_exit(&spa
->spa_proc_lock
);
1841 * We want to make sure spa_thread() has actually exited the ZFS
1842 * module, so that the module can't be unloaded out from underneath
1845 if (spa
->spa_did
!= 0) {
1846 thread_join(spa
->spa_did
);
1850 spa_deactivate_os(spa
);
1855 * Verify a pool configuration, and construct the vdev tree appropriately. This
1856 * will create all the necessary vdevs in the appropriate layout, with each vdev
1857 * in the CLOSED state. This will prep the pool before open/creation/import.
1858 * All vdev validation is done by the vdev_alloc() routine.
1861 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1862 uint_t id
, int atype
)
1868 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1871 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1874 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1877 if (error
== ENOENT
)
1883 return (SET_ERROR(EINVAL
));
1886 for (int c
= 0; c
< children
; c
++) {
1888 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1896 ASSERT(*vdp
!= NULL
);
1902 spa_should_flush_logs_on_unload(spa_t
*spa
)
1904 if (!spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
))
1907 if (!spa_writeable(spa
))
1910 if (!spa
->spa_sync_on
)
1913 if (spa_state(spa
) != POOL_STATE_EXPORTED
)
1916 if (zfs_keep_log_spacemaps_at_export
)
1923 * Opens a transaction that will set the flag that will instruct
1924 * spa_sync to attempt to flush all the metaslabs for that txg.
1927 spa_unload_log_sm_flush_all(spa_t
*spa
)
1929 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
1930 VERIFY0(dmu_tx_assign(tx
, TXG_WAIT
));
1932 ASSERT3U(spa
->spa_log_flushall_txg
, ==, 0);
1933 spa
->spa_log_flushall_txg
= dmu_tx_get_txg(tx
);
1936 txg_wait_synced(spa_get_dsl(spa
), spa
->spa_log_flushall_txg
);
1940 spa_unload_log_sm_metadata(spa_t
*spa
)
1942 void *cookie
= NULL
;
1944 log_summary_entry_t
*e
;
1946 while ((sls
= avl_destroy_nodes(&spa
->spa_sm_logs_by_txg
,
1947 &cookie
)) != NULL
) {
1948 VERIFY0(sls
->sls_mscount
);
1949 kmem_free(sls
, sizeof (spa_log_sm_t
));
1952 while ((e
= list_remove_head(&spa
->spa_log_summary
)) != NULL
) {
1953 VERIFY0(e
->lse_mscount
);
1954 kmem_free(e
, sizeof (log_summary_entry_t
));
1957 spa
->spa_unflushed_stats
.sus_nblocks
= 0;
1958 spa
->spa_unflushed_stats
.sus_memused
= 0;
1959 spa
->spa_unflushed_stats
.sus_blocklimit
= 0;
1963 spa_destroy_aux_threads(spa_t
*spa
)
1965 if (spa
->spa_condense_zthr
!= NULL
) {
1966 zthr_destroy(spa
->spa_condense_zthr
);
1967 spa
->spa_condense_zthr
= NULL
;
1969 if (spa
->spa_checkpoint_discard_zthr
!= NULL
) {
1970 zthr_destroy(spa
->spa_checkpoint_discard_zthr
);
1971 spa
->spa_checkpoint_discard_zthr
= NULL
;
1973 if (spa
->spa_livelist_delete_zthr
!= NULL
) {
1974 zthr_destroy(spa
->spa_livelist_delete_zthr
);
1975 spa
->spa_livelist_delete_zthr
= NULL
;
1977 if (spa
->spa_livelist_condense_zthr
!= NULL
) {
1978 zthr_destroy(spa
->spa_livelist_condense_zthr
);
1979 spa
->spa_livelist_condense_zthr
= NULL
;
1981 if (spa
->spa_raidz_expand_zthr
!= NULL
) {
1982 zthr_destroy(spa
->spa_raidz_expand_zthr
);
1983 spa
->spa_raidz_expand_zthr
= NULL
;
1988 * Opposite of spa_load().
1991 spa_unload(spa_t
*spa
)
1993 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1994 ASSERT(spa_state(spa
) != POOL_STATE_UNINITIALIZED
);
1996 spa_import_progress_remove(spa_guid(spa
));
1997 spa_load_note(spa
, "UNLOADING");
1999 spa_wake_waiters(spa
);
2002 * If we have set the spa_final_txg, we have already performed the
2003 * tasks below in spa_export_common(). We should not redo it here since
2004 * we delay the final TXGs beyond what spa_final_txg is set at.
2006 if (spa
->spa_final_txg
== UINT64_MAX
) {
2008 * If the log space map feature is enabled and the pool is
2009 * getting exported (but not destroyed), we want to spend some
2010 * time flushing as many metaslabs as we can in an attempt to
2011 * destroy log space maps and save import time.
2013 if (spa_should_flush_logs_on_unload(spa
))
2014 spa_unload_log_sm_flush_all(spa
);
2019 spa_async_suspend(spa
);
2021 if (spa
->spa_root_vdev
) {
2022 vdev_t
*root_vdev
= spa
->spa_root_vdev
;
2023 vdev_initialize_stop_all(root_vdev
,
2024 VDEV_INITIALIZE_ACTIVE
);
2025 vdev_trim_stop_all(root_vdev
, VDEV_TRIM_ACTIVE
);
2026 vdev_autotrim_stop_all(spa
);
2027 vdev_rebuild_stop_all(spa
);
2034 if (spa
->spa_sync_on
) {
2035 txg_sync_stop(spa
->spa_dsl_pool
);
2036 spa
->spa_sync_on
= B_FALSE
;
2040 * This ensures that there is no async metaslab prefetching
2041 * while we attempt to unload the spa.
2043 taskq_wait(spa
->spa_metaslab_taskq
);
2045 if (spa
->spa_mmp
.mmp_thread
)
2046 mmp_thread_stop(spa
);
2049 * Wait for any outstanding async I/O to complete.
2051 if (spa
->spa_async_zio_root
!= NULL
) {
2052 for (int i
= 0; i
< max_ncpus
; i
++)
2053 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
2054 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
2055 spa
->spa_async_zio_root
= NULL
;
2058 if (spa
->spa_vdev_removal
!= NULL
) {
2059 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
2060 spa
->spa_vdev_removal
= NULL
;
2063 spa_destroy_aux_threads(spa
);
2065 spa_condense_fini(spa
);
2067 bpobj_close(&spa
->spa_deferred_bpobj
);
2069 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
2074 if (spa
->spa_root_vdev
)
2075 vdev_free(spa
->spa_root_vdev
);
2076 ASSERT(spa
->spa_root_vdev
== NULL
);
2079 * Close the dsl pool.
2081 if (spa
->spa_dsl_pool
) {
2082 dsl_pool_close(spa
->spa_dsl_pool
);
2083 spa
->spa_dsl_pool
= NULL
;
2084 spa
->spa_meta_objset
= NULL
;
2089 spa_unload_log_sm_metadata(spa
);
2092 * Drop and purge level 2 cache
2094 spa_l2cache_drop(spa
);
2096 if (spa
->spa_spares
.sav_vdevs
) {
2097 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
2098 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
2099 kmem_free(spa
->spa_spares
.sav_vdevs
,
2100 spa
->spa_spares
.sav_count
* sizeof (void *));
2101 spa
->spa_spares
.sav_vdevs
= NULL
;
2103 if (spa
->spa_spares
.sav_config
) {
2104 nvlist_free(spa
->spa_spares
.sav_config
);
2105 spa
->spa_spares
.sav_config
= NULL
;
2107 spa
->spa_spares
.sav_count
= 0;
2109 if (spa
->spa_l2cache
.sav_vdevs
) {
2110 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
2111 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
2112 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
2114 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
2115 spa
->spa_l2cache
.sav_count
* sizeof (void *));
2116 spa
->spa_l2cache
.sav_vdevs
= NULL
;
2118 if (spa
->spa_l2cache
.sav_config
) {
2119 nvlist_free(spa
->spa_l2cache
.sav_config
);
2120 spa
->spa_l2cache
.sav_config
= NULL
;
2122 spa
->spa_l2cache
.sav_count
= 0;
2124 spa
->spa_async_suspended
= 0;
2126 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
2128 if (spa
->spa_comment
!= NULL
) {
2129 spa_strfree(spa
->spa_comment
);
2130 spa
->spa_comment
= NULL
;
2132 if (spa
->spa_compatibility
!= NULL
) {
2133 spa_strfree(spa
->spa_compatibility
);
2134 spa
->spa_compatibility
= NULL
;
2137 spa
->spa_raidz_expand
= NULL
;
2139 spa_config_exit(spa
, SCL_ALL
, spa
);
2143 * Load (or re-load) the current list of vdevs describing the active spares for
2144 * this pool. When this is called, we have some form of basic information in
2145 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
2146 * then re-generate a more complete list including status information.
2149 spa_load_spares(spa_t
*spa
)
2158 * zdb opens both the current state of the pool and the
2159 * checkpointed state (if present), with a different spa_t.
2161 * As spare vdevs are shared among open pools, we skip loading
2162 * them when we load the checkpointed state of the pool.
2164 if (!spa_writeable(spa
))
2168 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
2171 * First, close and free any existing spare vdevs.
2173 if (spa
->spa_spares
.sav_vdevs
) {
2174 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
2175 vd
= spa
->spa_spares
.sav_vdevs
[i
];
2177 /* Undo the call to spa_activate() below */
2178 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
2179 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
2180 spa_spare_remove(tvd
);
2185 kmem_free(spa
->spa_spares
.sav_vdevs
,
2186 spa
->spa_spares
.sav_count
* sizeof (void *));
2189 if (spa
->spa_spares
.sav_config
== NULL
)
2192 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
2193 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
2195 spa
->spa_spares
.sav_count
= (int)nspares
;
2196 spa
->spa_spares
.sav_vdevs
= NULL
;
2202 * Construct the array of vdevs, opening them to get status in the
2203 * process. For each spare, there is potentially two different vdev_t
2204 * structures associated with it: one in the list of spares (used only
2205 * for basic validation purposes) and one in the active vdev
2206 * configuration (if it's spared in). During this phase we open and
2207 * validate each vdev on the spare list. If the vdev also exists in the
2208 * active configuration, then we also mark this vdev as an active spare.
2210 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
2212 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
2213 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
2214 VDEV_ALLOC_SPARE
) == 0);
2217 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
2219 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
2220 B_FALSE
)) != NULL
) {
2221 if (!tvd
->vdev_isspare
)
2225 * We only mark the spare active if we were successfully
2226 * able to load the vdev. Otherwise, importing a pool
2227 * with a bad active spare would result in strange
2228 * behavior, because multiple pool would think the spare
2229 * is actively in use.
2231 * There is a vulnerability here to an equally bizarre
2232 * circumstance, where a dead active spare is later
2233 * brought back to life (onlined or otherwise). Given
2234 * the rarity of this scenario, and the extra complexity
2235 * it adds, we ignore the possibility.
2237 if (!vdev_is_dead(tvd
))
2238 spa_spare_activate(tvd
);
2242 vd
->vdev_aux
= &spa
->spa_spares
;
2244 if (vdev_open(vd
) != 0)
2247 if (vdev_validate_aux(vd
) == 0)
2252 * Recompute the stashed list of spares, with status information
2255 fnvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
);
2257 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
2259 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
2260 spares
[i
] = vdev_config_generate(spa
,
2261 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
2262 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
2263 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
2264 spa
->spa_spares
.sav_count
);
2265 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
2266 nvlist_free(spares
[i
]);
2267 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
2271 * Load (or re-load) the current list of vdevs describing the active l2cache for
2272 * this pool. When this is called, we have some form of basic information in
2273 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
2274 * then re-generate a more complete list including status information.
2275 * Devices which are already active have their details maintained, and are
2279 spa_load_l2cache(spa_t
*spa
)
2281 nvlist_t
**l2cache
= NULL
;
2283 int i
, j
, oldnvdevs
;
2285 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
2286 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
2290 * zdb opens both the current state of the pool and the
2291 * checkpointed state (if present), with a different spa_t.
2293 * As L2 caches are part of the ARC which is shared among open
2294 * pools, we skip loading them when we load the checkpointed
2295 * state of the pool.
2297 if (!spa_writeable(spa
))
2301 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
2303 oldvdevs
= sav
->sav_vdevs
;
2304 oldnvdevs
= sav
->sav_count
;
2305 sav
->sav_vdevs
= NULL
;
2308 if (sav
->sav_config
== NULL
) {
2314 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
,
2315 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
2316 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
2319 * Process new nvlist of vdevs.
2321 for (i
= 0; i
< nl2cache
; i
++) {
2322 guid
= fnvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
);
2325 for (j
= 0; j
< oldnvdevs
; j
++) {
2327 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
2329 * Retain previous vdev for add/remove ops.
2337 if (newvdevs
[i
] == NULL
) {
2341 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
2342 VDEV_ALLOC_L2CACHE
) == 0);
2347 * Commit this vdev as an l2cache device,
2348 * even if it fails to open.
2350 spa_l2cache_add(vd
);
2355 spa_l2cache_activate(vd
);
2357 if (vdev_open(vd
) != 0)
2360 (void) vdev_validate_aux(vd
);
2362 if (!vdev_is_dead(vd
))
2363 l2arc_add_vdev(spa
, vd
);
2366 * Upon cache device addition to a pool or pool
2367 * creation with a cache device or if the header
2368 * of the device is invalid we issue an async
2369 * TRIM command for the whole device which will
2370 * execute if l2arc_trim_ahead > 0.
2372 spa_async_request(spa
, SPA_ASYNC_L2CACHE_TRIM
);
2376 sav
->sav_vdevs
= newvdevs
;
2377 sav
->sav_count
= (int)nl2cache
;
2380 * Recompute the stashed list of l2cache devices, with status
2381 * information this time.
2383 fnvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
);
2385 if (sav
->sav_count
> 0)
2386 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
2388 for (i
= 0; i
< sav
->sav_count
; i
++)
2389 l2cache
[i
] = vdev_config_generate(spa
,
2390 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
2391 fnvlist_add_nvlist_array(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
2392 (const nvlist_t
* const *)l2cache
, sav
->sav_count
);
2396 * Purge vdevs that were dropped
2399 for (i
= 0; i
< oldnvdevs
; i
++) {
2404 ASSERT(vd
->vdev_isl2cache
);
2406 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
2407 pool
!= 0ULL && l2arc_vdev_present(vd
))
2408 l2arc_remove_vdev(vd
);
2409 vdev_clear_stats(vd
);
2414 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
2417 for (i
= 0; i
< sav
->sav_count
; i
++)
2418 nvlist_free(l2cache
[i
]);
2420 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
2424 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
2427 char *packed
= NULL
;
2432 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
2436 nvsize
= *(uint64_t *)db
->db_data
;
2437 dmu_buf_rele(db
, FTAG
);
2439 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
2440 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
2443 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
2444 vmem_free(packed
, nvsize
);
2450 * Concrete top-level vdevs that are not missing and are not logs. At every
2451 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
2454 spa_healthy_core_tvds(spa_t
*spa
)
2456 vdev_t
*rvd
= spa
->spa_root_vdev
;
2459 for (uint64_t i
= 0; i
< rvd
->vdev_children
; i
++) {
2460 vdev_t
*vd
= rvd
->vdev_child
[i
];
2463 if (vdev_is_concrete(vd
) && !vdev_is_dead(vd
))
2471 * Checks to see if the given vdev could not be opened, in which case we post a
2472 * sysevent to notify the autoreplace code that the device has been removed.
2475 spa_check_removed(vdev_t
*vd
)
2477 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
2478 spa_check_removed(vd
->vdev_child
[c
]);
2480 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
2481 vdev_is_concrete(vd
)) {
2482 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
2483 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
2488 spa_check_for_missing_logs(spa_t
*spa
)
2490 vdev_t
*rvd
= spa
->spa_root_vdev
;
2493 * If we're doing a normal import, then build up any additional
2494 * diagnostic information about missing log devices.
2495 * We'll pass this up to the user for further processing.
2497 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
2498 nvlist_t
**child
, *nv
;
2501 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
2503 nv
= fnvlist_alloc();
2505 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2506 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2509 * We consider a device as missing only if it failed
2510 * to open (i.e. offline or faulted is not considered
2513 if (tvd
->vdev_islog
&&
2514 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2515 child
[idx
++] = vdev_config_generate(spa
, tvd
,
2516 B_FALSE
, VDEV_CONFIG_MISSING
);
2521 fnvlist_add_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2522 (const nvlist_t
* const *)child
, idx
);
2523 fnvlist_add_nvlist(spa
->spa_load_info
,
2524 ZPOOL_CONFIG_MISSING_DEVICES
, nv
);
2526 for (uint64_t i
= 0; i
< idx
; i
++)
2527 nvlist_free(child
[i
]);
2530 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
2533 spa_load_failed(spa
, "some log devices are missing");
2534 vdev_dbgmsg_print_tree(rvd
, 2);
2535 return (SET_ERROR(ENXIO
));
2538 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2539 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2541 if (tvd
->vdev_islog
&&
2542 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2543 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2544 spa_load_note(spa
, "some log devices are "
2545 "missing, ZIL is dropped.");
2546 vdev_dbgmsg_print_tree(rvd
, 2);
2556 * Check for missing log devices
2559 spa_check_logs(spa_t
*spa
)
2561 boolean_t rv
= B_FALSE
;
2562 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2564 switch (spa
->spa_log_state
) {
2567 case SPA_LOG_MISSING
:
2568 /* need to recheck in case slog has been restored */
2569 case SPA_LOG_UNKNOWN
:
2570 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2571 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
2573 spa_set_log_state(spa
, SPA_LOG_MISSING
);
2580 * Passivate any log vdevs (note, does not apply to embedded log metaslabs).
2583 spa_passivate_log(spa_t
*spa
)
2585 vdev_t
*rvd
= spa
->spa_root_vdev
;
2586 boolean_t slog_found
= B_FALSE
;
2588 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2590 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2591 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2593 if (tvd
->vdev_islog
) {
2594 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2595 metaslab_group_passivate(tvd
->vdev_mg
);
2596 slog_found
= B_TRUE
;
2600 return (slog_found
);
2604 * Activate any log vdevs (note, does not apply to embedded log metaslabs).
2607 spa_activate_log(spa_t
*spa
)
2609 vdev_t
*rvd
= spa
->spa_root_vdev
;
2611 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2613 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2614 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2616 if (tvd
->vdev_islog
) {
2617 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2618 metaslab_group_activate(tvd
->vdev_mg
);
2624 spa_reset_logs(spa_t
*spa
)
2628 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
2629 NULL
, DS_FIND_CHILDREN
);
2632 * We successfully offlined the log device, sync out the
2633 * current txg so that the "stubby" block can be removed
2636 txg_wait_synced(spa
->spa_dsl_pool
, 0);
2642 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
2644 for (int i
= 0; i
< sav
->sav_count
; i
++)
2645 spa_check_removed(sav
->sav_vdevs
[i
]);
2649 spa_claim_notify(zio_t
*zio
)
2651 spa_t
*spa
= zio
->io_spa
;
2656 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2657 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
2658 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
2659 mutex_exit(&spa
->spa_props_lock
);
2662 typedef struct spa_load_error
{
2663 boolean_t sle_verify_data
;
2664 uint64_t sle_meta_count
;
2665 uint64_t sle_data_count
;
2669 spa_load_verify_done(zio_t
*zio
)
2671 blkptr_t
*bp
= zio
->io_bp
;
2672 spa_load_error_t
*sle
= zio
->io_private
;
2673 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2674 int error
= zio
->io_error
;
2675 spa_t
*spa
= zio
->io_spa
;
2677 abd_free(zio
->io_abd
);
2679 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2680 type
!= DMU_OT_INTENT_LOG
)
2681 atomic_inc_64(&sle
->sle_meta_count
);
2683 atomic_inc_64(&sle
->sle_data_count
);
2686 mutex_enter(&spa
->spa_scrub_lock
);
2687 spa
->spa_load_verify_bytes
-= BP_GET_PSIZE(bp
);
2688 cv_broadcast(&spa
->spa_scrub_io_cv
);
2689 mutex_exit(&spa
->spa_scrub_lock
);
2693 * Maximum number of inflight bytes is the log2 fraction of the arc size.
2694 * By default, we set it to 1/16th of the arc.
2696 static uint_t spa_load_verify_shift
= 4;
2697 static int spa_load_verify_metadata
= B_TRUE
;
2698 static int spa_load_verify_data
= B_TRUE
;
2701 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2702 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2705 spa_load_error_t
*sle
= rio
->io_private
;
2707 (void) zilog
, (void) dnp
;
2710 * Note: normally this routine will not be called if
2711 * spa_load_verify_metadata is not set. However, it may be useful
2712 * to manually set the flag after the traversal has begun.
2714 if (!spa_load_verify_metadata
)
2718 * Sanity check the block pointer in order to detect obvious damage
2719 * before using the contents in subsequent checks or in zio_read().
2720 * When damaged consider it to be a metadata error since we cannot
2721 * trust the BP_GET_TYPE and BP_GET_LEVEL values.
2723 if (!zfs_blkptr_verify(spa
, bp
, BLK_CONFIG_NEEDED
, BLK_VERIFY_LOG
)) {
2724 atomic_inc_64(&sle
->sle_meta_count
);
2728 if (zb
->zb_level
== ZB_DNODE_LEVEL
|| BP_IS_HOLE(bp
) ||
2729 BP_IS_EMBEDDED(bp
) || BP_IS_REDACTED(bp
))
2732 if (!BP_IS_METADATA(bp
) &&
2733 (!spa_load_verify_data
|| !sle
->sle_verify_data
))
2736 uint64_t maxinflight_bytes
=
2737 arc_target_bytes() >> spa_load_verify_shift
;
2738 size_t size
= BP_GET_PSIZE(bp
);
2740 mutex_enter(&spa
->spa_scrub_lock
);
2741 while (spa
->spa_load_verify_bytes
>= maxinflight_bytes
)
2742 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2743 spa
->spa_load_verify_bytes
+= size
;
2744 mutex_exit(&spa
->spa_scrub_lock
);
2746 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2747 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2748 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2749 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2754 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2756 (void) dp
, (void) arg
;
2758 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2759 return (SET_ERROR(ENAMETOOLONG
));
2765 spa_load_verify(spa_t
*spa
)
2768 spa_load_error_t sle
= { 0 };
2769 zpool_load_policy_t policy
;
2770 boolean_t verify_ok
= B_FALSE
;
2773 zpool_get_load_policy(spa
->spa_config
, &policy
);
2775 if (policy
.zlp_rewind
& ZPOOL_NEVER_REWIND
||
2776 policy
.zlp_maxmeta
== UINT64_MAX
)
2779 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2780 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2781 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2783 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2788 * Verify data only if we are rewinding or error limit was set.
2789 * Otherwise nothing except dbgmsg care about it to waste time.
2791 sle
.sle_verify_data
= (policy
.zlp_rewind
& ZPOOL_REWIND_MASK
) ||
2792 (policy
.zlp_maxdata
< UINT64_MAX
);
2794 rio
= zio_root(spa
, NULL
, &sle
,
2795 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2797 if (spa_load_verify_metadata
) {
2798 if (spa
->spa_extreme_rewind
) {
2799 spa_load_note(spa
, "performing a complete scan of the "
2800 "pool since extreme rewind is on. This may take "
2801 "a very long time.\n (spa_load_verify_data=%u, "
2802 "spa_load_verify_metadata=%u)",
2803 spa_load_verify_data
, spa_load_verify_metadata
);
2806 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2807 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2808 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2811 (void) zio_wait(rio
);
2812 ASSERT0(spa
->spa_load_verify_bytes
);
2814 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2815 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2817 if (sle
.sle_meta_count
!= 0 || sle
.sle_data_count
!= 0) {
2818 spa_load_note(spa
, "spa_load_verify found %llu metadata errors "
2819 "and %llu data errors", (u_longlong_t
)sle
.sle_meta_count
,
2820 (u_longlong_t
)sle
.sle_data_count
);
2823 if (spa_load_verify_dryrun
||
2824 (!error
&& sle
.sle_meta_count
<= policy
.zlp_maxmeta
&&
2825 sle
.sle_data_count
<= policy
.zlp_maxdata
)) {
2829 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2830 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2832 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2833 fnvlist_add_uint64(spa
->spa_load_info
, ZPOOL_CONFIG_LOAD_TIME
,
2834 spa
->spa_load_txg_ts
);
2835 fnvlist_add_int64(spa
->spa_load_info
, ZPOOL_CONFIG_REWIND_TIME
,
2837 fnvlist_add_uint64(spa
->spa_load_info
,
2838 ZPOOL_CONFIG_LOAD_META_ERRORS
, sle
.sle_meta_count
);
2839 fnvlist_add_uint64(spa
->spa_load_info
,
2840 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
);
2842 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2845 if (spa_load_verify_dryrun
)
2849 if (error
!= ENXIO
&& error
!= EIO
)
2850 error
= SET_ERROR(EIO
);
2854 return (verify_ok
? 0 : EIO
);
2858 * Find a value in the pool props object.
2861 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2863 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2864 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2868 * Find a value in the pool directory object.
2871 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
, boolean_t log_enoent
)
2873 int error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2874 name
, sizeof (uint64_t), 1, val
);
2876 if (error
!= 0 && (error
!= ENOENT
|| log_enoent
)) {
2877 spa_load_failed(spa
, "couldn't get '%s' value in MOS directory "
2878 "[error=%d]", name
, error
);
2885 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2887 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2888 return (SET_ERROR(err
));
2892 spa_livelist_delete_check(spa_t
*spa
)
2894 return (spa
->spa_livelists_to_delete
!= 0);
2898 spa_livelist_delete_cb_check(void *arg
, zthr_t
*z
)
2902 return (spa_livelist_delete_check(spa
));
2906 delete_blkptr_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
2909 zio_free(spa
, tx
->tx_txg
, bp
);
2910 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
2911 -bp_get_dsize_sync(spa
, bp
),
2912 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
2917 dsl_get_next_livelist_obj(objset_t
*os
, uint64_t zap_obj
, uint64_t *llp
)
2922 zap_cursor_init(&zc
, os
, zap_obj
);
2923 err
= zap_cursor_retrieve(&zc
, &za
);
2924 zap_cursor_fini(&zc
);
2926 *llp
= za
.za_first_integer
;
2931 * Components of livelist deletion that must be performed in syncing
2932 * context: freeing block pointers and updating the pool-wide data
2933 * structures to indicate how much work is left to do
2935 typedef struct sublist_delete_arg
{
2940 } sublist_delete_arg_t
;
2943 sublist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2945 sublist_delete_arg_t
*sda
= arg
;
2946 spa_t
*spa
= sda
->spa
;
2947 dsl_deadlist_t
*ll
= sda
->ll
;
2948 uint64_t key
= sda
->key
;
2949 bplist_t
*to_free
= sda
->to_free
;
2951 bplist_iterate(to_free
, delete_blkptr_cb
, spa
, tx
);
2952 dsl_deadlist_remove_entry(ll
, key
, tx
);
2955 typedef struct livelist_delete_arg
{
2959 } livelist_delete_arg_t
;
2962 livelist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2964 livelist_delete_arg_t
*lda
= arg
;
2965 spa_t
*spa
= lda
->spa
;
2966 uint64_t ll_obj
= lda
->ll_obj
;
2967 uint64_t zap_obj
= lda
->zap_obj
;
2968 objset_t
*mos
= spa
->spa_meta_objset
;
2971 /* free the livelist and decrement the feature count */
2972 VERIFY0(zap_remove_int(mos
, zap_obj
, ll_obj
, tx
));
2973 dsl_deadlist_free(mos
, ll_obj
, tx
);
2974 spa_feature_decr(spa
, SPA_FEATURE_LIVELIST
, tx
);
2975 VERIFY0(zap_count(mos
, zap_obj
, &count
));
2977 /* no more livelists to delete */
2978 VERIFY0(zap_remove(mos
, DMU_POOL_DIRECTORY_OBJECT
,
2979 DMU_POOL_DELETED_CLONES
, tx
));
2980 VERIFY0(zap_destroy(mos
, zap_obj
, tx
));
2981 spa
->spa_livelists_to_delete
= 0;
2982 spa_notify_waiters(spa
);
2987 * Load in the value for the livelist to be removed and open it. Then,
2988 * load its first sublist and determine which block pointers should actually
2989 * be freed. Then, call a synctask which performs the actual frees and updates
2990 * the pool-wide livelist data.
2993 spa_livelist_delete_cb(void *arg
, zthr_t
*z
)
2996 uint64_t ll_obj
= 0, count
;
2997 objset_t
*mos
= spa
->spa_meta_objset
;
2998 uint64_t zap_obj
= spa
->spa_livelists_to_delete
;
3000 * Determine the next livelist to delete. This function should only
3001 * be called if there is at least one deleted clone.
3003 VERIFY0(dsl_get_next_livelist_obj(mos
, zap_obj
, &ll_obj
));
3004 VERIFY0(zap_count(mos
, ll_obj
, &count
));
3007 dsl_deadlist_entry_t
*dle
;
3009 ll
= kmem_zalloc(sizeof (dsl_deadlist_t
), KM_SLEEP
);
3010 dsl_deadlist_open(ll
, mos
, ll_obj
);
3011 dle
= dsl_deadlist_first(ll
);
3012 ASSERT3P(dle
, !=, NULL
);
3013 bplist_create(&to_free
);
3014 int err
= dsl_process_sub_livelist(&dle
->dle_bpobj
, &to_free
,
3017 sublist_delete_arg_t sync_arg
= {
3020 .key
= dle
->dle_mintxg
,
3023 zfs_dbgmsg("deleting sublist (id %llu) from"
3024 " livelist %llu, %lld remaining",
3025 (u_longlong_t
)dle
->dle_bpobj
.bpo_object
,
3026 (u_longlong_t
)ll_obj
, (longlong_t
)count
- 1);
3027 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
3028 sublist_delete_sync
, &sync_arg
, 0,
3029 ZFS_SPACE_CHECK_DESTROY
));
3031 VERIFY3U(err
, ==, EINTR
);
3033 bplist_clear(&to_free
);
3034 bplist_destroy(&to_free
);
3035 dsl_deadlist_close(ll
);
3036 kmem_free(ll
, sizeof (dsl_deadlist_t
));
3038 livelist_delete_arg_t sync_arg
= {
3043 zfs_dbgmsg("deletion of livelist %llu completed",
3044 (u_longlong_t
)ll_obj
);
3045 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
, livelist_delete_sync
,
3046 &sync_arg
, 0, ZFS_SPACE_CHECK_DESTROY
));
3051 spa_start_livelist_destroy_thread(spa_t
*spa
)
3053 ASSERT3P(spa
->spa_livelist_delete_zthr
, ==, NULL
);
3054 spa
->spa_livelist_delete_zthr
=
3055 zthr_create("z_livelist_destroy",
3056 spa_livelist_delete_cb_check
, spa_livelist_delete_cb
, spa
,
3060 typedef struct livelist_new_arg
{
3063 } livelist_new_arg_t
;
3066 livelist_track_new_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
3070 livelist_new_arg_t
*lna
= arg
;
3072 bplist_append(lna
->frees
, bp
);
3074 bplist_append(lna
->allocs
, bp
);
3075 zfs_livelist_condense_new_alloc
++;
3080 typedef struct livelist_condense_arg
{
3083 uint64_t first_size
;
3085 } livelist_condense_arg_t
;
3088 spa_livelist_condense_sync(void *arg
, dmu_tx_t
*tx
)
3090 livelist_condense_arg_t
*lca
= arg
;
3091 spa_t
*spa
= lca
->spa
;
3093 dsl_dataset_t
*ds
= spa
->spa_to_condense
.ds
;
3095 /* Have we been cancelled? */
3096 if (spa
->spa_to_condense
.cancelled
) {
3097 zfs_livelist_condense_sync_cancel
++;
3101 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
3102 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
3103 dsl_deadlist_t
*ll
= &ds
->ds_dir
->dd_livelist
;
3106 * It's possible that the livelist was changed while the zthr was
3107 * running. Therefore, we need to check for new blkptrs in the two
3108 * entries being condensed and continue to track them in the livelist.
3109 * Because of the way we handle remapped blkptrs (see dbuf_remap_impl),
3110 * it's possible that the newly added blkptrs are FREEs or ALLOCs so
3111 * we need to sort them into two different bplists.
3113 uint64_t first_obj
= first
->dle_bpobj
.bpo_object
;
3114 uint64_t next_obj
= next
->dle_bpobj
.bpo_object
;
3115 uint64_t cur_first_size
= first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
3116 uint64_t cur_next_size
= next
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
3118 bplist_create(&new_frees
);
3119 livelist_new_arg_t new_bps
= {
3120 .allocs
= &lca
->to_keep
,
3121 .frees
= &new_frees
,
3124 if (cur_first_size
> lca
->first_size
) {
3125 VERIFY0(livelist_bpobj_iterate_from_nofree(&first
->dle_bpobj
,
3126 livelist_track_new_cb
, &new_bps
, lca
->first_size
));
3128 if (cur_next_size
> lca
->next_size
) {
3129 VERIFY0(livelist_bpobj_iterate_from_nofree(&next
->dle_bpobj
,
3130 livelist_track_new_cb
, &new_bps
, lca
->next_size
));
3133 dsl_deadlist_clear_entry(first
, ll
, tx
);
3134 ASSERT(bpobj_is_empty(&first
->dle_bpobj
));
3135 dsl_deadlist_remove_entry(ll
, next
->dle_mintxg
, tx
);
3137 bplist_iterate(&lca
->to_keep
, dsl_deadlist_insert_alloc_cb
, ll
, tx
);
3138 bplist_iterate(&new_frees
, dsl_deadlist_insert_free_cb
, ll
, tx
);
3139 bplist_destroy(&new_frees
);
3141 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
3142 dsl_dataset_name(ds
, dsname
);
3143 zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu "
3144 "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu "
3145 "(%llu blkptrs)", (u_longlong_t
)tx
->tx_txg
, dsname
,
3146 (u_longlong_t
)ds
->ds_object
, (u_longlong_t
)first_obj
,
3147 (u_longlong_t
)cur_first_size
, (u_longlong_t
)next_obj
,
3148 (u_longlong_t
)cur_next_size
,
3149 (u_longlong_t
)first
->dle_bpobj
.bpo_object
,
3150 (u_longlong_t
)first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
);
3152 dmu_buf_rele(ds
->ds_dbuf
, spa
);
3153 spa
->spa_to_condense
.ds
= NULL
;
3154 bplist_clear(&lca
->to_keep
);
3155 bplist_destroy(&lca
->to_keep
);
3156 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
3157 spa
->spa_to_condense
.syncing
= B_FALSE
;
3161 spa_livelist_condense_cb(void *arg
, zthr_t
*t
)
3163 while (zfs_livelist_condense_zthr_pause
&&
3164 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
3168 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
3169 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
3170 uint64_t first_size
, next_size
;
3172 livelist_condense_arg_t
*lca
=
3173 kmem_alloc(sizeof (livelist_condense_arg_t
), KM_SLEEP
);
3174 bplist_create(&lca
->to_keep
);
3177 * Process the livelists (matching FREEs and ALLOCs) in open context
3178 * so we have minimal work in syncing context to condense.
3180 * We save bpobj sizes (first_size and next_size) to use later in
3181 * syncing context to determine if entries were added to these sublists
3182 * while in open context. This is possible because the clone is still
3183 * active and open for normal writes and we want to make sure the new,
3184 * unprocessed blockpointers are inserted into the livelist normally.
3186 * Note that dsl_process_sub_livelist() both stores the size number of
3187 * blockpointers and iterates over them while the bpobj's lock held, so
3188 * the sizes returned to us are consistent which what was actually
3191 int err
= dsl_process_sub_livelist(&first
->dle_bpobj
, &lca
->to_keep
, t
,
3194 err
= dsl_process_sub_livelist(&next
->dle_bpobj
, &lca
->to_keep
,
3198 while (zfs_livelist_condense_sync_pause
&&
3199 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
3202 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
3203 dmu_tx_mark_netfree(tx
);
3204 dmu_tx_hold_space(tx
, 1);
3205 err
= dmu_tx_assign(tx
, TXG_NOWAIT
| TXG_NOTHROTTLE
);
3208 * Prevent the condense zthr restarting before
3209 * the synctask completes.
3211 spa
->spa_to_condense
.syncing
= B_TRUE
;
3213 lca
->first_size
= first_size
;
3214 lca
->next_size
= next_size
;
3215 dsl_sync_task_nowait(spa_get_dsl(spa
),
3216 spa_livelist_condense_sync
, lca
, tx
);
3222 * Condensing can not continue: either it was externally stopped or
3223 * we were unable to assign to a tx because the pool has run out of
3224 * space. In the second case, we'll just end up trying to condense
3225 * again in a later txg.
3228 bplist_clear(&lca
->to_keep
);
3229 bplist_destroy(&lca
->to_keep
);
3230 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
3231 dmu_buf_rele(spa
->spa_to_condense
.ds
->ds_dbuf
, spa
);
3232 spa
->spa_to_condense
.ds
= NULL
;
3234 zfs_livelist_condense_zthr_cancel
++;
3238 * Check that there is something to condense but that a condense is not
3239 * already in progress and that condensing has not been cancelled.
3242 spa_livelist_condense_cb_check(void *arg
, zthr_t
*z
)
3246 if ((spa
->spa_to_condense
.ds
!= NULL
) &&
3247 (spa
->spa_to_condense
.syncing
== B_FALSE
) &&
3248 (spa
->spa_to_condense
.cancelled
== B_FALSE
)) {
3255 spa_start_livelist_condensing_thread(spa_t
*spa
)
3257 spa
->spa_to_condense
.ds
= NULL
;
3258 spa
->spa_to_condense
.first
= NULL
;
3259 spa
->spa_to_condense
.next
= NULL
;
3260 spa
->spa_to_condense
.syncing
= B_FALSE
;
3261 spa
->spa_to_condense
.cancelled
= B_FALSE
;
3263 ASSERT3P(spa
->spa_livelist_condense_zthr
, ==, NULL
);
3264 spa
->spa_livelist_condense_zthr
=
3265 zthr_create("z_livelist_condense",
3266 spa_livelist_condense_cb_check
,
3267 spa_livelist_condense_cb
, spa
, minclsyspri
);
3271 spa_spawn_aux_threads(spa_t
*spa
)
3273 ASSERT(spa_writeable(spa
));
3275 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
3277 spa_start_raidz_expansion_thread(spa
);
3278 spa_start_indirect_condensing_thread(spa
);
3279 spa_start_livelist_destroy_thread(spa
);
3280 spa_start_livelist_condensing_thread(spa
);
3282 ASSERT3P(spa
->spa_checkpoint_discard_zthr
, ==, NULL
);
3283 spa
->spa_checkpoint_discard_zthr
=
3284 zthr_create("z_checkpoint_discard",
3285 spa_checkpoint_discard_thread_check
,
3286 spa_checkpoint_discard_thread
, spa
, minclsyspri
);
3290 * Fix up config after a partly-completed split. This is done with the
3291 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
3292 * pool have that entry in their config, but only the splitting one contains
3293 * a list of all the guids of the vdevs that are being split off.
3295 * This function determines what to do with that list: either rejoin
3296 * all the disks to the pool, or complete the splitting process. To attempt
3297 * the rejoin, each disk that is offlined is marked online again, and
3298 * we do a reopen() call. If the vdev label for every disk that was
3299 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
3300 * then we call vdev_split() on each disk, and complete the split.
3302 * Otherwise we leave the config alone, with all the vdevs in place in
3303 * the original pool.
3306 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
3313 boolean_t attempt_reopen
;
3315 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
3318 /* check that the config is complete */
3319 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
3320 &glist
, &gcount
) != 0)
3323 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
3325 /* attempt to online all the vdevs & validate */
3326 attempt_reopen
= B_TRUE
;
3327 for (i
= 0; i
< gcount
; i
++) {
3328 if (glist
[i
] == 0) /* vdev is hole */
3331 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
3332 if (vd
[i
] == NULL
) {
3334 * Don't bother attempting to reopen the disks;
3335 * just do the split.
3337 attempt_reopen
= B_FALSE
;
3339 /* attempt to re-online it */
3340 vd
[i
]->vdev_offline
= B_FALSE
;
3344 if (attempt_reopen
) {
3345 vdev_reopen(spa
->spa_root_vdev
);
3347 /* check each device to see what state it's in */
3348 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
3349 if (vd
[i
] != NULL
&&
3350 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
3357 * If every disk has been moved to the new pool, or if we never
3358 * even attempted to look at them, then we split them off for
3361 if (!attempt_reopen
|| gcount
== extracted
) {
3362 for (i
= 0; i
< gcount
; i
++)
3365 vdev_reopen(spa
->spa_root_vdev
);
3368 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
3372 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
)
3374 const char *ereport
= FM_EREPORT_ZFS_POOL
;
3377 spa
->spa_load_state
= state
;
3378 (void) spa_import_progress_set_state(spa_guid(spa
),
3379 spa_load_state(spa
));
3380 spa_import_progress_set_notes(spa
, "spa_load()");
3382 gethrestime(&spa
->spa_loaded_ts
);
3383 error
= spa_load_impl(spa
, type
, &ereport
);
3386 * Don't count references from objsets that are already closed
3387 * and are making their way through the eviction process.
3389 spa_evicting_os_wait(spa
);
3390 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
3392 if (error
!= EEXIST
) {
3393 spa
->spa_loaded_ts
.tv_sec
= 0;
3394 spa
->spa_loaded_ts
.tv_nsec
= 0;
3396 if (error
!= EBADF
) {
3397 (void) zfs_ereport_post(ereport
, spa
,
3398 NULL
, NULL
, NULL
, 0);
3401 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
3404 (void) spa_import_progress_set_state(spa_guid(spa
),
3405 spa_load_state(spa
));
3412 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
3413 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
3414 * spa's per-vdev ZAP list.
3417 vdev_count_verify_zaps(vdev_t
*vd
)
3419 spa_t
*spa
= vd
->vdev_spa
;
3422 if (spa_feature_is_active(vd
->vdev_spa
, SPA_FEATURE_AVZ_V2
) &&
3423 vd
->vdev_root_zap
!= 0) {
3425 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3426 spa
->spa_all_vdev_zaps
, vd
->vdev_root_zap
));
3428 if (vd
->vdev_top_zap
!= 0) {
3430 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3431 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
3433 if (vd
->vdev_leaf_zap
!= 0) {
3435 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3436 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
3439 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
3440 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
3446 #define vdev_count_verify_zaps(vd) ((void) sizeof (vd), 0)
3450 * Determine whether the activity check is required.
3453 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
3457 uint64_t hostid
= 0;
3458 uint64_t tryconfig_txg
= 0;
3459 uint64_t tryconfig_timestamp
= 0;
3460 uint16_t tryconfig_mmp_seq
= 0;
3463 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3464 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3465 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
3467 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
3468 &tryconfig_timestamp
);
3469 (void) nvlist_lookup_uint16(nvinfo
, ZPOOL_CONFIG_MMP_SEQ
,
3470 &tryconfig_mmp_seq
);
3473 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
3476 * Disable the MMP activity check - This is used by zdb which
3477 * is intended to be used on potentially active pools.
3479 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
3483 * Skip the activity check when the MMP feature is disabled.
3485 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
3489 * If the tryconfig_ values are nonzero, they are the results of an
3490 * earlier tryimport. If they all match the uberblock we just found,
3491 * then the pool has not changed and we return false so we do not test
3494 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
3495 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
&&
3496 tryconfig_mmp_seq
&& tryconfig_mmp_seq
==
3497 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0))
3501 * Allow the activity check to be skipped when importing the pool
3502 * on the same host which last imported it. Since the hostid from
3503 * configuration may be stale use the one read from the label.
3505 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
3506 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
3508 if (hostid
== spa_get_hostid(spa
))
3512 * Skip the activity test when the pool was cleanly exported.
3514 if (state
!= POOL_STATE_ACTIVE
)
3521 * Nanoseconds the activity check must watch for changes on-disk.
3524 spa_activity_check_duration(spa_t
*spa
, uberblock_t
*ub
)
3526 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
3527 uint64_t multihost_interval
= MSEC2NSEC(
3528 MMP_INTERVAL_OK(zfs_multihost_interval
));
3529 uint64_t import_delay
= MAX(NANOSEC
, import_intervals
*
3530 multihost_interval
);
3533 * Local tunables determine a minimum duration except for the case
3534 * where we know when the remote host will suspend the pool if MMP
3535 * writes do not land.
3537 * See Big Theory comment at the top of mmp.c for the reasoning behind
3538 * these cases and times.
3541 ASSERT(MMP_IMPORT_SAFETY_FACTOR
>= 100);
3543 if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3544 MMP_FAIL_INT(ub
) > 0) {
3546 /* MMP on remote host will suspend pool after failed writes */
3547 import_delay
= MMP_FAIL_INT(ub
) * MSEC2NSEC(MMP_INTERVAL(ub
)) *
3548 MMP_IMPORT_SAFETY_FACTOR
/ 100;
3550 zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp "
3551 "mmp_fails=%llu ub_mmp mmp_interval=%llu "
3552 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3553 (u_longlong_t
)MMP_FAIL_INT(ub
),
3554 (u_longlong_t
)MMP_INTERVAL(ub
),
3555 (u_longlong_t
)import_intervals
);
3557 } else if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3558 MMP_FAIL_INT(ub
) == 0) {
3560 /* MMP on remote host will never suspend pool */
3561 import_delay
= MAX(import_delay
, (MSEC2NSEC(MMP_INTERVAL(ub
)) +
3562 ub
->ub_mmp_delay
) * import_intervals
);
3564 zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp "
3565 "mmp_interval=%llu ub_mmp_delay=%llu "
3566 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3567 (u_longlong_t
)MMP_INTERVAL(ub
),
3568 (u_longlong_t
)ub
->ub_mmp_delay
,
3569 (u_longlong_t
)import_intervals
);
3571 } else if (MMP_VALID(ub
)) {
3573 * zfs-0.7 compatibility case
3576 import_delay
= MAX(import_delay
, (multihost_interval
+
3577 ub
->ub_mmp_delay
) * import_intervals
);
3579 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu "
3580 "import_intervals=%llu leaves=%u",
3581 (u_longlong_t
)import_delay
,
3582 (u_longlong_t
)ub
->ub_mmp_delay
,
3583 (u_longlong_t
)import_intervals
,
3584 vdev_count_leaves(spa
));
3586 /* Using local tunings is the only reasonable option */
3587 zfs_dbgmsg("pool last imported on non-MMP aware "
3588 "host using import_delay=%llu multihost_interval=%llu "
3589 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3590 (u_longlong_t
)multihost_interval
,
3591 (u_longlong_t
)import_intervals
);
3594 return (import_delay
);
3598 * Perform the import activity check. If the user canceled the import or
3599 * we detected activity then fail.
3602 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
3604 uint64_t txg
= ub
->ub_txg
;
3605 uint64_t timestamp
= ub
->ub_timestamp
;
3606 uint64_t mmp_config
= ub
->ub_mmp_config
;
3607 uint16_t mmp_seq
= MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0;
3608 uint64_t import_delay
;
3609 hrtime_t import_expire
, now
;
3610 nvlist_t
*mmp_label
= NULL
;
3611 vdev_t
*rvd
= spa
->spa_root_vdev
;
3616 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
3617 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3621 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
3622 * during the earlier tryimport. If the txg recorded there is 0 then
3623 * the pool is known to be active on another host.
3625 * Otherwise, the pool might be in use on another host. Check for
3626 * changes in the uberblocks on disk if necessary.
3628 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3629 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
3630 ZPOOL_CONFIG_LOAD_INFO
);
3632 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
3633 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
3634 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3635 error
= SET_ERROR(EREMOTEIO
);
3640 import_delay
= spa_activity_check_duration(spa
, ub
);
3642 /* Add a small random factor in case of simultaneous imports (0-25%) */
3643 import_delay
+= import_delay
* random_in_range(250) / 1000;
3645 import_expire
= gethrtime() + import_delay
;
3647 spa_import_progress_set_notes(spa
, "Checking MMP activity, waiting "
3648 "%llu ms", (u_longlong_t
)NSEC2MSEC(import_delay
));
3650 int interations
= 0;
3651 while ((now
= gethrtime()) < import_expire
) {
3652 if (interations
++ % 30 == 0) {
3653 spa_import_progress_set_notes(spa
, "Checking MMP "
3654 "activity, %llu ms remaining",
3655 (u_longlong_t
)NSEC2MSEC(import_expire
- now
));
3658 (void) spa_import_progress_set_mmp_check(spa_guid(spa
),
3659 NSEC2SEC(import_expire
- gethrtime()));
3661 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3663 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
||
3664 mmp_seq
!= (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0)) {
3665 zfs_dbgmsg("multihost activity detected "
3666 "txg %llu ub_txg %llu "
3667 "timestamp %llu ub_timestamp %llu "
3668 "mmp_config %#llx ub_mmp_config %#llx",
3669 (u_longlong_t
)txg
, (u_longlong_t
)ub
->ub_txg
,
3670 (u_longlong_t
)timestamp
,
3671 (u_longlong_t
)ub
->ub_timestamp
,
3672 (u_longlong_t
)mmp_config
,
3673 (u_longlong_t
)ub
->ub_mmp_config
);
3675 error
= SET_ERROR(EREMOTEIO
);
3680 nvlist_free(mmp_label
);
3684 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
3686 error
= SET_ERROR(EINTR
);
3694 mutex_destroy(&mtx
);
3698 * If the pool is determined to be active store the status in the
3699 * spa->spa_load_info nvlist. If the remote hostname or hostid are
3700 * available from configuration read from disk store them as well.
3701 * This allows 'zpool import' to generate a more useful message.
3703 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
3704 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
3705 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
3707 if (error
== EREMOTEIO
) {
3708 const char *hostname
= "<unknown>";
3709 uint64_t hostid
= 0;
3712 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
3713 hostname
= fnvlist_lookup_string(mmp_label
,
3714 ZPOOL_CONFIG_HOSTNAME
);
3715 fnvlist_add_string(spa
->spa_load_info
,
3716 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
3719 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
3720 hostid
= fnvlist_lookup_uint64(mmp_label
,
3721 ZPOOL_CONFIG_HOSTID
);
3722 fnvlist_add_uint64(spa
->spa_load_info
,
3723 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
3727 fnvlist_add_uint64(spa
->spa_load_info
,
3728 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
3729 fnvlist_add_uint64(spa
->spa_load_info
,
3730 ZPOOL_CONFIG_MMP_TXG
, 0);
3732 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
3736 nvlist_free(mmp_label
);
3742 spa_verify_host(spa_t
*spa
, nvlist_t
*mos_config
)
3745 const char *hostname
;
3746 uint64_t myhostid
= 0;
3748 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
3749 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
3750 hostname
= fnvlist_lookup_string(mos_config
,
3751 ZPOOL_CONFIG_HOSTNAME
);
3753 myhostid
= zone_get_hostid(NULL
);
3755 if (hostid
!= 0 && myhostid
!= 0 && hostid
!= myhostid
) {
3756 cmn_err(CE_WARN
, "pool '%s' could not be "
3757 "loaded as it was last accessed by "
3758 "another system (host: %s hostid: 0x%llx). "
3759 "See: https://openzfs.github.io/openzfs-docs/msg/"
3761 spa_name(spa
), hostname
, (u_longlong_t
)hostid
);
3762 spa_load_failed(spa
, "hostid verification failed: pool "
3763 "last accessed by host: %s (hostid: 0x%llx)",
3764 hostname
, (u_longlong_t
)hostid
);
3765 return (SET_ERROR(EBADF
));
3773 spa_ld_parse_config(spa_t
*spa
, spa_import_type_t type
)
3776 nvlist_t
*nvtree
, *nvl
, *config
= spa
->spa_config
;
3780 const char *comment
;
3781 const char *compatibility
;
3784 * Versioning wasn't explicitly added to the label until later, so if
3785 * it's not present treat it as the initial version.
3787 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
3788 &spa
->spa_ubsync
.ub_version
) != 0)
3789 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
3791 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
)) {
3792 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3793 ZPOOL_CONFIG_POOL_GUID
);
3794 return (SET_ERROR(EINVAL
));
3798 * If we are doing an import, ensure that the pool is not already
3799 * imported by checking if its pool guid already exists in the
3802 * The only case that we allow an already imported pool to be
3803 * imported again, is when the pool is checkpointed and we want to
3804 * look at its checkpointed state from userland tools like zdb.
3807 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3808 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3809 spa_guid_exists(pool_guid
, 0)) {
3811 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3812 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3813 spa_guid_exists(pool_guid
, 0) &&
3814 !spa_importing_readonly_checkpoint(spa
)) {
3816 spa_load_failed(spa
, "a pool with guid %llu is already open",
3817 (u_longlong_t
)pool_guid
);
3818 return (SET_ERROR(EEXIST
));
3821 spa
->spa_config_guid
= pool_guid
;
3823 nvlist_free(spa
->spa_load_info
);
3824 spa
->spa_load_info
= fnvlist_alloc();
3826 ASSERT(spa
->spa_comment
== NULL
);
3827 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
3828 spa
->spa_comment
= spa_strdup(comment
);
3830 ASSERT(spa
->spa_compatibility
== NULL
);
3831 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMPATIBILITY
,
3832 &compatibility
) == 0)
3833 spa
->spa_compatibility
= spa_strdup(compatibility
);
3835 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
3836 &spa
->spa_config_txg
);
3838 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) == 0)
3839 spa
->spa_config_splitting
= fnvlist_dup(nvl
);
3841 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvtree
)) {
3842 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3843 ZPOOL_CONFIG_VDEV_TREE
);
3844 return (SET_ERROR(EINVAL
));
3848 * Create "The Godfather" zio to hold all async IOs
3850 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3852 for (int i
= 0; i
< max_ncpus
; i
++) {
3853 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3854 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3855 ZIO_FLAG_GODFATHER
);
3859 * Parse the configuration into a vdev tree. We explicitly set the
3860 * value that will be returned by spa_version() since parsing the
3861 * configuration requires knowing the version number.
3863 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3864 parse
= (type
== SPA_IMPORT_EXISTING
?
3865 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
3866 error
= spa_config_parse(spa
, &rvd
, nvtree
, NULL
, 0, parse
);
3867 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3870 spa_load_failed(spa
, "unable to parse config [error=%d]",
3875 ASSERT(spa
->spa_root_vdev
== rvd
);
3876 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
3877 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
3879 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3880 ASSERT(spa_guid(spa
) == pool_guid
);
3887 * Recursively open all vdevs in the vdev tree. This function is called twice:
3888 * first with the untrusted config, then with the trusted config.
3891 spa_ld_open_vdevs(spa_t
*spa
)
3896 * spa_missing_tvds_allowed defines how many top-level vdevs can be
3897 * missing/unopenable for the root vdev to be still considered openable.
3899 if (spa
->spa_trust_config
) {
3900 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds
;
3901 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_CACHEFILE
) {
3902 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_cachefile
;
3903 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_SCAN
) {
3904 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_scan
;
3906 spa
->spa_missing_tvds_allowed
= 0;
3909 spa
->spa_missing_tvds_allowed
=
3910 MAX(zfs_max_missing_tvds
, spa
->spa_missing_tvds_allowed
);
3912 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3913 error
= vdev_open(spa
->spa_root_vdev
);
3914 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3916 if (spa
->spa_missing_tvds
!= 0) {
3917 spa_load_note(spa
, "vdev tree has %lld missing top-level "
3918 "vdevs.", (u_longlong_t
)spa
->spa_missing_tvds
);
3919 if (spa
->spa_trust_config
&& (spa
->spa_mode
& SPA_MODE_WRITE
)) {
3921 * Although theoretically we could allow users to open
3922 * incomplete pools in RW mode, we'd need to add a lot
3923 * of extra logic (e.g. adjust pool space to account
3924 * for missing vdevs).
3925 * This limitation also prevents users from accidentally
3926 * opening the pool in RW mode during data recovery and
3927 * damaging it further.
3929 spa_load_note(spa
, "pools with missing top-level "
3930 "vdevs can only be opened in read-only mode.");
3931 error
= SET_ERROR(ENXIO
);
3933 spa_load_note(spa
, "current settings allow for maximum "
3934 "%lld missing top-level vdevs at this stage.",
3935 (u_longlong_t
)spa
->spa_missing_tvds_allowed
);
3939 spa_load_failed(spa
, "unable to open vdev tree [error=%d]",
3942 if (spa
->spa_missing_tvds
!= 0 || error
!= 0)
3943 vdev_dbgmsg_print_tree(spa
->spa_root_vdev
, 2);
3949 * We need to validate the vdev labels against the configuration that
3950 * we have in hand. This function is called twice: first with an untrusted
3951 * config, then with a trusted config. The validation is more strict when the
3952 * config is trusted.
3955 spa_ld_validate_vdevs(spa_t
*spa
)
3958 vdev_t
*rvd
= spa
->spa_root_vdev
;
3960 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3961 error
= vdev_validate(rvd
);
3962 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3965 spa_load_failed(spa
, "vdev_validate failed [error=%d]", error
);
3969 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
) {
3970 spa_load_failed(spa
, "cannot open vdev tree after invalidating "
3972 vdev_dbgmsg_print_tree(rvd
, 2);
3973 return (SET_ERROR(ENXIO
));
3980 spa_ld_select_uberblock_done(spa_t
*spa
, uberblock_t
*ub
)
3982 spa
->spa_state
= POOL_STATE_ACTIVE
;
3983 spa
->spa_ubsync
= spa
->spa_uberblock
;
3984 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
3985 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
3986 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
3987 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
3988 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
3989 spa
->spa_prev_software_version
= ub
->ub_software_version
;
3993 spa_ld_select_uberblock(spa_t
*spa
, spa_import_type_t type
)
3995 vdev_t
*rvd
= spa
->spa_root_vdev
;
3997 uberblock_t
*ub
= &spa
->spa_uberblock
;
3998 boolean_t activity_check
= B_FALSE
;
4001 * If we are opening the checkpointed state of the pool by
4002 * rewinding to it, at this point we will have written the
4003 * checkpointed uberblock to the vdev labels, so searching
4004 * the labels will find the right uberblock. However, if
4005 * we are opening the checkpointed state read-only, we have
4006 * not modified the labels. Therefore, we must ignore the
4007 * labels and continue using the spa_uberblock that was set
4008 * by spa_ld_checkpoint_rewind.
4010 * Note that it would be fine to ignore the labels when
4011 * rewinding (opening writeable) as well. However, if we
4012 * crash just after writing the labels, we will end up
4013 * searching the labels. Doing so in the common case means
4014 * that this code path gets exercised normally, rather than
4015 * just in the edge case.
4017 if (ub
->ub_checkpoint_txg
!= 0 &&
4018 spa_importing_readonly_checkpoint(spa
)) {
4019 spa_ld_select_uberblock_done(spa
, ub
);
4024 * Find the best uberblock.
4026 vdev_uberblock_load(rvd
, ub
, &label
);
4029 * If we weren't able to find a single valid uberblock, return failure.
4031 if (ub
->ub_txg
== 0) {
4033 spa_load_failed(spa
, "no valid uberblock found");
4034 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
4037 if (spa
->spa_load_max_txg
!= UINT64_MAX
) {
4038 (void) spa_import_progress_set_max_txg(spa_guid(spa
),
4039 (u_longlong_t
)spa
->spa_load_max_txg
);
4041 spa_load_note(spa
, "using uberblock with txg=%llu",
4042 (u_longlong_t
)ub
->ub_txg
);
4043 if (ub
->ub_raidz_reflow_info
!= 0) {
4044 spa_load_note(spa
, "uberblock raidz_reflow_info: "
4045 "state=%u offset=%llu",
4046 (int)RRSS_GET_STATE(ub
),
4047 (u_longlong_t
)RRSS_GET_OFFSET(ub
));
4052 * For pools which have the multihost property on determine if the
4053 * pool is truly inactive and can be safely imported. Prevent
4054 * hosts which don't have a hostid set from importing the pool.
4056 activity_check
= spa_activity_check_required(spa
, ub
, label
,
4058 if (activity_check
) {
4059 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
4060 spa_get_hostid(spa
) == 0) {
4062 fnvlist_add_uint64(spa
->spa_load_info
,
4063 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
4064 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
4067 int error
= spa_activity_check(spa
, ub
, spa
->spa_config
);
4073 fnvlist_add_uint64(spa
->spa_load_info
,
4074 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
4075 fnvlist_add_uint64(spa
->spa_load_info
,
4076 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
4077 fnvlist_add_uint16(spa
->spa_load_info
,
4078 ZPOOL_CONFIG_MMP_SEQ
,
4079 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0));
4083 * If the pool has an unsupported version we can't open it.
4085 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
4087 spa_load_failed(spa
, "version %llu is not supported",
4088 (u_longlong_t
)ub
->ub_version
);
4089 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
4092 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
4096 * If we weren't able to find what's necessary for reading the
4097 * MOS in the label, return failure.
4099 if (label
== NULL
) {
4100 spa_load_failed(spa
, "label config unavailable");
4101 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
4105 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
4108 spa_load_failed(spa
, "invalid label: '%s' missing",
4109 ZPOOL_CONFIG_FEATURES_FOR_READ
);
4110 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
4115 * Update our in-core representation with the definitive values
4118 nvlist_free(spa
->spa_label_features
);
4119 spa
->spa_label_features
= fnvlist_dup(features
);
4125 * Look through entries in the label nvlist's features_for_read. If
4126 * there is a feature listed there which we don't understand then we
4127 * cannot open a pool.
4129 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
4130 nvlist_t
*unsup_feat
;
4132 unsup_feat
= fnvlist_alloc();
4134 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
4136 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
4137 if (!zfeature_is_supported(nvpair_name(nvp
))) {
4138 fnvlist_add_string(unsup_feat
,
4139 nvpair_name(nvp
), "");
4143 if (!nvlist_empty(unsup_feat
)) {
4144 fnvlist_add_nvlist(spa
->spa_load_info
,
4145 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
4146 nvlist_free(unsup_feat
);
4147 spa_load_failed(spa
, "some features are unsupported");
4148 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
4152 nvlist_free(unsup_feat
);
4155 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
4156 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4157 spa_try_repair(spa
, spa
->spa_config
);
4158 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4159 nvlist_free(spa
->spa_config_splitting
);
4160 spa
->spa_config_splitting
= NULL
;
4164 * Initialize internal SPA structures.
4166 spa_ld_select_uberblock_done(spa
, ub
);
4172 spa_ld_open_rootbp(spa_t
*spa
)
4175 vdev_t
*rvd
= spa
->spa_root_vdev
;
4177 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
4179 spa_load_failed(spa
, "unable to open rootbp in dsl_pool_init "
4180 "[error=%d]", error
);
4181 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4183 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
4189 spa_ld_trusted_config(spa_t
*spa
, spa_import_type_t type
,
4190 boolean_t reloading
)
4192 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
4193 nvlist_t
*nv
, *mos_config
, *policy
;
4194 int error
= 0, copy_error
;
4195 uint64_t healthy_tvds
, healthy_tvds_mos
;
4196 uint64_t mos_config_txg
;
4198 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
, B_TRUE
)
4200 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4203 * If we're assembling a pool from a split, the config provided is
4204 * already trusted so there is nothing to do.
4206 if (type
== SPA_IMPORT_ASSEMBLE
)
4209 healthy_tvds
= spa_healthy_core_tvds(spa
);
4211 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
)
4213 spa_load_failed(spa
, "unable to retrieve MOS config");
4214 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4218 * If we are doing an open, pool owner wasn't verified yet, thus do
4219 * the verification here.
4221 if (spa
->spa_load_state
== SPA_LOAD_OPEN
) {
4222 error
= spa_verify_host(spa
, mos_config
);
4224 nvlist_free(mos_config
);
4229 nv
= fnvlist_lookup_nvlist(mos_config
, ZPOOL_CONFIG_VDEV_TREE
);
4231 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4234 * Build a new vdev tree from the trusted config
4236 error
= spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
);
4238 nvlist_free(mos_config
);
4239 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4240 spa_load_failed(spa
, "spa_config_parse failed [error=%d]",
4242 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4246 * Vdev paths in the MOS may be obsolete. If the untrusted config was
4247 * obtained by scanning /dev/dsk, then it will have the right vdev
4248 * paths. We update the trusted MOS config with this information.
4249 * We first try to copy the paths with vdev_copy_path_strict, which
4250 * succeeds only when both configs have exactly the same vdev tree.
4251 * If that fails, we fall back to a more flexible method that has a
4252 * best effort policy.
4254 copy_error
= vdev_copy_path_strict(rvd
, mrvd
);
4255 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
4256 spa_load_note(spa
, "provided vdev tree:");
4257 vdev_dbgmsg_print_tree(rvd
, 2);
4258 spa_load_note(spa
, "MOS vdev tree:");
4259 vdev_dbgmsg_print_tree(mrvd
, 2);
4261 if (copy_error
!= 0) {
4262 spa_load_note(spa
, "vdev_copy_path_strict failed, falling "
4263 "back to vdev_copy_path_relaxed");
4264 vdev_copy_path_relaxed(rvd
, mrvd
);
4269 spa
->spa_root_vdev
= mrvd
;
4271 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4274 * If 'zpool import' used a cached config, then the on-disk hostid and
4275 * hostname may be different to the cached config in ways that should
4276 * prevent import. Userspace can't discover this without a scan, but
4277 * we know, so we add these values to LOAD_INFO so the caller can know
4280 * Note that we have to do this before the config is regenerated,
4281 * because the new config will have the hostid and hostname for this
4282 * host, in readiness for import.
4284 if (nvlist_exists(mos_config
, ZPOOL_CONFIG_HOSTID
))
4285 fnvlist_add_uint64(spa
->spa_load_info
, ZPOOL_CONFIG_HOSTID
,
4286 fnvlist_lookup_uint64(mos_config
, ZPOOL_CONFIG_HOSTID
));
4287 if (nvlist_exists(mos_config
, ZPOOL_CONFIG_HOSTNAME
))
4288 fnvlist_add_string(spa
->spa_load_info
, ZPOOL_CONFIG_HOSTNAME
,
4289 fnvlist_lookup_string(mos_config
, ZPOOL_CONFIG_HOSTNAME
));
4292 * We will use spa_config if we decide to reload the spa or if spa_load
4293 * fails and we rewind. We must thus regenerate the config using the
4294 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
4295 * pass settings on how to load the pool and is not stored in the MOS.
4296 * We copy it over to our new, trusted config.
4298 mos_config_txg
= fnvlist_lookup_uint64(mos_config
,
4299 ZPOOL_CONFIG_POOL_TXG
);
4300 nvlist_free(mos_config
);
4301 mos_config
= spa_config_generate(spa
, NULL
, mos_config_txg
, B_FALSE
);
4302 if (nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_LOAD_POLICY
,
4304 fnvlist_add_nvlist(mos_config
, ZPOOL_LOAD_POLICY
, policy
);
4305 spa_config_set(spa
, mos_config
);
4306 spa
->spa_config_source
= SPA_CONFIG_SRC_MOS
;
4309 * Now that we got the config from the MOS, we should be more strict
4310 * in checking blkptrs and can make assumptions about the consistency
4311 * of the vdev tree. spa_trust_config must be set to true before opening
4312 * vdevs in order for them to be writeable.
4314 spa
->spa_trust_config
= B_TRUE
;
4317 * Open and validate the new vdev tree
4319 error
= spa_ld_open_vdevs(spa
);
4323 error
= spa_ld_validate_vdevs(spa
);
4327 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
4328 spa_load_note(spa
, "final vdev tree:");
4329 vdev_dbgmsg_print_tree(rvd
, 2);
4332 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
&&
4333 !spa
->spa_extreme_rewind
&& zfs_max_missing_tvds
== 0) {
4335 * Sanity check to make sure that we are indeed loading the
4336 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
4337 * in the config provided and they happened to be the only ones
4338 * to have the latest uberblock, we could involuntarily perform
4339 * an extreme rewind.
4341 healthy_tvds_mos
= spa_healthy_core_tvds(spa
);
4342 if (healthy_tvds_mos
- healthy_tvds
>=
4343 SPA_SYNC_MIN_VDEVS
) {
4344 spa_load_note(spa
, "config provided misses too many "
4345 "top-level vdevs compared to MOS (%lld vs %lld). ",
4346 (u_longlong_t
)healthy_tvds
,
4347 (u_longlong_t
)healthy_tvds_mos
);
4348 spa_load_note(spa
, "vdev tree:");
4349 vdev_dbgmsg_print_tree(rvd
, 2);
4351 spa_load_failed(spa
, "config was already "
4352 "provided from MOS. Aborting.");
4353 return (spa_vdev_err(rvd
,
4354 VDEV_AUX_CORRUPT_DATA
, EIO
));
4356 spa_load_note(spa
, "spa must be reloaded using MOS "
4358 return (SET_ERROR(EAGAIN
));
4362 error
= spa_check_for_missing_logs(spa
);
4364 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
4366 if (rvd
->vdev_guid_sum
!= spa
->spa_uberblock
.ub_guid_sum
) {
4367 spa_load_failed(spa
, "uberblock guid sum doesn't match MOS "
4368 "guid sum (%llu != %llu)",
4369 (u_longlong_t
)spa
->spa_uberblock
.ub_guid_sum
,
4370 (u_longlong_t
)rvd
->vdev_guid_sum
);
4371 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
4379 spa_ld_open_indirect_vdev_metadata(spa_t
*spa
)
4382 vdev_t
*rvd
= spa
->spa_root_vdev
;
4385 * Everything that we read before spa_remove_init() must be stored
4386 * on concreted vdevs. Therefore we do this as early as possible.
4388 error
= spa_remove_init(spa
);
4390 spa_load_failed(spa
, "spa_remove_init failed [error=%d]",
4392 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4396 * Retrieve information needed to condense indirect vdev mappings.
4398 error
= spa_condense_init(spa
);
4400 spa_load_failed(spa
, "spa_condense_init failed [error=%d]",
4402 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4409 spa_ld_check_features(spa_t
*spa
, boolean_t
*missing_feat_writep
)
4412 vdev_t
*rvd
= spa
->spa_root_vdev
;
4414 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
4415 boolean_t missing_feat_read
= B_FALSE
;
4416 nvlist_t
*unsup_feat
, *enabled_feat
;
4418 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
4419 &spa
->spa_feat_for_read_obj
, B_TRUE
) != 0) {
4420 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4423 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
4424 &spa
->spa_feat_for_write_obj
, B_TRUE
) != 0) {
4425 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4428 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
4429 &spa
->spa_feat_desc_obj
, B_TRUE
) != 0) {
4430 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4433 enabled_feat
= fnvlist_alloc();
4434 unsup_feat
= fnvlist_alloc();
4436 if (!spa_features_check(spa
, B_FALSE
,
4437 unsup_feat
, enabled_feat
))
4438 missing_feat_read
= B_TRUE
;
4440 if (spa_writeable(spa
) ||
4441 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) {
4442 if (!spa_features_check(spa
, B_TRUE
,
4443 unsup_feat
, enabled_feat
)) {
4444 *missing_feat_writep
= B_TRUE
;
4448 fnvlist_add_nvlist(spa
->spa_load_info
,
4449 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
4451 if (!nvlist_empty(unsup_feat
)) {
4452 fnvlist_add_nvlist(spa
->spa_load_info
,
4453 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
4456 fnvlist_free(enabled_feat
);
4457 fnvlist_free(unsup_feat
);
4459 if (!missing_feat_read
) {
4460 fnvlist_add_boolean(spa
->spa_load_info
,
4461 ZPOOL_CONFIG_CAN_RDONLY
);
4465 * If the state is SPA_LOAD_TRYIMPORT, our objective is
4466 * twofold: to determine whether the pool is available for
4467 * import in read-write mode and (if it is not) whether the
4468 * pool is available for import in read-only mode. If the pool
4469 * is available for import in read-write mode, it is displayed
4470 * as available in userland; if it is not available for import
4471 * in read-only mode, it is displayed as unavailable in
4472 * userland. If the pool is available for import in read-only
4473 * mode but not read-write mode, it is displayed as unavailable
4474 * in userland with a special note that the pool is actually
4475 * available for open in read-only mode.
4477 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
4478 * missing a feature for write, we must first determine whether
4479 * the pool can be opened read-only before returning to
4480 * userland in order to know whether to display the
4481 * abovementioned note.
4483 if (missing_feat_read
|| (*missing_feat_writep
&&
4484 spa_writeable(spa
))) {
4485 spa_load_failed(spa
, "pool uses unsupported features");
4486 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
4491 * Load refcounts for ZFS features from disk into an in-memory
4492 * cache during SPA initialization.
4494 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
4497 error
= feature_get_refcount_from_disk(spa
,
4498 &spa_feature_table
[i
], &refcount
);
4500 spa
->spa_feat_refcount_cache
[i
] = refcount
;
4501 } else if (error
== ENOTSUP
) {
4502 spa
->spa_feat_refcount_cache
[i
] =
4503 SPA_FEATURE_DISABLED
;
4505 spa_load_failed(spa
, "error getting refcount "
4506 "for feature %s [error=%d]",
4507 spa_feature_table
[i
].fi_guid
, error
);
4508 return (spa_vdev_err(rvd
,
4509 VDEV_AUX_CORRUPT_DATA
, EIO
));
4514 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
4515 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
4516 &spa
->spa_feat_enabled_txg_obj
, B_TRUE
) != 0)
4517 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4521 * Encryption was added before bookmark_v2, even though bookmark_v2
4522 * is now a dependency. If this pool has encryption enabled without
4523 * bookmark_v2, trigger an errata message.
4525 if (spa_feature_is_enabled(spa
, SPA_FEATURE_ENCRYPTION
) &&
4526 !spa_feature_is_enabled(spa
, SPA_FEATURE_BOOKMARK_V2
)) {
4527 spa
->spa_errata
= ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
;
4534 spa_ld_load_special_directories(spa_t
*spa
)
4537 vdev_t
*rvd
= spa
->spa_root_vdev
;
4539 spa
->spa_is_initializing
= B_TRUE
;
4540 error
= dsl_pool_open(spa
->spa_dsl_pool
);
4541 spa
->spa_is_initializing
= B_FALSE
;
4543 spa_load_failed(spa
, "dsl_pool_open failed [error=%d]", error
);
4544 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4551 spa_ld_get_props(spa_t
*spa
)
4555 vdev_t
*rvd
= spa
->spa_root_vdev
;
4557 /* Grab the checksum salt from the MOS. */
4558 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4559 DMU_POOL_CHECKSUM_SALT
, 1,
4560 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
4561 spa
->spa_cksum_salt
.zcs_bytes
);
4562 if (error
== ENOENT
) {
4563 /* Generate a new salt for subsequent use */
4564 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
4565 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
4566 } else if (error
!= 0) {
4567 spa_load_failed(spa
, "unable to retrieve checksum salt from "
4568 "MOS [error=%d]", error
);
4569 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4572 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
, B_TRUE
) != 0)
4573 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4574 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
4576 spa_load_failed(spa
, "error opening deferred-frees bpobj "
4577 "[error=%d]", error
);
4578 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4582 * Load the bit that tells us to use the new accounting function
4583 * (raid-z deflation). If we have an older pool, this will not
4586 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
, B_FALSE
);
4587 if (error
!= 0 && error
!= ENOENT
)
4588 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4590 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
4591 &spa
->spa_creation_version
, B_FALSE
);
4592 if (error
!= 0 && error
!= ENOENT
)
4593 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4596 * Load the persistent error log. If we have an older pool, this will
4599 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
,
4601 if (error
!= 0 && error
!= ENOENT
)
4602 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4604 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
4605 &spa
->spa_errlog_scrub
, B_FALSE
);
4606 if (error
!= 0 && error
!= ENOENT
)
4607 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4610 * Load the livelist deletion field. If a livelist is queued for
4611 * deletion, indicate that in the spa
4613 error
= spa_dir_prop(spa
, DMU_POOL_DELETED_CLONES
,
4614 &spa
->spa_livelists_to_delete
, B_FALSE
);
4615 if (error
!= 0 && error
!= ENOENT
)
4616 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4619 * Load the history object. If we have an older pool, this
4620 * will not be present.
4622 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
, B_FALSE
);
4623 if (error
!= 0 && error
!= ENOENT
)
4624 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4627 * Load the per-vdev ZAP map. If we have an older pool, this will not
4628 * be present; in this case, defer its creation to a later time to
4629 * avoid dirtying the MOS this early / out of sync context. See
4630 * spa_sync_config_object.
4633 /* The sentinel is only available in the MOS config. */
4634 nvlist_t
*mos_config
;
4635 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0) {
4636 spa_load_failed(spa
, "unable to retrieve MOS config");
4637 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4640 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
4641 &spa
->spa_all_vdev_zaps
, B_FALSE
);
4643 if (error
== ENOENT
) {
4644 VERIFY(!nvlist_exists(mos_config
,
4645 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
4646 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
4647 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4648 } else if (error
!= 0) {
4649 nvlist_free(mos_config
);
4650 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4651 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
4653 * An older version of ZFS overwrote the sentinel value, so
4654 * we have orphaned per-vdev ZAPs in the MOS. Defer their
4655 * destruction to later; see spa_sync_config_object.
4657 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
4659 * We're assuming that no vdevs have had their ZAPs created
4660 * before this. Better be sure of it.
4662 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4664 nvlist_free(mos_config
);
4666 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
4668 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
,
4670 if (error
&& error
!= ENOENT
)
4671 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4674 uint64_t autoreplace
= 0;
4676 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
4677 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
4678 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
4679 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
4680 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
4681 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
4682 spa_prop_find(spa
, ZPOOL_PROP_AUTOTRIM
, &spa
->spa_autotrim
);
4683 spa
->spa_autoreplace
= (autoreplace
!= 0);
4687 * If we are importing a pool with missing top-level vdevs,
4688 * we enforce that the pool doesn't panic or get suspended on
4689 * error since the likelihood of missing data is extremely high.
4691 if (spa
->spa_missing_tvds
> 0 &&
4692 spa
->spa_failmode
!= ZIO_FAILURE_MODE_CONTINUE
&&
4693 spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4694 spa_load_note(spa
, "forcing failmode to 'continue' "
4695 "as some top level vdevs are missing");
4696 spa
->spa_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
4703 spa_ld_open_aux_vdevs(spa_t
*spa
, spa_import_type_t type
)
4706 vdev_t
*rvd
= spa
->spa_root_vdev
;
4709 * If we're assembling the pool from the split-off vdevs of
4710 * an existing pool, we don't want to attach the spares & cache
4715 * Load any hot spares for this pool.
4717 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
,
4719 if (error
!= 0 && error
!= ENOENT
)
4720 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4721 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4722 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
4723 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
4724 &spa
->spa_spares
.sav_config
) != 0) {
4725 spa_load_failed(spa
, "error loading spares nvlist");
4726 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4729 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4730 spa_load_spares(spa
);
4731 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4732 } else if (error
== 0) {
4733 spa
->spa_spares
.sav_sync
= B_TRUE
;
4737 * Load any level 2 ARC devices for this pool.
4739 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
4740 &spa
->spa_l2cache
.sav_object
, B_FALSE
);
4741 if (error
!= 0 && error
!= ENOENT
)
4742 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4743 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4744 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
4745 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
4746 &spa
->spa_l2cache
.sav_config
) != 0) {
4747 spa_load_failed(spa
, "error loading l2cache nvlist");
4748 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4751 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4752 spa_load_l2cache(spa
);
4753 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4754 } else if (error
== 0) {
4755 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4762 spa_ld_load_vdev_metadata(spa_t
*spa
)
4765 vdev_t
*rvd
= spa
->spa_root_vdev
;
4768 * If the 'multihost' property is set, then never allow a pool to
4769 * be imported when the system hostid is zero. The exception to
4770 * this rule is zdb which is always allowed to access pools.
4772 if (spa_multihost(spa
) && spa_get_hostid(spa
) == 0 &&
4773 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
4774 fnvlist_add_uint64(spa
->spa_load_info
,
4775 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
4776 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
4780 * If the 'autoreplace' property is set, then post a resource notifying
4781 * the ZFS DE that it should not issue any faults for unopenable
4782 * devices. We also iterate over the vdevs, and post a sysevent for any
4783 * unopenable vdevs so that the normal autoreplace handler can take
4786 if (spa
->spa_autoreplace
&& spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4787 spa_check_removed(spa
->spa_root_vdev
);
4789 * For the import case, this is done in spa_import(), because
4790 * at this point we're using the spare definitions from
4791 * the MOS config, not necessarily from the userland config.
4793 if (spa
->spa_load_state
!= SPA_LOAD_IMPORT
) {
4794 spa_aux_check_removed(&spa
->spa_spares
);
4795 spa_aux_check_removed(&spa
->spa_l2cache
);
4800 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
4802 error
= vdev_load(rvd
);
4804 spa_load_failed(spa
, "vdev_load failed [error=%d]", error
);
4805 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4808 error
= spa_ld_log_spacemaps(spa
);
4810 spa_load_failed(spa
, "spa_ld_log_spacemaps failed [error=%d]",
4812 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4816 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
4818 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4819 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
, B_FALSE
);
4820 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4826 spa_ld_load_dedup_tables(spa_t
*spa
)
4829 vdev_t
*rvd
= spa
->spa_root_vdev
;
4831 error
= ddt_load(spa
);
4833 spa_load_failed(spa
, "ddt_load failed [error=%d]", error
);
4834 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4841 spa_ld_load_brt(spa_t
*spa
)
4844 vdev_t
*rvd
= spa
->spa_root_vdev
;
4846 error
= brt_load(spa
);
4848 spa_load_failed(spa
, "brt_load failed [error=%d]", error
);
4849 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4856 spa_ld_verify_logs(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
4858 vdev_t
*rvd
= spa
->spa_root_vdev
;
4860 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
)) {
4861 boolean_t missing
= spa_check_logs(spa
);
4863 if (spa
->spa_missing_tvds
!= 0) {
4864 spa_load_note(spa
, "spa_check_logs failed "
4865 "so dropping the logs");
4867 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
4868 spa_load_failed(spa
, "spa_check_logs failed");
4869 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
,
4879 spa_ld_verify_pool_data(spa_t
*spa
)
4882 vdev_t
*rvd
= spa
->spa_root_vdev
;
4885 * We've successfully opened the pool, verify that we're ready
4886 * to start pushing transactions.
4888 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4889 error
= spa_load_verify(spa
);
4891 spa_load_failed(spa
, "spa_load_verify failed "
4892 "[error=%d]", error
);
4893 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
4902 spa_ld_claim_log_blocks(spa_t
*spa
)
4905 dsl_pool_t
*dp
= spa_get_dsl(spa
);
4908 * Claim log blocks that haven't been committed yet.
4909 * This must all happen in a single txg.
4910 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
4911 * invoked from zil_claim_log_block()'s i/o done callback.
4912 * Price of rollback is that we abandon the log.
4914 spa
->spa_claiming
= B_TRUE
;
4916 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
4917 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
4918 zil_claim
, tx
, DS_FIND_CHILDREN
);
4921 spa
->spa_claiming
= B_FALSE
;
4923 spa_set_log_state(spa
, SPA_LOG_GOOD
);
4927 spa_ld_check_for_config_update(spa_t
*spa
, uint64_t config_cache_txg
,
4928 boolean_t update_config_cache
)
4930 vdev_t
*rvd
= spa
->spa_root_vdev
;
4931 int need_update
= B_FALSE
;
4934 * If the config cache is stale, or we have uninitialized
4935 * metaslabs (see spa_vdev_add()), then update the config.
4937 * If this is a verbatim import, trust the current
4938 * in-core spa_config and update the disk labels.
4940 if (update_config_cache
|| config_cache_txg
!= spa
->spa_config_txg
||
4941 spa
->spa_load_state
== SPA_LOAD_IMPORT
||
4942 spa
->spa_load_state
== SPA_LOAD_RECOVER
||
4943 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
4944 need_update
= B_TRUE
;
4946 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
4947 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
4948 need_update
= B_TRUE
;
4951 * Update the config cache asynchronously in case we're the
4952 * root pool, in which case the config cache isn't writable yet.
4955 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
4959 spa_ld_prepare_for_reload(spa_t
*spa
)
4961 spa_mode_t mode
= spa
->spa_mode
;
4962 int async_suspended
= spa
->spa_async_suspended
;
4965 spa_deactivate(spa
);
4966 spa_activate(spa
, mode
);
4969 * We save the value of spa_async_suspended as it gets reset to 0 by
4970 * spa_unload(). We want to restore it back to the original value before
4971 * returning as we might be calling spa_async_resume() later.
4973 spa
->spa_async_suspended
= async_suspended
;
4977 spa_ld_read_checkpoint_txg(spa_t
*spa
)
4979 uberblock_t checkpoint
;
4982 ASSERT0(spa
->spa_checkpoint_txg
);
4983 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4985 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4986 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
4987 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
4989 if (error
== ENOENT
)
4995 ASSERT3U(checkpoint
.ub_txg
, !=, 0);
4996 ASSERT3U(checkpoint
.ub_checkpoint_txg
, !=, 0);
4997 ASSERT3U(checkpoint
.ub_timestamp
, !=, 0);
4998 spa
->spa_checkpoint_txg
= checkpoint
.ub_txg
;
4999 spa
->spa_checkpoint_info
.sci_timestamp
= checkpoint
.ub_timestamp
;
5005 spa_ld_mos_init(spa_t
*spa
, spa_import_type_t type
)
5009 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5010 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
5013 * Never trust the config that is provided unless we are assembling
5014 * a pool following a split.
5015 * This means don't trust blkptrs and the vdev tree in general. This
5016 * also effectively puts the spa in read-only mode since
5017 * spa_writeable() checks for spa_trust_config to be true.
5018 * We will later load a trusted config from the MOS.
5020 if (type
!= SPA_IMPORT_ASSEMBLE
)
5021 spa
->spa_trust_config
= B_FALSE
;
5024 * Parse the config provided to create a vdev tree.
5026 error
= spa_ld_parse_config(spa
, type
);
5030 spa_import_progress_add(spa
);
5033 * Now that we have the vdev tree, try to open each vdev. This involves
5034 * opening the underlying physical device, retrieving its geometry and
5035 * probing the vdev with a dummy I/O. The state of each vdev will be set
5036 * based on the success of those operations. After this we'll be ready
5037 * to read from the vdevs.
5039 error
= spa_ld_open_vdevs(spa
);
5044 * Read the label of each vdev and make sure that the GUIDs stored
5045 * there match the GUIDs in the config provided.
5046 * If we're assembling a new pool that's been split off from an
5047 * existing pool, the labels haven't yet been updated so we skip
5048 * validation for now.
5050 if (type
!= SPA_IMPORT_ASSEMBLE
) {
5051 error
= spa_ld_validate_vdevs(spa
);
5057 * Read all vdev labels to find the best uberblock (i.e. latest,
5058 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
5059 * get the list of features required to read blkptrs in the MOS from
5060 * the vdev label with the best uberblock and verify that our version
5061 * of zfs supports them all.
5063 error
= spa_ld_select_uberblock(spa
, type
);
5068 * Pass that uberblock to the dsl_pool layer which will open the root
5069 * blkptr. This blkptr points to the latest version of the MOS and will
5070 * allow us to read its contents.
5072 error
= spa_ld_open_rootbp(spa
);
5080 spa_ld_checkpoint_rewind(spa_t
*spa
)
5082 uberblock_t checkpoint
;
5085 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5086 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
5088 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
5089 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
5090 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
5093 spa_load_failed(spa
, "unable to retrieve checkpointed "
5094 "uberblock from the MOS config [error=%d]", error
);
5096 if (error
== ENOENT
)
5097 error
= ZFS_ERR_NO_CHECKPOINT
;
5102 ASSERT3U(checkpoint
.ub_txg
, <, spa
->spa_uberblock
.ub_txg
);
5103 ASSERT3U(checkpoint
.ub_txg
, ==, checkpoint
.ub_checkpoint_txg
);
5106 * We need to update the txg and timestamp of the checkpointed
5107 * uberblock to be higher than the latest one. This ensures that
5108 * the checkpointed uberblock is selected if we were to close and
5109 * reopen the pool right after we've written it in the vdev labels.
5110 * (also see block comment in vdev_uberblock_compare)
5112 checkpoint
.ub_txg
= spa
->spa_uberblock
.ub_txg
+ 1;
5113 checkpoint
.ub_timestamp
= gethrestime_sec();
5116 * Set current uberblock to be the checkpointed uberblock.
5118 spa
->spa_uberblock
= checkpoint
;
5121 * If we are doing a normal rewind, then the pool is open for
5122 * writing and we sync the "updated" checkpointed uberblock to
5123 * disk. Once this is done, we've basically rewound the whole
5124 * pool and there is no way back.
5126 * There are cases when we don't want to attempt and sync the
5127 * checkpointed uberblock to disk because we are opening a
5128 * pool as read-only. Specifically, verifying the checkpointed
5129 * state with zdb, and importing the checkpointed state to get
5130 * a "preview" of its content.
5132 if (spa_writeable(spa
)) {
5133 vdev_t
*rvd
= spa
->spa_root_vdev
;
5135 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5136 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
5138 int children
= rvd
->vdev_children
;
5139 int c0
= random_in_range(children
);
5141 for (int c
= 0; c
< children
; c
++) {
5142 vdev_t
*vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
5144 /* Stop when revisiting the first vdev */
5145 if (c
> 0 && svd
[0] == vd
)
5148 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
5149 !vdev_is_concrete(vd
))
5152 svd
[svdcount
++] = vd
;
5153 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
5156 error
= vdev_config_sync(svd
, svdcount
, spa
->spa_first_txg
);
5158 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
5159 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5162 spa_load_failed(spa
, "failed to write checkpointed "
5163 "uberblock to the vdev labels [error=%d]", error
);
5172 spa_ld_mos_with_trusted_config(spa_t
*spa
, spa_import_type_t type
,
5173 boolean_t
*update_config_cache
)
5178 * Parse the config for pool, open and validate vdevs,
5179 * select an uberblock, and use that uberblock to open
5182 error
= spa_ld_mos_init(spa
, type
);
5187 * Retrieve the trusted config stored in the MOS and use it to create
5188 * a new, exact version of the vdev tree, then reopen all vdevs.
5190 error
= spa_ld_trusted_config(spa
, type
, B_FALSE
);
5191 if (error
== EAGAIN
) {
5192 if (update_config_cache
!= NULL
)
5193 *update_config_cache
= B_TRUE
;
5196 * Redo the loading process with the trusted config if it is
5197 * too different from the untrusted config.
5199 spa_ld_prepare_for_reload(spa
);
5200 spa_load_note(spa
, "RELOADING");
5201 error
= spa_ld_mos_init(spa
, type
);
5205 error
= spa_ld_trusted_config(spa
, type
, B_TRUE
);
5209 } else if (error
!= 0) {
5217 * Load an existing storage pool, using the config provided. This config
5218 * describes which vdevs are part of the pool and is later validated against
5219 * partial configs present in each vdev's label and an entire copy of the
5220 * config stored in the MOS.
5223 spa_load_impl(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
5226 boolean_t missing_feat_write
= B_FALSE
;
5227 boolean_t checkpoint_rewind
=
5228 (spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
5229 boolean_t update_config_cache
= B_FALSE
;
5231 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5232 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
5234 spa_load_note(spa
, "LOADING");
5236 error
= spa_ld_mos_with_trusted_config(spa
, type
, &update_config_cache
);
5241 * If we are rewinding to the checkpoint then we need to repeat
5242 * everything we've done so far in this function but this time
5243 * selecting the checkpointed uberblock and using that to open
5246 if (checkpoint_rewind
) {
5248 * If we are rewinding to the checkpoint update config cache
5251 update_config_cache
= B_TRUE
;
5254 * Extract the checkpointed uberblock from the current MOS
5255 * and use this as the pool's uberblock from now on. If the
5256 * pool is imported as writeable we also write the checkpoint
5257 * uberblock to the labels, making the rewind permanent.
5259 error
= spa_ld_checkpoint_rewind(spa
);
5264 * Redo the loading process again with the
5265 * checkpointed uberblock.
5267 spa_ld_prepare_for_reload(spa
);
5268 spa_load_note(spa
, "LOADING checkpointed uberblock");
5269 error
= spa_ld_mos_with_trusted_config(spa
, type
, NULL
);
5275 * Retrieve the checkpoint txg if the pool has a checkpoint.
5277 spa_import_progress_set_notes(spa
, "Loading checkpoint txg");
5278 error
= spa_ld_read_checkpoint_txg(spa
);
5283 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
5284 * from the pool and their contents were re-mapped to other vdevs. Note
5285 * that everything that we read before this step must have been
5286 * rewritten on concrete vdevs after the last device removal was
5287 * initiated. Otherwise we could be reading from indirect vdevs before
5288 * we have loaded their mappings.
5290 spa_import_progress_set_notes(spa
, "Loading indirect vdev metadata");
5291 error
= spa_ld_open_indirect_vdev_metadata(spa
);
5296 * Retrieve the full list of active features from the MOS and check if
5297 * they are all supported.
5299 spa_import_progress_set_notes(spa
, "Checking feature flags");
5300 error
= spa_ld_check_features(spa
, &missing_feat_write
);
5305 * Load several special directories from the MOS needed by the dsl_pool
5308 spa_import_progress_set_notes(spa
, "Loading special MOS directories");
5309 error
= spa_ld_load_special_directories(spa
);
5314 * Retrieve pool properties from the MOS.
5316 spa_import_progress_set_notes(spa
, "Loading properties");
5317 error
= spa_ld_get_props(spa
);
5322 * Retrieve the list of auxiliary devices - cache devices and spares -
5325 spa_import_progress_set_notes(spa
, "Loading AUX vdevs");
5326 error
= spa_ld_open_aux_vdevs(spa
, type
);
5331 * Load the metadata for all vdevs. Also check if unopenable devices
5332 * should be autoreplaced.
5334 spa_import_progress_set_notes(spa
, "Loading vdev metadata");
5335 error
= spa_ld_load_vdev_metadata(spa
);
5339 spa_import_progress_set_notes(spa
, "Loading dedup tables");
5340 error
= spa_ld_load_dedup_tables(spa
);
5344 spa_import_progress_set_notes(spa
, "Loading BRT");
5345 error
= spa_ld_load_brt(spa
);
5350 * Verify the logs now to make sure we don't have any unexpected errors
5351 * when we claim log blocks later.
5353 spa_import_progress_set_notes(spa
, "Verifying Log Devices");
5354 error
= spa_ld_verify_logs(spa
, type
, ereport
);
5358 if (missing_feat_write
) {
5359 ASSERT(spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
);
5362 * At this point, we know that we can open the pool in
5363 * read-only mode but not read-write mode. We now have enough
5364 * information and can return to userland.
5366 return (spa_vdev_err(spa
->spa_root_vdev
, VDEV_AUX_UNSUP_FEAT
,
5371 * Traverse the last txgs to make sure the pool was left off in a safe
5372 * state. When performing an extreme rewind, we verify the whole pool,
5373 * which can take a very long time.
5375 spa_import_progress_set_notes(spa
, "Verifying pool data");
5376 error
= spa_ld_verify_pool_data(spa
);
5381 * Calculate the deflated space for the pool. This must be done before
5382 * we write anything to the pool because we'd need to update the space
5383 * accounting using the deflated sizes.
5385 spa_import_progress_set_notes(spa
, "Calculating deflated space");
5386 spa_update_dspace(spa
);
5389 * We have now retrieved all the information we needed to open the
5390 * pool. If we are importing the pool in read-write mode, a few
5391 * additional steps must be performed to finish the import.
5393 spa_import_progress_set_notes(spa
, "Starting import");
5394 if (spa_writeable(spa
) && (spa
->spa_load_state
== SPA_LOAD_RECOVER
||
5395 spa
->spa_load_max_txg
== UINT64_MAX
)) {
5396 uint64_t config_cache_txg
= spa
->spa_config_txg
;
5398 ASSERT(spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
);
5401 * Before we do any zio_write's, complete the raidz expansion
5402 * scratch space copying, if necessary.
5404 if (RRSS_GET_STATE(&spa
->spa_uberblock
) == RRSS_SCRATCH_VALID
)
5405 vdev_raidz_reflow_copy_scratch(spa
);
5408 * In case of a checkpoint rewind, log the original txg
5409 * of the checkpointed uberblock.
5411 if (checkpoint_rewind
) {
5412 spa_history_log_internal(spa
, "checkpoint rewind",
5413 NULL
, "rewound state to txg=%llu",
5414 (u_longlong_t
)spa
->spa_uberblock
.ub_checkpoint_txg
);
5417 spa_import_progress_set_notes(spa
, "Claiming ZIL blocks");
5419 * Traverse the ZIL and claim all blocks.
5421 spa_ld_claim_log_blocks(spa
);
5424 * Kick-off the syncing thread.
5426 spa
->spa_sync_on
= B_TRUE
;
5427 txg_sync_start(spa
->spa_dsl_pool
);
5428 mmp_thread_start(spa
);
5431 * Wait for all claims to sync. We sync up to the highest
5432 * claimed log block birth time so that claimed log blocks
5433 * don't appear to be from the future. spa_claim_max_txg
5434 * will have been set for us by ZIL traversal operations
5437 spa_import_progress_set_notes(spa
, "Syncing ZIL claims");
5438 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
5441 * Check if we need to request an update of the config. On the
5442 * next sync, we would update the config stored in vdev labels
5443 * and the cachefile (by default /etc/zfs/zpool.cache).
5445 spa_import_progress_set_notes(spa
, "Updating configs");
5446 spa_ld_check_for_config_update(spa
, config_cache_txg
,
5447 update_config_cache
);
5450 * Check if a rebuild was in progress and if so resume it.
5451 * Then check all DTLs to see if anything needs resilvering.
5452 * The resilver will be deferred if a rebuild was started.
5454 spa_import_progress_set_notes(spa
, "Starting resilvers");
5455 if (vdev_rebuild_active(spa
->spa_root_vdev
)) {
5456 vdev_rebuild_restart(spa
);
5457 } else if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
5458 vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5459 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
5463 * Log the fact that we booted up (so that we can detect if
5464 * we rebooted in the middle of an operation).
5466 spa_history_log_version(spa
, "open", NULL
);
5468 spa_import_progress_set_notes(spa
,
5469 "Restarting device removals");
5470 spa_restart_removal(spa
);
5471 spa_spawn_aux_threads(spa
);
5474 * Delete any inconsistent datasets.
5477 * Since we may be issuing deletes for clones here,
5478 * we make sure to do so after we've spawned all the
5479 * auxiliary threads above (from which the livelist
5480 * deletion zthr is part of).
5482 spa_import_progress_set_notes(spa
,
5483 "Cleaning up inconsistent objsets");
5484 (void) dmu_objset_find(spa_name(spa
),
5485 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
5488 * Clean up any stale temporary dataset userrefs.
5490 spa_import_progress_set_notes(spa
,
5491 "Cleaning up temporary userrefs");
5492 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
5494 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5495 spa_import_progress_set_notes(spa
, "Restarting initialize");
5496 vdev_initialize_restart(spa
->spa_root_vdev
);
5497 spa_import_progress_set_notes(spa
, "Restarting TRIM");
5498 vdev_trim_restart(spa
->spa_root_vdev
);
5499 vdev_autotrim_restart(spa
);
5500 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5501 spa_import_progress_set_notes(spa
, "Finished importing");
5504 spa_import_progress_remove(spa_guid(spa
));
5505 spa_async_request(spa
, SPA_ASYNC_L2CACHE_REBUILD
);
5507 spa_load_note(spa
, "LOADED");
5513 spa_load_retry(spa_t
*spa
, spa_load_state_t state
)
5515 spa_mode_t mode
= spa
->spa_mode
;
5518 spa_deactivate(spa
);
5520 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
5522 spa_activate(spa
, mode
);
5523 spa_async_suspend(spa
);
5525 spa_load_note(spa
, "spa_load_retry: rewind, max txg: %llu",
5526 (u_longlong_t
)spa
->spa_load_max_txg
);
5528 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
));
5532 * If spa_load() fails this function will try loading prior txg's. If
5533 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
5534 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
5535 * function will not rewind the pool and will return the same error as
5539 spa_load_best(spa_t
*spa
, spa_load_state_t state
, uint64_t max_request
,
5542 nvlist_t
*loadinfo
= NULL
;
5543 nvlist_t
*config
= NULL
;
5544 int load_error
, rewind_error
;
5545 uint64_t safe_rewind_txg
;
5548 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
5549 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
5550 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5552 spa
->spa_load_max_txg
= max_request
;
5553 if (max_request
!= UINT64_MAX
)
5554 spa
->spa_extreme_rewind
= B_TRUE
;
5557 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
);
5558 if (load_error
== 0)
5560 if (load_error
== ZFS_ERR_NO_CHECKPOINT
) {
5562 * When attempting checkpoint-rewind on a pool with no
5563 * checkpoint, we should not attempt to load uberblocks
5564 * from previous txgs when spa_load fails.
5566 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
5567 spa_import_progress_remove(spa_guid(spa
));
5568 return (load_error
);
5571 if (spa
->spa_root_vdev
!= NULL
)
5572 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5574 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
5575 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
5577 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
5578 nvlist_free(config
);
5579 spa_import_progress_remove(spa_guid(spa
));
5580 return (load_error
);
5583 if (state
== SPA_LOAD_RECOVER
) {
5584 /* Price of rolling back is discarding txgs, including log */
5585 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5588 * If we aren't rolling back save the load info from our first
5589 * import attempt so that we can restore it after attempting
5592 loadinfo
= spa
->spa_load_info
;
5593 spa
->spa_load_info
= fnvlist_alloc();
5596 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
5597 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
5598 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
5599 TXG_INITIAL
: safe_rewind_txg
;
5602 * Continue as long as we're finding errors, we're still within
5603 * the acceptable rewind range, and we're still finding uberblocks
5605 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
5606 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
5607 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
5608 spa
->spa_extreme_rewind
= B_TRUE
;
5609 rewind_error
= spa_load_retry(spa
, state
);
5612 spa
->spa_extreme_rewind
= B_FALSE
;
5613 spa
->spa_load_max_txg
= UINT64_MAX
;
5615 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
5616 spa_config_set(spa
, config
);
5618 nvlist_free(config
);
5620 if (state
== SPA_LOAD_RECOVER
) {
5621 ASSERT3P(loadinfo
, ==, NULL
);
5622 spa_import_progress_remove(spa_guid(spa
));
5623 return (rewind_error
);
5625 /* Store the rewind info as part of the initial load info */
5626 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
5627 spa
->spa_load_info
);
5629 /* Restore the initial load info */
5630 fnvlist_free(spa
->spa_load_info
);
5631 spa
->spa_load_info
= loadinfo
;
5633 spa_import_progress_remove(spa_guid(spa
));
5634 return (load_error
);
5641 * The import case is identical to an open except that the configuration is sent
5642 * down from userland, instead of grabbed from the configuration cache. For the
5643 * case of an open, the pool configuration will exist in the
5644 * POOL_STATE_UNINITIALIZED state.
5646 * The stats information (gen/count/ustats) is used to gather vdev statistics at
5647 * the same time open the pool, without having to keep around the spa_t in some
5651 spa_open_common(const char *pool
, spa_t
**spapp
, const void *tag
,
5652 nvlist_t
*nvpolicy
, nvlist_t
**config
)
5655 spa_load_state_t state
= SPA_LOAD_OPEN
;
5657 int locked
= B_FALSE
;
5658 int firstopen
= B_FALSE
;
5663 * As disgusting as this is, we need to support recursive calls to this
5664 * function because dsl_dir_open() is called during spa_load(), and ends
5665 * up calling spa_open() again. The real fix is to figure out how to
5666 * avoid dsl_dir_open() calling this in the first place.
5668 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
5669 mutex_enter(&spa_namespace_lock
);
5673 if ((spa
= spa_lookup(pool
)) == NULL
) {
5675 mutex_exit(&spa_namespace_lock
);
5676 return (SET_ERROR(ENOENT
));
5679 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
5680 zpool_load_policy_t policy
;
5684 zpool_get_load_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
5686 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
5687 state
= SPA_LOAD_RECOVER
;
5689 spa_activate(spa
, spa_mode_global
);
5691 if (state
!= SPA_LOAD_RECOVER
)
5692 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
5693 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
5695 zfs_dbgmsg("spa_open_common: opening %s", pool
);
5696 error
= spa_load_best(spa
, state
, policy
.zlp_txg
,
5699 if (error
== EBADF
) {
5701 * If vdev_validate() returns failure (indicated by
5702 * EBADF), it indicates that one of the vdevs indicates
5703 * that the pool has been exported or destroyed. If
5704 * this is the case, the config cache is out of sync and
5705 * we should remove the pool from the namespace.
5708 spa_deactivate(spa
);
5709 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
5712 mutex_exit(&spa_namespace_lock
);
5713 return (SET_ERROR(ENOENT
));
5718 * We can't open the pool, but we still have useful
5719 * information: the state of each vdev after the
5720 * attempted vdev_open(). Return this to the user.
5722 if (config
!= NULL
&& spa
->spa_config
) {
5723 *config
= fnvlist_dup(spa
->spa_config
);
5724 fnvlist_add_nvlist(*config
,
5725 ZPOOL_CONFIG_LOAD_INFO
,
5726 spa
->spa_load_info
);
5729 spa_deactivate(spa
);
5730 spa
->spa_last_open_failed
= error
;
5732 mutex_exit(&spa_namespace_lock
);
5738 spa_open_ref(spa
, tag
);
5741 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5744 * If we've recovered the pool, pass back any information we
5745 * gathered while doing the load.
5747 if (state
== SPA_LOAD_RECOVER
&& config
!= NULL
) {
5748 fnvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
5749 spa
->spa_load_info
);
5753 spa
->spa_last_open_failed
= 0;
5754 spa
->spa_last_ubsync_txg
= 0;
5755 spa
->spa_load_txg
= 0;
5756 mutex_exit(&spa_namespace_lock
);
5760 zvol_create_minors_recursive(spa_name(spa
));
5768 spa_open_rewind(const char *name
, spa_t
**spapp
, const void *tag
,
5769 nvlist_t
*policy
, nvlist_t
**config
)
5771 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
5775 spa_open(const char *name
, spa_t
**spapp
, const void *tag
)
5777 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
5781 * Lookup the given spa_t, incrementing the inject count in the process,
5782 * preventing it from being exported or destroyed.
5785 spa_inject_addref(char *name
)
5789 mutex_enter(&spa_namespace_lock
);
5790 if ((spa
= spa_lookup(name
)) == NULL
) {
5791 mutex_exit(&spa_namespace_lock
);
5794 spa
->spa_inject_ref
++;
5795 mutex_exit(&spa_namespace_lock
);
5801 spa_inject_delref(spa_t
*spa
)
5803 mutex_enter(&spa_namespace_lock
);
5804 spa
->spa_inject_ref
--;
5805 mutex_exit(&spa_namespace_lock
);
5809 * Add spares device information to the nvlist.
5812 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
5822 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5824 if (spa
->spa_spares
.sav_count
== 0)
5827 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
5828 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5829 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
5831 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5832 (const nvlist_t
* const *)spares
, nspares
);
5833 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5834 &spares
, &nspares
));
5837 * Go through and find any spares which have since been
5838 * repurposed as an active spare. If this is the case, update
5839 * their status appropriately.
5841 for (i
= 0; i
< nspares
; i
++) {
5842 guid
= fnvlist_lookup_uint64(spares
[i
],
5844 VERIFY0(nvlist_lookup_uint64_array(spares
[i
],
5845 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
));
5846 if (spa_spare_exists(guid
, &pool
, NULL
) &&
5848 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
5849 vs
->vs_aux
= VDEV_AUX_SPARED
;
5852 spa
->spa_spares
.sav_vdevs
[i
]->vdev_state
;
5859 * Add l2cache device information to the nvlist, including vdev stats.
5862 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
5865 uint_t i
, j
, nl2cache
;
5872 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5874 if (spa
->spa_l2cache
.sav_count
== 0)
5877 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
5878 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5879 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
5880 if (nl2cache
!= 0) {
5881 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5882 (const nvlist_t
* const *)l2cache
, nl2cache
);
5883 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5884 &l2cache
, &nl2cache
));
5887 * Update level 2 cache device stats.
5890 for (i
= 0; i
< nl2cache
; i
++) {
5891 guid
= fnvlist_lookup_uint64(l2cache
[i
],
5895 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
5897 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
5898 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
5904 VERIFY0(nvlist_lookup_uint64_array(l2cache
[i
],
5905 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
));
5906 vdev_get_stats(vd
, vs
);
5907 vdev_config_generate_stats(vd
, l2cache
[i
]);
5914 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
5919 if (spa
->spa_feat_for_read_obj
!= 0) {
5920 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
5921 spa
->spa_feat_for_read_obj
);
5922 zap_cursor_retrieve(&zc
, &za
) == 0;
5923 zap_cursor_advance(&zc
)) {
5924 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
5925 za
.za_num_integers
== 1);
5926 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
5927 za
.za_first_integer
));
5929 zap_cursor_fini(&zc
);
5932 if (spa
->spa_feat_for_write_obj
!= 0) {
5933 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
5934 spa
->spa_feat_for_write_obj
);
5935 zap_cursor_retrieve(&zc
, &za
) == 0;
5936 zap_cursor_advance(&zc
)) {
5937 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
5938 za
.za_num_integers
== 1);
5939 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
5940 za
.za_first_integer
));
5942 zap_cursor_fini(&zc
);
5947 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
5951 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5952 zfeature_info_t feature
= spa_feature_table
[i
];
5955 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
5958 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
5963 * Store a list of pool features and their reference counts in the
5966 * The first time this is called on a spa, allocate a new nvlist, fetch
5967 * the pool features and reference counts from disk, then save the list
5968 * in the spa. In subsequent calls on the same spa use the saved nvlist
5969 * and refresh its values from the cached reference counts. This
5970 * ensures we don't block here on I/O on a suspended pool so 'zpool
5971 * clear' can resume the pool.
5974 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
5978 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5980 mutex_enter(&spa
->spa_feat_stats_lock
);
5981 features
= spa
->spa_feat_stats
;
5983 if (features
!= NULL
) {
5984 spa_feature_stats_from_cache(spa
, features
);
5986 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
5987 spa
->spa_feat_stats
= features
;
5988 spa_feature_stats_from_disk(spa
, features
);
5991 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
5994 mutex_exit(&spa
->spa_feat_stats_lock
);
5998 spa_get_stats(const char *name
, nvlist_t
**config
,
5999 char *altroot
, size_t buflen
)
6005 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
6009 * This still leaves a window of inconsistency where the spares
6010 * or l2cache devices could change and the config would be
6011 * self-inconsistent.
6013 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6015 if (*config
!= NULL
) {
6016 uint64_t loadtimes
[2];
6018 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
6019 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
6020 fnvlist_add_uint64_array(*config
,
6021 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2);
6023 fnvlist_add_uint64(*config
,
6024 ZPOOL_CONFIG_ERRCOUNT
,
6025 spa_approx_errlog_size(spa
));
6027 if (spa_suspended(spa
)) {
6028 fnvlist_add_uint64(*config
,
6029 ZPOOL_CONFIG_SUSPENDED
,
6031 fnvlist_add_uint64(*config
,
6032 ZPOOL_CONFIG_SUSPENDED_REASON
,
6033 spa
->spa_suspended
);
6036 spa_add_spares(spa
, *config
);
6037 spa_add_l2cache(spa
, *config
);
6038 spa_add_feature_stats(spa
, *config
);
6043 * We want to get the alternate root even for faulted pools, so we cheat
6044 * and call spa_lookup() directly.
6048 mutex_enter(&spa_namespace_lock
);
6049 spa
= spa_lookup(name
);
6051 spa_altroot(spa
, altroot
, buflen
);
6055 mutex_exit(&spa_namespace_lock
);
6057 spa_altroot(spa
, altroot
, buflen
);
6062 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6063 spa_close(spa
, FTAG
);
6070 * Validate that the auxiliary device array is well formed. We must have an
6071 * array of nvlists, each which describes a valid leaf vdev. If this is an
6072 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
6073 * specified, as long as they are well-formed.
6076 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
6077 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
6078 vdev_labeltype_t label
)
6085 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
6088 * It's acceptable to have no devs specified.
6090 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
6094 return (SET_ERROR(EINVAL
));
6097 * Make sure the pool is formatted with a version that supports this
6100 if (spa_version(spa
) < version
)
6101 return (SET_ERROR(ENOTSUP
));
6104 * Set the pending device list so we correctly handle device in-use
6107 sav
->sav_pending
= dev
;
6108 sav
->sav_npending
= ndev
;
6110 for (i
= 0; i
< ndev
; i
++) {
6111 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
6115 if (!vd
->vdev_ops
->vdev_op_leaf
) {
6117 error
= SET_ERROR(EINVAL
);
6123 if ((error
= vdev_open(vd
)) == 0 &&
6124 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
6125 fnvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
6132 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
6139 sav
->sav_pending
= NULL
;
6140 sav
->sav_npending
= 0;
6145 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
6149 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
6151 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
6152 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
6153 VDEV_LABEL_SPARE
)) != 0) {
6157 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
6158 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
6159 VDEV_LABEL_L2CACHE
));
6163 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
6168 if (sav
->sav_config
!= NULL
) {
6174 * Generate new dev list by concatenating with the
6177 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
6178 &olddevs
, &oldndevs
));
6180 newdevs
= kmem_alloc(sizeof (void *) *
6181 (ndevs
+ oldndevs
), KM_SLEEP
);
6182 for (i
= 0; i
< oldndevs
; i
++)
6183 newdevs
[i
] = fnvlist_dup(olddevs
[i
]);
6184 for (i
= 0; i
< ndevs
; i
++)
6185 newdevs
[i
+ oldndevs
] = fnvlist_dup(devs
[i
]);
6187 fnvlist_remove(sav
->sav_config
, config
);
6189 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
6190 (const nvlist_t
* const *)newdevs
, ndevs
+ oldndevs
);
6191 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
6192 nvlist_free(newdevs
[i
]);
6193 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
6196 * Generate a new dev list.
6198 sav
->sav_config
= fnvlist_alloc();
6199 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
6200 (const nvlist_t
* const *)devs
, ndevs
);
6205 * Stop and drop level 2 ARC devices
6208 spa_l2cache_drop(spa_t
*spa
)
6212 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
6214 for (i
= 0; i
< sav
->sav_count
; i
++) {
6217 vd
= sav
->sav_vdevs
[i
];
6220 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
6221 pool
!= 0ULL && l2arc_vdev_present(vd
))
6222 l2arc_remove_vdev(vd
);
6227 * Verify encryption parameters for spa creation. If we are encrypting, we must
6228 * have the encryption feature flag enabled.
6231 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
6232 boolean_t has_encryption
)
6234 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
6235 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
6237 return (SET_ERROR(ENOTSUP
));
6239 return (dmu_objset_create_crypt_check(NULL
, dcp
, NULL
));
6246 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
6247 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
6250 const char *altroot
= NULL
;
6255 uint64_t txg
= TXG_INITIAL
;
6256 nvlist_t
**spares
, **l2cache
;
6257 uint_t nspares
, nl2cache
;
6258 uint64_t version
, obj
, ndraid
= 0;
6259 boolean_t has_features
;
6260 boolean_t has_encryption
;
6261 boolean_t has_allocclass
;
6263 const char *feat_name
;
6264 const char *poolname
;
6267 if (props
== NULL
||
6268 nvlist_lookup_string(props
, "tname", &poolname
) != 0)
6269 poolname
= (char *)pool
;
6272 * If this pool already exists, return failure.
6274 mutex_enter(&spa_namespace_lock
);
6275 if (spa_lookup(poolname
) != NULL
) {
6276 mutex_exit(&spa_namespace_lock
);
6277 return (SET_ERROR(EEXIST
));
6281 * Allocate a new spa_t structure.
6283 nvl
= fnvlist_alloc();
6284 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
6285 (void) nvlist_lookup_string(props
,
6286 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6287 spa
= spa_add(poolname
, nvl
, altroot
);
6289 spa_activate(spa
, spa_mode_global
);
6291 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
6292 spa_deactivate(spa
);
6294 mutex_exit(&spa_namespace_lock
);
6299 * Temporary pool names should never be written to disk.
6301 if (poolname
!= pool
)
6302 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
6304 has_features
= B_FALSE
;
6305 has_encryption
= B_FALSE
;
6306 has_allocclass
= B_FALSE
;
6307 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
6308 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
6309 if (zpool_prop_feature(nvpair_name(elem
))) {
6310 has_features
= B_TRUE
;
6312 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
6313 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
6314 if (feat
== SPA_FEATURE_ENCRYPTION
)
6315 has_encryption
= B_TRUE
;
6316 if (feat
== SPA_FEATURE_ALLOCATION_CLASSES
)
6317 has_allocclass
= B_TRUE
;
6321 /* verify encryption params, if they were provided */
6323 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
6325 spa_deactivate(spa
);
6327 mutex_exit(&spa_namespace_lock
);
6331 if (!has_allocclass
&& zfs_special_devs(nvroot
, NULL
)) {
6332 spa_deactivate(spa
);
6334 mutex_exit(&spa_namespace_lock
);
6338 if (has_features
|| nvlist_lookup_uint64(props
,
6339 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
6340 version
= SPA_VERSION
;
6342 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6344 spa
->spa_first_txg
= txg
;
6345 spa
->spa_uberblock
.ub_txg
= txg
- 1;
6346 spa
->spa_uberblock
.ub_version
= version
;
6347 spa
->spa_ubsync
= spa
->spa_uberblock
;
6348 spa
->spa_load_state
= SPA_LOAD_CREATE
;
6349 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
6350 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
6351 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
6352 spa
->spa_indirect_vdevs_loaded
= B_TRUE
;
6355 * Create "The Godfather" zio to hold all async IOs
6357 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
6359 for (int i
= 0; i
< max_ncpus
; i
++) {
6360 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
6361 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
6362 ZIO_FLAG_GODFATHER
);
6366 * Create the root vdev.
6368 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6370 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
6372 ASSERT(error
!= 0 || rvd
!= NULL
);
6373 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
6375 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
6376 error
= SET_ERROR(EINVAL
);
6379 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
6380 (error
= vdev_draid_spare_create(nvroot
, rvd
, &ndraid
, 0)) == 0 &&
6381 (error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) == 0) {
6383 * instantiate the metaslab groups (this will dirty the vdevs)
6384 * we can no longer error exit past this point
6386 for (int c
= 0; error
== 0 && c
< rvd
->vdev_children
; c
++) {
6387 vdev_t
*vd
= rvd
->vdev_child
[c
];
6389 vdev_metaslab_set_size(vd
);
6390 vdev_expand(vd
, txg
);
6394 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6398 spa_deactivate(spa
);
6400 mutex_exit(&spa_namespace_lock
);
6405 * Get the list of spares, if specified.
6407 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6408 &spares
, &nspares
) == 0) {
6409 spa
->spa_spares
.sav_config
= fnvlist_alloc();
6410 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6411 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
6413 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6414 spa_load_spares(spa
);
6415 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6416 spa
->spa_spares
.sav_sync
= B_TRUE
;
6420 * Get the list of level 2 cache devices, if specified.
6422 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6423 &l2cache
, &nl2cache
) == 0) {
6424 VERIFY0(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
6425 NV_UNIQUE_NAME
, KM_SLEEP
));
6426 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6427 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
6429 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6430 spa_load_l2cache(spa
);
6431 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6432 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6435 spa
->spa_is_initializing
= B_TRUE
;
6436 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
6437 spa
->spa_is_initializing
= B_FALSE
;
6440 * Create DDTs (dedup tables).
6444 * Create BRT table and BRT table object.
6448 spa_update_dspace(spa
);
6450 tx
= dmu_tx_create_assigned(dp
, txg
);
6453 * Create the pool's history object.
6455 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
6456 spa_history_create_obj(spa
, tx
);
6458 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
6459 spa_history_log_version(spa
, "create", tx
);
6462 * Create the pool config object.
6464 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6465 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
6466 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
6468 if (zap_add(spa
->spa_meta_objset
,
6469 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
6470 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
6471 cmn_err(CE_PANIC
, "failed to add pool config");
6474 if (zap_add(spa
->spa_meta_objset
,
6475 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
6476 sizeof (uint64_t), 1, &version
, tx
) != 0) {
6477 cmn_err(CE_PANIC
, "failed to add pool version");
6480 /* Newly created pools with the right version are always deflated. */
6481 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6482 spa
->spa_deflate
= TRUE
;
6483 if (zap_add(spa
->spa_meta_objset
,
6484 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6485 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
6486 cmn_err(CE_PANIC
, "failed to add deflate");
6491 * Create the deferred-free bpobj. Turn off compression
6492 * because sync-to-convergence takes longer if the blocksize
6495 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
6496 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
6497 ZIO_COMPRESS_OFF
, tx
);
6498 if (zap_add(spa
->spa_meta_objset
,
6499 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
6500 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
6501 cmn_err(CE_PANIC
, "failed to add bpobj");
6503 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
6504 spa
->spa_meta_objset
, obj
));
6507 * Generate some random noise for salted checksums to operate on.
6509 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
6510 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
6513 * Set pool properties.
6515 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
6516 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
6517 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
6518 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
6519 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
6520 spa
->spa_autotrim
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM
);
6522 if (props
!= NULL
) {
6523 spa_configfile_set(spa
, props
, B_FALSE
);
6524 spa_sync_props(props
, tx
);
6527 for (int i
= 0; i
< ndraid
; i
++)
6528 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
6532 spa
->spa_sync_on
= B_TRUE
;
6534 mmp_thread_start(spa
);
6535 txg_wait_synced(dp
, txg
);
6537 spa_spawn_aux_threads(spa
);
6539 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
6542 * Don't count references from objsets that are already closed
6543 * and are making their way through the eviction process.
6545 spa_evicting_os_wait(spa
);
6546 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
6547 spa
->spa_load_state
= SPA_LOAD_NONE
;
6551 mutex_exit(&spa_namespace_lock
);
6557 * Import a non-root pool into the system.
6560 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
6563 const char *altroot
= NULL
;
6564 spa_load_state_t state
= SPA_LOAD_IMPORT
;
6565 zpool_load_policy_t policy
;
6566 spa_mode_t mode
= spa_mode_global
;
6567 uint64_t readonly
= B_FALSE
;
6570 nvlist_t
**spares
, **l2cache
;
6571 uint_t nspares
, nl2cache
;
6574 * If a pool with this name exists, return failure.
6576 mutex_enter(&spa_namespace_lock
);
6577 if (spa_lookup(pool
) != NULL
) {
6578 mutex_exit(&spa_namespace_lock
);
6579 return (SET_ERROR(EEXIST
));
6583 * Create and initialize the spa structure.
6585 (void) nvlist_lookup_string(props
,
6586 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6587 (void) nvlist_lookup_uint64(props
,
6588 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
6590 mode
= SPA_MODE_READ
;
6591 spa
= spa_add(pool
, config
, altroot
);
6592 spa
->spa_import_flags
= flags
;
6595 * Verbatim import - Take a pool and insert it into the namespace
6596 * as if it had been loaded at boot.
6598 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
6600 spa_configfile_set(spa
, props
, B_FALSE
);
6602 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_FALSE
);
6603 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6604 zfs_dbgmsg("spa_import: verbatim import of %s", pool
);
6605 mutex_exit(&spa_namespace_lock
);
6609 spa_activate(spa
, mode
);
6612 * Don't start async tasks until we know everything is healthy.
6614 spa_async_suspend(spa
);
6616 zpool_get_load_policy(config
, &policy
);
6617 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
6618 state
= SPA_LOAD_RECOVER
;
6620 spa
->spa_config_source
= SPA_CONFIG_SRC_TRYIMPORT
;
6622 if (state
!= SPA_LOAD_RECOVER
) {
6623 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
6624 zfs_dbgmsg("spa_import: importing %s", pool
);
6626 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
6627 "(RECOVERY MODE)", pool
, (longlong_t
)policy
.zlp_txg
);
6629 error
= spa_load_best(spa
, state
, policy
.zlp_txg
, policy
.zlp_rewind
);
6632 * Propagate anything learned while loading the pool and pass it
6633 * back to caller (i.e. rewind info, missing devices, etc).
6635 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, spa
->spa_load_info
);
6637 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6639 * Toss any existing sparelist, as it doesn't have any validity
6640 * anymore, and conflicts with spa_has_spare().
6642 if (spa
->spa_spares
.sav_config
) {
6643 nvlist_free(spa
->spa_spares
.sav_config
);
6644 spa
->spa_spares
.sav_config
= NULL
;
6645 spa_load_spares(spa
);
6647 if (spa
->spa_l2cache
.sav_config
) {
6648 nvlist_free(spa
->spa_l2cache
.sav_config
);
6649 spa
->spa_l2cache
.sav_config
= NULL
;
6650 spa_load_l2cache(spa
);
6653 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6654 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6657 spa_configfile_set(spa
, props
, B_FALSE
);
6659 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
6660 (error
= spa_prop_set(spa
, props
)))) {
6662 spa_deactivate(spa
);
6664 mutex_exit(&spa_namespace_lock
);
6668 spa_async_resume(spa
);
6671 * Override any spares and level 2 cache devices as specified by
6672 * the user, as these may have correct device names/devids, etc.
6674 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6675 &spares
, &nspares
) == 0) {
6676 if (spa
->spa_spares
.sav_config
)
6677 fnvlist_remove(spa
->spa_spares
.sav_config
,
6678 ZPOOL_CONFIG_SPARES
);
6680 spa
->spa_spares
.sav_config
= fnvlist_alloc();
6681 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6682 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
6684 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6685 spa_load_spares(spa
);
6686 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6687 spa
->spa_spares
.sav_sync
= B_TRUE
;
6689 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6690 &l2cache
, &nl2cache
) == 0) {
6691 if (spa
->spa_l2cache
.sav_config
)
6692 fnvlist_remove(spa
->spa_l2cache
.sav_config
,
6693 ZPOOL_CONFIG_L2CACHE
);
6695 spa
->spa_l2cache
.sav_config
= fnvlist_alloc();
6696 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6697 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
6699 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6700 spa_load_l2cache(spa
);
6701 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6702 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6706 * Check for any removed devices.
6708 if (spa
->spa_autoreplace
) {
6709 spa_aux_check_removed(&spa
->spa_spares
);
6710 spa_aux_check_removed(&spa
->spa_l2cache
);
6713 if (spa_writeable(spa
)) {
6715 * Update the config cache to include the newly-imported pool.
6717 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6721 * It's possible that the pool was expanded while it was exported.
6722 * We kick off an async task to handle this for us.
6724 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
6726 spa_history_log_version(spa
, "import", NULL
);
6728 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6730 mutex_exit(&spa_namespace_lock
);
6732 zvol_create_minors_recursive(pool
);
6740 spa_tryimport(nvlist_t
*tryconfig
)
6742 nvlist_t
*config
= NULL
;
6743 const char *poolname
, *cachefile
;
6747 zpool_load_policy_t policy
;
6749 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
6752 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
6756 * Create and initialize the spa structure.
6758 mutex_enter(&spa_namespace_lock
);
6759 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
6760 spa_activate(spa
, SPA_MODE_READ
);
6763 * Rewind pool if a max txg was provided.
6765 zpool_get_load_policy(spa
->spa_config
, &policy
);
6766 if (policy
.zlp_txg
!= UINT64_MAX
) {
6767 spa
->spa_load_max_txg
= policy
.zlp_txg
;
6768 spa
->spa_extreme_rewind
= B_TRUE
;
6769 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
6770 poolname
, (longlong_t
)policy
.zlp_txg
);
6772 zfs_dbgmsg("spa_tryimport: importing %s", poolname
);
6775 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_CACHEFILE
, &cachefile
)
6777 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile
);
6778 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
6780 spa
->spa_config_source
= SPA_CONFIG_SRC_SCAN
;
6784 * spa_import() relies on a pool config fetched by spa_try_import()
6785 * for spare/cache devices. Import flags are not passed to
6786 * spa_tryimport(), which makes it return early due to a missing log
6787 * device and missing retrieving the cache device and spare eventually.
6788 * Passing ZFS_IMPORT_MISSING_LOG to spa_tryimport() makes it fetch
6789 * the correct configuration regardless of the missing log device.
6791 spa
->spa_import_flags
|= ZFS_IMPORT_MISSING_LOG
;
6793 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
);
6796 * If 'tryconfig' was at least parsable, return the current config.
6798 if (spa
->spa_root_vdev
!= NULL
) {
6799 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
6800 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, poolname
);
6801 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, state
);
6802 fnvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
6803 spa
->spa_uberblock
.ub_timestamp
);
6804 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
6805 spa
->spa_load_info
);
6806 fnvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
6810 * If the bootfs property exists on this pool then we
6811 * copy it out so that external consumers can tell which
6812 * pools are bootable.
6814 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
6815 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6818 * We have to play games with the name since the
6819 * pool was opened as TRYIMPORT_NAME.
6821 if (dsl_dsobj_to_dsname(spa_name(spa
),
6822 spa
->spa_bootfs
, tmpname
) == 0) {
6826 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6828 cp
= strchr(tmpname
, '/');
6830 (void) strlcpy(dsname
, tmpname
,
6833 (void) snprintf(dsname
, MAXPATHLEN
,
6834 "%s/%s", poolname
, ++cp
);
6836 fnvlist_add_string(config
, ZPOOL_CONFIG_BOOTFS
,
6838 kmem_free(dsname
, MAXPATHLEN
);
6840 kmem_free(tmpname
, MAXPATHLEN
);
6844 * Add the list of hot spares and level 2 cache devices.
6846 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6847 spa_add_spares(spa
, config
);
6848 spa_add_l2cache(spa
, config
);
6849 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6853 spa_deactivate(spa
);
6855 mutex_exit(&spa_namespace_lock
);
6861 * Pool export/destroy
6863 * The act of destroying or exporting a pool is very simple. We make sure there
6864 * is no more pending I/O and any references to the pool are gone. Then, we
6865 * update the pool state and sync all the labels to disk, removing the
6866 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
6867 * we don't sync the labels or remove the configuration cache.
6870 spa_export_common(const char *pool
, int new_state
, nvlist_t
**oldconfig
,
6871 boolean_t force
, boolean_t hardforce
)
6879 if (!(spa_mode_global
& SPA_MODE_WRITE
))
6880 return (SET_ERROR(EROFS
));
6882 mutex_enter(&spa_namespace_lock
);
6883 if ((spa
= spa_lookup(pool
)) == NULL
) {
6884 mutex_exit(&spa_namespace_lock
);
6885 return (SET_ERROR(ENOENT
));
6888 if (spa
->spa_is_exporting
) {
6889 /* the pool is being exported by another thread */
6890 mutex_exit(&spa_namespace_lock
);
6891 return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS
));
6893 spa
->spa_is_exporting
= B_TRUE
;
6896 * Put a hold on the pool, drop the namespace lock, stop async tasks,
6897 * reacquire the namespace lock, and see if we can export.
6899 spa_open_ref(spa
, FTAG
);
6900 mutex_exit(&spa_namespace_lock
);
6901 spa_async_suspend(spa
);
6902 if (spa
->spa_zvol_taskq
) {
6903 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
6904 taskq_wait(spa
->spa_zvol_taskq
);
6906 mutex_enter(&spa_namespace_lock
);
6907 spa_close(spa
, FTAG
);
6909 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
6912 * The pool will be in core if it's openable, in which case we can
6913 * modify its state. Objsets may be open only because they're dirty,
6914 * so we have to force it to sync before checking spa_refcnt.
6916 if (spa
->spa_sync_on
) {
6917 txg_wait_synced(spa
->spa_dsl_pool
, 0);
6918 spa_evicting_os_wait(spa
);
6922 * A pool cannot be exported or destroyed if there are active
6923 * references. If we are resetting a pool, allow references by
6924 * fault injection handlers.
6926 if (!spa_refcount_zero(spa
) || (spa
->spa_inject_ref
!= 0)) {
6927 error
= SET_ERROR(EBUSY
);
6931 if (spa
->spa_sync_on
) {
6932 vdev_t
*rvd
= spa
->spa_root_vdev
;
6934 * A pool cannot be exported if it has an active shared spare.
6935 * This is to prevent other pools stealing the active spare
6936 * from an exported pool. At user's own will, such pool can
6937 * be forcedly exported.
6939 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
6940 spa_has_active_shared_spare(spa
)) {
6941 error
= SET_ERROR(EXDEV
);
6946 * We're about to export or destroy this pool. Make sure
6947 * we stop all initialization and trim activity here before
6948 * we set the spa_final_txg. This will ensure that all
6949 * dirty data resulting from the initialization is
6950 * committed to disk before we unload the pool.
6952 vdev_initialize_stop_all(rvd
, VDEV_INITIALIZE_ACTIVE
);
6953 vdev_trim_stop_all(rvd
, VDEV_TRIM_ACTIVE
);
6954 vdev_autotrim_stop_all(spa
);
6955 vdev_rebuild_stop_all(spa
);
6958 * We want this to be reflected on every label,
6959 * so mark them all dirty. spa_unload() will do the
6960 * final sync that pushes these changes out.
6962 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
6963 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6964 spa
->spa_state
= new_state
;
6965 vdev_config_dirty(rvd
);
6966 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6970 * If the log space map feature is enabled and the pool is
6971 * getting exported (but not destroyed), we want to spend some
6972 * time flushing as many metaslabs as we can in an attempt to
6973 * destroy log space maps and save import time. This has to be
6974 * done before we set the spa_final_txg, otherwise
6975 * spa_sync() -> spa_flush_metaslabs() may dirty the final TXGs.
6976 * spa_should_flush_logs_on_unload() should be called after
6977 * spa_state has been set to the new_state.
6979 if (spa_should_flush_logs_on_unload(spa
))
6980 spa_unload_log_sm_flush_all(spa
);
6982 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
6983 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6984 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
6986 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6993 if (new_state
== POOL_STATE_DESTROYED
)
6994 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
6995 else if (new_state
== POOL_STATE_EXPORTED
)
6996 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
6998 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
7000 spa_deactivate(spa
);
7003 if (oldconfig
&& spa
->spa_config
)
7004 *oldconfig
= fnvlist_dup(spa
->spa_config
);
7006 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
7008 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
7012 * If spa_remove() is not called for this spa_t and
7013 * there is any possibility that it can be reused,
7014 * we make sure to reset the exporting flag.
7016 spa
->spa_is_exporting
= B_FALSE
;
7019 mutex_exit(&spa_namespace_lock
);
7023 spa
->spa_is_exporting
= B_FALSE
;
7024 spa_async_resume(spa
);
7025 mutex_exit(&spa_namespace_lock
);
7030 * Destroy a storage pool.
7033 spa_destroy(const char *pool
)
7035 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
7040 * Export a storage pool.
7043 spa_export(const char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
7044 boolean_t hardforce
)
7046 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
7051 * Similar to spa_export(), this unloads the spa_t without actually removing it
7052 * from the namespace in any way.
7055 spa_reset(const char *pool
)
7057 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
7062 * ==========================================================================
7063 * Device manipulation
7064 * ==========================================================================
7068 * This is called as a synctask to increment the draid feature flag
7071 spa_draid_feature_incr(void *arg
, dmu_tx_t
*tx
)
7073 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
7074 int draid
= (int)(uintptr_t)arg
;
7076 for (int c
= 0; c
< draid
; c
++)
7077 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
7081 * Add a device to a storage pool.
7084 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
7086 uint64_t txg
, ndraid
= 0;
7088 vdev_t
*rvd
= spa
->spa_root_vdev
;
7090 nvlist_t
**spares
, **l2cache
;
7091 uint_t nspares
, nl2cache
;
7093 ASSERT(spa_writeable(spa
));
7095 txg
= spa_vdev_enter(spa
);
7097 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
7098 VDEV_ALLOC_ADD
)) != 0)
7099 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7101 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
7103 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
7107 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
7111 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
7112 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
7114 if (vd
->vdev_children
!= 0 &&
7115 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0) {
7116 return (spa_vdev_exit(spa
, vd
, txg
, error
));
7120 * The virtual dRAID spares must be added after vdev tree is created
7121 * and the vdev guids are generated. The guid of their associated
7122 * dRAID is stored in the config and used when opening the spare.
7124 if ((error
= vdev_draid_spare_create(nvroot
, vd
, &ndraid
,
7125 rvd
->vdev_children
)) == 0) {
7126 if (ndraid
> 0 && nvlist_lookup_nvlist_array(nvroot
,
7127 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) != 0)
7130 return (spa_vdev_exit(spa
, vd
, txg
, error
));
7134 * We must validate the spares and l2cache devices after checking the
7135 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
7137 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
7138 return (spa_vdev_exit(spa
, vd
, txg
, error
));
7141 * If we are in the middle of a device removal, we can only add
7142 * devices which match the existing devices in the pool.
7143 * If we are in the middle of a removal, or have some indirect
7144 * vdevs, we can not add raidz or dRAID top levels.
7146 if (spa
->spa_vdev_removal
!= NULL
||
7147 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
7148 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7149 tvd
= vd
->vdev_child
[c
];
7150 if (spa
->spa_vdev_removal
!= NULL
&&
7151 tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
7152 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
7154 /* Fail if top level vdev is raidz or a dRAID */
7155 if (vdev_get_nparity(tvd
) != 0)
7156 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
7159 * Need the top level mirror to be
7160 * a mirror of leaf vdevs only
7162 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
7163 for (uint64_t cid
= 0;
7164 cid
< tvd
->vdev_children
; cid
++) {
7165 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
7166 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
7167 return (spa_vdev_exit(spa
, vd
,
7175 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7176 tvd
= vd
->vdev_child
[c
];
7177 vdev_remove_child(vd
, tvd
);
7178 tvd
->vdev_id
= rvd
->vdev_children
;
7179 vdev_add_child(rvd
, tvd
);
7180 vdev_config_dirty(tvd
);
7184 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
7185 ZPOOL_CONFIG_SPARES
);
7186 spa_load_spares(spa
);
7187 spa
->spa_spares
.sav_sync
= B_TRUE
;
7190 if (nl2cache
!= 0) {
7191 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
7192 ZPOOL_CONFIG_L2CACHE
);
7193 spa_load_l2cache(spa
);
7194 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
7198 * We can't increment a feature while holding spa_vdev so we
7199 * have to do it in a synctask.
7204 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
7205 dsl_sync_task_nowait(spa
->spa_dsl_pool
, spa_draid_feature_incr
,
7206 (void *)(uintptr_t)ndraid
, tx
);
7211 * We have to be careful when adding new vdevs to an existing pool.
7212 * If other threads start allocating from these vdevs before we
7213 * sync the config cache, and we lose power, then upon reboot we may
7214 * fail to open the pool because there are DVAs that the config cache
7215 * can't translate. Therefore, we first add the vdevs without
7216 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
7217 * and then let spa_config_update() initialize the new metaslabs.
7219 * spa_load() checks for added-but-not-initialized vdevs, so that
7220 * if we lose power at any point in this sequence, the remaining
7221 * steps will be completed the next time we load the pool.
7223 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
7225 mutex_enter(&spa_namespace_lock
);
7226 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
7227 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
7228 mutex_exit(&spa_namespace_lock
);
7234 * Attach a device to a vdev specified by its guid. The vdev type can be
7235 * a mirror, a raidz, or a leaf device that is also a top-level (e.g. a
7236 * single device). When the vdev is a single device, a mirror vdev will be
7237 * automatically inserted.
7239 * If 'replacing' is specified, the new device is intended to replace the
7240 * existing device; in this case the two devices are made into their own
7241 * mirror using the 'replacing' vdev, which is functionally identical to
7242 * the mirror vdev (it actually reuses all the same ops) but has a few
7243 * extra rules: you can't attach to it after it's been created, and upon
7244 * completion of resilvering, the first disk (the one being replaced)
7245 * is automatically detached.
7247 * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild)
7248 * should be performed instead of traditional healing reconstruction. From
7249 * an administrators perspective these are both resilver operations.
7252 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
,
7255 uint64_t txg
, dtl_max_txg
;
7256 vdev_t
*rvd
= spa
->spa_root_vdev
;
7257 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
7259 char *oldvdpath
, *newvdpath
;
7260 int newvd_isspare
= B_FALSE
;
7263 ASSERT(spa_writeable(spa
));
7265 txg
= spa_vdev_enter(spa
);
7267 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7269 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7270 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7271 error
= (spa_has_checkpoint(spa
)) ?
7272 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7273 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7277 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_DEVICE_REBUILD
))
7278 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7280 if (dsl_scan_resilvering(spa_get_dsl(spa
)) ||
7281 dsl_scan_resilver_scheduled(spa_get_dsl(spa
))) {
7282 return (spa_vdev_exit(spa
, NULL
, txg
,
7283 ZFS_ERR_RESILVER_IN_PROGRESS
));
7286 if (vdev_rebuild_active(rvd
))
7287 return (spa_vdev_exit(spa
, NULL
, txg
,
7288 ZFS_ERR_REBUILD_IN_PROGRESS
));
7291 if (spa
->spa_vdev_removal
!= NULL
) {
7292 return (spa_vdev_exit(spa
, NULL
, txg
,
7293 ZFS_ERR_DEVRM_IN_PROGRESS
));
7297 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
7299 boolean_t raidz
= oldvd
->vdev_ops
== &vdev_raidz_ops
;
7302 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_RAIDZ_EXPANSION
))
7303 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7306 * Can't expand a raidz while prior expand is in progress.
7308 if (spa
->spa_raidz_expand
!= NULL
) {
7309 return (spa_vdev_exit(spa
, NULL
, txg
,
7310 ZFS_ERR_RAIDZ_EXPAND_IN_PROGRESS
));
7312 } else if (!oldvd
->vdev_ops
->vdev_op_leaf
) {
7313 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7319 pvd
= oldvd
->vdev_parent
;
7321 if (spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
7322 VDEV_ALLOC_ATTACH
) != 0)
7323 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7325 if (newrootvd
->vdev_children
!= 1)
7326 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
7328 newvd
= newrootvd
->vdev_child
[0];
7330 if (!newvd
->vdev_ops
->vdev_op_leaf
)
7331 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
7333 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
7334 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
7337 * log, dedup and special vdevs should not be replaced by spares.
7339 if ((oldvd
->vdev_top
->vdev_alloc_bias
!= VDEV_BIAS_NONE
||
7340 oldvd
->vdev_top
->vdev_islog
) && newvd
->vdev_isspare
) {
7341 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7345 * A dRAID spare can only replace a child of its parent dRAID vdev.
7347 if (newvd
->vdev_ops
== &vdev_draid_spare_ops
&&
7348 oldvd
->vdev_top
!= vdev_draid_spare_get_parent(newvd
)) {
7349 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7354 * For rebuilds, the top vdev must support reconstruction
7355 * using only space maps. This means the only allowable
7356 * vdevs types are the root vdev, a mirror, or dRAID.
7359 if (pvd
->vdev_top
!= NULL
)
7360 tvd
= pvd
->vdev_top
;
7362 if (tvd
->vdev_ops
!= &vdev_mirror_ops
&&
7363 tvd
->vdev_ops
!= &vdev_root_ops
&&
7364 tvd
->vdev_ops
!= &vdev_draid_ops
) {
7365 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7371 * For attach, the only allowable parent is a mirror or
7372 * the root vdev. A raidz vdev can be attached to, but
7373 * you cannot attach to a raidz child.
7375 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
7376 pvd
->vdev_ops
!= &vdev_root_ops
&&
7378 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7380 pvops
= &vdev_mirror_ops
;
7383 * Active hot spares can only be replaced by inactive hot
7386 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
7387 oldvd
->vdev_isspare
&&
7388 !spa_has_spare(spa
, newvd
->vdev_guid
))
7389 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7392 * If the source is a hot spare, and the parent isn't already a
7393 * spare, then we want to create a new hot spare. Otherwise, we
7394 * want to create a replacing vdev. The user is not allowed to
7395 * attach to a spared vdev child unless the 'isspare' state is
7396 * the same (spare replaces spare, non-spare replaces
7399 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
7400 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
7401 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7402 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
7403 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
7404 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7407 if (newvd
->vdev_isspare
)
7408 pvops
= &vdev_spare_ops
;
7410 pvops
= &vdev_replacing_ops
;
7414 * Make sure the new device is big enough.
7416 vdev_t
*min_vdev
= raidz
? oldvd
->vdev_child
[0] : oldvd
;
7417 if (newvd
->vdev_asize
< vdev_get_min_asize(min_vdev
))
7418 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
7421 * The new device cannot have a higher alignment requirement
7422 * than the top-level vdev.
7424 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
7425 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7428 * RAIDZ-expansion-specific checks.
7431 if (vdev_raidz_attach_check(newvd
) != 0)
7432 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7435 * Fail early if a child is not healthy or being replaced
7437 for (int i
= 0; i
< oldvd
->vdev_children
; i
++) {
7438 if (vdev_is_dead(oldvd
->vdev_child
[i
]) ||
7439 !oldvd
->vdev_child
[i
]->vdev_ops
->vdev_op_leaf
) {
7440 return (spa_vdev_exit(spa
, newrootvd
, txg
,
7443 /* Also fail if reserved boot area is in-use */
7444 if (vdev_check_boot_reserve(spa
, oldvd
->vdev_child
[i
])
7446 return (spa_vdev_exit(spa
, newrootvd
, txg
,
7454 * Note: oldvdpath is freed by spa_strfree(), but
7455 * kmem_asprintf() is freed by kmem_strfree(), so we have to
7456 * move it to a spa_strdup-ed string.
7458 char *tmp
= kmem_asprintf("raidz%u-%u",
7459 (uint_t
)vdev_get_nparity(oldvd
), (uint_t
)oldvd
->vdev_id
);
7460 oldvdpath
= spa_strdup(tmp
);
7463 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
7465 newvdpath
= spa_strdup(newvd
->vdev_path
);
7468 * If this is an in-place replacement, update oldvd's path and devid
7469 * to make it distinguishable from newvd, and unopenable from now on.
7471 if (strcmp(oldvdpath
, newvdpath
) == 0) {
7472 spa_strfree(oldvd
->vdev_path
);
7473 oldvd
->vdev_path
= kmem_alloc(strlen(newvdpath
) + 5,
7475 (void) sprintf(oldvd
->vdev_path
, "%s/old",
7477 if (oldvd
->vdev_devid
!= NULL
) {
7478 spa_strfree(oldvd
->vdev_devid
);
7479 oldvd
->vdev_devid
= NULL
;
7481 spa_strfree(oldvdpath
);
7482 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
7486 * If the parent is not a mirror, or if we're replacing, insert the new
7487 * mirror/replacing/spare vdev above oldvd.
7489 if (!raidz
&& pvd
->vdev_ops
!= pvops
) {
7490 pvd
= vdev_add_parent(oldvd
, pvops
);
7491 ASSERT(pvd
->vdev_ops
== pvops
);
7492 ASSERT(oldvd
->vdev_parent
== pvd
);
7495 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
7498 * Extract the new device from its root and add it to pvd.
7500 vdev_remove_child(newrootvd
, newvd
);
7501 newvd
->vdev_id
= pvd
->vdev_children
;
7502 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
7503 vdev_add_child(pvd
, newvd
);
7506 * Reevaluate the parent vdev state.
7508 vdev_propagate_state(pvd
);
7510 tvd
= newvd
->vdev_top
;
7511 ASSERT(pvd
->vdev_top
== tvd
);
7512 ASSERT(tvd
->vdev_parent
== rvd
);
7514 vdev_config_dirty(tvd
);
7517 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
7518 * for any dmu_sync-ed blocks. It will propagate upward when
7519 * spa_vdev_exit() calls vdev_dtl_reassess().
7521 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
7525 * Wait for the youngest allocations and frees to sync,
7526 * and then wait for the deferral of those frees to finish.
7528 spa_vdev_config_exit(spa
, NULL
,
7529 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
7531 vdev_initialize_stop_all(tvd
, VDEV_INITIALIZE_ACTIVE
);
7532 vdev_trim_stop_all(tvd
, VDEV_TRIM_ACTIVE
);
7533 vdev_autotrim_stop_wait(tvd
);
7535 dtl_max_txg
= spa_vdev_config_enter(spa
);
7537 tvd
->vdev_rz_expanding
= B_TRUE
;
7539 vdev_dirty_leaves(tvd
, VDD_DTL
, dtl_max_txg
);
7540 vdev_config_dirty(tvd
);
7542 dmu_tx_t
*tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
,
7544 dsl_sync_task_nowait(spa
->spa_dsl_pool
, vdev_raidz_attach_sync
,
7548 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
7549 dtl_max_txg
- TXG_INITIAL
);
7551 if (newvd
->vdev_isspare
) {
7552 spa_spare_activate(newvd
);
7553 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
7556 newvd_isspare
= newvd
->vdev_isspare
;
7559 * Mark newvd's DTL dirty in this txg.
7561 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
7564 * Schedule the resilver or rebuild to restart in the future.
7565 * We do this to ensure that dmu_sync-ed blocks have been
7566 * stitched into the respective datasets.
7569 newvd
->vdev_rebuild_txg
= txg
;
7573 newvd
->vdev_resilver_txg
= txg
;
7575 if (dsl_scan_resilvering(spa_get_dsl(spa
)) &&
7576 spa_feature_is_enabled(spa
,
7577 SPA_FEATURE_RESILVER_DEFER
)) {
7578 vdev_defer_resilver(newvd
);
7580 dsl_scan_restart_resilver(spa
->spa_dsl_pool
,
7586 if (spa
->spa_bootfs
)
7587 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
7589 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
7594 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
7596 spa_history_log_internal(spa
, "vdev attach", NULL
,
7597 "%s vdev=%s %s vdev=%s",
7598 replacing
&& newvd_isspare
? "spare in" :
7599 replacing
? "replace" : "attach", newvdpath
,
7600 replacing
? "for" : "to", oldvdpath
);
7602 spa_strfree(oldvdpath
);
7603 spa_strfree(newvdpath
);
7609 * Detach a device from a mirror or replacing vdev.
7611 * If 'replace_done' is specified, only detach if the parent
7612 * is a replacing or a spare vdev.
7615 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
7619 vdev_t
*rvd __maybe_unused
= spa
->spa_root_vdev
;
7620 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
7621 boolean_t unspare
= B_FALSE
;
7622 uint64_t unspare_guid
= 0;
7625 ASSERT(spa_writeable(spa
));
7627 txg
= spa_vdev_detach_enter(spa
, guid
);
7629 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7632 * Besides being called directly from the userland through the
7633 * ioctl interface, spa_vdev_detach() can be potentially called
7634 * at the end of spa_vdev_resilver_done().
7636 * In the regular case, when we have a checkpoint this shouldn't
7637 * happen as we never empty the DTLs of a vdev during the scrub
7638 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
7639 * should never get here when we have a checkpoint.
7641 * That said, even in a case when we checkpoint the pool exactly
7642 * as spa_vdev_resilver_done() calls this function everything
7643 * should be fine as the resilver will return right away.
7645 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7646 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7647 error
= (spa_has_checkpoint(spa
)) ?
7648 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7649 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7653 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
7655 if (!vd
->vdev_ops
->vdev_op_leaf
)
7656 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7658 pvd
= vd
->vdev_parent
;
7661 * If the parent/child relationship is not as expected, don't do it.
7662 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
7663 * vdev that's replacing B with C. The user's intent in replacing
7664 * is to go from M(A,B) to M(A,C). If the user decides to cancel
7665 * the replace by detaching C, the expected behavior is to end up
7666 * M(A,B). But suppose that right after deciding to detach C,
7667 * the replacement of B completes. We would have M(A,C), and then
7668 * ask to detach C, which would leave us with just A -- not what
7669 * the user wanted. To prevent this, we make sure that the
7670 * parent/child relationship hasn't changed -- in this example,
7671 * that C's parent is still the replacing vdev R.
7673 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
7674 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7677 * Only 'replacing' or 'spare' vdevs can be replaced.
7679 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7680 pvd
->vdev_ops
!= &vdev_spare_ops
)
7681 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7683 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
7684 spa_version(spa
) >= SPA_VERSION_SPARES
);
7687 * Only mirror, replacing, and spare vdevs support detach.
7689 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7690 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
7691 pvd
->vdev_ops
!= &vdev_spare_ops
)
7692 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7695 * If this device has the only valid copy of some data,
7696 * we cannot safely detach it.
7698 if (vdev_dtl_required(vd
))
7699 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7701 ASSERT(pvd
->vdev_children
>= 2);
7704 * If we are detaching the second disk from a replacing vdev, then
7705 * check to see if we changed the original vdev's path to have "/old"
7706 * at the end in spa_vdev_attach(). If so, undo that change now.
7708 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
7709 vd
->vdev_path
!= NULL
) {
7710 size_t len
= strlen(vd
->vdev_path
);
7712 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
7713 cvd
= pvd
->vdev_child
[c
];
7715 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
7718 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
7719 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
7720 spa_strfree(cvd
->vdev_path
);
7721 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
7728 * If we are detaching the original disk from a normal spare, then it
7729 * implies that the spare should become a real disk, and be removed
7730 * from the active spare list for the pool. dRAID spares on the
7731 * other hand are coupled to the pool and thus should never be removed
7732 * from the spares list.
7734 if (pvd
->vdev_ops
== &vdev_spare_ops
&& vd
->vdev_id
== 0) {
7735 vdev_t
*last_cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7737 if (last_cvd
->vdev_isspare
&&
7738 last_cvd
->vdev_ops
!= &vdev_draid_spare_ops
) {
7744 * Erase the disk labels so the disk can be used for other things.
7745 * This must be done after all other error cases are handled,
7746 * but before we disembowel vd (so we can still do I/O to it).
7747 * But if we can't do it, don't treat the error as fatal --
7748 * it may be that the unwritability of the disk is the reason
7749 * it's being detached!
7751 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
7754 * Remove vd from its parent and compact the parent's children.
7756 vdev_remove_child(pvd
, vd
);
7757 vdev_compact_children(pvd
);
7760 * Remember one of the remaining children so we can get tvd below.
7762 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7765 * If we need to remove the remaining child from the list of hot spares,
7766 * do it now, marking the vdev as no longer a spare in the process.
7767 * We must do this before vdev_remove_parent(), because that can
7768 * change the GUID if it creates a new toplevel GUID. For a similar
7769 * reason, we must remove the spare now, in the same txg as the detach;
7770 * otherwise someone could attach a new sibling, change the GUID, and
7771 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
7774 ASSERT(cvd
->vdev_isspare
);
7775 spa_spare_remove(cvd
);
7776 unspare_guid
= cvd
->vdev_guid
;
7777 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
7778 cvd
->vdev_unspare
= B_TRUE
;
7782 * If the parent mirror/replacing vdev only has one child,
7783 * the parent is no longer needed. Remove it from the tree.
7785 if (pvd
->vdev_children
== 1) {
7786 if (pvd
->vdev_ops
== &vdev_spare_ops
)
7787 cvd
->vdev_unspare
= B_FALSE
;
7788 vdev_remove_parent(cvd
);
7792 * We don't set tvd until now because the parent we just removed
7793 * may have been the previous top-level vdev.
7795 tvd
= cvd
->vdev_top
;
7796 ASSERT(tvd
->vdev_parent
== rvd
);
7799 * Reevaluate the parent vdev state.
7801 vdev_propagate_state(cvd
);
7804 * If the 'autoexpand' property is set on the pool then automatically
7805 * try to expand the size of the pool. For example if the device we
7806 * just detached was smaller than the others, it may be possible to
7807 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
7808 * first so that we can obtain the updated sizes of the leaf vdevs.
7810 if (spa
->spa_autoexpand
) {
7812 vdev_expand(tvd
, txg
);
7815 vdev_config_dirty(tvd
);
7818 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
7819 * vd->vdev_detached is set and free vd's DTL object in syncing context.
7820 * But first make sure we're not on any *other* txg's DTL list, to
7821 * prevent vd from being accessed after it's freed.
7823 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
7824 for (int t
= 0; t
< TXG_SIZE
; t
++)
7825 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
7826 vd
->vdev_detached
= B_TRUE
;
7827 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
7829 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
7830 spa_notify_waiters(spa
);
7832 /* hang on to the spa before we release the lock */
7833 spa_open_ref(spa
, FTAG
);
7835 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
7837 spa_history_log_internal(spa
, "detach", NULL
,
7839 spa_strfree(vdpath
);
7842 * If this was the removal of the original device in a hot spare vdev,
7843 * then we want to go through and remove the device from the hot spare
7844 * list of every other pool.
7847 spa_t
*altspa
= NULL
;
7849 mutex_enter(&spa_namespace_lock
);
7850 while ((altspa
= spa_next(altspa
)) != NULL
) {
7851 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
7855 spa_open_ref(altspa
, FTAG
);
7856 mutex_exit(&spa_namespace_lock
);
7857 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
7858 mutex_enter(&spa_namespace_lock
);
7859 spa_close(altspa
, FTAG
);
7861 mutex_exit(&spa_namespace_lock
);
7863 /* search the rest of the vdevs for spares to remove */
7864 spa_vdev_resilver_done(spa
);
7867 /* all done with the spa; OK to release */
7868 mutex_enter(&spa_namespace_lock
);
7869 spa_close(spa
, FTAG
);
7870 mutex_exit(&spa_namespace_lock
);
7876 spa_vdev_initialize_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
7879 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7881 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7883 /* Look up vdev and ensure it's a leaf. */
7884 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7885 if (vd
== NULL
|| vd
->vdev_detached
) {
7886 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7887 return (SET_ERROR(ENODEV
));
7888 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
7889 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7890 return (SET_ERROR(EINVAL
));
7891 } else if (!vdev_writeable(vd
)) {
7892 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7893 return (SET_ERROR(EROFS
));
7895 mutex_enter(&vd
->vdev_initialize_lock
);
7896 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7899 * When we activate an initialize action we check to see
7900 * if the vdev_initialize_thread is NULL. We do this instead
7901 * of using the vdev_initialize_state since there might be
7902 * a previous initialization process which has completed but
7903 * the thread is not exited.
7905 if (cmd_type
== POOL_INITIALIZE_START
&&
7906 (vd
->vdev_initialize_thread
!= NULL
||
7907 vd
->vdev_top
->vdev_removing
|| vd
->vdev_top
->vdev_rz_expanding
)) {
7908 mutex_exit(&vd
->vdev_initialize_lock
);
7909 return (SET_ERROR(EBUSY
));
7910 } else if (cmd_type
== POOL_INITIALIZE_CANCEL
&&
7911 (vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
&&
7912 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_SUSPENDED
)) {
7913 mutex_exit(&vd
->vdev_initialize_lock
);
7914 return (SET_ERROR(ESRCH
));
7915 } else if (cmd_type
== POOL_INITIALIZE_SUSPEND
&&
7916 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
) {
7917 mutex_exit(&vd
->vdev_initialize_lock
);
7918 return (SET_ERROR(ESRCH
));
7919 } else if (cmd_type
== POOL_INITIALIZE_UNINIT
&&
7920 vd
->vdev_initialize_thread
!= NULL
) {
7921 mutex_exit(&vd
->vdev_initialize_lock
);
7922 return (SET_ERROR(EBUSY
));
7926 case POOL_INITIALIZE_START
:
7927 vdev_initialize(vd
);
7929 case POOL_INITIALIZE_CANCEL
:
7930 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
, vd_list
);
7932 case POOL_INITIALIZE_SUSPEND
:
7933 vdev_initialize_stop(vd
, VDEV_INITIALIZE_SUSPENDED
, vd_list
);
7935 case POOL_INITIALIZE_UNINIT
:
7936 vdev_uninitialize(vd
);
7939 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
7941 mutex_exit(&vd
->vdev_initialize_lock
);
7947 spa_vdev_initialize(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
,
7948 nvlist_t
*vdev_errlist
)
7950 int total_errors
= 0;
7953 list_create(&vd_list
, sizeof (vdev_t
),
7954 offsetof(vdev_t
, vdev_initialize_node
));
7957 * We hold the namespace lock through the whole function
7958 * to prevent any changes to the pool while we're starting or
7959 * stopping initialization. The config and state locks are held so that
7960 * we can properly assess the vdev state before we commit to
7961 * the initializing operation.
7963 mutex_enter(&spa_namespace_lock
);
7965 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
7966 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
7967 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
7969 int error
= spa_vdev_initialize_impl(spa
, vdev_guid
, cmd_type
,
7972 char guid_as_str
[MAXNAMELEN
];
7974 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
7975 "%llu", (unsigned long long)vdev_guid
);
7976 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
7981 /* Wait for all initialize threads to stop. */
7982 vdev_initialize_stop_wait(spa
, &vd_list
);
7984 /* Sync out the initializing state */
7985 txg_wait_synced(spa
->spa_dsl_pool
, 0);
7986 mutex_exit(&spa_namespace_lock
);
7988 list_destroy(&vd_list
);
7990 return (total_errors
);
7994 spa_vdev_trim_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
7995 uint64_t rate
, boolean_t partial
, boolean_t secure
, list_t
*vd_list
)
7997 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7999 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
8001 /* Look up vdev and ensure it's a leaf. */
8002 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
8003 if (vd
== NULL
|| vd
->vdev_detached
) {
8004 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8005 return (SET_ERROR(ENODEV
));
8006 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
8007 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8008 return (SET_ERROR(EINVAL
));
8009 } else if (!vdev_writeable(vd
)) {
8010 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8011 return (SET_ERROR(EROFS
));
8012 } else if (!vd
->vdev_has_trim
) {
8013 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8014 return (SET_ERROR(EOPNOTSUPP
));
8015 } else if (secure
&& !vd
->vdev_has_securetrim
) {
8016 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8017 return (SET_ERROR(EOPNOTSUPP
));
8019 mutex_enter(&vd
->vdev_trim_lock
);
8020 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8023 * When we activate a TRIM action we check to see if the
8024 * vdev_trim_thread is NULL. We do this instead of using the
8025 * vdev_trim_state since there might be a previous TRIM process
8026 * which has completed but the thread is not exited.
8028 if (cmd_type
== POOL_TRIM_START
&&
8029 (vd
->vdev_trim_thread
!= NULL
|| vd
->vdev_top
->vdev_removing
||
8030 vd
->vdev_top
->vdev_rz_expanding
)) {
8031 mutex_exit(&vd
->vdev_trim_lock
);
8032 return (SET_ERROR(EBUSY
));
8033 } else if (cmd_type
== POOL_TRIM_CANCEL
&&
8034 (vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
&&
8035 vd
->vdev_trim_state
!= VDEV_TRIM_SUSPENDED
)) {
8036 mutex_exit(&vd
->vdev_trim_lock
);
8037 return (SET_ERROR(ESRCH
));
8038 } else if (cmd_type
== POOL_TRIM_SUSPEND
&&
8039 vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
) {
8040 mutex_exit(&vd
->vdev_trim_lock
);
8041 return (SET_ERROR(ESRCH
));
8045 case POOL_TRIM_START
:
8046 vdev_trim(vd
, rate
, partial
, secure
);
8048 case POOL_TRIM_CANCEL
:
8049 vdev_trim_stop(vd
, VDEV_TRIM_CANCELED
, vd_list
);
8051 case POOL_TRIM_SUSPEND
:
8052 vdev_trim_stop(vd
, VDEV_TRIM_SUSPENDED
, vd_list
);
8055 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
8057 mutex_exit(&vd
->vdev_trim_lock
);
8063 * Initiates a manual TRIM for the requested vdevs. This kicks off individual
8064 * TRIM threads for each child vdev. These threads pass over all of the free
8065 * space in the vdev's metaslabs and issues TRIM commands for that space.
8068 spa_vdev_trim(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
, uint64_t rate
,
8069 boolean_t partial
, boolean_t secure
, nvlist_t
*vdev_errlist
)
8071 int total_errors
= 0;
8074 list_create(&vd_list
, sizeof (vdev_t
),
8075 offsetof(vdev_t
, vdev_trim_node
));
8078 * We hold the namespace lock through the whole function
8079 * to prevent any changes to the pool while we're starting or
8080 * stopping TRIM. The config and state locks are held so that
8081 * we can properly assess the vdev state before we commit to
8082 * the TRIM operation.
8084 mutex_enter(&spa_namespace_lock
);
8086 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
8087 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
8088 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
8090 int error
= spa_vdev_trim_impl(spa
, vdev_guid
, cmd_type
,
8091 rate
, partial
, secure
, &vd_list
);
8093 char guid_as_str
[MAXNAMELEN
];
8095 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
8096 "%llu", (unsigned long long)vdev_guid
);
8097 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
8102 /* Wait for all TRIM threads to stop. */
8103 vdev_trim_stop_wait(spa
, &vd_list
);
8105 /* Sync out the TRIM state */
8106 txg_wait_synced(spa
->spa_dsl_pool
, 0);
8107 mutex_exit(&spa_namespace_lock
);
8109 list_destroy(&vd_list
);
8111 return (total_errors
);
8115 * Split a set of devices from their mirrors, and create a new pool from them.
8118 spa_vdev_split_mirror(spa_t
*spa
, const char *newname
, nvlist_t
*config
,
8119 nvlist_t
*props
, boolean_t exp
)
8122 uint64_t txg
, *glist
;
8124 uint_t c
, children
, lastlog
;
8125 nvlist_t
**child
, *nvl
, *tmp
;
8127 const char *altroot
= NULL
;
8128 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
8129 boolean_t activate_slog
;
8131 ASSERT(spa_writeable(spa
));
8133 txg
= spa_vdev_enter(spa
);
8135 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
8136 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
8137 error
= (spa_has_checkpoint(spa
)) ?
8138 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
8139 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
8142 /* clear the log and flush everything up to now */
8143 activate_slog
= spa_passivate_log(spa
);
8144 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
8145 error
= spa_reset_logs(spa
);
8146 txg
= spa_vdev_config_enter(spa
);
8149 spa_activate_log(spa
);
8152 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
8154 /* check new spa name before going any further */
8155 if (spa_lookup(newname
) != NULL
)
8156 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
8159 * scan through all the children to ensure they're all mirrors
8161 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
8162 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
8164 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
8166 /* first, check to ensure we've got the right child count */
8167 rvd
= spa
->spa_root_vdev
;
8169 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
8170 vdev_t
*vd
= rvd
->vdev_child
[c
];
8172 /* don't count the holes & logs as children */
8173 if (vd
->vdev_islog
|| (vd
->vdev_ops
!= &vdev_indirect_ops
&&
8174 !vdev_is_concrete(vd
))) {
8182 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
8183 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
8185 /* next, ensure no spare or cache devices are part of the split */
8186 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
8187 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
8188 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
8190 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
8191 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
8193 /* then, loop over each vdev and validate it */
8194 for (c
= 0; c
< children
; c
++) {
8195 uint64_t is_hole
= 0;
8197 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
8201 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
8202 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
8205 error
= SET_ERROR(EINVAL
);
8210 /* deal with indirect vdevs */
8211 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ops
==
8215 /* which disk is going to be split? */
8216 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
8218 error
= SET_ERROR(EINVAL
);
8222 /* look it up in the spa */
8223 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
8224 if (vml
[c
] == NULL
) {
8225 error
= SET_ERROR(ENODEV
);
8229 /* make sure there's nothing stopping the split */
8230 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
8231 vml
[c
]->vdev_islog
||
8232 !vdev_is_concrete(vml
[c
]) ||
8233 vml
[c
]->vdev_isspare
||
8234 vml
[c
]->vdev_isl2cache
||
8235 !vdev_writeable(vml
[c
]) ||
8236 vml
[c
]->vdev_children
!= 0 ||
8237 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
8238 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
8239 error
= SET_ERROR(EINVAL
);
8243 if (vdev_dtl_required(vml
[c
]) ||
8244 vdev_resilver_needed(vml
[c
], NULL
, NULL
)) {
8245 error
= SET_ERROR(EBUSY
);
8249 /* we need certain info from the top level */
8250 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
8251 vml
[c
]->vdev_top
->vdev_ms_array
);
8252 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
8253 vml
[c
]->vdev_top
->vdev_ms_shift
);
8254 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
8255 vml
[c
]->vdev_top
->vdev_asize
);
8256 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
8257 vml
[c
]->vdev_top
->vdev_ashift
);
8259 /* transfer per-vdev ZAPs */
8260 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
8261 VERIFY0(nvlist_add_uint64(child
[c
],
8262 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
8264 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
8265 VERIFY0(nvlist_add_uint64(child
[c
],
8266 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
8267 vml
[c
]->vdev_parent
->vdev_top_zap
));
8271 kmem_free(vml
, children
* sizeof (vdev_t
*));
8272 kmem_free(glist
, children
* sizeof (uint64_t));
8273 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
8276 /* stop writers from using the disks */
8277 for (c
= 0; c
< children
; c
++) {
8279 vml
[c
]->vdev_offline
= B_TRUE
;
8281 vdev_reopen(spa
->spa_root_vdev
);
8284 * Temporarily record the splitting vdevs in the spa config. This
8285 * will disappear once the config is regenerated.
8287 nvl
= fnvlist_alloc();
8288 fnvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
, glist
, children
);
8289 kmem_free(glist
, children
* sizeof (uint64_t));
8291 mutex_enter(&spa
->spa_props_lock
);
8292 fnvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
, nvl
);
8293 mutex_exit(&spa
->spa_props_lock
);
8294 spa
->spa_config_splitting
= nvl
;
8295 vdev_config_dirty(spa
->spa_root_vdev
);
8297 /* configure and create the new pool */
8298 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
);
8299 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
8300 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
);
8301 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
, spa_version(spa
));
8302 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, spa
->spa_config_txg
);
8303 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
8304 spa_generate_guid(NULL
));
8305 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
8306 (void) nvlist_lookup_string(props
,
8307 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
8309 /* add the new pool to the namespace */
8310 newspa
= spa_add(newname
, config
, altroot
);
8311 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
8312 newspa
->spa_config_txg
= spa
->spa_config_txg
;
8313 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
8315 /* release the spa config lock, retaining the namespace lock */
8316 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
8318 if (zio_injection_enabled
)
8319 zio_handle_panic_injection(spa
, FTAG
, 1);
8321 spa_activate(newspa
, spa_mode_global
);
8322 spa_async_suspend(newspa
);
8325 * Temporarily stop the initializing and TRIM activity. We set the
8326 * state to ACTIVE so that we know to resume initializing or TRIM
8327 * once the split has completed.
8329 list_t vd_initialize_list
;
8330 list_create(&vd_initialize_list
, sizeof (vdev_t
),
8331 offsetof(vdev_t
, vdev_initialize_node
));
8333 list_t vd_trim_list
;
8334 list_create(&vd_trim_list
, sizeof (vdev_t
),
8335 offsetof(vdev_t
, vdev_trim_node
));
8337 for (c
= 0; c
< children
; c
++) {
8338 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
8339 mutex_enter(&vml
[c
]->vdev_initialize_lock
);
8340 vdev_initialize_stop(vml
[c
],
8341 VDEV_INITIALIZE_ACTIVE
, &vd_initialize_list
);
8342 mutex_exit(&vml
[c
]->vdev_initialize_lock
);
8344 mutex_enter(&vml
[c
]->vdev_trim_lock
);
8345 vdev_trim_stop(vml
[c
], VDEV_TRIM_ACTIVE
, &vd_trim_list
);
8346 mutex_exit(&vml
[c
]->vdev_trim_lock
);
8350 vdev_initialize_stop_wait(spa
, &vd_initialize_list
);
8351 vdev_trim_stop_wait(spa
, &vd_trim_list
);
8353 list_destroy(&vd_initialize_list
);
8354 list_destroy(&vd_trim_list
);
8356 newspa
->spa_config_source
= SPA_CONFIG_SRC_SPLIT
;
8357 newspa
->spa_is_splitting
= B_TRUE
;
8359 /* create the new pool from the disks of the original pool */
8360 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
);
8364 /* if that worked, generate a real config for the new pool */
8365 if (newspa
->spa_root_vdev
!= NULL
) {
8366 newspa
->spa_config_splitting
= fnvlist_alloc();
8367 fnvlist_add_uint64(newspa
->spa_config_splitting
,
8368 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
));
8369 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
8374 if (props
!= NULL
) {
8375 spa_configfile_set(newspa
, props
, B_FALSE
);
8376 error
= spa_prop_set(newspa
, props
);
8381 /* flush everything */
8382 txg
= spa_vdev_config_enter(newspa
);
8383 vdev_config_dirty(newspa
->spa_root_vdev
);
8384 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
8386 if (zio_injection_enabled
)
8387 zio_handle_panic_injection(spa
, FTAG
, 2);
8389 spa_async_resume(newspa
);
8391 /* finally, update the original pool's config */
8392 txg
= spa_vdev_config_enter(spa
);
8393 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
8394 error
= dmu_tx_assign(tx
, TXG_WAIT
);
8397 for (c
= 0; c
< children
; c
++) {
8398 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
8399 vdev_t
*tvd
= vml
[c
]->vdev_top
;
8402 * Need to be sure the detachable VDEV is not
8403 * on any *other* txg's DTL list to prevent it
8404 * from being accessed after it's freed.
8406 for (int t
= 0; t
< TXG_SIZE
; t
++) {
8407 (void) txg_list_remove_this(
8408 &tvd
->vdev_dtl_list
, vml
[c
], t
);
8413 spa_history_log_internal(spa
, "detach", tx
,
8414 "vdev=%s", vml
[c
]->vdev_path
);
8419 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
8420 vdev_config_dirty(spa
->spa_root_vdev
);
8421 spa
->spa_config_splitting
= NULL
;
8425 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
8427 if (zio_injection_enabled
)
8428 zio_handle_panic_injection(spa
, FTAG
, 3);
8430 /* split is complete; log a history record */
8431 spa_history_log_internal(newspa
, "split", NULL
,
8432 "from pool %s", spa_name(spa
));
8434 newspa
->spa_is_splitting
= B_FALSE
;
8435 kmem_free(vml
, children
* sizeof (vdev_t
*));
8437 /* if we're not going to mount the filesystems in userland, export */
8439 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
8446 spa_deactivate(newspa
);
8449 txg
= spa_vdev_config_enter(spa
);
8451 /* re-online all offlined disks */
8452 for (c
= 0; c
< children
; c
++) {
8454 vml
[c
]->vdev_offline
= B_FALSE
;
8457 /* restart initializing or trimming disks as necessary */
8458 spa_async_request(spa
, SPA_ASYNC_INITIALIZE_RESTART
);
8459 spa_async_request(spa
, SPA_ASYNC_TRIM_RESTART
);
8460 spa_async_request(spa
, SPA_ASYNC_AUTOTRIM_RESTART
);
8462 vdev_reopen(spa
->spa_root_vdev
);
8464 nvlist_free(spa
->spa_config_splitting
);
8465 spa
->spa_config_splitting
= NULL
;
8466 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
8468 kmem_free(vml
, children
* sizeof (vdev_t
*));
8473 * Find any device that's done replacing, or a vdev marked 'unspare' that's
8474 * currently spared, so we can detach it.
8477 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
8479 vdev_t
*newvd
, *oldvd
;
8481 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
8482 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
8488 * Check for a completed replacement. We always consider the first
8489 * vdev in the list to be the oldest vdev, and the last one to be
8490 * the newest (see spa_vdev_attach() for how that works). In
8491 * the case where the newest vdev is faulted, we will not automatically
8492 * remove it after a resilver completes. This is OK as it will require
8493 * user intervention to determine which disk the admin wishes to keep.
8495 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
8496 ASSERT(vd
->vdev_children
> 1);
8498 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
8499 oldvd
= vd
->vdev_child
[0];
8501 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
8502 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
8503 !vdev_dtl_required(oldvd
))
8508 * Check for a completed resilver with the 'unspare' flag set.
8509 * Also potentially update faulted state.
8511 if (vd
->vdev_ops
== &vdev_spare_ops
) {
8512 vdev_t
*first
= vd
->vdev_child
[0];
8513 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
8515 if (last
->vdev_unspare
) {
8518 } else if (first
->vdev_unspare
) {
8525 if (oldvd
!= NULL
&&
8526 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
8527 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
8528 !vdev_dtl_required(oldvd
))
8531 vdev_propagate_state(vd
);
8534 * If there are more than two spares attached to a disk,
8535 * and those spares are not required, then we want to
8536 * attempt to free them up now so that they can be used
8537 * by other pools. Once we're back down to a single
8538 * disk+spare, we stop removing them.
8540 if (vd
->vdev_children
> 2) {
8541 newvd
= vd
->vdev_child
[1];
8543 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
8544 vdev_dtl_empty(last
, DTL_MISSING
) &&
8545 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
8546 !vdev_dtl_required(newvd
))
8555 spa_vdev_resilver_done(spa_t
*spa
)
8557 vdev_t
*vd
, *pvd
, *ppvd
;
8558 uint64_t guid
, sguid
, pguid
, ppguid
;
8560 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8562 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
8563 pvd
= vd
->vdev_parent
;
8564 ppvd
= pvd
->vdev_parent
;
8565 guid
= vd
->vdev_guid
;
8566 pguid
= pvd
->vdev_guid
;
8567 ppguid
= ppvd
->vdev_guid
;
8570 * If we have just finished replacing a hot spared device, then
8571 * we need to detach the parent's first child (the original hot
8574 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
8575 ppvd
->vdev_children
== 2) {
8576 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
8577 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
8579 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
8581 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8582 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
8584 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
8586 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8589 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8592 * If a detach was not performed above replace waiters will not have
8593 * been notified. In which case we must do so now.
8595 spa_notify_waiters(spa
);
8599 * Update the stored path or FRU for this vdev.
8602 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
8606 boolean_t sync
= B_FALSE
;
8608 ASSERT(spa_writeable(spa
));
8610 spa_vdev_state_enter(spa
, SCL_ALL
);
8612 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
8613 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
8615 if (!vd
->vdev_ops
->vdev_op_leaf
)
8616 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
8619 if (strcmp(value
, vd
->vdev_path
) != 0) {
8620 spa_strfree(vd
->vdev_path
);
8621 vd
->vdev_path
= spa_strdup(value
);
8625 if (vd
->vdev_fru
== NULL
) {
8626 vd
->vdev_fru
= spa_strdup(value
);
8628 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
8629 spa_strfree(vd
->vdev_fru
);
8630 vd
->vdev_fru
= spa_strdup(value
);
8635 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
8639 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
8641 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
8645 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
8647 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
8651 * ==========================================================================
8653 * ==========================================================================
8656 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
8658 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8660 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8661 return (SET_ERROR(EBUSY
));
8663 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
8667 spa_scan_stop(spa_t
*spa
)
8669 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8670 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8671 return (SET_ERROR(EBUSY
));
8673 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
8677 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
8679 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8681 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
8682 return (SET_ERROR(ENOTSUP
));
8684 if (func
== POOL_SCAN_RESILVER
&&
8685 !spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
))
8686 return (SET_ERROR(ENOTSUP
));
8689 * If a resilver was requested, but there is no DTL on a
8690 * writeable leaf device, we have nothing to do.
8692 if (func
== POOL_SCAN_RESILVER
&&
8693 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
8694 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
8698 if (func
== POOL_SCAN_ERRORSCRUB
&&
8699 !spa_feature_is_enabled(spa
, SPA_FEATURE_HEAD_ERRLOG
))
8700 return (SET_ERROR(ENOTSUP
));
8702 return (dsl_scan(spa
->spa_dsl_pool
, func
));
8706 * ==========================================================================
8707 * SPA async task processing
8708 * ==========================================================================
8712 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
8714 if (vd
->vdev_remove_wanted
) {
8715 vd
->vdev_remove_wanted
= B_FALSE
;
8716 vd
->vdev_delayed_close
= B_FALSE
;
8717 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
8720 * We want to clear the stats, but we don't want to do a full
8721 * vdev_clear() as that will cause us to throw away
8722 * degraded/faulted state as well as attempt to reopen the
8723 * device, all of which is a waste.
8725 vd
->vdev_stat
.vs_read_errors
= 0;
8726 vd
->vdev_stat
.vs_write_errors
= 0;
8727 vd
->vdev_stat
.vs_checksum_errors
= 0;
8729 vdev_state_dirty(vd
->vdev_top
);
8731 /* Tell userspace that the vdev is gone. */
8732 zfs_post_remove(spa
, vd
);
8735 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8736 spa_async_remove(spa
, vd
->vdev_child
[c
]);
8740 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
8742 if (vd
->vdev_probe_wanted
) {
8743 vd
->vdev_probe_wanted
= B_FALSE
;
8744 vdev_reopen(vd
); /* vdev_open() does the actual probe */
8747 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8748 spa_async_probe(spa
, vd
->vdev_child
[c
]);
8752 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
8754 if (!spa
->spa_autoexpand
)
8757 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
8758 vdev_t
*cvd
= vd
->vdev_child
[c
];
8759 spa_async_autoexpand(spa
, cvd
);
8762 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
8765 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
8768 static __attribute__((noreturn
)) void
8769 spa_async_thread(void *arg
)
8771 spa_t
*spa
= (spa_t
*)arg
;
8772 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
8775 ASSERT(spa
->spa_sync_on
);
8777 mutex_enter(&spa
->spa_async_lock
);
8778 tasks
= spa
->spa_async_tasks
;
8779 spa
->spa_async_tasks
= 0;
8780 mutex_exit(&spa
->spa_async_lock
);
8783 * See if the config needs to be updated.
8785 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
8786 uint64_t old_space
, new_space
;
8788 mutex_enter(&spa_namespace_lock
);
8789 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
8790 old_space
+= metaslab_class_get_space(spa_special_class(spa
));
8791 old_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8792 old_space
+= metaslab_class_get_space(
8793 spa_embedded_log_class(spa
));
8795 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
8797 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
8798 new_space
+= metaslab_class_get_space(spa_special_class(spa
));
8799 new_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8800 new_space
+= metaslab_class_get_space(
8801 spa_embedded_log_class(spa
));
8802 mutex_exit(&spa_namespace_lock
);
8805 * If the pool grew as a result of the config update,
8806 * then log an internal history event.
8808 if (new_space
!= old_space
) {
8809 spa_history_log_internal(spa
, "vdev online", NULL
,
8810 "pool '%s' size: %llu(+%llu)",
8811 spa_name(spa
), (u_longlong_t
)new_space
,
8812 (u_longlong_t
)(new_space
- old_space
));
8817 * See if any devices need to be marked REMOVED.
8819 if (tasks
& SPA_ASYNC_REMOVE
) {
8820 spa_vdev_state_enter(spa
, SCL_NONE
);
8821 spa_async_remove(spa
, spa
->spa_root_vdev
);
8822 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
8823 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
8824 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
8825 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
8826 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8829 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
8830 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8831 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
8832 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8836 * See if any devices need to be probed.
8838 if (tasks
& SPA_ASYNC_PROBE
) {
8839 spa_vdev_state_enter(spa
, SCL_NONE
);
8840 spa_async_probe(spa
, spa
->spa_root_vdev
);
8841 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8845 * If any devices are done replacing, detach them.
8847 if (tasks
& SPA_ASYNC_RESILVER_DONE
||
8848 tasks
& SPA_ASYNC_REBUILD_DONE
||
8849 tasks
& SPA_ASYNC_DETACH_SPARE
) {
8850 spa_vdev_resilver_done(spa
);
8854 * Kick off a resilver.
8856 if (tasks
& SPA_ASYNC_RESILVER
&&
8857 !vdev_rebuild_active(spa
->spa_root_vdev
) &&
8858 (!dsl_scan_resilvering(dp
) ||
8859 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_RESILVER_DEFER
)))
8860 dsl_scan_restart_resilver(dp
, 0);
8862 if (tasks
& SPA_ASYNC_INITIALIZE_RESTART
) {
8863 mutex_enter(&spa_namespace_lock
);
8864 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8865 vdev_initialize_restart(spa
->spa_root_vdev
);
8866 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8867 mutex_exit(&spa_namespace_lock
);
8870 if (tasks
& SPA_ASYNC_TRIM_RESTART
) {
8871 mutex_enter(&spa_namespace_lock
);
8872 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8873 vdev_trim_restart(spa
->spa_root_vdev
);
8874 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8875 mutex_exit(&spa_namespace_lock
);
8878 if (tasks
& SPA_ASYNC_AUTOTRIM_RESTART
) {
8879 mutex_enter(&spa_namespace_lock
);
8880 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8881 vdev_autotrim_restart(spa
);
8882 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8883 mutex_exit(&spa_namespace_lock
);
8887 * Kick off L2 cache whole device TRIM.
8889 if (tasks
& SPA_ASYNC_L2CACHE_TRIM
) {
8890 mutex_enter(&spa_namespace_lock
);
8891 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8892 vdev_trim_l2arc(spa
);
8893 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8894 mutex_exit(&spa_namespace_lock
);
8898 * Kick off L2 cache rebuilding.
8900 if (tasks
& SPA_ASYNC_L2CACHE_REBUILD
) {
8901 mutex_enter(&spa_namespace_lock
);
8902 spa_config_enter(spa
, SCL_L2ARC
, FTAG
, RW_READER
);
8903 l2arc_spa_rebuild_start(spa
);
8904 spa_config_exit(spa
, SCL_L2ARC
, FTAG
);
8905 mutex_exit(&spa_namespace_lock
);
8909 * Let the world know that we're done.
8911 mutex_enter(&spa
->spa_async_lock
);
8912 spa
->spa_async_thread
= NULL
;
8913 cv_broadcast(&spa
->spa_async_cv
);
8914 mutex_exit(&spa
->spa_async_lock
);
8919 spa_async_suspend(spa_t
*spa
)
8921 mutex_enter(&spa
->spa_async_lock
);
8922 spa
->spa_async_suspended
++;
8923 while (spa
->spa_async_thread
!= NULL
)
8924 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
8925 mutex_exit(&spa
->spa_async_lock
);
8927 spa_vdev_remove_suspend(spa
);
8929 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
8930 if (condense_thread
!= NULL
)
8931 zthr_cancel(condense_thread
);
8933 zthr_t
*raidz_expand_thread
= spa
->spa_raidz_expand_zthr
;
8934 if (raidz_expand_thread
!= NULL
)
8935 zthr_cancel(raidz_expand_thread
);
8937 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
8938 if (discard_thread
!= NULL
)
8939 zthr_cancel(discard_thread
);
8941 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
8942 if (ll_delete_thread
!= NULL
)
8943 zthr_cancel(ll_delete_thread
);
8945 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
8946 if (ll_condense_thread
!= NULL
)
8947 zthr_cancel(ll_condense_thread
);
8951 spa_async_resume(spa_t
*spa
)
8953 mutex_enter(&spa
->spa_async_lock
);
8954 ASSERT(spa
->spa_async_suspended
!= 0);
8955 spa
->spa_async_suspended
--;
8956 mutex_exit(&spa
->spa_async_lock
);
8957 spa_restart_removal(spa
);
8959 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
8960 if (condense_thread
!= NULL
)
8961 zthr_resume(condense_thread
);
8963 zthr_t
*raidz_expand_thread
= spa
->spa_raidz_expand_zthr
;
8964 if (raidz_expand_thread
!= NULL
)
8965 zthr_resume(raidz_expand_thread
);
8967 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
8968 if (discard_thread
!= NULL
)
8969 zthr_resume(discard_thread
);
8971 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
8972 if (ll_delete_thread
!= NULL
)
8973 zthr_resume(ll_delete_thread
);
8975 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
8976 if (ll_condense_thread
!= NULL
)
8977 zthr_resume(ll_condense_thread
);
8981 spa_async_tasks_pending(spa_t
*spa
)
8983 uint_t non_config_tasks
;
8985 boolean_t config_task_suspended
;
8987 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
8988 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
8989 if (spa
->spa_ccw_fail_time
== 0) {
8990 config_task_suspended
= B_FALSE
;
8992 config_task_suspended
=
8993 (gethrtime() - spa
->spa_ccw_fail_time
) <
8994 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
8997 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
9001 spa_async_dispatch(spa_t
*spa
)
9003 mutex_enter(&spa
->spa_async_lock
);
9004 if (spa_async_tasks_pending(spa
) &&
9005 !spa
->spa_async_suspended
&&
9006 spa
->spa_async_thread
== NULL
)
9007 spa
->spa_async_thread
= thread_create(NULL
, 0,
9008 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
9009 mutex_exit(&spa
->spa_async_lock
);
9013 spa_async_request(spa_t
*spa
, int task
)
9015 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
9016 mutex_enter(&spa
->spa_async_lock
);
9017 spa
->spa_async_tasks
|= task
;
9018 mutex_exit(&spa
->spa_async_lock
);
9022 spa_async_tasks(spa_t
*spa
)
9024 return (spa
->spa_async_tasks
);
9028 * ==========================================================================
9029 * SPA syncing routines
9030 * ==========================================================================
9035 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
9039 bpobj_enqueue(bpo
, bp
, bp_freed
, tx
);
9044 bpobj_enqueue_alloc_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
9046 return (bpobj_enqueue_cb(arg
, bp
, B_FALSE
, tx
));
9050 bpobj_enqueue_free_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
9052 return (bpobj_enqueue_cb(arg
, bp
, B_TRUE
, tx
));
9056 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
9060 zio_nowait(zio_free_sync(pio
, pio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
9066 bpobj_spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
9070 return (spa_free_sync_cb(arg
, bp
, tx
));
9074 * Note: this simple function is not inlined to make it easier to dtrace the
9075 * amount of time spent syncing frees.
9078 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
9080 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
9081 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
9082 VERIFY(zio_wait(zio
) == 0);
9086 * Note: this simple function is not inlined to make it easier to dtrace the
9087 * amount of time spent syncing deferred frees.
9090 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
9092 if (spa_sync_pass(spa
) != 1)
9097 * If the log space map feature is active, we stop deferring
9098 * frees to the next TXG and therefore running this function
9099 * would be considered a no-op as spa_deferred_bpobj should
9100 * not have any entries.
9102 * That said we run this function anyway (instead of returning
9103 * immediately) for the edge-case scenario where we just
9104 * activated the log space map feature in this TXG but we have
9105 * deferred frees from the previous TXG.
9107 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
9108 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
9109 bpobj_spa_free_sync_cb
, zio
, tx
), ==, 0);
9110 VERIFY0(zio_wait(zio
));
9114 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
9116 char *packed
= NULL
;
9121 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
9124 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
9125 * information. This avoids the dmu_buf_will_dirty() path and
9126 * saves us a pre-read to get data we don't actually care about.
9128 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
9129 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
9131 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
9133 memset(packed
+ nvsize
, 0, bufsize
- nvsize
);
9135 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
9137 vmem_free(packed
, bufsize
);
9139 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
9140 dmu_buf_will_dirty(db
, tx
);
9141 *(uint64_t *)db
->db_data
= nvsize
;
9142 dmu_buf_rele(db
, FTAG
);
9146 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
9147 const char *config
, const char *entry
)
9157 * Update the MOS nvlist describing the list of available devices.
9158 * spa_validate_aux() will have already made sure this nvlist is
9159 * valid and the vdevs are labeled appropriately.
9161 if (sav
->sav_object
== 0) {
9162 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
9163 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
9164 sizeof (uint64_t), tx
);
9165 VERIFY(zap_update(spa
->spa_meta_objset
,
9166 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
9167 &sav
->sav_object
, tx
) == 0);
9170 nvroot
= fnvlist_alloc();
9171 if (sav
->sav_count
== 0) {
9172 fnvlist_add_nvlist_array(nvroot
, config
,
9173 (const nvlist_t
* const *)NULL
, 0);
9175 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
9176 for (i
= 0; i
< sav
->sav_count
; i
++)
9177 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
9178 B_FALSE
, VDEV_CONFIG_L2CACHE
);
9179 fnvlist_add_nvlist_array(nvroot
, config
,
9180 (const nvlist_t
* const *)list
, sav
->sav_count
);
9181 for (i
= 0; i
< sav
->sav_count
; i
++)
9182 nvlist_free(list
[i
]);
9183 kmem_free(list
, sav
->sav_count
* sizeof (void *));
9186 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
9187 nvlist_free(nvroot
);
9189 sav
->sav_sync
= B_FALSE
;
9193 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
9194 * The all-vdev ZAP must be empty.
9197 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
9199 spa_t
*spa
= vd
->vdev_spa
;
9201 if (vd
->vdev_root_zap
!= 0 &&
9202 spa_feature_is_active(spa
, SPA_FEATURE_AVZ_V2
)) {
9203 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
9204 vd
->vdev_root_zap
, tx
));
9206 if (vd
->vdev_top_zap
!= 0) {
9207 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
9208 vd
->vdev_top_zap
, tx
));
9210 if (vd
->vdev_leaf_zap
!= 0) {
9211 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
9212 vd
->vdev_leaf_zap
, tx
));
9214 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
9215 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
9220 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
9225 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
9226 * its config may not be dirty but we still need to build per-vdev ZAPs.
9227 * Similarly, if the pool is being assembled (e.g. after a split), we
9228 * need to rebuild the AVZ although the config may not be dirty.
9230 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
9231 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
9234 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9236 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
9237 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
9238 spa
->spa_all_vdev_zaps
!= 0);
9240 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
9241 /* Make and build the new AVZ */
9242 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
9243 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
9244 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
9246 /* Diff old AVZ with new one */
9250 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
9251 spa
->spa_all_vdev_zaps
);
9252 zap_cursor_retrieve(&zc
, &za
) == 0;
9253 zap_cursor_advance(&zc
)) {
9254 uint64_t vdzap
= za
.za_first_integer
;
9255 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
9258 * ZAP is listed in old AVZ but not in new one;
9261 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
9266 zap_cursor_fini(&zc
);
9268 /* Destroy the old AVZ */
9269 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
9270 spa
->spa_all_vdev_zaps
, tx
));
9272 /* Replace the old AVZ in the dir obj with the new one */
9273 VERIFY0(zap_update(spa
->spa_meta_objset
,
9274 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
9275 sizeof (new_avz
), 1, &new_avz
, tx
));
9277 spa
->spa_all_vdev_zaps
= new_avz
;
9278 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
9282 /* Walk through the AVZ and destroy all listed ZAPs */
9283 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
9284 spa
->spa_all_vdev_zaps
);
9285 zap_cursor_retrieve(&zc
, &za
) == 0;
9286 zap_cursor_advance(&zc
)) {
9287 uint64_t zap
= za
.za_first_integer
;
9288 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
9291 zap_cursor_fini(&zc
);
9293 /* Destroy and unlink the AVZ itself */
9294 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
9295 spa
->spa_all_vdev_zaps
, tx
));
9296 VERIFY0(zap_remove(spa
->spa_meta_objset
,
9297 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
9298 spa
->spa_all_vdev_zaps
= 0;
9301 if (spa
->spa_all_vdev_zaps
== 0) {
9302 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
9303 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
9304 DMU_POOL_VDEV_ZAP_MAP
, tx
);
9306 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
9308 /* Create ZAPs for vdevs that don't have them. */
9309 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
9311 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
9312 dmu_tx_get_txg(tx
), B_FALSE
);
9315 * If we're upgrading the spa version then make sure that
9316 * the config object gets updated with the correct version.
9318 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
9319 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
9320 spa
->spa_uberblock
.ub_version
);
9322 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9324 nvlist_free(spa
->spa_config_syncing
);
9325 spa
->spa_config_syncing
= config
;
9327 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
9331 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
9333 uint64_t *versionp
= arg
;
9334 uint64_t version
= *versionp
;
9335 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
9338 * Setting the version is special cased when first creating the pool.
9340 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
9342 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
9343 ASSERT(version
>= spa_version(spa
));
9345 spa
->spa_uberblock
.ub_version
= version
;
9346 vdev_config_dirty(spa
->spa_root_vdev
);
9347 spa_history_log_internal(spa
, "set", tx
, "version=%lld",
9348 (longlong_t
)version
);
9352 * Set zpool properties.
9355 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
9357 nvlist_t
*nvp
= arg
;
9358 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
9359 objset_t
*mos
= spa
->spa_meta_objset
;
9360 nvpair_t
*elem
= NULL
;
9362 mutex_enter(&spa
->spa_props_lock
);
9364 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
9366 const char *strval
, *fname
;
9368 const char *propname
;
9369 const char *elemname
= nvpair_name(elem
);
9370 zprop_type_t proptype
;
9373 switch (prop
= zpool_name_to_prop(elemname
)) {
9374 case ZPOOL_PROP_VERSION
:
9375 intval
= fnvpair_value_uint64(elem
);
9377 * The version is synced separately before other
9378 * properties and should be correct by now.
9380 ASSERT3U(spa_version(spa
), >=, intval
);
9383 case ZPOOL_PROP_ALTROOT
:
9385 * 'altroot' is a non-persistent property. It should
9386 * have been set temporarily at creation or import time.
9388 ASSERT(spa
->spa_root
!= NULL
);
9391 case ZPOOL_PROP_READONLY
:
9392 case ZPOOL_PROP_CACHEFILE
:
9394 * 'readonly' and 'cachefile' are also non-persistent
9398 case ZPOOL_PROP_COMMENT
:
9399 strval
= fnvpair_value_string(elem
);
9400 if (spa
->spa_comment
!= NULL
)
9401 spa_strfree(spa
->spa_comment
);
9402 spa
->spa_comment
= spa_strdup(strval
);
9404 * We need to dirty the configuration on all the vdevs
9405 * so that their labels get updated. We also need to
9406 * update the cache file to keep it in sync with the
9407 * MOS version. It's unnecessary to do this for pool
9408 * creation since the vdev's configuration has already
9411 if (tx
->tx_txg
!= TXG_INITIAL
) {
9412 vdev_config_dirty(spa
->spa_root_vdev
);
9413 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
9415 spa_history_log_internal(spa
, "set", tx
,
9416 "%s=%s", elemname
, strval
);
9418 case ZPOOL_PROP_COMPATIBILITY
:
9419 strval
= fnvpair_value_string(elem
);
9420 if (spa
->spa_compatibility
!= NULL
)
9421 spa_strfree(spa
->spa_compatibility
);
9422 spa
->spa_compatibility
= spa_strdup(strval
);
9424 * Dirty the configuration on vdevs as above.
9426 if (tx
->tx_txg
!= TXG_INITIAL
) {
9427 vdev_config_dirty(spa
->spa_root_vdev
);
9428 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
9431 spa_history_log_internal(spa
, "set", tx
,
9432 "%s=%s", nvpair_name(elem
), strval
);
9435 case ZPOOL_PROP_INVAL
:
9436 if (zpool_prop_feature(elemname
)) {
9437 fname
= strchr(elemname
, '@') + 1;
9438 VERIFY0(zfeature_lookup_name(fname
, &fid
));
9440 spa_feature_enable(spa
, fid
, tx
);
9441 spa_history_log_internal(spa
, "set", tx
,
9442 "%s=enabled", elemname
);
9444 } else if (!zfs_prop_user(elemname
)) {
9445 ASSERT(zpool_prop_feature(elemname
));
9451 * Set pool property values in the poolprops mos object.
9453 if (spa
->spa_pool_props_object
== 0) {
9454 spa
->spa_pool_props_object
=
9455 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
9456 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
9460 /* normalize the property name */
9461 if (prop
== ZPOOL_PROP_INVAL
) {
9462 propname
= elemname
;
9463 proptype
= PROP_TYPE_STRING
;
9465 propname
= zpool_prop_to_name(prop
);
9466 proptype
= zpool_prop_get_type(prop
);
9469 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
9470 ASSERT(proptype
== PROP_TYPE_STRING
);
9471 strval
= fnvpair_value_string(elem
);
9472 VERIFY0(zap_update(mos
,
9473 spa
->spa_pool_props_object
, propname
,
9474 1, strlen(strval
) + 1, strval
, tx
));
9475 spa_history_log_internal(spa
, "set", tx
,
9476 "%s=%s", elemname
, strval
);
9477 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
9478 intval
= fnvpair_value_uint64(elem
);
9480 if (proptype
== PROP_TYPE_INDEX
) {
9482 VERIFY0(zpool_prop_index_to_string(
9483 prop
, intval
, &unused
));
9485 VERIFY0(zap_update(mos
,
9486 spa
->spa_pool_props_object
, propname
,
9487 8, 1, &intval
, tx
));
9488 spa_history_log_internal(spa
, "set", tx
,
9489 "%s=%lld", elemname
,
9490 (longlong_t
)intval
);
9493 case ZPOOL_PROP_DELEGATION
:
9494 spa
->spa_delegation
= intval
;
9496 case ZPOOL_PROP_BOOTFS
:
9497 spa
->spa_bootfs
= intval
;
9499 case ZPOOL_PROP_FAILUREMODE
:
9500 spa
->spa_failmode
= intval
;
9502 case ZPOOL_PROP_AUTOTRIM
:
9503 spa
->spa_autotrim
= intval
;
9504 spa_async_request(spa
,
9505 SPA_ASYNC_AUTOTRIM_RESTART
);
9507 case ZPOOL_PROP_AUTOEXPAND
:
9508 spa
->spa_autoexpand
= intval
;
9509 if (tx
->tx_txg
!= TXG_INITIAL
)
9510 spa_async_request(spa
,
9511 SPA_ASYNC_AUTOEXPAND
);
9513 case ZPOOL_PROP_MULTIHOST
:
9514 spa
->spa_multihost
= intval
;
9520 ASSERT(0); /* not allowed */
9526 mutex_exit(&spa
->spa_props_lock
);
9530 * Perform one-time upgrade on-disk changes. spa_version() does not
9531 * reflect the new version this txg, so there must be no changes this
9532 * txg to anything that the upgrade code depends on after it executes.
9533 * Therefore this must be called after dsl_pool_sync() does the sync
9537 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
9539 if (spa_sync_pass(spa
) != 1)
9542 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9543 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
9545 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
9546 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
9547 dsl_pool_create_origin(dp
, tx
);
9549 /* Keeping the origin open increases spa_minref */
9550 spa
->spa_minref
+= 3;
9553 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
9554 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
9555 dsl_pool_upgrade_clones(dp
, tx
);
9558 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
9559 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
9560 dsl_pool_upgrade_dir_clones(dp
, tx
);
9562 /* Keeping the freedir open increases spa_minref */
9563 spa
->spa_minref
+= 3;
9566 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
9567 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
9568 spa_feature_create_zap_objects(spa
, tx
);
9572 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
9573 * when possibility to use lz4 compression for metadata was added
9574 * Old pools that have this feature enabled must be upgraded to have
9575 * this feature active
9577 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
9578 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
9579 SPA_FEATURE_LZ4_COMPRESS
);
9580 boolean_t lz4_ac
= spa_feature_is_active(spa
,
9581 SPA_FEATURE_LZ4_COMPRESS
);
9583 if (lz4_en
&& !lz4_ac
)
9584 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
9588 * If we haven't written the salt, do so now. Note that the
9589 * feature may not be activated yet, but that's fine since
9590 * the presence of this ZAP entry is backwards compatible.
9592 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
9593 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
9594 VERIFY0(zap_add(spa
->spa_meta_objset
,
9595 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
9596 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
9597 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
9600 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
9604 vdev_indirect_state_sync_verify(vdev_t
*vd
)
9606 vdev_indirect_mapping_t
*vim __maybe_unused
= vd
->vdev_indirect_mapping
;
9607 vdev_indirect_births_t
*vib __maybe_unused
= vd
->vdev_indirect_births
;
9609 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
9610 ASSERT(vim
!= NULL
);
9611 ASSERT(vib
!= NULL
);
9614 uint64_t obsolete_sm_object
= 0;
9615 ASSERT0(vdev_obsolete_sm_object(vd
, &obsolete_sm_object
));
9616 if (obsolete_sm_object
!= 0) {
9617 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
9618 ASSERT(vd
->vdev_removing
||
9619 vd
->vdev_ops
== &vdev_indirect_ops
);
9620 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
9621 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
9622 ASSERT3U(obsolete_sm_object
, ==,
9623 space_map_object(vd
->vdev_obsolete_sm
));
9624 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
9625 space_map_allocated(vd
->vdev_obsolete_sm
));
9627 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
9630 * Since frees / remaps to an indirect vdev can only
9631 * happen in syncing context, the obsolete segments
9632 * tree must be empty when we start syncing.
9634 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
9638 * Set the top-level vdev's max queue depth. Evaluate each top-level's
9639 * async write queue depth in case it changed. The max queue depth will
9640 * not change in the middle of syncing out this txg.
9643 spa_sync_adjust_vdev_max_queue_depth(spa_t
*spa
)
9645 ASSERT(spa_writeable(spa
));
9647 vdev_t
*rvd
= spa
->spa_root_vdev
;
9648 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
9649 zfs_vdev_queue_depth_pct
/ 100;
9650 metaslab_class_t
*normal
= spa_normal_class(spa
);
9651 metaslab_class_t
*special
= spa_special_class(spa
);
9652 metaslab_class_t
*dedup
= spa_dedup_class(spa
);
9654 uint64_t slots_per_allocator
= 0;
9655 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9656 vdev_t
*tvd
= rvd
->vdev_child
[c
];
9658 metaslab_group_t
*mg
= tvd
->vdev_mg
;
9659 if (mg
== NULL
|| !metaslab_group_initialized(mg
))
9662 metaslab_class_t
*mc
= mg
->mg_class
;
9663 if (mc
!= normal
&& mc
!= special
&& mc
!= dedup
)
9667 * It is safe to do a lock-free check here because only async
9668 * allocations look at mg_max_alloc_queue_depth, and async
9669 * allocations all happen from spa_sync().
9671 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9672 ASSERT0(zfs_refcount_count(
9673 &(mg
->mg_allocator
[i
].mga_alloc_queue_depth
)));
9675 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
9677 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9678 mg
->mg_allocator
[i
].mga_cur_max_alloc_queue_depth
=
9679 zfs_vdev_def_queue_depth
;
9681 slots_per_allocator
+= zfs_vdev_def_queue_depth
;
9684 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9685 ASSERT0(zfs_refcount_count(&normal
->mc_allocator
[i
].
9687 ASSERT0(zfs_refcount_count(&special
->mc_allocator
[i
].
9689 ASSERT0(zfs_refcount_count(&dedup
->mc_allocator
[i
].
9691 normal
->mc_allocator
[i
].mca_alloc_max_slots
=
9692 slots_per_allocator
;
9693 special
->mc_allocator
[i
].mca_alloc_max_slots
=
9694 slots_per_allocator
;
9695 dedup
->mc_allocator
[i
].mca_alloc_max_slots
=
9696 slots_per_allocator
;
9698 normal
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9699 special
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9700 dedup
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9704 spa_sync_condense_indirect(spa_t
*spa
, dmu_tx_t
*tx
)
9706 ASSERT(spa_writeable(spa
));
9708 vdev_t
*rvd
= spa
->spa_root_vdev
;
9709 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9710 vdev_t
*vd
= rvd
->vdev_child
[c
];
9711 vdev_indirect_state_sync_verify(vd
);
9713 if (vdev_indirect_should_condense(vd
)) {
9714 spa_condense_indirect_start_sync(vd
, tx
);
9721 spa_sync_iterate_to_convergence(spa_t
*spa
, dmu_tx_t
*tx
)
9723 objset_t
*mos
= spa
->spa_meta_objset
;
9724 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9725 uint64_t txg
= tx
->tx_txg
;
9726 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
9729 int pass
= ++spa
->spa_sync_pass
;
9731 spa_sync_config_object(spa
, tx
);
9732 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
9733 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
9734 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
9735 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
9736 spa_errlog_sync(spa
, txg
);
9737 dsl_pool_sync(dp
, txg
);
9739 if (pass
< zfs_sync_pass_deferred_free
||
9740 spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
)) {
9742 * If the log space map feature is active we don't
9743 * care about deferred frees and the deferred bpobj
9744 * as the log space map should effectively have the
9745 * same results (i.e. appending only to one object).
9747 spa_sync_frees(spa
, free_bpl
, tx
);
9750 * We can not defer frees in pass 1, because
9751 * we sync the deferred frees later in pass 1.
9753 ASSERT3U(pass
, >, 1);
9754 bplist_iterate(free_bpl
, bpobj_enqueue_alloc_cb
,
9755 &spa
->spa_deferred_bpobj
, tx
);
9760 dsl_scan_sync(dp
, tx
);
9761 dsl_errorscrub_sync(dp
, tx
);
9763 spa_sync_upgrades(spa
, tx
);
9765 spa_flush_metaslabs(spa
, tx
);
9768 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
9774 * dsl_pool_sync() -> dp_sync_tasks may have dirtied
9775 * the config. If that happens, this txg should not
9776 * be a no-op. So we must sync the config to the MOS
9777 * before checking for no-op.
9779 * Note that when the config is dirty, it will
9780 * be written to the MOS (i.e. the MOS will be
9781 * dirtied) every time we call spa_sync_config_object()
9782 * in this txg. Therefore we can't call this after
9783 * dsl_pool_sync() every pass, because it would
9784 * prevent us from converging, since we'd dirty
9785 * the MOS every pass.
9787 * Sync tasks can only be processed in pass 1, so
9788 * there's no need to do this in later passes.
9790 spa_sync_config_object(spa
, tx
);
9794 * Note: We need to check if the MOS is dirty because we could
9795 * have marked the MOS dirty without updating the uberblock
9796 * (e.g. if we have sync tasks but no dirty user data). We need
9797 * to check the uberblock's rootbp because it is updated if we
9798 * have synced out dirty data (though in this case the MOS will
9799 * most likely also be dirty due to second order effects, we
9800 * don't want to rely on that here).
9803 spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
9804 !dmu_objset_is_dirty(mos
, txg
)) {
9806 * Nothing changed on the first pass, therefore this
9807 * TXG is a no-op. Avoid syncing deferred frees, so
9808 * that we can keep this TXG as a no-op.
9810 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
9811 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
9812 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
9813 ASSERT(txg_list_empty(&dp
->dp_early_sync_tasks
, txg
));
9817 spa_sync_deferred_frees(spa
, tx
);
9818 } while (dmu_objset_is_dirty(mos
, txg
));
9822 * Rewrite the vdev configuration (which includes the uberblock) to
9823 * commit the transaction group.
9825 * If there are no dirty vdevs, we sync the uberblock to a few random
9826 * top-level vdevs that are known to be visible in the config cache
9827 * (see spa_vdev_add() for a complete description). If there *are* dirty
9828 * vdevs, sync the uberblock to all vdevs.
9831 spa_sync_rewrite_vdev_config(spa_t
*spa
, dmu_tx_t
*tx
)
9833 vdev_t
*rvd
= spa
->spa_root_vdev
;
9834 uint64_t txg
= tx
->tx_txg
;
9840 * We hold SCL_STATE to prevent vdev open/close/etc.
9841 * while we're attempting to write the vdev labels.
9843 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9845 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
9846 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
9848 int children
= rvd
->vdev_children
;
9849 int c0
= random_in_range(children
);
9851 for (int c
= 0; c
< children
; c
++) {
9853 rvd
->vdev_child
[(c0
+ c
) % children
];
9855 /* Stop when revisiting the first vdev */
9856 if (c
> 0 && svd
[0] == vd
)
9859 if (vd
->vdev_ms_array
== 0 ||
9861 !vdev_is_concrete(vd
))
9864 svd
[svdcount
++] = vd
;
9865 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
9868 error
= vdev_config_sync(svd
, svdcount
, txg
);
9870 error
= vdev_config_sync(rvd
->vdev_child
,
9871 rvd
->vdev_children
, txg
);
9875 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
9877 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9881 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
9882 zio_resume_wait(spa
);
9887 * Sync the specified transaction group. New blocks may be dirtied as
9888 * part of the process, so we iterate until it converges.
9891 spa_sync(spa_t
*spa
, uint64_t txg
)
9895 VERIFY(spa_writeable(spa
));
9898 * Wait for i/os issued in open context that need to complete
9899 * before this txg syncs.
9901 (void) zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]);
9902 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
,
9906 * Now that there can be no more cloning in this transaction group,
9907 * but we are still before issuing frees, we can process pending BRT
9910 brt_pending_apply(spa
, txg
);
9913 * Lock out configuration changes.
9915 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
9917 spa
->spa_syncing_txg
= txg
;
9918 spa
->spa_sync_pass
= 0;
9920 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9921 mutex_enter(&spa
->spa_allocs
[i
].spaa_lock
);
9922 VERIFY0(avl_numnodes(&spa
->spa_allocs
[i
].spaa_tree
));
9923 mutex_exit(&spa
->spa_allocs
[i
].spaa_lock
);
9927 * If there are any pending vdev state changes, convert them
9928 * into config changes that go out with this transaction group.
9930 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9931 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
9932 /* Avoid holding the write lock unless actually necessary */
9933 if (vd
->vdev_aux
== NULL
) {
9934 vdev_state_clean(vd
);
9935 vdev_config_dirty(vd
);
9939 * We need the write lock here because, for aux vdevs,
9940 * calling vdev_config_dirty() modifies sav_config.
9941 * This is ugly and will become unnecessary when we
9942 * eliminate the aux vdev wart by integrating all vdevs
9943 * into the root vdev tree.
9945 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9946 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
9947 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
9948 vdev_state_clean(vd
);
9949 vdev_config_dirty(vd
);
9951 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9952 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9954 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9956 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9957 dmu_tx_t
*tx
= dmu_tx_create_assigned(dp
, txg
);
9959 spa
->spa_sync_starttime
= gethrtime();
9960 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
9961 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
9962 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
9963 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
9966 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
9967 * set spa_deflate if we have no raid-z vdevs.
9969 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
9970 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
9971 vdev_t
*rvd
= spa
->spa_root_vdev
;
9974 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
9975 vd
= rvd
->vdev_child
[i
];
9976 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
9979 if (i
== rvd
->vdev_children
) {
9980 spa
->spa_deflate
= TRUE
;
9981 VERIFY0(zap_add(spa
->spa_meta_objset
,
9982 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
9983 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
9987 spa_sync_adjust_vdev_max_queue_depth(spa
);
9989 spa_sync_condense_indirect(spa
, tx
);
9991 spa_sync_iterate_to_convergence(spa
, tx
);
9994 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
9996 * Make sure that the number of ZAPs for all the vdevs matches
9997 * the number of ZAPs in the per-vdev ZAP list. This only gets
9998 * called if the config is dirty; otherwise there may be
9999 * outstanding AVZ operations that weren't completed in
10000 * spa_sync_config_object.
10002 uint64_t all_vdev_zap_entry_count
;
10003 ASSERT0(zap_count(spa
->spa_meta_objset
,
10004 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
10005 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
10006 all_vdev_zap_entry_count
);
10010 if (spa
->spa_vdev_removal
!= NULL
) {
10011 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
10014 spa_sync_rewrite_vdev_config(spa
, tx
);
10017 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
10018 spa
->spa_deadman_tqid
= 0;
10021 * Clear the dirty config list.
10023 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
10024 vdev_config_clean(vd
);
10027 * Now that the new config has synced transactionally,
10028 * let it become visible to the config cache.
10030 if (spa
->spa_config_syncing
!= NULL
) {
10031 spa_config_set(spa
, spa
->spa_config_syncing
);
10032 spa
->spa_config_txg
= txg
;
10033 spa
->spa_config_syncing
= NULL
;
10036 dsl_pool_sync_done(dp
, txg
);
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 * Update usable space statistics.
10047 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
)))
10049 vdev_sync_done(vd
, txg
);
10051 metaslab_class_evict_old(spa
->spa_normal_class
, txg
);
10052 metaslab_class_evict_old(spa
->spa_log_class
, txg
);
10054 spa_sync_close_syncing_log_sm(spa
);
10056 spa_update_dspace(spa
);
10058 if (spa_get_autotrim(spa
) == SPA_AUTOTRIM_ON
)
10059 vdev_autotrim_kick(spa
);
10062 * It had better be the case that we didn't dirty anything
10063 * since vdev_config_sync().
10065 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
10066 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
10067 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
10069 while (zfs_pause_spa_sync
)
10072 spa
->spa_sync_pass
= 0;
10075 * Update the last synced uberblock here. We want to do this at
10076 * the end of spa_sync() so that consumers of spa_last_synced_txg()
10077 * will be guaranteed that all the processing associated with
10078 * that txg has been completed.
10080 spa
->spa_ubsync
= spa
->spa_uberblock
;
10081 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
10083 spa_handle_ignored_writes(spa
);
10086 * If any async tasks have been requested, kick them off.
10088 spa_async_dispatch(spa
);
10092 * Sync all pools. We don't want to hold the namespace lock across these
10093 * operations, so we take a reference on the spa_t and drop the lock during the
10097 spa_sync_allpools(void)
10100 mutex_enter(&spa_namespace_lock
);
10101 while ((spa
= spa_next(spa
)) != NULL
) {
10102 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
10103 !spa_writeable(spa
) || spa_suspended(spa
))
10105 spa_open_ref(spa
, FTAG
);
10106 mutex_exit(&spa_namespace_lock
);
10107 txg_wait_synced(spa_get_dsl(spa
), 0);
10108 mutex_enter(&spa_namespace_lock
);
10109 spa_close(spa
, FTAG
);
10111 mutex_exit(&spa_namespace_lock
);
10115 spa_sync_tq_create(spa_t
*spa
, const char *name
)
10117 kthread_t
**kthreads
;
10119 ASSERT(spa
->spa_sync_tq
== NULL
);
10120 ASSERT3S(spa
->spa_alloc_count
, <=, boot_ncpus
);
10123 * - do not allow more allocators than cpus.
10124 * - there may be more cpus than allocators.
10125 * - do not allow more sync taskq threads than allocators or cpus.
10127 int nthreads
= spa
->spa_alloc_count
;
10128 spa
->spa_syncthreads
= kmem_zalloc(sizeof (spa_syncthread_info_t
) *
10129 nthreads
, KM_SLEEP
);
10131 spa
->spa_sync_tq
= taskq_create_synced(name
, nthreads
, minclsyspri
,
10132 nthreads
, INT_MAX
, TASKQ_PREPOPULATE
, &kthreads
);
10133 VERIFY(spa
->spa_sync_tq
!= NULL
);
10134 VERIFY(kthreads
!= NULL
);
10136 spa_taskqs_t
*tqs
=
10137 &spa
->spa_zio_taskq
[ZIO_TYPE_WRITE
][ZIO_TASKQ_ISSUE
];
10139 spa_syncthread_info_t
*ti
= spa
->spa_syncthreads
;
10140 for (int i
= 0, w
= 0; i
< nthreads
; i
++, w
++, ti
++) {
10141 ti
->sti_thread
= kthreads
[i
];
10142 if (w
== tqs
->stqs_count
) {
10145 ti
->sti_wr_iss_tq
= tqs
->stqs_taskq
[w
];
10148 kmem_free(kthreads
, sizeof (*kthreads
) * nthreads
);
10149 return (spa
->spa_sync_tq
);
10153 spa_sync_tq_destroy(spa_t
*spa
)
10155 ASSERT(spa
->spa_sync_tq
!= NULL
);
10157 taskq_wait(spa
->spa_sync_tq
);
10158 taskq_destroy(spa
->spa_sync_tq
);
10159 kmem_free(spa
->spa_syncthreads
,
10160 sizeof (spa_syncthread_info_t
) * spa
->spa_alloc_count
);
10161 spa
->spa_sync_tq
= NULL
;
10165 spa_select_allocator(zio_t
*zio
)
10167 zbookmark_phys_t
*bm
= &zio
->io_bookmark
;
10168 spa_t
*spa
= zio
->io_spa
;
10170 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
10173 * A gang block (for example) may have inherited its parent's
10174 * allocator, in which case there is nothing further to do here.
10176 if (ZIO_HAS_ALLOCATOR(zio
))
10179 ASSERT(spa
!= NULL
);
10180 ASSERT(bm
!= NULL
);
10183 * First try to use an allocator assigned to the syncthread, and set
10184 * the corresponding write issue taskq for the allocator.
10185 * Note, we must have an open pool to do this.
10187 if (spa
->spa_sync_tq
!= NULL
) {
10188 spa_syncthread_info_t
*ti
= spa
->spa_syncthreads
;
10189 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++, ti
++) {
10190 if (ti
->sti_thread
== curthread
) {
10191 zio
->io_allocator
= i
;
10192 zio
->io_wr_iss_tq
= ti
->sti_wr_iss_tq
;
10199 * We want to try to use as many allocators as possible to help improve
10200 * performance, but we also want logically adjacent IOs to be physically
10201 * adjacent to improve sequential read performance. We chunk each object
10202 * into 2^20 block regions, and then hash based on the objset, object,
10203 * level, and region to accomplish both of these goals.
10205 uint64_t hv
= cityhash4(bm
->zb_objset
, bm
->zb_object
, bm
->zb_level
,
10206 bm
->zb_blkid
>> 20);
10208 zio
->io_allocator
= (uint_t
)hv
% spa
->spa_alloc_count
;
10209 zio
->io_wr_iss_tq
= NULL
;
10213 * ==========================================================================
10214 * Miscellaneous routines
10215 * ==========================================================================
10219 * Remove all pools in the system.
10222 spa_evict_all(void)
10227 * Remove all cached state. All pools should be closed now,
10228 * so every spa in the AVL tree should be unreferenced.
10230 mutex_enter(&spa_namespace_lock
);
10231 while ((spa
= spa_next(NULL
)) != NULL
) {
10233 * Stop async tasks. The async thread may need to detach
10234 * a device that's been replaced, which requires grabbing
10235 * spa_namespace_lock, so we must drop it here.
10237 spa_open_ref(spa
, FTAG
);
10238 mutex_exit(&spa_namespace_lock
);
10239 spa_async_suspend(spa
);
10240 mutex_enter(&spa_namespace_lock
);
10241 spa_close(spa
, FTAG
);
10243 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
10245 spa_deactivate(spa
);
10249 mutex_exit(&spa_namespace_lock
);
10253 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
10258 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
10262 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
10263 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
10264 if (vd
->vdev_guid
== guid
)
10268 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
10269 vd
= spa
->spa_spares
.sav_vdevs
[i
];
10270 if (vd
->vdev_guid
== guid
)
10279 spa_upgrade(spa_t
*spa
, uint64_t version
)
10281 ASSERT(spa_writeable(spa
));
10283 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
10286 * This should only be called for a non-faulted pool, and since a
10287 * future version would result in an unopenable pool, this shouldn't be
10290 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
10291 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
10293 spa
->spa_uberblock
.ub_version
= version
;
10294 vdev_config_dirty(spa
->spa_root_vdev
);
10296 spa_config_exit(spa
, SCL_ALL
, FTAG
);
10298 txg_wait_synced(spa_get_dsl(spa
), 0);
10302 spa_has_aux_vdev(spa_t
*spa
, uint64_t guid
, spa_aux_vdev_t
*sav
)
10306 uint64_t vdev_guid
;
10308 for (i
= 0; i
< sav
->sav_count
; i
++)
10309 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
10312 for (i
= 0; i
< sav
->sav_npending
; i
++) {
10313 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
10314 &vdev_guid
) == 0 && vdev_guid
== guid
)
10322 spa_has_l2cache(spa_t
*spa
, uint64_t guid
)
10324 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_l2cache
));
10328 spa_has_spare(spa_t
*spa
, uint64_t guid
)
10330 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_spares
));
10334 * Check if a pool has an active shared spare device.
10335 * Note: reference count of an active spare is 2, as a spare and as a replace
10338 spa_has_active_shared_spare(spa_t
*spa
)
10342 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
10344 for (i
= 0; i
< sav
->sav_count
; i
++) {
10345 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
10346 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
10355 spa_total_metaslabs(spa_t
*spa
)
10357 vdev_t
*rvd
= spa
->spa_root_vdev
;
10360 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
10361 vdev_t
*vd
= rvd
->vdev_child
[c
];
10362 if (!vdev_is_concrete(vd
))
10364 m
+= vd
->vdev_ms_count
;
10370 * Notify any waiting threads that some activity has switched from being in-
10371 * progress to not-in-progress so that the thread can wake up and determine
10372 * whether it is finished waiting.
10375 spa_notify_waiters(spa_t
*spa
)
10378 * Acquiring spa_activities_lock here prevents the cv_broadcast from
10379 * happening between the waiting thread's check and cv_wait.
10381 mutex_enter(&spa
->spa_activities_lock
);
10382 cv_broadcast(&spa
->spa_activities_cv
);
10383 mutex_exit(&spa
->spa_activities_lock
);
10387 * Notify any waiting threads that the pool is exporting, and then block until
10388 * they are finished using the spa_t.
10391 spa_wake_waiters(spa_t
*spa
)
10393 mutex_enter(&spa
->spa_activities_lock
);
10394 spa
->spa_waiters_cancel
= B_TRUE
;
10395 cv_broadcast(&spa
->spa_activities_cv
);
10396 while (spa
->spa_waiters
!= 0)
10397 cv_wait(&spa
->spa_waiters_cv
, &spa
->spa_activities_lock
);
10398 spa
->spa_waiters_cancel
= B_FALSE
;
10399 mutex_exit(&spa
->spa_activities_lock
);
10402 /* Whether the vdev or any of its descendants are being initialized/trimmed. */
10404 spa_vdev_activity_in_progress_impl(vdev_t
*vd
, zpool_wait_activity_t activity
)
10406 spa_t
*spa
= vd
->vdev_spa
;
10408 ASSERT(spa_config_held(spa
, SCL_CONFIG
| SCL_STATE
, RW_READER
));
10409 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
10410 ASSERT(activity
== ZPOOL_WAIT_INITIALIZE
||
10411 activity
== ZPOOL_WAIT_TRIM
);
10413 kmutex_t
*lock
= activity
== ZPOOL_WAIT_INITIALIZE
?
10414 &vd
->vdev_initialize_lock
: &vd
->vdev_trim_lock
;
10416 mutex_exit(&spa
->spa_activities_lock
);
10418 mutex_enter(&spa
->spa_activities_lock
);
10420 boolean_t in_progress
= (activity
== ZPOOL_WAIT_INITIALIZE
) ?
10421 (vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
) :
10422 (vd
->vdev_trim_state
== VDEV_TRIM_ACTIVE
);
10428 for (int i
= 0; i
< vd
->vdev_children
; i
++) {
10429 if (spa_vdev_activity_in_progress_impl(vd
->vdev_child
[i
],
10438 * If use_guid is true, this checks whether the vdev specified by guid is
10439 * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool
10440 * is being initialized/trimmed. The caller must hold the config lock and
10441 * spa_activities_lock.
10444 spa_vdev_activity_in_progress(spa_t
*spa
, boolean_t use_guid
, uint64_t guid
,
10445 zpool_wait_activity_t activity
, boolean_t
*in_progress
)
10447 mutex_exit(&spa
->spa_activities_lock
);
10448 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
10449 mutex_enter(&spa
->spa_activities_lock
);
10453 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
10454 if (vd
== NULL
|| !vd
->vdev_ops
->vdev_op_leaf
) {
10455 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
10459 vd
= spa
->spa_root_vdev
;
10462 *in_progress
= spa_vdev_activity_in_progress_impl(vd
, activity
);
10464 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
10469 * Locking for waiting threads
10470 * ---------------------------
10472 * Waiting threads need a way to check whether a given activity is in progress,
10473 * and then, if it is, wait for it to complete. Each activity will have some
10474 * in-memory representation of the relevant on-disk state which can be used to
10475 * determine whether or not the activity is in progress. The in-memory state and
10476 * the locking used to protect it will be different for each activity, and may
10477 * not be suitable for use with a cvar (e.g., some state is protected by the
10478 * config lock). To allow waiting threads to wait without any races, another
10479 * lock, spa_activities_lock, is used.
10481 * When the state is checked, both the activity-specific lock (if there is one)
10482 * and spa_activities_lock are held. In some cases, the activity-specific lock
10483 * is acquired explicitly (e.g. the config lock). In others, the locking is
10484 * internal to some check (e.g. bpobj_is_empty). After checking, the waiting
10485 * thread releases the activity-specific lock and, if the activity is in
10486 * progress, then cv_waits using spa_activities_lock.
10488 * The waiting thread is woken when another thread, one completing some
10489 * activity, updates the state of the activity and then calls
10490 * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only
10491 * needs to hold its activity-specific lock when updating the state, and this
10492 * lock can (but doesn't have to) be dropped before calling spa_notify_waiters.
10494 * Because spa_notify_waiters acquires spa_activities_lock before broadcasting,
10495 * and because it is held when the waiting thread checks the state of the
10496 * activity, it can never be the case that the completing thread both updates
10497 * the activity state and cv_broadcasts in between the waiting thread's check
10498 * and cv_wait. Thus, a waiting thread can never miss a wakeup.
10500 * In order to prevent deadlock, when the waiting thread does its check, in some
10501 * cases it will temporarily drop spa_activities_lock in order to acquire the
10502 * activity-specific lock. The order in which spa_activities_lock and the
10503 * activity specific lock are acquired in the waiting thread is determined by
10504 * the order in which they are acquired in the completing thread; if the
10505 * completing thread calls spa_notify_waiters with the activity-specific lock
10506 * held, then the waiting thread must also acquire the activity-specific lock
10511 spa_activity_in_progress(spa_t
*spa
, zpool_wait_activity_t activity
,
10512 boolean_t use_tag
, uint64_t tag
, boolean_t
*in_progress
)
10516 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
10518 switch (activity
) {
10519 case ZPOOL_WAIT_CKPT_DISCARD
:
10521 (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
) &&
10522 zap_contains(spa_meta_objset(spa
),
10523 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_ZPOOL_CHECKPOINT
) ==
10526 case ZPOOL_WAIT_FREE
:
10527 *in_progress
= ((spa_version(spa
) >= SPA_VERSION_DEADLISTS
&&
10528 !bpobj_is_empty(&spa
->spa_dsl_pool
->dp_free_bpobj
)) ||
10529 spa_feature_is_active(spa
, SPA_FEATURE_ASYNC_DESTROY
) ||
10530 spa_livelist_delete_check(spa
));
10532 case ZPOOL_WAIT_INITIALIZE
:
10533 case ZPOOL_WAIT_TRIM
:
10534 error
= spa_vdev_activity_in_progress(spa
, use_tag
, tag
,
10535 activity
, in_progress
);
10537 case ZPOOL_WAIT_REPLACE
:
10538 mutex_exit(&spa
->spa_activities_lock
);
10539 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
10540 mutex_enter(&spa
->spa_activities_lock
);
10542 *in_progress
= vdev_replace_in_progress(spa
->spa_root_vdev
);
10543 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
10545 case ZPOOL_WAIT_REMOVE
:
10546 *in_progress
= (spa
->spa_removing_phys
.sr_state
==
10549 case ZPOOL_WAIT_RESILVER
:
10550 *in_progress
= vdev_rebuild_active(spa
->spa_root_vdev
);
10554 case ZPOOL_WAIT_SCRUB
:
10556 boolean_t scanning
, paused
, is_scrub
;
10557 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
10559 is_scrub
= (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
);
10560 scanning
= (scn
->scn_phys
.scn_state
== DSS_SCANNING
);
10561 paused
= dsl_scan_is_paused_scrub(scn
);
10562 *in_progress
= (scanning
&& !paused
&&
10563 is_scrub
== (activity
== ZPOOL_WAIT_SCRUB
));
10566 case ZPOOL_WAIT_RAIDZ_EXPAND
:
10568 vdev_raidz_expand_t
*vre
= spa
->spa_raidz_expand
;
10569 *in_progress
= (vre
!= NULL
&& vre
->vre_state
== DSS_SCANNING
);
10573 panic("unrecognized value for activity %d", activity
);
10580 spa_wait_common(const char *pool
, zpool_wait_activity_t activity
,
10581 boolean_t use_tag
, uint64_t tag
, boolean_t
*waited
)
10584 * The tag is used to distinguish between instances of an activity.
10585 * 'initialize' and 'trim' are the only activities that we use this for.
10586 * The other activities can only have a single instance in progress in a
10587 * pool at one time, making the tag unnecessary.
10589 * There can be multiple devices being replaced at once, but since they
10590 * all finish once resilvering finishes, we don't bother keeping track
10591 * of them individually, we just wait for them all to finish.
10593 if (use_tag
&& activity
!= ZPOOL_WAIT_INITIALIZE
&&
10594 activity
!= ZPOOL_WAIT_TRIM
)
10597 if (activity
< 0 || activity
>= ZPOOL_WAIT_NUM_ACTIVITIES
)
10601 int error
= spa_open(pool
, &spa
, FTAG
);
10606 * Increment the spa's waiter count so that we can call spa_close and
10607 * still ensure that the spa_t doesn't get freed before this thread is
10608 * finished with it when the pool is exported. We want to call spa_close
10609 * before we start waiting because otherwise the additional ref would
10610 * prevent the pool from being exported or destroyed throughout the
10611 * potentially long wait.
10613 mutex_enter(&spa
->spa_activities_lock
);
10614 spa
->spa_waiters
++;
10615 spa_close(spa
, FTAG
);
10619 boolean_t in_progress
;
10620 error
= spa_activity_in_progress(spa
, activity
, use_tag
, tag
,
10623 if (error
|| !in_progress
|| spa
->spa_waiters_cancel
)
10628 if (cv_wait_sig(&spa
->spa_activities_cv
,
10629 &spa
->spa_activities_lock
) == 0) {
10635 spa
->spa_waiters
--;
10636 cv_signal(&spa
->spa_waiters_cv
);
10637 mutex_exit(&spa
->spa_activities_lock
);
10643 * Wait for a particular instance of the specified activity to complete, where
10644 * the instance is identified by 'tag'
10647 spa_wait_tag(const char *pool
, zpool_wait_activity_t activity
, uint64_t tag
,
10650 return (spa_wait_common(pool
, activity
, B_TRUE
, tag
, waited
));
10654 * Wait for all instances of the specified activity complete
10657 spa_wait(const char *pool
, zpool_wait_activity_t activity
, boolean_t
*waited
)
10660 return (spa_wait_common(pool
, activity
, B_FALSE
, 0, waited
));
10664 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
10666 sysevent_t
*ev
= NULL
;
10668 nvlist_t
*resource
;
10670 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
10672 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
10673 ev
->resource
= resource
;
10676 (void) spa
, (void) vd
, (void) hist_nvl
, (void) name
;
10682 spa_event_post(sysevent_t
*ev
)
10686 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
10687 kmem_free(ev
, sizeof (*ev
));
10695 * Post a zevent corresponding to the given sysevent. The 'name' must be one
10696 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
10697 * filled in from the spa and (optionally) the vdev. This doesn't do anything
10698 * in the userland libzpool, as we don't want consumers to misinterpret ztest
10699 * or zdb as real changes.
10702 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
10704 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
10707 /* state manipulation functions */
10708 EXPORT_SYMBOL(spa_open
);
10709 EXPORT_SYMBOL(spa_open_rewind
);
10710 EXPORT_SYMBOL(spa_get_stats
);
10711 EXPORT_SYMBOL(spa_create
);
10712 EXPORT_SYMBOL(spa_import
);
10713 EXPORT_SYMBOL(spa_tryimport
);
10714 EXPORT_SYMBOL(spa_destroy
);
10715 EXPORT_SYMBOL(spa_export
);
10716 EXPORT_SYMBOL(spa_reset
);
10717 EXPORT_SYMBOL(spa_async_request
);
10718 EXPORT_SYMBOL(spa_async_suspend
);
10719 EXPORT_SYMBOL(spa_async_resume
);
10720 EXPORT_SYMBOL(spa_inject_addref
);
10721 EXPORT_SYMBOL(spa_inject_delref
);
10722 EXPORT_SYMBOL(spa_scan_stat_init
);
10723 EXPORT_SYMBOL(spa_scan_get_stats
);
10725 /* device manipulation */
10726 EXPORT_SYMBOL(spa_vdev_add
);
10727 EXPORT_SYMBOL(spa_vdev_attach
);
10728 EXPORT_SYMBOL(spa_vdev_detach
);
10729 EXPORT_SYMBOL(spa_vdev_setpath
);
10730 EXPORT_SYMBOL(spa_vdev_setfru
);
10731 EXPORT_SYMBOL(spa_vdev_split_mirror
);
10733 /* spare statech is global across all pools) */
10734 EXPORT_SYMBOL(spa_spare_add
);
10735 EXPORT_SYMBOL(spa_spare_remove
);
10736 EXPORT_SYMBOL(spa_spare_exists
);
10737 EXPORT_SYMBOL(spa_spare_activate
);
10739 /* L2ARC statech is global across all pools) */
10740 EXPORT_SYMBOL(spa_l2cache_add
);
10741 EXPORT_SYMBOL(spa_l2cache_remove
);
10742 EXPORT_SYMBOL(spa_l2cache_exists
);
10743 EXPORT_SYMBOL(spa_l2cache_activate
);
10744 EXPORT_SYMBOL(spa_l2cache_drop
);
10747 EXPORT_SYMBOL(spa_scan
);
10748 EXPORT_SYMBOL(spa_scan_stop
);
10751 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
10752 EXPORT_SYMBOL(spa_sync_allpools
);
10755 EXPORT_SYMBOL(spa_prop_set
);
10756 EXPORT_SYMBOL(spa_prop_get
);
10757 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
10759 /* asynchronous event notification */
10760 EXPORT_SYMBOL(spa_event_notify
);
10762 ZFS_MODULE_PARAM(zfs_metaslab
, metaslab_
, preload_pct
, UINT
, ZMOD_RW
,
10763 "Percentage of CPUs to run a metaslab preload taskq");
10765 /* BEGIN CSTYLED */
10766 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_shift
, UINT
, ZMOD_RW
,
10767 "log2 fraction of arc that can be used by inflight I/Os when "
10768 "verifying pool during import");
10771 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_metadata
, INT
, ZMOD_RW
,
10772 "Set to traverse metadata on pool import");
10774 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_data
, INT
, ZMOD_RW
,
10775 "Set to traverse data on pool import");
10777 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_print_vdev_tree
, INT
, ZMOD_RW
,
10778 "Print vdev tree to zfs_dbgmsg during pool import");
10780 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_pct
, UINT
, ZMOD_RD
,
10781 "Percentage of CPUs to run an IO worker thread");
10783 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_tpq
, UINT
, ZMOD_RD
,
10784 "Number of threads per IO worker taskqueue");
10786 /* BEGIN CSTYLED */
10787 ZFS_MODULE_PARAM(zfs
, zfs_
, max_missing_tvds
, U64
, ZMOD_RW
,
10788 "Allow importing pool with up to this number of missing top-level "
10789 "vdevs (in read-only mode)");
10792 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_pause
, INT
,
10793 ZMOD_RW
, "Set the livelist condense zthr to pause");
10795 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_pause
, INT
,
10796 ZMOD_RW
, "Set the livelist condense synctask to pause");
10798 /* BEGIN CSTYLED */
10799 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_cancel
,
10801 "Whether livelist condensing was canceled in the synctask");
10803 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_cancel
,
10805 "Whether livelist condensing was canceled in the zthr function");
10807 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, new_alloc
, INT
,
10809 "Whether extra ALLOC blkptrs were added to a livelist entry while it "
10810 "was being condensed");
10813 ZFS_MODULE_VIRTUAL_PARAM_CALL(zfs_zio
, zio_
, taskq_read
,
10814 spa_taskq_read_param_set
, spa_taskq_read_param_get
, ZMOD_RD
,
10815 "Configure IO queues for read IO");
10816 ZFS_MODULE_VIRTUAL_PARAM_CALL(zfs_zio
, zio_
, taskq_write
,
10817 spa_taskq_write_param_set
, spa_taskq_write_param_get
, ZMOD_RD
,
10818 "Configure IO queues for write IO");
10822 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_wr_iss_ncpus
, UINT
, ZMOD_RW
,
10823 "Number of CPUs to run write issue taskqs");