4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2024 by Delphix. All rights reserved.
25 * Copyright (c) 2018, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright 2013 Saso Kiselkov. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
29 * Copyright 2016 Toomas Soome <tsoome@me.com>
30 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
31 * Copyright 2018 Joyent, Inc.
32 * Copyright (c) 2017, 2019, Datto Inc. All rights reserved.
33 * Copyright 2017 Joyent, Inc.
34 * Copyright (c) 2017, Intel Corporation.
35 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
36 * Copyright (c) 2023 Hewlett Packard Enterprise Development LP.
37 * Copyright (c) 2023, 2024, Klara Inc.
41 * SPA: Storage Pool Allocator
43 * This file contains all the routines used when modifying on-disk SPA state.
44 * This includes opening, importing, destroying, exporting a pool, and syncing a
48 #include <sys/zfs_context.h>
49 #include <sys/fm/fs/zfs.h>
50 #include <sys/spa_impl.h>
52 #include <sys/zio_checksum.h>
54 #include <sys/dmu_tx.h>
59 #include <sys/vdev_impl.h>
60 #include <sys/vdev_removal.h>
61 #include <sys/vdev_indirect_mapping.h>
62 #include <sys/vdev_indirect_births.h>
63 #include <sys/vdev_initialize.h>
64 #include <sys/vdev_rebuild.h>
65 #include <sys/vdev_trim.h>
66 #include <sys/vdev_disk.h>
67 #include <sys/vdev_raidz.h>
68 #include <sys/vdev_draid.h>
69 #include <sys/metaslab.h>
70 #include <sys/metaslab_impl.h>
72 #include <sys/uberblock_impl.h>
75 #include <sys/bpobj.h>
76 #include <sys/dmu_traverse.h>
77 #include <sys/dmu_objset.h>
78 #include <sys/unique.h>
79 #include <sys/dsl_pool.h>
80 #include <sys/dsl_dataset.h>
81 #include <sys/dsl_dir.h>
82 #include <sys/dsl_prop.h>
83 #include <sys/dsl_synctask.h>
84 #include <sys/fs/zfs.h>
86 #include <sys/callb.h>
87 #include <sys/systeminfo.h>
88 #include <sys/zfs_ioctl.h>
89 #include <sys/dsl_scan.h>
90 #include <sys/zfeature.h>
91 #include <sys/dsl_destroy.h>
95 #include <sys/fm/protocol.h>
96 #include <sys/fm/util.h>
97 #include <sys/callb.h>
99 #include <sys/vmsystm.h>
102 #include "zfs_prop.h"
103 #include "zfs_comutil.h"
104 #include <cityhash.h>
107 * spa_thread() existed on Illumos as a parent thread for the various worker
108 * threads that actually run the pool, as a way to both reference the entire
109 * pool work as a single object, and to share properties like scheduling
110 * options. It has not yet been adapted to Linux or FreeBSD. This define is
111 * used to mark related parts of the code to make things easier for the reader,
112 * and to compile this code out. It can be removed when someone implements it,
113 * moves it to some Illumos-specific place, or removes it entirely.
115 #undef HAVE_SPA_THREAD
118 * The "System Duty Cycle" scheduling class is an Illumos feature to help
119 * prevent CPU-intensive kernel threads from affecting latency on interactive
120 * threads. It doesn't exist on Linux or FreeBSD, so the supporting code is
121 * gated behind a define. On Illumos SDC depends on spa_thread(), but
122 * spa_thread() also has other uses, so this is a separate define.
127 * The interval, in seconds, at which failed configuration cache file writes
130 int zfs_ccw_retry_interval
= 300;
132 typedef enum zti_modes
{
133 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
134 ZTI_MODE_SCALE
, /* Taskqs scale with CPUs. */
135 ZTI_MODE_SYNC
, /* sync thread assigned */
136 ZTI_MODE_NULL
, /* don't create a taskq */
140 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
141 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
142 #define ZTI_SCALE { ZTI_MODE_SCALE, 0, 1 }
143 #define ZTI_SYNC { ZTI_MODE_SYNC, 0, 1 }
144 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
146 #define ZTI_N(n) ZTI_P(n, 1)
147 #define ZTI_ONE ZTI_N(1)
149 typedef struct zio_taskq_info
{
150 zti_modes_t zti_mode
;
155 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
156 "iss", "iss_h", "int", "int_h"
160 * This table defines the taskq settings for each ZFS I/O type. When
161 * initializing a pool, we use this table to create an appropriately sized
162 * taskq. Some operations are low volume and therefore have a small, static
163 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
164 * macros. Other operations process a large amount of data; the ZTI_SCALE
165 * macro causes us to create a taskq oriented for throughput. Some operations
166 * are so high frequency and short-lived that the taskq itself can become a
167 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
168 * additional degree of parallelism specified by the number of threads per-
169 * taskq and the number of taskqs; when dispatching an event in this case, the
170 * particular taskq is chosen at random. ZTI_SCALE uses a number of taskqs
171 * that scales with the number of CPUs.
173 * The different taskq priorities are to handle the different contexts (issue
174 * and interrupt) and then to reserve threads for high priority I/Os that
175 * need to be handled with minimum delay. Illumos taskq has unfair TQ_FRONT
176 * implementation, so separate high priority threads are used there.
178 static zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
179 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
180 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
181 { ZTI_N(8), ZTI_NULL
, ZTI_SCALE
, ZTI_NULL
}, /* READ */
183 { ZTI_SYNC
, ZTI_N(5), ZTI_SCALE
, ZTI_N(5) }, /* WRITE */
185 { ZTI_SYNC
, ZTI_NULL
, ZTI_SCALE
, ZTI_NULL
}, /* WRITE */
187 { ZTI_SCALE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
188 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
189 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FLUSH */
190 { ZTI_N(4), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* TRIM */
193 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
194 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
195 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
196 static int spa_load_impl(spa_t
*spa
, spa_import_type_t type
,
197 const char **ereport
);
198 static void spa_vdev_resilver_done(spa_t
*spa
);
201 * Percentage of all CPUs that can be used by the metaslab preload taskq.
203 static uint_t metaslab_preload_pct
= 50;
205 static uint_t zio_taskq_batch_pct
= 80; /* 1 thread per cpu in pset */
206 static uint_t zio_taskq_batch_tpq
; /* threads per taskq */
209 static const boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
210 static const uint_t zio_taskq_basedc
= 80; /* base duty cycle */
213 #ifdef HAVE_SPA_THREAD
214 static const boolean_t spa_create_process
= B_TRUE
; /* no process => no sysdc */
217 static uint_t zio_taskq_write_tpq
= 16;
220 * Report any spa_load_verify errors found, but do not fail spa_load.
221 * This is used by zdb to analyze non-idle pools.
223 boolean_t spa_load_verify_dryrun
= B_FALSE
;
226 * Allow read spacemaps in case of readonly import (spa_mode == SPA_MODE_READ).
227 * This is used by zdb for spacemaps verification.
229 boolean_t spa_mode_readable_spacemaps
= B_FALSE
;
232 * This (illegal) pool name is used when temporarily importing a spa_t in order
233 * to get the vdev stats associated with the imported devices.
235 #define TRYIMPORT_NAME "$import"
238 * For debugging purposes: print out vdev tree during pool import.
240 static int spa_load_print_vdev_tree
= B_FALSE
;
243 * A non-zero value for zfs_max_missing_tvds means that we allow importing
244 * pools with missing top-level vdevs. This is strictly intended for advanced
245 * pool recovery cases since missing data is almost inevitable. Pools with
246 * missing devices can only be imported read-only for safety reasons, and their
247 * fail-mode will be automatically set to "continue".
249 * With 1 missing vdev we should be able to import the pool and mount all
250 * datasets. User data that was not modified after the missing device has been
251 * added should be recoverable. This means that snapshots created prior to the
252 * addition of that device should be completely intact.
254 * With 2 missing vdevs, some datasets may fail to mount since there are
255 * dataset statistics that are stored as regular metadata. Some data might be
256 * recoverable if those vdevs were added recently.
258 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
259 * may be missing entirely. Chances of data recovery are very low. Note that
260 * there are also risks of performing an inadvertent rewind as we might be
261 * missing all the vdevs with the latest uberblocks.
263 uint64_t zfs_max_missing_tvds
= 0;
266 * The parameters below are similar to zfs_max_missing_tvds but are only
267 * intended for a preliminary open of the pool with an untrusted config which
268 * might be incomplete or out-dated.
270 * We are more tolerant for pools opened from a cachefile since we could have
271 * an out-dated cachefile where a device removal was not registered.
272 * We could have set the limit arbitrarily high but in the case where devices
273 * are really missing we would want to return the proper error codes; we chose
274 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
275 * and we get a chance to retrieve the trusted config.
277 uint64_t zfs_max_missing_tvds_cachefile
= SPA_DVAS_PER_BP
- 1;
280 * In the case where config was assembled by scanning device paths (/dev/dsks
281 * by default) we are less tolerant since all the existing devices should have
282 * been detected and we want spa_load to return the right error codes.
284 uint64_t zfs_max_missing_tvds_scan
= 0;
287 * Debugging aid that pauses spa_sync() towards the end.
289 static const boolean_t zfs_pause_spa_sync
= B_FALSE
;
292 * Variables to indicate the livelist condense zthr func should wait at certain
293 * points for the livelist to be removed - used to test condense/destroy races
295 static int zfs_livelist_condense_zthr_pause
= 0;
296 static int zfs_livelist_condense_sync_pause
= 0;
299 * Variables to track whether or not condense cancellation has been
300 * triggered in testing.
302 static int zfs_livelist_condense_sync_cancel
= 0;
303 static int zfs_livelist_condense_zthr_cancel
= 0;
306 * Variable to track whether or not extra ALLOC blkptrs were added to a
307 * livelist entry while it was being condensed (caused by the way we track
308 * remapped blkptrs in dbuf_remap_impl)
310 static int zfs_livelist_condense_new_alloc
= 0;
313 * ==========================================================================
314 * SPA properties routines
315 * ==========================================================================
319 * Add a (source=src, propname=propval) list to an nvlist.
322 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, const char *strval
,
323 uint64_t intval
, zprop_source_t src
)
325 const char *propname
= zpool_prop_to_name(prop
);
328 propval
= fnvlist_alloc();
329 fnvlist_add_uint64(propval
, ZPROP_SOURCE
, src
);
332 fnvlist_add_string(propval
, ZPROP_VALUE
, strval
);
334 fnvlist_add_uint64(propval
, ZPROP_VALUE
, intval
);
336 fnvlist_add_nvlist(nvl
, propname
, propval
);
337 nvlist_free(propval
);
341 spa_prop_add(spa_t
*spa
, const char *propname
, nvlist_t
*outnvl
)
343 zpool_prop_t prop
= zpool_name_to_prop(propname
);
344 zprop_source_t src
= ZPROP_SRC_NONE
;
349 * NB: Not all properties lookups via this API require
350 * the spa props lock, so they must explicitly grab it here.
353 case ZPOOL_PROP_DEDUPCACHED
:
354 err
= ddt_get_pool_dedup_cached(spa
, &intval
);
356 return (SET_ERROR(err
));
359 return (SET_ERROR(EINVAL
));
362 spa_prop_add_list(outnvl
, prop
, NULL
, intval
, src
);
368 spa_prop_get_nvlist(spa_t
*spa
, char **props
, unsigned int n_props
,
376 for (unsigned int i
= 0; i
< n_props
&& err
== 0; i
++) {
377 err
= spa_prop_add(spa
, props
[i
], outnvl
);
384 * Add a user property (source=src, propname=propval) to an nvlist.
387 spa_prop_add_user(nvlist_t
*nvl
, const char *propname
, char *strval
,
392 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
393 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
394 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
395 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
396 nvlist_free(propval
);
400 * Get property values from the spa configuration.
403 spa_prop_get_config(spa_t
*spa
, nvlist_t
*nv
)
405 vdev_t
*rvd
= spa
->spa_root_vdev
;
406 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
407 uint64_t size
, alloc
, cap
, version
;
408 const zprop_source_t src
= ZPROP_SRC_NONE
;
409 spa_config_dirent_t
*dp
;
410 metaslab_class_t
*mc
= spa_normal_class(spa
);
412 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
415 alloc
= metaslab_class_get_alloc(mc
);
416 alloc
+= metaslab_class_get_alloc(spa_special_class(spa
));
417 alloc
+= metaslab_class_get_alloc(spa_dedup_class(spa
));
418 alloc
+= metaslab_class_get_alloc(spa_embedded_log_class(spa
));
420 size
= metaslab_class_get_space(mc
);
421 size
+= metaslab_class_get_space(spa_special_class(spa
));
422 size
+= metaslab_class_get_space(spa_dedup_class(spa
));
423 size
+= metaslab_class_get_space(spa_embedded_log_class(spa
));
425 spa_prop_add_list(nv
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
426 spa_prop_add_list(nv
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
427 spa_prop_add_list(nv
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
428 spa_prop_add_list(nv
, ZPOOL_PROP_FREE
, NULL
,
430 spa_prop_add_list(nv
, ZPOOL_PROP_CHECKPOINT
, NULL
,
431 spa
->spa_checkpoint_info
.sci_dspace
, src
);
433 spa_prop_add_list(nv
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
434 metaslab_class_fragmentation(mc
), src
);
435 spa_prop_add_list(nv
, ZPOOL_PROP_EXPANDSZ
, NULL
,
436 metaslab_class_expandable_space(mc
), src
);
437 spa_prop_add_list(nv
, ZPOOL_PROP_READONLY
, NULL
,
438 (spa_mode(spa
) == SPA_MODE_READ
), src
);
440 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
441 spa_prop_add_list(nv
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
443 spa_prop_add_list(nv
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
444 ddt_get_pool_dedup_ratio(spa
), src
);
445 spa_prop_add_list(nv
, ZPOOL_PROP_BCLONEUSED
, NULL
,
446 brt_get_used(spa
), src
);
447 spa_prop_add_list(nv
, ZPOOL_PROP_BCLONESAVED
, NULL
,
448 brt_get_saved(spa
), src
);
449 spa_prop_add_list(nv
, ZPOOL_PROP_BCLONERATIO
, NULL
,
450 brt_get_ratio(spa
), src
);
452 spa_prop_add_list(nv
, ZPOOL_PROP_DEDUP_TABLE_SIZE
, NULL
,
453 ddt_get_ddt_dsize(spa
), src
);
455 spa_prop_add_list(nv
, ZPOOL_PROP_HEALTH
, NULL
,
456 rvd
->vdev_state
, src
);
458 version
= spa_version(spa
);
459 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
460 spa_prop_add_list(nv
, ZPOOL_PROP_VERSION
, NULL
,
461 version
, ZPROP_SRC_DEFAULT
);
463 spa_prop_add_list(nv
, ZPOOL_PROP_VERSION
, NULL
,
464 version
, ZPROP_SRC_LOCAL
);
466 spa_prop_add_list(nv
, ZPOOL_PROP_LOAD_GUID
,
467 NULL
, spa_load_guid(spa
), src
);
472 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
473 * when opening pools before this version freedir will be NULL.
475 if (pool
->dp_free_dir
!= NULL
) {
476 spa_prop_add_list(nv
, ZPOOL_PROP_FREEING
, NULL
,
477 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
480 spa_prop_add_list(nv
, ZPOOL_PROP_FREEING
,
484 if (pool
->dp_leak_dir
!= NULL
) {
485 spa_prop_add_list(nv
, ZPOOL_PROP_LEAKED
, NULL
,
486 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
489 spa_prop_add_list(nv
, ZPOOL_PROP_LEAKED
,
494 spa_prop_add_list(nv
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
496 if (spa
->spa_comment
!= NULL
) {
497 spa_prop_add_list(nv
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
501 if (spa
->spa_compatibility
!= NULL
) {
502 spa_prop_add_list(nv
, ZPOOL_PROP_COMPATIBILITY
,
503 spa
->spa_compatibility
, 0, ZPROP_SRC_LOCAL
);
506 if (spa
->spa_root
!= NULL
)
507 spa_prop_add_list(nv
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
510 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
511 spa_prop_add_list(nv
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
512 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
514 spa_prop_add_list(nv
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
515 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
518 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
519 spa_prop_add_list(nv
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
520 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
522 spa_prop_add_list(nv
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
523 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
526 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
527 if (dp
->scd_path
== NULL
) {
528 spa_prop_add_list(nv
, ZPOOL_PROP_CACHEFILE
,
529 "none", 0, ZPROP_SRC_LOCAL
);
530 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
531 spa_prop_add_list(nv
, ZPOOL_PROP_CACHEFILE
,
532 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
538 * Get zpool property values.
541 spa_prop_get(spa_t
*spa
, nvlist_t
*nv
)
543 objset_t
*mos
= spa
->spa_meta_objset
;
549 dp
= spa_get_dsl(spa
);
550 dsl_pool_config_enter(dp
, FTAG
);
551 za
= zap_attribute_alloc();
552 mutex_enter(&spa
->spa_props_lock
);
555 * Get properties from the spa config.
557 spa_prop_get_config(spa
, nv
);
559 /* If no pool property object, no more prop to get. */
560 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0)
564 * Get properties from the MOS pool property object.
566 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
567 (err
= zap_cursor_retrieve(&zc
, za
)) == 0;
568 zap_cursor_advance(&zc
)) {
571 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
574 if ((prop
= zpool_name_to_prop(za
->za_name
)) ==
575 ZPOOL_PROP_INVAL
&& !zfs_prop_user(za
->za_name
))
578 switch (za
->za_integer_length
) {
580 /* integer property */
581 if (za
->za_first_integer
!=
582 zpool_prop_default_numeric(prop
))
583 src
= ZPROP_SRC_LOCAL
;
585 if (prop
== ZPOOL_PROP_BOOTFS
) {
586 dsl_dataset_t
*ds
= NULL
;
588 err
= dsl_dataset_hold_obj(dp
,
589 za
->za_first_integer
, FTAG
, &ds
);
593 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
595 dsl_dataset_name(ds
, strval
);
596 dsl_dataset_rele(ds
, FTAG
);
599 intval
= za
->za_first_integer
;
602 spa_prop_add_list(nv
, prop
, strval
, intval
, src
);
605 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
610 /* string property */
611 strval
= kmem_alloc(za
->za_num_integers
, KM_SLEEP
);
612 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
613 za
->za_name
, 1, za
->za_num_integers
, strval
);
615 kmem_free(strval
, za
->za_num_integers
);
618 if (prop
!= ZPOOL_PROP_INVAL
) {
619 spa_prop_add_list(nv
, prop
, strval
, 0, src
);
621 src
= ZPROP_SRC_LOCAL
;
622 spa_prop_add_user(nv
, za
->za_name
, strval
,
625 kmem_free(strval
, za
->za_num_integers
);
632 zap_cursor_fini(&zc
);
634 mutex_exit(&spa
->spa_props_lock
);
635 dsl_pool_config_exit(dp
, FTAG
);
636 zap_attribute_free(za
);
638 if (err
&& err
!= ENOENT
)
645 * Validate the given pool properties nvlist and modify the list
646 * for the property values to be set.
649 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
652 int error
= 0, reset_bootfs
= 0;
654 boolean_t has_feature
= B_FALSE
;
657 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
659 const char *strval
, *slash
, *check
, *fname
;
660 const char *propname
= nvpair_name(elem
);
661 zpool_prop_t prop
= zpool_name_to_prop(propname
);
664 case ZPOOL_PROP_INVAL
:
666 * Sanitize the input.
668 if (zfs_prop_user(propname
)) {
669 if (strlen(propname
) >= ZAP_MAXNAMELEN
) {
670 error
= SET_ERROR(ENAMETOOLONG
);
674 if (strlen(fnvpair_value_string(elem
)) >=
676 error
= SET_ERROR(E2BIG
);
679 } else if (zpool_prop_feature(propname
)) {
680 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
681 error
= SET_ERROR(EINVAL
);
685 if (nvpair_value_uint64(elem
, &intval
) != 0) {
686 error
= SET_ERROR(EINVAL
);
691 error
= SET_ERROR(EINVAL
);
695 fname
= strchr(propname
, '@') + 1;
696 if (zfeature_lookup_name(fname
, NULL
) != 0) {
697 error
= SET_ERROR(EINVAL
);
701 has_feature
= B_TRUE
;
703 error
= SET_ERROR(EINVAL
);
708 case ZPOOL_PROP_VERSION
:
709 error
= nvpair_value_uint64(elem
, &intval
);
711 (intval
< spa_version(spa
) ||
712 intval
> SPA_VERSION_BEFORE_FEATURES
||
714 error
= SET_ERROR(EINVAL
);
717 case ZPOOL_PROP_DEDUP_TABLE_QUOTA
:
718 error
= nvpair_value_uint64(elem
, &intval
);
721 case ZPOOL_PROP_DELEGATION
:
722 case ZPOOL_PROP_AUTOREPLACE
:
723 case ZPOOL_PROP_LISTSNAPS
:
724 case ZPOOL_PROP_AUTOEXPAND
:
725 case ZPOOL_PROP_AUTOTRIM
:
726 error
= nvpair_value_uint64(elem
, &intval
);
727 if (!error
&& intval
> 1)
728 error
= SET_ERROR(EINVAL
);
731 case ZPOOL_PROP_MULTIHOST
:
732 error
= nvpair_value_uint64(elem
, &intval
);
733 if (!error
&& intval
> 1)
734 error
= SET_ERROR(EINVAL
);
737 uint32_t hostid
= zone_get_hostid(NULL
);
739 spa
->spa_hostid
= hostid
;
741 error
= SET_ERROR(ENOTSUP
);
746 case ZPOOL_PROP_BOOTFS
:
748 * If the pool version is less than SPA_VERSION_BOOTFS,
749 * or the pool is still being created (version == 0),
750 * the bootfs property cannot be set.
752 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
753 error
= SET_ERROR(ENOTSUP
);
758 * Make sure the vdev config is bootable
760 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
761 error
= SET_ERROR(ENOTSUP
);
767 error
= nvpair_value_string(elem
, &strval
);
772 if (strval
== NULL
|| strval
[0] == '\0') {
773 objnum
= zpool_prop_default_numeric(
778 error
= dmu_objset_hold(strval
, FTAG
, &os
);
783 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
784 error
= SET_ERROR(ENOTSUP
);
786 objnum
= dmu_objset_id(os
);
788 dmu_objset_rele(os
, FTAG
);
792 case ZPOOL_PROP_FAILUREMODE
:
793 error
= nvpair_value_uint64(elem
, &intval
);
794 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
795 error
= SET_ERROR(EINVAL
);
798 * This is a special case which only occurs when
799 * the pool has completely failed. This allows
800 * the user to change the in-core failmode property
801 * without syncing it out to disk (I/Os might
802 * currently be blocked). We do this by returning
803 * EIO to the caller (spa_prop_set) to trick it
804 * into thinking we encountered a property validation
807 if (!error
&& spa_suspended(spa
)) {
808 spa
->spa_failmode
= intval
;
809 error
= SET_ERROR(EIO
);
813 case ZPOOL_PROP_CACHEFILE
:
814 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
817 if (strval
[0] == '\0')
820 if (strcmp(strval
, "none") == 0)
823 if (strval
[0] != '/') {
824 error
= SET_ERROR(EINVAL
);
828 slash
= strrchr(strval
, '/');
829 ASSERT(slash
!= NULL
);
831 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
832 strcmp(slash
, "/..") == 0)
833 error
= SET_ERROR(EINVAL
);
836 case ZPOOL_PROP_COMMENT
:
837 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
839 for (check
= strval
; *check
!= '\0'; check
++) {
840 if (!isprint(*check
)) {
841 error
= SET_ERROR(EINVAL
);
845 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
846 error
= SET_ERROR(E2BIG
);
857 (void) nvlist_remove_all(props
,
858 zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO
));
860 if (!error
&& reset_bootfs
) {
861 error
= nvlist_remove(props
,
862 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
865 error
= nvlist_add_uint64(props
,
866 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
874 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
876 const char *cachefile
;
877 spa_config_dirent_t
*dp
;
879 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
883 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
886 if (cachefile
[0] == '\0')
887 dp
->scd_path
= spa_strdup(spa_config_path
);
888 else if (strcmp(cachefile
, "none") == 0)
891 dp
->scd_path
= spa_strdup(cachefile
);
893 list_insert_head(&spa
->spa_config_list
, dp
);
895 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
899 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
902 nvpair_t
*elem
= NULL
;
903 boolean_t need_sync
= B_FALSE
;
905 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
908 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
909 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
911 if (prop
== ZPOOL_PROP_CACHEFILE
||
912 prop
== ZPOOL_PROP_ALTROOT
||
913 prop
== ZPOOL_PROP_READONLY
)
916 if (prop
== ZPOOL_PROP_INVAL
&&
917 zfs_prop_user(nvpair_name(elem
))) {
922 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
925 if (prop
== ZPOOL_PROP_VERSION
) {
926 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
928 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
929 ver
= SPA_VERSION_FEATURES
;
933 /* Save time if the version is already set. */
934 if (ver
== spa_version(spa
))
938 * In addition to the pool directory object, we might
939 * create the pool properties object, the features for
940 * read object, the features for write object, or the
941 * feature descriptions object.
943 error
= dsl_sync_task(spa
->spa_name
, NULL
,
944 spa_sync_version
, &ver
,
945 6, ZFS_SPACE_CHECK_RESERVED
);
956 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
957 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
964 * If the bootfs property value is dsobj, clear it.
967 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
969 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
970 VERIFY(zap_remove(spa
->spa_meta_objset
,
971 spa
->spa_pool_props_object
,
972 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
978 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
980 uint64_t *newguid __maybe_unused
= arg
;
981 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
982 vdev_t
*rvd
= spa
->spa_root_vdev
;
985 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
986 int error
= (spa_has_checkpoint(spa
)) ?
987 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
988 return (SET_ERROR(error
));
991 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
992 vdev_state
= rvd
->vdev_state
;
993 spa_config_exit(spa
, SCL_STATE
, FTAG
);
995 if (vdev_state
!= VDEV_STATE_HEALTHY
)
996 return (SET_ERROR(ENXIO
));
998 ASSERT3U(spa_guid(spa
), !=, *newguid
);
1004 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
1006 uint64_t *newguid
= arg
;
1007 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
1009 vdev_t
*rvd
= spa
->spa_root_vdev
;
1011 oldguid
= spa_guid(spa
);
1013 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
1014 rvd
->vdev_guid
= *newguid
;
1015 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
1016 vdev_config_dirty(rvd
);
1017 spa_config_exit(spa
, SCL_STATE
, FTAG
);
1019 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
1020 (u_longlong_t
)oldguid
, (u_longlong_t
)*newguid
);
1024 * Change the GUID for the pool. This is done so that we can later
1025 * re-import a pool built from a clone of our own vdevs. We will modify
1026 * the root vdev's guid, our own pool guid, and then mark all of our
1027 * vdevs dirty. Note that we must make sure that all our vdevs are
1028 * online when we do this, or else any vdevs that weren't present
1029 * would be orphaned from our pool. We are also going to issue a
1030 * sysevent to update any watchers.
1032 * The GUID of the pool will be changed to the value pointed to by guidp.
1033 * The GUID may not be set to the reserverd value of 0.
1034 * The new GUID will be generated if guidp is NULL.
1037 spa_change_guid(spa_t
*spa
, const uint64_t *guidp
)
1042 mutex_enter(&spa
->spa_vdev_top_lock
);
1043 mutex_enter(&spa_namespace_lock
);
1045 if (guidp
!= NULL
) {
1048 error
= SET_ERROR(EINVAL
);
1052 if (spa_guid_exists(guid
, 0)) {
1053 error
= SET_ERROR(EEXIST
);
1057 guid
= spa_generate_guid(NULL
);
1060 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
1061 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
1065 * Clear the kobj flag from all the vdevs to allow
1066 * vdev_cache_process_kobj_evt() to post events to all the
1067 * vdevs since GUID is updated.
1069 vdev_clear_kobj_evt(spa
->spa_root_vdev
);
1070 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
1071 vdev_clear_kobj_evt(spa
->spa_l2cache
.sav_vdevs
[i
]);
1073 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
1074 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
1078 mutex_exit(&spa_namespace_lock
);
1079 mutex_exit(&spa
->spa_vdev_top_lock
);
1085 * ==========================================================================
1086 * SPA state manipulation (open/create/destroy/import/export)
1087 * ==========================================================================
1091 spa_error_entry_compare(const void *a
, const void *b
)
1093 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
1094 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
1097 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
1098 sizeof (zbookmark_phys_t
));
1100 return (TREE_ISIGN(ret
));
1104 * Utility function which retrieves copies of the current logs and
1105 * re-initializes them in the process.
1108 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
1110 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
1112 memcpy(last
, &spa
->spa_errlist_last
, sizeof (avl_tree_t
));
1113 memcpy(scrub
, &spa
->spa_errlist_scrub
, sizeof (avl_tree_t
));
1115 avl_create(&spa
->spa_errlist_scrub
,
1116 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1117 offsetof(spa_error_entry_t
, se_avl
));
1118 avl_create(&spa
->spa_errlist_last
,
1119 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1120 offsetof(spa_error_entry_t
, se_avl
));
1124 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1126 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
1127 enum zti_modes mode
= ztip
->zti_mode
;
1128 uint_t value
= ztip
->zti_value
;
1129 uint_t count
= ztip
->zti_count
;
1130 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1131 uint_t cpus
, flags
= TASKQ_DYNAMIC
;
1134 case ZTI_MODE_FIXED
:
1135 ASSERT3U(value
, >, 0);
1141 * Create one wr_iss taskq for every 'zio_taskq_write_tpq' CPUs,
1142 * not to exceed the number of spa allocators, and align to it.
1144 cpus
= MAX(1, boot_ncpus
* zio_taskq_batch_pct
/ 100);
1145 count
= MAX(1, cpus
/ MAX(1, zio_taskq_write_tpq
));
1146 count
= MAX(count
, (zio_taskq_batch_pct
+ 99) / 100);
1147 count
= MIN(count
, spa
->spa_alloc_count
);
1148 while (spa
->spa_alloc_count
% count
!= 0 &&
1149 spa
->spa_alloc_count
< count
* 2)
1153 * zio_taskq_batch_pct is unbounded and may exceed 100%, but no
1154 * single taskq may have more threads than 100% of online cpus.
1156 value
= (zio_taskq_batch_pct
+ count
/ 2) / count
;
1157 value
= MIN(value
, 100);
1158 flags
|= TASKQ_THREADS_CPU_PCT
;
1161 case ZTI_MODE_SCALE
:
1162 flags
|= TASKQ_THREADS_CPU_PCT
;
1164 * We want more taskqs to reduce lock contention, but we want
1165 * less for better request ordering and CPU utilization.
1167 cpus
= MAX(1, boot_ncpus
* zio_taskq_batch_pct
/ 100);
1168 if (zio_taskq_batch_tpq
> 0) {
1169 count
= MAX(1, (cpus
+ zio_taskq_batch_tpq
/ 2) /
1170 zio_taskq_batch_tpq
);
1173 * Prefer 6 threads per taskq, but no more taskqs
1174 * than threads in them on large systems. For 80%:
1177 * cpus taskqs percent threads threads
1178 * ------- ------- ------- ------- -------
1189 count
= 1 + cpus
/ 6;
1190 while (count
* count
> cpus
)
1193 /* Limit each taskq within 100% to not trigger assertion. */
1194 count
= MAX(count
, (zio_taskq_batch_pct
+ 99) / 100);
1195 value
= (zio_taskq_batch_pct
+ count
/ 2) / count
;
1199 tqs
->stqs_count
= 0;
1200 tqs
->stqs_taskq
= NULL
;
1204 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
1205 "spa_taskqs_init()",
1206 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
1210 ASSERT3U(count
, >, 0);
1211 tqs
->stqs_count
= count
;
1212 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
1214 for (uint_t i
= 0; i
< count
; i
++) {
1219 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
1220 zio_type_name
[t
], zio_taskq_types
[q
], i
);
1222 (void) snprintf(name
, sizeof (name
), "%s_%s",
1223 zio_type_name
[t
], zio_taskq_types
[q
]);
1226 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
1227 (void) zio_taskq_basedc
;
1228 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
1229 spa
->spa_proc
, zio_taskq_basedc
, flags
);
1232 pri_t pri
= maxclsyspri
;
1234 * The write issue taskq can be extremely CPU
1235 * intensive. Run it at slightly less important
1236 * priority than the other taskqs.
1238 * Under Linux and FreeBSD this means incrementing
1239 * the priority value as opposed to platforms like
1240 * illumos where it should be decremented.
1242 * On FreeBSD, if priorities divided by four (RQ_PPQ)
1243 * are equal then a difference between them is
1246 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
) {
1247 #if defined(__linux__)
1249 #elif defined(__FreeBSD__)
1255 tq
= taskq_create_proc(name
, value
, pri
, 50,
1256 INT_MAX
, spa
->spa_proc
, flags
);
1261 tqs
->stqs_taskq
[i
] = tq
;
1266 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1268 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1270 if (tqs
->stqs_taskq
== NULL
) {
1271 ASSERT3U(tqs
->stqs_count
, ==, 0);
1275 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
1276 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
1277 taskq_destroy(tqs
->stqs_taskq
[i
]);
1280 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
1281 tqs
->stqs_taskq
= NULL
;
1286 * The READ and WRITE rows of zio_taskqs are configurable at module load time
1287 * by setting zio_taskq_read or zio_taskq_write.
1289 * Example (the defaults for READ and WRITE)
1290 * zio_taskq_read='fixed,1,8 null scale null'
1291 * zio_taskq_write='sync null scale null'
1293 * Each sets the entire row at a time.
1295 * 'fixed' is parameterised: fixed,Q,T where Q is number of taskqs, T is number
1296 * of threads per taskq.
1298 * 'null' can only be set on the high-priority queues (queue selection for
1299 * high-priority queues will fall back to the regular queue if the high-pri
1302 static const char *const modes
[ZTI_NMODES
] = {
1303 "fixed", "scale", "sync", "null"
1306 /* Parse the incoming config string. Modifies cfg */
1308 spa_taskq_param_set(zio_type_t t
, char *cfg
)
1312 zio_taskq_info_t row
[ZIO_TASKQ_TYPES
] = {{0}};
1314 char *next
= cfg
, *tok
, *c
;
1317 * Parse out each element from the string and fill `row`. The entire
1318 * row has to be set at once, so any errors are flagged by just
1319 * breaking out of this loop early.
1322 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1323 /* `next` is the start of the config */
1327 /* Eat up leading space */
1328 while (isspace(*next
))
1333 /* Mode ends at space or end of string */
1335 next
= strchr(tok
, ' ');
1336 if (next
!= NULL
) *next
++ = '\0';
1338 /* Parameters start after a comma */
1339 c
= strchr(tok
, ',');
1340 if (c
!= NULL
) *c
++ = '\0';
1342 /* Match mode string */
1344 for (mode
= 0; mode
< ZTI_NMODES
; mode
++)
1345 if (strcmp(tok
, modes
[mode
]) == 0)
1347 if (mode
== ZTI_NMODES
)
1350 /* Invalid canary */
1351 row
[q
].zti_mode
= ZTI_NMODES
;
1353 /* Per-mode setup */
1357 * FIXED is parameterised: number of queues, and number of
1358 * threads per queue.
1360 case ZTI_MODE_FIXED
: {
1361 /* No parameters? */
1362 if (c
== NULL
|| *c
== '\0')
1365 /* Find next parameter */
1367 c
= strchr(tok
, ',');
1371 /* Take digits and convert */
1372 unsigned long long nq
;
1373 if (!(isdigit(*tok
)))
1375 err
= ddi_strtoull(tok
, &tok
, 10, &nq
);
1376 /* Must succeed and also end at the next param sep */
1377 if (err
!= 0 || tok
!= c
)
1380 /* Move past the comma */
1382 /* Need another number */
1383 if (!(isdigit(*tok
)))
1385 /* Remember start to make sure we moved */
1389 unsigned long long ntpq
;
1390 err
= ddi_strtoull(tok
, &tok
, 10, &ntpq
);
1391 /* Must succeed, and moved forward */
1392 if (err
!= 0 || tok
== c
|| *tok
!= '\0')
1396 * sanity; zero queues/threads make no sense, and
1397 * 16K is almost certainly more than anyone will ever
1398 * need and avoids silly numbers like UINT32_MAX
1400 if (nq
== 0 || nq
>= 16384 ||
1401 ntpq
== 0 || ntpq
>= 16384)
1404 const zio_taskq_info_t zti
= ZTI_P(ntpq
, nq
);
1409 case ZTI_MODE_SCALE
: {
1410 const zio_taskq_info_t zti
= ZTI_SCALE
;
1415 case ZTI_MODE_SYNC
: {
1416 const zio_taskq_info_t zti
= ZTI_SYNC
;
1421 case ZTI_MODE_NULL
: {
1423 * Can only null the high-priority queues; the general-
1424 * purpose ones have to exist.
1426 if (q
!= ZIO_TASKQ_ISSUE_HIGH
&&
1427 q
!= ZIO_TASKQ_INTERRUPT_HIGH
)
1430 const zio_taskq_info_t zti
= ZTI_NULL
;
1439 /* Ensure we set a mode */
1440 if (row
[q
].zti_mode
== ZTI_NMODES
)
1444 /* Didn't get a full row, fail */
1445 if (q
< ZIO_TASKQ_TYPES
)
1446 return (SET_ERROR(EINVAL
));
1448 /* Eat trailing space */
1450 while (isspace(*next
))
1453 /* If there's anything left over then fail */
1454 if (next
!= NULL
&& *next
!= '\0')
1455 return (SET_ERROR(EINVAL
));
1457 /* Success! Copy it into the real config */
1458 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++)
1459 zio_taskqs
[t
][q
] = row
[q
];
1465 spa_taskq_param_get(zio_type_t t
, char *buf
, boolean_t add_newline
)
1469 /* Build paramater string from live config */
1470 const char *sep
= "";
1471 for (uint_t q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1472 const zio_taskq_info_t
*zti
= &zio_taskqs
[t
][q
];
1473 if (zti
->zti_mode
== ZTI_MODE_FIXED
)
1474 pos
+= sprintf(&buf
[pos
], "%s%s,%u,%u", sep
,
1475 modes
[zti
->zti_mode
], zti
->zti_count
,
1478 pos
+= sprintf(&buf
[pos
], "%s%s", sep
,
1479 modes
[zti
->zti_mode
]);
1492 spa_taskq_read_param_set(const char *val
, zfs_kernel_param_t
*kp
)
1494 char *cfg
= kmem_strdup(val
);
1495 int err
= spa_taskq_param_set(ZIO_TYPE_READ
, cfg
);
1496 kmem_free(cfg
, strlen(val
)+1);
1500 spa_taskq_read_param_get(char *buf
, zfs_kernel_param_t
*kp
)
1502 return (spa_taskq_param_get(ZIO_TYPE_READ
, buf
, TRUE
));
1506 spa_taskq_write_param_set(const char *val
, zfs_kernel_param_t
*kp
)
1508 char *cfg
= kmem_strdup(val
);
1509 int err
= spa_taskq_param_set(ZIO_TYPE_WRITE
, cfg
);
1510 kmem_free(cfg
, strlen(val
)+1);
1514 spa_taskq_write_param_get(char *buf
, zfs_kernel_param_t
*kp
)
1516 return (spa_taskq_param_get(ZIO_TYPE_WRITE
, buf
, TRUE
));
1520 * On FreeBSD load-time parameters can be set up before malloc() is available,
1521 * so we have to do all the parsing work on the stack.
1523 #define SPA_TASKQ_PARAM_MAX (128)
1526 spa_taskq_read_param(ZFS_MODULE_PARAM_ARGS
)
1528 char buf
[SPA_TASKQ_PARAM_MAX
];
1531 (void) spa_taskq_param_get(ZIO_TYPE_READ
, buf
, FALSE
);
1532 err
= sysctl_handle_string(oidp
, buf
, sizeof (buf
), req
);
1533 if (err
|| req
->newptr
== NULL
)
1535 return (spa_taskq_param_set(ZIO_TYPE_READ
, buf
));
1539 spa_taskq_write_param(ZFS_MODULE_PARAM_ARGS
)
1541 char buf
[SPA_TASKQ_PARAM_MAX
];
1544 (void) spa_taskq_param_get(ZIO_TYPE_WRITE
, buf
, FALSE
);
1545 err
= sysctl_handle_string(oidp
, buf
, sizeof (buf
), req
);
1546 if (err
|| req
->newptr
== NULL
)
1548 return (spa_taskq_param_set(ZIO_TYPE_WRITE
, buf
));
1551 #endif /* _KERNEL */
1554 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1555 * Note that a type may have multiple discrete taskqs to avoid lock contention
1556 * on the taskq itself.
1559 spa_taskq_dispatch(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1560 task_func_t
*func
, zio_t
*zio
, boolean_t cutinline
)
1562 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1565 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1566 ASSERT3U(tqs
->stqs_count
, !=, 0);
1569 * NB: We are assuming that the zio can only be dispatched
1570 * to a single taskq at a time. It would be a grievous error
1571 * to dispatch the zio to another taskq at the same time.
1574 ASSERT(taskq_empty_ent(&zio
->io_tqent
));
1576 if (tqs
->stqs_count
== 1) {
1577 tq
= tqs
->stqs_taskq
[0];
1578 } else if ((t
== ZIO_TYPE_WRITE
) && (q
== ZIO_TASKQ_ISSUE
) &&
1579 ZIO_HAS_ALLOCATOR(zio
)) {
1580 tq
= tqs
->stqs_taskq
[zio
->io_allocator
% tqs
->stqs_count
];
1582 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1585 taskq_dispatch_ent(tq
, func
, zio
, cutinline
? TQ_FRONT
: 0,
1590 spa_create_zio_taskqs(spa_t
*spa
)
1592 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1593 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1594 spa_taskqs_init(spa
, t
, q
);
1599 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1601 spa_thread(void *arg
)
1603 psetid_t zio_taskq_psrset_bind
= PS_NONE
;
1604 callb_cpr_t cprinfo
;
1607 user_t
*pu
= PTOU(curproc
);
1609 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1612 ASSERT(curproc
!= &p0
);
1613 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1614 "zpool-%s", spa
->spa_name
);
1615 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1617 /* bind this thread to the requested psrset */
1618 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1620 mutex_enter(&cpu_lock
);
1621 mutex_enter(&pidlock
);
1622 mutex_enter(&curproc
->p_lock
);
1624 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1625 0, NULL
, NULL
) == 0) {
1626 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1629 "Couldn't bind process for zfs pool \"%s\" to "
1630 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1633 mutex_exit(&curproc
->p_lock
);
1634 mutex_exit(&pidlock
);
1635 mutex_exit(&cpu_lock
);
1640 if (zio_taskq_sysdc
) {
1641 sysdc_thread_enter(curthread
, 100, 0);
1645 spa
->spa_proc
= curproc
;
1646 spa
->spa_did
= curthread
->t_did
;
1648 spa_create_zio_taskqs(spa
);
1650 mutex_enter(&spa
->spa_proc_lock
);
1651 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1653 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1654 cv_broadcast(&spa
->spa_proc_cv
);
1656 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1657 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1658 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1659 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1661 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1662 spa
->spa_proc_state
= SPA_PROC_GONE
;
1663 spa
->spa_proc
= &p0
;
1664 cv_broadcast(&spa
->spa_proc_cv
);
1665 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1667 mutex_enter(&curproc
->p_lock
);
1672 extern metaslab_ops_t
*metaslab_allocator(spa_t
*spa
);
1675 * Activate an uninitialized pool.
1678 spa_activate(spa_t
*spa
, spa_mode_t mode
)
1680 metaslab_ops_t
*msp
= metaslab_allocator(spa
);
1681 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1683 spa
->spa_state
= POOL_STATE_ACTIVE
;
1684 spa
->spa_mode
= mode
;
1685 spa
->spa_read_spacemaps
= spa_mode_readable_spacemaps
;
1687 spa
->spa_normal_class
= metaslab_class_create(spa
, msp
);
1688 spa
->spa_log_class
= metaslab_class_create(spa
, msp
);
1689 spa
->spa_embedded_log_class
= metaslab_class_create(spa
, msp
);
1690 spa
->spa_special_class
= metaslab_class_create(spa
, msp
);
1691 spa
->spa_dedup_class
= metaslab_class_create(spa
, msp
);
1693 /* Try to create a covering process */
1694 mutex_enter(&spa
->spa_proc_lock
);
1695 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1696 ASSERT(spa
->spa_proc
== &p0
);
1699 #ifdef HAVE_SPA_THREAD
1700 /* Only create a process if we're going to be around a while. */
1701 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1702 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1704 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1705 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1706 cv_wait(&spa
->spa_proc_cv
,
1707 &spa
->spa_proc_lock
);
1709 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1710 ASSERT(spa
->spa_proc
!= &p0
);
1711 ASSERT(spa
->spa_did
!= 0);
1715 "Couldn't create process for zfs pool \"%s\"\n",
1720 #endif /* HAVE_SPA_THREAD */
1721 mutex_exit(&spa
->spa_proc_lock
);
1723 /* If we didn't create a process, we need to create our taskqs. */
1724 if (spa
->spa_proc
== &p0
) {
1725 spa_create_zio_taskqs(spa
);
1728 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1729 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
,
1733 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1734 offsetof(vdev_t
, vdev_config_dirty_node
));
1735 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1736 offsetof(objset_t
, os_evicting_node
));
1737 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1738 offsetof(vdev_t
, vdev_state_dirty_node
));
1740 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1741 offsetof(struct vdev
, vdev_txg_node
));
1743 avl_create(&spa
->spa_errlist_scrub
,
1744 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1745 offsetof(spa_error_entry_t
, se_avl
));
1746 avl_create(&spa
->spa_errlist_last
,
1747 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1748 offsetof(spa_error_entry_t
, se_avl
));
1749 avl_create(&spa
->spa_errlist_healed
,
1750 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1751 offsetof(spa_error_entry_t
, se_avl
));
1753 spa_activate_os(spa
);
1755 spa_keystore_init(&spa
->spa_keystore
);
1758 * This taskq is used to perform zvol-minor-related tasks
1759 * asynchronously. This has several advantages, including easy
1760 * resolution of various deadlocks.
1762 * The taskq must be single threaded to ensure tasks are always
1763 * processed in the order in which they were dispatched.
1765 * A taskq per pool allows one to keep the pools independent.
1766 * This way if one pool is suspended, it will not impact another.
1768 * The preferred location to dispatch a zvol minor task is a sync
1769 * task. In this context, there is easy access to the spa_t and minimal
1770 * error handling is required because the sync task must succeed.
1772 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1776 * The taskq to preload metaslabs.
1778 spa
->spa_metaslab_taskq
= taskq_create("z_metaslab",
1779 metaslab_preload_pct
, maxclsyspri
, 1, INT_MAX
,
1780 TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1783 * Taskq dedicated to prefetcher threads: this is used to prevent the
1784 * pool traverse code from monopolizing the global (and limited)
1785 * system_taskq by inappropriately scheduling long running tasks on it.
1787 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", 100,
1788 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1791 * The taskq to upgrade datasets in this pool. Currently used by
1792 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1794 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", 100,
1795 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1799 * Opposite of spa_activate().
1802 spa_deactivate(spa_t
*spa
)
1804 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1805 ASSERT(spa
->spa_dsl_pool
== NULL
);
1806 ASSERT(spa
->spa_root_vdev
== NULL
);
1807 ASSERT(spa
->spa_async_zio_root
== NULL
);
1808 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1810 spa_evicting_os_wait(spa
);
1812 if (spa
->spa_zvol_taskq
) {
1813 taskq_destroy(spa
->spa_zvol_taskq
);
1814 spa
->spa_zvol_taskq
= NULL
;
1817 if (spa
->spa_metaslab_taskq
) {
1818 taskq_destroy(spa
->spa_metaslab_taskq
);
1819 spa
->spa_metaslab_taskq
= NULL
;
1822 if (spa
->spa_prefetch_taskq
) {
1823 taskq_destroy(spa
->spa_prefetch_taskq
);
1824 spa
->spa_prefetch_taskq
= NULL
;
1827 if (spa
->spa_upgrade_taskq
) {
1828 taskq_destroy(spa
->spa_upgrade_taskq
);
1829 spa
->spa_upgrade_taskq
= NULL
;
1832 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1834 list_destroy(&spa
->spa_config_dirty_list
);
1835 list_destroy(&spa
->spa_evicting_os_list
);
1836 list_destroy(&spa
->spa_state_dirty_list
);
1838 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1840 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1841 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1842 spa_taskqs_fini(spa
, t
, q
);
1846 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1847 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1848 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1849 spa
->spa_txg_zio
[i
] = NULL
;
1852 metaslab_class_destroy(spa
->spa_normal_class
);
1853 spa
->spa_normal_class
= NULL
;
1855 metaslab_class_destroy(spa
->spa_log_class
);
1856 spa
->spa_log_class
= NULL
;
1858 metaslab_class_destroy(spa
->spa_embedded_log_class
);
1859 spa
->spa_embedded_log_class
= NULL
;
1861 metaslab_class_destroy(spa
->spa_special_class
);
1862 spa
->spa_special_class
= NULL
;
1864 metaslab_class_destroy(spa
->spa_dedup_class
);
1865 spa
->spa_dedup_class
= NULL
;
1868 * If this was part of an import or the open otherwise failed, we may
1869 * still have errors left in the queues. Empty them just in case.
1871 spa_errlog_drain(spa
);
1872 avl_destroy(&spa
->spa_errlist_scrub
);
1873 avl_destroy(&spa
->spa_errlist_last
);
1874 avl_destroy(&spa
->spa_errlist_healed
);
1876 spa_keystore_fini(&spa
->spa_keystore
);
1878 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1880 mutex_enter(&spa
->spa_proc_lock
);
1881 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1882 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1883 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1884 cv_broadcast(&spa
->spa_proc_cv
);
1885 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1886 ASSERT(spa
->spa_proc
!= &p0
);
1887 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1889 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1890 spa
->spa_proc_state
= SPA_PROC_NONE
;
1892 ASSERT(spa
->spa_proc
== &p0
);
1893 mutex_exit(&spa
->spa_proc_lock
);
1896 * We want to make sure spa_thread() has actually exited the ZFS
1897 * module, so that the module can't be unloaded out from underneath
1900 if (spa
->spa_did
!= 0) {
1901 thread_join(spa
->spa_did
);
1905 spa_deactivate_os(spa
);
1910 * Verify a pool configuration, and construct the vdev tree appropriately. This
1911 * will create all the necessary vdevs in the appropriate layout, with each vdev
1912 * in the CLOSED state. This will prep the pool before open/creation/import.
1913 * All vdev validation is done by the vdev_alloc() routine.
1916 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1917 uint_t id
, int atype
)
1923 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1926 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1929 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1932 if (error
== ENOENT
)
1938 return (SET_ERROR(EINVAL
));
1941 for (int c
= 0; c
< children
; c
++) {
1943 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1951 ASSERT(*vdp
!= NULL
);
1957 spa_should_flush_logs_on_unload(spa_t
*spa
)
1959 if (!spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
))
1962 if (!spa_writeable(spa
))
1965 if (!spa
->spa_sync_on
)
1968 if (spa_state(spa
) != POOL_STATE_EXPORTED
)
1971 if (zfs_keep_log_spacemaps_at_export
)
1978 * Opens a transaction that will set the flag that will instruct
1979 * spa_sync to attempt to flush all the metaslabs for that txg.
1982 spa_unload_log_sm_flush_all(spa_t
*spa
)
1984 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
1985 VERIFY0(dmu_tx_assign(tx
, TXG_WAIT
));
1987 ASSERT3U(spa
->spa_log_flushall_txg
, ==, 0);
1988 spa
->spa_log_flushall_txg
= dmu_tx_get_txg(tx
);
1991 txg_wait_synced(spa_get_dsl(spa
), spa
->spa_log_flushall_txg
);
1995 spa_unload_log_sm_metadata(spa_t
*spa
)
1997 void *cookie
= NULL
;
1999 log_summary_entry_t
*e
;
2001 while ((sls
= avl_destroy_nodes(&spa
->spa_sm_logs_by_txg
,
2002 &cookie
)) != NULL
) {
2003 VERIFY0(sls
->sls_mscount
);
2004 kmem_free(sls
, sizeof (spa_log_sm_t
));
2007 while ((e
= list_remove_head(&spa
->spa_log_summary
)) != NULL
) {
2008 VERIFY0(e
->lse_mscount
);
2009 kmem_free(e
, sizeof (log_summary_entry_t
));
2012 spa
->spa_unflushed_stats
.sus_nblocks
= 0;
2013 spa
->spa_unflushed_stats
.sus_memused
= 0;
2014 spa
->spa_unflushed_stats
.sus_blocklimit
= 0;
2018 spa_destroy_aux_threads(spa_t
*spa
)
2020 if (spa
->spa_condense_zthr
!= NULL
) {
2021 zthr_destroy(spa
->spa_condense_zthr
);
2022 spa
->spa_condense_zthr
= NULL
;
2024 if (spa
->spa_checkpoint_discard_zthr
!= NULL
) {
2025 zthr_destroy(spa
->spa_checkpoint_discard_zthr
);
2026 spa
->spa_checkpoint_discard_zthr
= NULL
;
2028 if (spa
->spa_livelist_delete_zthr
!= NULL
) {
2029 zthr_destroy(spa
->spa_livelist_delete_zthr
);
2030 spa
->spa_livelist_delete_zthr
= NULL
;
2032 if (spa
->spa_livelist_condense_zthr
!= NULL
) {
2033 zthr_destroy(spa
->spa_livelist_condense_zthr
);
2034 spa
->spa_livelist_condense_zthr
= NULL
;
2036 if (spa
->spa_raidz_expand_zthr
!= NULL
) {
2037 zthr_destroy(spa
->spa_raidz_expand_zthr
);
2038 spa
->spa_raidz_expand_zthr
= NULL
;
2043 * Opposite of spa_load().
2046 spa_unload(spa_t
*spa
)
2048 ASSERT(MUTEX_HELD(&spa_namespace_lock
) ||
2049 spa
->spa_export_thread
== curthread
);
2050 ASSERT(spa_state(spa
) != POOL_STATE_UNINITIALIZED
);
2052 spa_import_progress_remove(spa_guid(spa
));
2053 spa_load_note(spa
, "UNLOADING");
2055 spa_wake_waiters(spa
);
2058 * If we have set the spa_final_txg, we have already performed the
2059 * tasks below in spa_export_common(). We should not redo it here since
2060 * we delay the final TXGs beyond what spa_final_txg is set at.
2062 if (spa
->spa_final_txg
== UINT64_MAX
) {
2064 * If the log space map feature is enabled and the pool is
2065 * getting exported (but not destroyed), we want to spend some
2066 * time flushing as many metaslabs as we can in an attempt to
2067 * destroy log space maps and save import time.
2069 if (spa_should_flush_logs_on_unload(spa
))
2070 spa_unload_log_sm_flush_all(spa
);
2075 spa_async_suspend(spa
);
2077 if (spa
->spa_root_vdev
) {
2078 vdev_t
*root_vdev
= spa
->spa_root_vdev
;
2079 vdev_initialize_stop_all(root_vdev
,
2080 VDEV_INITIALIZE_ACTIVE
);
2081 vdev_trim_stop_all(root_vdev
, VDEV_TRIM_ACTIVE
);
2082 vdev_autotrim_stop_all(spa
);
2083 vdev_rebuild_stop_all(spa
);
2090 if (spa
->spa_sync_on
) {
2091 txg_sync_stop(spa
->spa_dsl_pool
);
2092 spa
->spa_sync_on
= B_FALSE
;
2096 * This ensures that there is no async metaslab prefetching
2097 * while we attempt to unload the spa.
2099 taskq_wait(spa
->spa_metaslab_taskq
);
2101 if (spa
->spa_mmp
.mmp_thread
)
2102 mmp_thread_stop(spa
);
2105 * Wait for any outstanding async I/O to complete.
2107 if (spa
->spa_async_zio_root
!= NULL
) {
2108 for (int i
= 0; i
< max_ncpus
; i
++)
2109 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
2110 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
2111 spa
->spa_async_zio_root
= NULL
;
2114 if (spa
->spa_vdev_removal
!= NULL
) {
2115 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
2116 spa
->spa_vdev_removal
= NULL
;
2119 spa_destroy_aux_threads(spa
);
2121 spa_condense_fini(spa
);
2123 bpobj_close(&spa
->spa_deferred_bpobj
);
2125 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
2130 if (spa
->spa_root_vdev
)
2131 vdev_free(spa
->spa_root_vdev
);
2132 ASSERT(spa
->spa_root_vdev
== NULL
);
2135 * Close the dsl pool.
2137 if (spa
->spa_dsl_pool
) {
2138 dsl_pool_close(spa
->spa_dsl_pool
);
2139 spa
->spa_dsl_pool
= NULL
;
2140 spa
->spa_meta_objset
= NULL
;
2145 spa_unload_log_sm_metadata(spa
);
2148 * Drop and purge level 2 cache
2150 spa_l2cache_drop(spa
);
2152 if (spa
->spa_spares
.sav_vdevs
) {
2153 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
2154 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
2155 kmem_free(spa
->spa_spares
.sav_vdevs
,
2156 spa
->spa_spares
.sav_count
* sizeof (void *));
2157 spa
->spa_spares
.sav_vdevs
= NULL
;
2159 if (spa
->spa_spares
.sav_config
) {
2160 nvlist_free(spa
->spa_spares
.sav_config
);
2161 spa
->spa_spares
.sav_config
= NULL
;
2163 spa
->spa_spares
.sav_count
= 0;
2165 if (spa
->spa_l2cache
.sav_vdevs
) {
2166 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
2167 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
2168 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
2170 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
2171 spa
->spa_l2cache
.sav_count
* sizeof (void *));
2172 spa
->spa_l2cache
.sav_vdevs
= NULL
;
2174 if (spa
->spa_l2cache
.sav_config
) {
2175 nvlist_free(spa
->spa_l2cache
.sav_config
);
2176 spa
->spa_l2cache
.sav_config
= NULL
;
2178 spa
->spa_l2cache
.sav_count
= 0;
2180 spa
->spa_async_suspended
= 0;
2182 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
2184 if (spa
->spa_comment
!= NULL
) {
2185 spa_strfree(spa
->spa_comment
);
2186 spa
->spa_comment
= NULL
;
2188 if (spa
->spa_compatibility
!= NULL
) {
2189 spa_strfree(spa
->spa_compatibility
);
2190 spa
->spa_compatibility
= NULL
;
2193 spa
->spa_raidz_expand
= NULL
;
2195 spa_config_exit(spa
, SCL_ALL
, spa
);
2199 * Load (or re-load) the current list of vdevs describing the active spares for
2200 * this pool. When this is called, we have some form of basic information in
2201 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
2202 * then re-generate a more complete list including status information.
2205 spa_load_spares(spa_t
*spa
)
2214 * zdb opens both the current state of the pool and the
2215 * checkpointed state (if present), with a different spa_t.
2217 * As spare vdevs are shared among open pools, we skip loading
2218 * them when we load the checkpointed state of the pool.
2220 if (!spa_writeable(spa
))
2224 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
2227 * First, close and free any existing spare vdevs.
2229 if (spa
->spa_spares
.sav_vdevs
) {
2230 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
2231 vd
= spa
->spa_spares
.sav_vdevs
[i
];
2233 /* Undo the call to spa_activate() below */
2234 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
2235 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
2236 spa_spare_remove(tvd
);
2241 kmem_free(spa
->spa_spares
.sav_vdevs
,
2242 spa
->spa_spares
.sav_count
* sizeof (void *));
2245 if (spa
->spa_spares
.sav_config
== NULL
)
2248 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
2249 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
2251 spa
->spa_spares
.sav_count
= (int)nspares
;
2252 spa
->spa_spares
.sav_vdevs
= NULL
;
2258 * Construct the array of vdevs, opening them to get status in the
2259 * process. For each spare, there is potentially two different vdev_t
2260 * structures associated with it: one in the list of spares (used only
2261 * for basic validation purposes) and one in the active vdev
2262 * configuration (if it's spared in). During this phase we open and
2263 * validate each vdev on the spare list. If the vdev also exists in the
2264 * active configuration, then we also mark this vdev as an active spare.
2266 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
2268 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
2269 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
2270 VDEV_ALLOC_SPARE
) == 0);
2273 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
2275 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
2276 B_FALSE
)) != NULL
) {
2277 if (!tvd
->vdev_isspare
)
2281 * We only mark the spare active if we were successfully
2282 * able to load the vdev. Otherwise, importing a pool
2283 * with a bad active spare would result in strange
2284 * behavior, because multiple pool would think the spare
2285 * is actively in use.
2287 * There is a vulnerability here to an equally bizarre
2288 * circumstance, where a dead active spare is later
2289 * brought back to life (onlined or otherwise). Given
2290 * the rarity of this scenario, and the extra complexity
2291 * it adds, we ignore the possibility.
2293 if (!vdev_is_dead(tvd
))
2294 spa_spare_activate(tvd
);
2298 vd
->vdev_aux
= &spa
->spa_spares
;
2300 if (vdev_open(vd
) != 0)
2303 if (vdev_validate_aux(vd
) == 0)
2308 * Recompute the stashed list of spares, with status information
2311 fnvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
);
2313 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
2315 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
2316 spares
[i
] = vdev_config_generate(spa
,
2317 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
2318 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
2319 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
2320 spa
->spa_spares
.sav_count
);
2321 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
2322 nvlist_free(spares
[i
]);
2323 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
2327 * Load (or re-load) the current list of vdevs describing the active l2cache for
2328 * this pool. When this is called, we have some form of basic information in
2329 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
2330 * then re-generate a more complete list including status information.
2331 * Devices which are already active have their details maintained, and are
2335 spa_load_l2cache(spa_t
*spa
)
2337 nvlist_t
**l2cache
= NULL
;
2339 int i
, j
, oldnvdevs
;
2341 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
2342 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
2346 * zdb opens both the current state of the pool and the
2347 * checkpointed state (if present), with a different spa_t.
2349 * As L2 caches are part of the ARC which is shared among open
2350 * pools, we skip loading them when we load the checkpointed
2351 * state of the pool.
2353 if (!spa_writeable(spa
))
2357 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
2359 oldvdevs
= sav
->sav_vdevs
;
2360 oldnvdevs
= sav
->sav_count
;
2361 sav
->sav_vdevs
= NULL
;
2364 if (sav
->sav_config
== NULL
) {
2370 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
,
2371 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
2372 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
2375 * Process new nvlist of vdevs.
2377 for (i
= 0; i
< nl2cache
; i
++) {
2378 guid
= fnvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
);
2381 for (j
= 0; j
< oldnvdevs
; j
++) {
2383 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
2385 * Retain previous vdev for add/remove ops.
2393 if (newvdevs
[i
] == NULL
) {
2397 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
2398 VDEV_ALLOC_L2CACHE
) == 0);
2403 * Commit this vdev as an l2cache device,
2404 * even if it fails to open.
2406 spa_l2cache_add(vd
);
2411 spa_l2cache_activate(vd
);
2413 if (vdev_open(vd
) != 0)
2416 (void) vdev_validate_aux(vd
);
2418 if (!vdev_is_dead(vd
))
2419 l2arc_add_vdev(spa
, vd
);
2422 * Upon cache device addition to a pool or pool
2423 * creation with a cache device or if the header
2424 * of the device is invalid we issue an async
2425 * TRIM command for the whole device which will
2426 * execute if l2arc_trim_ahead > 0.
2428 spa_async_request(spa
, SPA_ASYNC_L2CACHE_TRIM
);
2432 sav
->sav_vdevs
= newvdevs
;
2433 sav
->sav_count
= (int)nl2cache
;
2436 * Recompute the stashed list of l2cache devices, with status
2437 * information this time.
2439 fnvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
);
2441 if (sav
->sav_count
> 0)
2442 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
2444 for (i
= 0; i
< sav
->sav_count
; i
++)
2445 l2cache
[i
] = vdev_config_generate(spa
,
2446 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
2447 fnvlist_add_nvlist_array(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
2448 (const nvlist_t
* const *)l2cache
, sav
->sav_count
);
2452 * Purge vdevs that were dropped
2455 for (i
= 0; i
< oldnvdevs
; i
++) {
2460 ASSERT(vd
->vdev_isl2cache
);
2462 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
2463 pool
!= 0ULL && l2arc_vdev_present(vd
))
2464 l2arc_remove_vdev(vd
);
2465 vdev_clear_stats(vd
);
2470 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
2473 for (i
= 0; i
< sav
->sav_count
; i
++)
2474 nvlist_free(l2cache
[i
]);
2476 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
2480 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
2483 char *packed
= NULL
;
2488 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
2492 nvsize
= *(uint64_t *)db
->db_data
;
2493 dmu_buf_rele(db
, FTAG
);
2495 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
2496 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
2499 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
2500 vmem_free(packed
, nvsize
);
2506 * Concrete top-level vdevs that are not missing and are not logs. At every
2507 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
2510 spa_healthy_core_tvds(spa_t
*spa
)
2512 vdev_t
*rvd
= spa
->spa_root_vdev
;
2515 for (uint64_t i
= 0; i
< rvd
->vdev_children
; i
++) {
2516 vdev_t
*vd
= rvd
->vdev_child
[i
];
2519 if (vdev_is_concrete(vd
) && !vdev_is_dead(vd
))
2527 * Checks to see if the given vdev could not be opened, in which case we post a
2528 * sysevent to notify the autoreplace code that the device has been removed.
2531 spa_check_removed(vdev_t
*vd
)
2533 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
2534 spa_check_removed(vd
->vdev_child
[c
]);
2536 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
2537 vdev_is_concrete(vd
)) {
2538 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
2539 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
2544 spa_check_for_missing_logs(spa_t
*spa
)
2546 vdev_t
*rvd
= spa
->spa_root_vdev
;
2549 * If we're doing a normal import, then build up any additional
2550 * diagnostic information about missing log devices.
2551 * We'll pass this up to the user for further processing.
2553 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
2554 nvlist_t
**child
, *nv
;
2557 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
2559 nv
= fnvlist_alloc();
2561 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2562 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2565 * We consider a device as missing only if it failed
2566 * to open (i.e. offline or faulted is not considered
2569 if (tvd
->vdev_islog
&&
2570 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2571 child
[idx
++] = vdev_config_generate(spa
, tvd
,
2572 B_FALSE
, VDEV_CONFIG_MISSING
);
2577 fnvlist_add_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2578 (const nvlist_t
* const *)child
, idx
);
2579 fnvlist_add_nvlist(spa
->spa_load_info
,
2580 ZPOOL_CONFIG_MISSING_DEVICES
, nv
);
2582 for (uint64_t i
= 0; i
< idx
; i
++)
2583 nvlist_free(child
[i
]);
2586 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
2589 spa_load_failed(spa
, "some log devices are missing");
2590 vdev_dbgmsg_print_tree(rvd
, 2);
2591 return (SET_ERROR(ENXIO
));
2594 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2595 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2597 if (tvd
->vdev_islog
&&
2598 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2599 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2600 spa_load_note(spa
, "some log devices are "
2601 "missing, ZIL is dropped.");
2602 vdev_dbgmsg_print_tree(rvd
, 2);
2612 * Check for missing log devices
2615 spa_check_logs(spa_t
*spa
)
2617 boolean_t rv
= B_FALSE
;
2618 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2620 switch (spa
->spa_log_state
) {
2623 case SPA_LOG_MISSING
:
2624 /* need to recheck in case slog has been restored */
2625 case SPA_LOG_UNKNOWN
:
2626 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2627 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
2629 spa_set_log_state(spa
, SPA_LOG_MISSING
);
2636 * Passivate any log vdevs (note, does not apply to embedded log metaslabs).
2639 spa_passivate_log(spa_t
*spa
)
2641 vdev_t
*rvd
= spa
->spa_root_vdev
;
2642 boolean_t slog_found
= B_FALSE
;
2644 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2646 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2647 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2649 if (tvd
->vdev_islog
) {
2650 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2651 metaslab_group_passivate(tvd
->vdev_mg
);
2652 slog_found
= B_TRUE
;
2656 return (slog_found
);
2660 * Activate any log vdevs (note, does not apply to embedded log metaslabs).
2663 spa_activate_log(spa_t
*spa
)
2665 vdev_t
*rvd
= spa
->spa_root_vdev
;
2667 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2669 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2670 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2672 if (tvd
->vdev_islog
) {
2673 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2674 metaslab_group_activate(tvd
->vdev_mg
);
2680 spa_reset_logs(spa_t
*spa
)
2684 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
2685 NULL
, DS_FIND_CHILDREN
);
2688 * We successfully offlined the log device, sync out the
2689 * current txg so that the "stubby" block can be removed
2692 txg_wait_synced(spa
->spa_dsl_pool
, 0);
2698 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
2700 for (int i
= 0; i
< sav
->sav_count
; i
++)
2701 spa_check_removed(sav
->sav_vdevs
[i
]);
2705 spa_claim_notify(zio_t
*zio
)
2707 spa_t
*spa
= zio
->io_spa
;
2712 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2713 if (spa
->spa_claim_max_txg
< BP_GET_LOGICAL_BIRTH(zio
->io_bp
))
2714 spa
->spa_claim_max_txg
= BP_GET_LOGICAL_BIRTH(zio
->io_bp
);
2715 mutex_exit(&spa
->spa_props_lock
);
2718 typedef struct spa_load_error
{
2719 boolean_t sle_verify_data
;
2720 uint64_t sle_meta_count
;
2721 uint64_t sle_data_count
;
2725 spa_load_verify_done(zio_t
*zio
)
2727 blkptr_t
*bp
= zio
->io_bp
;
2728 spa_load_error_t
*sle
= zio
->io_private
;
2729 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2730 int error
= zio
->io_error
;
2731 spa_t
*spa
= zio
->io_spa
;
2733 abd_free(zio
->io_abd
);
2735 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2736 type
!= DMU_OT_INTENT_LOG
)
2737 atomic_inc_64(&sle
->sle_meta_count
);
2739 atomic_inc_64(&sle
->sle_data_count
);
2742 mutex_enter(&spa
->spa_scrub_lock
);
2743 spa
->spa_load_verify_bytes
-= BP_GET_PSIZE(bp
);
2744 cv_broadcast(&spa
->spa_scrub_io_cv
);
2745 mutex_exit(&spa
->spa_scrub_lock
);
2749 * Maximum number of inflight bytes is the log2 fraction of the arc size.
2750 * By default, we set it to 1/16th of the arc.
2752 static uint_t spa_load_verify_shift
= 4;
2753 static int spa_load_verify_metadata
= B_TRUE
;
2754 static int spa_load_verify_data
= B_TRUE
;
2757 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2758 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2761 spa_load_error_t
*sle
= rio
->io_private
;
2763 (void) zilog
, (void) dnp
;
2766 * Note: normally this routine will not be called if
2767 * spa_load_verify_metadata is not set. However, it may be useful
2768 * to manually set the flag after the traversal has begun.
2770 if (!spa_load_verify_metadata
)
2774 * Sanity check the block pointer in order to detect obvious damage
2775 * before using the contents in subsequent checks or in zio_read().
2776 * When damaged consider it to be a metadata error since we cannot
2777 * trust the BP_GET_TYPE and BP_GET_LEVEL values.
2779 if (!zfs_blkptr_verify(spa
, bp
, BLK_CONFIG_NEEDED
, BLK_VERIFY_LOG
)) {
2780 atomic_inc_64(&sle
->sle_meta_count
);
2784 if (zb
->zb_level
== ZB_DNODE_LEVEL
|| BP_IS_HOLE(bp
) ||
2785 BP_IS_EMBEDDED(bp
) || BP_IS_REDACTED(bp
))
2788 if (!BP_IS_METADATA(bp
) &&
2789 (!spa_load_verify_data
|| !sle
->sle_verify_data
))
2792 uint64_t maxinflight_bytes
=
2793 arc_target_bytes() >> spa_load_verify_shift
;
2794 size_t size
= BP_GET_PSIZE(bp
);
2796 mutex_enter(&spa
->spa_scrub_lock
);
2797 while (spa
->spa_load_verify_bytes
>= maxinflight_bytes
)
2798 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2799 spa
->spa_load_verify_bytes
+= size
;
2800 mutex_exit(&spa
->spa_scrub_lock
);
2802 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2803 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2804 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2805 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2810 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2812 (void) dp
, (void) arg
;
2814 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2815 return (SET_ERROR(ENAMETOOLONG
));
2821 spa_load_verify(spa_t
*spa
)
2824 spa_load_error_t sle
= { 0 };
2825 zpool_load_policy_t policy
;
2826 boolean_t verify_ok
= B_FALSE
;
2829 zpool_get_load_policy(spa
->spa_config
, &policy
);
2831 if (policy
.zlp_rewind
& ZPOOL_NEVER_REWIND
||
2832 policy
.zlp_maxmeta
== UINT64_MAX
)
2835 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2836 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2837 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2839 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2844 * Verify data only if we are rewinding or error limit was set.
2845 * Otherwise nothing except dbgmsg care about it to waste time.
2847 sle
.sle_verify_data
= (policy
.zlp_rewind
& ZPOOL_REWIND_MASK
) ||
2848 (policy
.zlp_maxdata
< UINT64_MAX
);
2850 rio
= zio_root(spa
, NULL
, &sle
,
2851 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2853 if (spa_load_verify_metadata
) {
2854 if (spa
->spa_extreme_rewind
) {
2855 spa_load_note(spa
, "performing a complete scan of the "
2856 "pool since extreme rewind is on. This may take "
2857 "a very long time.\n (spa_load_verify_data=%u, "
2858 "spa_load_verify_metadata=%u)",
2859 spa_load_verify_data
, spa_load_verify_metadata
);
2862 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2863 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2864 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2867 (void) zio_wait(rio
);
2868 ASSERT0(spa
->spa_load_verify_bytes
);
2870 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2871 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2873 if (sle
.sle_meta_count
!= 0 || sle
.sle_data_count
!= 0) {
2874 spa_load_note(spa
, "spa_load_verify found %llu metadata errors "
2875 "and %llu data errors", (u_longlong_t
)sle
.sle_meta_count
,
2876 (u_longlong_t
)sle
.sle_data_count
);
2879 if (spa_load_verify_dryrun
||
2880 (!error
&& sle
.sle_meta_count
<= policy
.zlp_maxmeta
&&
2881 sle
.sle_data_count
<= policy
.zlp_maxdata
)) {
2885 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2886 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2888 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2889 fnvlist_add_uint64(spa
->spa_load_info
, ZPOOL_CONFIG_LOAD_TIME
,
2890 spa
->spa_load_txg_ts
);
2891 fnvlist_add_int64(spa
->spa_load_info
, ZPOOL_CONFIG_REWIND_TIME
,
2893 fnvlist_add_uint64(spa
->spa_load_info
,
2894 ZPOOL_CONFIG_LOAD_META_ERRORS
, sle
.sle_meta_count
);
2895 fnvlist_add_uint64(spa
->spa_load_info
,
2896 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
);
2898 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2901 if (spa_load_verify_dryrun
)
2905 if (error
!= ENXIO
&& error
!= EIO
)
2906 error
= SET_ERROR(EIO
);
2910 return (verify_ok
? 0 : EIO
);
2914 * Find a value in the pool props object.
2917 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2919 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2920 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2924 * Find a value in the pool directory object.
2927 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
, boolean_t log_enoent
)
2929 int error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2930 name
, sizeof (uint64_t), 1, val
);
2932 if (error
!= 0 && (error
!= ENOENT
|| log_enoent
)) {
2933 spa_load_failed(spa
, "couldn't get '%s' value in MOS directory "
2934 "[error=%d]", name
, error
);
2941 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2943 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2944 return (SET_ERROR(err
));
2948 spa_livelist_delete_check(spa_t
*spa
)
2950 return (spa
->spa_livelists_to_delete
!= 0);
2954 spa_livelist_delete_cb_check(void *arg
, zthr_t
*z
)
2958 return (spa_livelist_delete_check(spa
));
2962 delete_blkptr_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
2965 zio_free(spa
, tx
->tx_txg
, bp
);
2966 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
2967 -bp_get_dsize_sync(spa
, bp
),
2968 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
2973 dsl_get_next_livelist_obj(objset_t
*os
, uint64_t zap_obj
, uint64_t *llp
)
2977 zap_attribute_t
*za
= zap_attribute_alloc();
2978 zap_cursor_init(&zc
, os
, zap_obj
);
2979 err
= zap_cursor_retrieve(&zc
, za
);
2980 zap_cursor_fini(&zc
);
2982 *llp
= za
->za_first_integer
;
2983 zap_attribute_free(za
);
2988 * Components of livelist deletion that must be performed in syncing
2989 * context: freeing block pointers and updating the pool-wide data
2990 * structures to indicate how much work is left to do
2992 typedef struct sublist_delete_arg
{
2997 } sublist_delete_arg_t
;
3000 sublist_delete_sync(void *arg
, dmu_tx_t
*tx
)
3002 sublist_delete_arg_t
*sda
= arg
;
3003 spa_t
*spa
= sda
->spa
;
3004 dsl_deadlist_t
*ll
= sda
->ll
;
3005 uint64_t key
= sda
->key
;
3006 bplist_t
*to_free
= sda
->to_free
;
3008 bplist_iterate(to_free
, delete_blkptr_cb
, spa
, tx
);
3009 dsl_deadlist_remove_entry(ll
, key
, tx
);
3012 typedef struct livelist_delete_arg
{
3016 } livelist_delete_arg_t
;
3019 livelist_delete_sync(void *arg
, dmu_tx_t
*tx
)
3021 livelist_delete_arg_t
*lda
= arg
;
3022 spa_t
*spa
= lda
->spa
;
3023 uint64_t ll_obj
= lda
->ll_obj
;
3024 uint64_t zap_obj
= lda
->zap_obj
;
3025 objset_t
*mos
= spa
->spa_meta_objset
;
3028 /* free the livelist and decrement the feature count */
3029 VERIFY0(zap_remove_int(mos
, zap_obj
, ll_obj
, tx
));
3030 dsl_deadlist_free(mos
, ll_obj
, tx
);
3031 spa_feature_decr(spa
, SPA_FEATURE_LIVELIST
, tx
);
3032 VERIFY0(zap_count(mos
, zap_obj
, &count
));
3034 /* no more livelists to delete */
3035 VERIFY0(zap_remove(mos
, DMU_POOL_DIRECTORY_OBJECT
,
3036 DMU_POOL_DELETED_CLONES
, tx
));
3037 VERIFY0(zap_destroy(mos
, zap_obj
, tx
));
3038 spa
->spa_livelists_to_delete
= 0;
3039 spa_notify_waiters(spa
);
3044 * Load in the value for the livelist to be removed and open it. Then,
3045 * load its first sublist and determine which block pointers should actually
3046 * be freed. Then, call a synctask which performs the actual frees and updates
3047 * the pool-wide livelist data.
3050 spa_livelist_delete_cb(void *arg
, zthr_t
*z
)
3053 uint64_t ll_obj
= 0, count
;
3054 objset_t
*mos
= spa
->spa_meta_objset
;
3055 uint64_t zap_obj
= spa
->spa_livelists_to_delete
;
3057 * Determine the next livelist to delete. This function should only
3058 * be called if there is at least one deleted clone.
3060 VERIFY0(dsl_get_next_livelist_obj(mos
, zap_obj
, &ll_obj
));
3061 VERIFY0(zap_count(mos
, ll_obj
, &count
));
3064 dsl_deadlist_entry_t
*dle
;
3066 ll
= kmem_zalloc(sizeof (dsl_deadlist_t
), KM_SLEEP
);
3067 dsl_deadlist_open(ll
, mos
, ll_obj
);
3068 dle
= dsl_deadlist_first(ll
);
3069 ASSERT3P(dle
, !=, NULL
);
3070 bplist_create(&to_free
);
3071 int err
= dsl_process_sub_livelist(&dle
->dle_bpobj
, &to_free
,
3074 sublist_delete_arg_t sync_arg
= {
3077 .key
= dle
->dle_mintxg
,
3080 zfs_dbgmsg("deleting sublist (id %llu) from"
3081 " livelist %llu, %lld remaining",
3082 (u_longlong_t
)dle
->dle_bpobj
.bpo_object
,
3083 (u_longlong_t
)ll_obj
, (longlong_t
)count
- 1);
3084 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
3085 sublist_delete_sync
, &sync_arg
, 0,
3086 ZFS_SPACE_CHECK_DESTROY
));
3088 VERIFY3U(err
, ==, EINTR
);
3090 bplist_clear(&to_free
);
3091 bplist_destroy(&to_free
);
3092 dsl_deadlist_close(ll
);
3093 kmem_free(ll
, sizeof (dsl_deadlist_t
));
3095 livelist_delete_arg_t sync_arg
= {
3100 zfs_dbgmsg("deletion of livelist %llu completed",
3101 (u_longlong_t
)ll_obj
);
3102 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
, livelist_delete_sync
,
3103 &sync_arg
, 0, ZFS_SPACE_CHECK_DESTROY
));
3108 spa_start_livelist_destroy_thread(spa_t
*spa
)
3110 ASSERT3P(spa
->spa_livelist_delete_zthr
, ==, NULL
);
3111 spa
->spa_livelist_delete_zthr
=
3112 zthr_create("z_livelist_destroy",
3113 spa_livelist_delete_cb_check
, spa_livelist_delete_cb
, spa
,
3117 typedef struct livelist_new_arg
{
3120 } livelist_new_arg_t
;
3123 livelist_track_new_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
3127 livelist_new_arg_t
*lna
= arg
;
3129 bplist_append(lna
->frees
, bp
);
3131 bplist_append(lna
->allocs
, bp
);
3132 zfs_livelist_condense_new_alloc
++;
3137 typedef struct livelist_condense_arg
{
3140 uint64_t first_size
;
3142 } livelist_condense_arg_t
;
3145 spa_livelist_condense_sync(void *arg
, dmu_tx_t
*tx
)
3147 livelist_condense_arg_t
*lca
= arg
;
3148 spa_t
*spa
= lca
->spa
;
3150 dsl_dataset_t
*ds
= spa
->spa_to_condense
.ds
;
3152 /* Have we been cancelled? */
3153 if (spa
->spa_to_condense
.cancelled
) {
3154 zfs_livelist_condense_sync_cancel
++;
3158 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
3159 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
3160 dsl_deadlist_t
*ll
= &ds
->ds_dir
->dd_livelist
;
3163 * It's possible that the livelist was changed while the zthr was
3164 * running. Therefore, we need to check for new blkptrs in the two
3165 * entries being condensed and continue to track them in the livelist.
3166 * Because of the way we handle remapped blkptrs (see dbuf_remap_impl),
3167 * it's possible that the newly added blkptrs are FREEs or ALLOCs so
3168 * we need to sort them into two different bplists.
3170 uint64_t first_obj
= first
->dle_bpobj
.bpo_object
;
3171 uint64_t next_obj
= next
->dle_bpobj
.bpo_object
;
3172 uint64_t cur_first_size
= first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
3173 uint64_t cur_next_size
= next
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
3175 bplist_create(&new_frees
);
3176 livelist_new_arg_t new_bps
= {
3177 .allocs
= &lca
->to_keep
,
3178 .frees
= &new_frees
,
3181 if (cur_first_size
> lca
->first_size
) {
3182 VERIFY0(livelist_bpobj_iterate_from_nofree(&first
->dle_bpobj
,
3183 livelist_track_new_cb
, &new_bps
, lca
->first_size
));
3185 if (cur_next_size
> lca
->next_size
) {
3186 VERIFY0(livelist_bpobj_iterate_from_nofree(&next
->dle_bpobj
,
3187 livelist_track_new_cb
, &new_bps
, lca
->next_size
));
3190 dsl_deadlist_clear_entry(first
, ll
, tx
);
3191 ASSERT(bpobj_is_empty(&first
->dle_bpobj
));
3192 dsl_deadlist_remove_entry(ll
, next
->dle_mintxg
, tx
);
3194 bplist_iterate(&lca
->to_keep
, dsl_deadlist_insert_alloc_cb
, ll
, tx
);
3195 bplist_iterate(&new_frees
, dsl_deadlist_insert_free_cb
, ll
, tx
);
3196 bplist_destroy(&new_frees
);
3198 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
3199 dsl_dataset_name(ds
, dsname
);
3200 zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu "
3201 "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu "
3202 "(%llu blkptrs)", (u_longlong_t
)tx
->tx_txg
, dsname
,
3203 (u_longlong_t
)ds
->ds_object
, (u_longlong_t
)first_obj
,
3204 (u_longlong_t
)cur_first_size
, (u_longlong_t
)next_obj
,
3205 (u_longlong_t
)cur_next_size
,
3206 (u_longlong_t
)first
->dle_bpobj
.bpo_object
,
3207 (u_longlong_t
)first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
);
3209 dmu_buf_rele(ds
->ds_dbuf
, spa
);
3210 spa
->spa_to_condense
.ds
= NULL
;
3211 bplist_clear(&lca
->to_keep
);
3212 bplist_destroy(&lca
->to_keep
);
3213 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
3214 spa
->spa_to_condense
.syncing
= B_FALSE
;
3218 spa_livelist_condense_cb(void *arg
, zthr_t
*t
)
3220 while (zfs_livelist_condense_zthr_pause
&&
3221 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
3225 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
3226 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
3227 uint64_t first_size
, next_size
;
3229 livelist_condense_arg_t
*lca
=
3230 kmem_alloc(sizeof (livelist_condense_arg_t
), KM_SLEEP
);
3231 bplist_create(&lca
->to_keep
);
3234 * Process the livelists (matching FREEs and ALLOCs) in open context
3235 * so we have minimal work in syncing context to condense.
3237 * We save bpobj sizes (first_size and next_size) to use later in
3238 * syncing context to determine if entries were added to these sublists
3239 * while in open context. This is possible because the clone is still
3240 * active and open for normal writes and we want to make sure the new,
3241 * unprocessed blockpointers are inserted into the livelist normally.
3243 * Note that dsl_process_sub_livelist() both stores the size number of
3244 * blockpointers and iterates over them while the bpobj's lock held, so
3245 * the sizes returned to us are consistent which what was actually
3248 int err
= dsl_process_sub_livelist(&first
->dle_bpobj
, &lca
->to_keep
, t
,
3251 err
= dsl_process_sub_livelist(&next
->dle_bpobj
, &lca
->to_keep
,
3255 while (zfs_livelist_condense_sync_pause
&&
3256 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
3259 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
3260 dmu_tx_mark_netfree(tx
);
3261 dmu_tx_hold_space(tx
, 1);
3262 err
= dmu_tx_assign(tx
, TXG_NOWAIT
| TXG_NOTHROTTLE
);
3265 * Prevent the condense zthr restarting before
3266 * the synctask completes.
3268 spa
->spa_to_condense
.syncing
= B_TRUE
;
3270 lca
->first_size
= first_size
;
3271 lca
->next_size
= next_size
;
3272 dsl_sync_task_nowait(spa_get_dsl(spa
),
3273 spa_livelist_condense_sync
, lca
, tx
);
3279 * Condensing can not continue: either it was externally stopped or
3280 * we were unable to assign to a tx because the pool has run out of
3281 * space. In the second case, we'll just end up trying to condense
3282 * again in a later txg.
3285 bplist_clear(&lca
->to_keep
);
3286 bplist_destroy(&lca
->to_keep
);
3287 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
3288 dmu_buf_rele(spa
->spa_to_condense
.ds
->ds_dbuf
, spa
);
3289 spa
->spa_to_condense
.ds
= NULL
;
3291 zfs_livelist_condense_zthr_cancel
++;
3295 * Check that there is something to condense but that a condense is not
3296 * already in progress and that condensing has not been cancelled.
3299 spa_livelist_condense_cb_check(void *arg
, zthr_t
*z
)
3303 if ((spa
->spa_to_condense
.ds
!= NULL
) &&
3304 (spa
->spa_to_condense
.syncing
== B_FALSE
) &&
3305 (spa
->spa_to_condense
.cancelled
== B_FALSE
)) {
3312 spa_start_livelist_condensing_thread(spa_t
*spa
)
3314 spa
->spa_to_condense
.ds
= NULL
;
3315 spa
->spa_to_condense
.first
= NULL
;
3316 spa
->spa_to_condense
.next
= NULL
;
3317 spa
->spa_to_condense
.syncing
= B_FALSE
;
3318 spa
->spa_to_condense
.cancelled
= B_FALSE
;
3320 ASSERT3P(spa
->spa_livelist_condense_zthr
, ==, NULL
);
3321 spa
->spa_livelist_condense_zthr
=
3322 zthr_create("z_livelist_condense",
3323 spa_livelist_condense_cb_check
,
3324 spa_livelist_condense_cb
, spa
, minclsyspri
);
3328 spa_spawn_aux_threads(spa_t
*spa
)
3330 ASSERT(spa_writeable(spa
));
3332 spa_start_raidz_expansion_thread(spa
);
3333 spa_start_indirect_condensing_thread(spa
);
3334 spa_start_livelist_destroy_thread(spa
);
3335 spa_start_livelist_condensing_thread(spa
);
3337 ASSERT3P(spa
->spa_checkpoint_discard_zthr
, ==, NULL
);
3338 spa
->spa_checkpoint_discard_zthr
=
3339 zthr_create("z_checkpoint_discard",
3340 spa_checkpoint_discard_thread_check
,
3341 spa_checkpoint_discard_thread
, spa
, minclsyspri
);
3345 * Fix up config after a partly-completed split. This is done with the
3346 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
3347 * pool have that entry in their config, but only the splitting one contains
3348 * a list of all the guids of the vdevs that are being split off.
3350 * This function determines what to do with that list: either rejoin
3351 * all the disks to the pool, or complete the splitting process. To attempt
3352 * the rejoin, each disk that is offlined is marked online again, and
3353 * we do a reopen() call. If the vdev label for every disk that was
3354 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
3355 * then we call vdev_split() on each disk, and complete the split.
3357 * Otherwise we leave the config alone, with all the vdevs in place in
3358 * the original pool.
3361 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
3368 boolean_t attempt_reopen
;
3370 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
3373 /* check that the config is complete */
3374 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
3375 &glist
, &gcount
) != 0)
3378 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
3380 /* attempt to online all the vdevs & validate */
3381 attempt_reopen
= B_TRUE
;
3382 for (i
= 0; i
< gcount
; i
++) {
3383 if (glist
[i
] == 0) /* vdev is hole */
3386 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
3387 if (vd
[i
] == NULL
) {
3389 * Don't bother attempting to reopen the disks;
3390 * just do the split.
3392 attempt_reopen
= B_FALSE
;
3394 /* attempt to re-online it */
3395 vd
[i
]->vdev_offline
= B_FALSE
;
3399 if (attempt_reopen
) {
3400 vdev_reopen(spa
->spa_root_vdev
);
3402 /* check each device to see what state it's in */
3403 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
3404 if (vd
[i
] != NULL
&&
3405 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
3412 * If every disk has been moved to the new pool, or if we never
3413 * even attempted to look at them, then we split them off for
3416 if (!attempt_reopen
|| gcount
== extracted
) {
3417 for (i
= 0; i
< gcount
; i
++)
3420 vdev_reopen(spa
->spa_root_vdev
);
3423 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
3427 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
)
3429 const char *ereport
= FM_EREPORT_ZFS_POOL
;
3432 spa
->spa_load_state
= state
;
3433 (void) spa_import_progress_set_state(spa_guid(spa
),
3434 spa_load_state(spa
));
3435 spa_import_progress_set_notes(spa
, "spa_load()");
3437 gethrestime(&spa
->spa_loaded_ts
);
3438 error
= spa_load_impl(spa
, type
, &ereport
);
3441 * Don't count references from objsets that are already closed
3442 * and are making their way through the eviction process.
3444 spa_evicting_os_wait(spa
);
3445 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
3447 if (error
!= EEXIST
) {
3448 spa
->spa_loaded_ts
.tv_sec
= 0;
3449 spa
->spa_loaded_ts
.tv_nsec
= 0;
3451 if (error
!= EBADF
) {
3452 (void) zfs_ereport_post(ereport
, spa
,
3453 NULL
, NULL
, NULL
, 0);
3456 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
3459 (void) spa_import_progress_set_state(spa_guid(spa
),
3460 spa_load_state(spa
));
3467 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
3468 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
3469 * spa's per-vdev ZAP list.
3472 vdev_count_verify_zaps(vdev_t
*vd
)
3474 spa_t
*spa
= vd
->vdev_spa
;
3477 if (spa_feature_is_active(vd
->vdev_spa
, SPA_FEATURE_AVZ_V2
) &&
3478 vd
->vdev_root_zap
!= 0) {
3480 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3481 spa
->spa_all_vdev_zaps
, vd
->vdev_root_zap
));
3483 if (vd
->vdev_top_zap
!= 0) {
3485 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3486 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
3488 if (vd
->vdev_leaf_zap
!= 0) {
3490 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3491 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
3494 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
3495 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
3501 #define vdev_count_verify_zaps(vd) ((void) sizeof (vd), 0)
3505 * Determine whether the activity check is required.
3508 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
3512 uint64_t hostid
= 0;
3513 uint64_t tryconfig_txg
= 0;
3514 uint64_t tryconfig_timestamp
= 0;
3515 uint16_t tryconfig_mmp_seq
= 0;
3518 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3519 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3520 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
3522 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
3523 &tryconfig_timestamp
);
3524 (void) nvlist_lookup_uint16(nvinfo
, ZPOOL_CONFIG_MMP_SEQ
,
3525 &tryconfig_mmp_seq
);
3528 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
3531 * Disable the MMP activity check - This is used by zdb which
3532 * is intended to be used on potentially active pools.
3534 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
3538 * Skip the activity check when the MMP feature is disabled.
3540 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
3544 * If the tryconfig_ values are nonzero, they are the results of an
3545 * earlier tryimport. If they all match the uberblock we just found,
3546 * then the pool has not changed and we return false so we do not test
3549 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
3550 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
&&
3551 tryconfig_mmp_seq
&& tryconfig_mmp_seq
==
3552 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0))
3556 * Allow the activity check to be skipped when importing the pool
3557 * on the same host which last imported it. Since the hostid from
3558 * configuration may be stale use the one read from the label.
3560 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
3561 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
3563 if (hostid
== spa_get_hostid(spa
))
3567 * Skip the activity test when the pool was cleanly exported.
3569 if (state
!= POOL_STATE_ACTIVE
)
3576 * Nanoseconds the activity check must watch for changes on-disk.
3579 spa_activity_check_duration(spa_t
*spa
, uberblock_t
*ub
)
3581 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
3582 uint64_t multihost_interval
= MSEC2NSEC(
3583 MMP_INTERVAL_OK(zfs_multihost_interval
));
3584 uint64_t import_delay
= MAX(NANOSEC
, import_intervals
*
3585 multihost_interval
);
3588 * Local tunables determine a minimum duration except for the case
3589 * where we know when the remote host will suspend the pool if MMP
3590 * writes do not land.
3592 * See Big Theory comment at the top of mmp.c for the reasoning behind
3593 * these cases and times.
3596 ASSERT(MMP_IMPORT_SAFETY_FACTOR
>= 100);
3598 if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3599 MMP_FAIL_INT(ub
) > 0) {
3601 /* MMP on remote host will suspend pool after failed writes */
3602 import_delay
= MMP_FAIL_INT(ub
) * MSEC2NSEC(MMP_INTERVAL(ub
)) *
3603 MMP_IMPORT_SAFETY_FACTOR
/ 100;
3605 zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp "
3606 "mmp_fails=%llu ub_mmp mmp_interval=%llu "
3607 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3608 (u_longlong_t
)MMP_FAIL_INT(ub
),
3609 (u_longlong_t
)MMP_INTERVAL(ub
),
3610 (u_longlong_t
)import_intervals
);
3612 } else if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3613 MMP_FAIL_INT(ub
) == 0) {
3615 /* MMP on remote host will never suspend pool */
3616 import_delay
= MAX(import_delay
, (MSEC2NSEC(MMP_INTERVAL(ub
)) +
3617 ub
->ub_mmp_delay
) * import_intervals
);
3619 zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp "
3620 "mmp_interval=%llu ub_mmp_delay=%llu "
3621 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3622 (u_longlong_t
)MMP_INTERVAL(ub
),
3623 (u_longlong_t
)ub
->ub_mmp_delay
,
3624 (u_longlong_t
)import_intervals
);
3626 } else if (MMP_VALID(ub
)) {
3628 * zfs-0.7 compatibility case
3631 import_delay
= MAX(import_delay
, (multihost_interval
+
3632 ub
->ub_mmp_delay
) * import_intervals
);
3634 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu "
3635 "import_intervals=%llu leaves=%u",
3636 (u_longlong_t
)import_delay
,
3637 (u_longlong_t
)ub
->ub_mmp_delay
,
3638 (u_longlong_t
)import_intervals
,
3639 vdev_count_leaves(spa
));
3641 /* Using local tunings is the only reasonable option */
3642 zfs_dbgmsg("pool last imported on non-MMP aware "
3643 "host using import_delay=%llu multihost_interval=%llu "
3644 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3645 (u_longlong_t
)multihost_interval
,
3646 (u_longlong_t
)import_intervals
);
3649 return (import_delay
);
3653 * Remote host activity check.
3656 * 0 - no activity detected
3657 * EREMOTEIO - remote activity detected
3658 * EINTR - user canceled the operation
3661 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
,
3662 boolean_t importing
)
3664 uint64_t txg
= ub
->ub_txg
;
3665 uint64_t timestamp
= ub
->ub_timestamp
;
3666 uint64_t mmp_config
= ub
->ub_mmp_config
;
3667 uint16_t mmp_seq
= MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0;
3668 uint64_t import_delay
;
3669 hrtime_t import_expire
, now
;
3670 nvlist_t
*mmp_label
= NULL
;
3671 vdev_t
*rvd
= spa
->spa_root_vdev
;
3676 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
3677 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3681 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
3682 * during the earlier tryimport. If the txg recorded there is 0 then
3683 * the pool is known to be active on another host.
3685 * Otherwise, the pool might be in use on another host. Check for
3686 * changes in the uberblocks on disk if necessary.
3688 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3689 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
3690 ZPOOL_CONFIG_LOAD_INFO
);
3692 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
3693 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
3694 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3695 error
= SET_ERROR(EREMOTEIO
);
3700 import_delay
= spa_activity_check_duration(spa
, ub
);
3702 /* Add a small random factor in case of simultaneous imports (0-25%) */
3703 import_delay
+= import_delay
* random_in_range(250) / 1000;
3705 import_expire
= gethrtime() + import_delay
;
3708 spa_import_progress_set_notes(spa
, "Checking MMP activity, "
3709 "waiting %llu ms", (u_longlong_t
)NSEC2MSEC(import_delay
));
3713 while ((now
= gethrtime()) < import_expire
) {
3714 if (importing
&& iterations
++ % 30 == 0) {
3715 spa_import_progress_set_notes(spa
, "Checking MMP "
3716 "activity, %llu ms remaining",
3717 (u_longlong_t
)NSEC2MSEC(import_expire
- now
));
3721 (void) spa_import_progress_set_mmp_check(spa_guid(spa
),
3722 NSEC2SEC(import_expire
- gethrtime()));
3725 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3727 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
||
3728 mmp_seq
!= (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0)) {
3729 zfs_dbgmsg("multihost activity detected "
3730 "txg %llu ub_txg %llu "
3731 "timestamp %llu ub_timestamp %llu "
3732 "mmp_config %#llx ub_mmp_config %#llx",
3733 (u_longlong_t
)txg
, (u_longlong_t
)ub
->ub_txg
,
3734 (u_longlong_t
)timestamp
,
3735 (u_longlong_t
)ub
->ub_timestamp
,
3736 (u_longlong_t
)mmp_config
,
3737 (u_longlong_t
)ub
->ub_mmp_config
);
3739 error
= SET_ERROR(EREMOTEIO
);
3744 nvlist_free(mmp_label
);
3748 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
3750 error
= SET_ERROR(EINTR
);
3758 mutex_destroy(&mtx
);
3762 * If the pool is determined to be active store the status in the
3763 * spa->spa_load_info nvlist. If the remote hostname or hostid are
3764 * available from configuration read from disk store them as well.
3765 * This allows 'zpool import' to generate a more useful message.
3767 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
3768 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
3769 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
3771 if (error
== EREMOTEIO
) {
3772 const char *hostname
= "<unknown>";
3773 uint64_t hostid
= 0;
3776 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
3777 hostname
= fnvlist_lookup_string(mmp_label
,
3778 ZPOOL_CONFIG_HOSTNAME
);
3779 fnvlist_add_string(spa
->spa_load_info
,
3780 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
3783 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
3784 hostid
= fnvlist_lookup_uint64(mmp_label
,
3785 ZPOOL_CONFIG_HOSTID
);
3786 fnvlist_add_uint64(spa
->spa_load_info
,
3787 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
3791 fnvlist_add_uint64(spa
->spa_load_info
,
3792 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
3793 fnvlist_add_uint64(spa
->spa_load_info
,
3794 ZPOOL_CONFIG_MMP_TXG
, 0);
3796 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
3800 nvlist_free(mmp_label
);
3806 * Called from zfs_ioc_clear for a pool that was suspended
3807 * after failing mmp write checks.
3810 spa_mmp_remote_host_activity(spa_t
*spa
)
3812 ASSERT(spa_multihost(spa
) && spa_suspended(spa
));
3814 nvlist_t
*best_label
;
3815 uberblock_t best_ub
;
3818 * Locate the best uberblock on disk
3820 vdev_uberblock_load(spa
->spa_root_vdev
, &best_ub
, &best_label
);
3823 * confirm that the best hostid matches our hostid
3825 if (nvlist_exists(best_label
, ZPOOL_CONFIG_HOSTID
) &&
3826 spa_get_hostid(spa
) !=
3827 fnvlist_lookup_uint64(best_label
, ZPOOL_CONFIG_HOSTID
)) {
3828 nvlist_free(best_label
);
3831 nvlist_free(best_label
);
3836 if (!MMP_VALID(&best_ub
) ||
3837 !MMP_FAIL_INT_VALID(&best_ub
) ||
3838 MMP_FAIL_INT(&best_ub
) == 0) {
3842 if (best_ub
.ub_txg
!= spa
->spa_uberblock
.ub_txg
||
3843 best_ub
.ub_timestamp
!= spa
->spa_uberblock
.ub_timestamp
) {
3844 zfs_dbgmsg("txg mismatch detected during pool clear "
3845 "txg %llu ub_txg %llu timestamp %llu ub_timestamp %llu",
3846 (u_longlong_t
)spa
->spa_uberblock
.ub_txg
,
3847 (u_longlong_t
)best_ub
.ub_txg
,
3848 (u_longlong_t
)spa
->spa_uberblock
.ub_timestamp
,
3849 (u_longlong_t
)best_ub
.ub_timestamp
);
3854 * Perform an activity check looking for any remote writer
3856 return (spa_activity_check(spa
, &spa
->spa_uberblock
, spa
->spa_config
,
3861 spa_verify_host(spa_t
*spa
, nvlist_t
*mos_config
)
3864 const char *hostname
;
3865 uint64_t myhostid
= 0;
3867 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
3868 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
3869 hostname
= fnvlist_lookup_string(mos_config
,
3870 ZPOOL_CONFIG_HOSTNAME
);
3872 myhostid
= zone_get_hostid(NULL
);
3874 if (hostid
!= 0 && myhostid
!= 0 && hostid
!= myhostid
) {
3875 cmn_err(CE_WARN
, "pool '%s' could not be "
3876 "loaded as it was last accessed by "
3877 "another system (host: %s hostid: 0x%llx). "
3878 "See: https://openzfs.github.io/openzfs-docs/msg/"
3880 spa_name(spa
), hostname
, (u_longlong_t
)hostid
);
3881 spa_load_failed(spa
, "hostid verification failed: pool "
3882 "last accessed by host: %s (hostid: 0x%llx)",
3883 hostname
, (u_longlong_t
)hostid
);
3884 return (SET_ERROR(EBADF
));
3892 spa_ld_parse_config(spa_t
*spa
, spa_import_type_t type
)
3895 nvlist_t
*nvtree
, *nvl
, *config
= spa
->spa_config
;
3899 const char *comment
;
3900 const char *compatibility
;
3903 * Versioning wasn't explicitly added to the label until later, so if
3904 * it's not present treat it as the initial version.
3906 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
3907 &spa
->spa_ubsync
.ub_version
) != 0)
3908 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
3910 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
)) {
3911 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3912 ZPOOL_CONFIG_POOL_GUID
);
3913 return (SET_ERROR(EINVAL
));
3917 * If we are doing an import, ensure that the pool is not already
3918 * imported by checking if its pool guid already exists in the
3921 * The only case that we allow an already imported pool to be
3922 * imported again, is when the pool is checkpointed and we want to
3923 * look at its checkpointed state from userland tools like zdb.
3926 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3927 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3928 spa_guid_exists(pool_guid
, 0)) {
3930 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3931 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3932 spa_guid_exists(pool_guid
, 0) &&
3933 !spa_importing_readonly_checkpoint(spa
)) {
3935 spa_load_failed(spa
, "a pool with guid %llu is already open",
3936 (u_longlong_t
)pool_guid
);
3937 return (SET_ERROR(EEXIST
));
3940 spa
->spa_config_guid
= pool_guid
;
3942 nvlist_free(spa
->spa_load_info
);
3943 spa
->spa_load_info
= fnvlist_alloc();
3945 ASSERT(spa
->spa_comment
== NULL
);
3946 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
3947 spa
->spa_comment
= spa_strdup(comment
);
3949 ASSERT(spa
->spa_compatibility
== NULL
);
3950 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMPATIBILITY
,
3951 &compatibility
) == 0)
3952 spa
->spa_compatibility
= spa_strdup(compatibility
);
3954 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
3955 &spa
->spa_config_txg
);
3957 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) == 0)
3958 spa
->spa_config_splitting
= fnvlist_dup(nvl
);
3960 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvtree
)) {
3961 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3962 ZPOOL_CONFIG_VDEV_TREE
);
3963 return (SET_ERROR(EINVAL
));
3967 * Create "The Godfather" zio to hold all async IOs
3969 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3971 for (int i
= 0; i
< max_ncpus
; i
++) {
3972 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3973 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3974 ZIO_FLAG_GODFATHER
);
3978 * Parse the configuration into a vdev tree. We explicitly set the
3979 * value that will be returned by spa_version() since parsing the
3980 * configuration requires knowing the version number.
3982 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3983 parse
= (type
== SPA_IMPORT_EXISTING
?
3984 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
3985 error
= spa_config_parse(spa
, &rvd
, nvtree
, NULL
, 0, parse
);
3986 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3989 spa_load_failed(spa
, "unable to parse config [error=%d]",
3994 ASSERT(spa
->spa_root_vdev
== rvd
);
3995 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
3996 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
3998 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3999 ASSERT(spa_guid(spa
) == pool_guid
);
4006 * Recursively open all vdevs in the vdev tree. This function is called twice:
4007 * first with the untrusted config, then with the trusted config.
4010 spa_ld_open_vdevs(spa_t
*spa
)
4015 * spa_missing_tvds_allowed defines how many top-level vdevs can be
4016 * missing/unopenable for the root vdev to be still considered openable.
4018 if (spa
->spa_trust_config
) {
4019 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds
;
4020 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_CACHEFILE
) {
4021 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_cachefile
;
4022 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_SCAN
) {
4023 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_scan
;
4025 spa
->spa_missing_tvds_allowed
= 0;
4028 spa
->spa_missing_tvds_allowed
=
4029 MAX(zfs_max_missing_tvds
, spa
->spa_missing_tvds_allowed
);
4031 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4032 error
= vdev_open(spa
->spa_root_vdev
);
4033 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4035 if (spa
->spa_missing_tvds
!= 0) {
4036 spa_load_note(spa
, "vdev tree has %lld missing top-level "
4037 "vdevs.", (u_longlong_t
)spa
->spa_missing_tvds
);
4038 if (spa
->spa_trust_config
&& (spa
->spa_mode
& SPA_MODE_WRITE
)) {
4040 * Although theoretically we could allow users to open
4041 * incomplete pools in RW mode, we'd need to add a lot
4042 * of extra logic (e.g. adjust pool space to account
4043 * for missing vdevs).
4044 * This limitation also prevents users from accidentally
4045 * opening the pool in RW mode during data recovery and
4046 * damaging it further.
4048 spa_load_note(spa
, "pools with missing top-level "
4049 "vdevs can only be opened in read-only mode.");
4050 error
= SET_ERROR(ENXIO
);
4052 spa_load_note(spa
, "current settings allow for maximum "
4053 "%lld missing top-level vdevs at this stage.",
4054 (u_longlong_t
)spa
->spa_missing_tvds_allowed
);
4058 spa_load_failed(spa
, "unable to open vdev tree [error=%d]",
4061 if (spa
->spa_missing_tvds
!= 0 || error
!= 0)
4062 vdev_dbgmsg_print_tree(spa
->spa_root_vdev
, 2);
4068 * We need to validate the vdev labels against the configuration that
4069 * we have in hand. This function is called twice: first with an untrusted
4070 * config, then with a trusted config. The validation is more strict when the
4071 * config is trusted.
4074 spa_ld_validate_vdevs(spa_t
*spa
)
4077 vdev_t
*rvd
= spa
->spa_root_vdev
;
4079 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4080 error
= vdev_validate(rvd
);
4081 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4084 spa_load_failed(spa
, "vdev_validate failed [error=%d]", error
);
4088 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
) {
4089 spa_load_failed(spa
, "cannot open vdev tree after invalidating "
4091 vdev_dbgmsg_print_tree(rvd
, 2);
4092 return (SET_ERROR(ENXIO
));
4099 spa_ld_select_uberblock_done(spa_t
*spa
, uberblock_t
*ub
)
4101 spa
->spa_state
= POOL_STATE_ACTIVE
;
4102 spa
->spa_ubsync
= spa
->spa_uberblock
;
4103 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
4104 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
4105 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
4106 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
4107 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
4108 spa
->spa_prev_software_version
= ub
->ub_software_version
;
4112 spa_ld_select_uberblock(spa_t
*spa
, spa_import_type_t type
)
4114 vdev_t
*rvd
= spa
->spa_root_vdev
;
4116 uberblock_t
*ub
= &spa
->spa_uberblock
;
4117 boolean_t activity_check
= B_FALSE
;
4120 * If we are opening the checkpointed state of the pool by
4121 * rewinding to it, at this point we will have written the
4122 * checkpointed uberblock to the vdev labels, so searching
4123 * the labels will find the right uberblock. However, if
4124 * we are opening the checkpointed state read-only, we have
4125 * not modified the labels. Therefore, we must ignore the
4126 * labels and continue using the spa_uberblock that was set
4127 * by spa_ld_checkpoint_rewind.
4129 * Note that it would be fine to ignore the labels when
4130 * rewinding (opening writeable) as well. However, if we
4131 * crash just after writing the labels, we will end up
4132 * searching the labels. Doing so in the common case means
4133 * that this code path gets exercised normally, rather than
4134 * just in the edge case.
4136 if (ub
->ub_checkpoint_txg
!= 0 &&
4137 spa_importing_readonly_checkpoint(spa
)) {
4138 spa_ld_select_uberblock_done(spa
, ub
);
4143 * Find the best uberblock.
4145 vdev_uberblock_load(rvd
, ub
, &label
);
4148 * If we weren't able to find a single valid uberblock, return failure.
4150 if (ub
->ub_txg
== 0) {
4152 spa_load_failed(spa
, "no valid uberblock found");
4153 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
4156 if (spa
->spa_load_max_txg
!= UINT64_MAX
) {
4157 (void) spa_import_progress_set_max_txg(spa_guid(spa
),
4158 (u_longlong_t
)spa
->spa_load_max_txg
);
4160 spa_load_note(spa
, "using uberblock with txg=%llu",
4161 (u_longlong_t
)ub
->ub_txg
);
4162 if (ub
->ub_raidz_reflow_info
!= 0) {
4163 spa_load_note(spa
, "uberblock raidz_reflow_info: "
4164 "state=%u offset=%llu",
4165 (int)RRSS_GET_STATE(ub
),
4166 (u_longlong_t
)RRSS_GET_OFFSET(ub
));
4171 * For pools which have the multihost property on determine if the
4172 * pool is truly inactive and can be safely imported. Prevent
4173 * hosts which don't have a hostid set from importing the pool.
4175 activity_check
= spa_activity_check_required(spa
, ub
, label
,
4177 if (activity_check
) {
4178 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
4179 spa_get_hostid(spa
) == 0) {
4181 fnvlist_add_uint64(spa
->spa_load_info
,
4182 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
4183 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
4187 spa_activity_check(spa
, ub
, spa
->spa_config
, B_TRUE
);
4193 fnvlist_add_uint64(spa
->spa_load_info
,
4194 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
4195 fnvlist_add_uint64(spa
->spa_load_info
,
4196 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
4197 fnvlist_add_uint16(spa
->spa_load_info
,
4198 ZPOOL_CONFIG_MMP_SEQ
,
4199 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0));
4203 * If the pool has an unsupported version we can't open it.
4205 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
4207 spa_load_failed(spa
, "version %llu is not supported",
4208 (u_longlong_t
)ub
->ub_version
);
4209 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
4212 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
4216 * If we weren't able to find what's necessary for reading the
4217 * MOS in the label, return failure.
4219 if (label
== NULL
) {
4220 spa_load_failed(spa
, "label config unavailable");
4221 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
4225 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
4228 spa_load_failed(spa
, "invalid label: '%s' missing",
4229 ZPOOL_CONFIG_FEATURES_FOR_READ
);
4230 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
4235 * Update our in-core representation with the definitive values
4238 nvlist_free(spa
->spa_label_features
);
4239 spa
->spa_label_features
= fnvlist_dup(features
);
4245 * Look through entries in the label nvlist's features_for_read. If
4246 * there is a feature listed there which we don't understand then we
4247 * cannot open a pool.
4249 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
4250 nvlist_t
*unsup_feat
;
4252 unsup_feat
= fnvlist_alloc();
4254 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
4256 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
4257 if (!zfeature_is_supported(nvpair_name(nvp
))) {
4258 fnvlist_add_string(unsup_feat
,
4259 nvpair_name(nvp
), "");
4263 if (!nvlist_empty(unsup_feat
)) {
4264 fnvlist_add_nvlist(spa
->spa_load_info
,
4265 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
4266 nvlist_free(unsup_feat
);
4267 spa_load_failed(spa
, "some features are unsupported");
4268 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
4272 nvlist_free(unsup_feat
);
4275 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
4276 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4277 spa_try_repair(spa
, spa
->spa_config
);
4278 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4279 nvlist_free(spa
->spa_config_splitting
);
4280 spa
->spa_config_splitting
= NULL
;
4284 * Initialize internal SPA structures.
4286 spa_ld_select_uberblock_done(spa
, ub
);
4292 spa_ld_open_rootbp(spa_t
*spa
)
4295 vdev_t
*rvd
= spa
->spa_root_vdev
;
4297 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
4299 spa_load_failed(spa
, "unable to open rootbp in dsl_pool_init "
4300 "[error=%d]", error
);
4301 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4303 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
4309 spa_ld_trusted_config(spa_t
*spa
, spa_import_type_t type
,
4310 boolean_t reloading
)
4312 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
4313 nvlist_t
*nv
, *mos_config
, *policy
;
4314 int error
= 0, copy_error
;
4315 uint64_t healthy_tvds
, healthy_tvds_mos
;
4316 uint64_t mos_config_txg
;
4318 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
, B_TRUE
)
4320 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4323 * If we're assembling a pool from a split, the config provided is
4324 * already trusted so there is nothing to do.
4326 if (type
== SPA_IMPORT_ASSEMBLE
)
4329 healthy_tvds
= spa_healthy_core_tvds(spa
);
4331 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
)
4333 spa_load_failed(spa
, "unable to retrieve MOS config");
4334 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4338 * If we are doing an open, pool owner wasn't verified yet, thus do
4339 * the verification here.
4341 if (spa
->spa_load_state
== SPA_LOAD_OPEN
) {
4342 error
= spa_verify_host(spa
, mos_config
);
4344 nvlist_free(mos_config
);
4349 nv
= fnvlist_lookup_nvlist(mos_config
, ZPOOL_CONFIG_VDEV_TREE
);
4351 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4354 * Build a new vdev tree from the trusted config
4356 error
= spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
);
4358 nvlist_free(mos_config
);
4359 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4360 spa_load_failed(spa
, "spa_config_parse failed [error=%d]",
4362 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4366 * Vdev paths in the MOS may be obsolete. If the untrusted config was
4367 * obtained by scanning /dev/dsk, then it will have the right vdev
4368 * paths. We update the trusted MOS config with this information.
4369 * We first try to copy the paths with vdev_copy_path_strict, which
4370 * succeeds only when both configs have exactly the same vdev tree.
4371 * If that fails, we fall back to a more flexible method that has a
4372 * best effort policy.
4374 copy_error
= vdev_copy_path_strict(rvd
, mrvd
);
4375 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
4376 spa_load_note(spa
, "provided vdev tree:");
4377 vdev_dbgmsg_print_tree(rvd
, 2);
4378 spa_load_note(spa
, "MOS vdev tree:");
4379 vdev_dbgmsg_print_tree(mrvd
, 2);
4381 if (copy_error
!= 0) {
4382 spa_load_note(spa
, "vdev_copy_path_strict failed, falling "
4383 "back to vdev_copy_path_relaxed");
4384 vdev_copy_path_relaxed(rvd
, mrvd
);
4389 spa
->spa_root_vdev
= mrvd
;
4391 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4394 * If 'zpool import' used a cached config, then the on-disk hostid and
4395 * hostname may be different to the cached config in ways that should
4396 * prevent import. Userspace can't discover this without a scan, but
4397 * we know, so we add these values to LOAD_INFO so the caller can know
4400 * Note that we have to do this before the config is regenerated,
4401 * because the new config will have the hostid and hostname for this
4402 * host, in readiness for import.
4404 if (nvlist_exists(mos_config
, ZPOOL_CONFIG_HOSTID
))
4405 fnvlist_add_uint64(spa
->spa_load_info
, ZPOOL_CONFIG_HOSTID
,
4406 fnvlist_lookup_uint64(mos_config
, ZPOOL_CONFIG_HOSTID
));
4407 if (nvlist_exists(mos_config
, ZPOOL_CONFIG_HOSTNAME
))
4408 fnvlist_add_string(spa
->spa_load_info
, ZPOOL_CONFIG_HOSTNAME
,
4409 fnvlist_lookup_string(mos_config
, ZPOOL_CONFIG_HOSTNAME
));
4412 * We will use spa_config if we decide to reload the spa or if spa_load
4413 * fails and we rewind. We must thus regenerate the config using the
4414 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
4415 * pass settings on how to load the pool and is not stored in the MOS.
4416 * We copy it over to our new, trusted config.
4418 mos_config_txg
= fnvlist_lookup_uint64(mos_config
,
4419 ZPOOL_CONFIG_POOL_TXG
);
4420 nvlist_free(mos_config
);
4421 mos_config
= spa_config_generate(spa
, NULL
, mos_config_txg
, B_FALSE
);
4422 if (nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_LOAD_POLICY
,
4424 fnvlist_add_nvlist(mos_config
, ZPOOL_LOAD_POLICY
, policy
);
4425 spa_config_set(spa
, mos_config
);
4426 spa
->spa_config_source
= SPA_CONFIG_SRC_MOS
;
4429 * Now that we got the config from the MOS, we should be more strict
4430 * in checking blkptrs and can make assumptions about the consistency
4431 * of the vdev tree. spa_trust_config must be set to true before opening
4432 * vdevs in order for them to be writeable.
4434 spa
->spa_trust_config
= B_TRUE
;
4437 * Open and validate the new vdev tree
4439 error
= spa_ld_open_vdevs(spa
);
4443 error
= spa_ld_validate_vdevs(spa
);
4447 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
4448 spa_load_note(spa
, "final vdev tree:");
4449 vdev_dbgmsg_print_tree(rvd
, 2);
4452 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
&&
4453 !spa
->spa_extreme_rewind
&& zfs_max_missing_tvds
== 0) {
4455 * Sanity check to make sure that we are indeed loading the
4456 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
4457 * in the config provided and they happened to be the only ones
4458 * to have the latest uberblock, we could involuntarily perform
4459 * an extreme rewind.
4461 healthy_tvds_mos
= spa_healthy_core_tvds(spa
);
4462 if (healthy_tvds_mos
- healthy_tvds
>=
4463 SPA_SYNC_MIN_VDEVS
) {
4464 spa_load_note(spa
, "config provided misses too many "
4465 "top-level vdevs compared to MOS (%lld vs %lld). ",
4466 (u_longlong_t
)healthy_tvds
,
4467 (u_longlong_t
)healthy_tvds_mos
);
4468 spa_load_note(spa
, "vdev tree:");
4469 vdev_dbgmsg_print_tree(rvd
, 2);
4471 spa_load_failed(spa
, "config was already "
4472 "provided from MOS. Aborting.");
4473 return (spa_vdev_err(rvd
,
4474 VDEV_AUX_CORRUPT_DATA
, EIO
));
4476 spa_load_note(spa
, "spa must be reloaded using MOS "
4478 return (SET_ERROR(EAGAIN
));
4482 error
= spa_check_for_missing_logs(spa
);
4484 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
4486 if (rvd
->vdev_guid_sum
!= spa
->spa_uberblock
.ub_guid_sum
) {
4487 spa_load_failed(spa
, "uberblock guid sum doesn't match MOS "
4488 "guid sum (%llu != %llu)",
4489 (u_longlong_t
)spa
->spa_uberblock
.ub_guid_sum
,
4490 (u_longlong_t
)rvd
->vdev_guid_sum
);
4491 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
4499 spa_ld_open_indirect_vdev_metadata(spa_t
*spa
)
4502 vdev_t
*rvd
= spa
->spa_root_vdev
;
4505 * Everything that we read before spa_remove_init() must be stored
4506 * on concreted vdevs. Therefore we do this as early as possible.
4508 error
= spa_remove_init(spa
);
4510 spa_load_failed(spa
, "spa_remove_init failed [error=%d]",
4512 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4516 * Retrieve information needed to condense indirect vdev mappings.
4518 error
= spa_condense_init(spa
);
4520 spa_load_failed(spa
, "spa_condense_init failed [error=%d]",
4522 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4529 spa_ld_check_features(spa_t
*spa
, boolean_t
*missing_feat_writep
)
4532 vdev_t
*rvd
= spa
->spa_root_vdev
;
4534 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
4535 boolean_t missing_feat_read
= B_FALSE
;
4536 nvlist_t
*unsup_feat
, *enabled_feat
;
4538 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
4539 &spa
->spa_feat_for_read_obj
, B_TRUE
) != 0) {
4540 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4543 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
4544 &spa
->spa_feat_for_write_obj
, B_TRUE
) != 0) {
4545 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4548 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
4549 &spa
->spa_feat_desc_obj
, B_TRUE
) != 0) {
4550 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4553 enabled_feat
= fnvlist_alloc();
4554 unsup_feat
= fnvlist_alloc();
4556 if (!spa_features_check(spa
, B_FALSE
,
4557 unsup_feat
, enabled_feat
))
4558 missing_feat_read
= B_TRUE
;
4560 if (spa_writeable(spa
) ||
4561 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) {
4562 if (!spa_features_check(spa
, B_TRUE
,
4563 unsup_feat
, enabled_feat
)) {
4564 *missing_feat_writep
= B_TRUE
;
4568 fnvlist_add_nvlist(spa
->spa_load_info
,
4569 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
4571 if (!nvlist_empty(unsup_feat
)) {
4572 fnvlist_add_nvlist(spa
->spa_load_info
,
4573 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
4576 fnvlist_free(enabled_feat
);
4577 fnvlist_free(unsup_feat
);
4579 if (!missing_feat_read
) {
4580 fnvlist_add_boolean(spa
->spa_load_info
,
4581 ZPOOL_CONFIG_CAN_RDONLY
);
4585 * If the state is SPA_LOAD_TRYIMPORT, our objective is
4586 * twofold: to determine whether the pool is available for
4587 * import in read-write mode and (if it is not) whether the
4588 * pool is available for import in read-only mode. If the pool
4589 * is available for import in read-write mode, it is displayed
4590 * as available in userland; if it is not available for import
4591 * in read-only mode, it is displayed as unavailable in
4592 * userland. If the pool is available for import in read-only
4593 * mode but not read-write mode, it is displayed as unavailable
4594 * in userland with a special note that the pool is actually
4595 * available for open in read-only mode.
4597 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
4598 * missing a feature for write, we must first determine whether
4599 * the pool can be opened read-only before returning to
4600 * userland in order to know whether to display the
4601 * abovementioned note.
4603 if (missing_feat_read
|| (*missing_feat_writep
&&
4604 spa_writeable(spa
))) {
4605 spa_load_failed(spa
, "pool uses unsupported features");
4606 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
4611 * Load refcounts for ZFS features from disk into an in-memory
4612 * cache during SPA initialization.
4614 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
4617 error
= feature_get_refcount_from_disk(spa
,
4618 &spa_feature_table
[i
], &refcount
);
4620 spa
->spa_feat_refcount_cache
[i
] = refcount
;
4621 } else if (error
== ENOTSUP
) {
4622 spa
->spa_feat_refcount_cache
[i
] =
4623 SPA_FEATURE_DISABLED
;
4625 spa_load_failed(spa
, "error getting refcount "
4626 "for feature %s [error=%d]",
4627 spa_feature_table
[i
].fi_guid
, error
);
4628 return (spa_vdev_err(rvd
,
4629 VDEV_AUX_CORRUPT_DATA
, EIO
));
4634 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
4635 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
4636 &spa
->spa_feat_enabled_txg_obj
, B_TRUE
) != 0)
4637 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4641 * Encryption was added before bookmark_v2, even though bookmark_v2
4642 * is now a dependency. If this pool has encryption enabled without
4643 * bookmark_v2, trigger an errata message.
4645 if (spa_feature_is_enabled(spa
, SPA_FEATURE_ENCRYPTION
) &&
4646 !spa_feature_is_enabled(spa
, SPA_FEATURE_BOOKMARK_V2
)) {
4647 spa
->spa_errata
= ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
;
4654 spa_ld_load_special_directories(spa_t
*spa
)
4657 vdev_t
*rvd
= spa
->spa_root_vdev
;
4659 spa
->spa_is_initializing
= B_TRUE
;
4660 error
= dsl_pool_open(spa
->spa_dsl_pool
);
4661 spa
->spa_is_initializing
= B_FALSE
;
4663 spa_load_failed(spa
, "dsl_pool_open failed [error=%d]", error
);
4664 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4671 spa_ld_get_props(spa_t
*spa
)
4675 vdev_t
*rvd
= spa
->spa_root_vdev
;
4677 /* Grab the checksum salt from the MOS. */
4678 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4679 DMU_POOL_CHECKSUM_SALT
, 1,
4680 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
4681 spa
->spa_cksum_salt
.zcs_bytes
);
4682 if (error
== ENOENT
) {
4683 /* Generate a new salt for subsequent use */
4684 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
4685 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
4686 } else if (error
!= 0) {
4687 spa_load_failed(spa
, "unable to retrieve checksum salt from "
4688 "MOS [error=%d]", error
);
4689 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4692 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
, B_TRUE
) != 0)
4693 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4694 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
4696 spa_load_failed(spa
, "error opening deferred-frees bpobj "
4697 "[error=%d]", error
);
4698 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4702 * Load the bit that tells us to use the new accounting function
4703 * (raid-z deflation). If we have an older pool, this will not
4706 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
, B_FALSE
);
4707 if (error
!= 0 && error
!= ENOENT
)
4708 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4710 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
4711 &spa
->spa_creation_version
, B_FALSE
);
4712 if (error
!= 0 && error
!= ENOENT
)
4713 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4716 * Load the persistent error log. If we have an older pool, this will
4719 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
,
4721 if (error
!= 0 && error
!= ENOENT
)
4722 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4724 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
4725 &spa
->spa_errlog_scrub
, B_FALSE
);
4726 if (error
!= 0 && error
!= ENOENT
)
4727 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4730 * Load the livelist deletion field. If a livelist is queued for
4731 * deletion, indicate that in the spa
4733 error
= spa_dir_prop(spa
, DMU_POOL_DELETED_CLONES
,
4734 &spa
->spa_livelists_to_delete
, B_FALSE
);
4735 if (error
!= 0 && error
!= ENOENT
)
4736 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4739 * Load the history object. If we have an older pool, this
4740 * will not be present.
4742 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
, B_FALSE
);
4743 if (error
!= 0 && error
!= ENOENT
)
4744 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4747 * Load the per-vdev ZAP map. If we have an older pool, this will not
4748 * be present; in this case, defer its creation to a later time to
4749 * avoid dirtying the MOS this early / out of sync context. See
4750 * spa_sync_config_object.
4753 /* The sentinel is only available in the MOS config. */
4754 nvlist_t
*mos_config
;
4755 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0) {
4756 spa_load_failed(spa
, "unable to retrieve MOS config");
4757 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4760 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
4761 &spa
->spa_all_vdev_zaps
, B_FALSE
);
4763 if (error
== ENOENT
) {
4764 VERIFY(!nvlist_exists(mos_config
,
4765 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
4766 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
4767 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4768 } else if (error
!= 0) {
4769 nvlist_free(mos_config
);
4770 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4771 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
4773 * An older version of ZFS overwrote the sentinel value, so
4774 * we have orphaned per-vdev ZAPs in the MOS. Defer their
4775 * destruction to later; see spa_sync_config_object.
4777 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
4779 * We're assuming that no vdevs have had their ZAPs created
4780 * before this. Better be sure of it.
4782 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4784 nvlist_free(mos_config
);
4786 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
4788 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
,
4790 if (error
&& error
!= ENOENT
)
4791 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4794 uint64_t autoreplace
= 0;
4796 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
4797 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
4798 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
4799 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
4800 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
4801 spa_prop_find(spa
, ZPOOL_PROP_DEDUP_TABLE_QUOTA
,
4802 &spa
->spa_dedup_table_quota
);
4803 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
4804 spa_prop_find(spa
, ZPOOL_PROP_AUTOTRIM
, &spa
->spa_autotrim
);
4805 spa
->spa_autoreplace
= (autoreplace
!= 0);
4809 * If we are importing a pool with missing top-level vdevs,
4810 * we enforce that the pool doesn't panic or get suspended on
4811 * error since the likelihood of missing data is extremely high.
4813 if (spa
->spa_missing_tvds
> 0 &&
4814 spa
->spa_failmode
!= ZIO_FAILURE_MODE_CONTINUE
&&
4815 spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4816 spa_load_note(spa
, "forcing failmode to 'continue' "
4817 "as some top level vdevs are missing");
4818 spa
->spa_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
4825 spa_ld_open_aux_vdevs(spa_t
*spa
, spa_import_type_t type
)
4828 vdev_t
*rvd
= spa
->spa_root_vdev
;
4831 * If we're assembling the pool from the split-off vdevs of
4832 * an existing pool, we don't want to attach the spares & cache
4837 * Load any hot spares for this pool.
4839 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
,
4841 if (error
!= 0 && error
!= ENOENT
)
4842 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4843 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4844 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
4845 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
4846 &spa
->spa_spares
.sav_config
) != 0) {
4847 spa_load_failed(spa
, "error loading spares nvlist");
4848 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4851 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4852 spa_load_spares(spa
);
4853 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4854 } else if (error
== 0) {
4855 spa
->spa_spares
.sav_sync
= B_TRUE
;
4859 * Load any level 2 ARC devices for this pool.
4861 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
4862 &spa
->spa_l2cache
.sav_object
, B_FALSE
);
4863 if (error
!= 0 && error
!= ENOENT
)
4864 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4865 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4866 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
4867 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
4868 &spa
->spa_l2cache
.sav_config
) != 0) {
4869 spa_load_failed(spa
, "error loading l2cache nvlist");
4870 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4873 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4874 spa_load_l2cache(spa
);
4875 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4876 } else if (error
== 0) {
4877 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4884 spa_ld_load_vdev_metadata(spa_t
*spa
)
4887 vdev_t
*rvd
= spa
->spa_root_vdev
;
4890 * If the 'multihost' property is set, then never allow a pool to
4891 * be imported when the system hostid is zero. The exception to
4892 * this rule is zdb which is always allowed to access pools.
4894 if (spa_multihost(spa
) && spa_get_hostid(spa
) == 0 &&
4895 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
4896 fnvlist_add_uint64(spa
->spa_load_info
,
4897 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
4898 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
4902 * If the 'autoreplace' property is set, then post a resource notifying
4903 * the ZFS DE that it should not issue any faults for unopenable
4904 * devices. We also iterate over the vdevs, and post a sysevent for any
4905 * unopenable vdevs so that the normal autoreplace handler can take
4908 if (spa
->spa_autoreplace
&& spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4909 spa_check_removed(spa
->spa_root_vdev
);
4911 * For the import case, this is done in spa_import(), because
4912 * at this point we're using the spare definitions from
4913 * the MOS config, not necessarily from the userland config.
4915 if (spa
->spa_load_state
!= SPA_LOAD_IMPORT
) {
4916 spa_aux_check_removed(&spa
->spa_spares
);
4917 spa_aux_check_removed(&spa
->spa_l2cache
);
4922 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
4924 error
= vdev_load(rvd
);
4926 spa_load_failed(spa
, "vdev_load failed [error=%d]", error
);
4927 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4930 error
= spa_ld_log_spacemaps(spa
);
4932 spa_load_failed(spa
, "spa_ld_log_spacemaps failed [error=%d]",
4934 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4938 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
4940 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4941 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
, B_FALSE
);
4942 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4948 spa_ld_load_dedup_tables(spa_t
*spa
)
4951 vdev_t
*rvd
= spa
->spa_root_vdev
;
4953 error
= ddt_load(spa
);
4955 spa_load_failed(spa
, "ddt_load failed [error=%d]", error
);
4956 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4963 spa_ld_load_brt(spa_t
*spa
)
4966 vdev_t
*rvd
= spa
->spa_root_vdev
;
4968 error
= brt_load(spa
);
4970 spa_load_failed(spa
, "brt_load failed [error=%d]", error
);
4971 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4978 spa_ld_verify_logs(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
4980 vdev_t
*rvd
= spa
->spa_root_vdev
;
4982 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
)) {
4983 boolean_t missing
= spa_check_logs(spa
);
4985 if (spa
->spa_missing_tvds
!= 0) {
4986 spa_load_note(spa
, "spa_check_logs failed "
4987 "so dropping the logs");
4989 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
4990 spa_load_failed(spa
, "spa_check_logs failed");
4991 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
,
5001 spa_ld_verify_pool_data(spa_t
*spa
)
5004 vdev_t
*rvd
= spa
->spa_root_vdev
;
5007 * We've successfully opened the pool, verify that we're ready
5008 * to start pushing transactions.
5010 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
5011 error
= spa_load_verify(spa
);
5013 spa_load_failed(spa
, "spa_load_verify failed "
5014 "[error=%d]", error
);
5015 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
5024 spa_ld_claim_log_blocks(spa_t
*spa
)
5027 dsl_pool_t
*dp
= spa_get_dsl(spa
);
5030 * Claim log blocks that haven't been committed yet.
5031 * This must all happen in a single txg.
5032 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
5033 * invoked from zil_claim_log_block()'s i/o done callback.
5034 * Price of rollback is that we abandon the log.
5036 spa
->spa_claiming
= B_TRUE
;
5038 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
5039 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
5040 zil_claim
, tx
, DS_FIND_CHILDREN
);
5043 spa
->spa_claiming
= B_FALSE
;
5045 spa_set_log_state(spa
, SPA_LOG_GOOD
);
5049 spa_ld_check_for_config_update(spa_t
*spa
, uint64_t config_cache_txg
,
5050 boolean_t update_config_cache
)
5052 vdev_t
*rvd
= spa
->spa_root_vdev
;
5053 int need_update
= B_FALSE
;
5056 * If the config cache is stale, or we have uninitialized
5057 * metaslabs (see spa_vdev_add()), then update the config.
5059 * If this is a verbatim import, trust the current
5060 * in-core spa_config and update the disk labels.
5062 if (update_config_cache
|| config_cache_txg
!= spa
->spa_config_txg
||
5063 spa
->spa_load_state
== SPA_LOAD_IMPORT
||
5064 spa
->spa_load_state
== SPA_LOAD_RECOVER
||
5065 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
5066 need_update
= B_TRUE
;
5068 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
5069 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
5070 need_update
= B_TRUE
;
5073 * Update the config cache asynchronously in case we're the
5074 * root pool, in which case the config cache isn't writable yet.
5077 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
5081 spa_ld_prepare_for_reload(spa_t
*spa
)
5083 spa_mode_t mode
= spa
->spa_mode
;
5084 int async_suspended
= spa
->spa_async_suspended
;
5087 spa_deactivate(spa
);
5088 spa_activate(spa
, mode
);
5091 * We save the value of spa_async_suspended as it gets reset to 0 by
5092 * spa_unload(). We want to restore it back to the original value before
5093 * returning as we might be calling spa_async_resume() later.
5095 spa
->spa_async_suspended
= async_suspended
;
5099 spa_ld_read_checkpoint_txg(spa_t
*spa
)
5101 uberblock_t checkpoint
;
5104 ASSERT0(spa
->spa_checkpoint_txg
);
5105 ASSERT(MUTEX_HELD(&spa_namespace_lock
) ||
5106 spa
->spa_load_thread
== curthread
);
5108 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
5109 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
5110 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
5112 if (error
== ENOENT
)
5118 ASSERT3U(checkpoint
.ub_txg
, !=, 0);
5119 ASSERT3U(checkpoint
.ub_checkpoint_txg
, !=, 0);
5120 ASSERT3U(checkpoint
.ub_timestamp
, !=, 0);
5121 spa
->spa_checkpoint_txg
= checkpoint
.ub_txg
;
5122 spa
->spa_checkpoint_info
.sci_timestamp
= checkpoint
.ub_timestamp
;
5128 spa_ld_mos_init(spa_t
*spa
, spa_import_type_t type
)
5132 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5133 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
5136 * Never trust the config that is provided unless we are assembling
5137 * a pool following a split.
5138 * This means don't trust blkptrs and the vdev tree in general. This
5139 * also effectively puts the spa in read-only mode since
5140 * spa_writeable() checks for spa_trust_config to be true.
5141 * We will later load a trusted config from the MOS.
5143 if (type
!= SPA_IMPORT_ASSEMBLE
)
5144 spa
->spa_trust_config
= B_FALSE
;
5147 * Parse the config provided to create a vdev tree.
5149 error
= spa_ld_parse_config(spa
, type
);
5153 spa_import_progress_add(spa
);
5156 * Now that we have the vdev tree, try to open each vdev. This involves
5157 * opening the underlying physical device, retrieving its geometry and
5158 * probing the vdev with a dummy I/O. The state of each vdev will be set
5159 * based on the success of those operations. After this we'll be ready
5160 * to read from the vdevs.
5162 error
= spa_ld_open_vdevs(spa
);
5167 * Read the label of each vdev and make sure that the GUIDs stored
5168 * there match the GUIDs in the config provided.
5169 * If we're assembling a new pool that's been split off from an
5170 * existing pool, the labels haven't yet been updated so we skip
5171 * validation for now.
5173 if (type
!= SPA_IMPORT_ASSEMBLE
) {
5174 error
= spa_ld_validate_vdevs(spa
);
5180 * Read all vdev labels to find the best uberblock (i.e. latest,
5181 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
5182 * get the list of features required to read blkptrs in the MOS from
5183 * the vdev label with the best uberblock and verify that our version
5184 * of zfs supports them all.
5186 error
= spa_ld_select_uberblock(spa
, type
);
5191 * Pass that uberblock to the dsl_pool layer which will open the root
5192 * blkptr. This blkptr points to the latest version of the MOS and will
5193 * allow us to read its contents.
5195 error
= spa_ld_open_rootbp(spa
);
5203 spa_ld_checkpoint_rewind(spa_t
*spa
)
5205 uberblock_t checkpoint
;
5208 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5209 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
5211 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
5212 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
5213 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
5216 spa_load_failed(spa
, "unable to retrieve checkpointed "
5217 "uberblock from the MOS config [error=%d]", error
);
5219 if (error
== ENOENT
)
5220 error
= ZFS_ERR_NO_CHECKPOINT
;
5225 ASSERT3U(checkpoint
.ub_txg
, <, spa
->spa_uberblock
.ub_txg
);
5226 ASSERT3U(checkpoint
.ub_txg
, ==, checkpoint
.ub_checkpoint_txg
);
5229 * We need to update the txg and timestamp of the checkpointed
5230 * uberblock to be higher than the latest one. This ensures that
5231 * the checkpointed uberblock is selected if we were to close and
5232 * reopen the pool right after we've written it in the vdev labels.
5233 * (also see block comment in vdev_uberblock_compare)
5235 checkpoint
.ub_txg
= spa
->spa_uberblock
.ub_txg
+ 1;
5236 checkpoint
.ub_timestamp
= gethrestime_sec();
5239 * Set current uberblock to be the checkpointed uberblock.
5241 spa
->spa_uberblock
= checkpoint
;
5244 * If we are doing a normal rewind, then the pool is open for
5245 * writing and we sync the "updated" checkpointed uberblock to
5246 * disk. Once this is done, we've basically rewound the whole
5247 * pool and there is no way back.
5249 * There are cases when we don't want to attempt and sync the
5250 * checkpointed uberblock to disk because we are opening a
5251 * pool as read-only. Specifically, verifying the checkpointed
5252 * state with zdb, and importing the checkpointed state to get
5253 * a "preview" of its content.
5255 if (spa_writeable(spa
)) {
5256 vdev_t
*rvd
= spa
->spa_root_vdev
;
5258 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5259 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
5261 int children
= rvd
->vdev_children
;
5262 int c0
= random_in_range(children
);
5264 for (int c
= 0; c
< children
; c
++) {
5265 vdev_t
*vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
5267 /* Stop when revisiting the first vdev */
5268 if (c
> 0 && svd
[0] == vd
)
5271 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
5272 !vdev_is_concrete(vd
))
5275 svd
[svdcount
++] = vd
;
5276 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
5279 error
= vdev_config_sync(svd
, svdcount
, spa
->spa_first_txg
);
5281 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
5282 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5285 spa_load_failed(spa
, "failed to write checkpointed "
5286 "uberblock to the vdev labels [error=%d]", error
);
5295 spa_ld_mos_with_trusted_config(spa_t
*spa
, spa_import_type_t type
,
5296 boolean_t
*update_config_cache
)
5301 * Parse the config for pool, open and validate vdevs,
5302 * select an uberblock, and use that uberblock to open
5305 error
= spa_ld_mos_init(spa
, type
);
5310 * Retrieve the trusted config stored in the MOS and use it to create
5311 * a new, exact version of the vdev tree, then reopen all vdevs.
5313 error
= spa_ld_trusted_config(spa
, type
, B_FALSE
);
5314 if (error
== EAGAIN
) {
5315 if (update_config_cache
!= NULL
)
5316 *update_config_cache
= B_TRUE
;
5319 * Redo the loading process with the trusted config if it is
5320 * too different from the untrusted config.
5322 spa_ld_prepare_for_reload(spa
);
5323 spa_load_note(spa
, "RELOADING");
5324 error
= spa_ld_mos_init(spa
, type
);
5328 error
= spa_ld_trusted_config(spa
, type
, B_TRUE
);
5332 } else if (error
!= 0) {
5340 * Load an existing storage pool, using the config provided. This config
5341 * describes which vdevs are part of the pool and is later validated against
5342 * partial configs present in each vdev's label and an entire copy of the
5343 * config stored in the MOS.
5346 spa_load_impl(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
5349 boolean_t missing_feat_write
= B_FALSE
;
5350 boolean_t checkpoint_rewind
=
5351 (spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
5352 boolean_t update_config_cache
= B_FALSE
;
5353 hrtime_t load_start
= gethrtime();
5355 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5356 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
5358 spa_load_note(spa
, "LOADING");
5360 error
= spa_ld_mos_with_trusted_config(spa
, type
, &update_config_cache
);
5365 * If we are rewinding to the checkpoint then we need to repeat
5366 * everything we've done so far in this function but this time
5367 * selecting the checkpointed uberblock and using that to open
5370 if (checkpoint_rewind
) {
5372 * If we are rewinding to the checkpoint update config cache
5375 update_config_cache
= B_TRUE
;
5378 * Extract the checkpointed uberblock from the current MOS
5379 * and use this as the pool's uberblock from now on. If the
5380 * pool is imported as writeable we also write the checkpoint
5381 * uberblock to the labels, making the rewind permanent.
5383 error
= spa_ld_checkpoint_rewind(spa
);
5388 * Redo the loading process again with the
5389 * checkpointed uberblock.
5391 spa_ld_prepare_for_reload(spa
);
5392 spa_load_note(spa
, "LOADING checkpointed uberblock");
5393 error
= spa_ld_mos_with_trusted_config(spa
, type
, NULL
);
5399 * Drop the namespace lock for the rest of the function.
5401 spa
->spa_load_thread
= curthread
;
5402 mutex_exit(&spa_namespace_lock
);
5405 * Retrieve the checkpoint txg if the pool has a checkpoint.
5407 spa_import_progress_set_notes(spa
, "Loading checkpoint txg");
5408 error
= spa_ld_read_checkpoint_txg(spa
);
5413 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
5414 * from the pool and their contents were re-mapped to other vdevs. Note
5415 * that everything that we read before this step must have been
5416 * rewritten on concrete vdevs after the last device removal was
5417 * initiated. Otherwise we could be reading from indirect vdevs before
5418 * we have loaded their mappings.
5420 spa_import_progress_set_notes(spa
, "Loading indirect vdev metadata");
5421 error
= spa_ld_open_indirect_vdev_metadata(spa
);
5426 * Retrieve the full list of active features from the MOS and check if
5427 * they are all supported.
5429 spa_import_progress_set_notes(spa
, "Checking feature flags");
5430 error
= spa_ld_check_features(spa
, &missing_feat_write
);
5435 * Load several special directories from the MOS needed by the dsl_pool
5438 spa_import_progress_set_notes(spa
, "Loading special MOS directories");
5439 error
= spa_ld_load_special_directories(spa
);
5444 * Retrieve pool properties from the MOS.
5446 spa_import_progress_set_notes(spa
, "Loading properties");
5447 error
= spa_ld_get_props(spa
);
5452 * Retrieve the list of auxiliary devices - cache devices and spares -
5455 spa_import_progress_set_notes(spa
, "Loading AUX vdevs");
5456 error
= spa_ld_open_aux_vdevs(spa
, type
);
5461 * Load the metadata for all vdevs. Also check if unopenable devices
5462 * should be autoreplaced.
5464 spa_import_progress_set_notes(spa
, "Loading vdev metadata");
5465 error
= spa_ld_load_vdev_metadata(spa
);
5469 spa_import_progress_set_notes(spa
, "Loading dedup tables");
5470 error
= spa_ld_load_dedup_tables(spa
);
5474 spa_import_progress_set_notes(spa
, "Loading BRT");
5475 error
= spa_ld_load_brt(spa
);
5480 * Verify the logs now to make sure we don't have any unexpected errors
5481 * when we claim log blocks later.
5483 spa_import_progress_set_notes(spa
, "Verifying Log Devices");
5484 error
= spa_ld_verify_logs(spa
, type
, ereport
);
5488 if (missing_feat_write
) {
5489 ASSERT(spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
);
5492 * At this point, we know that we can open the pool in
5493 * read-only mode but not read-write mode. We now have enough
5494 * information and can return to userland.
5496 error
= spa_vdev_err(spa
->spa_root_vdev
, VDEV_AUX_UNSUP_FEAT
,
5502 * Traverse the last txgs to make sure the pool was left off in a safe
5503 * state. When performing an extreme rewind, we verify the whole pool,
5504 * which can take a very long time.
5506 spa_import_progress_set_notes(spa
, "Verifying pool data");
5507 error
= spa_ld_verify_pool_data(spa
);
5512 * Calculate the deflated space for the pool. This must be done before
5513 * we write anything to the pool because we'd need to update the space
5514 * accounting using the deflated sizes.
5516 spa_import_progress_set_notes(spa
, "Calculating deflated space");
5517 spa_update_dspace(spa
);
5520 * We have now retrieved all the information we needed to open the
5521 * pool. If we are importing the pool in read-write mode, a few
5522 * additional steps must be performed to finish the import.
5524 spa_import_progress_set_notes(spa
, "Starting import");
5525 if (spa_writeable(spa
) && (spa
->spa_load_state
== SPA_LOAD_RECOVER
||
5526 spa
->spa_load_max_txg
== UINT64_MAX
)) {
5527 uint64_t config_cache_txg
= spa
->spa_config_txg
;
5529 ASSERT(spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
);
5532 * Before we do any zio_write's, complete the raidz expansion
5533 * scratch space copying, if necessary.
5535 if (RRSS_GET_STATE(&spa
->spa_uberblock
) == RRSS_SCRATCH_VALID
)
5536 vdev_raidz_reflow_copy_scratch(spa
);
5539 * In case of a checkpoint rewind, log the original txg
5540 * of the checkpointed uberblock.
5542 if (checkpoint_rewind
) {
5543 spa_history_log_internal(spa
, "checkpoint rewind",
5544 NULL
, "rewound state to txg=%llu",
5545 (u_longlong_t
)spa
->spa_uberblock
.ub_checkpoint_txg
);
5548 spa_import_progress_set_notes(spa
, "Claiming ZIL blocks");
5550 * Traverse the ZIL and claim all blocks.
5552 spa_ld_claim_log_blocks(spa
);
5555 * Kick-off the syncing thread.
5557 spa
->spa_sync_on
= B_TRUE
;
5558 txg_sync_start(spa
->spa_dsl_pool
);
5559 mmp_thread_start(spa
);
5562 * Wait for all claims to sync. We sync up to the highest
5563 * claimed log block birth time so that claimed log blocks
5564 * don't appear to be from the future. spa_claim_max_txg
5565 * will have been set for us by ZIL traversal operations
5568 spa_import_progress_set_notes(spa
, "Syncing ZIL claims");
5569 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
5572 * Check if we need to request an update of the config. On the
5573 * next sync, we would update the config stored in vdev labels
5574 * and the cachefile (by default /etc/zfs/zpool.cache).
5576 spa_import_progress_set_notes(spa
, "Updating configs");
5577 spa_ld_check_for_config_update(spa
, config_cache_txg
,
5578 update_config_cache
);
5581 * Check if a rebuild was in progress and if so resume it.
5582 * Then check all DTLs to see if anything needs resilvering.
5583 * The resilver will be deferred if a rebuild was started.
5585 spa_import_progress_set_notes(spa
, "Starting resilvers");
5586 if (vdev_rebuild_active(spa
->spa_root_vdev
)) {
5587 vdev_rebuild_restart(spa
);
5588 } else if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
5589 vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5590 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
5594 * Log the fact that we booted up (so that we can detect if
5595 * we rebooted in the middle of an operation).
5597 spa_history_log_version(spa
, "open", NULL
);
5599 spa_import_progress_set_notes(spa
,
5600 "Restarting device removals");
5601 spa_restart_removal(spa
);
5602 spa_spawn_aux_threads(spa
);
5605 * Delete any inconsistent datasets.
5608 * Since we may be issuing deletes for clones here,
5609 * we make sure to do so after we've spawned all the
5610 * auxiliary threads above (from which the livelist
5611 * deletion zthr is part of).
5613 spa_import_progress_set_notes(spa
,
5614 "Cleaning up inconsistent objsets");
5615 (void) dmu_objset_find(spa_name(spa
),
5616 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
5619 * Clean up any stale temporary dataset userrefs.
5621 spa_import_progress_set_notes(spa
,
5622 "Cleaning up temporary userrefs");
5623 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
5625 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5626 spa_import_progress_set_notes(spa
, "Restarting initialize");
5627 vdev_initialize_restart(spa
->spa_root_vdev
);
5628 spa_import_progress_set_notes(spa
, "Restarting TRIM");
5629 vdev_trim_restart(spa
->spa_root_vdev
);
5630 vdev_autotrim_restart(spa
);
5631 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5632 spa_import_progress_set_notes(spa
, "Finished importing");
5634 zio_handle_import_delay(spa
, gethrtime() - load_start
);
5636 spa_import_progress_remove(spa_guid(spa
));
5637 spa_async_request(spa
, SPA_ASYNC_L2CACHE_REBUILD
);
5639 spa_load_note(spa
, "LOADED");
5641 mutex_enter(&spa_namespace_lock
);
5642 spa
->spa_load_thread
= NULL
;
5643 cv_broadcast(&spa_namespace_cv
);
5650 spa_load_retry(spa_t
*spa
, spa_load_state_t state
)
5652 spa_mode_t mode
= spa
->spa_mode
;
5655 spa_deactivate(spa
);
5657 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
5659 spa_activate(spa
, mode
);
5660 spa_async_suspend(spa
);
5662 spa_load_note(spa
, "spa_load_retry: rewind, max txg: %llu",
5663 (u_longlong_t
)spa
->spa_load_max_txg
);
5665 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
));
5669 * If spa_load() fails this function will try loading prior txg's. If
5670 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
5671 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
5672 * function will not rewind the pool and will return the same error as
5676 spa_load_best(spa_t
*spa
, spa_load_state_t state
, uint64_t max_request
,
5679 nvlist_t
*loadinfo
= NULL
;
5680 nvlist_t
*config
= NULL
;
5681 int load_error
, rewind_error
;
5682 uint64_t safe_rewind_txg
;
5685 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
5686 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
5687 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5689 spa
->spa_load_max_txg
= max_request
;
5690 if (max_request
!= UINT64_MAX
)
5691 spa
->spa_extreme_rewind
= B_TRUE
;
5694 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
);
5695 if (load_error
== 0)
5697 if (load_error
== ZFS_ERR_NO_CHECKPOINT
) {
5699 * When attempting checkpoint-rewind on a pool with no
5700 * checkpoint, we should not attempt to load uberblocks
5701 * from previous txgs when spa_load fails.
5703 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
5704 spa_import_progress_remove(spa_guid(spa
));
5705 return (load_error
);
5708 if (spa
->spa_root_vdev
!= NULL
)
5709 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5711 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
5712 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
5714 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
5715 nvlist_free(config
);
5716 spa_import_progress_remove(spa_guid(spa
));
5717 return (load_error
);
5720 if (state
== SPA_LOAD_RECOVER
) {
5721 /* Price of rolling back is discarding txgs, including log */
5722 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5725 * If we aren't rolling back save the load info from our first
5726 * import attempt so that we can restore it after attempting
5729 loadinfo
= spa
->spa_load_info
;
5730 spa
->spa_load_info
= fnvlist_alloc();
5733 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
5734 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
5735 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
5736 TXG_INITIAL
: safe_rewind_txg
;
5739 * Continue as long as we're finding errors, we're still within
5740 * the acceptable rewind range, and we're still finding uberblocks
5742 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
5743 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
5744 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
5745 spa
->spa_extreme_rewind
= B_TRUE
;
5746 rewind_error
= spa_load_retry(spa
, state
);
5749 spa
->spa_extreme_rewind
= B_FALSE
;
5750 spa
->spa_load_max_txg
= UINT64_MAX
;
5752 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
5753 spa_config_set(spa
, config
);
5755 nvlist_free(config
);
5757 if (state
== SPA_LOAD_RECOVER
) {
5758 ASSERT3P(loadinfo
, ==, NULL
);
5759 spa_import_progress_remove(spa_guid(spa
));
5760 return (rewind_error
);
5762 /* Store the rewind info as part of the initial load info */
5763 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
5764 spa
->spa_load_info
);
5766 /* Restore the initial load info */
5767 fnvlist_free(spa
->spa_load_info
);
5768 spa
->spa_load_info
= loadinfo
;
5770 spa_import_progress_remove(spa_guid(spa
));
5771 return (load_error
);
5778 * The import case is identical to an open except that the configuration is sent
5779 * down from userland, instead of grabbed from the configuration cache. For the
5780 * case of an open, the pool configuration will exist in the
5781 * POOL_STATE_UNINITIALIZED state.
5783 * The stats information (gen/count/ustats) is used to gather vdev statistics at
5784 * the same time open the pool, without having to keep around the spa_t in some
5788 spa_open_common(const char *pool
, spa_t
**spapp
, const void *tag
,
5789 nvlist_t
*nvpolicy
, nvlist_t
**config
)
5792 spa_load_state_t state
= SPA_LOAD_OPEN
;
5794 int locked
= B_FALSE
;
5795 int firstopen
= B_FALSE
;
5800 * As disgusting as this is, we need to support recursive calls to this
5801 * function because dsl_dir_open() is called during spa_load(), and ends
5802 * up calling spa_open() again. The real fix is to figure out how to
5803 * avoid dsl_dir_open() calling this in the first place.
5805 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
5806 mutex_enter(&spa_namespace_lock
);
5810 if ((spa
= spa_lookup(pool
)) == NULL
) {
5812 mutex_exit(&spa_namespace_lock
);
5813 return (SET_ERROR(ENOENT
));
5816 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
5817 zpool_load_policy_t policy
;
5821 zpool_get_load_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
5823 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
5824 state
= SPA_LOAD_RECOVER
;
5826 spa_activate(spa
, spa_mode_global
);
5828 if (state
!= SPA_LOAD_RECOVER
)
5829 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
5830 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
5832 zfs_dbgmsg("spa_open_common: opening %s", pool
);
5833 error
= spa_load_best(spa
, state
, policy
.zlp_txg
,
5836 if (error
== EBADF
) {
5838 * If vdev_validate() returns failure (indicated by
5839 * EBADF), it indicates that one of the vdevs indicates
5840 * that the pool has been exported or destroyed. If
5841 * this is the case, the config cache is out of sync and
5842 * we should remove the pool from the namespace.
5845 spa_deactivate(spa
);
5846 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
5849 mutex_exit(&spa_namespace_lock
);
5850 return (SET_ERROR(ENOENT
));
5855 * We can't open the pool, but we still have useful
5856 * information: the state of each vdev after the
5857 * attempted vdev_open(). Return this to the user.
5859 if (config
!= NULL
&& spa
->spa_config
) {
5860 *config
= fnvlist_dup(spa
->spa_config
);
5861 fnvlist_add_nvlist(*config
,
5862 ZPOOL_CONFIG_LOAD_INFO
,
5863 spa
->spa_load_info
);
5866 spa_deactivate(spa
);
5867 spa
->spa_last_open_failed
= error
;
5869 mutex_exit(&spa_namespace_lock
);
5875 spa_open_ref(spa
, tag
);
5878 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5881 * If we've recovered the pool, pass back any information we
5882 * gathered while doing the load.
5884 if (state
== SPA_LOAD_RECOVER
&& config
!= NULL
) {
5885 fnvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
5886 spa
->spa_load_info
);
5890 spa
->spa_last_open_failed
= 0;
5891 spa
->spa_last_ubsync_txg
= 0;
5892 spa
->spa_load_txg
= 0;
5893 mutex_exit(&spa_namespace_lock
);
5897 zvol_create_minors_recursive(spa_name(spa
));
5905 spa_open_rewind(const char *name
, spa_t
**spapp
, const void *tag
,
5906 nvlist_t
*policy
, nvlist_t
**config
)
5908 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
5912 spa_open(const char *name
, spa_t
**spapp
, const void *tag
)
5914 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
5918 * Lookup the given spa_t, incrementing the inject count in the process,
5919 * preventing it from being exported or destroyed.
5922 spa_inject_addref(char *name
)
5926 mutex_enter(&spa_namespace_lock
);
5927 if ((spa
= spa_lookup(name
)) == NULL
) {
5928 mutex_exit(&spa_namespace_lock
);
5931 spa
->spa_inject_ref
++;
5932 mutex_exit(&spa_namespace_lock
);
5938 spa_inject_delref(spa_t
*spa
)
5940 mutex_enter(&spa_namespace_lock
);
5941 spa
->spa_inject_ref
--;
5942 mutex_exit(&spa_namespace_lock
);
5946 * Add spares device information to the nvlist.
5949 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
5959 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5961 if (spa
->spa_spares
.sav_count
== 0)
5964 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
5965 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5966 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
5968 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5969 (const nvlist_t
* const *)spares
, nspares
);
5970 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5971 &spares
, &nspares
));
5974 * Go through and find any spares which have since been
5975 * repurposed as an active spare. If this is the case, update
5976 * their status appropriately.
5978 for (i
= 0; i
< nspares
; i
++) {
5979 guid
= fnvlist_lookup_uint64(spares
[i
],
5981 VERIFY0(nvlist_lookup_uint64_array(spares
[i
],
5982 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
));
5983 if (spa_spare_exists(guid
, &pool
, NULL
) &&
5985 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
5986 vs
->vs_aux
= VDEV_AUX_SPARED
;
5989 spa
->spa_spares
.sav_vdevs
[i
]->vdev_state
;
5996 * Add l2cache device information to the nvlist, including vdev stats.
5999 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
6002 uint_t i
, j
, nl2cache
;
6009 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
6011 if (spa
->spa_l2cache
.sav_count
== 0)
6014 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6015 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
6016 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
6017 if (nl2cache
!= 0) {
6018 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6019 (const nvlist_t
* const *)l2cache
, nl2cache
);
6020 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6021 &l2cache
, &nl2cache
));
6024 * Update level 2 cache device stats.
6027 for (i
= 0; i
< nl2cache
; i
++) {
6028 guid
= fnvlist_lookup_uint64(l2cache
[i
],
6032 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
6034 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
6035 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
6041 VERIFY0(nvlist_lookup_uint64_array(l2cache
[i
],
6042 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
));
6043 vdev_get_stats(vd
, vs
);
6044 vdev_config_generate_stats(vd
, l2cache
[i
]);
6051 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
6054 zap_attribute_t
*za
= zap_attribute_alloc();
6056 if (spa
->spa_feat_for_read_obj
!= 0) {
6057 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6058 spa
->spa_feat_for_read_obj
);
6059 zap_cursor_retrieve(&zc
, za
) == 0;
6060 zap_cursor_advance(&zc
)) {
6061 ASSERT(za
->za_integer_length
== sizeof (uint64_t) &&
6062 za
->za_num_integers
== 1);
6063 VERIFY0(nvlist_add_uint64(features
, za
->za_name
,
6064 za
->za_first_integer
));
6066 zap_cursor_fini(&zc
);
6069 if (spa
->spa_feat_for_write_obj
!= 0) {
6070 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6071 spa
->spa_feat_for_write_obj
);
6072 zap_cursor_retrieve(&zc
, za
) == 0;
6073 zap_cursor_advance(&zc
)) {
6074 ASSERT(za
->za_integer_length
== sizeof (uint64_t) &&
6075 za
->za_num_integers
== 1);
6076 VERIFY0(nvlist_add_uint64(features
, za
->za_name
,
6077 za
->za_first_integer
));
6079 zap_cursor_fini(&zc
);
6081 zap_attribute_free(za
);
6085 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
6089 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6090 zfeature_info_t feature
= spa_feature_table
[i
];
6093 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
6096 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
6101 * Store a list of pool features and their reference counts in the
6104 * The first time this is called on a spa, allocate a new nvlist, fetch
6105 * the pool features and reference counts from disk, then save the list
6106 * in the spa. In subsequent calls on the same spa use the saved nvlist
6107 * and refresh its values from the cached reference counts. This
6108 * ensures we don't block here on I/O on a suspended pool so 'zpool
6109 * clear' can resume the pool.
6112 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
6116 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
6118 mutex_enter(&spa
->spa_feat_stats_lock
);
6119 features
= spa
->spa_feat_stats
;
6121 if (features
!= NULL
) {
6122 spa_feature_stats_from_cache(spa
, features
);
6124 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
6125 spa
->spa_feat_stats
= features
;
6126 spa_feature_stats_from_disk(spa
, features
);
6129 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
6132 mutex_exit(&spa
->spa_feat_stats_lock
);
6136 spa_get_stats(const char *name
, nvlist_t
**config
,
6137 char *altroot
, size_t buflen
)
6143 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
6147 * This still leaves a window of inconsistency where the spares
6148 * or l2cache devices could change and the config would be
6149 * self-inconsistent.
6151 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6153 if (*config
!= NULL
) {
6154 uint64_t loadtimes
[2];
6156 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
6157 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
6158 fnvlist_add_uint64_array(*config
,
6159 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2);
6161 fnvlist_add_uint64(*config
,
6162 ZPOOL_CONFIG_ERRCOUNT
,
6163 spa_approx_errlog_size(spa
));
6165 if (spa_suspended(spa
)) {
6166 fnvlist_add_uint64(*config
,
6167 ZPOOL_CONFIG_SUSPENDED
,
6169 fnvlist_add_uint64(*config
,
6170 ZPOOL_CONFIG_SUSPENDED_REASON
,
6171 spa
->spa_suspended
);
6174 spa_add_spares(spa
, *config
);
6175 spa_add_l2cache(spa
, *config
);
6176 spa_add_feature_stats(spa
, *config
);
6181 * We want to get the alternate root even for faulted pools, so we cheat
6182 * and call spa_lookup() directly.
6186 mutex_enter(&spa_namespace_lock
);
6187 spa
= spa_lookup(name
);
6189 spa_altroot(spa
, altroot
, buflen
);
6193 mutex_exit(&spa_namespace_lock
);
6195 spa_altroot(spa
, altroot
, buflen
);
6200 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6201 spa_close(spa
, FTAG
);
6208 * Validate that the auxiliary device array is well formed. We must have an
6209 * array of nvlists, each which describes a valid leaf vdev. If this is an
6210 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
6211 * specified, as long as they are well-formed.
6214 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
6215 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
6216 vdev_labeltype_t label
)
6223 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
6226 * It's acceptable to have no devs specified.
6228 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
6232 return (SET_ERROR(EINVAL
));
6235 * Make sure the pool is formatted with a version that supports this
6238 if (spa_version(spa
) < version
)
6239 return (SET_ERROR(ENOTSUP
));
6242 * Set the pending device list so we correctly handle device in-use
6245 sav
->sav_pending
= dev
;
6246 sav
->sav_npending
= ndev
;
6248 for (i
= 0; i
< ndev
; i
++) {
6249 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
6253 if (!vd
->vdev_ops
->vdev_op_leaf
) {
6255 error
= SET_ERROR(EINVAL
);
6261 if ((error
= vdev_open(vd
)) == 0 &&
6262 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
6263 fnvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
6270 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
6277 sav
->sav_pending
= NULL
;
6278 sav
->sav_npending
= 0;
6283 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
6287 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
6289 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
6290 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
6291 VDEV_LABEL_SPARE
)) != 0) {
6295 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
6296 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
6297 VDEV_LABEL_L2CACHE
));
6301 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
6306 if (sav
->sav_config
!= NULL
) {
6312 * Generate new dev list by concatenating with the
6315 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
6316 &olddevs
, &oldndevs
));
6318 newdevs
= kmem_alloc(sizeof (void *) *
6319 (ndevs
+ oldndevs
), KM_SLEEP
);
6320 for (i
= 0; i
< oldndevs
; i
++)
6321 newdevs
[i
] = fnvlist_dup(olddevs
[i
]);
6322 for (i
= 0; i
< ndevs
; i
++)
6323 newdevs
[i
+ oldndevs
] = fnvlist_dup(devs
[i
]);
6325 fnvlist_remove(sav
->sav_config
, config
);
6327 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
6328 (const nvlist_t
* const *)newdevs
, ndevs
+ oldndevs
);
6329 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
6330 nvlist_free(newdevs
[i
]);
6331 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
6334 * Generate a new dev list.
6336 sav
->sav_config
= fnvlist_alloc();
6337 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
6338 (const nvlist_t
* const *)devs
, ndevs
);
6343 * Stop and drop level 2 ARC devices
6346 spa_l2cache_drop(spa_t
*spa
)
6350 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
6352 for (i
= 0; i
< sav
->sav_count
; i
++) {
6355 vd
= sav
->sav_vdevs
[i
];
6358 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
6359 pool
!= 0ULL && l2arc_vdev_present(vd
))
6360 l2arc_remove_vdev(vd
);
6365 * Verify encryption parameters for spa creation. If we are encrypting, we must
6366 * have the encryption feature flag enabled.
6369 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
6370 boolean_t has_encryption
)
6372 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
6373 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
6375 return (SET_ERROR(ENOTSUP
));
6377 return (dmu_objset_create_crypt_check(NULL
, dcp
, NULL
));
6384 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
6385 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
6388 const char *altroot
= NULL
;
6393 uint64_t txg
= TXG_INITIAL
;
6394 nvlist_t
**spares
, **l2cache
;
6395 uint_t nspares
, nl2cache
;
6396 uint64_t version
, obj
, ndraid
= 0;
6397 boolean_t has_features
;
6398 boolean_t has_encryption
;
6399 boolean_t has_allocclass
;
6401 const char *feat_name
;
6402 const char *poolname
;
6405 if (props
== NULL
||
6406 nvlist_lookup_string(props
,
6407 zpool_prop_to_name(ZPOOL_PROP_TNAME
), &poolname
) != 0)
6408 poolname
= (char *)pool
;
6411 * If this pool already exists, return failure.
6413 mutex_enter(&spa_namespace_lock
);
6414 if (spa_lookup(poolname
) != NULL
) {
6415 mutex_exit(&spa_namespace_lock
);
6416 return (SET_ERROR(EEXIST
));
6420 * Allocate a new spa_t structure.
6422 nvl
= fnvlist_alloc();
6423 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
6424 (void) nvlist_lookup_string(props
,
6425 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6426 spa
= spa_add(poolname
, nvl
, altroot
);
6428 spa_activate(spa
, spa_mode_global
);
6430 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
6431 spa_deactivate(spa
);
6433 mutex_exit(&spa_namespace_lock
);
6438 * Temporary pool names should never be written to disk.
6440 if (poolname
!= pool
)
6441 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
6443 has_features
= B_FALSE
;
6444 has_encryption
= B_FALSE
;
6445 has_allocclass
= B_FALSE
;
6446 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
6447 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
6448 if (zpool_prop_feature(nvpair_name(elem
))) {
6449 has_features
= B_TRUE
;
6451 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
6452 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
6453 if (feat
== SPA_FEATURE_ENCRYPTION
)
6454 has_encryption
= B_TRUE
;
6455 if (feat
== SPA_FEATURE_ALLOCATION_CLASSES
)
6456 has_allocclass
= B_TRUE
;
6460 /* verify encryption params, if they were provided */
6462 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
6464 spa_deactivate(spa
);
6466 mutex_exit(&spa_namespace_lock
);
6470 if (!has_allocclass
&& zfs_special_devs(nvroot
, NULL
)) {
6471 spa_deactivate(spa
);
6473 mutex_exit(&spa_namespace_lock
);
6477 if (has_features
|| nvlist_lookup_uint64(props
,
6478 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
6479 version
= SPA_VERSION
;
6481 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6483 spa
->spa_first_txg
= txg
;
6484 spa
->spa_uberblock
.ub_txg
= txg
- 1;
6485 spa
->spa_uberblock
.ub_version
= version
;
6486 spa
->spa_ubsync
= spa
->spa_uberblock
;
6487 spa
->spa_load_state
= SPA_LOAD_CREATE
;
6488 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
6489 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
6490 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
6491 spa
->spa_indirect_vdevs_loaded
= B_TRUE
;
6494 * Create "The Godfather" zio to hold all async IOs
6496 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
6498 for (int i
= 0; i
< max_ncpus
; i
++) {
6499 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
6500 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
6501 ZIO_FLAG_GODFATHER
);
6505 * Create the root vdev.
6507 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6509 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
6511 ASSERT(error
!= 0 || rvd
!= NULL
);
6512 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
6514 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
6515 error
= SET_ERROR(EINVAL
);
6518 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
6519 (error
= vdev_draid_spare_create(nvroot
, rvd
, &ndraid
, 0)) == 0 &&
6520 (error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) == 0) {
6522 * instantiate the metaslab groups (this will dirty the vdevs)
6523 * we can no longer error exit past this point
6525 for (int c
= 0; error
== 0 && c
< rvd
->vdev_children
; c
++) {
6526 vdev_t
*vd
= rvd
->vdev_child
[c
];
6528 vdev_metaslab_set_size(vd
);
6529 vdev_expand(vd
, txg
);
6533 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6537 spa_deactivate(spa
);
6539 mutex_exit(&spa_namespace_lock
);
6544 * Get the list of spares, if specified.
6546 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6547 &spares
, &nspares
) == 0) {
6548 spa
->spa_spares
.sav_config
= fnvlist_alloc();
6549 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6550 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
6552 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6553 spa_load_spares(spa
);
6554 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6555 spa
->spa_spares
.sav_sync
= B_TRUE
;
6559 * Get the list of level 2 cache devices, if specified.
6561 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6562 &l2cache
, &nl2cache
) == 0) {
6563 VERIFY0(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
6564 NV_UNIQUE_NAME
, KM_SLEEP
));
6565 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6566 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
6568 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6569 spa_load_l2cache(spa
);
6570 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6571 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6574 spa
->spa_is_initializing
= B_TRUE
;
6575 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
6576 spa
->spa_is_initializing
= B_FALSE
;
6579 * Create DDTs (dedup tables).
6583 * Create BRT table and BRT table object.
6587 spa_update_dspace(spa
);
6589 tx
= dmu_tx_create_assigned(dp
, txg
);
6592 * Create the pool's history object.
6594 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
6595 spa_history_create_obj(spa
, tx
);
6597 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
6598 spa_history_log_version(spa
, "create", tx
);
6601 * Create the pool config object.
6603 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6604 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
6605 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
6607 if (zap_add(spa
->spa_meta_objset
,
6608 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
6609 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
6610 cmn_err(CE_PANIC
, "failed to add pool config");
6613 if (zap_add(spa
->spa_meta_objset
,
6614 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
6615 sizeof (uint64_t), 1, &version
, tx
) != 0) {
6616 cmn_err(CE_PANIC
, "failed to add pool version");
6619 /* Newly created pools with the right version are always deflated. */
6620 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6621 spa
->spa_deflate
= TRUE
;
6622 if (zap_add(spa
->spa_meta_objset
,
6623 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6624 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
6625 cmn_err(CE_PANIC
, "failed to add deflate");
6630 * Create the deferred-free bpobj. Turn off compression
6631 * because sync-to-convergence takes longer if the blocksize
6634 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
6635 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
6636 ZIO_COMPRESS_OFF
, tx
);
6637 if (zap_add(spa
->spa_meta_objset
,
6638 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
6639 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
6640 cmn_err(CE_PANIC
, "failed to add bpobj");
6642 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
6643 spa
->spa_meta_objset
, obj
));
6646 * Generate some random noise for salted checksums to operate on.
6648 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
6649 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
6652 * Set pool properties.
6654 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
6655 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
6656 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
6657 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
6658 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
6659 spa
->spa_autotrim
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM
);
6660 spa
->spa_dedup_table_quota
=
6661 zpool_prop_default_numeric(ZPOOL_PROP_DEDUP_TABLE_QUOTA
);
6663 if (props
!= NULL
) {
6664 spa_configfile_set(spa
, props
, B_FALSE
);
6665 spa_sync_props(props
, tx
);
6668 for (int i
= 0; i
< ndraid
; i
++)
6669 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
6673 spa
->spa_sync_on
= B_TRUE
;
6675 mmp_thread_start(spa
);
6676 txg_wait_synced(dp
, txg
);
6678 spa_spawn_aux_threads(spa
);
6680 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
6683 * Don't count references from objsets that are already closed
6684 * and are making their way through the eviction process.
6686 spa_evicting_os_wait(spa
);
6687 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
6688 spa
->spa_load_state
= SPA_LOAD_NONE
;
6692 mutex_exit(&spa_namespace_lock
);
6698 * Import a non-root pool into the system.
6701 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
6704 const char *altroot
= NULL
;
6705 spa_load_state_t state
= SPA_LOAD_IMPORT
;
6706 zpool_load_policy_t policy
;
6707 spa_mode_t mode
= spa_mode_global
;
6708 uint64_t readonly
= B_FALSE
;
6711 nvlist_t
**spares
, **l2cache
;
6712 uint_t nspares
, nl2cache
;
6715 * If a pool with this name exists, return failure.
6717 mutex_enter(&spa_namespace_lock
);
6718 if (spa_lookup(pool
) != NULL
) {
6719 mutex_exit(&spa_namespace_lock
);
6720 return (SET_ERROR(EEXIST
));
6724 * Create and initialize the spa structure.
6726 (void) nvlist_lookup_string(props
,
6727 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6728 (void) nvlist_lookup_uint64(props
,
6729 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
6731 mode
= SPA_MODE_READ
;
6732 spa
= spa_add(pool
, config
, altroot
);
6733 spa
->spa_import_flags
= flags
;
6736 * Verbatim import - Take a pool and insert it into the namespace
6737 * as if it had been loaded at boot.
6739 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
6741 spa_configfile_set(spa
, props
, B_FALSE
);
6743 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_FALSE
);
6744 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6745 zfs_dbgmsg("spa_import: verbatim import of %s", pool
);
6746 mutex_exit(&spa_namespace_lock
);
6750 spa_activate(spa
, mode
);
6753 * Don't start async tasks until we know everything is healthy.
6755 spa_async_suspend(spa
);
6757 zpool_get_load_policy(config
, &policy
);
6758 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
6759 state
= SPA_LOAD_RECOVER
;
6761 spa
->spa_config_source
= SPA_CONFIG_SRC_TRYIMPORT
;
6763 if (state
!= SPA_LOAD_RECOVER
) {
6764 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
6765 zfs_dbgmsg("spa_import: importing %s", pool
);
6767 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
6768 "(RECOVERY MODE)", pool
, (longlong_t
)policy
.zlp_txg
);
6770 error
= spa_load_best(spa
, state
, policy
.zlp_txg
, policy
.zlp_rewind
);
6773 * Propagate anything learned while loading the pool and pass it
6774 * back to caller (i.e. rewind info, missing devices, etc).
6776 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, spa
->spa_load_info
);
6778 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6780 * Toss any existing sparelist, as it doesn't have any validity
6781 * anymore, and conflicts with spa_has_spare().
6783 if (spa
->spa_spares
.sav_config
) {
6784 nvlist_free(spa
->spa_spares
.sav_config
);
6785 spa
->spa_spares
.sav_config
= NULL
;
6786 spa_load_spares(spa
);
6788 if (spa
->spa_l2cache
.sav_config
) {
6789 nvlist_free(spa
->spa_l2cache
.sav_config
);
6790 spa
->spa_l2cache
.sav_config
= NULL
;
6791 spa_load_l2cache(spa
);
6794 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6795 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6798 spa_configfile_set(spa
, props
, B_FALSE
);
6800 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
6801 (error
= spa_prop_set(spa
, props
)))) {
6803 spa_deactivate(spa
);
6805 mutex_exit(&spa_namespace_lock
);
6809 spa_async_resume(spa
);
6812 * Override any spares and level 2 cache devices as specified by
6813 * the user, as these may have correct device names/devids, etc.
6815 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6816 &spares
, &nspares
) == 0) {
6817 if (spa
->spa_spares
.sav_config
)
6818 fnvlist_remove(spa
->spa_spares
.sav_config
,
6819 ZPOOL_CONFIG_SPARES
);
6821 spa
->spa_spares
.sav_config
= fnvlist_alloc();
6822 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6823 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
6825 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6826 spa_load_spares(spa
);
6827 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6828 spa
->spa_spares
.sav_sync
= B_TRUE
;
6829 spa
->spa_spares
.sav_label_sync
= B_TRUE
;
6831 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6832 &l2cache
, &nl2cache
) == 0) {
6833 if (spa
->spa_l2cache
.sav_config
)
6834 fnvlist_remove(spa
->spa_l2cache
.sav_config
,
6835 ZPOOL_CONFIG_L2CACHE
);
6837 spa
->spa_l2cache
.sav_config
= fnvlist_alloc();
6838 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6839 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
6841 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6842 spa_load_l2cache(spa
);
6843 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6844 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6845 spa
->spa_l2cache
.sav_label_sync
= B_TRUE
;
6849 * Check for any removed devices.
6851 if (spa
->spa_autoreplace
) {
6852 spa_aux_check_removed(&spa
->spa_spares
);
6853 spa_aux_check_removed(&spa
->spa_l2cache
);
6856 if (spa_writeable(spa
)) {
6858 * Update the config cache to include the newly-imported pool.
6860 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6864 * It's possible that the pool was expanded while it was exported.
6865 * We kick off an async task to handle this for us.
6867 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
6869 spa_history_log_version(spa
, "import", NULL
);
6871 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6873 mutex_exit(&spa_namespace_lock
);
6875 zvol_create_minors_recursive(pool
);
6883 spa_tryimport(nvlist_t
*tryconfig
)
6885 nvlist_t
*config
= NULL
;
6886 const char *poolname
, *cachefile
;
6890 zpool_load_policy_t policy
;
6892 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
6895 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
6899 * Create and initialize the spa structure.
6901 char *name
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6902 (void) snprintf(name
, MAXPATHLEN
, "%s-%llx-%s",
6903 TRYIMPORT_NAME
, (u_longlong_t
)(uintptr_t)curthread
, poolname
);
6905 mutex_enter(&spa_namespace_lock
);
6906 spa
= spa_add(name
, tryconfig
, NULL
);
6907 spa_activate(spa
, SPA_MODE_READ
);
6908 kmem_free(name
, MAXPATHLEN
);
6911 * Rewind pool if a max txg was provided.
6913 zpool_get_load_policy(spa
->spa_config
, &policy
);
6914 if (policy
.zlp_txg
!= UINT64_MAX
) {
6915 spa
->spa_load_max_txg
= policy
.zlp_txg
;
6916 spa
->spa_extreme_rewind
= B_TRUE
;
6917 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
6918 poolname
, (longlong_t
)policy
.zlp_txg
);
6920 zfs_dbgmsg("spa_tryimport: importing %s", poolname
);
6923 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_CACHEFILE
, &cachefile
)
6925 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile
);
6926 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
6928 spa
->spa_config_source
= SPA_CONFIG_SRC_SCAN
;
6932 * spa_import() relies on a pool config fetched by spa_try_import()
6933 * for spare/cache devices. Import flags are not passed to
6934 * spa_tryimport(), which makes it return early due to a missing log
6935 * device and missing retrieving the cache device and spare eventually.
6936 * Passing ZFS_IMPORT_MISSING_LOG to spa_tryimport() makes it fetch
6937 * the correct configuration regardless of the missing log device.
6939 spa
->spa_import_flags
|= ZFS_IMPORT_MISSING_LOG
;
6941 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
);
6944 * If 'tryconfig' was at least parsable, return the current config.
6946 if (spa
->spa_root_vdev
!= NULL
) {
6947 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
6948 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, poolname
);
6949 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, state
);
6950 fnvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
6951 spa
->spa_uberblock
.ub_timestamp
);
6952 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
6953 spa
->spa_load_info
);
6954 fnvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
6958 * If the bootfs property exists on this pool then we
6959 * copy it out so that external consumers can tell which
6960 * pools are bootable.
6962 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
6963 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6966 * We have to play games with the name since the
6967 * pool was opened as TRYIMPORT_NAME.
6969 if (dsl_dsobj_to_dsname(spa_name(spa
),
6970 spa
->spa_bootfs
, tmpname
) == 0) {
6974 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6976 cp
= strchr(tmpname
, '/');
6978 (void) strlcpy(dsname
, tmpname
,
6981 (void) snprintf(dsname
, MAXPATHLEN
,
6982 "%s/%s", poolname
, ++cp
);
6984 fnvlist_add_string(config
, ZPOOL_CONFIG_BOOTFS
,
6986 kmem_free(dsname
, MAXPATHLEN
);
6988 kmem_free(tmpname
, MAXPATHLEN
);
6992 * Add the list of hot spares and level 2 cache devices.
6994 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6995 spa_add_spares(spa
, config
);
6996 spa_add_l2cache(spa
, config
);
6997 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
7001 spa_deactivate(spa
);
7003 mutex_exit(&spa_namespace_lock
);
7009 * Pool export/destroy
7011 * The act of destroying or exporting a pool is very simple. We make sure there
7012 * is no more pending I/O and any references to the pool are gone. Then, we
7013 * update the pool state and sync all the labels to disk, removing the
7014 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
7015 * we don't sync the labels or remove the configuration cache.
7018 spa_export_common(const char *pool
, int new_state
, nvlist_t
**oldconfig
,
7019 boolean_t force
, boolean_t hardforce
)
7023 hrtime_t export_start
= gethrtime();
7028 if (!(spa_mode_global
& SPA_MODE_WRITE
))
7029 return (SET_ERROR(EROFS
));
7031 mutex_enter(&spa_namespace_lock
);
7032 if ((spa
= spa_lookup(pool
)) == NULL
) {
7033 mutex_exit(&spa_namespace_lock
);
7034 return (SET_ERROR(ENOENT
));
7037 if (spa
->spa_is_exporting
) {
7038 /* the pool is being exported by another thread */
7039 mutex_exit(&spa_namespace_lock
);
7040 return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS
));
7042 spa
->spa_is_exporting
= B_TRUE
;
7045 * Put a hold on the pool, drop the namespace lock, stop async tasks
7046 * and see if we can export.
7048 spa_open_ref(spa
, FTAG
);
7049 mutex_exit(&spa_namespace_lock
);
7050 spa_async_suspend(spa
);
7051 if (spa
->spa_zvol_taskq
) {
7052 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
7053 taskq_wait(spa
->spa_zvol_taskq
);
7055 mutex_enter(&spa_namespace_lock
);
7056 spa
->spa_export_thread
= curthread
;
7057 spa_close(spa
, FTAG
);
7059 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
7060 mutex_exit(&spa_namespace_lock
);
7065 * The pool will be in core if it's openable, in which case we can
7066 * modify its state. Objsets may be open only because they're dirty,
7067 * so we have to force it to sync before checking spa_refcnt.
7069 if (spa
->spa_sync_on
) {
7070 txg_wait_synced(spa
->spa_dsl_pool
, 0);
7071 spa_evicting_os_wait(spa
);
7075 * A pool cannot be exported or destroyed if there are active
7076 * references. If we are resetting a pool, allow references by
7077 * fault injection handlers.
7079 if (!spa_refcount_zero(spa
) || (spa
->spa_inject_ref
!= 0)) {
7080 error
= SET_ERROR(EBUSY
);
7084 mutex_exit(&spa_namespace_lock
);
7086 * At this point we no longer hold the spa_namespace_lock and
7087 * there were no references on the spa. Future spa_lookups will
7088 * notice the spa->spa_export_thread and wait until we signal
7089 * that we are finshed.
7092 if (spa
->spa_sync_on
) {
7093 vdev_t
*rvd
= spa
->spa_root_vdev
;
7095 * A pool cannot be exported if it has an active shared spare.
7096 * This is to prevent other pools stealing the active spare
7097 * from an exported pool. At user's own will, such pool can
7098 * be forcedly exported.
7100 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
7101 spa_has_active_shared_spare(spa
)) {
7102 error
= SET_ERROR(EXDEV
);
7103 mutex_enter(&spa_namespace_lock
);
7108 * We're about to export or destroy this pool. Make sure
7109 * we stop all initialization and trim activity here before
7110 * we set the spa_final_txg. This will ensure that all
7111 * dirty data resulting from the initialization is
7112 * committed to disk before we unload the pool.
7114 vdev_initialize_stop_all(rvd
, VDEV_INITIALIZE_ACTIVE
);
7115 vdev_trim_stop_all(rvd
, VDEV_TRIM_ACTIVE
);
7116 vdev_autotrim_stop_all(spa
);
7117 vdev_rebuild_stop_all(spa
);
7120 * We want this to be reflected on every label,
7121 * so mark them all dirty. spa_unload() will do the
7122 * final sync that pushes these changes out.
7124 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
7125 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
7126 spa
->spa_state
= new_state
;
7127 vdev_config_dirty(rvd
);
7128 spa_config_exit(spa
, SCL_ALL
, FTAG
);
7132 * If the log space map feature is enabled and the pool is
7133 * getting exported (but not destroyed), we want to spend some
7134 * time flushing as many metaslabs as we can in an attempt to
7135 * destroy log space maps and save import time. This has to be
7136 * done before we set the spa_final_txg, otherwise
7137 * spa_sync() -> spa_flush_metaslabs() may dirty the final TXGs.
7138 * spa_should_flush_logs_on_unload() should be called after
7139 * spa_state has been set to the new_state.
7141 if (spa_should_flush_logs_on_unload(spa
))
7142 spa_unload_log_sm_flush_all(spa
);
7144 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
7145 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
7146 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
7148 spa_config_exit(spa
, SCL_ALL
, FTAG
);
7155 if (new_state
== POOL_STATE_DESTROYED
)
7156 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
7157 else if (new_state
== POOL_STATE_EXPORTED
)
7158 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
7160 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
7162 spa_deactivate(spa
);
7165 if (oldconfig
&& spa
->spa_config
)
7166 *oldconfig
= fnvlist_dup(spa
->spa_config
);
7168 if (new_state
== POOL_STATE_EXPORTED
)
7169 zio_handle_export_delay(spa
, gethrtime() - export_start
);
7172 * Take the namespace lock for the actual spa_t removal
7174 mutex_enter(&spa_namespace_lock
);
7175 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
7177 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
7181 * If spa_remove() is not called for this spa_t and
7182 * there is any possibility that it can be reused,
7183 * we make sure to reset the exporting flag.
7185 spa
->spa_is_exporting
= B_FALSE
;
7186 spa
->spa_export_thread
= NULL
;
7190 * Wake up any waiters in spa_lookup()
7192 cv_broadcast(&spa_namespace_cv
);
7193 mutex_exit(&spa_namespace_lock
);
7197 spa
->spa_is_exporting
= B_FALSE
;
7198 spa
->spa_export_thread
= NULL
;
7200 spa_async_resume(spa
);
7202 * Wake up any waiters in spa_lookup()
7204 cv_broadcast(&spa_namespace_cv
);
7205 mutex_exit(&spa_namespace_lock
);
7210 * Destroy a storage pool.
7213 spa_destroy(const char *pool
)
7215 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
7220 * Export a storage pool.
7223 spa_export(const char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
7224 boolean_t hardforce
)
7226 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
7231 * Similar to spa_export(), this unloads the spa_t without actually removing it
7232 * from the namespace in any way.
7235 spa_reset(const char *pool
)
7237 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
7242 * ==========================================================================
7243 * Device manipulation
7244 * ==========================================================================
7248 * This is called as a synctask to increment the draid feature flag
7251 spa_draid_feature_incr(void *arg
, dmu_tx_t
*tx
)
7253 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
7254 int draid
= (int)(uintptr_t)arg
;
7256 for (int c
= 0; c
< draid
; c
++)
7257 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
7261 * Add a device to a storage pool.
7264 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
, boolean_t check_ashift
)
7266 uint64_t txg
, ndraid
= 0;
7268 vdev_t
*rvd
= spa
->spa_root_vdev
;
7270 nvlist_t
**spares
, **l2cache
;
7271 uint_t nspares
, nl2cache
;
7273 ASSERT(spa_writeable(spa
));
7275 txg
= spa_vdev_enter(spa
);
7277 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
7278 VDEV_ALLOC_ADD
)) != 0)
7279 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7281 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
7283 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
7287 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
7291 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
7292 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
7294 if (vd
->vdev_children
!= 0 &&
7295 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0) {
7296 return (spa_vdev_exit(spa
, vd
, txg
, error
));
7300 * The virtual dRAID spares must be added after vdev tree is created
7301 * and the vdev guids are generated. The guid of their associated
7302 * dRAID is stored in the config and used when opening the spare.
7304 if ((error
= vdev_draid_spare_create(nvroot
, vd
, &ndraid
,
7305 rvd
->vdev_children
)) == 0) {
7306 if (ndraid
> 0 && nvlist_lookup_nvlist_array(nvroot
,
7307 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) != 0)
7310 return (spa_vdev_exit(spa
, vd
, txg
, error
));
7314 * We must validate the spares and l2cache devices after checking the
7315 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
7317 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
7318 return (spa_vdev_exit(spa
, vd
, txg
, error
));
7321 * If we are in the middle of a device removal, we can only add
7322 * devices which match the existing devices in the pool.
7323 * If we are in the middle of a removal, or have some indirect
7324 * vdevs, we can not add raidz or dRAID top levels.
7326 if (spa
->spa_vdev_removal
!= NULL
||
7327 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
7328 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7329 tvd
= vd
->vdev_child
[c
];
7330 if (spa
->spa_vdev_removal
!= NULL
&&
7331 tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
7332 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
7334 /* Fail if top level vdev is raidz or a dRAID */
7335 if (vdev_get_nparity(tvd
) != 0)
7336 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
7339 * Need the top level mirror to be
7340 * a mirror of leaf vdevs only
7342 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
7343 for (uint64_t cid
= 0;
7344 cid
< tvd
->vdev_children
; cid
++) {
7345 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
7346 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
7347 return (spa_vdev_exit(spa
, vd
,
7355 if (check_ashift
&& spa
->spa_max_ashift
== spa
->spa_min_ashift
) {
7356 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7357 tvd
= vd
->vdev_child
[c
];
7358 if (tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
7359 return (spa_vdev_exit(spa
, vd
, txg
,
7360 ZFS_ERR_ASHIFT_MISMATCH
));
7365 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7366 tvd
= vd
->vdev_child
[c
];
7367 vdev_remove_child(vd
, tvd
);
7368 tvd
->vdev_id
= rvd
->vdev_children
;
7369 vdev_add_child(rvd
, tvd
);
7370 vdev_config_dirty(tvd
);
7374 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
7375 ZPOOL_CONFIG_SPARES
);
7376 spa_load_spares(spa
);
7377 spa
->spa_spares
.sav_sync
= B_TRUE
;
7380 if (nl2cache
!= 0) {
7381 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
7382 ZPOOL_CONFIG_L2CACHE
);
7383 spa_load_l2cache(spa
);
7384 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
7388 * We can't increment a feature while holding spa_vdev so we
7389 * have to do it in a synctask.
7394 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
7395 dsl_sync_task_nowait(spa
->spa_dsl_pool
, spa_draid_feature_incr
,
7396 (void *)(uintptr_t)ndraid
, tx
);
7401 * We have to be careful when adding new vdevs to an existing pool.
7402 * If other threads start allocating from these vdevs before we
7403 * sync the config cache, and we lose power, then upon reboot we may
7404 * fail to open the pool because there are DVAs that the config cache
7405 * can't translate. Therefore, we first add the vdevs without
7406 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
7407 * and then let spa_config_update() initialize the new metaslabs.
7409 * spa_load() checks for added-but-not-initialized vdevs, so that
7410 * if we lose power at any point in this sequence, the remaining
7411 * steps will be completed the next time we load the pool.
7413 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
7415 mutex_enter(&spa_namespace_lock
);
7416 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
7417 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
7418 mutex_exit(&spa_namespace_lock
);
7424 * Attach a device to a vdev specified by its guid. The vdev type can be
7425 * a mirror, a raidz, or a leaf device that is also a top-level (e.g. a
7426 * single device). When the vdev is a single device, a mirror vdev will be
7427 * automatically inserted.
7429 * If 'replacing' is specified, the new device is intended to replace the
7430 * existing device; in this case the two devices are made into their own
7431 * mirror using the 'replacing' vdev, which is functionally identical to
7432 * the mirror vdev (it actually reuses all the same ops) but has a few
7433 * extra rules: you can't attach to it after it's been created, and upon
7434 * completion of resilvering, the first disk (the one being replaced)
7435 * is automatically detached.
7437 * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild)
7438 * should be performed instead of traditional healing reconstruction. From
7439 * an administrators perspective these are both resilver operations.
7442 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
,
7445 uint64_t txg
, dtl_max_txg
;
7446 vdev_t
*rvd
= spa
->spa_root_vdev
;
7447 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
7449 char *oldvdpath
, *newvdpath
;
7450 int newvd_isspare
= B_FALSE
;
7453 ASSERT(spa_writeable(spa
));
7455 txg
= spa_vdev_enter(spa
);
7457 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7459 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7460 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7461 error
= (spa_has_checkpoint(spa
)) ?
7462 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7463 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7467 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_DEVICE_REBUILD
))
7468 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7470 if (dsl_scan_resilvering(spa_get_dsl(spa
)) ||
7471 dsl_scan_resilver_scheduled(spa_get_dsl(spa
))) {
7472 return (spa_vdev_exit(spa
, NULL
, txg
,
7473 ZFS_ERR_RESILVER_IN_PROGRESS
));
7476 if (vdev_rebuild_active(rvd
))
7477 return (spa_vdev_exit(spa
, NULL
, txg
,
7478 ZFS_ERR_REBUILD_IN_PROGRESS
));
7481 if (spa
->spa_vdev_removal
!= NULL
) {
7482 return (spa_vdev_exit(spa
, NULL
, txg
,
7483 ZFS_ERR_DEVRM_IN_PROGRESS
));
7487 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
7489 boolean_t raidz
= oldvd
->vdev_ops
== &vdev_raidz_ops
;
7492 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_RAIDZ_EXPANSION
))
7493 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7496 * Can't expand a raidz while prior expand is in progress.
7498 if (spa
->spa_raidz_expand
!= NULL
) {
7499 return (spa_vdev_exit(spa
, NULL
, txg
,
7500 ZFS_ERR_RAIDZ_EXPAND_IN_PROGRESS
));
7502 } else if (!oldvd
->vdev_ops
->vdev_op_leaf
) {
7503 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7509 pvd
= oldvd
->vdev_parent
;
7511 if (spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
7512 VDEV_ALLOC_ATTACH
) != 0)
7513 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7515 if (newrootvd
->vdev_children
!= 1)
7516 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
7518 newvd
= newrootvd
->vdev_child
[0];
7520 if (!newvd
->vdev_ops
->vdev_op_leaf
)
7521 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
7523 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
7524 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
7527 * log, dedup and special vdevs should not be replaced by spares.
7529 if ((oldvd
->vdev_top
->vdev_alloc_bias
!= VDEV_BIAS_NONE
||
7530 oldvd
->vdev_top
->vdev_islog
) && newvd
->vdev_isspare
) {
7531 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7535 * A dRAID spare can only replace a child of its parent dRAID vdev.
7537 if (newvd
->vdev_ops
== &vdev_draid_spare_ops
&&
7538 oldvd
->vdev_top
!= vdev_draid_spare_get_parent(newvd
)) {
7539 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7544 * For rebuilds, the top vdev must support reconstruction
7545 * using only space maps. This means the only allowable
7546 * vdevs types are the root vdev, a mirror, or dRAID.
7549 if (pvd
->vdev_top
!= NULL
)
7550 tvd
= pvd
->vdev_top
;
7552 if (tvd
->vdev_ops
!= &vdev_mirror_ops
&&
7553 tvd
->vdev_ops
!= &vdev_root_ops
&&
7554 tvd
->vdev_ops
!= &vdev_draid_ops
) {
7555 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7561 * For attach, the only allowable parent is a mirror or
7562 * the root vdev. A raidz vdev can be attached to, but
7563 * you cannot attach to a raidz child.
7565 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
7566 pvd
->vdev_ops
!= &vdev_root_ops
&&
7568 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7570 pvops
= &vdev_mirror_ops
;
7573 * Active hot spares can only be replaced by inactive hot
7576 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
7577 oldvd
->vdev_isspare
&&
7578 !spa_has_spare(spa
, newvd
->vdev_guid
))
7579 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7582 * If the source is a hot spare, and the parent isn't already a
7583 * spare, then we want to create a new hot spare. Otherwise, we
7584 * want to create a replacing vdev. The user is not allowed to
7585 * attach to a spared vdev child unless the 'isspare' state is
7586 * the same (spare replaces spare, non-spare replaces
7589 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
7590 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
7591 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7592 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
7593 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
7594 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7597 if (newvd
->vdev_isspare
)
7598 pvops
= &vdev_spare_ops
;
7600 pvops
= &vdev_replacing_ops
;
7604 * Make sure the new device is big enough.
7606 vdev_t
*min_vdev
= raidz
? oldvd
->vdev_child
[0] : oldvd
;
7607 if (newvd
->vdev_asize
< vdev_get_min_asize(min_vdev
))
7608 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
7611 * The new device cannot have a higher alignment requirement
7612 * than the top-level vdev.
7614 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
) {
7615 return (spa_vdev_exit(spa
, newrootvd
, txg
,
7616 ZFS_ERR_ASHIFT_MISMATCH
));
7620 * RAIDZ-expansion-specific checks.
7623 if (vdev_raidz_attach_check(newvd
) != 0)
7624 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7627 * Fail early if a child is not healthy or being replaced
7629 for (int i
= 0; i
< oldvd
->vdev_children
; i
++) {
7630 if (vdev_is_dead(oldvd
->vdev_child
[i
]) ||
7631 !oldvd
->vdev_child
[i
]->vdev_ops
->vdev_op_leaf
) {
7632 return (spa_vdev_exit(spa
, newrootvd
, txg
,
7635 /* Also fail if reserved boot area is in-use */
7636 if (vdev_check_boot_reserve(spa
, oldvd
->vdev_child
[i
])
7638 return (spa_vdev_exit(spa
, newrootvd
, txg
,
7646 * Note: oldvdpath is freed by spa_strfree(), but
7647 * kmem_asprintf() is freed by kmem_strfree(), so we have to
7648 * move it to a spa_strdup-ed string.
7650 char *tmp
= kmem_asprintf("raidz%u-%u",
7651 (uint_t
)vdev_get_nparity(oldvd
), (uint_t
)oldvd
->vdev_id
);
7652 oldvdpath
= spa_strdup(tmp
);
7655 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
7657 newvdpath
= spa_strdup(newvd
->vdev_path
);
7660 * If this is an in-place replacement, update oldvd's path and devid
7661 * to make it distinguishable from newvd, and unopenable from now on.
7663 if (strcmp(oldvdpath
, newvdpath
) == 0) {
7664 spa_strfree(oldvd
->vdev_path
);
7665 oldvd
->vdev_path
= kmem_alloc(strlen(newvdpath
) + 5,
7667 (void) sprintf(oldvd
->vdev_path
, "%s/old",
7669 if (oldvd
->vdev_devid
!= NULL
) {
7670 spa_strfree(oldvd
->vdev_devid
);
7671 oldvd
->vdev_devid
= NULL
;
7673 spa_strfree(oldvdpath
);
7674 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
7678 * If the parent is not a mirror, or if we're replacing, insert the new
7679 * mirror/replacing/spare vdev above oldvd.
7681 if (!raidz
&& pvd
->vdev_ops
!= pvops
) {
7682 pvd
= vdev_add_parent(oldvd
, pvops
);
7683 ASSERT(pvd
->vdev_ops
== pvops
);
7684 ASSERT(oldvd
->vdev_parent
== pvd
);
7687 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
7690 * Extract the new device from its root and add it to pvd.
7692 vdev_remove_child(newrootvd
, newvd
);
7693 newvd
->vdev_id
= pvd
->vdev_children
;
7694 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
7695 vdev_add_child(pvd
, newvd
);
7698 * Reevaluate the parent vdev state.
7700 vdev_propagate_state(pvd
);
7702 tvd
= newvd
->vdev_top
;
7703 ASSERT(pvd
->vdev_top
== tvd
);
7704 ASSERT(tvd
->vdev_parent
== rvd
);
7706 vdev_config_dirty(tvd
);
7709 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
7710 * for any dmu_sync-ed blocks. It will propagate upward when
7711 * spa_vdev_exit() calls vdev_dtl_reassess().
7713 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
7717 * Wait for the youngest allocations and frees to sync,
7718 * and then wait for the deferral of those frees to finish.
7720 spa_vdev_config_exit(spa
, NULL
,
7721 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
7723 vdev_initialize_stop_all(tvd
, VDEV_INITIALIZE_ACTIVE
);
7724 vdev_trim_stop_all(tvd
, VDEV_TRIM_ACTIVE
);
7725 vdev_autotrim_stop_wait(tvd
);
7727 dtl_max_txg
= spa_vdev_config_enter(spa
);
7729 tvd
->vdev_rz_expanding
= B_TRUE
;
7731 vdev_dirty_leaves(tvd
, VDD_DTL
, dtl_max_txg
);
7732 vdev_config_dirty(tvd
);
7734 dmu_tx_t
*tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
,
7736 dsl_sync_task_nowait(spa
->spa_dsl_pool
, vdev_raidz_attach_sync
,
7740 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
7741 dtl_max_txg
- TXG_INITIAL
);
7743 if (newvd
->vdev_isspare
) {
7744 spa_spare_activate(newvd
);
7745 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
7748 newvd_isspare
= newvd
->vdev_isspare
;
7751 * Mark newvd's DTL dirty in this txg.
7753 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
7756 * Schedule the resilver or rebuild to restart in the future.
7757 * We do this to ensure that dmu_sync-ed blocks have been
7758 * stitched into the respective datasets.
7761 newvd
->vdev_rebuild_txg
= txg
;
7765 newvd
->vdev_resilver_txg
= txg
;
7767 if (dsl_scan_resilvering(spa_get_dsl(spa
)) &&
7768 spa_feature_is_enabled(spa
,
7769 SPA_FEATURE_RESILVER_DEFER
)) {
7770 vdev_defer_resilver(newvd
);
7772 dsl_scan_restart_resilver(spa
->spa_dsl_pool
,
7778 if (spa
->spa_bootfs
)
7779 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
7781 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
7786 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
7788 spa_history_log_internal(spa
, "vdev attach", NULL
,
7789 "%s vdev=%s %s vdev=%s",
7790 replacing
&& newvd_isspare
? "spare in" :
7791 replacing
? "replace" : "attach", newvdpath
,
7792 replacing
? "for" : "to", oldvdpath
);
7794 spa_strfree(oldvdpath
);
7795 spa_strfree(newvdpath
);
7801 * Detach a device from a mirror or replacing vdev.
7803 * If 'replace_done' is specified, only detach if the parent
7804 * is a replacing or a spare vdev.
7807 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
7811 vdev_t
*rvd __maybe_unused
= spa
->spa_root_vdev
;
7812 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
7813 boolean_t unspare
= B_FALSE
;
7814 uint64_t unspare_guid
= 0;
7817 ASSERT(spa_writeable(spa
));
7819 txg
= spa_vdev_detach_enter(spa
, guid
);
7821 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7824 * Besides being called directly from the userland through the
7825 * ioctl interface, spa_vdev_detach() can be potentially called
7826 * at the end of spa_vdev_resilver_done().
7828 * In the regular case, when we have a checkpoint this shouldn't
7829 * happen as we never empty the DTLs of a vdev during the scrub
7830 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
7831 * should never get here when we have a checkpoint.
7833 * That said, even in a case when we checkpoint the pool exactly
7834 * as spa_vdev_resilver_done() calls this function everything
7835 * should be fine as the resilver will return right away.
7837 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7838 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7839 error
= (spa_has_checkpoint(spa
)) ?
7840 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7841 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7845 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
7847 if (!vd
->vdev_ops
->vdev_op_leaf
)
7848 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7850 pvd
= vd
->vdev_parent
;
7853 * If the parent/child relationship is not as expected, don't do it.
7854 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
7855 * vdev that's replacing B with C. The user's intent in replacing
7856 * is to go from M(A,B) to M(A,C). If the user decides to cancel
7857 * the replace by detaching C, the expected behavior is to end up
7858 * M(A,B). But suppose that right after deciding to detach C,
7859 * the replacement of B completes. We would have M(A,C), and then
7860 * ask to detach C, which would leave us with just A -- not what
7861 * the user wanted. To prevent this, we make sure that the
7862 * parent/child relationship hasn't changed -- in this example,
7863 * that C's parent is still the replacing vdev R.
7865 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
7866 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7869 * Only 'replacing' or 'spare' vdevs can be replaced.
7871 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7872 pvd
->vdev_ops
!= &vdev_spare_ops
)
7873 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7875 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
7876 spa_version(spa
) >= SPA_VERSION_SPARES
);
7879 * Only mirror, replacing, and spare vdevs support detach.
7881 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7882 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
7883 pvd
->vdev_ops
!= &vdev_spare_ops
)
7884 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7887 * If this device has the only valid copy of some data,
7888 * we cannot safely detach it.
7890 if (vdev_dtl_required(vd
))
7891 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7893 ASSERT(pvd
->vdev_children
>= 2);
7896 * If we are detaching the second disk from a replacing vdev, then
7897 * check to see if we changed the original vdev's path to have "/old"
7898 * at the end in spa_vdev_attach(). If so, undo that change now.
7900 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
7901 vd
->vdev_path
!= NULL
) {
7902 size_t len
= strlen(vd
->vdev_path
);
7904 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
7905 cvd
= pvd
->vdev_child
[c
];
7907 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
7910 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
7911 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
7912 spa_strfree(cvd
->vdev_path
);
7913 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
7920 * If we are detaching the original disk from a normal spare, then it
7921 * implies that the spare should become a real disk, and be removed
7922 * from the active spare list for the pool. dRAID spares on the
7923 * other hand are coupled to the pool and thus should never be removed
7924 * from the spares list.
7926 if (pvd
->vdev_ops
== &vdev_spare_ops
&& vd
->vdev_id
== 0) {
7927 vdev_t
*last_cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7929 if (last_cvd
->vdev_isspare
&&
7930 last_cvd
->vdev_ops
!= &vdev_draid_spare_ops
) {
7936 * Erase the disk labels so the disk can be used for other things.
7937 * This must be done after all other error cases are handled,
7938 * but before we disembowel vd (so we can still do I/O to it).
7939 * But if we can't do it, don't treat the error as fatal --
7940 * it may be that the unwritability of the disk is the reason
7941 * it's being detached!
7943 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
7946 * Remove vd from its parent and compact the parent's children.
7948 vdev_remove_child(pvd
, vd
);
7949 vdev_compact_children(pvd
);
7952 * Remember one of the remaining children so we can get tvd below.
7954 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7957 * If we need to remove the remaining child from the list of hot spares,
7958 * do it now, marking the vdev as no longer a spare in the process.
7959 * We must do this before vdev_remove_parent(), because that can
7960 * change the GUID if it creates a new toplevel GUID. For a similar
7961 * reason, we must remove the spare now, in the same txg as the detach;
7962 * otherwise someone could attach a new sibling, change the GUID, and
7963 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
7966 ASSERT(cvd
->vdev_isspare
);
7967 spa_spare_remove(cvd
);
7968 unspare_guid
= cvd
->vdev_guid
;
7969 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
7970 cvd
->vdev_unspare
= B_TRUE
;
7974 * If the parent mirror/replacing vdev only has one child,
7975 * the parent is no longer needed. Remove it from the tree.
7977 if (pvd
->vdev_children
== 1) {
7978 if (pvd
->vdev_ops
== &vdev_spare_ops
)
7979 cvd
->vdev_unspare
= B_FALSE
;
7980 vdev_remove_parent(cvd
);
7984 * We don't set tvd until now because the parent we just removed
7985 * may have been the previous top-level vdev.
7987 tvd
= cvd
->vdev_top
;
7988 ASSERT(tvd
->vdev_parent
== rvd
);
7991 * Reevaluate the parent vdev state.
7993 vdev_propagate_state(cvd
);
7996 * If the 'autoexpand' property is set on the pool then automatically
7997 * try to expand the size of the pool. For example if the device we
7998 * just detached was smaller than the others, it may be possible to
7999 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
8000 * first so that we can obtain the updated sizes of the leaf vdevs.
8002 if (spa
->spa_autoexpand
) {
8004 vdev_expand(tvd
, txg
);
8007 vdev_config_dirty(tvd
);
8010 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
8011 * vd->vdev_detached is set and free vd's DTL object in syncing context.
8012 * But first make sure we're not on any *other* txg's DTL list, to
8013 * prevent vd from being accessed after it's freed.
8015 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
8016 for (int t
= 0; t
< TXG_SIZE
; t
++)
8017 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
8018 vd
->vdev_detached
= B_TRUE
;
8019 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
8021 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
8022 spa_notify_waiters(spa
);
8024 /* hang on to the spa before we release the lock */
8025 spa_open_ref(spa
, FTAG
);
8027 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
8029 spa_history_log_internal(spa
, "detach", NULL
,
8031 spa_strfree(vdpath
);
8034 * If this was the removal of the original device in a hot spare vdev,
8035 * then we want to go through and remove the device from the hot spare
8036 * list of every other pool.
8039 spa_t
*altspa
= NULL
;
8041 mutex_enter(&spa_namespace_lock
);
8042 while ((altspa
= spa_next(altspa
)) != NULL
) {
8043 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
8047 spa_open_ref(altspa
, FTAG
);
8048 mutex_exit(&spa_namespace_lock
);
8049 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
8050 mutex_enter(&spa_namespace_lock
);
8051 spa_close(altspa
, FTAG
);
8053 mutex_exit(&spa_namespace_lock
);
8055 /* search the rest of the vdevs for spares to remove */
8056 spa_vdev_resilver_done(spa
);
8059 /* all done with the spa; OK to release */
8060 mutex_enter(&spa_namespace_lock
);
8061 spa_close(spa
, FTAG
);
8062 mutex_exit(&spa_namespace_lock
);
8068 spa_vdev_initialize_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
8071 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
8073 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
8075 /* Look up vdev and ensure it's a leaf. */
8076 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
8077 if (vd
== NULL
|| vd
->vdev_detached
) {
8078 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8079 return (SET_ERROR(ENODEV
));
8080 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
8081 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8082 return (SET_ERROR(EINVAL
));
8083 } else if (!vdev_writeable(vd
)) {
8084 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8085 return (SET_ERROR(EROFS
));
8087 mutex_enter(&vd
->vdev_initialize_lock
);
8088 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8091 * When we activate an initialize action we check to see
8092 * if the vdev_initialize_thread is NULL. We do this instead
8093 * of using the vdev_initialize_state since there might be
8094 * a previous initialization process which has completed but
8095 * the thread is not exited.
8097 if (cmd_type
== POOL_INITIALIZE_START
&&
8098 (vd
->vdev_initialize_thread
!= NULL
||
8099 vd
->vdev_top
->vdev_removing
|| vd
->vdev_top
->vdev_rz_expanding
)) {
8100 mutex_exit(&vd
->vdev_initialize_lock
);
8101 return (SET_ERROR(EBUSY
));
8102 } else if (cmd_type
== POOL_INITIALIZE_CANCEL
&&
8103 (vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
&&
8104 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_SUSPENDED
)) {
8105 mutex_exit(&vd
->vdev_initialize_lock
);
8106 return (SET_ERROR(ESRCH
));
8107 } else if (cmd_type
== POOL_INITIALIZE_SUSPEND
&&
8108 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
) {
8109 mutex_exit(&vd
->vdev_initialize_lock
);
8110 return (SET_ERROR(ESRCH
));
8111 } else if (cmd_type
== POOL_INITIALIZE_UNINIT
&&
8112 vd
->vdev_initialize_thread
!= NULL
) {
8113 mutex_exit(&vd
->vdev_initialize_lock
);
8114 return (SET_ERROR(EBUSY
));
8118 case POOL_INITIALIZE_START
:
8119 vdev_initialize(vd
);
8121 case POOL_INITIALIZE_CANCEL
:
8122 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
, vd_list
);
8124 case POOL_INITIALIZE_SUSPEND
:
8125 vdev_initialize_stop(vd
, VDEV_INITIALIZE_SUSPENDED
, vd_list
);
8127 case POOL_INITIALIZE_UNINIT
:
8128 vdev_uninitialize(vd
);
8131 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
8133 mutex_exit(&vd
->vdev_initialize_lock
);
8139 spa_vdev_initialize(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
,
8140 nvlist_t
*vdev_errlist
)
8142 int total_errors
= 0;
8145 list_create(&vd_list
, sizeof (vdev_t
),
8146 offsetof(vdev_t
, vdev_initialize_node
));
8149 * We hold the namespace lock through the whole function
8150 * to prevent any changes to the pool while we're starting or
8151 * stopping initialization. The config and state locks are held so that
8152 * we can properly assess the vdev state before we commit to
8153 * the initializing operation.
8155 mutex_enter(&spa_namespace_lock
);
8157 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
8158 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
8159 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
8161 int error
= spa_vdev_initialize_impl(spa
, vdev_guid
, cmd_type
,
8164 char guid_as_str
[MAXNAMELEN
];
8166 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
8167 "%llu", (unsigned long long)vdev_guid
);
8168 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
8173 /* Wait for all initialize threads to stop. */
8174 vdev_initialize_stop_wait(spa
, &vd_list
);
8176 /* Sync out the initializing state */
8177 txg_wait_synced(spa
->spa_dsl_pool
, 0);
8178 mutex_exit(&spa_namespace_lock
);
8180 list_destroy(&vd_list
);
8182 return (total_errors
);
8186 spa_vdev_trim_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
8187 uint64_t rate
, boolean_t partial
, boolean_t secure
, list_t
*vd_list
)
8189 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
8191 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
8193 /* Look up vdev and ensure it's a leaf. */
8194 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
8195 if (vd
== NULL
|| vd
->vdev_detached
) {
8196 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8197 return (SET_ERROR(ENODEV
));
8198 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
8199 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8200 return (SET_ERROR(EINVAL
));
8201 } else if (!vdev_writeable(vd
)) {
8202 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8203 return (SET_ERROR(EROFS
));
8204 } else if (!vd
->vdev_has_trim
) {
8205 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8206 return (SET_ERROR(EOPNOTSUPP
));
8207 } else if (secure
&& !vd
->vdev_has_securetrim
) {
8208 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8209 return (SET_ERROR(EOPNOTSUPP
));
8211 mutex_enter(&vd
->vdev_trim_lock
);
8212 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8215 * When we activate a TRIM action we check to see if the
8216 * vdev_trim_thread is NULL. We do this instead of using the
8217 * vdev_trim_state since there might be a previous TRIM process
8218 * which has completed but the thread is not exited.
8220 if (cmd_type
== POOL_TRIM_START
&&
8221 (vd
->vdev_trim_thread
!= NULL
|| vd
->vdev_top
->vdev_removing
||
8222 vd
->vdev_top
->vdev_rz_expanding
)) {
8223 mutex_exit(&vd
->vdev_trim_lock
);
8224 return (SET_ERROR(EBUSY
));
8225 } else if (cmd_type
== POOL_TRIM_CANCEL
&&
8226 (vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
&&
8227 vd
->vdev_trim_state
!= VDEV_TRIM_SUSPENDED
)) {
8228 mutex_exit(&vd
->vdev_trim_lock
);
8229 return (SET_ERROR(ESRCH
));
8230 } else if (cmd_type
== POOL_TRIM_SUSPEND
&&
8231 vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
) {
8232 mutex_exit(&vd
->vdev_trim_lock
);
8233 return (SET_ERROR(ESRCH
));
8237 case POOL_TRIM_START
:
8238 vdev_trim(vd
, rate
, partial
, secure
);
8240 case POOL_TRIM_CANCEL
:
8241 vdev_trim_stop(vd
, VDEV_TRIM_CANCELED
, vd_list
);
8243 case POOL_TRIM_SUSPEND
:
8244 vdev_trim_stop(vd
, VDEV_TRIM_SUSPENDED
, vd_list
);
8247 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
8249 mutex_exit(&vd
->vdev_trim_lock
);
8255 * Initiates a manual TRIM for the requested vdevs. This kicks off individual
8256 * TRIM threads for each child vdev. These threads pass over all of the free
8257 * space in the vdev's metaslabs and issues TRIM commands for that space.
8260 spa_vdev_trim(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
, uint64_t rate
,
8261 boolean_t partial
, boolean_t secure
, nvlist_t
*vdev_errlist
)
8263 int total_errors
= 0;
8266 list_create(&vd_list
, sizeof (vdev_t
),
8267 offsetof(vdev_t
, vdev_trim_node
));
8270 * We hold the namespace lock through the whole function
8271 * to prevent any changes to the pool while we're starting or
8272 * stopping TRIM. The config and state locks are held so that
8273 * we can properly assess the vdev state before we commit to
8274 * the TRIM operation.
8276 mutex_enter(&spa_namespace_lock
);
8278 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
8279 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
8280 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
8282 int error
= spa_vdev_trim_impl(spa
, vdev_guid
, cmd_type
,
8283 rate
, partial
, secure
, &vd_list
);
8285 char guid_as_str
[MAXNAMELEN
];
8287 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
8288 "%llu", (unsigned long long)vdev_guid
);
8289 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
8294 /* Wait for all TRIM threads to stop. */
8295 vdev_trim_stop_wait(spa
, &vd_list
);
8297 /* Sync out the TRIM state */
8298 txg_wait_synced(spa
->spa_dsl_pool
, 0);
8299 mutex_exit(&spa_namespace_lock
);
8301 list_destroy(&vd_list
);
8303 return (total_errors
);
8307 * Split a set of devices from their mirrors, and create a new pool from them.
8310 spa_vdev_split_mirror(spa_t
*spa
, const char *newname
, nvlist_t
*config
,
8311 nvlist_t
*props
, boolean_t exp
)
8314 uint64_t txg
, *glist
;
8316 uint_t c
, children
, lastlog
;
8317 nvlist_t
**child
, *nvl
, *tmp
;
8319 const char *altroot
= NULL
;
8320 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
8321 boolean_t activate_slog
;
8323 ASSERT(spa_writeable(spa
));
8325 txg
= spa_vdev_enter(spa
);
8327 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
8328 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
8329 error
= (spa_has_checkpoint(spa
)) ?
8330 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
8331 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
8334 /* clear the log and flush everything up to now */
8335 activate_slog
= spa_passivate_log(spa
);
8336 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
8337 error
= spa_reset_logs(spa
);
8338 txg
= spa_vdev_config_enter(spa
);
8341 spa_activate_log(spa
);
8344 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
8346 /* check new spa name before going any further */
8347 if (spa_lookup(newname
) != NULL
)
8348 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
8351 * scan through all the children to ensure they're all mirrors
8353 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
8354 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
8356 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
8358 /* first, check to ensure we've got the right child count */
8359 rvd
= spa
->spa_root_vdev
;
8361 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
8362 vdev_t
*vd
= rvd
->vdev_child
[c
];
8364 /* don't count the holes & logs as children */
8365 if (vd
->vdev_islog
|| (vd
->vdev_ops
!= &vdev_indirect_ops
&&
8366 !vdev_is_concrete(vd
))) {
8374 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
8375 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
8377 /* next, ensure no spare or cache devices are part of the split */
8378 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
8379 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
8380 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
8382 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
8383 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
8385 /* then, loop over each vdev and validate it */
8386 for (c
= 0; c
< children
; c
++) {
8387 uint64_t is_hole
= 0;
8389 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
8393 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
8394 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
8397 error
= SET_ERROR(EINVAL
);
8402 /* deal with indirect vdevs */
8403 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ops
==
8407 /* which disk is going to be split? */
8408 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
8410 error
= SET_ERROR(EINVAL
);
8414 /* look it up in the spa */
8415 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
8416 if (vml
[c
] == NULL
) {
8417 error
= SET_ERROR(ENODEV
);
8421 /* make sure there's nothing stopping the split */
8422 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
8423 vml
[c
]->vdev_islog
||
8424 !vdev_is_concrete(vml
[c
]) ||
8425 vml
[c
]->vdev_isspare
||
8426 vml
[c
]->vdev_isl2cache
||
8427 !vdev_writeable(vml
[c
]) ||
8428 vml
[c
]->vdev_children
!= 0 ||
8429 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
8430 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
8431 error
= SET_ERROR(EINVAL
);
8435 if (vdev_dtl_required(vml
[c
]) ||
8436 vdev_resilver_needed(vml
[c
], NULL
, NULL
)) {
8437 error
= SET_ERROR(EBUSY
);
8441 /* we need certain info from the top level */
8442 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
8443 vml
[c
]->vdev_top
->vdev_ms_array
);
8444 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
8445 vml
[c
]->vdev_top
->vdev_ms_shift
);
8446 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
8447 vml
[c
]->vdev_top
->vdev_asize
);
8448 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
8449 vml
[c
]->vdev_top
->vdev_ashift
);
8451 /* transfer per-vdev ZAPs */
8452 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
8453 VERIFY0(nvlist_add_uint64(child
[c
],
8454 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
8456 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
8457 VERIFY0(nvlist_add_uint64(child
[c
],
8458 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
8459 vml
[c
]->vdev_parent
->vdev_top_zap
));
8463 kmem_free(vml
, children
* sizeof (vdev_t
*));
8464 kmem_free(glist
, children
* sizeof (uint64_t));
8465 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
8468 /* stop writers from using the disks */
8469 for (c
= 0; c
< children
; c
++) {
8471 vml
[c
]->vdev_offline
= B_TRUE
;
8473 vdev_reopen(spa
->spa_root_vdev
);
8476 * Temporarily record the splitting vdevs in the spa config. This
8477 * will disappear once the config is regenerated.
8479 nvl
= fnvlist_alloc();
8480 fnvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
, glist
, children
);
8481 kmem_free(glist
, children
* sizeof (uint64_t));
8483 mutex_enter(&spa
->spa_props_lock
);
8484 fnvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
, nvl
);
8485 mutex_exit(&spa
->spa_props_lock
);
8486 spa
->spa_config_splitting
= nvl
;
8487 vdev_config_dirty(spa
->spa_root_vdev
);
8489 /* configure and create the new pool */
8490 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
);
8491 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
8492 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
);
8493 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
, spa_version(spa
));
8494 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, spa
->spa_config_txg
);
8495 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
8496 spa_generate_guid(NULL
));
8497 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
8498 (void) nvlist_lookup_string(props
,
8499 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
8501 /* add the new pool to the namespace */
8502 newspa
= spa_add(newname
, config
, altroot
);
8503 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
8504 newspa
->spa_config_txg
= spa
->spa_config_txg
;
8505 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
8507 /* release the spa config lock, retaining the namespace lock */
8508 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
8510 if (zio_injection_enabled
)
8511 zio_handle_panic_injection(spa
, FTAG
, 1);
8513 spa_activate(newspa
, spa_mode_global
);
8514 spa_async_suspend(newspa
);
8517 * Temporarily stop the initializing and TRIM activity. We set the
8518 * state to ACTIVE so that we know to resume initializing or TRIM
8519 * once the split has completed.
8521 list_t vd_initialize_list
;
8522 list_create(&vd_initialize_list
, sizeof (vdev_t
),
8523 offsetof(vdev_t
, vdev_initialize_node
));
8525 list_t vd_trim_list
;
8526 list_create(&vd_trim_list
, sizeof (vdev_t
),
8527 offsetof(vdev_t
, vdev_trim_node
));
8529 for (c
= 0; c
< children
; c
++) {
8530 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
8531 mutex_enter(&vml
[c
]->vdev_initialize_lock
);
8532 vdev_initialize_stop(vml
[c
],
8533 VDEV_INITIALIZE_ACTIVE
, &vd_initialize_list
);
8534 mutex_exit(&vml
[c
]->vdev_initialize_lock
);
8536 mutex_enter(&vml
[c
]->vdev_trim_lock
);
8537 vdev_trim_stop(vml
[c
], VDEV_TRIM_ACTIVE
, &vd_trim_list
);
8538 mutex_exit(&vml
[c
]->vdev_trim_lock
);
8542 vdev_initialize_stop_wait(spa
, &vd_initialize_list
);
8543 vdev_trim_stop_wait(spa
, &vd_trim_list
);
8545 list_destroy(&vd_initialize_list
);
8546 list_destroy(&vd_trim_list
);
8548 newspa
->spa_config_source
= SPA_CONFIG_SRC_SPLIT
;
8549 newspa
->spa_is_splitting
= B_TRUE
;
8551 /* create the new pool from the disks of the original pool */
8552 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
);
8556 /* if that worked, generate a real config for the new pool */
8557 if (newspa
->spa_root_vdev
!= NULL
) {
8558 newspa
->spa_config_splitting
= fnvlist_alloc();
8559 fnvlist_add_uint64(newspa
->spa_config_splitting
,
8560 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
));
8561 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
8566 if (props
!= NULL
) {
8567 spa_configfile_set(newspa
, props
, B_FALSE
);
8568 error
= spa_prop_set(newspa
, props
);
8573 /* flush everything */
8574 txg
= spa_vdev_config_enter(newspa
);
8575 vdev_config_dirty(newspa
->spa_root_vdev
);
8576 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
8578 if (zio_injection_enabled
)
8579 zio_handle_panic_injection(spa
, FTAG
, 2);
8581 spa_async_resume(newspa
);
8583 /* finally, update the original pool's config */
8584 txg
= spa_vdev_config_enter(spa
);
8585 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
8586 error
= dmu_tx_assign(tx
, TXG_WAIT
);
8589 for (c
= 0; c
< children
; c
++) {
8590 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
8591 vdev_t
*tvd
= vml
[c
]->vdev_top
;
8594 * Need to be sure the detachable VDEV is not
8595 * on any *other* txg's DTL list to prevent it
8596 * from being accessed after it's freed.
8598 for (int t
= 0; t
< TXG_SIZE
; t
++) {
8599 (void) txg_list_remove_this(
8600 &tvd
->vdev_dtl_list
, vml
[c
], t
);
8605 spa_history_log_internal(spa
, "detach", tx
,
8606 "vdev=%s", vml
[c
]->vdev_path
);
8611 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
8612 vdev_config_dirty(spa
->spa_root_vdev
);
8613 spa
->spa_config_splitting
= NULL
;
8617 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
8619 if (zio_injection_enabled
)
8620 zio_handle_panic_injection(spa
, FTAG
, 3);
8622 /* split is complete; log a history record */
8623 spa_history_log_internal(newspa
, "split", NULL
,
8624 "from pool %s", spa_name(spa
));
8626 newspa
->spa_is_splitting
= B_FALSE
;
8627 kmem_free(vml
, children
* sizeof (vdev_t
*));
8629 /* if we're not going to mount the filesystems in userland, export */
8631 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
8638 spa_deactivate(newspa
);
8641 txg
= spa_vdev_config_enter(spa
);
8643 /* re-online all offlined disks */
8644 for (c
= 0; c
< children
; c
++) {
8646 vml
[c
]->vdev_offline
= B_FALSE
;
8649 /* restart initializing or trimming disks as necessary */
8650 spa_async_request(spa
, SPA_ASYNC_INITIALIZE_RESTART
);
8651 spa_async_request(spa
, SPA_ASYNC_TRIM_RESTART
);
8652 spa_async_request(spa
, SPA_ASYNC_AUTOTRIM_RESTART
);
8654 vdev_reopen(spa
->spa_root_vdev
);
8656 nvlist_free(spa
->spa_config_splitting
);
8657 spa
->spa_config_splitting
= NULL
;
8658 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
8660 kmem_free(vml
, children
* sizeof (vdev_t
*));
8665 * Find any device that's done replacing, or a vdev marked 'unspare' that's
8666 * currently spared, so we can detach it.
8669 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
8671 vdev_t
*newvd
, *oldvd
;
8673 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
8674 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
8680 * Check for a completed replacement. We always consider the first
8681 * vdev in the list to be the oldest vdev, and the last one to be
8682 * the newest (see spa_vdev_attach() for how that works). In
8683 * the case where the newest vdev is faulted, we will not automatically
8684 * remove it after a resilver completes. This is OK as it will require
8685 * user intervention to determine which disk the admin wishes to keep.
8687 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
8688 ASSERT(vd
->vdev_children
> 1);
8690 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
8691 oldvd
= vd
->vdev_child
[0];
8693 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
8694 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
8695 !vdev_dtl_required(oldvd
))
8700 * Check for a completed resilver with the 'unspare' flag set.
8701 * Also potentially update faulted state.
8703 if (vd
->vdev_ops
== &vdev_spare_ops
) {
8704 vdev_t
*first
= vd
->vdev_child
[0];
8705 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
8707 if (last
->vdev_unspare
) {
8710 } else if (first
->vdev_unspare
) {
8717 if (oldvd
!= NULL
&&
8718 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
8719 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
8720 !vdev_dtl_required(oldvd
))
8723 vdev_propagate_state(vd
);
8726 * If there are more than two spares attached to a disk,
8727 * and those spares are not required, then we want to
8728 * attempt to free them up now so that they can be used
8729 * by other pools. Once we're back down to a single
8730 * disk+spare, we stop removing them.
8732 if (vd
->vdev_children
> 2) {
8733 newvd
= vd
->vdev_child
[1];
8735 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
8736 vdev_dtl_empty(last
, DTL_MISSING
) &&
8737 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
8738 !vdev_dtl_required(newvd
))
8747 spa_vdev_resilver_done(spa_t
*spa
)
8749 vdev_t
*vd
, *pvd
, *ppvd
;
8750 uint64_t guid
, sguid
, pguid
, ppguid
;
8752 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8754 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
8755 pvd
= vd
->vdev_parent
;
8756 ppvd
= pvd
->vdev_parent
;
8757 guid
= vd
->vdev_guid
;
8758 pguid
= pvd
->vdev_guid
;
8759 ppguid
= ppvd
->vdev_guid
;
8762 * If we have just finished replacing a hot spared device, then
8763 * we need to detach the parent's first child (the original hot
8766 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
8767 ppvd
->vdev_children
== 2) {
8768 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
8769 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
8771 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
8773 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8774 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
8776 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
8778 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8781 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8784 * If a detach was not performed above replace waiters will not have
8785 * been notified. In which case we must do so now.
8787 spa_notify_waiters(spa
);
8791 * Update the stored path or FRU for this vdev.
8794 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
8798 boolean_t sync
= B_FALSE
;
8800 ASSERT(spa_writeable(spa
));
8802 spa_vdev_state_enter(spa
, SCL_ALL
);
8804 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
8805 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
8807 if (!vd
->vdev_ops
->vdev_op_leaf
)
8808 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
8811 if (strcmp(value
, vd
->vdev_path
) != 0) {
8812 spa_strfree(vd
->vdev_path
);
8813 vd
->vdev_path
= spa_strdup(value
);
8817 if (vd
->vdev_fru
== NULL
) {
8818 vd
->vdev_fru
= spa_strdup(value
);
8820 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
8821 spa_strfree(vd
->vdev_fru
);
8822 vd
->vdev_fru
= spa_strdup(value
);
8827 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
8831 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
8833 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
8837 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
8839 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
8843 * ==========================================================================
8845 * ==========================================================================
8848 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
8850 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8852 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8853 return (SET_ERROR(EBUSY
));
8855 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
8859 spa_scan_stop(spa_t
*spa
)
8861 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8862 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8863 return (SET_ERROR(EBUSY
));
8865 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
8869 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
8871 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8873 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
8874 return (SET_ERROR(ENOTSUP
));
8876 if (func
== POOL_SCAN_RESILVER
&&
8877 !spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
))
8878 return (SET_ERROR(ENOTSUP
));
8881 * If a resilver was requested, but there is no DTL on a
8882 * writeable leaf device, we have nothing to do.
8884 if (func
== POOL_SCAN_RESILVER
&&
8885 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
8886 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
8890 if (func
== POOL_SCAN_ERRORSCRUB
&&
8891 !spa_feature_is_enabled(spa
, SPA_FEATURE_HEAD_ERRLOG
))
8892 return (SET_ERROR(ENOTSUP
));
8894 return (dsl_scan(spa
->spa_dsl_pool
, func
));
8898 * ==========================================================================
8899 * SPA async task processing
8900 * ==========================================================================
8904 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
8906 if (vd
->vdev_remove_wanted
) {
8907 vd
->vdev_remove_wanted
= B_FALSE
;
8908 vd
->vdev_delayed_close
= B_FALSE
;
8909 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
8912 * We want to clear the stats, but we don't want to do a full
8913 * vdev_clear() as that will cause us to throw away
8914 * degraded/faulted state as well as attempt to reopen the
8915 * device, all of which is a waste.
8917 vd
->vdev_stat
.vs_read_errors
= 0;
8918 vd
->vdev_stat
.vs_write_errors
= 0;
8919 vd
->vdev_stat
.vs_checksum_errors
= 0;
8921 vdev_state_dirty(vd
->vdev_top
);
8923 /* Tell userspace that the vdev is gone. */
8924 zfs_post_remove(spa
, vd
);
8927 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8928 spa_async_remove(spa
, vd
->vdev_child
[c
]);
8932 spa_async_fault_vdev(spa_t
*spa
, vdev_t
*vd
)
8934 if (vd
->vdev_fault_wanted
) {
8935 vd
->vdev_fault_wanted
= B_FALSE
;
8936 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
8937 VDEV_AUX_ERR_EXCEEDED
);
8940 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8941 spa_async_fault_vdev(spa
, vd
->vdev_child
[c
]);
8945 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
8947 if (!spa
->spa_autoexpand
)
8950 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
8951 vdev_t
*cvd
= vd
->vdev_child
[c
];
8952 spa_async_autoexpand(spa
, cvd
);
8955 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
8958 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
8961 static __attribute__((noreturn
)) void
8962 spa_async_thread(void *arg
)
8964 spa_t
*spa
= (spa_t
*)arg
;
8965 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
8968 ASSERT(spa
->spa_sync_on
);
8970 mutex_enter(&spa
->spa_async_lock
);
8971 tasks
= spa
->spa_async_tasks
;
8972 spa
->spa_async_tasks
= 0;
8973 mutex_exit(&spa
->spa_async_lock
);
8976 * See if the config needs to be updated.
8978 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
8979 uint64_t old_space
, new_space
;
8981 mutex_enter(&spa_namespace_lock
);
8982 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
8983 old_space
+= metaslab_class_get_space(spa_special_class(spa
));
8984 old_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8985 old_space
+= metaslab_class_get_space(
8986 spa_embedded_log_class(spa
));
8988 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
8990 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
8991 new_space
+= metaslab_class_get_space(spa_special_class(spa
));
8992 new_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8993 new_space
+= metaslab_class_get_space(
8994 spa_embedded_log_class(spa
));
8995 mutex_exit(&spa_namespace_lock
);
8998 * If the pool grew as a result of the config update,
8999 * then log an internal history event.
9001 if (new_space
!= old_space
) {
9002 spa_history_log_internal(spa
, "vdev online", NULL
,
9003 "pool '%s' size: %llu(+%llu)",
9004 spa_name(spa
), (u_longlong_t
)new_space
,
9005 (u_longlong_t
)(new_space
- old_space
));
9010 * See if any devices need to be marked REMOVED.
9012 if (tasks
& SPA_ASYNC_REMOVE
) {
9013 spa_vdev_state_enter(spa
, SCL_NONE
);
9014 spa_async_remove(spa
, spa
->spa_root_vdev
);
9015 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
9016 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
9017 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
9018 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
9019 (void) spa_vdev_state_exit(spa
, NULL
, 0);
9022 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
9023 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
9024 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
9025 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
9029 * See if any devices need to be marked faulted.
9031 if (tasks
& SPA_ASYNC_FAULT_VDEV
) {
9032 spa_vdev_state_enter(spa
, SCL_NONE
);
9033 spa_async_fault_vdev(spa
, spa
->spa_root_vdev
);
9034 (void) spa_vdev_state_exit(spa
, NULL
, 0);
9038 * If any devices are done replacing, detach them.
9040 if (tasks
& SPA_ASYNC_RESILVER_DONE
||
9041 tasks
& SPA_ASYNC_REBUILD_DONE
||
9042 tasks
& SPA_ASYNC_DETACH_SPARE
) {
9043 spa_vdev_resilver_done(spa
);
9047 * Kick off a resilver.
9049 if (tasks
& SPA_ASYNC_RESILVER
&&
9050 !vdev_rebuild_active(spa
->spa_root_vdev
) &&
9051 (!dsl_scan_resilvering(dp
) ||
9052 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_RESILVER_DEFER
)))
9053 dsl_scan_restart_resilver(dp
, 0);
9055 if (tasks
& SPA_ASYNC_INITIALIZE_RESTART
) {
9056 mutex_enter(&spa_namespace_lock
);
9057 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
9058 vdev_initialize_restart(spa
->spa_root_vdev
);
9059 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
9060 mutex_exit(&spa_namespace_lock
);
9063 if (tasks
& SPA_ASYNC_TRIM_RESTART
) {
9064 mutex_enter(&spa_namespace_lock
);
9065 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
9066 vdev_trim_restart(spa
->spa_root_vdev
);
9067 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
9068 mutex_exit(&spa_namespace_lock
);
9071 if (tasks
& SPA_ASYNC_AUTOTRIM_RESTART
) {
9072 mutex_enter(&spa_namespace_lock
);
9073 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
9074 vdev_autotrim_restart(spa
);
9075 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
9076 mutex_exit(&spa_namespace_lock
);
9080 * Kick off L2 cache whole device TRIM.
9082 if (tasks
& SPA_ASYNC_L2CACHE_TRIM
) {
9083 mutex_enter(&spa_namespace_lock
);
9084 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
9085 vdev_trim_l2arc(spa
);
9086 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
9087 mutex_exit(&spa_namespace_lock
);
9091 * Kick off L2 cache rebuilding.
9093 if (tasks
& SPA_ASYNC_L2CACHE_REBUILD
) {
9094 mutex_enter(&spa_namespace_lock
);
9095 spa_config_enter(spa
, SCL_L2ARC
, FTAG
, RW_READER
);
9096 l2arc_spa_rebuild_start(spa
);
9097 spa_config_exit(spa
, SCL_L2ARC
, FTAG
);
9098 mutex_exit(&spa_namespace_lock
);
9102 * Let the world know that we're done.
9104 mutex_enter(&spa
->spa_async_lock
);
9105 spa
->spa_async_thread
= NULL
;
9106 cv_broadcast(&spa
->spa_async_cv
);
9107 mutex_exit(&spa
->spa_async_lock
);
9112 spa_async_suspend(spa_t
*spa
)
9114 mutex_enter(&spa
->spa_async_lock
);
9115 spa
->spa_async_suspended
++;
9116 while (spa
->spa_async_thread
!= NULL
)
9117 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
9118 mutex_exit(&spa
->spa_async_lock
);
9120 spa_vdev_remove_suspend(spa
);
9122 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
9123 if (condense_thread
!= NULL
)
9124 zthr_cancel(condense_thread
);
9126 zthr_t
*raidz_expand_thread
= spa
->spa_raidz_expand_zthr
;
9127 if (raidz_expand_thread
!= NULL
)
9128 zthr_cancel(raidz_expand_thread
);
9130 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
9131 if (discard_thread
!= NULL
)
9132 zthr_cancel(discard_thread
);
9134 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
9135 if (ll_delete_thread
!= NULL
)
9136 zthr_cancel(ll_delete_thread
);
9138 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
9139 if (ll_condense_thread
!= NULL
)
9140 zthr_cancel(ll_condense_thread
);
9144 spa_async_resume(spa_t
*spa
)
9146 mutex_enter(&spa
->spa_async_lock
);
9147 ASSERT(spa
->spa_async_suspended
!= 0);
9148 spa
->spa_async_suspended
--;
9149 mutex_exit(&spa
->spa_async_lock
);
9150 spa_restart_removal(spa
);
9152 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
9153 if (condense_thread
!= NULL
)
9154 zthr_resume(condense_thread
);
9156 zthr_t
*raidz_expand_thread
= spa
->spa_raidz_expand_zthr
;
9157 if (raidz_expand_thread
!= NULL
)
9158 zthr_resume(raidz_expand_thread
);
9160 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
9161 if (discard_thread
!= NULL
)
9162 zthr_resume(discard_thread
);
9164 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
9165 if (ll_delete_thread
!= NULL
)
9166 zthr_resume(ll_delete_thread
);
9168 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
9169 if (ll_condense_thread
!= NULL
)
9170 zthr_resume(ll_condense_thread
);
9174 spa_async_tasks_pending(spa_t
*spa
)
9176 uint_t non_config_tasks
;
9178 boolean_t config_task_suspended
;
9180 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
9181 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
9182 if (spa
->spa_ccw_fail_time
== 0) {
9183 config_task_suspended
= B_FALSE
;
9185 config_task_suspended
=
9186 (gethrtime() - spa
->spa_ccw_fail_time
) <
9187 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
9190 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
9194 spa_async_dispatch(spa_t
*spa
)
9196 mutex_enter(&spa
->spa_async_lock
);
9197 if (spa_async_tasks_pending(spa
) &&
9198 !spa
->spa_async_suspended
&&
9199 spa
->spa_async_thread
== NULL
)
9200 spa
->spa_async_thread
= thread_create(NULL
, 0,
9201 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
9202 mutex_exit(&spa
->spa_async_lock
);
9206 spa_async_request(spa_t
*spa
, int task
)
9208 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
9209 mutex_enter(&spa
->spa_async_lock
);
9210 spa
->spa_async_tasks
|= task
;
9211 mutex_exit(&spa
->spa_async_lock
);
9215 spa_async_tasks(spa_t
*spa
)
9217 return (spa
->spa_async_tasks
);
9221 * ==========================================================================
9222 * SPA syncing routines
9223 * ==========================================================================
9228 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
9232 bpobj_enqueue(bpo
, bp
, bp_freed
, tx
);
9237 bpobj_enqueue_alloc_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
9239 return (bpobj_enqueue_cb(arg
, bp
, B_FALSE
, tx
));
9243 bpobj_enqueue_free_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
9245 return (bpobj_enqueue_cb(arg
, bp
, B_TRUE
, tx
));
9249 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
9253 zio_nowait(zio_free_sync(pio
, pio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
9259 bpobj_spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
9263 return (spa_free_sync_cb(arg
, bp
, tx
));
9267 * Note: this simple function is not inlined to make it easier to dtrace the
9268 * amount of time spent syncing frees.
9271 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
9273 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
9274 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
9275 VERIFY(zio_wait(zio
) == 0);
9279 * Note: this simple function is not inlined to make it easier to dtrace the
9280 * amount of time spent syncing deferred frees.
9283 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
9285 if (spa_sync_pass(spa
) != 1)
9290 * If the log space map feature is active, we stop deferring
9291 * frees to the next TXG and therefore running this function
9292 * would be considered a no-op as spa_deferred_bpobj should
9293 * not have any entries.
9295 * That said we run this function anyway (instead of returning
9296 * immediately) for the edge-case scenario where we just
9297 * activated the log space map feature in this TXG but we have
9298 * deferred frees from the previous TXG.
9300 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
9301 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
9302 bpobj_spa_free_sync_cb
, zio
, tx
), ==, 0);
9303 VERIFY0(zio_wait(zio
));
9307 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
9309 char *packed
= NULL
;
9314 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
9317 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
9318 * information. This avoids the dmu_buf_will_dirty() path and
9319 * saves us a pre-read to get data we don't actually care about.
9321 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
9322 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
9324 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
9326 memset(packed
+ nvsize
, 0, bufsize
- nvsize
);
9328 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
9330 vmem_free(packed
, bufsize
);
9332 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
9333 dmu_buf_will_dirty(db
, tx
);
9334 *(uint64_t *)db
->db_data
= nvsize
;
9335 dmu_buf_rele(db
, FTAG
);
9339 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
9340 const char *config
, const char *entry
)
9350 * Update the MOS nvlist describing the list of available devices.
9351 * spa_validate_aux() will have already made sure this nvlist is
9352 * valid and the vdevs are labeled appropriately.
9354 if (sav
->sav_object
== 0) {
9355 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
9356 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
9357 sizeof (uint64_t), tx
);
9358 VERIFY(zap_update(spa
->spa_meta_objset
,
9359 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
9360 &sav
->sav_object
, tx
) == 0);
9363 nvroot
= fnvlist_alloc();
9364 if (sav
->sav_count
== 0) {
9365 fnvlist_add_nvlist_array(nvroot
, config
,
9366 (const nvlist_t
* const *)NULL
, 0);
9368 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
9369 for (i
= 0; i
< sav
->sav_count
; i
++)
9370 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
9371 B_FALSE
, VDEV_CONFIG_L2CACHE
);
9372 fnvlist_add_nvlist_array(nvroot
, config
,
9373 (const nvlist_t
* const *)list
, sav
->sav_count
);
9374 for (i
= 0; i
< sav
->sav_count
; i
++)
9375 nvlist_free(list
[i
]);
9376 kmem_free(list
, sav
->sav_count
* sizeof (void *));
9379 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
9380 nvlist_free(nvroot
);
9382 sav
->sav_sync
= B_FALSE
;
9386 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
9387 * The all-vdev ZAP must be empty.
9390 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
9392 spa_t
*spa
= vd
->vdev_spa
;
9394 if (vd
->vdev_root_zap
!= 0 &&
9395 spa_feature_is_active(spa
, SPA_FEATURE_AVZ_V2
)) {
9396 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
9397 vd
->vdev_root_zap
, tx
));
9399 if (vd
->vdev_top_zap
!= 0) {
9400 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
9401 vd
->vdev_top_zap
, tx
));
9403 if (vd
->vdev_leaf_zap
!= 0) {
9404 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
9405 vd
->vdev_leaf_zap
, tx
));
9407 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
9408 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
9413 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
9418 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
9419 * its config may not be dirty but we still need to build per-vdev ZAPs.
9420 * Similarly, if the pool is being assembled (e.g. after a split), we
9421 * need to rebuild the AVZ although the config may not be dirty.
9423 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
9424 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
9427 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9429 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
9430 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
9431 spa
->spa_all_vdev_zaps
!= 0);
9433 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
9434 /* Make and build the new AVZ */
9435 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
9436 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
9437 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
9439 /* Diff old AVZ with new one */
9441 zap_attribute_t
*za
= zap_attribute_alloc();
9443 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
9444 spa
->spa_all_vdev_zaps
);
9445 zap_cursor_retrieve(&zc
, za
) == 0;
9446 zap_cursor_advance(&zc
)) {
9447 uint64_t vdzap
= za
->za_first_integer
;
9448 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
9451 * ZAP is listed in old AVZ but not in new one;
9454 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
9459 zap_cursor_fini(&zc
);
9460 zap_attribute_free(za
);
9462 /* Destroy the old AVZ */
9463 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
9464 spa
->spa_all_vdev_zaps
, tx
));
9466 /* Replace the old AVZ in the dir obj with the new one */
9467 VERIFY0(zap_update(spa
->spa_meta_objset
,
9468 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
9469 sizeof (new_avz
), 1, &new_avz
, tx
));
9471 spa
->spa_all_vdev_zaps
= new_avz
;
9472 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
9474 zap_attribute_t
*za
= zap_attribute_alloc();
9476 /* Walk through the AVZ and destroy all listed ZAPs */
9477 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
9478 spa
->spa_all_vdev_zaps
);
9479 zap_cursor_retrieve(&zc
, za
) == 0;
9480 zap_cursor_advance(&zc
)) {
9481 uint64_t zap
= za
->za_first_integer
;
9482 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
9485 zap_cursor_fini(&zc
);
9486 zap_attribute_free(za
);
9488 /* Destroy and unlink the AVZ itself */
9489 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
9490 spa
->spa_all_vdev_zaps
, tx
));
9491 VERIFY0(zap_remove(spa
->spa_meta_objset
,
9492 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
9493 spa
->spa_all_vdev_zaps
= 0;
9496 if (spa
->spa_all_vdev_zaps
== 0) {
9497 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
9498 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
9499 DMU_POOL_VDEV_ZAP_MAP
, tx
);
9501 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
9503 /* Create ZAPs for vdevs that don't have them. */
9504 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
9506 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
9507 dmu_tx_get_txg(tx
), B_FALSE
);
9510 * If we're upgrading the spa version then make sure that
9511 * the config object gets updated with the correct version.
9513 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
9514 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
9515 spa
->spa_uberblock
.ub_version
);
9517 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9519 nvlist_free(spa
->spa_config_syncing
);
9520 spa
->spa_config_syncing
= config
;
9522 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
9526 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
9528 uint64_t *versionp
= arg
;
9529 uint64_t version
= *versionp
;
9530 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
9533 * Setting the version is special cased when first creating the pool.
9535 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
9537 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
9538 ASSERT(version
>= spa_version(spa
));
9540 spa
->spa_uberblock
.ub_version
= version
;
9541 vdev_config_dirty(spa
->spa_root_vdev
);
9542 spa_history_log_internal(spa
, "set", tx
, "version=%lld",
9543 (longlong_t
)version
);
9547 * Set zpool properties.
9550 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
9552 nvlist_t
*nvp
= arg
;
9553 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
9554 objset_t
*mos
= spa
->spa_meta_objset
;
9555 nvpair_t
*elem
= NULL
;
9557 mutex_enter(&spa
->spa_props_lock
);
9559 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
9561 const char *strval
, *fname
;
9563 const char *propname
;
9564 const char *elemname
= nvpair_name(elem
);
9565 zprop_type_t proptype
;
9568 switch (prop
= zpool_name_to_prop(elemname
)) {
9569 case ZPOOL_PROP_VERSION
:
9570 intval
= fnvpair_value_uint64(elem
);
9572 * The version is synced separately before other
9573 * properties and should be correct by now.
9575 ASSERT3U(spa_version(spa
), >=, intval
);
9578 case ZPOOL_PROP_ALTROOT
:
9580 * 'altroot' is a non-persistent property. It should
9581 * have been set temporarily at creation or import time.
9583 ASSERT(spa
->spa_root
!= NULL
);
9586 case ZPOOL_PROP_READONLY
:
9587 case ZPOOL_PROP_CACHEFILE
:
9589 * 'readonly' and 'cachefile' are also non-persistent
9593 case ZPOOL_PROP_COMMENT
:
9594 strval
= fnvpair_value_string(elem
);
9595 if (spa
->spa_comment
!= NULL
)
9596 spa_strfree(spa
->spa_comment
);
9597 spa
->spa_comment
= spa_strdup(strval
);
9599 * We need to dirty the configuration on all the vdevs
9600 * so that their labels get updated. We also need to
9601 * update the cache file to keep it in sync with the
9602 * MOS version. It's unnecessary to do this for pool
9603 * creation since the vdev's configuration has already
9606 if (tx
->tx_txg
!= TXG_INITIAL
) {
9607 vdev_config_dirty(spa
->spa_root_vdev
);
9608 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
9610 spa_history_log_internal(spa
, "set", tx
,
9611 "%s=%s", elemname
, strval
);
9613 case ZPOOL_PROP_COMPATIBILITY
:
9614 strval
= fnvpair_value_string(elem
);
9615 if (spa
->spa_compatibility
!= NULL
)
9616 spa_strfree(spa
->spa_compatibility
);
9617 spa
->spa_compatibility
= spa_strdup(strval
);
9619 * Dirty the configuration on vdevs as above.
9621 if (tx
->tx_txg
!= TXG_INITIAL
) {
9622 vdev_config_dirty(spa
->spa_root_vdev
);
9623 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
9626 spa_history_log_internal(spa
, "set", tx
,
9627 "%s=%s", nvpair_name(elem
), strval
);
9630 case ZPOOL_PROP_INVAL
:
9631 if (zpool_prop_feature(elemname
)) {
9632 fname
= strchr(elemname
, '@') + 1;
9633 VERIFY0(zfeature_lookup_name(fname
, &fid
));
9635 spa_feature_enable(spa
, fid
, tx
);
9636 spa_history_log_internal(spa
, "set", tx
,
9637 "%s=enabled", elemname
);
9639 } else if (!zfs_prop_user(elemname
)) {
9640 ASSERT(zpool_prop_feature(elemname
));
9646 * Set pool property values in the poolprops mos object.
9648 if (spa
->spa_pool_props_object
== 0) {
9649 spa
->spa_pool_props_object
=
9650 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
9651 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
9655 /* normalize the property name */
9656 if (prop
== ZPOOL_PROP_INVAL
) {
9657 propname
= elemname
;
9658 proptype
= PROP_TYPE_STRING
;
9660 propname
= zpool_prop_to_name(prop
);
9661 proptype
= zpool_prop_get_type(prop
);
9664 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
9665 ASSERT(proptype
== PROP_TYPE_STRING
);
9666 strval
= fnvpair_value_string(elem
);
9667 VERIFY0(zap_update(mos
,
9668 spa
->spa_pool_props_object
, propname
,
9669 1, strlen(strval
) + 1, strval
, tx
));
9670 spa_history_log_internal(spa
, "set", tx
,
9671 "%s=%s", elemname
, strval
);
9672 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
9673 intval
= fnvpair_value_uint64(elem
);
9675 if (proptype
== PROP_TYPE_INDEX
) {
9677 VERIFY0(zpool_prop_index_to_string(
9678 prop
, intval
, &unused
));
9680 VERIFY0(zap_update(mos
,
9681 spa
->spa_pool_props_object
, propname
,
9682 8, 1, &intval
, tx
));
9683 spa_history_log_internal(spa
, "set", tx
,
9684 "%s=%lld", elemname
,
9685 (longlong_t
)intval
);
9688 case ZPOOL_PROP_DELEGATION
:
9689 spa
->spa_delegation
= intval
;
9691 case ZPOOL_PROP_BOOTFS
:
9692 spa
->spa_bootfs
= intval
;
9694 case ZPOOL_PROP_FAILUREMODE
:
9695 spa
->spa_failmode
= intval
;
9697 case ZPOOL_PROP_AUTOTRIM
:
9698 spa
->spa_autotrim
= intval
;
9699 spa_async_request(spa
,
9700 SPA_ASYNC_AUTOTRIM_RESTART
);
9702 case ZPOOL_PROP_AUTOEXPAND
:
9703 spa
->spa_autoexpand
= intval
;
9704 if (tx
->tx_txg
!= TXG_INITIAL
)
9705 spa_async_request(spa
,
9706 SPA_ASYNC_AUTOEXPAND
);
9708 case ZPOOL_PROP_MULTIHOST
:
9709 spa
->spa_multihost
= intval
;
9711 case ZPOOL_PROP_DEDUP_TABLE_QUOTA
:
9712 spa
->spa_dedup_table_quota
= intval
;
9718 ASSERT(0); /* not allowed */
9724 mutex_exit(&spa
->spa_props_lock
);
9728 * Perform one-time upgrade on-disk changes. spa_version() does not
9729 * reflect the new version this txg, so there must be no changes this
9730 * txg to anything that the upgrade code depends on after it executes.
9731 * Therefore this must be called after dsl_pool_sync() does the sync
9735 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
9737 if (spa_sync_pass(spa
) != 1)
9740 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9741 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
9743 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
9744 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
9745 dsl_pool_create_origin(dp
, tx
);
9747 /* Keeping the origin open increases spa_minref */
9748 spa
->spa_minref
+= 3;
9751 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
9752 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
9753 dsl_pool_upgrade_clones(dp
, tx
);
9756 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
9757 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
9758 dsl_pool_upgrade_dir_clones(dp
, tx
);
9760 /* Keeping the freedir open increases spa_minref */
9761 spa
->spa_minref
+= 3;
9764 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
9765 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
9766 spa_feature_create_zap_objects(spa
, tx
);
9770 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
9771 * when possibility to use lz4 compression for metadata was added
9772 * Old pools that have this feature enabled must be upgraded to have
9773 * this feature active
9775 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
9776 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
9777 SPA_FEATURE_LZ4_COMPRESS
);
9778 boolean_t lz4_ac
= spa_feature_is_active(spa
,
9779 SPA_FEATURE_LZ4_COMPRESS
);
9781 if (lz4_en
&& !lz4_ac
)
9782 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
9786 * If we haven't written the salt, do so now. Note that the
9787 * feature may not be activated yet, but that's fine since
9788 * the presence of this ZAP entry is backwards compatible.
9790 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
9791 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
9792 VERIFY0(zap_add(spa
->spa_meta_objset
,
9793 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
9794 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
9795 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
9798 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
9802 vdev_indirect_state_sync_verify(vdev_t
*vd
)
9804 vdev_indirect_mapping_t
*vim __maybe_unused
= vd
->vdev_indirect_mapping
;
9805 vdev_indirect_births_t
*vib __maybe_unused
= vd
->vdev_indirect_births
;
9807 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
9808 ASSERT(vim
!= NULL
);
9809 ASSERT(vib
!= NULL
);
9812 uint64_t obsolete_sm_object
= 0;
9813 ASSERT0(vdev_obsolete_sm_object(vd
, &obsolete_sm_object
));
9814 if (obsolete_sm_object
!= 0) {
9815 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
9816 ASSERT(vd
->vdev_removing
||
9817 vd
->vdev_ops
== &vdev_indirect_ops
);
9818 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
9819 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
9820 ASSERT3U(obsolete_sm_object
, ==,
9821 space_map_object(vd
->vdev_obsolete_sm
));
9822 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
9823 space_map_allocated(vd
->vdev_obsolete_sm
));
9825 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
9828 * Since frees / remaps to an indirect vdev can only
9829 * happen in syncing context, the obsolete segments
9830 * tree must be empty when we start syncing.
9832 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
9836 * Set the top-level vdev's max queue depth. Evaluate each top-level's
9837 * async write queue depth in case it changed. The max queue depth will
9838 * not change in the middle of syncing out this txg.
9841 spa_sync_adjust_vdev_max_queue_depth(spa_t
*spa
)
9843 ASSERT(spa_writeable(spa
));
9845 vdev_t
*rvd
= spa
->spa_root_vdev
;
9846 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
9847 zfs_vdev_queue_depth_pct
/ 100;
9848 metaslab_class_t
*normal
= spa_normal_class(spa
);
9849 metaslab_class_t
*special
= spa_special_class(spa
);
9850 metaslab_class_t
*dedup
= spa_dedup_class(spa
);
9852 uint64_t slots_per_allocator
= 0;
9853 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9854 vdev_t
*tvd
= rvd
->vdev_child
[c
];
9856 metaslab_group_t
*mg
= tvd
->vdev_mg
;
9857 if (mg
== NULL
|| !metaslab_group_initialized(mg
))
9860 metaslab_class_t
*mc
= mg
->mg_class
;
9861 if (mc
!= normal
&& mc
!= special
&& mc
!= dedup
)
9865 * It is safe to do a lock-free check here because only async
9866 * allocations look at mg_max_alloc_queue_depth, and async
9867 * allocations all happen from spa_sync().
9869 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9870 ASSERT0(zfs_refcount_count(
9871 &(mg
->mg_allocator
[i
].mga_alloc_queue_depth
)));
9873 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
9875 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9876 mg
->mg_allocator
[i
].mga_cur_max_alloc_queue_depth
=
9877 zfs_vdev_def_queue_depth
;
9879 slots_per_allocator
+= zfs_vdev_def_queue_depth
;
9882 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9883 ASSERT0(zfs_refcount_count(&normal
->mc_allocator
[i
].
9885 ASSERT0(zfs_refcount_count(&special
->mc_allocator
[i
].
9887 ASSERT0(zfs_refcount_count(&dedup
->mc_allocator
[i
].
9889 normal
->mc_allocator
[i
].mca_alloc_max_slots
=
9890 slots_per_allocator
;
9891 special
->mc_allocator
[i
].mca_alloc_max_slots
=
9892 slots_per_allocator
;
9893 dedup
->mc_allocator
[i
].mca_alloc_max_slots
=
9894 slots_per_allocator
;
9896 normal
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9897 special
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9898 dedup
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9902 spa_sync_condense_indirect(spa_t
*spa
, dmu_tx_t
*tx
)
9904 ASSERT(spa_writeable(spa
));
9906 vdev_t
*rvd
= spa
->spa_root_vdev
;
9907 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9908 vdev_t
*vd
= rvd
->vdev_child
[c
];
9909 vdev_indirect_state_sync_verify(vd
);
9911 if (vdev_indirect_should_condense(vd
)) {
9912 spa_condense_indirect_start_sync(vd
, tx
);
9919 spa_sync_iterate_to_convergence(spa_t
*spa
, dmu_tx_t
*tx
)
9921 objset_t
*mos
= spa
->spa_meta_objset
;
9922 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9923 uint64_t txg
= tx
->tx_txg
;
9924 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
9927 int pass
= ++spa
->spa_sync_pass
;
9929 spa_sync_config_object(spa
, tx
);
9930 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
9931 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
9932 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
9933 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
9934 spa_errlog_sync(spa
, txg
);
9935 dsl_pool_sync(dp
, txg
);
9937 if (pass
< zfs_sync_pass_deferred_free
||
9938 spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
)) {
9940 * If the log space map feature is active we don't
9941 * care about deferred frees and the deferred bpobj
9942 * as the log space map should effectively have the
9943 * same results (i.e. appending only to one object).
9945 spa_sync_frees(spa
, free_bpl
, tx
);
9948 * We can not defer frees in pass 1, because
9949 * we sync the deferred frees later in pass 1.
9951 ASSERT3U(pass
, >, 1);
9952 bplist_iterate(free_bpl
, bpobj_enqueue_alloc_cb
,
9953 &spa
->spa_deferred_bpobj
, tx
);
9958 dsl_scan_sync(dp
, tx
);
9959 dsl_errorscrub_sync(dp
, tx
);
9961 spa_sync_upgrades(spa
, tx
);
9963 spa_flush_metaslabs(spa
, tx
);
9966 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
9972 * dsl_pool_sync() -> dp_sync_tasks may have dirtied
9973 * the config. If that happens, this txg should not
9974 * be a no-op. So we must sync the config to the MOS
9975 * before checking for no-op.
9977 * Note that when the config is dirty, it will
9978 * be written to the MOS (i.e. the MOS will be
9979 * dirtied) every time we call spa_sync_config_object()
9980 * in this txg. Therefore we can't call this after
9981 * dsl_pool_sync() every pass, because it would
9982 * prevent us from converging, since we'd dirty
9983 * the MOS every pass.
9985 * Sync tasks can only be processed in pass 1, so
9986 * there's no need to do this in later passes.
9988 spa_sync_config_object(spa
, tx
);
9992 * Note: We need to check if the MOS is dirty because we could
9993 * have marked the MOS dirty without updating the uberblock
9994 * (e.g. if we have sync tasks but no dirty user data). We need
9995 * to check the uberblock's rootbp because it is updated if we
9996 * have synced out dirty data (though in this case the MOS will
9997 * most likely also be dirty due to second order effects, we
9998 * don't want to rely on that here).
10001 BP_GET_LOGICAL_BIRTH(&spa
->spa_uberblock
.ub_rootbp
) < txg
&&
10002 !dmu_objset_is_dirty(mos
, txg
)) {
10004 * Nothing changed on the first pass, therefore this
10005 * TXG is a no-op. Avoid syncing deferred frees, so
10006 * that we can keep this TXG as a no-op.
10008 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
10009 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
10010 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
10011 ASSERT(txg_list_empty(&dp
->dp_early_sync_tasks
, txg
));
10015 spa_sync_deferred_frees(spa
, tx
);
10016 } while (dmu_objset_is_dirty(mos
, txg
));
10020 * Rewrite the vdev configuration (which includes the uberblock) to
10021 * commit the transaction group.
10023 * If there are no dirty vdevs, we sync the uberblock to a few random
10024 * top-level vdevs that are known to be visible in the config cache
10025 * (see spa_vdev_add() for a complete description). If there *are* dirty
10026 * vdevs, sync the uberblock to all vdevs.
10029 spa_sync_rewrite_vdev_config(spa_t
*spa
, dmu_tx_t
*tx
)
10031 vdev_t
*rvd
= spa
->spa_root_vdev
;
10032 uint64_t txg
= tx
->tx_txg
;
10038 * We hold SCL_STATE to prevent vdev open/close/etc.
10039 * while we're attempting to write the vdev labels.
10041 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
10043 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
10044 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
10046 int children
= rvd
->vdev_children
;
10047 int c0
= random_in_range(children
);
10049 for (int c
= 0; c
< children
; c
++) {
10051 rvd
->vdev_child
[(c0
+ c
) % children
];
10053 /* Stop when revisiting the first vdev */
10054 if (c
> 0 && svd
[0] == vd
)
10057 if (vd
->vdev_ms_array
== 0 ||
10059 !vdev_is_concrete(vd
))
10062 svd
[svdcount
++] = vd
;
10063 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
10066 error
= vdev_config_sync(svd
, svdcount
, txg
);
10068 error
= vdev_config_sync(rvd
->vdev_child
,
10069 rvd
->vdev_children
, txg
);
10073 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
10075 spa_config_exit(spa
, SCL_STATE
, FTAG
);
10079 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
10080 zio_resume_wait(spa
);
10085 * Sync the specified transaction group. New blocks may be dirtied as
10086 * part of the process, so we iterate until it converges.
10089 spa_sync(spa_t
*spa
, uint64_t txg
)
10093 VERIFY(spa_writeable(spa
));
10096 * Wait for i/os issued in open context that need to complete
10097 * before this txg syncs.
10099 (void) zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]);
10100 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
,
10104 * Now that there can be no more cloning in this transaction group,
10105 * but we are still before issuing frees, we can process pending BRT
10108 brt_pending_apply(spa
, txg
);
10111 * Lock out configuration changes.
10113 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
10115 spa
->spa_syncing_txg
= txg
;
10116 spa
->spa_sync_pass
= 0;
10118 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
10119 mutex_enter(&spa
->spa_allocs
[i
].spaa_lock
);
10120 VERIFY0(avl_numnodes(&spa
->spa_allocs
[i
].spaa_tree
));
10121 mutex_exit(&spa
->spa_allocs
[i
].spaa_lock
);
10125 * If there are any pending vdev state changes, convert them
10126 * into config changes that go out with this transaction group.
10128 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
10129 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
10130 /* Avoid holding the write lock unless actually necessary */
10131 if (vd
->vdev_aux
== NULL
) {
10132 vdev_state_clean(vd
);
10133 vdev_config_dirty(vd
);
10137 * We need the write lock here because, for aux vdevs,
10138 * calling vdev_config_dirty() modifies sav_config.
10139 * This is ugly and will become unnecessary when we
10140 * eliminate the aux vdev wart by integrating all vdevs
10141 * into the root vdev tree.
10143 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
10144 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
10145 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
10146 vdev_state_clean(vd
);
10147 vdev_config_dirty(vd
);
10149 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
10150 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
10152 spa_config_exit(spa
, SCL_STATE
, FTAG
);
10154 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
10155 dmu_tx_t
*tx
= dmu_tx_create_assigned(dp
, txg
);
10157 spa
->spa_sync_starttime
= gethrtime();
10158 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
10159 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
10160 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
10161 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
10164 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
10165 * set spa_deflate if we have no raid-z vdevs.
10167 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
10168 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
10169 vdev_t
*rvd
= spa
->spa_root_vdev
;
10172 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
10173 vd
= rvd
->vdev_child
[i
];
10174 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
10177 if (i
== rvd
->vdev_children
) {
10178 spa
->spa_deflate
= TRUE
;
10179 VERIFY0(zap_add(spa
->spa_meta_objset
,
10180 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
10181 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
10185 spa_sync_adjust_vdev_max_queue_depth(spa
);
10187 spa_sync_condense_indirect(spa
, tx
);
10189 spa_sync_iterate_to_convergence(spa
, tx
);
10192 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
10194 * Make sure that the number of ZAPs for all the vdevs matches
10195 * the number of ZAPs in the per-vdev ZAP list. This only gets
10196 * called if the config is dirty; otherwise there may be
10197 * outstanding AVZ operations that weren't completed in
10198 * spa_sync_config_object.
10200 uint64_t all_vdev_zap_entry_count
;
10201 ASSERT0(zap_count(spa
->spa_meta_objset
,
10202 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
10203 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
10204 all_vdev_zap_entry_count
);
10208 if (spa
->spa_vdev_removal
!= NULL
) {
10209 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
10212 spa_sync_rewrite_vdev_config(spa
, tx
);
10215 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
10216 spa
->spa_deadman_tqid
= 0;
10219 * Clear the dirty config list.
10221 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
10222 vdev_config_clean(vd
);
10225 * Now that the new config has synced transactionally,
10226 * let it become visible to the config cache.
10228 if (spa
->spa_config_syncing
!= NULL
) {
10229 spa_config_set(spa
, spa
->spa_config_syncing
);
10230 spa
->spa_config_txg
= txg
;
10231 spa
->spa_config_syncing
= NULL
;
10234 dsl_pool_sync_done(dp
, txg
);
10236 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
10237 mutex_enter(&spa
->spa_allocs
[i
].spaa_lock
);
10238 VERIFY0(avl_numnodes(&spa
->spa_allocs
[i
].spaa_tree
));
10239 mutex_exit(&spa
->spa_allocs
[i
].spaa_lock
);
10243 * Update usable space statistics.
10245 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
)))
10247 vdev_sync_done(vd
, txg
);
10249 metaslab_class_evict_old(spa
->spa_normal_class
, txg
);
10250 metaslab_class_evict_old(spa
->spa_log_class
, txg
);
10251 /* spa_embedded_log_class has only one metaslab per vdev. */
10252 metaslab_class_evict_old(spa
->spa_special_class
, txg
);
10253 metaslab_class_evict_old(spa
->spa_dedup_class
, txg
);
10255 spa_sync_close_syncing_log_sm(spa
);
10257 spa_update_dspace(spa
);
10259 if (spa_get_autotrim(spa
) == SPA_AUTOTRIM_ON
)
10260 vdev_autotrim_kick(spa
);
10263 * It had better be the case that we didn't dirty anything
10264 * since vdev_config_sync().
10266 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
10267 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
10268 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
10270 while (zfs_pause_spa_sync
)
10273 spa
->spa_sync_pass
= 0;
10276 * Update the last synced uberblock here. We want to do this at
10277 * the end of spa_sync() so that consumers of spa_last_synced_txg()
10278 * will be guaranteed that all the processing associated with
10279 * that txg has been completed.
10281 spa
->spa_ubsync
= spa
->spa_uberblock
;
10282 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
10284 spa_handle_ignored_writes(spa
);
10287 * If any async tasks have been requested, kick them off.
10289 spa_async_dispatch(spa
);
10293 * Sync all pools. We don't want to hold the namespace lock across these
10294 * operations, so we take a reference on the spa_t and drop the lock during the
10298 spa_sync_allpools(void)
10301 mutex_enter(&spa_namespace_lock
);
10302 while ((spa
= spa_next(spa
)) != NULL
) {
10303 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
10304 !spa_writeable(spa
) || spa_suspended(spa
))
10306 spa_open_ref(spa
, FTAG
);
10307 mutex_exit(&spa_namespace_lock
);
10308 txg_wait_synced(spa_get_dsl(spa
), 0);
10309 mutex_enter(&spa_namespace_lock
);
10310 spa_close(spa
, FTAG
);
10312 mutex_exit(&spa_namespace_lock
);
10316 spa_sync_tq_create(spa_t
*spa
, const char *name
)
10318 kthread_t
**kthreads
;
10320 ASSERT(spa
->spa_sync_tq
== NULL
);
10321 ASSERT3S(spa
->spa_alloc_count
, <=, boot_ncpus
);
10324 * - do not allow more allocators than cpus.
10325 * - there may be more cpus than allocators.
10326 * - do not allow more sync taskq threads than allocators or cpus.
10328 int nthreads
= spa
->spa_alloc_count
;
10329 spa
->spa_syncthreads
= kmem_zalloc(sizeof (spa_syncthread_info_t
) *
10330 nthreads
, KM_SLEEP
);
10332 spa
->spa_sync_tq
= taskq_create_synced(name
, nthreads
, minclsyspri
,
10333 nthreads
, INT_MAX
, TASKQ_PREPOPULATE
, &kthreads
);
10334 VERIFY(spa
->spa_sync_tq
!= NULL
);
10335 VERIFY(kthreads
!= NULL
);
10337 spa_syncthread_info_t
*ti
= spa
->spa_syncthreads
;
10338 for (int i
= 0; i
< nthreads
; i
++, ti
++) {
10339 ti
->sti_thread
= kthreads
[i
];
10340 ti
->sti_allocator
= i
;
10343 kmem_free(kthreads
, sizeof (*kthreads
) * nthreads
);
10344 return (spa
->spa_sync_tq
);
10348 spa_sync_tq_destroy(spa_t
*spa
)
10350 ASSERT(spa
->spa_sync_tq
!= NULL
);
10352 taskq_wait(spa
->spa_sync_tq
);
10353 taskq_destroy(spa
->spa_sync_tq
);
10354 kmem_free(spa
->spa_syncthreads
,
10355 sizeof (spa_syncthread_info_t
) * spa
->spa_alloc_count
);
10356 spa
->spa_sync_tq
= NULL
;
10360 spa_acq_allocator(spa_t
*spa
)
10364 if (spa
->spa_alloc_count
== 1)
10367 mutex_enter(&spa
->spa_allocs_use
->sau_lock
);
10368 uint_t r
= spa
->spa_allocs_use
->sau_rotor
;
10370 if (++r
== spa
->spa_alloc_count
)
10372 } while (spa
->spa_allocs_use
->sau_inuse
[r
]);
10373 spa
->spa_allocs_use
->sau_inuse
[r
] = B_TRUE
;
10374 spa
->spa_allocs_use
->sau_rotor
= r
;
10375 mutex_exit(&spa
->spa_allocs_use
->sau_lock
);
10377 spa_syncthread_info_t
*ti
= spa
->spa_syncthreads
;
10378 for (i
= 0; i
< spa
->spa_alloc_count
; i
++, ti
++) {
10379 if (ti
->sti_thread
== curthread
) {
10380 ti
->sti_allocator
= r
;
10384 ASSERT3S(i
, <, spa
->spa_alloc_count
);
10389 spa_rel_allocator(spa_t
*spa
, uint_t allocator
)
10391 if (spa
->spa_alloc_count
> 1)
10392 spa
->spa_allocs_use
->sau_inuse
[allocator
] = B_FALSE
;
10396 spa_select_allocator(zio_t
*zio
)
10398 zbookmark_phys_t
*bm
= &zio
->io_bookmark
;
10399 spa_t
*spa
= zio
->io_spa
;
10401 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
10404 * A gang block (for example) may have inherited its parent's
10405 * allocator, in which case there is nothing further to do here.
10407 if (ZIO_HAS_ALLOCATOR(zio
))
10410 ASSERT(spa
!= NULL
);
10411 ASSERT(bm
!= NULL
);
10414 * First try to use an allocator assigned to the syncthread, and set
10415 * the corresponding write issue taskq for the allocator.
10416 * Note, we must have an open pool to do this.
10418 if (spa
->spa_sync_tq
!= NULL
) {
10419 spa_syncthread_info_t
*ti
= spa
->spa_syncthreads
;
10420 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++, ti
++) {
10421 if (ti
->sti_thread
== curthread
) {
10422 zio
->io_allocator
= ti
->sti_allocator
;
10429 * We want to try to use as many allocators as possible to help improve
10430 * performance, but we also want logically adjacent IOs to be physically
10431 * adjacent to improve sequential read performance. We chunk each object
10432 * into 2^20 block regions, and then hash based on the objset, object,
10433 * level, and region to accomplish both of these goals.
10435 uint64_t hv
= cityhash4(bm
->zb_objset
, bm
->zb_object
, bm
->zb_level
,
10436 bm
->zb_blkid
>> 20);
10438 zio
->io_allocator
= (uint_t
)hv
% spa
->spa_alloc_count
;
10442 * ==========================================================================
10443 * Miscellaneous routines
10444 * ==========================================================================
10448 * Remove all pools in the system.
10451 spa_evict_all(void)
10456 * Remove all cached state. All pools should be closed now,
10457 * so every spa in the AVL tree should be unreferenced.
10459 mutex_enter(&spa_namespace_lock
);
10460 while ((spa
= spa_next(NULL
)) != NULL
) {
10462 * Stop async tasks. The async thread may need to detach
10463 * a device that's been replaced, which requires grabbing
10464 * spa_namespace_lock, so we must drop it here.
10466 spa_open_ref(spa
, FTAG
);
10467 mutex_exit(&spa_namespace_lock
);
10468 spa_async_suspend(spa
);
10469 mutex_enter(&spa_namespace_lock
);
10470 spa_close(spa
, FTAG
);
10472 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
10474 spa_deactivate(spa
);
10478 mutex_exit(&spa_namespace_lock
);
10482 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
10487 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
10491 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
10492 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
10493 if (vd
->vdev_guid
== guid
)
10497 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
10498 vd
= spa
->spa_spares
.sav_vdevs
[i
];
10499 if (vd
->vdev_guid
== guid
)
10508 spa_upgrade(spa_t
*spa
, uint64_t version
)
10510 ASSERT(spa_writeable(spa
));
10512 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
10515 * This should only be called for a non-faulted pool, and since a
10516 * future version would result in an unopenable pool, this shouldn't be
10519 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
10520 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
10522 spa
->spa_uberblock
.ub_version
= version
;
10523 vdev_config_dirty(spa
->spa_root_vdev
);
10525 spa_config_exit(spa
, SCL_ALL
, FTAG
);
10527 txg_wait_synced(spa_get_dsl(spa
), 0);
10531 spa_has_aux_vdev(spa_t
*spa
, uint64_t guid
, spa_aux_vdev_t
*sav
)
10535 uint64_t vdev_guid
;
10537 for (i
= 0; i
< sav
->sav_count
; i
++)
10538 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
10541 for (i
= 0; i
< sav
->sav_npending
; i
++) {
10542 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
10543 &vdev_guid
) == 0 && vdev_guid
== guid
)
10551 spa_has_l2cache(spa_t
*spa
, uint64_t guid
)
10553 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_l2cache
));
10557 spa_has_spare(spa_t
*spa
, uint64_t guid
)
10559 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_spares
));
10563 * Check if a pool has an active shared spare device.
10564 * Note: reference count of an active spare is 2, as a spare and as a replace
10567 spa_has_active_shared_spare(spa_t
*spa
)
10571 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
10573 for (i
= 0; i
< sav
->sav_count
; i
++) {
10574 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
10575 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
10584 spa_total_metaslabs(spa_t
*spa
)
10586 vdev_t
*rvd
= spa
->spa_root_vdev
;
10589 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
10590 vdev_t
*vd
= rvd
->vdev_child
[c
];
10591 if (!vdev_is_concrete(vd
))
10593 m
+= vd
->vdev_ms_count
;
10599 * Notify any waiting threads that some activity has switched from being in-
10600 * progress to not-in-progress so that the thread can wake up and determine
10601 * whether it is finished waiting.
10604 spa_notify_waiters(spa_t
*spa
)
10607 * Acquiring spa_activities_lock here prevents the cv_broadcast from
10608 * happening between the waiting thread's check and cv_wait.
10610 mutex_enter(&spa
->spa_activities_lock
);
10611 cv_broadcast(&spa
->spa_activities_cv
);
10612 mutex_exit(&spa
->spa_activities_lock
);
10616 * Notify any waiting threads that the pool is exporting, and then block until
10617 * they are finished using the spa_t.
10620 spa_wake_waiters(spa_t
*spa
)
10622 mutex_enter(&spa
->spa_activities_lock
);
10623 spa
->spa_waiters_cancel
= B_TRUE
;
10624 cv_broadcast(&spa
->spa_activities_cv
);
10625 while (spa
->spa_waiters
!= 0)
10626 cv_wait(&spa
->spa_waiters_cv
, &spa
->spa_activities_lock
);
10627 spa
->spa_waiters_cancel
= B_FALSE
;
10628 mutex_exit(&spa
->spa_activities_lock
);
10631 /* Whether the vdev or any of its descendants are being initialized/trimmed. */
10633 spa_vdev_activity_in_progress_impl(vdev_t
*vd
, zpool_wait_activity_t activity
)
10635 spa_t
*spa
= vd
->vdev_spa
;
10637 ASSERT(spa_config_held(spa
, SCL_CONFIG
| SCL_STATE
, RW_READER
));
10638 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
10639 ASSERT(activity
== ZPOOL_WAIT_INITIALIZE
||
10640 activity
== ZPOOL_WAIT_TRIM
);
10642 kmutex_t
*lock
= activity
== ZPOOL_WAIT_INITIALIZE
?
10643 &vd
->vdev_initialize_lock
: &vd
->vdev_trim_lock
;
10645 mutex_exit(&spa
->spa_activities_lock
);
10647 mutex_enter(&spa
->spa_activities_lock
);
10649 boolean_t in_progress
= (activity
== ZPOOL_WAIT_INITIALIZE
) ?
10650 (vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
) :
10651 (vd
->vdev_trim_state
== VDEV_TRIM_ACTIVE
);
10657 for (int i
= 0; i
< vd
->vdev_children
; i
++) {
10658 if (spa_vdev_activity_in_progress_impl(vd
->vdev_child
[i
],
10667 * If use_guid is true, this checks whether the vdev specified by guid is
10668 * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool
10669 * is being initialized/trimmed. The caller must hold the config lock and
10670 * spa_activities_lock.
10673 spa_vdev_activity_in_progress(spa_t
*spa
, boolean_t use_guid
, uint64_t guid
,
10674 zpool_wait_activity_t activity
, boolean_t
*in_progress
)
10676 mutex_exit(&spa
->spa_activities_lock
);
10677 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
10678 mutex_enter(&spa
->spa_activities_lock
);
10682 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
10683 if (vd
== NULL
|| !vd
->vdev_ops
->vdev_op_leaf
) {
10684 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
10688 vd
= spa
->spa_root_vdev
;
10691 *in_progress
= spa_vdev_activity_in_progress_impl(vd
, activity
);
10693 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
10698 * Locking for waiting threads
10699 * ---------------------------
10701 * Waiting threads need a way to check whether a given activity is in progress,
10702 * and then, if it is, wait for it to complete. Each activity will have some
10703 * in-memory representation of the relevant on-disk state which can be used to
10704 * determine whether or not the activity is in progress. The in-memory state and
10705 * the locking used to protect it will be different for each activity, and may
10706 * not be suitable for use with a cvar (e.g., some state is protected by the
10707 * config lock). To allow waiting threads to wait without any races, another
10708 * lock, spa_activities_lock, is used.
10710 * When the state is checked, both the activity-specific lock (if there is one)
10711 * and spa_activities_lock are held. In some cases, the activity-specific lock
10712 * is acquired explicitly (e.g. the config lock). In others, the locking is
10713 * internal to some check (e.g. bpobj_is_empty). After checking, the waiting
10714 * thread releases the activity-specific lock and, if the activity is in
10715 * progress, then cv_waits using spa_activities_lock.
10717 * The waiting thread is woken when another thread, one completing some
10718 * activity, updates the state of the activity and then calls
10719 * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only
10720 * needs to hold its activity-specific lock when updating the state, and this
10721 * lock can (but doesn't have to) be dropped before calling spa_notify_waiters.
10723 * Because spa_notify_waiters acquires spa_activities_lock before broadcasting,
10724 * and because it is held when the waiting thread checks the state of the
10725 * activity, it can never be the case that the completing thread both updates
10726 * the activity state and cv_broadcasts in between the waiting thread's check
10727 * and cv_wait. Thus, a waiting thread can never miss a wakeup.
10729 * In order to prevent deadlock, when the waiting thread does its check, in some
10730 * cases it will temporarily drop spa_activities_lock in order to acquire the
10731 * activity-specific lock. The order in which spa_activities_lock and the
10732 * activity specific lock are acquired in the waiting thread is determined by
10733 * the order in which they are acquired in the completing thread; if the
10734 * completing thread calls spa_notify_waiters with the activity-specific lock
10735 * held, then the waiting thread must also acquire the activity-specific lock
10740 spa_activity_in_progress(spa_t
*spa
, zpool_wait_activity_t activity
,
10741 boolean_t use_tag
, uint64_t tag
, boolean_t
*in_progress
)
10745 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
10747 switch (activity
) {
10748 case ZPOOL_WAIT_CKPT_DISCARD
:
10750 (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
) &&
10751 zap_contains(spa_meta_objset(spa
),
10752 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_ZPOOL_CHECKPOINT
) ==
10755 case ZPOOL_WAIT_FREE
:
10756 *in_progress
= ((spa_version(spa
) >= SPA_VERSION_DEADLISTS
&&
10757 !bpobj_is_empty(&spa
->spa_dsl_pool
->dp_free_bpobj
)) ||
10758 spa_feature_is_active(spa
, SPA_FEATURE_ASYNC_DESTROY
) ||
10759 spa_livelist_delete_check(spa
));
10761 case ZPOOL_WAIT_INITIALIZE
:
10762 case ZPOOL_WAIT_TRIM
:
10763 error
= spa_vdev_activity_in_progress(spa
, use_tag
, tag
,
10764 activity
, in_progress
);
10766 case ZPOOL_WAIT_REPLACE
:
10767 mutex_exit(&spa
->spa_activities_lock
);
10768 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
10769 mutex_enter(&spa
->spa_activities_lock
);
10771 *in_progress
= vdev_replace_in_progress(spa
->spa_root_vdev
);
10772 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
10774 case ZPOOL_WAIT_REMOVE
:
10775 *in_progress
= (spa
->spa_removing_phys
.sr_state
==
10778 case ZPOOL_WAIT_RESILVER
:
10779 *in_progress
= vdev_rebuild_active(spa
->spa_root_vdev
);
10783 case ZPOOL_WAIT_SCRUB
:
10785 boolean_t scanning
, paused
, is_scrub
;
10786 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
10788 is_scrub
= (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
);
10789 scanning
= (scn
->scn_phys
.scn_state
== DSS_SCANNING
);
10790 paused
= dsl_scan_is_paused_scrub(scn
);
10791 *in_progress
= (scanning
&& !paused
&&
10792 is_scrub
== (activity
== ZPOOL_WAIT_SCRUB
));
10795 case ZPOOL_WAIT_RAIDZ_EXPAND
:
10797 vdev_raidz_expand_t
*vre
= spa
->spa_raidz_expand
;
10798 *in_progress
= (vre
!= NULL
&& vre
->vre_state
== DSS_SCANNING
);
10802 panic("unrecognized value for activity %d", activity
);
10809 spa_wait_common(const char *pool
, zpool_wait_activity_t activity
,
10810 boolean_t use_tag
, uint64_t tag
, boolean_t
*waited
)
10813 * The tag is used to distinguish between instances of an activity.
10814 * 'initialize' and 'trim' are the only activities that we use this for.
10815 * The other activities can only have a single instance in progress in a
10816 * pool at one time, making the tag unnecessary.
10818 * There can be multiple devices being replaced at once, but since they
10819 * all finish once resilvering finishes, we don't bother keeping track
10820 * of them individually, we just wait for them all to finish.
10822 if (use_tag
&& activity
!= ZPOOL_WAIT_INITIALIZE
&&
10823 activity
!= ZPOOL_WAIT_TRIM
)
10826 if (activity
< 0 || activity
>= ZPOOL_WAIT_NUM_ACTIVITIES
)
10830 int error
= spa_open(pool
, &spa
, FTAG
);
10835 * Increment the spa's waiter count so that we can call spa_close and
10836 * still ensure that the spa_t doesn't get freed before this thread is
10837 * finished with it when the pool is exported. We want to call spa_close
10838 * before we start waiting because otherwise the additional ref would
10839 * prevent the pool from being exported or destroyed throughout the
10840 * potentially long wait.
10842 mutex_enter(&spa
->spa_activities_lock
);
10843 spa
->spa_waiters
++;
10844 spa_close(spa
, FTAG
);
10848 boolean_t in_progress
;
10849 error
= spa_activity_in_progress(spa
, activity
, use_tag
, tag
,
10852 if (error
|| !in_progress
|| spa
->spa_waiters_cancel
)
10857 if (cv_wait_sig(&spa
->spa_activities_cv
,
10858 &spa
->spa_activities_lock
) == 0) {
10864 spa
->spa_waiters
--;
10865 cv_signal(&spa
->spa_waiters_cv
);
10866 mutex_exit(&spa
->spa_activities_lock
);
10872 * Wait for a particular instance of the specified activity to complete, where
10873 * the instance is identified by 'tag'
10876 spa_wait_tag(const char *pool
, zpool_wait_activity_t activity
, uint64_t tag
,
10879 return (spa_wait_common(pool
, activity
, B_TRUE
, tag
, waited
));
10883 * Wait for all instances of the specified activity complete
10886 spa_wait(const char *pool
, zpool_wait_activity_t activity
, boolean_t
*waited
)
10889 return (spa_wait_common(pool
, activity
, B_FALSE
, 0, waited
));
10893 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
10895 sysevent_t
*ev
= NULL
;
10897 nvlist_t
*resource
;
10899 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
10901 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
10902 ev
->resource
= resource
;
10905 (void) spa
, (void) vd
, (void) hist_nvl
, (void) name
;
10911 spa_event_post(sysevent_t
*ev
)
10915 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
10916 kmem_free(ev
, sizeof (*ev
));
10924 * Post a zevent corresponding to the given sysevent. The 'name' must be one
10925 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
10926 * filled in from the spa and (optionally) the vdev. This doesn't do anything
10927 * in the userland libzpool, as we don't want consumers to misinterpret ztest
10928 * or zdb as real changes.
10931 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
10933 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
10936 /* state manipulation functions */
10937 EXPORT_SYMBOL(spa_open
);
10938 EXPORT_SYMBOL(spa_open_rewind
);
10939 EXPORT_SYMBOL(spa_get_stats
);
10940 EXPORT_SYMBOL(spa_create
);
10941 EXPORT_SYMBOL(spa_import
);
10942 EXPORT_SYMBOL(spa_tryimport
);
10943 EXPORT_SYMBOL(spa_destroy
);
10944 EXPORT_SYMBOL(spa_export
);
10945 EXPORT_SYMBOL(spa_reset
);
10946 EXPORT_SYMBOL(spa_async_request
);
10947 EXPORT_SYMBOL(spa_async_suspend
);
10948 EXPORT_SYMBOL(spa_async_resume
);
10949 EXPORT_SYMBOL(spa_inject_addref
);
10950 EXPORT_SYMBOL(spa_inject_delref
);
10951 EXPORT_SYMBOL(spa_scan_stat_init
);
10952 EXPORT_SYMBOL(spa_scan_get_stats
);
10954 /* device manipulation */
10955 EXPORT_SYMBOL(spa_vdev_add
);
10956 EXPORT_SYMBOL(spa_vdev_attach
);
10957 EXPORT_SYMBOL(spa_vdev_detach
);
10958 EXPORT_SYMBOL(spa_vdev_setpath
);
10959 EXPORT_SYMBOL(spa_vdev_setfru
);
10960 EXPORT_SYMBOL(spa_vdev_split_mirror
);
10962 /* spare statech is global across all pools) */
10963 EXPORT_SYMBOL(spa_spare_add
);
10964 EXPORT_SYMBOL(spa_spare_remove
);
10965 EXPORT_SYMBOL(spa_spare_exists
);
10966 EXPORT_SYMBOL(spa_spare_activate
);
10968 /* L2ARC statech is global across all pools) */
10969 EXPORT_SYMBOL(spa_l2cache_add
);
10970 EXPORT_SYMBOL(spa_l2cache_remove
);
10971 EXPORT_SYMBOL(spa_l2cache_exists
);
10972 EXPORT_SYMBOL(spa_l2cache_activate
);
10973 EXPORT_SYMBOL(spa_l2cache_drop
);
10976 EXPORT_SYMBOL(spa_scan
);
10977 EXPORT_SYMBOL(spa_scan_stop
);
10980 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
10981 EXPORT_SYMBOL(spa_sync_allpools
);
10984 EXPORT_SYMBOL(spa_prop_set
);
10985 EXPORT_SYMBOL(spa_prop_get
);
10986 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
10988 /* asynchronous event notification */
10989 EXPORT_SYMBOL(spa_event_notify
);
10991 ZFS_MODULE_PARAM(zfs_metaslab
, metaslab_
, preload_pct
, UINT
, ZMOD_RW
,
10992 "Percentage of CPUs to run a metaslab preload taskq");
10994 /* BEGIN CSTYLED */
10995 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_shift
, UINT
, ZMOD_RW
,
10996 "log2 fraction of arc that can be used by inflight I/Os when "
10997 "verifying pool during import");
11000 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_metadata
, INT
, ZMOD_RW
,
11001 "Set to traverse metadata on pool import");
11003 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_data
, INT
, ZMOD_RW
,
11004 "Set to traverse data on pool import");
11006 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_print_vdev_tree
, INT
, ZMOD_RW
,
11007 "Print vdev tree to zfs_dbgmsg during pool import");
11009 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_pct
, UINT
, ZMOD_RW
,
11010 "Percentage of CPUs to run an IO worker thread");
11012 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_tpq
, UINT
, ZMOD_RW
,
11013 "Number of threads per IO worker taskqueue");
11015 /* BEGIN CSTYLED */
11016 ZFS_MODULE_PARAM(zfs
, zfs_
, max_missing_tvds
, U64
, ZMOD_RW
,
11017 "Allow importing pool with up to this number of missing top-level "
11018 "vdevs (in read-only mode)");
11021 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_pause
, INT
,
11022 ZMOD_RW
, "Set the livelist condense zthr to pause");
11024 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_pause
, INT
,
11025 ZMOD_RW
, "Set the livelist condense synctask to pause");
11027 /* BEGIN CSTYLED */
11028 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_cancel
,
11030 "Whether livelist condensing was canceled in the synctask");
11032 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_cancel
,
11034 "Whether livelist condensing was canceled in the zthr function");
11036 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, new_alloc
, INT
,
11038 "Whether extra ALLOC blkptrs were added to a livelist entry while it "
11039 "was being condensed");
11042 ZFS_MODULE_VIRTUAL_PARAM_CALL(zfs_zio
, zio_
, taskq_read
,
11043 spa_taskq_read_param_set
, spa_taskq_read_param_get
, ZMOD_RW
,
11044 "Configure IO queues for read IO");
11045 ZFS_MODULE_VIRTUAL_PARAM_CALL(zfs_zio
, zio_
, taskq_write
,
11046 spa_taskq_write_param_set
, spa_taskq_write_param_get
, ZMOD_RW
,
11047 "Configure IO queues for write IO");
11051 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_write_tpq
, UINT
, ZMOD_RW
,
11052 "Number of CPUs per write issue taskq");