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, 2021 by Delphix. All rights reserved.
25 * Copyright 2017 Nexenta Systems, Inc.
26 * Copyright (c) 2014 Integros [integros.com]
27 * Copyright 2016 Toomas Soome <tsoome@me.com>
28 * Copyright 2017 Joyent, Inc.
29 * Copyright (c) 2017, Intel Corporation.
30 * Copyright (c) 2019, Datto Inc. All rights reserved.
31 * Copyright (c) 2021, Klara Inc.
32 * Copyright (c) 2021, 2023 Hewlett Packard Enterprise Development LP.
35 #include <sys/zfs_context.h>
36 #include <sys/fm/fs/zfs.h>
38 #include <sys/spa_impl.h>
39 #include <sys/bpobj.h>
41 #include <sys/dmu_tx.h>
42 #include <sys/dsl_dir.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/vdev_rebuild.h>
45 #include <sys/vdev_draid.h>
46 #include <sys/uberblock_impl.h>
47 #include <sys/metaslab.h>
48 #include <sys/metaslab_impl.h>
49 #include <sys/space_map.h>
50 #include <sys/space_reftree.h>
53 #include <sys/fs/zfs.h>
56 #include <sys/dsl_scan.h>
57 #include <sys/vdev_raidz.h>
59 #include <sys/vdev_initialize.h>
60 #include <sys/vdev_trim.h>
62 #include <sys/zfs_ratelimit.h>
66 * One metaslab from each (normal-class) vdev is used by the ZIL. These are
67 * called "embedded slog metaslabs", are referenced by vdev_log_mg, and are
68 * part of the spa_embedded_log_class. The metaslab with the most free space
69 * in each vdev is selected for this purpose when the pool is opened (or a
70 * vdev is added). See vdev_metaslab_init().
72 * Log blocks can be allocated from the following locations. Each one is tried
73 * in order until the allocation succeeds:
74 * 1. dedicated log vdevs, aka "slog" (spa_log_class)
75 * 2. embedded slog metaslabs (spa_embedded_log_class)
76 * 3. other metaslabs in normal vdevs (spa_normal_class)
78 * zfs_embedded_slog_min_ms disables the embedded slog if there are fewer
79 * than this number of metaslabs in the vdev. This ensures that we don't set
80 * aside an unreasonable amount of space for the ZIL. If set to less than
81 * 1 << (spa_slop_shift + 1), on small pools the usable space may be reduced
82 * (by more than 1<<spa_slop_shift) due to the embedded slog metaslab.
84 static uint_t zfs_embedded_slog_min_ms
= 64;
86 /* default target for number of metaslabs per top-level vdev */
87 static uint_t zfs_vdev_default_ms_count
= 200;
89 /* minimum number of metaslabs per top-level vdev */
90 static uint_t zfs_vdev_min_ms_count
= 16;
92 /* practical upper limit of total metaslabs per top-level vdev */
93 static uint_t zfs_vdev_ms_count_limit
= 1ULL << 17;
95 /* lower limit for metaslab size (512M) */
96 static uint_t zfs_vdev_default_ms_shift
= 29;
98 /* upper limit for metaslab size (16G) */
99 static uint_t zfs_vdev_max_ms_shift
= 34;
101 int vdev_validate_skip
= B_FALSE
;
104 * Since the DTL space map of a vdev is not expected to have a lot of
105 * entries, we default its block size to 4K.
107 int zfs_vdev_dtl_sm_blksz
= (1 << 12);
110 * Rate limit slow IO (delay) events to this many per second.
112 static unsigned int zfs_slow_io_events_per_second
= 20;
115 * Rate limit checksum events after this many checksum errors per second.
117 static unsigned int zfs_checksum_events_per_second
= 20;
120 * Ignore errors during scrub/resilver. Allows to work around resilver
121 * upon import when there are pool errors.
123 static int zfs_scan_ignore_errors
= 0;
126 * vdev-wide space maps that have lots of entries written to them at
127 * the end of each transaction can benefit from a higher I/O bandwidth
128 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
130 int zfs_vdev_standard_sm_blksz
= (1 << 17);
133 * Tunable parameter for debugging or performance analysis. Setting this
134 * will cause pool corruption on power loss if a volatile out-of-order
135 * write cache is enabled.
137 int zfs_nocacheflush
= 0;
140 * Maximum and minimum ashift values that can be automatically set based on
141 * vdev's physical ashift (disk's physical sector size). While ASHIFT_MAX
142 * is higher than the maximum value, it is intentionally limited here to not
143 * excessively impact pool space efficiency. Higher ashift values may still
144 * be forced by vdev logical ashift or by user via ashift property, but won't
145 * be set automatically as a performance optimization.
147 uint_t zfs_vdev_max_auto_ashift
= 14;
148 uint_t zfs_vdev_min_auto_ashift
= ASHIFT_MIN
;
151 vdev_dbgmsg(vdev_t
*vd
, const char *fmt
, ...)
157 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
160 if (vd
->vdev_path
!= NULL
) {
161 zfs_dbgmsg("%s vdev '%s': %s", vd
->vdev_ops
->vdev_op_type
,
164 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
165 vd
->vdev_ops
->vdev_op_type
,
166 (u_longlong_t
)vd
->vdev_id
,
167 (u_longlong_t
)vd
->vdev_guid
, buf
);
172 vdev_dbgmsg_print_tree(vdev_t
*vd
, int indent
)
176 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
) {
177 zfs_dbgmsg("%*svdev %llu: %s", indent
, "",
178 (u_longlong_t
)vd
->vdev_id
,
179 vd
->vdev_ops
->vdev_op_type
);
183 switch (vd
->vdev_state
) {
184 case VDEV_STATE_UNKNOWN
:
185 (void) snprintf(state
, sizeof (state
), "unknown");
187 case VDEV_STATE_CLOSED
:
188 (void) snprintf(state
, sizeof (state
), "closed");
190 case VDEV_STATE_OFFLINE
:
191 (void) snprintf(state
, sizeof (state
), "offline");
193 case VDEV_STATE_REMOVED
:
194 (void) snprintf(state
, sizeof (state
), "removed");
196 case VDEV_STATE_CANT_OPEN
:
197 (void) snprintf(state
, sizeof (state
), "can't open");
199 case VDEV_STATE_FAULTED
:
200 (void) snprintf(state
, sizeof (state
), "faulted");
202 case VDEV_STATE_DEGRADED
:
203 (void) snprintf(state
, sizeof (state
), "degraded");
205 case VDEV_STATE_HEALTHY
:
206 (void) snprintf(state
, sizeof (state
), "healthy");
209 (void) snprintf(state
, sizeof (state
), "<state %u>",
210 (uint_t
)vd
->vdev_state
);
213 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent
,
214 "", (int)vd
->vdev_id
, vd
->vdev_ops
->vdev_op_type
,
215 vd
->vdev_islog
? " (log)" : "",
216 (u_longlong_t
)vd
->vdev_guid
,
217 vd
->vdev_path
? vd
->vdev_path
: "N/A", state
);
219 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++)
220 vdev_dbgmsg_print_tree(vd
->vdev_child
[i
], indent
+ 2);
224 * Virtual device management.
227 static vdev_ops_t
*const vdev_ops_table
[] = {
231 &vdev_draid_spare_ops
,
244 * Given a vdev type, return the appropriate ops vector.
247 vdev_getops(const char *type
)
249 vdev_ops_t
*ops
, *const *opspp
;
251 for (opspp
= vdev_ops_table
; (ops
= *opspp
) != NULL
; opspp
++)
252 if (strcmp(ops
->vdev_op_type
, type
) == 0)
259 * Given a vdev and a metaslab class, find which metaslab group we're
260 * interested in. All vdevs may belong to two different metaslab classes.
261 * Dedicated slog devices use only the primary metaslab group, rather than a
262 * separate log group. For embedded slogs, the vdev_log_mg will be non-NULL.
265 vdev_get_mg(vdev_t
*vd
, metaslab_class_t
*mc
)
267 if (mc
== spa_embedded_log_class(vd
->vdev_spa
) &&
268 vd
->vdev_log_mg
!= NULL
)
269 return (vd
->vdev_log_mg
);
271 return (vd
->vdev_mg
);
275 vdev_default_xlate(vdev_t
*vd
, const range_seg64_t
*logical_rs
,
276 range_seg64_t
*physical_rs
, range_seg64_t
*remain_rs
)
278 (void) vd
, (void) remain_rs
;
280 physical_rs
->rs_start
= logical_rs
->rs_start
;
281 physical_rs
->rs_end
= logical_rs
->rs_end
;
285 * Derive the enumerated allocation bias from string input.
286 * String origin is either the per-vdev zap or zpool(8).
288 static vdev_alloc_bias_t
289 vdev_derive_alloc_bias(const char *bias
)
291 vdev_alloc_bias_t alloc_bias
= VDEV_BIAS_NONE
;
293 if (strcmp(bias
, VDEV_ALLOC_BIAS_LOG
) == 0)
294 alloc_bias
= VDEV_BIAS_LOG
;
295 else if (strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
296 alloc_bias
= VDEV_BIAS_SPECIAL
;
297 else if (strcmp(bias
, VDEV_ALLOC_BIAS_DEDUP
) == 0)
298 alloc_bias
= VDEV_BIAS_DEDUP
;
304 * Default asize function: return the MAX of psize with the asize of
305 * all children. This is what's used by anything other than RAID-Z.
308 vdev_default_asize(vdev_t
*vd
, uint64_t psize
)
310 uint64_t asize
= P2ROUNDUP(psize
, 1ULL << vd
->vdev_top
->vdev_ashift
);
313 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
314 csize
= vdev_psize_to_asize(vd
->vdev_child
[c
], psize
);
315 asize
= MAX(asize
, csize
);
322 vdev_default_min_asize(vdev_t
*vd
)
324 return (vd
->vdev_min_asize
);
328 * Get the minimum allocatable size. We define the allocatable size as
329 * the vdev's asize rounded to the nearest metaslab. This allows us to
330 * replace or attach devices which don't have the same physical size but
331 * can still satisfy the same number of allocations.
334 vdev_get_min_asize(vdev_t
*vd
)
336 vdev_t
*pvd
= vd
->vdev_parent
;
339 * If our parent is NULL (inactive spare or cache) or is the root,
340 * just return our own asize.
343 return (vd
->vdev_asize
);
346 * The top-level vdev just returns the allocatable size rounded
347 * to the nearest metaslab.
349 if (vd
== vd
->vdev_top
)
350 return (P2ALIGN(vd
->vdev_asize
, 1ULL << vd
->vdev_ms_shift
));
352 return (pvd
->vdev_ops
->vdev_op_min_asize(pvd
));
356 vdev_set_min_asize(vdev_t
*vd
)
358 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
360 for (int c
= 0; c
< vd
->vdev_children
; c
++)
361 vdev_set_min_asize(vd
->vdev_child
[c
]);
365 * Get the minimal allocation size for the top-level vdev.
368 vdev_get_min_alloc(vdev_t
*vd
)
370 uint64_t min_alloc
= 1ULL << vd
->vdev_ashift
;
372 if (vd
->vdev_ops
->vdev_op_min_alloc
!= NULL
)
373 min_alloc
= vd
->vdev_ops
->vdev_op_min_alloc(vd
);
379 * Get the parity level for a top-level vdev.
382 vdev_get_nparity(vdev_t
*vd
)
384 uint64_t nparity
= 0;
386 if (vd
->vdev_ops
->vdev_op_nparity
!= NULL
)
387 nparity
= vd
->vdev_ops
->vdev_op_nparity(vd
);
393 vdev_prop_get_int(vdev_t
*vd
, vdev_prop_t prop
, uint64_t *value
)
395 spa_t
*spa
= vd
->vdev_spa
;
396 objset_t
*mos
= spa
->spa_meta_objset
;
400 if (vd
->vdev_root_zap
!= 0) {
401 objid
= vd
->vdev_root_zap
;
402 } else if (vd
->vdev_top_zap
!= 0) {
403 objid
= vd
->vdev_top_zap
;
404 } else if (vd
->vdev_leaf_zap
!= 0) {
405 objid
= vd
->vdev_leaf_zap
;
410 err
= zap_lookup(mos
, objid
, vdev_prop_to_name(prop
),
411 sizeof (uint64_t), 1, value
);
414 *value
= vdev_prop_default_numeric(prop
);
420 * Get the number of data disks for a top-level vdev.
423 vdev_get_ndisks(vdev_t
*vd
)
427 if (vd
->vdev_ops
->vdev_op_ndisks
!= NULL
)
428 ndisks
= vd
->vdev_ops
->vdev_op_ndisks(vd
);
434 vdev_lookup_top(spa_t
*spa
, uint64_t vdev
)
436 vdev_t
*rvd
= spa
->spa_root_vdev
;
438 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
440 if (vdev
< rvd
->vdev_children
) {
441 ASSERT(rvd
->vdev_child
[vdev
] != NULL
);
442 return (rvd
->vdev_child
[vdev
]);
449 vdev_lookup_by_guid(vdev_t
*vd
, uint64_t guid
)
453 if (vd
->vdev_guid
== guid
)
456 for (int c
= 0; c
< vd
->vdev_children
; c
++)
457 if ((mvd
= vdev_lookup_by_guid(vd
->vdev_child
[c
], guid
)) !=
465 vdev_count_leaves_impl(vdev_t
*vd
)
469 if (vd
->vdev_ops
->vdev_op_leaf
)
472 for (int c
= 0; c
< vd
->vdev_children
; c
++)
473 n
+= vdev_count_leaves_impl(vd
->vdev_child
[c
]);
479 vdev_count_leaves(spa_t
*spa
)
483 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
484 rc
= vdev_count_leaves_impl(spa
->spa_root_vdev
);
485 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
491 vdev_add_child(vdev_t
*pvd
, vdev_t
*cvd
)
493 size_t oldsize
, newsize
;
494 uint64_t id
= cvd
->vdev_id
;
497 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
498 ASSERT(cvd
->vdev_parent
== NULL
);
500 cvd
->vdev_parent
= pvd
;
505 ASSERT(id
>= pvd
->vdev_children
|| pvd
->vdev_child
[id
] == NULL
);
507 oldsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
508 pvd
->vdev_children
= MAX(pvd
->vdev_children
, id
+ 1);
509 newsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
511 newchild
= kmem_alloc(newsize
, KM_SLEEP
);
512 if (pvd
->vdev_child
!= NULL
) {
513 memcpy(newchild
, pvd
->vdev_child
, oldsize
);
514 kmem_free(pvd
->vdev_child
, oldsize
);
517 pvd
->vdev_child
= newchild
;
518 pvd
->vdev_child
[id
] = cvd
;
520 cvd
->vdev_top
= (pvd
->vdev_top
? pvd
->vdev_top
: cvd
);
521 ASSERT(cvd
->vdev_top
->vdev_parent
->vdev_parent
== NULL
);
524 * Walk up all ancestors to update guid sum.
526 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
527 pvd
->vdev_guid_sum
+= cvd
->vdev_guid_sum
;
529 if (cvd
->vdev_ops
->vdev_op_leaf
) {
530 list_insert_head(&cvd
->vdev_spa
->spa_leaf_list
, cvd
);
531 cvd
->vdev_spa
->spa_leaf_list_gen
++;
536 vdev_remove_child(vdev_t
*pvd
, vdev_t
*cvd
)
539 uint_t id
= cvd
->vdev_id
;
541 ASSERT(cvd
->vdev_parent
== pvd
);
546 ASSERT(id
< pvd
->vdev_children
);
547 ASSERT(pvd
->vdev_child
[id
] == cvd
);
549 pvd
->vdev_child
[id
] = NULL
;
550 cvd
->vdev_parent
= NULL
;
552 for (c
= 0; c
< pvd
->vdev_children
; c
++)
553 if (pvd
->vdev_child
[c
])
556 if (c
== pvd
->vdev_children
) {
557 kmem_free(pvd
->vdev_child
, c
* sizeof (vdev_t
*));
558 pvd
->vdev_child
= NULL
;
559 pvd
->vdev_children
= 0;
562 if (cvd
->vdev_ops
->vdev_op_leaf
) {
563 spa_t
*spa
= cvd
->vdev_spa
;
564 list_remove(&spa
->spa_leaf_list
, cvd
);
565 spa
->spa_leaf_list_gen
++;
569 * Walk up all ancestors to update guid sum.
571 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
572 pvd
->vdev_guid_sum
-= cvd
->vdev_guid_sum
;
576 * Remove any holes in the child array.
579 vdev_compact_children(vdev_t
*pvd
)
581 vdev_t
**newchild
, *cvd
;
582 int oldc
= pvd
->vdev_children
;
585 ASSERT(spa_config_held(pvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
590 for (int c
= newc
= 0; c
< oldc
; c
++)
591 if (pvd
->vdev_child
[c
])
595 newchild
= kmem_zalloc(newc
* sizeof (vdev_t
*), KM_SLEEP
);
597 for (int c
= newc
= 0; c
< oldc
; c
++) {
598 if ((cvd
= pvd
->vdev_child
[c
]) != NULL
) {
599 newchild
[newc
] = cvd
;
600 cvd
->vdev_id
= newc
++;
607 kmem_free(pvd
->vdev_child
, oldc
* sizeof (vdev_t
*));
608 pvd
->vdev_child
= newchild
;
609 pvd
->vdev_children
= newc
;
613 * Allocate and minimally initialize a vdev_t.
616 vdev_alloc_common(spa_t
*spa
, uint_t id
, uint64_t guid
, vdev_ops_t
*ops
)
619 vdev_indirect_config_t
*vic
;
621 vd
= kmem_zalloc(sizeof (vdev_t
), KM_SLEEP
);
622 vic
= &vd
->vdev_indirect_config
;
624 if (spa
->spa_root_vdev
== NULL
) {
625 ASSERT(ops
== &vdev_root_ops
);
626 spa
->spa_root_vdev
= vd
;
627 spa
->spa_load_guid
= spa_generate_guid(NULL
);
630 if (guid
== 0 && ops
!= &vdev_hole_ops
) {
631 if (spa
->spa_root_vdev
== vd
) {
633 * The root vdev's guid will also be the pool guid,
634 * which must be unique among all pools.
636 guid
= spa_generate_guid(NULL
);
639 * Any other vdev's guid must be unique within the pool.
641 guid
= spa_generate_guid(spa
);
643 ASSERT(!spa_guid_exists(spa_guid(spa
), guid
));
648 vd
->vdev_guid
= guid
;
649 vd
->vdev_guid_sum
= guid
;
651 vd
->vdev_state
= VDEV_STATE_CLOSED
;
652 vd
->vdev_ishole
= (ops
== &vdev_hole_ops
);
653 vic
->vic_prev_indirect_vdev
= UINT64_MAX
;
655 rw_init(&vd
->vdev_indirect_rwlock
, NULL
, RW_DEFAULT
, NULL
);
656 mutex_init(&vd
->vdev_obsolete_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
657 vd
->vdev_obsolete_segments
= range_tree_create(NULL
, RANGE_SEG64
, NULL
,
661 * Initialize rate limit structs for events. We rate limit ZIO delay
662 * and checksum events so that we don't overwhelm ZED with thousands
663 * of events when a disk is acting up.
665 zfs_ratelimit_init(&vd
->vdev_delay_rl
, &zfs_slow_io_events_per_second
,
667 zfs_ratelimit_init(&vd
->vdev_deadman_rl
, &zfs_slow_io_events_per_second
,
669 zfs_ratelimit_init(&vd
->vdev_checksum_rl
,
670 &zfs_checksum_events_per_second
, 1);
673 * Default Thresholds for tuning ZED
675 vd
->vdev_checksum_n
= vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_N
);
676 vd
->vdev_checksum_t
= vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_T
);
677 vd
->vdev_io_n
= vdev_prop_default_numeric(VDEV_PROP_IO_N
);
678 vd
->vdev_io_t
= vdev_prop_default_numeric(VDEV_PROP_IO_T
);
680 list_link_init(&vd
->vdev_config_dirty_node
);
681 list_link_init(&vd
->vdev_state_dirty_node
);
682 list_link_init(&vd
->vdev_initialize_node
);
683 list_link_init(&vd
->vdev_leaf_node
);
684 list_link_init(&vd
->vdev_trim_node
);
686 mutex_init(&vd
->vdev_dtl_lock
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
687 mutex_init(&vd
->vdev_stat_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
688 mutex_init(&vd
->vdev_probe_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
689 mutex_init(&vd
->vdev_scan_io_queue_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
691 mutex_init(&vd
->vdev_initialize_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
692 mutex_init(&vd
->vdev_initialize_io_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
693 cv_init(&vd
->vdev_initialize_cv
, NULL
, CV_DEFAULT
, NULL
);
694 cv_init(&vd
->vdev_initialize_io_cv
, NULL
, CV_DEFAULT
, NULL
);
696 mutex_init(&vd
->vdev_trim_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
697 mutex_init(&vd
->vdev_autotrim_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
698 mutex_init(&vd
->vdev_trim_io_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
699 cv_init(&vd
->vdev_trim_cv
, NULL
, CV_DEFAULT
, NULL
);
700 cv_init(&vd
->vdev_autotrim_cv
, NULL
, CV_DEFAULT
, NULL
);
701 cv_init(&vd
->vdev_autotrim_kick_cv
, NULL
, CV_DEFAULT
, NULL
);
702 cv_init(&vd
->vdev_trim_io_cv
, NULL
, CV_DEFAULT
, NULL
);
704 mutex_init(&vd
->vdev_rebuild_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
705 cv_init(&vd
->vdev_rebuild_cv
, NULL
, CV_DEFAULT
, NULL
);
707 for (int t
= 0; t
< DTL_TYPES
; t
++) {
708 vd
->vdev_dtl
[t
] = range_tree_create(NULL
, RANGE_SEG64
, NULL
, 0,
712 txg_list_create(&vd
->vdev_ms_list
, spa
,
713 offsetof(struct metaslab
, ms_txg_node
));
714 txg_list_create(&vd
->vdev_dtl_list
, spa
,
715 offsetof(struct vdev
, vdev_dtl_node
));
716 vd
->vdev_stat
.vs_timestamp
= gethrtime();
723 * Allocate a new vdev. The 'alloctype' is used to control whether we are
724 * creating a new vdev or loading an existing one - the behavior is slightly
725 * different for each case.
728 vdev_alloc(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
, uint_t id
,
733 uint64_t guid
= 0, islog
;
735 vdev_indirect_config_t
*vic
;
736 const char *tmp
= NULL
;
738 vdev_alloc_bias_t alloc_bias
= VDEV_BIAS_NONE
;
739 boolean_t top_level
= (parent
&& !parent
->vdev_parent
);
741 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
743 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
744 return (SET_ERROR(EINVAL
));
746 if ((ops
= vdev_getops(type
)) == NULL
)
747 return (SET_ERROR(EINVAL
));
750 * If this is a load, get the vdev guid from the nvlist.
751 * Otherwise, vdev_alloc_common() will generate one for us.
753 if (alloctype
== VDEV_ALLOC_LOAD
) {
756 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
, &label_id
) ||
758 return (SET_ERROR(EINVAL
));
760 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
761 return (SET_ERROR(EINVAL
));
762 } else if (alloctype
== VDEV_ALLOC_SPARE
) {
763 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
764 return (SET_ERROR(EINVAL
));
765 } else if (alloctype
== VDEV_ALLOC_L2CACHE
) {
766 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
767 return (SET_ERROR(EINVAL
));
768 } else if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
769 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
770 return (SET_ERROR(EINVAL
));
774 * The first allocated vdev must be of type 'root'.
776 if (ops
!= &vdev_root_ops
&& spa
->spa_root_vdev
== NULL
)
777 return (SET_ERROR(EINVAL
));
780 * Determine whether we're a log vdev.
783 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &islog
);
784 if (islog
&& spa_version(spa
) < SPA_VERSION_SLOGS
)
785 return (SET_ERROR(ENOTSUP
));
787 if (ops
== &vdev_hole_ops
&& spa_version(spa
) < SPA_VERSION_HOLES
)
788 return (SET_ERROR(ENOTSUP
));
790 if (top_level
&& alloctype
== VDEV_ALLOC_ADD
) {
794 * If creating a top-level vdev, check for allocation
797 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_ALLOCATION_BIAS
,
799 alloc_bias
= vdev_derive_alloc_bias(bias
);
801 /* spa_vdev_add() expects feature to be enabled */
802 if (spa
->spa_load_state
!= SPA_LOAD_CREATE
&&
803 !spa_feature_is_enabled(spa
,
804 SPA_FEATURE_ALLOCATION_CLASSES
)) {
805 return (SET_ERROR(ENOTSUP
));
809 /* spa_vdev_add() expects feature to be enabled */
810 if (ops
== &vdev_draid_ops
&&
811 spa
->spa_load_state
!= SPA_LOAD_CREATE
&&
812 !spa_feature_is_enabled(spa
, SPA_FEATURE_DRAID
)) {
813 return (SET_ERROR(ENOTSUP
));
818 * Initialize the vdev specific data. This is done before calling
819 * vdev_alloc_common() since it may fail and this simplifies the
820 * error reporting and cleanup code paths.
823 if (ops
->vdev_op_init
!= NULL
) {
824 rc
= ops
->vdev_op_init(spa
, nv
, &tsd
);
830 vd
= vdev_alloc_common(spa
, id
, guid
, ops
);
832 vd
->vdev_islog
= islog
;
834 if (top_level
&& alloc_bias
!= VDEV_BIAS_NONE
)
835 vd
->vdev_alloc_bias
= alloc_bias
;
837 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &tmp
) == 0)
838 vd
->vdev_path
= spa_strdup(tmp
);
841 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
842 * fault on a vdev and want it to persist across imports (like with
845 rc
= nvlist_lookup_string(nv
, ZPOOL_CONFIG_AUX_STATE
, &tmp
);
846 if (rc
== 0 && tmp
!= NULL
&& strcmp(tmp
, "external") == 0) {
847 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
848 vd
->vdev_faulted
= 1;
849 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
852 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &tmp
) == 0)
853 vd
->vdev_devid
= spa_strdup(tmp
);
854 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PHYS_PATH
, &tmp
) == 0)
855 vd
->vdev_physpath
= spa_strdup(tmp
);
857 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH
,
859 vd
->vdev_enc_sysfs_path
= spa_strdup(tmp
);
861 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_FRU
, &tmp
) == 0)
862 vd
->vdev_fru
= spa_strdup(tmp
);
865 * Set the whole_disk property. If it's not specified, leave the value
868 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
869 &vd
->vdev_wholedisk
) != 0)
870 vd
->vdev_wholedisk
= -1ULL;
872 vic
= &vd
->vdev_indirect_config
;
874 ASSERT0(vic
->vic_mapping_object
);
875 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_OBJECT
,
876 &vic
->vic_mapping_object
);
877 ASSERT0(vic
->vic_births_object
);
878 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_BIRTHS
,
879 &vic
->vic_births_object
);
880 ASSERT3U(vic
->vic_prev_indirect_vdev
, ==, UINT64_MAX
);
881 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_PREV_INDIRECT_VDEV
,
882 &vic
->vic_prev_indirect_vdev
);
885 * Look for the 'not present' flag. This will only be set if the device
886 * was not present at the time of import.
888 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
889 &vd
->vdev_not_present
);
892 * Get the alignment requirement. Ignore pool ashift for vdev
895 if (alloctype
!= VDEV_ALLOC_ATTACH
) {
896 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASHIFT
,
899 vd
->vdev_attaching
= B_TRUE
;
903 * Retrieve the vdev creation time.
905 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_CREATE_TXG
,
908 if (vd
->vdev_ops
== &vdev_root_ops
&&
909 (alloctype
== VDEV_ALLOC_LOAD
||
910 alloctype
== VDEV_ALLOC_SPLIT
||
911 alloctype
== VDEV_ALLOC_ROOTPOOL
)) {
912 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_VDEV_ROOT_ZAP
,
917 * If we're a top-level vdev, try to load the allocation parameters.
920 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
921 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_ARRAY
,
923 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_SHIFT
,
925 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASIZE
,
927 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NONALLOCATING
,
929 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVING
,
931 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_VDEV_TOP_ZAP
,
934 ASSERT0(vd
->vdev_top_zap
);
937 if (top_level
&& alloctype
!= VDEV_ALLOC_ATTACH
) {
938 ASSERT(alloctype
== VDEV_ALLOC_LOAD
||
939 alloctype
== VDEV_ALLOC_ADD
||
940 alloctype
== VDEV_ALLOC_SPLIT
||
941 alloctype
== VDEV_ALLOC_ROOTPOOL
);
942 /* Note: metaslab_group_create() is now deferred */
945 if (vd
->vdev_ops
->vdev_op_leaf
&&
946 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
947 (void) nvlist_lookup_uint64(nv
,
948 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, &vd
->vdev_leaf_zap
);
950 ASSERT0(vd
->vdev_leaf_zap
);
954 * If we're a leaf vdev, try to load the DTL object and other state.
957 if (vd
->vdev_ops
->vdev_op_leaf
&&
958 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_L2CACHE
||
959 alloctype
== VDEV_ALLOC_ROOTPOOL
)) {
960 if (alloctype
== VDEV_ALLOC_LOAD
) {
961 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DTL
,
962 &vd
->vdev_dtl_object
);
963 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_UNSPARE
,
967 if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
970 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
971 &spare
) == 0 && spare
)
975 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
,
978 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_RESILVER_TXG
,
979 &vd
->vdev_resilver_txg
);
981 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REBUILD_TXG
,
982 &vd
->vdev_rebuild_txg
);
984 if (nvlist_exists(nv
, ZPOOL_CONFIG_RESILVER_DEFER
))
985 vdev_defer_resilver(vd
);
988 * In general, when importing a pool we want to ignore the
989 * persistent fault state, as the diagnosis made on another
990 * system may not be valid in the current context. The only
991 * exception is if we forced a vdev to a persistently faulted
992 * state with 'zpool offline -f'. The persistent fault will
993 * remain across imports until cleared.
995 * Local vdevs will remain in the faulted state.
997 if (spa_load_state(spa
) == SPA_LOAD_OPEN
||
998 spa_load_state(spa
) == SPA_LOAD_IMPORT
) {
999 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
,
1001 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DEGRADED
,
1002 &vd
->vdev_degraded
);
1003 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
,
1006 if (vd
->vdev_faulted
|| vd
->vdev_degraded
) {
1009 vd
->vdev_label_aux
=
1010 VDEV_AUX_ERR_EXCEEDED
;
1011 if (nvlist_lookup_string(nv
,
1012 ZPOOL_CONFIG_AUX_STATE
, &aux
) == 0 &&
1013 strcmp(aux
, "external") == 0)
1014 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
1016 vd
->vdev_faulted
= 0ULL;
1022 * Add ourselves to the parent's list of children.
1024 vdev_add_child(parent
, vd
);
1032 vdev_free(vdev_t
*vd
)
1034 spa_t
*spa
= vd
->vdev_spa
;
1036 ASSERT3P(vd
->vdev_initialize_thread
, ==, NULL
);
1037 ASSERT3P(vd
->vdev_trim_thread
, ==, NULL
);
1038 ASSERT3P(vd
->vdev_autotrim_thread
, ==, NULL
);
1039 ASSERT3P(vd
->vdev_rebuild_thread
, ==, NULL
);
1042 * Scan queues are normally destroyed at the end of a scan. If the
1043 * queue exists here, that implies the vdev is being removed while
1044 * the scan is still running.
1046 if (vd
->vdev_scan_io_queue
!= NULL
) {
1047 mutex_enter(&vd
->vdev_scan_io_queue_lock
);
1048 dsl_scan_io_queue_destroy(vd
->vdev_scan_io_queue
);
1049 vd
->vdev_scan_io_queue
= NULL
;
1050 mutex_exit(&vd
->vdev_scan_io_queue_lock
);
1054 * vdev_free() implies closing the vdev first. This is simpler than
1055 * trying to ensure complicated semantics for all callers.
1059 ASSERT(!list_link_active(&vd
->vdev_config_dirty_node
));
1060 ASSERT(!list_link_active(&vd
->vdev_state_dirty_node
));
1063 * Free all children.
1065 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1066 vdev_free(vd
->vdev_child
[c
]);
1068 ASSERT(vd
->vdev_child
== NULL
);
1069 ASSERT(vd
->vdev_guid_sum
== vd
->vdev_guid
);
1071 if (vd
->vdev_ops
->vdev_op_fini
!= NULL
)
1072 vd
->vdev_ops
->vdev_op_fini(vd
);
1075 * Discard allocation state.
1077 if (vd
->vdev_mg
!= NULL
) {
1078 vdev_metaslab_fini(vd
);
1079 metaslab_group_destroy(vd
->vdev_mg
);
1082 if (vd
->vdev_log_mg
!= NULL
) {
1083 ASSERT0(vd
->vdev_ms_count
);
1084 metaslab_group_destroy(vd
->vdev_log_mg
);
1085 vd
->vdev_log_mg
= NULL
;
1088 ASSERT0(vd
->vdev_stat
.vs_space
);
1089 ASSERT0(vd
->vdev_stat
.vs_dspace
);
1090 ASSERT0(vd
->vdev_stat
.vs_alloc
);
1093 * Remove this vdev from its parent's child list.
1095 vdev_remove_child(vd
->vdev_parent
, vd
);
1097 ASSERT(vd
->vdev_parent
== NULL
);
1098 ASSERT(!list_link_active(&vd
->vdev_leaf_node
));
1101 * Clean up vdev structure.
1103 vdev_queue_fini(vd
);
1106 spa_strfree(vd
->vdev_path
);
1108 spa_strfree(vd
->vdev_devid
);
1109 if (vd
->vdev_physpath
)
1110 spa_strfree(vd
->vdev_physpath
);
1112 if (vd
->vdev_enc_sysfs_path
)
1113 spa_strfree(vd
->vdev_enc_sysfs_path
);
1116 spa_strfree(vd
->vdev_fru
);
1118 if (vd
->vdev_isspare
)
1119 spa_spare_remove(vd
);
1120 if (vd
->vdev_isl2cache
)
1121 spa_l2cache_remove(vd
);
1123 txg_list_destroy(&vd
->vdev_ms_list
);
1124 txg_list_destroy(&vd
->vdev_dtl_list
);
1126 mutex_enter(&vd
->vdev_dtl_lock
);
1127 space_map_close(vd
->vdev_dtl_sm
);
1128 for (int t
= 0; t
< DTL_TYPES
; t
++) {
1129 range_tree_vacate(vd
->vdev_dtl
[t
], NULL
, NULL
);
1130 range_tree_destroy(vd
->vdev_dtl
[t
]);
1132 mutex_exit(&vd
->vdev_dtl_lock
);
1134 EQUIV(vd
->vdev_indirect_births
!= NULL
,
1135 vd
->vdev_indirect_mapping
!= NULL
);
1136 if (vd
->vdev_indirect_births
!= NULL
) {
1137 vdev_indirect_mapping_close(vd
->vdev_indirect_mapping
);
1138 vdev_indirect_births_close(vd
->vdev_indirect_births
);
1141 if (vd
->vdev_obsolete_sm
!= NULL
) {
1142 ASSERT(vd
->vdev_removing
||
1143 vd
->vdev_ops
== &vdev_indirect_ops
);
1144 space_map_close(vd
->vdev_obsolete_sm
);
1145 vd
->vdev_obsolete_sm
= NULL
;
1147 range_tree_destroy(vd
->vdev_obsolete_segments
);
1148 rw_destroy(&vd
->vdev_indirect_rwlock
);
1149 mutex_destroy(&vd
->vdev_obsolete_lock
);
1151 mutex_destroy(&vd
->vdev_dtl_lock
);
1152 mutex_destroy(&vd
->vdev_stat_lock
);
1153 mutex_destroy(&vd
->vdev_probe_lock
);
1154 mutex_destroy(&vd
->vdev_scan_io_queue_lock
);
1156 mutex_destroy(&vd
->vdev_initialize_lock
);
1157 mutex_destroy(&vd
->vdev_initialize_io_lock
);
1158 cv_destroy(&vd
->vdev_initialize_io_cv
);
1159 cv_destroy(&vd
->vdev_initialize_cv
);
1161 mutex_destroy(&vd
->vdev_trim_lock
);
1162 mutex_destroy(&vd
->vdev_autotrim_lock
);
1163 mutex_destroy(&vd
->vdev_trim_io_lock
);
1164 cv_destroy(&vd
->vdev_trim_cv
);
1165 cv_destroy(&vd
->vdev_autotrim_cv
);
1166 cv_destroy(&vd
->vdev_autotrim_kick_cv
);
1167 cv_destroy(&vd
->vdev_trim_io_cv
);
1169 mutex_destroy(&vd
->vdev_rebuild_lock
);
1170 cv_destroy(&vd
->vdev_rebuild_cv
);
1172 zfs_ratelimit_fini(&vd
->vdev_delay_rl
);
1173 zfs_ratelimit_fini(&vd
->vdev_deadman_rl
);
1174 zfs_ratelimit_fini(&vd
->vdev_checksum_rl
);
1176 if (vd
== spa
->spa_root_vdev
)
1177 spa
->spa_root_vdev
= NULL
;
1179 kmem_free(vd
, sizeof (vdev_t
));
1183 * Transfer top-level vdev state from svd to tvd.
1186 vdev_top_transfer(vdev_t
*svd
, vdev_t
*tvd
)
1188 spa_t
*spa
= svd
->vdev_spa
;
1193 ASSERT(tvd
== tvd
->vdev_top
);
1195 tvd
->vdev_ms_array
= svd
->vdev_ms_array
;
1196 tvd
->vdev_ms_shift
= svd
->vdev_ms_shift
;
1197 tvd
->vdev_ms_count
= svd
->vdev_ms_count
;
1198 tvd
->vdev_top_zap
= svd
->vdev_top_zap
;
1200 svd
->vdev_ms_array
= 0;
1201 svd
->vdev_ms_shift
= 0;
1202 svd
->vdev_ms_count
= 0;
1203 svd
->vdev_top_zap
= 0;
1206 ASSERT3P(tvd
->vdev_mg
, ==, svd
->vdev_mg
);
1207 if (tvd
->vdev_log_mg
)
1208 ASSERT3P(tvd
->vdev_log_mg
, ==, svd
->vdev_log_mg
);
1209 tvd
->vdev_mg
= svd
->vdev_mg
;
1210 tvd
->vdev_log_mg
= svd
->vdev_log_mg
;
1211 tvd
->vdev_ms
= svd
->vdev_ms
;
1213 svd
->vdev_mg
= NULL
;
1214 svd
->vdev_log_mg
= NULL
;
1215 svd
->vdev_ms
= NULL
;
1217 if (tvd
->vdev_mg
!= NULL
)
1218 tvd
->vdev_mg
->mg_vd
= tvd
;
1219 if (tvd
->vdev_log_mg
!= NULL
)
1220 tvd
->vdev_log_mg
->mg_vd
= tvd
;
1222 tvd
->vdev_checkpoint_sm
= svd
->vdev_checkpoint_sm
;
1223 svd
->vdev_checkpoint_sm
= NULL
;
1225 tvd
->vdev_alloc_bias
= svd
->vdev_alloc_bias
;
1226 svd
->vdev_alloc_bias
= VDEV_BIAS_NONE
;
1228 tvd
->vdev_stat
.vs_alloc
= svd
->vdev_stat
.vs_alloc
;
1229 tvd
->vdev_stat
.vs_space
= svd
->vdev_stat
.vs_space
;
1230 tvd
->vdev_stat
.vs_dspace
= svd
->vdev_stat
.vs_dspace
;
1232 svd
->vdev_stat
.vs_alloc
= 0;
1233 svd
->vdev_stat
.vs_space
= 0;
1234 svd
->vdev_stat
.vs_dspace
= 0;
1237 * State which may be set on a top-level vdev that's in the
1238 * process of being removed.
1240 ASSERT0(tvd
->vdev_indirect_config
.vic_births_object
);
1241 ASSERT0(tvd
->vdev_indirect_config
.vic_mapping_object
);
1242 ASSERT3U(tvd
->vdev_indirect_config
.vic_prev_indirect_vdev
, ==, -1ULL);
1243 ASSERT3P(tvd
->vdev_indirect_mapping
, ==, NULL
);
1244 ASSERT3P(tvd
->vdev_indirect_births
, ==, NULL
);
1245 ASSERT3P(tvd
->vdev_obsolete_sm
, ==, NULL
);
1246 ASSERT0(tvd
->vdev_noalloc
);
1247 ASSERT0(tvd
->vdev_removing
);
1248 ASSERT0(tvd
->vdev_rebuilding
);
1249 tvd
->vdev_noalloc
= svd
->vdev_noalloc
;
1250 tvd
->vdev_removing
= svd
->vdev_removing
;
1251 tvd
->vdev_rebuilding
= svd
->vdev_rebuilding
;
1252 tvd
->vdev_rebuild_config
= svd
->vdev_rebuild_config
;
1253 tvd
->vdev_indirect_config
= svd
->vdev_indirect_config
;
1254 tvd
->vdev_indirect_mapping
= svd
->vdev_indirect_mapping
;
1255 tvd
->vdev_indirect_births
= svd
->vdev_indirect_births
;
1256 range_tree_swap(&svd
->vdev_obsolete_segments
,
1257 &tvd
->vdev_obsolete_segments
);
1258 tvd
->vdev_obsolete_sm
= svd
->vdev_obsolete_sm
;
1259 svd
->vdev_indirect_config
.vic_mapping_object
= 0;
1260 svd
->vdev_indirect_config
.vic_births_object
= 0;
1261 svd
->vdev_indirect_config
.vic_prev_indirect_vdev
= -1ULL;
1262 svd
->vdev_indirect_mapping
= NULL
;
1263 svd
->vdev_indirect_births
= NULL
;
1264 svd
->vdev_obsolete_sm
= NULL
;
1265 svd
->vdev_noalloc
= 0;
1266 svd
->vdev_removing
= 0;
1267 svd
->vdev_rebuilding
= 0;
1269 for (t
= 0; t
< TXG_SIZE
; t
++) {
1270 while ((msp
= txg_list_remove(&svd
->vdev_ms_list
, t
)) != NULL
)
1271 (void) txg_list_add(&tvd
->vdev_ms_list
, msp
, t
);
1272 while ((vd
= txg_list_remove(&svd
->vdev_dtl_list
, t
)) != NULL
)
1273 (void) txg_list_add(&tvd
->vdev_dtl_list
, vd
, t
);
1274 if (txg_list_remove_this(&spa
->spa_vdev_txg_list
, svd
, t
))
1275 (void) txg_list_add(&spa
->spa_vdev_txg_list
, tvd
, t
);
1278 if (list_link_active(&svd
->vdev_config_dirty_node
)) {
1279 vdev_config_clean(svd
);
1280 vdev_config_dirty(tvd
);
1283 if (list_link_active(&svd
->vdev_state_dirty_node
)) {
1284 vdev_state_clean(svd
);
1285 vdev_state_dirty(tvd
);
1288 tvd
->vdev_deflate_ratio
= svd
->vdev_deflate_ratio
;
1289 svd
->vdev_deflate_ratio
= 0;
1291 tvd
->vdev_islog
= svd
->vdev_islog
;
1292 svd
->vdev_islog
= 0;
1294 dsl_scan_io_queue_vdev_xfer(svd
, tvd
);
1298 vdev_top_update(vdev_t
*tvd
, vdev_t
*vd
)
1305 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1306 vdev_top_update(tvd
, vd
->vdev_child
[c
]);
1310 * Add a mirror/replacing vdev above an existing vdev. There is no need to
1311 * call .vdev_op_init() since mirror/replacing vdevs do not have private state.
1314 vdev_add_parent(vdev_t
*cvd
, vdev_ops_t
*ops
)
1316 spa_t
*spa
= cvd
->vdev_spa
;
1317 vdev_t
*pvd
= cvd
->vdev_parent
;
1320 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1322 mvd
= vdev_alloc_common(spa
, cvd
->vdev_id
, 0, ops
);
1324 mvd
->vdev_asize
= cvd
->vdev_asize
;
1325 mvd
->vdev_min_asize
= cvd
->vdev_min_asize
;
1326 mvd
->vdev_max_asize
= cvd
->vdev_max_asize
;
1327 mvd
->vdev_psize
= cvd
->vdev_psize
;
1328 mvd
->vdev_ashift
= cvd
->vdev_ashift
;
1329 mvd
->vdev_logical_ashift
= cvd
->vdev_logical_ashift
;
1330 mvd
->vdev_physical_ashift
= cvd
->vdev_physical_ashift
;
1331 mvd
->vdev_state
= cvd
->vdev_state
;
1332 mvd
->vdev_crtxg
= cvd
->vdev_crtxg
;
1334 vdev_remove_child(pvd
, cvd
);
1335 vdev_add_child(pvd
, mvd
);
1336 cvd
->vdev_id
= mvd
->vdev_children
;
1337 vdev_add_child(mvd
, cvd
);
1338 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1340 if (mvd
== mvd
->vdev_top
)
1341 vdev_top_transfer(cvd
, mvd
);
1347 * Remove a 1-way mirror/replacing vdev from the tree.
1350 vdev_remove_parent(vdev_t
*cvd
)
1352 vdev_t
*mvd
= cvd
->vdev_parent
;
1353 vdev_t
*pvd
= mvd
->vdev_parent
;
1355 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1357 ASSERT(mvd
->vdev_children
== 1);
1358 ASSERT(mvd
->vdev_ops
== &vdev_mirror_ops
||
1359 mvd
->vdev_ops
== &vdev_replacing_ops
||
1360 mvd
->vdev_ops
== &vdev_spare_ops
);
1361 cvd
->vdev_ashift
= mvd
->vdev_ashift
;
1362 cvd
->vdev_logical_ashift
= mvd
->vdev_logical_ashift
;
1363 cvd
->vdev_physical_ashift
= mvd
->vdev_physical_ashift
;
1364 vdev_remove_child(mvd
, cvd
);
1365 vdev_remove_child(pvd
, mvd
);
1368 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1369 * Otherwise, we could have detached an offline device, and when we
1370 * go to import the pool we'll think we have two top-level vdevs,
1371 * instead of a different version of the same top-level vdev.
1373 if (mvd
->vdev_top
== mvd
) {
1374 uint64_t guid_delta
= mvd
->vdev_guid
- cvd
->vdev_guid
;
1375 cvd
->vdev_orig_guid
= cvd
->vdev_guid
;
1376 cvd
->vdev_guid
+= guid_delta
;
1377 cvd
->vdev_guid_sum
+= guid_delta
;
1380 * If pool not set for autoexpand, we need to also preserve
1381 * mvd's asize to prevent automatic expansion of cvd.
1382 * Otherwise if we are adjusting the mirror by attaching and
1383 * detaching children of non-uniform sizes, the mirror could
1384 * autoexpand, unexpectedly requiring larger devices to
1385 * re-establish the mirror.
1387 if (!cvd
->vdev_spa
->spa_autoexpand
)
1388 cvd
->vdev_asize
= mvd
->vdev_asize
;
1390 cvd
->vdev_id
= mvd
->vdev_id
;
1391 vdev_add_child(pvd
, cvd
);
1392 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1394 if (cvd
== cvd
->vdev_top
)
1395 vdev_top_transfer(mvd
, cvd
);
1397 ASSERT(mvd
->vdev_children
== 0);
1402 * Choose GCD for spa_gcd_alloc.
1405 vdev_gcd(uint64_t a
, uint64_t b
)
1416 * Set spa_min_alloc and spa_gcd_alloc.
1419 vdev_spa_set_alloc(spa_t
*spa
, uint64_t min_alloc
)
1421 if (min_alloc
< spa
->spa_min_alloc
)
1422 spa
->spa_min_alloc
= min_alloc
;
1423 if (spa
->spa_gcd_alloc
== INT_MAX
) {
1424 spa
->spa_gcd_alloc
= min_alloc
;
1426 spa
->spa_gcd_alloc
= vdev_gcd(min_alloc
,
1427 spa
->spa_gcd_alloc
);
1432 vdev_metaslab_group_create(vdev_t
*vd
)
1434 spa_t
*spa
= vd
->vdev_spa
;
1437 * metaslab_group_create was delayed until allocation bias was available
1439 if (vd
->vdev_mg
== NULL
) {
1440 metaslab_class_t
*mc
;
1442 if (vd
->vdev_islog
&& vd
->vdev_alloc_bias
== VDEV_BIAS_NONE
)
1443 vd
->vdev_alloc_bias
= VDEV_BIAS_LOG
;
1445 ASSERT3U(vd
->vdev_islog
, ==,
1446 (vd
->vdev_alloc_bias
== VDEV_BIAS_LOG
));
1448 switch (vd
->vdev_alloc_bias
) {
1450 mc
= spa_log_class(spa
);
1452 case VDEV_BIAS_SPECIAL
:
1453 mc
= spa_special_class(spa
);
1455 case VDEV_BIAS_DEDUP
:
1456 mc
= spa_dedup_class(spa
);
1459 mc
= spa_normal_class(spa
);
1462 vd
->vdev_mg
= metaslab_group_create(mc
, vd
,
1463 spa
->spa_alloc_count
);
1465 if (!vd
->vdev_islog
) {
1466 vd
->vdev_log_mg
= metaslab_group_create(
1467 spa_embedded_log_class(spa
), vd
, 1);
1471 * The spa ashift min/max only apply for the normal metaslab
1472 * class. Class destination is late binding so ashift boundary
1473 * setting had to wait until now.
1475 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
1476 mc
== spa_normal_class(spa
) && vd
->vdev_aux
== NULL
) {
1477 if (vd
->vdev_ashift
> spa
->spa_max_ashift
)
1478 spa
->spa_max_ashift
= vd
->vdev_ashift
;
1479 if (vd
->vdev_ashift
< spa
->spa_min_ashift
)
1480 spa
->spa_min_ashift
= vd
->vdev_ashift
;
1482 uint64_t min_alloc
= vdev_get_min_alloc(vd
);
1483 vdev_spa_set_alloc(spa
, min_alloc
);
1489 vdev_metaslab_init(vdev_t
*vd
, uint64_t txg
)
1491 spa_t
*spa
= vd
->vdev_spa
;
1492 uint64_t oldc
= vd
->vdev_ms_count
;
1493 uint64_t newc
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
1496 boolean_t expanding
= (oldc
!= 0);
1498 ASSERT(txg
== 0 || spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1501 * This vdev is not being allocated from yet or is a hole.
1503 if (vd
->vdev_ms_shift
== 0)
1506 ASSERT(!vd
->vdev_ishole
);
1508 ASSERT(oldc
<= newc
);
1510 mspp
= vmem_zalloc(newc
* sizeof (*mspp
), KM_SLEEP
);
1513 memcpy(mspp
, vd
->vdev_ms
, oldc
* sizeof (*mspp
));
1514 vmem_free(vd
->vdev_ms
, oldc
* sizeof (*mspp
));
1518 vd
->vdev_ms_count
= newc
;
1520 for (uint64_t m
= oldc
; m
< newc
; m
++) {
1521 uint64_t object
= 0;
1523 * vdev_ms_array may be 0 if we are creating the "fake"
1524 * metaslabs for an indirect vdev for zdb's leak detection.
1525 * See zdb_leak_init().
1527 if (txg
== 0 && vd
->vdev_ms_array
!= 0) {
1528 error
= dmu_read(spa
->spa_meta_objset
,
1530 m
* sizeof (uint64_t), sizeof (uint64_t), &object
,
1533 vdev_dbgmsg(vd
, "unable to read the metaslab "
1534 "array [error=%d]", error
);
1539 error
= metaslab_init(vd
->vdev_mg
, m
, object
, txg
,
1542 vdev_dbgmsg(vd
, "metaslab_init failed [error=%d]",
1549 * Find the emptiest metaslab on the vdev and mark it for use for
1550 * embedded slog by moving it from the regular to the log metaslab
1553 if (vd
->vdev_mg
->mg_class
== spa_normal_class(spa
) &&
1554 vd
->vdev_ms_count
> zfs_embedded_slog_min_ms
&&
1555 avl_is_empty(&vd
->vdev_log_mg
->mg_metaslab_tree
)) {
1556 uint64_t slog_msid
= 0;
1557 uint64_t smallest
= UINT64_MAX
;
1560 * Note, we only search the new metaslabs, because the old
1561 * (pre-existing) ones may be active (e.g. have non-empty
1562 * range_tree's), and we don't move them to the new
1565 for (uint64_t m
= oldc
; m
< newc
; m
++) {
1567 space_map_allocated(vd
->vdev_ms
[m
]->ms_sm
);
1568 if (alloc
< smallest
) {
1573 metaslab_t
*slog_ms
= vd
->vdev_ms
[slog_msid
];
1575 * The metaslab was marked as dirty at the end of
1576 * metaslab_init(). Remove it from the dirty list so that we
1577 * can uninitialize and reinitialize it to the new class.
1580 (void) txg_list_remove_this(&vd
->vdev_ms_list
,
1583 uint64_t sm_obj
= space_map_object(slog_ms
->ms_sm
);
1584 metaslab_fini(slog_ms
);
1585 VERIFY0(metaslab_init(vd
->vdev_log_mg
, slog_msid
, sm_obj
, txg
,
1586 &vd
->vdev_ms
[slog_msid
]));
1590 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_WRITER
);
1593 * If the vdev is marked as non-allocating then don't
1594 * activate the metaslabs since we want to ensure that
1595 * no allocations are performed on this device.
1597 if (vd
->vdev_noalloc
) {
1598 /* track non-allocating vdev space */
1599 spa
->spa_nonallocating_dspace
+= spa_deflate(spa
) ?
1600 vd
->vdev_stat
.vs_dspace
: vd
->vdev_stat
.vs_space
;
1601 } else if (!expanding
) {
1602 metaslab_group_activate(vd
->vdev_mg
);
1603 if (vd
->vdev_log_mg
!= NULL
)
1604 metaslab_group_activate(vd
->vdev_log_mg
);
1608 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
1614 vdev_metaslab_fini(vdev_t
*vd
)
1616 if (vd
->vdev_checkpoint_sm
!= NULL
) {
1617 ASSERT(spa_feature_is_active(vd
->vdev_spa
,
1618 SPA_FEATURE_POOL_CHECKPOINT
));
1619 space_map_close(vd
->vdev_checkpoint_sm
);
1621 * Even though we close the space map, we need to set its
1622 * pointer to NULL. The reason is that vdev_metaslab_fini()
1623 * may be called multiple times for certain operations
1624 * (i.e. when destroying a pool) so we need to ensure that
1625 * this clause never executes twice. This logic is similar
1626 * to the one used for the vdev_ms clause below.
1628 vd
->vdev_checkpoint_sm
= NULL
;
1631 if (vd
->vdev_ms
!= NULL
) {
1632 metaslab_group_t
*mg
= vd
->vdev_mg
;
1634 metaslab_group_passivate(mg
);
1635 if (vd
->vdev_log_mg
!= NULL
) {
1636 ASSERT(!vd
->vdev_islog
);
1637 metaslab_group_passivate(vd
->vdev_log_mg
);
1640 uint64_t count
= vd
->vdev_ms_count
;
1641 for (uint64_t m
= 0; m
< count
; m
++) {
1642 metaslab_t
*msp
= vd
->vdev_ms
[m
];
1646 vmem_free(vd
->vdev_ms
, count
* sizeof (metaslab_t
*));
1648 vd
->vdev_ms_count
= 0;
1650 for (int i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++) {
1651 ASSERT0(mg
->mg_histogram
[i
]);
1652 if (vd
->vdev_log_mg
!= NULL
)
1653 ASSERT0(vd
->vdev_log_mg
->mg_histogram
[i
]);
1656 ASSERT0(vd
->vdev_ms_count
);
1659 typedef struct vdev_probe_stats
{
1660 boolean_t vps_readable
;
1661 boolean_t vps_writeable
;
1663 } vdev_probe_stats_t
;
1666 vdev_probe_done(zio_t
*zio
)
1668 spa_t
*spa
= zio
->io_spa
;
1669 vdev_t
*vd
= zio
->io_vd
;
1670 vdev_probe_stats_t
*vps
= zio
->io_private
;
1672 ASSERT(vd
->vdev_probe_zio
!= NULL
);
1674 if (zio
->io_type
== ZIO_TYPE_READ
) {
1675 if (zio
->io_error
== 0)
1676 vps
->vps_readable
= 1;
1677 if (zio
->io_error
== 0 && spa_writeable(spa
)) {
1678 zio_nowait(zio_write_phys(vd
->vdev_probe_zio
, vd
,
1679 zio
->io_offset
, zio
->io_size
, zio
->io_abd
,
1680 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1681 ZIO_PRIORITY_SYNC_WRITE
, vps
->vps_flags
, B_TRUE
));
1683 abd_free(zio
->io_abd
);
1685 } else if (zio
->io_type
== ZIO_TYPE_WRITE
) {
1686 if (zio
->io_error
== 0)
1687 vps
->vps_writeable
= 1;
1688 abd_free(zio
->io_abd
);
1689 } else if (zio
->io_type
== ZIO_TYPE_NULL
) {
1693 vd
->vdev_cant_read
|= !vps
->vps_readable
;
1694 vd
->vdev_cant_write
|= !vps
->vps_writeable
;
1696 if (vdev_readable(vd
) &&
1697 (vdev_writeable(vd
) || !spa_writeable(spa
))) {
1700 ASSERT(zio
->io_error
!= 0);
1701 vdev_dbgmsg(vd
, "failed probe");
1702 (void) zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE
,
1703 spa
, vd
, NULL
, NULL
, 0);
1704 zio
->io_error
= SET_ERROR(ENXIO
);
1707 mutex_enter(&vd
->vdev_probe_lock
);
1708 ASSERT(vd
->vdev_probe_zio
== zio
);
1709 vd
->vdev_probe_zio
= NULL
;
1710 mutex_exit(&vd
->vdev_probe_lock
);
1713 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
)
1714 if (!vdev_accessible(vd
, pio
))
1715 pio
->io_error
= SET_ERROR(ENXIO
);
1717 kmem_free(vps
, sizeof (*vps
));
1722 * Determine whether this device is accessible.
1724 * Read and write to several known locations: the pad regions of each
1725 * vdev label but the first, which we leave alone in case it contains
1729 vdev_probe(vdev_t
*vd
, zio_t
*zio
)
1731 spa_t
*spa
= vd
->vdev_spa
;
1732 vdev_probe_stats_t
*vps
= NULL
;
1735 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1738 * Don't probe the probe.
1740 if (zio
&& (zio
->io_flags
& ZIO_FLAG_PROBE
))
1744 * To prevent 'probe storms' when a device fails, we create
1745 * just one probe i/o at a time. All zios that want to probe
1746 * this vdev will become parents of the probe io.
1748 mutex_enter(&vd
->vdev_probe_lock
);
1750 if ((pio
= vd
->vdev_probe_zio
) == NULL
) {
1751 vps
= kmem_zalloc(sizeof (*vps
), KM_SLEEP
);
1753 vps
->vps_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_PROBE
|
1754 ZIO_FLAG_DONT_AGGREGATE
| ZIO_FLAG_TRYHARD
;
1756 if (spa_config_held(spa
, SCL_ZIO
, RW_WRITER
)) {
1758 * vdev_cant_read and vdev_cant_write can only
1759 * transition from TRUE to FALSE when we have the
1760 * SCL_ZIO lock as writer; otherwise they can only
1761 * transition from FALSE to TRUE. This ensures that
1762 * any zio looking at these values can assume that
1763 * failures persist for the life of the I/O. That's
1764 * important because when a device has intermittent
1765 * connectivity problems, we want to ensure that
1766 * they're ascribed to the device (ENXIO) and not
1769 * Since we hold SCL_ZIO as writer here, clear both
1770 * values so the probe can reevaluate from first
1773 vps
->vps_flags
|= ZIO_FLAG_CONFIG_WRITER
;
1774 vd
->vdev_cant_read
= B_FALSE
;
1775 vd
->vdev_cant_write
= B_FALSE
;
1778 vd
->vdev_probe_zio
= pio
= zio_null(NULL
, spa
, vd
,
1779 vdev_probe_done
, vps
,
1780 vps
->vps_flags
| ZIO_FLAG_DONT_PROPAGATE
);
1783 * We can't change the vdev state in this context, so we
1784 * kick off an async task to do it on our behalf.
1787 vd
->vdev_probe_wanted
= B_TRUE
;
1788 spa_async_request(spa
, SPA_ASYNC_PROBE
);
1793 zio_add_child(zio
, pio
);
1795 mutex_exit(&vd
->vdev_probe_lock
);
1798 ASSERT(zio
!= NULL
);
1802 for (int l
= 1; l
< VDEV_LABELS
; l
++) {
1803 zio_nowait(zio_read_phys(pio
, vd
,
1804 vdev_label_offset(vd
->vdev_psize
, l
,
1805 offsetof(vdev_label_t
, vl_be
)), VDEV_PAD_SIZE
,
1806 abd_alloc_for_io(VDEV_PAD_SIZE
, B_TRUE
),
1807 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1808 ZIO_PRIORITY_SYNC_READ
, vps
->vps_flags
, B_TRUE
));
1819 vdev_load_child(void *arg
)
1823 vd
->vdev_load_error
= vdev_load(vd
);
1827 vdev_open_child(void *arg
)
1831 vd
->vdev_open_thread
= curthread
;
1832 vd
->vdev_open_error
= vdev_open(vd
);
1833 vd
->vdev_open_thread
= NULL
;
1837 vdev_uses_zvols(vdev_t
*vd
)
1840 if (zvol_is_zvol(vd
->vdev_path
))
1844 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1845 if (vdev_uses_zvols(vd
->vdev_child
[c
]))
1852 * Returns B_TRUE if the passed child should be opened.
1855 vdev_default_open_children_func(vdev_t
*vd
)
1862 * Open the requested child vdevs. If any of the leaf vdevs are using
1863 * a ZFS volume then do the opens in a single thread. This avoids a
1864 * deadlock when the current thread is holding the spa_namespace_lock.
1867 vdev_open_children_impl(vdev_t
*vd
, vdev_open_children_func_t
*open_func
)
1869 int children
= vd
->vdev_children
;
1871 taskq_t
*tq
= taskq_create("vdev_open", children
, minclsyspri
,
1872 children
, children
, TASKQ_PREPOPULATE
);
1873 vd
->vdev_nonrot
= B_TRUE
;
1875 for (int c
= 0; c
< children
; c
++) {
1876 vdev_t
*cvd
= vd
->vdev_child
[c
];
1878 if (open_func(cvd
) == B_FALSE
)
1881 if (tq
== NULL
|| vdev_uses_zvols(vd
)) {
1882 cvd
->vdev_open_error
= vdev_open(cvd
);
1884 VERIFY(taskq_dispatch(tq
, vdev_open_child
,
1885 cvd
, TQ_SLEEP
) != TASKQID_INVALID
);
1888 vd
->vdev_nonrot
&= cvd
->vdev_nonrot
;
1898 * Open all child vdevs.
1901 vdev_open_children(vdev_t
*vd
)
1903 vdev_open_children_impl(vd
, vdev_default_open_children_func
);
1907 * Conditionally open a subset of child vdevs.
1910 vdev_open_children_subset(vdev_t
*vd
, vdev_open_children_func_t
*open_func
)
1912 vdev_open_children_impl(vd
, open_func
);
1916 * Compute the raidz-deflation ratio. Note, we hard-code
1917 * in 128k (1 << 17) because it is the "typical" blocksize.
1918 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1919 * otherwise it would inconsistently account for existing bp's.
1922 vdev_set_deflate_ratio(vdev_t
*vd
)
1924 if (vd
== vd
->vdev_top
&& !vd
->vdev_ishole
&& vd
->vdev_ashift
!= 0) {
1925 vd
->vdev_deflate_ratio
= (1 << 17) /
1926 (vdev_psize_to_asize(vd
, 1 << 17) >> SPA_MINBLOCKSHIFT
);
1931 * Choose the best of two ashifts, preferring one between logical ashift
1932 * (absolute minimum) and administrator defined maximum, otherwise take
1933 * the biggest of the two.
1936 vdev_best_ashift(uint64_t logical
, uint64_t a
, uint64_t b
)
1938 if (a
> logical
&& a
<= zfs_vdev_max_auto_ashift
) {
1939 if (b
<= logical
|| b
> zfs_vdev_max_auto_ashift
)
1943 } else if (b
<= logical
|| b
> zfs_vdev_max_auto_ashift
)
1949 * Maximize performance by inflating the configured ashift for top level
1950 * vdevs to be as close to the physical ashift as possible while maintaining
1951 * administrator defined limits and ensuring it doesn't go below the
1955 vdev_ashift_optimize(vdev_t
*vd
)
1957 ASSERT(vd
== vd
->vdev_top
);
1959 if (vd
->vdev_ashift
< vd
->vdev_physical_ashift
&&
1960 vd
->vdev_physical_ashift
<= zfs_vdev_max_auto_ashift
) {
1961 vd
->vdev_ashift
= MIN(
1962 MAX(zfs_vdev_max_auto_ashift
, vd
->vdev_ashift
),
1963 MAX(zfs_vdev_min_auto_ashift
,
1964 vd
->vdev_physical_ashift
));
1967 * If the logical and physical ashifts are the same, then
1968 * we ensure that the top-level vdev's ashift is not smaller
1969 * than our minimum ashift value. For the unusual case
1970 * where logical ashift > physical ashift, we can't cap
1971 * the calculated ashift based on max ashift as that
1972 * would cause failures.
1973 * We still check if we need to increase it to match
1976 vd
->vdev_ashift
= MAX(zfs_vdev_min_auto_ashift
,
1982 * Prepare a virtual device for access.
1985 vdev_open(vdev_t
*vd
)
1987 spa_t
*spa
= vd
->vdev_spa
;
1990 uint64_t max_osize
= 0;
1991 uint64_t asize
, max_asize
, psize
;
1992 uint64_t logical_ashift
= 0;
1993 uint64_t physical_ashift
= 0;
1995 ASSERT(vd
->vdev_open_thread
== curthread
||
1996 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1997 ASSERT(vd
->vdev_state
== VDEV_STATE_CLOSED
||
1998 vd
->vdev_state
== VDEV_STATE_CANT_OPEN
||
1999 vd
->vdev_state
== VDEV_STATE_OFFLINE
);
2001 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
2002 vd
->vdev_cant_read
= B_FALSE
;
2003 vd
->vdev_cant_write
= B_FALSE
;
2004 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
2007 * If this vdev is not removed, check its fault status. If it's
2008 * faulted, bail out of the open.
2010 if (!vd
->vdev_removed
&& vd
->vdev_faulted
) {
2011 ASSERT(vd
->vdev_children
== 0);
2012 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
2013 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
2014 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
2015 vd
->vdev_label_aux
);
2016 return (SET_ERROR(ENXIO
));
2017 } else if (vd
->vdev_offline
) {
2018 ASSERT(vd
->vdev_children
== 0);
2019 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
, VDEV_AUX_NONE
);
2020 return (SET_ERROR(ENXIO
));
2023 error
= vd
->vdev_ops
->vdev_op_open(vd
, &osize
, &max_osize
,
2024 &logical_ashift
, &physical_ashift
);
2026 /* Keep the device in removed state if unplugged */
2027 if (error
== ENOENT
&& vd
->vdev_removed
) {
2028 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_REMOVED
,
2034 * Physical volume size should never be larger than its max size, unless
2035 * the disk has shrunk while we were reading it or the device is buggy
2036 * or damaged: either way it's not safe for use, bail out of the open.
2038 if (osize
> max_osize
) {
2039 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2040 VDEV_AUX_OPEN_FAILED
);
2041 return (SET_ERROR(ENXIO
));
2045 * Reset the vdev_reopening flag so that we actually close
2046 * the vdev on error.
2048 vd
->vdev_reopening
= B_FALSE
;
2049 if (zio_injection_enabled
&& error
== 0)
2050 error
= zio_handle_device_injection(vd
, NULL
, SET_ERROR(ENXIO
));
2053 if (vd
->vdev_removed
&&
2054 vd
->vdev_stat
.vs_aux
!= VDEV_AUX_OPEN_FAILED
)
2055 vd
->vdev_removed
= B_FALSE
;
2057 if (vd
->vdev_stat
.vs_aux
== VDEV_AUX_CHILDREN_OFFLINE
) {
2058 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
,
2059 vd
->vdev_stat
.vs_aux
);
2061 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2062 vd
->vdev_stat
.vs_aux
);
2067 vd
->vdev_removed
= B_FALSE
;
2070 * Recheck the faulted flag now that we have confirmed that
2071 * the vdev is accessible. If we're faulted, bail.
2073 if (vd
->vdev_faulted
) {
2074 ASSERT(vd
->vdev_children
== 0);
2075 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
2076 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
2077 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
2078 vd
->vdev_label_aux
);
2079 return (SET_ERROR(ENXIO
));
2082 if (vd
->vdev_degraded
) {
2083 ASSERT(vd
->vdev_children
== 0);
2084 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
2085 VDEV_AUX_ERR_EXCEEDED
);
2087 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_HEALTHY
, 0);
2091 * For hole or missing vdevs we just return success.
2093 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
)
2096 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2097 if (vd
->vdev_child
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
) {
2098 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
2104 osize
= P2ALIGN(osize
, (uint64_t)sizeof (vdev_label_t
));
2105 max_osize
= P2ALIGN(max_osize
, (uint64_t)sizeof (vdev_label_t
));
2107 if (vd
->vdev_children
== 0) {
2108 if (osize
< SPA_MINDEVSIZE
) {
2109 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2110 VDEV_AUX_TOO_SMALL
);
2111 return (SET_ERROR(EOVERFLOW
));
2114 asize
= osize
- (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
);
2115 max_asize
= max_osize
- (VDEV_LABEL_START_SIZE
+
2116 VDEV_LABEL_END_SIZE
);
2118 if (vd
->vdev_parent
!= NULL
&& osize
< SPA_MINDEVSIZE
-
2119 (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
)) {
2120 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2121 VDEV_AUX_TOO_SMALL
);
2122 return (SET_ERROR(EOVERFLOW
));
2126 max_asize
= max_osize
;
2130 * If the vdev was expanded, record this so that we can re-create the
2131 * uberblock rings in labels {2,3}, during the next sync.
2133 if ((psize
> vd
->vdev_psize
) && (vd
->vdev_psize
!= 0))
2134 vd
->vdev_copy_uberblocks
= B_TRUE
;
2136 vd
->vdev_psize
= psize
;
2139 * Make sure the allocatable size hasn't shrunk too much.
2141 if (asize
< vd
->vdev_min_asize
) {
2142 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2143 VDEV_AUX_BAD_LABEL
);
2144 return (SET_ERROR(EINVAL
));
2148 * We can always set the logical/physical ashift members since
2149 * their values are only used to calculate the vdev_ashift when
2150 * the device is first added to the config. These values should
2151 * not be used for anything else since they may change whenever
2152 * the device is reopened and we don't store them in the label.
2154 vd
->vdev_physical_ashift
=
2155 MAX(physical_ashift
, vd
->vdev_physical_ashift
);
2156 vd
->vdev_logical_ashift
= MAX(logical_ashift
,
2157 vd
->vdev_logical_ashift
);
2159 if (vd
->vdev_asize
== 0) {
2161 * This is the first-ever open, so use the computed values.
2162 * For compatibility, a different ashift can be requested.
2164 vd
->vdev_asize
= asize
;
2165 vd
->vdev_max_asize
= max_asize
;
2168 * If the vdev_ashift was not overridden at creation time,
2169 * then set it the logical ashift and optimize the ashift.
2171 if (vd
->vdev_ashift
== 0) {
2172 vd
->vdev_ashift
= vd
->vdev_logical_ashift
;
2174 if (vd
->vdev_logical_ashift
> ASHIFT_MAX
) {
2175 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2176 VDEV_AUX_ASHIFT_TOO_BIG
);
2177 return (SET_ERROR(EDOM
));
2180 if (vd
->vdev_top
== vd
&& vd
->vdev_attaching
== B_FALSE
)
2181 vdev_ashift_optimize(vd
);
2182 vd
->vdev_attaching
= B_FALSE
;
2184 if (vd
->vdev_ashift
!= 0 && (vd
->vdev_ashift
< ASHIFT_MIN
||
2185 vd
->vdev_ashift
> ASHIFT_MAX
)) {
2186 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2187 VDEV_AUX_BAD_ASHIFT
);
2188 return (SET_ERROR(EDOM
));
2192 * Make sure the alignment required hasn't increased.
2194 if (vd
->vdev_ashift
> vd
->vdev_top
->vdev_ashift
&&
2195 vd
->vdev_ops
->vdev_op_leaf
) {
2196 (void) zfs_ereport_post(
2197 FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
,
2198 spa
, vd
, NULL
, NULL
, 0);
2199 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2200 VDEV_AUX_BAD_LABEL
);
2201 return (SET_ERROR(EDOM
));
2203 vd
->vdev_max_asize
= max_asize
;
2207 * If all children are healthy we update asize if either:
2208 * The asize has increased, due to a device expansion caused by dynamic
2209 * LUN growth or vdev replacement, and automatic expansion is enabled;
2210 * making the additional space available.
2212 * The asize has decreased, due to a device shrink usually caused by a
2213 * vdev replace with a smaller device. This ensures that calculations
2214 * based of max_asize and asize e.g. esize are always valid. It's safe
2215 * to do this as we've already validated that asize is greater than
2218 if (vd
->vdev_state
== VDEV_STATE_HEALTHY
&&
2219 ((asize
> vd
->vdev_asize
&&
2220 (vd
->vdev_expanding
|| spa
->spa_autoexpand
)) ||
2221 (asize
< vd
->vdev_asize
)))
2222 vd
->vdev_asize
= asize
;
2224 vdev_set_min_asize(vd
);
2227 * Ensure we can issue some IO before declaring the
2228 * vdev open for business.
2230 if (vd
->vdev_ops
->vdev_op_leaf
&&
2231 (error
= zio_wait(vdev_probe(vd
, NULL
))) != 0) {
2232 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
2233 VDEV_AUX_ERR_EXCEEDED
);
2238 * Track the minimum allocation size.
2240 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
2241 vd
->vdev_islog
== 0 && vd
->vdev_aux
== NULL
) {
2242 uint64_t min_alloc
= vdev_get_min_alloc(vd
);
2243 vdev_spa_set_alloc(spa
, min_alloc
);
2247 * If this is a leaf vdev, assess whether a resilver is needed.
2248 * But don't do this if we are doing a reopen for a scrub, since
2249 * this would just restart the scrub we are already doing.
2251 if (vd
->vdev_ops
->vdev_op_leaf
&& !spa
->spa_scrub_reopen
)
2252 dsl_scan_assess_vdev(spa
->spa_dsl_pool
, vd
);
2258 vdev_validate_child(void *arg
)
2262 vd
->vdev_validate_thread
= curthread
;
2263 vd
->vdev_validate_error
= vdev_validate(vd
);
2264 vd
->vdev_validate_thread
= NULL
;
2268 * Called once the vdevs are all opened, this routine validates the label
2269 * contents. This needs to be done before vdev_load() so that we don't
2270 * inadvertently do repair I/Os to the wrong device.
2272 * This function will only return failure if one of the vdevs indicates that it
2273 * has since been destroyed or exported. This is only possible if
2274 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
2275 * will be updated but the function will return 0.
2278 vdev_validate(vdev_t
*vd
)
2280 spa_t
*spa
= vd
->vdev_spa
;
2283 uint64_t guid
= 0, aux_guid
= 0, top_guid
;
2287 int children
= vd
->vdev_children
;
2289 if (vdev_validate_skip
)
2293 tq
= taskq_create("vdev_validate", children
, minclsyspri
,
2294 children
, children
, TASKQ_PREPOPULATE
);
2297 for (uint64_t c
= 0; c
< children
; c
++) {
2298 vdev_t
*cvd
= vd
->vdev_child
[c
];
2300 if (tq
== NULL
|| vdev_uses_zvols(cvd
)) {
2301 vdev_validate_child(cvd
);
2303 VERIFY(taskq_dispatch(tq
, vdev_validate_child
, cvd
,
2304 TQ_SLEEP
) != TASKQID_INVALID
);
2311 for (int c
= 0; c
< children
; c
++) {
2312 int error
= vd
->vdev_child
[c
]->vdev_validate_error
;
2315 return (SET_ERROR(EBADF
));
2320 * If the device has already failed, or was marked offline, don't do
2321 * any further validation. Otherwise, label I/O will fail and we will
2322 * overwrite the previous state.
2324 if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_readable(vd
))
2328 * If we are performing an extreme rewind, we allow for a label that
2329 * was modified at a point after the current txg.
2330 * If config lock is not held do not check for the txg. spa_sync could
2331 * be updating the vdev's label before updating spa_last_synced_txg.
2333 if (spa
->spa_extreme_rewind
|| spa_last_synced_txg(spa
) == 0 ||
2334 spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) != SCL_CONFIG
)
2337 txg
= spa_last_synced_txg(spa
);
2339 if ((label
= vdev_label_read_config(vd
, txg
)) == NULL
) {
2340 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2341 VDEV_AUX_BAD_LABEL
);
2342 vdev_dbgmsg(vd
, "vdev_validate: failed reading config for "
2343 "txg %llu", (u_longlong_t
)txg
);
2348 * Determine if this vdev has been split off into another
2349 * pool. If so, then refuse to open it.
2351 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_SPLIT_GUID
,
2352 &aux_guid
) == 0 && aux_guid
== spa_guid(spa
)) {
2353 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2354 VDEV_AUX_SPLIT_POOL
);
2356 vdev_dbgmsg(vd
, "vdev_validate: vdev split into other pool");
2360 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0) {
2361 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2362 VDEV_AUX_CORRUPT_DATA
);
2364 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2365 ZPOOL_CONFIG_POOL_GUID
);
2370 * If config is not trusted then ignore the spa guid check. This is
2371 * necessary because if the machine crashed during a re-guid the new
2372 * guid might have been written to all of the vdev labels, but not the
2373 * cached config. The check will be performed again once we have the
2374 * trusted config from the MOS.
2376 if (spa
->spa_trust_config
&& guid
!= spa_guid(spa
)) {
2377 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2378 VDEV_AUX_CORRUPT_DATA
);
2380 vdev_dbgmsg(vd
, "vdev_validate: vdev label pool_guid doesn't "
2381 "match config (%llu != %llu)", (u_longlong_t
)guid
,
2382 (u_longlong_t
)spa_guid(spa
));
2386 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
)
2387 != 0 || nvlist_lookup_uint64(nvl
, ZPOOL_CONFIG_ORIG_GUID
,
2391 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0) {
2392 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2393 VDEV_AUX_CORRUPT_DATA
);
2395 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2400 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_TOP_GUID
, &top_guid
)
2402 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2403 VDEV_AUX_CORRUPT_DATA
);
2405 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2406 ZPOOL_CONFIG_TOP_GUID
);
2411 * If this vdev just became a top-level vdev because its sibling was
2412 * detached, it will have adopted the parent's vdev guid -- but the
2413 * label may or may not be on disk yet. Fortunately, either version
2414 * of the label will have the same top guid, so if we're a top-level
2415 * vdev, we can safely compare to that instead.
2416 * However, if the config comes from a cachefile that failed to update
2417 * after the detach, a top-level vdev will appear as a non top-level
2418 * vdev in the config. Also relax the constraints if we perform an
2421 * If we split this vdev off instead, then we also check the
2422 * original pool's guid. We don't want to consider the vdev
2423 * corrupt if it is partway through a split operation.
2425 if (vd
->vdev_guid
!= guid
&& vd
->vdev_guid
!= aux_guid
) {
2426 boolean_t mismatch
= B_FALSE
;
2427 if (spa
->spa_trust_config
&& !spa
->spa_extreme_rewind
) {
2428 if (vd
!= vd
->vdev_top
|| vd
->vdev_guid
!= top_guid
)
2431 if (vd
->vdev_guid
!= top_guid
&&
2432 vd
->vdev_top
->vdev_guid
!= guid
)
2437 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2438 VDEV_AUX_CORRUPT_DATA
);
2440 vdev_dbgmsg(vd
, "vdev_validate: config guid "
2441 "doesn't match label guid");
2442 vdev_dbgmsg(vd
, "CONFIG: guid %llu, top_guid %llu",
2443 (u_longlong_t
)vd
->vdev_guid
,
2444 (u_longlong_t
)vd
->vdev_top
->vdev_guid
);
2445 vdev_dbgmsg(vd
, "LABEL: guid %llu, top_guid %llu, "
2446 "aux_guid %llu", (u_longlong_t
)guid
,
2447 (u_longlong_t
)top_guid
, (u_longlong_t
)aux_guid
);
2452 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
,
2454 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2455 VDEV_AUX_CORRUPT_DATA
);
2457 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2458 ZPOOL_CONFIG_POOL_STATE
);
2465 * If this is a verbatim import, no need to check the
2466 * state of the pool.
2468 if (!(spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) &&
2469 spa_load_state(spa
) == SPA_LOAD_OPEN
&&
2470 state
!= POOL_STATE_ACTIVE
) {
2471 vdev_dbgmsg(vd
, "vdev_validate: invalid pool state (%llu) "
2472 "for spa %s", (u_longlong_t
)state
, spa
->spa_name
);
2473 return (SET_ERROR(EBADF
));
2477 * If we were able to open and validate a vdev that was
2478 * previously marked permanently unavailable, clear that state
2481 if (vd
->vdev_not_present
)
2482 vd
->vdev_not_present
= 0;
2488 vdev_copy_path_impl(vdev_t
*svd
, vdev_t
*dvd
)
2491 if (svd
->vdev_path
!= NULL
&& dvd
->vdev_path
!= NULL
) {
2492 if (strcmp(svd
->vdev_path
, dvd
->vdev_path
) != 0) {
2493 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
2494 "from '%s' to '%s'", (u_longlong_t
)dvd
->vdev_guid
,
2495 dvd
->vdev_path
, svd
->vdev_path
);
2496 spa_strfree(dvd
->vdev_path
);
2497 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
2499 } else if (svd
->vdev_path
!= NULL
) {
2500 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
2501 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
2502 (u_longlong_t
)dvd
->vdev_guid
, dvd
->vdev_path
);
2506 * Our enclosure sysfs path may have changed between imports
2508 old
= dvd
->vdev_enc_sysfs_path
;
2509 new = svd
->vdev_enc_sysfs_path
;
2510 if ((old
!= NULL
&& new == NULL
) ||
2511 (old
== NULL
&& new != NULL
) ||
2512 ((old
!= NULL
&& new != NULL
) && strcmp(new, old
) != 0)) {
2513 zfs_dbgmsg("vdev_copy_path: vdev %llu: vdev_enc_sysfs_path "
2514 "changed from '%s' to '%s'", (u_longlong_t
)dvd
->vdev_guid
,
2517 if (dvd
->vdev_enc_sysfs_path
)
2518 spa_strfree(dvd
->vdev_enc_sysfs_path
);
2520 if (svd
->vdev_enc_sysfs_path
) {
2521 dvd
->vdev_enc_sysfs_path
= spa_strdup(
2522 svd
->vdev_enc_sysfs_path
);
2524 dvd
->vdev_enc_sysfs_path
= NULL
;
2530 * Recursively copy vdev paths from one vdev to another. Source and destination
2531 * vdev trees must have same geometry otherwise return error. Intended to copy
2532 * paths from userland config into MOS config.
2535 vdev_copy_path_strict(vdev_t
*svd
, vdev_t
*dvd
)
2537 if ((svd
->vdev_ops
== &vdev_missing_ops
) ||
2538 (svd
->vdev_ishole
&& dvd
->vdev_ishole
) ||
2539 (dvd
->vdev_ops
== &vdev_indirect_ops
))
2542 if (svd
->vdev_ops
!= dvd
->vdev_ops
) {
2543 vdev_dbgmsg(svd
, "vdev_copy_path: vdev type mismatch: %s != %s",
2544 svd
->vdev_ops
->vdev_op_type
, dvd
->vdev_ops
->vdev_op_type
);
2545 return (SET_ERROR(EINVAL
));
2548 if (svd
->vdev_guid
!= dvd
->vdev_guid
) {
2549 vdev_dbgmsg(svd
, "vdev_copy_path: guids mismatch (%llu != "
2550 "%llu)", (u_longlong_t
)svd
->vdev_guid
,
2551 (u_longlong_t
)dvd
->vdev_guid
);
2552 return (SET_ERROR(EINVAL
));
2555 if (svd
->vdev_children
!= dvd
->vdev_children
) {
2556 vdev_dbgmsg(svd
, "vdev_copy_path: children count mismatch: "
2557 "%llu != %llu", (u_longlong_t
)svd
->vdev_children
,
2558 (u_longlong_t
)dvd
->vdev_children
);
2559 return (SET_ERROR(EINVAL
));
2562 for (uint64_t i
= 0; i
< svd
->vdev_children
; i
++) {
2563 int error
= vdev_copy_path_strict(svd
->vdev_child
[i
],
2564 dvd
->vdev_child
[i
]);
2569 if (svd
->vdev_ops
->vdev_op_leaf
)
2570 vdev_copy_path_impl(svd
, dvd
);
2576 vdev_copy_path_search(vdev_t
*stvd
, vdev_t
*dvd
)
2578 ASSERT(stvd
->vdev_top
== stvd
);
2579 ASSERT3U(stvd
->vdev_id
, ==, dvd
->vdev_top
->vdev_id
);
2581 for (uint64_t i
= 0; i
< dvd
->vdev_children
; i
++) {
2582 vdev_copy_path_search(stvd
, dvd
->vdev_child
[i
]);
2585 if (!dvd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(dvd
))
2589 * The idea here is that while a vdev can shift positions within
2590 * a top vdev (when replacing, attaching mirror, etc.) it cannot
2591 * step outside of it.
2593 vdev_t
*vd
= vdev_lookup_by_guid(stvd
, dvd
->vdev_guid
);
2595 if (vd
== NULL
|| vd
->vdev_ops
!= dvd
->vdev_ops
)
2598 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2600 vdev_copy_path_impl(vd
, dvd
);
2604 * Recursively copy vdev paths from one root vdev to another. Source and
2605 * destination vdev trees may differ in geometry. For each destination leaf
2606 * vdev, search a vdev with the same guid and top vdev id in the source.
2607 * Intended to copy paths from userland config into MOS config.
2610 vdev_copy_path_relaxed(vdev_t
*srvd
, vdev_t
*drvd
)
2612 uint64_t children
= MIN(srvd
->vdev_children
, drvd
->vdev_children
);
2613 ASSERT(srvd
->vdev_ops
== &vdev_root_ops
);
2614 ASSERT(drvd
->vdev_ops
== &vdev_root_ops
);
2616 for (uint64_t i
= 0; i
< children
; i
++) {
2617 vdev_copy_path_search(srvd
->vdev_child
[i
],
2618 drvd
->vdev_child
[i
]);
2623 * Close a virtual device.
2626 vdev_close(vdev_t
*vd
)
2628 vdev_t
*pvd
= vd
->vdev_parent
;
2629 spa_t
*spa __maybe_unused
= vd
->vdev_spa
;
2632 ASSERT(vd
->vdev_open_thread
== curthread
||
2633 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2636 * If our parent is reopening, then we are as well, unless we are
2639 if (pvd
!= NULL
&& pvd
->vdev_reopening
)
2640 vd
->vdev_reopening
= (pvd
->vdev_reopening
&& !vd
->vdev_offline
);
2642 vd
->vdev_ops
->vdev_op_close(vd
);
2645 * We record the previous state before we close it, so that if we are
2646 * doing a reopen(), we don't generate FMA ereports if we notice that
2647 * it's still faulted.
2649 vd
->vdev_prevstate
= vd
->vdev_state
;
2651 if (vd
->vdev_offline
)
2652 vd
->vdev_state
= VDEV_STATE_OFFLINE
;
2654 vd
->vdev_state
= VDEV_STATE_CLOSED
;
2655 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
2659 vdev_hold(vdev_t
*vd
)
2661 spa_t
*spa
= vd
->vdev_spa
;
2663 ASSERT(spa_is_root(spa
));
2664 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
2667 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2668 vdev_hold(vd
->vdev_child
[c
]);
2670 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_ops
->vdev_op_hold
!= NULL
)
2671 vd
->vdev_ops
->vdev_op_hold(vd
);
2675 vdev_rele(vdev_t
*vd
)
2677 ASSERT(spa_is_root(vd
->vdev_spa
));
2678 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2679 vdev_rele(vd
->vdev_child
[c
]);
2681 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_ops
->vdev_op_rele
!= NULL
)
2682 vd
->vdev_ops
->vdev_op_rele(vd
);
2686 * Reopen all interior vdevs and any unopened leaves. We don't actually
2687 * reopen leaf vdevs which had previously been opened as they might deadlock
2688 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2689 * If the leaf has never been opened then open it, as usual.
2692 vdev_reopen(vdev_t
*vd
)
2694 spa_t
*spa
= vd
->vdev_spa
;
2696 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2698 /* set the reopening flag unless we're taking the vdev offline */
2699 vd
->vdev_reopening
= !vd
->vdev_offline
;
2701 (void) vdev_open(vd
);
2704 * Call vdev_validate() here to make sure we have the same device.
2705 * Otherwise, a device with an invalid label could be successfully
2706 * opened in response to vdev_reopen().
2709 (void) vdev_validate_aux(vd
);
2710 if (vdev_readable(vd
) && vdev_writeable(vd
) &&
2711 vd
->vdev_aux
== &spa
->spa_l2cache
) {
2713 * In case the vdev is present we should evict all ARC
2714 * buffers and pointers to log blocks and reclaim their
2715 * space before restoring its contents to L2ARC.
2717 if (l2arc_vdev_present(vd
)) {
2718 l2arc_rebuild_vdev(vd
, B_TRUE
);
2720 l2arc_add_vdev(spa
, vd
);
2722 spa_async_request(spa
, SPA_ASYNC_L2CACHE_REBUILD
);
2723 spa_async_request(spa
, SPA_ASYNC_L2CACHE_TRIM
);
2726 (void) vdev_validate(vd
);
2730 * Recheck if resilver is still needed and cancel any
2731 * scheduled resilver if resilver is unneeded.
2733 if (!vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
) &&
2734 spa
->spa_async_tasks
& SPA_ASYNC_RESILVER
) {
2735 mutex_enter(&spa
->spa_async_lock
);
2736 spa
->spa_async_tasks
&= ~SPA_ASYNC_RESILVER
;
2737 mutex_exit(&spa
->spa_async_lock
);
2741 * Reassess parent vdev's health.
2743 vdev_propagate_state(vd
);
2747 vdev_create(vdev_t
*vd
, uint64_t txg
, boolean_t isreplacing
)
2752 * Normally, partial opens (e.g. of a mirror) are allowed.
2753 * For a create, however, we want to fail the request if
2754 * there are any components we can't open.
2756 error
= vdev_open(vd
);
2758 if (error
|| vd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
2760 return (error
? error
: SET_ERROR(ENXIO
));
2764 * Recursively load DTLs and initialize all labels.
2766 if ((error
= vdev_dtl_load(vd
)) != 0 ||
2767 (error
= vdev_label_init(vd
, txg
, isreplacing
?
2768 VDEV_LABEL_REPLACE
: VDEV_LABEL_CREATE
)) != 0) {
2777 vdev_metaslab_set_size(vdev_t
*vd
)
2779 uint64_t asize
= vd
->vdev_asize
;
2780 uint64_t ms_count
= asize
>> zfs_vdev_default_ms_shift
;
2784 * There are two dimensions to the metaslab sizing calculation:
2785 * the size of the metaslab and the count of metaslabs per vdev.
2787 * The default values used below are a good balance between memory
2788 * usage (larger metaslab size means more memory needed for loaded
2789 * metaslabs; more metaslabs means more memory needed for the
2790 * metaslab_t structs), metaslab load time (larger metaslabs take
2791 * longer to load), and metaslab sync time (more metaslabs means
2792 * more time spent syncing all of them).
2794 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2795 * The range of the dimensions are as follows:
2797 * 2^29 <= ms_size <= 2^34
2798 * 16 <= ms_count <= 131,072
2800 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2801 * at least 512MB (2^29) to minimize fragmentation effects when
2802 * testing with smaller devices. However, the count constraint
2803 * of at least 16 metaslabs will override this minimum size goal.
2805 * On the upper end of vdev sizes, we aim for a maximum metaslab
2806 * size of 16GB. However, we will cap the total count to 2^17
2807 * metaslabs to keep our memory footprint in check and let the
2808 * metaslab size grow from there if that limit is hit.
2810 * The net effect of applying above constrains is summarized below.
2812 * vdev size metaslab count
2813 * --------------|-----------------
2815 * 8GB - 100GB one per 512MB
2817 * 3TB - 2PB one per 16GB
2819 * --------------------------------
2821 * Finally, note that all of the above calculate the initial
2822 * number of metaslabs. Expanding a top-level vdev will result
2823 * in additional metaslabs being allocated making it possible
2824 * to exceed the zfs_vdev_ms_count_limit.
2827 if (ms_count
< zfs_vdev_min_ms_count
)
2828 ms_shift
= highbit64(asize
/ zfs_vdev_min_ms_count
);
2829 else if (ms_count
> zfs_vdev_default_ms_count
)
2830 ms_shift
= highbit64(asize
/ zfs_vdev_default_ms_count
);
2832 ms_shift
= zfs_vdev_default_ms_shift
;
2834 if (ms_shift
< SPA_MAXBLOCKSHIFT
) {
2835 ms_shift
= SPA_MAXBLOCKSHIFT
;
2836 } else if (ms_shift
> zfs_vdev_max_ms_shift
) {
2837 ms_shift
= zfs_vdev_max_ms_shift
;
2838 /* cap the total count to constrain memory footprint */
2839 if ((asize
>> ms_shift
) > zfs_vdev_ms_count_limit
)
2840 ms_shift
= highbit64(asize
/ zfs_vdev_ms_count_limit
);
2843 vd
->vdev_ms_shift
= ms_shift
;
2844 ASSERT3U(vd
->vdev_ms_shift
, >=, SPA_MAXBLOCKSHIFT
);
2848 vdev_dirty(vdev_t
*vd
, int flags
, void *arg
, uint64_t txg
)
2850 ASSERT(vd
== vd
->vdev_top
);
2851 /* indirect vdevs don't have metaslabs or dtls */
2852 ASSERT(vdev_is_concrete(vd
) || flags
== 0);
2853 ASSERT(ISP2(flags
));
2854 ASSERT(spa_writeable(vd
->vdev_spa
));
2856 if (flags
& VDD_METASLAB
)
2857 (void) txg_list_add(&vd
->vdev_ms_list
, arg
, txg
);
2859 if (flags
& VDD_DTL
)
2860 (void) txg_list_add(&vd
->vdev_dtl_list
, arg
, txg
);
2862 (void) txg_list_add(&vd
->vdev_spa
->spa_vdev_txg_list
, vd
, txg
);
2866 vdev_dirty_leaves(vdev_t
*vd
, int flags
, uint64_t txg
)
2868 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2869 vdev_dirty_leaves(vd
->vdev_child
[c
], flags
, txg
);
2871 if (vd
->vdev_ops
->vdev_op_leaf
)
2872 vdev_dirty(vd
->vdev_top
, flags
, vd
, txg
);
2878 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2879 * the vdev has less than perfect replication. There are four kinds of DTL:
2881 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2883 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2885 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2886 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2887 * txgs that was scrubbed.
2889 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2890 * persistent errors or just some device being offline.
2891 * Unlike the other three, the DTL_OUTAGE map is not generally
2892 * maintained; it's only computed when needed, typically to
2893 * determine whether a device can be detached.
2895 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2896 * either has the data or it doesn't.
2898 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2899 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2900 * if any child is less than fully replicated, then so is its parent.
2901 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2902 * comprising only those txgs which appear in 'maxfaults' or more children;
2903 * those are the txgs we don't have enough replication to read. For example,
2904 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2905 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2906 * two child DTL_MISSING maps.
2908 * It should be clear from the above that to compute the DTLs and outage maps
2909 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2910 * Therefore, that is all we keep on disk. When loading the pool, or after
2911 * a configuration change, we generate all other DTLs from first principles.
2914 vdev_dtl_dirty(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2916 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2918 ASSERT(t
< DTL_TYPES
);
2919 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2920 ASSERT(spa_writeable(vd
->vdev_spa
));
2922 mutex_enter(&vd
->vdev_dtl_lock
);
2923 if (!range_tree_contains(rt
, txg
, size
))
2924 range_tree_add(rt
, txg
, size
);
2925 mutex_exit(&vd
->vdev_dtl_lock
);
2929 vdev_dtl_contains(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2931 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2932 boolean_t dirty
= B_FALSE
;
2934 ASSERT(t
< DTL_TYPES
);
2935 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2938 * While we are loading the pool, the DTLs have not been loaded yet.
2939 * This isn't a problem but it can result in devices being tried
2940 * which are known to not have the data. In which case, the import
2941 * is relying on the checksum to ensure that we get the right data.
2942 * Note that while importing we are only reading the MOS, which is
2943 * always checksummed.
2945 mutex_enter(&vd
->vdev_dtl_lock
);
2946 if (!range_tree_is_empty(rt
))
2947 dirty
= range_tree_contains(rt
, txg
, size
);
2948 mutex_exit(&vd
->vdev_dtl_lock
);
2954 vdev_dtl_empty(vdev_t
*vd
, vdev_dtl_type_t t
)
2956 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2959 mutex_enter(&vd
->vdev_dtl_lock
);
2960 empty
= range_tree_is_empty(rt
);
2961 mutex_exit(&vd
->vdev_dtl_lock
);
2967 * Check if the txg falls within the range which must be
2968 * resilvered. DVAs outside this range can always be skipped.
2971 vdev_default_need_resilver(vdev_t
*vd
, const dva_t
*dva
, size_t psize
,
2972 uint64_t phys_birth
)
2974 (void) dva
, (void) psize
;
2976 /* Set by sequential resilver. */
2977 if (phys_birth
== TXG_UNKNOWN
)
2980 return (vdev_dtl_contains(vd
, DTL_PARTIAL
, phys_birth
, 1));
2984 * Returns B_TRUE if the vdev determines the DVA needs to be resilvered.
2987 vdev_dtl_need_resilver(vdev_t
*vd
, const dva_t
*dva
, size_t psize
,
2988 uint64_t phys_birth
)
2990 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2992 if (vd
->vdev_ops
->vdev_op_need_resilver
== NULL
||
2993 vd
->vdev_ops
->vdev_op_leaf
)
2996 return (vd
->vdev_ops
->vdev_op_need_resilver(vd
, dva
, psize
,
3001 * Returns the lowest txg in the DTL range.
3004 vdev_dtl_min(vdev_t
*vd
)
3006 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
3007 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
3008 ASSERT0(vd
->vdev_children
);
3010 return (range_tree_min(vd
->vdev_dtl
[DTL_MISSING
]) - 1);
3014 * Returns the highest txg in the DTL.
3017 vdev_dtl_max(vdev_t
*vd
)
3019 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
3020 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
3021 ASSERT0(vd
->vdev_children
);
3023 return (range_tree_max(vd
->vdev_dtl
[DTL_MISSING
]));
3027 * Determine if a resilvering vdev should remove any DTL entries from
3028 * its range. If the vdev was resilvering for the entire duration of the
3029 * scan then it should excise that range from its DTLs. Otherwise, this
3030 * vdev is considered partially resilvered and should leave its DTL
3031 * entries intact. The comment in vdev_dtl_reassess() describes how we
3035 vdev_dtl_should_excise(vdev_t
*vd
, boolean_t rebuild_done
)
3037 ASSERT0(vd
->vdev_children
);
3039 if (vd
->vdev_state
< VDEV_STATE_DEGRADED
)
3042 if (vd
->vdev_resilver_deferred
)
3045 if (range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]))
3049 vdev_rebuild_t
*vr
= &vd
->vdev_top
->vdev_rebuild_config
;
3050 vdev_rebuild_phys_t
*vrp
= &vr
->vr_rebuild_phys
;
3052 /* Rebuild not initiated by attach */
3053 if (vd
->vdev_rebuild_txg
== 0)
3057 * When a rebuild completes without error then all missing data
3058 * up to the rebuild max txg has been reconstructed and the DTL
3059 * is eligible for excision.
3061 if (vrp
->vrp_rebuild_state
== VDEV_REBUILD_COMPLETE
&&
3062 vdev_dtl_max(vd
) <= vrp
->vrp_max_txg
) {
3063 ASSERT3U(vrp
->vrp_min_txg
, <=, vdev_dtl_min(vd
));
3064 ASSERT3U(vrp
->vrp_min_txg
, <, vd
->vdev_rebuild_txg
);
3065 ASSERT3U(vd
->vdev_rebuild_txg
, <=, vrp
->vrp_max_txg
);
3069 dsl_scan_t
*scn
= vd
->vdev_spa
->spa_dsl_pool
->dp_scan
;
3070 dsl_scan_phys_t
*scnp __maybe_unused
= &scn
->scn_phys
;
3072 /* Resilver not initiated by attach */
3073 if (vd
->vdev_resilver_txg
== 0)
3077 * When a resilver is initiated the scan will assign the
3078 * scn_max_txg value to the highest txg value that exists
3079 * in all DTLs. If this device's max DTL is not part of this
3080 * scan (i.e. it is not in the range (scn_min_txg, scn_max_txg]
3081 * then it is not eligible for excision.
3083 if (vdev_dtl_max(vd
) <= scn
->scn_phys
.scn_max_txg
) {
3084 ASSERT3U(scnp
->scn_min_txg
, <=, vdev_dtl_min(vd
));
3085 ASSERT3U(scnp
->scn_min_txg
, <, vd
->vdev_resilver_txg
);
3086 ASSERT3U(vd
->vdev_resilver_txg
, <=, scnp
->scn_max_txg
);
3095 * Reassess DTLs after a config change or scrub completion. If txg == 0 no
3096 * write operations will be issued to the pool.
3099 vdev_dtl_reassess(vdev_t
*vd
, uint64_t txg
, uint64_t scrub_txg
,
3100 boolean_t scrub_done
, boolean_t rebuild_done
)
3102 spa_t
*spa
= vd
->vdev_spa
;
3106 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
3108 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3109 vdev_dtl_reassess(vd
->vdev_child
[c
], txg
,
3110 scrub_txg
, scrub_done
, rebuild_done
);
3112 if (vd
== spa
->spa_root_vdev
|| !vdev_is_concrete(vd
) || vd
->vdev_aux
)
3115 if (vd
->vdev_ops
->vdev_op_leaf
) {
3116 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
3117 vdev_rebuild_t
*vr
= &vd
->vdev_top
->vdev_rebuild_config
;
3118 boolean_t check_excise
= B_FALSE
;
3119 boolean_t wasempty
= B_TRUE
;
3121 mutex_enter(&vd
->vdev_dtl_lock
);
3124 * If requested, pretend the scan or rebuild completed cleanly.
3126 if (zfs_scan_ignore_errors
) {
3128 scn
->scn_phys
.scn_errors
= 0;
3130 vr
->vr_rebuild_phys
.vrp_errors
= 0;
3133 if (scrub_txg
!= 0 &&
3134 !range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
])) {
3136 zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d "
3137 "dtl:%llu/%llu errors:%llu",
3138 (u_longlong_t
)vd
->vdev_guid
, (u_longlong_t
)txg
,
3139 (u_longlong_t
)scrub_txg
, spa
->spa_scrub_started
,
3140 (u_longlong_t
)vdev_dtl_min(vd
),
3141 (u_longlong_t
)vdev_dtl_max(vd
),
3142 (u_longlong_t
)(scn
? scn
->scn_phys
.scn_errors
: 0));
3146 * If we've completed a scrub/resilver or a rebuild cleanly
3147 * then determine if this vdev should remove any DTLs. We
3148 * only want to excise regions on vdevs that were available
3149 * during the entire duration of this scan.
3152 vr
!= NULL
&& vr
->vr_rebuild_phys
.vrp_errors
== 0) {
3153 check_excise
= B_TRUE
;
3155 if (spa
->spa_scrub_started
||
3156 (scn
!= NULL
&& scn
->scn_phys
.scn_errors
== 0)) {
3157 check_excise
= B_TRUE
;
3161 if (scrub_txg
&& check_excise
&&
3162 vdev_dtl_should_excise(vd
, rebuild_done
)) {
3164 * We completed a scrub, resilver or rebuild up to
3165 * scrub_txg. If we did it without rebooting, then
3166 * the scrub dtl will be valid, so excise the old
3167 * region and fold in the scrub dtl. Otherwise,
3168 * leave the dtl as-is if there was an error.
3170 * There's little trick here: to excise the beginning
3171 * of the DTL_MISSING map, we put it into a reference
3172 * tree and then add a segment with refcnt -1 that
3173 * covers the range [0, scrub_txg). This means
3174 * that each txg in that range has refcnt -1 or 0.
3175 * We then add DTL_SCRUB with a refcnt of 2, so that
3176 * entries in the range [0, scrub_txg) will have a
3177 * positive refcnt -- either 1 or 2. We then convert
3178 * the reference tree into the new DTL_MISSING map.
3180 space_reftree_create(&reftree
);
3181 space_reftree_add_map(&reftree
,
3182 vd
->vdev_dtl
[DTL_MISSING
], 1);
3183 space_reftree_add_seg(&reftree
, 0, scrub_txg
, -1);
3184 space_reftree_add_map(&reftree
,
3185 vd
->vdev_dtl
[DTL_SCRUB
], 2);
3186 space_reftree_generate_map(&reftree
,
3187 vd
->vdev_dtl
[DTL_MISSING
], 1);
3188 space_reftree_destroy(&reftree
);
3190 if (!range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
])) {
3191 zfs_dbgmsg("update DTL_MISSING:%llu/%llu",
3192 (u_longlong_t
)vdev_dtl_min(vd
),
3193 (u_longlong_t
)vdev_dtl_max(vd
));
3194 } else if (!wasempty
) {
3195 zfs_dbgmsg("DTL_MISSING is now empty");
3198 range_tree_vacate(vd
->vdev_dtl
[DTL_PARTIAL
], NULL
, NULL
);
3199 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
3200 range_tree_add
, vd
->vdev_dtl
[DTL_PARTIAL
]);
3202 range_tree_vacate(vd
->vdev_dtl
[DTL_SCRUB
], NULL
, NULL
);
3203 range_tree_vacate(vd
->vdev_dtl
[DTL_OUTAGE
], NULL
, NULL
);
3204 if (!vdev_readable(vd
))
3205 range_tree_add(vd
->vdev_dtl
[DTL_OUTAGE
], 0, -1ULL);
3207 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
3208 range_tree_add
, vd
->vdev_dtl
[DTL_OUTAGE
]);
3211 * If the vdev was resilvering or rebuilding and no longer
3212 * has any DTLs then reset the appropriate flag and dirty
3213 * the top level so that we persist the change.
3216 range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]) &&
3217 range_tree_is_empty(vd
->vdev_dtl
[DTL_OUTAGE
])) {
3218 if (vd
->vdev_rebuild_txg
!= 0) {
3219 vd
->vdev_rebuild_txg
= 0;
3220 vdev_config_dirty(vd
->vdev_top
);
3221 } else if (vd
->vdev_resilver_txg
!= 0) {
3222 vd
->vdev_resilver_txg
= 0;
3223 vdev_config_dirty(vd
->vdev_top
);
3227 mutex_exit(&vd
->vdev_dtl_lock
);
3230 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, txg
);
3234 mutex_enter(&vd
->vdev_dtl_lock
);
3235 for (int t
= 0; t
< DTL_TYPES
; t
++) {
3236 /* account for child's outage in parent's missing map */
3237 int s
= (t
== DTL_MISSING
) ? DTL_OUTAGE
: t
;
3239 continue; /* leaf vdevs only */
3240 if (t
== DTL_PARTIAL
)
3241 minref
= 1; /* i.e. non-zero */
3242 else if (vdev_get_nparity(vd
) != 0)
3243 minref
= vdev_get_nparity(vd
) + 1; /* RAID-Z, dRAID */
3245 minref
= vd
->vdev_children
; /* any kind of mirror */
3246 space_reftree_create(&reftree
);
3247 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3248 vdev_t
*cvd
= vd
->vdev_child
[c
];
3249 mutex_enter(&cvd
->vdev_dtl_lock
);
3250 space_reftree_add_map(&reftree
, cvd
->vdev_dtl
[s
], 1);
3251 mutex_exit(&cvd
->vdev_dtl_lock
);
3253 space_reftree_generate_map(&reftree
, vd
->vdev_dtl
[t
], minref
);
3254 space_reftree_destroy(&reftree
);
3256 mutex_exit(&vd
->vdev_dtl_lock
);
3260 * Iterate over all the vdevs except spare, and post kobj events
3263 vdev_post_kobj_evt(vdev_t
*vd
)
3265 if (vd
->vdev_ops
->vdev_op_kobj_evt_post
&&
3266 vd
->vdev_kobj_flag
== B_FALSE
) {
3267 vd
->vdev_kobj_flag
= B_TRUE
;
3268 vd
->vdev_ops
->vdev_op_kobj_evt_post(vd
);
3271 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3272 vdev_post_kobj_evt(vd
->vdev_child
[c
]);
3276 * Iterate over all the vdevs except spare, and clear kobj events
3279 vdev_clear_kobj_evt(vdev_t
*vd
)
3281 vd
->vdev_kobj_flag
= B_FALSE
;
3283 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3284 vdev_clear_kobj_evt(vd
->vdev_child
[c
]);
3288 vdev_dtl_load(vdev_t
*vd
)
3290 spa_t
*spa
= vd
->vdev_spa
;
3291 objset_t
*mos
= spa
->spa_meta_objset
;
3295 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_dtl_object
!= 0) {
3296 ASSERT(vdev_is_concrete(vd
));
3299 * If the dtl cannot be sync'd there is no need to open it.
3301 if (spa
->spa_mode
== SPA_MODE_READ
&& !spa
->spa_read_spacemaps
)
3304 error
= space_map_open(&vd
->vdev_dtl_sm
, mos
,
3305 vd
->vdev_dtl_object
, 0, -1ULL, 0);
3308 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
3310 rt
= range_tree_create(NULL
, RANGE_SEG64
, NULL
, 0, 0);
3311 error
= space_map_load(vd
->vdev_dtl_sm
, rt
, SM_ALLOC
);
3313 mutex_enter(&vd
->vdev_dtl_lock
);
3314 range_tree_walk(rt
, range_tree_add
,
3315 vd
->vdev_dtl
[DTL_MISSING
]);
3316 mutex_exit(&vd
->vdev_dtl_lock
);
3319 range_tree_vacate(rt
, NULL
, NULL
);
3320 range_tree_destroy(rt
);
3325 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3326 error
= vdev_dtl_load(vd
->vdev_child
[c
]);
3335 vdev_zap_allocation_data(vdev_t
*vd
, dmu_tx_t
*tx
)
3337 spa_t
*spa
= vd
->vdev_spa
;
3338 objset_t
*mos
= spa
->spa_meta_objset
;
3339 vdev_alloc_bias_t alloc_bias
= vd
->vdev_alloc_bias
;
3342 ASSERT(alloc_bias
!= VDEV_BIAS_NONE
);
3345 (alloc_bias
== VDEV_BIAS_LOG
) ? VDEV_ALLOC_BIAS_LOG
:
3346 (alloc_bias
== VDEV_BIAS_SPECIAL
) ? VDEV_ALLOC_BIAS_SPECIAL
:
3347 (alloc_bias
== VDEV_BIAS_DEDUP
) ? VDEV_ALLOC_BIAS_DEDUP
: NULL
;
3349 ASSERT(string
!= NULL
);
3350 VERIFY0(zap_add(mos
, vd
->vdev_top_zap
, VDEV_TOP_ZAP_ALLOCATION_BIAS
,
3351 1, strlen(string
) + 1, string
, tx
));
3353 if (alloc_bias
== VDEV_BIAS_SPECIAL
|| alloc_bias
== VDEV_BIAS_DEDUP
) {
3354 spa_activate_allocation_classes(spa
, tx
);
3359 vdev_destroy_unlink_zap(vdev_t
*vd
, uint64_t zapobj
, dmu_tx_t
*tx
)
3361 spa_t
*spa
= vd
->vdev_spa
;
3363 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zapobj
, tx
));
3364 VERIFY0(zap_remove_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
3369 vdev_create_link_zap(vdev_t
*vd
, dmu_tx_t
*tx
)
3371 spa_t
*spa
= vd
->vdev_spa
;
3372 uint64_t zap
= zap_create(spa
->spa_meta_objset
, DMU_OTN_ZAP_METADATA
,
3373 DMU_OT_NONE
, 0, tx
);
3376 VERIFY0(zap_add_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
3383 vdev_construct_zaps(vdev_t
*vd
, dmu_tx_t
*tx
)
3385 if (vd
->vdev_ops
!= &vdev_hole_ops
&&
3386 vd
->vdev_ops
!= &vdev_missing_ops
&&
3387 vd
->vdev_ops
!= &vdev_root_ops
&&
3388 !vd
->vdev_top
->vdev_removing
) {
3389 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_leaf_zap
== 0) {
3390 vd
->vdev_leaf_zap
= vdev_create_link_zap(vd
, tx
);
3392 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
== 0) {
3393 vd
->vdev_top_zap
= vdev_create_link_zap(vd
, tx
);
3394 if (vd
->vdev_alloc_bias
!= VDEV_BIAS_NONE
)
3395 vdev_zap_allocation_data(vd
, tx
);
3398 if (vd
->vdev_ops
== &vdev_root_ops
&& vd
->vdev_root_zap
== 0 &&
3399 spa_feature_is_enabled(vd
->vdev_spa
, SPA_FEATURE_AVZ_V2
)) {
3400 if (!spa_feature_is_active(vd
->vdev_spa
, SPA_FEATURE_AVZ_V2
))
3401 spa_feature_incr(vd
->vdev_spa
, SPA_FEATURE_AVZ_V2
, tx
);
3402 vd
->vdev_root_zap
= vdev_create_link_zap(vd
, tx
);
3405 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
3406 vdev_construct_zaps(vd
->vdev_child
[i
], tx
);
3411 vdev_dtl_sync(vdev_t
*vd
, uint64_t txg
)
3413 spa_t
*spa
= vd
->vdev_spa
;
3414 range_tree_t
*rt
= vd
->vdev_dtl
[DTL_MISSING
];
3415 objset_t
*mos
= spa
->spa_meta_objset
;
3416 range_tree_t
*rtsync
;
3418 uint64_t object
= space_map_object(vd
->vdev_dtl_sm
);
3420 ASSERT(vdev_is_concrete(vd
));
3421 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3423 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
3425 if (vd
->vdev_detached
|| vd
->vdev_top
->vdev_removing
) {
3426 mutex_enter(&vd
->vdev_dtl_lock
);
3427 space_map_free(vd
->vdev_dtl_sm
, tx
);
3428 space_map_close(vd
->vdev_dtl_sm
);
3429 vd
->vdev_dtl_sm
= NULL
;
3430 mutex_exit(&vd
->vdev_dtl_lock
);
3433 * We only destroy the leaf ZAP for detached leaves or for
3434 * removed log devices. Removed data devices handle leaf ZAP
3435 * cleanup later, once cancellation is no longer possible.
3437 if (vd
->vdev_leaf_zap
!= 0 && (vd
->vdev_detached
||
3438 vd
->vdev_top
->vdev_islog
)) {
3439 vdev_destroy_unlink_zap(vd
, vd
->vdev_leaf_zap
, tx
);
3440 vd
->vdev_leaf_zap
= 0;
3447 if (vd
->vdev_dtl_sm
== NULL
) {
3448 uint64_t new_object
;
3450 new_object
= space_map_alloc(mos
, zfs_vdev_dtl_sm_blksz
, tx
);
3451 VERIFY3U(new_object
, !=, 0);
3453 VERIFY0(space_map_open(&vd
->vdev_dtl_sm
, mos
, new_object
,
3455 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
3458 rtsync
= range_tree_create(NULL
, RANGE_SEG64
, NULL
, 0, 0);
3460 mutex_enter(&vd
->vdev_dtl_lock
);
3461 range_tree_walk(rt
, range_tree_add
, rtsync
);
3462 mutex_exit(&vd
->vdev_dtl_lock
);
3464 space_map_truncate(vd
->vdev_dtl_sm
, zfs_vdev_dtl_sm_blksz
, tx
);
3465 space_map_write(vd
->vdev_dtl_sm
, rtsync
, SM_ALLOC
, SM_NO_VDEVID
, tx
);
3466 range_tree_vacate(rtsync
, NULL
, NULL
);
3468 range_tree_destroy(rtsync
);
3471 * If the object for the space map has changed then dirty
3472 * the top level so that we update the config.
3474 if (object
!= space_map_object(vd
->vdev_dtl_sm
)) {
3475 vdev_dbgmsg(vd
, "txg %llu, spa %s, DTL old object %llu, "
3476 "new object %llu", (u_longlong_t
)txg
, spa_name(spa
),
3477 (u_longlong_t
)object
,
3478 (u_longlong_t
)space_map_object(vd
->vdev_dtl_sm
));
3479 vdev_config_dirty(vd
->vdev_top
);
3486 * Determine whether the specified vdev can be offlined/detached/removed
3487 * without losing data.
3490 vdev_dtl_required(vdev_t
*vd
)
3492 spa_t
*spa
= vd
->vdev_spa
;
3493 vdev_t
*tvd
= vd
->vdev_top
;
3494 uint8_t cant_read
= vd
->vdev_cant_read
;
3497 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
3499 if (vd
== spa
->spa_root_vdev
|| vd
== tvd
)
3503 * Temporarily mark the device as unreadable, and then determine
3504 * whether this results in any DTL outages in the top-level vdev.
3505 * If not, we can safely offline/detach/remove the device.
3507 vd
->vdev_cant_read
= B_TRUE
;
3508 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
, B_FALSE
);
3509 required
= !vdev_dtl_empty(tvd
, DTL_OUTAGE
);
3510 vd
->vdev_cant_read
= cant_read
;
3511 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
, B_FALSE
);
3513 if (!required
&& zio_injection_enabled
) {
3514 required
= !!zio_handle_device_injection(vd
, NULL
,
3522 * Determine if resilver is needed, and if so the txg range.
3525 vdev_resilver_needed(vdev_t
*vd
, uint64_t *minp
, uint64_t *maxp
)
3527 boolean_t needed
= B_FALSE
;
3528 uint64_t thismin
= UINT64_MAX
;
3529 uint64_t thismax
= 0;
3531 if (vd
->vdev_children
== 0) {
3532 mutex_enter(&vd
->vdev_dtl_lock
);
3533 if (!range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]) &&
3534 vdev_writeable(vd
)) {
3536 thismin
= vdev_dtl_min(vd
);
3537 thismax
= vdev_dtl_max(vd
);
3540 mutex_exit(&vd
->vdev_dtl_lock
);
3542 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3543 vdev_t
*cvd
= vd
->vdev_child
[c
];
3544 uint64_t cmin
, cmax
;
3546 if (vdev_resilver_needed(cvd
, &cmin
, &cmax
)) {
3547 thismin
= MIN(thismin
, cmin
);
3548 thismax
= MAX(thismax
, cmax
);
3554 if (needed
&& minp
) {
3562 * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj
3563 * will contain either the checkpoint spacemap object or zero if none exists.
3564 * All other errors are returned to the caller.
3567 vdev_checkpoint_sm_object(vdev_t
*vd
, uint64_t *sm_obj
)
3569 ASSERT0(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
));
3571 if (vd
->vdev_top_zap
== 0) {
3576 int error
= zap_lookup(spa_meta_objset(vd
->vdev_spa
), vd
->vdev_top_zap
,
3577 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM
, sizeof (uint64_t), 1, sm_obj
);
3578 if (error
== ENOENT
) {
3587 vdev_load(vdev_t
*vd
)
3589 int children
= vd
->vdev_children
;
3594 * It's only worthwhile to use the taskq for the root vdev, because the
3595 * slow part is metaslab_init, and that only happens for top-level
3598 if (vd
->vdev_ops
== &vdev_root_ops
&& vd
->vdev_children
> 0) {
3599 tq
= taskq_create("vdev_load", children
, minclsyspri
,
3600 children
, children
, TASKQ_PREPOPULATE
);
3604 * Recursively load all children.
3606 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3607 vdev_t
*cvd
= vd
->vdev_child
[c
];
3609 if (tq
== NULL
|| vdev_uses_zvols(cvd
)) {
3610 cvd
->vdev_load_error
= vdev_load(cvd
);
3612 VERIFY(taskq_dispatch(tq
, vdev_load_child
,
3613 cvd
, TQ_SLEEP
) != TASKQID_INVALID
);
3622 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3623 int error
= vd
->vdev_child
[c
]->vdev_load_error
;
3629 vdev_set_deflate_ratio(vd
);
3632 * On spa_load path, grab the allocation bias from our zap
3634 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
!= 0) {
3635 spa_t
*spa
= vd
->vdev_spa
;
3638 error
= zap_lookup(spa
->spa_meta_objset
, vd
->vdev_top_zap
,
3639 VDEV_TOP_ZAP_ALLOCATION_BIAS
, 1, sizeof (bias_str
),
3642 ASSERT(vd
->vdev_alloc_bias
== VDEV_BIAS_NONE
);
3643 vd
->vdev_alloc_bias
= vdev_derive_alloc_bias(bias_str
);
3644 } else if (error
!= ENOENT
) {
3645 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3646 VDEV_AUX_CORRUPT_DATA
);
3647 vdev_dbgmsg(vd
, "vdev_load: zap_lookup(top_zap=%llu) "
3648 "failed [error=%d]",
3649 (u_longlong_t
)vd
->vdev_top_zap
, error
);
3654 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
!= 0) {
3655 spa_t
*spa
= vd
->vdev_spa
;
3658 error
= zap_lookup(spa
->spa_meta_objset
, vd
->vdev_top_zap
,
3659 vdev_prop_to_name(VDEV_PROP_FAILFAST
), sizeof (failfast
),
3662 vd
->vdev_failfast
= failfast
& 1;
3663 } else if (error
== ENOENT
) {
3664 vd
->vdev_failfast
= vdev_prop_default_numeric(
3665 VDEV_PROP_FAILFAST
);
3668 "vdev_load: zap_lookup(top_zap=%llu) "
3669 "failed [error=%d]",
3670 (u_longlong_t
)vd
->vdev_top_zap
, error
);
3675 * Load any rebuild state from the top-level vdev zap.
3677 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
!= 0) {
3678 error
= vdev_rebuild_load(vd
);
3679 if (error
&& error
!= ENOTSUP
) {
3680 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3681 VDEV_AUX_CORRUPT_DATA
);
3682 vdev_dbgmsg(vd
, "vdev_load: vdev_rebuild_load "
3683 "failed [error=%d]", error
);
3688 if (vd
->vdev_top_zap
!= 0 || vd
->vdev_leaf_zap
!= 0) {
3691 if (vd
->vdev_top_zap
!= 0)
3692 zapobj
= vd
->vdev_top_zap
;
3694 zapobj
= vd
->vdev_leaf_zap
;
3696 error
= vdev_prop_get_int(vd
, VDEV_PROP_CHECKSUM_N
,
3697 &vd
->vdev_checksum_n
);
3698 if (error
&& error
!= ENOENT
)
3699 vdev_dbgmsg(vd
, "vdev_load: zap_lookup(zap=%llu) "
3700 "failed [error=%d]", (u_longlong_t
)zapobj
, error
);
3702 error
= vdev_prop_get_int(vd
, VDEV_PROP_CHECKSUM_T
,
3703 &vd
->vdev_checksum_t
);
3704 if (error
&& error
!= ENOENT
)
3705 vdev_dbgmsg(vd
, "vdev_load: zap_lookup(zap=%llu) "
3706 "failed [error=%d]", (u_longlong_t
)zapobj
, error
);
3708 error
= vdev_prop_get_int(vd
, VDEV_PROP_IO_N
,
3710 if (error
&& error
!= ENOENT
)
3711 vdev_dbgmsg(vd
, "vdev_load: zap_lookup(zap=%llu) "
3712 "failed [error=%d]", (u_longlong_t
)zapobj
, error
);
3714 error
= vdev_prop_get_int(vd
, VDEV_PROP_IO_T
,
3716 if (error
&& error
!= ENOENT
)
3717 vdev_dbgmsg(vd
, "vdev_load: zap_lookup(zap=%llu) "
3718 "failed [error=%d]", (u_longlong_t
)zapobj
, error
);
3722 * If this is a top-level vdev, initialize its metaslabs.
3724 if (vd
== vd
->vdev_top
&& vdev_is_concrete(vd
)) {
3725 vdev_metaslab_group_create(vd
);
3727 if (vd
->vdev_ashift
== 0 || vd
->vdev_asize
== 0) {
3728 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3729 VDEV_AUX_CORRUPT_DATA
);
3730 vdev_dbgmsg(vd
, "vdev_load: invalid size. ashift=%llu, "
3731 "asize=%llu", (u_longlong_t
)vd
->vdev_ashift
,
3732 (u_longlong_t
)vd
->vdev_asize
);
3733 return (SET_ERROR(ENXIO
));
3736 error
= vdev_metaslab_init(vd
, 0);
3738 vdev_dbgmsg(vd
, "vdev_load: metaslab_init failed "
3739 "[error=%d]", error
);
3740 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3741 VDEV_AUX_CORRUPT_DATA
);
3745 uint64_t checkpoint_sm_obj
;
3746 error
= vdev_checkpoint_sm_object(vd
, &checkpoint_sm_obj
);
3747 if (error
== 0 && checkpoint_sm_obj
!= 0) {
3748 objset_t
*mos
= spa_meta_objset(vd
->vdev_spa
);
3749 ASSERT(vd
->vdev_asize
!= 0);
3750 ASSERT3P(vd
->vdev_checkpoint_sm
, ==, NULL
);
3752 error
= space_map_open(&vd
->vdev_checkpoint_sm
,
3753 mos
, checkpoint_sm_obj
, 0, vd
->vdev_asize
,
3756 vdev_dbgmsg(vd
, "vdev_load: space_map_open "
3757 "failed for checkpoint spacemap (obj %llu) "
3759 (u_longlong_t
)checkpoint_sm_obj
, error
);
3762 ASSERT3P(vd
->vdev_checkpoint_sm
, !=, NULL
);
3765 * Since the checkpoint_sm contains free entries
3766 * exclusively we can use space_map_allocated() to
3767 * indicate the cumulative checkpointed space that
3770 vd
->vdev_stat
.vs_checkpoint_space
=
3771 -space_map_allocated(vd
->vdev_checkpoint_sm
);
3772 vd
->vdev_spa
->spa_checkpoint_info
.sci_dspace
+=
3773 vd
->vdev_stat
.vs_checkpoint_space
;
3774 } else if (error
!= 0) {
3775 vdev_dbgmsg(vd
, "vdev_load: failed to retrieve "
3776 "checkpoint space map object from vdev ZAP "
3777 "[error=%d]", error
);
3783 * If this is a leaf vdev, load its DTL.
3785 if (vd
->vdev_ops
->vdev_op_leaf
&& (error
= vdev_dtl_load(vd
)) != 0) {
3786 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3787 VDEV_AUX_CORRUPT_DATA
);
3788 vdev_dbgmsg(vd
, "vdev_load: vdev_dtl_load failed "
3789 "[error=%d]", error
);
3793 uint64_t obsolete_sm_object
;
3794 error
= vdev_obsolete_sm_object(vd
, &obsolete_sm_object
);
3795 if (error
== 0 && obsolete_sm_object
!= 0) {
3796 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
3797 ASSERT(vd
->vdev_asize
!= 0);
3798 ASSERT3P(vd
->vdev_obsolete_sm
, ==, NULL
);
3800 if ((error
= space_map_open(&vd
->vdev_obsolete_sm
, mos
,
3801 obsolete_sm_object
, 0, vd
->vdev_asize
, 0))) {
3802 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3803 VDEV_AUX_CORRUPT_DATA
);
3804 vdev_dbgmsg(vd
, "vdev_load: space_map_open failed for "
3805 "obsolete spacemap (obj %llu) [error=%d]",
3806 (u_longlong_t
)obsolete_sm_object
, error
);
3809 } else if (error
!= 0) {
3810 vdev_dbgmsg(vd
, "vdev_load: failed to retrieve obsolete "
3811 "space map object from vdev ZAP [error=%d]", error
);
3819 * The special vdev case is used for hot spares and l2cache devices. Its
3820 * sole purpose it to set the vdev state for the associated vdev. To do this,
3821 * we make sure that we can open the underlying device, then try to read the
3822 * label, and make sure that the label is sane and that it hasn't been
3823 * repurposed to another pool.
3826 vdev_validate_aux(vdev_t
*vd
)
3829 uint64_t guid
, version
;
3832 if (!vdev_readable(vd
))
3835 if ((label
= vdev_label_read_config(vd
, -1ULL)) == NULL
) {
3836 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
3837 VDEV_AUX_CORRUPT_DATA
);
3841 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_VERSION
, &version
) != 0 ||
3842 !SPA_VERSION_IS_SUPPORTED(version
) ||
3843 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0 ||
3844 guid
!= vd
->vdev_guid
||
3845 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
, &state
) != 0) {
3846 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
3847 VDEV_AUX_CORRUPT_DATA
);
3853 * We don't actually check the pool state here. If it's in fact in
3854 * use by another pool, we update this fact on the fly when requested.
3861 vdev_destroy_ms_flush_data(vdev_t
*vd
, dmu_tx_t
*tx
)
3863 objset_t
*mos
= spa_meta_objset(vd
->vdev_spa
);
3865 if (vd
->vdev_top_zap
== 0)
3868 uint64_t object
= 0;
3869 int err
= zap_lookup(mos
, vd
->vdev_top_zap
,
3870 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS
, sizeof (uint64_t), 1, &object
);
3875 VERIFY0(dmu_object_free(mos
, object
, tx
));
3876 VERIFY0(zap_remove(mos
, vd
->vdev_top_zap
,
3877 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS
, tx
));
3881 * Free the objects used to store this vdev's spacemaps, and the array
3882 * that points to them.
3885 vdev_destroy_spacemaps(vdev_t
*vd
, dmu_tx_t
*tx
)
3887 if (vd
->vdev_ms_array
== 0)
3890 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
3891 uint64_t array_count
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
3892 size_t array_bytes
= array_count
* sizeof (uint64_t);
3893 uint64_t *smobj_array
= kmem_alloc(array_bytes
, KM_SLEEP
);
3894 VERIFY0(dmu_read(mos
, vd
->vdev_ms_array
, 0,
3895 array_bytes
, smobj_array
, 0));
3897 for (uint64_t i
= 0; i
< array_count
; i
++) {
3898 uint64_t smobj
= smobj_array
[i
];
3902 space_map_free_obj(mos
, smobj
, tx
);
3905 kmem_free(smobj_array
, array_bytes
);
3906 VERIFY0(dmu_object_free(mos
, vd
->vdev_ms_array
, tx
));
3907 vdev_destroy_ms_flush_data(vd
, tx
);
3908 vd
->vdev_ms_array
= 0;
3912 vdev_remove_empty_log(vdev_t
*vd
, uint64_t txg
)
3914 spa_t
*spa
= vd
->vdev_spa
;
3916 ASSERT(vd
->vdev_islog
);
3917 ASSERT(vd
== vd
->vdev_top
);
3918 ASSERT3U(txg
, ==, spa_syncing_txg(spa
));
3920 dmu_tx_t
*tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
3922 vdev_destroy_spacemaps(vd
, tx
);
3923 if (vd
->vdev_top_zap
!= 0) {
3924 vdev_destroy_unlink_zap(vd
, vd
->vdev_top_zap
, tx
);
3925 vd
->vdev_top_zap
= 0;
3932 vdev_sync_done(vdev_t
*vd
, uint64_t txg
)
3935 boolean_t reassess
= !txg_list_empty(&vd
->vdev_ms_list
, TXG_CLEAN(txg
));
3937 ASSERT(vdev_is_concrete(vd
));
3939 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, TXG_CLEAN(txg
)))
3941 metaslab_sync_done(msp
, txg
);
3944 metaslab_sync_reassess(vd
->vdev_mg
);
3945 if (vd
->vdev_log_mg
!= NULL
)
3946 metaslab_sync_reassess(vd
->vdev_log_mg
);
3951 vdev_sync(vdev_t
*vd
, uint64_t txg
)
3953 spa_t
*spa
= vd
->vdev_spa
;
3957 ASSERT3U(txg
, ==, spa
->spa_syncing_txg
);
3958 dmu_tx_t
*tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
3959 if (range_tree_space(vd
->vdev_obsolete_segments
) > 0) {
3960 ASSERT(vd
->vdev_removing
||
3961 vd
->vdev_ops
== &vdev_indirect_ops
);
3963 vdev_indirect_sync_obsolete(vd
, tx
);
3966 * If the vdev is indirect, it can't have dirty
3967 * metaslabs or DTLs.
3969 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
3970 ASSERT(txg_list_empty(&vd
->vdev_ms_list
, txg
));
3971 ASSERT(txg_list_empty(&vd
->vdev_dtl_list
, txg
));
3977 ASSERT(vdev_is_concrete(vd
));
3979 if (vd
->vdev_ms_array
== 0 && vd
->vdev_ms_shift
!= 0 &&
3980 !vd
->vdev_removing
) {
3981 ASSERT(vd
== vd
->vdev_top
);
3982 ASSERT0(vd
->vdev_indirect_config
.vic_mapping_object
);
3983 vd
->vdev_ms_array
= dmu_object_alloc(spa
->spa_meta_objset
,
3984 DMU_OT_OBJECT_ARRAY
, 0, DMU_OT_NONE
, 0, tx
);
3985 ASSERT(vd
->vdev_ms_array
!= 0);
3986 vdev_config_dirty(vd
);
3989 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, txg
)) != NULL
) {
3990 metaslab_sync(msp
, txg
);
3991 (void) txg_list_add(&vd
->vdev_ms_list
, msp
, TXG_CLEAN(txg
));
3994 while ((lvd
= txg_list_remove(&vd
->vdev_dtl_list
, txg
)) != NULL
)
3995 vdev_dtl_sync(lvd
, txg
);
3998 * If this is an empty log device being removed, destroy the
3999 * metadata associated with it.
4001 if (vd
->vdev_islog
&& vd
->vdev_stat
.vs_alloc
== 0 && vd
->vdev_removing
)
4002 vdev_remove_empty_log(vd
, txg
);
4004 (void) txg_list_add(&spa
->spa_vdev_txg_list
, vd
, TXG_CLEAN(txg
));
4009 vdev_psize_to_asize(vdev_t
*vd
, uint64_t psize
)
4011 return (vd
->vdev_ops
->vdev_op_asize(vd
, psize
));
4015 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
4016 * not be opened, and no I/O is attempted.
4019 vdev_fault(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
4023 spa_vdev_state_enter(spa
, SCL_NONE
);
4025 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
4026 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
4028 if (!vd
->vdev_ops
->vdev_op_leaf
)
4029 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
4034 * If user did a 'zpool offline -f' then make the fault persist across
4037 if (aux
== VDEV_AUX_EXTERNAL_PERSIST
) {
4039 * There are two kinds of forced faults: temporary and
4040 * persistent. Temporary faults go away at pool import, while
4041 * persistent faults stay set. Both types of faults can be
4042 * cleared with a zpool clear.
4044 * We tell if a vdev is persistently faulted by looking at the
4045 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
4046 * import then it's a persistent fault. Otherwise, it's
4047 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
4048 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
4049 * tells vdev_config_generate() (which gets run later) to set
4050 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
4052 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
4053 vd
->vdev_tmpoffline
= B_FALSE
;
4054 aux
= VDEV_AUX_EXTERNAL
;
4056 vd
->vdev_tmpoffline
= B_TRUE
;
4060 * We don't directly use the aux state here, but if we do a
4061 * vdev_reopen(), we need this value to be present to remember why we
4064 vd
->vdev_label_aux
= aux
;
4067 * Faulted state takes precedence over degraded.
4069 vd
->vdev_delayed_close
= B_FALSE
;
4070 vd
->vdev_faulted
= 1ULL;
4071 vd
->vdev_degraded
= 0ULL;
4072 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_FAULTED
, aux
);
4075 * If this device has the only valid copy of the data, then
4076 * back off and simply mark the vdev as degraded instead.
4078 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&& vdev_dtl_required(vd
)) {
4079 vd
->vdev_degraded
= 1ULL;
4080 vd
->vdev_faulted
= 0ULL;
4083 * If we reopen the device and it's not dead, only then do we
4088 if (vdev_readable(vd
))
4089 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
, aux
);
4092 return (spa_vdev_state_exit(spa
, vd
, 0));
4096 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
4097 * user that something is wrong. The vdev continues to operate as normal as far
4098 * as I/O is concerned.
4101 vdev_degrade(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
4105 spa_vdev_state_enter(spa
, SCL_NONE
);
4107 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
4108 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
4110 if (!vd
->vdev_ops
->vdev_op_leaf
)
4111 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
4114 * If the vdev is already faulted, then don't do anything.
4116 if (vd
->vdev_faulted
|| vd
->vdev_degraded
)
4117 return (spa_vdev_state_exit(spa
, NULL
, 0));
4119 vd
->vdev_degraded
= 1ULL;
4120 if (!vdev_is_dead(vd
))
4121 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
,
4124 return (spa_vdev_state_exit(spa
, vd
, 0));
4128 vdev_remove_wanted(spa_t
*spa
, uint64_t guid
)
4132 spa_vdev_state_enter(spa
, SCL_NONE
);
4134 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
4135 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
4138 * If the vdev is already removed, or expanding which can trigger
4139 * repartition add/remove events, then don't do anything.
4141 if (vd
->vdev_removed
|| vd
->vdev_expanding
)
4142 return (spa_vdev_state_exit(spa
, NULL
, 0));
4145 * Confirm the vdev has been removed, otherwise don't do anything.
4147 if (vd
->vdev_ops
->vdev_op_leaf
&& !zio_wait(vdev_probe(vd
, NULL
)))
4148 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(EEXIST
)));
4150 vd
->vdev_remove_wanted
= B_TRUE
;
4151 spa_async_request(spa
, SPA_ASYNC_REMOVE
);
4153 return (spa_vdev_state_exit(spa
, vd
, 0));
4158 * Online the given vdev.
4160 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
4161 * spare device should be detached when the device finishes resilvering.
4162 * Second, the online should be treated like a 'test' online case, so no FMA
4163 * events are generated if the device fails to open.
4166 vdev_online(spa_t
*spa
, uint64_t guid
, uint64_t flags
, vdev_state_t
*newstate
)
4168 vdev_t
*vd
, *tvd
, *pvd
, *rvd
= spa
->spa_root_vdev
;
4169 boolean_t wasoffline
;
4170 vdev_state_t oldstate
;
4172 spa_vdev_state_enter(spa
, SCL_NONE
);
4174 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
4175 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
4177 if (!vd
->vdev_ops
->vdev_op_leaf
)
4178 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
4180 wasoffline
= (vd
->vdev_offline
|| vd
->vdev_tmpoffline
);
4181 oldstate
= vd
->vdev_state
;
4184 vd
->vdev_offline
= B_FALSE
;
4185 vd
->vdev_tmpoffline
= B_FALSE
;
4186 vd
->vdev_checkremove
= !!(flags
& ZFS_ONLINE_CHECKREMOVE
);
4187 vd
->vdev_forcefault
= !!(flags
& ZFS_ONLINE_FORCEFAULT
);
4189 /* XXX - L2ARC 1.0 does not support expansion */
4190 if (!vd
->vdev_aux
) {
4191 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
4192 pvd
->vdev_expanding
= !!((flags
& ZFS_ONLINE_EXPAND
) ||
4193 spa
->spa_autoexpand
);
4194 vd
->vdev_expansion_time
= gethrestime_sec();
4198 vd
->vdev_checkremove
= vd
->vdev_forcefault
= B_FALSE
;
4200 if (!vd
->vdev_aux
) {
4201 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
4202 pvd
->vdev_expanding
= B_FALSE
;
4206 *newstate
= vd
->vdev_state
;
4207 if ((flags
& ZFS_ONLINE_UNSPARE
) &&
4208 !vdev_is_dead(vd
) && vd
->vdev_parent
&&
4209 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
4210 vd
->vdev_parent
->vdev_child
[0] == vd
)
4211 vd
->vdev_unspare
= B_TRUE
;
4213 if ((flags
& ZFS_ONLINE_EXPAND
) || spa
->spa_autoexpand
) {
4215 /* XXX - L2ARC 1.0 does not support expansion */
4217 return (spa_vdev_state_exit(spa
, vd
, ENOTSUP
));
4218 spa
->spa_ccw_fail_time
= 0;
4219 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
4222 /* Restart initializing if necessary */
4223 mutex_enter(&vd
->vdev_initialize_lock
);
4224 if (vdev_writeable(vd
) &&
4225 vd
->vdev_initialize_thread
== NULL
&&
4226 vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
4227 (void) vdev_initialize(vd
);
4229 mutex_exit(&vd
->vdev_initialize_lock
);
4232 * Restart trimming if necessary. We do not restart trimming for cache
4233 * devices here. This is triggered by l2arc_rebuild_vdev()
4234 * asynchronously for the whole device or in l2arc_evict() as it evicts
4235 * space for upcoming writes.
4237 mutex_enter(&vd
->vdev_trim_lock
);
4238 if (vdev_writeable(vd
) && !vd
->vdev_isl2cache
&&
4239 vd
->vdev_trim_thread
== NULL
&&
4240 vd
->vdev_trim_state
== VDEV_TRIM_ACTIVE
) {
4241 (void) vdev_trim(vd
, vd
->vdev_trim_rate
, vd
->vdev_trim_partial
,
4242 vd
->vdev_trim_secure
);
4244 mutex_exit(&vd
->vdev_trim_lock
);
4247 (oldstate
< VDEV_STATE_DEGRADED
&&
4248 vd
->vdev_state
>= VDEV_STATE_DEGRADED
)) {
4249 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_ONLINE
);
4252 * Asynchronously detach spare vdev if resilver or
4253 * rebuild is not required
4255 if (vd
->vdev_unspare
&&
4256 !dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
4257 !dsl_scan_resilver_scheduled(spa
->spa_dsl_pool
) &&
4258 !vdev_rebuild_active(tvd
))
4259 spa_async_request(spa
, SPA_ASYNC_DETACH_SPARE
);
4261 return (spa_vdev_state_exit(spa
, vd
, 0));
4265 vdev_offline_locked(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
4269 uint64_t generation
;
4270 metaslab_group_t
*mg
;
4273 spa_vdev_state_enter(spa
, SCL_ALLOC
);
4275 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
4276 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
4278 if (!vd
->vdev_ops
->vdev_op_leaf
)
4279 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
4281 if (vd
->vdev_ops
== &vdev_draid_spare_ops
)
4282 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
4286 generation
= spa
->spa_config_generation
+ 1;
4289 * If the device isn't already offline, try to offline it.
4291 if (!vd
->vdev_offline
) {
4293 * If this device has the only valid copy of some data,
4294 * don't allow it to be offlined. Log devices are always
4297 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
4298 vdev_dtl_required(vd
))
4299 return (spa_vdev_state_exit(spa
, NULL
,
4303 * If the top-level is a slog and it has had allocations
4304 * then proceed. We check that the vdev's metaslab group
4305 * is not NULL since it's possible that we may have just
4306 * added this vdev but not yet initialized its metaslabs.
4308 if (tvd
->vdev_islog
&& mg
!= NULL
) {
4310 * Prevent any future allocations.
4312 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
4313 metaslab_group_passivate(mg
);
4314 (void) spa_vdev_state_exit(spa
, vd
, 0);
4316 error
= spa_reset_logs(spa
);
4319 * If the log device was successfully reset but has
4320 * checkpointed data, do not offline it.
4323 tvd
->vdev_checkpoint_sm
!= NULL
) {
4324 ASSERT3U(space_map_allocated(
4325 tvd
->vdev_checkpoint_sm
), !=, 0);
4326 error
= ZFS_ERR_CHECKPOINT_EXISTS
;
4329 spa_vdev_state_enter(spa
, SCL_ALLOC
);
4332 * Check to see if the config has changed.
4334 if (error
|| generation
!= spa
->spa_config_generation
) {
4335 metaslab_group_activate(mg
);
4337 return (spa_vdev_state_exit(spa
,
4339 (void) spa_vdev_state_exit(spa
, vd
, 0);
4342 ASSERT0(tvd
->vdev_stat
.vs_alloc
);
4346 * Offline this device and reopen its top-level vdev.
4347 * If the top-level vdev is a log device then just offline
4348 * it. Otherwise, if this action results in the top-level
4349 * vdev becoming unusable, undo it and fail the request.
4351 vd
->vdev_offline
= B_TRUE
;
4354 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
4355 vdev_is_dead(tvd
)) {
4356 vd
->vdev_offline
= B_FALSE
;
4358 return (spa_vdev_state_exit(spa
, NULL
,
4363 * Add the device back into the metaslab rotor so that
4364 * once we online the device it's open for business.
4366 if (tvd
->vdev_islog
&& mg
!= NULL
)
4367 metaslab_group_activate(mg
);
4370 vd
->vdev_tmpoffline
= !!(flags
& ZFS_OFFLINE_TEMPORARY
);
4372 return (spa_vdev_state_exit(spa
, vd
, 0));
4376 vdev_offline(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
4380 mutex_enter(&spa
->spa_vdev_top_lock
);
4381 error
= vdev_offline_locked(spa
, guid
, flags
);
4382 mutex_exit(&spa
->spa_vdev_top_lock
);
4388 * Clear the error counts associated with this vdev. Unlike vdev_online() and
4389 * vdev_offline(), we assume the spa config is locked. We also clear all
4390 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
4393 vdev_clear(spa_t
*spa
, vdev_t
*vd
)
4395 vdev_t
*rvd
= spa
->spa_root_vdev
;
4397 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
4402 vd
->vdev_stat
.vs_read_errors
= 0;
4403 vd
->vdev_stat
.vs_write_errors
= 0;
4404 vd
->vdev_stat
.vs_checksum_errors
= 0;
4405 vd
->vdev_stat
.vs_slow_ios
= 0;
4407 for (int c
= 0; c
< vd
->vdev_children
; c
++)
4408 vdev_clear(spa
, vd
->vdev_child
[c
]);
4411 * It makes no sense to "clear" an indirect or removed vdev.
4413 if (!vdev_is_concrete(vd
) || vd
->vdev_removed
)
4417 * If we're in the FAULTED state or have experienced failed I/O, then
4418 * clear the persistent state and attempt to reopen the device. We
4419 * also mark the vdev config dirty, so that the new faulted state is
4420 * written out to disk.
4422 if (vd
->vdev_faulted
|| vd
->vdev_degraded
||
4423 !vdev_readable(vd
) || !vdev_writeable(vd
)) {
4425 * When reopening in response to a clear event, it may be due to
4426 * a fmadm repair request. In this case, if the device is
4427 * still broken, we want to still post the ereport again.
4429 vd
->vdev_forcefault
= B_TRUE
;
4431 vd
->vdev_faulted
= vd
->vdev_degraded
= 0ULL;
4432 vd
->vdev_cant_read
= B_FALSE
;
4433 vd
->vdev_cant_write
= B_FALSE
;
4434 vd
->vdev_stat
.vs_aux
= 0;
4436 vdev_reopen(vd
== rvd
? rvd
: vd
->vdev_top
);
4438 vd
->vdev_forcefault
= B_FALSE
;
4440 if (vd
!= rvd
&& vdev_writeable(vd
->vdev_top
))
4441 vdev_state_dirty(vd
->vdev_top
);
4443 /* If a resilver isn't required, check if vdevs can be culled */
4444 if (vd
->vdev_aux
== NULL
&& !vdev_is_dead(vd
) &&
4445 !dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
4446 !dsl_scan_resilver_scheduled(spa
->spa_dsl_pool
))
4447 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
4449 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_CLEAR
);
4453 * When clearing a FMA-diagnosed fault, we always want to
4454 * unspare the device, as we assume that the original spare was
4455 * done in response to the FMA fault.
4457 if (!vdev_is_dead(vd
) && vd
->vdev_parent
!= NULL
&&
4458 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
4459 vd
->vdev_parent
->vdev_child
[0] == vd
)
4460 vd
->vdev_unspare
= B_TRUE
;
4462 /* Clear recent error events cache (i.e. duplicate events tracking) */
4463 zfs_ereport_clear(spa
, vd
);
4467 vdev_is_dead(vdev_t
*vd
)
4470 * Holes and missing devices are always considered "dead".
4471 * This simplifies the code since we don't have to check for
4472 * these types of devices in the various code paths.
4473 * Instead we rely on the fact that we skip over dead devices
4474 * before issuing I/O to them.
4476 return (vd
->vdev_state
< VDEV_STATE_DEGRADED
||
4477 vd
->vdev_ops
== &vdev_hole_ops
||
4478 vd
->vdev_ops
== &vdev_missing_ops
);
4482 vdev_readable(vdev_t
*vd
)
4484 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_read
);
4488 vdev_writeable(vdev_t
*vd
)
4490 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_write
&&
4491 vdev_is_concrete(vd
));
4495 vdev_allocatable(vdev_t
*vd
)
4497 uint64_t state
= vd
->vdev_state
;
4500 * We currently allow allocations from vdevs which may be in the
4501 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
4502 * fails to reopen then we'll catch it later when we're holding
4503 * the proper locks. Note that we have to get the vdev state
4504 * in a local variable because although it changes atomically,
4505 * we're asking two separate questions about it.
4507 return (!(state
< VDEV_STATE_DEGRADED
&& state
!= VDEV_STATE_CLOSED
) &&
4508 !vd
->vdev_cant_write
&& vdev_is_concrete(vd
) &&
4509 vd
->vdev_mg
->mg_initialized
);
4513 vdev_accessible(vdev_t
*vd
, zio_t
*zio
)
4515 ASSERT(zio
->io_vd
== vd
);
4517 if (vdev_is_dead(vd
) || vd
->vdev_remove_wanted
)
4520 if (zio
->io_type
== ZIO_TYPE_READ
)
4521 return (!vd
->vdev_cant_read
);
4523 if (zio
->io_type
== ZIO_TYPE_WRITE
)
4524 return (!vd
->vdev_cant_write
);
4530 vdev_get_child_stat(vdev_t
*cvd
, vdev_stat_t
*vs
, vdev_stat_t
*cvs
)
4533 * Exclude the dRAID spare when aggregating to avoid double counting
4534 * the ops and bytes. These IOs are counted by the physical leaves.
4536 if (cvd
->vdev_ops
== &vdev_draid_spare_ops
)
4539 for (int t
= 0; t
< VS_ZIO_TYPES
; t
++) {
4540 vs
->vs_ops
[t
] += cvs
->vs_ops
[t
];
4541 vs
->vs_bytes
[t
] += cvs
->vs_bytes
[t
];
4544 cvs
->vs_scan_removing
= cvd
->vdev_removing
;
4548 * Get extended stats
4551 vdev_get_child_stat_ex(vdev_t
*cvd
, vdev_stat_ex_t
*vsx
, vdev_stat_ex_t
*cvsx
)
4556 for (t
= 0; t
< ZIO_TYPES
; t
++) {
4557 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_disk_histo
[0]); b
++)
4558 vsx
->vsx_disk_histo
[t
][b
] += cvsx
->vsx_disk_histo
[t
][b
];
4560 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_total_histo
[0]); b
++) {
4561 vsx
->vsx_total_histo
[t
][b
] +=
4562 cvsx
->vsx_total_histo
[t
][b
];
4566 for (t
= 0; t
< ZIO_PRIORITY_NUM_QUEUEABLE
; t
++) {
4567 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_queue_histo
[0]); b
++) {
4568 vsx
->vsx_queue_histo
[t
][b
] +=
4569 cvsx
->vsx_queue_histo
[t
][b
];
4571 vsx
->vsx_active_queue
[t
] += cvsx
->vsx_active_queue
[t
];
4572 vsx
->vsx_pend_queue
[t
] += cvsx
->vsx_pend_queue
[t
];
4574 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_ind_histo
[0]); b
++)
4575 vsx
->vsx_ind_histo
[t
][b
] += cvsx
->vsx_ind_histo
[t
][b
];
4577 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_agg_histo
[0]); b
++)
4578 vsx
->vsx_agg_histo
[t
][b
] += cvsx
->vsx_agg_histo
[t
][b
];
4584 vdev_is_spacemap_addressable(vdev_t
*vd
)
4586 if (spa_feature_is_active(vd
->vdev_spa
, SPA_FEATURE_SPACEMAP_V2
))
4590 * If double-word space map entries are not enabled we assume
4591 * 47 bits of the space map entry are dedicated to the entry's
4592 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
4593 * to calculate the maximum address that can be described by a
4594 * space map entry for the given device.
4596 uint64_t shift
= vd
->vdev_ashift
+ SM_OFFSET_BITS
;
4598 if (shift
>= 63) /* detect potential overflow */
4601 return (vd
->vdev_asize
< (1ULL << shift
));
4605 * Get statistics for the given vdev.
4608 vdev_get_stats_ex_impl(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
4612 * If we're getting stats on the root vdev, aggregate the I/O counts
4613 * over all top-level vdevs (i.e. the direct children of the root).
4615 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4617 memset(vs
->vs_ops
, 0, sizeof (vs
->vs_ops
));
4618 memset(vs
->vs_bytes
, 0, sizeof (vs
->vs_bytes
));
4621 memset(vsx
, 0, sizeof (*vsx
));
4623 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4624 vdev_t
*cvd
= vd
->vdev_child
[c
];
4625 vdev_stat_t
*cvs
= &cvd
->vdev_stat
;
4626 vdev_stat_ex_t
*cvsx
= &cvd
->vdev_stat_ex
;
4628 vdev_get_stats_ex_impl(cvd
, cvs
, cvsx
);
4630 vdev_get_child_stat(cvd
, vs
, cvs
);
4632 vdev_get_child_stat_ex(cvd
, vsx
, cvsx
);
4636 * We're a leaf. Just copy our ZIO active queue stats in. The
4637 * other leaf stats are updated in vdev_stat_update().
4642 memcpy(vsx
, &vd
->vdev_stat_ex
, sizeof (vd
->vdev_stat_ex
));
4644 for (t
= 0; t
< ZIO_PRIORITY_NUM_QUEUEABLE
; t
++) {
4645 vsx
->vsx_active_queue
[t
] = vd
->vdev_queue
.vq_cactive
[t
];
4646 vsx
->vsx_pend_queue
[t
] = vdev_queue_class_length(vd
, t
);
4652 vdev_get_stats_ex(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
4654 vdev_t
*tvd
= vd
->vdev_top
;
4655 mutex_enter(&vd
->vdev_stat_lock
);
4657 memcpy(vs
, &vd
->vdev_stat
, sizeof (*vs
));
4658 vs
->vs_timestamp
= gethrtime() - vs
->vs_timestamp
;
4659 vs
->vs_state
= vd
->vdev_state
;
4660 vs
->vs_rsize
= vdev_get_min_asize(vd
);
4662 if (vd
->vdev_ops
->vdev_op_leaf
) {
4663 vs
->vs_pspace
= vd
->vdev_psize
;
4664 vs
->vs_rsize
+= VDEV_LABEL_START_SIZE
+
4665 VDEV_LABEL_END_SIZE
;
4667 * Report initializing progress. Since we don't
4668 * have the initializing locks held, this is only
4669 * an estimate (although a fairly accurate one).
4671 vs
->vs_initialize_bytes_done
=
4672 vd
->vdev_initialize_bytes_done
;
4673 vs
->vs_initialize_bytes_est
=
4674 vd
->vdev_initialize_bytes_est
;
4675 vs
->vs_initialize_state
= vd
->vdev_initialize_state
;
4676 vs
->vs_initialize_action_time
=
4677 vd
->vdev_initialize_action_time
;
4680 * Report manual TRIM progress. Since we don't have
4681 * the manual TRIM locks held, this is only an
4682 * estimate (although fairly accurate one).
4684 vs
->vs_trim_notsup
= !vd
->vdev_has_trim
;
4685 vs
->vs_trim_bytes_done
= vd
->vdev_trim_bytes_done
;
4686 vs
->vs_trim_bytes_est
= vd
->vdev_trim_bytes_est
;
4687 vs
->vs_trim_state
= vd
->vdev_trim_state
;
4688 vs
->vs_trim_action_time
= vd
->vdev_trim_action_time
;
4690 /* Set when there is a deferred resilver. */
4691 vs
->vs_resilver_deferred
= vd
->vdev_resilver_deferred
;
4695 * Report expandable space on top-level, non-auxiliary devices
4696 * only. The expandable space is reported in terms of metaslab
4697 * sized units since that determines how much space the pool
4700 if (vd
->vdev_aux
== NULL
&& tvd
!= NULL
) {
4701 vs
->vs_esize
= P2ALIGN(
4702 vd
->vdev_max_asize
- vd
->vdev_asize
,
4703 1ULL << tvd
->vdev_ms_shift
);
4706 vs
->vs_configured_ashift
= vd
->vdev_top
!= NULL
4707 ? vd
->vdev_top
->vdev_ashift
: vd
->vdev_ashift
;
4708 vs
->vs_logical_ashift
= vd
->vdev_logical_ashift
;
4709 if (vd
->vdev_physical_ashift
<= ASHIFT_MAX
)
4710 vs
->vs_physical_ashift
= vd
->vdev_physical_ashift
;
4712 vs
->vs_physical_ashift
= 0;
4715 * Report fragmentation and rebuild progress for top-level,
4716 * non-auxiliary, concrete devices.
4718 if (vd
->vdev_aux
== NULL
&& vd
== vd
->vdev_top
&&
4719 vdev_is_concrete(vd
)) {
4721 * The vdev fragmentation rating doesn't take into
4722 * account the embedded slog metaslab (vdev_log_mg).
4723 * Since it's only one metaslab, it would have a tiny
4724 * impact on the overall fragmentation.
4726 vs
->vs_fragmentation
= (vd
->vdev_mg
!= NULL
) ?
4727 vd
->vdev_mg
->mg_fragmentation
: 0;
4729 vs
->vs_noalloc
= MAX(vd
->vdev_noalloc
,
4730 tvd
? tvd
->vdev_noalloc
: 0);
4733 vdev_get_stats_ex_impl(vd
, vs
, vsx
);
4734 mutex_exit(&vd
->vdev_stat_lock
);
4738 vdev_get_stats(vdev_t
*vd
, vdev_stat_t
*vs
)
4740 return (vdev_get_stats_ex(vd
, vs
, NULL
));
4744 vdev_clear_stats(vdev_t
*vd
)
4746 mutex_enter(&vd
->vdev_stat_lock
);
4747 vd
->vdev_stat
.vs_space
= 0;
4748 vd
->vdev_stat
.vs_dspace
= 0;
4749 vd
->vdev_stat
.vs_alloc
= 0;
4750 mutex_exit(&vd
->vdev_stat_lock
);
4754 vdev_scan_stat_init(vdev_t
*vd
)
4756 vdev_stat_t
*vs
= &vd
->vdev_stat
;
4758 for (int c
= 0; c
< vd
->vdev_children
; c
++)
4759 vdev_scan_stat_init(vd
->vdev_child
[c
]);
4761 mutex_enter(&vd
->vdev_stat_lock
);
4762 vs
->vs_scan_processed
= 0;
4763 mutex_exit(&vd
->vdev_stat_lock
);
4767 vdev_stat_update(zio_t
*zio
, uint64_t psize
)
4769 spa_t
*spa
= zio
->io_spa
;
4770 vdev_t
*rvd
= spa
->spa_root_vdev
;
4771 vdev_t
*vd
= zio
->io_vd
? zio
->io_vd
: rvd
;
4773 uint64_t txg
= zio
->io_txg
;
4774 /* Suppress ASAN false positive */
4775 #ifdef __SANITIZE_ADDRESS__
4776 vdev_stat_t
*vs
= vd
? &vd
->vdev_stat
: NULL
;
4777 vdev_stat_ex_t
*vsx
= vd
? &vd
->vdev_stat_ex
: NULL
;
4779 vdev_stat_t
*vs
= &vd
->vdev_stat
;
4780 vdev_stat_ex_t
*vsx
= &vd
->vdev_stat_ex
;
4782 zio_type_t type
= zio
->io_type
;
4783 int flags
= zio
->io_flags
;
4786 * If this i/o is a gang leader, it didn't do any actual work.
4788 if (zio
->io_gang_tree
)
4791 if (zio
->io_error
== 0) {
4793 * If this is a root i/o, don't count it -- we've already
4794 * counted the top-level vdevs, and vdev_get_stats() will
4795 * aggregate them when asked. This reduces contention on
4796 * the root vdev_stat_lock and implicitly handles blocks
4797 * that compress away to holes, for which there is no i/o.
4798 * (Holes never create vdev children, so all the counters
4799 * remain zero, which is what we want.)
4801 * Note: this only applies to successful i/o (io_error == 0)
4802 * because unlike i/o counts, errors are not additive.
4803 * When reading a ditto block, for example, failure of
4804 * one top-level vdev does not imply a root-level error.
4809 ASSERT(vd
== zio
->io_vd
);
4811 if (flags
& ZIO_FLAG_IO_BYPASS
)
4814 mutex_enter(&vd
->vdev_stat_lock
);
4816 if (flags
& ZIO_FLAG_IO_REPAIR
) {
4818 * Repair is the result of a resilver issued by the
4819 * scan thread (spa_sync).
4821 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
4822 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
4823 dsl_scan_phys_t
*scn_phys
= &scn
->scn_phys
;
4824 uint64_t *processed
= &scn_phys
->scn_processed
;
4826 if (vd
->vdev_ops
->vdev_op_leaf
)
4827 atomic_add_64(processed
, psize
);
4828 vs
->vs_scan_processed
+= psize
;
4832 * Repair is the result of a rebuild issued by the
4833 * rebuild thread (vdev_rebuild_thread). To avoid
4834 * double counting repaired bytes the virtual dRAID
4835 * spare vdev is excluded from the processed bytes.
4837 if (zio
->io_priority
== ZIO_PRIORITY_REBUILD
) {
4838 vdev_t
*tvd
= vd
->vdev_top
;
4839 vdev_rebuild_t
*vr
= &tvd
->vdev_rebuild_config
;
4840 vdev_rebuild_phys_t
*vrp
= &vr
->vr_rebuild_phys
;
4841 uint64_t *rebuilt
= &vrp
->vrp_bytes_rebuilt
;
4843 if (vd
->vdev_ops
->vdev_op_leaf
&&
4844 vd
->vdev_ops
!= &vdev_draid_spare_ops
) {
4845 atomic_add_64(rebuilt
, psize
);
4847 vs
->vs_rebuild_processed
+= psize
;
4850 if (flags
& ZIO_FLAG_SELF_HEAL
)
4851 vs
->vs_self_healed
+= psize
;
4855 * The bytes/ops/histograms are recorded at the leaf level and
4856 * aggregated into the higher level vdevs in vdev_get_stats().
4858 if (vd
->vdev_ops
->vdev_op_leaf
&&
4859 (zio
->io_priority
< ZIO_PRIORITY_NUM_QUEUEABLE
)) {
4860 zio_type_t vs_type
= type
;
4861 zio_priority_t priority
= zio
->io_priority
;
4864 * TRIM ops and bytes are reported to user space as
4865 * ZIO_TYPE_IOCTL. This is done to preserve the
4866 * vdev_stat_t structure layout for user space.
4868 if (type
== ZIO_TYPE_TRIM
)
4869 vs_type
= ZIO_TYPE_IOCTL
;
4872 * Solely for the purposes of 'zpool iostat -lqrw'
4873 * reporting use the priority to categorize the IO.
4874 * Only the following are reported to user space:
4876 * ZIO_PRIORITY_SYNC_READ,
4877 * ZIO_PRIORITY_SYNC_WRITE,
4878 * ZIO_PRIORITY_ASYNC_READ,
4879 * ZIO_PRIORITY_ASYNC_WRITE,
4880 * ZIO_PRIORITY_SCRUB,
4881 * ZIO_PRIORITY_TRIM,
4882 * ZIO_PRIORITY_REBUILD.
4884 if (priority
== ZIO_PRIORITY_INITIALIZING
) {
4885 ASSERT3U(type
, ==, ZIO_TYPE_WRITE
);
4886 priority
= ZIO_PRIORITY_ASYNC_WRITE
;
4887 } else if (priority
== ZIO_PRIORITY_REMOVAL
) {
4888 priority
= ((type
== ZIO_TYPE_WRITE
) ?
4889 ZIO_PRIORITY_ASYNC_WRITE
:
4890 ZIO_PRIORITY_ASYNC_READ
);
4893 vs
->vs_ops
[vs_type
]++;
4894 vs
->vs_bytes
[vs_type
] += psize
;
4896 if (flags
& ZIO_FLAG_DELEGATED
) {
4897 vsx
->vsx_agg_histo
[priority
]
4898 [RQ_HISTO(zio
->io_size
)]++;
4900 vsx
->vsx_ind_histo
[priority
]
4901 [RQ_HISTO(zio
->io_size
)]++;
4904 if (zio
->io_delta
&& zio
->io_delay
) {
4905 vsx
->vsx_queue_histo
[priority
]
4906 [L_HISTO(zio
->io_delta
- zio
->io_delay
)]++;
4907 vsx
->vsx_disk_histo
[type
]
4908 [L_HISTO(zio
->io_delay
)]++;
4909 vsx
->vsx_total_histo
[type
]
4910 [L_HISTO(zio
->io_delta
)]++;
4914 mutex_exit(&vd
->vdev_stat_lock
);
4918 if (flags
& ZIO_FLAG_SPECULATIVE
)
4922 * If this is an I/O error that is going to be retried, then ignore the
4923 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
4924 * hard errors, when in reality they can happen for any number of
4925 * innocuous reasons (bus resets, MPxIO link failure, etc).
4927 if (zio
->io_error
== EIO
&&
4928 !(zio
->io_flags
& ZIO_FLAG_IO_RETRY
))
4932 * Intent logs writes won't propagate their error to the root
4933 * I/O so don't mark these types of failures as pool-level
4936 if (zio
->io_vd
== NULL
&& (zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
4939 if (type
== ZIO_TYPE_WRITE
&& txg
!= 0 &&
4940 (!(flags
& ZIO_FLAG_IO_REPAIR
) ||
4941 (flags
& ZIO_FLAG_SCAN_THREAD
) ||
4942 spa
->spa_claiming
)) {
4944 * This is either a normal write (not a repair), or it's
4945 * a repair induced by the scrub thread, or it's a repair
4946 * made by zil_claim() during spa_load() in the first txg.
4947 * In the normal case, we commit the DTL change in the same
4948 * txg as the block was born. In the scrub-induced repair
4949 * case, we know that scrubs run in first-pass syncing context,
4950 * so we commit the DTL change in spa_syncing_txg(spa).
4951 * In the zil_claim() case, we commit in spa_first_txg(spa).
4953 * We currently do not make DTL entries for failed spontaneous
4954 * self-healing writes triggered by normal (non-scrubbing)
4955 * reads, because we have no transactional context in which to
4956 * do so -- and it's not clear that it'd be desirable anyway.
4958 if (vd
->vdev_ops
->vdev_op_leaf
) {
4959 uint64_t commit_txg
= txg
;
4960 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
4961 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
4962 ASSERT(spa_sync_pass(spa
) == 1);
4963 vdev_dtl_dirty(vd
, DTL_SCRUB
, txg
, 1);
4964 commit_txg
= spa_syncing_txg(spa
);
4965 } else if (spa
->spa_claiming
) {
4966 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
4967 commit_txg
= spa_first_txg(spa
);
4969 ASSERT(commit_txg
>= spa_syncing_txg(spa
));
4970 if (vdev_dtl_contains(vd
, DTL_MISSING
, txg
, 1))
4972 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
4973 vdev_dtl_dirty(pvd
, DTL_PARTIAL
, txg
, 1);
4974 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, commit_txg
);
4977 vdev_dtl_dirty(vd
, DTL_MISSING
, txg
, 1);
4982 vdev_deflated_space(vdev_t
*vd
, int64_t space
)
4984 ASSERT((space
& (SPA_MINBLOCKSIZE
-1)) == 0);
4985 ASSERT(vd
->vdev_deflate_ratio
!= 0 || vd
->vdev_isl2cache
);
4987 return ((space
>> SPA_MINBLOCKSHIFT
) * vd
->vdev_deflate_ratio
);
4991 * Update the in-core space usage stats for this vdev, its metaslab class,
4992 * and the root vdev.
4995 vdev_space_update(vdev_t
*vd
, int64_t alloc_delta
, int64_t defer_delta
,
4996 int64_t space_delta
)
4999 int64_t dspace_delta
;
5000 spa_t
*spa
= vd
->vdev_spa
;
5001 vdev_t
*rvd
= spa
->spa_root_vdev
;
5003 ASSERT(vd
== vd
->vdev_top
);
5006 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
5007 * factor. We must calculate this here and not at the root vdev
5008 * because the root vdev's psize-to-asize is simply the max of its
5009 * children's, thus not accurate enough for us.
5011 dspace_delta
= vdev_deflated_space(vd
, space_delta
);
5013 mutex_enter(&vd
->vdev_stat_lock
);
5014 /* ensure we won't underflow */
5015 if (alloc_delta
< 0) {
5016 ASSERT3U(vd
->vdev_stat
.vs_alloc
, >=, -alloc_delta
);
5019 vd
->vdev_stat
.vs_alloc
+= alloc_delta
;
5020 vd
->vdev_stat
.vs_space
+= space_delta
;
5021 vd
->vdev_stat
.vs_dspace
+= dspace_delta
;
5022 mutex_exit(&vd
->vdev_stat_lock
);
5024 /* every class but log contributes to root space stats */
5025 if (vd
->vdev_mg
!= NULL
&& !vd
->vdev_islog
) {
5026 ASSERT(!vd
->vdev_isl2cache
);
5027 mutex_enter(&rvd
->vdev_stat_lock
);
5028 rvd
->vdev_stat
.vs_alloc
+= alloc_delta
;
5029 rvd
->vdev_stat
.vs_space
+= space_delta
;
5030 rvd
->vdev_stat
.vs_dspace
+= dspace_delta
;
5031 mutex_exit(&rvd
->vdev_stat_lock
);
5033 /* Note: metaslab_class_space_update moved to metaslab_space_update */
5037 * Mark a top-level vdev's config as dirty, placing it on the dirty list
5038 * so that it will be written out next time the vdev configuration is synced.
5039 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
5042 vdev_config_dirty(vdev_t
*vd
)
5044 spa_t
*spa
= vd
->vdev_spa
;
5045 vdev_t
*rvd
= spa
->spa_root_vdev
;
5048 ASSERT(spa_writeable(spa
));
5051 * If this is an aux vdev (as with l2cache and spare devices), then we
5052 * update the vdev config manually and set the sync flag.
5054 if (vd
->vdev_aux
!= NULL
) {
5055 spa_aux_vdev_t
*sav
= vd
->vdev_aux
;
5059 for (c
= 0; c
< sav
->sav_count
; c
++) {
5060 if (sav
->sav_vdevs
[c
] == vd
)
5064 if (c
== sav
->sav_count
) {
5066 * We're being removed. There's nothing more to do.
5068 ASSERT(sav
->sav_sync
== B_TRUE
);
5072 sav
->sav_sync
= B_TRUE
;
5074 if (nvlist_lookup_nvlist_array(sav
->sav_config
,
5075 ZPOOL_CONFIG_L2CACHE
, &aux
, &naux
) != 0) {
5076 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
5077 ZPOOL_CONFIG_SPARES
, &aux
, &naux
) == 0);
5083 * Setting the nvlist in the middle if the array is a little
5084 * sketchy, but it will work.
5086 nvlist_free(aux
[c
]);
5087 aux
[c
] = vdev_config_generate(spa
, vd
, B_TRUE
, 0);
5093 * The dirty list is protected by the SCL_CONFIG lock. The caller
5094 * must either hold SCL_CONFIG as writer, or must be the sync thread
5095 * (which holds SCL_CONFIG as reader). There's only one sync thread,
5096 * so this is sufficient to ensure mutual exclusion.
5098 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
5099 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
5100 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
5103 for (c
= 0; c
< rvd
->vdev_children
; c
++)
5104 vdev_config_dirty(rvd
->vdev_child
[c
]);
5106 ASSERT(vd
== vd
->vdev_top
);
5108 if (!list_link_active(&vd
->vdev_config_dirty_node
) &&
5109 vdev_is_concrete(vd
)) {
5110 list_insert_head(&spa
->spa_config_dirty_list
, vd
);
5116 vdev_config_clean(vdev_t
*vd
)
5118 spa_t
*spa
= vd
->vdev_spa
;
5120 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
5121 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
5122 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
5124 ASSERT(list_link_active(&vd
->vdev_config_dirty_node
));
5125 list_remove(&spa
->spa_config_dirty_list
, vd
);
5129 * Mark a top-level vdev's state as dirty, so that the next pass of
5130 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
5131 * the state changes from larger config changes because they require
5132 * much less locking, and are often needed for administrative actions.
5135 vdev_state_dirty(vdev_t
*vd
)
5137 spa_t
*spa
= vd
->vdev_spa
;
5139 ASSERT(spa_writeable(spa
));
5140 ASSERT(vd
== vd
->vdev_top
);
5143 * The state list is protected by the SCL_STATE lock. The caller
5144 * must either hold SCL_STATE as writer, or must be the sync thread
5145 * (which holds SCL_STATE as reader). There's only one sync thread,
5146 * so this is sufficient to ensure mutual exclusion.
5148 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
5149 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
5150 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
5152 if (!list_link_active(&vd
->vdev_state_dirty_node
) &&
5153 vdev_is_concrete(vd
))
5154 list_insert_head(&spa
->spa_state_dirty_list
, vd
);
5158 vdev_state_clean(vdev_t
*vd
)
5160 spa_t
*spa
= vd
->vdev_spa
;
5162 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
5163 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
5164 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
5166 ASSERT(list_link_active(&vd
->vdev_state_dirty_node
));
5167 list_remove(&spa
->spa_state_dirty_list
, vd
);
5171 * Propagate vdev state up from children to parent.
5174 vdev_propagate_state(vdev_t
*vd
)
5176 spa_t
*spa
= vd
->vdev_spa
;
5177 vdev_t
*rvd
= spa
->spa_root_vdev
;
5178 int degraded
= 0, faulted
= 0;
5182 if (vd
->vdev_children
> 0) {
5183 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5184 child
= vd
->vdev_child
[c
];
5187 * Don't factor holes or indirect vdevs into the
5190 if (!vdev_is_concrete(child
))
5193 if (!vdev_readable(child
) ||
5194 (!vdev_writeable(child
) && spa_writeable(spa
))) {
5196 * Root special: if there is a top-level log
5197 * device, treat the root vdev as if it were
5200 if (child
->vdev_islog
&& vd
== rvd
)
5204 } else if (child
->vdev_state
<= VDEV_STATE_DEGRADED
) {
5208 if (child
->vdev_stat
.vs_aux
== VDEV_AUX_CORRUPT_DATA
)
5212 vd
->vdev_ops
->vdev_op_state_change(vd
, faulted
, degraded
);
5215 * Root special: if there is a top-level vdev that cannot be
5216 * opened due to corrupted metadata, then propagate the root
5217 * vdev's aux state as 'corrupt' rather than 'insufficient
5220 if (corrupted
&& vd
== rvd
&&
5221 rvd
->vdev_state
== VDEV_STATE_CANT_OPEN
)
5222 vdev_set_state(rvd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
5223 VDEV_AUX_CORRUPT_DATA
);
5226 if (vd
->vdev_parent
)
5227 vdev_propagate_state(vd
->vdev_parent
);
5231 * Set a vdev's state. If this is during an open, we don't update the parent
5232 * state, because we're in the process of opening children depth-first.
5233 * Otherwise, we propagate the change to the parent.
5235 * If this routine places a device in a faulted state, an appropriate ereport is
5239 vdev_set_state(vdev_t
*vd
, boolean_t isopen
, vdev_state_t state
, vdev_aux_t aux
)
5241 uint64_t save_state
;
5242 spa_t
*spa
= vd
->vdev_spa
;
5244 if (state
== vd
->vdev_state
) {
5246 * Since vdev_offline() code path is already in an offline
5247 * state we can miss a statechange event to OFFLINE. Check
5248 * the previous state to catch this condition.
5250 if (vd
->vdev_ops
->vdev_op_leaf
&&
5251 (state
== VDEV_STATE_OFFLINE
) &&
5252 (vd
->vdev_prevstate
>= VDEV_STATE_FAULTED
)) {
5253 /* post an offline state change */
5254 zfs_post_state_change(spa
, vd
, vd
->vdev_prevstate
);
5256 vd
->vdev_stat
.vs_aux
= aux
;
5260 save_state
= vd
->vdev_state
;
5262 vd
->vdev_state
= state
;
5263 vd
->vdev_stat
.vs_aux
= aux
;
5266 * If we are setting the vdev state to anything but an open state, then
5267 * always close the underlying device unless the device has requested
5268 * a delayed close (i.e. we're about to remove or fault the device).
5269 * Otherwise, we keep accessible but invalid devices open forever.
5270 * We don't call vdev_close() itself, because that implies some extra
5271 * checks (offline, etc) that we don't want here. This is limited to
5272 * leaf devices, because otherwise closing the device will affect other
5275 if (!vd
->vdev_delayed_close
&& vdev_is_dead(vd
) &&
5276 vd
->vdev_ops
->vdev_op_leaf
)
5277 vd
->vdev_ops
->vdev_op_close(vd
);
5279 if (vd
->vdev_removed
&&
5280 state
== VDEV_STATE_CANT_OPEN
&&
5281 (aux
== VDEV_AUX_OPEN_FAILED
|| vd
->vdev_checkremove
)) {
5283 * If the previous state is set to VDEV_STATE_REMOVED, then this
5284 * device was previously marked removed and someone attempted to
5285 * reopen it. If this failed due to a nonexistent device, then
5286 * keep the device in the REMOVED state. We also let this be if
5287 * it is one of our special test online cases, which is only
5288 * attempting to online the device and shouldn't generate an FMA
5291 vd
->vdev_state
= VDEV_STATE_REMOVED
;
5292 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
5293 } else if (state
== VDEV_STATE_REMOVED
) {
5294 vd
->vdev_removed
= B_TRUE
;
5295 } else if (state
== VDEV_STATE_CANT_OPEN
) {
5297 * If we fail to open a vdev during an import or recovery, we
5298 * mark it as "not available", which signifies that it was
5299 * never there to begin with. Failure to open such a device
5300 * is not considered an error.
5302 if ((spa_load_state(spa
) == SPA_LOAD_IMPORT
||
5303 spa_load_state(spa
) == SPA_LOAD_RECOVER
) &&
5304 vd
->vdev_ops
->vdev_op_leaf
)
5305 vd
->vdev_not_present
= 1;
5308 * Post the appropriate ereport. If the 'prevstate' field is
5309 * set to something other than VDEV_STATE_UNKNOWN, it indicates
5310 * that this is part of a vdev_reopen(). In this case, we don't
5311 * want to post the ereport if the device was already in the
5312 * CANT_OPEN state beforehand.
5314 * If the 'checkremove' flag is set, then this is an attempt to
5315 * online the device in response to an insertion event. If we
5316 * hit this case, then we have detected an insertion event for a
5317 * faulted or offline device that wasn't in the removed state.
5318 * In this scenario, we don't post an ereport because we are
5319 * about to replace the device, or attempt an online with
5320 * vdev_forcefault, which will generate the fault for us.
5322 if ((vd
->vdev_prevstate
!= state
|| vd
->vdev_forcefault
) &&
5323 !vd
->vdev_not_present
&& !vd
->vdev_checkremove
&&
5324 vd
!= spa
->spa_root_vdev
) {
5328 case VDEV_AUX_OPEN_FAILED
:
5329 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED
;
5331 case VDEV_AUX_CORRUPT_DATA
:
5332 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA
;
5334 case VDEV_AUX_NO_REPLICAS
:
5335 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS
;
5337 case VDEV_AUX_BAD_GUID_SUM
:
5338 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM
;
5340 case VDEV_AUX_TOO_SMALL
:
5341 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL
;
5343 case VDEV_AUX_BAD_LABEL
:
5344 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL
;
5346 case VDEV_AUX_BAD_ASHIFT
:
5347 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
;
5350 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN
;
5353 (void) zfs_ereport_post(class, spa
, vd
, NULL
, NULL
,
5357 /* Erase any notion of persistent removed state */
5358 vd
->vdev_removed
= B_FALSE
;
5360 vd
->vdev_removed
= B_FALSE
;
5364 * Notify ZED of any significant state-change on a leaf vdev.
5367 if (vd
->vdev_ops
->vdev_op_leaf
) {
5368 /* preserve original state from a vdev_reopen() */
5369 if ((vd
->vdev_prevstate
!= VDEV_STATE_UNKNOWN
) &&
5370 (vd
->vdev_prevstate
!= vd
->vdev_state
) &&
5371 (save_state
<= VDEV_STATE_CLOSED
))
5372 save_state
= vd
->vdev_prevstate
;
5374 /* filter out state change due to initial vdev_open */
5375 if (save_state
> VDEV_STATE_CLOSED
)
5376 zfs_post_state_change(spa
, vd
, save_state
);
5379 if (!isopen
&& vd
->vdev_parent
)
5380 vdev_propagate_state(vd
->vdev_parent
);
5384 vdev_children_are_offline(vdev_t
*vd
)
5386 ASSERT(!vd
->vdev_ops
->vdev_op_leaf
);
5388 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
5389 if (vd
->vdev_child
[i
]->vdev_state
!= VDEV_STATE_OFFLINE
)
5397 * Check the vdev configuration to ensure that it's capable of supporting
5398 * a root pool. We do not support partial configuration.
5401 vdev_is_bootable(vdev_t
*vd
)
5403 if (!vd
->vdev_ops
->vdev_op_leaf
) {
5404 const char *vdev_type
= vd
->vdev_ops
->vdev_op_type
;
5406 if (strcmp(vdev_type
, VDEV_TYPE_MISSING
) == 0)
5410 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5411 if (!vdev_is_bootable(vd
->vdev_child
[c
]))
5418 vdev_is_concrete(vdev_t
*vd
)
5420 vdev_ops_t
*ops
= vd
->vdev_ops
;
5421 if (ops
== &vdev_indirect_ops
|| ops
== &vdev_hole_ops
||
5422 ops
== &vdev_missing_ops
|| ops
== &vdev_root_ops
) {
5430 * Determine if a log device has valid content. If the vdev was
5431 * removed or faulted in the MOS config then we know that
5432 * the content on the log device has already been written to the pool.
5435 vdev_log_state_valid(vdev_t
*vd
)
5437 if (vd
->vdev_ops
->vdev_op_leaf
&& !vd
->vdev_faulted
&&
5441 for (int c
= 0; c
< vd
->vdev_children
; c
++)
5442 if (vdev_log_state_valid(vd
->vdev_child
[c
]))
5449 * Expand a vdev if possible.
5452 vdev_expand(vdev_t
*vd
, uint64_t txg
)
5454 ASSERT(vd
->vdev_top
== vd
);
5455 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5456 ASSERT(vdev_is_concrete(vd
));
5458 vdev_set_deflate_ratio(vd
);
5460 if ((vd
->vdev_asize
>> vd
->vdev_ms_shift
) > vd
->vdev_ms_count
&&
5461 vdev_is_concrete(vd
)) {
5462 vdev_metaslab_group_create(vd
);
5463 VERIFY(vdev_metaslab_init(vd
, txg
) == 0);
5464 vdev_config_dirty(vd
);
5472 vdev_split(vdev_t
*vd
)
5474 vdev_t
*cvd
, *pvd
= vd
->vdev_parent
;
5476 VERIFY3U(pvd
->vdev_children
, >, 1);
5478 vdev_remove_child(pvd
, vd
);
5479 vdev_compact_children(pvd
);
5481 ASSERT3P(pvd
->vdev_child
, !=, NULL
);
5483 cvd
= pvd
->vdev_child
[0];
5484 if (pvd
->vdev_children
== 1) {
5485 vdev_remove_parent(cvd
);
5486 cvd
->vdev_splitting
= B_TRUE
;
5488 vdev_propagate_state(cvd
);
5492 vdev_deadman(vdev_t
*vd
, const char *tag
)
5494 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5495 vdev_t
*cvd
= vd
->vdev_child
[c
];
5497 vdev_deadman(cvd
, tag
);
5500 if (vd
->vdev_ops
->vdev_op_leaf
) {
5501 vdev_queue_t
*vq
= &vd
->vdev_queue
;
5503 mutex_enter(&vq
->vq_lock
);
5504 if (vq
->vq_active
> 0) {
5505 spa_t
*spa
= vd
->vdev_spa
;
5509 zfs_dbgmsg("slow vdev: %s has %u active IOs",
5510 vd
->vdev_path
, vq
->vq_active
);
5513 * Look at the head of all the pending queues,
5514 * if any I/O has been outstanding for longer than
5515 * the spa_deadman_synctime invoke the deadman logic.
5517 fio
= list_head(&vq
->vq_active_list
);
5518 delta
= gethrtime() - fio
->io_timestamp
;
5519 if (delta
> spa_deadman_synctime(spa
))
5520 zio_deadman(fio
, tag
);
5522 mutex_exit(&vq
->vq_lock
);
5527 vdev_defer_resilver(vdev_t
*vd
)
5529 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
5531 vd
->vdev_resilver_deferred
= B_TRUE
;
5532 vd
->vdev_spa
->spa_resilver_deferred
= B_TRUE
;
5536 * Clears the resilver deferred flag on all leaf devs under vd. Returns
5537 * B_TRUE if we have devices that need to be resilvered and are available to
5538 * accept resilver I/Os.
5541 vdev_clear_resilver_deferred(vdev_t
*vd
, dmu_tx_t
*tx
)
5543 boolean_t resilver_needed
= B_FALSE
;
5544 spa_t
*spa
= vd
->vdev_spa
;
5546 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5547 vdev_t
*cvd
= vd
->vdev_child
[c
];
5548 resilver_needed
|= vdev_clear_resilver_deferred(cvd
, tx
);
5551 if (vd
== spa
->spa_root_vdev
&&
5552 spa_feature_is_active(spa
, SPA_FEATURE_RESILVER_DEFER
)) {
5553 spa_feature_decr(spa
, SPA_FEATURE_RESILVER_DEFER
, tx
);
5554 vdev_config_dirty(vd
);
5555 spa
->spa_resilver_deferred
= B_FALSE
;
5556 return (resilver_needed
);
5559 if (!vdev_is_concrete(vd
) || vd
->vdev_aux
||
5560 !vd
->vdev_ops
->vdev_op_leaf
)
5561 return (resilver_needed
);
5563 vd
->vdev_resilver_deferred
= B_FALSE
;
5565 return (!vdev_is_dead(vd
) && !vd
->vdev_offline
&&
5566 vdev_resilver_needed(vd
, NULL
, NULL
));
5570 vdev_xlate_is_empty(range_seg64_t
*rs
)
5572 return (rs
->rs_start
== rs
->rs_end
);
5576 * Translate a logical range to the first contiguous physical range for the
5577 * specified vdev_t. This function is initially called with a leaf vdev and
5578 * will walk each parent vdev until it reaches a top-level vdev. Once the
5579 * top-level is reached the physical range is initialized and the recursive
5580 * function begins to unwind. As it unwinds it calls the parent's vdev
5581 * specific translation function to do the real conversion.
5584 vdev_xlate(vdev_t
*vd
, const range_seg64_t
*logical_rs
,
5585 range_seg64_t
*physical_rs
, range_seg64_t
*remain_rs
)
5588 * Walk up the vdev tree
5590 if (vd
!= vd
->vdev_top
) {
5591 vdev_xlate(vd
->vdev_parent
, logical_rs
, physical_rs
,
5595 * We've reached the top-level vdev, initialize the physical
5596 * range to the logical range and set an empty remaining
5597 * range then start to unwind.
5599 physical_rs
->rs_start
= logical_rs
->rs_start
;
5600 physical_rs
->rs_end
= logical_rs
->rs_end
;
5602 remain_rs
->rs_start
= logical_rs
->rs_start
;
5603 remain_rs
->rs_end
= logical_rs
->rs_start
;
5608 vdev_t
*pvd
= vd
->vdev_parent
;
5609 ASSERT3P(pvd
, !=, NULL
);
5610 ASSERT3P(pvd
->vdev_ops
->vdev_op_xlate
, !=, NULL
);
5613 * As this recursive function unwinds, translate the logical
5614 * range into its physical and any remaining components by calling
5615 * the vdev specific translate function.
5617 range_seg64_t intermediate
= { 0 };
5618 pvd
->vdev_ops
->vdev_op_xlate(vd
, physical_rs
, &intermediate
, remain_rs
);
5620 physical_rs
->rs_start
= intermediate
.rs_start
;
5621 physical_rs
->rs_end
= intermediate
.rs_end
;
5625 vdev_xlate_walk(vdev_t
*vd
, const range_seg64_t
*logical_rs
,
5626 vdev_xlate_func_t
*func
, void *arg
)
5628 range_seg64_t iter_rs
= *logical_rs
;
5629 range_seg64_t physical_rs
;
5630 range_seg64_t remain_rs
;
5632 while (!vdev_xlate_is_empty(&iter_rs
)) {
5634 vdev_xlate(vd
, &iter_rs
, &physical_rs
, &remain_rs
);
5637 * With raidz and dRAID, it's possible that the logical range
5638 * does not live on this leaf vdev. Only when there is a non-
5639 * zero physical size call the provided function.
5641 if (!vdev_xlate_is_empty(&physical_rs
))
5642 func(arg
, &physical_rs
);
5644 iter_rs
= remain_rs
;
5649 vdev_name(vdev_t
*vd
, char *buf
, int buflen
)
5651 if (vd
->vdev_path
== NULL
) {
5652 if (strcmp(vd
->vdev_ops
->vdev_op_type
, "root") == 0) {
5653 strlcpy(buf
, vd
->vdev_spa
->spa_name
, buflen
);
5654 } else if (!vd
->vdev_ops
->vdev_op_leaf
) {
5655 snprintf(buf
, buflen
, "%s-%llu",
5656 vd
->vdev_ops
->vdev_op_type
,
5657 (u_longlong_t
)vd
->vdev_id
);
5660 strlcpy(buf
, vd
->vdev_path
, buflen
);
5666 * Look at the vdev tree and determine whether any devices are currently being
5670 vdev_replace_in_progress(vdev_t
*vdev
)
5672 ASSERT(spa_config_held(vdev
->vdev_spa
, SCL_ALL
, RW_READER
) != 0);
5674 if (vdev
->vdev_ops
== &vdev_replacing_ops
)
5678 * A 'spare' vdev indicates that we have a replace in progress, unless
5679 * it has exactly two children, and the second, the hot spare, has
5680 * finished being resilvered.
5682 if (vdev
->vdev_ops
== &vdev_spare_ops
&& (vdev
->vdev_children
> 2 ||
5683 !vdev_dtl_empty(vdev
->vdev_child
[1], DTL_MISSING
)))
5686 for (int i
= 0; i
< vdev
->vdev_children
; i
++) {
5687 if (vdev_replace_in_progress(vdev
->vdev_child
[i
]))
5695 * Add a (source=src, propname=propval) list to an nvlist.
5698 vdev_prop_add_list(nvlist_t
*nvl
, const char *propname
, const char *strval
,
5699 uint64_t intval
, zprop_source_t src
)
5703 propval
= fnvlist_alloc();
5704 fnvlist_add_uint64(propval
, ZPROP_SOURCE
, src
);
5707 fnvlist_add_string(propval
, ZPROP_VALUE
, strval
);
5709 fnvlist_add_uint64(propval
, ZPROP_VALUE
, intval
);
5711 fnvlist_add_nvlist(nvl
, propname
, propval
);
5712 nvlist_free(propval
);
5716 vdev_props_set_sync(void *arg
, dmu_tx_t
*tx
)
5719 nvlist_t
*nvp
= arg
;
5720 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
5721 objset_t
*mos
= spa
->spa_meta_objset
;
5722 nvpair_t
*elem
= NULL
;
5727 vdev_guid
= fnvlist_lookup_uint64(nvp
, ZPOOL_VDEV_PROPS_SET_VDEV
);
5728 nvprops
= fnvlist_lookup_nvlist(nvp
, ZPOOL_VDEV_PROPS_SET_PROPS
);
5729 vd
= spa_lookup_by_guid(spa
, vdev_guid
, B_TRUE
);
5731 /* this vdev could get removed while waiting for this sync task */
5736 * Set vdev property values in the vdev props mos object.
5738 if (vd
->vdev_root_zap
!= 0) {
5739 objid
= vd
->vdev_root_zap
;
5740 } else if (vd
->vdev_top_zap
!= 0) {
5741 objid
= vd
->vdev_top_zap
;
5742 } else if (vd
->vdev_leaf_zap
!= 0) {
5743 objid
= vd
->vdev_leaf_zap
;
5745 panic("unexpected vdev type");
5748 mutex_enter(&spa
->spa_props_lock
);
5750 while ((elem
= nvlist_next_nvpair(nvprops
, elem
)) != NULL
) {
5754 const char *propname
= nvpair_name(elem
);
5755 zprop_type_t proptype
;
5757 switch (prop
= vdev_name_to_prop(propname
)) {
5758 case VDEV_PROP_USERPROP
:
5759 if (vdev_prop_user(propname
)) {
5760 strval
= fnvpair_value_string(elem
);
5761 if (strlen(strval
) == 0) {
5762 /* remove the property if value == "" */
5763 (void) zap_remove(mos
, objid
, propname
,
5766 VERIFY0(zap_update(mos
, objid
, propname
,
5767 1, strlen(strval
) + 1, strval
, tx
));
5769 spa_history_log_internal(spa
, "vdev set", tx
,
5770 "vdev_guid=%llu: %s=%s",
5771 (u_longlong_t
)vdev_guid
, nvpair_name(elem
),
5776 /* normalize the property name */
5777 propname
= vdev_prop_to_name(prop
);
5778 proptype
= vdev_prop_get_type(prop
);
5780 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
5781 ASSERT(proptype
== PROP_TYPE_STRING
);
5782 strval
= fnvpair_value_string(elem
);
5783 VERIFY0(zap_update(mos
, objid
, propname
,
5784 1, strlen(strval
) + 1, strval
, tx
));
5785 spa_history_log_internal(spa
, "vdev set", tx
,
5786 "vdev_guid=%llu: %s=%s",
5787 (u_longlong_t
)vdev_guid
, nvpair_name(elem
),
5789 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
5790 intval
= fnvpair_value_uint64(elem
);
5792 if (proptype
== PROP_TYPE_INDEX
) {
5794 VERIFY0(vdev_prop_index_to_string(
5795 prop
, intval
, &unused
));
5797 VERIFY0(zap_update(mos
, objid
, propname
,
5798 sizeof (uint64_t), 1, &intval
, tx
));
5799 spa_history_log_internal(spa
, "vdev set", tx
,
5800 "vdev_guid=%llu: %s=%lld",
5801 (u_longlong_t
)vdev_guid
,
5802 nvpair_name(elem
), (longlong_t
)intval
);
5804 panic("invalid vdev property type %u",
5811 mutex_exit(&spa
->spa_props_lock
);
5815 vdev_prop_set(vdev_t
*vd
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5817 spa_t
*spa
= vd
->vdev_spa
;
5818 nvpair_t
*elem
= NULL
;
5825 /* Check that vdev has a zap we can use */
5826 if (vd
->vdev_root_zap
== 0 &&
5827 vd
->vdev_top_zap
== 0 &&
5828 vd
->vdev_leaf_zap
== 0)
5829 return (SET_ERROR(EINVAL
));
5831 if (nvlist_lookup_uint64(innvl
, ZPOOL_VDEV_PROPS_SET_VDEV
,
5833 return (SET_ERROR(EINVAL
));
5835 if (nvlist_lookup_nvlist(innvl
, ZPOOL_VDEV_PROPS_SET_PROPS
,
5837 return (SET_ERROR(EINVAL
));
5839 if ((vd
= spa_lookup_by_guid(spa
, vdev_guid
, B_TRUE
)) == NULL
)
5840 return (SET_ERROR(EINVAL
));
5842 while ((elem
= nvlist_next_nvpair(nvprops
, elem
)) != NULL
) {
5843 const char *propname
= nvpair_name(elem
);
5844 vdev_prop_t prop
= vdev_name_to_prop(propname
);
5845 uint64_t intval
= 0;
5846 const char *strval
= NULL
;
5848 if (prop
== VDEV_PROP_USERPROP
&& !vdev_prop_user(propname
)) {
5853 if (vdev_prop_readonly(prop
)) {
5858 /* Special Processing */
5860 case VDEV_PROP_PATH
:
5861 if (vd
->vdev_path
== NULL
) {
5865 if (nvpair_value_string(elem
, &strval
) != 0) {
5869 /* New path must start with /dev/ */
5870 if (strncmp(strval
, "/dev/", 5)) {
5874 error
= spa_vdev_setpath(spa
, vdev_guid
, strval
);
5876 case VDEV_PROP_ALLOCATING
:
5877 if (nvpair_value_uint64(elem
, &intval
) != 0) {
5881 if (intval
!= vd
->vdev_noalloc
)
5884 error
= spa_vdev_noalloc(spa
, vdev_guid
);
5886 error
= spa_vdev_alloc(spa
, vdev_guid
);
5888 case VDEV_PROP_FAILFAST
:
5889 if (nvpair_value_uint64(elem
, &intval
) != 0) {
5893 vd
->vdev_failfast
= intval
& 1;
5895 case VDEV_PROP_CHECKSUM_N
:
5896 if (nvpair_value_uint64(elem
, &intval
) != 0) {
5900 vd
->vdev_checksum_n
= intval
;
5902 case VDEV_PROP_CHECKSUM_T
:
5903 if (nvpair_value_uint64(elem
, &intval
) != 0) {
5907 vd
->vdev_checksum_t
= intval
;
5909 case VDEV_PROP_IO_N
:
5910 if (nvpair_value_uint64(elem
, &intval
) != 0) {
5914 vd
->vdev_io_n
= intval
;
5916 case VDEV_PROP_IO_T
:
5917 if (nvpair_value_uint64(elem
, &intval
) != 0) {
5921 vd
->vdev_io_t
= intval
;
5924 /* Most processing is done in vdev_props_set_sync */
5930 vdev_prop_add_list(outnvl
, propname
, strval
, intval
, 0);
5935 return (dsl_sync_task(spa
->spa_name
, NULL
, vdev_props_set_sync
,
5936 innvl
, 6, ZFS_SPACE_CHECK_EXTRA_RESERVED
));
5940 vdev_prop_get(vdev_t
*vd
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5942 spa_t
*spa
= vd
->vdev_spa
;
5943 objset_t
*mos
= spa
->spa_meta_objset
;
5947 nvpair_t
*elem
= NULL
;
5948 nvlist_t
*nvprops
= NULL
;
5949 uint64_t intval
= 0;
5950 char *strval
= NULL
;
5951 const char *propname
= NULL
;
5955 ASSERT(mos
!= NULL
);
5957 if (nvlist_lookup_uint64(innvl
, ZPOOL_VDEV_PROPS_GET_VDEV
,
5959 return (SET_ERROR(EINVAL
));
5961 nvlist_lookup_nvlist(innvl
, ZPOOL_VDEV_PROPS_GET_PROPS
, &nvprops
);
5963 if (vd
->vdev_root_zap
!= 0) {
5964 objid
= vd
->vdev_root_zap
;
5965 } else if (vd
->vdev_top_zap
!= 0) {
5966 objid
= vd
->vdev_top_zap
;
5967 } else if (vd
->vdev_leaf_zap
!= 0) {
5968 objid
= vd
->vdev_leaf_zap
;
5970 return (SET_ERROR(EINVAL
));
5974 mutex_enter(&spa
->spa_props_lock
);
5976 if (nvprops
!= NULL
) {
5977 char namebuf
[64] = { 0 };
5979 while ((elem
= nvlist_next_nvpair(nvprops
, elem
)) != NULL
) {
5982 propname
= nvpair_name(elem
);
5983 prop
= vdev_name_to_prop(propname
);
5984 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
5985 uint64_t integer_size
, num_integers
;
5988 /* Special Read-only Properties */
5989 case VDEV_PROP_NAME
:
5990 strval
= vdev_name(vd
, namebuf
,
5994 vdev_prop_add_list(outnvl
, propname
, strval
, 0,
5997 case VDEV_PROP_CAPACITY
:
5999 intval
= (vd
->vdev_stat
.vs_dspace
== 0) ? 0 :
6000 (vd
->vdev_stat
.vs_alloc
* 100 /
6001 vd
->vdev_stat
.vs_dspace
);
6002 vdev_prop_add_list(outnvl
, propname
, NULL
,
6003 intval
, ZPROP_SRC_NONE
);
6005 case VDEV_PROP_STATE
:
6006 vdev_prop_add_list(outnvl
, propname
, NULL
,
6007 vd
->vdev_state
, ZPROP_SRC_NONE
);
6009 case VDEV_PROP_GUID
:
6010 vdev_prop_add_list(outnvl
, propname
, NULL
,
6011 vd
->vdev_guid
, ZPROP_SRC_NONE
);
6013 case VDEV_PROP_ASIZE
:
6014 vdev_prop_add_list(outnvl
, propname
, NULL
,
6015 vd
->vdev_asize
, ZPROP_SRC_NONE
);
6017 case VDEV_PROP_PSIZE
:
6018 vdev_prop_add_list(outnvl
, propname
, NULL
,
6019 vd
->vdev_psize
, ZPROP_SRC_NONE
);
6021 case VDEV_PROP_ASHIFT
:
6022 vdev_prop_add_list(outnvl
, propname
, NULL
,
6023 vd
->vdev_ashift
, ZPROP_SRC_NONE
);
6025 case VDEV_PROP_SIZE
:
6026 vdev_prop_add_list(outnvl
, propname
, NULL
,
6027 vd
->vdev_stat
.vs_dspace
, ZPROP_SRC_NONE
);
6029 case VDEV_PROP_FREE
:
6030 vdev_prop_add_list(outnvl
, propname
, NULL
,
6031 vd
->vdev_stat
.vs_dspace
-
6032 vd
->vdev_stat
.vs_alloc
, ZPROP_SRC_NONE
);
6034 case VDEV_PROP_ALLOCATED
:
6035 vdev_prop_add_list(outnvl
, propname
, NULL
,
6036 vd
->vdev_stat
.vs_alloc
, ZPROP_SRC_NONE
);
6038 case VDEV_PROP_EXPANDSZ
:
6039 vdev_prop_add_list(outnvl
, propname
, NULL
,
6040 vd
->vdev_stat
.vs_esize
, ZPROP_SRC_NONE
);
6042 case VDEV_PROP_FRAGMENTATION
:
6043 vdev_prop_add_list(outnvl
, propname
, NULL
,
6044 vd
->vdev_stat
.vs_fragmentation
,
6047 case VDEV_PROP_PARITY
:
6048 vdev_prop_add_list(outnvl
, propname
, NULL
,
6049 vdev_get_nparity(vd
), ZPROP_SRC_NONE
);
6051 case VDEV_PROP_PATH
:
6052 if (vd
->vdev_path
== NULL
)
6054 vdev_prop_add_list(outnvl
, propname
,
6055 vd
->vdev_path
, 0, ZPROP_SRC_NONE
);
6057 case VDEV_PROP_DEVID
:
6058 if (vd
->vdev_devid
== NULL
)
6060 vdev_prop_add_list(outnvl
, propname
,
6061 vd
->vdev_devid
, 0, ZPROP_SRC_NONE
);
6063 case VDEV_PROP_PHYS_PATH
:
6064 if (vd
->vdev_physpath
== NULL
)
6066 vdev_prop_add_list(outnvl
, propname
,
6067 vd
->vdev_physpath
, 0, ZPROP_SRC_NONE
);
6069 case VDEV_PROP_ENC_PATH
:
6070 if (vd
->vdev_enc_sysfs_path
== NULL
)
6072 vdev_prop_add_list(outnvl
, propname
,
6073 vd
->vdev_enc_sysfs_path
, 0, ZPROP_SRC_NONE
);
6076 if (vd
->vdev_fru
== NULL
)
6078 vdev_prop_add_list(outnvl
, propname
,
6079 vd
->vdev_fru
, 0, ZPROP_SRC_NONE
);
6081 case VDEV_PROP_PARENT
:
6082 if (vd
->vdev_parent
!= NULL
) {
6083 strval
= vdev_name(vd
->vdev_parent
,
6084 namebuf
, sizeof (namebuf
));
6085 vdev_prop_add_list(outnvl
, propname
,
6086 strval
, 0, ZPROP_SRC_NONE
);
6089 case VDEV_PROP_CHILDREN
:
6090 if (vd
->vdev_children
> 0)
6091 strval
= kmem_zalloc(ZAP_MAXVALUELEN
,
6093 for (uint64_t i
= 0; i
< vd
->vdev_children
;
6097 vname
= vdev_name(vd
->vdev_child
[i
],
6098 namebuf
, sizeof (namebuf
));
6100 vname
= "(unknown)";
6101 if (strlen(strval
) > 0)
6102 strlcat(strval
, ",",
6104 strlcat(strval
, vname
, ZAP_MAXVALUELEN
);
6106 if (strval
!= NULL
) {
6107 vdev_prop_add_list(outnvl
, propname
,
6108 strval
, 0, ZPROP_SRC_NONE
);
6109 kmem_free(strval
, ZAP_MAXVALUELEN
);
6112 case VDEV_PROP_NUMCHILDREN
:
6113 vdev_prop_add_list(outnvl
, propname
, NULL
,
6114 vd
->vdev_children
, ZPROP_SRC_NONE
);
6116 case VDEV_PROP_READ_ERRORS
:
6117 vdev_prop_add_list(outnvl
, propname
, NULL
,
6118 vd
->vdev_stat
.vs_read_errors
,
6121 case VDEV_PROP_WRITE_ERRORS
:
6122 vdev_prop_add_list(outnvl
, propname
, NULL
,
6123 vd
->vdev_stat
.vs_write_errors
,
6126 case VDEV_PROP_CHECKSUM_ERRORS
:
6127 vdev_prop_add_list(outnvl
, propname
, NULL
,
6128 vd
->vdev_stat
.vs_checksum_errors
,
6131 case VDEV_PROP_INITIALIZE_ERRORS
:
6132 vdev_prop_add_list(outnvl
, propname
, NULL
,
6133 vd
->vdev_stat
.vs_initialize_errors
,
6136 case VDEV_PROP_OPS_NULL
:
6137 vdev_prop_add_list(outnvl
, propname
, NULL
,
6138 vd
->vdev_stat
.vs_ops
[ZIO_TYPE_NULL
],
6141 case VDEV_PROP_OPS_READ
:
6142 vdev_prop_add_list(outnvl
, propname
, NULL
,
6143 vd
->vdev_stat
.vs_ops
[ZIO_TYPE_READ
],
6146 case VDEV_PROP_OPS_WRITE
:
6147 vdev_prop_add_list(outnvl
, propname
, NULL
,
6148 vd
->vdev_stat
.vs_ops
[ZIO_TYPE_WRITE
],
6151 case VDEV_PROP_OPS_FREE
:
6152 vdev_prop_add_list(outnvl
, propname
, NULL
,
6153 vd
->vdev_stat
.vs_ops
[ZIO_TYPE_FREE
],
6156 case VDEV_PROP_OPS_CLAIM
:
6157 vdev_prop_add_list(outnvl
, propname
, NULL
,
6158 vd
->vdev_stat
.vs_ops
[ZIO_TYPE_CLAIM
],
6161 case VDEV_PROP_OPS_TRIM
:
6163 * TRIM ops and bytes are reported to user
6164 * space as ZIO_TYPE_IOCTL. This is done to
6165 * preserve the vdev_stat_t structure layout
6168 vdev_prop_add_list(outnvl
, propname
, NULL
,
6169 vd
->vdev_stat
.vs_ops
[ZIO_TYPE_IOCTL
],
6172 case VDEV_PROP_BYTES_NULL
:
6173 vdev_prop_add_list(outnvl
, propname
, NULL
,
6174 vd
->vdev_stat
.vs_bytes
[ZIO_TYPE_NULL
],
6177 case VDEV_PROP_BYTES_READ
:
6178 vdev_prop_add_list(outnvl
, propname
, NULL
,
6179 vd
->vdev_stat
.vs_bytes
[ZIO_TYPE_READ
],
6182 case VDEV_PROP_BYTES_WRITE
:
6183 vdev_prop_add_list(outnvl
, propname
, NULL
,
6184 vd
->vdev_stat
.vs_bytes
[ZIO_TYPE_WRITE
],
6187 case VDEV_PROP_BYTES_FREE
:
6188 vdev_prop_add_list(outnvl
, propname
, NULL
,
6189 vd
->vdev_stat
.vs_bytes
[ZIO_TYPE_FREE
],
6192 case VDEV_PROP_BYTES_CLAIM
:
6193 vdev_prop_add_list(outnvl
, propname
, NULL
,
6194 vd
->vdev_stat
.vs_bytes
[ZIO_TYPE_CLAIM
],
6197 case VDEV_PROP_BYTES_TRIM
:
6199 * TRIM ops and bytes are reported to user
6200 * space as ZIO_TYPE_IOCTL. This is done to
6201 * preserve the vdev_stat_t structure layout
6204 vdev_prop_add_list(outnvl
, propname
, NULL
,
6205 vd
->vdev_stat
.vs_bytes
[ZIO_TYPE_IOCTL
],
6208 case VDEV_PROP_REMOVING
:
6209 vdev_prop_add_list(outnvl
, propname
, NULL
,
6210 vd
->vdev_removing
, ZPROP_SRC_NONE
);
6212 /* Numeric Properites */
6213 case VDEV_PROP_ALLOCATING
:
6214 /* Leaf vdevs cannot have this property */
6215 if (vd
->vdev_mg
== NULL
&&
6216 vd
->vdev_top
!= NULL
) {
6217 src
= ZPROP_SRC_NONE
;
6218 intval
= ZPROP_BOOLEAN_NA
;
6220 err
= vdev_prop_get_int(vd
, prop
,
6222 if (err
&& err
!= ENOENT
)
6226 vdev_prop_default_numeric(prop
))
6227 src
= ZPROP_SRC_DEFAULT
;
6229 src
= ZPROP_SRC_LOCAL
;
6232 vdev_prop_add_list(outnvl
, propname
, NULL
,
6235 case VDEV_PROP_FAILFAST
:
6236 src
= ZPROP_SRC_LOCAL
;
6239 err
= zap_lookup(mos
, objid
, nvpair_name(elem
),
6240 sizeof (uint64_t), 1, &intval
);
6241 if (err
== ENOENT
) {
6242 intval
= vdev_prop_default_numeric(
6248 if (intval
== vdev_prop_default_numeric(prop
))
6249 src
= ZPROP_SRC_DEFAULT
;
6251 vdev_prop_add_list(outnvl
, propname
, strval
,
6254 case VDEV_PROP_CHECKSUM_N
:
6255 case VDEV_PROP_CHECKSUM_T
:
6256 case VDEV_PROP_IO_N
:
6257 case VDEV_PROP_IO_T
:
6258 err
= vdev_prop_get_int(vd
, prop
, &intval
);
6259 if (err
&& err
!= ENOENT
)
6262 if (intval
== vdev_prop_default_numeric(prop
))
6263 src
= ZPROP_SRC_DEFAULT
;
6265 src
= ZPROP_SRC_LOCAL
;
6267 vdev_prop_add_list(outnvl
, propname
, NULL
,
6270 /* Text Properties */
6271 case VDEV_PROP_COMMENT
:
6272 /* Exists in the ZAP below */
6274 case VDEV_PROP_USERPROP
:
6275 /* User Properites */
6276 src
= ZPROP_SRC_LOCAL
;
6278 err
= zap_length(mos
, objid
, nvpair_name(elem
),
6279 &integer_size
, &num_integers
);
6283 switch (integer_size
) {
6285 /* User properties cannot be integers */
6289 /* string property */
6290 strval
= kmem_alloc(num_integers
,
6292 err
= zap_lookup(mos
, objid
,
6293 nvpair_name(elem
), 1,
6294 num_integers
, strval
);
6300 vdev_prop_add_list(outnvl
, propname
,
6302 kmem_free(strval
, num_integers
);
6315 * Get all properties from the MOS vdev property object.
6319 for (zap_cursor_init(&zc
, mos
, objid
);
6320 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
6321 zap_cursor_advance(&zc
)) {
6324 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
6325 propname
= za
.za_name
;
6327 switch (za
.za_integer_length
) {
6329 /* We do not allow integer user properties */
6330 /* This is likely an internal value */
6333 /* string property */
6334 strval
= kmem_alloc(za
.za_num_integers
,
6336 err
= zap_lookup(mos
, objid
, za
.za_name
, 1,
6337 za
.za_num_integers
, strval
);
6339 kmem_free(strval
, za
.za_num_integers
);
6342 vdev_prop_add_list(outnvl
, propname
, strval
, 0,
6344 kmem_free(strval
, za
.za_num_integers
);
6351 zap_cursor_fini(&zc
);
6354 mutex_exit(&spa
->spa_props_lock
);
6355 if (err
&& err
!= ENOENT
) {
6362 EXPORT_SYMBOL(vdev_fault
);
6363 EXPORT_SYMBOL(vdev_degrade
);
6364 EXPORT_SYMBOL(vdev_online
);
6365 EXPORT_SYMBOL(vdev_offline
);
6366 EXPORT_SYMBOL(vdev_clear
);
6368 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, default_ms_count
, UINT
, ZMOD_RW
,
6369 "Target number of metaslabs per top-level vdev");
6371 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, default_ms_shift
, UINT
, ZMOD_RW
,
6372 "Default lower limit for metaslab size");
6374 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, max_ms_shift
, UINT
, ZMOD_RW
,
6375 "Default upper limit for metaslab size");
6377 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, min_ms_count
, UINT
, ZMOD_RW
,
6378 "Minimum number of metaslabs per top-level vdev");
6380 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, ms_count_limit
, UINT
, ZMOD_RW
,
6381 "Practical upper limit of total metaslabs per top-level vdev");
6383 ZFS_MODULE_PARAM(zfs
, zfs_
, slow_io_events_per_second
, UINT
, ZMOD_RW
,
6384 "Rate limit slow IO (delay) events to this many per second");
6387 ZFS_MODULE_PARAM(zfs
, zfs_
, checksum_events_per_second
, UINT
, ZMOD_RW
,
6388 "Rate limit checksum events to this many checksum errors per second "
6389 "(do not set below ZED threshold).");
6392 ZFS_MODULE_PARAM(zfs
, zfs_
, scan_ignore_errors
, INT
, ZMOD_RW
,
6393 "Ignore errors during resilver/scrub");
6395 ZFS_MODULE_PARAM(zfs_vdev
, vdev_
, validate_skip
, INT
, ZMOD_RW
,
6396 "Bypass vdev_validate()");
6398 ZFS_MODULE_PARAM(zfs
, zfs_
, nocacheflush
, INT
, ZMOD_RW
,
6399 "Disable cache flushes");
6401 ZFS_MODULE_PARAM(zfs
, zfs_
, embedded_slog_min_ms
, UINT
, ZMOD_RW
,
6402 "Minimum number of metaslabs required to dedicate one for log blocks");
6405 ZFS_MODULE_PARAM_CALL(zfs_vdev
, zfs_vdev_
, min_auto_ashift
,
6406 param_set_min_auto_ashift
, param_get_uint
, ZMOD_RW
,
6407 "Minimum ashift used when creating new top-level vdevs");
6409 ZFS_MODULE_PARAM_CALL(zfs_vdev
, zfs_vdev_
, max_auto_ashift
,
6410 param_set_max_auto_ashift
, param_get_uint
, ZMOD_RW
,
6411 "Maximum ashift used when optimizing for logical -> physical sector "
6412 "size on new top-level vdevs");