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 http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25 * Copyright (c) 2018, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright 2013 Saso Kiselkov. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
29 * Copyright 2016 Toomas Soome <tsoome@me.com>
30 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
31 * Copyright 2018 Joyent, Inc.
32 * Copyright (c) 2017, 2019, Datto Inc. All rights reserved.
33 * Copyright 2017 Joyent, Inc.
34 * Copyright (c) 2017, Intel Corporation.
38 * SPA: Storage Pool Allocator
40 * This file contains all the routines used when modifying on-disk SPA state.
41 * This includes opening, importing, destroying, exporting a pool, and syncing a
45 #include <sys/zfs_context.h>
46 #include <sys/fm/fs/zfs.h>
47 #include <sys/spa_impl.h>
49 #include <sys/zio_checksum.h>
51 #include <sys/dmu_tx.h>
55 #include <sys/vdev_impl.h>
56 #include <sys/vdev_removal.h>
57 #include <sys/vdev_indirect_mapping.h>
58 #include <sys/vdev_indirect_births.h>
59 #include <sys/vdev_initialize.h>
60 #include <sys/vdev_rebuild.h>
61 #include <sys/vdev_trim.h>
62 #include <sys/vdev_disk.h>
63 #include <sys/vdev_draid.h>
64 #include <sys/metaslab.h>
65 #include <sys/metaslab_impl.h>
67 #include <sys/uberblock_impl.h>
70 #include <sys/bpobj.h>
71 #include <sys/dmu_traverse.h>
72 #include <sys/dmu_objset.h>
73 #include <sys/unique.h>
74 #include <sys/dsl_pool.h>
75 #include <sys/dsl_dataset.h>
76 #include <sys/dsl_dir.h>
77 #include <sys/dsl_prop.h>
78 #include <sys/dsl_synctask.h>
79 #include <sys/fs/zfs.h>
81 #include <sys/callb.h>
82 #include <sys/systeminfo.h>
83 #include <sys/spa_boot.h>
84 #include <sys/zfs_ioctl.h>
85 #include <sys/dsl_scan.h>
86 #include <sys/zfeature.h>
87 #include <sys/dsl_destroy.h>
91 #include <sys/fm/protocol.h>
92 #include <sys/fm/util.h>
93 #include <sys/callb.h>
95 #include <sys/vmsystm.h>
99 #include "zfs_comutil.h"
102 * The interval, in seconds, at which failed configuration cache file writes
105 int zfs_ccw_retry_interval
= 300;
107 typedef enum zti_modes
{
108 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
109 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
110 ZTI_MODE_NULL
, /* don't create a taskq */
114 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
115 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
116 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
117 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
119 #define ZTI_N(n) ZTI_P(n, 1)
120 #define ZTI_ONE ZTI_N(1)
122 typedef struct zio_taskq_info
{
123 zti_modes_t zti_mode
;
128 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
129 "iss", "iss_h", "int", "int_h"
133 * This table defines the taskq settings for each ZFS I/O type. When
134 * initializing a pool, we use this table to create an appropriately sized
135 * taskq. Some operations are low volume and therefore have a small, static
136 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
137 * macros. Other operations process a large amount of data; the ZTI_BATCH
138 * macro causes us to create a taskq oriented for throughput. Some operations
139 * are so high frequency and short-lived that the taskq itself can become a
140 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
141 * additional degree of parallelism specified by the number of threads per-
142 * taskq and the number of taskqs; when dispatching an event in this case, the
143 * particular taskq is chosen at random.
145 * The different taskq priorities are to handle the different contexts (issue
146 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
147 * need to be handled with minimum delay.
149 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
150 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
151 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
152 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
153 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
154 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
155 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
156 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
157 { ZTI_N(4), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* TRIM */
160 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
161 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
162 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
163 static int spa_load_impl(spa_t
*spa
, spa_import_type_t type
, char **ereport
);
164 static void spa_vdev_resilver_done(spa_t
*spa
);
166 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
167 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
168 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
170 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
173 * Report any spa_load_verify errors found, but do not fail spa_load.
174 * This is used by zdb to analyze non-idle pools.
176 boolean_t spa_load_verify_dryrun
= B_FALSE
;
179 * This (illegal) pool name is used when temporarily importing a spa_t in order
180 * to get the vdev stats associated with the imported devices.
182 #define TRYIMPORT_NAME "$import"
185 * For debugging purposes: print out vdev tree during pool import.
187 int spa_load_print_vdev_tree
= B_FALSE
;
190 * A non-zero value for zfs_max_missing_tvds means that we allow importing
191 * pools with missing top-level vdevs. This is strictly intended for advanced
192 * pool recovery cases since missing data is almost inevitable. Pools with
193 * missing devices can only be imported read-only for safety reasons, and their
194 * fail-mode will be automatically set to "continue".
196 * With 1 missing vdev we should be able to import the pool and mount all
197 * datasets. User data that was not modified after the missing device has been
198 * added should be recoverable. This means that snapshots created prior to the
199 * addition of that device should be completely intact.
201 * With 2 missing vdevs, some datasets may fail to mount since there are
202 * dataset statistics that are stored as regular metadata. Some data might be
203 * recoverable if those vdevs were added recently.
205 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
206 * may be missing entirely. Chances of data recovery are very low. Note that
207 * there are also risks of performing an inadvertent rewind as we might be
208 * missing all the vdevs with the latest uberblocks.
210 unsigned long zfs_max_missing_tvds
= 0;
213 * The parameters below are similar to zfs_max_missing_tvds but are only
214 * intended for a preliminary open of the pool with an untrusted config which
215 * might be incomplete or out-dated.
217 * We are more tolerant for pools opened from a cachefile since we could have
218 * an out-dated cachefile where a device removal was not registered.
219 * We could have set the limit arbitrarily high but in the case where devices
220 * are really missing we would want to return the proper error codes; we chose
221 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
222 * and we get a chance to retrieve the trusted config.
224 uint64_t zfs_max_missing_tvds_cachefile
= SPA_DVAS_PER_BP
- 1;
227 * In the case where config was assembled by scanning device paths (/dev/dsks
228 * by default) we are less tolerant since all the existing devices should have
229 * been detected and we want spa_load to return the right error codes.
231 uint64_t zfs_max_missing_tvds_scan
= 0;
234 * Debugging aid that pauses spa_sync() towards the end.
236 boolean_t zfs_pause_spa_sync
= B_FALSE
;
239 * Variables to indicate the livelist condense zthr func should wait at certain
240 * points for the livelist to be removed - used to test condense/destroy races
242 int zfs_livelist_condense_zthr_pause
= 0;
243 int zfs_livelist_condense_sync_pause
= 0;
246 * Variables to track whether or not condense cancellation has been
247 * triggered in testing.
249 int zfs_livelist_condense_sync_cancel
= 0;
250 int zfs_livelist_condense_zthr_cancel
= 0;
253 * Variable to track whether or not extra ALLOC blkptrs were added to a
254 * livelist entry while it was being condensed (caused by the way we track
255 * remapped blkptrs in dbuf_remap_impl)
257 int zfs_livelist_condense_new_alloc
= 0;
260 * ==========================================================================
261 * SPA properties routines
262 * ==========================================================================
266 * Add a (source=src, propname=propval) list to an nvlist.
269 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
270 uint64_t intval
, zprop_source_t src
)
272 const char *propname
= zpool_prop_to_name(prop
);
275 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
276 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
279 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
281 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
283 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
284 nvlist_free(propval
);
288 * Get property values from the spa configuration.
291 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
293 vdev_t
*rvd
= spa
->spa_root_vdev
;
294 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
295 uint64_t size
, alloc
, cap
, version
;
296 const zprop_source_t src
= ZPROP_SRC_NONE
;
297 spa_config_dirent_t
*dp
;
298 metaslab_class_t
*mc
= spa_normal_class(spa
);
300 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
303 alloc
= metaslab_class_get_alloc(mc
);
304 alloc
+= metaslab_class_get_alloc(spa_special_class(spa
));
305 alloc
+= metaslab_class_get_alloc(spa_dedup_class(spa
));
306 alloc
+= metaslab_class_get_alloc(spa_embedded_log_class(spa
));
308 size
= metaslab_class_get_space(mc
);
309 size
+= metaslab_class_get_space(spa_special_class(spa
));
310 size
+= metaslab_class_get_space(spa_dedup_class(spa
));
311 size
+= metaslab_class_get_space(spa_embedded_log_class(spa
));
313 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
314 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
315 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
316 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
318 spa_prop_add_list(*nvp
, ZPOOL_PROP_CHECKPOINT
, NULL
,
319 spa
->spa_checkpoint_info
.sci_dspace
, src
);
321 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
322 metaslab_class_fragmentation(mc
), src
);
323 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
324 metaslab_class_expandable_space(mc
), src
);
325 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
326 (spa_mode(spa
) == SPA_MODE_READ
), src
);
328 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
329 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
331 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
332 ddt_get_pool_dedup_ratio(spa
), src
);
334 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
335 rvd
->vdev_state
, src
);
337 version
= spa_version(spa
);
338 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
339 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
340 version
, ZPROP_SRC_DEFAULT
);
342 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
343 version
, ZPROP_SRC_LOCAL
);
345 spa_prop_add_list(*nvp
, ZPOOL_PROP_LOAD_GUID
,
346 NULL
, spa_load_guid(spa
), src
);
351 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
352 * when opening pools before this version freedir will be NULL.
354 if (pool
->dp_free_dir
!= NULL
) {
355 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
356 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
359 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
363 if (pool
->dp_leak_dir
!= NULL
) {
364 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
365 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
368 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
373 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
375 if (spa
->spa_comment
!= NULL
) {
376 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
380 if (spa
->spa_root
!= NULL
)
381 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
384 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
385 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
386 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
388 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
389 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
392 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
393 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
394 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
396 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
397 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
400 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
401 if (dp
->scd_path
== NULL
) {
402 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
403 "none", 0, ZPROP_SRC_LOCAL
);
404 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
405 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
406 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
412 * Get zpool property values.
415 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
417 objset_t
*mos
= spa
->spa_meta_objset
;
423 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
427 dp
= spa_get_dsl(spa
);
428 dsl_pool_config_enter(dp
, FTAG
);
429 mutex_enter(&spa
->spa_props_lock
);
432 * Get properties from the spa config.
434 spa_prop_get_config(spa
, nvp
);
436 /* If no pool property object, no more prop to get. */
437 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0)
441 * Get properties from the MOS pool property object.
443 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
444 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
445 zap_cursor_advance(&zc
)) {
448 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
451 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPOOL_PROP_INVAL
)
454 switch (za
.za_integer_length
) {
456 /* integer property */
457 if (za
.za_first_integer
!=
458 zpool_prop_default_numeric(prop
))
459 src
= ZPROP_SRC_LOCAL
;
461 if (prop
== ZPOOL_PROP_BOOTFS
) {
462 dsl_dataset_t
*ds
= NULL
;
464 err
= dsl_dataset_hold_obj(dp
,
465 za
.za_first_integer
, FTAG
, &ds
);
469 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
471 dsl_dataset_name(ds
, strval
);
472 dsl_dataset_rele(ds
, FTAG
);
475 intval
= za
.za_first_integer
;
478 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
481 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
486 /* string property */
487 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
488 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
489 za
.za_name
, 1, za
.za_num_integers
, strval
);
491 kmem_free(strval
, za
.za_num_integers
);
494 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
495 kmem_free(strval
, za
.za_num_integers
);
502 zap_cursor_fini(&zc
);
504 mutex_exit(&spa
->spa_props_lock
);
505 dsl_pool_config_exit(dp
, FTAG
);
506 if (err
&& err
!= ENOENT
) {
516 * Validate the given pool properties nvlist and modify the list
517 * for the property values to be set.
520 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
523 int error
= 0, reset_bootfs
= 0;
525 boolean_t has_feature
= B_FALSE
;
528 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
530 char *strval
, *slash
, *check
, *fname
;
531 const char *propname
= nvpair_name(elem
);
532 zpool_prop_t prop
= zpool_name_to_prop(propname
);
535 case ZPOOL_PROP_INVAL
:
536 if (!zpool_prop_feature(propname
)) {
537 error
= SET_ERROR(EINVAL
);
542 * Sanitize the input.
544 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
545 error
= SET_ERROR(EINVAL
);
549 if (nvpair_value_uint64(elem
, &intval
) != 0) {
550 error
= SET_ERROR(EINVAL
);
555 error
= SET_ERROR(EINVAL
);
559 fname
= strchr(propname
, '@') + 1;
560 if (zfeature_lookup_name(fname
, NULL
) != 0) {
561 error
= SET_ERROR(EINVAL
);
565 has_feature
= B_TRUE
;
568 case ZPOOL_PROP_VERSION
:
569 error
= nvpair_value_uint64(elem
, &intval
);
571 (intval
< spa_version(spa
) ||
572 intval
> SPA_VERSION_BEFORE_FEATURES
||
574 error
= SET_ERROR(EINVAL
);
577 case ZPOOL_PROP_DELEGATION
:
578 case ZPOOL_PROP_AUTOREPLACE
:
579 case ZPOOL_PROP_LISTSNAPS
:
580 case ZPOOL_PROP_AUTOEXPAND
:
581 case ZPOOL_PROP_AUTOTRIM
:
582 error
= nvpair_value_uint64(elem
, &intval
);
583 if (!error
&& intval
> 1)
584 error
= SET_ERROR(EINVAL
);
587 case ZPOOL_PROP_MULTIHOST
:
588 error
= nvpair_value_uint64(elem
, &intval
);
589 if (!error
&& intval
> 1)
590 error
= SET_ERROR(EINVAL
);
593 uint32_t hostid
= zone_get_hostid(NULL
);
595 spa
->spa_hostid
= hostid
;
597 error
= SET_ERROR(ENOTSUP
);
602 case ZPOOL_PROP_BOOTFS
:
604 * If the pool version is less than SPA_VERSION_BOOTFS,
605 * or the pool is still being created (version == 0),
606 * the bootfs property cannot be set.
608 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
609 error
= SET_ERROR(ENOTSUP
);
614 * Make sure the vdev config is bootable
616 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
617 error
= SET_ERROR(ENOTSUP
);
623 error
= nvpair_value_string(elem
, &strval
);
628 if (strval
== NULL
|| strval
[0] == '\0') {
629 objnum
= zpool_prop_default_numeric(
634 error
= dmu_objset_hold(strval
, FTAG
, &os
);
639 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
640 error
= SET_ERROR(ENOTSUP
);
642 objnum
= dmu_objset_id(os
);
644 dmu_objset_rele(os
, FTAG
);
648 case ZPOOL_PROP_FAILUREMODE
:
649 error
= nvpair_value_uint64(elem
, &intval
);
650 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
651 error
= SET_ERROR(EINVAL
);
654 * This is a special case which only occurs when
655 * the pool has completely failed. This allows
656 * the user to change the in-core failmode property
657 * without syncing it out to disk (I/Os might
658 * currently be blocked). We do this by returning
659 * EIO to the caller (spa_prop_set) to trick it
660 * into thinking we encountered a property validation
663 if (!error
&& spa_suspended(spa
)) {
664 spa
->spa_failmode
= intval
;
665 error
= SET_ERROR(EIO
);
669 case ZPOOL_PROP_CACHEFILE
:
670 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
673 if (strval
[0] == '\0')
676 if (strcmp(strval
, "none") == 0)
679 if (strval
[0] != '/') {
680 error
= SET_ERROR(EINVAL
);
684 slash
= strrchr(strval
, '/');
685 ASSERT(slash
!= NULL
);
687 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
688 strcmp(slash
, "/..") == 0)
689 error
= SET_ERROR(EINVAL
);
692 case ZPOOL_PROP_COMMENT
:
693 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
695 for (check
= strval
; *check
!= '\0'; check
++) {
696 if (!isprint(*check
)) {
697 error
= SET_ERROR(EINVAL
);
701 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
702 error
= SET_ERROR(E2BIG
);
713 (void) nvlist_remove_all(props
,
714 zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO
));
716 if (!error
&& reset_bootfs
) {
717 error
= nvlist_remove(props
,
718 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
721 error
= nvlist_add_uint64(props
,
722 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
730 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
733 spa_config_dirent_t
*dp
;
735 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
739 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
742 if (cachefile
[0] == '\0')
743 dp
->scd_path
= spa_strdup(spa_config_path
);
744 else if (strcmp(cachefile
, "none") == 0)
747 dp
->scd_path
= spa_strdup(cachefile
);
749 list_insert_head(&spa
->spa_config_list
, dp
);
751 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
755 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
758 nvpair_t
*elem
= NULL
;
759 boolean_t need_sync
= B_FALSE
;
761 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
764 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
765 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
767 if (prop
== ZPOOL_PROP_CACHEFILE
||
768 prop
== ZPOOL_PROP_ALTROOT
||
769 prop
== ZPOOL_PROP_READONLY
)
772 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
775 if (prop
== ZPOOL_PROP_VERSION
) {
776 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
778 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
779 ver
= SPA_VERSION_FEATURES
;
783 /* Save time if the version is already set. */
784 if (ver
== spa_version(spa
))
788 * In addition to the pool directory object, we might
789 * create the pool properties object, the features for
790 * read object, the features for write object, or the
791 * feature descriptions object.
793 error
= dsl_sync_task(spa
->spa_name
, NULL
,
794 spa_sync_version
, &ver
,
795 6, ZFS_SPACE_CHECK_RESERVED
);
806 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
807 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
814 * If the bootfs property value is dsobj, clear it.
817 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
819 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
820 VERIFY(zap_remove(spa
->spa_meta_objset
,
821 spa
->spa_pool_props_object
,
822 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
829 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
831 uint64_t *newguid __maybe_unused
= arg
;
832 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
833 vdev_t
*rvd
= spa
->spa_root_vdev
;
836 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
837 int error
= (spa_has_checkpoint(spa
)) ?
838 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
839 return (SET_ERROR(error
));
842 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
843 vdev_state
= rvd
->vdev_state
;
844 spa_config_exit(spa
, SCL_STATE
, FTAG
);
846 if (vdev_state
!= VDEV_STATE_HEALTHY
)
847 return (SET_ERROR(ENXIO
));
849 ASSERT3U(spa_guid(spa
), !=, *newguid
);
855 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
857 uint64_t *newguid
= arg
;
858 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
860 vdev_t
*rvd
= spa
->spa_root_vdev
;
862 oldguid
= spa_guid(spa
);
864 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
865 rvd
->vdev_guid
= *newguid
;
866 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
867 vdev_config_dirty(rvd
);
868 spa_config_exit(spa
, SCL_STATE
, FTAG
);
870 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
871 (u_longlong_t
)oldguid
, (u_longlong_t
)*newguid
);
875 * Change the GUID for the pool. This is done so that we can later
876 * re-import a pool built from a clone of our own vdevs. We will modify
877 * the root vdev's guid, our own pool guid, and then mark all of our
878 * vdevs dirty. Note that we must make sure that all our vdevs are
879 * online when we do this, or else any vdevs that weren't present
880 * would be orphaned from our pool. We are also going to issue a
881 * sysevent to update any watchers.
884 spa_change_guid(spa_t
*spa
)
889 mutex_enter(&spa
->spa_vdev_top_lock
);
890 mutex_enter(&spa_namespace_lock
);
891 guid
= spa_generate_guid(NULL
);
893 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
894 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
897 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
898 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
901 mutex_exit(&spa_namespace_lock
);
902 mutex_exit(&spa
->spa_vdev_top_lock
);
908 * ==========================================================================
909 * SPA state manipulation (open/create/destroy/import/export)
910 * ==========================================================================
914 spa_error_entry_compare(const void *a
, const void *b
)
916 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
917 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
920 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
921 sizeof (zbookmark_phys_t
));
923 return (TREE_ISIGN(ret
));
927 * Utility function which retrieves copies of the current logs and
928 * re-initializes them in the process.
931 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
933 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
935 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
936 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
938 avl_create(&spa
->spa_errlist_scrub
,
939 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
940 offsetof(spa_error_entry_t
, se_avl
));
941 avl_create(&spa
->spa_errlist_last
,
942 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
943 offsetof(spa_error_entry_t
, se_avl
));
947 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
949 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
950 enum zti_modes mode
= ztip
->zti_mode
;
951 uint_t value
= ztip
->zti_value
;
952 uint_t count
= ztip
->zti_count
;
953 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
955 boolean_t batch
= B_FALSE
;
957 if (mode
== ZTI_MODE_NULL
) {
959 tqs
->stqs_taskq
= NULL
;
963 ASSERT3U(count
, >, 0);
965 tqs
->stqs_count
= count
;
966 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
970 ASSERT3U(value
, >=, 1);
971 value
= MAX(value
, 1);
972 flags
|= TASKQ_DYNAMIC
;
977 flags
|= TASKQ_THREADS_CPU_PCT
;
978 value
= MIN(zio_taskq_batch_pct
, 100);
982 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
984 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
988 for (uint_t i
= 0; i
< count
; i
++) {
992 (void) snprintf(name
, sizeof (name
), "%s_%s",
993 zio_type_name
[t
], zio_taskq_types
[q
]);
995 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
997 flags
|= TASKQ_DC_BATCH
;
999 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
1000 spa
->spa_proc
, zio_taskq_basedc
, flags
);
1002 pri_t pri
= maxclsyspri
;
1004 * The write issue taskq can be extremely CPU
1005 * intensive. Run it at slightly less important
1006 * priority than the other taskqs.
1008 * Under Linux and FreeBSD this means incrementing
1009 * the priority value as opposed to platforms like
1010 * illumos where it should be decremented.
1012 * On FreeBSD, if priorities divided by four (RQ_PPQ)
1013 * are equal then a difference between them is
1016 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
) {
1017 #if defined(__linux__)
1019 #elif defined(__FreeBSD__)
1025 tq
= taskq_create_proc(name
, value
, pri
, 50,
1026 INT_MAX
, spa
->spa_proc
, flags
);
1029 tqs
->stqs_taskq
[i
] = tq
;
1034 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1036 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1038 if (tqs
->stqs_taskq
== NULL
) {
1039 ASSERT3U(tqs
->stqs_count
, ==, 0);
1043 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
1044 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
1045 taskq_destroy(tqs
->stqs_taskq
[i
]);
1048 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
1049 tqs
->stqs_taskq
= NULL
;
1053 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1054 * Note that a type may have multiple discrete taskqs to avoid lock contention
1055 * on the taskq itself. In that case we choose which taskq at random by using
1056 * the low bits of gethrtime().
1059 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1060 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
1062 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1065 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1066 ASSERT3U(tqs
->stqs_count
, !=, 0);
1068 if (tqs
->stqs_count
== 1) {
1069 tq
= tqs
->stqs_taskq
[0];
1071 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1074 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
1078 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
1081 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1082 task_func_t
*func
, void *arg
, uint_t flags
)
1084 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1088 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1089 ASSERT3U(tqs
->stqs_count
, !=, 0);
1091 if (tqs
->stqs_count
== 1) {
1092 tq
= tqs
->stqs_taskq
[0];
1094 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1097 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1099 taskq_wait_id(tq
, id
);
1103 spa_create_zio_taskqs(spa_t
*spa
)
1105 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1106 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1107 spa_taskqs_init(spa
, t
, q
);
1113 * Disabled until spa_thread() can be adapted for Linux.
1115 #undef HAVE_SPA_THREAD
1117 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1119 spa_thread(void *arg
)
1121 psetid_t zio_taskq_psrset_bind
= PS_NONE
;
1122 callb_cpr_t cprinfo
;
1125 user_t
*pu
= PTOU(curproc
);
1127 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1130 ASSERT(curproc
!= &p0
);
1131 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1132 "zpool-%s", spa
->spa_name
);
1133 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1135 /* bind this thread to the requested psrset */
1136 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1138 mutex_enter(&cpu_lock
);
1139 mutex_enter(&pidlock
);
1140 mutex_enter(&curproc
->p_lock
);
1142 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1143 0, NULL
, NULL
) == 0) {
1144 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1147 "Couldn't bind process for zfs pool \"%s\" to "
1148 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1151 mutex_exit(&curproc
->p_lock
);
1152 mutex_exit(&pidlock
);
1153 mutex_exit(&cpu_lock
);
1157 if (zio_taskq_sysdc
) {
1158 sysdc_thread_enter(curthread
, 100, 0);
1161 spa
->spa_proc
= curproc
;
1162 spa
->spa_did
= curthread
->t_did
;
1164 spa_create_zio_taskqs(spa
);
1166 mutex_enter(&spa
->spa_proc_lock
);
1167 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1169 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1170 cv_broadcast(&spa
->spa_proc_cv
);
1172 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1173 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1174 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1175 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1177 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1178 spa
->spa_proc_state
= SPA_PROC_GONE
;
1179 spa
->spa_proc
= &p0
;
1180 cv_broadcast(&spa
->spa_proc_cv
);
1181 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1183 mutex_enter(&curproc
->p_lock
);
1189 * Activate an uninitialized pool.
1192 spa_activate(spa_t
*spa
, spa_mode_t mode
)
1194 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1196 spa
->spa_state
= POOL_STATE_ACTIVE
;
1197 spa
->spa_mode
= mode
;
1199 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1200 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1201 spa
->spa_embedded_log_class
=
1202 metaslab_class_create(spa
, zfs_metaslab_ops
);
1203 spa
->spa_special_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1204 spa
->spa_dedup_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1206 /* Try to create a covering process */
1207 mutex_enter(&spa
->spa_proc_lock
);
1208 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1209 ASSERT(spa
->spa_proc
== &p0
);
1212 #ifdef HAVE_SPA_THREAD
1213 /* Only create a process if we're going to be around a while. */
1214 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1215 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1217 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1218 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1219 cv_wait(&spa
->spa_proc_cv
,
1220 &spa
->spa_proc_lock
);
1222 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1223 ASSERT(spa
->spa_proc
!= &p0
);
1224 ASSERT(spa
->spa_did
!= 0);
1228 "Couldn't create process for zfs pool \"%s\"\n",
1233 #endif /* HAVE_SPA_THREAD */
1234 mutex_exit(&spa
->spa_proc_lock
);
1236 /* If we didn't create a process, we need to create our taskqs. */
1237 if (spa
->spa_proc
== &p0
) {
1238 spa_create_zio_taskqs(spa
);
1241 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1242 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
,
1246 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1247 offsetof(vdev_t
, vdev_config_dirty_node
));
1248 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1249 offsetof(objset_t
, os_evicting_node
));
1250 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1251 offsetof(vdev_t
, vdev_state_dirty_node
));
1253 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1254 offsetof(struct vdev
, vdev_txg_node
));
1256 avl_create(&spa
->spa_errlist_scrub
,
1257 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1258 offsetof(spa_error_entry_t
, se_avl
));
1259 avl_create(&spa
->spa_errlist_last
,
1260 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1261 offsetof(spa_error_entry_t
, se_avl
));
1263 spa_keystore_init(&spa
->spa_keystore
);
1266 * This taskq is used to perform zvol-minor-related tasks
1267 * asynchronously. This has several advantages, including easy
1268 * resolution of various deadlocks.
1270 * The taskq must be single threaded to ensure tasks are always
1271 * processed in the order in which they were dispatched.
1273 * A taskq per pool allows one to keep the pools independent.
1274 * This way if one pool is suspended, it will not impact another.
1276 * The preferred location to dispatch a zvol minor task is a sync
1277 * task. In this context, there is easy access to the spa_t and minimal
1278 * error handling is required because the sync task must succeed.
1280 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1284 * Taskq dedicated to prefetcher threads: this is used to prevent the
1285 * pool traverse code from monopolizing the global (and limited)
1286 * system_taskq by inappropriately scheduling long running tasks on it.
1288 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", 100,
1289 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1292 * The taskq to upgrade datasets in this pool. Currently used by
1293 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1295 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", 100,
1296 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1300 * Opposite of spa_activate().
1303 spa_deactivate(spa_t
*spa
)
1305 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1306 ASSERT(spa
->spa_dsl_pool
== NULL
);
1307 ASSERT(spa
->spa_root_vdev
== NULL
);
1308 ASSERT(spa
->spa_async_zio_root
== NULL
);
1309 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1311 spa_evicting_os_wait(spa
);
1313 if (spa
->spa_zvol_taskq
) {
1314 taskq_destroy(spa
->spa_zvol_taskq
);
1315 spa
->spa_zvol_taskq
= NULL
;
1318 if (spa
->spa_prefetch_taskq
) {
1319 taskq_destroy(spa
->spa_prefetch_taskq
);
1320 spa
->spa_prefetch_taskq
= NULL
;
1323 if (spa
->spa_upgrade_taskq
) {
1324 taskq_destroy(spa
->spa_upgrade_taskq
);
1325 spa
->spa_upgrade_taskq
= NULL
;
1328 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1330 list_destroy(&spa
->spa_config_dirty_list
);
1331 list_destroy(&spa
->spa_evicting_os_list
);
1332 list_destroy(&spa
->spa_state_dirty_list
);
1334 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1336 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1337 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1338 spa_taskqs_fini(spa
, t
, q
);
1342 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1343 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1344 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1345 spa
->spa_txg_zio
[i
] = NULL
;
1348 metaslab_class_destroy(spa
->spa_normal_class
);
1349 spa
->spa_normal_class
= NULL
;
1351 metaslab_class_destroy(spa
->spa_log_class
);
1352 spa
->spa_log_class
= NULL
;
1354 metaslab_class_destroy(spa
->spa_embedded_log_class
);
1355 spa
->spa_embedded_log_class
= NULL
;
1357 metaslab_class_destroy(spa
->spa_special_class
);
1358 spa
->spa_special_class
= NULL
;
1360 metaslab_class_destroy(spa
->spa_dedup_class
);
1361 spa
->spa_dedup_class
= NULL
;
1364 * If this was part of an import or the open otherwise failed, we may
1365 * still have errors left in the queues. Empty them just in case.
1367 spa_errlog_drain(spa
);
1368 avl_destroy(&spa
->spa_errlist_scrub
);
1369 avl_destroy(&spa
->spa_errlist_last
);
1371 spa_keystore_fini(&spa
->spa_keystore
);
1373 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1375 mutex_enter(&spa
->spa_proc_lock
);
1376 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1377 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1378 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1379 cv_broadcast(&spa
->spa_proc_cv
);
1380 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1381 ASSERT(spa
->spa_proc
!= &p0
);
1382 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1384 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1385 spa
->spa_proc_state
= SPA_PROC_NONE
;
1387 ASSERT(spa
->spa_proc
== &p0
);
1388 mutex_exit(&spa
->spa_proc_lock
);
1391 * We want to make sure spa_thread() has actually exited the ZFS
1392 * module, so that the module can't be unloaded out from underneath
1395 if (spa
->spa_did
!= 0) {
1396 thread_join(spa
->spa_did
);
1402 * Verify a pool configuration, and construct the vdev tree appropriately. This
1403 * will create all the necessary vdevs in the appropriate layout, with each vdev
1404 * in the CLOSED state. This will prep the pool before open/creation/import.
1405 * All vdev validation is done by the vdev_alloc() routine.
1408 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1409 uint_t id
, int atype
)
1415 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1418 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1421 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1424 if (error
== ENOENT
)
1430 return (SET_ERROR(EINVAL
));
1433 for (int c
= 0; c
< children
; c
++) {
1435 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1443 ASSERT(*vdp
!= NULL
);
1449 spa_should_flush_logs_on_unload(spa_t
*spa
)
1451 if (!spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
))
1454 if (!spa_writeable(spa
))
1457 if (!spa
->spa_sync_on
)
1460 if (spa_state(spa
) != POOL_STATE_EXPORTED
)
1463 if (zfs_keep_log_spacemaps_at_export
)
1470 * Opens a transaction that will set the flag that will instruct
1471 * spa_sync to attempt to flush all the metaslabs for that txg.
1474 spa_unload_log_sm_flush_all(spa_t
*spa
)
1476 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
1477 VERIFY0(dmu_tx_assign(tx
, TXG_WAIT
));
1479 ASSERT3U(spa
->spa_log_flushall_txg
, ==, 0);
1480 spa
->spa_log_flushall_txg
= dmu_tx_get_txg(tx
);
1483 txg_wait_synced(spa_get_dsl(spa
), spa
->spa_log_flushall_txg
);
1487 spa_unload_log_sm_metadata(spa_t
*spa
)
1489 void *cookie
= NULL
;
1491 while ((sls
= avl_destroy_nodes(&spa
->spa_sm_logs_by_txg
,
1492 &cookie
)) != NULL
) {
1493 VERIFY0(sls
->sls_mscount
);
1494 kmem_free(sls
, sizeof (spa_log_sm_t
));
1497 for (log_summary_entry_t
*e
= list_head(&spa
->spa_log_summary
);
1498 e
!= NULL
; e
= list_head(&spa
->spa_log_summary
)) {
1499 VERIFY0(e
->lse_mscount
);
1500 list_remove(&spa
->spa_log_summary
, e
);
1501 kmem_free(e
, sizeof (log_summary_entry_t
));
1504 spa
->spa_unflushed_stats
.sus_nblocks
= 0;
1505 spa
->spa_unflushed_stats
.sus_memused
= 0;
1506 spa
->spa_unflushed_stats
.sus_blocklimit
= 0;
1510 spa_destroy_aux_threads(spa_t
*spa
)
1512 if (spa
->spa_condense_zthr
!= NULL
) {
1513 zthr_destroy(spa
->spa_condense_zthr
);
1514 spa
->spa_condense_zthr
= NULL
;
1516 if (spa
->spa_checkpoint_discard_zthr
!= NULL
) {
1517 zthr_destroy(spa
->spa_checkpoint_discard_zthr
);
1518 spa
->spa_checkpoint_discard_zthr
= NULL
;
1520 if (spa
->spa_livelist_delete_zthr
!= NULL
) {
1521 zthr_destroy(spa
->spa_livelist_delete_zthr
);
1522 spa
->spa_livelist_delete_zthr
= NULL
;
1524 if (spa
->spa_livelist_condense_zthr
!= NULL
) {
1525 zthr_destroy(spa
->spa_livelist_condense_zthr
);
1526 spa
->spa_livelist_condense_zthr
= NULL
;
1531 * Opposite of spa_load().
1534 spa_unload(spa_t
*spa
)
1536 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1537 ASSERT(spa_state(spa
) != POOL_STATE_UNINITIALIZED
);
1539 spa_import_progress_remove(spa_guid(spa
));
1540 spa_load_note(spa
, "UNLOADING");
1542 spa_wake_waiters(spa
);
1545 * If the log space map feature is enabled and the pool is getting
1546 * exported (but not destroyed), we want to spend some time flushing
1547 * as many metaslabs as we can in an attempt to destroy log space
1548 * maps and save import time.
1550 if (spa_should_flush_logs_on_unload(spa
))
1551 spa_unload_log_sm_flush_all(spa
);
1556 spa_async_suspend(spa
);
1558 if (spa
->spa_root_vdev
) {
1559 vdev_t
*root_vdev
= spa
->spa_root_vdev
;
1560 vdev_initialize_stop_all(root_vdev
, VDEV_INITIALIZE_ACTIVE
);
1561 vdev_trim_stop_all(root_vdev
, VDEV_TRIM_ACTIVE
);
1562 vdev_autotrim_stop_all(spa
);
1563 vdev_rebuild_stop_all(spa
);
1569 if (spa
->spa_sync_on
) {
1570 txg_sync_stop(spa
->spa_dsl_pool
);
1571 spa
->spa_sync_on
= B_FALSE
;
1575 * This ensures that there is no async metaslab prefetching
1576 * while we attempt to unload the spa.
1578 if (spa
->spa_root_vdev
!= NULL
) {
1579 for (int c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++) {
1580 vdev_t
*vc
= spa
->spa_root_vdev
->vdev_child
[c
];
1581 if (vc
->vdev_mg
!= NULL
)
1582 taskq_wait(vc
->vdev_mg
->mg_taskq
);
1586 if (spa
->spa_mmp
.mmp_thread
)
1587 mmp_thread_stop(spa
);
1590 * Wait for any outstanding async I/O to complete.
1592 if (spa
->spa_async_zio_root
!= NULL
) {
1593 for (int i
= 0; i
< max_ncpus
; i
++)
1594 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1595 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1596 spa
->spa_async_zio_root
= NULL
;
1599 if (spa
->spa_vdev_removal
!= NULL
) {
1600 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
1601 spa
->spa_vdev_removal
= NULL
;
1604 spa_destroy_aux_threads(spa
);
1606 spa_condense_fini(spa
);
1608 bpobj_close(&spa
->spa_deferred_bpobj
);
1610 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1615 if (spa
->spa_root_vdev
)
1616 vdev_free(spa
->spa_root_vdev
);
1617 ASSERT(spa
->spa_root_vdev
== NULL
);
1620 * Close the dsl pool.
1622 if (spa
->spa_dsl_pool
) {
1623 dsl_pool_close(spa
->spa_dsl_pool
);
1624 spa
->spa_dsl_pool
= NULL
;
1625 spa
->spa_meta_objset
= NULL
;
1629 spa_unload_log_sm_metadata(spa
);
1632 * Drop and purge level 2 cache
1634 spa_l2cache_drop(spa
);
1636 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1637 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1638 if (spa
->spa_spares
.sav_vdevs
) {
1639 kmem_free(spa
->spa_spares
.sav_vdevs
,
1640 spa
->spa_spares
.sav_count
* sizeof (void *));
1641 spa
->spa_spares
.sav_vdevs
= NULL
;
1643 if (spa
->spa_spares
.sav_config
) {
1644 nvlist_free(spa
->spa_spares
.sav_config
);
1645 spa
->spa_spares
.sav_config
= NULL
;
1647 spa
->spa_spares
.sav_count
= 0;
1649 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1650 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1651 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1653 if (spa
->spa_l2cache
.sav_vdevs
) {
1654 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1655 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1656 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1658 if (spa
->spa_l2cache
.sav_config
) {
1659 nvlist_free(spa
->spa_l2cache
.sav_config
);
1660 spa
->spa_l2cache
.sav_config
= NULL
;
1662 spa
->spa_l2cache
.sav_count
= 0;
1664 spa
->spa_async_suspended
= 0;
1666 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
1668 if (spa
->spa_comment
!= NULL
) {
1669 spa_strfree(spa
->spa_comment
);
1670 spa
->spa_comment
= NULL
;
1673 spa_config_exit(spa
, SCL_ALL
, spa
);
1677 * Load (or re-load) the current list of vdevs describing the active spares for
1678 * this pool. When this is called, we have some form of basic information in
1679 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1680 * then re-generate a more complete list including status information.
1683 spa_load_spares(spa_t
*spa
)
1692 * zdb opens both the current state of the pool and the
1693 * checkpointed state (if present), with a different spa_t.
1695 * As spare vdevs are shared among open pools, we skip loading
1696 * them when we load the checkpointed state of the pool.
1698 if (!spa_writeable(spa
))
1702 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1705 * First, close and free any existing spare vdevs.
1707 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1708 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1710 /* Undo the call to spa_activate() below */
1711 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1712 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1713 spa_spare_remove(tvd
);
1718 if (spa
->spa_spares
.sav_vdevs
)
1719 kmem_free(spa
->spa_spares
.sav_vdevs
,
1720 spa
->spa_spares
.sav_count
* sizeof (void *));
1722 if (spa
->spa_spares
.sav_config
== NULL
)
1725 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1726 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1728 spa
->spa_spares
.sav_count
= (int)nspares
;
1729 spa
->spa_spares
.sav_vdevs
= NULL
;
1735 * Construct the array of vdevs, opening them to get status in the
1736 * process. For each spare, there is potentially two different vdev_t
1737 * structures associated with it: one in the list of spares (used only
1738 * for basic validation purposes) and one in the active vdev
1739 * configuration (if it's spared in). During this phase we open and
1740 * validate each vdev on the spare list. If the vdev also exists in the
1741 * active configuration, then we also mark this vdev as an active spare.
1743 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1745 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1746 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1747 VDEV_ALLOC_SPARE
) == 0);
1750 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1752 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1753 B_FALSE
)) != NULL
) {
1754 if (!tvd
->vdev_isspare
)
1758 * We only mark the spare active if we were successfully
1759 * able to load the vdev. Otherwise, importing a pool
1760 * with a bad active spare would result in strange
1761 * behavior, because multiple pool would think the spare
1762 * is actively in use.
1764 * There is a vulnerability here to an equally bizarre
1765 * circumstance, where a dead active spare is later
1766 * brought back to life (onlined or otherwise). Given
1767 * the rarity of this scenario, and the extra complexity
1768 * it adds, we ignore the possibility.
1770 if (!vdev_is_dead(tvd
))
1771 spa_spare_activate(tvd
);
1775 vd
->vdev_aux
= &spa
->spa_spares
;
1777 if (vdev_open(vd
) != 0)
1780 if (vdev_validate_aux(vd
) == 0)
1785 * Recompute the stashed list of spares, with status information
1788 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1789 DATA_TYPE_NVLIST_ARRAY
) == 0);
1791 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1793 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1794 spares
[i
] = vdev_config_generate(spa
,
1795 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1796 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1797 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1798 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1799 nvlist_free(spares
[i
]);
1800 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1804 * Load (or re-load) the current list of vdevs describing the active l2cache for
1805 * this pool. When this is called, we have some form of basic information in
1806 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1807 * then re-generate a more complete list including status information.
1808 * Devices which are already active have their details maintained, and are
1812 spa_load_l2cache(spa_t
*spa
)
1814 nvlist_t
**l2cache
= NULL
;
1816 int i
, j
, oldnvdevs
;
1818 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1819 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1823 * zdb opens both the current state of the pool and the
1824 * checkpointed state (if present), with a different spa_t.
1826 * As L2 caches are part of the ARC which is shared among open
1827 * pools, we skip loading them when we load the checkpointed
1828 * state of the pool.
1830 if (!spa_writeable(spa
))
1834 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1836 oldvdevs
= sav
->sav_vdevs
;
1837 oldnvdevs
= sav
->sav_count
;
1838 sav
->sav_vdevs
= NULL
;
1841 if (sav
->sav_config
== NULL
) {
1847 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1848 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1849 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1852 * Process new nvlist of vdevs.
1854 for (i
= 0; i
< nl2cache
; i
++) {
1855 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1859 for (j
= 0; j
< oldnvdevs
; j
++) {
1861 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1863 * Retain previous vdev for add/remove ops.
1871 if (newvdevs
[i
] == NULL
) {
1875 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1876 VDEV_ALLOC_L2CACHE
) == 0);
1881 * Commit this vdev as an l2cache device,
1882 * even if it fails to open.
1884 spa_l2cache_add(vd
);
1889 spa_l2cache_activate(vd
);
1891 if (vdev_open(vd
) != 0)
1894 (void) vdev_validate_aux(vd
);
1896 if (!vdev_is_dead(vd
))
1897 l2arc_add_vdev(spa
, vd
);
1900 * Upon cache device addition to a pool or pool
1901 * creation with a cache device or if the header
1902 * of the device is invalid we issue an async
1903 * TRIM command for the whole device which will
1904 * execute if l2arc_trim_ahead > 0.
1906 spa_async_request(spa
, SPA_ASYNC_L2CACHE_TRIM
);
1910 sav
->sav_vdevs
= newvdevs
;
1911 sav
->sav_count
= (int)nl2cache
;
1914 * Recompute the stashed list of l2cache devices, with status
1915 * information this time.
1917 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1918 DATA_TYPE_NVLIST_ARRAY
) == 0);
1920 if (sav
->sav_count
> 0)
1921 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
1923 for (i
= 0; i
< sav
->sav_count
; i
++)
1924 l2cache
[i
] = vdev_config_generate(spa
,
1925 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1926 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1927 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1931 * Purge vdevs that were dropped
1933 for (i
= 0; i
< oldnvdevs
; i
++) {
1938 ASSERT(vd
->vdev_isl2cache
);
1940 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1941 pool
!= 0ULL && l2arc_vdev_present(vd
))
1942 l2arc_remove_vdev(vd
);
1943 vdev_clear_stats(vd
);
1949 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1951 for (i
= 0; i
< sav
->sav_count
; i
++)
1952 nvlist_free(l2cache
[i
]);
1954 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1958 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1961 char *packed
= NULL
;
1966 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1970 nvsize
= *(uint64_t *)db
->db_data
;
1971 dmu_buf_rele(db
, FTAG
);
1973 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1974 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1977 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1978 vmem_free(packed
, nvsize
);
1984 * Concrete top-level vdevs that are not missing and are not logs. At every
1985 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
1988 spa_healthy_core_tvds(spa_t
*spa
)
1990 vdev_t
*rvd
= spa
->spa_root_vdev
;
1993 for (uint64_t i
= 0; i
< rvd
->vdev_children
; i
++) {
1994 vdev_t
*vd
= rvd
->vdev_child
[i
];
1997 if (vdev_is_concrete(vd
) && !vdev_is_dead(vd
))
2005 * Checks to see if the given vdev could not be opened, in which case we post a
2006 * sysevent to notify the autoreplace code that the device has been removed.
2009 spa_check_removed(vdev_t
*vd
)
2011 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
2012 spa_check_removed(vd
->vdev_child
[c
]);
2014 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
2015 vdev_is_concrete(vd
)) {
2016 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
2017 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
2022 spa_check_for_missing_logs(spa_t
*spa
)
2024 vdev_t
*rvd
= spa
->spa_root_vdev
;
2027 * If we're doing a normal import, then build up any additional
2028 * diagnostic information about missing log devices.
2029 * We'll pass this up to the user for further processing.
2031 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
2032 nvlist_t
**child
, *nv
;
2035 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
2037 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2039 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2040 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2043 * We consider a device as missing only if it failed
2044 * to open (i.e. offline or faulted is not considered
2047 if (tvd
->vdev_islog
&&
2048 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2049 child
[idx
++] = vdev_config_generate(spa
, tvd
,
2050 B_FALSE
, VDEV_CONFIG_MISSING
);
2055 fnvlist_add_nvlist_array(nv
,
2056 ZPOOL_CONFIG_CHILDREN
, child
, idx
);
2057 fnvlist_add_nvlist(spa
->spa_load_info
,
2058 ZPOOL_CONFIG_MISSING_DEVICES
, nv
);
2060 for (uint64_t i
= 0; i
< idx
; i
++)
2061 nvlist_free(child
[i
]);
2064 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
2067 spa_load_failed(spa
, "some log devices are missing");
2068 vdev_dbgmsg_print_tree(rvd
, 2);
2069 return (SET_ERROR(ENXIO
));
2072 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2073 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2075 if (tvd
->vdev_islog
&&
2076 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2077 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2078 spa_load_note(spa
, "some log devices are "
2079 "missing, ZIL is dropped.");
2080 vdev_dbgmsg_print_tree(rvd
, 2);
2090 * Check for missing log devices
2093 spa_check_logs(spa_t
*spa
)
2095 boolean_t rv
= B_FALSE
;
2096 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2098 switch (spa
->spa_log_state
) {
2101 case SPA_LOG_MISSING
:
2102 /* need to recheck in case slog has been restored */
2103 case SPA_LOG_UNKNOWN
:
2104 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2105 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
2107 spa_set_log_state(spa
, SPA_LOG_MISSING
);
2114 * Passivate any log vdevs (note, does not apply to embedded log metaslabs).
2117 spa_passivate_log(spa_t
*spa
)
2119 vdev_t
*rvd
= spa
->spa_root_vdev
;
2120 boolean_t slog_found
= B_FALSE
;
2122 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2124 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2125 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2127 if (tvd
->vdev_islog
) {
2128 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2129 metaslab_group_passivate(tvd
->vdev_mg
);
2130 slog_found
= B_TRUE
;
2134 return (slog_found
);
2138 * Activate any log vdevs (note, does not apply to embedded log metaslabs).
2141 spa_activate_log(spa_t
*spa
)
2143 vdev_t
*rvd
= spa
->spa_root_vdev
;
2145 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2147 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2148 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2150 if (tvd
->vdev_islog
) {
2151 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2152 metaslab_group_activate(tvd
->vdev_mg
);
2158 spa_reset_logs(spa_t
*spa
)
2162 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
2163 NULL
, DS_FIND_CHILDREN
);
2166 * We successfully offlined the log device, sync out the
2167 * current txg so that the "stubby" block can be removed
2170 txg_wait_synced(spa
->spa_dsl_pool
, 0);
2176 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
2178 for (int i
= 0; i
< sav
->sav_count
; i
++)
2179 spa_check_removed(sav
->sav_vdevs
[i
]);
2183 spa_claim_notify(zio_t
*zio
)
2185 spa_t
*spa
= zio
->io_spa
;
2190 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2191 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
2192 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
2193 mutex_exit(&spa
->spa_props_lock
);
2196 typedef struct spa_load_error
{
2197 uint64_t sle_meta_count
;
2198 uint64_t sle_data_count
;
2202 spa_load_verify_done(zio_t
*zio
)
2204 blkptr_t
*bp
= zio
->io_bp
;
2205 spa_load_error_t
*sle
= zio
->io_private
;
2206 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2207 int error
= zio
->io_error
;
2208 spa_t
*spa
= zio
->io_spa
;
2210 abd_free(zio
->io_abd
);
2212 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2213 type
!= DMU_OT_INTENT_LOG
)
2214 atomic_inc_64(&sle
->sle_meta_count
);
2216 atomic_inc_64(&sle
->sle_data_count
);
2219 mutex_enter(&spa
->spa_scrub_lock
);
2220 spa
->spa_load_verify_bytes
-= BP_GET_PSIZE(bp
);
2221 cv_broadcast(&spa
->spa_scrub_io_cv
);
2222 mutex_exit(&spa
->spa_scrub_lock
);
2226 * Maximum number of inflight bytes is the log2 fraction of the arc size.
2227 * By default, we set it to 1/16th of the arc.
2229 int spa_load_verify_shift
= 4;
2230 int spa_load_verify_metadata
= B_TRUE
;
2231 int spa_load_verify_data
= B_TRUE
;
2235 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2236 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2238 if (zb
->zb_level
== ZB_DNODE_LEVEL
|| BP_IS_HOLE(bp
) ||
2239 BP_IS_EMBEDDED(bp
) || BP_IS_REDACTED(bp
))
2242 * Note: normally this routine will not be called if
2243 * spa_load_verify_metadata is not set. However, it may be useful
2244 * to manually set the flag after the traversal has begun.
2246 if (!spa_load_verify_metadata
)
2248 if (!BP_IS_METADATA(bp
) && !spa_load_verify_data
)
2251 uint64_t maxinflight_bytes
=
2252 arc_target_bytes() >> spa_load_verify_shift
;
2254 size_t size
= BP_GET_PSIZE(bp
);
2256 mutex_enter(&spa
->spa_scrub_lock
);
2257 while (spa
->spa_load_verify_bytes
>= maxinflight_bytes
)
2258 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2259 spa
->spa_load_verify_bytes
+= size
;
2260 mutex_exit(&spa
->spa_scrub_lock
);
2262 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2263 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2264 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2265 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2271 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2273 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2274 return (SET_ERROR(ENAMETOOLONG
));
2280 spa_load_verify(spa_t
*spa
)
2283 spa_load_error_t sle
= { 0 };
2284 zpool_load_policy_t policy
;
2285 boolean_t verify_ok
= B_FALSE
;
2288 zpool_get_load_policy(spa
->spa_config
, &policy
);
2290 if (policy
.zlp_rewind
& ZPOOL_NEVER_REWIND
)
2293 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2294 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2295 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2297 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2301 rio
= zio_root(spa
, NULL
, &sle
,
2302 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2304 if (spa_load_verify_metadata
) {
2305 if (spa
->spa_extreme_rewind
) {
2306 spa_load_note(spa
, "performing a complete scan of the "
2307 "pool since extreme rewind is on. This may take "
2308 "a very long time.\n (spa_load_verify_data=%u, "
2309 "spa_load_verify_metadata=%u)",
2310 spa_load_verify_data
, spa_load_verify_metadata
);
2313 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2314 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2315 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2318 (void) zio_wait(rio
);
2319 ASSERT0(spa
->spa_load_verify_bytes
);
2321 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2322 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2324 if (sle
.sle_meta_count
!= 0 || sle
.sle_data_count
!= 0) {
2325 spa_load_note(spa
, "spa_load_verify found %llu metadata errors "
2326 "and %llu data errors", (u_longlong_t
)sle
.sle_meta_count
,
2327 (u_longlong_t
)sle
.sle_data_count
);
2330 if (spa_load_verify_dryrun
||
2331 (!error
&& sle
.sle_meta_count
<= policy
.zlp_maxmeta
&&
2332 sle
.sle_data_count
<= policy
.zlp_maxdata
)) {
2336 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2337 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2339 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2340 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2341 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2342 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2343 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2344 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2345 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2347 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2350 if (spa_load_verify_dryrun
)
2354 if (error
!= ENXIO
&& error
!= EIO
)
2355 error
= SET_ERROR(EIO
);
2359 return (verify_ok
? 0 : EIO
);
2363 * Find a value in the pool props object.
2366 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2368 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2369 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2373 * Find a value in the pool directory object.
2376 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
, boolean_t log_enoent
)
2378 int error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2379 name
, sizeof (uint64_t), 1, val
);
2381 if (error
!= 0 && (error
!= ENOENT
|| log_enoent
)) {
2382 spa_load_failed(spa
, "couldn't get '%s' value in MOS directory "
2383 "[error=%d]", name
, error
);
2390 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2392 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2393 return (SET_ERROR(err
));
2397 spa_livelist_delete_check(spa_t
*spa
)
2399 return (spa
->spa_livelists_to_delete
!= 0);
2404 spa_livelist_delete_cb_check(void *arg
, zthr_t
*z
)
2407 return (spa_livelist_delete_check(spa
));
2411 delete_blkptr_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
2414 zio_free(spa
, tx
->tx_txg
, bp
);
2415 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
2416 -bp_get_dsize_sync(spa
, bp
),
2417 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
2422 dsl_get_next_livelist_obj(objset_t
*os
, uint64_t zap_obj
, uint64_t *llp
)
2427 zap_cursor_init(&zc
, os
, zap_obj
);
2428 err
= zap_cursor_retrieve(&zc
, &za
);
2429 zap_cursor_fini(&zc
);
2431 *llp
= za
.za_first_integer
;
2436 * Components of livelist deletion that must be performed in syncing
2437 * context: freeing block pointers and updating the pool-wide data
2438 * structures to indicate how much work is left to do
2440 typedef struct sublist_delete_arg
{
2445 } sublist_delete_arg_t
;
2448 sublist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2450 sublist_delete_arg_t
*sda
= arg
;
2451 spa_t
*spa
= sda
->spa
;
2452 dsl_deadlist_t
*ll
= sda
->ll
;
2453 uint64_t key
= sda
->key
;
2454 bplist_t
*to_free
= sda
->to_free
;
2456 bplist_iterate(to_free
, delete_blkptr_cb
, spa
, tx
);
2457 dsl_deadlist_remove_entry(ll
, key
, tx
);
2460 typedef struct livelist_delete_arg
{
2464 } livelist_delete_arg_t
;
2467 livelist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2469 livelist_delete_arg_t
*lda
= arg
;
2470 spa_t
*spa
= lda
->spa
;
2471 uint64_t ll_obj
= lda
->ll_obj
;
2472 uint64_t zap_obj
= lda
->zap_obj
;
2473 objset_t
*mos
= spa
->spa_meta_objset
;
2476 /* free the livelist and decrement the feature count */
2477 VERIFY0(zap_remove_int(mos
, zap_obj
, ll_obj
, tx
));
2478 dsl_deadlist_free(mos
, ll_obj
, tx
);
2479 spa_feature_decr(spa
, SPA_FEATURE_LIVELIST
, tx
);
2480 VERIFY0(zap_count(mos
, zap_obj
, &count
));
2482 /* no more livelists to delete */
2483 VERIFY0(zap_remove(mos
, DMU_POOL_DIRECTORY_OBJECT
,
2484 DMU_POOL_DELETED_CLONES
, tx
));
2485 VERIFY0(zap_destroy(mos
, zap_obj
, tx
));
2486 spa
->spa_livelists_to_delete
= 0;
2487 spa_notify_waiters(spa
);
2492 * Load in the value for the livelist to be removed and open it. Then,
2493 * load its first sublist and determine which block pointers should actually
2494 * be freed. Then, call a synctask which performs the actual frees and updates
2495 * the pool-wide livelist data.
2499 spa_livelist_delete_cb(void *arg
, zthr_t
*z
)
2502 uint64_t ll_obj
= 0, count
;
2503 objset_t
*mos
= spa
->spa_meta_objset
;
2504 uint64_t zap_obj
= spa
->spa_livelists_to_delete
;
2506 * Determine the next livelist to delete. This function should only
2507 * be called if there is at least one deleted clone.
2509 VERIFY0(dsl_get_next_livelist_obj(mos
, zap_obj
, &ll_obj
));
2510 VERIFY0(zap_count(mos
, ll_obj
, &count
));
2513 dsl_deadlist_entry_t
*dle
;
2515 ll
= kmem_zalloc(sizeof (dsl_deadlist_t
), KM_SLEEP
);
2516 dsl_deadlist_open(ll
, mos
, ll_obj
);
2517 dle
= dsl_deadlist_first(ll
);
2518 ASSERT3P(dle
, !=, NULL
);
2519 bplist_create(&to_free
);
2520 int err
= dsl_process_sub_livelist(&dle
->dle_bpobj
, &to_free
,
2523 sublist_delete_arg_t sync_arg
= {
2526 .key
= dle
->dle_mintxg
,
2529 zfs_dbgmsg("deleting sublist (id %llu) from"
2530 " livelist %llu, %d remaining",
2531 dle
->dle_bpobj
.bpo_object
, ll_obj
, count
- 1);
2532 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
2533 sublist_delete_sync
, &sync_arg
, 0,
2534 ZFS_SPACE_CHECK_DESTROY
));
2536 VERIFY3U(err
, ==, EINTR
);
2538 bplist_clear(&to_free
);
2539 bplist_destroy(&to_free
);
2540 dsl_deadlist_close(ll
);
2541 kmem_free(ll
, sizeof (dsl_deadlist_t
));
2543 livelist_delete_arg_t sync_arg
= {
2548 zfs_dbgmsg("deletion of livelist %llu completed", ll_obj
);
2549 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
, livelist_delete_sync
,
2550 &sync_arg
, 0, ZFS_SPACE_CHECK_DESTROY
));
2555 spa_start_livelist_destroy_thread(spa_t
*spa
)
2557 ASSERT3P(spa
->spa_livelist_delete_zthr
, ==, NULL
);
2558 spa
->spa_livelist_delete_zthr
=
2559 zthr_create("z_livelist_destroy",
2560 spa_livelist_delete_cb_check
, spa_livelist_delete_cb
, spa
);
2563 typedef struct livelist_new_arg
{
2566 } livelist_new_arg_t
;
2569 livelist_track_new_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
2573 livelist_new_arg_t
*lna
= arg
;
2575 bplist_append(lna
->frees
, bp
);
2577 bplist_append(lna
->allocs
, bp
);
2578 zfs_livelist_condense_new_alloc
++;
2583 typedef struct livelist_condense_arg
{
2586 uint64_t first_size
;
2588 } livelist_condense_arg_t
;
2591 spa_livelist_condense_sync(void *arg
, dmu_tx_t
*tx
)
2593 livelist_condense_arg_t
*lca
= arg
;
2594 spa_t
*spa
= lca
->spa
;
2596 dsl_dataset_t
*ds
= spa
->spa_to_condense
.ds
;
2598 /* Have we been cancelled? */
2599 if (spa
->spa_to_condense
.cancelled
) {
2600 zfs_livelist_condense_sync_cancel
++;
2604 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
2605 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
2606 dsl_deadlist_t
*ll
= &ds
->ds_dir
->dd_livelist
;
2609 * It's possible that the livelist was changed while the zthr was
2610 * running. Therefore, we need to check for new blkptrs in the two
2611 * entries being condensed and continue to track them in the livelist.
2612 * Because of the way we handle remapped blkptrs (see dbuf_remap_impl),
2613 * it's possible that the newly added blkptrs are FREEs or ALLOCs so
2614 * we need to sort them into two different bplists.
2616 uint64_t first_obj
= first
->dle_bpobj
.bpo_object
;
2617 uint64_t next_obj
= next
->dle_bpobj
.bpo_object
;
2618 uint64_t cur_first_size
= first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
2619 uint64_t cur_next_size
= next
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
2621 bplist_create(&new_frees
);
2622 livelist_new_arg_t new_bps
= {
2623 .allocs
= &lca
->to_keep
,
2624 .frees
= &new_frees
,
2627 if (cur_first_size
> lca
->first_size
) {
2628 VERIFY0(livelist_bpobj_iterate_from_nofree(&first
->dle_bpobj
,
2629 livelist_track_new_cb
, &new_bps
, lca
->first_size
));
2631 if (cur_next_size
> lca
->next_size
) {
2632 VERIFY0(livelist_bpobj_iterate_from_nofree(&next
->dle_bpobj
,
2633 livelist_track_new_cb
, &new_bps
, lca
->next_size
));
2636 dsl_deadlist_clear_entry(first
, ll
, tx
);
2637 ASSERT(bpobj_is_empty(&first
->dle_bpobj
));
2638 dsl_deadlist_remove_entry(ll
, next
->dle_mintxg
, tx
);
2640 bplist_iterate(&lca
->to_keep
, dsl_deadlist_insert_alloc_cb
, ll
, tx
);
2641 bplist_iterate(&new_frees
, dsl_deadlist_insert_free_cb
, ll
, tx
);
2642 bplist_destroy(&new_frees
);
2644 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
2645 dsl_dataset_name(ds
, dsname
);
2646 zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu "
2647 "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu "
2648 "(%llu blkptrs)", tx
->tx_txg
, dsname
, ds
->ds_object
, first_obj
,
2649 cur_first_size
, next_obj
, cur_next_size
,
2650 first
->dle_bpobj
.bpo_object
,
2651 first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
);
2653 dmu_buf_rele(ds
->ds_dbuf
, spa
);
2654 spa
->spa_to_condense
.ds
= NULL
;
2655 bplist_clear(&lca
->to_keep
);
2656 bplist_destroy(&lca
->to_keep
);
2657 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
2658 spa
->spa_to_condense
.syncing
= B_FALSE
;
2662 spa_livelist_condense_cb(void *arg
, zthr_t
*t
)
2664 while (zfs_livelist_condense_zthr_pause
&&
2665 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
2669 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
2670 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
2671 uint64_t first_size
, next_size
;
2673 livelist_condense_arg_t
*lca
=
2674 kmem_alloc(sizeof (livelist_condense_arg_t
), KM_SLEEP
);
2675 bplist_create(&lca
->to_keep
);
2678 * Process the livelists (matching FREEs and ALLOCs) in open context
2679 * so we have minimal work in syncing context to condense.
2681 * We save bpobj sizes (first_size and next_size) to use later in
2682 * syncing context to determine if entries were added to these sublists
2683 * while in open context. This is possible because the clone is still
2684 * active and open for normal writes and we want to make sure the new,
2685 * unprocessed blockpointers are inserted into the livelist normally.
2687 * Note that dsl_process_sub_livelist() both stores the size number of
2688 * blockpointers and iterates over them while the bpobj's lock held, so
2689 * the sizes returned to us are consistent which what was actually
2692 int err
= dsl_process_sub_livelist(&first
->dle_bpobj
, &lca
->to_keep
, t
,
2695 err
= dsl_process_sub_livelist(&next
->dle_bpobj
, &lca
->to_keep
,
2699 while (zfs_livelist_condense_sync_pause
&&
2700 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
2703 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
2704 dmu_tx_mark_netfree(tx
);
2705 dmu_tx_hold_space(tx
, 1);
2706 err
= dmu_tx_assign(tx
, TXG_NOWAIT
| TXG_NOTHROTTLE
);
2709 * Prevent the condense zthr restarting before
2710 * the synctask completes.
2712 spa
->spa_to_condense
.syncing
= B_TRUE
;
2714 lca
->first_size
= first_size
;
2715 lca
->next_size
= next_size
;
2716 dsl_sync_task_nowait(spa_get_dsl(spa
),
2717 spa_livelist_condense_sync
, lca
, tx
);
2723 * Condensing can not continue: either it was externally stopped or
2724 * we were unable to assign to a tx because the pool has run out of
2725 * space. In the second case, we'll just end up trying to condense
2726 * again in a later txg.
2729 bplist_clear(&lca
->to_keep
);
2730 bplist_destroy(&lca
->to_keep
);
2731 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
2732 dmu_buf_rele(spa
->spa_to_condense
.ds
->ds_dbuf
, spa
);
2733 spa
->spa_to_condense
.ds
= NULL
;
2735 zfs_livelist_condense_zthr_cancel
++;
2740 * Check that there is something to condense but that a condense is not
2741 * already in progress and that condensing has not been cancelled.
2744 spa_livelist_condense_cb_check(void *arg
, zthr_t
*z
)
2747 if ((spa
->spa_to_condense
.ds
!= NULL
) &&
2748 (spa
->spa_to_condense
.syncing
== B_FALSE
) &&
2749 (spa
->spa_to_condense
.cancelled
== B_FALSE
)) {
2756 spa_start_livelist_condensing_thread(spa_t
*spa
)
2758 spa
->spa_to_condense
.ds
= NULL
;
2759 spa
->spa_to_condense
.first
= NULL
;
2760 spa
->spa_to_condense
.next
= NULL
;
2761 spa
->spa_to_condense
.syncing
= B_FALSE
;
2762 spa
->spa_to_condense
.cancelled
= B_FALSE
;
2764 ASSERT3P(spa
->spa_livelist_condense_zthr
, ==, NULL
);
2765 spa
->spa_livelist_condense_zthr
=
2766 zthr_create("z_livelist_condense",
2767 spa_livelist_condense_cb_check
,
2768 spa_livelist_condense_cb
, spa
);
2772 spa_spawn_aux_threads(spa_t
*spa
)
2774 ASSERT(spa_writeable(spa
));
2776 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2778 spa_start_indirect_condensing_thread(spa
);
2779 spa_start_livelist_destroy_thread(spa
);
2780 spa_start_livelist_condensing_thread(spa
);
2782 ASSERT3P(spa
->spa_checkpoint_discard_zthr
, ==, NULL
);
2783 spa
->spa_checkpoint_discard_zthr
=
2784 zthr_create("z_checkpoint_discard",
2785 spa_checkpoint_discard_thread_check
,
2786 spa_checkpoint_discard_thread
, spa
);
2790 * Fix up config after a partly-completed split. This is done with the
2791 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2792 * pool have that entry in their config, but only the splitting one contains
2793 * a list of all the guids of the vdevs that are being split off.
2795 * This function determines what to do with that list: either rejoin
2796 * all the disks to the pool, or complete the splitting process. To attempt
2797 * the rejoin, each disk that is offlined is marked online again, and
2798 * we do a reopen() call. If the vdev label for every disk that was
2799 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2800 * then we call vdev_split() on each disk, and complete the split.
2802 * Otherwise we leave the config alone, with all the vdevs in place in
2803 * the original pool.
2806 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2813 boolean_t attempt_reopen
;
2815 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2818 /* check that the config is complete */
2819 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2820 &glist
, &gcount
) != 0)
2823 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2825 /* attempt to online all the vdevs & validate */
2826 attempt_reopen
= B_TRUE
;
2827 for (i
= 0; i
< gcount
; i
++) {
2828 if (glist
[i
] == 0) /* vdev is hole */
2831 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2832 if (vd
[i
] == NULL
) {
2834 * Don't bother attempting to reopen the disks;
2835 * just do the split.
2837 attempt_reopen
= B_FALSE
;
2839 /* attempt to re-online it */
2840 vd
[i
]->vdev_offline
= B_FALSE
;
2844 if (attempt_reopen
) {
2845 vdev_reopen(spa
->spa_root_vdev
);
2847 /* check each device to see what state it's in */
2848 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2849 if (vd
[i
] != NULL
&&
2850 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2857 * If every disk has been moved to the new pool, or if we never
2858 * even attempted to look at them, then we split them off for
2861 if (!attempt_reopen
|| gcount
== extracted
) {
2862 for (i
= 0; i
< gcount
; i
++)
2865 vdev_reopen(spa
->spa_root_vdev
);
2868 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2872 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
)
2874 char *ereport
= FM_EREPORT_ZFS_POOL
;
2877 spa
->spa_load_state
= state
;
2878 (void) spa_import_progress_set_state(spa_guid(spa
),
2879 spa_load_state(spa
));
2881 gethrestime(&spa
->spa_loaded_ts
);
2882 error
= spa_load_impl(spa
, type
, &ereport
);
2885 * Don't count references from objsets that are already closed
2886 * and are making their way through the eviction process.
2888 spa_evicting_os_wait(spa
);
2889 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
2891 if (error
!= EEXIST
) {
2892 spa
->spa_loaded_ts
.tv_sec
= 0;
2893 spa
->spa_loaded_ts
.tv_nsec
= 0;
2895 if (error
!= EBADF
) {
2896 (void) zfs_ereport_post(ereport
, spa
,
2897 NULL
, NULL
, NULL
, 0);
2900 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2903 (void) spa_import_progress_set_state(spa_guid(spa
),
2904 spa_load_state(spa
));
2911 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2912 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2913 * spa's per-vdev ZAP list.
2916 vdev_count_verify_zaps(vdev_t
*vd
)
2918 spa_t
*spa
= vd
->vdev_spa
;
2921 if (vd
->vdev_top_zap
!= 0) {
2923 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2924 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2926 if (vd
->vdev_leaf_zap
!= 0) {
2928 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2929 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2932 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2933 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2941 * Determine whether the activity check is required.
2944 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
2948 uint64_t hostid
= 0;
2949 uint64_t tryconfig_txg
= 0;
2950 uint64_t tryconfig_timestamp
= 0;
2951 uint16_t tryconfig_mmp_seq
= 0;
2954 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2955 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2956 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
2958 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
2959 &tryconfig_timestamp
);
2960 (void) nvlist_lookup_uint16(nvinfo
, ZPOOL_CONFIG_MMP_SEQ
,
2961 &tryconfig_mmp_seq
);
2964 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
2967 * Disable the MMP activity check - This is used by zdb which
2968 * is intended to be used on potentially active pools.
2970 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
2974 * Skip the activity check when the MMP feature is disabled.
2976 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
2980 * If the tryconfig_ values are nonzero, they are the results of an
2981 * earlier tryimport. If they all match the uberblock we just found,
2982 * then the pool has not changed and we return false so we do not test
2985 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
2986 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
&&
2987 tryconfig_mmp_seq
&& tryconfig_mmp_seq
==
2988 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0))
2992 * Allow the activity check to be skipped when importing the pool
2993 * on the same host which last imported it. Since the hostid from
2994 * configuration may be stale use the one read from the label.
2996 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
2997 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
2999 if (hostid
== spa_get_hostid(spa
))
3003 * Skip the activity test when the pool was cleanly exported.
3005 if (state
!= POOL_STATE_ACTIVE
)
3012 * Nanoseconds the activity check must watch for changes on-disk.
3015 spa_activity_check_duration(spa_t
*spa
, uberblock_t
*ub
)
3017 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
3018 uint64_t multihost_interval
= MSEC2NSEC(
3019 MMP_INTERVAL_OK(zfs_multihost_interval
));
3020 uint64_t import_delay
= MAX(NANOSEC
, import_intervals
*
3021 multihost_interval
);
3024 * Local tunables determine a minimum duration except for the case
3025 * where we know when the remote host will suspend the pool if MMP
3026 * writes do not land.
3028 * See Big Theory comment at the top of mmp.c for the reasoning behind
3029 * these cases and times.
3032 ASSERT(MMP_IMPORT_SAFETY_FACTOR
>= 100);
3034 if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3035 MMP_FAIL_INT(ub
) > 0) {
3037 /* MMP on remote host will suspend pool after failed writes */
3038 import_delay
= MMP_FAIL_INT(ub
) * MSEC2NSEC(MMP_INTERVAL(ub
)) *
3039 MMP_IMPORT_SAFETY_FACTOR
/ 100;
3041 zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp "
3042 "mmp_fails=%llu ub_mmp mmp_interval=%llu "
3043 "import_intervals=%u", import_delay
, MMP_FAIL_INT(ub
),
3044 MMP_INTERVAL(ub
), import_intervals
);
3046 } else if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3047 MMP_FAIL_INT(ub
) == 0) {
3049 /* MMP on remote host will never suspend pool */
3050 import_delay
= MAX(import_delay
, (MSEC2NSEC(MMP_INTERVAL(ub
)) +
3051 ub
->ub_mmp_delay
) * import_intervals
);
3053 zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp "
3054 "mmp_interval=%llu ub_mmp_delay=%llu "
3055 "import_intervals=%u", import_delay
, MMP_INTERVAL(ub
),
3056 ub
->ub_mmp_delay
, import_intervals
);
3058 } else if (MMP_VALID(ub
)) {
3060 * zfs-0.7 compatibility case
3063 import_delay
= MAX(import_delay
, (multihost_interval
+
3064 ub
->ub_mmp_delay
) * import_intervals
);
3066 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu "
3067 "import_intervals=%u leaves=%u", import_delay
,
3068 ub
->ub_mmp_delay
, import_intervals
,
3069 vdev_count_leaves(spa
));
3071 /* Using local tunings is the only reasonable option */
3072 zfs_dbgmsg("pool last imported on non-MMP aware "
3073 "host using import_delay=%llu multihost_interval=%llu "
3074 "import_intervals=%u", import_delay
, multihost_interval
,
3078 return (import_delay
);
3082 * Perform the import activity check. If the user canceled the import or
3083 * we detected activity then fail.
3086 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
3088 uint64_t txg
= ub
->ub_txg
;
3089 uint64_t timestamp
= ub
->ub_timestamp
;
3090 uint64_t mmp_config
= ub
->ub_mmp_config
;
3091 uint16_t mmp_seq
= MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0;
3092 uint64_t import_delay
;
3093 hrtime_t import_expire
;
3094 nvlist_t
*mmp_label
= NULL
;
3095 vdev_t
*rvd
= spa
->spa_root_vdev
;
3100 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
3101 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3105 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
3106 * during the earlier tryimport. If the txg recorded there is 0 then
3107 * the pool is known to be active on another host.
3109 * Otherwise, the pool might be in use on another host. Check for
3110 * changes in the uberblocks on disk if necessary.
3112 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3113 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
3114 ZPOOL_CONFIG_LOAD_INFO
);
3116 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
3117 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
3118 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3119 error
= SET_ERROR(EREMOTEIO
);
3124 import_delay
= spa_activity_check_duration(spa
, ub
);
3126 /* Add a small random factor in case of simultaneous imports (0-25%) */
3127 import_delay
+= import_delay
* spa_get_random(250) / 1000;
3129 import_expire
= gethrtime() + import_delay
;
3131 while (gethrtime() < import_expire
) {
3132 (void) spa_import_progress_set_mmp_check(spa_guid(spa
),
3133 NSEC2SEC(import_expire
- gethrtime()));
3135 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3137 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
||
3138 mmp_seq
!= (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0)) {
3139 zfs_dbgmsg("multihost activity detected "
3140 "txg %llu ub_txg %llu "
3141 "timestamp %llu ub_timestamp %llu "
3142 "mmp_config %#llx ub_mmp_config %#llx",
3143 txg
, ub
->ub_txg
, timestamp
, ub
->ub_timestamp
,
3144 mmp_config
, ub
->ub_mmp_config
);
3146 error
= SET_ERROR(EREMOTEIO
);
3151 nvlist_free(mmp_label
);
3155 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
3157 error
= SET_ERROR(EINTR
);
3165 mutex_destroy(&mtx
);
3169 * If the pool is determined to be active store the status in the
3170 * spa->spa_load_info nvlist. If the remote hostname or hostid are
3171 * available from configuration read from disk store them as well.
3172 * This allows 'zpool import' to generate a more useful message.
3174 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
3175 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
3176 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
3178 if (error
== EREMOTEIO
) {
3179 char *hostname
= "<unknown>";
3180 uint64_t hostid
= 0;
3183 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
3184 hostname
= fnvlist_lookup_string(mmp_label
,
3185 ZPOOL_CONFIG_HOSTNAME
);
3186 fnvlist_add_string(spa
->spa_load_info
,
3187 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
3190 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
3191 hostid
= fnvlist_lookup_uint64(mmp_label
,
3192 ZPOOL_CONFIG_HOSTID
);
3193 fnvlist_add_uint64(spa
->spa_load_info
,
3194 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
3198 fnvlist_add_uint64(spa
->spa_load_info
,
3199 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
3200 fnvlist_add_uint64(spa
->spa_load_info
,
3201 ZPOOL_CONFIG_MMP_TXG
, 0);
3203 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
3207 nvlist_free(mmp_label
);
3213 spa_verify_host(spa_t
*spa
, nvlist_t
*mos_config
)
3217 uint64_t myhostid
= 0;
3219 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
3220 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
3221 hostname
= fnvlist_lookup_string(mos_config
,
3222 ZPOOL_CONFIG_HOSTNAME
);
3224 myhostid
= zone_get_hostid(NULL
);
3226 if (hostid
!= 0 && myhostid
!= 0 && hostid
!= myhostid
) {
3227 cmn_err(CE_WARN
, "pool '%s' could not be "
3228 "loaded as it was last accessed by "
3229 "another system (host: %s hostid: 0x%llx). "
3230 "See: https://openzfs.github.io/openzfs-docs/msg/"
3232 spa_name(spa
), hostname
, (u_longlong_t
)hostid
);
3233 spa_load_failed(spa
, "hostid verification failed: pool "
3234 "last accessed by host: %s (hostid: 0x%llx)",
3235 hostname
, (u_longlong_t
)hostid
);
3236 return (SET_ERROR(EBADF
));
3244 spa_ld_parse_config(spa_t
*spa
, spa_import_type_t type
)
3247 nvlist_t
*nvtree
, *nvl
, *config
= spa
->spa_config
;
3254 * Versioning wasn't explicitly added to the label until later, so if
3255 * it's not present treat it as the initial version.
3257 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
3258 &spa
->spa_ubsync
.ub_version
) != 0)
3259 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
3261 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
)) {
3262 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3263 ZPOOL_CONFIG_POOL_GUID
);
3264 return (SET_ERROR(EINVAL
));
3268 * If we are doing an import, ensure that the pool is not already
3269 * imported by checking if its pool guid already exists in the
3272 * The only case that we allow an already imported pool to be
3273 * imported again, is when the pool is checkpointed and we want to
3274 * look at its checkpointed state from userland tools like zdb.
3277 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3278 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3279 spa_guid_exists(pool_guid
, 0)) {
3281 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3282 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3283 spa_guid_exists(pool_guid
, 0) &&
3284 !spa_importing_readonly_checkpoint(spa
)) {
3286 spa_load_failed(spa
, "a pool with guid %llu is already open",
3287 (u_longlong_t
)pool_guid
);
3288 return (SET_ERROR(EEXIST
));
3291 spa
->spa_config_guid
= pool_guid
;
3293 nvlist_free(spa
->spa_load_info
);
3294 spa
->spa_load_info
= fnvlist_alloc();
3296 ASSERT(spa
->spa_comment
== NULL
);
3297 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
3298 spa
->spa_comment
= spa_strdup(comment
);
3300 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
3301 &spa
->spa_config_txg
);
3303 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) == 0)
3304 spa
->spa_config_splitting
= fnvlist_dup(nvl
);
3306 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvtree
)) {
3307 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3308 ZPOOL_CONFIG_VDEV_TREE
);
3309 return (SET_ERROR(EINVAL
));
3313 * Create "The Godfather" zio to hold all async IOs
3315 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3317 for (int i
= 0; i
< max_ncpus
; i
++) {
3318 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3319 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3320 ZIO_FLAG_GODFATHER
);
3324 * Parse the configuration into a vdev tree. We explicitly set the
3325 * value that will be returned by spa_version() since parsing the
3326 * configuration requires knowing the version number.
3328 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3329 parse
= (type
== SPA_IMPORT_EXISTING
?
3330 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
3331 error
= spa_config_parse(spa
, &rvd
, nvtree
, NULL
, 0, parse
);
3332 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3335 spa_load_failed(spa
, "unable to parse config [error=%d]",
3340 ASSERT(spa
->spa_root_vdev
== rvd
);
3341 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
3342 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
3344 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3345 ASSERT(spa_guid(spa
) == pool_guid
);
3352 * Recursively open all vdevs in the vdev tree. This function is called twice:
3353 * first with the untrusted config, then with the trusted config.
3356 spa_ld_open_vdevs(spa_t
*spa
)
3361 * spa_missing_tvds_allowed defines how many top-level vdevs can be
3362 * missing/unopenable for the root vdev to be still considered openable.
3364 if (spa
->spa_trust_config
) {
3365 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds
;
3366 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_CACHEFILE
) {
3367 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_cachefile
;
3368 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_SCAN
) {
3369 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_scan
;
3371 spa
->spa_missing_tvds_allowed
= 0;
3374 spa
->spa_missing_tvds_allowed
=
3375 MAX(zfs_max_missing_tvds
, spa
->spa_missing_tvds_allowed
);
3377 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3378 error
= vdev_open(spa
->spa_root_vdev
);
3379 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3381 if (spa
->spa_missing_tvds
!= 0) {
3382 spa_load_note(spa
, "vdev tree has %lld missing top-level "
3383 "vdevs.", (u_longlong_t
)spa
->spa_missing_tvds
);
3384 if (spa
->spa_trust_config
&& (spa
->spa_mode
& SPA_MODE_WRITE
)) {
3386 * Although theoretically we could allow users to open
3387 * incomplete pools in RW mode, we'd need to add a lot
3388 * of extra logic (e.g. adjust pool space to account
3389 * for missing vdevs).
3390 * This limitation also prevents users from accidentally
3391 * opening the pool in RW mode during data recovery and
3392 * damaging it further.
3394 spa_load_note(spa
, "pools with missing top-level "
3395 "vdevs can only be opened in read-only mode.");
3396 error
= SET_ERROR(ENXIO
);
3398 spa_load_note(spa
, "current settings allow for maximum "
3399 "%lld missing top-level vdevs at this stage.",
3400 (u_longlong_t
)spa
->spa_missing_tvds_allowed
);
3404 spa_load_failed(spa
, "unable to open vdev tree [error=%d]",
3407 if (spa
->spa_missing_tvds
!= 0 || error
!= 0)
3408 vdev_dbgmsg_print_tree(spa
->spa_root_vdev
, 2);
3414 * We need to validate the vdev labels against the configuration that
3415 * we have in hand. This function is called twice: first with an untrusted
3416 * config, then with a trusted config. The validation is more strict when the
3417 * config is trusted.
3420 spa_ld_validate_vdevs(spa_t
*spa
)
3423 vdev_t
*rvd
= spa
->spa_root_vdev
;
3425 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3426 error
= vdev_validate(rvd
);
3427 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3430 spa_load_failed(spa
, "vdev_validate failed [error=%d]", error
);
3434 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
) {
3435 spa_load_failed(spa
, "cannot open vdev tree after invalidating "
3437 vdev_dbgmsg_print_tree(rvd
, 2);
3438 return (SET_ERROR(ENXIO
));
3445 spa_ld_select_uberblock_done(spa_t
*spa
, uberblock_t
*ub
)
3447 spa
->spa_state
= POOL_STATE_ACTIVE
;
3448 spa
->spa_ubsync
= spa
->spa_uberblock
;
3449 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
3450 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
3451 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
3452 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
3453 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
3454 spa
->spa_prev_software_version
= ub
->ub_software_version
;
3458 spa_ld_select_uberblock(spa_t
*spa
, spa_import_type_t type
)
3460 vdev_t
*rvd
= spa
->spa_root_vdev
;
3462 uberblock_t
*ub
= &spa
->spa_uberblock
;
3463 boolean_t activity_check
= B_FALSE
;
3466 * If we are opening the checkpointed state of the pool by
3467 * rewinding to it, at this point we will have written the
3468 * checkpointed uberblock to the vdev labels, so searching
3469 * the labels will find the right uberblock. However, if
3470 * we are opening the checkpointed state read-only, we have
3471 * not modified the labels. Therefore, we must ignore the
3472 * labels and continue using the spa_uberblock that was set
3473 * by spa_ld_checkpoint_rewind.
3475 * Note that it would be fine to ignore the labels when
3476 * rewinding (opening writeable) as well. However, if we
3477 * crash just after writing the labels, we will end up
3478 * searching the labels. Doing so in the common case means
3479 * that this code path gets exercised normally, rather than
3480 * just in the edge case.
3482 if (ub
->ub_checkpoint_txg
!= 0 &&
3483 spa_importing_readonly_checkpoint(spa
)) {
3484 spa_ld_select_uberblock_done(spa
, ub
);
3489 * Find the best uberblock.
3491 vdev_uberblock_load(rvd
, ub
, &label
);
3494 * If we weren't able to find a single valid uberblock, return failure.
3496 if (ub
->ub_txg
== 0) {
3498 spa_load_failed(spa
, "no valid uberblock found");
3499 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
3502 if (spa
->spa_load_max_txg
!= UINT64_MAX
) {
3503 (void) spa_import_progress_set_max_txg(spa_guid(spa
),
3504 (u_longlong_t
)spa
->spa_load_max_txg
);
3506 spa_load_note(spa
, "using uberblock with txg=%llu",
3507 (u_longlong_t
)ub
->ub_txg
);
3511 * For pools which have the multihost property on determine if the
3512 * pool is truly inactive and can be safely imported. Prevent
3513 * hosts which don't have a hostid set from importing the pool.
3515 activity_check
= spa_activity_check_required(spa
, ub
, label
,
3517 if (activity_check
) {
3518 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
3519 spa_get_hostid(spa
) == 0) {
3521 fnvlist_add_uint64(spa
->spa_load_info
,
3522 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3523 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3526 int error
= spa_activity_check(spa
, ub
, spa
->spa_config
);
3532 fnvlist_add_uint64(spa
->spa_load_info
,
3533 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
3534 fnvlist_add_uint64(spa
->spa_load_info
,
3535 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
3536 fnvlist_add_uint16(spa
->spa_load_info
,
3537 ZPOOL_CONFIG_MMP_SEQ
,
3538 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0));
3542 * If the pool has an unsupported version we can't open it.
3544 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
3546 spa_load_failed(spa
, "version %llu is not supported",
3547 (u_longlong_t
)ub
->ub_version
);
3548 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
3551 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
3555 * If we weren't able to find what's necessary for reading the
3556 * MOS in the label, return failure.
3558 if (label
== NULL
) {
3559 spa_load_failed(spa
, "label config unavailable");
3560 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3564 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
3567 spa_load_failed(spa
, "invalid label: '%s' missing",
3568 ZPOOL_CONFIG_FEATURES_FOR_READ
);
3569 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3574 * Update our in-core representation with the definitive values
3577 nvlist_free(spa
->spa_label_features
);
3578 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
3584 * Look through entries in the label nvlist's features_for_read. If
3585 * there is a feature listed there which we don't understand then we
3586 * cannot open a pool.
3588 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
3589 nvlist_t
*unsup_feat
;
3591 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
3594 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
3596 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
3597 if (!zfeature_is_supported(nvpair_name(nvp
))) {
3598 VERIFY(nvlist_add_string(unsup_feat
,
3599 nvpair_name(nvp
), "") == 0);
3603 if (!nvlist_empty(unsup_feat
)) {
3604 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
3605 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
3606 nvlist_free(unsup_feat
);
3607 spa_load_failed(spa
, "some features are unsupported");
3608 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3612 nvlist_free(unsup_feat
);
3615 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
3616 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3617 spa_try_repair(spa
, spa
->spa_config
);
3618 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3619 nvlist_free(spa
->spa_config_splitting
);
3620 spa
->spa_config_splitting
= NULL
;
3624 * Initialize internal SPA structures.
3626 spa_ld_select_uberblock_done(spa
, ub
);
3632 spa_ld_open_rootbp(spa_t
*spa
)
3635 vdev_t
*rvd
= spa
->spa_root_vdev
;
3637 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
3639 spa_load_failed(spa
, "unable to open rootbp in dsl_pool_init "
3640 "[error=%d]", error
);
3641 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3643 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
3649 spa_ld_trusted_config(spa_t
*spa
, spa_import_type_t type
,
3650 boolean_t reloading
)
3652 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
3653 nvlist_t
*nv
, *mos_config
, *policy
;
3654 int error
= 0, copy_error
;
3655 uint64_t healthy_tvds
, healthy_tvds_mos
;
3656 uint64_t mos_config_txg
;
3658 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
, B_TRUE
)
3660 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3663 * If we're assembling a pool from a split, the config provided is
3664 * already trusted so there is nothing to do.
3666 if (type
== SPA_IMPORT_ASSEMBLE
)
3669 healthy_tvds
= spa_healthy_core_tvds(spa
);
3671 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
)
3673 spa_load_failed(spa
, "unable to retrieve MOS config");
3674 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3678 * If we are doing an open, pool owner wasn't verified yet, thus do
3679 * the verification here.
3681 if (spa
->spa_load_state
== SPA_LOAD_OPEN
) {
3682 error
= spa_verify_host(spa
, mos_config
);
3684 nvlist_free(mos_config
);
3689 nv
= fnvlist_lookup_nvlist(mos_config
, ZPOOL_CONFIG_VDEV_TREE
);
3691 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3694 * Build a new vdev tree from the trusted config
3696 error
= spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
);
3698 nvlist_free(mos_config
);
3699 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3700 spa_load_failed(spa
, "spa_config_parse failed [error=%d]",
3702 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3706 * Vdev paths in the MOS may be obsolete. If the untrusted config was
3707 * obtained by scanning /dev/dsk, then it will have the right vdev
3708 * paths. We update the trusted MOS config with this information.
3709 * We first try to copy the paths with vdev_copy_path_strict, which
3710 * succeeds only when both configs have exactly the same vdev tree.
3711 * If that fails, we fall back to a more flexible method that has a
3712 * best effort policy.
3714 copy_error
= vdev_copy_path_strict(rvd
, mrvd
);
3715 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3716 spa_load_note(spa
, "provided vdev tree:");
3717 vdev_dbgmsg_print_tree(rvd
, 2);
3718 spa_load_note(spa
, "MOS vdev tree:");
3719 vdev_dbgmsg_print_tree(mrvd
, 2);
3721 if (copy_error
!= 0) {
3722 spa_load_note(spa
, "vdev_copy_path_strict failed, falling "
3723 "back to vdev_copy_path_relaxed");
3724 vdev_copy_path_relaxed(rvd
, mrvd
);
3729 spa
->spa_root_vdev
= mrvd
;
3731 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3734 * We will use spa_config if we decide to reload the spa or if spa_load
3735 * fails and we rewind. We must thus regenerate the config using the
3736 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
3737 * pass settings on how to load the pool and is not stored in the MOS.
3738 * We copy it over to our new, trusted config.
3740 mos_config_txg
= fnvlist_lookup_uint64(mos_config
,
3741 ZPOOL_CONFIG_POOL_TXG
);
3742 nvlist_free(mos_config
);
3743 mos_config
= spa_config_generate(spa
, NULL
, mos_config_txg
, B_FALSE
);
3744 if (nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_LOAD_POLICY
,
3746 fnvlist_add_nvlist(mos_config
, ZPOOL_LOAD_POLICY
, policy
);
3747 spa_config_set(spa
, mos_config
);
3748 spa
->spa_config_source
= SPA_CONFIG_SRC_MOS
;
3751 * Now that we got the config from the MOS, we should be more strict
3752 * in checking blkptrs and can make assumptions about the consistency
3753 * of the vdev tree. spa_trust_config must be set to true before opening
3754 * vdevs in order for them to be writeable.
3756 spa
->spa_trust_config
= B_TRUE
;
3759 * Open and validate the new vdev tree
3761 error
= spa_ld_open_vdevs(spa
);
3765 error
= spa_ld_validate_vdevs(spa
);
3769 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3770 spa_load_note(spa
, "final vdev tree:");
3771 vdev_dbgmsg_print_tree(rvd
, 2);
3774 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
&&
3775 !spa
->spa_extreme_rewind
&& zfs_max_missing_tvds
== 0) {
3777 * Sanity check to make sure that we are indeed loading the
3778 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
3779 * in the config provided and they happened to be the only ones
3780 * to have the latest uberblock, we could involuntarily perform
3781 * an extreme rewind.
3783 healthy_tvds_mos
= spa_healthy_core_tvds(spa
);
3784 if (healthy_tvds_mos
- healthy_tvds
>=
3785 SPA_SYNC_MIN_VDEVS
) {
3786 spa_load_note(spa
, "config provided misses too many "
3787 "top-level vdevs compared to MOS (%lld vs %lld). ",
3788 (u_longlong_t
)healthy_tvds
,
3789 (u_longlong_t
)healthy_tvds_mos
);
3790 spa_load_note(spa
, "vdev tree:");
3791 vdev_dbgmsg_print_tree(rvd
, 2);
3793 spa_load_failed(spa
, "config was already "
3794 "provided from MOS. Aborting.");
3795 return (spa_vdev_err(rvd
,
3796 VDEV_AUX_CORRUPT_DATA
, EIO
));
3798 spa_load_note(spa
, "spa must be reloaded using MOS "
3800 return (SET_ERROR(EAGAIN
));
3804 error
= spa_check_for_missing_logs(spa
);
3806 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
3808 if (rvd
->vdev_guid_sum
!= spa
->spa_uberblock
.ub_guid_sum
) {
3809 spa_load_failed(spa
, "uberblock guid sum doesn't match MOS "
3810 "guid sum (%llu != %llu)",
3811 (u_longlong_t
)spa
->spa_uberblock
.ub_guid_sum
,
3812 (u_longlong_t
)rvd
->vdev_guid_sum
);
3813 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
3821 spa_ld_open_indirect_vdev_metadata(spa_t
*spa
)
3824 vdev_t
*rvd
= spa
->spa_root_vdev
;
3827 * Everything that we read before spa_remove_init() must be stored
3828 * on concreted vdevs. Therefore we do this as early as possible.
3830 error
= spa_remove_init(spa
);
3832 spa_load_failed(spa
, "spa_remove_init failed [error=%d]",
3834 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3838 * Retrieve information needed to condense indirect vdev mappings.
3840 error
= spa_condense_init(spa
);
3842 spa_load_failed(spa
, "spa_condense_init failed [error=%d]",
3844 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3851 spa_ld_check_features(spa_t
*spa
, boolean_t
*missing_feat_writep
)
3854 vdev_t
*rvd
= spa
->spa_root_vdev
;
3856 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
3857 boolean_t missing_feat_read
= B_FALSE
;
3858 nvlist_t
*unsup_feat
, *enabled_feat
;
3860 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
3861 &spa
->spa_feat_for_read_obj
, B_TRUE
) != 0) {
3862 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3865 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
3866 &spa
->spa_feat_for_write_obj
, B_TRUE
) != 0) {
3867 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3870 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
3871 &spa
->spa_feat_desc_obj
, B_TRUE
) != 0) {
3872 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3875 enabled_feat
= fnvlist_alloc();
3876 unsup_feat
= fnvlist_alloc();
3878 if (!spa_features_check(spa
, B_FALSE
,
3879 unsup_feat
, enabled_feat
))
3880 missing_feat_read
= B_TRUE
;
3882 if (spa_writeable(spa
) ||
3883 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) {
3884 if (!spa_features_check(spa
, B_TRUE
,
3885 unsup_feat
, enabled_feat
)) {
3886 *missing_feat_writep
= B_TRUE
;
3890 fnvlist_add_nvlist(spa
->spa_load_info
,
3891 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
3893 if (!nvlist_empty(unsup_feat
)) {
3894 fnvlist_add_nvlist(spa
->spa_load_info
,
3895 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
3898 fnvlist_free(enabled_feat
);
3899 fnvlist_free(unsup_feat
);
3901 if (!missing_feat_read
) {
3902 fnvlist_add_boolean(spa
->spa_load_info
,
3903 ZPOOL_CONFIG_CAN_RDONLY
);
3907 * If the state is SPA_LOAD_TRYIMPORT, our objective is
3908 * twofold: to determine whether the pool is available for
3909 * import in read-write mode and (if it is not) whether the
3910 * pool is available for import in read-only mode. If the pool
3911 * is available for import in read-write mode, it is displayed
3912 * as available in userland; if it is not available for import
3913 * in read-only mode, it is displayed as unavailable in
3914 * userland. If the pool is available for import in read-only
3915 * mode but not read-write mode, it is displayed as unavailable
3916 * in userland with a special note that the pool is actually
3917 * available for open in read-only mode.
3919 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
3920 * missing a feature for write, we must first determine whether
3921 * the pool can be opened read-only before returning to
3922 * userland in order to know whether to display the
3923 * abovementioned note.
3925 if (missing_feat_read
|| (*missing_feat_writep
&&
3926 spa_writeable(spa
))) {
3927 spa_load_failed(spa
, "pool uses unsupported features");
3928 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3933 * Load refcounts for ZFS features from disk into an in-memory
3934 * cache during SPA initialization.
3936 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
3939 error
= feature_get_refcount_from_disk(spa
,
3940 &spa_feature_table
[i
], &refcount
);
3942 spa
->spa_feat_refcount_cache
[i
] = refcount
;
3943 } else if (error
== ENOTSUP
) {
3944 spa
->spa_feat_refcount_cache
[i
] =
3945 SPA_FEATURE_DISABLED
;
3947 spa_load_failed(spa
, "error getting refcount "
3948 "for feature %s [error=%d]",
3949 spa_feature_table
[i
].fi_guid
, error
);
3950 return (spa_vdev_err(rvd
,
3951 VDEV_AUX_CORRUPT_DATA
, EIO
));
3956 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
3957 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
3958 &spa
->spa_feat_enabled_txg_obj
, B_TRUE
) != 0)
3959 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3963 * Encryption was added before bookmark_v2, even though bookmark_v2
3964 * is now a dependency. If this pool has encryption enabled without
3965 * bookmark_v2, trigger an errata message.
3967 if (spa_feature_is_enabled(spa
, SPA_FEATURE_ENCRYPTION
) &&
3968 !spa_feature_is_enabled(spa
, SPA_FEATURE_BOOKMARK_V2
)) {
3969 spa
->spa_errata
= ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
;
3976 spa_ld_load_special_directories(spa_t
*spa
)
3979 vdev_t
*rvd
= spa
->spa_root_vdev
;
3981 spa
->spa_is_initializing
= B_TRUE
;
3982 error
= dsl_pool_open(spa
->spa_dsl_pool
);
3983 spa
->spa_is_initializing
= B_FALSE
;
3985 spa_load_failed(spa
, "dsl_pool_open failed [error=%d]", error
);
3986 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3993 spa_ld_get_props(spa_t
*spa
)
3997 vdev_t
*rvd
= spa
->spa_root_vdev
;
3999 /* Grab the checksum salt from the MOS. */
4000 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4001 DMU_POOL_CHECKSUM_SALT
, 1,
4002 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
4003 spa
->spa_cksum_salt
.zcs_bytes
);
4004 if (error
== ENOENT
) {
4005 /* Generate a new salt for subsequent use */
4006 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
4007 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
4008 } else if (error
!= 0) {
4009 spa_load_failed(spa
, "unable to retrieve checksum salt from "
4010 "MOS [error=%d]", error
);
4011 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4014 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
, B_TRUE
) != 0)
4015 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4016 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
4018 spa_load_failed(spa
, "error opening deferred-frees bpobj "
4019 "[error=%d]", error
);
4020 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4024 * Load the bit that tells us to use the new accounting function
4025 * (raid-z deflation). If we have an older pool, this will not
4028 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
, B_FALSE
);
4029 if (error
!= 0 && error
!= ENOENT
)
4030 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4032 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
4033 &spa
->spa_creation_version
, B_FALSE
);
4034 if (error
!= 0 && error
!= ENOENT
)
4035 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4038 * Load the persistent error log. If we have an older pool, this will
4041 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
,
4043 if (error
!= 0 && error
!= ENOENT
)
4044 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4046 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
4047 &spa
->spa_errlog_scrub
, B_FALSE
);
4048 if (error
!= 0 && error
!= ENOENT
)
4049 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4052 * Load the livelist deletion field. If a livelist is queued for
4053 * deletion, indicate that in the spa
4055 error
= spa_dir_prop(spa
, DMU_POOL_DELETED_CLONES
,
4056 &spa
->spa_livelists_to_delete
, B_FALSE
);
4057 if (error
!= 0 && error
!= ENOENT
)
4058 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4061 * Load the history object. If we have an older pool, this
4062 * will not be present.
4064 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
, B_FALSE
);
4065 if (error
!= 0 && error
!= ENOENT
)
4066 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4069 * Load the per-vdev ZAP map. If we have an older pool, this will not
4070 * be present; in this case, defer its creation to a later time to
4071 * avoid dirtying the MOS this early / out of sync context. See
4072 * spa_sync_config_object.
4075 /* The sentinel is only available in the MOS config. */
4076 nvlist_t
*mos_config
;
4077 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0) {
4078 spa_load_failed(spa
, "unable to retrieve MOS config");
4079 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4082 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
4083 &spa
->spa_all_vdev_zaps
, B_FALSE
);
4085 if (error
== ENOENT
) {
4086 VERIFY(!nvlist_exists(mos_config
,
4087 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
4088 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
4089 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4090 } else if (error
!= 0) {
4091 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4092 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
4094 * An older version of ZFS overwrote the sentinel value, so
4095 * we have orphaned per-vdev ZAPs in the MOS. Defer their
4096 * destruction to later; see spa_sync_config_object.
4098 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
4100 * We're assuming that no vdevs have had their ZAPs created
4101 * before this. Better be sure of it.
4103 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4105 nvlist_free(mos_config
);
4107 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
4109 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
,
4111 if (error
&& error
!= ENOENT
)
4112 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4115 uint64_t autoreplace
;
4117 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
4118 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
4119 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
4120 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
4121 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
4122 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
4123 spa_prop_find(spa
, ZPOOL_PROP_AUTOTRIM
, &spa
->spa_autotrim
);
4124 spa
->spa_autoreplace
= (autoreplace
!= 0);
4128 * If we are importing a pool with missing top-level vdevs,
4129 * we enforce that the pool doesn't panic or get suspended on
4130 * error since the likelihood of missing data is extremely high.
4132 if (spa
->spa_missing_tvds
> 0 &&
4133 spa
->spa_failmode
!= ZIO_FAILURE_MODE_CONTINUE
&&
4134 spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4135 spa_load_note(spa
, "forcing failmode to 'continue' "
4136 "as some top level vdevs are missing");
4137 spa
->spa_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
4144 spa_ld_open_aux_vdevs(spa_t
*spa
, spa_import_type_t type
)
4147 vdev_t
*rvd
= spa
->spa_root_vdev
;
4150 * If we're assembling the pool from the split-off vdevs of
4151 * an existing pool, we don't want to attach the spares & cache
4156 * Load any hot spares for this pool.
4158 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
,
4160 if (error
!= 0 && error
!= ENOENT
)
4161 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4162 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4163 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
4164 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
4165 &spa
->spa_spares
.sav_config
) != 0) {
4166 spa_load_failed(spa
, "error loading spares nvlist");
4167 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4170 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4171 spa_load_spares(spa
);
4172 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4173 } else if (error
== 0) {
4174 spa
->spa_spares
.sav_sync
= B_TRUE
;
4178 * Load any level 2 ARC devices for this pool.
4180 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
4181 &spa
->spa_l2cache
.sav_object
, B_FALSE
);
4182 if (error
!= 0 && error
!= ENOENT
)
4183 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4184 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4185 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
4186 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
4187 &spa
->spa_l2cache
.sav_config
) != 0) {
4188 spa_load_failed(spa
, "error loading l2cache nvlist");
4189 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4192 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4193 spa_load_l2cache(spa
);
4194 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4195 } else if (error
== 0) {
4196 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4203 spa_ld_load_vdev_metadata(spa_t
*spa
)
4206 vdev_t
*rvd
= spa
->spa_root_vdev
;
4209 * If the 'multihost' property is set, then never allow a pool to
4210 * be imported when the system hostid is zero. The exception to
4211 * this rule is zdb which is always allowed to access pools.
4213 if (spa_multihost(spa
) && spa_get_hostid(spa
) == 0 &&
4214 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
4215 fnvlist_add_uint64(spa
->spa_load_info
,
4216 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
4217 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
4221 * If the 'autoreplace' property is set, then post a resource notifying
4222 * the ZFS DE that it should not issue any faults for unopenable
4223 * devices. We also iterate over the vdevs, and post a sysevent for any
4224 * unopenable vdevs so that the normal autoreplace handler can take
4227 if (spa
->spa_autoreplace
&& spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4228 spa_check_removed(spa
->spa_root_vdev
);
4230 * For the import case, this is done in spa_import(), because
4231 * at this point we're using the spare definitions from
4232 * the MOS config, not necessarily from the userland config.
4234 if (spa
->spa_load_state
!= SPA_LOAD_IMPORT
) {
4235 spa_aux_check_removed(&spa
->spa_spares
);
4236 spa_aux_check_removed(&spa
->spa_l2cache
);
4241 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
4243 error
= vdev_load(rvd
);
4245 spa_load_failed(spa
, "vdev_load failed [error=%d]", error
);
4246 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4249 error
= spa_ld_log_spacemaps(spa
);
4251 spa_load_failed(spa
, "spa_ld_log_sm_data failed [error=%d]",
4253 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4257 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
4259 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4260 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
, B_FALSE
);
4261 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4267 spa_ld_load_dedup_tables(spa_t
*spa
)
4270 vdev_t
*rvd
= spa
->spa_root_vdev
;
4272 error
= ddt_load(spa
);
4274 spa_load_failed(spa
, "ddt_load failed [error=%d]", error
);
4275 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4282 spa_ld_verify_logs(spa_t
*spa
, spa_import_type_t type
, char **ereport
)
4284 vdev_t
*rvd
= spa
->spa_root_vdev
;
4286 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
)) {
4287 boolean_t missing
= spa_check_logs(spa
);
4289 if (spa
->spa_missing_tvds
!= 0) {
4290 spa_load_note(spa
, "spa_check_logs failed "
4291 "so dropping the logs");
4293 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
4294 spa_load_failed(spa
, "spa_check_logs failed");
4295 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
,
4305 spa_ld_verify_pool_data(spa_t
*spa
)
4308 vdev_t
*rvd
= spa
->spa_root_vdev
;
4311 * We've successfully opened the pool, verify that we're ready
4312 * to start pushing transactions.
4314 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4315 error
= spa_load_verify(spa
);
4317 spa_load_failed(spa
, "spa_load_verify failed "
4318 "[error=%d]", error
);
4319 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
4328 spa_ld_claim_log_blocks(spa_t
*spa
)
4331 dsl_pool_t
*dp
= spa_get_dsl(spa
);
4334 * Claim log blocks that haven't been committed yet.
4335 * This must all happen in a single txg.
4336 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
4337 * invoked from zil_claim_log_block()'s i/o done callback.
4338 * Price of rollback is that we abandon the log.
4340 spa
->spa_claiming
= B_TRUE
;
4342 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
4343 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
4344 zil_claim
, tx
, DS_FIND_CHILDREN
);
4347 spa
->spa_claiming
= B_FALSE
;
4349 spa_set_log_state(spa
, SPA_LOG_GOOD
);
4353 spa_ld_check_for_config_update(spa_t
*spa
, uint64_t config_cache_txg
,
4354 boolean_t update_config_cache
)
4356 vdev_t
*rvd
= spa
->spa_root_vdev
;
4357 int need_update
= B_FALSE
;
4360 * If the config cache is stale, or we have uninitialized
4361 * metaslabs (see spa_vdev_add()), then update the config.
4363 * If this is a verbatim import, trust the current
4364 * in-core spa_config and update the disk labels.
4366 if (update_config_cache
|| config_cache_txg
!= spa
->spa_config_txg
||
4367 spa
->spa_load_state
== SPA_LOAD_IMPORT
||
4368 spa
->spa_load_state
== SPA_LOAD_RECOVER
||
4369 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
4370 need_update
= B_TRUE
;
4372 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
4373 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
4374 need_update
= B_TRUE
;
4377 * Update the config cache asynchronously in case we're the
4378 * root pool, in which case the config cache isn't writable yet.
4381 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
4385 spa_ld_prepare_for_reload(spa_t
*spa
)
4387 spa_mode_t mode
= spa
->spa_mode
;
4388 int async_suspended
= spa
->spa_async_suspended
;
4391 spa_deactivate(spa
);
4392 spa_activate(spa
, mode
);
4395 * We save the value of spa_async_suspended as it gets reset to 0 by
4396 * spa_unload(). We want to restore it back to the original value before
4397 * returning as we might be calling spa_async_resume() later.
4399 spa
->spa_async_suspended
= async_suspended
;
4403 spa_ld_read_checkpoint_txg(spa_t
*spa
)
4405 uberblock_t checkpoint
;
4408 ASSERT0(spa
->spa_checkpoint_txg
);
4409 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4411 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4412 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
4413 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
4415 if (error
== ENOENT
)
4421 ASSERT3U(checkpoint
.ub_txg
, !=, 0);
4422 ASSERT3U(checkpoint
.ub_checkpoint_txg
, !=, 0);
4423 ASSERT3U(checkpoint
.ub_timestamp
, !=, 0);
4424 spa
->spa_checkpoint_txg
= checkpoint
.ub_txg
;
4425 spa
->spa_checkpoint_info
.sci_timestamp
= checkpoint
.ub_timestamp
;
4431 spa_ld_mos_init(spa_t
*spa
, spa_import_type_t type
)
4435 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4436 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4439 * Never trust the config that is provided unless we are assembling
4440 * a pool following a split.
4441 * This means don't trust blkptrs and the vdev tree in general. This
4442 * also effectively puts the spa in read-only mode since
4443 * spa_writeable() checks for spa_trust_config to be true.
4444 * We will later load a trusted config from the MOS.
4446 if (type
!= SPA_IMPORT_ASSEMBLE
)
4447 spa
->spa_trust_config
= B_FALSE
;
4450 * Parse the config provided to create a vdev tree.
4452 error
= spa_ld_parse_config(spa
, type
);
4456 spa_import_progress_add(spa
);
4459 * Now that we have the vdev tree, try to open each vdev. This involves
4460 * opening the underlying physical device, retrieving its geometry and
4461 * probing the vdev with a dummy I/O. The state of each vdev will be set
4462 * based on the success of those operations. After this we'll be ready
4463 * to read from the vdevs.
4465 error
= spa_ld_open_vdevs(spa
);
4470 * Read the label of each vdev and make sure that the GUIDs stored
4471 * there match the GUIDs in the config provided.
4472 * If we're assembling a new pool that's been split off from an
4473 * existing pool, the labels haven't yet been updated so we skip
4474 * validation for now.
4476 if (type
!= SPA_IMPORT_ASSEMBLE
) {
4477 error
= spa_ld_validate_vdevs(spa
);
4483 * Read all vdev labels to find the best uberblock (i.e. latest,
4484 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
4485 * get the list of features required to read blkptrs in the MOS from
4486 * the vdev label with the best uberblock and verify that our version
4487 * of zfs supports them all.
4489 error
= spa_ld_select_uberblock(spa
, type
);
4494 * Pass that uberblock to the dsl_pool layer which will open the root
4495 * blkptr. This blkptr points to the latest version of the MOS and will
4496 * allow us to read its contents.
4498 error
= spa_ld_open_rootbp(spa
);
4506 spa_ld_checkpoint_rewind(spa_t
*spa
)
4508 uberblock_t checkpoint
;
4511 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4512 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4514 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4515 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
4516 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
4519 spa_load_failed(spa
, "unable to retrieve checkpointed "
4520 "uberblock from the MOS config [error=%d]", error
);
4522 if (error
== ENOENT
)
4523 error
= ZFS_ERR_NO_CHECKPOINT
;
4528 ASSERT3U(checkpoint
.ub_txg
, <, spa
->spa_uberblock
.ub_txg
);
4529 ASSERT3U(checkpoint
.ub_txg
, ==, checkpoint
.ub_checkpoint_txg
);
4532 * We need to update the txg and timestamp of the checkpointed
4533 * uberblock to be higher than the latest one. This ensures that
4534 * the checkpointed uberblock is selected if we were to close and
4535 * reopen the pool right after we've written it in the vdev labels.
4536 * (also see block comment in vdev_uberblock_compare)
4538 checkpoint
.ub_txg
= spa
->spa_uberblock
.ub_txg
+ 1;
4539 checkpoint
.ub_timestamp
= gethrestime_sec();
4542 * Set current uberblock to be the checkpointed uberblock.
4544 spa
->spa_uberblock
= checkpoint
;
4547 * If we are doing a normal rewind, then the pool is open for
4548 * writing and we sync the "updated" checkpointed uberblock to
4549 * disk. Once this is done, we've basically rewound the whole
4550 * pool and there is no way back.
4552 * There are cases when we don't want to attempt and sync the
4553 * checkpointed uberblock to disk because we are opening a
4554 * pool as read-only. Specifically, verifying the checkpointed
4555 * state with zdb, and importing the checkpointed state to get
4556 * a "preview" of its content.
4558 if (spa_writeable(spa
)) {
4559 vdev_t
*rvd
= spa
->spa_root_vdev
;
4561 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4562 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
4564 int children
= rvd
->vdev_children
;
4565 int c0
= spa_get_random(children
);
4567 for (int c
= 0; c
< children
; c
++) {
4568 vdev_t
*vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
4570 /* Stop when revisiting the first vdev */
4571 if (c
> 0 && svd
[0] == vd
)
4574 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
4575 !vdev_is_concrete(vd
))
4578 svd
[svdcount
++] = vd
;
4579 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
4582 error
= vdev_config_sync(svd
, svdcount
, spa
->spa_first_txg
);
4584 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
4585 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4588 spa_load_failed(spa
, "failed to write checkpointed "
4589 "uberblock to the vdev labels [error=%d]", error
);
4598 spa_ld_mos_with_trusted_config(spa_t
*spa
, spa_import_type_t type
,
4599 boolean_t
*update_config_cache
)
4604 * Parse the config for pool, open and validate vdevs,
4605 * select an uberblock, and use that uberblock to open
4608 error
= spa_ld_mos_init(spa
, type
);
4613 * Retrieve the trusted config stored in the MOS and use it to create
4614 * a new, exact version of the vdev tree, then reopen all vdevs.
4616 error
= spa_ld_trusted_config(spa
, type
, B_FALSE
);
4617 if (error
== EAGAIN
) {
4618 if (update_config_cache
!= NULL
)
4619 *update_config_cache
= B_TRUE
;
4622 * Redo the loading process with the trusted config if it is
4623 * too different from the untrusted config.
4625 spa_ld_prepare_for_reload(spa
);
4626 spa_load_note(spa
, "RELOADING");
4627 error
= spa_ld_mos_init(spa
, type
);
4631 error
= spa_ld_trusted_config(spa
, type
, B_TRUE
);
4635 } else if (error
!= 0) {
4643 * Load an existing storage pool, using the config provided. This config
4644 * describes which vdevs are part of the pool and is later validated against
4645 * partial configs present in each vdev's label and an entire copy of the
4646 * config stored in the MOS.
4649 spa_load_impl(spa_t
*spa
, spa_import_type_t type
, char **ereport
)
4652 boolean_t missing_feat_write
= B_FALSE
;
4653 boolean_t checkpoint_rewind
=
4654 (spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4655 boolean_t update_config_cache
= B_FALSE
;
4657 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4658 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4660 spa_load_note(spa
, "LOADING");
4662 error
= spa_ld_mos_with_trusted_config(spa
, type
, &update_config_cache
);
4667 * If we are rewinding to the checkpoint then we need to repeat
4668 * everything we've done so far in this function but this time
4669 * selecting the checkpointed uberblock and using that to open
4672 if (checkpoint_rewind
) {
4674 * If we are rewinding to the checkpoint update config cache
4677 update_config_cache
= B_TRUE
;
4680 * Extract the checkpointed uberblock from the current MOS
4681 * and use this as the pool's uberblock from now on. If the
4682 * pool is imported as writeable we also write the checkpoint
4683 * uberblock to the labels, making the rewind permanent.
4685 error
= spa_ld_checkpoint_rewind(spa
);
4690 * Redo the loading process again with the
4691 * checkpointed uberblock.
4693 spa_ld_prepare_for_reload(spa
);
4694 spa_load_note(spa
, "LOADING checkpointed uberblock");
4695 error
= spa_ld_mos_with_trusted_config(spa
, type
, NULL
);
4701 * Retrieve the checkpoint txg if the pool has a checkpoint.
4703 error
= spa_ld_read_checkpoint_txg(spa
);
4708 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
4709 * from the pool and their contents were re-mapped to other vdevs. Note
4710 * that everything that we read before this step must have been
4711 * rewritten on concrete vdevs after the last device removal was
4712 * initiated. Otherwise we could be reading from indirect vdevs before
4713 * we have loaded their mappings.
4715 error
= spa_ld_open_indirect_vdev_metadata(spa
);
4720 * Retrieve the full list of active features from the MOS and check if
4721 * they are all supported.
4723 error
= spa_ld_check_features(spa
, &missing_feat_write
);
4728 * Load several special directories from the MOS needed by the dsl_pool
4731 error
= spa_ld_load_special_directories(spa
);
4736 * Retrieve pool properties from the MOS.
4738 error
= spa_ld_get_props(spa
);
4743 * Retrieve the list of auxiliary devices - cache devices and spares -
4746 error
= spa_ld_open_aux_vdevs(spa
, type
);
4751 * Load the metadata for all vdevs. Also check if unopenable devices
4752 * should be autoreplaced.
4754 error
= spa_ld_load_vdev_metadata(spa
);
4758 error
= spa_ld_load_dedup_tables(spa
);
4763 * Verify the logs now to make sure we don't have any unexpected errors
4764 * when we claim log blocks later.
4766 error
= spa_ld_verify_logs(spa
, type
, ereport
);
4770 if (missing_feat_write
) {
4771 ASSERT(spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
);
4774 * At this point, we know that we can open the pool in
4775 * read-only mode but not read-write mode. We now have enough
4776 * information and can return to userland.
4778 return (spa_vdev_err(spa
->spa_root_vdev
, VDEV_AUX_UNSUP_FEAT
,
4783 * Traverse the last txgs to make sure the pool was left off in a safe
4784 * state. When performing an extreme rewind, we verify the whole pool,
4785 * which can take a very long time.
4787 error
= spa_ld_verify_pool_data(spa
);
4792 * Calculate the deflated space for the pool. This must be done before
4793 * we write anything to the pool because we'd need to update the space
4794 * accounting using the deflated sizes.
4796 spa_update_dspace(spa
);
4799 * We have now retrieved all the information we needed to open the
4800 * pool. If we are importing the pool in read-write mode, a few
4801 * additional steps must be performed to finish the import.
4803 if (spa_writeable(spa
) && (spa
->spa_load_state
== SPA_LOAD_RECOVER
||
4804 spa
->spa_load_max_txg
== UINT64_MAX
)) {
4805 uint64_t config_cache_txg
= spa
->spa_config_txg
;
4807 ASSERT(spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
);
4810 * In case of a checkpoint rewind, log the original txg
4811 * of the checkpointed uberblock.
4813 if (checkpoint_rewind
) {
4814 spa_history_log_internal(spa
, "checkpoint rewind",
4815 NULL
, "rewound state to txg=%llu",
4816 (u_longlong_t
)spa
->spa_uberblock
.ub_checkpoint_txg
);
4820 * Traverse the ZIL and claim all blocks.
4822 spa_ld_claim_log_blocks(spa
);
4825 * Kick-off the syncing thread.
4827 spa
->spa_sync_on
= B_TRUE
;
4828 txg_sync_start(spa
->spa_dsl_pool
);
4829 mmp_thread_start(spa
);
4832 * Wait for all claims to sync. We sync up to the highest
4833 * claimed log block birth time so that claimed log blocks
4834 * don't appear to be from the future. spa_claim_max_txg
4835 * will have been set for us by ZIL traversal operations
4838 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
4841 * Check if we need to request an update of the config. On the
4842 * next sync, we would update the config stored in vdev labels
4843 * and the cachefile (by default /etc/zfs/zpool.cache).
4845 spa_ld_check_for_config_update(spa
, config_cache_txg
,
4846 update_config_cache
);
4849 * Check if a rebuild was in progress and if so resume it.
4850 * Then check all DTLs to see if anything needs resilvering.
4851 * The resilver will be deferred if a rebuild was started.
4853 if (vdev_rebuild_active(spa
->spa_root_vdev
)) {
4854 vdev_rebuild_restart(spa
);
4855 } else if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
4856 vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
4857 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
4861 * Log the fact that we booted up (so that we can detect if
4862 * we rebooted in the middle of an operation).
4864 spa_history_log_version(spa
, "open", NULL
);
4866 spa_restart_removal(spa
);
4867 spa_spawn_aux_threads(spa
);
4870 * Delete any inconsistent datasets.
4873 * Since we may be issuing deletes for clones here,
4874 * we make sure to do so after we've spawned all the
4875 * auxiliary threads above (from which the livelist
4876 * deletion zthr is part of).
4878 (void) dmu_objset_find(spa_name(spa
),
4879 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
4882 * Clean up any stale temporary dataset userrefs.
4884 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
4886 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4887 vdev_initialize_restart(spa
->spa_root_vdev
);
4888 vdev_trim_restart(spa
->spa_root_vdev
);
4889 vdev_autotrim_restart(spa
);
4890 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4893 spa_import_progress_remove(spa_guid(spa
));
4894 spa_async_request(spa
, SPA_ASYNC_L2CACHE_REBUILD
);
4896 spa_load_note(spa
, "LOADED");
4902 spa_load_retry(spa_t
*spa
, spa_load_state_t state
)
4904 spa_mode_t mode
= spa
->spa_mode
;
4907 spa_deactivate(spa
);
4909 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
4911 spa_activate(spa
, mode
);
4912 spa_async_suspend(spa
);
4914 spa_load_note(spa
, "spa_load_retry: rewind, max txg: %llu",
4915 (u_longlong_t
)spa
->spa_load_max_txg
);
4917 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
));
4921 * If spa_load() fails this function will try loading prior txg's. If
4922 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
4923 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
4924 * function will not rewind the pool and will return the same error as
4928 spa_load_best(spa_t
*spa
, spa_load_state_t state
, uint64_t max_request
,
4931 nvlist_t
*loadinfo
= NULL
;
4932 nvlist_t
*config
= NULL
;
4933 int load_error
, rewind_error
;
4934 uint64_t safe_rewind_txg
;
4937 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
4938 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
4939 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4941 spa
->spa_load_max_txg
= max_request
;
4942 if (max_request
!= UINT64_MAX
)
4943 spa
->spa_extreme_rewind
= B_TRUE
;
4946 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
);
4947 if (load_error
== 0)
4949 if (load_error
== ZFS_ERR_NO_CHECKPOINT
) {
4951 * When attempting checkpoint-rewind on a pool with no
4952 * checkpoint, we should not attempt to load uberblocks
4953 * from previous txgs when spa_load fails.
4955 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4956 spa_import_progress_remove(spa_guid(spa
));
4957 return (load_error
);
4960 if (spa
->spa_root_vdev
!= NULL
)
4961 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4963 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
4964 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
4966 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
4967 nvlist_free(config
);
4968 spa_import_progress_remove(spa_guid(spa
));
4969 return (load_error
);
4972 if (state
== SPA_LOAD_RECOVER
) {
4973 /* Price of rolling back is discarding txgs, including log */
4974 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4977 * If we aren't rolling back save the load info from our first
4978 * import attempt so that we can restore it after attempting
4981 loadinfo
= spa
->spa_load_info
;
4982 spa
->spa_load_info
= fnvlist_alloc();
4985 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
4986 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
4987 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
4988 TXG_INITIAL
: safe_rewind_txg
;
4991 * Continue as long as we're finding errors, we're still within
4992 * the acceptable rewind range, and we're still finding uberblocks
4994 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
4995 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
4996 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
4997 spa
->spa_extreme_rewind
= B_TRUE
;
4998 rewind_error
= spa_load_retry(spa
, state
);
5001 spa
->spa_extreme_rewind
= B_FALSE
;
5002 spa
->spa_load_max_txg
= UINT64_MAX
;
5004 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
5005 spa_config_set(spa
, config
);
5007 nvlist_free(config
);
5009 if (state
== SPA_LOAD_RECOVER
) {
5010 ASSERT3P(loadinfo
, ==, NULL
);
5011 spa_import_progress_remove(spa_guid(spa
));
5012 return (rewind_error
);
5014 /* Store the rewind info as part of the initial load info */
5015 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
5016 spa
->spa_load_info
);
5018 /* Restore the initial load info */
5019 fnvlist_free(spa
->spa_load_info
);
5020 spa
->spa_load_info
= loadinfo
;
5022 spa_import_progress_remove(spa_guid(spa
));
5023 return (load_error
);
5030 * The import case is identical to an open except that the configuration is sent
5031 * down from userland, instead of grabbed from the configuration cache. For the
5032 * case of an open, the pool configuration will exist in the
5033 * POOL_STATE_UNINITIALIZED state.
5035 * The stats information (gen/count/ustats) is used to gather vdev statistics at
5036 * the same time open the pool, without having to keep around the spa_t in some
5040 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
5044 spa_load_state_t state
= SPA_LOAD_OPEN
;
5046 int locked
= B_FALSE
;
5047 int firstopen
= B_FALSE
;
5052 * As disgusting as this is, we need to support recursive calls to this
5053 * function because dsl_dir_open() is called during spa_load(), and ends
5054 * up calling spa_open() again. The real fix is to figure out how to
5055 * avoid dsl_dir_open() calling this in the first place.
5057 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
5058 mutex_enter(&spa_namespace_lock
);
5062 if ((spa
= spa_lookup(pool
)) == NULL
) {
5064 mutex_exit(&spa_namespace_lock
);
5065 return (SET_ERROR(ENOENT
));
5068 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
5069 zpool_load_policy_t policy
;
5073 zpool_get_load_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
5075 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
5076 state
= SPA_LOAD_RECOVER
;
5078 spa_activate(spa
, spa_mode_global
);
5080 if (state
!= SPA_LOAD_RECOVER
)
5081 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
5082 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
5084 zfs_dbgmsg("spa_open_common: opening %s", pool
);
5085 error
= spa_load_best(spa
, state
, policy
.zlp_txg
,
5088 if (error
== EBADF
) {
5090 * If vdev_validate() returns failure (indicated by
5091 * EBADF), it indicates that one of the vdevs indicates
5092 * that the pool has been exported or destroyed. If
5093 * this is the case, the config cache is out of sync and
5094 * we should remove the pool from the namespace.
5097 spa_deactivate(spa
);
5098 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
5101 mutex_exit(&spa_namespace_lock
);
5102 return (SET_ERROR(ENOENT
));
5107 * We can't open the pool, but we still have useful
5108 * information: the state of each vdev after the
5109 * attempted vdev_open(). Return this to the user.
5111 if (config
!= NULL
&& spa
->spa_config
) {
5112 VERIFY(nvlist_dup(spa
->spa_config
, config
,
5114 VERIFY(nvlist_add_nvlist(*config
,
5115 ZPOOL_CONFIG_LOAD_INFO
,
5116 spa
->spa_load_info
) == 0);
5119 spa_deactivate(spa
);
5120 spa
->spa_last_open_failed
= error
;
5122 mutex_exit(&spa_namespace_lock
);
5128 spa_open_ref(spa
, tag
);
5131 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5134 * If we've recovered the pool, pass back any information we
5135 * gathered while doing the load.
5137 if (state
== SPA_LOAD_RECOVER
) {
5138 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
5139 spa
->spa_load_info
) == 0);
5143 spa
->spa_last_open_failed
= 0;
5144 spa
->spa_last_ubsync_txg
= 0;
5145 spa
->spa_load_txg
= 0;
5146 mutex_exit(&spa_namespace_lock
);
5150 zvol_create_minors_recursive(spa_name(spa
));
5158 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
5161 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
5165 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
5167 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
5171 * Lookup the given spa_t, incrementing the inject count in the process,
5172 * preventing it from being exported or destroyed.
5175 spa_inject_addref(char *name
)
5179 mutex_enter(&spa_namespace_lock
);
5180 if ((spa
= spa_lookup(name
)) == NULL
) {
5181 mutex_exit(&spa_namespace_lock
);
5184 spa
->spa_inject_ref
++;
5185 mutex_exit(&spa_namespace_lock
);
5191 spa_inject_delref(spa_t
*spa
)
5193 mutex_enter(&spa_namespace_lock
);
5194 spa
->spa_inject_ref
--;
5195 mutex_exit(&spa_namespace_lock
);
5199 * Add spares device information to the nvlist.
5202 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
5212 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5214 if (spa
->spa_spares
.sav_count
== 0)
5217 VERIFY(nvlist_lookup_nvlist(config
,
5218 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
5219 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5220 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
5222 VERIFY(nvlist_add_nvlist_array(nvroot
,
5223 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
5224 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
5225 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
5228 * Go through and find any spares which have since been
5229 * repurposed as an active spare. If this is the case, update
5230 * their status appropriately.
5232 for (i
= 0; i
< nspares
; i
++) {
5233 VERIFY(nvlist_lookup_uint64(spares
[i
],
5234 ZPOOL_CONFIG_GUID
, &guid
) == 0);
5235 if (spa_spare_exists(guid
, &pool
, NULL
) &&
5237 VERIFY(nvlist_lookup_uint64_array(
5238 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
5239 (uint64_t **)&vs
, &vsc
) == 0);
5240 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
5241 vs
->vs_aux
= VDEV_AUX_SPARED
;
5248 * Add l2cache device information to the nvlist, including vdev stats.
5251 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
5254 uint_t i
, j
, nl2cache
;
5261 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5263 if (spa
->spa_l2cache
.sav_count
== 0)
5266 VERIFY(nvlist_lookup_nvlist(config
,
5267 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
5268 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5269 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
5270 if (nl2cache
!= 0) {
5271 VERIFY(nvlist_add_nvlist_array(nvroot
,
5272 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
5273 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
5274 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
5277 * Update level 2 cache device stats.
5280 for (i
= 0; i
< nl2cache
; i
++) {
5281 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
5282 ZPOOL_CONFIG_GUID
, &guid
) == 0);
5285 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
5287 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
5288 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
5294 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
5295 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
5297 vdev_get_stats(vd
, vs
);
5298 vdev_config_generate_stats(vd
, l2cache
[i
]);
5305 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
5310 if (spa
->spa_feat_for_read_obj
!= 0) {
5311 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
5312 spa
->spa_feat_for_read_obj
);
5313 zap_cursor_retrieve(&zc
, &za
) == 0;
5314 zap_cursor_advance(&zc
)) {
5315 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
5316 za
.za_num_integers
== 1);
5317 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
5318 za
.za_first_integer
));
5320 zap_cursor_fini(&zc
);
5323 if (spa
->spa_feat_for_write_obj
!= 0) {
5324 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
5325 spa
->spa_feat_for_write_obj
);
5326 zap_cursor_retrieve(&zc
, &za
) == 0;
5327 zap_cursor_advance(&zc
)) {
5328 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
5329 za
.za_num_integers
== 1);
5330 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
5331 za
.za_first_integer
));
5333 zap_cursor_fini(&zc
);
5338 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
5342 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5343 zfeature_info_t feature
= spa_feature_table
[i
];
5346 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
5349 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
5354 * Store a list of pool features and their reference counts in the
5357 * The first time this is called on a spa, allocate a new nvlist, fetch
5358 * the pool features and reference counts from disk, then save the list
5359 * in the spa. In subsequent calls on the same spa use the saved nvlist
5360 * and refresh its values from the cached reference counts. This
5361 * ensures we don't block here on I/O on a suspended pool so 'zpool
5362 * clear' can resume the pool.
5365 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
5369 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5371 mutex_enter(&spa
->spa_feat_stats_lock
);
5372 features
= spa
->spa_feat_stats
;
5374 if (features
!= NULL
) {
5375 spa_feature_stats_from_cache(spa
, features
);
5377 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
5378 spa
->spa_feat_stats
= features
;
5379 spa_feature_stats_from_disk(spa
, features
);
5382 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
5385 mutex_exit(&spa
->spa_feat_stats_lock
);
5389 spa_get_stats(const char *name
, nvlist_t
**config
,
5390 char *altroot
, size_t buflen
)
5396 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
5400 * This still leaves a window of inconsistency where the spares
5401 * or l2cache devices could change and the config would be
5402 * self-inconsistent.
5404 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5406 if (*config
!= NULL
) {
5407 uint64_t loadtimes
[2];
5409 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
5410 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
5411 VERIFY(nvlist_add_uint64_array(*config
,
5412 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
5414 VERIFY(nvlist_add_uint64(*config
,
5415 ZPOOL_CONFIG_ERRCOUNT
,
5416 spa_get_errlog_size(spa
)) == 0);
5418 if (spa_suspended(spa
)) {
5419 VERIFY(nvlist_add_uint64(*config
,
5420 ZPOOL_CONFIG_SUSPENDED
,
5421 spa
->spa_failmode
) == 0);
5422 VERIFY(nvlist_add_uint64(*config
,
5423 ZPOOL_CONFIG_SUSPENDED_REASON
,
5424 spa
->spa_suspended
) == 0);
5427 spa_add_spares(spa
, *config
);
5428 spa_add_l2cache(spa
, *config
);
5429 spa_add_feature_stats(spa
, *config
);
5434 * We want to get the alternate root even for faulted pools, so we cheat
5435 * and call spa_lookup() directly.
5439 mutex_enter(&spa_namespace_lock
);
5440 spa
= spa_lookup(name
);
5442 spa_altroot(spa
, altroot
, buflen
);
5446 mutex_exit(&spa_namespace_lock
);
5448 spa_altroot(spa
, altroot
, buflen
);
5453 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5454 spa_close(spa
, FTAG
);
5461 * Validate that the auxiliary device array is well formed. We must have an
5462 * array of nvlists, each which describes a valid leaf vdev. If this is an
5463 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
5464 * specified, as long as they are well-formed.
5467 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
5468 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
5469 vdev_labeltype_t label
)
5476 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5479 * It's acceptable to have no devs specified.
5481 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
5485 return (SET_ERROR(EINVAL
));
5488 * Make sure the pool is formatted with a version that supports this
5491 if (spa_version(spa
) < version
)
5492 return (SET_ERROR(ENOTSUP
));
5495 * Set the pending device list so we correctly handle device in-use
5498 sav
->sav_pending
= dev
;
5499 sav
->sav_npending
= ndev
;
5501 for (i
= 0; i
< ndev
; i
++) {
5502 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
5506 if (!vd
->vdev_ops
->vdev_op_leaf
) {
5508 error
= SET_ERROR(EINVAL
);
5514 if ((error
= vdev_open(vd
)) == 0 &&
5515 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
5516 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
5517 vd
->vdev_guid
) == 0);
5523 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
5530 sav
->sav_pending
= NULL
;
5531 sav
->sav_npending
= 0;
5536 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
5540 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5542 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
5543 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
5544 VDEV_LABEL_SPARE
)) != 0) {
5548 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
5549 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
5550 VDEV_LABEL_L2CACHE
));
5554 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
5559 if (sav
->sav_config
!= NULL
) {
5565 * Generate new dev list by concatenating with the
5568 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
5569 &olddevs
, &oldndevs
) == 0);
5571 newdevs
= kmem_alloc(sizeof (void *) *
5572 (ndevs
+ oldndevs
), KM_SLEEP
);
5573 for (i
= 0; i
< oldndevs
; i
++)
5574 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
5576 for (i
= 0; i
< ndevs
; i
++)
5577 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
5580 VERIFY(nvlist_remove(sav
->sav_config
, config
,
5581 DATA_TYPE_NVLIST_ARRAY
) == 0);
5583 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
5584 config
, newdevs
, ndevs
+ oldndevs
) == 0);
5585 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
5586 nvlist_free(newdevs
[i
]);
5587 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
5590 * Generate a new dev list.
5592 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
5594 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
5600 * Stop and drop level 2 ARC devices
5603 spa_l2cache_drop(spa_t
*spa
)
5607 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5609 for (i
= 0; i
< sav
->sav_count
; i
++) {
5612 vd
= sav
->sav_vdevs
[i
];
5615 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
5616 pool
!= 0ULL && l2arc_vdev_present(vd
))
5617 l2arc_remove_vdev(vd
);
5622 * Verify encryption parameters for spa creation. If we are encrypting, we must
5623 * have the encryption feature flag enabled.
5626 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
5627 boolean_t has_encryption
)
5629 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
5630 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
5632 return (SET_ERROR(ENOTSUP
));
5634 return (dmu_objset_create_crypt_check(NULL
, dcp
, NULL
));
5641 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
5642 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
5645 char *altroot
= NULL
;
5650 uint64_t txg
= TXG_INITIAL
;
5651 nvlist_t
**spares
, **l2cache
;
5652 uint_t nspares
, nl2cache
;
5653 uint64_t version
, obj
, ndraid
= 0;
5654 boolean_t has_features
;
5655 boolean_t has_encryption
;
5656 boolean_t has_allocclass
;
5662 if (props
== NULL
||
5663 nvlist_lookup_string(props
, "tname", &poolname
) != 0)
5664 poolname
= (char *)pool
;
5667 * If this pool already exists, return failure.
5669 mutex_enter(&spa_namespace_lock
);
5670 if (spa_lookup(poolname
) != NULL
) {
5671 mutex_exit(&spa_namespace_lock
);
5672 return (SET_ERROR(EEXIST
));
5676 * Allocate a new spa_t structure.
5678 nvl
= fnvlist_alloc();
5679 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
5680 (void) nvlist_lookup_string(props
,
5681 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5682 spa
= spa_add(poolname
, nvl
, altroot
);
5684 spa_activate(spa
, spa_mode_global
);
5686 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
5687 spa_deactivate(spa
);
5689 mutex_exit(&spa_namespace_lock
);
5694 * Temporary pool names should never be written to disk.
5696 if (poolname
!= pool
)
5697 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
5699 has_features
= B_FALSE
;
5700 has_encryption
= B_FALSE
;
5701 has_allocclass
= B_FALSE
;
5702 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
5703 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
5704 if (zpool_prop_feature(nvpair_name(elem
))) {
5705 has_features
= B_TRUE
;
5707 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
5708 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
5709 if (feat
== SPA_FEATURE_ENCRYPTION
)
5710 has_encryption
= B_TRUE
;
5711 if (feat
== SPA_FEATURE_ALLOCATION_CLASSES
)
5712 has_allocclass
= B_TRUE
;
5716 /* verify encryption params, if they were provided */
5718 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
5720 spa_deactivate(spa
);
5722 mutex_exit(&spa_namespace_lock
);
5726 if (!has_allocclass
&& zfs_special_devs(nvroot
, NULL
)) {
5727 spa_deactivate(spa
);
5729 mutex_exit(&spa_namespace_lock
);
5733 if (has_features
|| nvlist_lookup_uint64(props
,
5734 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
5735 version
= SPA_VERSION
;
5737 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
5739 spa
->spa_first_txg
= txg
;
5740 spa
->spa_uberblock
.ub_txg
= txg
- 1;
5741 spa
->spa_uberblock
.ub_version
= version
;
5742 spa
->spa_ubsync
= spa
->spa_uberblock
;
5743 spa
->spa_load_state
= SPA_LOAD_CREATE
;
5744 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
5745 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
5746 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
5747 spa
->spa_indirect_vdevs_loaded
= B_TRUE
;
5750 * Create "The Godfather" zio to hold all async IOs
5752 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
5754 for (int i
= 0; i
< max_ncpus
; i
++) {
5755 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
5756 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
5757 ZIO_FLAG_GODFATHER
);
5761 * Create the root vdev.
5763 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5765 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
5767 ASSERT(error
!= 0 || rvd
!= NULL
);
5768 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
5770 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
5771 error
= SET_ERROR(EINVAL
);
5774 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
5775 (error
= vdev_draid_spare_create(nvroot
, rvd
, &ndraid
, 0)) == 0 &&
5776 (error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) == 0) {
5778 * instantiate the metaslab groups (this will dirty the vdevs)
5779 * we can no longer error exit past this point
5781 for (int c
= 0; error
== 0 && c
< rvd
->vdev_children
; c
++) {
5782 vdev_t
*vd
= rvd
->vdev_child
[c
];
5784 vdev_metaslab_set_size(vd
);
5785 vdev_expand(vd
, txg
);
5789 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5793 spa_deactivate(spa
);
5795 mutex_exit(&spa_namespace_lock
);
5800 * Get the list of spares, if specified.
5802 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5803 &spares
, &nspares
) == 0) {
5804 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
5806 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
5807 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
5808 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5809 spa_load_spares(spa
);
5810 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5811 spa
->spa_spares
.sav_sync
= B_TRUE
;
5815 * Get the list of level 2 cache devices, if specified.
5817 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5818 &l2cache
, &nl2cache
) == 0) {
5819 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
5820 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5821 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
5822 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
5823 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5824 spa_load_l2cache(spa
);
5825 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5826 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5829 spa
->spa_is_initializing
= B_TRUE
;
5830 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
5831 spa
->spa_is_initializing
= B_FALSE
;
5834 * Create DDTs (dedup tables).
5838 spa_update_dspace(spa
);
5840 tx
= dmu_tx_create_assigned(dp
, txg
);
5843 * Create the pool's history object.
5845 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
5846 spa_history_create_obj(spa
, tx
);
5848 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
5849 spa_history_log_version(spa
, "create", tx
);
5852 * Create the pool config object.
5854 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
5855 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
5856 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
5858 if (zap_add(spa
->spa_meta_objset
,
5859 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
5860 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
5861 cmn_err(CE_PANIC
, "failed to add pool config");
5864 if (zap_add(spa
->spa_meta_objset
,
5865 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
5866 sizeof (uint64_t), 1, &version
, tx
) != 0) {
5867 cmn_err(CE_PANIC
, "failed to add pool version");
5870 /* Newly created pools with the right version are always deflated. */
5871 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
5872 spa
->spa_deflate
= TRUE
;
5873 if (zap_add(spa
->spa_meta_objset
,
5874 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
5875 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
5876 cmn_err(CE_PANIC
, "failed to add deflate");
5881 * Create the deferred-free bpobj. Turn off compression
5882 * because sync-to-convergence takes longer if the blocksize
5885 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
5886 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
5887 ZIO_COMPRESS_OFF
, tx
);
5888 if (zap_add(spa
->spa_meta_objset
,
5889 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
5890 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
5891 cmn_err(CE_PANIC
, "failed to add bpobj");
5893 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
5894 spa
->spa_meta_objset
, obj
));
5897 * Generate some random noise for salted checksums to operate on.
5899 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
5900 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
5903 * Set pool properties.
5905 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
5906 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
5907 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
5908 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
5909 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
5910 spa
->spa_autotrim
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM
);
5912 if (props
!= NULL
) {
5913 spa_configfile_set(spa
, props
, B_FALSE
);
5914 spa_sync_props(props
, tx
);
5917 for (int i
= 0; i
< ndraid
; i
++)
5918 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
5922 spa
->spa_sync_on
= B_TRUE
;
5924 mmp_thread_start(spa
);
5925 txg_wait_synced(dp
, txg
);
5927 spa_spawn_aux_threads(spa
);
5929 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
5932 * Don't count references from objsets that are already closed
5933 * and are making their way through the eviction process.
5935 spa_evicting_os_wait(spa
);
5936 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
5937 spa
->spa_load_state
= SPA_LOAD_NONE
;
5939 mutex_exit(&spa_namespace_lock
);
5945 * Import a non-root pool into the system.
5948 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
5951 char *altroot
= NULL
;
5952 spa_load_state_t state
= SPA_LOAD_IMPORT
;
5953 zpool_load_policy_t policy
;
5954 spa_mode_t mode
= spa_mode_global
;
5955 uint64_t readonly
= B_FALSE
;
5958 nvlist_t
**spares
, **l2cache
;
5959 uint_t nspares
, nl2cache
;
5962 * If a pool with this name exists, return failure.
5964 mutex_enter(&spa_namespace_lock
);
5965 if (spa_lookup(pool
) != NULL
) {
5966 mutex_exit(&spa_namespace_lock
);
5967 return (SET_ERROR(EEXIST
));
5971 * Create and initialize the spa structure.
5973 (void) nvlist_lookup_string(props
,
5974 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5975 (void) nvlist_lookup_uint64(props
,
5976 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
5978 mode
= SPA_MODE_READ
;
5979 spa
= spa_add(pool
, config
, altroot
);
5980 spa
->spa_import_flags
= flags
;
5983 * Verbatim import - Take a pool and insert it into the namespace
5984 * as if it had been loaded at boot.
5986 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
5988 spa_configfile_set(spa
, props
, B_FALSE
);
5990 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
5991 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
5992 zfs_dbgmsg("spa_import: verbatim import of %s", pool
);
5993 mutex_exit(&spa_namespace_lock
);
5997 spa_activate(spa
, mode
);
6000 * Don't start async tasks until we know everything is healthy.
6002 spa_async_suspend(spa
);
6004 zpool_get_load_policy(config
, &policy
);
6005 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
6006 state
= SPA_LOAD_RECOVER
;
6008 spa
->spa_config_source
= SPA_CONFIG_SRC_TRYIMPORT
;
6010 if (state
!= SPA_LOAD_RECOVER
) {
6011 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
6012 zfs_dbgmsg("spa_import: importing %s", pool
);
6014 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
6015 "(RECOVERY MODE)", pool
, (longlong_t
)policy
.zlp_txg
);
6017 error
= spa_load_best(spa
, state
, policy
.zlp_txg
, policy
.zlp_rewind
);
6020 * Propagate anything learned while loading the pool and pass it
6021 * back to caller (i.e. rewind info, missing devices, etc).
6023 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
6024 spa
->spa_load_info
) == 0);
6026 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6028 * Toss any existing sparelist, as it doesn't have any validity
6029 * anymore, and conflicts with spa_has_spare().
6031 if (spa
->spa_spares
.sav_config
) {
6032 nvlist_free(spa
->spa_spares
.sav_config
);
6033 spa
->spa_spares
.sav_config
= NULL
;
6034 spa_load_spares(spa
);
6036 if (spa
->spa_l2cache
.sav_config
) {
6037 nvlist_free(spa
->spa_l2cache
.sav_config
);
6038 spa
->spa_l2cache
.sav_config
= NULL
;
6039 spa_load_l2cache(spa
);
6042 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
6044 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6047 spa_configfile_set(spa
, props
, B_FALSE
);
6049 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
6050 (error
= spa_prop_set(spa
, props
)))) {
6052 spa_deactivate(spa
);
6054 mutex_exit(&spa_namespace_lock
);
6058 spa_async_resume(spa
);
6061 * Override any spares and level 2 cache devices as specified by
6062 * the user, as these may have correct device names/devids, etc.
6064 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6065 &spares
, &nspares
) == 0) {
6066 if (spa
->spa_spares
.sav_config
)
6067 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
6068 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
6070 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
6071 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6072 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6073 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
6074 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6075 spa_load_spares(spa
);
6076 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6077 spa
->spa_spares
.sav_sync
= B_TRUE
;
6079 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6080 &l2cache
, &nl2cache
) == 0) {
6081 if (spa
->spa_l2cache
.sav_config
)
6082 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
6083 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
6085 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
6086 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6087 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6088 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
6089 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6090 spa_load_l2cache(spa
);
6091 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6092 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6096 * Check for any removed devices.
6098 if (spa
->spa_autoreplace
) {
6099 spa_aux_check_removed(&spa
->spa_spares
);
6100 spa_aux_check_removed(&spa
->spa_l2cache
);
6103 if (spa_writeable(spa
)) {
6105 * Update the config cache to include the newly-imported pool.
6107 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6111 * It's possible that the pool was expanded while it was exported.
6112 * We kick off an async task to handle this for us.
6114 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
6116 spa_history_log_version(spa
, "import", NULL
);
6118 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6120 mutex_exit(&spa_namespace_lock
);
6122 zvol_create_minors_recursive(pool
);
6128 spa_tryimport(nvlist_t
*tryconfig
)
6130 nvlist_t
*config
= NULL
;
6131 char *poolname
, *cachefile
;
6135 zpool_load_policy_t policy
;
6137 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
6140 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
6144 * Create and initialize the spa structure.
6146 mutex_enter(&spa_namespace_lock
);
6147 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
6148 spa_activate(spa
, SPA_MODE_READ
);
6151 * Rewind pool if a max txg was provided.
6153 zpool_get_load_policy(spa
->spa_config
, &policy
);
6154 if (policy
.zlp_txg
!= UINT64_MAX
) {
6155 spa
->spa_load_max_txg
= policy
.zlp_txg
;
6156 spa
->spa_extreme_rewind
= B_TRUE
;
6157 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
6158 poolname
, (longlong_t
)policy
.zlp_txg
);
6160 zfs_dbgmsg("spa_tryimport: importing %s", poolname
);
6163 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_CACHEFILE
, &cachefile
)
6165 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile
);
6166 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
6168 spa
->spa_config_source
= SPA_CONFIG_SRC_SCAN
;
6171 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
);
6174 * If 'tryconfig' was at least parsable, return the current config.
6176 if (spa
->spa_root_vdev
!= NULL
) {
6177 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
6178 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
6180 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
6182 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
6183 spa
->spa_uberblock
.ub_timestamp
) == 0);
6184 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
6185 spa
->spa_load_info
) == 0);
6186 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
6187 spa
->spa_errata
) == 0);
6190 * If the bootfs property exists on this pool then we
6191 * copy it out so that external consumers can tell which
6192 * pools are bootable.
6194 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
6195 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6198 * We have to play games with the name since the
6199 * pool was opened as TRYIMPORT_NAME.
6201 if (dsl_dsobj_to_dsname(spa_name(spa
),
6202 spa
->spa_bootfs
, tmpname
) == 0) {
6206 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6208 cp
= strchr(tmpname
, '/');
6210 (void) strlcpy(dsname
, tmpname
,
6213 (void) snprintf(dsname
, MAXPATHLEN
,
6214 "%s/%s", poolname
, ++cp
);
6216 VERIFY(nvlist_add_string(config
,
6217 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
6218 kmem_free(dsname
, MAXPATHLEN
);
6220 kmem_free(tmpname
, MAXPATHLEN
);
6224 * Add the list of hot spares and level 2 cache devices.
6226 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6227 spa_add_spares(spa
, config
);
6228 spa_add_l2cache(spa
, config
);
6229 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6233 spa_deactivate(spa
);
6235 mutex_exit(&spa_namespace_lock
);
6241 * Pool export/destroy
6243 * The act of destroying or exporting a pool is very simple. We make sure there
6244 * is no more pending I/O and any references to the pool are gone. Then, we
6245 * update the pool state and sync all the labels to disk, removing the
6246 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
6247 * we don't sync the labels or remove the configuration cache.
6250 spa_export_common(const char *pool
, int new_state
, nvlist_t
**oldconfig
,
6251 boolean_t force
, boolean_t hardforce
)
6258 if (!(spa_mode_global
& SPA_MODE_WRITE
))
6259 return (SET_ERROR(EROFS
));
6261 mutex_enter(&spa_namespace_lock
);
6262 if ((spa
= spa_lookup(pool
)) == NULL
) {
6263 mutex_exit(&spa_namespace_lock
);
6264 return (SET_ERROR(ENOENT
));
6267 if (spa
->spa_is_exporting
) {
6268 /* the pool is being exported by another thread */
6269 mutex_exit(&spa_namespace_lock
);
6270 return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS
));
6272 spa
->spa_is_exporting
= B_TRUE
;
6275 * Put a hold on the pool, drop the namespace lock, stop async tasks,
6276 * reacquire the namespace lock, and see if we can export.
6278 spa_open_ref(spa
, FTAG
);
6279 mutex_exit(&spa_namespace_lock
);
6280 spa_async_suspend(spa
);
6281 if (spa
->spa_zvol_taskq
) {
6282 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
6283 taskq_wait(spa
->spa_zvol_taskq
);
6285 mutex_enter(&spa_namespace_lock
);
6286 spa_close(spa
, FTAG
);
6288 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
6291 * The pool will be in core if it's openable, in which case we can
6292 * modify its state. Objsets may be open only because they're dirty,
6293 * so we have to force it to sync before checking spa_refcnt.
6295 if (spa
->spa_sync_on
) {
6296 txg_wait_synced(spa
->spa_dsl_pool
, 0);
6297 spa_evicting_os_wait(spa
);
6301 * A pool cannot be exported or destroyed if there are active
6302 * references. If we are resetting a pool, allow references by
6303 * fault injection handlers.
6305 if (!spa_refcount_zero(spa
) ||
6306 (spa
->spa_inject_ref
!= 0 &&
6307 new_state
!= POOL_STATE_UNINITIALIZED
)) {
6308 spa_async_resume(spa
);
6309 spa
->spa_is_exporting
= B_FALSE
;
6310 mutex_exit(&spa_namespace_lock
);
6311 return (SET_ERROR(EBUSY
));
6314 if (spa
->spa_sync_on
) {
6316 * A pool cannot be exported if it has an active shared spare.
6317 * This is to prevent other pools stealing the active spare
6318 * from an exported pool. At user's own will, such pool can
6319 * be forcedly exported.
6321 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
6322 spa_has_active_shared_spare(spa
)) {
6323 spa_async_resume(spa
);
6324 spa
->spa_is_exporting
= B_FALSE
;
6325 mutex_exit(&spa_namespace_lock
);
6326 return (SET_ERROR(EXDEV
));
6330 * We're about to export or destroy this pool. Make sure
6331 * we stop all initialization and trim activity here before
6332 * we set the spa_final_txg. This will ensure that all
6333 * dirty data resulting from the initialization is
6334 * committed to disk before we unload the pool.
6336 if (spa
->spa_root_vdev
!= NULL
) {
6337 vdev_t
*rvd
= spa
->spa_root_vdev
;
6338 vdev_initialize_stop_all(rvd
, VDEV_INITIALIZE_ACTIVE
);
6339 vdev_trim_stop_all(rvd
, VDEV_TRIM_ACTIVE
);
6340 vdev_autotrim_stop_all(spa
);
6341 vdev_rebuild_stop_all(spa
);
6345 * We want this to be reflected on every label,
6346 * so mark them all dirty. spa_unload() will do the
6347 * final sync that pushes these changes out.
6349 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
6350 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6351 spa
->spa_state
= new_state
;
6352 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
6354 vdev_config_dirty(spa
->spa_root_vdev
);
6355 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6360 if (new_state
== POOL_STATE_DESTROYED
)
6361 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
6362 else if (new_state
== POOL_STATE_EXPORTED
)
6363 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
6365 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6367 spa_deactivate(spa
);
6370 if (oldconfig
&& spa
->spa_config
)
6371 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
6373 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
6375 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
6379 * If spa_remove() is not called for this spa_t and
6380 * there is any possibility that it can be reused,
6381 * we make sure to reset the exporting flag.
6383 spa
->spa_is_exporting
= B_FALSE
;
6386 mutex_exit(&spa_namespace_lock
);
6391 * Destroy a storage pool.
6394 spa_destroy(const char *pool
)
6396 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
6401 * Export a storage pool.
6404 spa_export(const char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
6405 boolean_t hardforce
)
6407 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
6412 * Similar to spa_export(), this unloads the spa_t without actually removing it
6413 * from the namespace in any way.
6416 spa_reset(const char *pool
)
6418 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
6423 * ==========================================================================
6424 * Device manipulation
6425 * ==========================================================================
6429 * This is called as a synctask to increment the draid feature flag
6432 spa_draid_feature_incr(void *arg
, dmu_tx_t
*tx
)
6434 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6435 int draid
= (int)(uintptr_t)arg
;
6437 for (int c
= 0; c
< draid
; c
++)
6438 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
6442 * Add a device to a storage pool.
6445 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
6447 uint64_t txg
, ndraid
= 0;
6449 vdev_t
*rvd
= spa
->spa_root_vdev
;
6451 nvlist_t
**spares
, **l2cache
;
6452 uint_t nspares
, nl2cache
;
6454 ASSERT(spa_writeable(spa
));
6456 txg
= spa_vdev_enter(spa
);
6458 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
6459 VDEV_ALLOC_ADD
)) != 0)
6460 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6462 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
6464 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
6468 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
6472 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
6473 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6475 if (vd
->vdev_children
!= 0 &&
6476 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0) {
6477 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6481 * The virtual dRAID spares must be added after vdev tree is created
6482 * and the vdev guids are generated. The guid of their assoicated
6483 * dRAID is stored in the config and used when opening the spare.
6485 if ((error
= vdev_draid_spare_create(nvroot
, vd
, &ndraid
,
6486 rvd
->vdev_children
)) == 0) {
6487 if (ndraid
> 0 && nvlist_lookup_nvlist_array(nvroot
,
6488 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) != 0)
6491 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6495 * We must validate the spares and l2cache devices after checking the
6496 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
6498 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
6499 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6502 * If we are in the middle of a device removal, we can only add
6503 * devices which match the existing devices in the pool.
6504 * If we are in the middle of a removal, or have some indirect
6505 * vdevs, we can not add raidz or dRAID top levels.
6507 if (spa
->spa_vdev_removal
!= NULL
||
6508 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
6509 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6510 tvd
= vd
->vdev_child
[c
];
6511 if (spa
->spa_vdev_removal
!= NULL
&&
6512 tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
6513 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6515 /* Fail if top level vdev is raidz or a dRAID */
6516 if (vdev_get_nparity(tvd
) != 0)
6517 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6520 * Need the top level mirror to be
6521 * a mirror of leaf vdevs only
6523 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
6524 for (uint64_t cid
= 0;
6525 cid
< tvd
->vdev_children
; cid
++) {
6526 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
6527 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
6528 return (spa_vdev_exit(spa
, vd
,
6536 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6537 tvd
= vd
->vdev_child
[c
];
6538 vdev_remove_child(vd
, tvd
);
6539 tvd
->vdev_id
= rvd
->vdev_children
;
6540 vdev_add_child(rvd
, tvd
);
6541 vdev_config_dirty(tvd
);
6545 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
6546 ZPOOL_CONFIG_SPARES
);
6547 spa_load_spares(spa
);
6548 spa
->spa_spares
.sav_sync
= B_TRUE
;
6551 if (nl2cache
!= 0) {
6552 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
6553 ZPOOL_CONFIG_L2CACHE
);
6554 spa_load_l2cache(spa
);
6555 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6559 * We can't increment a feature while holding spa_vdev so we
6560 * have to do it in a synctask.
6565 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
6566 dsl_sync_task_nowait(spa
->spa_dsl_pool
, spa_draid_feature_incr
,
6567 (void *)(uintptr_t)ndraid
, tx
);
6572 * We have to be careful when adding new vdevs to an existing pool.
6573 * If other threads start allocating from these vdevs before we
6574 * sync the config cache, and we lose power, then upon reboot we may
6575 * fail to open the pool because there are DVAs that the config cache
6576 * can't translate. Therefore, we first add the vdevs without
6577 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
6578 * and then let spa_config_update() initialize the new metaslabs.
6580 * spa_load() checks for added-but-not-initialized vdevs, so that
6581 * if we lose power at any point in this sequence, the remaining
6582 * steps will be completed the next time we load the pool.
6584 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
6586 mutex_enter(&spa_namespace_lock
);
6587 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6588 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
6589 mutex_exit(&spa_namespace_lock
);
6595 * Attach a device to a mirror. The arguments are the path to any device
6596 * in the mirror, and the nvroot for the new device. If the path specifies
6597 * a device that is not mirrored, we automatically insert the mirror vdev.
6599 * If 'replacing' is specified, the new device is intended to replace the
6600 * existing device; in this case the two devices are made into their own
6601 * mirror using the 'replacing' vdev, which is functionally identical to
6602 * the mirror vdev (it actually reuses all the same ops) but has a few
6603 * extra rules: you can't attach to it after it's been created, and upon
6604 * completion of resilvering, the first disk (the one being replaced)
6605 * is automatically detached.
6607 * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild)
6608 * should be performed instead of traditional healing reconstruction. From
6609 * an administrators perspective these are both resilver operations.
6612 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
,
6615 uint64_t txg
, dtl_max_txg
;
6616 vdev_t
*rvd
= spa
->spa_root_vdev
;
6617 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
6619 char *oldvdpath
, *newvdpath
;
6623 ASSERT(spa_writeable(spa
));
6625 txg
= spa_vdev_enter(spa
);
6627 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6629 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6630 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6631 error
= (spa_has_checkpoint(spa
)) ?
6632 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6633 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6637 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_DEVICE_REBUILD
))
6638 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6640 if (dsl_scan_resilvering(spa_get_dsl(spa
)))
6641 return (spa_vdev_exit(spa
, NULL
, txg
,
6642 ZFS_ERR_RESILVER_IN_PROGRESS
));
6644 if (vdev_rebuild_active(rvd
))
6645 return (spa_vdev_exit(spa
, NULL
, txg
,
6646 ZFS_ERR_REBUILD_IN_PROGRESS
));
6649 if (spa
->spa_vdev_removal
!= NULL
)
6650 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6653 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
6655 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
6656 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6658 pvd
= oldvd
->vdev_parent
;
6660 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
6661 VDEV_ALLOC_ATTACH
)) != 0)
6662 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6664 if (newrootvd
->vdev_children
!= 1)
6665 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
6667 newvd
= newrootvd
->vdev_child
[0];
6669 if (!newvd
->vdev_ops
->vdev_op_leaf
)
6670 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
6672 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
6673 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
6676 * Spares can't replace logs
6678 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
6679 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6682 * A dRAID spare can only replace a child of its parent dRAID vdev.
6684 if (newvd
->vdev_ops
== &vdev_draid_spare_ops
&&
6685 oldvd
->vdev_top
!= vdev_draid_spare_get_parent(newvd
)) {
6686 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6691 * For rebuilds, the top vdev must support reconstruction
6692 * using only space maps. This means the only allowable
6693 * vdevs types are the root vdev, a mirror, or dRAID.
6696 if (pvd
->vdev_top
!= NULL
)
6697 tvd
= pvd
->vdev_top
;
6699 if (tvd
->vdev_ops
!= &vdev_mirror_ops
&&
6700 tvd
->vdev_ops
!= &vdev_root_ops
&&
6701 tvd
->vdev_ops
!= &vdev_draid_ops
) {
6702 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6708 * For attach, the only allowable parent is a mirror or the root
6711 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
6712 pvd
->vdev_ops
!= &vdev_root_ops
)
6713 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6715 pvops
= &vdev_mirror_ops
;
6718 * Active hot spares can only be replaced by inactive hot
6721 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6722 oldvd
->vdev_isspare
&&
6723 !spa_has_spare(spa
, newvd
->vdev_guid
))
6724 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6727 * If the source is a hot spare, and the parent isn't already a
6728 * spare, then we want to create a new hot spare. Otherwise, we
6729 * want to create a replacing vdev. The user is not allowed to
6730 * attach to a spared vdev child unless the 'isspare' state is
6731 * the same (spare replaces spare, non-spare replaces
6734 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
6735 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
6736 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6737 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6738 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
6739 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6742 if (newvd
->vdev_isspare
)
6743 pvops
= &vdev_spare_ops
;
6745 pvops
= &vdev_replacing_ops
;
6749 * Make sure the new device is big enough.
6751 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
6752 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
6755 * The new device cannot have a higher alignment requirement
6756 * than the top-level vdev.
6758 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
6759 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6762 * If this is an in-place replacement, update oldvd's path and devid
6763 * to make it distinguishable from newvd, and unopenable from now on.
6765 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
6766 spa_strfree(oldvd
->vdev_path
);
6767 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
6769 (void) snprintf(oldvd
->vdev_path
, strlen(newvd
->vdev_path
) + 5,
6770 "%s/%s", newvd
->vdev_path
, "old");
6771 if (oldvd
->vdev_devid
!= NULL
) {
6772 spa_strfree(oldvd
->vdev_devid
);
6773 oldvd
->vdev_devid
= NULL
;
6778 * If the parent is not a mirror, or if we're replacing, insert the new
6779 * mirror/replacing/spare vdev above oldvd.
6781 if (pvd
->vdev_ops
!= pvops
)
6782 pvd
= vdev_add_parent(oldvd
, pvops
);
6784 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
6785 ASSERT(pvd
->vdev_ops
== pvops
);
6786 ASSERT(oldvd
->vdev_parent
== pvd
);
6789 * Extract the new device from its root and add it to pvd.
6791 vdev_remove_child(newrootvd
, newvd
);
6792 newvd
->vdev_id
= pvd
->vdev_children
;
6793 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
6794 vdev_add_child(pvd
, newvd
);
6797 * Reevaluate the parent vdev state.
6799 vdev_propagate_state(pvd
);
6801 tvd
= newvd
->vdev_top
;
6802 ASSERT(pvd
->vdev_top
== tvd
);
6803 ASSERT(tvd
->vdev_parent
== rvd
);
6805 vdev_config_dirty(tvd
);
6808 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
6809 * for any dmu_sync-ed blocks. It will propagate upward when
6810 * spa_vdev_exit() calls vdev_dtl_reassess().
6812 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
6814 vdev_dtl_dirty(newvd
, DTL_MISSING
,
6815 TXG_INITIAL
, dtl_max_txg
- TXG_INITIAL
);
6817 if (newvd
->vdev_isspare
) {
6818 spa_spare_activate(newvd
);
6819 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
6822 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
6823 newvdpath
= spa_strdup(newvd
->vdev_path
);
6824 newvd_isspare
= newvd
->vdev_isspare
;
6827 * Mark newvd's DTL dirty in this txg.
6829 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
6832 * Schedule the resilver or rebuild to restart in the future. We do
6833 * this to ensure that dmu_sync-ed blocks have been stitched into the
6834 * respective datasets.
6837 newvd
->vdev_rebuild_txg
= txg
;
6841 newvd
->vdev_resilver_txg
= txg
;
6843 if (dsl_scan_resilvering(spa_get_dsl(spa
)) &&
6844 spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
)) {
6845 vdev_defer_resilver(newvd
);
6847 dsl_scan_restart_resilver(spa
->spa_dsl_pool
,
6852 if (spa
->spa_bootfs
)
6853 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
6855 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
6860 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
6862 spa_history_log_internal(spa
, "vdev attach", NULL
,
6863 "%s vdev=%s %s vdev=%s",
6864 replacing
&& newvd_isspare
? "spare in" :
6865 replacing
? "replace" : "attach", newvdpath
,
6866 replacing
? "for" : "to", oldvdpath
);
6868 spa_strfree(oldvdpath
);
6869 spa_strfree(newvdpath
);
6875 * Detach a device from a mirror or replacing vdev.
6877 * If 'replace_done' is specified, only detach if the parent
6878 * is a replacing vdev.
6881 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
6885 vdev_t
*rvd __maybe_unused
= spa
->spa_root_vdev
;
6886 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
6887 boolean_t unspare
= B_FALSE
;
6888 uint64_t unspare_guid
= 0;
6891 ASSERT(spa_writeable(spa
));
6893 txg
= spa_vdev_detach_enter(spa
, guid
);
6895 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6898 * Besides being called directly from the userland through the
6899 * ioctl interface, spa_vdev_detach() can be potentially called
6900 * at the end of spa_vdev_resilver_done().
6902 * In the regular case, when we have a checkpoint this shouldn't
6903 * happen as we never empty the DTLs of a vdev during the scrub
6904 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
6905 * should never get here when we have a checkpoint.
6907 * That said, even in a case when we checkpoint the pool exactly
6908 * as spa_vdev_resilver_done() calls this function everything
6909 * should be fine as the resilver will return right away.
6911 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6912 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6913 error
= (spa_has_checkpoint(spa
)) ?
6914 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6915 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6919 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
6921 if (!vd
->vdev_ops
->vdev_op_leaf
)
6922 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6924 pvd
= vd
->vdev_parent
;
6927 * If the parent/child relationship is not as expected, don't do it.
6928 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
6929 * vdev that's replacing B with C. The user's intent in replacing
6930 * is to go from M(A,B) to M(A,C). If the user decides to cancel
6931 * the replace by detaching C, the expected behavior is to end up
6932 * M(A,B). But suppose that right after deciding to detach C,
6933 * the replacement of B completes. We would have M(A,C), and then
6934 * ask to detach C, which would leave us with just A -- not what
6935 * the user wanted. To prevent this, we make sure that the
6936 * parent/child relationship hasn't changed -- in this example,
6937 * that C's parent is still the replacing vdev R.
6939 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
6940 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6943 * Only 'replacing' or 'spare' vdevs can be replaced.
6945 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
6946 pvd
->vdev_ops
!= &vdev_spare_ops
)
6947 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6949 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
6950 spa_version(spa
) >= SPA_VERSION_SPARES
);
6953 * Only mirror, replacing, and spare vdevs support detach.
6955 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
6956 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
6957 pvd
->vdev_ops
!= &vdev_spare_ops
)
6958 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6961 * If this device has the only valid copy of some data,
6962 * we cannot safely detach it.
6964 if (vdev_dtl_required(vd
))
6965 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6967 ASSERT(pvd
->vdev_children
>= 2);
6970 * If we are detaching the second disk from a replacing vdev, then
6971 * check to see if we changed the original vdev's path to have "/old"
6972 * at the end in spa_vdev_attach(). If so, undo that change now.
6974 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
6975 vd
->vdev_path
!= NULL
) {
6976 size_t len
= strlen(vd
->vdev_path
);
6978 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
6979 cvd
= pvd
->vdev_child
[c
];
6981 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
6984 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
6985 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
6986 spa_strfree(cvd
->vdev_path
);
6987 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
6994 * If we are detaching the original disk from a normal spare, then it
6995 * implies that the spare should become a real disk, and be removed
6996 * from the active spare list for the pool. dRAID spares on the
6997 * other hand are coupled to the pool and thus should never be removed
6998 * from the spares list.
7000 if (pvd
->vdev_ops
== &vdev_spare_ops
&& vd
->vdev_id
== 0) {
7001 vdev_t
*last_cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7003 if (last_cvd
->vdev_isspare
&&
7004 last_cvd
->vdev_ops
!= &vdev_draid_spare_ops
) {
7010 * Erase the disk labels so the disk can be used for other things.
7011 * This must be done after all other error cases are handled,
7012 * but before we disembowel vd (so we can still do I/O to it).
7013 * But if we can't do it, don't treat the error as fatal --
7014 * it may be that the unwritability of the disk is the reason
7015 * it's being detached!
7017 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
7020 * Remove vd from its parent and compact the parent's children.
7022 vdev_remove_child(pvd
, vd
);
7023 vdev_compact_children(pvd
);
7026 * Remember one of the remaining children so we can get tvd below.
7028 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7031 * If we need to remove the remaining child from the list of hot spares,
7032 * do it now, marking the vdev as no longer a spare in the process.
7033 * We must do this before vdev_remove_parent(), because that can
7034 * change the GUID if it creates a new toplevel GUID. For a similar
7035 * reason, we must remove the spare now, in the same txg as the detach;
7036 * otherwise someone could attach a new sibling, change the GUID, and
7037 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
7040 ASSERT(cvd
->vdev_isspare
);
7041 spa_spare_remove(cvd
);
7042 unspare_guid
= cvd
->vdev_guid
;
7043 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
7044 cvd
->vdev_unspare
= B_TRUE
;
7048 * If the parent mirror/replacing vdev only has one child,
7049 * the parent is no longer needed. Remove it from the tree.
7051 if (pvd
->vdev_children
== 1) {
7052 if (pvd
->vdev_ops
== &vdev_spare_ops
)
7053 cvd
->vdev_unspare
= B_FALSE
;
7054 vdev_remove_parent(cvd
);
7058 * We don't set tvd until now because the parent we just removed
7059 * may have been the previous top-level vdev.
7061 tvd
= cvd
->vdev_top
;
7062 ASSERT(tvd
->vdev_parent
== rvd
);
7065 * Reevaluate the parent vdev state.
7067 vdev_propagate_state(cvd
);
7070 * If the 'autoexpand' property is set on the pool then automatically
7071 * try to expand the size of the pool. For example if the device we
7072 * just detached was smaller than the others, it may be possible to
7073 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
7074 * first so that we can obtain the updated sizes of the leaf vdevs.
7076 if (spa
->spa_autoexpand
) {
7078 vdev_expand(tvd
, txg
);
7081 vdev_config_dirty(tvd
);
7084 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
7085 * vd->vdev_detached is set and free vd's DTL object in syncing context.
7086 * But first make sure we're not on any *other* txg's DTL list, to
7087 * prevent vd from being accessed after it's freed.
7089 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
7090 for (int t
= 0; t
< TXG_SIZE
; t
++)
7091 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
7092 vd
->vdev_detached
= B_TRUE
;
7093 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
7095 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
7096 spa_notify_waiters(spa
);
7098 /* hang on to the spa before we release the lock */
7099 spa_open_ref(spa
, FTAG
);
7101 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
7103 spa_history_log_internal(spa
, "detach", NULL
,
7105 spa_strfree(vdpath
);
7108 * If this was the removal of the original device in a hot spare vdev,
7109 * then we want to go through and remove the device from the hot spare
7110 * list of every other pool.
7113 spa_t
*altspa
= NULL
;
7115 mutex_enter(&spa_namespace_lock
);
7116 while ((altspa
= spa_next(altspa
)) != NULL
) {
7117 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
7121 spa_open_ref(altspa
, FTAG
);
7122 mutex_exit(&spa_namespace_lock
);
7123 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
7124 mutex_enter(&spa_namespace_lock
);
7125 spa_close(altspa
, FTAG
);
7127 mutex_exit(&spa_namespace_lock
);
7129 /* search the rest of the vdevs for spares to remove */
7130 spa_vdev_resilver_done(spa
);
7133 /* all done with the spa; OK to release */
7134 mutex_enter(&spa_namespace_lock
);
7135 spa_close(spa
, FTAG
);
7136 mutex_exit(&spa_namespace_lock
);
7142 spa_vdev_initialize_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
7145 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7147 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7149 /* Look up vdev and ensure it's a leaf. */
7150 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7151 if (vd
== NULL
|| vd
->vdev_detached
) {
7152 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7153 return (SET_ERROR(ENODEV
));
7154 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
7155 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7156 return (SET_ERROR(EINVAL
));
7157 } else if (!vdev_writeable(vd
)) {
7158 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7159 return (SET_ERROR(EROFS
));
7161 mutex_enter(&vd
->vdev_initialize_lock
);
7162 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7165 * When we activate an initialize action we check to see
7166 * if the vdev_initialize_thread is NULL. We do this instead
7167 * of using the vdev_initialize_state since there might be
7168 * a previous initialization process which has completed but
7169 * the thread is not exited.
7171 if (cmd_type
== POOL_INITIALIZE_START
&&
7172 (vd
->vdev_initialize_thread
!= NULL
||
7173 vd
->vdev_top
->vdev_removing
)) {
7174 mutex_exit(&vd
->vdev_initialize_lock
);
7175 return (SET_ERROR(EBUSY
));
7176 } else if (cmd_type
== POOL_INITIALIZE_CANCEL
&&
7177 (vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
&&
7178 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_SUSPENDED
)) {
7179 mutex_exit(&vd
->vdev_initialize_lock
);
7180 return (SET_ERROR(ESRCH
));
7181 } else if (cmd_type
== POOL_INITIALIZE_SUSPEND
&&
7182 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
) {
7183 mutex_exit(&vd
->vdev_initialize_lock
);
7184 return (SET_ERROR(ESRCH
));
7188 case POOL_INITIALIZE_START
:
7189 vdev_initialize(vd
);
7191 case POOL_INITIALIZE_CANCEL
:
7192 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
, vd_list
);
7194 case POOL_INITIALIZE_SUSPEND
:
7195 vdev_initialize_stop(vd
, VDEV_INITIALIZE_SUSPENDED
, vd_list
);
7198 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
7200 mutex_exit(&vd
->vdev_initialize_lock
);
7206 spa_vdev_initialize(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
,
7207 nvlist_t
*vdev_errlist
)
7209 int total_errors
= 0;
7212 list_create(&vd_list
, sizeof (vdev_t
),
7213 offsetof(vdev_t
, vdev_initialize_node
));
7216 * We hold the namespace lock through the whole function
7217 * to prevent any changes to the pool while we're starting or
7218 * stopping initialization. The config and state locks are held so that
7219 * we can properly assess the vdev state before we commit to
7220 * the initializing operation.
7222 mutex_enter(&spa_namespace_lock
);
7224 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
7225 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
7226 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
7228 int error
= spa_vdev_initialize_impl(spa
, vdev_guid
, cmd_type
,
7231 char guid_as_str
[MAXNAMELEN
];
7233 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
7234 "%llu", (unsigned long long)vdev_guid
);
7235 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
7240 /* Wait for all initialize threads to stop. */
7241 vdev_initialize_stop_wait(spa
, &vd_list
);
7243 /* Sync out the initializing state */
7244 txg_wait_synced(spa
->spa_dsl_pool
, 0);
7245 mutex_exit(&spa_namespace_lock
);
7247 list_destroy(&vd_list
);
7249 return (total_errors
);
7253 spa_vdev_trim_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
7254 uint64_t rate
, boolean_t partial
, boolean_t secure
, list_t
*vd_list
)
7256 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7258 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7260 /* Look up vdev and ensure it's a leaf. */
7261 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7262 if (vd
== NULL
|| vd
->vdev_detached
) {
7263 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7264 return (SET_ERROR(ENODEV
));
7265 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
7266 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7267 return (SET_ERROR(EINVAL
));
7268 } else if (!vdev_writeable(vd
)) {
7269 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7270 return (SET_ERROR(EROFS
));
7271 } else if (!vd
->vdev_has_trim
) {
7272 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7273 return (SET_ERROR(EOPNOTSUPP
));
7274 } else if (secure
&& !vd
->vdev_has_securetrim
) {
7275 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7276 return (SET_ERROR(EOPNOTSUPP
));
7278 mutex_enter(&vd
->vdev_trim_lock
);
7279 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7282 * When we activate a TRIM action we check to see if the
7283 * vdev_trim_thread is NULL. We do this instead of using the
7284 * vdev_trim_state since there might be a previous TRIM process
7285 * which has completed but the thread is not exited.
7287 if (cmd_type
== POOL_TRIM_START
&&
7288 (vd
->vdev_trim_thread
!= NULL
|| vd
->vdev_top
->vdev_removing
)) {
7289 mutex_exit(&vd
->vdev_trim_lock
);
7290 return (SET_ERROR(EBUSY
));
7291 } else if (cmd_type
== POOL_TRIM_CANCEL
&&
7292 (vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
&&
7293 vd
->vdev_trim_state
!= VDEV_TRIM_SUSPENDED
)) {
7294 mutex_exit(&vd
->vdev_trim_lock
);
7295 return (SET_ERROR(ESRCH
));
7296 } else if (cmd_type
== POOL_TRIM_SUSPEND
&&
7297 vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
) {
7298 mutex_exit(&vd
->vdev_trim_lock
);
7299 return (SET_ERROR(ESRCH
));
7303 case POOL_TRIM_START
:
7304 vdev_trim(vd
, rate
, partial
, secure
);
7306 case POOL_TRIM_CANCEL
:
7307 vdev_trim_stop(vd
, VDEV_TRIM_CANCELED
, vd_list
);
7309 case POOL_TRIM_SUSPEND
:
7310 vdev_trim_stop(vd
, VDEV_TRIM_SUSPENDED
, vd_list
);
7313 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
7315 mutex_exit(&vd
->vdev_trim_lock
);
7321 * Initiates a manual TRIM for the requested vdevs. This kicks off individual
7322 * TRIM threads for each child vdev. These threads pass over all of the free
7323 * space in the vdev's metaslabs and issues TRIM commands for that space.
7326 spa_vdev_trim(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
, uint64_t rate
,
7327 boolean_t partial
, boolean_t secure
, nvlist_t
*vdev_errlist
)
7329 int total_errors
= 0;
7332 list_create(&vd_list
, sizeof (vdev_t
),
7333 offsetof(vdev_t
, vdev_trim_node
));
7336 * We hold the namespace lock through the whole function
7337 * to prevent any changes to the pool while we're starting or
7338 * stopping TRIM. The config and state locks are held so that
7339 * we can properly assess the vdev state before we commit to
7340 * the TRIM operation.
7342 mutex_enter(&spa_namespace_lock
);
7344 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
7345 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
7346 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
7348 int error
= spa_vdev_trim_impl(spa
, vdev_guid
, cmd_type
,
7349 rate
, partial
, secure
, &vd_list
);
7351 char guid_as_str
[MAXNAMELEN
];
7353 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
7354 "%llu", (unsigned long long)vdev_guid
);
7355 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
7360 /* Wait for all TRIM threads to stop. */
7361 vdev_trim_stop_wait(spa
, &vd_list
);
7363 /* Sync out the TRIM state */
7364 txg_wait_synced(spa
->spa_dsl_pool
, 0);
7365 mutex_exit(&spa_namespace_lock
);
7367 list_destroy(&vd_list
);
7369 return (total_errors
);
7373 * Split a set of devices from their mirrors, and create a new pool from them.
7376 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
7377 nvlist_t
*props
, boolean_t exp
)
7380 uint64_t txg
, *glist
;
7382 uint_t c
, children
, lastlog
;
7383 nvlist_t
**child
, *nvl
, *tmp
;
7385 char *altroot
= NULL
;
7386 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
7387 boolean_t activate_slog
;
7389 ASSERT(spa_writeable(spa
));
7391 txg
= spa_vdev_enter(spa
);
7393 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7394 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7395 error
= (spa_has_checkpoint(spa
)) ?
7396 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7397 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7400 /* clear the log and flush everything up to now */
7401 activate_slog
= spa_passivate_log(spa
);
7402 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
7403 error
= spa_reset_logs(spa
);
7404 txg
= spa_vdev_config_enter(spa
);
7407 spa_activate_log(spa
);
7410 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7412 /* check new spa name before going any further */
7413 if (spa_lookup(newname
) != NULL
)
7414 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
7417 * scan through all the children to ensure they're all mirrors
7419 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
7420 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
7422 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7424 /* first, check to ensure we've got the right child count */
7425 rvd
= spa
->spa_root_vdev
;
7427 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
7428 vdev_t
*vd
= rvd
->vdev_child
[c
];
7430 /* don't count the holes & logs as children */
7431 if (vd
->vdev_islog
|| (vd
->vdev_ops
!= &vdev_indirect_ops
&&
7432 !vdev_is_concrete(vd
))) {
7440 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
7441 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7443 /* next, ensure no spare or cache devices are part of the split */
7444 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
7445 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
7446 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7448 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
7449 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
7451 /* then, loop over each vdev and validate it */
7452 for (c
= 0; c
< children
; c
++) {
7453 uint64_t is_hole
= 0;
7455 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
7459 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
7460 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
7463 error
= SET_ERROR(EINVAL
);
7468 /* deal with indirect vdevs */
7469 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ops
==
7473 /* which disk is going to be split? */
7474 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
7476 error
= SET_ERROR(EINVAL
);
7480 /* look it up in the spa */
7481 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
7482 if (vml
[c
] == NULL
) {
7483 error
= SET_ERROR(ENODEV
);
7487 /* make sure there's nothing stopping the split */
7488 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
7489 vml
[c
]->vdev_islog
||
7490 !vdev_is_concrete(vml
[c
]) ||
7491 vml
[c
]->vdev_isspare
||
7492 vml
[c
]->vdev_isl2cache
||
7493 !vdev_writeable(vml
[c
]) ||
7494 vml
[c
]->vdev_children
!= 0 ||
7495 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
7496 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
7497 error
= SET_ERROR(EINVAL
);
7501 if (vdev_dtl_required(vml
[c
]) ||
7502 vdev_resilver_needed(vml
[c
], NULL
, NULL
)) {
7503 error
= SET_ERROR(EBUSY
);
7507 /* we need certain info from the top level */
7508 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
7509 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
7510 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
7511 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
7512 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
7513 vml
[c
]->vdev_top
->vdev_asize
) == 0);
7514 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
7515 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
7517 /* transfer per-vdev ZAPs */
7518 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
7519 VERIFY0(nvlist_add_uint64(child
[c
],
7520 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
7522 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
7523 VERIFY0(nvlist_add_uint64(child
[c
],
7524 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
7525 vml
[c
]->vdev_parent
->vdev_top_zap
));
7529 kmem_free(vml
, children
* sizeof (vdev_t
*));
7530 kmem_free(glist
, children
* sizeof (uint64_t));
7531 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7534 /* stop writers from using the disks */
7535 for (c
= 0; c
< children
; c
++) {
7537 vml
[c
]->vdev_offline
= B_TRUE
;
7539 vdev_reopen(spa
->spa_root_vdev
);
7542 * Temporarily record the splitting vdevs in the spa config. This
7543 * will disappear once the config is regenerated.
7545 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
7546 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
7547 glist
, children
) == 0);
7548 kmem_free(glist
, children
* sizeof (uint64_t));
7550 mutex_enter(&spa
->spa_props_lock
);
7551 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
7553 mutex_exit(&spa
->spa_props_lock
);
7554 spa
->spa_config_splitting
= nvl
;
7555 vdev_config_dirty(spa
->spa_root_vdev
);
7557 /* configure and create the new pool */
7558 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
7559 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
7560 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
7561 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
7562 spa_version(spa
)) == 0);
7563 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
7564 spa
->spa_config_txg
) == 0);
7565 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
7566 spa_generate_guid(NULL
)) == 0);
7567 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
7568 (void) nvlist_lookup_string(props
,
7569 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
7571 /* add the new pool to the namespace */
7572 newspa
= spa_add(newname
, config
, altroot
);
7573 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
7574 newspa
->spa_config_txg
= spa
->spa_config_txg
;
7575 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
7577 /* release the spa config lock, retaining the namespace lock */
7578 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
7580 if (zio_injection_enabled
)
7581 zio_handle_panic_injection(spa
, FTAG
, 1);
7583 spa_activate(newspa
, spa_mode_global
);
7584 spa_async_suspend(newspa
);
7587 * Temporarily stop the initializing and TRIM activity. We set the
7588 * state to ACTIVE so that we know to resume initializing or TRIM
7589 * once the split has completed.
7591 list_t vd_initialize_list
;
7592 list_create(&vd_initialize_list
, sizeof (vdev_t
),
7593 offsetof(vdev_t
, vdev_initialize_node
));
7595 list_t vd_trim_list
;
7596 list_create(&vd_trim_list
, sizeof (vdev_t
),
7597 offsetof(vdev_t
, vdev_trim_node
));
7599 for (c
= 0; c
< children
; c
++) {
7600 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
7601 mutex_enter(&vml
[c
]->vdev_initialize_lock
);
7602 vdev_initialize_stop(vml
[c
],
7603 VDEV_INITIALIZE_ACTIVE
, &vd_initialize_list
);
7604 mutex_exit(&vml
[c
]->vdev_initialize_lock
);
7606 mutex_enter(&vml
[c
]->vdev_trim_lock
);
7607 vdev_trim_stop(vml
[c
], VDEV_TRIM_ACTIVE
, &vd_trim_list
);
7608 mutex_exit(&vml
[c
]->vdev_trim_lock
);
7612 vdev_initialize_stop_wait(spa
, &vd_initialize_list
);
7613 vdev_trim_stop_wait(spa
, &vd_trim_list
);
7615 list_destroy(&vd_initialize_list
);
7616 list_destroy(&vd_trim_list
);
7618 newspa
->spa_config_source
= SPA_CONFIG_SRC_SPLIT
;
7619 newspa
->spa_is_splitting
= B_TRUE
;
7621 /* create the new pool from the disks of the original pool */
7622 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
);
7626 /* if that worked, generate a real config for the new pool */
7627 if (newspa
->spa_root_vdev
!= NULL
) {
7628 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
7629 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
7630 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
7631 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
7632 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
7637 if (props
!= NULL
) {
7638 spa_configfile_set(newspa
, props
, B_FALSE
);
7639 error
= spa_prop_set(newspa
, props
);
7644 /* flush everything */
7645 txg
= spa_vdev_config_enter(newspa
);
7646 vdev_config_dirty(newspa
->spa_root_vdev
);
7647 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
7649 if (zio_injection_enabled
)
7650 zio_handle_panic_injection(spa
, FTAG
, 2);
7652 spa_async_resume(newspa
);
7654 /* finally, update the original pool's config */
7655 txg
= spa_vdev_config_enter(spa
);
7656 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
7657 error
= dmu_tx_assign(tx
, TXG_WAIT
);
7660 for (c
= 0; c
< children
; c
++) {
7661 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
7662 vdev_t
*tvd
= vml
[c
]->vdev_top
;
7665 * Need to be sure the detachable VDEV is not
7666 * on any *other* txg's DTL list to prevent it
7667 * from being accessed after it's freed.
7669 for (int t
= 0; t
< TXG_SIZE
; t
++) {
7670 (void) txg_list_remove_this(
7671 &tvd
->vdev_dtl_list
, vml
[c
], t
);
7676 spa_history_log_internal(spa
, "detach", tx
,
7677 "vdev=%s", vml
[c
]->vdev_path
);
7682 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
7683 vdev_config_dirty(spa
->spa_root_vdev
);
7684 spa
->spa_config_splitting
= NULL
;
7688 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
7690 if (zio_injection_enabled
)
7691 zio_handle_panic_injection(spa
, FTAG
, 3);
7693 /* split is complete; log a history record */
7694 spa_history_log_internal(newspa
, "split", NULL
,
7695 "from pool %s", spa_name(spa
));
7697 newspa
->spa_is_splitting
= B_FALSE
;
7698 kmem_free(vml
, children
* sizeof (vdev_t
*));
7700 /* if we're not going to mount the filesystems in userland, export */
7702 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
7709 spa_deactivate(newspa
);
7712 txg
= spa_vdev_config_enter(spa
);
7714 /* re-online all offlined disks */
7715 for (c
= 0; c
< children
; c
++) {
7717 vml
[c
]->vdev_offline
= B_FALSE
;
7720 /* restart initializing or trimming disks as necessary */
7721 spa_async_request(spa
, SPA_ASYNC_INITIALIZE_RESTART
);
7722 spa_async_request(spa
, SPA_ASYNC_TRIM_RESTART
);
7723 spa_async_request(spa
, SPA_ASYNC_AUTOTRIM_RESTART
);
7725 vdev_reopen(spa
->spa_root_vdev
);
7727 nvlist_free(spa
->spa_config_splitting
);
7728 spa
->spa_config_splitting
= NULL
;
7729 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
7731 kmem_free(vml
, children
* sizeof (vdev_t
*));
7736 * Find any device that's done replacing, or a vdev marked 'unspare' that's
7737 * currently spared, so we can detach it.
7740 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
7742 vdev_t
*newvd
, *oldvd
;
7744 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7745 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
7751 * Check for a completed replacement. We always consider the first
7752 * vdev in the list to be the oldest vdev, and the last one to be
7753 * the newest (see spa_vdev_attach() for how that works). In
7754 * the case where the newest vdev is faulted, we will not automatically
7755 * remove it after a resilver completes. This is OK as it will require
7756 * user intervention to determine which disk the admin wishes to keep.
7758 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
7759 ASSERT(vd
->vdev_children
> 1);
7761 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
7762 oldvd
= vd
->vdev_child
[0];
7764 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
7765 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
7766 !vdev_dtl_required(oldvd
))
7771 * Check for a completed resilver with the 'unspare' flag set.
7772 * Also potentially update faulted state.
7774 if (vd
->vdev_ops
== &vdev_spare_ops
) {
7775 vdev_t
*first
= vd
->vdev_child
[0];
7776 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
7778 if (last
->vdev_unspare
) {
7781 } else if (first
->vdev_unspare
) {
7788 if (oldvd
!= NULL
&&
7789 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
7790 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
7791 !vdev_dtl_required(oldvd
))
7794 vdev_propagate_state(vd
);
7797 * If there are more than two spares attached to a disk,
7798 * and those spares are not required, then we want to
7799 * attempt to free them up now so that they can be used
7800 * by other pools. Once we're back down to a single
7801 * disk+spare, we stop removing them.
7803 if (vd
->vdev_children
> 2) {
7804 newvd
= vd
->vdev_child
[1];
7806 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
7807 vdev_dtl_empty(last
, DTL_MISSING
) &&
7808 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
7809 !vdev_dtl_required(newvd
))
7818 spa_vdev_resilver_done(spa_t
*spa
)
7820 vdev_t
*vd
, *pvd
, *ppvd
;
7821 uint64_t guid
, sguid
, pguid
, ppguid
;
7823 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
7825 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
7826 pvd
= vd
->vdev_parent
;
7827 ppvd
= pvd
->vdev_parent
;
7828 guid
= vd
->vdev_guid
;
7829 pguid
= pvd
->vdev_guid
;
7830 ppguid
= ppvd
->vdev_guid
;
7833 * If we have just finished replacing a hot spared device, then
7834 * we need to detach the parent's first child (the original hot
7837 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
7838 ppvd
->vdev_children
== 2) {
7839 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
7840 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
7842 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
7844 spa_config_exit(spa
, SCL_ALL
, FTAG
);
7845 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
7847 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
7849 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
7852 spa_config_exit(spa
, SCL_ALL
, FTAG
);
7855 * If a detach was not performed above replace waiters will not have
7856 * been notified. In which case we must do so now.
7858 spa_notify_waiters(spa
);
7862 * Update the stored path or FRU for this vdev.
7865 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
7869 boolean_t sync
= B_FALSE
;
7871 ASSERT(spa_writeable(spa
));
7873 spa_vdev_state_enter(spa
, SCL_ALL
);
7875 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
7876 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
7878 if (!vd
->vdev_ops
->vdev_op_leaf
)
7879 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
7882 if (strcmp(value
, vd
->vdev_path
) != 0) {
7883 spa_strfree(vd
->vdev_path
);
7884 vd
->vdev_path
= spa_strdup(value
);
7888 if (vd
->vdev_fru
== NULL
) {
7889 vd
->vdev_fru
= spa_strdup(value
);
7891 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
7892 spa_strfree(vd
->vdev_fru
);
7893 vd
->vdev_fru
= spa_strdup(value
);
7898 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
7902 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
7904 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
7908 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
7910 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
7914 * ==========================================================================
7916 * ==========================================================================
7919 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
7921 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
7923 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
7924 return (SET_ERROR(EBUSY
));
7926 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
7930 spa_scan_stop(spa_t
*spa
)
7932 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
7933 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
7934 return (SET_ERROR(EBUSY
));
7935 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
7939 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
7941 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
7943 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
7944 return (SET_ERROR(ENOTSUP
));
7946 if (func
== POOL_SCAN_RESILVER
&&
7947 !spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
))
7948 return (SET_ERROR(ENOTSUP
));
7951 * If a resilver was requested, but there is no DTL on a
7952 * writeable leaf device, we have nothing to do.
7954 if (func
== POOL_SCAN_RESILVER
&&
7955 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
7956 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
7960 return (dsl_scan(spa
->spa_dsl_pool
, func
));
7964 * ==========================================================================
7965 * SPA async task processing
7966 * ==========================================================================
7970 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
7972 if (vd
->vdev_remove_wanted
) {
7973 vd
->vdev_remove_wanted
= B_FALSE
;
7974 vd
->vdev_delayed_close
= B_FALSE
;
7975 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
7978 * We want to clear the stats, but we don't want to do a full
7979 * vdev_clear() as that will cause us to throw away
7980 * degraded/faulted state as well as attempt to reopen the
7981 * device, all of which is a waste.
7983 vd
->vdev_stat
.vs_read_errors
= 0;
7984 vd
->vdev_stat
.vs_write_errors
= 0;
7985 vd
->vdev_stat
.vs_checksum_errors
= 0;
7987 vdev_state_dirty(vd
->vdev_top
);
7989 /* Tell userspace that the vdev is gone. */
7990 zfs_post_remove(spa
, vd
);
7993 for (int c
= 0; c
< vd
->vdev_children
; c
++)
7994 spa_async_remove(spa
, vd
->vdev_child
[c
]);
7998 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
8000 if (vd
->vdev_probe_wanted
) {
8001 vd
->vdev_probe_wanted
= B_FALSE
;
8002 vdev_reopen(vd
); /* vdev_open() does the actual probe */
8005 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8006 spa_async_probe(spa
, vd
->vdev_child
[c
]);
8010 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
8012 if (!spa
->spa_autoexpand
)
8015 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
8016 vdev_t
*cvd
= vd
->vdev_child
[c
];
8017 spa_async_autoexpand(spa
, cvd
);
8020 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
8023 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
8027 spa_async_thread(void *arg
)
8029 spa_t
*spa
= (spa_t
*)arg
;
8030 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
8033 ASSERT(spa
->spa_sync_on
);
8035 mutex_enter(&spa
->spa_async_lock
);
8036 tasks
= spa
->spa_async_tasks
;
8037 spa
->spa_async_tasks
= 0;
8038 mutex_exit(&spa
->spa_async_lock
);
8041 * See if the config needs to be updated.
8043 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
8044 uint64_t old_space
, new_space
;
8046 mutex_enter(&spa_namespace_lock
);
8047 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
8048 old_space
+= metaslab_class_get_space(spa_special_class(spa
));
8049 old_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8050 old_space
+= metaslab_class_get_space(
8051 spa_embedded_log_class(spa
));
8053 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
8055 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
8056 new_space
+= metaslab_class_get_space(spa_special_class(spa
));
8057 new_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8058 new_space
+= metaslab_class_get_space(
8059 spa_embedded_log_class(spa
));
8060 mutex_exit(&spa_namespace_lock
);
8063 * If the pool grew as a result of the config update,
8064 * then log an internal history event.
8066 if (new_space
!= old_space
) {
8067 spa_history_log_internal(spa
, "vdev online", NULL
,
8068 "pool '%s' size: %llu(+%llu)",
8069 spa_name(spa
), (u_longlong_t
)new_space
,
8070 (u_longlong_t
)(new_space
- old_space
));
8075 * See if any devices need to be marked REMOVED.
8077 if (tasks
& SPA_ASYNC_REMOVE
) {
8078 spa_vdev_state_enter(spa
, SCL_NONE
);
8079 spa_async_remove(spa
, spa
->spa_root_vdev
);
8080 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
8081 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
8082 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
8083 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
8084 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8087 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
8088 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8089 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
8090 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8094 * See if any devices need to be probed.
8096 if (tasks
& SPA_ASYNC_PROBE
) {
8097 spa_vdev_state_enter(spa
, SCL_NONE
);
8098 spa_async_probe(spa
, spa
->spa_root_vdev
);
8099 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8103 * If any devices are done replacing, detach them.
8105 if (tasks
& SPA_ASYNC_RESILVER_DONE
||
8106 tasks
& SPA_ASYNC_REBUILD_DONE
) {
8107 spa_vdev_resilver_done(spa
);
8111 * Kick off a resilver.
8113 if (tasks
& SPA_ASYNC_RESILVER
&&
8114 !vdev_rebuild_active(spa
->spa_root_vdev
) &&
8115 (!dsl_scan_resilvering(dp
) ||
8116 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_RESILVER_DEFER
)))
8117 dsl_scan_restart_resilver(dp
, 0);
8119 if (tasks
& SPA_ASYNC_INITIALIZE_RESTART
) {
8120 mutex_enter(&spa_namespace_lock
);
8121 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8122 vdev_initialize_restart(spa
->spa_root_vdev
);
8123 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8124 mutex_exit(&spa_namespace_lock
);
8127 if (tasks
& SPA_ASYNC_TRIM_RESTART
) {
8128 mutex_enter(&spa_namespace_lock
);
8129 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8130 vdev_trim_restart(spa
->spa_root_vdev
);
8131 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8132 mutex_exit(&spa_namespace_lock
);
8135 if (tasks
& SPA_ASYNC_AUTOTRIM_RESTART
) {
8136 mutex_enter(&spa_namespace_lock
);
8137 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8138 vdev_autotrim_restart(spa
);
8139 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8140 mutex_exit(&spa_namespace_lock
);
8144 * Kick off L2 cache whole device TRIM.
8146 if (tasks
& SPA_ASYNC_L2CACHE_TRIM
) {
8147 mutex_enter(&spa_namespace_lock
);
8148 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8149 vdev_trim_l2arc(spa
);
8150 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8151 mutex_exit(&spa_namespace_lock
);
8155 * Kick off L2 cache rebuilding.
8157 if (tasks
& SPA_ASYNC_L2CACHE_REBUILD
) {
8158 mutex_enter(&spa_namespace_lock
);
8159 spa_config_enter(spa
, SCL_L2ARC
, FTAG
, RW_READER
);
8160 l2arc_spa_rebuild_start(spa
);
8161 spa_config_exit(spa
, SCL_L2ARC
, FTAG
);
8162 mutex_exit(&spa_namespace_lock
);
8166 * Let the world know that we're done.
8168 mutex_enter(&spa
->spa_async_lock
);
8169 spa
->spa_async_thread
= NULL
;
8170 cv_broadcast(&spa
->spa_async_cv
);
8171 mutex_exit(&spa
->spa_async_lock
);
8176 spa_async_suspend(spa_t
*spa
)
8178 mutex_enter(&spa
->spa_async_lock
);
8179 spa
->spa_async_suspended
++;
8180 while (spa
->spa_async_thread
!= NULL
)
8181 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
8182 mutex_exit(&spa
->spa_async_lock
);
8184 spa_vdev_remove_suspend(spa
);
8186 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
8187 if (condense_thread
!= NULL
)
8188 zthr_cancel(condense_thread
);
8190 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
8191 if (discard_thread
!= NULL
)
8192 zthr_cancel(discard_thread
);
8194 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
8195 if (ll_delete_thread
!= NULL
)
8196 zthr_cancel(ll_delete_thread
);
8198 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
8199 if (ll_condense_thread
!= NULL
)
8200 zthr_cancel(ll_condense_thread
);
8204 spa_async_resume(spa_t
*spa
)
8206 mutex_enter(&spa
->spa_async_lock
);
8207 ASSERT(spa
->spa_async_suspended
!= 0);
8208 spa
->spa_async_suspended
--;
8209 mutex_exit(&spa
->spa_async_lock
);
8210 spa_restart_removal(spa
);
8212 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
8213 if (condense_thread
!= NULL
)
8214 zthr_resume(condense_thread
);
8216 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
8217 if (discard_thread
!= NULL
)
8218 zthr_resume(discard_thread
);
8220 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
8221 if (ll_delete_thread
!= NULL
)
8222 zthr_resume(ll_delete_thread
);
8224 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
8225 if (ll_condense_thread
!= NULL
)
8226 zthr_resume(ll_condense_thread
);
8230 spa_async_tasks_pending(spa_t
*spa
)
8232 uint_t non_config_tasks
;
8234 boolean_t config_task_suspended
;
8236 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
8237 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
8238 if (spa
->spa_ccw_fail_time
== 0) {
8239 config_task_suspended
= B_FALSE
;
8241 config_task_suspended
=
8242 (gethrtime() - spa
->spa_ccw_fail_time
) <
8243 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
8246 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
8250 spa_async_dispatch(spa_t
*spa
)
8252 mutex_enter(&spa
->spa_async_lock
);
8253 if (spa_async_tasks_pending(spa
) &&
8254 !spa
->spa_async_suspended
&&
8255 spa
->spa_async_thread
== NULL
)
8256 spa
->spa_async_thread
= thread_create(NULL
, 0,
8257 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
8258 mutex_exit(&spa
->spa_async_lock
);
8262 spa_async_request(spa_t
*spa
, int task
)
8264 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
8265 mutex_enter(&spa
->spa_async_lock
);
8266 spa
->spa_async_tasks
|= task
;
8267 mutex_exit(&spa
->spa_async_lock
);
8271 spa_async_tasks(spa_t
*spa
)
8273 return (spa
->spa_async_tasks
);
8277 * ==========================================================================
8278 * SPA syncing routines
8279 * ==========================================================================
8284 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
8288 bpobj_enqueue(bpo
, bp
, bp_freed
, tx
);
8293 bpobj_enqueue_alloc_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8295 return (bpobj_enqueue_cb(arg
, bp
, B_FALSE
, tx
));
8299 bpobj_enqueue_free_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8301 return (bpobj_enqueue_cb(arg
, bp
, B_TRUE
, tx
));
8305 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8309 zio_nowait(zio_free_sync(pio
, pio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
8315 bpobj_spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
8319 return (spa_free_sync_cb(arg
, bp
, tx
));
8323 * Note: this simple function is not inlined to make it easier to dtrace the
8324 * amount of time spent syncing frees.
8327 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
8329 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
8330 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
8331 VERIFY(zio_wait(zio
) == 0);
8335 * Note: this simple function is not inlined to make it easier to dtrace the
8336 * amount of time spent syncing deferred frees.
8339 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
8341 if (spa_sync_pass(spa
) != 1)
8346 * If the log space map feature is active, we stop deferring
8347 * frees to the next TXG and therefore running this function
8348 * would be considered a no-op as spa_deferred_bpobj should
8349 * not have any entries.
8351 * That said we run this function anyway (instead of returning
8352 * immediately) for the edge-case scenario where we just
8353 * activated the log space map feature in this TXG but we have
8354 * deferred frees from the previous TXG.
8356 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
8357 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
8358 bpobj_spa_free_sync_cb
, zio
, tx
), ==, 0);
8359 VERIFY0(zio_wait(zio
));
8363 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
8365 char *packed
= NULL
;
8370 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
8373 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
8374 * information. This avoids the dmu_buf_will_dirty() path and
8375 * saves us a pre-read to get data we don't actually care about.
8377 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
8378 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
8380 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
8382 bzero(packed
+ nvsize
, bufsize
- nvsize
);
8384 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
8386 vmem_free(packed
, bufsize
);
8388 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
8389 dmu_buf_will_dirty(db
, tx
);
8390 *(uint64_t *)db
->db_data
= nvsize
;
8391 dmu_buf_rele(db
, FTAG
);
8395 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
8396 const char *config
, const char *entry
)
8406 * Update the MOS nvlist describing the list of available devices.
8407 * spa_validate_aux() will have already made sure this nvlist is
8408 * valid and the vdevs are labeled appropriately.
8410 if (sav
->sav_object
== 0) {
8411 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
8412 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
8413 sizeof (uint64_t), tx
);
8414 VERIFY(zap_update(spa
->spa_meta_objset
,
8415 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
8416 &sav
->sav_object
, tx
) == 0);
8419 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
8420 if (sav
->sav_count
== 0) {
8421 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
8423 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
8424 for (i
= 0; i
< sav
->sav_count
; i
++)
8425 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
8426 B_FALSE
, VDEV_CONFIG_L2CACHE
);
8427 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
8428 sav
->sav_count
) == 0);
8429 for (i
= 0; i
< sav
->sav_count
; i
++)
8430 nvlist_free(list
[i
]);
8431 kmem_free(list
, sav
->sav_count
* sizeof (void *));
8434 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
8435 nvlist_free(nvroot
);
8437 sav
->sav_sync
= B_FALSE
;
8441 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
8442 * The all-vdev ZAP must be empty.
8445 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
8447 spa_t
*spa
= vd
->vdev_spa
;
8449 if (vd
->vdev_top_zap
!= 0) {
8450 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
8451 vd
->vdev_top_zap
, tx
));
8453 if (vd
->vdev_leaf_zap
!= 0) {
8454 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
8455 vd
->vdev_leaf_zap
, tx
));
8457 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
8458 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
8463 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
8468 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
8469 * its config may not be dirty but we still need to build per-vdev ZAPs.
8470 * Similarly, if the pool is being assembled (e.g. after a split), we
8471 * need to rebuild the AVZ although the config may not be dirty.
8473 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
8474 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
8477 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
8479 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
8480 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
8481 spa
->spa_all_vdev_zaps
!= 0);
8483 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
8484 /* Make and build the new AVZ */
8485 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
8486 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
8487 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
8489 /* Diff old AVZ with new one */
8493 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
8494 spa
->spa_all_vdev_zaps
);
8495 zap_cursor_retrieve(&zc
, &za
) == 0;
8496 zap_cursor_advance(&zc
)) {
8497 uint64_t vdzap
= za
.za_first_integer
;
8498 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
8501 * ZAP is listed in old AVZ but not in new one;
8504 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
8509 zap_cursor_fini(&zc
);
8511 /* Destroy the old AVZ */
8512 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
8513 spa
->spa_all_vdev_zaps
, tx
));
8515 /* Replace the old AVZ in the dir obj with the new one */
8516 VERIFY0(zap_update(spa
->spa_meta_objset
,
8517 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
8518 sizeof (new_avz
), 1, &new_avz
, tx
));
8520 spa
->spa_all_vdev_zaps
= new_avz
;
8521 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
8525 /* Walk through the AVZ and destroy all listed ZAPs */
8526 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
8527 spa
->spa_all_vdev_zaps
);
8528 zap_cursor_retrieve(&zc
, &za
) == 0;
8529 zap_cursor_advance(&zc
)) {
8530 uint64_t zap
= za
.za_first_integer
;
8531 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
8534 zap_cursor_fini(&zc
);
8536 /* Destroy and unlink the AVZ itself */
8537 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
8538 spa
->spa_all_vdev_zaps
, tx
));
8539 VERIFY0(zap_remove(spa
->spa_meta_objset
,
8540 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
8541 spa
->spa_all_vdev_zaps
= 0;
8544 if (spa
->spa_all_vdev_zaps
== 0) {
8545 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
8546 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
8547 DMU_POOL_VDEV_ZAP_MAP
, tx
);
8549 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
8551 /* Create ZAPs for vdevs that don't have them. */
8552 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
8554 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
8555 dmu_tx_get_txg(tx
), B_FALSE
);
8558 * If we're upgrading the spa version then make sure that
8559 * the config object gets updated with the correct version.
8561 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
8562 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
8563 spa
->spa_uberblock
.ub_version
);
8565 spa_config_exit(spa
, SCL_STATE
, FTAG
);
8567 nvlist_free(spa
->spa_config_syncing
);
8568 spa
->spa_config_syncing
= config
;
8570 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
8574 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
8576 uint64_t *versionp
= arg
;
8577 uint64_t version
= *versionp
;
8578 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
8581 * Setting the version is special cased when first creating the pool.
8583 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
8585 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
8586 ASSERT(version
>= spa_version(spa
));
8588 spa
->spa_uberblock
.ub_version
= version
;
8589 vdev_config_dirty(spa
->spa_root_vdev
);
8590 spa_history_log_internal(spa
, "set", tx
, "version=%lld",
8591 (longlong_t
)version
);
8595 * Set zpool properties.
8598 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
8600 nvlist_t
*nvp
= arg
;
8601 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
8602 objset_t
*mos
= spa
->spa_meta_objset
;
8603 nvpair_t
*elem
= NULL
;
8605 mutex_enter(&spa
->spa_props_lock
);
8607 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
8609 char *strval
, *fname
;
8611 const char *propname
;
8612 zprop_type_t proptype
;
8615 switch (prop
= zpool_name_to_prop(nvpair_name(elem
))) {
8616 case ZPOOL_PROP_INVAL
:
8618 * We checked this earlier in spa_prop_validate().
8620 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
8622 fname
= strchr(nvpair_name(elem
), '@') + 1;
8623 VERIFY0(zfeature_lookup_name(fname
, &fid
));
8625 spa_feature_enable(spa
, fid
, tx
);
8626 spa_history_log_internal(spa
, "set", tx
,
8627 "%s=enabled", nvpair_name(elem
));
8630 case ZPOOL_PROP_VERSION
:
8631 intval
= fnvpair_value_uint64(elem
);
8633 * The version is synced separately before other
8634 * properties and should be correct by now.
8636 ASSERT3U(spa_version(spa
), >=, intval
);
8639 case ZPOOL_PROP_ALTROOT
:
8641 * 'altroot' is a non-persistent property. It should
8642 * have been set temporarily at creation or import time.
8644 ASSERT(spa
->spa_root
!= NULL
);
8647 case ZPOOL_PROP_READONLY
:
8648 case ZPOOL_PROP_CACHEFILE
:
8650 * 'readonly' and 'cachefile' are also non-persistent
8654 case ZPOOL_PROP_COMMENT
:
8655 strval
= fnvpair_value_string(elem
);
8656 if (spa
->spa_comment
!= NULL
)
8657 spa_strfree(spa
->spa_comment
);
8658 spa
->spa_comment
= spa_strdup(strval
);
8660 * We need to dirty the configuration on all the vdevs
8661 * so that their labels get updated. It's unnecessary
8662 * to do this for pool creation since the vdev's
8663 * configuration has already been dirtied.
8665 if (tx
->tx_txg
!= TXG_INITIAL
)
8666 vdev_config_dirty(spa
->spa_root_vdev
);
8667 spa_history_log_internal(spa
, "set", tx
,
8668 "%s=%s", nvpair_name(elem
), strval
);
8672 * Set pool property values in the poolprops mos object.
8674 if (spa
->spa_pool_props_object
== 0) {
8675 spa
->spa_pool_props_object
=
8676 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
8677 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
8681 /* normalize the property name */
8682 propname
= zpool_prop_to_name(prop
);
8683 proptype
= zpool_prop_get_type(prop
);
8685 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
8686 ASSERT(proptype
== PROP_TYPE_STRING
);
8687 strval
= fnvpair_value_string(elem
);
8688 VERIFY0(zap_update(mos
,
8689 spa
->spa_pool_props_object
, propname
,
8690 1, strlen(strval
) + 1, strval
, tx
));
8691 spa_history_log_internal(spa
, "set", tx
,
8692 "%s=%s", nvpair_name(elem
), strval
);
8693 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
8694 intval
= fnvpair_value_uint64(elem
);
8696 if (proptype
== PROP_TYPE_INDEX
) {
8698 VERIFY0(zpool_prop_index_to_string(
8699 prop
, intval
, &unused
));
8701 VERIFY0(zap_update(mos
,
8702 spa
->spa_pool_props_object
, propname
,
8703 8, 1, &intval
, tx
));
8704 spa_history_log_internal(spa
, "set", tx
,
8705 "%s=%lld", nvpair_name(elem
),
8706 (longlong_t
)intval
);
8708 ASSERT(0); /* not allowed */
8712 case ZPOOL_PROP_DELEGATION
:
8713 spa
->spa_delegation
= intval
;
8715 case ZPOOL_PROP_BOOTFS
:
8716 spa
->spa_bootfs
= intval
;
8718 case ZPOOL_PROP_FAILUREMODE
:
8719 spa
->spa_failmode
= intval
;
8721 case ZPOOL_PROP_AUTOTRIM
:
8722 spa
->spa_autotrim
= intval
;
8723 spa_async_request(spa
,
8724 SPA_ASYNC_AUTOTRIM_RESTART
);
8726 case ZPOOL_PROP_AUTOEXPAND
:
8727 spa
->spa_autoexpand
= intval
;
8728 if (tx
->tx_txg
!= TXG_INITIAL
)
8729 spa_async_request(spa
,
8730 SPA_ASYNC_AUTOEXPAND
);
8732 case ZPOOL_PROP_MULTIHOST
:
8733 spa
->spa_multihost
= intval
;
8742 mutex_exit(&spa
->spa_props_lock
);
8746 * Perform one-time upgrade on-disk changes. spa_version() does not
8747 * reflect the new version this txg, so there must be no changes this
8748 * txg to anything that the upgrade code depends on after it executes.
8749 * Therefore this must be called after dsl_pool_sync() does the sync
8753 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
8755 if (spa_sync_pass(spa
) != 1)
8758 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
8759 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
8761 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
8762 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
8763 dsl_pool_create_origin(dp
, tx
);
8765 /* Keeping the origin open increases spa_minref */
8766 spa
->spa_minref
+= 3;
8769 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
8770 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
8771 dsl_pool_upgrade_clones(dp
, tx
);
8774 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
8775 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
8776 dsl_pool_upgrade_dir_clones(dp
, tx
);
8778 /* Keeping the freedir open increases spa_minref */
8779 spa
->spa_minref
+= 3;
8782 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
8783 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
8784 spa_feature_create_zap_objects(spa
, tx
);
8788 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
8789 * when possibility to use lz4 compression for metadata was added
8790 * Old pools that have this feature enabled must be upgraded to have
8791 * this feature active
8793 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
8794 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
8795 SPA_FEATURE_LZ4_COMPRESS
);
8796 boolean_t lz4_ac
= spa_feature_is_active(spa
,
8797 SPA_FEATURE_LZ4_COMPRESS
);
8799 if (lz4_en
&& !lz4_ac
)
8800 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
8804 * If we haven't written the salt, do so now. Note that the
8805 * feature may not be activated yet, but that's fine since
8806 * the presence of this ZAP entry is backwards compatible.
8808 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
8809 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
8810 VERIFY0(zap_add(spa
->spa_meta_objset
,
8811 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
8812 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
8813 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
8816 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
8820 vdev_indirect_state_sync_verify(vdev_t
*vd
)
8822 vdev_indirect_mapping_t
*vim __maybe_unused
= vd
->vdev_indirect_mapping
;
8823 vdev_indirect_births_t
*vib __maybe_unused
= vd
->vdev_indirect_births
;
8825 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
8826 ASSERT(vim
!= NULL
);
8827 ASSERT(vib
!= NULL
);
8830 uint64_t obsolete_sm_object
= 0;
8831 ASSERT0(vdev_obsolete_sm_object(vd
, &obsolete_sm_object
));
8832 if (obsolete_sm_object
!= 0) {
8833 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
8834 ASSERT(vd
->vdev_removing
||
8835 vd
->vdev_ops
== &vdev_indirect_ops
);
8836 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
8837 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
8838 ASSERT3U(obsolete_sm_object
, ==,
8839 space_map_object(vd
->vdev_obsolete_sm
));
8840 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
8841 space_map_allocated(vd
->vdev_obsolete_sm
));
8843 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
8846 * Since frees / remaps to an indirect vdev can only
8847 * happen in syncing context, the obsolete segments
8848 * tree must be empty when we start syncing.
8850 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
8854 * Set the top-level vdev's max queue depth. Evaluate each top-level's
8855 * async write queue depth in case it changed. The max queue depth will
8856 * not change in the middle of syncing out this txg.
8859 spa_sync_adjust_vdev_max_queue_depth(spa_t
*spa
)
8861 ASSERT(spa_writeable(spa
));
8863 vdev_t
*rvd
= spa
->spa_root_vdev
;
8864 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
8865 zfs_vdev_queue_depth_pct
/ 100;
8866 metaslab_class_t
*normal
= spa_normal_class(spa
);
8867 metaslab_class_t
*special
= spa_special_class(spa
);
8868 metaslab_class_t
*dedup
= spa_dedup_class(spa
);
8870 uint64_t slots_per_allocator
= 0;
8871 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
8872 vdev_t
*tvd
= rvd
->vdev_child
[c
];
8874 metaslab_group_t
*mg
= tvd
->vdev_mg
;
8875 if (mg
== NULL
|| !metaslab_group_initialized(mg
))
8878 metaslab_class_t
*mc
= mg
->mg_class
;
8879 if (mc
!= normal
&& mc
!= special
&& mc
!= dedup
)
8883 * It is safe to do a lock-free check here because only async
8884 * allocations look at mg_max_alloc_queue_depth, and async
8885 * allocations all happen from spa_sync().
8887 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
8888 ASSERT0(zfs_refcount_count(
8889 &(mg
->mg_allocator
[i
].mga_alloc_queue_depth
)));
8891 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
8893 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
8894 mg
->mg_allocator
[i
].mga_cur_max_alloc_queue_depth
=
8895 zfs_vdev_def_queue_depth
;
8897 slots_per_allocator
+= zfs_vdev_def_queue_depth
;
8900 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
8901 ASSERT0(zfs_refcount_count(&normal
->mc_allocator
[i
].
8903 ASSERT0(zfs_refcount_count(&special
->mc_allocator
[i
].
8905 ASSERT0(zfs_refcount_count(&dedup
->mc_allocator
[i
].
8907 normal
->mc_allocator
[i
].mca_alloc_max_slots
=
8908 slots_per_allocator
;
8909 special
->mc_allocator
[i
].mca_alloc_max_slots
=
8910 slots_per_allocator
;
8911 dedup
->mc_allocator
[i
].mca_alloc_max_slots
=
8912 slots_per_allocator
;
8914 normal
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
8915 special
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
8916 dedup
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
8920 spa_sync_condense_indirect(spa_t
*spa
, dmu_tx_t
*tx
)
8922 ASSERT(spa_writeable(spa
));
8924 vdev_t
*rvd
= spa
->spa_root_vdev
;
8925 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
8926 vdev_t
*vd
= rvd
->vdev_child
[c
];
8927 vdev_indirect_state_sync_verify(vd
);
8929 if (vdev_indirect_should_condense(vd
)) {
8930 spa_condense_indirect_start_sync(vd
, tx
);
8937 spa_sync_iterate_to_convergence(spa_t
*spa
, dmu_tx_t
*tx
)
8939 objset_t
*mos
= spa
->spa_meta_objset
;
8940 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
8941 uint64_t txg
= tx
->tx_txg
;
8942 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
8945 int pass
= ++spa
->spa_sync_pass
;
8947 spa_sync_config_object(spa
, tx
);
8948 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
8949 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
8950 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
8951 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
8952 spa_errlog_sync(spa
, txg
);
8953 dsl_pool_sync(dp
, txg
);
8955 if (pass
< zfs_sync_pass_deferred_free
||
8956 spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
)) {
8958 * If the log space map feature is active we don't
8959 * care about deferred frees and the deferred bpobj
8960 * as the log space map should effectively have the
8961 * same results (i.e. appending only to one object).
8963 spa_sync_frees(spa
, free_bpl
, tx
);
8966 * We can not defer frees in pass 1, because
8967 * we sync the deferred frees later in pass 1.
8969 ASSERT3U(pass
, >, 1);
8970 bplist_iterate(free_bpl
, bpobj_enqueue_alloc_cb
,
8971 &spa
->spa_deferred_bpobj
, tx
);
8975 dsl_scan_sync(dp
, tx
);
8977 spa_sync_upgrades(spa
, tx
);
8979 spa_flush_metaslabs(spa
, tx
);
8982 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
8987 * Note: We need to check if the MOS is dirty because we could
8988 * have marked the MOS dirty without updating the uberblock
8989 * (e.g. if we have sync tasks but no dirty user data). We need
8990 * to check the uberblock's rootbp because it is updated if we
8991 * have synced out dirty data (though in this case the MOS will
8992 * most likely also be dirty due to second order effects, we
8993 * don't want to rely on that here).
8996 spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
8997 !dmu_objset_is_dirty(mos
, txg
)) {
8999 * Nothing changed on the first pass, therefore this
9000 * TXG is a no-op. Avoid syncing deferred frees, so
9001 * that we can keep this TXG as a no-op.
9003 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
9004 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
9005 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
9006 ASSERT(txg_list_empty(&dp
->dp_early_sync_tasks
, txg
));
9010 spa_sync_deferred_frees(spa
, tx
);
9011 } while (dmu_objset_is_dirty(mos
, txg
));
9015 * Rewrite the vdev configuration (which includes the uberblock) to
9016 * commit the transaction group.
9018 * If there are no dirty vdevs, we sync the uberblock to a few random
9019 * top-level vdevs that are known to be visible in the config cache
9020 * (see spa_vdev_add() for a complete description). If there *are* dirty
9021 * vdevs, sync the uberblock to all vdevs.
9024 spa_sync_rewrite_vdev_config(spa_t
*spa
, dmu_tx_t
*tx
)
9026 vdev_t
*rvd
= spa
->spa_root_vdev
;
9027 uint64_t txg
= tx
->tx_txg
;
9033 * We hold SCL_STATE to prevent vdev open/close/etc.
9034 * while we're attempting to write the vdev labels.
9036 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9038 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
9039 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
9041 int children
= rvd
->vdev_children
;
9042 int c0
= spa_get_random(children
);
9044 for (int c
= 0; c
< children
; c
++) {
9046 rvd
->vdev_child
[(c0
+ c
) % children
];
9048 /* Stop when revisiting the first vdev */
9049 if (c
> 0 && svd
[0] == vd
)
9052 if (vd
->vdev_ms_array
== 0 ||
9054 !vdev_is_concrete(vd
))
9057 svd
[svdcount
++] = vd
;
9058 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
9061 error
= vdev_config_sync(svd
, svdcount
, txg
);
9063 error
= vdev_config_sync(rvd
->vdev_child
,
9064 rvd
->vdev_children
, txg
);
9068 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
9070 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9074 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
9075 zio_resume_wait(spa
);
9080 * Sync the specified transaction group. New blocks may be dirtied as
9081 * part of the process, so we iterate until it converges.
9084 spa_sync(spa_t
*spa
, uint64_t txg
)
9088 VERIFY(spa_writeable(spa
));
9091 * Wait for i/os issued in open context that need to complete
9092 * before this txg syncs.
9094 (void) zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]);
9095 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
,
9099 * Lock out configuration changes.
9101 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
9103 spa
->spa_syncing_txg
= txg
;
9104 spa
->spa_sync_pass
= 0;
9106 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9107 mutex_enter(&spa
->spa_alloc_locks
[i
]);
9108 VERIFY0(avl_numnodes(&spa
->spa_alloc_trees
[i
]));
9109 mutex_exit(&spa
->spa_alloc_locks
[i
]);
9113 * If there are any pending vdev state changes, convert them
9114 * into config changes that go out with this transaction group.
9116 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9117 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
9119 * We need the write lock here because, for aux vdevs,
9120 * calling vdev_config_dirty() modifies sav_config.
9121 * This is ugly and will become unnecessary when we
9122 * eliminate the aux vdev wart by integrating all vdevs
9123 * into the root vdev tree.
9125 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9126 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
9127 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
9128 vdev_state_clean(vd
);
9129 vdev_config_dirty(vd
);
9131 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9132 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9134 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9136 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9137 dmu_tx_t
*tx
= dmu_tx_create_assigned(dp
, txg
);
9139 spa
->spa_sync_starttime
= gethrtime();
9140 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
9141 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
9142 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
9143 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
9146 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
9147 * set spa_deflate if we have no raid-z vdevs.
9149 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
9150 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
9151 vdev_t
*rvd
= spa
->spa_root_vdev
;
9154 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
9155 vd
= rvd
->vdev_child
[i
];
9156 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
9159 if (i
== rvd
->vdev_children
) {
9160 spa
->spa_deflate
= TRUE
;
9161 VERIFY0(zap_add(spa
->spa_meta_objset
,
9162 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
9163 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
9167 spa_sync_adjust_vdev_max_queue_depth(spa
);
9169 spa_sync_condense_indirect(spa
, tx
);
9171 spa_sync_iterate_to_convergence(spa
, tx
);
9174 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
9176 * Make sure that the number of ZAPs for all the vdevs matches
9177 * the number of ZAPs in the per-vdev ZAP list. This only gets
9178 * called if the config is dirty; otherwise there may be
9179 * outstanding AVZ operations that weren't completed in
9180 * spa_sync_config_object.
9182 uint64_t all_vdev_zap_entry_count
;
9183 ASSERT0(zap_count(spa
->spa_meta_objset
,
9184 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
9185 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
9186 all_vdev_zap_entry_count
);
9190 if (spa
->spa_vdev_removal
!= NULL
) {
9191 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
9194 spa_sync_rewrite_vdev_config(spa
, tx
);
9197 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
9198 spa
->spa_deadman_tqid
= 0;
9201 * Clear the dirty config list.
9203 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
9204 vdev_config_clean(vd
);
9207 * Now that the new config has synced transactionally,
9208 * let it become visible to the config cache.
9210 if (spa
->spa_config_syncing
!= NULL
) {
9211 spa_config_set(spa
, spa
->spa_config_syncing
);
9212 spa
->spa_config_txg
= txg
;
9213 spa
->spa_config_syncing
= NULL
;
9216 dsl_pool_sync_done(dp
, txg
);
9218 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9219 mutex_enter(&spa
->spa_alloc_locks
[i
]);
9220 VERIFY0(avl_numnodes(&spa
->spa_alloc_trees
[i
]));
9221 mutex_exit(&spa
->spa_alloc_locks
[i
]);
9225 * Update usable space statistics.
9227 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
)))
9229 vdev_sync_done(vd
, txg
);
9231 metaslab_class_evict_old(spa
->spa_normal_class
, txg
);
9232 metaslab_class_evict_old(spa
->spa_log_class
, txg
);
9234 spa_sync_close_syncing_log_sm(spa
);
9236 spa_update_dspace(spa
);
9239 * It had better be the case that we didn't dirty anything
9240 * since vdev_config_sync().
9242 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
9243 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
9244 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
9246 while (zfs_pause_spa_sync
)
9249 spa
->spa_sync_pass
= 0;
9252 * Update the last synced uberblock here. We want to do this at
9253 * the end of spa_sync() so that consumers of spa_last_synced_txg()
9254 * will be guaranteed that all the processing associated with
9255 * that txg has been completed.
9257 spa
->spa_ubsync
= spa
->spa_uberblock
;
9258 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
9260 spa_handle_ignored_writes(spa
);
9263 * If any async tasks have been requested, kick them off.
9265 spa_async_dispatch(spa
);
9269 * Sync all pools. We don't want to hold the namespace lock across these
9270 * operations, so we take a reference on the spa_t and drop the lock during the
9274 spa_sync_allpools(void)
9277 mutex_enter(&spa_namespace_lock
);
9278 while ((spa
= spa_next(spa
)) != NULL
) {
9279 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
9280 !spa_writeable(spa
) || spa_suspended(spa
))
9282 spa_open_ref(spa
, FTAG
);
9283 mutex_exit(&spa_namespace_lock
);
9284 txg_wait_synced(spa_get_dsl(spa
), 0);
9285 mutex_enter(&spa_namespace_lock
);
9286 spa_close(spa
, FTAG
);
9288 mutex_exit(&spa_namespace_lock
);
9292 * ==========================================================================
9293 * Miscellaneous routines
9294 * ==========================================================================
9298 * Remove all pools in the system.
9306 * Remove all cached state. All pools should be closed now,
9307 * so every spa in the AVL tree should be unreferenced.
9309 mutex_enter(&spa_namespace_lock
);
9310 while ((spa
= spa_next(NULL
)) != NULL
) {
9312 * Stop async tasks. The async thread may need to detach
9313 * a device that's been replaced, which requires grabbing
9314 * spa_namespace_lock, so we must drop it here.
9316 spa_open_ref(spa
, FTAG
);
9317 mutex_exit(&spa_namespace_lock
);
9318 spa_async_suspend(spa
);
9319 mutex_enter(&spa_namespace_lock
);
9320 spa_close(spa
, FTAG
);
9322 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
9324 spa_deactivate(spa
);
9328 mutex_exit(&spa_namespace_lock
);
9332 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
9337 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
9341 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
9342 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
9343 if (vd
->vdev_guid
== guid
)
9347 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
9348 vd
= spa
->spa_spares
.sav_vdevs
[i
];
9349 if (vd
->vdev_guid
== guid
)
9358 spa_upgrade(spa_t
*spa
, uint64_t version
)
9360 ASSERT(spa_writeable(spa
));
9362 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
9365 * This should only be called for a non-faulted pool, and since a
9366 * future version would result in an unopenable pool, this shouldn't be
9369 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
9370 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
9372 spa
->spa_uberblock
.ub_version
= version
;
9373 vdev_config_dirty(spa
->spa_root_vdev
);
9375 spa_config_exit(spa
, SCL_ALL
, FTAG
);
9377 txg_wait_synced(spa_get_dsl(spa
), 0);
9381 spa_has_spare(spa_t
*spa
, uint64_t guid
)
9385 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
9387 for (i
= 0; i
< sav
->sav_count
; i
++)
9388 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
9391 for (i
= 0; i
< sav
->sav_npending
; i
++) {
9392 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
9393 &spareguid
) == 0 && spareguid
== guid
)
9401 * Check if a pool has an active shared spare device.
9402 * Note: reference count of an active spare is 2, as a spare and as a replace
9405 spa_has_active_shared_spare(spa_t
*spa
)
9409 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
9411 for (i
= 0; i
< sav
->sav_count
; i
++) {
9412 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
9413 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
9422 spa_total_metaslabs(spa_t
*spa
)
9424 vdev_t
*rvd
= spa
->spa_root_vdev
;
9427 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
9428 vdev_t
*vd
= rvd
->vdev_child
[c
];
9429 if (!vdev_is_concrete(vd
))
9431 m
+= vd
->vdev_ms_count
;
9437 * Notify any waiting threads that some activity has switched from being in-
9438 * progress to not-in-progress so that the thread can wake up and determine
9439 * whether it is finished waiting.
9442 spa_notify_waiters(spa_t
*spa
)
9445 * Acquiring spa_activities_lock here prevents the cv_broadcast from
9446 * happening between the waiting thread's check and cv_wait.
9448 mutex_enter(&spa
->spa_activities_lock
);
9449 cv_broadcast(&spa
->spa_activities_cv
);
9450 mutex_exit(&spa
->spa_activities_lock
);
9454 * Notify any waiting threads that the pool is exporting, and then block until
9455 * they are finished using the spa_t.
9458 spa_wake_waiters(spa_t
*spa
)
9460 mutex_enter(&spa
->spa_activities_lock
);
9461 spa
->spa_waiters_cancel
= B_TRUE
;
9462 cv_broadcast(&spa
->spa_activities_cv
);
9463 while (spa
->spa_waiters
!= 0)
9464 cv_wait(&spa
->spa_waiters_cv
, &spa
->spa_activities_lock
);
9465 spa
->spa_waiters_cancel
= B_FALSE
;
9466 mutex_exit(&spa
->spa_activities_lock
);
9469 /* Whether the vdev or any of its descendants are being initialized/trimmed. */
9471 spa_vdev_activity_in_progress_impl(vdev_t
*vd
, zpool_wait_activity_t activity
)
9473 spa_t
*spa
= vd
->vdev_spa
;
9475 ASSERT(spa_config_held(spa
, SCL_CONFIG
| SCL_STATE
, RW_READER
));
9476 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
9477 ASSERT(activity
== ZPOOL_WAIT_INITIALIZE
||
9478 activity
== ZPOOL_WAIT_TRIM
);
9480 kmutex_t
*lock
= activity
== ZPOOL_WAIT_INITIALIZE
?
9481 &vd
->vdev_initialize_lock
: &vd
->vdev_trim_lock
;
9483 mutex_exit(&spa
->spa_activities_lock
);
9485 mutex_enter(&spa
->spa_activities_lock
);
9487 boolean_t in_progress
= (activity
== ZPOOL_WAIT_INITIALIZE
) ?
9488 (vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
) :
9489 (vd
->vdev_trim_state
== VDEV_TRIM_ACTIVE
);
9495 for (int i
= 0; i
< vd
->vdev_children
; i
++) {
9496 if (spa_vdev_activity_in_progress_impl(vd
->vdev_child
[i
],
9505 * If use_guid is true, this checks whether the vdev specified by guid is
9506 * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool
9507 * is being initialized/trimmed. The caller must hold the config lock and
9508 * spa_activities_lock.
9511 spa_vdev_activity_in_progress(spa_t
*spa
, boolean_t use_guid
, uint64_t guid
,
9512 zpool_wait_activity_t activity
, boolean_t
*in_progress
)
9514 mutex_exit(&spa
->spa_activities_lock
);
9515 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9516 mutex_enter(&spa
->spa_activities_lock
);
9520 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
9521 if (vd
== NULL
|| !vd
->vdev_ops
->vdev_op_leaf
) {
9522 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9526 vd
= spa
->spa_root_vdev
;
9529 *in_progress
= spa_vdev_activity_in_progress_impl(vd
, activity
);
9531 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9536 * Locking for waiting threads
9537 * ---------------------------
9539 * Waiting threads need a way to check whether a given activity is in progress,
9540 * and then, if it is, wait for it to complete. Each activity will have some
9541 * in-memory representation of the relevant on-disk state which can be used to
9542 * determine whether or not the activity is in progress. The in-memory state and
9543 * the locking used to protect it will be different for each activity, and may
9544 * not be suitable for use with a cvar (e.g., some state is protected by the
9545 * config lock). To allow waiting threads to wait without any races, another
9546 * lock, spa_activities_lock, is used.
9548 * When the state is checked, both the activity-specific lock (if there is one)
9549 * and spa_activities_lock are held. In some cases, the activity-specific lock
9550 * is acquired explicitly (e.g. the config lock). In others, the locking is
9551 * internal to some check (e.g. bpobj_is_empty). After checking, the waiting
9552 * thread releases the activity-specific lock and, if the activity is in
9553 * progress, then cv_waits using spa_activities_lock.
9555 * The waiting thread is woken when another thread, one completing some
9556 * activity, updates the state of the activity and then calls
9557 * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only
9558 * needs to hold its activity-specific lock when updating the state, and this
9559 * lock can (but doesn't have to) be dropped before calling spa_notify_waiters.
9561 * Because spa_notify_waiters acquires spa_activities_lock before broadcasting,
9562 * and because it is held when the waiting thread checks the state of the
9563 * activity, it can never be the case that the completing thread both updates
9564 * the activity state and cv_broadcasts in between the waiting thread's check
9565 * and cv_wait. Thus, a waiting thread can never miss a wakeup.
9567 * In order to prevent deadlock, when the waiting thread does its check, in some
9568 * cases it will temporarily drop spa_activities_lock in order to acquire the
9569 * activity-specific lock. The order in which spa_activities_lock and the
9570 * activity specific lock are acquired in the waiting thread is determined by
9571 * the order in which they are acquired in the completing thread; if the
9572 * completing thread calls spa_notify_waiters with the activity-specific lock
9573 * held, then the waiting thread must also acquire the activity-specific lock
9578 spa_activity_in_progress(spa_t
*spa
, zpool_wait_activity_t activity
,
9579 boolean_t use_tag
, uint64_t tag
, boolean_t
*in_progress
)
9583 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
9586 case ZPOOL_WAIT_CKPT_DISCARD
:
9588 (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
) &&
9589 zap_contains(spa_meta_objset(spa
),
9590 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_ZPOOL_CHECKPOINT
) ==
9593 case ZPOOL_WAIT_FREE
:
9594 *in_progress
= ((spa_version(spa
) >= SPA_VERSION_DEADLISTS
&&
9595 !bpobj_is_empty(&spa
->spa_dsl_pool
->dp_free_bpobj
)) ||
9596 spa_feature_is_active(spa
, SPA_FEATURE_ASYNC_DESTROY
) ||
9597 spa_livelist_delete_check(spa
));
9599 case ZPOOL_WAIT_INITIALIZE
:
9600 case ZPOOL_WAIT_TRIM
:
9601 error
= spa_vdev_activity_in_progress(spa
, use_tag
, tag
,
9602 activity
, in_progress
);
9604 case ZPOOL_WAIT_REPLACE
:
9605 mutex_exit(&spa
->spa_activities_lock
);
9606 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9607 mutex_enter(&spa
->spa_activities_lock
);
9609 *in_progress
= vdev_replace_in_progress(spa
->spa_root_vdev
);
9610 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9612 case ZPOOL_WAIT_REMOVE
:
9613 *in_progress
= (spa
->spa_removing_phys
.sr_state
==
9616 case ZPOOL_WAIT_RESILVER
:
9617 if ((*in_progress
= vdev_rebuild_active(spa
->spa_root_vdev
)))
9620 case ZPOOL_WAIT_SCRUB
:
9622 boolean_t scanning
, paused
, is_scrub
;
9623 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
9625 is_scrub
= (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
);
9626 scanning
= (scn
->scn_phys
.scn_state
== DSS_SCANNING
);
9627 paused
= dsl_scan_is_paused_scrub(scn
);
9628 *in_progress
= (scanning
&& !paused
&&
9629 is_scrub
== (activity
== ZPOOL_WAIT_SCRUB
));
9633 panic("unrecognized value for activity %d", activity
);
9640 spa_wait_common(const char *pool
, zpool_wait_activity_t activity
,
9641 boolean_t use_tag
, uint64_t tag
, boolean_t
*waited
)
9644 * The tag is used to distinguish between instances of an activity.
9645 * 'initialize' and 'trim' are the only activities that we use this for.
9646 * The other activities can only have a single instance in progress in a
9647 * pool at one time, making the tag unnecessary.
9649 * There can be multiple devices being replaced at once, but since they
9650 * all finish once resilvering finishes, we don't bother keeping track
9651 * of them individually, we just wait for them all to finish.
9653 if (use_tag
&& activity
!= ZPOOL_WAIT_INITIALIZE
&&
9654 activity
!= ZPOOL_WAIT_TRIM
)
9657 if (activity
< 0 || activity
>= ZPOOL_WAIT_NUM_ACTIVITIES
)
9661 int error
= spa_open(pool
, &spa
, FTAG
);
9666 * Increment the spa's waiter count so that we can call spa_close and
9667 * still ensure that the spa_t doesn't get freed before this thread is
9668 * finished with it when the pool is exported. We want to call spa_close
9669 * before we start waiting because otherwise the additional ref would
9670 * prevent the pool from being exported or destroyed throughout the
9671 * potentially long wait.
9673 mutex_enter(&spa
->spa_activities_lock
);
9675 spa_close(spa
, FTAG
);
9679 boolean_t in_progress
;
9680 error
= spa_activity_in_progress(spa
, activity
, use_tag
, tag
,
9683 if (error
|| !in_progress
|| spa
->spa_waiters_cancel
)
9688 if (cv_wait_sig(&spa
->spa_activities_cv
,
9689 &spa
->spa_activities_lock
) == 0) {
9696 cv_signal(&spa
->spa_waiters_cv
);
9697 mutex_exit(&spa
->spa_activities_lock
);
9703 * Wait for a particular instance of the specified activity to complete, where
9704 * the instance is identified by 'tag'
9707 spa_wait_tag(const char *pool
, zpool_wait_activity_t activity
, uint64_t tag
,
9710 return (spa_wait_common(pool
, activity
, B_TRUE
, tag
, waited
));
9714 * Wait for all instances of the specified activity complete
9717 spa_wait(const char *pool
, zpool_wait_activity_t activity
, boolean_t
*waited
)
9720 return (spa_wait_common(pool
, activity
, B_FALSE
, 0, waited
));
9724 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
9726 sysevent_t
*ev
= NULL
;
9730 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
9732 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
9733 ev
->resource
= resource
;
9740 spa_event_post(sysevent_t
*ev
)
9744 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
9745 kmem_free(ev
, sizeof (*ev
));
9751 * Post a zevent corresponding to the given sysevent. The 'name' must be one
9752 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
9753 * filled in from the spa and (optionally) the vdev. This doesn't do anything
9754 * in the userland libzpool, as we don't want consumers to misinterpret ztest
9755 * or zdb as real changes.
9758 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
9760 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
9763 /* state manipulation functions */
9764 EXPORT_SYMBOL(spa_open
);
9765 EXPORT_SYMBOL(spa_open_rewind
);
9766 EXPORT_SYMBOL(spa_get_stats
);
9767 EXPORT_SYMBOL(spa_create
);
9768 EXPORT_SYMBOL(spa_import
);
9769 EXPORT_SYMBOL(spa_tryimport
);
9770 EXPORT_SYMBOL(spa_destroy
);
9771 EXPORT_SYMBOL(spa_export
);
9772 EXPORT_SYMBOL(spa_reset
);
9773 EXPORT_SYMBOL(spa_async_request
);
9774 EXPORT_SYMBOL(spa_async_suspend
);
9775 EXPORT_SYMBOL(spa_async_resume
);
9776 EXPORT_SYMBOL(spa_inject_addref
);
9777 EXPORT_SYMBOL(spa_inject_delref
);
9778 EXPORT_SYMBOL(spa_scan_stat_init
);
9779 EXPORT_SYMBOL(spa_scan_get_stats
);
9781 /* device manipulation */
9782 EXPORT_SYMBOL(spa_vdev_add
);
9783 EXPORT_SYMBOL(spa_vdev_attach
);
9784 EXPORT_SYMBOL(spa_vdev_detach
);
9785 EXPORT_SYMBOL(spa_vdev_setpath
);
9786 EXPORT_SYMBOL(spa_vdev_setfru
);
9787 EXPORT_SYMBOL(spa_vdev_split_mirror
);
9789 /* spare statech is global across all pools) */
9790 EXPORT_SYMBOL(spa_spare_add
);
9791 EXPORT_SYMBOL(spa_spare_remove
);
9792 EXPORT_SYMBOL(spa_spare_exists
);
9793 EXPORT_SYMBOL(spa_spare_activate
);
9795 /* L2ARC statech is global across all pools) */
9796 EXPORT_SYMBOL(spa_l2cache_add
);
9797 EXPORT_SYMBOL(spa_l2cache_remove
);
9798 EXPORT_SYMBOL(spa_l2cache_exists
);
9799 EXPORT_SYMBOL(spa_l2cache_activate
);
9800 EXPORT_SYMBOL(spa_l2cache_drop
);
9803 EXPORT_SYMBOL(spa_scan
);
9804 EXPORT_SYMBOL(spa_scan_stop
);
9807 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
9808 EXPORT_SYMBOL(spa_sync_allpools
);
9811 EXPORT_SYMBOL(spa_prop_set
);
9812 EXPORT_SYMBOL(spa_prop_get
);
9813 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
9815 /* asynchronous event notification */
9816 EXPORT_SYMBOL(spa_event_notify
);
9819 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_shift
, INT
, ZMOD_RW
,
9820 "log2(fraction of arc that can be used by inflight I/Os when "
9821 "verifying pool during import");
9823 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_metadata
, INT
, ZMOD_RW
,
9824 "Set to traverse metadata on pool import");
9826 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_data
, INT
, ZMOD_RW
,
9827 "Set to traverse data on pool import");
9829 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_print_vdev_tree
, INT
, ZMOD_RW
,
9830 "Print vdev tree to zfs_dbgmsg during pool import");
9832 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_pct
, UINT
, ZMOD_RD
,
9833 "Percentage of CPUs to run an IO worker thread");
9835 ZFS_MODULE_PARAM(zfs
, zfs_
, max_missing_tvds
, ULONG
, ZMOD_RW
,
9836 "Allow importing pool with up to this number of missing top-level "
9837 "vdevs (in read-only mode)");
9839 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_pause
, INT
, ZMOD_RW
,
9840 "Set the livelist condense zthr to pause");
9842 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_pause
, INT
, ZMOD_RW
,
9843 "Set the livelist condense synctask to pause");
9845 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_cancel
, INT
, ZMOD_RW
,
9846 "Whether livelist condensing was canceled in the synctask");
9848 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_cancel
, INT
, ZMOD_RW
,
9849 "Whether livelist condensing was canceled in the zthr function");
9851 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, new_alloc
, INT
, ZMOD_RW
,
9852 "Whether extra ALLOC blkptrs were added to a livelist entry while it "
9853 "was being condensed");