4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25 * Copyright (c) 2018, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright 2013 Saso Kiselkov. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
29 * Copyright 2016 Toomas Soome <tsoome@me.com>
30 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
31 * Copyright 2018 Joyent, Inc.
32 * Copyright (c) 2017, 2019, Datto Inc. All rights reserved.
33 * Copyright 2017 Joyent, Inc.
34 * Copyright (c) 2017, Intel Corporation.
35 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
39 * SPA: Storage Pool Allocator
41 * This file contains all the routines used when modifying on-disk SPA state.
42 * This includes opening, importing, destroying, exporting a pool, and syncing a
46 #include <sys/zfs_context.h>
47 #include <sys/fm/fs/zfs.h>
48 #include <sys/spa_impl.h>
50 #include <sys/zio_checksum.h>
52 #include <sys/dmu_tx.h>
56 #include <sys/vdev_impl.h>
57 #include <sys/vdev_removal.h>
58 #include <sys/vdev_indirect_mapping.h>
59 #include <sys/vdev_indirect_births.h>
60 #include <sys/vdev_initialize.h>
61 #include <sys/vdev_rebuild.h>
62 #include <sys/vdev_trim.h>
63 #include <sys/vdev_disk.h>
64 #include <sys/vdev_draid.h>
65 #include <sys/metaslab.h>
66 #include <sys/metaslab_impl.h>
68 #include <sys/uberblock_impl.h>
71 #include <sys/bpobj.h>
72 #include <sys/dmu_traverse.h>
73 #include <sys/dmu_objset.h>
74 #include <sys/unique.h>
75 #include <sys/dsl_pool.h>
76 #include <sys/dsl_dataset.h>
77 #include <sys/dsl_dir.h>
78 #include <sys/dsl_prop.h>
79 #include <sys/dsl_synctask.h>
80 #include <sys/fs/zfs.h>
82 #include <sys/callb.h>
83 #include <sys/systeminfo.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_SCALE
, /* Taskqs scale with CPUs. */
111 ZTI_MODE_NULL
, /* don't create a taskq */
115 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
116 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
117 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
118 #define ZTI_SCALE { ZTI_MODE_SCALE, 0, 1 }
119 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
121 #define ZTI_N(n) ZTI_P(n, 1)
122 #define ZTI_ONE ZTI_N(1)
124 typedef struct zio_taskq_info
{
125 zti_modes_t zti_mode
;
130 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
131 "iss", "iss_h", "int", "int_h"
135 * This table defines the taskq settings for each ZFS I/O type. When
136 * initializing a pool, we use this table to create an appropriately sized
137 * taskq. Some operations are low volume and therefore have a small, static
138 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
139 * macros. Other operations process a large amount of data; the ZTI_BATCH
140 * macro causes us to create a taskq oriented for throughput. Some operations
141 * are so high frequency and short-lived that the taskq itself can become a
142 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
143 * additional degree of parallelism specified by the number of threads per-
144 * taskq and the number of taskqs; when dispatching an event in this case, the
145 * particular taskq is chosen at random. ZTI_SCALE is similar to ZTI_BATCH,
146 * but with number of taskqs also scaling with number of CPUs.
148 * The different taskq priorities are to handle the different contexts (issue
149 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
150 * need to be handled with minimum delay.
152 static const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
153 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
154 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
155 { ZTI_N(8), ZTI_NULL
, ZTI_SCALE
, ZTI_NULL
}, /* READ */
156 { ZTI_BATCH
, ZTI_N(5), ZTI_SCALE
, ZTI_N(5) }, /* WRITE */
157 { ZTI_SCALE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
158 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
159 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
160 { ZTI_N(4), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* TRIM */
163 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
164 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
165 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
166 static int spa_load_impl(spa_t
*spa
, spa_import_type_t type
,
167 const char **ereport
);
168 static void spa_vdev_resilver_done(spa_t
*spa
);
170 static uint_t zio_taskq_batch_pct
= 80; /* 1 thread per cpu in pset */
171 static uint_t zio_taskq_batch_tpq
; /* threads per taskq */
172 static const boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
173 static const uint_t zio_taskq_basedc
= 80; /* base duty cycle */
175 static const boolean_t spa_create_process
= B_TRUE
; /* no process => no sysdc */
178 * Report any spa_load_verify errors found, but do not fail spa_load.
179 * This is used by zdb to analyze non-idle pools.
181 boolean_t spa_load_verify_dryrun
= B_FALSE
;
184 * Allow read spacemaps in case of readonly import (spa_mode == SPA_MODE_READ).
185 * This is used by zdb for spacemaps verification.
187 boolean_t spa_mode_readable_spacemaps
= B_FALSE
;
190 * This (illegal) pool name is used when temporarily importing a spa_t in order
191 * to get the vdev stats associated with the imported devices.
193 #define TRYIMPORT_NAME "$import"
196 * For debugging purposes: print out vdev tree during pool import.
198 static int spa_load_print_vdev_tree
= B_FALSE
;
201 * A non-zero value for zfs_max_missing_tvds means that we allow importing
202 * pools with missing top-level vdevs. This is strictly intended for advanced
203 * pool recovery cases since missing data is almost inevitable. Pools with
204 * missing devices can only be imported read-only for safety reasons, and their
205 * fail-mode will be automatically set to "continue".
207 * With 1 missing vdev we should be able to import the pool and mount all
208 * datasets. User data that was not modified after the missing device has been
209 * added should be recoverable. This means that snapshots created prior to the
210 * addition of that device should be completely intact.
212 * With 2 missing vdevs, some datasets may fail to mount since there are
213 * dataset statistics that are stored as regular metadata. Some data might be
214 * recoverable if those vdevs were added recently.
216 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
217 * may be missing entirely. Chances of data recovery are very low. Note that
218 * there are also risks of performing an inadvertent rewind as we might be
219 * missing all the vdevs with the latest uberblocks.
221 uint64_t zfs_max_missing_tvds
= 0;
224 * The parameters below are similar to zfs_max_missing_tvds but are only
225 * intended for a preliminary open of the pool with an untrusted config which
226 * might be incomplete or out-dated.
228 * We are more tolerant for pools opened from a cachefile since we could have
229 * an out-dated cachefile where a device removal was not registered.
230 * We could have set the limit arbitrarily high but in the case where devices
231 * are really missing we would want to return the proper error codes; we chose
232 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
233 * and we get a chance to retrieve the trusted config.
235 uint64_t zfs_max_missing_tvds_cachefile
= SPA_DVAS_PER_BP
- 1;
238 * In the case where config was assembled by scanning device paths (/dev/dsks
239 * by default) we are less tolerant since all the existing devices should have
240 * been detected and we want spa_load to return the right error codes.
242 uint64_t zfs_max_missing_tvds_scan
= 0;
245 * Debugging aid that pauses spa_sync() towards the end.
247 static const boolean_t zfs_pause_spa_sync
= B_FALSE
;
250 * Variables to indicate the livelist condense zthr func should wait at certain
251 * points for the livelist to be removed - used to test condense/destroy races
253 static int zfs_livelist_condense_zthr_pause
= 0;
254 static int zfs_livelist_condense_sync_pause
= 0;
257 * Variables to track whether or not condense cancellation has been
258 * triggered in testing.
260 static int zfs_livelist_condense_sync_cancel
= 0;
261 static int zfs_livelist_condense_zthr_cancel
= 0;
264 * Variable to track whether or not extra ALLOC blkptrs were added to a
265 * livelist entry while it was being condensed (caused by the way we track
266 * remapped blkptrs in dbuf_remap_impl)
268 static int zfs_livelist_condense_new_alloc
= 0;
271 * ==========================================================================
272 * SPA properties routines
273 * ==========================================================================
277 * Add a (source=src, propname=propval) list to an nvlist.
280 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, const char *strval
,
281 uint64_t intval
, zprop_source_t src
)
283 const char *propname
= zpool_prop_to_name(prop
);
286 propval
= fnvlist_alloc();
287 fnvlist_add_uint64(propval
, ZPROP_SOURCE
, src
);
290 fnvlist_add_string(propval
, ZPROP_VALUE
, strval
);
292 fnvlist_add_uint64(propval
, ZPROP_VALUE
, intval
);
294 fnvlist_add_nvlist(nvl
, propname
, propval
);
295 nvlist_free(propval
);
299 * Get property values from the spa configuration.
302 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
304 vdev_t
*rvd
= spa
->spa_root_vdev
;
305 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
306 uint64_t size
, alloc
, cap
, version
;
307 const zprop_source_t src
= ZPROP_SRC_NONE
;
308 spa_config_dirent_t
*dp
;
309 metaslab_class_t
*mc
= spa_normal_class(spa
);
311 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
314 alloc
= metaslab_class_get_alloc(mc
);
315 alloc
+= metaslab_class_get_alloc(spa_special_class(spa
));
316 alloc
+= metaslab_class_get_alloc(spa_dedup_class(spa
));
317 alloc
+= metaslab_class_get_alloc(spa_embedded_log_class(spa
));
319 size
= metaslab_class_get_space(mc
);
320 size
+= metaslab_class_get_space(spa_special_class(spa
));
321 size
+= metaslab_class_get_space(spa_dedup_class(spa
));
322 size
+= metaslab_class_get_space(spa_embedded_log_class(spa
));
324 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
325 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
326 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
327 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
329 spa_prop_add_list(*nvp
, ZPOOL_PROP_CHECKPOINT
, NULL
,
330 spa
->spa_checkpoint_info
.sci_dspace
, src
);
332 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
333 metaslab_class_fragmentation(mc
), src
);
334 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
335 metaslab_class_expandable_space(mc
), src
);
336 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
337 (spa_mode(spa
) == SPA_MODE_READ
), src
);
339 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
340 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
342 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
343 ddt_get_pool_dedup_ratio(spa
), src
);
345 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
346 rvd
->vdev_state
, src
);
348 version
= spa_version(spa
);
349 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
350 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
351 version
, ZPROP_SRC_DEFAULT
);
353 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
354 version
, ZPROP_SRC_LOCAL
);
356 spa_prop_add_list(*nvp
, ZPOOL_PROP_LOAD_GUID
,
357 NULL
, spa_load_guid(spa
), src
);
362 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
363 * when opening pools before this version freedir will be NULL.
365 if (pool
->dp_free_dir
!= NULL
) {
366 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
367 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
370 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
374 if (pool
->dp_leak_dir
!= NULL
) {
375 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
376 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
379 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
384 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
386 if (spa
->spa_comment
!= NULL
) {
387 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
391 if (spa
->spa_compatibility
!= NULL
) {
392 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMPATIBILITY
,
393 spa
->spa_compatibility
, 0, ZPROP_SRC_LOCAL
);
396 if (spa
->spa_root
!= NULL
)
397 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
400 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
401 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
402 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
404 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
405 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
408 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
409 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
410 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
412 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
413 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
416 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
417 if (dp
->scd_path
== NULL
) {
418 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
419 "none", 0, ZPROP_SRC_LOCAL
);
420 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
421 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
422 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
428 * Get zpool property values.
431 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
433 objset_t
*mos
= spa
->spa_meta_objset
;
439 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
443 dp
= spa_get_dsl(spa
);
444 dsl_pool_config_enter(dp
, FTAG
);
445 mutex_enter(&spa
->spa_props_lock
);
448 * Get properties from the spa config.
450 spa_prop_get_config(spa
, nvp
);
452 /* If no pool property object, no more prop to get. */
453 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0)
457 * Get properties from the MOS pool property object.
459 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
460 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
461 zap_cursor_advance(&zc
)) {
464 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
467 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPOOL_PROP_INVAL
)
470 switch (za
.za_integer_length
) {
472 /* integer property */
473 if (za
.za_first_integer
!=
474 zpool_prop_default_numeric(prop
))
475 src
= ZPROP_SRC_LOCAL
;
477 if (prop
== ZPOOL_PROP_BOOTFS
) {
478 dsl_dataset_t
*ds
= NULL
;
480 err
= dsl_dataset_hold_obj(dp
,
481 za
.za_first_integer
, FTAG
, &ds
);
485 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
487 dsl_dataset_name(ds
, strval
);
488 dsl_dataset_rele(ds
, FTAG
);
491 intval
= za
.za_first_integer
;
494 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
497 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
502 /* string property */
503 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
504 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
505 za
.za_name
, 1, za
.za_num_integers
, strval
);
507 kmem_free(strval
, za
.za_num_integers
);
510 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
511 kmem_free(strval
, za
.za_num_integers
);
518 zap_cursor_fini(&zc
);
520 mutex_exit(&spa
->spa_props_lock
);
521 dsl_pool_config_exit(dp
, FTAG
);
522 if (err
&& err
!= ENOENT
) {
532 * Validate the given pool properties nvlist and modify the list
533 * for the property values to be set.
536 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
539 int error
= 0, reset_bootfs
= 0;
541 boolean_t has_feature
= B_FALSE
;
544 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
546 char *strval
, *slash
, *check
, *fname
;
547 const char *propname
= nvpair_name(elem
);
548 zpool_prop_t prop
= zpool_name_to_prop(propname
);
551 case ZPOOL_PROP_INVAL
:
552 if (!zpool_prop_feature(propname
)) {
553 error
= SET_ERROR(EINVAL
);
558 * Sanitize the input.
560 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
561 error
= SET_ERROR(EINVAL
);
565 if (nvpair_value_uint64(elem
, &intval
) != 0) {
566 error
= SET_ERROR(EINVAL
);
571 error
= SET_ERROR(EINVAL
);
575 fname
= strchr(propname
, '@') + 1;
576 if (zfeature_lookup_name(fname
, NULL
) != 0) {
577 error
= SET_ERROR(EINVAL
);
581 has_feature
= B_TRUE
;
584 case ZPOOL_PROP_VERSION
:
585 error
= nvpair_value_uint64(elem
, &intval
);
587 (intval
< spa_version(spa
) ||
588 intval
> SPA_VERSION_BEFORE_FEATURES
||
590 error
= SET_ERROR(EINVAL
);
593 case ZPOOL_PROP_DELEGATION
:
594 case ZPOOL_PROP_AUTOREPLACE
:
595 case ZPOOL_PROP_LISTSNAPS
:
596 case ZPOOL_PROP_AUTOEXPAND
:
597 case ZPOOL_PROP_AUTOTRIM
:
598 error
= nvpair_value_uint64(elem
, &intval
);
599 if (!error
&& intval
> 1)
600 error
= SET_ERROR(EINVAL
);
603 case ZPOOL_PROP_MULTIHOST
:
604 error
= nvpair_value_uint64(elem
, &intval
);
605 if (!error
&& intval
> 1)
606 error
= SET_ERROR(EINVAL
);
609 uint32_t hostid
= zone_get_hostid(NULL
);
611 spa
->spa_hostid
= hostid
;
613 error
= SET_ERROR(ENOTSUP
);
618 case ZPOOL_PROP_BOOTFS
:
620 * If the pool version is less than SPA_VERSION_BOOTFS,
621 * or the pool is still being created (version == 0),
622 * the bootfs property cannot be set.
624 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
625 error
= SET_ERROR(ENOTSUP
);
630 * Make sure the vdev config is bootable
632 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
633 error
= SET_ERROR(ENOTSUP
);
639 error
= nvpair_value_string(elem
, &strval
);
644 if (strval
== NULL
|| strval
[0] == '\0') {
645 objnum
= zpool_prop_default_numeric(
650 error
= dmu_objset_hold(strval
, FTAG
, &os
);
655 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
656 error
= SET_ERROR(ENOTSUP
);
658 objnum
= dmu_objset_id(os
);
660 dmu_objset_rele(os
, FTAG
);
664 case ZPOOL_PROP_FAILUREMODE
:
665 error
= nvpair_value_uint64(elem
, &intval
);
666 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
667 error
= SET_ERROR(EINVAL
);
670 * This is a special case which only occurs when
671 * the pool has completely failed. This allows
672 * the user to change the in-core failmode property
673 * without syncing it out to disk (I/Os might
674 * currently be blocked). We do this by returning
675 * EIO to the caller (spa_prop_set) to trick it
676 * into thinking we encountered a property validation
679 if (!error
&& spa_suspended(spa
)) {
680 spa
->spa_failmode
= intval
;
681 error
= SET_ERROR(EIO
);
685 case ZPOOL_PROP_CACHEFILE
:
686 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
689 if (strval
[0] == '\0')
692 if (strcmp(strval
, "none") == 0)
695 if (strval
[0] != '/') {
696 error
= SET_ERROR(EINVAL
);
700 slash
= strrchr(strval
, '/');
701 ASSERT(slash
!= NULL
);
703 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
704 strcmp(slash
, "/..") == 0)
705 error
= SET_ERROR(EINVAL
);
708 case ZPOOL_PROP_COMMENT
:
709 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
711 for (check
= strval
; *check
!= '\0'; check
++) {
712 if (!isprint(*check
)) {
713 error
= SET_ERROR(EINVAL
);
717 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
718 error
= SET_ERROR(E2BIG
);
729 (void) nvlist_remove_all(props
,
730 zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO
));
732 if (!error
&& reset_bootfs
) {
733 error
= nvlist_remove(props
,
734 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
737 error
= nvlist_add_uint64(props
,
738 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
746 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
749 spa_config_dirent_t
*dp
;
751 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
755 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
758 if (cachefile
[0] == '\0')
759 dp
->scd_path
= spa_strdup(spa_config_path
);
760 else if (strcmp(cachefile
, "none") == 0)
763 dp
->scd_path
= spa_strdup(cachefile
);
765 list_insert_head(&spa
->spa_config_list
, dp
);
767 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
771 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
774 nvpair_t
*elem
= NULL
;
775 boolean_t need_sync
= B_FALSE
;
777 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
780 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
781 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
783 if (prop
== ZPOOL_PROP_CACHEFILE
||
784 prop
== ZPOOL_PROP_ALTROOT
||
785 prop
== ZPOOL_PROP_READONLY
)
788 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
791 if (prop
== ZPOOL_PROP_VERSION
) {
792 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
794 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
795 ver
= SPA_VERSION_FEATURES
;
799 /* Save time if the version is already set. */
800 if (ver
== spa_version(spa
))
804 * In addition to the pool directory object, we might
805 * create the pool properties object, the features for
806 * read object, the features for write object, or the
807 * feature descriptions object.
809 error
= dsl_sync_task(spa
->spa_name
, NULL
,
810 spa_sync_version
, &ver
,
811 6, ZFS_SPACE_CHECK_RESERVED
);
822 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
823 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
830 * If the bootfs property value is dsobj, clear it.
833 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
835 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
836 VERIFY(zap_remove(spa
->spa_meta_objset
,
837 spa
->spa_pool_props_object
,
838 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
844 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
846 uint64_t *newguid __maybe_unused
= arg
;
847 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
848 vdev_t
*rvd
= spa
->spa_root_vdev
;
851 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
852 int error
= (spa_has_checkpoint(spa
)) ?
853 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
854 return (SET_ERROR(error
));
857 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
858 vdev_state
= rvd
->vdev_state
;
859 spa_config_exit(spa
, SCL_STATE
, FTAG
);
861 if (vdev_state
!= VDEV_STATE_HEALTHY
)
862 return (SET_ERROR(ENXIO
));
864 ASSERT3U(spa_guid(spa
), !=, *newguid
);
870 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
872 uint64_t *newguid
= arg
;
873 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
875 vdev_t
*rvd
= spa
->spa_root_vdev
;
877 oldguid
= spa_guid(spa
);
879 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
880 rvd
->vdev_guid
= *newguid
;
881 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
882 vdev_config_dirty(rvd
);
883 spa_config_exit(spa
, SCL_STATE
, FTAG
);
885 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
886 (u_longlong_t
)oldguid
, (u_longlong_t
)*newguid
);
890 * Change the GUID for the pool. This is done so that we can later
891 * re-import a pool built from a clone of our own vdevs. We will modify
892 * the root vdev's guid, our own pool guid, and then mark all of our
893 * vdevs dirty. Note that we must make sure that all our vdevs are
894 * online when we do this, or else any vdevs that weren't present
895 * would be orphaned from our pool. We are also going to issue a
896 * sysevent to update any watchers.
899 spa_change_guid(spa_t
*spa
)
904 mutex_enter(&spa
->spa_vdev_top_lock
);
905 mutex_enter(&spa_namespace_lock
);
906 guid
= spa_generate_guid(NULL
);
908 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
909 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
913 * Clear the kobj flag from all the vdevs to allow
914 * vdev_cache_process_kobj_evt() to post events to all the
915 * vdevs since GUID is updated.
917 vdev_clear_kobj_evt(spa
->spa_root_vdev
);
918 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
919 vdev_clear_kobj_evt(spa
->spa_l2cache
.sav_vdevs
[i
]);
921 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
922 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
925 mutex_exit(&spa_namespace_lock
);
926 mutex_exit(&spa
->spa_vdev_top_lock
);
932 * ==========================================================================
933 * SPA state manipulation (open/create/destroy/import/export)
934 * ==========================================================================
938 spa_error_entry_compare(const void *a
, const void *b
)
940 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
941 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
944 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
945 sizeof (zbookmark_phys_t
));
947 return (TREE_ISIGN(ret
));
951 * Utility function which retrieves copies of the current logs and
952 * re-initializes them in the process.
955 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
957 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
959 memcpy(last
, &spa
->spa_errlist_last
, sizeof (avl_tree_t
));
960 memcpy(scrub
, &spa
->spa_errlist_scrub
, sizeof (avl_tree_t
));
962 avl_create(&spa
->spa_errlist_scrub
,
963 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
964 offsetof(spa_error_entry_t
, se_avl
));
965 avl_create(&spa
->spa_errlist_last
,
966 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
967 offsetof(spa_error_entry_t
, se_avl
));
971 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
973 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
974 enum zti_modes mode
= ztip
->zti_mode
;
975 uint_t value
= ztip
->zti_value
;
976 uint_t count
= ztip
->zti_count
;
977 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
978 uint_t cpus
, flags
= TASKQ_DYNAMIC
;
979 boolean_t batch
= B_FALSE
;
983 ASSERT3U(value
, >, 0);
988 flags
|= TASKQ_THREADS_CPU_PCT
;
989 value
= MIN(zio_taskq_batch_pct
, 100);
993 flags
|= TASKQ_THREADS_CPU_PCT
;
995 * We want more taskqs to reduce lock contention, but we want
996 * less for better request ordering and CPU utilization.
998 cpus
= MAX(1, boot_ncpus
* zio_taskq_batch_pct
/ 100);
999 if (zio_taskq_batch_tpq
> 0) {
1000 count
= MAX(1, (cpus
+ zio_taskq_batch_tpq
/ 2) /
1001 zio_taskq_batch_tpq
);
1004 * Prefer 6 threads per taskq, but no more taskqs
1005 * than threads in them on large systems. For 80%:
1008 * cpus taskqs percent threads threads
1009 * ------- ------- ------- ------- -------
1020 count
= 1 + cpus
/ 6;
1021 while (count
* count
> cpus
)
1024 /* Limit each taskq within 100% to not trigger assertion. */
1025 count
= MAX(count
, (zio_taskq_batch_pct
+ 99) / 100);
1026 value
= (zio_taskq_batch_pct
+ count
/ 2) / count
;
1030 tqs
->stqs_count
= 0;
1031 tqs
->stqs_taskq
= NULL
;
1035 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
1037 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
1041 ASSERT3U(count
, >, 0);
1042 tqs
->stqs_count
= count
;
1043 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
1045 for (uint_t i
= 0; i
< count
; i
++) {
1050 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
1051 zio_type_name
[t
], zio_taskq_types
[q
], i
);
1053 (void) snprintf(name
, sizeof (name
), "%s_%s",
1054 zio_type_name
[t
], zio_taskq_types
[q
]);
1056 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
1058 flags
|= TASKQ_DC_BATCH
;
1060 (void) zio_taskq_basedc
;
1061 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
1062 spa
->spa_proc
, zio_taskq_basedc
, flags
);
1064 pri_t pri
= maxclsyspri
;
1066 * The write issue taskq can be extremely CPU
1067 * intensive. Run it at slightly less important
1068 * priority than the other taskqs.
1070 * Under Linux and FreeBSD this means incrementing
1071 * the priority value as opposed to platforms like
1072 * illumos where it should be decremented.
1074 * On FreeBSD, if priorities divided by four (RQ_PPQ)
1075 * are equal then a difference between them is
1078 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
) {
1079 #if defined(__linux__)
1081 #elif defined(__FreeBSD__)
1087 tq
= taskq_create_proc(name
, value
, pri
, 50,
1088 INT_MAX
, spa
->spa_proc
, flags
);
1091 tqs
->stqs_taskq
[i
] = tq
;
1096 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1098 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1100 if (tqs
->stqs_taskq
== NULL
) {
1101 ASSERT3U(tqs
->stqs_count
, ==, 0);
1105 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
1106 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
1107 taskq_destroy(tqs
->stqs_taskq
[i
]);
1110 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
1111 tqs
->stqs_taskq
= NULL
;
1115 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1116 * Note that a type may have multiple discrete taskqs to avoid lock contention
1117 * on the taskq itself. In that case we choose which taskq at random by using
1118 * the low bits of gethrtime().
1121 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1122 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
1124 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1127 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1128 ASSERT3U(tqs
->stqs_count
, !=, 0);
1130 if (tqs
->stqs_count
== 1) {
1131 tq
= tqs
->stqs_taskq
[0];
1133 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1136 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
1140 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
1143 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1144 task_func_t
*func
, void *arg
, uint_t flags
)
1146 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1150 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1151 ASSERT3U(tqs
->stqs_count
, !=, 0);
1153 if (tqs
->stqs_count
== 1) {
1154 tq
= tqs
->stqs_taskq
[0];
1156 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1159 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1161 taskq_wait_id(tq
, id
);
1165 spa_create_zio_taskqs(spa_t
*spa
)
1167 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1168 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1169 spa_taskqs_init(spa
, t
, q
);
1175 * Disabled until spa_thread() can be adapted for Linux.
1177 #undef HAVE_SPA_THREAD
1179 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1181 spa_thread(void *arg
)
1183 psetid_t zio_taskq_psrset_bind
= PS_NONE
;
1184 callb_cpr_t cprinfo
;
1187 user_t
*pu
= PTOU(curproc
);
1189 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1192 ASSERT(curproc
!= &p0
);
1193 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1194 "zpool-%s", spa
->spa_name
);
1195 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1197 /* bind this thread to the requested psrset */
1198 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1200 mutex_enter(&cpu_lock
);
1201 mutex_enter(&pidlock
);
1202 mutex_enter(&curproc
->p_lock
);
1204 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1205 0, NULL
, NULL
) == 0) {
1206 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1209 "Couldn't bind process for zfs pool \"%s\" to "
1210 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1213 mutex_exit(&curproc
->p_lock
);
1214 mutex_exit(&pidlock
);
1215 mutex_exit(&cpu_lock
);
1219 if (zio_taskq_sysdc
) {
1220 sysdc_thread_enter(curthread
, 100, 0);
1223 spa
->spa_proc
= curproc
;
1224 spa
->spa_did
= curthread
->t_did
;
1226 spa_create_zio_taskqs(spa
);
1228 mutex_enter(&spa
->spa_proc_lock
);
1229 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1231 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1232 cv_broadcast(&spa
->spa_proc_cv
);
1234 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1235 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1236 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1237 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1239 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1240 spa
->spa_proc_state
= SPA_PROC_GONE
;
1241 spa
->spa_proc
= &p0
;
1242 cv_broadcast(&spa
->spa_proc_cv
);
1243 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1245 mutex_enter(&curproc
->p_lock
);
1251 * Activate an uninitialized pool.
1254 spa_activate(spa_t
*spa
, spa_mode_t mode
)
1256 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1258 spa
->spa_state
= POOL_STATE_ACTIVE
;
1259 spa
->spa_mode
= mode
;
1260 spa
->spa_read_spacemaps
= spa_mode_readable_spacemaps
;
1262 spa
->spa_normal_class
= metaslab_class_create(spa
, &zfs_metaslab_ops
);
1263 spa
->spa_log_class
= metaslab_class_create(spa
, &zfs_metaslab_ops
);
1264 spa
->spa_embedded_log_class
=
1265 metaslab_class_create(spa
, &zfs_metaslab_ops
);
1266 spa
->spa_special_class
= metaslab_class_create(spa
, &zfs_metaslab_ops
);
1267 spa
->spa_dedup_class
= metaslab_class_create(spa
, &zfs_metaslab_ops
);
1269 /* Try to create a covering process */
1270 mutex_enter(&spa
->spa_proc_lock
);
1271 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1272 ASSERT(spa
->spa_proc
== &p0
);
1275 (void) spa_create_process
;
1276 #ifdef HAVE_SPA_THREAD
1277 /* Only create a process if we're going to be around a while. */
1278 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1279 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1281 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1282 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1283 cv_wait(&spa
->spa_proc_cv
,
1284 &spa
->spa_proc_lock
);
1286 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1287 ASSERT(spa
->spa_proc
!= &p0
);
1288 ASSERT(spa
->spa_did
!= 0);
1292 "Couldn't create process for zfs pool \"%s\"\n",
1297 #endif /* HAVE_SPA_THREAD */
1298 mutex_exit(&spa
->spa_proc_lock
);
1300 /* If we didn't create a process, we need to create our taskqs. */
1301 if (spa
->spa_proc
== &p0
) {
1302 spa_create_zio_taskqs(spa
);
1305 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1306 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
,
1310 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1311 offsetof(vdev_t
, vdev_config_dirty_node
));
1312 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1313 offsetof(objset_t
, os_evicting_node
));
1314 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1315 offsetof(vdev_t
, vdev_state_dirty_node
));
1317 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1318 offsetof(struct vdev
, vdev_txg_node
));
1320 avl_create(&spa
->spa_errlist_scrub
,
1321 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1322 offsetof(spa_error_entry_t
, se_avl
));
1323 avl_create(&spa
->spa_errlist_last
,
1324 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1325 offsetof(spa_error_entry_t
, se_avl
));
1326 avl_create(&spa
->spa_errlist_healed
,
1327 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1328 offsetof(spa_error_entry_t
, se_avl
));
1330 spa_activate_os(spa
);
1332 spa_keystore_init(&spa
->spa_keystore
);
1335 * This taskq is used to perform zvol-minor-related tasks
1336 * asynchronously. This has several advantages, including easy
1337 * resolution of various deadlocks.
1339 * The taskq must be single threaded to ensure tasks are always
1340 * processed in the order in which they were dispatched.
1342 * A taskq per pool allows one to keep the pools independent.
1343 * This way if one pool is suspended, it will not impact another.
1345 * The preferred location to dispatch a zvol minor task is a sync
1346 * task. In this context, there is easy access to the spa_t and minimal
1347 * error handling is required because the sync task must succeed.
1349 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1353 * Taskq dedicated to prefetcher threads: this is used to prevent the
1354 * pool traverse code from monopolizing the global (and limited)
1355 * system_taskq by inappropriately scheduling long running tasks on it.
1357 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", 100,
1358 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1361 * The taskq to upgrade datasets in this pool. Currently used by
1362 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1364 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", 100,
1365 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1369 * Opposite of spa_activate().
1372 spa_deactivate(spa_t
*spa
)
1374 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1375 ASSERT(spa
->spa_dsl_pool
== NULL
);
1376 ASSERT(spa
->spa_root_vdev
== NULL
);
1377 ASSERT(spa
->spa_async_zio_root
== NULL
);
1378 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1380 spa_evicting_os_wait(spa
);
1382 if (spa
->spa_zvol_taskq
) {
1383 taskq_destroy(spa
->spa_zvol_taskq
);
1384 spa
->spa_zvol_taskq
= NULL
;
1387 if (spa
->spa_prefetch_taskq
) {
1388 taskq_destroy(spa
->spa_prefetch_taskq
);
1389 spa
->spa_prefetch_taskq
= NULL
;
1392 if (spa
->spa_upgrade_taskq
) {
1393 taskq_destroy(spa
->spa_upgrade_taskq
);
1394 spa
->spa_upgrade_taskq
= NULL
;
1397 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1399 list_destroy(&spa
->spa_config_dirty_list
);
1400 list_destroy(&spa
->spa_evicting_os_list
);
1401 list_destroy(&spa
->spa_state_dirty_list
);
1403 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1405 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1406 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1407 spa_taskqs_fini(spa
, t
, q
);
1411 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1412 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1413 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1414 spa
->spa_txg_zio
[i
] = NULL
;
1417 metaslab_class_destroy(spa
->spa_normal_class
);
1418 spa
->spa_normal_class
= NULL
;
1420 metaslab_class_destroy(spa
->spa_log_class
);
1421 spa
->spa_log_class
= NULL
;
1423 metaslab_class_destroy(spa
->spa_embedded_log_class
);
1424 spa
->spa_embedded_log_class
= NULL
;
1426 metaslab_class_destroy(spa
->spa_special_class
);
1427 spa
->spa_special_class
= NULL
;
1429 metaslab_class_destroy(spa
->spa_dedup_class
);
1430 spa
->spa_dedup_class
= NULL
;
1433 * If this was part of an import or the open otherwise failed, we may
1434 * still have errors left in the queues. Empty them just in case.
1436 spa_errlog_drain(spa
);
1437 avl_destroy(&spa
->spa_errlist_scrub
);
1438 avl_destroy(&spa
->spa_errlist_last
);
1439 avl_destroy(&spa
->spa_errlist_healed
);
1441 spa_keystore_fini(&spa
->spa_keystore
);
1443 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1445 mutex_enter(&spa
->spa_proc_lock
);
1446 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1447 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1448 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1449 cv_broadcast(&spa
->spa_proc_cv
);
1450 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1451 ASSERT(spa
->spa_proc
!= &p0
);
1452 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1454 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1455 spa
->spa_proc_state
= SPA_PROC_NONE
;
1457 ASSERT(spa
->spa_proc
== &p0
);
1458 mutex_exit(&spa
->spa_proc_lock
);
1461 * We want to make sure spa_thread() has actually exited the ZFS
1462 * module, so that the module can't be unloaded out from underneath
1465 if (spa
->spa_did
!= 0) {
1466 thread_join(spa
->spa_did
);
1470 spa_deactivate_os(spa
);
1475 * Verify a pool configuration, and construct the vdev tree appropriately. This
1476 * will create all the necessary vdevs in the appropriate layout, with each vdev
1477 * in the CLOSED state. This will prep the pool before open/creation/import.
1478 * All vdev validation is done by the vdev_alloc() routine.
1481 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1482 uint_t id
, int atype
)
1488 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1491 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1494 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1497 if (error
== ENOENT
)
1503 return (SET_ERROR(EINVAL
));
1506 for (int c
= 0; c
< children
; c
++) {
1508 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1516 ASSERT(*vdp
!= NULL
);
1522 spa_should_flush_logs_on_unload(spa_t
*spa
)
1524 if (!spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
))
1527 if (!spa_writeable(spa
))
1530 if (!spa
->spa_sync_on
)
1533 if (spa_state(spa
) != POOL_STATE_EXPORTED
)
1536 if (zfs_keep_log_spacemaps_at_export
)
1543 * Opens a transaction that will set the flag that will instruct
1544 * spa_sync to attempt to flush all the metaslabs for that txg.
1547 spa_unload_log_sm_flush_all(spa_t
*spa
)
1549 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
1550 VERIFY0(dmu_tx_assign(tx
, TXG_WAIT
));
1552 ASSERT3U(spa
->spa_log_flushall_txg
, ==, 0);
1553 spa
->spa_log_flushall_txg
= dmu_tx_get_txg(tx
);
1556 txg_wait_synced(spa_get_dsl(spa
), spa
->spa_log_flushall_txg
);
1560 spa_unload_log_sm_metadata(spa_t
*spa
)
1562 void *cookie
= NULL
;
1564 while ((sls
= avl_destroy_nodes(&spa
->spa_sm_logs_by_txg
,
1565 &cookie
)) != NULL
) {
1566 VERIFY0(sls
->sls_mscount
);
1567 kmem_free(sls
, sizeof (spa_log_sm_t
));
1570 for (log_summary_entry_t
*e
= list_head(&spa
->spa_log_summary
);
1571 e
!= NULL
; e
= list_head(&spa
->spa_log_summary
)) {
1572 VERIFY0(e
->lse_mscount
);
1573 list_remove(&spa
->spa_log_summary
, e
);
1574 kmem_free(e
, sizeof (log_summary_entry_t
));
1577 spa
->spa_unflushed_stats
.sus_nblocks
= 0;
1578 spa
->spa_unflushed_stats
.sus_memused
= 0;
1579 spa
->spa_unflushed_stats
.sus_blocklimit
= 0;
1583 spa_destroy_aux_threads(spa_t
*spa
)
1585 if (spa
->spa_condense_zthr
!= NULL
) {
1586 zthr_destroy(spa
->spa_condense_zthr
);
1587 spa
->spa_condense_zthr
= NULL
;
1589 if (spa
->spa_checkpoint_discard_zthr
!= NULL
) {
1590 zthr_destroy(spa
->spa_checkpoint_discard_zthr
);
1591 spa
->spa_checkpoint_discard_zthr
= NULL
;
1593 if (spa
->spa_livelist_delete_zthr
!= NULL
) {
1594 zthr_destroy(spa
->spa_livelist_delete_zthr
);
1595 spa
->spa_livelist_delete_zthr
= NULL
;
1597 if (spa
->spa_livelist_condense_zthr
!= NULL
) {
1598 zthr_destroy(spa
->spa_livelist_condense_zthr
);
1599 spa
->spa_livelist_condense_zthr
= NULL
;
1604 * Opposite of spa_load().
1607 spa_unload(spa_t
*spa
)
1609 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1610 ASSERT(spa_state(spa
) != POOL_STATE_UNINITIALIZED
);
1612 spa_import_progress_remove(spa_guid(spa
));
1613 spa_load_note(spa
, "UNLOADING");
1615 spa_wake_waiters(spa
);
1618 * If we have set the spa_final_txg, we have already performed the
1619 * tasks below in spa_export_common(). We should not redo it here since
1620 * we delay the final TXGs beyond what spa_final_txg is set at.
1622 if (spa
->spa_final_txg
== UINT64_MAX
) {
1624 * If the log space map feature is enabled and the pool is
1625 * getting exported (but not destroyed), we want to spend some
1626 * time flushing as many metaslabs as we can in an attempt to
1627 * destroy log space maps and save import time.
1629 if (spa_should_flush_logs_on_unload(spa
))
1630 spa_unload_log_sm_flush_all(spa
);
1635 spa_async_suspend(spa
);
1637 if (spa
->spa_root_vdev
) {
1638 vdev_t
*root_vdev
= spa
->spa_root_vdev
;
1639 vdev_initialize_stop_all(root_vdev
,
1640 VDEV_INITIALIZE_ACTIVE
);
1641 vdev_trim_stop_all(root_vdev
, VDEV_TRIM_ACTIVE
);
1642 vdev_autotrim_stop_all(spa
);
1643 vdev_rebuild_stop_all(spa
);
1650 if (spa
->spa_sync_on
) {
1651 txg_sync_stop(spa
->spa_dsl_pool
);
1652 spa
->spa_sync_on
= B_FALSE
;
1656 * This ensures that there is no async metaslab prefetching
1657 * while we attempt to unload the spa.
1659 if (spa
->spa_root_vdev
!= NULL
) {
1660 for (int c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++) {
1661 vdev_t
*vc
= spa
->spa_root_vdev
->vdev_child
[c
];
1662 if (vc
->vdev_mg
!= NULL
)
1663 taskq_wait(vc
->vdev_mg
->mg_taskq
);
1667 if (spa
->spa_mmp
.mmp_thread
)
1668 mmp_thread_stop(spa
);
1671 * Wait for any outstanding async I/O to complete.
1673 if (spa
->spa_async_zio_root
!= NULL
) {
1674 for (int i
= 0; i
< max_ncpus
; i
++)
1675 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1676 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1677 spa
->spa_async_zio_root
= NULL
;
1680 if (spa
->spa_vdev_removal
!= NULL
) {
1681 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
1682 spa
->spa_vdev_removal
= NULL
;
1685 spa_destroy_aux_threads(spa
);
1687 spa_condense_fini(spa
);
1689 bpobj_close(&spa
->spa_deferred_bpobj
);
1691 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1696 if (spa
->spa_root_vdev
)
1697 vdev_free(spa
->spa_root_vdev
);
1698 ASSERT(spa
->spa_root_vdev
== NULL
);
1701 * Close the dsl pool.
1703 if (spa
->spa_dsl_pool
) {
1704 dsl_pool_close(spa
->spa_dsl_pool
);
1705 spa
->spa_dsl_pool
= NULL
;
1706 spa
->spa_meta_objset
= NULL
;
1710 spa_unload_log_sm_metadata(spa
);
1713 * Drop and purge level 2 cache
1715 spa_l2cache_drop(spa
);
1717 if (spa
->spa_spares
.sav_vdevs
) {
1718 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1719 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1720 kmem_free(spa
->spa_spares
.sav_vdevs
,
1721 spa
->spa_spares
.sav_count
* sizeof (void *));
1722 spa
->spa_spares
.sav_vdevs
= NULL
;
1724 if (spa
->spa_spares
.sav_config
) {
1725 nvlist_free(spa
->spa_spares
.sav_config
);
1726 spa
->spa_spares
.sav_config
= NULL
;
1728 spa
->spa_spares
.sav_count
= 0;
1730 if (spa
->spa_l2cache
.sav_vdevs
) {
1731 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1732 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1733 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1735 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1736 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1737 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1739 if (spa
->spa_l2cache
.sav_config
) {
1740 nvlist_free(spa
->spa_l2cache
.sav_config
);
1741 spa
->spa_l2cache
.sav_config
= NULL
;
1743 spa
->spa_l2cache
.sav_count
= 0;
1745 spa
->spa_async_suspended
= 0;
1747 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
1749 if (spa
->spa_comment
!= NULL
) {
1750 spa_strfree(spa
->spa_comment
);
1751 spa
->spa_comment
= NULL
;
1753 if (spa
->spa_compatibility
!= NULL
) {
1754 spa_strfree(spa
->spa_compatibility
);
1755 spa
->spa_compatibility
= NULL
;
1758 spa_config_exit(spa
, SCL_ALL
, spa
);
1762 * Load (or re-load) the current list of vdevs describing the active spares for
1763 * this pool. When this is called, we have some form of basic information in
1764 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1765 * then re-generate a more complete list including status information.
1768 spa_load_spares(spa_t
*spa
)
1777 * zdb opens both the current state of the pool and the
1778 * checkpointed state (if present), with a different spa_t.
1780 * As spare vdevs are shared among open pools, we skip loading
1781 * them when we load the checkpointed state of the pool.
1783 if (!spa_writeable(spa
))
1787 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1790 * First, close and free any existing spare vdevs.
1792 if (spa
->spa_spares
.sav_vdevs
) {
1793 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1794 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1796 /* Undo the call to spa_activate() below */
1797 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1798 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1799 spa_spare_remove(tvd
);
1804 kmem_free(spa
->spa_spares
.sav_vdevs
,
1805 spa
->spa_spares
.sav_count
* sizeof (void *));
1808 if (spa
->spa_spares
.sav_config
== NULL
)
1811 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1812 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
1814 spa
->spa_spares
.sav_count
= (int)nspares
;
1815 spa
->spa_spares
.sav_vdevs
= NULL
;
1821 * Construct the array of vdevs, opening them to get status in the
1822 * process. For each spare, there is potentially two different vdev_t
1823 * structures associated with it: one in the list of spares (used only
1824 * for basic validation purposes) and one in the active vdev
1825 * configuration (if it's spared in). During this phase we open and
1826 * validate each vdev on the spare list. If the vdev also exists in the
1827 * active configuration, then we also mark this vdev as an active spare.
1829 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1831 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1832 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1833 VDEV_ALLOC_SPARE
) == 0);
1836 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1838 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1839 B_FALSE
)) != NULL
) {
1840 if (!tvd
->vdev_isspare
)
1844 * We only mark the spare active if we were successfully
1845 * able to load the vdev. Otherwise, importing a pool
1846 * with a bad active spare would result in strange
1847 * behavior, because multiple pool would think the spare
1848 * is actively in use.
1850 * There is a vulnerability here to an equally bizarre
1851 * circumstance, where a dead active spare is later
1852 * brought back to life (onlined or otherwise). Given
1853 * the rarity of this scenario, and the extra complexity
1854 * it adds, we ignore the possibility.
1856 if (!vdev_is_dead(tvd
))
1857 spa_spare_activate(tvd
);
1861 vd
->vdev_aux
= &spa
->spa_spares
;
1863 if (vdev_open(vd
) != 0)
1866 if (vdev_validate_aux(vd
) == 0)
1871 * Recompute the stashed list of spares, with status information
1874 fnvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
);
1876 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1878 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1879 spares
[i
] = vdev_config_generate(spa
,
1880 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1881 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1882 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
1883 spa
->spa_spares
.sav_count
);
1884 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1885 nvlist_free(spares
[i
]);
1886 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1890 * Load (or re-load) the current list of vdevs describing the active l2cache for
1891 * this pool. When this is called, we have some form of basic information in
1892 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1893 * then re-generate a more complete list including status information.
1894 * Devices which are already active have their details maintained, and are
1898 spa_load_l2cache(spa_t
*spa
)
1900 nvlist_t
**l2cache
= NULL
;
1902 int i
, j
, oldnvdevs
;
1904 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1905 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1909 * zdb opens both the current state of the pool and the
1910 * checkpointed state (if present), with a different spa_t.
1912 * As L2 caches are part of the ARC which is shared among open
1913 * pools, we skip loading them when we load the checkpointed
1914 * state of the pool.
1916 if (!spa_writeable(spa
))
1920 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1922 oldvdevs
= sav
->sav_vdevs
;
1923 oldnvdevs
= sav
->sav_count
;
1924 sav
->sav_vdevs
= NULL
;
1927 if (sav
->sav_config
== NULL
) {
1933 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
,
1934 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
1935 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1938 * Process new nvlist of vdevs.
1940 for (i
= 0; i
< nl2cache
; i
++) {
1941 guid
= fnvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
);
1944 for (j
= 0; j
< oldnvdevs
; j
++) {
1946 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1948 * Retain previous vdev for add/remove ops.
1956 if (newvdevs
[i
] == NULL
) {
1960 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1961 VDEV_ALLOC_L2CACHE
) == 0);
1966 * Commit this vdev as an l2cache device,
1967 * even if it fails to open.
1969 spa_l2cache_add(vd
);
1974 spa_l2cache_activate(vd
);
1976 if (vdev_open(vd
) != 0)
1979 (void) vdev_validate_aux(vd
);
1981 if (!vdev_is_dead(vd
))
1982 l2arc_add_vdev(spa
, vd
);
1985 * Upon cache device addition to a pool or pool
1986 * creation with a cache device or if the header
1987 * of the device is invalid we issue an async
1988 * TRIM command for the whole device which will
1989 * execute if l2arc_trim_ahead > 0.
1991 spa_async_request(spa
, SPA_ASYNC_L2CACHE_TRIM
);
1995 sav
->sav_vdevs
= newvdevs
;
1996 sav
->sav_count
= (int)nl2cache
;
1999 * Recompute the stashed list of l2cache devices, with status
2000 * information this time.
2002 fnvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
);
2004 if (sav
->sav_count
> 0)
2005 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
2007 for (i
= 0; i
< sav
->sav_count
; i
++)
2008 l2cache
[i
] = vdev_config_generate(spa
,
2009 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
2010 fnvlist_add_nvlist_array(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
2011 (const nvlist_t
* const *)l2cache
, sav
->sav_count
);
2015 * Purge vdevs that were dropped
2018 for (i
= 0; i
< oldnvdevs
; i
++) {
2023 ASSERT(vd
->vdev_isl2cache
);
2025 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
2026 pool
!= 0ULL && l2arc_vdev_present(vd
))
2027 l2arc_remove_vdev(vd
);
2028 vdev_clear_stats(vd
);
2033 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
2036 for (i
= 0; i
< sav
->sav_count
; i
++)
2037 nvlist_free(l2cache
[i
]);
2039 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
2043 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
2046 char *packed
= NULL
;
2051 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
2055 nvsize
= *(uint64_t *)db
->db_data
;
2056 dmu_buf_rele(db
, FTAG
);
2058 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
2059 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
2062 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
2063 vmem_free(packed
, nvsize
);
2069 * Concrete top-level vdevs that are not missing and are not logs. At every
2070 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
2073 spa_healthy_core_tvds(spa_t
*spa
)
2075 vdev_t
*rvd
= spa
->spa_root_vdev
;
2078 for (uint64_t i
= 0; i
< rvd
->vdev_children
; i
++) {
2079 vdev_t
*vd
= rvd
->vdev_child
[i
];
2082 if (vdev_is_concrete(vd
) && !vdev_is_dead(vd
))
2090 * Checks to see if the given vdev could not be opened, in which case we post a
2091 * sysevent to notify the autoreplace code that the device has been removed.
2094 spa_check_removed(vdev_t
*vd
)
2096 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
2097 spa_check_removed(vd
->vdev_child
[c
]);
2099 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
2100 vdev_is_concrete(vd
)) {
2101 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
2102 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
2107 spa_check_for_missing_logs(spa_t
*spa
)
2109 vdev_t
*rvd
= spa
->spa_root_vdev
;
2112 * If we're doing a normal import, then build up any additional
2113 * diagnostic information about missing log devices.
2114 * We'll pass this up to the user for further processing.
2116 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
2117 nvlist_t
**child
, *nv
;
2120 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
2122 nv
= fnvlist_alloc();
2124 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2125 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2128 * We consider a device as missing only if it failed
2129 * to open (i.e. offline or faulted is not considered
2132 if (tvd
->vdev_islog
&&
2133 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2134 child
[idx
++] = vdev_config_generate(spa
, tvd
,
2135 B_FALSE
, VDEV_CONFIG_MISSING
);
2140 fnvlist_add_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2141 (const nvlist_t
* const *)child
, idx
);
2142 fnvlist_add_nvlist(spa
->spa_load_info
,
2143 ZPOOL_CONFIG_MISSING_DEVICES
, nv
);
2145 for (uint64_t i
= 0; i
< idx
; i
++)
2146 nvlist_free(child
[i
]);
2149 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
2152 spa_load_failed(spa
, "some log devices are missing");
2153 vdev_dbgmsg_print_tree(rvd
, 2);
2154 return (SET_ERROR(ENXIO
));
2157 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2158 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2160 if (tvd
->vdev_islog
&&
2161 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2162 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2163 spa_load_note(spa
, "some log devices are "
2164 "missing, ZIL is dropped.");
2165 vdev_dbgmsg_print_tree(rvd
, 2);
2175 * Check for missing log devices
2178 spa_check_logs(spa_t
*spa
)
2180 boolean_t rv
= B_FALSE
;
2181 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2183 switch (spa
->spa_log_state
) {
2186 case SPA_LOG_MISSING
:
2187 /* need to recheck in case slog has been restored */
2188 case SPA_LOG_UNKNOWN
:
2189 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2190 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
2192 spa_set_log_state(spa
, SPA_LOG_MISSING
);
2199 * Passivate any log vdevs (note, does not apply to embedded log metaslabs).
2202 spa_passivate_log(spa_t
*spa
)
2204 vdev_t
*rvd
= spa
->spa_root_vdev
;
2205 boolean_t slog_found
= B_FALSE
;
2207 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2209 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2210 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2212 if (tvd
->vdev_islog
) {
2213 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2214 metaslab_group_passivate(tvd
->vdev_mg
);
2215 slog_found
= B_TRUE
;
2219 return (slog_found
);
2223 * Activate any log vdevs (note, does not apply to embedded log metaslabs).
2226 spa_activate_log(spa_t
*spa
)
2228 vdev_t
*rvd
= spa
->spa_root_vdev
;
2230 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2232 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2233 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2235 if (tvd
->vdev_islog
) {
2236 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2237 metaslab_group_activate(tvd
->vdev_mg
);
2243 spa_reset_logs(spa_t
*spa
)
2247 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
2248 NULL
, DS_FIND_CHILDREN
);
2251 * We successfully offlined the log device, sync out the
2252 * current txg so that the "stubby" block can be removed
2255 txg_wait_synced(spa
->spa_dsl_pool
, 0);
2261 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
2263 for (int i
= 0; i
< sav
->sav_count
; i
++)
2264 spa_check_removed(sav
->sav_vdevs
[i
]);
2268 spa_claim_notify(zio_t
*zio
)
2270 spa_t
*spa
= zio
->io_spa
;
2275 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2276 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
2277 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
2278 mutex_exit(&spa
->spa_props_lock
);
2281 typedef struct spa_load_error
{
2282 boolean_t sle_verify_data
;
2283 uint64_t sle_meta_count
;
2284 uint64_t sle_data_count
;
2288 spa_load_verify_done(zio_t
*zio
)
2290 blkptr_t
*bp
= zio
->io_bp
;
2291 spa_load_error_t
*sle
= zio
->io_private
;
2292 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2293 int error
= zio
->io_error
;
2294 spa_t
*spa
= zio
->io_spa
;
2296 abd_free(zio
->io_abd
);
2298 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2299 type
!= DMU_OT_INTENT_LOG
)
2300 atomic_inc_64(&sle
->sle_meta_count
);
2302 atomic_inc_64(&sle
->sle_data_count
);
2305 mutex_enter(&spa
->spa_scrub_lock
);
2306 spa
->spa_load_verify_bytes
-= BP_GET_PSIZE(bp
);
2307 cv_broadcast(&spa
->spa_scrub_io_cv
);
2308 mutex_exit(&spa
->spa_scrub_lock
);
2312 * Maximum number of inflight bytes is the log2 fraction of the arc size.
2313 * By default, we set it to 1/16th of the arc.
2315 static uint_t spa_load_verify_shift
= 4;
2316 static int spa_load_verify_metadata
= B_TRUE
;
2317 static int spa_load_verify_data
= B_TRUE
;
2320 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2321 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2324 spa_load_error_t
*sle
= rio
->io_private
;
2326 (void) zilog
, (void) dnp
;
2329 * Note: normally this routine will not be called if
2330 * spa_load_verify_metadata is not set. However, it may be useful
2331 * to manually set the flag after the traversal has begun.
2333 if (!spa_load_verify_metadata
)
2337 * Sanity check the block pointer in order to detect obvious damage
2338 * before using the contents in subsequent checks or in zio_read().
2339 * When damaged consider it to be a metadata error since we cannot
2340 * trust the BP_GET_TYPE and BP_GET_LEVEL values.
2342 if (!zfs_blkptr_verify(spa
, bp
, B_FALSE
, BLK_VERIFY_LOG
)) {
2343 atomic_inc_64(&sle
->sle_meta_count
);
2347 if (zb
->zb_level
== ZB_DNODE_LEVEL
|| BP_IS_HOLE(bp
) ||
2348 BP_IS_EMBEDDED(bp
) || BP_IS_REDACTED(bp
))
2351 if (!BP_IS_METADATA(bp
) &&
2352 (!spa_load_verify_data
|| !sle
->sle_verify_data
))
2355 uint64_t maxinflight_bytes
=
2356 arc_target_bytes() >> spa_load_verify_shift
;
2357 size_t size
= BP_GET_PSIZE(bp
);
2359 mutex_enter(&spa
->spa_scrub_lock
);
2360 while (spa
->spa_load_verify_bytes
>= maxinflight_bytes
)
2361 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2362 spa
->spa_load_verify_bytes
+= size
;
2363 mutex_exit(&spa
->spa_scrub_lock
);
2365 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2366 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2367 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2368 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2373 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2375 (void) dp
, (void) arg
;
2377 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2378 return (SET_ERROR(ENAMETOOLONG
));
2384 spa_load_verify(spa_t
*spa
)
2387 spa_load_error_t sle
= { 0 };
2388 zpool_load_policy_t policy
;
2389 boolean_t verify_ok
= B_FALSE
;
2392 zpool_get_load_policy(spa
->spa_config
, &policy
);
2394 if (policy
.zlp_rewind
& ZPOOL_NEVER_REWIND
||
2395 policy
.zlp_maxmeta
== UINT64_MAX
)
2398 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2399 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2400 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2402 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2407 * Verify data only if we are rewinding or error limit was set.
2408 * Otherwise nothing except dbgmsg care about it to waste time.
2410 sle
.sle_verify_data
= (policy
.zlp_rewind
& ZPOOL_REWIND_MASK
) ||
2411 (policy
.zlp_maxdata
< UINT64_MAX
);
2413 rio
= zio_root(spa
, NULL
, &sle
,
2414 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2416 if (spa_load_verify_metadata
) {
2417 if (spa
->spa_extreme_rewind
) {
2418 spa_load_note(spa
, "performing a complete scan of the "
2419 "pool since extreme rewind is on. This may take "
2420 "a very long time.\n (spa_load_verify_data=%u, "
2421 "spa_load_verify_metadata=%u)",
2422 spa_load_verify_data
, spa_load_verify_metadata
);
2425 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2426 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2427 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2430 (void) zio_wait(rio
);
2431 ASSERT0(spa
->spa_load_verify_bytes
);
2433 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2434 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2436 if (sle
.sle_meta_count
!= 0 || sle
.sle_data_count
!= 0) {
2437 spa_load_note(spa
, "spa_load_verify found %llu metadata errors "
2438 "and %llu data errors", (u_longlong_t
)sle
.sle_meta_count
,
2439 (u_longlong_t
)sle
.sle_data_count
);
2442 if (spa_load_verify_dryrun
||
2443 (!error
&& sle
.sle_meta_count
<= policy
.zlp_maxmeta
&&
2444 sle
.sle_data_count
<= policy
.zlp_maxdata
)) {
2448 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2449 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2451 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2452 fnvlist_add_uint64(spa
->spa_load_info
, ZPOOL_CONFIG_LOAD_TIME
,
2453 spa
->spa_load_txg_ts
);
2454 fnvlist_add_int64(spa
->spa_load_info
, ZPOOL_CONFIG_REWIND_TIME
,
2456 fnvlist_add_uint64(spa
->spa_load_info
,
2457 ZPOOL_CONFIG_LOAD_META_ERRORS
, sle
.sle_meta_count
);
2458 fnvlist_add_uint64(spa
->spa_load_info
,
2459 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
);
2461 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2464 if (spa_load_verify_dryrun
)
2468 if (error
!= ENXIO
&& error
!= EIO
)
2469 error
= SET_ERROR(EIO
);
2473 return (verify_ok
? 0 : EIO
);
2477 * Find a value in the pool props object.
2480 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2482 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2483 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2487 * Find a value in the pool directory object.
2490 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
, boolean_t log_enoent
)
2492 int error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2493 name
, sizeof (uint64_t), 1, val
);
2495 if (error
!= 0 && (error
!= ENOENT
|| log_enoent
)) {
2496 spa_load_failed(spa
, "couldn't get '%s' value in MOS directory "
2497 "[error=%d]", name
, error
);
2504 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2506 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2507 return (SET_ERROR(err
));
2511 spa_livelist_delete_check(spa_t
*spa
)
2513 return (spa
->spa_livelists_to_delete
!= 0);
2517 spa_livelist_delete_cb_check(void *arg
, zthr_t
*z
)
2521 return (spa_livelist_delete_check(spa
));
2525 delete_blkptr_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
2528 zio_free(spa
, tx
->tx_txg
, bp
);
2529 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
2530 -bp_get_dsize_sync(spa
, bp
),
2531 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
2536 dsl_get_next_livelist_obj(objset_t
*os
, uint64_t zap_obj
, uint64_t *llp
)
2541 zap_cursor_init(&zc
, os
, zap_obj
);
2542 err
= zap_cursor_retrieve(&zc
, &za
);
2543 zap_cursor_fini(&zc
);
2545 *llp
= za
.za_first_integer
;
2550 * Components of livelist deletion that must be performed in syncing
2551 * context: freeing block pointers and updating the pool-wide data
2552 * structures to indicate how much work is left to do
2554 typedef struct sublist_delete_arg
{
2559 } sublist_delete_arg_t
;
2562 sublist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2564 sublist_delete_arg_t
*sda
= arg
;
2565 spa_t
*spa
= sda
->spa
;
2566 dsl_deadlist_t
*ll
= sda
->ll
;
2567 uint64_t key
= sda
->key
;
2568 bplist_t
*to_free
= sda
->to_free
;
2570 bplist_iterate(to_free
, delete_blkptr_cb
, spa
, tx
);
2571 dsl_deadlist_remove_entry(ll
, key
, tx
);
2574 typedef struct livelist_delete_arg
{
2578 } livelist_delete_arg_t
;
2581 livelist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2583 livelist_delete_arg_t
*lda
= arg
;
2584 spa_t
*spa
= lda
->spa
;
2585 uint64_t ll_obj
= lda
->ll_obj
;
2586 uint64_t zap_obj
= lda
->zap_obj
;
2587 objset_t
*mos
= spa
->spa_meta_objset
;
2590 /* free the livelist and decrement the feature count */
2591 VERIFY0(zap_remove_int(mos
, zap_obj
, ll_obj
, tx
));
2592 dsl_deadlist_free(mos
, ll_obj
, tx
);
2593 spa_feature_decr(spa
, SPA_FEATURE_LIVELIST
, tx
);
2594 VERIFY0(zap_count(mos
, zap_obj
, &count
));
2596 /* no more livelists to delete */
2597 VERIFY0(zap_remove(mos
, DMU_POOL_DIRECTORY_OBJECT
,
2598 DMU_POOL_DELETED_CLONES
, tx
));
2599 VERIFY0(zap_destroy(mos
, zap_obj
, tx
));
2600 spa
->spa_livelists_to_delete
= 0;
2601 spa_notify_waiters(spa
);
2606 * Load in the value for the livelist to be removed and open it. Then,
2607 * load its first sublist and determine which block pointers should actually
2608 * be freed. Then, call a synctask which performs the actual frees and updates
2609 * the pool-wide livelist data.
2612 spa_livelist_delete_cb(void *arg
, zthr_t
*z
)
2615 uint64_t ll_obj
= 0, count
;
2616 objset_t
*mos
= spa
->spa_meta_objset
;
2617 uint64_t zap_obj
= spa
->spa_livelists_to_delete
;
2619 * Determine the next livelist to delete. This function should only
2620 * be called if there is at least one deleted clone.
2622 VERIFY0(dsl_get_next_livelist_obj(mos
, zap_obj
, &ll_obj
));
2623 VERIFY0(zap_count(mos
, ll_obj
, &count
));
2626 dsl_deadlist_entry_t
*dle
;
2628 ll
= kmem_zalloc(sizeof (dsl_deadlist_t
), KM_SLEEP
);
2629 dsl_deadlist_open(ll
, mos
, ll_obj
);
2630 dle
= dsl_deadlist_first(ll
);
2631 ASSERT3P(dle
, !=, NULL
);
2632 bplist_create(&to_free
);
2633 int err
= dsl_process_sub_livelist(&dle
->dle_bpobj
, &to_free
,
2636 sublist_delete_arg_t sync_arg
= {
2639 .key
= dle
->dle_mintxg
,
2642 zfs_dbgmsg("deleting sublist (id %llu) from"
2643 " livelist %llu, %lld remaining",
2644 (u_longlong_t
)dle
->dle_bpobj
.bpo_object
,
2645 (u_longlong_t
)ll_obj
, (longlong_t
)count
- 1);
2646 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
2647 sublist_delete_sync
, &sync_arg
, 0,
2648 ZFS_SPACE_CHECK_DESTROY
));
2650 VERIFY3U(err
, ==, EINTR
);
2652 bplist_clear(&to_free
);
2653 bplist_destroy(&to_free
);
2654 dsl_deadlist_close(ll
);
2655 kmem_free(ll
, sizeof (dsl_deadlist_t
));
2657 livelist_delete_arg_t sync_arg
= {
2662 zfs_dbgmsg("deletion of livelist %llu completed",
2663 (u_longlong_t
)ll_obj
);
2664 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
, livelist_delete_sync
,
2665 &sync_arg
, 0, ZFS_SPACE_CHECK_DESTROY
));
2670 spa_start_livelist_destroy_thread(spa_t
*spa
)
2672 ASSERT3P(spa
->spa_livelist_delete_zthr
, ==, NULL
);
2673 spa
->spa_livelist_delete_zthr
=
2674 zthr_create("z_livelist_destroy",
2675 spa_livelist_delete_cb_check
, spa_livelist_delete_cb
, spa
,
2679 typedef struct livelist_new_arg
{
2682 } livelist_new_arg_t
;
2685 livelist_track_new_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
2689 livelist_new_arg_t
*lna
= arg
;
2691 bplist_append(lna
->frees
, bp
);
2693 bplist_append(lna
->allocs
, bp
);
2694 zfs_livelist_condense_new_alloc
++;
2699 typedef struct livelist_condense_arg
{
2702 uint64_t first_size
;
2704 } livelist_condense_arg_t
;
2707 spa_livelist_condense_sync(void *arg
, dmu_tx_t
*tx
)
2709 livelist_condense_arg_t
*lca
= arg
;
2710 spa_t
*spa
= lca
->spa
;
2712 dsl_dataset_t
*ds
= spa
->spa_to_condense
.ds
;
2714 /* Have we been cancelled? */
2715 if (spa
->spa_to_condense
.cancelled
) {
2716 zfs_livelist_condense_sync_cancel
++;
2720 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
2721 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
2722 dsl_deadlist_t
*ll
= &ds
->ds_dir
->dd_livelist
;
2725 * It's possible that the livelist was changed while the zthr was
2726 * running. Therefore, we need to check for new blkptrs in the two
2727 * entries being condensed and continue to track them in the livelist.
2728 * Because of the way we handle remapped blkptrs (see dbuf_remap_impl),
2729 * it's possible that the newly added blkptrs are FREEs or ALLOCs so
2730 * we need to sort them into two different bplists.
2732 uint64_t first_obj
= first
->dle_bpobj
.bpo_object
;
2733 uint64_t next_obj
= next
->dle_bpobj
.bpo_object
;
2734 uint64_t cur_first_size
= first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
2735 uint64_t cur_next_size
= next
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
2737 bplist_create(&new_frees
);
2738 livelist_new_arg_t new_bps
= {
2739 .allocs
= &lca
->to_keep
,
2740 .frees
= &new_frees
,
2743 if (cur_first_size
> lca
->first_size
) {
2744 VERIFY0(livelist_bpobj_iterate_from_nofree(&first
->dle_bpobj
,
2745 livelist_track_new_cb
, &new_bps
, lca
->first_size
));
2747 if (cur_next_size
> lca
->next_size
) {
2748 VERIFY0(livelist_bpobj_iterate_from_nofree(&next
->dle_bpobj
,
2749 livelist_track_new_cb
, &new_bps
, lca
->next_size
));
2752 dsl_deadlist_clear_entry(first
, ll
, tx
);
2753 ASSERT(bpobj_is_empty(&first
->dle_bpobj
));
2754 dsl_deadlist_remove_entry(ll
, next
->dle_mintxg
, tx
);
2756 bplist_iterate(&lca
->to_keep
, dsl_deadlist_insert_alloc_cb
, ll
, tx
);
2757 bplist_iterate(&new_frees
, dsl_deadlist_insert_free_cb
, ll
, tx
);
2758 bplist_destroy(&new_frees
);
2760 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
2761 dsl_dataset_name(ds
, dsname
);
2762 zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu "
2763 "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu "
2764 "(%llu blkptrs)", (u_longlong_t
)tx
->tx_txg
, dsname
,
2765 (u_longlong_t
)ds
->ds_object
, (u_longlong_t
)first_obj
,
2766 (u_longlong_t
)cur_first_size
, (u_longlong_t
)next_obj
,
2767 (u_longlong_t
)cur_next_size
,
2768 (u_longlong_t
)first
->dle_bpobj
.bpo_object
,
2769 (u_longlong_t
)first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
);
2771 dmu_buf_rele(ds
->ds_dbuf
, spa
);
2772 spa
->spa_to_condense
.ds
= NULL
;
2773 bplist_clear(&lca
->to_keep
);
2774 bplist_destroy(&lca
->to_keep
);
2775 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
2776 spa
->spa_to_condense
.syncing
= B_FALSE
;
2780 spa_livelist_condense_cb(void *arg
, zthr_t
*t
)
2782 while (zfs_livelist_condense_zthr_pause
&&
2783 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
2787 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
2788 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
2789 uint64_t first_size
, next_size
;
2791 livelist_condense_arg_t
*lca
=
2792 kmem_alloc(sizeof (livelist_condense_arg_t
), KM_SLEEP
);
2793 bplist_create(&lca
->to_keep
);
2796 * Process the livelists (matching FREEs and ALLOCs) in open context
2797 * so we have minimal work in syncing context to condense.
2799 * We save bpobj sizes (first_size and next_size) to use later in
2800 * syncing context to determine if entries were added to these sublists
2801 * while in open context. This is possible because the clone is still
2802 * active and open for normal writes and we want to make sure the new,
2803 * unprocessed blockpointers are inserted into the livelist normally.
2805 * Note that dsl_process_sub_livelist() both stores the size number of
2806 * blockpointers and iterates over them while the bpobj's lock held, so
2807 * the sizes returned to us are consistent which what was actually
2810 int err
= dsl_process_sub_livelist(&first
->dle_bpobj
, &lca
->to_keep
, t
,
2813 err
= dsl_process_sub_livelist(&next
->dle_bpobj
, &lca
->to_keep
,
2817 while (zfs_livelist_condense_sync_pause
&&
2818 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
2821 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
2822 dmu_tx_mark_netfree(tx
);
2823 dmu_tx_hold_space(tx
, 1);
2824 err
= dmu_tx_assign(tx
, TXG_NOWAIT
| TXG_NOTHROTTLE
);
2827 * Prevent the condense zthr restarting before
2828 * the synctask completes.
2830 spa
->spa_to_condense
.syncing
= B_TRUE
;
2832 lca
->first_size
= first_size
;
2833 lca
->next_size
= next_size
;
2834 dsl_sync_task_nowait(spa_get_dsl(spa
),
2835 spa_livelist_condense_sync
, lca
, tx
);
2841 * Condensing can not continue: either it was externally stopped or
2842 * we were unable to assign to a tx because the pool has run out of
2843 * space. In the second case, we'll just end up trying to condense
2844 * again in a later txg.
2847 bplist_clear(&lca
->to_keep
);
2848 bplist_destroy(&lca
->to_keep
);
2849 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
2850 dmu_buf_rele(spa
->spa_to_condense
.ds
->ds_dbuf
, spa
);
2851 spa
->spa_to_condense
.ds
= NULL
;
2853 zfs_livelist_condense_zthr_cancel
++;
2857 * Check that there is something to condense but that a condense is not
2858 * already in progress and that condensing has not been cancelled.
2861 spa_livelist_condense_cb_check(void *arg
, zthr_t
*z
)
2865 if ((spa
->spa_to_condense
.ds
!= NULL
) &&
2866 (spa
->spa_to_condense
.syncing
== B_FALSE
) &&
2867 (spa
->spa_to_condense
.cancelled
== B_FALSE
)) {
2874 spa_start_livelist_condensing_thread(spa_t
*spa
)
2876 spa
->spa_to_condense
.ds
= NULL
;
2877 spa
->spa_to_condense
.first
= NULL
;
2878 spa
->spa_to_condense
.next
= NULL
;
2879 spa
->spa_to_condense
.syncing
= B_FALSE
;
2880 spa
->spa_to_condense
.cancelled
= B_FALSE
;
2882 ASSERT3P(spa
->spa_livelist_condense_zthr
, ==, NULL
);
2883 spa
->spa_livelist_condense_zthr
=
2884 zthr_create("z_livelist_condense",
2885 spa_livelist_condense_cb_check
,
2886 spa_livelist_condense_cb
, spa
, minclsyspri
);
2890 spa_spawn_aux_threads(spa_t
*spa
)
2892 ASSERT(spa_writeable(spa
));
2894 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2896 spa_start_indirect_condensing_thread(spa
);
2897 spa_start_livelist_destroy_thread(spa
);
2898 spa_start_livelist_condensing_thread(spa
);
2900 ASSERT3P(spa
->spa_checkpoint_discard_zthr
, ==, NULL
);
2901 spa
->spa_checkpoint_discard_zthr
=
2902 zthr_create("z_checkpoint_discard",
2903 spa_checkpoint_discard_thread_check
,
2904 spa_checkpoint_discard_thread
, spa
, minclsyspri
);
2908 * Fix up config after a partly-completed split. This is done with the
2909 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2910 * pool have that entry in their config, but only the splitting one contains
2911 * a list of all the guids of the vdevs that are being split off.
2913 * This function determines what to do with that list: either rejoin
2914 * all the disks to the pool, or complete the splitting process. To attempt
2915 * the rejoin, each disk that is offlined is marked online again, and
2916 * we do a reopen() call. If the vdev label for every disk that was
2917 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2918 * then we call vdev_split() on each disk, and complete the split.
2920 * Otherwise we leave the config alone, with all the vdevs in place in
2921 * the original pool.
2924 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2931 boolean_t attempt_reopen
;
2933 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2936 /* check that the config is complete */
2937 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2938 &glist
, &gcount
) != 0)
2941 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2943 /* attempt to online all the vdevs & validate */
2944 attempt_reopen
= B_TRUE
;
2945 for (i
= 0; i
< gcount
; i
++) {
2946 if (glist
[i
] == 0) /* vdev is hole */
2949 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2950 if (vd
[i
] == NULL
) {
2952 * Don't bother attempting to reopen the disks;
2953 * just do the split.
2955 attempt_reopen
= B_FALSE
;
2957 /* attempt to re-online it */
2958 vd
[i
]->vdev_offline
= B_FALSE
;
2962 if (attempt_reopen
) {
2963 vdev_reopen(spa
->spa_root_vdev
);
2965 /* check each device to see what state it's in */
2966 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2967 if (vd
[i
] != NULL
&&
2968 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2975 * If every disk has been moved to the new pool, or if we never
2976 * even attempted to look at them, then we split them off for
2979 if (!attempt_reopen
|| gcount
== extracted
) {
2980 for (i
= 0; i
< gcount
; i
++)
2983 vdev_reopen(spa
->spa_root_vdev
);
2986 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2990 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
)
2992 const char *ereport
= FM_EREPORT_ZFS_POOL
;
2995 spa
->spa_load_state
= state
;
2996 (void) spa_import_progress_set_state(spa_guid(spa
),
2997 spa_load_state(spa
));
2999 gethrestime(&spa
->spa_loaded_ts
);
3000 error
= spa_load_impl(spa
, type
, &ereport
);
3003 * Don't count references from objsets that are already closed
3004 * and are making their way through the eviction process.
3006 spa_evicting_os_wait(spa
);
3007 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
3009 if (error
!= EEXIST
) {
3010 spa
->spa_loaded_ts
.tv_sec
= 0;
3011 spa
->spa_loaded_ts
.tv_nsec
= 0;
3013 if (error
!= EBADF
) {
3014 (void) zfs_ereport_post(ereport
, spa
,
3015 NULL
, NULL
, NULL
, 0);
3018 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
3021 (void) spa_import_progress_set_state(spa_guid(spa
),
3022 spa_load_state(spa
));
3029 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
3030 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
3031 * spa's per-vdev ZAP list.
3034 vdev_count_verify_zaps(vdev_t
*vd
)
3036 spa_t
*spa
= vd
->vdev_spa
;
3039 if (vd
->vdev_top_zap
!= 0) {
3041 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3042 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
3044 if (vd
->vdev_leaf_zap
!= 0) {
3046 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3047 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
3050 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
3051 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
3057 #define vdev_count_verify_zaps(vd) ((void) sizeof (vd), 0)
3061 * Determine whether the activity check is required.
3064 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
3068 uint64_t hostid
= 0;
3069 uint64_t tryconfig_txg
= 0;
3070 uint64_t tryconfig_timestamp
= 0;
3071 uint16_t tryconfig_mmp_seq
= 0;
3074 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3075 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3076 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
3078 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
3079 &tryconfig_timestamp
);
3080 (void) nvlist_lookup_uint16(nvinfo
, ZPOOL_CONFIG_MMP_SEQ
,
3081 &tryconfig_mmp_seq
);
3084 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
3087 * Disable the MMP activity check - This is used by zdb which
3088 * is intended to be used on potentially active pools.
3090 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
3094 * Skip the activity check when the MMP feature is disabled.
3096 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
3100 * If the tryconfig_ values are nonzero, they are the results of an
3101 * earlier tryimport. If they all match the uberblock we just found,
3102 * then the pool has not changed and we return false so we do not test
3105 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
3106 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
&&
3107 tryconfig_mmp_seq
&& tryconfig_mmp_seq
==
3108 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0))
3112 * Allow the activity check to be skipped when importing the pool
3113 * on the same host which last imported it. Since the hostid from
3114 * configuration may be stale use the one read from the label.
3116 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
3117 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
3119 if (hostid
== spa_get_hostid(spa
))
3123 * Skip the activity test when the pool was cleanly exported.
3125 if (state
!= POOL_STATE_ACTIVE
)
3132 * Nanoseconds the activity check must watch for changes on-disk.
3135 spa_activity_check_duration(spa_t
*spa
, uberblock_t
*ub
)
3137 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
3138 uint64_t multihost_interval
= MSEC2NSEC(
3139 MMP_INTERVAL_OK(zfs_multihost_interval
));
3140 uint64_t import_delay
= MAX(NANOSEC
, import_intervals
*
3141 multihost_interval
);
3144 * Local tunables determine a minimum duration except for the case
3145 * where we know when the remote host will suspend the pool if MMP
3146 * writes do not land.
3148 * See Big Theory comment at the top of mmp.c for the reasoning behind
3149 * these cases and times.
3152 ASSERT(MMP_IMPORT_SAFETY_FACTOR
>= 100);
3154 if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3155 MMP_FAIL_INT(ub
) > 0) {
3157 /* MMP on remote host will suspend pool after failed writes */
3158 import_delay
= MMP_FAIL_INT(ub
) * MSEC2NSEC(MMP_INTERVAL(ub
)) *
3159 MMP_IMPORT_SAFETY_FACTOR
/ 100;
3161 zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp "
3162 "mmp_fails=%llu ub_mmp mmp_interval=%llu "
3163 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3164 (u_longlong_t
)MMP_FAIL_INT(ub
),
3165 (u_longlong_t
)MMP_INTERVAL(ub
),
3166 (u_longlong_t
)import_intervals
);
3168 } else if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3169 MMP_FAIL_INT(ub
) == 0) {
3171 /* MMP on remote host will never suspend pool */
3172 import_delay
= MAX(import_delay
, (MSEC2NSEC(MMP_INTERVAL(ub
)) +
3173 ub
->ub_mmp_delay
) * import_intervals
);
3175 zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp "
3176 "mmp_interval=%llu ub_mmp_delay=%llu "
3177 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3178 (u_longlong_t
)MMP_INTERVAL(ub
),
3179 (u_longlong_t
)ub
->ub_mmp_delay
,
3180 (u_longlong_t
)import_intervals
);
3182 } else if (MMP_VALID(ub
)) {
3184 * zfs-0.7 compatibility case
3187 import_delay
= MAX(import_delay
, (multihost_interval
+
3188 ub
->ub_mmp_delay
) * import_intervals
);
3190 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu "
3191 "import_intervals=%llu leaves=%u",
3192 (u_longlong_t
)import_delay
,
3193 (u_longlong_t
)ub
->ub_mmp_delay
,
3194 (u_longlong_t
)import_intervals
,
3195 vdev_count_leaves(spa
));
3197 /* Using local tunings is the only reasonable option */
3198 zfs_dbgmsg("pool last imported on non-MMP aware "
3199 "host using import_delay=%llu multihost_interval=%llu "
3200 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3201 (u_longlong_t
)multihost_interval
,
3202 (u_longlong_t
)import_intervals
);
3205 return (import_delay
);
3209 * Perform the import activity check. If the user canceled the import or
3210 * we detected activity then fail.
3213 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
3215 uint64_t txg
= ub
->ub_txg
;
3216 uint64_t timestamp
= ub
->ub_timestamp
;
3217 uint64_t mmp_config
= ub
->ub_mmp_config
;
3218 uint16_t mmp_seq
= MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0;
3219 uint64_t import_delay
;
3220 hrtime_t import_expire
;
3221 nvlist_t
*mmp_label
= NULL
;
3222 vdev_t
*rvd
= spa
->spa_root_vdev
;
3227 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
3228 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3232 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
3233 * during the earlier tryimport. If the txg recorded there is 0 then
3234 * the pool is known to be active on another host.
3236 * Otherwise, the pool might be in use on another host. Check for
3237 * changes in the uberblocks on disk if necessary.
3239 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3240 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
3241 ZPOOL_CONFIG_LOAD_INFO
);
3243 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
3244 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
3245 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3246 error
= SET_ERROR(EREMOTEIO
);
3251 import_delay
= spa_activity_check_duration(spa
, ub
);
3253 /* Add a small random factor in case of simultaneous imports (0-25%) */
3254 import_delay
+= import_delay
* random_in_range(250) / 1000;
3256 import_expire
= gethrtime() + import_delay
;
3258 while (gethrtime() < import_expire
) {
3259 (void) spa_import_progress_set_mmp_check(spa_guid(spa
),
3260 NSEC2SEC(import_expire
- gethrtime()));
3262 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3264 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
||
3265 mmp_seq
!= (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0)) {
3266 zfs_dbgmsg("multihost activity detected "
3267 "txg %llu ub_txg %llu "
3268 "timestamp %llu ub_timestamp %llu "
3269 "mmp_config %#llx ub_mmp_config %#llx",
3270 (u_longlong_t
)txg
, (u_longlong_t
)ub
->ub_txg
,
3271 (u_longlong_t
)timestamp
,
3272 (u_longlong_t
)ub
->ub_timestamp
,
3273 (u_longlong_t
)mmp_config
,
3274 (u_longlong_t
)ub
->ub_mmp_config
);
3276 error
= SET_ERROR(EREMOTEIO
);
3281 nvlist_free(mmp_label
);
3285 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
3287 error
= SET_ERROR(EINTR
);
3295 mutex_destroy(&mtx
);
3299 * If the pool is determined to be active store the status in the
3300 * spa->spa_load_info nvlist. If the remote hostname or hostid are
3301 * available from configuration read from disk store them as well.
3302 * This allows 'zpool import' to generate a more useful message.
3304 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
3305 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
3306 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
3308 if (error
== EREMOTEIO
) {
3309 const char *hostname
= "<unknown>";
3310 uint64_t hostid
= 0;
3313 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
3314 hostname
= fnvlist_lookup_string(mmp_label
,
3315 ZPOOL_CONFIG_HOSTNAME
);
3316 fnvlist_add_string(spa
->spa_load_info
,
3317 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
3320 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
3321 hostid
= fnvlist_lookup_uint64(mmp_label
,
3322 ZPOOL_CONFIG_HOSTID
);
3323 fnvlist_add_uint64(spa
->spa_load_info
,
3324 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
3328 fnvlist_add_uint64(spa
->spa_load_info
,
3329 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
3330 fnvlist_add_uint64(spa
->spa_load_info
,
3331 ZPOOL_CONFIG_MMP_TXG
, 0);
3333 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
3337 nvlist_free(mmp_label
);
3343 spa_verify_host(spa_t
*spa
, nvlist_t
*mos_config
)
3347 uint64_t myhostid
= 0;
3349 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
3350 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
3351 hostname
= fnvlist_lookup_string(mos_config
,
3352 ZPOOL_CONFIG_HOSTNAME
);
3354 myhostid
= zone_get_hostid(NULL
);
3356 if (hostid
!= 0 && myhostid
!= 0 && hostid
!= myhostid
) {
3357 cmn_err(CE_WARN
, "pool '%s' could not be "
3358 "loaded as it was last accessed by "
3359 "another system (host: %s hostid: 0x%llx). "
3360 "See: https://openzfs.github.io/openzfs-docs/msg/"
3362 spa_name(spa
), hostname
, (u_longlong_t
)hostid
);
3363 spa_load_failed(spa
, "hostid verification failed: pool "
3364 "last accessed by host: %s (hostid: 0x%llx)",
3365 hostname
, (u_longlong_t
)hostid
);
3366 return (SET_ERROR(EBADF
));
3374 spa_ld_parse_config(spa_t
*spa
, spa_import_type_t type
)
3377 nvlist_t
*nvtree
, *nvl
, *config
= spa
->spa_config
;
3382 char *compatibility
;
3385 * Versioning wasn't explicitly added to the label until later, so if
3386 * it's not present treat it as the initial version.
3388 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
3389 &spa
->spa_ubsync
.ub_version
) != 0)
3390 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
3392 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
)) {
3393 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3394 ZPOOL_CONFIG_POOL_GUID
);
3395 return (SET_ERROR(EINVAL
));
3399 * If we are doing an import, ensure that the pool is not already
3400 * imported by checking if its pool guid already exists in the
3403 * The only case that we allow an already imported pool to be
3404 * imported again, is when the pool is checkpointed and we want to
3405 * look at its checkpointed state from userland tools like zdb.
3408 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3409 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3410 spa_guid_exists(pool_guid
, 0)) {
3412 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3413 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3414 spa_guid_exists(pool_guid
, 0) &&
3415 !spa_importing_readonly_checkpoint(spa
)) {
3417 spa_load_failed(spa
, "a pool with guid %llu is already open",
3418 (u_longlong_t
)pool_guid
);
3419 return (SET_ERROR(EEXIST
));
3422 spa
->spa_config_guid
= pool_guid
;
3424 nvlist_free(spa
->spa_load_info
);
3425 spa
->spa_load_info
= fnvlist_alloc();
3427 ASSERT(spa
->spa_comment
== NULL
);
3428 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
3429 spa
->spa_comment
= spa_strdup(comment
);
3431 ASSERT(spa
->spa_compatibility
== NULL
);
3432 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMPATIBILITY
,
3433 &compatibility
) == 0)
3434 spa
->spa_compatibility
= spa_strdup(compatibility
);
3436 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
3437 &spa
->spa_config_txg
);
3439 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) == 0)
3440 spa
->spa_config_splitting
= fnvlist_dup(nvl
);
3442 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvtree
)) {
3443 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3444 ZPOOL_CONFIG_VDEV_TREE
);
3445 return (SET_ERROR(EINVAL
));
3449 * Create "The Godfather" zio to hold all async IOs
3451 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3453 for (int i
= 0; i
< max_ncpus
; i
++) {
3454 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3455 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3456 ZIO_FLAG_GODFATHER
);
3460 * Parse the configuration into a vdev tree. We explicitly set the
3461 * value that will be returned by spa_version() since parsing the
3462 * configuration requires knowing the version number.
3464 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3465 parse
= (type
== SPA_IMPORT_EXISTING
?
3466 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
3467 error
= spa_config_parse(spa
, &rvd
, nvtree
, NULL
, 0, parse
);
3468 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3471 spa_load_failed(spa
, "unable to parse config [error=%d]",
3476 ASSERT(spa
->spa_root_vdev
== rvd
);
3477 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
3478 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
3480 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3481 ASSERT(spa_guid(spa
) == pool_guid
);
3488 * Recursively open all vdevs in the vdev tree. This function is called twice:
3489 * first with the untrusted config, then with the trusted config.
3492 spa_ld_open_vdevs(spa_t
*spa
)
3497 * spa_missing_tvds_allowed defines how many top-level vdevs can be
3498 * missing/unopenable for the root vdev to be still considered openable.
3500 if (spa
->spa_trust_config
) {
3501 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds
;
3502 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_CACHEFILE
) {
3503 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_cachefile
;
3504 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_SCAN
) {
3505 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_scan
;
3507 spa
->spa_missing_tvds_allowed
= 0;
3510 spa
->spa_missing_tvds_allowed
=
3511 MAX(zfs_max_missing_tvds
, spa
->spa_missing_tvds_allowed
);
3513 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3514 error
= vdev_open(spa
->spa_root_vdev
);
3515 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3517 if (spa
->spa_missing_tvds
!= 0) {
3518 spa_load_note(spa
, "vdev tree has %lld missing top-level "
3519 "vdevs.", (u_longlong_t
)spa
->spa_missing_tvds
);
3520 if (spa
->spa_trust_config
&& (spa
->spa_mode
& SPA_MODE_WRITE
)) {
3522 * Although theoretically we could allow users to open
3523 * incomplete pools in RW mode, we'd need to add a lot
3524 * of extra logic (e.g. adjust pool space to account
3525 * for missing vdevs).
3526 * This limitation also prevents users from accidentally
3527 * opening the pool in RW mode during data recovery and
3528 * damaging it further.
3530 spa_load_note(spa
, "pools with missing top-level "
3531 "vdevs can only be opened in read-only mode.");
3532 error
= SET_ERROR(ENXIO
);
3534 spa_load_note(spa
, "current settings allow for maximum "
3535 "%lld missing top-level vdevs at this stage.",
3536 (u_longlong_t
)spa
->spa_missing_tvds_allowed
);
3540 spa_load_failed(spa
, "unable to open vdev tree [error=%d]",
3543 if (spa
->spa_missing_tvds
!= 0 || error
!= 0)
3544 vdev_dbgmsg_print_tree(spa
->spa_root_vdev
, 2);
3550 * We need to validate the vdev labels against the configuration that
3551 * we have in hand. This function is called twice: first with an untrusted
3552 * config, then with a trusted config. The validation is more strict when the
3553 * config is trusted.
3556 spa_ld_validate_vdevs(spa_t
*spa
)
3559 vdev_t
*rvd
= spa
->spa_root_vdev
;
3561 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3562 error
= vdev_validate(rvd
);
3563 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3566 spa_load_failed(spa
, "vdev_validate failed [error=%d]", error
);
3570 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
) {
3571 spa_load_failed(spa
, "cannot open vdev tree after invalidating "
3573 vdev_dbgmsg_print_tree(rvd
, 2);
3574 return (SET_ERROR(ENXIO
));
3581 spa_ld_select_uberblock_done(spa_t
*spa
, uberblock_t
*ub
)
3583 spa
->spa_state
= POOL_STATE_ACTIVE
;
3584 spa
->spa_ubsync
= spa
->spa_uberblock
;
3585 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
3586 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
3587 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
3588 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
3589 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
3590 spa
->spa_prev_software_version
= ub
->ub_software_version
;
3594 spa_ld_select_uberblock(spa_t
*spa
, spa_import_type_t type
)
3596 vdev_t
*rvd
= spa
->spa_root_vdev
;
3598 uberblock_t
*ub
= &spa
->spa_uberblock
;
3599 boolean_t activity_check
= B_FALSE
;
3602 * If we are opening the checkpointed state of the pool by
3603 * rewinding to it, at this point we will have written the
3604 * checkpointed uberblock to the vdev labels, so searching
3605 * the labels will find the right uberblock. However, if
3606 * we are opening the checkpointed state read-only, we have
3607 * not modified the labels. Therefore, we must ignore the
3608 * labels and continue using the spa_uberblock that was set
3609 * by spa_ld_checkpoint_rewind.
3611 * Note that it would be fine to ignore the labels when
3612 * rewinding (opening writeable) as well. However, if we
3613 * crash just after writing the labels, we will end up
3614 * searching the labels. Doing so in the common case means
3615 * that this code path gets exercised normally, rather than
3616 * just in the edge case.
3618 if (ub
->ub_checkpoint_txg
!= 0 &&
3619 spa_importing_readonly_checkpoint(spa
)) {
3620 spa_ld_select_uberblock_done(spa
, ub
);
3625 * Find the best uberblock.
3627 vdev_uberblock_load(rvd
, ub
, &label
);
3630 * If we weren't able to find a single valid uberblock, return failure.
3632 if (ub
->ub_txg
== 0) {
3634 spa_load_failed(spa
, "no valid uberblock found");
3635 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
3638 if (spa
->spa_load_max_txg
!= UINT64_MAX
) {
3639 (void) spa_import_progress_set_max_txg(spa_guid(spa
),
3640 (u_longlong_t
)spa
->spa_load_max_txg
);
3642 spa_load_note(spa
, "using uberblock with txg=%llu",
3643 (u_longlong_t
)ub
->ub_txg
);
3647 * For pools which have the multihost property on determine if the
3648 * pool is truly inactive and can be safely imported. Prevent
3649 * hosts which don't have a hostid set from importing the pool.
3651 activity_check
= spa_activity_check_required(spa
, ub
, label
,
3653 if (activity_check
) {
3654 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
3655 spa_get_hostid(spa
) == 0) {
3657 fnvlist_add_uint64(spa
->spa_load_info
,
3658 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3659 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3662 int error
= spa_activity_check(spa
, ub
, spa
->spa_config
);
3668 fnvlist_add_uint64(spa
->spa_load_info
,
3669 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
3670 fnvlist_add_uint64(spa
->spa_load_info
,
3671 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
3672 fnvlist_add_uint16(spa
->spa_load_info
,
3673 ZPOOL_CONFIG_MMP_SEQ
,
3674 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0));
3678 * If the pool has an unsupported version we can't open it.
3680 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
3682 spa_load_failed(spa
, "version %llu is not supported",
3683 (u_longlong_t
)ub
->ub_version
);
3684 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
3687 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
3691 * If we weren't able to find what's necessary for reading the
3692 * MOS in the label, return failure.
3694 if (label
== NULL
) {
3695 spa_load_failed(spa
, "label config unavailable");
3696 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3700 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
3703 spa_load_failed(spa
, "invalid label: '%s' missing",
3704 ZPOOL_CONFIG_FEATURES_FOR_READ
);
3705 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3710 * Update our in-core representation with the definitive values
3713 nvlist_free(spa
->spa_label_features
);
3714 spa
->spa_label_features
= fnvlist_dup(features
);
3720 * Look through entries in the label nvlist's features_for_read. If
3721 * there is a feature listed there which we don't understand then we
3722 * cannot open a pool.
3724 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
3725 nvlist_t
*unsup_feat
;
3727 unsup_feat
= fnvlist_alloc();
3729 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
3731 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
3732 if (!zfeature_is_supported(nvpair_name(nvp
))) {
3733 fnvlist_add_string(unsup_feat
,
3734 nvpair_name(nvp
), "");
3738 if (!nvlist_empty(unsup_feat
)) {
3739 fnvlist_add_nvlist(spa
->spa_load_info
,
3740 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
3741 nvlist_free(unsup_feat
);
3742 spa_load_failed(spa
, "some features are unsupported");
3743 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3747 nvlist_free(unsup_feat
);
3750 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
3751 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3752 spa_try_repair(spa
, spa
->spa_config
);
3753 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3754 nvlist_free(spa
->spa_config_splitting
);
3755 spa
->spa_config_splitting
= NULL
;
3759 * Initialize internal SPA structures.
3761 spa_ld_select_uberblock_done(spa
, ub
);
3767 spa_ld_open_rootbp(spa_t
*spa
)
3770 vdev_t
*rvd
= spa
->spa_root_vdev
;
3772 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
3774 spa_load_failed(spa
, "unable to open rootbp in dsl_pool_init "
3775 "[error=%d]", error
);
3776 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3778 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
3784 spa_ld_trusted_config(spa_t
*spa
, spa_import_type_t type
,
3785 boolean_t reloading
)
3787 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
3788 nvlist_t
*nv
, *mos_config
, *policy
;
3789 int error
= 0, copy_error
;
3790 uint64_t healthy_tvds
, healthy_tvds_mos
;
3791 uint64_t mos_config_txg
;
3793 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
, B_TRUE
)
3795 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3798 * If we're assembling a pool from a split, the config provided is
3799 * already trusted so there is nothing to do.
3801 if (type
== SPA_IMPORT_ASSEMBLE
)
3804 healthy_tvds
= spa_healthy_core_tvds(spa
);
3806 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
)
3808 spa_load_failed(spa
, "unable to retrieve MOS config");
3809 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3813 * If we are doing an open, pool owner wasn't verified yet, thus do
3814 * the verification here.
3816 if (spa
->spa_load_state
== SPA_LOAD_OPEN
) {
3817 error
= spa_verify_host(spa
, mos_config
);
3819 nvlist_free(mos_config
);
3824 nv
= fnvlist_lookup_nvlist(mos_config
, ZPOOL_CONFIG_VDEV_TREE
);
3826 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3829 * Build a new vdev tree from the trusted config
3831 error
= spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
);
3833 nvlist_free(mos_config
);
3834 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3835 spa_load_failed(spa
, "spa_config_parse failed [error=%d]",
3837 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3841 * Vdev paths in the MOS may be obsolete. If the untrusted config was
3842 * obtained by scanning /dev/dsk, then it will have the right vdev
3843 * paths. We update the trusted MOS config with this information.
3844 * We first try to copy the paths with vdev_copy_path_strict, which
3845 * succeeds only when both configs have exactly the same vdev tree.
3846 * If that fails, we fall back to a more flexible method that has a
3847 * best effort policy.
3849 copy_error
= vdev_copy_path_strict(rvd
, mrvd
);
3850 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3851 spa_load_note(spa
, "provided vdev tree:");
3852 vdev_dbgmsg_print_tree(rvd
, 2);
3853 spa_load_note(spa
, "MOS vdev tree:");
3854 vdev_dbgmsg_print_tree(mrvd
, 2);
3856 if (copy_error
!= 0) {
3857 spa_load_note(spa
, "vdev_copy_path_strict failed, falling "
3858 "back to vdev_copy_path_relaxed");
3859 vdev_copy_path_relaxed(rvd
, mrvd
);
3864 spa
->spa_root_vdev
= mrvd
;
3866 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3869 * We will use spa_config if we decide to reload the spa or if spa_load
3870 * fails and we rewind. We must thus regenerate the config using the
3871 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
3872 * pass settings on how to load the pool and is not stored in the MOS.
3873 * We copy it over to our new, trusted config.
3875 mos_config_txg
= fnvlist_lookup_uint64(mos_config
,
3876 ZPOOL_CONFIG_POOL_TXG
);
3877 nvlist_free(mos_config
);
3878 mos_config
= spa_config_generate(spa
, NULL
, mos_config_txg
, B_FALSE
);
3879 if (nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_LOAD_POLICY
,
3881 fnvlist_add_nvlist(mos_config
, ZPOOL_LOAD_POLICY
, policy
);
3882 spa_config_set(spa
, mos_config
);
3883 spa
->spa_config_source
= SPA_CONFIG_SRC_MOS
;
3886 * Now that we got the config from the MOS, we should be more strict
3887 * in checking blkptrs and can make assumptions about the consistency
3888 * of the vdev tree. spa_trust_config must be set to true before opening
3889 * vdevs in order for them to be writeable.
3891 spa
->spa_trust_config
= B_TRUE
;
3894 * Open and validate the new vdev tree
3896 error
= spa_ld_open_vdevs(spa
);
3900 error
= spa_ld_validate_vdevs(spa
);
3904 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3905 spa_load_note(spa
, "final vdev tree:");
3906 vdev_dbgmsg_print_tree(rvd
, 2);
3909 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
&&
3910 !spa
->spa_extreme_rewind
&& zfs_max_missing_tvds
== 0) {
3912 * Sanity check to make sure that we are indeed loading the
3913 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
3914 * in the config provided and they happened to be the only ones
3915 * to have the latest uberblock, we could involuntarily perform
3916 * an extreme rewind.
3918 healthy_tvds_mos
= spa_healthy_core_tvds(spa
);
3919 if (healthy_tvds_mos
- healthy_tvds
>=
3920 SPA_SYNC_MIN_VDEVS
) {
3921 spa_load_note(spa
, "config provided misses too many "
3922 "top-level vdevs compared to MOS (%lld vs %lld). ",
3923 (u_longlong_t
)healthy_tvds
,
3924 (u_longlong_t
)healthy_tvds_mos
);
3925 spa_load_note(spa
, "vdev tree:");
3926 vdev_dbgmsg_print_tree(rvd
, 2);
3928 spa_load_failed(spa
, "config was already "
3929 "provided from MOS. Aborting.");
3930 return (spa_vdev_err(rvd
,
3931 VDEV_AUX_CORRUPT_DATA
, EIO
));
3933 spa_load_note(spa
, "spa must be reloaded using MOS "
3935 return (SET_ERROR(EAGAIN
));
3939 error
= spa_check_for_missing_logs(spa
);
3941 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
3943 if (rvd
->vdev_guid_sum
!= spa
->spa_uberblock
.ub_guid_sum
) {
3944 spa_load_failed(spa
, "uberblock guid sum doesn't match MOS "
3945 "guid sum (%llu != %llu)",
3946 (u_longlong_t
)spa
->spa_uberblock
.ub_guid_sum
,
3947 (u_longlong_t
)rvd
->vdev_guid_sum
);
3948 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
3956 spa_ld_open_indirect_vdev_metadata(spa_t
*spa
)
3959 vdev_t
*rvd
= spa
->spa_root_vdev
;
3962 * Everything that we read before spa_remove_init() must be stored
3963 * on concreted vdevs. Therefore we do this as early as possible.
3965 error
= spa_remove_init(spa
);
3967 spa_load_failed(spa
, "spa_remove_init failed [error=%d]",
3969 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3973 * Retrieve information needed to condense indirect vdev mappings.
3975 error
= spa_condense_init(spa
);
3977 spa_load_failed(spa
, "spa_condense_init failed [error=%d]",
3979 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3986 spa_ld_check_features(spa_t
*spa
, boolean_t
*missing_feat_writep
)
3989 vdev_t
*rvd
= spa
->spa_root_vdev
;
3991 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
3992 boolean_t missing_feat_read
= B_FALSE
;
3993 nvlist_t
*unsup_feat
, *enabled_feat
;
3995 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
3996 &spa
->spa_feat_for_read_obj
, B_TRUE
) != 0) {
3997 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4000 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
4001 &spa
->spa_feat_for_write_obj
, B_TRUE
) != 0) {
4002 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4005 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
4006 &spa
->spa_feat_desc_obj
, B_TRUE
) != 0) {
4007 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4010 enabled_feat
= fnvlist_alloc();
4011 unsup_feat
= fnvlist_alloc();
4013 if (!spa_features_check(spa
, B_FALSE
,
4014 unsup_feat
, enabled_feat
))
4015 missing_feat_read
= B_TRUE
;
4017 if (spa_writeable(spa
) ||
4018 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) {
4019 if (!spa_features_check(spa
, B_TRUE
,
4020 unsup_feat
, enabled_feat
)) {
4021 *missing_feat_writep
= B_TRUE
;
4025 fnvlist_add_nvlist(spa
->spa_load_info
,
4026 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
4028 if (!nvlist_empty(unsup_feat
)) {
4029 fnvlist_add_nvlist(spa
->spa_load_info
,
4030 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
4033 fnvlist_free(enabled_feat
);
4034 fnvlist_free(unsup_feat
);
4036 if (!missing_feat_read
) {
4037 fnvlist_add_boolean(spa
->spa_load_info
,
4038 ZPOOL_CONFIG_CAN_RDONLY
);
4042 * If the state is SPA_LOAD_TRYIMPORT, our objective is
4043 * twofold: to determine whether the pool is available for
4044 * import in read-write mode and (if it is not) whether the
4045 * pool is available for import in read-only mode. If the pool
4046 * is available for import in read-write mode, it is displayed
4047 * as available in userland; if it is not available for import
4048 * in read-only mode, it is displayed as unavailable in
4049 * userland. If the pool is available for import in read-only
4050 * mode but not read-write mode, it is displayed as unavailable
4051 * in userland with a special note that the pool is actually
4052 * available for open in read-only mode.
4054 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
4055 * missing a feature for write, we must first determine whether
4056 * the pool can be opened read-only before returning to
4057 * userland in order to know whether to display the
4058 * abovementioned note.
4060 if (missing_feat_read
|| (*missing_feat_writep
&&
4061 spa_writeable(spa
))) {
4062 spa_load_failed(spa
, "pool uses unsupported features");
4063 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
4068 * Load refcounts for ZFS features from disk into an in-memory
4069 * cache during SPA initialization.
4071 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
4074 error
= feature_get_refcount_from_disk(spa
,
4075 &spa_feature_table
[i
], &refcount
);
4077 spa
->spa_feat_refcount_cache
[i
] = refcount
;
4078 } else if (error
== ENOTSUP
) {
4079 spa
->spa_feat_refcount_cache
[i
] =
4080 SPA_FEATURE_DISABLED
;
4082 spa_load_failed(spa
, "error getting refcount "
4083 "for feature %s [error=%d]",
4084 spa_feature_table
[i
].fi_guid
, error
);
4085 return (spa_vdev_err(rvd
,
4086 VDEV_AUX_CORRUPT_DATA
, EIO
));
4091 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
4092 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
4093 &spa
->spa_feat_enabled_txg_obj
, B_TRUE
) != 0)
4094 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4098 * Encryption was added before bookmark_v2, even though bookmark_v2
4099 * is now a dependency. If this pool has encryption enabled without
4100 * bookmark_v2, trigger an errata message.
4102 if (spa_feature_is_enabled(spa
, SPA_FEATURE_ENCRYPTION
) &&
4103 !spa_feature_is_enabled(spa
, SPA_FEATURE_BOOKMARK_V2
)) {
4104 spa
->spa_errata
= ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
;
4111 spa_ld_load_special_directories(spa_t
*spa
)
4114 vdev_t
*rvd
= spa
->spa_root_vdev
;
4116 spa
->spa_is_initializing
= B_TRUE
;
4117 error
= dsl_pool_open(spa
->spa_dsl_pool
);
4118 spa
->spa_is_initializing
= B_FALSE
;
4120 spa_load_failed(spa
, "dsl_pool_open failed [error=%d]", error
);
4121 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4128 spa_ld_get_props(spa_t
*spa
)
4132 vdev_t
*rvd
= spa
->spa_root_vdev
;
4134 /* Grab the checksum salt from the MOS. */
4135 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4136 DMU_POOL_CHECKSUM_SALT
, 1,
4137 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
4138 spa
->spa_cksum_salt
.zcs_bytes
);
4139 if (error
== ENOENT
) {
4140 /* Generate a new salt for subsequent use */
4141 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
4142 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
4143 } else if (error
!= 0) {
4144 spa_load_failed(spa
, "unable to retrieve checksum salt from "
4145 "MOS [error=%d]", error
);
4146 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4149 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
, B_TRUE
) != 0)
4150 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4151 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
4153 spa_load_failed(spa
, "error opening deferred-frees bpobj "
4154 "[error=%d]", error
);
4155 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4159 * Load the bit that tells us to use the new accounting function
4160 * (raid-z deflation). If we have an older pool, this will not
4163 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
, B_FALSE
);
4164 if (error
!= 0 && error
!= ENOENT
)
4165 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4167 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
4168 &spa
->spa_creation_version
, B_FALSE
);
4169 if (error
!= 0 && error
!= ENOENT
)
4170 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4173 * Load the persistent error log. If we have an older pool, this will
4176 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
,
4178 if (error
!= 0 && error
!= ENOENT
)
4179 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4181 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
4182 &spa
->spa_errlog_scrub
, B_FALSE
);
4183 if (error
!= 0 && error
!= ENOENT
)
4184 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4187 * Load the livelist deletion field. If a livelist is queued for
4188 * deletion, indicate that in the spa
4190 error
= spa_dir_prop(spa
, DMU_POOL_DELETED_CLONES
,
4191 &spa
->spa_livelists_to_delete
, B_FALSE
);
4192 if (error
!= 0 && error
!= ENOENT
)
4193 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4196 * Load the history object. If we have an older pool, this
4197 * will not be present.
4199 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
, B_FALSE
);
4200 if (error
!= 0 && error
!= ENOENT
)
4201 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4204 * Load the per-vdev ZAP map. If we have an older pool, this will not
4205 * be present; in this case, defer its creation to a later time to
4206 * avoid dirtying the MOS this early / out of sync context. See
4207 * spa_sync_config_object.
4210 /* The sentinel is only available in the MOS config. */
4211 nvlist_t
*mos_config
;
4212 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0) {
4213 spa_load_failed(spa
, "unable to retrieve MOS config");
4214 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4217 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
4218 &spa
->spa_all_vdev_zaps
, B_FALSE
);
4220 if (error
== ENOENT
) {
4221 VERIFY(!nvlist_exists(mos_config
,
4222 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
4223 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
4224 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4225 } else if (error
!= 0) {
4226 nvlist_free(mos_config
);
4227 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4228 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
4230 * An older version of ZFS overwrote the sentinel value, so
4231 * we have orphaned per-vdev ZAPs in the MOS. Defer their
4232 * destruction to later; see spa_sync_config_object.
4234 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
4236 * We're assuming that no vdevs have had their ZAPs created
4237 * before this. Better be sure of it.
4239 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4241 nvlist_free(mos_config
);
4243 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
4245 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
,
4247 if (error
&& error
!= ENOENT
)
4248 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4251 uint64_t autoreplace
= 0;
4253 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
4254 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
4255 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
4256 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
4257 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
4258 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
4259 spa_prop_find(spa
, ZPOOL_PROP_AUTOTRIM
, &spa
->spa_autotrim
);
4260 spa
->spa_autoreplace
= (autoreplace
!= 0);
4264 * If we are importing a pool with missing top-level vdevs,
4265 * we enforce that the pool doesn't panic or get suspended on
4266 * error since the likelihood of missing data is extremely high.
4268 if (spa
->spa_missing_tvds
> 0 &&
4269 spa
->spa_failmode
!= ZIO_FAILURE_MODE_CONTINUE
&&
4270 spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4271 spa_load_note(spa
, "forcing failmode to 'continue' "
4272 "as some top level vdevs are missing");
4273 spa
->spa_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
4280 spa_ld_open_aux_vdevs(spa_t
*spa
, spa_import_type_t type
)
4283 vdev_t
*rvd
= spa
->spa_root_vdev
;
4286 * If we're assembling the pool from the split-off vdevs of
4287 * an existing pool, we don't want to attach the spares & cache
4292 * Load any hot spares for this pool.
4294 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
,
4296 if (error
!= 0 && error
!= ENOENT
)
4297 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4298 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4299 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
4300 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
4301 &spa
->spa_spares
.sav_config
) != 0) {
4302 spa_load_failed(spa
, "error loading spares nvlist");
4303 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4306 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4307 spa_load_spares(spa
);
4308 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4309 } else if (error
== 0) {
4310 spa
->spa_spares
.sav_sync
= B_TRUE
;
4314 * Load any level 2 ARC devices for this pool.
4316 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
4317 &spa
->spa_l2cache
.sav_object
, B_FALSE
);
4318 if (error
!= 0 && error
!= ENOENT
)
4319 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4320 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4321 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
4322 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
4323 &spa
->spa_l2cache
.sav_config
) != 0) {
4324 spa_load_failed(spa
, "error loading l2cache nvlist");
4325 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4328 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4329 spa_load_l2cache(spa
);
4330 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4331 } else if (error
== 0) {
4332 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4339 spa_ld_load_vdev_metadata(spa_t
*spa
)
4342 vdev_t
*rvd
= spa
->spa_root_vdev
;
4345 * If the 'multihost' property is set, then never allow a pool to
4346 * be imported when the system hostid is zero. The exception to
4347 * this rule is zdb which is always allowed to access pools.
4349 if (spa_multihost(spa
) && spa_get_hostid(spa
) == 0 &&
4350 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
4351 fnvlist_add_uint64(spa
->spa_load_info
,
4352 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
4353 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
4357 * If the 'autoreplace' property is set, then post a resource notifying
4358 * the ZFS DE that it should not issue any faults for unopenable
4359 * devices. We also iterate over the vdevs, and post a sysevent for any
4360 * unopenable vdevs so that the normal autoreplace handler can take
4363 if (spa
->spa_autoreplace
&& spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4364 spa_check_removed(spa
->spa_root_vdev
);
4366 * For the import case, this is done in spa_import(), because
4367 * at this point we're using the spare definitions from
4368 * the MOS config, not necessarily from the userland config.
4370 if (spa
->spa_load_state
!= SPA_LOAD_IMPORT
) {
4371 spa_aux_check_removed(&spa
->spa_spares
);
4372 spa_aux_check_removed(&spa
->spa_l2cache
);
4377 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
4379 error
= vdev_load(rvd
);
4381 spa_load_failed(spa
, "vdev_load failed [error=%d]", error
);
4382 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4385 error
= spa_ld_log_spacemaps(spa
);
4387 spa_load_failed(spa
, "spa_ld_log_spacemaps failed [error=%d]",
4389 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4393 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
4395 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4396 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
, B_FALSE
);
4397 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4403 spa_ld_load_dedup_tables(spa_t
*spa
)
4406 vdev_t
*rvd
= spa
->spa_root_vdev
;
4408 error
= ddt_load(spa
);
4410 spa_load_failed(spa
, "ddt_load failed [error=%d]", error
);
4411 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4418 spa_ld_verify_logs(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
4420 vdev_t
*rvd
= spa
->spa_root_vdev
;
4422 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
)) {
4423 boolean_t missing
= spa_check_logs(spa
);
4425 if (spa
->spa_missing_tvds
!= 0) {
4426 spa_load_note(spa
, "spa_check_logs failed "
4427 "so dropping the logs");
4429 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
4430 spa_load_failed(spa
, "spa_check_logs failed");
4431 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
,
4441 spa_ld_verify_pool_data(spa_t
*spa
)
4444 vdev_t
*rvd
= spa
->spa_root_vdev
;
4447 * We've successfully opened the pool, verify that we're ready
4448 * to start pushing transactions.
4450 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4451 error
= spa_load_verify(spa
);
4453 spa_load_failed(spa
, "spa_load_verify failed "
4454 "[error=%d]", error
);
4455 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
4464 spa_ld_claim_log_blocks(spa_t
*spa
)
4467 dsl_pool_t
*dp
= spa_get_dsl(spa
);
4470 * Claim log blocks that haven't been committed yet.
4471 * This must all happen in a single txg.
4472 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
4473 * invoked from zil_claim_log_block()'s i/o done callback.
4474 * Price of rollback is that we abandon the log.
4476 spa
->spa_claiming
= B_TRUE
;
4478 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
4479 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
4480 zil_claim
, tx
, DS_FIND_CHILDREN
);
4483 spa
->spa_claiming
= B_FALSE
;
4485 spa_set_log_state(spa
, SPA_LOG_GOOD
);
4489 spa_ld_check_for_config_update(spa_t
*spa
, uint64_t config_cache_txg
,
4490 boolean_t update_config_cache
)
4492 vdev_t
*rvd
= spa
->spa_root_vdev
;
4493 int need_update
= B_FALSE
;
4496 * If the config cache is stale, or we have uninitialized
4497 * metaslabs (see spa_vdev_add()), then update the config.
4499 * If this is a verbatim import, trust the current
4500 * in-core spa_config and update the disk labels.
4502 if (update_config_cache
|| config_cache_txg
!= spa
->spa_config_txg
||
4503 spa
->spa_load_state
== SPA_LOAD_IMPORT
||
4504 spa
->spa_load_state
== SPA_LOAD_RECOVER
||
4505 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
4506 need_update
= B_TRUE
;
4508 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
4509 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
4510 need_update
= B_TRUE
;
4513 * Update the config cache asynchronously in case we're the
4514 * root pool, in which case the config cache isn't writable yet.
4517 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
4521 spa_ld_prepare_for_reload(spa_t
*spa
)
4523 spa_mode_t mode
= spa
->spa_mode
;
4524 int async_suspended
= spa
->spa_async_suspended
;
4527 spa_deactivate(spa
);
4528 spa_activate(spa
, mode
);
4531 * We save the value of spa_async_suspended as it gets reset to 0 by
4532 * spa_unload(). We want to restore it back to the original value before
4533 * returning as we might be calling spa_async_resume() later.
4535 spa
->spa_async_suspended
= async_suspended
;
4539 spa_ld_read_checkpoint_txg(spa_t
*spa
)
4541 uberblock_t checkpoint
;
4544 ASSERT0(spa
->spa_checkpoint_txg
);
4545 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4547 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4548 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
4549 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
4551 if (error
== ENOENT
)
4557 ASSERT3U(checkpoint
.ub_txg
, !=, 0);
4558 ASSERT3U(checkpoint
.ub_checkpoint_txg
, !=, 0);
4559 ASSERT3U(checkpoint
.ub_timestamp
, !=, 0);
4560 spa
->spa_checkpoint_txg
= checkpoint
.ub_txg
;
4561 spa
->spa_checkpoint_info
.sci_timestamp
= checkpoint
.ub_timestamp
;
4567 spa_ld_mos_init(spa_t
*spa
, spa_import_type_t type
)
4571 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4572 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4575 * Never trust the config that is provided unless we are assembling
4576 * a pool following a split.
4577 * This means don't trust blkptrs and the vdev tree in general. This
4578 * also effectively puts the spa in read-only mode since
4579 * spa_writeable() checks for spa_trust_config to be true.
4580 * We will later load a trusted config from the MOS.
4582 if (type
!= SPA_IMPORT_ASSEMBLE
)
4583 spa
->spa_trust_config
= B_FALSE
;
4586 * Parse the config provided to create a vdev tree.
4588 error
= spa_ld_parse_config(spa
, type
);
4592 spa_import_progress_add(spa
);
4595 * Now that we have the vdev tree, try to open each vdev. This involves
4596 * opening the underlying physical device, retrieving its geometry and
4597 * probing the vdev with a dummy I/O. The state of each vdev will be set
4598 * based on the success of those operations. After this we'll be ready
4599 * to read from the vdevs.
4601 error
= spa_ld_open_vdevs(spa
);
4606 * Read the label of each vdev and make sure that the GUIDs stored
4607 * there match the GUIDs in the config provided.
4608 * If we're assembling a new pool that's been split off from an
4609 * existing pool, the labels haven't yet been updated so we skip
4610 * validation for now.
4612 if (type
!= SPA_IMPORT_ASSEMBLE
) {
4613 error
= spa_ld_validate_vdevs(spa
);
4619 * Read all vdev labels to find the best uberblock (i.e. latest,
4620 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
4621 * get the list of features required to read blkptrs in the MOS from
4622 * the vdev label with the best uberblock and verify that our version
4623 * of zfs supports them all.
4625 error
= spa_ld_select_uberblock(spa
, type
);
4630 * Pass that uberblock to the dsl_pool layer which will open the root
4631 * blkptr. This blkptr points to the latest version of the MOS and will
4632 * allow us to read its contents.
4634 error
= spa_ld_open_rootbp(spa
);
4642 spa_ld_checkpoint_rewind(spa_t
*spa
)
4644 uberblock_t checkpoint
;
4647 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4648 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4650 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4651 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
4652 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
4655 spa_load_failed(spa
, "unable to retrieve checkpointed "
4656 "uberblock from the MOS config [error=%d]", error
);
4658 if (error
== ENOENT
)
4659 error
= ZFS_ERR_NO_CHECKPOINT
;
4664 ASSERT3U(checkpoint
.ub_txg
, <, spa
->spa_uberblock
.ub_txg
);
4665 ASSERT3U(checkpoint
.ub_txg
, ==, checkpoint
.ub_checkpoint_txg
);
4668 * We need to update the txg and timestamp of the checkpointed
4669 * uberblock to be higher than the latest one. This ensures that
4670 * the checkpointed uberblock is selected if we were to close and
4671 * reopen the pool right after we've written it in the vdev labels.
4672 * (also see block comment in vdev_uberblock_compare)
4674 checkpoint
.ub_txg
= spa
->spa_uberblock
.ub_txg
+ 1;
4675 checkpoint
.ub_timestamp
= gethrestime_sec();
4678 * Set current uberblock to be the checkpointed uberblock.
4680 spa
->spa_uberblock
= checkpoint
;
4683 * If we are doing a normal rewind, then the pool is open for
4684 * writing and we sync the "updated" checkpointed uberblock to
4685 * disk. Once this is done, we've basically rewound the whole
4686 * pool and there is no way back.
4688 * There are cases when we don't want to attempt and sync the
4689 * checkpointed uberblock to disk because we are opening a
4690 * pool as read-only. Specifically, verifying the checkpointed
4691 * state with zdb, and importing the checkpointed state to get
4692 * a "preview" of its content.
4694 if (spa_writeable(spa
)) {
4695 vdev_t
*rvd
= spa
->spa_root_vdev
;
4697 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4698 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
4700 int children
= rvd
->vdev_children
;
4701 int c0
= random_in_range(children
);
4703 for (int c
= 0; c
< children
; c
++) {
4704 vdev_t
*vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
4706 /* Stop when revisiting the first vdev */
4707 if (c
> 0 && svd
[0] == vd
)
4710 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
4711 !vdev_is_concrete(vd
))
4714 svd
[svdcount
++] = vd
;
4715 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
4718 error
= vdev_config_sync(svd
, svdcount
, spa
->spa_first_txg
);
4720 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
4721 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4724 spa_load_failed(spa
, "failed to write checkpointed "
4725 "uberblock to the vdev labels [error=%d]", error
);
4734 spa_ld_mos_with_trusted_config(spa_t
*spa
, spa_import_type_t type
,
4735 boolean_t
*update_config_cache
)
4740 * Parse the config for pool, open and validate vdevs,
4741 * select an uberblock, and use that uberblock to open
4744 error
= spa_ld_mos_init(spa
, type
);
4749 * Retrieve the trusted config stored in the MOS and use it to create
4750 * a new, exact version of the vdev tree, then reopen all vdevs.
4752 error
= spa_ld_trusted_config(spa
, type
, B_FALSE
);
4753 if (error
== EAGAIN
) {
4754 if (update_config_cache
!= NULL
)
4755 *update_config_cache
= B_TRUE
;
4758 * Redo the loading process with the trusted config if it is
4759 * too different from the untrusted config.
4761 spa_ld_prepare_for_reload(spa
);
4762 spa_load_note(spa
, "RELOADING");
4763 error
= spa_ld_mos_init(spa
, type
);
4767 error
= spa_ld_trusted_config(spa
, type
, B_TRUE
);
4771 } else if (error
!= 0) {
4779 * Load an existing storage pool, using the config provided. This config
4780 * describes which vdevs are part of the pool and is later validated against
4781 * partial configs present in each vdev's label and an entire copy of the
4782 * config stored in the MOS.
4785 spa_load_impl(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
4788 boolean_t missing_feat_write
= B_FALSE
;
4789 boolean_t checkpoint_rewind
=
4790 (spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4791 boolean_t update_config_cache
= B_FALSE
;
4793 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4794 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4796 spa_load_note(spa
, "LOADING");
4798 error
= spa_ld_mos_with_trusted_config(spa
, type
, &update_config_cache
);
4803 * If we are rewinding to the checkpoint then we need to repeat
4804 * everything we've done so far in this function but this time
4805 * selecting the checkpointed uberblock and using that to open
4808 if (checkpoint_rewind
) {
4810 * If we are rewinding to the checkpoint update config cache
4813 update_config_cache
= B_TRUE
;
4816 * Extract the checkpointed uberblock from the current MOS
4817 * and use this as the pool's uberblock from now on. If the
4818 * pool is imported as writeable we also write the checkpoint
4819 * uberblock to the labels, making the rewind permanent.
4821 error
= spa_ld_checkpoint_rewind(spa
);
4826 * Redo the loading process again with the
4827 * checkpointed uberblock.
4829 spa_ld_prepare_for_reload(spa
);
4830 spa_load_note(spa
, "LOADING checkpointed uberblock");
4831 error
= spa_ld_mos_with_trusted_config(spa
, type
, NULL
);
4837 * Retrieve the checkpoint txg if the pool has a checkpoint.
4839 error
= spa_ld_read_checkpoint_txg(spa
);
4844 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
4845 * from the pool and their contents were re-mapped to other vdevs. Note
4846 * that everything that we read before this step must have been
4847 * rewritten on concrete vdevs after the last device removal was
4848 * initiated. Otherwise we could be reading from indirect vdevs before
4849 * we have loaded their mappings.
4851 error
= spa_ld_open_indirect_vdev_metadata(spa
);
4856 * Retrieve the full list of active features from the MOS and check if
4857 * they are all supported.
4859 error
= spa_ld_check_features(spa
, &missing_feat_write
);
4864 * Load several special directories from the MOS needed by the dsl_pool
4867 error
= spa_ld_load_special_directories(spa
);
4872 * Retrieve pool properties from the MOS.
4874 error
= spa_ld_get_props(spa
);
4879 * Retrieve the list of auxiliary devices - cache devices and spares -
4882 error
= spa_ld_open_aux_vdevs(spa
, type
);
4887 * Load the metadata for all vdevs. Also check if unopenable devices
4888 * should be autoreplaced.
4890 error
= spa_ld_load_vdev_metadata(spa
);
4894 error
= spa_ld_load_dedup_tables(spa
);
4899 * Verify the logs now to make sure we don't have any unexpected errors
4900 * when we claim log blocks later.
4902 error
= spa_ld_verify_logs(spa
, type
, ereport
);
4906 if (missing_feat_write
) {
4907 ASSERT(spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
);
4910 * At this point, we know that we can open the pool in
4911 * read-only mode but not read-write mode. We now have enough
4912 * information and can return to userland.
4914 return (spa_vdev_err(spa
->spa_root_vdev
, VDEV_AUX_UNSUP_FEAT
,
4919 * Traverse the last txgs to make sure the pool was left off in a safe
4920 * state. When performing an extreme rewind, we verify the whole pool,
4921 * which can take a very long time.
4923 error
= spa_ld_verify_pool_data(spa
);
4928 * Calculate the deflated space for the pool. This must be done before
4929 * we write anything to the pool because we'd need to update the space
4930 * accounting using the deflated sizes.
4932 spa_update_dspace(spa
);
4935 * We have now retrieved all the information we needed to open the
4936 * pool. If we are importing the pool in read-write mode, a few
4937 * additional steps must be performed to finish the import.
4939 if (spa_writeable(spa
) && (spa
->spa_load_state
== SPA_LOAD_RECOVER
||
4940 spa
->spa_load_max_txg
== UINT64_MAX
)) {
4941 uint64_t config_cache_txg
= spa
->spa_config_txg
;
4943 ASSERT(spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
);
4946 * In case of a checkpoint rewind, log the original txg
4947 * of the checkpointed uberblock.
4949 if (checkpoint_rewind
) {
4950 spa_history_log_internal(spa
, "checkpoint rewind",
4951 NULL
, "rewound state to txg=%llu",
4952 (u_longlong_t
)spa
->spa_uberblock
.ub_checkpoint_txg
);
4956 * Traverse the ZIL and claim all blocks.
4958 spa_ld_claim_log_blocks(spa
);
4961 * Kick-off the syncing thread.
4963 spa
->spa_sync_on
= B_TRUE
;
4964 txg_sync_start(spa
->spa_dsl_pool
);
4965 mmp_thread_start(spa
);
4968 * Wait for all claims to sync. We sync up to the highest
4969 * claimed log block birth time so that claimed log blocks
4970 * don't appear to be from the future. spa_claim_max_txg
4971 * will have been set for us by ZIL traversal operations
4974 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
4977 * Check if we need to request an update of the config. On the
4978 * next sync, we would update the config stored in vdev labels
4979 * and the cachefile (by default /etc/zfs/zpool.cache).
4981 spa_ld_check_for_config_update(spa
, config_cache_txg
,
4982 update_config_cache
);
4985 * Check if a rebuild was in progress and if so resume it.
4986 * Then check all DTLs to see if anything needs resilvering.
4987 * The resilver will be deferred if a rebuild was started.
4989 if (vdev_rebuild_active(spa
->spa_root_vdev
)) {
4990 vdev_rebuild_restart(spa
);
4991 } else if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
4992 vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
4993 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
4997 * Log the fact that we booted up (so that we can detect if
4998 * we rebooted in the middle of an operation).
5000 spa_history_log_version(spa
, "open", NULL
);
5002 spa_restart_removal(spa
);
5003 spa_spawn_aux_threads(spa
);
5006 * Delete any inconsistent datasets.
5009 * Since we may be issuing deletes for clones here,
5010 * we make sure to do so after we've spawned all the
5011 * auxiliary threads above (from which the livelist
5012 * deletion zthr is part of).
5014 (void) dmu_objset_find(spa_name(spa
),
5015 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
5018 * Clean up any stale temporary dataset userrefs.
5020 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
5022 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5023 vdev_initialize_restart(spa
->spa_root_vdev
);
5024 vdev_trim_restart(spa
->spa_root_vdev
);
5025 vdev_autotrim_restart(spa
);
5026 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5029 spa_import_progress_remove(spa_guid(spa
));
5030 spa_async_request(spa
, SPA_ASYNC_L2CACHE_REBUILD
);
5032 spa_load_note(spa
, "LOADED");
5038 spa_load_retry(spa_t
*spa
, spa_load_state_t state
)
5040 spa_mode_t mode
= spa
->spa_mode
;
5043 spa_deactivate(spa
);
5045 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
5047 spa_activate(spa
, mode
);
5048 spa_async_suspend(spa
);
5050 spa_load_note(spa
, "spa_load_retry: rewind, max txg: %llu",
5051 (u_longlong_t
)spa
->spa_load_max_txg
);
5053 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
));
5057 * If spa_load() fails this function will try loading prior txg's. If
5058 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
5059 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
5060 * function will not rewind the pool and will return the same error as
5064 spa_load_best(spa_t
*spa
, spa_load_state_t state
, uint64_t max_request
,
5067 nvlist_t
*loadinfo
= NULL
;
5068 nvlist_t
*config
= NULL
;
5069 int load_error
, rewind_error
;
5070 uint64_t safe_rewind_txg
;
5073 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
5074 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
5075 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5077 spa
->spa_load_max_txg
= max_request
;
5078 if (max_request
!= UINT64_MAX
)
5079 spa
->spa_extreme_rewind
= B_TRUE
;
5082 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
);
5083 if (load_error
== 0)
5085 if (load_error
== ZFS_ERR_NO_CHECKPOINT
) {
5087 * When attempting checkpoint-rewind on a pool with no
5088 * checkpoint, we should not attempt to load uberblocks
5089 * from previous txgs when spa_load fails.
5091 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
5092 spa_import_progress_remove(spa_guid(spa
));
5093 return (load_error
);
5096 if (spa
->spa_root_vdev
!= NULL
)
5097 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5099 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
5100 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
5102 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
5103 nvlist_free(config
);
5104 spa_import_progress_remove(spa_guid(spa
));
5105 return (load_error
);
5108 if (state
== SPA_LOAD_RECOVER
) {
5109 /* Price of rolling back is discarding txgs, including log */
5110 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5113 * If we aren't rolling back save the load info from our first
5114 * import attempt so that we can restore it after attempting
5117 loadinfo
= spa
->spa_load_info
;
5118 spa
->spa_load_info
= fnvlist_alloc();
5121 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
5122 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
5123 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
5124 TXG_INITIAL
: safe_rewind_txg
;
5127 * Continue as long as we're finding errors, we're still within
5128 * the acceptable rewind range, and we're still finding uberblocks
5130 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
5131 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
5132 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
5133 spa
->spa_extreme_rewind
= B_TRUE
;
5134 rewind_error
= spa_load_retry(spa
, state
);
5137 spa
->spa_extreme_rewind
= B_FALSE
;
5138 spa
->spa_load_max_txg
= UINT64_MAX
;
5140 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
5141 spa_config_set(spa
, config
);
5143 nvlist_free(config
);
5145 if (state
== SPA_LOAD_RECOVER
) {
5146 ASSERT3P(loadinfo
, ==, NULL
);
5147 spa_import_progress_remove(spa_guid(spa
));
5148 return (rewind_error
);
5150 /* Store the rewind info as part of the initial load info */
5151 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
5152 spa
->spa_load_info
);
5154 /* Restore the initial load info */
5155 fnvlist_free(spa
->spa_load_info
);
5156 spa
->spa_load_info
= loadinfo
;
5158 spa_import_progress_remove(spa_guid(spa
));
5159 return (load_error
);
5166 * The import case is identical to an open except that the configuration is sent
5167 * down from userland, instead of grabbed from the configuration cache. For the
5168 * case of an open, the pool configuration will exist in the
5169 * POOL_STATE_UNINITIALIZED state.
5171 * The stats information (gen/count/ustats) is used to gather vdev statistics at
5172 * the same time open the pool, without having to keep around the spa_t in some
5176 spa_open_common(const char *pool
, spa_t
**spapp
, const void *tag
,
5177 nvlist_t
*nvpolicy
, nvlist_t
**config
)
5180 spa_load_state_t state
= SPA_LOAD_OPEN
;
5182 int locked
= B_FALSE
;
5183 int firstopen
= B_FALSE
;
5188 * As disgusting as this is, we need to support recursive calls to this
5189 * function because dsl_dir_open() is called during spa_load(), and ends
5190 * up calling spa_open() again. The real fix is to figure out how to
5191 * avoid dsl_dir_open() calling this in the first place.
5193 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
5194 mutex_enter(&spa_namespace_lock
);
5198 if ((spa
= spa_lookup(pool
)) == NULL
) {
5200 mutex_exit(&spa_namespace_lock
);
5201 return (SET_ERROR(ENOENT
));
5204 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
5205 zpool_load_policy_t policy
;
5209 zpool_get_load_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
5211 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
5212 state
= SPA_LOAD_RECOVER
;
5214 spa_activate(spa
, spa_mode_global
);
5216 if (state
!= SPA_LOAD_RECOVER
)
5217 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
5218 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
5220 zfs_dbgmsg("spa_open_common: opening %s", pool
);
5221 error
= spa_load_best(spa
, state
, policy
.zlp_txg
,
5224 if (error
== EBADF
) {
5226 * If vdev_validate() returns failure (indicated by
5227 * EBADF), it indicates that one of the vdevs indicates
5228 * that the pool has been exported or destroyed. If
5229 * this is the case, the config cache is out of sync and
5230 * we should remove the pool from the namespace.
5233 spa_deactivate(spa
);
5234 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
5237 mutex_exit(&spa_namespace_lock
);
5238 return (SET_ERROR(ENOENT
));
5243 * We can't open the pool, but we still have useful
5244 * information: the state of each vdev after the
5245 * attempted vdev_open(). Return this to the user.
5247 if (config
!= NULL
&& spa
->spa_config
) {
5248 *config
= fnvlist_dup(spa
->spa_config
);
5249 fnvlist_add_nvlist(*config
,
5250 ZPOOL_CONFIG_LOAD_INFO
,
5251 spa
->spa_load_info
);
5254 spa_deactivate(spa
);
5255 spa
->spa_last_open_failed
= error
;
5257 mutex_exit(&spa_namespace_lock
);
5263 spa_open_ref(spa
, tag
);
5266 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5269 * If we've recovered the pool, pass back any information we
5270 * gathered while doing the load.
5272 if (state
== SPA_LOAD_RECOVER
&& config
!= NULL
) {
5273 fnvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
5274 spa
->spa_load_info
);
5278 spa
->spa_last_open_failed
= 0;
5279 spa
->spa_last_ubsync_txg
= 0;
5280 spa
->spa_load_txg
= 0;
5281 mutex_exit(&spa_namespace_lock
);
5285 zvol_create_minors_recursive(spa_name(spa
));
5293 spa_open_rewind(const char *name
, spa_t
**spapp
, const void *tag
,
5294 nvlist_t
*policy
, nvlist_t
**config
)
5296 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
5300 spa_open(const char *name
, spa_t
**spapp
, const void *tag
)
5302 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
5306 * Lookup the given spa_t, incrementing the inject count in the process,
5307 * preventing it from being exported or destroyed.
5310 spa_inject_addref(char *name
)
5314 mutex_enter(&spa_namespace_lock
);
5315 if ((spa
= spa_lookup(name
)) == NULL
) {
5316 mutex_exit(&spa_namespace_lock
);
5319 spa
->spa_inject_ref
++;
5320 mutex_exit(&spa_namespace_lock
);
5326 spa_inject_delref(spa_t
*spa
)
5328 mutex_enter(&spa_namespace_lock
);
5329 spa
->spa_inject_ref
--;
5330 mutex_exit(&spa_namespace_lock
);
5334 * Add spares device information to the nvlist.
5337 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
5347 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5349 if (spa
->spa_spares
.sav_count
== 0)
5352 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
5353 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5354 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
5356 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5357 (const nvlist_t
* const *)spares
, nspares
);
5358 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5359 &spares
, &nspares
));
5362 * Go through and find any spares which have since been
5363 * repurposed as an active spare. If this is the case, update
5364 * their status appropriately.
5366 for (i
= 0; i
< nspares
; i
++) {
5367 guid
= fnvlist_lookup_uint64(spares
[i
],
5369 if (spa_spare_exists(guid
, &pool
, NULL
) &&
5371 VERIFY0(nvlist_lookup_uint64_array(spares
[i
],
5372 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
,
5374 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
5375 vs
->vs_aux
= VDEV_AUX_SPARED
;
5382 * Add l2cache device information to the nvlist, including vdev stats.
5385 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
5388 uint_t i
, j
, nl2cache
;
5395 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5397 if (spa
->spa_l2cache
.sav_count
== 0)
5400 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
5401 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5402 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
5403 if (nl2cache
!= 0) {
5404 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5405 (const nvlist_t
* const *)l2cache
, nl2cache
);
5406 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5407 &l2cache
, &nl2cache
));
5410 * Update level 2 cache device stats.
5413 for (i
= 0; i
< nl2cache
; i
++) {
5414 guid
= fnvlist_lookup_uint64(l2cache
[i
],
5418 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
5420 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
5421 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
5427 VERIFY0(nvlist_lookup_uint64_array(l2cache
[i
],
5428 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
));
5429 vdev_get_stats(vd
, vs
);
5430 vdev_config_generate_stats(vd
, l2cache
[i
]);
5437 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
5442 if (spa
->spa_feat_for_read_obj
!= 0) {
5443 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
5444 spa
->spa_feat_for_read_obj
);
5445 zap_cursor_retrieve(&zc
, &za
) == 0;
5446 zap_cursor_advance(&zc
)) {
5447 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
5448 za
.za_num_integers
== 1);
5449 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
5450 za
.za_first_integer
));
5452 zap_cursor_fini(&zc
);
5455 if (spa
->spa_feat_for_write_obj
!= 0) {
5456 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
5457 spa
->spa_feat_for_write_obj
);
5458 zap_cursor_retrieve(&zc
, &za
) == 0;
5459 zap_cursor_advance(&zc
)) {
5460 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
5461 za
.za_num_integers
== 1);
5462 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
5463 za
.za_first_integer
));
5465 zap_cursor_fini(&zc
);
5470 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
5474 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5475 zfeature_info_t feature
= spa_feature_table
[i
];
5478 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
5481 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
5486 * Store a list of pool features and their reference counts in the
5489 * The first time this is called on a spa, allocate a new nvlist, fetch
5490 * the pool features and reference counts from disk, then save the list
5491 * in the spa. In subsequent calls on the same spa use the saved nvlist
5492 * and refresh its values from the cached reference counts. This
5493 * ensures we don't block here on I/O on a suspended pool so 'zpool
5494 * clear' can resume the pool.
5497 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
5501 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5503 mutex_enter(&spa
->spa_feat_stats_lock
);
5504 features
= spa
->spa_feat_stats
;
5506 if (features
!= NULL
) {
5507 spa_feature_stats_from_cache(spa
, features
);
5509 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
5510 spa
->spa_feat_stats
= features
;
5511 spa_feature_stats_from_disk(spa
, features
);
5514 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
5517 mutex_exit(&spa
->spa_feat_stats_lock
);
5521 spa_get_stats(const char *name
, nvlist_t
**config
,
5522 char *altroot
, size_t buflen
)
5528 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
5532 * This still leaves a window of inconsistency where the spares
5533 * or l2cache devices could change and the config would be
5534 * self-inconsistent.
5536 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5538 if (*config
!= NULL
) {
5539 uint64_t loadtimes
[2];
5541 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
5542 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
5543 fnvlist_add_uint64_array(*config
,
5544 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2);
5546 fnvlist_add_uint64(*config
,
5547 ZPOOL_CONFIG_ERRCOUNT
,
5548 spa_approx_errlog_size(spa
));
5550 if (spa_suspended(spa
)) {
5551 fnvlist_add_uint64(*config
,
5552 ZPOOL_CONFIG_SUSPENDED
,
5554 fnvlist_add_uint64(*config
,
5555 ZPOOL_CONFIG_SUSPENDED_REASON
,
5556 spa
->spa_suspended
);
5559 spa_add_spares(spa
, *config
);
5560 spa_add_l2cache(spa
, *config
);
5561 spa_add_feature_stats(spa
, *config
);
5566 * We want to get the alternate root even for faulted pools, so we cheat
5567 * and call spa_lookup() directly.
5571 mutex_enter(&spa_namespace_lock
);
5572 spa
= spa_lookup(name
);
5574 spa_altroot(spa
, altroot
, buflen
);
5578 mutex_exit(&spa_namespace_lock
);
5580 spa_altroot(spa
, altroot
, buflen
);
5585 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5586 spa_close(spa
, FTAG
);
5593 * Validate that the auxiliary device array is well formed. We must have an
5594 * array of nvlists, each which describes a valid leaf vdev. If this is an
5595 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
5596 * specified, as long as they are well-formed.
5599 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
5600 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
5601 vdev_labeltype_t label
)
5608 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5611 * It's acceptable to have no devs specified.
5613 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
5617 return (SET_ERROR(EINVAL
));
5620 * Make sure the pool is formatted with a version that supports this
5623 if (spa_version(spa
) < version
)
5624 return (SET_ERROR(ENOTSUP
));
5627 * Set the pending device list so we correctly handle device in-use
5630 sav
->sav_pending
= dev
;
5631 sav
->sav_npending
= ndev
;
5633 for (i
= 0; i
< ndev
; i
++) {
5634 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
5638 if (!vd
->vdev_ops
->vdev_op_leaf
) {
5640 error
= SET_ERROR(EINVAL
);
5646 if ((error
= vdev_open(vd
)) == 0 &&
5647 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
5648 fnvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
5655 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
5662 sav
->sav_pending
= NULL
;
5663 sav
->sav_npending
= 0;
5668 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
5672 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5674 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
5675 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
5676 VDEV_LABEL_SPARE
)) != 0) {
5680 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
5681 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
5682 VDEV_LABEL_L2CACHE
));
5686 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
5691 if (sav
->sav_config
!= NULL
) {
5697 * Generate new dev list by concatenating with the
5700 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
5701 &olddevs
, &oldndevs
));
5703 newdevs
= kmem_alloc(sizeof (void *) *
5704 (ndevs
+ oldndevs
), KM_SLEEP
);
5705 for (i
= 0; i
< oldndevs
; i
++)
5706 newdevs
[i
] = fnvlist_dup(olddevs
[i
]);
5707 for (i
= 0; i
< ndevs
; i
++)
5708 newdevs
[i
+ oldndevs
] = fnvlist_dup(devs
[i
]);
5710 fnvlist_remove(sav
->sav_config
, config
);
5712 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
5713 (const nvlist_t
* const *)newdevs
, ndevs
+ oldndevs
);
5714 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
5715 nvlist_free(newdevs
[i
]);
5716 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
5719 * Generate a new dev list.
5721 sav
->sav_config
= fnvlist_alloc();
5722 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
5723 (const nvlist_t
* const *)devs
, ndevs
);
5728 * Stop and drop level 2 ARC devices
5731 spa_l2cache_drop(spa_t
*spa
)
5735 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5737 for (i
= 0; i
< sav
->sav_count
; i
++) {
5740 vd
= sav
->sav_vdevs
[i
];
5743 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
5744 pool
!= 0ULL && l2arc_vdev_present(vd
))
5745 l2arc_remove_vdev(vd
);
5750 * Verify encryption parameters for spa creation. If we are encrypting, we must
5751 * have the encryption feature flag enabled.
5754 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
5755 boolean_t has_encryption
)
5757 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
5758 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
5760 return (SET_ERROR(ENOTSUP
));
5762 return (dmu_objset_create_crypt_check(NULL
, dcp
, NULL
));
5769 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
5770 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
5773 char *altroot
= NULL
;
5778 uint64_t txg
= TXG_INITIAL
;
5779 nvlist_t
**spares
, **l2cache
;
5780 uint_t nspares
, nl2cache
;
5781 uint64_t version
, obj
, ndraid
= 0;
5782 boolean_t has_features
;
5783 boolean_t has_encryption
;
5784 boolean_t has_allocclass
;
5790 if (props
== NULL
||
5791 nvlist_lookup_string(props
, "tname", &poolname
) != 0)
5792 poolname
= (char *)pool
;
5795 * If this pool already exists, return failure.
5797 mutex_enter(&spa_namespace_lock
);
5798 if (spa_lookup(poolname
) != NULL
) {
5799 mutex_exit(&spa_namespace_lock
);
5800 return (SET_ERROR(EEXIST
));
5804 * Allocate a new spa_t structure.
5806 nvl
= fnvlist_alloc();
5807 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
5808 (void) nvlist_lookup_string(props
,
5809 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5810 spa
= spa_add(poolname
, nvl
, altroot
);
5812 spa_activate(spa
, spa_mode_global
);
5814 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
5815 spa_deactivate(spa
);
5817 mutex_exit(&spa_namespace_lock
);
5822 * Temporary pool names should never be written to disk.
5824 if (poolname
!= pool
)
5825 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
5827 has_features
= B_FALSE
;
5828 has_encryption
= B_FALSE
;
5829 has_allocclass
= B_FALSE
;
5830 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
5831 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
5832 if (zpool_prop_feature(nvpair_name(elem
))) {
5833 has_features
= B_TRUE
;
5835 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
5836 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
5837 if (feat
== SPA_FEATURE_ENCRYPTION
)
5838 has_encryption
= B_TRUE
;
5839 if (feat
== SPA_FEATURE_ALLOCATION_CLASSES
)
5840 has_allocclass
= B_TRUE
;
5844 /* verify encryption params, if they were provided */
5846 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
5848 spa_deactivate(spa
);
5850 mutex_exit(&spa_namespace_lock
);
5854 if (!has_allocclass
&& zfs_special_devs(nvroot
, NULL
)) {
5855 spa_deactivate(spa
);
5857 mutex_exit(&spa_namespace_lock
);
5861 if (has_features
|| nvlist_lookup_uint64(props
,
5862 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
5863 version
= SPA_VERSION
;
5865 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
5867 spa
->spa_first_txg
= txg
;
5868 spa
->spa_uberblock
.ub_txg
= txg
- 1;
5869 spa
->spa_uberblock
.ub_version
= version
;
5870 spa
->spa_ubsync
= spa
->spa_uberblock
;
5871 spa
->spa_load_state
= SPA_LOAD_CREATE
;
5872 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
5873 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
5874 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
5875 spa
->spa_indirect_vdevs_loaded
= B_TRUE
;
5878 * Create "The Godfather" zio to hold all async IOs
5880 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
5882 for (int i
= 0; i
< max_ncpus
; i
++) {
5883 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
5884 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
5885 ZIO_FLAG_GODFATHER
);
5889 * Create the root vdev.
5891 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5893 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
5895 ASSERT(error
!= 0 || rvd
!= NULL
);
5896 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
5898 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
5899 error
= SET_ERROR(EINVAL
);
5902 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
5903 (error
= vdev_draid_spare_create(nvroot
, rvd
, &ndraid
, 0)) == 0 &&
5904 (error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) == 0) {
5906 * instantiate the metaslab groups (this will dirty the vdevs)
5907 * we can no longer error exit past this point
5909 for (int c
= 0; error
== 0 && c
< rvd
->vdev_children
; c
++) {
5910 vdev_t
*vd
= rvd
->vdev_child
[c
];
5912 vdev_metaslab_set_size(vd
);
5913 vdev_expand(vd
, txg
);
5917 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5921 spa_deactivate(spa
);
5923 mutex_exit(&spa_namespace_lock
);
5928 * Get the list of spares, if specified.
5930 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5931 &spares
, &nspares
) == 0) {
5932 spa
->spa_spares
.sav_config
= fnvlist_alloc();
5933 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
5934 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
5936 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5937 spa_load_spares(spa
);
5938 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5939 spa
->spa_spares
.sav_sync
= B_TRUE
;
5943 * Get the list of level 2 cache devices, if specified.
5945 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5946 &l2cache
, &nl2cache
) == 0) {
5947 VERIFY0(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
5948 NV_UNIQUE_NAME
, KM_SLEEP
));
5949 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
5950 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
5952 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5953 spa_load_l2cache(spa
);
5954 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5955 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5958 spa
->spa_is_initializing
= B_TRUE
;
5959 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
5960 spa
->spa_is_initializing
= B_FALSE
;
5963 * Create DDTs (dedup tables).
5967 spa_update_dspace(spa
);
5969 tx
= dmu_tx_create_assigned(dp
, txg
);
5972 * Create the pool's history object.
5974 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
5975 spa_history_create_obj(spa
, tx
);
5977 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
5978 spa_history_log_version(spa
, "create", tx
);
5981 * Create the pool config object.
5983 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
5984 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
5985 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
5987 if (zap_add(spa
->spa_meta_objset
,
5988 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
5989 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
5990 cmn_err(CE_PANIC
, "failed to add pool config");
5993 if (zap_add(spa
->spa_meta_objset
,
5994 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
5995 sizeof (uint64_t), 1, &version
, tx
) != 0) {
5996 cmn_err(CE_PANIC
, "failed to add pool version");
5999 /* Newly created pools with the right version are always deflated. */
6000 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6001 spa
->spa_deflate
= TRUE
;
6002 if (zap_add(spa
->spa_meta_objset
,
6003 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6004 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
6005 cmn_err(CE_PANIC
, "failed to add deflate");
6010 * Create the deferred-free bpobj. Turn off compression
6011 * because sync-to-convergence takes longer if the blocksize
6014 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
6015 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
6016 ZIO_COMPRESS_OFF
, tx
);
6017 if (zap_add(spa
->spa_meta_objset
,
6018 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
6019 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
6020 cmn_err(CE_PANIC
, "failed to add bpobj");
6022 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
6023 spa
->spa_meta_objset
, obj
));
6026 * Generate some random noise for salted checksums to operate on.
6028 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
6029 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
6032 * Set pool properties.
6034 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
6035 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
6036 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
6037 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
6038 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
6039 spa
->spa_autotrim
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM
);
6041 if (props
!= NULL
) {
6042 spa_configfile_set(spa
, props
, B_FALSE
);
6043 spa_sync_props(props
, tx
);
6046 for (int i
= 0; i
< ndraid
; i
++)
6047 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
6051 spa
->spa_sync_on
= B_TRUE
;
6053 mmp_thread_start(spa
);
6054 txg_wait_synced(dp
, txg
);
6056 spa_spawn_aux_threads(spa
);
6058 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
6061 * Don't count references from objsets that are already closed
6062 * and are making their way through the eviction process.
6064 spa_evicting_os_wait(spa
);
6065 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
6066 spa
->spa_load_state
= SPA_LOAD_NONE
;
6070 mutex_exit(&spa_namespace_lock
);
6076 * Import a non-root pool into the system.
6079 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
6082 char *altroot
= NULL
;
6083 spa_load_state_t state
= SPA_LOAD_IMPORT
;
6084 zpool_load_policy_t policy
;
6085 spa_mode_t mode
= spa_mode_global
;
6086 uint64_t readonly
= B_FALSE
;
6089 nvlist_t
**spares
, **l2cache
;
6090 uint_t nspares
, nl2cache
;
6093 * If a pool with this name exists, return failure.
6095 mutex_enter(&spa_namespace_lock
);
6096 if (spa_lookup(pool
) != NULL
) {
6097 mutex_exit(&spa_namespace_lock
);
6098 return (SET_ERROR(EEXIST
));
6102 * Create and initialize the spa structure.
6104 (void) nvlist_lookup_string(props
,
6105 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6106 (void) nvlist_lookup_uint64(props
,
6107 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
6109 mode
= SPA_MODE_READ
;
6110 spa
= spa_add(pool
, config
, altroot
);
6111 spa
->spa_import_flags
= flags
;
6114 * Verbatim import - Take a pool and insert it into the namespace
6115 * as if it had been loaded at boot.
6117 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
6119 spa_configfile_set(spa
, props
, B_FALSE
);
6121 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_FALSE
);
6122 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6123 zfs_dbgmsg("spa_import: verbatim import of %s", pool
);
6124 mutex_exit(&spa_namespace_lock
);
6128 spa_activate(spa
, mode
);
6131 * Don't start async tasks until we know everything is healthy.
6133 spa_async_suspend(spa
);
6135 zpool_get_load_policy(config
, &policy
);
6136 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
6137 state
= SPA_LOAD_RECOVER
;
6139 spa
->spa_config_source
= SPA_CONFIG_SRC_TRYIMPORT
;
6141 if (state
!= SPA_LOAD_RECOVER
) {
6142 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
6143 zfs_dbgmsg("spa_import: importing %s", pool
);
6145 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
6146 "(RECOVERY MODE)", pool
, (longlong_t
)policy
.zlp_txg
);
6148 error
= spa_load_best(spa
, state
, policy
.zlp_txg
, policy
.zlp_rewind
);
6151 * Propagate anything learned while loading the pool and pass it
6152 * back to caller (i.e. rewind info, missing devices, etc).
6154 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, spa
->spa_load_info
);
6156 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6158 * Toss any existing sparelist, as it doesn't have any validity
6159 * anymore, and conflicts with spa_has_spare().
6161 if (spa
->spa_spares
.sav_config
) {
6162 nvlist_free(spa
->spa_spares
.sav_config
);
6163 spa
->spa_spares
.sav_config
= NULL
;
6164 spa_load_spares(spa
);
6166 if (spa
->spa_l2cache
.sav_config
) {
6167 nvlist_free(spa
->spa_l2cache
.sav_config
);
6168 spa
->spa_l2cache
.sav_config
= NULL
;
6169 spa_load_l2cache(spa
);
6172 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6173 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6176 spa_configfile_set(spa
, props
, B_FALSE
);
6178 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
6179 (error
= spa_prop_set(spa
, props
)))) {
6181 spa_deactivate(spa
);
6183 mutex_exit(&spa_namespace_lock
);
6187 spa_async_resume(spa
);
6190 * Override any spares and level 2 cache devices as specified by
6191 * the user, as these may have correct device names/devids, etc.
6193 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6194 &spares
, &nspares
) == 0) {
6195 if (spa
->spa_spares
.sav_config
)
6196 fnvlist_remove(spa
->spa_spares
.sav_config
,
6197 ZPOOL_CONFIG_SPARES
);
6199 spa
->spa_spares
.sav_config
= fnvlist_alloc();
6200 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6201 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
6203 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6204 spa_load_spares(spa
);
6205 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6206 spa
->spa_spares
.sav_sync
= B_TRUE
;
6208 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6209 &l2cache
, &nl2cache
) == 0) {
6210 if (spa
->spa_l2cache
.sav_config
)
6211 fnvlist_remove(spa
->spa_l2cache
.sav_config
,
6212 ZPOOL_CONFIG_L2CACHE
);
6214 spa
->spa_l2cache
.sav_config
= fnvlist_alloc();
6215 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6216 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
6218 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6219 spa_load_l2cache(spa
);
6220 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6221 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6225 * Check for any removed devices.
6227 if (spa
->spa_autoreplace
) {
6228 spa_aux_check_removed(&spa
->spa_spares
);
6229 spa_aux_check_removed(&spa
->spa_l2cache
);
6232 if (spa_writeable(spa
)) {
6234 * Update the config cache to include the newly-imported pool.
6236 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6240 * It's possible that the pool was expanded while it was exported.
6241 * We kick off an async task to handle this for us.
6243 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
6245 spa_history_log_version(spa
, "import", NULL
);
6247 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6249 mutex_exit(&spa_namespace_lock
);
6251 zvol_create_minors_recursive(pool
);
6259 spa_tryimport(nvlist_t
*tryconfig
)
6261 nvlist_t
*config
= NULL
;
6262 char *poolname
, *cachefile
;
6266 zpool_load_policy_t policy
;
6268 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
6271 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
6275 * Create and initialize the spa structure.
6277 mutex_enter(&spa_namespace_lock
);
6278 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
6279 spa_activate(spa
, SPA_MODE_READ
);
6282 * Rewind pool if a max txg was provided.
6284 zpool_get_load_policy(spa
->spa_config
, &policy
);
6285 if (policy
.zlp_txg
!= UINT64_MAX
) {
6286 spa
->spa_load_max_txg
= policy
.zlp_txg
;
6287 spa
->spa_extreme_rewind
= B_TRUE
;
6288 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
6289 poolname
, (longlong_t
)policy
.zlp_txg
);
6291 zfs_dbgmsg("spa_tryimport: importing %s", poolname
);
6294 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_CACHEFILE
, &cachefile
)
6296 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile
);
6297 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
6299 spa
->spa_config_source
= SPA_CONFIG_SRC_SCAN
;
6302 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
);
6305 * If 'tryconfig' was at least parsable, return the current config.
6307 if (spa
->spa_root_vdev
!= NULL
) {
6308 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
6309 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, poolname
);
6310 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, state
);
6311 fnvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
6312 spa
->spa_uberblock
.ub_timestamp
);
6313 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
6314 spa
->spa_load_info
);
6315 fnvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
6319 * If the bootfs property exists on this pool then we
6320 * copy it out so that external consumers can tell which
6321 * pools are bootable.
6323 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
6324 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6327 * We have to play games with the name since the
6328 * pool was opened as TRYIMPORT_NAME.
6330 if (dsl_dsobj_to_dsname(spa_name(spa
),
6331 spa
->spa_bootfs
, tmpname
) == 0) {
6335 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6337 cp
= strchr(tmpname
, '/');
6339 (void) strlcpy(dsname
, tmpname
,
6342 (void) snprintf(dsname
, MAXPATHLEN
,
6343 "%s/%s", poolname
, ++cp
);
6345 fnvlist_add_string(config
, ZPOOL_CONFIG_BOOTFS
,
6347 kmem_free(dsname
, MAXPATHLEN
);
6349 kmem_free(tmpname
, MAXPATHLEN
);
6353 * Add the list of hot spares and level 2 cache devices.
6355 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6356 spa_add_spares(spa
, config
);
6357 spa_add_l2cache(spa
, config
);
6358 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6362 spa_deactivate(spa
);
6364 mutex_exit(&spa_namespace_lock
);
6370 * Pool export/destroy
6372 * The act of destroying or exporting a pool is very simple. We make sure there
6373 * is no more pending I/O and any references to the pool are gone. Then, we
6374 * update the pool state and sync all the labels to disk, removing the
6375 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
6376 * we don't sync the labels or remove the configuration cache.
6379 spa_export_common(const char *pool
, int new_state
, nvlist_t
**oldconfig
,
6380 boolean_t force
, boolean_t hardforce
)
6388 if (!(spa_mode_global
& SPA_MODE_WRITE
))
6389 return (SET_ERROR(EROFS
));
6391 mutex_enter(&spa_namespace_lock
);
6392 if ((spa
= spa_lookup(pool
)) == NULL
) {
6393 mutex_exit(&spa_namespace_lock
);
6394 return (SET_ERROR(ENOENT
));
6397 if (spa
->spa_is_exporting
) {
6398 /* the pool is being exported by another thread */
6399 mutex_exit(&spa_namespace_lock
);
6400 return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS
));
6402 spa
->spa_is_exporting
= B_TRUE
;
6405 * Put a hold on the pool, drop the namespace lock, stop async tasks,
6406 * reacquire the namespace lock, and see if we can export.
6408 spa_open_ref(spa
, FTAG
);
6409 mutex_exit(&spa_namespace_lock
);
6410 spa_async_suspend(spa
);
6411 if (spa
->spa_zvol_taskq
) {
6412 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
6413 taskq_wait(spa
->spa_zvol_taskq
);
6415 mutex_enter(&spa_namespace_lock
);
6416 spa_close(spa
, FTAG
);
6418 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
6421 * The pool will be in core if it's openable, in which case we can
6422 * modify its state. Objsets may be open only because they're dirty,
6423 * so we have to force it to sync before checking spa_refcnt.
6425 if (spa
->spa_sync_on
) {
6426 txg_wait_synced(spa
->spa_dsl_pool
, 0);
6427 spa_evicting_os_wait(spa
);
6431 * A pool cannot be exported or destroyed if there are active
6432 * references. If we are resetting a pool, allow references by
6433 * fault injection handlers.
6435 if (!spa_refcount_zero(spa
) || (spa
->spa_inject_ref
!= 0)) {
6436 error
= SET_ERROR(EBUSY
);
6440 if (spa
->spa_sync_on
) {
6441 vdev_t
*rvd
= spa
->spa_root_vdev
;
6443 * A pool cannot be exported if it has an active shared spare.
6444 * This is to prevent other pools stealing the active spare
6445 * from an exported pool. At user's own will, such pool can
6446 * be forcedly exported.
6448 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
6449 spa_has_active_shared_spare(spa
)) {
6450 error
= SET_ERROR(EXDEV
);
6455 * We're about to export or destroy this pool. Make sure
6456 * we stop all initialization and trim activity here before
6457 * we set the spa_final_txg. This will ensure that all
6458 * dirty data resulting from the initialization is
6459 * committed to disk before we unload the pool.
6461 vdev_initialize_stop_all(rvd
, VDEV_INITIALIZE_ACTIVE
);
6462 vdev_trim_stop_all(rvd
, VDEV_TRIM_ACTIVE
);
6463 vdev_autotrim_stop_all(spa
);
6464 vdev_rebuild_stop_all(spa
);
6467 * We want this to be reflected on every label,
6468 * so mark them all dirty. spa_unload() will do the
6469 * final sync that pushes these changes out.
6471 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
6472 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6473 spa
->spa_state
= new_state
;
6474 vdev_config_dirty(rvd
);
6475 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6479 * If the log space map feature is enabled and the pool is
6480 * getting exported (but not destroyed), we want to spend some
6481 * time flushing as many metaslabs as we can in an attempt to
6482 * destroy log space maps and save import time. This has to be
6483 * done before we set the spa_final_txg, otherwise
6484 * spa_sync() -> spa_flush_metaslabs() may dirty the final TXGs.
6485 * spa_should_flush_logs_on_unload() should be called after
6486 * spa_state has been set to the new_state.
6488 if (spa_should_flush_logs_on_unload(spa
))
6489 spa_unload_log_sm_flush_all(spa
);
6491 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
6492 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6493 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
6495 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6502 if (new_state
== POOL_STATE_DESTROYED
)
6503 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
6504 else if (new_state
== POOL_STATE_EXPORTED
)
6505 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
6507 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6509 spa_deactivate(spa
);
6512 if (oldconfig
&& spa
->spa_config
)
6513 *oldconfig
= fnvlist_dup(spa
->spa_config
);
6515 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
6517 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
6521 * If spa_remove() is not called for this spa_t and
6522 * there is any possibility that it can be reused,
6523 * we make sure to reset the exporting flag.
6525 spa
->spa_is_exporting
= B_FALSE
;
6528 mutex_exit(&spa_namespace_lock
);
6532 spa
->spa_is_exporting
= B_FALSE
;
6533 spa_async_resume(spa
);
6534 mutex_exit(&spa_namespace_lock
);
6539 * Destroy a storage pool.
6542 spa_destroy(const char *pool
)
6544 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
6549 * Export a storage pool.
6552 spa_export(const char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
6553 boolean_t hardforce
)
6555 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
6560 * Similar to spa_export(), this unloads the spa_t without actually removing it
6561 * from the namespace in any way.
6564 spa_reset(const char *pool
)
6566 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
6571 * ==========================================================================
6572 * Device manipulation
6573 * ==========================================================================
6577 * This is called as a synctask to increment the draid feature flag
6580 spa_draid_feature_incr(void *arg
, dmu_tx_t
*tx
)
6582 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6583 int draid
= (int)(uintptr_t)arg
;
6585 for (int c
= 0; c
< draid
; c
++)
6586 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
6590 * Add a device to a storage pool.
6593 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
6595 uint64_t txg
, ndraid
= 0;
6597 vdev_t
*rvd
= spa
->spa_root_vdev
;
6599 nvlist_t
**spares
, **l2cache
;
6600 uint_t nspares
, nl2cache
;
6602 ASSERT(spa_writeable(spa
));
6604 txg
= spa_vdev_enter(spa
);
6606 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
6607 VDEV_ALLOC_ADD
)) != 0)
6608 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6610 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
6612 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
6616 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
6620 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
6621 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6623 if (vd
->vdev_children
!= 0 &&
6624 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0) {
6625 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6629 * The virtual dRAID spares must be added after vdev tree is created
6630 * and the vdev guids are generated. The guid of their associated
6631 * dRAID is stored in the config and used when opening the spare.
6633 if ((error
= vdev_draid_spare_create(nvroot
, vd
, &ndraid
,
6634 rvd
->vdev_children
)) == 0) {
6635 if (ndraid
> 0 && nvlist_lookup_nvlist_array(nvroot
,
6636 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) != 0)
6639 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6643 * We must validate the spares and l2cache devices after checking the
6644 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
6646 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
6647 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6650 * If we are in the middle of a device removal, we can only add
6651 * devices which match the existing devices in the pool.
6652 * If we are in the middle of a removal, or have some indirect
6653 * vdevs, we can not add raidz or dRAID top levels.
6655 if (spa
->spa_vdev_removal
!= NULL
||
6656 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
6657 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6658 tvd
= vd
->vdev_child
[c
];
6659 if (spa
->spa_vdev_removal
!= NULL
&&
6660 tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
6661 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6663 /* Fail if top level vdev is raidz or a dRAID */
6664 if (vdev_get_nparity(tvd
) != 0)
6665 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6668 * Need the top level mirror to be
6669 * a mirror of leaf vdevs only
6671 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
6672 for (uint64_t cid
= 0;
6673 cid
< tvd
->vdev_children
; cid
++) {
6674 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
6675 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
6676 return (spa_vdev_exit(spa
, vd
,
6684 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6685 tvd
= vd
->vdev_child
[c
];
6686 vdev_remove_child(vd
, tvd
);
6687 tvd
->vdev_id
= rvd
->vdev_children
;
6688 vdev_add_child(rvd
, tvd
);
6689 vdev_config_dirty(tvd
);
6693 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
6694 ZPOOL_CONFIG_SPARES
);
6695 spa_load_spares(spa
);
6696 spa
->spa_spares
.sav_sync
= B_TRUE
;
6699 if (nl2cache
!= 0) {
6700 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
6701 ZPOOL_CONFIG_L2CACHE
);
6702 spa_load_l2cache(spa
);
6703 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6707 * We can't increment a feature while holding spa_vdev so we
6708 * have to do it in a synctask.
6713 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
6714 dsl_sync_task_nowait(spa
->spa_dsl_pool
, spa_draid_feature_incr
,
6715 (void *)(uintptr_t)ndraid
, tx
);
6720 * We have to be careful when adding new vdevs to an existing pool.
6721 * If other threads start allocating from these vdevs before we
6722 * sync the config cache, and we lose power, then upon reboot we may
6723 * fail to open the pool because there are DVAs that the config cache
6724 * can't translate. Therefore, we first add the vdevs without
6725 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
6726 * and then let spa_config_update() initialize the new metaslabs.
6728 * spa_load() checks for added-but-not-initialized vdevs, so that
6729 * if we lose power at any point in this sequence, the remaining
6730 * steps will be completed the next time we load the pool.
6732 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
6734 mutex_enter(&spa_namespace_lock
);
6735 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6736 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
6737 mutex_exit(&spa_namespace_lock
);
6743 * Attach a device to a mirror. The arguments are the path to any device
6744 * in the mirror, and the nvroot for the new device. If the path specifies
6745 * a device that is not mirrored, we automatically insert the mirror vdev.
6747 * If 'replacing' is specified, the new device is intended to replace the
6748 * existing device; in this case the two devices are made into their own
6749 * mirror using the 'replacing' vdev, which is functionally identical to
6750 * the mirror vdev (it actually reuses all the same ops) but has a few
6751 * extra rules: you can't attach to it after it's been created, and upon
6752 * completion of resilvering, the first disk (the one being replaced)
6753 * is automatically detached.
6755 * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild)
6756 * should be performed instead of traditional healing reconstruction. From
6757 * an administrators perspective these are both resilver operations.
6760 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
,
6763 uint64_t txg
, dtl_max_txg
;
6764 vdev_t
*rvd
= spa
->spa_root_vdev
;
6765 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
6767 char *oldvdpath
, *newvdpath
;
6771 ASSERT(spa_writeable(spa
));
6773 txg
= spa_vdev_enter(spa
);
6775 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6777 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6778 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6779 error
= (spa_has_checkpoint(spa
)) ?
6780 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6781 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6785 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_DEVICE_REBUILD
))
6786 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6788 if (dsl_scan_resilvering(spa_get_dsl(spa
)))
6789 return (spa_vdev_exit(spa
, NULL
, txg
,
6790 ZFS_ERR_RESILVER_IN_PROGRESS
));
6792 if (vdev_rebuild_active(rvd
))
6793 return (spa_vdev_exit(spa
, NULL
, txg
,
6794 ZFS_ERR_REBUILD_IN_PROGRESS
));
6797 if (spa
->spa_vdev_removal
!= NULL
)
6798 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6801 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
6803 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
6804 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6806 pvd
= oldvd
->vdev_parent
;
6808 if (spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
6809 VDEV_ALLOC_ATTACH
) != 0)
6810 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6812 if (newrootvd
->vdev_children
!= 1)
6813 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
6815 newvd
= newrootvd
->vdev_child
[0];
6817 if (!newvd
->vdev_ops
->vdev_op_leaf
)
6818 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
6820 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
6821 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
6824 * log, dedup and special vdevs should not be replaced by spares.
6826 if ((oldvd
->vdev_top
->vdev_alloc_bias
!= VDEV_BIAS_NONE
||
6827 oldvd
->vdev_top
->vdev_islog
) && newvd
->vdev_isspare
) {
6828 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6832 * A dRAID spare can only replace a child of its parent dRAID vdev.
6834 if (newvd
->vdev_ops
== &vdev_draid_spare_ops
&&
6835 oldvd
->vdev_top
!= vdev_draid_spare_get_parent(newvd
)) {
6836 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6841 * For rebuilds, the top vdev must support reconstruction
6842 * using only space maps. This means the only allowable
6843 * vdevs types are the root vdev, a mirror, or dRAID.
6846 if (pvd
->vdev_top
!= NULL
)
6847 tvd
= pvd
->vdev_top
;
6849 if (tvd
->vdev_ops
!= &vdev_mirror_ops
&&
6850 tvd
->vdev_ops
!= &vdev_root_ops
&&
6851 tvd
->vdev_ops
!= &vdev_draid_ops
) {
6852 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6858 * For attach, the only allowable parent is a mirror or the root
6861 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
6862 pvd
->vdev_ops
!= &vdev_root_ops
)
6863 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6865 pvops
= &vdev_mirror_ops
;
6868 * Active hot spares can only be replaced by inactive hot
6871 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6872 oldvd
->vdev_isspare
&&
6873 !spa_has_spare(spa
, newvd
->vdev_guid
))
6874 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6877 * If the source is a hot spare, and the parent isn't already a
6878 * spare, then we want to create a new hot spare. Otherwise, we
6879 * want to create a replacing vdev. The user is not allowed to
6880 * attach to a spared vdev child unless the 'isspare' state is
6881 * the same (spare replaces spare, non-spare replaces
6884 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
6885 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
6886 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6887 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6888 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
6889 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6892 if (newvd
->vdev_isspare
)
6893 pvops
= &vdev_spare_ops
;
6895 pvops
= &vdev_replacing_ops
;
6899 * Make sure the new device is big enough.
6901 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
6902 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
6905 * The new device cannot have a higher alignment requirement
6906 * than the top-level vdev.
6908 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
6909 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6912 * If this is an in-place replacement, update oldvd's path and devid
6913 * to make it distinguishable from newvd, and unopenable from now on.
6915 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
6916 spa_strfree(oldvd
->vdev_path
);
6917 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
6919 (void) snprintf(oldvd
->vdev_path
, strlen(newvd
->vdev_path
) + 5,
6920 "%s/%s", newvd
->vdev_path
, "old");
6921 if (oldvd
->vdev_devid
!= NULL
) {
6922 spa_strfree(oldvd
->vdev_devid
);
6923 oldvd
->vdev_devid
= NULL
;
6928 * If the parent is not a mirror, or if we're replacing, insert the new
6929 * mirror/replacing/spare vdev above oldvd.
6931 if (pvd
->vdev_ops
!= pvops
)
6932 pvd
= vdev_add_parent(oldvd
, pvops
);
6934 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
6935 ASSERT(pvd
->vdev_ops
== pvops
);
6936 ASSERT(oldvd
->vdev_parent
== pvd
);
6939 * Extract the new device from its root and add it to pvd.
6941 vdev_remove_child(newrootvd
, newvd
);
6942 newvd
->vdev_id
= pvd
->vdev_children
;
6943 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
6944 vdev_add_child(pvd
, newvd
);
6947 * Reevaluate the parent vdev state.
6949 vdev_propagate_state(pvd
);
6951 tvd
= newvd
->vdev_top
;
6952 ASSERT(pvd
->vdev_top
== tvd
);
6953 ASSERT(tvd
->vdev_parent
== rvd
);
6955 vdev_config_dirty(tvd
);
6958 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
6959 * for any dmu_sync-ed blocks. It will propagate upward when
6960 * spa_vdev_exit() calls vdev_dtl_reassess().
6962 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
6964 vdev_dtl_dirty(newvd
, DTL_MISSING
,
6965 TXG_INITIAL
, dtl_max_txg
- TXG_INITIAL
);
6967 if (newvd
->vdev_isspare
) {
6968 spa_spare_activate(newvd
);
6969 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
6972 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
6973 newvdpath
= spa_strdup(newvd
->vdev_path
);
6974 newvd_isspare
= newvd
->vdev_isspare
;
6977 * Mark newvd's DTL dirty in this txg.
6979 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
6982 * Schedule the resilver or rebuild to restart in the future. We do
6983 * this to ensure that dmu_sync-ed blocks have been stitched into the
6984 * respective datasets.
6987 newvd
->vdev_rebuild_txg
= txg
;
6991 newvd
->vdev_resilver_txg
= txg
;
6993 if (dsl_scan_resilvering(spa_get_dsl(spa
)) &&
6994 spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
)) {
6995 vdev_defer_resilver(newvd
);
6997 dsl_scan_restart_resilver(spa
->spa_dsl_pool
,
7002 if (spa
->spa_bootfs
)
7003 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
7005 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
7010 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
7012 spa_history_log_internal(spa
, "vdev attach", NULL
,
7013 "%s vdev=%s %s vdev=%s",
7014 replacing
&& newvd_isspare
? "spare in" :
7015 replacing
? "replace" : "attach", newvdpath
,
7016 replacing
? "for" : "to", oldvdpath
);
7018 spa_strfree(oldvdpath
);
7019 spa_strfree(newvdpath
);
7025 * Detach a device from a mirror or replacing vdev.
7027 * If 'replace_done' is specified, only detach if the parent
7028 * is a replacing vdev.
7031 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
7035 vdev_t
*rvd __maybe_unused
= spa
->spa_root_vdev
;
7036 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
7037 boolean_t unspare
= B_FALSE
;
7038 uint64_t unspare_guid
= 0;
7041 ASSERT(spa_writeable(spa
));
7043 txg
= spa_vdev_detach_enter(spa
, guid
);
7045 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7048 * Besides being called directly from the userland through the
7049 * ioctl interface, spa_vdev_detach() can be potentially called
7050 * at the end of spa_vdev_resilver_done().
7052 * In the regular case, when we have a checkpoint this shouldn't
7053 * happen as we never empty the DTLs of a vdev during the scrub
7054 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
7055 * should never get here when we have a checkpoint.
7057 * That said, even in a case when we checkpoint the pool exactly
7058 * as spa_vdev_resilver_done() calls this function everything
7059 * should be fine as the resilver will return right away.
7061 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7062 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7063 error
= (spa_has_checkpoint(spa
)) ?
7064 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7065 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7069 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
7071 if (!vd
->vdev_ops
->vdev_op_leaf
)
7072 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7074 pvd
= vd
->vdev_parent
;
7077 * If the parent/child relationship is not as expected, don't do it.
7078 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
7079 * vdev that's replacing B with C. The user's intent in replacing
7080 * is to go from M(A,B) to M(A,C). If the user decides to cancel
7081 * the replace by detaching C, the expected behavior is to end up
7082 * M(A,B). But suppose that right after deciding to detach C,
7083 * the replacement of B completes. We would have M(A,C), and then
7084 * ask to detach C, which would leave us with just A -- not what
7085 * the user wanted. To prevent this, we make sure that the
7086 * parent/child relationship hasn't changed -- in this example,
7087 * that C's parent is still the replacing vdev R.
7089 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
7090 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7093 * Only 'replacing' or 'spare' vdevs can be replaced.
7095 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7096 pvd
->vdev_ops
!= &vdev_spare_ops
)
7097 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7099 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
7100 spa_version(spa
) >= SPA_VERSION_SPARES
);
7103 * Only mirror, replacing, and spare vdevs support detach.
7105 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7106 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
7107 pvd
->vdev_ops
!= &vdev_spare_ops
)
7108 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7111 * If this device has the only valid copy of some data,
7112 * we cannot safely detach it.
7114 if (vdev_dtl_required(vd
))
7115 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7117 ASSERT(pvd
->vdev_children
>= 2);
7120 * If we are detaching the second disk from a replacing vdev, then
7121 * check to see if we changed the original vdev's path to have "/old"
7122 * at the end in spa_vdev_attach(). If so, undo that change now.
7124 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
7125 vd
->vdev_path
!= NULL
) {
7126 size_t len
= strlen(vd
->vdev_path
);
7128 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
7129 cvd
= pvd
->vdev_child
[c
];
7131 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
7134 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
7135 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
7136 spa_strfree(cvd
->vdev_path
);
7137 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
7144 * If we are detaching the original disk from a normal spare, then it
7145 * implies that the spare should become a real disk, and be removed
7146 * from the active spare list for the pool. dRAID spares on the
7147 * other hand are coupled to the pool and thus should never be removed
7148 * from the spares list.
7150 if (pvd
->vdev_ops
== &vdev_spare_ops
&& vd
->vdev_id
== 0) {
7151 vdev_t
*last_cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7153 if (last_cvd
->vdev_isspare
&&
7154 last_cvd
->vdev_ops
!= &vdev_draid_spare_ops
) {
7160 * Erase the disk labels so the disk can be used for other things.
7161 * This must be done after all other error cases are handled,
7162 * but before we disembowel vd (so we can still do I/O to it).
7163 * But if we can't do it, don't treat the error as fatal --
7164 * it may be that the unwritability of the disk is the reason
7165 * it's being detached!
7167 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
7170 * Remove vd from its parent and compact the parent's children.
7172 vdev_remove_child(pvd
, vd
);
7173 vdev_compact_children(pvd
);
7176 * Remember one of the remaining children so we can get tvd below.
7178 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7181 * If we need to remove the remaining child from the list of hot spares,
7182 * do it now, marking the vdev as no longer a spare in the process.
7183 * We must do this before vdev_remove_parent(), because that can
7184 * change the GUID if it creates a new toplevel GUID. For a similar
7185 * reason, we must remove the spare now, in the same txg as the detach;
7186 * otherwise someone could attach a new sibling, change the GUID, and
7187 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
7190 ASSERT(cvd
->vdev_isspare
);
7191 spa_spare_remove(cvd
);
7192 unspare_guid
= cvd
->vdev_guid
;
7193 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
7194 cvd
->vdev_unspare
= B_TRUE
;
7198 * If the parent mirror/replacing vdev only has one child,
7199 * the parent is no longer needed. Remove it from the tree.
7201 if (pvd
->vdev_children
== 1) {
7202 if (pvd
->vdev_ops
== &vdev_spare_ops
)
7203 cvd
->vdev_unspare
= B_FALSE
;
7204 vdev_remove_parent(cvd
);
7208 * We don't set tvd until now because the parent we just removed
7209 * may have been the previous top-level vdev.
7211 tvd
= cvd
->vdev_top
;
7212 ASSERT(tvd
->vdev_parent
== rvd
);
7215 * Reevaluate the parent vdev state.
7217 vdev_propagate_state(cvd
);
7220 * If the 'autoexpand' property is set on the pool then automatically
7221 * try to expand the size of the pool. For example if the device we
7222 * just detached was smaller than the others, it may be possible to
7223 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
7224 * first so that we can obtain the updated sizes of the leaf vdevs.
7226 if (spa
->spa_autoexpand
) {
7228 vdev_expand(tvd
, txg
);
7231 vdev_config_dirty(tvd
);
7234 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
7235 * vd->vdev_detached is set and free vd's DTL object in syncing context.
7236 * But first make sure we're not on any *other* txg's DTL list, to
7237 * prevent vd from being accessed after it's freed.
7239 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
7240 for (int t
= 0; t
< TXG_SIZE
; t
++)
7241 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
7242 vd
->vdev_detached
= B_TRUE
;
7243 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
7245 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
7246 spa_notify_waiters(spa
);
7248 /* hang on to the spa before we release the lock */
7249 spa_open_ref(spa
, FTAG
);
7251 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
7253 spa_history_log_internal(spa
, "detach", NULL
,
7255 spa_strfree(vdpath
);
7258 * If this was the removal of the original device in a hot spare vdev,
7259 * then we want to go through and remove the device from the hot spare
7260 * list of every other pool.
7263 spa_t
*altspa
= NULL
;
7265 mutex_enter(&spa_namespace_lock
);
7266 while ((altspa
= spa_next(altspa
)) != NULL
) {
7267 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
7271 spa_open_ref(altspa
, FTAG
);
7272 mutex_exit(&spa_namespace_lock
);
7273 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
7274 mutex_enter(&spa_namespace_lock
);
7275 spa_close(altspa
, FTAG
);
7277 mutex_exit(&spa_namespace_lock
);
7279 /* search the rest of the vdevs for spares to remove */
7280 spa_vdev_resilver_done(spa
);
7283 /* all done with the spa; OK to release */
7284 mutex_enter(&spa_namespace_lock
);
7285 spa_close(spa
, FTAG
);
7286 mutex_exit(&spa_namespace_lock
);
7292 spa_vdev_initialize_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
7295 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7297 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7299 /* Look up vdev and ensure it's a leaf. */
7300 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7301 if (vd
== NULL
|| vd
->vdev_detached
) {
7302 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7303 return (SET_ERROR(ENODEV
));
7304 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
7305 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7306 return (SET_ERROR(EINVAL
));
7307 } else if (!vdev_writeable(vd
)) {
7308 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7309 return (SET_ERROR(EROFS
));
7311 mutex_enter(&vd
->vdev_initialize_lock
);
7312 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7315 * When we activate an initialize action we check to see
7316 * if the vdev_initialize_thread is NULL. We do this instead
7317 * of using the vdev_initialize_state since there might be
7318 * a previous initialization process which has completed but
7319 * the thread is not exited.
7321 if (cmd_type
== POOL_INITIALIZE_START
&&
7322 (vd
->vdev_initialize_thread
!= NULL
||
7323 vd
->vdev_top
->vdev_removing
)) {
7324 mutex_exit(&vd
->vdev_initialize_lock
);
7325 return (SET_ERROR(EBUSY
));
7326 } else if (cmd_type
== POOL_INITIALIZE_CANCEL
&&
7327 (vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
&&
7328 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_SUSPENDED
)) {
7329 mutex_exit(&vd
->vdev_initialize_lock
);
7330 return (SET_ERROR(ESRCH
));
7331 } else if (cmd_type
== POOL_INITIALIZE_SUSPEND
&&
7332 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
) {
7333 mutex_exit(&vd
->vdev_initialize_lock
);
7334 return (SET_ERROR(ESRCH
));
7338 case POOL_INITIALIZE_START
:
7339 vdev_initialize(vd
);
7341 case POOL_INITIALIZE_CANCEL
:
7342 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
, vd_list
);
7344 case POOL_INITIALIZE_SUSPEND
:
7345 vdev_initialize_stop(vd
, VDEV_INITIALIZE_SUSPENDED
, vd_list
);
7348 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
7350 mutex_exit(&vd
->vdev_initialize_lock
);
7356 spa_vdev_initialize(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
,
7357 nvlist_t
*vdev_errlist
)
7359 int total_errors
= 0;
7362 list_create(&vd_list
, sizeof (vdev_t
),
7363 offsetof(vdev_t
, vdev_initialize_node
));
7366 * We hold the namespace lock through the whole function
7367 * to prevent any changes to the pool while we're starting or
7368 * stopping initialization. The config and state locks are held so that
7369 * we can properly assess the vdev state before we commit to
7370 * the initializing operation.
7372 mutex_enter(&spa_namespace_lock
);
7374 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
7375 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
7376 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
7378 int error
= spa_vdev_initialize_impl(spa
, vdev_guid
, cmd_type
,
7381 char guid_as_str
[MAXNAMELEN
];
7383 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
7384 "%llu", (unsigned long long)vdev_guid
);
7385 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
7390 /* Wait for all initialize threads to stop. */
7391 vdev_initialize_stop_wait(spa
, &vd_list
);
7393 /* Sync out the initializing state */
7394 txg_wait_synced(spa
->spa_dsl_pool
, 0);
7395 mutex_exit(&spa_namespace_lock
);
7397 list_destroy(&vd_list
);
7399 return (total_errors
);
7403 spa_vdev_trim_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
7404 uint64_t rate
, boolean_t partial
, boolean_t secure
, list_t
*vd_list
)
7406 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7408 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7410 /* Look up vdev and ensure it's a leaf. */
7411 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7412 if (vd
== NULL
|| vd
->vdev_detached
) {
7413 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7414 return (SET_ERROR(ENODEV
));
7415 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
7416 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7417 return (SET_ERROR(EINVAL
));
7418 } else if (!vdev_writeable(vd
)) {
7419 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7420 return (SET_ERROR(EROFS
));
7421 } else if (!vd
->vdev_has_trim
) {
7422 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7423 return (SET_ERROR(EOPNOTSUPP
));
7424 } else if (secure
&& !vd
->vdev_has_securetrim
) {
7425 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7426 return (SET_ERROR(EOPNOTSUPP
));
7428 mutex_enter(&vd
->vdev_trim_lock
);
7429 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7432 * When we activate a TRIM action we check to see if the
7433 * vdev_trim_thread is NULL. We do this instead of using the
7434 * vdev_trim_state since there might be a previous TRIM process
7435 * which has completed but the thread is not exited.
7437 if (cmd_type
== POOL_TRIM_START
&&
7438 (vd
->vdev_trim_thread
!= NULL
|| vd
->vdev_top
->vdev_removing
)) {
7439 mutex_exit(&vd
->vdev_trim_lock
);
7440 return (SET_ERROR(EBUSY
));
7441 } else if (cmd_type
== POOL_TRIM_CANCEL
&&
7442 (vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
&&
7443 vd
->vdev_trim_state
!= VDEV_TRIM_SUSPENDED
)) {
7444 mutex_exit(&vd
->vdev_trim_lock
);
7445 return (SET_ERROR(ESRCH
));
7446 } else if (cmd_type
== POOL_TRIM_SUSPEND
&&
7447 vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
) {
7448 mutex_exit(&vd
->vdev_trim_lock
);
7449 return (SET_ERROR(ESRCH
));
7453 case POOL_TRIM_START
:
7454 vdev_trim(vd
, rate
, partial
, secure
);
7456 case POOL_TRIM_CANCEL
:
7457 vdev_trim_stop(vd
, VDEV_TRIM_CANCELED
, vd_list
);
7459 case POOL_TRIM_SUSPEND
:
7460 vdev_trim_stop(vd
, VDEV_TRIM_SUSPENDED
, vd_list
);
7463 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
7465 mutex_exit(&vd
->vdev_trim_lock
);
7471 * Initiates a manual TRIM for the requested vdevs. This kicks off individual
7472 * TRIM threads for each child vdev. These threads pass over all of the free
7473 * space in the vdev's metaslabs and issues TRIM commands for that space.
7476 spa_vdev_trim(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
, uint64_t rate
,
7477 boolean_t partial
, boolean_t secure
, nvlist_t
*vdev_errlist
)
7479 int total_errors
= 0;
7482 list_create(&vd_list
, sizeof (vdev_t
),
7483 offsetof(vdev_t
, vdev_trim_node
));
7486 * We hold the namespace lock through the whole function
7487 * to prevent any changes to the pool while we're starting or
7488 * stopping TRIM. The config and state locks are held so that
7489 * we can properly assess the vdev state before we commit to
7490 * the TRIM operation.
7492 mutex_enter(&spa_namespace_lock
);
7494 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
7495 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
7496 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
7498 int error
= spa_vdev_trim_impl(spa
, vdev_guid
, cmd_type
,
7499 rate
, partial
, secure
, &vd_list
);
7501 char guid_as_str
[MAXNAMELEN
];
7503 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
7504 "%llu", (unsigned long long)vdev_guid
);
7505 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
7510 /* Wait for all TRIM threads to stop. */
7511 vdev_trim_stop_wait(spa
, &vd_list
);
7513 /* Sync out the TRIM state */
7514 txg_wait_synced(spa
->spa_dsl_pool
, 0);
7515 mutex_exit(&spa_namespace_lock
);
7517 list_destroy(&vd_list
);
7519 return (total_errors
);
7523 * Split a set of devices from their mirrors, and create a new pool from them.
7526 spa_vdev_split_mirror(spa_t
*spa
, const char *newname
, nvlist_t
*config
,
7527 nvlist_t
*props
, boolean_t exp
)
7530 uint64_t txg
, *glist
;
7532 uint_t c
, children
, lastlog
;
7533 nvlist_t
**child
, *nvl
, *tmp
;
7535 char *altroot
= NULL
;
7536 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
7537 boolean_t activate_slog
;
7539 ASSERT(spa_writeable(spa
));
7541 txg
= spa_vdev_enter(spa
);
7543 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7544 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7545 error
= (spa_has_checkpoint(spa
)) ?
7546 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7547 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7550 /* clear the log and flush everything up to now */
7551 activate_slog
= spa_passivate_log(spa
);
7552 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
7553 error
= spa_reset_logs(spa
);
7554 txg
= spa_vdev_config_enter(spa
);
7557 spa_activate_log(spa
);
7560 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7562 /* check new spa name before going any further */
7563 if (spa_lookup(newname
) != NULL
)
7564 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
7567 * scan through all the children to ensure they're all mirrors
7569 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
7570 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
7572 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7574 /* first, check to ensure we've got the right child count */
7575 rvd
= spa
->spa_root_vdev
;
7577 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
7578 vdev_t
*vd
= rvd
->vdev_child
[c
];
7580 /* don't count the holes & logs as children */
7581 if (vd
->vdev_islog
|| (vd
->vdev_ops
!= &vdev_indirect_ops
&&
7582 !vdev_is_concrete(vd
))) {
7590 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
7591 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7593 /* next, ensure no spare or cache devices are part of the split */
7594 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
7595 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
7596 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7598 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
7599 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
7601 /* then, loop over each vdev and validate it */
7602 for (c
= 0; c
< children
; c
++) {
7603 uint64_t is_hole
= 0;
7605 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
7609 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
7610 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
7613 error
= SET_ERROR(EINVAL
);
7618 /* deal with indirect vdevs */
7619 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ops
==
7623 /* which disk is going to be split? */
7624 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
7626 error
= SET_ERROR(EINVAL
);
7630 /* look it up in the spa */
7631 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
7632 if (vml
[c
] == NULL
) {
7633 error
= SET_ERROR(ENODEV
);
7637 /* make sure there's nothing stopping the split */
7638 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
7639 vml
[c
]->vdev_islog
||
7640 !vdev_is_concrete(vml
[c
]) ||
7641 vml
[c
]->vdev_isspare
||
7642 vml
[c
]->vdev_isl2cache
||
7643 !vdev_writeable(vml
[c
]) ||
7644 vml
[c
]->vdev_children
!= 0 ||
7645 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
7646 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
7647 error
= SET_ERROR(EINVAL
);
7651 if (vdev_dtl_required(vml
[c
]) ||
7652 vdev_resilver_needed(vml
[c
], NULL
, NULL
)) {
7653 error
= SET_ERROR(EBUSY
);
7657 /* we need certain info from the top level */
7658 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
7659 vml
[c
]->vdev_top
->vdev_ms_array
);
7660 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
7661 vml
[c
]->vdev_top
->vdev_ms_shift
);
7662 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
7663 vml
[c
]->vdev_top
->vdev_asize
);
7664 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
7665 vml
[c
]->vdev_top
->vdev_ashift
);
7667 /* transfer per-vdev ZAPs */
7668 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
7669 VERIFY0(nvlist_add_uint64(child
[c
],
7670 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
7672 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
7673 VERIFY0(nvlist_add_uint64(child
[c
],
7674 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
7675 vml
[c
]->vdev_parent
->vdev_top_zap
));
7679 kmem_free(vml
, children
* sizeof (vdev_t
*));
7680 kmem_free(glist
, children
* sizeof (uint64_t));
7681 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7684 /* stop writers from using the disks */
7685 for (c
= 0; c
< children
; c
++) {
7687 vml
[c
]->vdev_offline
= B_TRUE
;
7689 vdev_reopen(spa
->spa_root_vdev
);
7692 * Temporarily record the splitting vdevs in the spa config. This
7693 * will disappear once the config is regenerated.
7695 nvl
= fnvlist_alloc();
7696 fnvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
, glist
, children
);
7697 kmem_free(glist
, children
* sizeof (uint64_t));
7699 mutex_enter(&spa
->spa_props_lock
);
7700 fnvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
, nvl
);
7701 mutex_exit(&spa
->spa_props_lock
);
7702 spa
->spa_config_splitting
= nvl
;
7703 vdev_config_dirty(spa
->spa_root_vdev
);
7705 /* configure and create the new pool */
7706 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
);
7707 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
7708 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
);
7709 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
, spa_version(spa
));
7710 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, spa
->spa_config_txg
);
7711 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
7712 spa_generate_guid(NULL
));
7713 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
7714 (void) nvlist_lookup_string(props
,
7715 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
7717 /* add the new pool to the namespace */
7718 newspa
= spa_add(newname
, config
, altroot
);
7719 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
7720 newspa
->spa_config_txg
= spa
->spa_config_txg
;
7721 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
7723 /* release the spa config lock, retaining the namespace lock */
7724 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
7726 if (zio_injection_enabled
)
7727 zio_handle_panic_injection(spa
, FTAG
, 1);
7729 spa_activate(newspa
, spa_mode_global
);
7730 spa_async_suspend(newspa
);
7733 * Temporarily stop the initializing and TRIM activity. We set the
7734 * state to ACTIVE so that we know to resume initializing or TRIM
7735 * once the split has completed.
7737 list_t vd_initialize_list
;
7738 list_create(&vd_initialize_list
, sizeof (vdev_t
),
7739 offsetof(vdev_t
, vdev_initialize_node
));
7741 list_t vd_trim_list
;
7742 list_create(&vd_trim_list
, sizeof (vdev_t
),
7743 offsetof(vdev_t
, vdev_trim_node
));
7745 for (c
= 0; c
< children
; c
++) {
7746 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
7747 mutex_enter(&vml
[c
]->vdev_initialize_lock
);
7748 vdev_initialize_stop(vml
[c
],
7749 VDEV_INITIALIZE_ACTIVE
, &vd_initialize_list
);
7750 mutex_exit(&vml
[c
]->vdev_initialize_lock
);
7752 mutex_enter(&vml
[c
]->vdev_trim_lock
);
7753 vdev_trim_stop(vml
[c
], VDEV_TRIM_ACTIVE
, &vd_trim_list
);
7754 mutex_exit(&vml
[c
]->vdev_trim_lock
);
7758 vdev_initialize_stop_wait(spa
, &vd_initialize_list
);
7759 vdev_trim_stop_wait(spa
, &vd_trim_list
);
7761 list_destroy(&vd_initialize_list
);
7762 list_destroy(&vd_trim_list
);
7764 newspa
->spa_config_source
= SPA_CONFIG_SRC_SPLIT
;
7765 newspa
->spa_is_splitting
= B_TRUE
;
7767 /* create the new pool from the disks of the original pool */
7768 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
);
7772 /* if that worked, generate a real config for the new pool */
7773 if (newspa
->spa_root_vdev
!= NULL
) {
7774 newspa
->spa_config_splitting
= fnvlist_alloc();
7775 fnvlist_add_uint64(newspa
->spa_config_splitting
,
7776 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
));
7777 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
7782 if (props
!= NULL
) {
7783 spa_configfile_set(newspa
, props
, B_FALSE
);
7784 error
= spa_prop_set(newspa
, props
);
7789 /* flush everything */
7790 txg
= spa_vdev_config_enter(newspa
);
7791 vdev_config_dirty(newspa
->spa_root_vdev
);
7792 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
7794 if (zio_injection_enabled
)
7795 zio_handle_panic_injection(spa
, FTAG
, 2);
7797 spa_async_resume(newspa
);
7799 /* finally, update the original pool's config */
7800 txg
= spa_vdev_config_enter(spa
);
7801 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
7802 error
= dmu_tx_assign(tx
, TXG_WAIT
);
7805 for (c
= 0; c
< children
; c
++) {
7806 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
7807 vdev_t
*tvd
= vml
[c
]->vdev_top
;
7810 * Need to be sure the detachable VDEV is not
7811 * on any *other* txg's DTL list to prevent it
7812 * from being accessed after it's freed.
7814 for (int t
= 0; t
< TXG_SIZE
; t
++) {
7815 (void) txg_list_remove_this(
7816 &tvd
->vdev_dtl_list
, vml
[c
], t
);
7821 spa_history_log_internal(spa
, "detach", tx
,
7822 "vdev=%s", vml
[c
]->vdev_path
);
7827 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
7828 vdev_config_dirty(spa
->spa_root_vdev
);
7829 spa
->spa_config_splitting
= NULL
;
7833 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
7835 if (zio_injection_enabled
)
7836 zio_handle_panic_injection(spa
, FTAG
, 3);
7838 /* split is complete; log a history record */
7839 spa_history_log_internal(newspa
, "split", NULL
,
7840 "from pool %s", spa_name(spa
));
7842 newspa
->spa_is_splitting
= B_FALSE
;
7843 kmem_free(vml
, children
* sizeof (vdev_t
*));
7845 /* if we're not going to mount the filesystems in userland, export */
7847 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
7854 spa_deactivate(newspa
);
7857 txg
= spa_vdev_config_enter(spa
);
7859 /* re-online all offlined disks */
7860 for (c
= 0; c
< children
; c
++) {
7862 vml
[c
]->vdev_offline
= B_FALSE
;
7865 /* restart initializing or trimming disks as necessary */
7866 spa_async_request(spa
, SPA_ASYNC_INITIALIZE_RESTART
);
7867 spa_async_request(spa
, SPA_ASYNC_TRIM_RESTART
);
7868 spa_async_request(spa
, SPA_ASYNC_AUTOTRIM_RESTART
);
7870 vdev_reopen(spa
->spa_root_vdev
);
7872 nvlist_free(spa
->spa_config_splitting
);
7873 spa
->spa_config_splitting
= NULL
;
7874 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
7876 kmem_free(vml
, children
* sizeof (vdev_t
*));
7881 * Find any device that's done replacing, or a vdev marked 'unspare' that's
7882 * currently spared, so we can detach it.
7885 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
7887 vdev_t
*newvd
, *oldvd
;
7889 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7890 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
7896 * Check for a completed replacement. We always consider the first
7897 * vdev in the list to be the oldest vdev, and the last one to be
7898 * the newest (see spa_vdev_attach() for how that works). In
7899 * the case where the newest vdev is faulted, we will not automatically
7900 * remove it after a resilver completes. This is OK as it will require
7901 * user intervention to determine which disk the admin wishes to keep.
7903 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
7904 ASSERT(vd
->vdev_children
> 1);
7906 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
7907 oldvd
= vd
->vdev_child
[0];
7909 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
7910 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
7911 !vdev_dtl_required(oldvd
))
7916 * Check for a completed resilver with the 'unspare' flag set.
7917 * Also potentially update faulted state.
7919 if (vd
->vdev_ops
== &vdev_spare_ops
) {
7920 vdev_t
*first
= vd
->vdev_child
[0];
7921 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
7923 if (last
->vdev_unspare
) {
7926 } else if (first
->vdev_unspare
) {
7933 if (oldvd
!= NULL
&&
7934 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
7935 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
7936 !vdev_dtl_required(oldvd
))
7939 vdev_propagate_state(vd
);
7942 * If there are more than two spares attached to a disk,
7943 * and those spares are not required, then we want to
7944 * attempt to free them up now so that they can be used
7945 * by other pools. Once we're back down to a single
7946 * disk+spare, we stop removing them.
7948 if (vd
->vdev_children
> 2) {
7949 newvd
= vd
->vdev_child
[1];
7951 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
7952 vdev_dtl_empty(last
, DTL_MISSING
) &&
7953 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
7954 !vdev_dtl_required(newvd
))
7963 spa_vdev_resilver_done(spa_t
*spa
)
7965 vdev_t
*vd
, *pvd
, *ppvd
;
7966 uint64_t guid
, sguid
, pguid
, ppguid
;
7968 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
7970 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
7971 pvd
= vd
->vdev_parent
;
7972 ppvd
= pvd
->vdev_parent
;
7973 guid
= vd
->vdev_guid
;
7974 pguid
= pvd
->vdev_guid
;
7975 ppguid
= ppvd
->vdev_guid
;
7978 * If we have just finished replacing a hot spared device, then
7979 * we need to detach the parent's first child (the original hot
7982 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
7983 ppvd
->vdev_children
== 2) {
7984 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
7985 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
7987 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
7989 spa_config_exit(spa
, SCL_ALL
, FTAG
);
7990 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
7992 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
7994 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
7997 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8000 * If a detach was not performed above replace waiters will not have
8001 * been notified. In which case we must do so now.
8003 spa_notify_waiters(spa
);
8007 * Update the stored path or FRU for this vdev.
8010 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
8014 boolean_t sync
= B_FALSE
;
8016 ASSERT(spa_writeable(spa
));
8018 spa_vdev_state_enter(spa
, SCL_ALL
);
8020 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
8021 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
8023 if (!vd
->vdev_ops
->vdev_op_leaf
)
8024 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
8027 if (strcmp(value
, vd
->vdev_path
) != 0) {
8028 spa_strfree(vd
->vdev_path
);
8029 vd
->vdev_path
= spa_strdup(value
);
8033 if (vd
->vdev_fru
== NULL
) {
8034 vd
->vdev_fru
= spa_strdup(value
);
8036 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
8037 spa_strfree(vd
->vdev_fru
);
8038 vd
->vdev_fru
= spa_strdup(value
);
8043 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
8047 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
8049 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
8053 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
8055 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
8059 * ==========================================================================
8061 * ==========================================================================
8064 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
8066 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8068 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8069 return (SET_ERROR(EBUSY
));
8071 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
8075 spa_scan_stop(spa_t
*spa
)
8077 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8078 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8079 return (SET_ERROR(EBUSY
));
8080 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
8084 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
8086 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8088 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
8089 return (SET_ERROR(ENOTSUP
));
8091 if (func
== POOL_SCAN_RESILVER
&&
8092 !spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
))
8093 return (SET_ERROR(ENOTSUP
));
8096 * If a resilver was requested, but there is no DTL on a
8097 * writeable leaf device, we have nothing to do.
8099 if (func
== POOL_SCAN_RESILVER
&&
8100 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
8101 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
8105 return (dsl_scan(spa
->spa_dsl_pool
, func
));
8109 * ==========================================================================
8110 * SPA async task processing
8111 * ==========================================================================
8115 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
8117 if (vd
->vdev_remove_wanted
) {
8118 vd
->vdev_remove_wanted
= B_FALSE
;
8119 vd
->vdev_delayed_close
= B_FALSE
;
8120 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
8123 * We want to clear the stats, but we don't want to do a full
8124 * vdev_clear() as that will cause us to throw away
8125 * degraded/faulted state as well as attempt to reopen the
8126 * device, all of which is a waste.
8128 vd
->vdev_stat
.vs_read_errors
= 0;
8129 vd
->vdev_stat
.vs_write_errors
= 0;
8130 vd
->vdev_stat
.vs_checksum_errors
= 0;
8132 vdev_state_dirty(vd
->vdev_top
);
8134 /* Tell userspace that the vdev is gone. */
8135 zfs_post_remove(spa
, vd
);
8138 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8139 spa_async_remove(spa
, vd
->vdev_child
[c
]);
8143 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
8145 if (vd
->vdev_probe_wanted
) {
8146 vd
->vdev_probe_wanted
= B_FALSE
;
8147 vdev_reopen(vd
); /* vdev_open() does the actual probe */
8150 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8151 spa_async_probe(spa
, vd
->vdev_child
[c
]);
8155 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
8157 if (!spa
->spa_autoexpand
)
8160 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
8161 vdev_t
*cvd
= vd
->vdev_child
[c
];
8162 spa_async_autoexpand(spa
, cvd
);
8165 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
8168 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
8171 static __attribute__((noreturn
)) void
8172 spa_async_thread(void *arg
)
8174 spa_t
*spa
= (spa_t
*)arg
;
8175 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
8178 ASSERT(spa
->spa_sync_on
);
8180 mutex_enter(&spa
->spa_async_lock
);
8181 tasks
= spa
->spa_async_tasks
;
8182 spa
->spa_async_tasks
= 0;
8183 mutex_exit(&spa
->spa_async_lock
);
8186 * See if the config needs to be updated.
8188 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
8189 uint64_t old_space
, new_space
;
8191 mutex_enter(&spa_namespace_lock
);
8192 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
8193 old_space
+= metaslab_class_get_space(spa_special_class(spa
));
8194 old_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8195 old_space
+= metaslab_class_get_space(
8196 spa_embedded_log_class(spa
));
8198 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
8200 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
8201 new_space
+= metaslab_class_get_space(spa_special_class(spa
));
8202 new_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8203 new_space
+= metaslab_class_get_space(
8204 spa_embedded_log_class(spa
));
8205 mutex_exit(&spa_namespace_lock
);
8208 * If the pool grew as a result of the config update,
8209 * then log an internal history event.
8211 if (new_space
!= old_space
) {
8212 spa_history_log_internal(spa
, "vdev online", NULL
,
8213 "pool '%s' size: %llu(+%llu)",
8214 spa_name(spa
), (u_longlong_t
)new_space
,
8215 (u_longlong_t
)(new_space
- old_space
));
8220 * See if any devices need to be marked REMOVED.
8222 if (tasks
& SPA_ASYNC_REMOVE
) {
8223 spa_vdev_state_enter(spa
, SCL_NONE
);
8224 spa_async_remove(spa
, spa
->spa_root_vdev
);
8225 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
8226 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
8227 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
8228 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
8229 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8232 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
8233 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8234 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
8235 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8239 * See if any devices need to be probed.
8241 if (tasks
& SPA_ASYNC_PROBE
) {
8242 spa_vdev_state_enter(spa
, SCL_NONE
);
8243 spa_async_probe(spa
, spa
->spa_root_vdev
);
8244 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8248 * If any devices are done replacing, detach them.
8250 if (tasks
& SPA_ASYNC_RESILVER_DONE
||
8251 tasks
& SPA_ASYNC_REBUILD_DONE
) {
8252 spa_vdev_resilver_done(spa
);
8256 * Kick off a resilver.
8258 if (tasks
& SPA_ASYNC_RESILVER
&&
8259 !vdev_rebuild_active(spa
->spa_root_vdev
) &&
8260 (!dsl_scan_resilvering(dp
) ||
8261 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_RESILVER_DEFER
)))
8262 dsl_scan_restart_resilver(dp
, 0);
8264 if (tasks
& SPA_ASYNC_INITIALIZE_RESTART
) {
8265 mutex_enter(&spa_namespace_lock
);
8266 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8267 vdev_initialize_restart(spa
->spa_root_vdev
);
8268 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8269 mutex_exit(&spa_namespace_lock
);
8272 if (tasks
& SPA_ASYNC_TRIM_RESTART
) {
8273 mutex_enter(&spa_namespace_lock
);
8274 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8275 vdev_trim_restart(spa
->spa_root_vdev
);
8276 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8277 mutex_exit(&spa_namespace_lock
);
8280 if (tasks
& SPA_ASYNC_AUTOTRIM_RESTART
) {
8281 mutex_enter(&spa_namespace_lock
);
8282 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8283 vdev_autotrim_restart(spa
);
8284 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8285 mutex_exit(&spa_namespace_lock
);
8289 * Kick off L2 cache whole device TRIM.
8291 if (tasks
& SPA_ASYNC_L2CACHE_TRIM
) {
8292 mutex_enter(&spa_namespace_lock
);
8293 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8294 vdev_trim_l2arc(spa
);
8295 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8296 mutex_exit(&spa_namespace_lock
);
8300 * Kick off L2 cache rebuilding.
8302 if (tasks
& SPA_ASYNC_L2CACHE_REBUILD
) {
8303 mutex_enter(&spa_namespace_lock
);
8304 spa_config_enter(spa
, SCL_L2ARC
, FTAG
, RW_READER
);
8305 l2arc_spa_rebuild_start(spa
);
8306 spa_config_exit(spa
, SCL_L2ARC
, FTAG
);
8307 mutex_exit(&spa_namespace_lock
);
8311 * Let the world know that we're done.
8313 mutex_enter(&spa
->spa_async_lock
);
8314 spa
->spa_async_thread
= NULL
;
8315 cv_broadcast(&spa
->spa_async_cv
);
8316 mutex_exit(&spa
->spa_async_lock
);
8321 spa_async_suspend(spa_t
*spa
)
8323 mutex_enter(&spa
->spa_async_lock
);
8324 spa
->spa_async_suspended
++;
8325 while (spa
->spa_async_thread
!= NULL
)
8326 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
8327 mutex_exit(&spa
->spa_async_lock
);
8329 spa_vdev_remove_suspend(spa
);
8331 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
8332 if (condense_thread
!= NULL
)
8333 zthr_cancel(condense_thread
);
8335 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
8336 if (discard_thread
!= NULL
)
8337 zthr_cancel(discard_thread
);
8339 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
8340 if (ll_delete_thread
!= NULL
)
8341 zthr_cancel(ll_delete_thread
);
8343 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
8344 if (ll_condense_thread
!= NULL
)
8345 zthr_cancel(ll_condense_thread
);
8349 spa_async_resume(spa_t
*spa
)
8351 mutex_enter(&spa
->spa_async_lock
);
8352 ASSERT(spa
->spa_async_suspended
!= 0);
8353 spa
->spa_async_suspended
--;
8354 mutex_exit(&spa
->spa_async_lock
);
8355 spa_restart_removal(spa
);
8357 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
8358 if (condense_thread
!= NULL
)
8359 zthr_resume(condense_thread
);
8361 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
8362 if (discard_thread
!= NULL
)
8363 zthr_resume(discard_thread
);
8365 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
8366 if (ll_delete_thread
!= NULL
)
8367 zthr_resume(ll_delete_thread
);
8369 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
8370 if (ll_condense_thread
!= NULL
)
8371 zthr_resume(ll_condense_thread
);
8375 spa_async_tasks_pending(spa_t
*spa
)
8377 uint_t non_config_tasks
;
8379 boolean_t config_task_suspended
;
8381 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
8382 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
8383 if (spa
->spa_ccw_fail_time
== 0) {
8384 config_task_suspended
= B_FALSE
;
8386 config_task_suspended
=
8387 (gethrtime() - spa
->spa_ccw_fail_time
) <
8388 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
8391 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
8395 spa_async_dispatch(spa_t
*spa
)
8397 mutex_enter(&spa
->spa_async_lock
);
8398 if (spa_async_tasks_pending(spa
) &&
8399 !spa
->spa_async_suspended
&&
8400 spa
->spa_async_thread
== NULL
)
8401 spa
->spa_async_thread
= thread_create(NULL
, 0,
8402 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
8403 mutex_exit(&spa
->spa_async_lock
);
8407 spa_async_request(spa_t
*spa
, int task
)
8409 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
8410 mutex_enter(&spa
->spa_async_lock
);
8411 spa
->spa_async_tasks
|= task
;
8412 mutex_exit(&spa
->spa_async_lock
);
8416 spa_async_tasks(spa_t
*spa
)
8418 return (spa
->spa_async_tasks
);
8422 * ==========================================================================
8423 * SPA syncing routines
8424 * ==========================================================================
8429 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
8433 bpobj_enqueue(bpo
, bp
, bp_freed
, tx
);
8438 bpobj_enqueue_alloc_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8440 return (bpobj_enqueue_cb(arg
, bp
, B_FALSE
, tx
));
8444 bpobj_enqueue_free_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8446 return (bpobj_enqueue_cb(arg
, bp
, B_TRUE
, tx
));
8450 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8454 zio_nowait(zio_free_sync(pio
, pio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
8460 bpobj_spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
8464 return (spa_free_sync_cb(arg
, bp
, tx
));
8468 * Note: this simple function is not inlined to make it easier to dtrace the
8469 * amount of time spent syncing frees.
8472 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
8474 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
8475 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
8476 VERIFY(zio_wait(zio
) == 0);
8480 * Note: this simple function is not inlined to make it easier to dtrace the
8481 * amount of time spent syncing deferred frees.
8484 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
8486 if (spa_sync_pass(spa
) != 1)
8491 * If the log space map feature is active, we stop deferring
8492 * frees to the next TXG and therefore running this function
8493 * would be considered a no-op as spa_deferred_bpobj should
8494 * not have any entries.
8496 * That said we run this function anyway (instead of returning
8497 * immediately) for the edge-case scenario where we just
8498 * activated the log space map feature in this TXG but we have
8499 * deferred frees from the previous TXG.
8501 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
8502 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
8503 bpobj_spa_free_sync_cb
, zio
, tx
), ==, 0);
8504 VERIFY0(zio_wait(zio
));
8508 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
8510 char *packed
= NULL
;
8515 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
8518 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
8519 * information. This avoids the dmu_buf_will_dirty() path and
8520 * saves us a pre-read to get data we don't actually care about.
8522 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
8523 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
8525 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
8527 memset(packed
+ nvsize
, 0, bufsize
- nvsize
);
8529 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
8531 vmem_free(packed
, bufsize
);
8533 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
8534 dmu_buf_will_dirty(db
, tx
);
8535 *(uint64_t *)db
->db_data
= nvsize
;
8536 dmu_buf_rele(db
, FTAG
);
8540 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
8541 const char *config
, const char *entry
)
8551 * Update the MOS nvlist describing the list of available devices.
8552 * spa_validate_aux() will have already made sure this nvlist is
8553 * valid and the vdevs are labeled appropriately.
8555 if (sav
->sav_object
== 0) {
8556 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
8557 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
8558 sizeof (uint64_t), tx
);
8559 VERIFY(zap_update(spa
->spa_meta_objset
,
8560 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
8561 &sav
->sav_object
, tx
) == 0);
8564 nvroot
= fnvlist_alloc();
8565 if (sav
->sav_count
== 0) {
8566 fnvlist_add_nvlist_array(nvroot
, config
,
8567 (const nvlist_t
* const *)NULL
, 0);
8569 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
8570 for (i
= 0; i
< sav
->sav_count
; i
++)
8571 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
8572 B_FALSE
, VDEV_CONFIG_L2CACHE
);
8573 fnvlist_add_nvlist_array(nvroot
, config
,
8574 (const nvlist_t
* const *)list
, sav
->sav_count
);
8575 for (i
= 0; i
< sav
->sav_count
; i
++)
8576 nvlist_free(list
[i
]);
8577 kmem_free(list
, sav
->sav_count
* sizeof (void *));
8580 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
8581 nvlist_free(nvroot
);
8583 sav
->sav_sync
= B_FALSE
;
8587 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
8588 * The all-vdev ZAP must be empty.
8591 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
8593 spa_t
*spa
= vd
->vdev_spa
;
8595 if (vd
->vdev_top_zap
!= 0) {
8596 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
8597 vd
->vdev_top_zap
, tx
));
8599 if (vd
->vdev_leaf_zap
!= 0) {
8600 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
8601 vd
->vdev_leaf_zap
, tx
));
8603 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
8604 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
8609 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
8614 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
8615 * its config may not be dirty but we still need to build per-vdev ZAPs.
8616 * Similarly, if the pool is being assembled (e.g. after a split), we
8617 * need to rebuild the AVZ although the config may not be dirty.
8619 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
8620 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
8623 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
8625 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
8626 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
8627 spa
->spa_all_vdev_zaps
!= 0);
8629 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
8630 /* Make and build the new AVZ */
8631 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
8632 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
8633 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
8635 /* Diff old AVZ with new one */
8639 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
8640 spa
->spa_all_vdev_zaps
);
8641 zap_cursor_retrieve(&zc
, &za
) == 0;
8642 zap_cursor_advance(&zc
)) {
8643 uint64_t vdzap
= za
.za_first_integer
;
8644 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
8647 * ZAP is listed in old AVZ but not in new one;
8650 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
8655 zap_cursor_fini(&zc
);
8657 /* Destroy the old AVZ */
8658 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
8659 spa
->spa_all_vdev_zaps
, tx
));
8661 /* Replace the old AVZ in the dir obj with the new one */
8662 VERIFY0(zap_update(spa
->spa_meta_objset
,
8663 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
8664 sizeof (new_avz
), 1, &new_avz
, tx
));
8666 spa
->spa_all_vdev_zaps
= new_avz
;
8667 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
8671 /* Walk through the AVZ and destroy all listed ZAPs */
8672 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
8673 spa
->spa_all_vdev_zaps
);
8674 zap_cursor_retrieve(&zc
, &za
) == 0;
8675 zap_cursor_advance(&zc
)) {
8676 uint64_t zap
= za
.za_first_integer
;
8677 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
8680 zap_cursor_fini(&zc
);
8682 /* Destroy and unlink the AVZ itself */
8683 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
8684 spa
->spa_all_vdev_zaps
, tx
));
8685 VERIFY0(zap_remove(spa
->spa_meta_objset
,
8686 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
8687 spa
->spa_all_vdev_zaps
= 0;
8690 if (spa
->spa_all_vdev_zaps
== 0) {
8691 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
8692 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
8693 DMU_POOL_VDEV_ZAP_MAP
, tx
);
8695 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
8697 /* Create ZAPs for vdevs that don't have them. */
8698 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
8700 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
8701 dmu_tx_get_txg(tx
), B_FALSE
);
8704 * If we're upgrading the spa version then make sure that
8705 * the config object gets updated with the correct version.
8707 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
8708 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
8709 spa
->spa_uberblock
.ub_version
);
8711 spa_config_exit(spa
, SCL_STATE
, FTAG
);
8713 nvlist_free(spa
->spa_config_syncing
);
8714 spa
->spa_config_syncing
= config
;
8716 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
8720 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
8722 uint64_t *versionp
= arg
;
8723 uint64_t version
= *versionp
;
8724 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
8727 * Setting the version is special cased when first creating the pool.
8729 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
8731 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
8732 ASSERT(version
>= spa_version(spa
));
8734 spa
->spa_uberblock
.ub_version
= version
;
8735 vdev_config_dirty(spa
->spa_root_vdev
);
8736 spa_history_log_internal(spa
, "set", tx
, "version=%lld",
8737 (longlong_t
)version
);
8741 * Set zpool properties.
8744 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
8746 nvlist_t
*nvp
= arg
;
8747 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
8748 objset_t
*mos
= spa
->spa_meta_objset
;
8749 nvpair_t
*elem
= NULL
;
8751 mutex_enter(&spa
->spa_props_lock
);
8753 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
8755 char *strval
, *fname
;
8757 const char *propname
;
8758 zprop_type_t proptype
;
8761 switch (prop
= zpool_name_to_prop(nvpair_name(elem
))) {
8762 case ZPOOL_PROP_INVAL
:
8764 * We checked this earlier in spa_prop_validate().
8766 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
8768 fname
= strchr(nvpair_name(elem
), '@') + 1;
8769 VERIFY0(zfeature_lookup_name(fname
, &fid
));
8771 spa_feature_enable(spa
, fid
, tx
);
8772 spa_history_log_internal(spa
, "set", tx
,
8773 "%s=enabled", nvpair_name(elem
));
8776 case ZPOOL_PROP_VERSION
:
8777 intval
= fnvpair_value_uint64(elem
);
8779 * The version is synced separately before other
8780 * properties and should be correct by now.
8782 ASSERT3U(spa_version(spa
), >=, intval
);
8785 case ZPOOL_PROP_ALTROOT
:
8787 * 'altroot' is a non-persistent property. It should
8788 * have been set temporarily at creation or import time.
8790 ASSERT(spa
->spa_root
!= NULL
);
8793 case ZPOOL_PROP_READONLY
:
8794 case ZPOOL_PROP_CACHEFILE
:
8796 * 'readonly' and 'cachefile' are also non-persistent
8800 case ZPOOL_PROP_COMMENT
:
8801 strval
= fnvpair_value_string(elem
);
8802 if (spa
->spa_comment
!= NULL
)
8803 spa_strfree(spa
->spa_comment
);
8804 spa
->spa_comment
= spa_strdup(strval
);
8806 * We need to dirty the configuration on all the vdevs
8807 * so that their labels get updated. We also need to
8808 * update the cache file to keep it in sync with the
8809 * MOS version. It's unnecessary to do this for pool
8810 * creation since the vdev's configuration has already
8813 if (tx
->tx_txg
!= TXG_INITIAL
) {
8814 vdev_config_dirty(spa
->spa_root_vdev
);
8815 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
8817 spa_history_log_internal(spa
, "set", tx
,
8818 "%s=%s", nvpair_name(elem
), strval
);
8820 case ZPOOL_PROP_COMPATIBILITY
:
8821 strval
= fnvpair_value_string(elem
);
8822 if (spa
->spa_compatibility
!= NULL
)
8823 spa_strfree(spa
->spa_compatibility
);
8824 spa
->spa_compatibility
= spa_strdup(strval
);
8826 * Dirty the configuration on vdevs as above.
8828 if (tx
->tx_txg
!= TXG_INITIAL
) {
8829 vdev_config_dirty(spa
->spa_root_vdev
);
8830 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
8833 spa_history_log_internal(spa
, "set", tx
,
8834 "%s=%s", nvpair_name(elem
), strval
);
8839 * Set pool property values in the poolprops mos object.
8841 if (spa
->spa_pool_props_object
== 0) {
8842 spa
->spa_pool_props_object
=
8843 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
8844 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
8848 /* normalize the property name */
8849 propname
= zpool_prop_to_name(prop
);
8850 proptype
= zpool_prop_get_type(prop
);
8852 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
8853 ASSERT(proptype
== PROP_TYPE_STRING
);
8854 strval
= fnvpair_value_string(elem
);
8855 VERIFY0(zap_update(mos
,
8856 spa
->spa_pool_props_object
, propname
,
8857 1, strlen(strval
) + 1, strval
, tx
));
8858 spa_history_log_internal(spa
, "set", tx
,
8859 "%s=%s", nvpair_name(elem
), strval
);
8860 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
8861 intval
= fnvpair_value_uint64(elem
);
8863 if (proptype
== PROP_TYPE_INDEX
) {
8865 VERIFY0(zpool_prop_index_to_string(
8866 prop
, intval
, &unused
));
8868 VERIFY0(zap_update(mos
,
8869 spa
->spa_pool_props_object
, propname
,
8870 8, 1, &intval
, tx
));
8871 spa_history_log_internal(spa
, "set", tx
,
8872 "%s=%lld", nvpair_name(elem
),
8873 (longlong_t
)intval
);
8876 case ZPOOL_PROP_DELEGATION
:
8877 spa
->spa_delegation
= intval
;
8879 case ZPOOL_PROP_BOOTFS
:
8880 spa
->spa_bootfs
= intval
;
8882 case ZPOOL_PROP_FAILUREMODE
:
8883 spa
->spa_failmode
= intval
;
8885 case ZPOOL_PROP_AUTOTRIM
:
8886 spa
->spa_autotrim
= intval
;
8887 spa_async_request(spa
,
8888 SPA_ASYNC_AUTOTRIM_RESTART
);
8890 case ZPOOL_PROP_AUTOEXPAND
:
8891 spa
->spa_autoexpand
= intval
;
8892 if (tx
->tx_txg
!= TXG_INITIAL
)
8893 spa_async_request(spa
,
8894 SPA_ASYNC_AUTOEXPAND
);
8896 case ZPOOL_PROP_MULTIHOST
:
8897 spa
->spa_multihost
= intval
;
8903 ASSERT(0); /* not allowed */
8909 mutex_exit(&spa
->spa_props_lock
);
8913 * Perform one-time upgrade on-disk changes. spa_version() does not
8914 * reflect the new version this txg, so there must be no changes this
8915 * txg to anything that the upgrade code depends on after it executes.
8916 * Therefore this must be called after dsl_pool_sync() does the sync
8920 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
8922 if (spa_sync_pass(spa
) != 1)
8925 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
8926 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
8928 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
8929 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
8930 dsl_pool_create_origin(dp
, tx
);
8932 /* Keeping the origin open increases spa_minref */
8933 spa
->spa_minref
+= 3;
8936 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
8937 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
8938 dsl_pool_upgrade_clones(dp
, tx
);
8941 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
8942 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
8943 dsl_pool_upgrade_dir_clones(dp
, tx
);
8945 /* Keeping the freedir open increases spa_minref */
8946 spa
->spa_minref
+= 3;
8949 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
8950 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
8951 spa_feature_create_zap_objects(spa
, tx
);
8955 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
8956 * when possibility to use lz4 compression for metadata was added
8957 * Old pools that have this feature enabled must be upgraded to have
8958 * this feature active
8960 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
8961 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
8962 SPA_FEATURE_LZ4_COMPRESS
);
8963 boolean_t lz4_ac
= spa_feature_is_active(spa
,
8964 SPA_FEATURE_LZ4_COMPRESS
);
8966 if (lz4_en
&& !lz4_ac
)
8967 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
8971 * If we haven't written the salt, do so now. Note that the
8972 * feature may not be activated yet, but that's fine since
8973 * the presence of this ZAP entry is backwards compatible.
8975 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
8976 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
8977 VERIFY0(zap_add(spa
->spa_meta_objset
,
8978 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
8979 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
8980 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
8983 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
8987 vdev_indirect_state_sync_verify(vdev_t
*vd
)
8989 vdev_indirect_mapping_t
*vim __maybe_unused
= vd
->vdev_indirect_mapping
;
8990 vdev_indirect_births_t
*vib __maybe_unused
= vd
->vdev_indirect_births
;
8992 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
8993 ASSERT(vim
!= NULL
);
8994 ASSERT(vib
!= NULL
);
8997 uint64_t obsolete_sm_object
= 0;
8998 ASSERT0(vdev_obsolete_sm_object(vd
, &obsolete_sm_object
));
8999 if (obsolete_sm_object
!= 0) {
9000 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
9001 ASSERT(vd
->vdev_removing
||
9002 vd
->vdev_ops
== &vdev_indirect_ops
);
9003 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
9004 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
9005 ASSERT3U(obsolete_sm_object
, ==,
9006 space_map_object(vd
->vdev_obsolete_sm
));
9007 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
9008 space_map_allocated(vd
->vdev_obsolete_sm
));
9010 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
9013 * Since frees / remaps to an indirect vdev can only
9014 * happen in syncing context, the obsolete segments
9015 * tree must be empty when we start syncing.
9017 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
9021 * Set the top-level vdev's max queue depth. Evaluate each top-level's
9022 * async write queue depth in case it changed. The max queue depth will
9023 * not change in the middle of syncing out this txg.
9026 spa_sync_adjust_vdev_max_queue_depth(spa_t
*spa
)
9028 ASSERT(spa_writeable(spa
));
9030 vdev_t
*rvd
= spa
->spa_root_vdev
;
9031 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
9032 zfs_vdev_queue_depth_pct
/ 100;
9033 metaslab_class_t
*normal
= spa_normal_class(spa
);
9034 metaslab_class_t
*special
= spa_special_class(spa
);
9035 metaslab_class_t
*dedup
= spa_dedup_class(spa
);
9037 uint64_t slots_per_allocator
= 0;
9038 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9039 vdev_t
*tvd
= rvd
->vdev_child
[c
];
9041 metaslab_group_t
*mg
= tvd
->vdev_mg
;
9042 if (mg
== NULL
|| !metaslab_group_initialized(mg
))
9045 metaslab_class_t
*mc
= mg
->mg_class
;
9046 if (mc
!= normal
&& mc
!= special
&& mc
!= dedup
)
9050 * It is safe to do a lock-free check here because only async
9051 * allocations look at mg_max_alloc_queue_depth, and async
9052 * allocations all happen from spa_sync().
9054 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9055 ASSERT0(zfs_refcount_count(
9056 &(mg
->mg_allocator
[i
].mga_alloc_queue_depth
)));
9058 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
9060 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9061 mg
->mg_allocator
[i
].mga_cur_max_alloc_queue_depth
=
9062 zfs_vdev_def_queue_depth
;
9064 slots_per_allocator
+= zfs_vdev_def_queue_depth
;
9067 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9068 ASSERT0(zfs_refcount_count(&normal
->mc_allocator
[i
].
9070 ASSERT0(zfs_refcount_count(&special
->mc_allocator
[i
].
9072 ASSERT0(zfs_refcount_count(&dedup
->mc_allocator
[i
].
9074 normal
->mc_allocator
[i
].mca_alloc_max_slots
=
9075 slots_per_allocator
;
9076 special
->mc_allocator
[i
].mca_alloc_max_slots
=
9077 slots_per_allocator
;
9078 dedup
->mc_allocator
[i
].mca_alloc_max_slots
=
9079 slots_per_allocator
;
9081 normal
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9082 special
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9083 dedup
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9087 spa_sync_condense_indirect(spa_t
*spa
, dmu_tx_t
*tx
)
9089 ASSERT(spa_writeable(spa
));
9091 vdev_t
*rvd
= spa
->spa_root_vdev
;
9092 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9093 vdev_t
*vd
= rvd
->vdev_child
[c
];
9094 vdev_indirect_state_sync_verify(vd
);
9096 if (vdev_indirect_should_condense(vd
)) {
9097 spa_condense_indirect_start_sync(vd
, tx
);
9104 spa_sync_iterate_to_convergence(spa_t
*spa
, dmu_tx_t
*tx
)
9106 objset_t
*mos
= spa
->spa_meta_objset
;
9107 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9108 uint64_t txg
= tx
->tx_txg
;
9109 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
9112 int pass
= ++spa
->spa_sync_pass
;
9114 spa_sync_config_object(spa
, tx
);
9115 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
9116 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
9117 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
9118 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
9119 spa_errlog_sync(spa
, txg
);
9120 dsl_pool_sync(dp
, txg
);
9122 if (pass
< zfs_sync_pass_deferred_free
||
9123 spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
)) {
9125 * If the log space map feature is active we don't
9126 * care about deferred frees and the deferred bpobj
9127 * as the log space map should effectively have the
9128 * same results (i.e. appending only to one object).
9130 spa_sync_frees(spa
, free_bpl
, tx
);
9133 * We can not defer frees in pass 1, because
9134 * we sync the deferred frees later in pass 1.
9136 ASSERT3U(pass
, >, 1);
9137 bplist_iterate(free_bpl
, bpobj_enqueue_alloc_cb
,
9138 &spa
->spa_deferred_bpobj
, tx
);
9142 dsl_scan_sync(dp
, tx
);
9144 spa_sync_upgrades(spa
, tx
);
9146 spa_flush_metaslabs(spa
, tx
);
9149 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
9154 * Note: We need to check if the MOS is dirty because we could
9155 * have marked the MOS dirty without updating the uberblock
9156 * (e.g. if we have sync tasks but no dirty user data). We need
9157 * to check the uberblock's rootbp because it is updated if we
9158 * have synced out dirty data (though in this case the MOS will
9159 * most likely also be dirty due to second order effects, we
9160 * don't want to rely on that here).
9163 spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
9164 !dmu_objset_is_dirty(mos
, txg
)) {
9166 * Nothing changed on the first pass, therefore this
9167 * TXG is a no-op. Avoid syncing deferred frees, so
9168 * that we can keep this TXG as a no-op.
9170 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
9171 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
9172 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
9173 ASSERT(txg_list_empty(&dp
->dp_early_sync_tasks
, txg
));
9177 spa_sync_deferred_frees(spa
, tx
);
9178 } while (dmu_objset_is_dirty(mos
, txg
));
9182 * Rewrite the vdev configuration (which includes the uberblock) to
9183 * commit the transaction group.
9185 * If there are no dirty vdevs, we sync the uberblock to a few random
9186 * top-level vdevs that are known to be visible in the config cache
9187 * (see spa_vdev_add() for a complete description). If there *are* dirty
9188 * vdevs, sync the uberblock to all vdevs.
9191 spa_sync_rewrite_vdev_config(spa_t
*spa
, dmu_tx_t
*tx
)
9193 vdev_t
*rvd
= spa
->spa_root_vdev
;
9194 uint64_t txg
= tx
->tx_txg
;
9200 * We hold SCL_STATE to prevent vdev open/close/etc.
9201 * while we're attempting to write the vdev labels.
9203 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9205 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
9206 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
9208 int children
= rvd
->vdev_children
;
9209 int c0
= random_in_range(children
);
9211 for (int c
= 0; c
< children
; c
++) {
9213 rvd
->vdev_child
[(c0
+ c
) % children
];
9215 /* Stop when revisiting the first vdev */
9216 if (c
> 0 && svd
[0] == vd
)
9219 if (vd
->vdev_ms_array
== 0 ||
9221 !vdev_is_concrete(vd
))
9224 svd
[svdcount
++] = vd
;
9225 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
9228 error
= vdev_config_sync(svd
, svdcount
, txg
);
9230 error
= vdev_config_sync(rvd
->vdev_child
,
9231 rvd
->vdev_children
, txg
);
9235 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
9237 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9241 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
9242 zio_resume_wait(spa
);
9247 * Sync the specified transaction group. New blocks may be dirtied as
9248 * part of the process, so we iterate until it converges.
9251 spa_sync(spa_t
*spa
, uint64_t txg
)
9255 VERIFY(spa_writeable(spa
));
9258 * Wait for i/os issued in open context that need to complete
9259 * before this txg syncs.
9261 (void) zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]);
9262 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
,
9266 * Lock out configuration changes.
9268 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
9270 spa
->spa_syncing_txg
= txg
;
9271 spa
->spa_sync_pass
= 0;
9273 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9274 mutex_enter(&spa
->spa_allocs
[i
].spaa_lock
);
9275 VERIFY0(avl_numnodes(&spa
->spa_allocs
[i
].spaa_tree
));
9276 mutex_exit(&spa
->spa_allocs
[i
].spaa_lock
);
9280 * If there are any pending vdev state changes, convert them
9281 * into config changes that go out with this transaction group.
9283 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9284 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
9285 /* Avoid holding the write lock unless actually necessary */
9286 if (vd
->vdev_aux
== NULL
) {
9287 vdev_state_clean(vd
);
9288 vdev_config_dirty(vd
);
9292 * We need the write lock here because, for aux vdevs,
9293 * calling vdev_config_dirty() modifies sav_config.
9294 * This is ugly and will become unnecessary when we
9295 * eliminate the aux vdev wart by integrating all vdevs
9296 * into the root vdev tree.
9298 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9299 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
9300 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
9301 vdev_state_clean(vd
);
9302 vdev_config_dirty(vd
);
9304 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9305 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9307 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9309 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9310 dmu_tx_t
*tx
= dmu_tx_create_assigned(dp
, txg
);
9312 spa
->spa_sync_starttime
= gethrtime();
9313 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
9314 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
9315 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
9316 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
9319 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
9320 * set spa_deflate if we have no raid-z vdevs.
9322 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
9323 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
9324 vdev_t
*rvd
= spa
->spa_root_vdev
;
9327 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
9328 vd
= rvd
->vdev_child
[i
];
9329 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
9332 if (i
== rvd
->vdev_children
) {
9333 spa
->spa_deflate
= TRUE
;
9334 VERIFY0(zap_add(spa
->spa_meta_objset
,
9335 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
9336 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
9340 spa_sync_adjust_vdev_max_queue_depth(spa
);
9342 spa_sync_condense_indirect(spa
, tx
);
9344 spa_sync_iterate_to_convergence(spa
, tx
);
9347 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
9349 * Make sure that the number of ZAPs for all the vdevs matches
9350 * the number of ZAPs in the per-vdev ZAP list. This only gets
9351 * called if the config is dirty; otherwise there may be
9352 * outstanding AVZ operations that weren't completed in
9353 * spa_sync_config_object.
9355 uint64_t all_vdev_zap_entry_count
;
9356 ASSERT0(zap_count(spa
->spa_meta_objset
,
9357 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
9358 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
9359 all_vdev_zap_entry_count
);
9363 if (spa
->spa_vdev_removal
!= NULL
) {
9364 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
9367 spa_sync_rewrite_vdev_config(spa
, tx
);
9370 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
9371 spa
->spa_deadman_tqid
= 0;
9374 * Clear the dirty config list.
9376 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
9377 vdev_config_clean(vd
);
9380 * Now that the new config has synced transactionally,
9381 * let it become visible to the config cache.
9383 if (spa
->spa_config_syncing
!= NULL
) {
9384 spa_config_set(spa
, spa
->spa_config_syncing
);
9385 spa
->spa_config_txg
= txg
;
9386 spa
->spa_config_syncing
= NULL
;
9389 dsl_pool_sync_done(dp
, txg
);
9391 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9392 mutex_enter(&spa
->spa_allocs
[i
].spaa_lock
);
9393 VERIFY0(avl_numnodes(&spa
->spa_allocs
[i
].spaa_tree
));
9394 mutex_exit(&spa
->spa_allocs
[i
].spaa_lock
);
9398 * Update usable space statistics.
9400 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
)))
9402 vdev_sync_done(vd
, txg
);
9404 metaslab_class_evict_old(spa
->spa_normal_class
, txg
);
9405 metaslab_class_evict_old(spa
->spa_log_class
, txg
);
9407 spa_sync_close_syncing_log_sm(spa
);
9409 spa_update_dspace(spa
);
9412 * It had better be the case that we didn't dirty anything
9413 * since vdev_config_sync().
9415 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
9416 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
9417 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
9419 while (zfs_pause_spa_sync
)
9422 spa
->spa_sync_pass
= 0;
9425 * Update the last synced uberblock here. We want to do this at
9426 * the end of spa_sync() so that consumers of spa_last_synced_txg()
9427 * will be guaranteed that all the processing associated with
9428 * that txg has been completed.
9430 spa
->spa_ubsync
= spa
->spa_uberblock
;
9431 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
9433 spa_handle_ignored_writes(spa
);
9436 * If any async tasks have been requested, kick them off.
9438 spa_async_dispatch(spa
);
9442 * Sync all pools. We don't want to hold the namespace lock across these
9443 * operations, so we take a reference on the spa_t and drop the lock during the
9447 spa_sync_allpools(void)
9450 mutex_enter(&spa_namespace_lock
);
9451 while ((spa
= spa_next(spa
)) != NULL
) {
9452 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
9453 !spa_writeable(spa
) || spa_suspended(spa
))
9455 spa_open_ref(spa
, FTAG
);
9456 mutex_exit(&spa_namespace_lock
);
9457 txg_wait_synced(spa_get_dsl(spa
), 0);
9458 mutex_enter(&spa_namespace_lock
);
9459 spa_close(spa
, FTAG
);
9461 mutex_exit(&spa_namespace_lock
);
9465 * ==========================================================================
9466 * Miscellaneous routines
9467 * ==========================================================================
9471 * Remove all pools in the system.
9479 * Remove all cached state. All pools should be closed now,
9480 * so every spa in the AVL tree should be unreferenced.
9482 mutex_enter(&spa_namespace_lock
);
9483 while ((spa
= spa_next(NULL
)) != NULL
) {
9485 * Stop async tasks. The async thread may need to detach
9486 * a device that's been replaced, which requires grabbing
9487 * spa_namespace_lock, so we must drop it here.
9489 spa_open_ref(spa
, FTAG
);
9490 mutex_exit(&spa_namespace_lock
);
9491 spa_async_suspend(spa
);
9492 mutex_enter(&spa_namespace_lock
);
9493 spa_close(spa
, FTAG
);
9495 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
9497 spa_deactivate(spa
);
9501 mutex_exit(&spa_namespace_lock
);
9505 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
9510 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
9514 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
9515 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
9516 if (vd
->vdev_guid
== guid
)
9520 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
9521 vd
= spa
->spa_spares
.sav_vdevs
[i
];
9522 if (vd
->vdev_guid
== guid
)
9531 spa_upgrade(spa_t
*spa
, uint64_t version
)
9533 ASSERT(spa_writeable(spa
));
9535 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
9538 * This should only be called for a non-faulted pool, and since a
9539 * future version would result in an unopenable pool, this shouldn't be
9542 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
9543 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
9545 spa
->spa_uberblock
.ub_version
= version
;
9546 vdev_config_dirty(spa
->spa_root_vdev
);
9548 spa_config_exit(spa
, SCL_ALL
, FTAG
);
9550 txg_wait_synced(spa_get_dsl(spa
), 0);
9554 spa_has_aux_vdev(spa_t
*spa
, uint64_t guid
, spa_aux_vdev_t
*sav
)
9560 for (i
= 0; i
< sav
->sav_count
; i
++)
9561 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
9564 for (i
= 0; i
< sav
->sav_npending
; i
++) {
9565 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
9566 &vdev_guid
) == 0 && vdev_guid
== guid
)
9574 spa_has_l2cache(spa_t
*spa
, uint64_t guid
)
9576 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_l2cache
));
9580 spa_has_spare(spa_t
*spa
, uint64_t guid
)
9582 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_spares
));
9586 * Check if a pool has an active shared spare device.
9587 * Note: reference count of an active spare is 2, as a spare and as a replace
9590 spa_has_active_shared_spare(spa_t
*spa
)
9594 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
9596 for (i
= 0; i
< sav
->sav_count
; i
++) {
9597 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
9598 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
9607 spa_total_metaslabs(spa_t
*spa
)
9609 vdev_t
*rvd
= spa
->spa_root_vdev
;
9612 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
9613 vdev_t
*vd
= rvd
->vdev_child
[c
];
9614 if (!vdev_is_concrete(vd
))
9616 m
+= vd
->vdev_ms_count
;
9622 * Notify any waiting threads that some activity has switched from being in-
9623 * progress to not-in-progress so that the thread can wake up and determine
9624 * whether it is finished waiting.
9627 spa_notify_waiters(spa_t
*spa
)
9630 * Acquiring spa_activities_lock here prevents the cv_broadcast from
9631 * happening between the waiting thread's check and cv_wait.
9633 mutex_enter(&spa
->spa_activities_lock
);
9634 cv_broadcast(&spa
->spa_activities_cv
);
9635 mutex_exit(&spa
->spa_activities_lock
);
9639 * Notify any waiting threads that the pool is exporting, and then block until
9640 * they are finished using the spa_t.
9643 spa_wake_waiters(spa_t
*spa
)
9645 mutex_enter(&spa
->spa_activities_lock
);
9646 spa
->spa_waiters_cancel
= B_TRUE
;
9647 cv_broadcast(&spa
->spa_activities_cv
);
9648 while (spa
->spa_waiters
!= 0)
9649 cv_wait(&spa
->spa_waiters_cv
, &spa
->spa_activities_lock
);
9650 spa
->spa_waiters_cancel
= B_FALSE
;
9651 mutex_exit(&spa
->spa_activities_lock
);
9654 /* Whether the vdev or any of its descendants are being initialized/trimmed. */
9656 spa_vdev_activity_in_progress_impl(vdev_t
*vd
, zpool_wait_activity_t activity
)
9658 spa_t
*spa
= vd
->vdev_spa
;
9660 ASSERT(spa_config_held(spa
, SCL_CONFIG
| SCL_STATE
, RW_READER
));
9661 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
9662 ASSERT(activity
== ZPOOL_WAIT_INITIALIZE
||
9663 activity
== ZPOOL_WAIT_TRIM
);
9665 kmutex_t
*lock
= activity
== ZPOOL_WAIT_INITIALIZE
?
9666 &vd
->vdev_initialize_lock
: &vd
->vdev_trim_lock
;
9668 mutex_exit(&spa
->spa_activities_lock
);
9670 mutex_enter(&spa
->spa_activities_lock
);
9672 boolean_t in_progress
= (activity
== ZPOOL_WAIT_INITIALIZE
) ?
9673 (vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
) :
9674 (vd
->vdev_trim_state
== VDEV_TRIM_ACTIVE
);
9680 for (int i
= 0; i
< vd
->vdev_children
; i
++) {
9681 if (spa_vdev_activity_in_progress_impl(vd
->vdev_child
[i
],
9690 * If use_guid is true, this checks whether the vdev specified by guid is
9691 * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool
9692 * is being initialized/trimmed. The caller must hold the config lock and
9693 * spa_activities_lock.
9696 spa_vdev_activity_in_progress(spa_t
*spa
, boolean_t use_guid
, uint64_t guid
,
9697 zpool_wait_activity_t activity
, boolean_t
*in_progress
)
9699 mutex_exit(&spa
->spa_activities_lock
);
9700 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9701 mutex_enter(&spa
->spa_activities_lock
);
9705 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
9706 if (vd
== NULL
|| !vd
->vdev_ops
->vdev_op_leaf
) {
9707 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9711 vd
= spa
->spa_root_vdev
;
9714 *in_progress
= spa_vdev_activity_in_progress_impl(vd
, activity
);
9716 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9721 * Locking for waiting threads
9722 * ---------------------------
9724 * Waiting threads need a way to check whether a given activity is in progress,
9725 * and then, if it is, wait for it to complete. Each activity will have some
9726 * in-memory representation of the relevant on-disk state which can be used to
9727 * determine whether or not the activity is in progress. The in-memory state and
9728 * the locking used to protect it will be different for each activity, and may
9729 * not be suitable for use with a cvar (e.g., some state is protected by the
9730 * config lock). To allow waiting threads to wait without any races, another
9731 * lock, spa_activities_lock, is used.
9733 * When the state is checked, both the activity-specific lock (if there is one)
9734 * and spa_activities_lock are held. In some cases, the activity-specific lock
9735 * is acquired explicitly (e.g. the config lock). In others, the locking is
9736 * internal to some check (e.g. bpobj_is_empty). After checking, the waiting
9737 * thread releases the activity-specific lock and, if the activity is in
9738 * progress, then cv_waits using spa_activities_lock.
9740 * The waiting thread is woken when another thread, one completing some
9741 * activity, updates the state of the activity and then calls
9742 * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only
9743 * needs to hold its activity-specific lock when updating the state, and this
9744 * lock can (but doesn't have to) be dropped before calling spa_notify_waiters.
9746 * Because spa_notify_waiters acquires spa_activities_lock before broadcasting,
9747 * and because it is held when the waiting thread checks the state of the
9748 * activity, it can never be the case that the completing thread both updates
9749 * the activity state and cv_broadcasts in between the waiting thread's check
9750 * and cv_wait. Thus, a waiting thread can never miss a wakeup.
9752 * In order to prevent deadlock, when the waiting thread does its check, in some
9753 * cases it will temporarily drop spa_activities_lock in order to acquire the
9754 * activity-specific lock. The order in which spa_activities_lock and the
9755 * activity specific lock are acquired in the waiting thread is determined by
9756 * the order in which they are acquired in the completing thread; if the
9757 * completing thread calls spa_notify_waiters with the activity-specific lock
9758 * held, then the waiting thread must also acquire the activity-specific lock
9763 spa_activity_in_progress(spa_t
*spa
, zpool_wait_activity_t activity
,
9764 boolean_t use_tag
, uint64_t tag
, boolean_t
*in_progress
)
9768 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
9771 case ZPOOL_WAIT_CKPT_DISCARD
:
9773 (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
) &&
9774 zap_contains(spa_meta_objset(spa
),
9775 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_ZPOOL_CHECKPOINT
) ==
9778 case ZPOOL_WAIT_FREE
:
9779 *in_progress
= ((spa_version(spa
) >= SPA_VERSION_DEADLISTS
&&
9780 !bpobj_is_empty(&spa
->spa_dsl_pool
->dp_free_bpobj
)) ||
9781 spa_feature_is_active(spa
, SPA_FEATURE_ASYNC_DESTROY
) ||
9782 spa_livelist_delete_check(spa
));
9784 case ZPOOL_WAIT_INITIALIZE
:
9785 case ZPOOL_WAIT_TRIM
:
9786 error
= spa_vdev_activity_in_progress(spa
, use_tag
, tag
,
9787 activity
, in_progress
);
9789 case ZPOOL_WAIT_REPLACE
:
9790 mutex_exit(&spa
->spa_activities_lock
);
9791 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9792 mutex_enter(&spa
->spa_activities_lock
);
9794 *in_progress
= vdev_replace_in_progress(spa
->spa_root_vdev
);
9795 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9797 case ZPOOL_WAIT_REMOVE
:
9798 *in_progress
= (spa
->spa_removing_phys
.sr_state
==
9801 case ZPOOL_WAIT_RESILVER
:
9802 if ((*in_progress
= vdev_rebuild_active(spa
->spa_root_vdev
)))
9805 case ZPOOL_WAIT_SCRUB
:
9807 boolean_t scanning
, paused
, is_scrub
;
9808 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
9810 is_scrub
= (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
);
9811 scanning
= (scn
->scn_phys
.scn_state
== DSS_SCANNING
);
9812 paused
= dsl_scan_is_paused_scrub(scn
);
9813 *in_progress
= (scanning
&& !paused
&&
9814 is_scrub
== (activity
== ZPOOL_WAIT_SCRUB
));
9818 panic("unrecognized value for activity %d", activity
);
9825 spa_wait_common(const char *pool
, zpool_wait_activity_t activity
,
9826 boolean_t use_tag
, uint64_t tag
, boolean_t
*waited
)
9829 * The tag is used to distinguish between instances of an activity.
9830 * 'initialize' and 'trim' are the only activities that we use this for.
9831 * The other activities can only have a single instance in progress in a
9832 * pool at one time, making the tag unnecessary.
9834 * There can be multiple devices being replaced at once, but since they
9835 * all finish once resilvering finishes, we don't bother keeping track
9836 * of them individually, we just wait for them all to finish.
9838 if (use_tag
&& activity
!= ZPOOL_WAIT_INITIALIZE
&&
9839 activity
!= ZPOOL_WAIT_TRIM
)
9842 if (activity
< 0 || activity
>= ZPOOL_WAIT_NUM_ACTIVITIES
)
9846 int error
= spa_open(pool
, &spa
, FTAG
);
9851 * Increment the spa's waiter count so that we can call spa_close and
9852 * still ensure that the spa_t doesn't get freed before this thread is
9853 * finished with it when the pool is exported. We want to call spa_close
9854 * before we start waiting because otherwise the additional ref would
9855 * prevent the pool from being exported or destroyed throughout the
9856 * potentially long wait.
9858 mutex_enter(&spa
->spa_activities_lock
);
9860 spa_close(spa
, FTAG
);
9864 boolean_t in_progress
;
9865 error
= spa_activity_in_progress(spa
, activity
, use_tag
, tag
,
9868 if (error
|| !in_progress
|| spa
->spa_waiters_cancel
)
9873 if (cv_wait_sig(&spa
->spa_activities_cv
,
9874 &spa
->spa_activities_lock
) == 0) {
9881 cv_signal(&spa
->spa_waiters_cv
);
9882 mutex_exit(&spa
->spa_activities_lock
);
9888 * Wait for a particular instance of the specified activity to complete, where
9889 * the instance is identified by 'tag'
9892 spa_wait_tag(const char *pool
, zpool_wait_activity_t activity
, uint64_t tag
,
9895 return (spa_wait_common(pool
, activity
, B_TRUE
, tag
, waited
));
9899 * Wait for all instances of the specified activity complete
9902 spa_wait(const char *pool
, zpool_wait_activity_t activity
, boolean_t
*waited
)
9905 return (spa_wait_common(pool
, activity
, B_FALSE
, 0, waited
));
9909 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
9911 sysevent_t
*ev
= NULL
;
9915 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
9917 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
9918 ev
->resource
= resource
;
9921 (void) spa
, (void) vd
, (void) hist_nvl
, (void) name
;
9927 spa_event_post(sysevent_t
*ev
)
9931 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
9932 kmem_free(ev
, sizeof (*ev
));
9940 * Post a zevent corresponding to the given sysevent. The 'name' must be one
9941 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
9942 * filled in from the spa and (optionally) the vdev. This doesn't do anything
9943 * in the userland libzpool, as we don't want consumers to misinterpret ztest
9944 * or zdb as real changes.
9947 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
9949 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
9952 /* state manipulation functions */
9953 EXPORT_SYMBOL(spa_open
);
9954 EXPORT_SYMBOL(spa_open_rewind
);
9955 EXPORT_SYMBOL(spa_get_stats
);
9956 EXPORT_SYMBOL(spa_create
);
9957 EXPORT_SYMBOL(spa_import
);
9958 EXPORT_SYMBOL(spa_tryimport
);
9959 EXPORT_SYMBOL(spa_destroy
);
9960 EXPORT_SYMBOL(spa_export
);
9961 EXPORT_SYMBOL(spa_reset
);
9962 EXPORT_SYMBOL(spa_async_request
);
9963 EXPORT_SYMBOL(spa_async_suspend
);
9964 EXPORT_SYMBOL(spa_async_resume
);
9965 EXPORT_SYMBOL(spa_inject_addref
);
9966 EXPORT_SYMBOL(spa_inject_delref
);
9967 EXPORT_SYMBOL(spa_scan_stat_init
);
9968 EXPORT_SYMBOL(spa_scan_get_stats
);
9970 /* device manipulation */
9971 EXPORT_SYMBOL(spa_vdev_add
);
9972 EXPORT_SYMBOL(spa_vdev_attach
);
9973 EXPORT_SYMBOL(spa_vdev_detach
);
9974 EXPORT_SYMBOL(spa_vdev_setpath
);
9975 EXPORT_SYMBOL(spa_vdev_setfru
);
9976 EXPORT_SYMBOL(spa_vdev_split_mirror
);
9978 /* spare statech is global across all pools) */
9979 EXPORT_SYMBOL(spa_spare_add
);
9980 EXPORT_SYMBOL(spa_spare_remove
);
9981 EXPORT_SYMBOL(spa_spare_exists
);
9982 EXPORT_SYMBOL(spa_spare_activate
);
9984 /* L2ARC statech is global across all pools) */
9985 EXPORT_SYMBOL(spa_l2cache_add
);
9986 EXPORT_SYMBOL(spa_l2cache_remove
);
9987 EXPORT_SYMBOL(spa_l2cache_exists
);
9988 EXPORT_SYMBOL(spa_l2cache_activate
);
9989 EXPORT_SYMBOL(spa_l2cache_drop
);
9992 EXPORT_SYMBOL(spa_scan
);
9993 EXPORT_SYMBOL(spa_scan_stop
);
9996 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
9997 EXPORT_SYMBOL(spa_sync_allpools
);
10000 EXPORT_SYMBOL(spa_prop_set
);
10001 EXPORT_SYMBOL(spa_prop_get
);
10002 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
10004 /* asynchronous event notification */
10005 EXPORT_SYMBOL(spa_event_notify
);
10007 /* BEGIN CSTYLED */
10008 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_shift
, UINT
, ZMOD_RW
,
10009 "log2 fraction of arc that can be used by inflight I/Os when "
10010 "verifying pool during import");
10013 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_metadata
, INT
, ZMOD_RW
,
10014 "Set to traverse metadata on pool import");
10016 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_data
, INT
, ZMOD_RW
,
10017 "Set to traverse data on pool import");
10019 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_print_vdev_tree
, INT
, ZMOD_RW
,
10020 "Print vdev tree to zfs_dbgmsg during pool import");
10022 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_pct
, UINT
, ZMOD_RD
,
10023 "Percentage of CPUs to run an IO worker thread");
10025 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_tpq
, UINT
, ZMOD_RD
,
10026 "Number of threads per IO worker taskqueue");
10028 /* BEGIN CSTYLED */
10029 ZFS_MODULE_PARAM(zfs
, zfs_
, max_missing_tvds
, U64
, ZMOD_RW
,
10030 "Allow importing pool with up to this number of missing top-level "
10031 "vdevs (in read-only mode)");
10034 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_pause
, INT
,
10035 ZMOD_RW
, "Set the livelist condense zthr to pause");
10037 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_pause
, INT
,
10038 ZMOD_RW
, "Set the livelist condense synctask to pause");
10040 /* BEGIN CSTYLED */
10041 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_cancel
,
10043 "Whether livelist condensing was canceled in the synctask");
10045 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_cancel
,
10047 "Whether livelist condensing was canceled in the zthr function");
10049 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, new_alloc
, INT
,
10051 "Whether extra ALLOC blkptrs were added to a livelist entry while it "
10052 "was being condensed");