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_pending_fastwrite
= svd
->vdev_pending_fastwrite
;
1196 tvd
->vdev_ms_array
= svd
->vdev_ms_array
;
1197 tvd
->vdev_ms_shift
= svd
->vdev_ms_shift
;
1198 tvd
->vdev_ms_count
= svd
->vdev_ms_count
;
1199 tvd
->vdev_top_zap
= svd
->vdev_top_zap
;
1201 svd
->vdev_ms_array
= 0;
1202 svd
->vdev_ms_shift
= 0;
1203 svd
->vdev_ms_count
= 0;
1204 svd
->vdev_top_zap
= 0;
1207 ASSERT3P(tvd
->vdev_mg
, ==, svd
->vdev_mg
);
1208 if (tvd
->vdev_log_mg
)
1209 ASSERT3P(tvd
->vdev_log_mg
, ==, svd
->vdev_log_mg
);
1210 tvd
->vdev_mg
= svd
->vdev_mg
;
1211 tvd
->vdev_log_mg
= svd
->vdev_log_mg
;
1212 tvd
->vdev_ms
= svd
->vdev_ms
;
1214 svd
->vdev_mg
= NULL
;
1215 svd
->vdev_log_mg
= NULL
;
1216 svd
->vdev_ms
= NULL
;
1218 if (tvd
->vdev_mg
!= NULL
)
1219 tvd
->vdev_mg
->mg_vd
= tvd
;
1220 if (tvd
->vdev_log_mg
!= NULL
)
1221 tvd
->vdev_log_mg
->mg_vd
= tvd
;
1223 tvd
->vdev_checkpoint_sm
= svd
->vdev_checkpoint_sm
;
1224 svd
->vdev_checkpoint_sm
= NULL
;
1226 tvd
->vdev_alloc_bias
= svd
->vdev_alloc_bias
;
1227 svd
->vdev_alloc_bias
= VDEV_BIAS_NONE
;
1229 tvd
->vdev_stat
.vs_alloc
= svd
->vdev_stat
.vs_alloc
;
1230 tvd
->vdev_stat
.vs_space
= svd
->vdev_stat
.vs_space
;
1231 tvd
->vdev_stat
.vs_dspace
= svd
->vdev_stat
.vs_dspace
;
1233 svd
->vdev_stat
.vs_alloc
= 0;
1234 svd
->vdev_stat
.vs_space
= 0;
1235 svd
->vdev_stat
.vs_dspace
= 0;
1238 * State which may be set on a top-level vdev that's in the
1239 * process of being removed.
1241 ASSERT0(tvd
->vdev_indirect_config
.vic_births_object
);
1242 ASSERT0(tvd
->vdev_indirect_config
.vic_mapping_object
);
1243 ASSERT3U(tvd
->vdev_indirect_config
.vic_prev_indirect_vdev
, ==, -1ULL);
1244 ASSERT3P(tvd
->vdev_indirect_mapping
, ==, NULL
);
1245 ASSERT3P(tvd
->vdev_indirect_births
, ==, NULL
);
1246 ASSERT3P(tvd
->vdev_obsolete_sm
, ==, NULL
);
1247 ASSERT0(tvd
->vdev_noalloc
);
1248 ASSERT0(tvd
->vdev_removing
);
1249 ASSERT0(tvd
->vdev_rebuilding
);
1250 tvd
->vdev_noalloc
= svd
->vdev_noalloc
;
1251 tvd
->vdev_removing
= svd
->vdev_removing
;
1252 tvd
->vdev_rebuilding
= svd
->vdev_rebuilding
;
1253 tvd
->vdev_rebuild_config
= svd
->vdev_rebuild_config
;
1254 tvd
->vdev_indirect_config
= svd
->vdev_indirect_config
;
1255 tvd
->vdev_indirect_mapping
= svd
->vdev_indirect_mapping
;
1256 tvd
->vdev_indirect_births
= svd
->vdev_indirect_births
;
1257 range_tree_swap(&svd
->vdev_obsolete_segments
,
1258 &tvd
->vdev_obsolete_segments
);
1259 tvd
->vdev_obsolete_sm
= svd
->vdev_obsolete_sm
;
1260 svd
->vdev_indirect_config
.vic_mapping_object
= 0;
1261 svd
->vdev_indirect_config
.vic_births_object
= 0;
1262 svd
->vdev_indirect_config
.vic_prev_indirect_vdev
= -1ULL;
1263 svd
->vdev_indirect_mapping
= NULL
;
1264 svd
->vdev_indirect_births
= NULL
;
1265 svd
->vdev_obsolete_sm
= NULL
;
1266 svd
->vdev_noalloc
= 0;
1267 svd
->vdev_removing
= 0;
1268 svd
->vdev_rebuilding
= 0;
1270 for (t
= 0; t
< TXG_SIZE
; t
++) {
1271 while ((msp
= txg_list_remove(&svd
->vdev_ms_list
, t
)) != NULL
)
1272 (void) txg_list_add(&tvd
->vdev_ms_list
, msp
, t
);
1273 while ((vd
= txg_list_remove(&svd
->vdev_dtl_list
, t
)) != NULL
)
1274 (void) txg_list_add(&tvd
->vdev_dtl_list
, vd
, t
);
1275 if (txg_list_remove_this(&spa
->spa_vdev_txg_list
, svd
, t
))
1276 (void) txg_list_add(&spa
->spa_vdev_txg_list
, tvd
, t
);
1279 if (list_link_active(&svd
->vdev_config_dirty_node
)) {
1280 vdev_config_clean(svd
);
1281 vdev_config_dirty(tvd
);
1284 if (list_link_active(&svd
->vdev_state_dirty_node
)) {
1285 vdev_state_clean(svd
);
1286 vdev_state_dirty(tvd
);
1289 tvd
->vdev_deflate_ratio
= svd
->vdev_deflate_ratio
;
1290 svd
->vdev_deflate_ratio
= 0;
1292 tvd
->vdev_islog
= svd
->vdev_islog
;
1293 svd
->vdev_islog
= 0;
1295 dsl_scan_io_queue_vdev_xfer(svd
, tvd
);
1299 vdev_top_update(vdev_t
*tvd
, vdev_t
*vd
)
1306 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1307 vdev_top_update(tvd
, vd
->vdev_child
[c
]);
1311 * Add a mirror/replacing vdev above an existing vdev. There is no need to
1312 * call .vdev_op_init() since mirror/replacing vdevs do not have private state.
1315 vdev_add_parent(vdev_t
*cvd
, vdev_ops_t
*ops
)
1317 spa_t
*spa
= cvd
->vdev_spa
;
1318 vdev_t
*pvd
= cvd
->vdev_parent
;
1321 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1323 mvd
= vdev_alloc_common(spa
, cvd
->vdev_id
, 0, ops
);
1325 mvd
->vdev_asize
= cvd
->vdev_asize
;
1326 mvd
->vdev_min_asize
= cvd
->vdev_min_asize
;
1327 mvd
->vdev_max_asize
= cvd
->vdev_max_asize
;
1328 mvd
->vdev_psize
= cvd
->vdev_psize
;
1329 mvd
->vdev_ashift
= cvd
->vdev_ashift
;
1330 mvd
->vdev_logical_ashift
= cvd
->vdev_logical_ashift
;
1331 mvd
->vdev_physical_ashift
= cvd
->vdev_physical_ashift
;
1332 mvd
->vdev_state
= cvd
->vdev_state
;
1333 mvd
->vdev_crtxg
= cvd
->vdev_crtxg
;
1335 vdev_remove_child(pvd
, cvd
);
1336 vdev_add_child(pvd
, mvd
);
1337 cvd
->vdev_id
= mvd
->vdev_children
;
1338 vdev_add_child(mvd
, cvd
);
1339 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1341 if (mvd
== mvd
->vdev_top
)
1342 vdev_top_transfer(cvd
, mvd
);
1348 * Remove a 1-way mirror/replacing vdev from the tree.
1351 vdev_remove_parent(vdev_t
*cvd
)
1353 vdev_t
*mvd
= cvd
->vdev_parent
;
1354 vdev_t
*pvd
= mvd
->vdev_parent
;
1356 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1358 ASSERT(mvd
->vdev_children
== 1);
1359 ASSERT(mvd
->vdev_ops
== &vdev_mirror_ops
||
1360 mvd
->vdev_ops
== &vdev_replacing_ops
||
1361 mvd
->vdev_ops
== &vdev_spare_ops
);
1362 cvd
->vdev_ashift
= mvd
->vdev_ashift
;
1363 cvd
->vdev_logical_ashift
= mvd
->vdev_logical_ashift
;
1364 cvd
->vdev_physical_ashift
= mvd
->vdev_physical_ashift
;
1365 vdev_remove_child(mvd
, cvd
);
1366 vdev_remove_child(pvd
, mvd
);
1369 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1370 * Otherwise, we could have detached an offline device, and when we
1371 * go to import the pool we'll think we have two top-level vdevs,
1372 * instead of a different version of the same top-level vdev.
1374 if (mvd
->vdev_top
== mvd
) {
1375 uint64_t guid_delta
= mvd
->vdev_guid
- cvd
->vdev_guid
;
1376 cvd
->vdev_orig_guid
= cvd
->vdev_guid
;
1377 cvd
->vdev_guid
+= guid_delta
;
1378 cvd
->vdev_guid_sum
+= guid_delta
;
1381 * If pool not set for autoexpand, we need to also preserve
1382 * mvd's asize to prevent automatic expansion of cvd.
1383 * Otherwise if we are adjusting the mirror by attaching and
1384 * detaching children of non-uniform sizes, the mirror could
1385 * autoexpand, unexpectedly requiring larger devices to
1386 * re-establish the mirror.
1388 if (!cvd
->vdev_spa
->spa_autoexpand
)
1389 cvd
->vdev_asize
= mvd
->vdev_asize
;
1391 cvd
->vdev_id
= mvd
->vdev_id
;
1392 vdev_add_child(pvd
, cvd
);
1393 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1395 if (cvd
== cvd
->vdev_top
)
1396 vdev_top_transfer(mvd
, cvd
);
1398 ASSERT(mvd
->vdev_children
== 0);
1403 vdev_metaslab_group_create(vdev_t
*vd
)
1405 spa_t
*spa
= vd
->vdev_spa
;
1408 * metaslab_group_create was delayed until allocation bias was available
1410 if (vd
->vdev_mg
== NULL
) {
1411 metaslab_class_t
*mc
;
1413 if (vd
->vdev_islog
&& vd
->vdev_alloc_bias
== VDEV_BIAS_NONE
)
1414 vd
->vdev_alloc_bias
= VDEV_BIAS_LOG
;
1416 ASSERT3U(vd
->vdev_islog
, ==,
1417 (vd
->vdev_alloc_bias
== VDEV_BIAS_LOG
));
1419 switch (vd
->vdev_alloc_bias
) {
1421 mc
= spa_log_class(spa
);
1423 case VDEV_BIAS_SPECIAL
:
1424 mc
= spa_special_class(spa
);
1426 case VDEV_BIAS_DEDUP
:
1427 mc
= spa_dedup_class(spa
);
1430 mc
= spa_normal_class(spa
);
1433 vd
->vdev_mg
= metaslab_group_create(mc
, vd
,
1434 spa
->spa_alloc_count
);
1436 if (!vd
->vdev_islog
) {
1437 vd
->vdev_log_mg
= metaslab_group_create(
1438 spa_embedded_log_class(spa
), vd
, 1);
1442 * The spa ashift min/max only apply for the normal metaslab
1443 * class. Class destination is late binding so ashift boundary
1444 * setting had to wait until now.
1446 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
1447 mc
== spa_normal_class(spa
) && vd
->vdev_aux
== NULL
) {
1448 if (vd
->vdev_ashift
> spa
->spa_max_ashift
)
1449 spa
->spa_max_ashift
= vd
->vdev_ashift
;
1450 if (vd
->vdev_ashift
< spa
->spa_min_ashift
)
1451 spa
->spa_min_ashift
= vd
->vdev_ashift
;
1453 uint64_t min_alloc
= vdev_get_min_alloc(vd
);
1454 if (min_alloc
< spa
->spa_min_alloc
)
1455 spa
->spa_min_alloc
= min_alloc
;
1461 vdev_metaslab_init(vdev_t
*vd
, uint64_t txg
)
1463 spa_t
*spa
= vd
->vdev_spa
;
1464 uint64_t oldc
= vd
->vdev_ms_count
;
1465 uint64_t newc
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
1468 boolean_t expanding
= (oldc
!= 0);
1470 ASSERT(txg
== 0 || spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1473 * This vdev is not being allocated from yet or is a hole.
1475 if (vd
->vdev_ms_shift
== 0)
1478 ASSERT(!vd
->vdev_ishole
);
1480 ASSERT(oldc
<= newc
);
1482 mspp
= vmem_zalloc(newc
* sizeof (*mspp
), KM_SLEEP
);
1485 memcpy(mspp
, vd
->vdev_ms
, oldc
* sizeof (*mspp
));
1486 vmem_free(vd
->vdev_ms
, oldc
* sizeof (*mspp
));
1490 vd
->vdev_ms_count
= newc
;
1492 for (uint64_t m
= oldc
; m
< newc
; m
++) {
1493 uint64_t object
= 0;
1495 * vdev_ms_array may be 0 if we are creating the "fake"
1496 * metaslabs for an indirect vdev for zdb's leak detection.
1497 * See zdb_leak_init().
1499 if (txg
== 0 && vd
->vdev_ms_array
!= 0) {
1500 error
= dmu_read(spa
->spa_meta_objset
,
1502 m
* sizeof (uint64_t), sizeof (uint64_t), &object
,
1505 vdev_dbgmsg(vd
, "unable to read the metaslab "
1506 "array [error=%d]", error
);
1511 error
= metaslab_init(vd
->vdev_mg
, m
, object
, txg
,
1514 vdev_dbgmsg(vd
, "metaslab_init failed [error=%d]",
1521 * Find the emptiest metaslab on the vdev and mark it for use for
1522 * embedded slog by moving it from the regular to the log metaslab
1525 if (vd
->vdev_mg
->mg_class
== spa_normal_class(spa
) &&
1526 vd
->vdev_ms_count
> zfs_embedded_slog_min_ms
&&
1527 avl_is_empty(&vd
->vdev_log_mg
->mg_metaslab_tree
)) {
1528 uint64_t slog_msid
= 0;
1529 uint64_t smallest
= UINT64_MAX
;
1532 * Note, we only search the new metaslabs, because the old
1533 * (pre-existing) ones may be active (e.g. have non-empty
1534 * range_tree's), and we don't move them to the new
1537 for (uint64_t m
= oldc
; m
< newc
; m
++) {
1539 space_map_allocated(vd
->vdev_ms
[m
]->ms_sm
);
1540 if (alloc
< smallest
) {
1545 metaslab_t
*slog_ms
= vd
->vdev_ms
[slog_msid
];
1547 * The metaslab was marked as dirty at the end of
1548 * metaslab_init(). Remove it from the dirty list so that we
1549 * can uninitialize and reinitialize it to the new class.
1552 (void) txg_list_remove_this(&vd
->vdev_ms_list
,
1555 uint64_t sm_obj
= space_map_object(slog_ms
->ms_sm
);
1556 metaslab_fini(slog_ms
);
1557 VERIFY0(metaslab_init(vd
->vdev_log_mg
, slog_msid
, sm_obj
, txg
,
1558 &vd
->vdev_ms
[slog_msid
]));
1562 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_WRITER
);
1565 * If the vdev is marked as non-allocating then don't
1566 * activate the metaslabs since we want to ensure that
1567 * no allocations are performed on this device.
1569 if (vd
->vdev_noalloc
) {
1570 /* track non-allocating vdev space */
1571 spa
->spa_nonallocating_dspace
+= spa_deflate(spa
) ?
1572 vd
->vdev_stat
.vs_dspace
: vd
->vdev_stat
.vs_space
;
1573 } else if (!expanding
) {
1574 metaslab_group_activate(vd
->vdev_mg
);
1575 if (vd
->vdev_log_mg
!= NULL
)
1576 metaslab_group_activate(vd
->vdev_log_mg
);
1580 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
1586 vdev_metaslab_fini(vdev_t
*vd
)
1588 if (vd
->vdev_checkpoint_sm
!= NULL
) {
1589 ASSERT(spa_feature_is_active(vd
->vdev_spa
,
1590 SPA_FEATURE_POOL_CHECKPOINT
));
1591 space_map_close(vd
->vdev_checkpoint_sm
);
1593 * Even though we close the space map, we need to set its
1594 * pointer to NULL. The reason is that vdev_metaslab_fini()
1595 * may be called multiple times for certain operations
1596 * (i.e. when destroying a pool) so we need to ensure that
1597 * this clause never executes twice. This logic is similar
1598 * to the one used for the vdev_ms clause below.
1600 vd
->vdev_checkpoint_sm
= NULL
;
1603 if (vd
->vdev_ms
!= NULL
) {
1604 metaslab_group_t
*mg
= vd
->vdev_mg
;
1606 metaslab_group_passivate(mg
);
1607 if (vd
->vdev_log_mg
!= NULL
) {
1608 ASSERT(!vd
->vdev_islog
);
1609 metaslab_group_passivate(vd
->vdev_log_mg
);
1612 uint64_t count
= vd
->vdev_ms_count
;
1613 for (uint64_t m
= 0; m
< count
; m
++) {
1614 metaslab_t
*msp
= vd
->vdev_ms
[m
];
1618 vmem_free(vd
->vdev_ms
, count
* sizeof (metaslab_t
*));
1620 vd
->vdev_ms_count
= 0;
1622 for (int i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++) {
1623 ASSERT0(mg
->mg_histogram
[i
]);
1624 if (vd
->vdev_log_mg
!= NULL
)
1625 ASSERT0(vd
->vdev_log_mg
->mg_histogram
[i
]);
1628 ASSERT0(vd
->vdev_ms_count
);
1629 ASSERT3U(vd
->vdev_pending_fastwrite
, ==, 0);
1632 typedef struct vdev_probe_stats
{
1633 boolean_t vps_readable
;
1634 boolean_t vps_writeable
;
1636 } vdev_probe_stats_t
;
1639 vdev_probe_done(zio_t
*zio
)
1641 spa_t
*spa
= zio
->io_spa
;
1642 vdev_t
*vd
= zio
->io_vd
;
1643 vdev_probe_stats_t
*vps
= zio
->io_private
;
1645 ASSERT(vd
->vdev_probe_zio
!= NULL
);
1647 if (zio
->io_type
== ZIO_TYPE_READ
) {
1648 if (zio
->io_error
== 0)
1649 vps
->vps_readable
= 1;
1650 if (zio
->io_error
== 0 && spa_writeable(spa
)) {
1651 zio_nowait(zio_write_phys(vd
->vdev_probe_zio
, vd
,
1652 zio
->io_offset
, zio
->io_size
, zio
->io_abd
,
1653 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1654 ZIO_PRIORITY_SYNC_WRITE
, vps
->vps_flags
, B_TRUE
));
1656 abd_free(zio
->io_abd
);
1658 } else if (zio
->io_type
== ZIO_TYPE_WRITE
) {
1659 if (zio
->io_error
== 0)
1660 vps
->vps_writeable
= 1;
1661 abd_free(zio
->io_abd
);
1662 } else if (zio
->io_type
== ZIO_TYPE_NULL
) {
1666 vd
->vdev_cant_read
|= !vps
->vps_readable
;
1667 vd
->vdev_cant_write
|= !vps
->vps_writeable
;
1669 if (vdev_readable(vd
) &&
1670 (vdev_writeable(vd
) || !spa_writeable(spa
))) {
1673 ASSERT(zio
->io_error
!= 0);
1674 vdev_dbgmsg(vd
, "failed probe");
1675 (void) zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE
,
1676 spa
, vd
, NULL
, NULL
, 0);
1677 zio
->io_error
= SET_ERROR(ENXIO
);
1680 mutex_enter(&vd
->vdev_probe_lock
);
1681 ASSERT(vd
->vdev_probe_zio
== zio
);
1682 vd
->vdev_probe_zio
= NULL
;
1683 mutex_exit(&vd
->vdev_probe_lock
);
1686 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
)
1687 if (!vdev_accessible(vd
, pio
))
1688 pio
->io_error
= SET_ERROR(ENXIO
);
1690 kmem_free(vps
, sizeof (*vps
));
1695 * Determine whether this device is accessible.
1697 * Read and write to several known locations: the pad regions of each
1698 * vdev label but the first, which we leave alone in case it contains
1702 vdev_probe(vdev_t
*vd
, zio_t
*zio
)
1704 spa_t
*spa
= vd
->vdev_spa
;
1705 vdev_probe_stats_t
*vps
= NULL
;
1708 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1711 * Don't probe the probe.
1713 if (zio
&& (zio
->io_flags
& ZIO_FLAG_PROBE
))
1717 * To prevent 'probe storms' when a device fails, we create
1718 * just one probe i/o at a time. All zios that want to probe
1719 * this vdev will become parents of the probe io.
1721 mutex_enter(&vd
->vdev_probe_lock
);
1723 if ((pio
= vd
->vdev_probe_zio
) == NULL
) {
1724 vps
= kmem_zalloc(sizeof (*vps
), KM_SLEEP
);
1726 vps
->vps_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_PROBE
|
1727 ZIO_FLAG_DONT_AGGREGATE
| ZIO_FLAG_TRYHARD
;
1729 if (spa_config_held(spa
, SCL_ZIO
, RW_WRITER
)) {
1731 * vdev_cant_read and vdev_cant_write can only
1732 * transition from TRUE to FALSE when we have the
1733 * SCL_ZIO lock as writer; otherwise they can only
1734 * transition from FALSE to TRUE. This ensures that
1735 * any zio looking at these values can assume that
1736 * failures persist for the life of the I/O. That's
1737 * important because when a device has intermittent
1738 * connectivity problems, we want to ensure that
1739 * they're ascribed to the device (ENXIO) and not
1742 * Since we hold SCL_ZIO as writer here, clear both
1743 * values so the probe can reevaluate from first
1746 vps
->vps_flags
|= ZIO_FLAG_CONFIG_WRITER
;
1747 vd
->vdev_cant_read
= B_FALSE
;
1748 vd
->vdev_cant_write
= B_FALSE
;
1751 vd
->vdev_probe_zio
= pio
= zio_null(NULL
, spa
, vd
,
1752 vdev_probe_done
, vps
,
1753 vps
->vps_flags
| ZIO_FLAG_DONT_PROPAGATE
);
1756 * We can't change the vdev state in this context, so we
1757 * kick off an async task to do it on our behalf.
1760 vd
->vdev_probe_wanted
= B_TRUE
;
1761 spa_async_request(spa
, SPA_ASYNC_PROBE
);
1766 zio_add_child(zio
, pio
);
1768 mutex_exit(&vd
->vdev_probe_lock
);
1771 ASSERT(zio
!= NULL
);
1775 for (int l
= 1; l
< VDEV_LABELS
; l
++) {
1776 zio_nowait(zio_read_phys(pio
, vd
,
1777 vdev_label_offset(vd
->vdev_psize
, l
,
1778 offsetof(vdev_label_t
, vl_be
)), VDEV_PAD_SIZE
,
1779 abd_alloc_for_io(VDEV_PAD_SIZE
, B_TRUE
),
1780 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1781 ZIO_PRIORITY_SYNC_READ
, vps
->vps_flags
, B_TRUE
));
1792 vdev_load_child(void *arg
)
1796 vd
->vdev_load_error
= vdev_load(vd
);
1800 vdev_open_child(void *arg
)
1804 vd
->vdev_open_thread
= curthread
;
1805 vd
->vdev_open_error
= vdev_open(vd
);
1806 vd
->vdev_open_thread
= NULL
;
1810 vdev_uses_zvols(vdev_t
*vd
)
1813 if (zvol_is_zvol(vd
->vdev_path
))
1817 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1818 if (vdev_uses_zvols(vd
->vdev_child
[c
]))
1825 * Returns B_TRUE if the passed child should be opened.
1828 vdev_default_open_children_func(vdev_t
*vd
)
1835 * Open the requested child vdevs. If any of the leaf vdevs are using
1836 * a ZFS volume then do the opens in a single thread. This avoids a
1837 * deadlock when the current thread is holding the spa_namespace_lock.
1840 vdev_open_children_impl(vdev_t
*vd
, vdev_open_children_func_t
*open_func
)
1842 int children
= vd
->vdev_children
;
1844 taskq_t
*tq
= taskq_create("vdev_open", children
, minclsyspri
,
1845 children
, children
, TASKQ_PREPOPULATE
);
1846 vd
->vdev_nonrot
= B_TRUE
;
1848 for (int c
= 0; c
< children
; c
++) {
1849 vdev_t
*cvd
= vd
->vdev_child
[c
];
1851 if (open_func(cvd
) == B_FALSE
)
1854 if (tq
== NULL
|| vdev_uses_zvols(vd
)) {
1855 cvd
->vdev_open_error
= vdev_open(cvd
);
1857 VERIFY(taskq_dispatch(tq
, vdev_open_child
,
1858 cvd
, TQ_SLEEP
) != TASKQID_INVALID
);
1861 vd
->vdev_nonrot
&= cvd
->vdev_nonrot
;
1871 * Open all child vdevs.
1874 vdev_open_children(vdev_t
*vd
)
1876 vdev_open_children_impl(vd
, vdev_default_open_children_func
);
1880 * Conditionally open a subset of child vdevs.
1883 vdev_open_children_subset(vdev_t
*vd
, vdev_open_children_func_t
*open_func
)
1885 vdev_open_children_impl(vd
, open_func
);
1889 * Compute the raidz-deflation ratio. Note, we hard-code
1890 * in 128k (1 << 17) because it is the "typical" blocksize.
1891 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1892 * otherwise it would inconsistently account for existing bp's.
1895 vdev_set_deflate_ratio(vdev_t
*vd
)
1897 if (vd
== vd
->vdev_top
&& !vd
->vdev_ishole
&& vd
->vdev_ashift
!= 0) {
1898 vd
->vdev_deflate_ratio
= (1 << 17) /
1899 (vdev_psize_to_asize(vd
, 1 << 17) >> SPA_MINBLOCKSHIFT
);
1904 * Choose the best of two ashifts, preferring one between logical ashift
1905 * (absolute minimum) and administrator defined maximum, otherwise take
1906 * the biggest of the two.
1909 vdev_best_ashift(uint64_t logical
, uint64_t a
, uint64_t b
)
1911 if (a
> logical
&& a
<= zfs_vdev_max_auto_ashift
) {
1912 if (b
<= logical
|| b
> zfs_vdev_max_auto_ashift
)
1916 } else if (b
<= logical
|| b
> zfs_vdev_max_auto_ashift
)
1922 * Maximize performance by inflating the configured ashift for top level
1923 * vdevs to be as close to the physical ashift as possible while maintaining
1924 * administrator defined limits and ensuring it doesn't go below the
1928 vdev_ashift_optimize(vdev_t
*vd
)
1930 ASSERT(vd
== vd
->vdev_top
);
1932 if (vd
->vdev_ashift
< vd
->vdev_physical_ashift
&&
1933 vd
->vdev_physical_ashift
<= zfs_vdev_max_auto_ashift
) {
1934 vd
->vdev_ashift
= MIN(
1935 MAX(zfs_vdev_max_auto_ashift
, vd
->vdev_ashift
),
1936 MAX(zfs_vdev_min_auto_ashift
,
1937 vd
->vdev_physical_ashift
));
1940 * If the logical and physical ashifts are the same, then
1941 * we ensure that the top-level vdev's ashift is not smaller
1942 * than our minimum ashift value. For the unusual case
1943 * where logical ashift > physical ashift, we can't cap
1944 * the calculated ashift based on max ashift as that
1945 * would cause failures.
1946 * We still check if we need to increase it to match
1949 vd
->vdev_ashift
= MAX(zfs_vdev_min_auto_ashift
,
1955 * Prepare a virtual device for access.
1958 vdev_open(vdev_t
*vd
)
1960 spa_t
*spa
= vd
->vdev_spa
;
1963 uint64_t max_osize
= 0;
1964 uint64_t asize
, max_asize
, psize
;
1965 uint64_t logical_ashift
= 0;
1966 uint64_t physical_ashift
= 0;
1968 ASSERT(vd
->vdev_open_thread
== curthread
||
1969 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1970 ASSERT(vd
->vdev_state
== VDEV_STATE_CLOSED
||
1971 vd
->vdev_state
== VDEV_STATE_CANT_OPEN
||
1972 vd
->vdev_state
== VDEV_STATE_OFFLINE
);
1974 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1975 vd
->vdev_cant_read
= B_FALSE
;
1976 vd
->vdev_cant_write
= B_FALSE
;
1977 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
1980 * If this vdev is not removed, check its fault status. If it's
1981 * faulted, bail out of the open.
1983 if (!vd
->vdev_removed
&& vd
->vdev_faulted
) {
1984 ASSERT(vd
->vdev_children
== 0);
1985 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1986 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1987 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1988 vd
->vdev_label_aux
);
1989 return (SET_ERROR(ENXIO
));
1990 } else if (vd
->vdev_offline
) {
1991 ASSERT(vd
->vdev_children
== 0);
1992 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
, VDEV_AUX_NONE
);
1993 return (SET_ERROR(ENXIO
));
1996 error
= vd
->vdev_ops
->vdev_op_open(vd
, &osize
, &max_osize
,
1997 &logical_ashift
, &physical_ashift
);
1999 /* Keep the device in removed state if unplugged */
2000 if (error
== ENOENT
&& vd
->vdev_removed
) {
2001 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_REMOVED
,
2007 * Physical volume size should never be larger than its max size, unless
2008 * the disk has shrunk while we were reading it or the device is buggy
2009 * or damaged: either way it's not safe for use, bail out of the open.
2011 if (osize
> max_osize
) {
2012 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2013 VDEV_AUX_OPEN_FAILED
);
2014 return (SET_ERROR(ENXIO
));
2018 * Reset the vdev_reopening flag so that we actually close
2019 * the vdev on error.
2021 vd
->vdev_reopening
= B_FALSE
;
2022 if (zio_injection_enabled
&& error
== 0)
2023 error
= zio_handle_device_injection(vd
, NULL
, SET_ERROR(ENXIO
));
2026 if (vd
->vdev_removed
&&
2027 vd
->vdev_stat
.vs_aux
!= VDEV_AUX_OPEN_FAILED
)
2028 vd
->vdev_removed
= B_FALSE
;
2030 if (vd
->vdev_stat
.vs_aux
== VDEV_AUX_CHILDREN_OFFLINE
) {
2031 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
,
2032 vd
->vdev_stat
.vs_aux
);
2034 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2035 vd
->vdev_stat
.vs_aux
);
2040 vd
->vdev_removed
= B_FALSE
;
2043 * Recheck the faulted flag now that we have confirmed that
2044 * the vdev is accessible. If we're faulted, bail.
2046 if (vd
->vdev_faulted
) {
2047 ASSERT(vd
->vdev_children
== 0);
2048 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
2049 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
2050 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
2051 vd
->vdev_label_aux
);
2052 return (SET_ERROR(ENXIO
));
2055 if (vd
->vdev_degraded
) {
2056 ASSERT(vd
->vdev_children
== 0);
2057 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
2058 VDEV_AUX_ERR_EXCEEDED
);
2060 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_HEALTHY
, 0);
2064 * For hole or missing vdevs we just return success.
2066 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
)
2069 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2070 if (vd
->vdev_child
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
) {
2071 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
2077 osize
= P2ALIGN(osize
, (uint64_t)sizeof (vdev_label_t
));
2078 max_osize
= P2ALIGN(max_osize
, (uint64_t)sizeof (vdev_label_t
));
2080 if (vd
->vdev_children
== 0) {
2081 if (osize
< SPA_MINDEVSIZE
) {
2082 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2083 VDEV_AUX_TOO_SMALL
);
2084 return (SET_ERROR(EOVERFLOW
));
2087 asize
= osize
- (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
);
2088 max_asize
= max_osize
- (VDEV_LABEL_START_SIZE
+
2089 VDEV_LABEL_END_SIZE
);
2091 if (vd
->vdev_parent
!= NULL
&& osize
< SPA_MINDEVSIZE
-
2092 (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
)) {
2093 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2094 VDEV_AUX_TOO_SMALL
);
2095 return (SET_ERROR(EOVERFLOW
));
2099 max_asize
= max_osize
;
2103 * If the vdev was expanded, record this so that we can re-create the
2104 * uberblock rings in labels {2,3}, during the next sync.
2106 if ((psize
> vd
->vdev_psize
) && (vd
->vdev_psize
!= 0))
2107 vd
->vdev_copy_uberblocks
= B_TRUE
;
2109 vd
->vdev_psize
= psize
;
2112 * Make sure the allocatable size hasn't shrunk too much.
2114 if (asize
< vd
->vdev_min_asize
) {
2115 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2116 VDEV_AUX_BAD_LABEL
);
2117 return (SET_ERROR(EINVAL
));
2121 * We can always set the logical/physical ashift members since
2122 * their values are only used to calculate the vdev_ashift when
2123 * the device is first added to the config. These values should
2124 * not be used for anything else since they may change whenever
2125 * the device is reopened and we don't store them in the label.
2127 vd
->vdev_physical_ashift
=
2128 MAX(physical_ashift
, vd
->vdev_physical_ashift
);
2129 vd
->vdev_logical_ashift
= MAX(logical_ashift
,
2130 vd
->vdev_logical_ashift
);
2132 if (vd
->vdev_asize
== 0) {
2134 * This is the first-ever open, so use the computed values.
2135 * For compatibility, a different ashift can be requested.
2137 vd
->vdev_asize
= asize
;
2138 vd
->vdev_max_asize
= max_asize
;
2141 * If the vdev_ashift was not overridden at creation time,
2142 * then set it the logical ashift and optimize the ashift.
2144 if (vd
->vdev_ashift
== 0) {
2145 vd
->vdev_ashift
= vd
->vdev_logical_ashift
;
2147 if (vd
->vdev_logical_ashift
> ASHIFT_MAX
) {
2148 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2149 VDEV_AUX_ASHIFT_TOO_BIG
);
2150 return (SET_ERROR(EDOM
));
2153 if (vd
->vdev_top
== vd
&& vd
->vdev_attaching
== B_FALSE
)
2154 vdev_ashift_optimize(vd
);
2155 vd
->vdev_attaching
= B_FALSE
;
2157 if (vd
->vdev_ashift
!= 0 && (vd
->vdev_ashift
< ASHIFT_MIN
||
2158 vd
->vdev_ashift
> ASHIFT_MAX
)) {
2159 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2160 VDEV_AUX_BAD_ASHIFT
);
2161 return (SET_ERROR(EDOM
));
2165 * Make sure the alignment required hasn't increased.
2167 if (vd
->vdev_ashift
> vd
->vdev_top
->vdev_ashift
&&
2168 vd
->vdev_ops
->vdev_op_leaf
) {
2169 (void) zfs_ereport_post(
2170 FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
,
2171 spa
, vd
, NULL
, NULL
, 0);
2172 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2173 VDEV_AUX_BAD_LABEL
);
2174 return (SET_ERROR(EDOM
));
2176 vd
->vdev_max_asize
= max_asize
;
2180 * If all children are healthy we update asize if either:
2181 * The asize has increased, due to a device expansion caused by dynamic
2182 * LUN growth or vdev replacement, and automatic expansion is enabled;
2183 * making the additional space available.
2185 * The asize has decreased, due to a device shrink usually caused by a
2186 * vdev replace with a smaller device. This ensures that calculations
2187 * based of max_asize and asize e.g. esize are always valid. It's safe
2188 * to do this as we've already validated that asize is greater than
2191 if (vd
->vdev_state
== VDEV_STATE_HEALTHY
&&
2192 ((asize
> vd
->vdev_asize
&&
2193 (vd
->vdev_expanding
|| spa
->spa_autoexpand
)) ||
2194 (asize
< vd
->vdev_asize
)))
2195 vd
->vdev_asize
= asize
;
2197 vdev_set_min_asize(vd
);
2200 * Ensure we can issue some IO before declaring the
2201 * vdev open for business.
2203 if (vd
->vdev_ops
->vdev_op_leaf
&&
2204 (error
= zio_wait(vdev_probe(vd
, NULL
))) != 0) {
2205 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
2206 VDEV_AUX_ERR_EXCEEDED
);
2211 * Track the minimum allocation size.
2213 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
2214 vd
->vdev_islog
== 0 && vd
->vdev_aux
== NULL
) {
2215 uint64_t min_alloc
= vdev_get_min_alloc(vd
);
2216 if (min_alloc
< spa
->spa_min_alloc
)
2217 spa
->spa_min_alloc
= min_alloc
;
2221 * If this is a leaf vdev, assess whether a resilver is needed.
2222 * But don't do this if we are doing a reopen for a scrub, since
2223 * this would just restart the scrub we are already doing.
2225 if (vd
->vdev_ops
->vdev_op_leaf
&& !spa
->spa_scrub_reopen
)
2226 dsl_scan_assess_vdev(spa
->spa_dsl_pool
, vd
);
2232 vdev_validate_child(void *arg
)
2236 vd
->vdev_validate_thread
= curthread
;
2237 vd
->vdev_validate_error
= vdev_validate(vd
);
2238 vd
->vdev_validate_thread
= NULL
;
2242 * Called once the vdevs are all opened, this routine validates the label
2243 * contents. This needs to be done before vdev_load() so that we don't
2244 * inadvertently do repair I/Os to the wrong device.
2246 * This function will only return failure if one of the vdevs indicates that it
2247 * has since been destroyed or exported. This is only possible if
2248 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
2249 * will be updated but the function will return 0.
2252 vdev_validate(vdev_t
*vd
)
2254 spa_t
*spa
= vd
->vdev_spa
;
2257 uint64_t guid
= 0, aux_guid
= 0, top_guid
;
2261 int children
= vd
->vdev_children
;
2263 if (vdev_validate_skip
)
2267 tq
= taskq_create("vdev_validate", children
, minclsyspri
,
2268 children
, children
, TASKQ_PREPOPULATE
);
2271 for (uint64_t c
= 0; c
< children
; c
++) {
2272 vdev_t
*cvd
= vd
->vdev_child
[c
];
2274 if (tq
== NULL
|| vdev_uses_zvols(cvd
)) {
2275 vdev_validate_child(cvd
);
2277 VERIFY(taskq_dispatch(tq
, vdev_validate_child
, cvd
,
2278 TQ_SLEEP
) != TASKQID_INVALID
);
2285 for (int c
= 0; c
< children
; c
++) {
2286 int error
= vd
->vdev_child
[c
]->vdev_validate_error
;
2289 return (SET_ERROR(EBADF
));
2294 * If the device has already failed, or was marked offline, don't do
2295 * any further validation. Otherwise, label I/O will fail and we will
2296 * overwrite the previous state.
2298 if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_readable(vd
))
2302 * If we are performing an extreme rewind, we allow for a label that
2303 * was modified at a point after the current txg.
2304 * If config lock is not held do not check for the txg. spa_sync could
2305 * be updating the vdev's label before updating spa_last_synced_txg.
2307 if (spa
->spa_extreme_rewind
|| spa_last_synced_txg(spa
) == 0 ||
2308 spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) != SCL_CONFIG
)
2311 txg
= spa_last_synced_txg(spa
);
2313 if ((label
= vdev_label_read_config(vd
, txg
)) == NULL
) {
2314 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2315 VDEV_AUX_BAD_LABEL
);
2316 vdev_dbgmsg(vd
, "vdev_validate: failed reading config for "
2317 "txg %llu", (u_longlong_t
)txg
);
2322 * Determine if this vdev has been split off into another
2323 * pool. If so, then refuse to open it.
2325 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_SPLIT_GUID
,
2326 &aux_guid
) == 0 && aux_guid
== spa_guid(spa
)) {
2327 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2328 VDEV_AUX_SPLIT_POOL
);
2330 vdev_dbgmsg(vd
, "vdev_validate: vdev split into other pool");
2334 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0) {
2335 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2336 VDEV_AUX_CORRUPT_DATA
);
2338 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2339 ZPOOL_CONFIG_POOL_GUID
);
2344 * If config is not trusted then ignore the spa guid check. This is
2345 * necessary because if the machine crashed during a re-guid the new
2346 * guid might have been written to all of the vdev labels, but not the
2347 * cached config. The check will be performed again once we have the
2348 * trusted config from the MOS.
2350 if (spa
->spa_trust_config
&& guid
!= spa_guid(spa
)) {
2351 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2352 VDEV_AUX_CORRUPT_DATA
);
2354 vdev_dbgmsg(vd
, "vdev_validate: vdev label pool_guid doesn't "
2355 "match config (%llu != %llu)", (u_longlong_t
)guid
,
2356 (u_longlong_t
)spa_guid(spa
));
2360 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
)
2361 != 0 || nvlist_lookup_uint64(nvl
, ZPOOL_CONFIG_ORIG_GUID
,
2365 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0) {
2366 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2367 VDEV_AUX_CORRUPT_DATA
);
2369 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2374 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_TOP_GUID
, &top_guid
)
2376 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2377 VDEV_AUX_CORRUPT_DATA
);
2379 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2380 ZPOOL_CONFIG_TOP_GUID
);
2385 * If this vdev just became a top-level vdev because its sibling was
2386 * detached, it will have adopted the parent's vdev guid -- but the
2387 * label may or may not be on disk yet. Fortunately, either version
2388 * of the label will have the same top guid, so if we're a top-level
2389 * vdev, we can safely compare to that instead.
2390 * However, if the config comes from a cachefile that failed to update
2391 * after the detach, a top-level vdev will appear as a non top-level
2392 * vdev in the config. Also relax the constraints if we perform an
2395 * If we split this vdev off instead, then we also check the
2396 * original pool's guid. We don't want to consider the vdev
2397 * corrupt if it is partway through a split operation.
2399 if (vd
->vdev_guid
!= guid
&& vd
->vdev_guid
!= aux_guid
) {
2400 boolean_t mismatch
= B_FALSE
;
2401 if (spa
->spa_trust_config
&& !spa
->spa_extreme_rewind
) {
2402 if (vd
!= vd
->vdev_top
|| vd
->vdev_guid
!= top_guid
)
2405 if (vd
->vdev_guid
!= top_guid
&&
2406 vd
->vdev_top
->vdev_guid
!= guid
)
2411 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2412 VDEV_AUX_CORRUPT_DATA
);
2414 vdev_dbgmsg(vd
, "vdev_validate: config guid "
2415 "doesn't match label guid");
2416 vdev_dbgmsg(vd
, "CONFIG: guid %llu, top_guid %llu",
2417 (u_longlong_t
)vd
->vdev_guid
,
2418 (u_longlong_t
)vd
->vdev_top
->vdev_guid
);
2419 vdev_dbgmsg(vd
, "LABEL: guid %llu, top_guid %llu, "
2420 "aux_guid %llu", (u_longlong_t
)guid
,
2421 (u_longlong_t
)top_guid
, (u_longlong_t
)aux_guid
);
2426 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
,
2428 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2429 VDEV_AUX_CORRUPT_DATA
);
2431 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2432 ZPOOL_CONFIG_POOL_STATE
);
2439 * If this is a verbatim import, no need to check the
2440 * state of the pool.
2442 if (!(spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) &&
2443 spa_load_state(spa
) == SPA_LOAD_OPEN
&&
2444 state
!= POOL_STATE_ACTIVE
) {
2445 vdev_dbgmsg(vd
, "vdev_validate: invalid pool state (%llu) "
2446 "for spa %s", (u_longlong_t
)state
, spa
->spa_name
);
2447 return (SET_ERROR(EBADF
));
2451 * If we were able to open and validate a vdev that was
2452 * previously marked permanently unavailable, clear that state
2455 if (vd
->vdev_not_present
)
2456 vd
->vdev_not_present
= 0;
2462 vdev_copy_path_impl(vdev_t
*svd
, vdev_t
*dvd
)
2465 if (svd
->vdev_path
!= NULL
&& dvd
->vdev_path
!= NULL
) {
2466 if (strcmp(svd
->vdev_path
, dvd
->vdev_path
) != 0) {
2467 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
2468 "from '%s' to '%s'", (u_longlong_t
)dvd
->vdev_guid
,
2469 dvd
->vdev_path
, svd
->vdev_path
);
2470 spa_strfree(dvd
->vdev_path
);
2471 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
2473 } else if (svd
->vdev_path
!= NULL
) {
2474 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
2475 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
2476 (u_longlong_t
)dvd
->vdev_guid
, dvd
->vdev_path
);
2480 * Our enclosure sysfs path may have changed between imports
2482 old
= dvd
->vdev_enc_sysfs_path
;
2483 new = svd
->vdev_enc_sysfs_path
;
2484 if ((old
!= NULL
&& new == NULL
) ||
2485 (old
== NULL
&& new != NULL
) ||
2486 ((old
!= NULL
&& new != NULL
) && strcmp(new, old
) != 0)) {
2487 zfs_dbgmsg("vdev_copy_path: vdev %llu: vdev_enc_sysfs_path "
2488 "changed from '%s' to '%s'", (u_longlong_t
)dvd
->vdev_guid
,
2491 if (dvd
->vdev_enc_sysfs_path
)
2492 spa_strfree(dvd
->vdev_enc_sysfs_path
);
2494 if (svd
->vdev_enc_sysfs_path
) {
2495 dvd
->vdev_enc_sysfs_path
= spa_strdup(
2496 svd
->vdev_enc_sysfs_path
);
2498 dvd
->vdev_enc_sysfs_path
= NULL
;
2504 * Recursively copy vdev paths from one vdev to another. Source and destination
2505 * vdev trees must have same geometry otherwise return error. Intended to copy
2506 * paths from userland config into MOS config.
2509 vdev_copy_path_strict(vdev_t
*svd
, vdev_t
*dvd
)
2511 if ((svd
->vdev_ops
== &vdev_missing_ops
) ||
2512 (svd
->vdev_ishole
&& dvd
->vdev_ishole
) ||
2513 (dvd
->vdev_ops
== &vdev_indirect_ops
))
2516 if (svd
->vdev_ops
!= dvd
->vdev_ops
) {
2517 vdev_dbgmsg(svd
, "vdev_copy_path: vdev type mismatch: %s != %s",
2518 svd
->vdev_ops
->vdev_op_type
, dvd
->vdev_ops
->vdev_op_type
);
2519 return (SET_ERROR(EINVAL
));
2522 if (svd
->vdev_guid
!= dvd
->vdev_guid
) {
2523 vdev_dbgmsg(svd
, "vdev_copy_path: guids mismatch (%llu != "
2524 "%llu)", (u_longlong_t
)svd
->vdev_guid
,
2525 (u_longlong_t
)dvd
->vdev_guid
);
2526 return (SET_ERROR(EINVAL
));
2529 if (svd
->vdev_children
!= dvd
->vdev_children
) {
2530 vdev_dbgmsg(svd
, "vdev_copy_path: children count mismatch: "
2531 "%llu != %llu", (u_longlong_t
)svd
->vdev_children
,
2532 (u_longlong_t
)dvd
->vdev_children
);
2533 return (SET_ERROR(EINVAL
));
2536 for (uint64_t i
= 0; i
< svd
->vdev_children
; i
++) {
2537 int error
= vdev_copy_path_strict(svd
->vdev_child
[i
],
2538 dvd
->vdev_child
[i
]);
2543 if (svd
->vdev_ops
->vdev_op_leaf
)
2544 vdev_copy_path_impl(svd
, dvd
);
2550 vdev_copy_path_search(vdev_t
*stvd
, vdev_t
*dvd
)
2552 ASSERT(stvd
->vdev_top
== stvd
);
2553 ASSERT3U(stvd
->vdev_id
, ==, dvd
->vdev_top
->vdev_id
);
2555 for (uint64_t i
= 0; i
< dvd
->vdev_children
; i
++) {
2556 vdev_copy_path_search(stvd
, dvd
->vdev_child
[i
]);
2559 if (!dvd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(dvd
))
2563 * The idea here is that while a vdev can shift positions within
2564 * a top vdev (when replacing, attaching mirror, etc.) it cannot
2565 * step outside of it.
2567 vdev_t
*vd
= vdev_lookup_by_guid(stvd
, dvd
->vdev_guid
);
2569 if (vd
== NULL
|| vd
->vdev_ops
!= dvd
->vdev_ops
)
2572 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2574 vdev_copy_path_impl(vd
, dvd
);
2578 * Recursively copy vdev paths from one root vdev to another. Source and
2579 * destination vdev trees may differ in geometry. For each destination leaf
2580 * vdev, search a vdev with the same guid and top vdev id in the source.
2581 * Intended to copy paths from userland config into MOS config.
2584 vdev_copy_path_relaxed(vdev_t
*srvd
, vdev_t
*drvd
)
2586 uint64_t children
= MIN(srvd
->vdev_children
, drvd
->vdev_children
);
2587 ASSERT(srvd
->vdev_ops
== &vdev_root_ops
);
2588 ASSERT(drvd
->vdev_ops
== &vdev_root_ops
);
2590 for (uint64_t i
= 0; i
< children
; i
++) {
2591 vdev_copy_path_search(srvd
->vdev_child
[i
],
2592 drvd
->vdev_child
[i
]);
2597 * Close a virtual device.
2600 vdev_close(vdev_t
*vd
)
2602 vdev_t
*pvd
= vd
->vdev_parent
;
2603 spa_t
*spa __maybe_unused
= vd
->vdev_spa
;
2606 ASSERT(vd
->vdev_open_thread
== curthread
||
2607 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2610 * If our parent is reopening, then we are as well, unless we are
2613 if (pvd
!= NULL
&& pvd
->vdev_reopening
)
2614 vd
->vdev_reopening
= (pvd
->vdev_reopening
&& !vd
->vdev_offline
);
2616 vd
->vdev_ops
->vdev_op_close(vd
);
2619 * We record the previous state before we close it, so that if we are
2620 * doing a reopen(), we don't generate FMA ereports if we notice that
2621 * it's still faulted.
2623 vd
->vdev_prevstate
= vd
->vdev_state
;
2625 if (vd
->vdev_offline
)
2626 vd
->vdev_state
= VDEV_STATE_OFFLINE
;
2628 vd
->vdev_state
= VDEV_STATE_CLOSED
;
2629 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
2633 vdev_hold(vdev_t
*vd
)
2635 spa_t
*spa
= vd
->vdev_spa
;
2637 ASSERT(spa_is_root(spa
));
2638 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
2641 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2642 vdev_hold(vd
->vdev_child
[c
]);
2644 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_ops
->vdev_op_hold
!= NULL
)
2645 vd
->vdev_ops
->vdev_op_hold(vd
);
2649 vdev_rele(vdev_t
*vd
)
2651 ASSERT(spa_is_root(vd
->vdev_spa
));
2652 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2653 vdev_rele(vd
->vdev_child
[c
]);
2655 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_ops
->vdev_op_rele
!= NULL
)
2656 vd
->vdev_ops
->vdev_op_rele(vd
);
2660 * Reopen all interior vdevs and any unopened leaves. We don't actually
2661 * reopen leaf vdevs which had previously been opened as they might deadlock
2662 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2663 * If the leaf has never been opened then open it, as usual.
2666 vdev_reopen(vdev_t
*vd
)
2668 spa_t
*spa
= vd
->vdev_spa
;
2670 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2672 /* set the reopening flag unless we're taking the vdev offline */
2673 vd
->vdev_reopening
= !vd
->vdev_offline
;
2675 (void) vdev_open(vd
);
2678 * Call vdev_validate() here to make sure we have the same device.
2679 * Otherwise, a device with an invalid label could be successfully
2680 * opened in response to vdev_reopen().
2683 (void) vdev_validate_aux(vd
);
2684 if (vdev_readable(vd
) && vdev_writeable(vd
) &&
2685 vd
->vdev_aux
== &spa
->spa_l2cache
) {
2687 * In case the vdev is present we should evict all ARC
2688 * buffers and pointers to log blocks and reclaim their
2689 * space before restoring its contents to L2ARC.
2691 if (l2arc_vdev_present(vd
)) {
2692 l2arc_rebuild_vdev(vd
, B_TRUE
);
2694 l2arc_add_vdev(spa
, vd
);
2696 spa_async_request(spa
, SPA_ASYNC_L2CACHE_REBUILD
);
2697 spa_async_request(spa
, SPA_ASYNC_L2CACHE_TRIM
);
2700 (void) vdev_validate(vd
);
2704 * Recheck if resilver is still needed and cancel any
2705 * scheduled resilver if resilver is unneeded.
2707 if (!vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
) &&
2708 spa
->spa_async_tasks
& SPA_ASYNC_RESILVER
) {
2709 mutex_enter(&spa
->spa_async_lock
);
2710 spa
->spa_async_tasks
&= ~SPA_ASYNC_RESILVER
;
2711 mutex_exit(&spa
->spa_async_lock
);
2715 * Reassess parent vdev's health.
2717 vdev_propagate_state(vd
);
2721 vdev_create(vdev_t
*vd
, uint64_t txg
, boolean_t isreplacing
)
2726 * Normally, partial opens (e.g. of a mirror) are allowed.
2727 * For a create, however, we want to fail the request if
2728 * there are any components we can't open.
2730 error
= vdev_open(vd
);
2732 if (error
|| vd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
2734 return (error
? error
: SET_ERROR(ENXIO
));
2738 * Recursively load DTLs and initialize all labels.
2740 if ((error
= vdev_dtl_load(vd
)) != 0 ||
2741 (error
= vdev_label_init(vd
, txg
, isreplacing
?
2742 VDEV_LABEL_REPLACE
: VDEV_LABEL_CREATE
)) != 0) {
2751 vdev_metaslab_set_size(vdev_t
*vd
)
2753 uint64_t asize
= vd
->vdev_asize
;
2754 uint64_t ms_count
= asize
>> zfs_vdev_default_ms_shift
;
2758 * There are two dimensions to the metaslab sizing calculation:
2759 * the size of the metaslab and the count of metaslabs per vdev.
2761 * The default values used below are a good balance between memory
2762 * usage (larger metaslab size means more memory needed for loaded
2763 * metaslabs; more metaslabs means more memory needed for the
2764 * metaslab_t structs), metaslab load time (larger metaslabs take
2765 * longer to load), and metaslab sync time (more metaslabs means
2766 * more time spent syncing all of them).
2768 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2769 * The range of the dimensions are as follows:
2771 * 2^29 <= ms_size <= 2^34
2772 * 16 <= ms_count <= 131,072
2774 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2775 * at least 512MB (2^29) to minimize fragmentation effects when
2776 * testing with smaller devices. However, the count constraint
2777 * of at least 16 metaslabs will override this minimum size goal.
2779 * On the upper end of vdev sizes, we aim for a maximum metaslab
2780 * size of 16GB. However, we will cap the total count to 2^17
2781 * metaslabs to keep our memory footprint in check and let the
2782 * metaslab size grow from there if that limit is hit.
2784 * The net effect of applying above constrains is summarized below.
2786 * vdev size metaslab count
2787 * --------------|-----------------
2789 * 8GB - 100GB one per 512MB
2791 * 3TB - 2PB one per 16GB
2793 * --------------------------------
2795 * Finally, note that all of the above calculate the initial
2796 * number of metaslabs. Expanding a top-level vdev will result
2797 * in additional metaslabs being allocated making it possible
2798 * to exceed the zfs_vdev_ms_count_limit.
2801 if (ms_count
< zfs_vdev_min_ms_count
)
2802 ms_shift
= highbit64(asize
/ zfs_vdev_min_ms_count
);
2803 else if (ms_count
> zfs_vdev_default_ms_count
)
2804 ms_shift
= highbit64(asize
/ zfs_vdev_default_ms_count
);
2806 ms_shift
= zfs_vdev_default_ms_shift
;
2808 if (ms_shift
< SPA_MAXBLOCKSHIFT
) {
2809 ms_shift
= SPA_MAXBLOCKSHIFT
;
2810 } else if (ms_shift
> zfs_vdev_max_ms_shift
) {
2811 ms_shift
= zfs_vdev_max_ms_shift
;
2812 /* cap the total count to constrain memory footprint */
2813 if ((asize
>> ms_shift
) > zfs_vdev_ms_count_limit
)
2814 ms_shift
= highbit64(asize
/ zfs_vdev_ms_count_limit
);
2817 vd
->vdev_ms_shift
= ms_shift
;
2818 ASSERT3U(vd
->vdev_ms_shift
, >=, SPA_MAXBLOCKSHIFT
);
2822 vdev_dirty(vdev_t
*vd
, int flags
, void *arg
, uint64_t txg
)
2824 ASSERT(vd
== vd
->vdev_top
);
2825 /* indirect vdevs don't have metaslabs or dtls */
2826 ASSERT(vdev_is_concrete(vd
) || flags
== 0);
2827 ASSERT(ISP2(flags
));
2828 ASSERT(spa_writeable(vd
->vdev_spa
));
2830 if (flags
& VDD_METASLAB
)
2831 (void) txg_list_add(&vd
->vdev_ms_list
, arg
, txg
);
2833 if (flags
& VDD_DTL
)
2834 (void) txg_list_add(&vd
->vdev_dtl_list
, arg
, txg
);
2836 (void) txg_list_add(&vd
->vdev_spa
->spa_vdev_txg_list
, vd
, txg
);
2840 vdev_dirty_leaves(vdev_t
*vd
, int flags
, uint64_t txg
)
2842 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2843 vdev_dirty_leaves(vd
->vdev_child
[c
], flags
, txg
);
2845 if (vd
->vdev_ops
->vdev_op_leaf
)
2846 vdev_dirty(vd
->vdev_top
, flags
, vd
, txg
);
2852 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2853 * the vdev has less than perfect replication. There are four kinds of DTL:
2855 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2857 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2859 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2860 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2861 * txgs that was scrubbed.
2863 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2864 * persistent errors or just some device being offline.
2865 * Unlike the other three, the DTL_OUTAGE map is not generally
2866 * maintained; it's only computed when needed, typically to
2867 * determine whether a device can be detached.
2869 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2870 * either has the data or it doesn't.
2872 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2873 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2874 * if any child is less than fully replicated, then so is its parent.
2875 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2876 * comprising only those txgs which appear in 'maxfaults' or more children;
2877 * those are the txgs we don't have enough replication to read. For example,
2878 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2879 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2880 * two child DTL_MISSING maps.
2882 * It should be clear from the above that to compute the DTLs and outage maps
2883 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2884 * Therefore, that is all we keep on disk. When loading the pool, or after
2885 * a configuration change, we generate all other DTLs from first principles.
2888 vdev_dtl_dirty(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2890 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2892 ASSERT(t
< DTL_TYPES
);
2893 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2894 ASSERT(spa_writeable(vd
->vdev_spa
));
2896 mutex_enter(&vd
->vdev_dtl_lock
);
2897 if (!range_tree_contains(rt
, txg
, size
))
2898 range_tree_add(rt
, txg
, size
);
2899 mutex_exit(&vd
->vdev_dtl_lock
);
2903 vdev_dtl_contains(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2905 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2906 boolean_t dirty
= B_FALSE
;
2908 ASSERT(t
< DTL_TYPES
);
2909 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2912 * While we are loading the pool, the DTLs have not been loaded yet.
2913 * This isn't a problem but it can result in devices being tried
2914 * which are known to not have the data. In which case, the import
2915 * is relying on the checksum to ensure that we get the right data.
2916 * Note that while importing we are only reading the MOS, which is
2917 * always checksummed.
2919 mutex_enter(&vd
->vdev_dtl_lock
);
2920 if (!range_tree_is_empty(rt
))
2921 dirty
= range_tree_contains(rt
, txg
, size
);
2922 mutex_exit(&vd
->vdev_dtl_lock
);
2928 vdev_dtl_empty(vdev_t
*vd
, vdev_dtl_type_t t
)
2930 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2933 mutex_enter(&vd
->vdev_dtl_lock
);
2934 empty
= range_tree_is_empty(rt
);
2935 mutex_exit(&vd
->vdev_dtl_lock
);
2941 * Check if the txg falls within the range which must be
2942 * resilvered. DVAs outside this range can always be skipped.
2945 vdev_default_need_resilver(vdev_t
*vd
, const dva_t
*dva
, size_t psize
,
2946 uint64_t phys_birth
)
2948 (void) dva
, (void) psize
;
2950 /* Set by sequential resilver. */
2951 if (phys_birth
== TXG_UNKNOWN
)
2954 return (vdev_dtl_contains(vd
, DTL_PARTIAL
, phys_birth
, 1));
2958 * Returns B_TRUE if the vdev determines the DVA needs to be resilvered.
2961 vdev_dtl_need_resilver(vdev_t
*vd
, const dva_t
*dva
, size_t psize
,
2962 uint64_t phys_birth
)
2964 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2966 if (vd
->vdev_ops
->vdev_op_need_resilver
== NULL
||
2967 vd
->vdev_ops
->vdev_op_leaf
)
2970 return (vd
->vdev_ops
->vdev_op_need_resilver(vd
, dva
, psize
,
2975 * Returns the lowest txg in the DTL range.
2978 vdev_dtl_min(vdev_t
*vd
)
2980 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2981 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2982 ASSERT0(vd
->vdev_children
);
2984 return (range_tree_min(vd
->vdev_dtl
[DTL_MISSING
]) - 1);
2988 * Returns the highest txg in the DTL.
2991 vdev_dtl_max(vdev_t
*vd
)
2993 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2994 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2995 ASSERT0(vd
->vdev_children
);
2997 return (range_tree_max(vd
->vdev_dtl
[DTL_MISSING
]));
3001 * Determine if a resilvering vdev should remove any DTL entries from
3002 * its range. If the vdev was resilvering for the entire duration of the
3003 * scan then it should excise that range from its DTLs. Otherwise, this
3004 * vdev is considered partially resilvered and should leave its DTL
3005 * entries intact. The comment in vdev_dtl_reassess() describes how we
3009 vdev_dtl_should_excise(vdev_t
*vd
, boolean_t rebuild_done
)
3011 ASSERT0(vd
->vdev_children
);
3013 if (vd
->vdev_state
< VDEV_STATE_DEGRADED
)
3016 if (vd
->vdev_resilver_deferred
)
3019 if (range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]))
3023 vdev_rebuild_t
*vr
= &vd
->vdev_top
->vdev_rebuild_config
;
3024 vdev_rebuild_phys_t
*vrp
= &vr
->vr_rebuild_phys
;
3026 /* Rebuild not initiated by attach */
3027 if (vd
->vdev_rebuild_txg
== 0)
3031 * When a rebuild completes without error then all missing data
3032 * up to the rebuild max txg has been reconstructed and the DTL
3033 * is eligible for excision.
3035 if (vrp
->vrp_rebuild_state
== VDEV_REBUILD_COMPLETE
&&
3036 vdev_dtl_max(vd
) <= vrp
->vrp_max_txg
) {
3037 ASSERT3U(vrp
->vrp_min_txg
, <=, vdev_dtl_min(vd
));
3038 ASSERT3U(vrp
->vrp_min_txg
, <, vd
->vdev_rebuild_txg
);
3039 ASSERT3U(vd
->vdev_rebuild_txg
, <=, vrp
->vrp_max_txg
);
3043 dsl_scan_t
*scn
= vd
->vdev_spa
->spa_dsl_pool
->dp_scan
;
3044 dsl_scan_phys_t
*scnp __maybe_unused
= &scn
->scn_phys
;
3046 /* Resilver not initiated by attach */
3047 if (vd
->vdev_resilver_txg
== 0)
3051 * When a resilver is initiated the scan will assign the
3052 * scn_max_txg value to the highest txg value that exists
3053 * in all DTLs. If this device's max DTL is not part of this
3054 * scan (i.e. it is not in the range (scn_min_txg, scn_max_txg]
3055 * then it is not eligible for excision.
3057 if (vdev_dtl_max(vd
) <= scn
->scn_phys
.scn_max_txg
) {
3058 ASSERT3U(scnp
->scn_min_txg
, <=, vdev_dtl_min(vd
));
3059 ASSERT3U(scnp
->scn_min_txg
, <, vd
->vdev_resilver_txg
);
3060 ASSERT3U(vd
->vdev_resilver_txg
, <=, scnp
->scn_max_txg
);
3069 * Reassess DTLs after a config change or scrub completion. If txg == 0 no
3070 * write operations will be issued to the pool.
3073 vdev_dtl_reassess(vdev_t
*vd
, uint64_t txg
, uint64_t scrub_txg
,
3074 boolean_t scrub_done
, boolean_t rebuild_done
)
3076 spa_t
*spa
= vd
->vdev_spa
;
3080 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
3082 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3083 vdev_dtl_reassess(vd
->vdev_child
[c
], txg
,
3084 scrub_txg
, scrub_done
, rebuild_done
);
3086 if (vd
== spa
->spa_root_vdev
|| !vdev_is_concrete(vd
) || vd
->vdev_aux
)
3089 if (vd
->vdev_ops
->vdev_op_leaf
) {
3090 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
3091 vdev_rebuild_t
*vr
= &vd
->vdev_top
->vdev_rebuild_config
;
3092 boolean_t check_excise
= B_FALSE
;
3093 boolean_t wasempty
= B_TRUE
;
3095 mutex_enter(&vd
->vdev_dtl_lock
);
3098 * If requested, pretend the scan or rebuild completed cleanly.
3100 if (zfs_scan_ignore_errors
) {
3102 scn
->scn_phys
.scn_errors
= 0;
3104 vr
->vr_rebuild_phys
.vrp_errors
= 0;
3107 if (scrub_txg
!= 0 &&
3108 !range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
])) {
3110 zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d "
3111 "dtl:%llu/%llu errors:%llu",
3112 (u_longlong_t
)vd
->vdev_guid
, (u_longlong_t
)txg
,
3113 (u_longlong_t
)scrub_txg
, spa
->spa_scrub_started
,
3114 (u_longlong_t
)vdev_dtl_min(vd
),
3115 (u_longlong_t
)vdev_dtl_max(vd
),
3116 (u_longlong_t
)(scn
? scn
->scn_phys
.scn_errors
: 0));
3120 * If we've completed a scrub/resilver or a rebuild cleanly
3121 * then determine if this vdev should remove any DTLs. We
3122 * only want to excise regions on vdevs that were available
3123 * during the entire duration of this scan.
3126 vr
!= NULL
&& vr
->vr_rebuild_phys
.vrp_errors
== 0) {
3127 check_excise
= B_TRUE
;
3129 if (spa
->spa_scrub_started
||
3130 (scn
!= NULL
&& scn
->scn_phys
.scn_errors
== 0)) {
3131 check_excise
= B_TRUE
;
3135 if (scrub_txg
&& check_excise
&&
3136 vdev_dtl_should_excise(vd
, rebuild_done
)) {
3138 * We completed a scrub, resilver or rebuild up to
3139 * scrub_txg. If we did it without rebooting, then
3140 * the scrub dtl will be valid, so excise the old
3141 * region and fold in the scrub dtl. Otherwise,
3142 * leave the dtl as-is if there was an error.
3144 * There's little trick here: to excise the beginning
3145 * of the DTL_MISSING map, we put it into a reference
3146 * tree and then add a segment with refcnt -1 that
3147 * covers the range [0, scrub_txg). This means
3148 * that each txg in that range has refcnt -1 or 0.
3149 * We then add DTL_SCRUB with a refcnt of 2, so that
3150 * entries in the range [0, scrub_txg) will have a
3151 * positive refcnt -- either 1 or 2. We then convert
3152 * the reference tree into the new DTL_MISSING map.
3154 space_reftree_create(&reftree
);
3155 space_reftree_add_map(&reftree
,
3156 vd
->vdev_dtl
[DTL_MISSING
], 1);
3157 space_reftree_add_seg(&reftree
, 0, scrub_txg
, -1);
3158 space_reftree_add_map(&reftree
,
3159 vd
->vdev_dtl
[DTL_SCRUB
], 2);
3160 space_reftree_generate_map(&reftree
,
3161 vd
->vdev_dtl
[DTL_MISSING
], 1);
3162 space_reftree_destroy(&reftree
);
3164 if (!range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
])) {
3165 zfs_dbgmsg("update DTL_MISSING:%llu/%llu",
3166 (u_longlong_t
)vdev_dtl_min(vd
),
3167 (u_longlong_t
)vdev_dtl_max(vd
));
3168 } else if (!wasempty
) {
3169 zfs_dbgmsg("DTL_MISSING is now empty");
3172 range_tree_vacate(vd
->vdev_dtl
[DTL_PARTIAL
], NULL
, NULL
);
3173 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
3174 range_tree_add
, vd
->vdev_dtl
[DTL_PARTIAL
]);
3176 range_tree_vacate(vd
->vdev_dtl
[DTL_SCRUB
], NULL
, NULL
);
3177 range_tree_vacate(vd
->vdev_dtl
[DTL_OUTAGE
], NULL
, NULL
);
3178 if (!vdev_readable(vd
))
3179 range_tree_add(vd
->vdev_dtl
[DTL_OUTAGE
], 0, -1ULL);
3181 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
3182 range_tree_add
, vd
->vdev_dtl
[DTL_OUTAGE
]);
3185 * If the vdev was resilvering or rebuilding and no longer
3186 * has any DTLs then reset the appropriate flag and dirty
3187 * the top level so that we persist the change.
3190 range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]) &&
3191 range_tree_is_empty(vd
->vdev_dtl
[DTL_OUTAGE
])) {
3192 if (vd
->vdev_rebuild_txg
!= 0) {
3193 vd
->vdev_rebuild_txg
= 0;
3194 vdev_config_dirty(vd
->vdev_top
);
3195 } else if (vd
->vdev_resilver_txg
!= 0) {
3196 vd
->vdev_resilver_txg
= 0;
3197 vdev_config_dirty(vd
->vdev_top
);
3201 mutex_exit(&vd
->vdev_dtl_lock
);
3204 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, txg
);
3208 mutex_enter(&vd
->vdev_dtl_lock
);
3209 for (int t
= 0; t
< DTL_TYPES
; t
++) {
3210 /* account for child's outage in parent's missing map */
3211 int s
= (t
== DTL_MISSING
) ? DTL_OUTAGE
: t
;
3213 continue; /* leaf vdevs only */
3214 if (t
== DTL_PARTIAL
)
3215 minref
= 1; /* i.e. non-zero */
3216 else if (vdev_get_nparity(vd
) != 0)
3217 minref
= vdev_get_nparity(vd
) + 1; /* RAID-Z, dRAID */
3219 minref
= vd
->vdev_children
; /* any kind of mirror */
3220 space_reftree_create(&reftree
);
3221 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3222 vdev_t
*cvd
= vd
->vdev_child
[c
];
3223 mutex_enter(&cvd
->vdev_dtl_lock
);
3224 space_reftree_add_map(&reftree
, cvd
->vdev_dtl
[s
], 1);
3225 mutex_exit(&cvd
->vdev_dtl_lock
);
3227 space_reftree_generate_map(&reftree
, vd
->vdev_dtl
[t
], minref
);
3228 space_reftree_destroy(&reftree
);
3230 mutex_exit(&vd
->vdev_dtl_lock
);
3234 * Iterate over all the vdevs except spare, and post kobj events
3237 vdev_post_kobj_evt(vdev_t
*vd
)
3239 if (vd
->vdev_ops
->vdev_op_kobj_evt_post
&&
3240 vd
->vdev_kobj_flag
== B_FALSE
) {
3241 vd
->vdev_kobj_flag
= B_TRUE
;
3242 vd
->vdev_ops
->vdev_op_kobj_evt_post(vd
);
3245 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3246 vdev_post_kobj_evt(vd
->vdev_child
[c
]);
3250 * Iterate over all the vdevs except spare, and clear kobj events
3253 vdev_clear_kobj_evt(vdev_t
*vd
)
3255 vd
->vdev_kobj_flag
= B_FALSE
;
3257 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3258 vdev_clear_kobj_evt(vd
->vdev_child
[c
]);
3262 vdev_dtl_load(vdev_t
*vd
)
3264 spa_t
*spa
= vd
->vdev_spa
;
3265 objset_t
*mos
= spa
->spa_meta_objset
;
3269 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_dtl_object
!= 0) {
3270 ASSERT(vdev_is_concrete(vd
));
3273 * If the dtl cannot be sync'd there is no need to open it.
3275 if (spa
->spa_mode
== SPA_MODE_READ
&& !spa
->spa_read_spacemaps
)
3278 error
= space_map_open(&vd
->vdev_dtl_sm
, mos
,
3279 vd
->vdev_dtl_object
, 0, -1ULL, 0);
3282 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
3284 rt
= range_tree_create(NULL
, RANGE_SEG64
, NULL
, 0, 0);
3285 error
= space_map_load(vd
->vdev_dtl_sm
, rt
, SM_ALLOC
);
3287 mutex_enter(&vd
->vdev_dtl_lock
);
3288 range_tree_walk(rt
, range_tree_add
,
3289 vd
->vdev_dtl
[DTL_MISSING
]);
3290 mutex_exit(&vd
->vdev_dtl_lock
);
3293 range_tree_vacate(rt
, NULL
, NULL
);
3294 range_tree_destroy(rt
);
3299 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3300 error
= vdev_dtl_load(vd
->vdev_child
[c
]);
3309 vdev_zap_allocation_data(vdev_t
*vd
, dmu_tx_t
*tx
)
3311 spa_t
*spa
= vd
->vdev_spa
;
3312 objset_t
*mos
= spa
->spa_meta_objset
;
3313 vdev_alloc_bias_t alloc_bias
= vd
->vdev_alloc_bias
;
3316 ASSERT(alloc_bias
!= VDEV_BIAS_NONE
);
3319 (alloc_bias
== VDEV_BIAS_LOG
) ? VDEV_ALLOC_BIAS_LOG
:
3320 (alloc_bias
== VDEV_BIAS_SPECIAL
) ? VDEV_ALLOC_BIAS_SPECIAL
:
3321 (alloc_bias
== VDEV_BIAS_DEDUP
) ? VDEV_ALLOC_BIAS_DEDUP
: NULL
;
3323 ASSERT(string
!= NULL
);
3324 VERIFY0(zap_add(mos
, vd
->vdev_top_zap
, VDEV_TOP_ZAP_ALLOCATION_BIAS
,
3325 1, strlen(string
) + 1, string
, tx
));
3327 if (alloc_bias
== VDEV_BIAS_SPECIAL
|| alloc_bias
== VDEV_BIAS_DEDUP
) {
3328 spa_activate_allocation_classes(spa
, tx
);
3333 vdev_destroy_unlink_zap(vdev_t
*vd
, uint64_t zapobj
, dmu_tx_t
*tx
)
3335 spa_t
*spa
= vd
->vdev_spa
;
3337 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zapobj
, tx
));
3338 VERIFY0(zap_remove_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
3343 vdev_create_link_zap(vdev_t
*vd
, dmu_tx_t
*tx
)
3345 spa_t
*spa
= vd
->vdev_spa
;
3346 uint64_t zap
= zap_create(spa
->spa_meta_objset
, DMU_OTN_ZAP_METADATA
,
3347 DMU_OT_NONE
, 0, tx
);
3350 VERIFY0(zap_add_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
3357 vdev_construct_zaps(vdev_t
*vd
, dmu_tx_t
*tx
)
3359 if (vd
->vdev_ops
!= &vdev_hole_ops
&&
3360 vd
->vdev_ops
!= &vdev_missing_ops
&&
3361 vd
->vdev_ops
!= &vdev_root_ops
&&
3362 !vd
->vdev_top
->vdev_removing
) {
3363 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_leaf_zap
== 0) {
3364 vd
->vdev_leaf_zap
= vdev_create_link_zap(vd
, tx
);
3366 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
== 0) {
3367 vd
->vdev_top_zap
= vdev_create_link_zap(vd
, tx
);
3368 if (vd
->vdev_alloc_bias
!= VDEV_BIAS_NONE
)
3369 vdev_zap_allocation_data(vd
, tx
);
3372 if (vd
->vdev_ops
== &vdev_root_ops
&& vd
->vdev_root_zap
== 0 &&
3373 spa_feature_is_enabled(vd
->vdev_spa
, SPA_FEATURE_AVZ_V2
)) {
3374 if (!spa_feature_is_active(vd
->vdev_spa
, SPA_FEATURE_AVZ_V2
))
3375 spa_feature_incr(vd
->vdev_spa
, SPA_FEATURE_AVZ_V2
, tx
);
3376 vd
->vdev_root_zap
= vdev_create_link_zap(vd
, tx
);
3379 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
3380 vdev_construct_zaps(vd
->vdev_child
[i
], tx
);
3385 vdev_dtl_sync(vdev_t
*vd
, uint64_t txg
)
3387 spa_t
*spa
= vd
->vdev_spa
;
3388 range_tree_t
*rt
= vd
->vdev_dtl
[DTL_MISSING
];
3389 objset_t
*mos
= spa
->spa_meta_objset
;
3390 range_tree_t
*rtsync
;
3392 uint64_t object
= space_map_object(vd
->vdev_dtl_sm
);
3394 ASSERT(vdev_is_concrete(vd
));
3395 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3397 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
3399 if (vd
->vdev_detached
|| vd
->vdev_top
->vdev_removing
) {
3400 mutex_enter(&vd
->vdev_dtl_lock
);
3401 space_map_free(vd
->vdev_dtl_sm
, tx
);
3402 space_map_close(vd
->vdev_dtl_sm
);
3403 vd
->vdev_dtl_sm
= NULL
;
3404 mutex_exit(&vd
->vdev_dtl_lock
);
3407 * We only destroy the leaf ZAP for detached leaves or for
3408 * removed log devices. Removed data devices handle leaf ZAP
3409 * cleanup later, once cancellation is no longer possible.
3411 if (vd
->vdev_leaf_zap
!= 0 && (vd
->vdev_detached
||
3412 vd
->vdev_top
->vdev_islog
)) {
3413 vdev_destroy_unlink_zap(vd
, vd
->vdev_leaf_zap
, tx
);
3414 vd
->vdev_leaf_zap
= 0;
3421 if (vd
->vdev_dtl_sm
== NULL
) {
3422 uint64_t new_object
;
3424 new_object
= space_map_alloc(mos
, zfs_vdev_dtl_sm_blksz
, tx
);
3425 VERIFY3U(new_object
, !=, 0);
3427 VERIFY0(space_map_open(&vd
->vdev_dtl_sm
, mos
, new_object
,
3429 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
3432 rtsync
= range_tree_create(NULL
, RANGE_SEG64
, NULL
, 0, 0);
3434 mutex_enter(&vd
->vdev_dtl_lock
);
3435 range_tree_walk(rt
, range_tree_add
, rtsync
);
3436 mutex_exit(&vd
->vdev_dtl_lock
);
3438 space_map_truncate(vd
->vdev_dtl_sm
, zfs_vdev_dtl_sm_blksz
, tx
);
3439 space_map_write(vd
->vdev_dtl_sm
, rtsync
, SM_ALLOC
, SM_NO_VDEVID
, tx
);
3440 range_tree_vacate(rtsync
, NULL
, NULL
);
3442 range_tree_destroy(rtsync
);
3445 * If the object for the space map has changed then dirty
3446 * the top level so that we update the config.
3448 if (object
!= space_map_object(vd
->vdev_dtl_sm
)) {
3449 vdev_dbgmsg(vd
, "txg %llu, spa %s, DTL old object %llu, "
3450 "new object %llu", (u_longlong_t
)txg
, spa_name(spa
),
3451 (u_longlong_t
)object
,
3452 (u_longlong_t
)space_map_object(vd
->vdev_dtl_sm
));
3453 vdev_config_dirty(vd
->vdev_top
);
3460 * Determine whether the specified vdev can be offlined/detached/removed
3461 * without losing data.
3464 vdev_dtl_required(vdev_t
*vd
)
3466 spa_t
*spa
= vd
->vdev_spa
;
3467 vdev_t
*tvd
= vd
->vdev_top
;
3468 uint8_t cant_read
= vd
->vdev_cant_read
;
3471 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
3473 if (vd
== spa
->spa_root_vdev
|| vd
== tvd
)
3477 * Temporarily mark the device as unreadable, and then determine
3478 * whether this results in any DTL outages in the top-level vdev.
3479 * If not, we can safely offline/detach/remove the device.
3481 vd
->vdev_cant_read
= B_TRUE
;
3482 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
, B_FALSE
);
3483 required
= !vdev_dtl_empty(tvd
, DTL_OUTAGE
);
3484 vd
->vdev_cant_read
= cant_read
;
3485 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
, B_FALSE
);
3487 if (!required
&& zio_injection_enabled
) {
3488 required
= !!zio_handle_device_injection(vd
, NULL
,
3496 * Determine if resilver is needed, and if so the txg range.
3499 vdev_resilver_needed(vdev_t
*vd
, uint64_t *minp
, uint64_t *maxp
)
3501 boolean_t needed
= B_FALSE
;
3502 uint64_t thismin
= UINT64_MAX
;
3503 uint64_t thismax
= 0;
3505 if (vd
->vdev_children
== 0) {
3506 mutex_enter(&vd
->vdev_dtl_lock
);
3507 if (!range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]) &&
3508 vdev_writeable(vd
)) {
3510 thismin
= vdev_dtl_min(vd
);
3511 thismax
= vdev_dtl_max(vd
);
3514 mutex_exit(&vd
->vdev_dtl_lock
);
3516 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3517 vdev_t
*cvd
= vd
->vdev_child
[c
];
3518 uint64_t cmin
, cmax
;
3520 if (vdev_resilver_needed(cvd
, &cmin
, &cmax
)) {
3521 thismin
= MIN(thismin
, cmin
);
3522 thismax
= MAX(thismax
, cmax
);
3528 if (needed
&& minp
) {
3536 * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj
3537 * will contain either the checkpoint spacemap object or zero if none exists.
3538 * All other errors are returned to the caller.
3541 vdev_checkpoint_sm_object(vdev_t
*vd
, uint64_t *sm_obj
)
3543 ASSERT0(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
));
3545 if (vd
->vdev_top_zap
== 0) {
3550 int error
= zap_lookup(spa_meta_objset(vd
->vdev_spa
), vd
->vdev_top_zap
,
3551 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM
, sizeof (uint64_t), 1, sm_obj
);
3552 if (error
== ENOENT
) {
3561 vdev_load(vdev_t
*vd
)
3563 int children
= vd
->vdev_children
;
3568 * It's only worthwhile to use the taskq for the root vdev, because the
3569 * slow part is metaslab_init, and that only happens for top-level
3572 if (vd
->vdev_ops
== &vdev_root_ops
&& vd
->vdev_children
> 0) {
3573 tq
= taskq_create("vdev_load", children
, minclsyspri
,
3574 children
, children
, TASKQ_PREPOPULATE
);
3578 * Recursively load all children.
3580 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3581 vdev_t
*cvd
= vd
->vdev_child
[c
];
3583 if (tq
== NULL
|| vdev_uses_zvols(cvd
)) {
3584 cvd
->vdev_load_error
= vdev_load(cvd
);
3586 VERIFY(taskq_dispatch(tq
, vdev_load_child
,
3587 cvd
, TQ_SLEEP
) != TASKQID_INVALID
);
3596 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3597 int error
= vd
->vdev_child
[c
]->vdev_load_error
;
3603 vdev_set_deflate_ratio(vd
);
3606 * On spa_load path, grab the allocation bias from our zap
3608 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
!= 0) {
3609 spa_t
*spa
= vd
->vdev_spa
;
3612 error
= zap_lookup(spa
->spa_meta_objset
, vd
->vdev_top_zap
,
3613 VDEV_TOP_ZAP_ALLOCATION_BIAS
, 1, sizeof (bias_str
),
3616 ASSERT(vd
->vdev_alloc_bias
== VDEV_BIAS_NONE
);
3617 vd
->vdev_alloc_bias
= vdev_derive_alloc_bias(bias_str
);
3618 } else if (error
!= ENOENT
) {
3619 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3620 VDEV_AUX_CORRUPT_DATA
);
3621 vdev_dbgmsg(vd
, "vdev_load: zap_lookup(top_zap=%llu) "
3622 "failed [error=%d]",
3623 (u_longlong_t
)vd
->vdev_top_zap
, error
);
3628 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
!= 0) {
3629 spa_t
*spa
= vd
->vdev_spa
;
3632 error
= zap_lookup(spa
->spa_meta_objset
, vd
->vdev_top_zap
,
3633 vdev_prop_to_name(VDEV_PROP_FAILFAST
), sizeof (failfast
),
3636 vd
->vdev_failfast
= failfast
& 1;
3637 } else if (error
== ENOENT
) {
3638 vd
->vdev_failfast
= vdev_prop_default_numeric(
3639 VDEV_PROP_FAILFAST
);
3642 "vdev_load: zap_lookup(top_zap=%llu) "
3643 "failed [error=%d]",
3644 (u_longlong_t
)vd
->vdev_top_zap
, error
);
3649 * Load any rebuild state from the top-level vdev zap.
3651 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
!= 0) {
3652 error
= vdev_rebuild_load(vd
);
3653 if (error
&& error
!= ENOTSUP
) {
3654 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3655 VDEV_AUX_CORRUPT_DATA
);
3656 vdev_dbgmsg(vd
, "vdev_load: vdev_rebuild_load "
3657 "failed [error=%d]", error
);
3662 if (vd
->vdev_top_zap
!= 0 || vd
->vdev_leaf_zap
!= 0) {
3665 if (vd
->vdev_top_zap
!= 0)
3666 zapobj
= vd
->vdev_top_zap
;
3668 zapobj
= vd
->vdev_leaf_zap
;
3670 error
= vdev_prop_get_int(vd
, VDEV_PROP_CHECKSUM_N
,
3671 &vd
->vdev_checksum_n
);
3672 if (error
&& error
!= ENOENT
)
3673 vdev_dbgmsg(vd
, "vdev_load: zap_lookup(zap=%llu) "
3674 "failed [error=%d]", (u_longlong_t
)zapobj
, error
);
3676 error
= vdev_prop_get_int(vd
, VDEV_PROP_CHECKSUM_T
,
3677 &vd
->vdev_checksum_t
);
3678 if (error
&& error
!= ENOENT
)
3679 vdev_dbgmsg(vd
, "vdev_load: zap_lookup(zap=%llu) "
3680 "failed [error=%d]", (u_longlong_t
)zapobj
, error
);
3682 error
= vdev_prop_get_int(vd
, VDEV_PROP_IO_N
,
3684 if (error
&& error
!= ENOENT
)
3685 vdev_dbgmsg(vd
, "vdev_load: zap_lookup(zap=%llu) "
3686 "failed [error=%d]", (u_longlong_t
)zapobj
, error
);
3688 error
= vdev_prop_get_int(vd
, VDEV_PROP_IO_T
,
3690 if (error
&& error
!= ENOENT
)
3691 vdev_dbgmsg(vd
, "vdev_load: zap_lookup(zap=%llu) "
3692 "failed [error=%d]", (u_longlong_t
)zapobj
, error
);
3696 * If this is a top-level vdev, initialize its metaslabs.
3698 if (vd
== vd
->vdev_top
&& vdev_is_concrete(vd
)) {
3699 vdev_metaslab_group_create(vd
);
3701 if (vd
->vdev_ashift
== 0 || vd
->vdev_asize
== 0) {
3702 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3703 VDEV_AUX_CORRUPT_DATA
);
3704 vdev_dbgmsg(vd
, "vdev_load: invalid size. ashift=%llu, "
3705 "asize=%llu", (u_longlong_t
)vd
->vdev_ashift
,
3706 (u_longlong_t
)vd
->vdev_asize
);
3707 return (SET_ERROR(ENXIO
));
3710 error
= vdev_metaslab_init(vd
, 0);
3712 vdev_dbgmsg(vd
, "vdev_load: metaslab_init failed "
3713 "[error=%d]", error
);
3714 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3715 VDEV_AUX_CORRUPT_DATA
);
3719 uint64_t checkpoint_sm_obj
;
3720 error
= vdev_checkpoint_sm_object(vd
, &checkpoint_sm_obj
);
3721 if (error
== 0 && checkpoint_sm_obj
!= 0) {
3722 objset_t
*mos
= spa_meta_objset(vd
->vdev_spa
);
3723 ASSERT(vd
->vdev_asize
!= 0);
3724 ASSERT3P(vd
->vdev_checkpoint_sm
, ==, NULL
);
3726 error
= space_map_open(&vd
->vdev_checkpoint_sm
,
3727 mos
, checkpoint_sm_obj
, 0, vd
->vdev_asize
,
3730 vdev_dbgmsg(vd
, "vdev_load: space_map_open "
3731 "failed for checkpoint spacemap (obj %llu) "
3733 (u_longlong_t
)checkpoint_sm_obj
, error
);
3736 ASSERT3P(vd
->vdev_checkpoint_sm
, !=, NULL
);
3739 * Since the checkpoint_sm contains free entries
3740 * exclusively we can use space_map_allocated() to
3741 * indicate the cumulative checkpointed space that
3744 vd
->vdev_stat
.vs_checkpoint_space
=
3745 -space_map_allocated(vd
->vdev_checkpoint_sm
);
3746 vd
->vdev_spa
->spa_checkpoint_info
.sci_dspace
+=
3747 vd
->vdev_stat
.vs_checkpoint_space
;
3748 } else if (error
!= 0) {
3749 vdev_dbgmsg(vd
, "vdev_load: failed to retrieve "
3750 "checkpoint space map object from vdev ZAP "
3751 "[error=%d]", error
);
3757 * If this is a leaf vdev, load its DTL.
3759 if (vd
->vdev_ops
->vdev_op_leaf
&& (error
= vdev_dtl_load(vd
)) != 0) {
3760 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3761 VDEV_AUX_CORRUPT_DATA
);
3762 vdev_dbgmsg(vd
, "vdev_load: vdev_dtl_load failed "
3763 "[error=%d]", error
);
3767 uint64_t obsolete_sm_object
;
3768 error
= vdev_obsolete_sm_object(vd
, &obsolete_sm_object
);
3769 if (error
== 0 && obsolete_sm_object
!= 0) {
3770 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
3771 ASSERT(vd
->vdev_asize
!= 0);
3772 ASSERT3P(vd
->vdev_obsolete_sm
, ==, NULL
);
3774 if ((error
= space_map_open(&vd
->vdev_obsolete_sm
, mos
,
3775 obsolete_sm_object
, 0, vd
->vdev_asize
, 0))) {
3776 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3777 VDEV_AUX_CORRUPT_DATA
);
3778 vdev_dbgmsg(vd
, "vdev_load: space_map_open failed for "
3779 "obsolete spacemap (obj %llu) [error=%d]",
3780 (u_longlong_t
)obsolete_sm_object
, error
);
3783 } else if (error
!= 0) {
3784 vdev_dbgmsg(vd
, "vdev_load: failed to retrieve obsolete "
3785 "space map object from vdev ZAP [error=%d]", error
);
3793 * The special vdev case is used for hot spares and l2cache devices. Its
3794 * sole purpose it to set the vdev state for the associated vdev. To do this,
3795 * we make sure that we can open the underlying device, then try to read the
3796 * label, and make sure that the label is sane and that it hasn't been
3797 * repurposed to another pool.
3800 vdev_validate_aux(vdev_t
*vd
)
3803 uint64_t guid
, version
;
3806 if (!vdev_readable(vd
))
3809 if ((label
= vdev_label_read_config(vd
, -1ULL)) == NULL
) {
3810 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
3811 VDEV_AUX_CORRUPT_DATA
);
3815 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_VERSION
, &version
) != 0 ||
3816 !SPA_VERSION_IS_SUPPORTED(version
) ||
3817 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0 ||
3818 guid
!= vd
->vdev_guid
||
3819 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
, &state
) != 0) {
3820 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
3821 VDEV_AUX_CORRUPT_DATA
);
3827 * We don't actually check the pool state here. If it's in fact in
3828 * use by another pool, we update this fact on the fly when requested.
3835 vdev_destroy_ms_flush_data(vdev_t
*vd
, dmu_tx_t
*tx
)
3837 objset_t
*mos
= spa_meta_objset(vd
->vdev_spa
);
3839 if (vd
->vdev_top_zap
== 0)
3842 uint64_t object
= 0;
3843 int err
= zap_lookup(mos
, vd
->vdev_top_zap
,
3844 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS
, sizeof (uint64_t), 1, &object
);
3849 VERIFY0(dmu_object_free(mos
, object
, tx
));
3850 VERIFY0(zap_remove(mos
, vd
->vdev_top_zap
,
3851 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS
, tx
));
3855 * Free the objects used to store this vdev's spacemaps, and the array
3856 * that points to them.
3859 vdev_destroy_spacemaps(vdev_t
*vd
, dmu_tx_t
*tx
)
3861 if (vd
->vdev_ms_array
== 0)
3864 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
3865 uint64_t array_count
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
3866 size_t array_bytes
= array_count
* sizeof (uint64_t);
3867 uint64_t *smobj_array
= kmem_alloc(array_bytes
, KM_SLEEP
);
3868 VERIFY0(dmu_read(mos
, vd
->vdev_ms_array
, 0,
3869 array_bytes
, smobj_array
, 0));
3871 for (uint64_t i
= 0; i
< array_count
; i
++) {
3872 uint64_t smobj
= smobj_array
[i
];
3876 space_map_free_obj(mos
, smobj
, tx
);
3879 kmem_free(smobj_array
, array_bytes
);
3880 VERIFY0(dmu_object_free(mos
, vd
->vdev_ms_array
, tx
));
3881 vdev_destroy_ms_flush_data(vd
, tx
);
3882 vd
->vdev_ms_array
= 0;
3886 vdev_remove_empty_log(vdev_t
*vd
, uint64_t txg
)
3888 spa_t
*spa
= vd
->vdev_spa
;
3890 ASSERT(vd
->vdev_islog
);
3891 ASSERT(vd
== vd
->vdev_top
);
3892 ASSERT3U(txg
, ==, spa_syncing_txg(spa
));
3894 dmu_tx_t
*tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
3896 vdev_destroy_spacemaps(vd
, tx
);
3897 if (vd
->vdev_top_zap
!= 0) {
3898 vdev_destroy_unlink_zap(vd
, vd
->vdev_top_zap
, tx
);
3899 vd
->vdev_top_zap
= 0;
3906 vdev_sync_done(vdev_t
*vd
, uint64_t txg
)
3909 boolean_t reassess
= !txg_list_empty(&vd
->vdev_ms_list
, TXG_CLEAN(txg
));
3911 ASSERT(vdev_is_concrete(vd
));
3913 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, TXG_CLEAN(txg
)))
3915 metaslab_sync_done(msp
, txg
);
3918 metaslab_sync_reassess(vd
->vdev_mg
);
3919 if (vd
->vdev_log_mg
!= NULL
)
3920 metaslab_sync_reassess(vd
->vdev_log_mg
);
3925 vdev_sync(vdev_t
*vd
, uint64_t txg
)
3927 spa_t
*spa
= vd
->vdev_spa
;
3931 ASSERT3U(txg
, ==, spa
->spa_syncing_txg
);
3932 dmu_tx_t
*tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
3933 if (range_tree_space(vd
->vdev_obsolete_segments
) > 0) {
3934 ASSERT(vd
->vdev_removing
||
3935 vd
->vdev_ops
== &vdev_indirect_ops
);
3937 vdev_indirect_sync_obsolete(vd
, tx
);
3940 * If the vdev is indirect, it can't have dirty
3941 * metaslabs or DTLs.
3943 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
3944 ASSERT(txg_list_empty(&vd
->vdev_ms_list
, txg
));
3945 ASSERT(txg_list_empty(&vd
->vdev_dtl_list
, txg
));
3951 ASSERT(vdev_is_concrete(vd
));
3953 if (vd
->vdev_ms_array
== 0 && vd
->vdev_ms_shift
!= 0 &&
3954 !vd
->vdev_removing
) {
3955 ASSERT(vd
== vd
->vdev_top
);
3956 ASSERT0(vd
->vdev_indirect_config
.vic_mapping_object
);
3957 vd
->vdev_ms_array
= dmu_object_alloc(spa
->spa_meta_objset
,
3958 DMU_OT_OBJECT_ARRAY
, 0, DMU_OT_NONE
, 0, tx
);
3959 ASSERT(vd
->vdev_ms_array
!= 0);
3960 vdev_config_dirty(vd
);
3963 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, txg
)) != NULL
) {
3964 metaslab_sync(msp
, txg
);
3965 (void) txg_list_add(&vd
->vdev_ms_list
, msp
, TXG_CLEAN(txg
));
3968 while ((lvd
= txg_list_remove(&vd
->vdev_dtl_list
, txg
)) != NULL
)
3969 vdev_dtl_sync(lvd
, txg
);
3972 * If this is an empty log device being removed, destroy the
3973 * metadata associated with it.
3975 if (vd
->vdev_islog
&& vd
->vdev_stat
.vs_alloc
== 0 && vd
->vdev_removing
)
3976 vdev_remove_empty_log(vd
, txg
);
3978 (void) txg_list_add(&spa
->spa_vdev_txg_list
, vd
, TXG_CLEAN(txg
));
3983 vdev_psize_to_asize(vdev_t
*vd
, uint64_t psize
)
3985 return (vd
->vdev_ops
->vdev_op_asize(vd
, psize
));
3989 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
3990 * not be opened, and no I/O is attempted.
3993 vdev_fault(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
3997 spa_vdev_state_enter(spa
, SCL_NONE
);
3999 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
4000 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
4002 if (!vd
->vdev_ops
->vdev_op_leaf
)
4003 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
4008 * If user did a 'zpool offline -f' then make the fault persist across
4011 if (aux
== VDEV_AUX_EXTERNAL_PERSIST
) {
4013 * There are two kinds of forced faults: temporary and
4014 * persistent. Temporary faults go away at pool import, while
4015 * persistent faults stay set. Both types of faults can be
4016 * cleared with a zpool clear.
4018 * We tell if a vdev is persistently faulted by looking at the
4019 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
4020 * import then it's a persistent fault. Otherwise, it's
4021 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
4022 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
4023 * tells vdev_config_generate() (which gets run later) to set
4024 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
4026 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
4027 vd
->vdev_tmpoffline
= B_FALSE
;
4028 aux
= VDEV_AUX_EXTERNAL
;
4030 vd
->vdev_tmpoffline
= B_TRUE
;
4034 * We don't directly use the aux state here, but if we do a
4035 * vdev_reopen(), we need this value to be present to remember why we
4038 vd
->vdev_label_aux
= aux
;
4041 * Faulted state takes precedence over degraded.
4043 vd
->vdev_delayed_close
= B_FALSE
;
4044 vd
->vdev_faulted
= 1ULL;
4045 vd
->vdev_degraded
= 0ULL;
4046 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_FAULTED
, aux
);
4049 * If this device has the only valid copy of the data, then
4050 * back off and simply mark the vdev as degraded instead.
4052 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&& vdev_dtl_required(vd
)) {
4053 vd
->vdev_degraded
= 1ULL;
4054 vd
->vdev_faulted
= 0ULL;
4057 * If we reopen the device and it's not dead, only then do we
4062 if (vdev_readable(vd
))
4063 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
, aux
);
4066 return (spa_vdev_state_exit(spa
, vd
, 0));
4070 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
4071 * user that something is wrong. The vdev continues to operate as normal as far
4072 * as I/O is concerned.
4075 vdev_degrade(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
4079 spa_vdev_state_enter(spa
, SCL_NONE
);
4081 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
4082 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
4084 if (!vd
->vdev_ops
->vdev_op_leaf
)
4085 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
4088 * If the vdev is already faulted, then don't do anything.
4090 if (vd
->vdev_faulted
|| vd
->vdev_degraded
)
4091 return (spa_vdev_state_exit(spa
, NULL
, 0));
4093 vd
->vdev_degraded
= 1ULL;
4094 if (!vdev_is_dead(vd
))
4095 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
,
4098 return (spa_vdev_state_exit(spa
, vd
, 0));
4102 vdev_remove_wanted(spa_t
*spa
, uint64_t guid
)
4106 spa_vdev_state_enter(spa
, SCL_NONE
);
4108 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
4109 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
4112 * If the vdev is already removed, or expanding which can trigger
4113 * repartition add/remove events, then don't do anything.
4115 if (vd
->vdev_removed
|| vd
->vdev_expanding
)
4116 return (spa_vdev_state_exit(spa
, NULL
, 0));
4119 * Confirm the vdev has been removed, otherwise don't do anything.
4121 if (vd
->vdev_ops
->vdev_op_leaf
&& !zio_wait(vdev_probe(vd
, NULL
)))
4122 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(EEXIST
)));
4124 vd
->vdev_remove_wanted
= B_TRUE
;
4125 spa_async_request(spa
, SPA_ASYNC_REMOVE
);
4127 return (spa_vdev_state_exit(spa
, vd
, 0));
4132 * Online the given vdev.
4134 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
4135 * spare device should be detached when the device finishes resilvering.
4136 * Second, the online should be treated like a 'test' online case, so no FMA
4137 * events are generated if the device fails to open.
4140 vdev_online(spa_t
*spa
, uint64_t guid
, uint64_t flags
, vdev_state_t
*newstate
)
4142 vdev_t
*vd
, *tvd
, *pvd
, *rvd
= spa
->spa_root_vdev
;
4143 boolean_t wasoffline
;
4144 vdev_state_t oldstate
;
4146 spa_vdev_state_enter(spa
, SCL_NONE
);
4148 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
4149 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
4151 if (!vd
->vdev_ops
->vdev_op_leaf
)
4152 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
4154 wasoffline
= (vd
->vdev_offline
|| vd
->vdev_tmpoffline
);
4155 oldstate
= vd
->vdev_state
;
4158 vd
->vdev_offline
= B_FALSE
;
4159 vd
->vdev_tmpoffline
= B_FALSE
;
4160 vd
->vdev_checkremove
= !!(flags
& ZFS_ONLINE_CHECKREMOVE
);
4161 vd
->vdev_forcefault
= !!(flags
& ZFS_ONLINE_FORCEFAULT
);
4163 /* XXX - L2ARC 1.0 does not support expansion */
4164 if (!vd
->vdev_aux
) {
4165 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
4166 pvd
->vdev_expanding
= !!((flags
& ZFS_ONLINE_EXPAND
) ||
4167 spa
->spa_autoexpand
);
4168 vd
->vdev_expansion_time
= gethrestime_sec();
4172 vd
->vdev_checkremove
= vd
->vdev_forcefault
= B_FALSE
;
4174 if (!vd
->vdev_aux
) {
4175 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
4176 pvd
->vdev_expanding
= B_FALSE
;
4180 *newstate
= vd
->vdev_state
;
4181 if ((flags
& ZFS_ONLINE_UNSPARE
) &&
4182 !vdev_is_dead(vd
) && vd
->vdev_parent
&&
4183 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
4184 vd
->vdev_parent
->vdev_child
[0] == vd
)
4185 vd
->vdev_unspare
= B_TRUE
;
4187 if ((flags
& ZFS_ONLINE_EXPAND
) || spa
->spa_autoexpand
) {
4189 /* XXX - L2ARC 1.0 does not support expansion */
4191 return (spa_vdev_state_exit(spa
, vd
, ENOTSUP
));
4192 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
4195 /* Restart initializing if necessary */
4196 mutex_enter(&vd
->vdev_initialize_lock
);
4197 if (vdev_writeable(vd
) &&
4198 vd
->vdev_initialize_thread
== NULL
&&
4199 vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
4200 (void) vdev_initialize(vd
);
4202 mutex_exit(&vd
->vdev_initialize_lock
);
4205 * Restart trimming if necessary. We do not restart trimming for cache
4206 * devices here. This is triggered by l2arc_rebuild_vdev()
4207 * asynchronously for the whole device or in l2arc_evict() as it evicts
4208 * space for upcoming writes.
4210 mutex_enter(&vd
->vdev_trim_lock
);
4211 if (vdev_writeable(vd
) && !vd
->vdev_isl2cache
&&
4212 vd
->vdev_trim_thread
== NULL
&&
4213 vd
->vdev_trim_state
== VDEV_TRIM_ACTIVE
) {
4214 (void) vdev_trim(vd
, vd
->vdev_trim_rate
, vd
->vdev_trim_partial
,
4215 vd
->vdev_trim_secure
);
4217 mutex_exit(&vd
->vdev_trim_lock
);
4220 (oldstate
< VDEV_STATE_DEGRADED
&&
4221 vd
->vdev_state
>= VDEV_STATE_DEGRADED
)) {
4222 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_ONLINE
);
4225 * Asynchronously detach spare vdev if resilver or
4226 * rebuild is not required
4228 if (vd
->vdev_unspare
&&
4229 !dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
4230 !dsl_scan_resilver_scheduled(spa
->spa_dsl_pool
) &&
4231 !vdev_rebuild_active(tvd
))
4232 spa_async_request(spa
, SPA_ASYNC_DETACH_SPARE
);
4234 return (spa_vdev_state_exit(spa
, vd
, 0));
4238 vdev_offline_locked(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
4242 uint64_t generation
;
4243 metaslab_group_t
*mg
;
4246 spa_vdev_state_enter(spa
, SCL_ALLOC
);
4248 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
4249 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
4251 if (!vd
->vdev_ops
->vdev_op_leaf
)
4252 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
4254 if (vd
->vdev_ops
== &vdev_draid_spare_ops
)
4255 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
4259 generation
= spa
->spa_config_generation
+ 1;
4262 * If the device isn't already offline, try to offline it.
4264 if (!vd
->vdev_offline
) {
4266 * If this device has the only valid copy of some data,
4267 * don't allow it to be offlined. Log devices are always
4270 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
4271 vdev_dtl_required(vd
))
4272 return (spa_vdev_state_exit(spa
, NULL
,
4276 * If the top-level is a slog and it has had allocations
4277 * then proceed. We check that the vdev's metaslab group
4278 * is not NULL since it's possible that we may have just
4279 * added this vdev but not yet initialized its metaslabs.
4281 if (tvd
->vdev_islog
&& mg
!= NULL
) {
4283 * Prevent any future allocations.
4285 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
4286 metaslab_group_passivate(mg
);
4287 (void) spa_vdev_state_exit(spa
, vd
, 0);
4289 error
= spa_reset_logs(spa
);
4292 * If the log device was successfully reset but has
4293 * checkpointed data, do not offline it.
4296 tvd
->vdev_checkpoint_sm
!= NULL
) {
4297 ASSERT3U(space_map_allocated(
4298 tvd
->vdev_checkpoint_sm
), !=, 0);
4299 error
= ZFS_ERR_CHECKPOINT_EXISTS
;
4302 spa_vdev_state_enter(spa
, SCL_ALLOC
);
4305 * Check to see if the config has changed.
4307 if (error
|| generation
!= spa
->spa_config_generation
) {
4308 metaslab_group_activate(mg
);
4310 return (spa_vdev_state_exit(spa
,
4312 (void) spa_vdev_state_exit(spa
, vd
, 0);
4315 ASSERT0(tvd
->vdev_stat
.vs_alloc
);
4319 * Offline this device and reopen its top-level vdev.
4320 * If the top-level vdev is a log device then just offline
4321 * it. Otherwise, if this action results in the top-level
4322 * vdev becoming unusable, undo it and fail the request.
4324 vd
->vdev_offline
= B_TRUE
;
4327 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
4328 vdev_is_dead(tvd
)) {
4329 vd
->vdev_offline
= B_FALSE
;
4331 return (spa_vdev_state_exit(spa
, NULL
,
4336 * Add the device back into the metaslab rotor so that
4337 * once we online the device it's open for business.
4339 if (tvd
->vdev_islog
&& mg
!= NULL
)
4340 metaslab_group_activate(mg
);
4343 vd
->vdev_tmpoffline
= !!(flags
& ZFS_OFFLINE_TEMPORARY
);
4345 return (spa_vdev_state_exit(spa
, vd
, 0));
4349 vdev_offline(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
4353 mutex_enter(&spa
->spa_vdev_top_lock
);
4354 error
= vdev_offline_locked(spa
, guid
, flags
);
4355 mutex_exit(&spa
->spa_vdev_top_lock
);
4361 * Clear the error counts associated with this vdev. Unlike vdev_online() and
4362 * vdev_offline(), we assume the spa config is locked. We also clear all
4363 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
4366 vdev_clear(spa_t
*spa
, vdev_t
*vd
)
4368 vdev_t
*rvd
= spa
->spa_root_vdev
;
4370 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
4375 vd
->vdev_stat
.vs_read_errors
= 0;
4376 vd
->vdev_stat
.vs_write_errors
= 0;
4377 vd
->vdev_stat
.vs_checksum_errors
= 0;
4378 vd
->vdev_stat
.vs_slow_ios
= 0;
4380 for (int c
= 0; c
< vd
->vdev_children
; c
++)
4381 vdev_clear(spa
, vd
->vdev_child
[c
]);
4384 * It makes no sense to "clear" an indirect or removed vdev.
4386 if (!vdev_is_concrete(vd
) || vd
->vdev_removed
)
4390 * If we're in the FAULTED state or have experienced failed I/O, then
4391 * clear the persistent state and attempt to reopen the device. We
4392 * also mark the vdev config dirty, so that the new faulted state is
4393 * written out to disk.
4395 if (vd
->vdev_faulted
|| vd
->vdev_degraded
||
4396 !vdev_readable(vd
) || !vdev_writeable(vd
)) {
4398 * When reopening in response to a clear event, it may be due to
4399 * a fmadm repair request. In this case, if the device is
4400 * still broken, we want to still post the ereport again.
4402 vd
->vdev_forcefault
= B_TRUE
;
4404 vd
->vdev_faulted
= vd
->vdev_degraded
= 0ULL;
4405 vd
->vdev_cant_read
= B_FALSE
;
4406 vd
->vdev_cant_write
= B_FALSE
;
4407 vd
->vdev_stat
.vs_aux
= 0;
4409 vdev_reopen(vd
== rvd
? rvd
: vd
->vdev_top
);
4411 vd
->vdev_forcefault
= B_FALSE
;
4413 if (vd
!= rvd
&& vdev_writeable(vd
->vdev_top
))
4414 vdev_state_dirty(vd
->vdev_top
);
4416 /* If a resilver isn't required, check if vdevs can be culled */
4417 if (vd
->vdev_aux
== NULL
&& !vdev_is_dead(vd
) &&
4418 !dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
4419 !dsl_scan_resilver_scheduled(spa
->spa_dsl_pool
))
4420 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
4422 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_CLEAR
);
4426 * When clearing a FMA-diagnosed fault, we always want to
4427 * unspare the device, as we assume that the original spare was
4428 * done in response to the FMA fault.
4430 if (!vdev_is_dead(vd
) && vd
->vdev_parent
!= NULL
&&
4431 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
4432 vd
->vdev_parent
->vdev_child
[0] == vd
)
4433 vd
->vdev_unspare
= B_TRUE
;
4435 /* Clear recent error events cache (i.e. duplicate events tracking) */
4436 zfs_ereport_clear(spa
, vd
);
4440 vdev_is_dead(vdev_t
*vd
)
4443 * Holes and missing devices are always considered "dead".
4444 * This simplifies the code since we don't have to check for
4445 * these types of devices in the various code paths.
4446 * Instead we rely on the fact that we skip over dead devices
4447 * before issuing I/O to them.
4449 return (vd
->vdev_state
< VDEV_STATE_DEGRADED
||
4450 vd
->vdev_ops
== &vdev_hole_ops
||
4451 vd
->vdev_ops
== &vdev_missing_ops
);
4455 vdev_readable(vdev_t
*vd
)
4457 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_read
);
4461 vdev_writeable(vdev_t
*vd
)
4463 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_write
&&
4464 vdev_is_concrete(vd
));
4468 vdev_allocatable(vdev_t
*vd
)
4470 uint64_t state
= vd
->vdev_state
;
4473 * We currently allow allocations from vdevs which may be in the
4474 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
4475 * fails to reopen then we'll catch it later when we're holding
4476 * the proper locks. Note that we have to get the vdev state
4477 * in a local variable because although it changes atomically,
4478 * we're asking two separate questions about it.
4480 return (!(state
< VDEV_STATE_DEGRADED
&& state
!= VDEV_STATE_CLOSED
) &&
4481 !vd
->vdev_cant_write
&& vdev_is_concrete(vd
) &&
4482 vd
->vdev_mg
->mg_initialized
);
4486 vdev_accessible(vdev_t
*vd
, zio_t
*zio
)
4488 ASSERT(zio
->io_vd
== vd
);
4490 if (vdev_is_dead(vd
) || vd
->vdev_remove_wanted
)
4493 if (zio
->io_type
== ZIO_TYPE_READ
)
4494 return (!vd
->vdev_cant_read
);
4496 if (zio
->io_type
== ZIO_TYPE_WRITE
)
4497 return (!vd
->vdev_cant_write
);
4503 vdev_get_child_stat(vdev_t
*cvd
, vdev_stat_t
*vs
, vdev_stat_t
*cvs
)
4506 * Exclude the dRAID spare when aggregating to avoid double counting
4507 * the ops and bytes. These IOs are counted by the physical leaves.
4509 if (cvd
->vdev_ops
== &vdev_draid_spare_ops
)
4512 for (int t
= 0; t
< VS_ZIO_TYPES
; t
++) {
4513 vs
->vs_ops
[t
] += cvs
->vs_ops
[t
];
4514 vs
->vs_bytes
[t
] += cvs
->vs_bytes
[t
];
4517 cvs
->vs_scan_removing
= cvd
->vdev_removing
;
4521 * Get extended stats
4524 vdev_get_child_stat_ex(vdev_t
*cvd
, vdev_stat_ex_t
*vsx
, vdev_stat_ex_t
*cvsx
)
4529 for (t
= 0; t
< ZIO_TYPES
; t
++) {
4530 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_disk_histo
[0]); b
++)
4531 vsx
->vsx_disk_histo
[t
][b
] += cvsx
->vsx_disk_histo
[t
][b
];
4533 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_total_histo
[0]); b
++) {
4534 vsx
->vsx_total_histo
[t
][b
] +=
4535 cvsx
->vsx_total_histo
[t
][b
];
4539 for (t
= 0; t
< ZIO_PRIORITY_NUM_QUEUEABLE
; t
++) {
4540 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_queue_histo
[0]); b
++) {
4541 vsx
->vsx_queue_histo
[t
][b
] +=
4542 cvsx
->vsx_queue_histo
[t
][b
];
4544 vsx
->vsx_active_queue
[t
] += cvsx
->vsx_active_queue
[t
];
4545 vsx
->vsx_pend_queue
[t
] += cvsx
->vsx_pend_queue
[t
];
4547 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_ind_histo
[0]); b
++)
4548 vsx
->vsx_ind_histo
[t
][b
] += cvsx
->vsx_ind_histo
[t
][b
];
4550 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_agg_histo
[0]); b
++)
4551 vsx
->vsx_agg_histo
[t
][b
] += cvsx
->vsx_agg_histo
[t
][b
];
4557 vdev_is_spacemap_addressable(vdev_t
*vd
)
4559 if (spa_feature_is_active(vd
->vdev_spa
, SPA_FEATURE_SPACEMAP_V2
))
4563 * If double-word space map entries are not enabled we assume
4564 * 47 bits of the space map entry are dedicated to the entry's
4565 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
4566 * to calculate the maximum address that can be described by a
4567 * space map entry for the given device.
4569 uint64_t shift
= vd
->vdev_ashift
+ SM_OFFSET_BITS
;
4571 if (shift
>= 63) /* detect potential overflow */
4574 return (vd
->vdev_asize
< (1ULL << shift
));
4578 * Get statistics for the given vdev.
4581 vdev_get_stats_ex_impl(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
4585 * If we're getting stats on the root vdev, aggregate the I/O counts
4586 * over all top-level vdevs (i.e. the direct children of the root).
4588 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4590 memset(vs
->vs_ops
, 0, sizeof (vs
->vs_ops
));
4591 memset(vs
->vs_bytes
, 0, sizeof (vs
->vs_bytes
));
4594 memset(vsx
, 0, sizeof (*vsx
));
4596 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4597 vdev_t
*cvd
= vd
->vdev_child
[c
];
4598 vdev_stat_t
*cvs
= &cvd
->vdev_stat
;
4599 vdev_stat_ex_t
*cvsx
= &cvd
->vdev_stat_ex
;
4601 vdev_get_stats_ex_impl(cvd
, cvs
, cvsx
);
4603 vdev_get_child_stat(cvd
, vs
, cvs
);
4605 vdev_get_child_stat_ex(cvd
, vsx
, cvsx
);
4609 * We're a leaf. Just copy our ZIO active queue stats in. The
4610 * other leaf stats are updated in vdev_stat_update().
4615 memcpy(vsx
, &vd
->vdev_stat_ex
, sizeof (vd
->vdev_stat_ex
));
4617 for (t
= 0; t
< ZIO_PRIORITY_NUM_QUEUEABLE
; t
++) {
4618 vsx
->vsx_active_queue
[t
] = vd
->vdev_queue
.vq_cactive
[t
];
4619 vsx
->vsx_pend_queue
[t
] = vdev_queue_class_length(vd
, t
);
4625 vdev_get_stats_ex(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
4627 vdev_t
*tvd
= vd
->vdev_top
;
4628 mutex_enter(&vd
->vdev_stat_lock
);
4630 memcpy(vs
, &vd
->vdev_stat
, sizeof (*vs
));
4631 vs
->vs_timestamp
= gethrtime() - vs
->vs_timestamp
;
4632 vs
->vs_state
= vd
->vdev_state
;
4633 vs
->vs_rsize
= vdev_get_min_asize(vd
);
4635 if (vd
->vdev_ops
->vdev_op_leaf
) {
4636 vs
->vs_pspace
= vd
->vdev_psize
;
4637 vs
->vs_rsize
+= VDEV_LABEL_START_SIZE
+
4638 VDEV_LABEL_END_SIZE
;
4640 * Report initializing progress. Since we don't
4641 * have the initializing locks held, this is only
4642 * an estimate (although a fairly accurate one).
4644 vs
->vs_initialize_bytes_done
=
4645 vd
->vdev_initialize_bytes_done
;
4646 vs
->vs_initialize_bytes_est
=
4647 vd
->vdev_initialize_bytes_est
;
4648 vs
->vs_initialize_state
= vd
->vdev_initialize_state
;
4649 vs
->vs_initialize_action_time
=
4650 vd
->vdev_initialize_action_time
;
4653 * Report manual TRIM progress. Since we don't have
4654 * the manual TRIM locks held, this is only an
4655 * estimate (although fairly accurate one).
4657 vs
->vs_trim_notsup
= !vd
->vdev_has_trim
;
4658 vs
->vs_trim_bytes_done
= vd
->vdev_trim_bytes_done
;
4659 vs
->vs_trim_bytes_est
= vd
->vdev_trim_bytes_est
;
4660 vs
->vs_trim_state
= vd
->vdev_trim_state
;
4661 vs
->vs_trim_action_time
= vd
->vdev_trim_action_time
;
4663 /* Set when there is a deferred resilver. */
4664 vs
->vs_resilver_deferred
= vd
->vdev_resilver_deferred
;
4668 * Report expandable space on top-level, non-auxiliary devices
4669 * only. The expandable space is reported in terms of metaslab
4670 * sized units since that determines how much space the pool
4673 if (vd
->vdev_aux
== NULL
&& tvd
!= NULL
) {
4674 vs
->vs_esize
= P2ALIGN(
4675 vd
->vdev_max_asize
- vd
->vdev_asize
,
4676 1ULL << tvd
->vdev_ms_shift
);
4679 vs
->vs_configured_ashift
= vd
->vdev_top
!= NULL
4680 ? vd
->vdev_top
->vdev_ashift
: vd
->vdev_ashift
;
4681 vs
->vs_logical_ashift
= vd
->vdev_logical_ashift
;
4682 if (vd
->vdev_physical_ashift
<= ASHIFT_MAX
)
4683 vs
->vs_physical_ashift
= vd
->vdev_physical_ashift
;
4685 vs
->vs_physical_ashift
= 0;
4688 * Report fragmentation and rebuild progress for top-level,
4689 * non-auxiliary, concrete devices.
4691 if (vd
->vdev_aux
== NULL
&& vd
== vd
->vdev_top
&&
4692 vdev_is_concrete(vd
)) {
4694 * The vdev fragmentation rating doesn't take into
4695 * account the embedded slog metaslab (vdev_log_mg).
4696 * Since it's only one metaslab, it would have a tiny
4697 * impact on the overall fragmentation.
4699 vs
->vs_fragmentation
= (vd
->vdev_mg
!= NULL
) ?
4700 vd
->vdev_mg
->mg_fragmentation
: 0;
4702 vs
->vs_noalloc
= MAX(vd
->vdev_noalloc
,
4703 tvd
? tvd
->vdev_noalloc
: 0);
4706 vdev_get_stats_ex_impl(vd
, vs
, vsx
);
4707 mutex_exit(&vd
->vdev_stat_lock
);
4711 vdev_get_stats(vdev_t
*vd
, vdev_stat_t
*vs
)
4713 return (vdev_get_stats_ex(vd
, vs
, NULL
));
4717 vdev_clear_stats(vdev_t
*vd
)
4719 mutex_enter(&vd
->vdev_stat_lock
);
4720 vd
->vdev_stat
.vs_space
= 0;
4721 vd
->vdev_stat
.vs_dspace
= 0;
4722 vd
->vdev_stat
.vs_alloc
= 0;
4723 mutex_exit(&vd
->vdev_stat_lock
);
4727 vdev_scan_stat_init(vdev_t
*vd
)
4729 vdev_stat_t
*vs
= &vd
->vdev_stat
;
4731 for (int c
= 0; c
< vd
->vdev_children
; c
++)
4732 vdev_scan_stat_init(vd
->vdev_child
[c
]);
4734 mutex_enter(&vd
->vdev_stat_lock
);
4735 vs
->vs_scan_processed
= 0;
4736 mutex_exit(&vd
->vdev_stat_lock
);
4740 vdev_stat_update(zio_t
*zio
, uint64_t psize
)
4742 spa_t
*spa
= zio
->io_spa
;
4743 vdev_t
*rvd
= spa
->spa_root_vdev
;
4744 vdev_t
*vd
= zio
->io_vd
? zio
->io_vd
: rvd
;
4746 uint64_t txg
= zio
->io_txg
;
4747 /* Suppress ASAN false positive */
4748 #ifdef __SANITIZE_ADDRESS__
4749 vdev_stat_t
*vs
= vd
? &vd
->vdev_stat
: NULL
;
4750 vdev_stat_ex_t
*vsx
= vd
? &vd
->vdev_stat_ex
: NULL
;
4752 vdev_stat_t
*vs
= &vd
->vdev_stat
;
4753 vdev_stat_ex_t
*vsx
= &vd
->vdev_stat_ex
;
4755 zio_type_t type
= zio
->io_type
;
4756 int flags
= zio
->io_flags
;
4759 * If this i/o is a gang leader, it didn't do any actual work.
4761 if (zio
->io_gang_tree
)
4764 if (zio
->io_error
== 0) {
4766 * If this is a root i/o, don't count it -- we've already
4767 * counted the top-level vdevs, and vdev_get_stats() will
4768 * aggregate them when asked. This reduces contention on
4769 * the root vdev_stat_lock and implicitly handles blocks
4770 * that compress away to holes, for which there is no i/o.
4771 * (Holes never create vdev children, so all the counters
4772 * remain zero, which is what we want.)
4774 * Note: this only applies to successful i/o (io_error == 0)
4775 * because unlike i/o counts, errors are not additive.
4776 * When reading a ditto block, for example, failure of
4777 * one top-level vdev does not imply a root-level error.
4782 ASSERT(vd
== zio
->io_vd
);
4784 if (flags
& ZIO_FLAG_IO_BYPASS
)
4787 mutex_enter(&vd
->vdev_stat_lock
);
4789 if (flags
& ZIO_FLAG_IO_REPAIR
) {
4791 * Repair is the result of a resilver issued by the
4792 * scan thread (spa_sync).
4794 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
4795 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
4796 dsl_scan_phys_t
*scn_phys
= &scn
->scn_phys
;
4797 uint64_t *processed
= &scn_phys
->scn_processed
;
4799 if (vd
->vdev_ops
->vdev_op_leaf
)
4800 atomic_add_64(processed
, psize
);
4801 vs
->vs_scan_processed
+= psize
;
4805 * Repair is the result of a rebuild issued by the
4806 * rebuild thread (vdev_rebuild_thread). To avoid
4807 * double counting repaired bytes the virtual dRAID
4808 * spare vdev is excluded from the processed bytes.
4810 if (zio
->io_priority
== ZIO_PRIORITY_REBUILD
) {
4811 vdev_t
*tvd
= vd
->vdev_top
;
4812 vdev_rebuild_t
*vr
= &tvd
->vdev_rebuild_config
;
4813 vdev_rebuild_phys_t
*vrp
= &vr
->vr_rebuild_phys
;
4814 uint64_t *rebuilt
= &vrp
->vrp_bytes_rebuilt
;
4816 if (vd
->vdev_ops
->vdev_op_leaf
&&
4817 vd
->vdev_ops
!= &vdev_draid_spare_ops
) {
4818 atomic_add_64(rebuilt
, psize
);
4820 vs
->vs_rebuild_processed
+= psize
;
4823 if (flags
& ZIO_FLAG_SELF_HEAL
)
4824 vs
->vs_self_healed
+= psize
;
4828 * The bytes/ops/histograms are recorded at the leaf level and
4829 * aggregated into the higher level vdevs in vdev_get_stats().
4831 if (vd
->vdev_ops
->vdev_op_leaf
&&
4832 (zio
->io_priority
< ZIO_PRIORITY_NUM_QUEUEABLE
)) {
4833 zio_type_t vs_type
= type
;
4834 zio_priority_t priority
= zio
->io_priority
;
4837 * TRIM ops and bytes are reported to user space as
4838 * ZIO_TYPE_IOCTL. This is done to preserve the
4839 * vdev_stat_t structure layout for user space.
4841 if (type
== ZIO_TYPE_TRIM
)
4842 vs_type
= ZIO_TYPE_IOCTL
;
4845 * Solely for the purposes of 'zpool iostat -lqrw'
4846 * reporting use the priority to categorize the IO.
4847 * Only the following are reported to user space:
4849 * ZIO_PRIORITY_SYNC_READ,
4850 * ZIO_PRIORITY_SYNC_WRITE,
4851 * ZIO_PRIORITY_ASYNC_READ,
4852 * ZIO_PRIORITY_ASYNC_WRITE,
4853 * ZIO_PRIORITY_SCRUB,
4854 * ZIO_PRIORITY_TRIM,
4855 * ZIO_PRIORITY_REBUILD.
4857 if (priority
== ZIO_PRIORITY_INITIALIZING
) {
4858 ASSERT3U(type
, ==, ZIO_TYPE_WRITE
);
4859 priority
= ZIO_PRIORITY_ASYNC_WRITE
;
4860 } else if (priority
== ZIO_PRIORITY_REMOVAL
) {
4861 priority
= ((type
== ZIO_TYPE_WRITE
) ?
4862 ZIO_PRIORITY_ASYNC_WRITE
:
4863 ZIO_PRIORITY_ASYNC_READ
);
4866 vs
->vs_ops
[vs_type
]++;
4867 vs
->vs_bytes
[vs_type
] += psize
;
4869 if (flags
& ZIO_FLAG_DELEGATED
) {
4870 vsx
->vsx_agg_histo
[priority
]
4871 [RQ_HISTO(zio
->io_size
)]++;
4873 vsx
->vsx_ind_histo
[priority
]
4874 [RQ_HISTO(zio
->io_size
)]++;
4877 if (zio
->io_delta
&& zio
->io_delay
) {
4878 vsx
->vsx_queue_histo
[priority
]
4879 [L_HISTO(zio
->io_delta
- zio
->io_delay
)]++;
4880 vsx
->vsx_disk_histo
[type
]
4881 [L_HISTO(zio
->io_delay
)]++;
4882 vsx
->vsx_total_histo
[type
]
4883 [L_HISTO(zio
->io_delta
)]++;
4887 mutex_exit(&vd
->vdev_stat_lock
);
4891 if (flags
& ZIO_FLAG_SPECULATIVE
)
4895 * If this is an I/O error that is going to be retried, then ignore the
4896 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
4897 * hard errors, when in reality they can happen for any number of
4898 * innocuous reasons (bus resets, MPxIO link failure, etc).
4900 if (zio
->io_error
== EIO
&&
4901 !(zio
->io_flags
& ZIO_FLAG_IO_RETRY
))
4905 * Intent logs writes won't propagate their error to the root
4906 * I/O so don't mark these types of failures as pool-level
4909 if (zio
->io_vd
== NULL
&& (zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
4912 if (type
== ZIO_TYPE_WRITE
&& txg
!= 0 &&
4913 (!(flags
& ZIO_FLAG_IO_REPAIR
) ||
4914 (flags
& ZIO_FLAG_SCAN_THREAD
) ||
4915 spa
->spa_claiming
)) {
4917 * This is either a normal write (not a repair), or it's
4918 * a repair induced by the scrub thread, or it's a repair
4919 * made by zil_claim() during spa_load() in the first txg.
4920 * In the normal case, we commit the DTL change in the same
4921 * txg as the block was born. In the scrub-induced repair
4922 * case, we know that scrubs run in first-pass syncing context,
4923 * so we commit the DTL change in spa_syncing_txg(spa).
4924 * In the zil_claim() case, we commit in spa_first_txg(spa).
4926 * We currently do not make DTL entries for failed spontaneous
4927 * self-healing writes triggered by normal (non-scrubbing)
4928 * reads, because we have no transactional context in which to
4929 * do so -- and it's not clear that it'd be desirable anyway.
4931 if (vd
->vdev_ops
->vdev_op_leaf
) {
4932 uint64_t commit_txg
= txg
;
4933 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
4934 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
4935 ASSERT(spa_sync_pass(spa
) == 1);
4936 vdev_dtl_dirty(vd
, DTL_SCRUB
, txg
, 1);
4937 commit_txg
= spa_syncing_txg(spa
);
4938 } else if (spa
->spa_claiming
) {
4939 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
4940 commit_txg
= spa_first_txg(spa
);
4942 ASSERT(commit_txg
>= spa_syncing_txg(spa
));
4943 if (vdev_dtl_contains(vd
, DTL_MISSING
, txg
, 1))
4945 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
4946 vdev_dtl_dirty(pvd
, DTL_PARTIAL
, txg
, 1);
4947 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, commit_txg
);
4950 vdev_dtl_dirty(vd
, DTL_MISSING
, txg
, 1);
4955 vdev_deflated_space(vdev_t
*vd
, int64_t space
)
4957 ASSERT((space
& (SPA_MINBLOCKSIZE
-1)) == 0);
4958 ASSERT(vd
->vdev_deflate_ratio
!= 0 || vd
->vdev_isl2cache
);
4960 return ((space
>> SPA_MINBLOCKSHIFT
) * vd
->vdev_deflate_ratio
);
4964 * Update the in-core space usage stats for this vdev, its metaslab class,
4965 * and the root vdev.
4968 vdev_space_update(vdev_t
*vd
, int64_t alloc_delta
, int64_t defer_delta
,
4969 int64_t space_delta
)
4972 int64_t dspace_delta
;
4973 spa_t
*spa
= vd
->vdev_spa
;
4974 vdev_t
*rvd
= spa
->spa_root_vdev
;
4976 ASSERT(vd
== vd
->vdev_top
);
4979 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
4980 * factor. We must calculate this here and not at the root vdev
4981 * because the root vdev's psize-to-asize is simply the max of its
4982 * children's, thus not accurate enough for us.
4984 dspace_delta
= vdev_deflated_space(vd
, space_delta
);
4986 mutex_enter(&vd
->vdev_stat_lock
);
4987 /* ensure we won't underflow */
4988 if (alloc_delta
< 0) {
4989 ASSERT3U(vd
->vdev_stat
.vs_alloc
, >=, -alloc_delta
);
4992 vd
->vdev_stat
.vs_alloc
+= alloc_delta
;
4993 vd
->vdev_stat
.vs_space
+= space_delta
;
4994 vd
->vdev_stat
.vs_dspace
+= dspace_delta
;
4995 mutex_exit(&vd
->vdev_stat_lock
);
4997 /* every class but log contributes to root space stats */
4998 if (vd
->vdev_mg
!= NULL
&& !vd
->vdev_islog
) {
4999 ASSERT(!vd
->vdev_isl2cache
);
5000 mutex_enter(&rvd
->vdev_stat_lock
);
5001 rvd
->vdev_stat
.vs_alloc
+= alloc_delta
;
5002 rvd
->vdev_stat
.vs_space
+= space_delta
;
5003 rvd
->vdev_stat
.vs_dspace
+= dspace_delta
;
5004 mutex_exit(&rvd
->vdev_stat_lock
);
5006 /* Note: metaslab_class_space_update moved to metaslab_space_update */
5010 * Mark a top-level vdev's config as dirty, placing it on the dirty list
5011 * so that it will be written out next time the vdev configuration is synced.
5012 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
5015 vdev_config_dirty(vdev_t
*vd
)
5017 spa_t
*spa
= vd
->vdev_spa
;
5018 vdev_t
*rvd
= spa
->spa_root_vdev
;
5021 ASSERT(spa_writeable(spa
));
5024 * If this is an aux vdev (as with l2cache and spare devices), then we
5025 * update the vdev config manually and set the sync flag.
5027 if (vd
->vdev_aux
!= NULL
) {
5028 spa_aux_vdev_t
*sav
= vd
->vdev_aux
;
5032 for (c
= 0; c
< sav
->sav_count
; c
++) {
5033 if (sav
->sav_vdevs
[c
] == vd
)
5037 if (c
== sav
->sav_count
) {
5039 * We're being removed. There's nothing more to do.
5041 ASSERT(sav
->sav_sync
== B_TRUE
);
5045 sav
->sav_sync
= B_TRUE
;
5047 if (nvlist_lookup_nvlist_array(sav
->sav_config
,
5048 ZPOOL_CONFIG_L2CACHE
, &aux
, &naux
) != 0) {
5049 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
5050 ZPOOL_CONFIG_SPARES
, &aux
, &naux
) == 0);
5056 * Setting the nvlist in the middle if the array is a little
5057 * sketchy, but it will work.
5059 nvlist_free(aux
[c
]);
5060 aux
[c
] = vdev_config_generate(spa
, vd
, B_TRUE
, 0);
5066 * The dirty list is protected by the SCL_CONFIG lock. The caller
5067 * must either hold SCL_CONFIG as writer, or must be the sync thread
5068 * (which holds SCL_CONFIG as reader). There's only one sync thread,
5069 * so this is sufficient to ensure mutual exclusion.
5071 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
5072 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
5073 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
5076 for (c
= 0; c
< rvd
->vdev_children
; c
++)
5077 vdev_config_dirty(rvd
->vdev_child
[c
]);
5079 ASSERT(vd
== vd
->vdev_top
);
5081 if (!list_link_active(&vd
->vdev_config_dirty_node
) &&
5082 vdev_is_concrete(vd
)) {
5083 list_insert_head(&spa
->spa_config_dirty_list
, vd
);
5089 vdev_config_clean(vdev_t
*vd
)
5091 spa_t
*spa
= vd
->vdev_spa
;
5093 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
5094 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
5095 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
5097 ASSERT(list_link_active(&vd
->vdev_config_dirty_node
));
5098 list_remove(&spa
->spa_config_dirty_list
, vd
);
5102 * Mark a top-level vdev's state as dirty, so that the next pass of
5103 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
5104 * the state changes from larger config changes because they require
5105 * much less locking, and are often needed for administrative actions.
5108 vdev_state_dirty(vdev_t
*vd
)
5110 spa_t
*spa
= vd
->vdev_spa
;
5112 ASSERT(spa_writeable(spa
));
5113 ASSERT(vd
== vd
->vdev_top
);
5116 * The state list is protected by the SCL_STATE lock. The caller
5117 * must either hold SCL_STATE as writer, or must be the sync thread
5118 * (which holds SCL_STATE as reader). There's only one sync thread,
5119 * so this is sufficient to ensure mutual exclusion.
5121 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
5122 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
5123 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
5125 if (!list_link_active(&vd
->vdev_state_dirty_node
) &&
5126 vdev_is_concrete(vd
))
5127 list_insert_head(&spa
->spa_state_dirty_list
, vd
);
5131 vdev_state_clean(vdev_t
*vd
)
5133 spa_t
*spa
= vd
->vdev_spa
;
5135 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
5136 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
5137 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
5139 ASSERT(list_link_active(&vd
->vdev_state_dirty_node
));
5140 list_remove(&spa
->spa_state_dirty_list
, vd
);
5144 * Propagate vdev state up from children to parent.
5147 vdev_propagate_state(vdev_t
*vd
)
5149 spa_t
*spa
= vd
->vdev_spa
;
5150 vdev_t
*rvd
= spa
->spa_root_vdev
;
5151 int degraded
= 0, faulted
= 0;
5155 if (vd
->vdev_children
> 0) {
5156 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5157 child
= vd
->vdev_child
[c
];
5160 * Don't factor holes or indirect vdevs into the
5163 if (!vdev_is_concrete(child
))
5166 if (!vdev_readable(child
) ||
5167 (!vdev_writeable(child
) && spa_writeable(spa
))) {
5169 * Root special: if there is a top-level log
5170 * device, treat the root vdev as if it were
5173 if (child
->vdev_islog
&& vd
== rvd
)
5177 } else if (child
->vdev_state
<= VDEV_STATE_DEGRADED
) {
5181 if (child
->vdev_stat
.vs_aux
== VDEV_AUX_CORRUPT_DATA
)
5185 vd
->vdev_ops
->vdev_op_state_change(vd
, faulted
, degraded
);
5188 * Root special: if there is a top-level vdev that cannot be
5189 * opened due to corrupted metadata, then propagate the root
5190 * vdev's aux state as 'corrupt' rather than 'insufficient
5193 if (corrupted
&& vd
== rvd
&&
5194 rvd
->vdev_state
== VDEV_STATE_CANT_OPEN
)
5195 vdev_set_state(rvd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
5196 VDEV_AUX_CORRUPT_DATA
);
5199 if (vd
->vdev_parent
)
5200 vdev_propagate_state(vd
->vdev_parent
);
5204 * Set a vdev's state. If this is during an open, we don't update the parent
5205 * state, because we're in the process of opening children depth-first.
5206 * Otherwise, we propagate the change to the parent.
5208 * If this routine places a device in a faulted state, an appropriate ereport is
5212 vdev_set_state(vdev_t
*vd
, boolean_t isopen
, vdev_state_t state
, vdev_aux_t aux
)
5214 uint64_t save_state
;
5215 spa_t
*spa
= vd
->vdev_spa
;
5217 if (state
== vd
->vdev_state
) {
5219 * Since vdev_offline() code path is already in an offline
5220 * state we can miss a statechange event to OFFLINE. Check
5221 * the previous state to catch this condition.
5223 if (vd
->vdev_ops
->vdev_op_leaf
&&
5224 (state
== VDEV_STATE_OFFLINE
) &&
5225 (vd
->vdev_prevstate
>= VDEV_STATE_FAULTED
)) {
5226 /* post an offline state change */
5227 zfs_post_state_change(spa
, vd
, vd
->vdev_prevstate
);
5229 vd
->vdev_stat
.vs_aux
= aux
;
5233 save_state
= vd
->vdev_state
;
5235 vd
->vdev_state
= state
;
5236 vd
->vdev_stat
.vs_aux
= aux
;
5239 * If we are setting the vdev state to anything but an open state, then
5240 * always close the underlying device unless the device has requested
5241 * a delayed close (i.e. we're about to remove or fault the device).
5242 * Otherwise, we keep accessible but invalid devices open forever.
5243 * We don't call vdev_close() itself, because that implies some extra
5244 * checks (offline, etc) that we don't want here. This is limited to
5245 * leaf devices, because otherwise closing the device will affect other
5248 if (!vd
->vdev_delayed_close
&& vdev_is_dead(vd
) &&
5249 vd
->vdev_ops
->vdev_op_leaf
)
5250 vd
->vdev_ops
->vdev_op_close(vd
);
5252 if (vd
->vdev_removed
&&
5253 state
== VDEV_STATE_CANT_OPEN
&&
5254 (aux
== VDEV_AUX_OPEN_FAILED
|| vd
->vdev_checkremove
)) {
5256 * If the previous state is set to VDEV_STATE_REMOVED, then this
5257 * device was previously marked removed and someone attempted to
5258 * reopen it. If this failed due to a nonexistent device, then
5259 * keep the device in the REMOVED state. We also let this be if
5260 * it is one of our special test online cases, which is only
5261 * attempting to online the device and shouldn't generate an FMA
5264 vd
->vdev_state
= VDEV_STATE_REMOVED
;
5265 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
5266 } else if (state
== VDEV_STATE_REMOVED
) {
5267 vd
->vdev_removed
= B_TRUE
;
5268 } else if (state
== VDEV_STATE_CANT_OPEN
) {
5270 * If we fail to open a vdev during an import or recovery, we
5271 * mark it as "not available", which signifies that it was
5272 * never there to begin with. Failure to open such a device
5273 * is not considered an error.
5275 if ((spa_load_state(spa
) == SPA_LOAD_IMPORT
||
5276 spa_load_state(spa
) == SPA_LOAD_RECOVER
) &&
5277 vd
->vdev_ops
->vdev_op_leaf
)
5278 vd
->vdev_not_present
= 1;
5281 * Post the appropriate ereport. If the 'prevstate' field is
5282 * set to something other than VDEV_STATE_UNKNOWN, it indicates
5283 * that this is part of a vdev_reopen(). In this case, we don't
5284 * want to post the ereport if the device was already in the
5285 * CANT_OPEN state beforehand.
5287 * If the 'checkremove' flag is set, then this is an attempt to
5288 * online the device in response to an insertion event. If we
5289 * hit this case, then we have detected an insertion event for a
5290 * faulted or offline device that wasn't in the removed state.
5291 * In this scenario, we don't post an ereport because we are
5292 * about to replace the device, or attempt an online with
5293 * vdev_forcefault, which will generate the fault for us.
5295 if ((vd
->vdev_prevstate
!= state
|| vd
->vdev_forcefault
) &&
5296 !vd
->vdev_not_present
&& !vd
->vdev_checkremove
&&
5297 vd
!= spa
->spa_root_vdev
) {
5301 case VDEV_AUX_OPEN_FAILED
:
5302 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED
;
5304 case VDEV_AUX_CORRUPT_DATA
:
5305 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA
;
5307 case VDEV_AUX_NO_REPLICAS
:
5308 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS
;
5310 case VDEV_AUX_BAD_GUID_SUM
:
5311 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM
;
5313 case VDEV_AUX_TOO_SMALL
:
5314 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL
;
5316 case VDEV_AUX_BAD_LABEL
:
5317 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL
;
5319 case VDEV_AUX_BAD_ASHIFT
:
5320 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
;
5323 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN
;
5326 (void) zfs_ereport_post(class, spa
, vd
, NULL
, NULL
,
5330 /* Erase any notion of persistent removed state */
5331 vd
->vdev_removed
= B_FALSE
;
5333 vd
->vdev_removed
= B_FALSE
;
5337 * Notify ZED of any significant state-change on a leaf vdev.
5340 if (vd
->vdev_ops
->vdev_op_leaf
) {
5341 /* preserve original state from a vdev_reopen() */
5342 if ((vd
->vdev_prevstate
!= VDEV_STATE_UNKNOWN
) &&
5343 (vd
->vdev_prevstate
!= vd
->vdev_state
) &&
5344 (save_state
<= VDEV_STATE_CLOSED
))
5345 save_state
= vd
->vdev_prevstate
;
5347 /* filter out state change due to initial vdev_open */
5348 if (save_state
> VDEV_STATE_CLOSED
)
5349 zfs_post_state_change(spa
, vd
, save_state
);
5352 if (!isopen
&& vd
->vdev_parent
)
5353 vdev_propagate_state(vd
->vdev_parent
);
5357 vdev_children_are_offline(vdev_t
*vd
)
5359 ASSERT(!vd
->vdev_ops
->vdev_op_leaf
);
5361 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
5362 if (vd
->vdev_child
[i
]->vdev_state
!= VDEV_STATE_OFFLINE
)
5370 * Check the vdev configuration to ensure that it's capable of supporting
5371 * a root pool. We do not support partial configuration.
5374 vdev_is_bootable(vdev_t
*vd
)
5376 if (!vd
->vdev_ops
->vdev_op_leaf
) {
5377 const char *vdev_type
= vd
->vdev_ops
->vdev_op_type
;
5379 if (strcmp(vdev_type
, VDEV_TYPE_MISSING
) == 0)
5383 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5384 if (!vdev_is_bootable(vd
->vdev_child
[c
]))
5391 vdev_is_concrete(vdev_t
*vd
)
5393 vdev_ops_t
*ops
= vd
->vdev_ops
;
5394 if (ops
== &vdev_indirect_ops
|| ops
== &vdev_hole_ops
||
5395 ops
== &vdev_missing_ops
|| ops
== &vdev_root_ops
) {
5403 * Determine if a log device has valid content. If the vdev was
5404 * removed or faulted in the MOS config then we know that
5405 * the content on the log device has already been written to the pool.
5408 vdev_log_state_valid(vdev_t
*vd
)
5410 if (vd
->vdev_ops
->vdev_op_leaf
&& !vd
->vdev_faulted
&&
5414 for (int c
= 0; c
< vd
->vdev_children
; c
++)
5415 if (vdev_log_state_valid(vd
->vdev_child
[c
]))
5422 * Expand a vdev if possible.
5425 vdev_expand(vdev_t
*vd
, uint64_t txg
)
5427 ASSERT(vd
->vdev_top
== vd
);
5428 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5429 ASSERT(vdev_is_concrete(vd
));
5431 vdev_set_deflate_ratio(vd
);
5433 if ((vd
->vdev_asize
>> vd
->vdev_ms_shift
) > vd
->vdev_ms_count
&&
5434 vdev_is_concrete(vd
)) {
5435 vdev_metaslab_group_create(vd
);
5436 VERIFY(vdev_metaslab_init(vd
, txg
) == 0);
5437 vdev_config_dirty(vd
);
5445 vdev_split(vdev_t
*vd
)
5447 vdev_t
*cvd
, *pvd
= vd
->vdev_parent
;
5449 VERIFY3U(pvd
->vdev_children
, >, 1);
5451 vdev_remove_child(pvd
, vd
);
5452 vdev_compact_children(pvd
);
5454 ASSERT3P(pvd
->vdev_child
, !=, NULL
);
5456 cvd
= pvd
->vdev_child
[0];
5457 if (pvd
->vdev_children
== 1) {
5458 vdev_remove_parent(cvd
);
5459 cvd
->vdev_splitting
= B_TRUE
;
5461 vdev_propagate_state(cvd
);
5465 vdev_deadman(vdev_t
*vd
, const char *tag
)
5467 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5468 vdev_t
*cvd
= vd
->vdev_child
[c
];
5470 vdev_deadman(cvd
, tag
);
5473 if (vd
->vdev_ops
->vdev_op_leaf
) {
5474 vdev_queue_t
*vq
= &vd
->vdev_queue
;
5476 mutex_enter(&vq
->vq_lock
);
5477 if (vq
->vq_active
> 0) {
5478 spa_t
*spa
= vd
->vdev_spa
;
5482 zfs_dbgmsg("slow vdev: %s has %u active IOs",
5483 vd
->vdev_path
, vq
->vq_active
);
5486 * Look at the head of all the pending queues,
5487 * if any I/O has been outstanding for longer than
5488 * the spa_deadman_synctime invoke the deadman logic.
5490 fio
= list_head(&vq
->vq_active_list
);
5491 delta
= gethrtime() - fio
->io_timestamp
;
5492 if (delta
> spa_deadman_synctime(spa
))
5493 zio_deadman(fio
, tag
);
5495 mutex_exit(&vq
->vq_lock
);
5500 vdev_defer_resilver(vdev_t
*vd
)
5502 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
5504 vd
->vdev_resilver_deferred
= B_TRUE
;
5505 vd
->vdev_spa
->spa_resilver_deferred
= B_TRUE
;
5509 * Clears the resilver deferred flag on all leaf devs under vd. Returns
5510 * B_TRUE if we have devices that need to be resilvered and are available to
5511 * accept resilver I/Os.
5514 vdev_clear_resilver_deferred(vdev_t
*vd
, dmu_tx_t
*tx
)
5516 boolean_t resilver_needed
= B_FALSE
;
5517 spa_t
*spa
= vd
->vdev_spa
;
5519 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5520 vdev_t
*cvd
= vd
->vdev_child
[c
];
5521 resilver_needed
|= vdev_clear_resilver_deferred(cvd
, tx
);
5524 if (vd
== spa
->spa_root_vdev
&&
5525 spa_feature_is_active(spa
, SPA_FEATURE_RESILVER_DEFER
)) {
5526 spa_feature_decr(spa
, SPA_FEATURE_RESILVER_DEFER
, tx
);
5527 vdev_config_dirty(vd
);
5528 spa
->spa_resilver_deferred
= B_FALSE
;
5529 return (resilver_needed
);
5532 if (!vdev_is_concrete(vd
) || vd
->vdev_aux
||
5533 !vd
->vdev_ops
->vdev_op_leaf
)
5534 return (resilver_needed
);
5536 vd
->vdev_resilver_deferred
= B_FALSE
;
5538 return (!vdev_is_dead(vd
) && !vd
->vdev_offline
&&
5539 vdev_resilver_needed(vd
, NULL
, NULL
));
5543 vdev_xlate_is_empty(range_seg64_t
*rs
)
5545 return (rs
->rs_start
== rs
->rs_end
);
5549 * Translate a logical range to the first contiguous physical range for the
5550 * specified vdev_t. This function is initially called with a leaf vdev and
5551 * will walk each parent vdev until it reaches a top-level vdev. Once the
5552 * top-level is reached the physical range is initialized and the recursive
5553 * function begins to unwind. As it unwinds it calls the parent's vdev
5554 * specific translation function to do the real conversion.
5557 vdev_xlate(vdev_t
*vd
, const range_seg64_t
*logical_rs
,
5558 range_seg64_t
*physical_rs
, range_seg64_t
*remain_rs
)
5561 * Walk up the vdev tree
5563 if (vd
!= vd
->vdev_top
) {
5564 vdev_xlate(vd
->vdev_parent
, logical_rs
, physical_rs
,
5568 * We've reached the top-level vdev, initialize the physical
5569 * range to the logical range and set an empty remaining
5570 * range then start to unwind.
5572 physical_rs
->rs_start
= logical_rs
->rs_start
;
5573 physical_rs
->rs_end
= logical_rs
->rs_end
;
5575 remain_rs
->rs_start
= logical_rs
->rs_start
;
5576 remain_rs
->rs_end
= logical_rs
->rs_start
;
5581 vdev_t
*pvd
= vd
->vdev_parent
;
5582 ASSERT3P(pvd
, !=, NULL
);
5583 ASSERT3P(pvd
->vdev_ops
->vdev_op_xlate
, !=, NULL
);
5586 * As this recursive function unwinds, translate the logical
5587 * range into its physical and any remaining components by calling
5588 * the vdev specific translate function.
5590 range_seg64_t intermediate
= { 0 };
5591 pvd
->vdev_ops
->vdev_op_xlate(vd
, physical_rs
, &intermediate
, remain_rs
);
5593 physical_rs
->rs_start
= intermediate
.rs_start
;
5594 physical_rs
->rs_end
= intermediate
.rs_end
;
5598 vdev_xlate_walk(vdev_t
*vd
, const range_seg64_t
*logical_rs
,
5599 vdev_xlate_func_t
*func
, void *arg
)
5601 range_seg64_t iter_rs
= *logical_rs
;
5602 range_seg64_t physical_rs
;
5603 range_seg64_t remain_rs
;
5605 while (!vdev_xlate_is_empty(&iter_rs
)) {
5607 vdev_xlate(vd
, &iter_rs
, &physical_rs
, &remain_rs
);
5610 * With raidz and dRAID, it's possible that the logical range
5611 * does not live on this leaf vdev. Only when there is a non-
5612 * zero physical size call the provided function.
5614 if (!vdev_xlate_is_empty(&physical_rs
))
5615 func(arg
, &physical_rs
);
5617 iter_rs
= remain_rs
;
5622 vdev_name(vdev_t
*vd
, char *buf
, int buflen
)
5624 if (vd
->vdev_path
== NULL
) {
5625 if (strcmp(vd
->vdev_ops
->vdev_op_type
, "root") == 0) {
5626 strlcpy(buf
, vd
->vdev_spa
->spa_name
, buflen
);
5627 } else if (!vd
->vdev_ops
->vdev_op_leaf
) {
5628 snprintf(buf
, buflen
, "%s-%llu",
5629 vd
->vdev_ops
->vdev_op_type
,
5630 (u_longlong_t
)vd
->vdev_id
);
5633 strlcpy(buf
, vd
->vdev_path
, buflen
);
5639 * Look at the vdev tree and determine whether any devices are currently being
5643 vdev_replace_in_progress(vdev_t
*vdev
)
5645 ASSERT(spa_config_held(vdev
->vdev_spa
, SCL_ALL
, RW_READER
) != 0);
5647 if (vdev
->vdev_ops
== &vdev_replacing_ops
)
5651 * A 'spare' vdev indicates that we have a replace in progress, unless
5652 * it has exactly two children, and the second, the hot spare, has
5653 * finished being resilvered.
5655 if (vdev
->vdev_ops
== &vdev_spare_ops
&& (vdev
->vdev_children
> 2 ||
5656 !vdev_dtl_empty(vdev
->vdev_child
[1], DTL_MISSING
)))
5659 for (int i
= 0; i
< vdev
->vdev_children
; i
++) {
5660 if (vdev_replace_in_progress(vdev
->vdev_child
[i
]))
5668 * Add a (source=src, propname=propval) list to an nvlist.
5671 vdev_prop_add_list(nvlist_t
*nvl
, const char *propname
, const char *strval
,
5672 uint64_t intval
, zprop_source_t src
)
5676 propval
= fnvlist_alloc();
5677 fnvlist_add_uint64(propval
, ZPROP_SOURCE
, src
);
5680 fnvlist_add_string(propval
, ZPROP_VALUE
, strval
);
5682 fnvlist_add_uint64(propval
, ZPROP_VALUE
, intval
);
5684 fnvlist_add_nvlist(nvl
, propname
, propval
);
5685 nvlist_free(propval
);
5689 vdev_props_set_sync(void *arg
, dmu_tx_t
*tx
)
5692 nvlist_t
*nvp
= arg
;
5693 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
5694 objset_t
*mos
= spa
->spa_meta_objset
;
5695 nvpair_t
*elem
= NULL
;
5699 vdev_guid
= fnvlist_lookup_uint64(nvp
, ZPOOL_VDEV_PROPS_SET_VDEV
);
5700 nvprops
= fnvlist_lookup_nvlist(nvp
, ZPOOL_VDEV_PROPS_SET_PROPS
);
5701 vd
= spa_lookup_by_guid(spa
, vdev_guid
, B_TRUE
);
5703 /* this vdev could get removed while waiting for this sync task */
5707 mutex_enter(&spa
->spa_props_lock
);
5709 while ((elem
= nvlist_next_nvpair(nvprops
, elem
)) != NULL
) {
5710 uint64_t intval
, objid
= 0;
5713 const char *propname
= nvpair_name(elem
);
5714 zprop_type_t proptype
;
5717 * Set vdev property values in the vdev props mos object.
5719 if (vd
->vdev_root_zap
!= 0) {
5720 objid
= vd
->vdev_root_zap
;
5721 } else if (vd
->vdev_top_zap
!= 0) {
5722 objid
= vd
->vdev_top_zap
;
5723 } else if (vd
->vdev_leaf_zap
!= 0) {
5724 objid
= vd
->vdev_leaf_zap
;
5727 * XXX: implement vdev_props_set_check()
5729 panic("vdev not root/top/leaf");
5732 switch (prop
= vdev_name_to_prop(propname
)) {
5733 case VDEV_PROP_USERPROP
:
5734 if (vdev_prop_user(propname
)) {
5735 strval
= fnvpair_value_string(elem
);
5736 if (strlen(strval
) == 0) {
5737 /* remove the property if value == "" */
5738 (void) zap_remove(mos
, objid
, propname
,
5741 VERIFY0(zap_update(mos
, objid
, propname
,
5742 1, strlen(strval
) + 1, strval
, tx
));
5744 spa_history_log_internal(spa
, "vdev set", tx
,
5745 "vdev_guid=%llu: %s=%s",
5746 (u_longlong_t
)vdev_guid
, nvpair_name(elem
),
5751 /* normalize the property name */
5752 propname
= vdev_prop_to_name(prop
);
5753 proptype
= vdev_prop_get_type(prop
);
5755 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
5756 ASSERT(proptype
== PROP_TYPE_STRING
);
5757 strval
= fnvpair_value_string(elem
);
5758 VERIFY0(zap_update(mos
, objid
, propname
,
5759 1, strlen(strval
) + 1, strval
, tx
));
5760 spa_history_log_internal(spa
, "vdev set", tx
,
5761 "vdev_guid=%llu: %s=%s",
5762 (u_longlong_t
)vdev_guid
, nvpair_name(elem
),
5764 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
5765 intval
= fnvpair_value_uint64(elem
);
5767 if (proptype
== PROP_TYPE_INDEX
) {
5769 VERIFY0(vdev_prop_index_to_string(
5770 prop
, intval
, &unused
));
5772 VERIFY0(zap_update(mos
, objid
, propname
,
5773 sizeof (uint64_t), 1, &intval
, tx
));
5774 spa_history_log_internal(spa
, "vdev set", tx
,
5775 "vdev_guid=%llu: %s=%lld",
5776 (u_longlong_t
)vdev_guid
,
5777 nvpair_name(elem
), (longlong_t
)intval
);
5779 panic("invalid vdev property type %u",
5786 mutex_exit(&spa
->spa_props_lock
);
5790 vdev_prop_set(vdev_t
*vd
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5792 spa_t
*spa
= vd
->vdev_spa
;
5793 nvpair_t
*elem
= NULL
;
5800 if (nvlist_lookup_uint64(innvl
, ZPOOL_VDEV_PROPS_SET_VDEV
,
5802 return (SET_ERROR(EINVAL
));
5804 if (nvlist_lookup_nvlist(innvl
, ZPOOL_VDEV_PROPS_SET_PROPS
,
5806 return (SET_ERROR(EINVAL
));
5808 if ((vd
= spa_lookup_by_guid(spa
, vdev_guid
, B_TRUE
)) == NULL
)
5809 return (SET_ERROR(EINVAL
));
5811 while ((elem
= nvlist_next_nvpair(nvprops
, elem
)) != NULL
) {
5812 const char *propname
= nvpair_name(elem
);
5813 vdev_prop_t prop
= vdev_name_to_prop(propname
);
5814 uint64_t intval
= 0;
5815 const char *strval
= NULL
;
5817 if (prop
== VDEV_PROP_USERPROP
&& !vdev_prop_user(propname
)) {
5822 if (vdev_prop_readonly(prop
)) {
5827 /* Special Processing */
5829 case VDEV_PROP_PATH
:
5830 if (vd
->vdev_path
== NULL
) {
5834 if (nvpair_value_string(elem
, &strval
) != 0) {
5838 /* New path must start with /dev/ */
5839 if (strncmp(strval
, "/dev/", 5)) {
5843 error
= spa_vdev_setpath(spa
, vdev_guid
, strval
);
5845 case VDEV_PROP_ALLOCATING
:
5846 if (nvpair_value_uint64(elem
, &intval
) != 0) {
5850 if (intval
!= vd
->vdev_noalloc
)
5853 error
= spa_vdev_noalloc(spa
, vdev_guid
);
5855 error
= spa_vdev_alloc(spa
, vdev_guid
);
5857 case VDEV_PROP_FAILFAST
:
5858 if (nvpair_value_uint64(elem
, &intval
) != 0) {
5862 vd
->vdev_failfast
= intval
& 1;
5864 case VDEV_PROP_CHECKSUM_N
:
5865 if (nvpair_value_uint64(elem
, &intval
) != 0) {
5869 vd
->vdev_checksum_n
= intval
;
5871 case VDEV_PROP_CHECKSUM_T
:
5872 if (nvpair_value_uint64(elem
, &intval
) != 0) {
5876 vd
->vdev_checksum_t
= intval
;
5878 case VDEV_PROP_IO_N
:
5879 if (nvpair_value_uint64(elem
, &intval
) != 0) {
5883 vd
->vdev_io_n
= intval
;
5885 case VDEV_PROP_IO_T
:
5886 if (nvpair_value_uint64(elem
, &intval
) != 0) {
5890 vd
->vdev_io_t
= intval
;
5893 /* Most processing is done in vdev_props_set_sync */
5899 vdev_prop_add_list(outnvl
, propname
, strval
, intval
, 0);
5904 return (dsl_sync_task(spa
->spa_name
, NULL
, vdev_props_set_sync
,
5905 innvl
, 6, ZFS_SPACE_CHECK_EXTRA_RESERVED
));
5909 vdev_prop_get(vdev_t
*vd
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5911 spa_t
*spa
= vd
->vdev_spa
;
5912 objset_t
*mos
= spa
->spa_meta_objset
;
5916 nvpair_t
*elem
= NULL
;
5917 nvlist_t
*nvprops
= NULL
;
5918 uint64_t intval
= 0;
5919 char *strval
= NULL
;
5920 const char *propname
= NULL
;
5924 ASSERT(mos
!= NULL
);
5926 if (nvlist_lookup_uint64(innvl
, ZPOOL_VDEV_PROPS_GET_VDEV
,
5928 return (SET_ERROR(EINVAL
));
5930 nvlist_lookup_nvlist(innvl
, ZPOOL_VDEV_PROPS_GET_PROPS
, &nvprops
);
5932 if (vd
->vdev_root_zap
!= 0) {
5933 objid
= vd
->vdev_root_zap
;
5934 } else if (vd
->vdev_top_zap
!= 0) {
5935 objid
= vd
->vdev_top_zap
;
5936 } else if (vd
->vdev_leaf_zap
!= 0) {
5937 objid
= vd
->vdev_leaf_zap
;
5939 return (SET_ERROR(EINVAL
));
5943 mutex_enter(&spa
->spa_props_lock
);
5945 if (nvprops
!= NULL
) {
5946 char namebuf
[64] = { 0 };
5948 while ((elem
= nvlist_next_nvpair(nvprops
, elem
)) != NULL
) {
5951 propname
= nvpair_name(elem
);
5952 prop
= vdev_name_to_prop(propname
);
5953 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
5954 uint64_t integer_size
, num_integers
;
5957 /* Special Read-only Properties */
5958 case VDEV_PROP_NAME
:
5959 strval
= vdev_name(vd
, namebuf
,
5963 vdev_prop_add_list(outnvl
, propname
, strval
, 0,
5966 case VDEV_PROP_CAPACITY
:
5968 intval
= (vd
->vdev_stat
.vs_dspace
== 0) ? 0 :
5969 (vd
->vdev_stat
.vs_alloc
* 100 /
5970 vd
->vdev_stat
.vs_dspace
);
5971 vdev_prop_add_list(outnvl
, propname
, NULL
,
5972 intval
, ZPROP_SRC_NONE
);
5974 case VDEV_PROP_STATE
:
5975 vdev_prop_add_list(outnvl
, propname
, NULL
,
5976 vd
->vdev_state
, ZPROP_SRC_NONE
);
5978 case VDEV_PROP_GUID
:
5979 vdev_prop_add_list(outnvl
, propname
, NULL
,
5980 vd
->vdev_guid
, ZPROP_SRC_NONE
);
5982 case VDEV_PROP_ASIZE
:
5983 vdev_prop_add_list(outnvl
, propname
, NULL
,
5984 vd
->vdev_asize
, ZPROP_SRC_NONE
);
5986 case VDEV_PROP_PSIZE
:
5987 vdev_prop_add_list(outnvl
, propname
, NULL
,
5988 vd
->vdev_psize
, ZPROP_SRC_NONE
);
5990 case VDEV_PROP_ASHIFT
:
5991 vdev_prop_add_list(outnvl
, propname
, NULL
,
5992 vd
->vdev_ashift
, ZPROP_SRC_NONE
);
5994 case VDEV_PROP_SIZE
:
5995 vdev_prop_add_list(outnvl
, propname
, NULL
,
5996 vd
->vdev_stat
.vs_dspace
, ZPROP_SRC_NONE
);
5998 case VDEV_PROP_FREE
:
5999 vdev_prop_add_list(outnvl
, propname
, NULL
,
6000 vd
->vdev_stat
.vs_dspace
-
6001 vd
->vdev_stat
.vs_alloc
, ZPROP_SRC_NONE
);
6003 case VDEV_PROP_ALLOCATED
:
6004 vdev_prop_add_list(outnvl
, propname
, NULL
,
6005 vd
->vdev_stat
.vs_alloc
, ZPROP_SRC_NONE
);
6007 case VDEV_PROP_EXPANDSZ
:
6008 vdev_prop_add_list(outnvl
, propname
, NULL
,
6009 vd
->vdev_stat
.vs_esize
, ZPROP_SRC_NONE
);
6011 case VDEV_PROP_FRAGMENTATION
:
6012 vdev_prop_add_list(outnvl
, propname
, NULL
,
6013 vd
->vdev_stat
.vs_fragmentation
,
6016 case VDEV_PROP_PARITY
:
6017 vdev_prop_add_list(outnvl
, propname
, NULL
,
6018 vdev_get_nparity(vd
), ZPROP_SRC_NONE
);
6020 case VDEV_PROP_PATH
:
6021 if (vd
->vdev_path
== NULL
)
6023 vdev_prop_add_list(outnvl
, propname
,
6024 vd
->vdev_path
, 0, ZPROP_SRC_NONE
);
6026 case VDEV_PROP_DEVID
:
6027 if (vd
->vdev_devid
== NULL
)
6029 vdev_prop_add_list(outnvl
, propname
,
6030 vd
->vdev_devid
, 0, ZPROP_SRC_NONE
);
6032 case VDEV_PROP_PHYS_PATH
:
6033 if (vd
->vdev_physpath
== NULL
)
6035 vdev_prop_add_list(outnvl
, propname
,
6036 vd
->vdev_physpath
, 0, ZPROP_SRC_NONE
);
6038 case VDEV_PROP_ENC_PATH
:
6039 if (vd
->vdev_enc_sysfs_path
== NULL
)
6041 vdev_prop_add_list(outnvl
, propname
,
6042 vd
->vdev_enc_sysfs_path
, 0, ZPROP_SRC_NONE
);
6045 if (vd
->vdev_fru
== NULL
)
6047 vdev_prop_add_list(outnvl
, propname
,
6048 vd
->vdev_fru
, 0, ZPROP_SRC_NONE
);
6050 case VDEV_PROP_PARENT
:
6051 if (vd
->vdev_parent
!= NULL
) {
6052 strval
= vdev_name(vd
->vdev_parent
,
6053 namebuf
, sizeof (namebuf
));
6054 vdev_prop_add_list(outnvl
, propname
,
6055 strval
, 0, ZPROP_SRC_NONE
);
6058 case VDEV_PROP_CHILDREN
:
6059 if (vd
->vdev_children
> 0)
6060 strval
= kmem_zalloc(ZAP_MAXVALUELEN
,
6062 for (uint64_t i
= 0; i
< vd
->vdev_children
;
6066 vname
= vdev_name(vd
->vdev_child
[i
],
6067 namebuf
, sizeof (namebuf
));
6069 vname
= "(unknown)";
6070 if (strlen(strval
) > 0)
6071 strlcat(strval
, ",",
6073 strlcat(strval
, vname
, ZAP_MAXVALUELEN
);
6075 if (strval
!= NULL
) {
6076 vdev_prop_add_list(outnvl
, propname
,
6077 strval
, 0, ZPROP_SRC_NONE
);
6078 kmem_free(strval
, ZAP_MAXVALUELEN
);
6081 case VDEV_PROP_NUMCHILDREN
:
6082 vdev_prop_add_list(outnvl
, propname
, NULL
,
6083 vd
->vdev_children
, ZPROP_SRC_NONE
);
6085 case VDEV_PROP_READ_ERRORS
:
6086 vdev_prop_add_list(outnvl
, propname
, NULL
,
6087 vd
->vdev_stat
.vs_read_errors
,
6090 case VDEV_PROP_WRITE_ERRORS
:
6091 vdev_prop_add_list(outnvl
, propname
, NULL
,
6092 vd
->vdev_stat
.vs_write_errors
,
6095 case VDEV_PROP_CHECKSUM_ERRORS
:
6096 vdev_prop_add_list(outnvl
, propname
, NULL
,
6097 vd
->vdev_stat
.vs_checksum_errors
,
6100 case VDEV_PROP_INITIALIZE_ERRORS
:
6101 vdev_prop_add_list(outnvl
, propname
, NULL
,
6102 vd
->vdev_stat
.vs_initialize_errors
,
6105 case VDEV_PROP_OPS_NULL
:
6106 vdev_prop_add_list(outnvl
, propname
, NULL
,
6107 vd
->vdev_stat
.vs_ops
[ZIO_TYPE_NULL
],
6110 case VDEV_PROP_OPS_READ
:
6111 vdev_prop_add_list(outnvl
, propname
, NULL
,
6112 vd
->vdev_stat
.vs_ops
[ZIO_TYPE_READ
],
6115 case VDEV_PROP_OPS_WRITE
:
6116 vdev_prop_add_list(outnvl
, propname
, NULL
,
6117 vd
->vdev_stat
.vs_ops
[ZIO_TYPE_WRITE
],
6120 case VDEV_PROP_OPS_FREE
:
6121 vdev_prop_add_list(outnvl
, propname
, NULL
,
6122 vd
->vdev_stat
.vs_ops
[ZIO_TYPE_FREE
],
6125 case VDEV_PROP_OPS_CLAIM
:
6126 vdev_prop_add_list(outnvl
, propname
, NULL
,
6127 vd
->vdev_stat
.vs_ops
[ZIO_TYPE_CLAIM
],
6130 case VDEV_PROP_OPS_TRIM
:
6132 * TRIM ops and bytes are reported to user
6133 * space as ZIO_TYPE_IOCTL. This is done to
6134 * preserve the vdev_stat_t structure layout
6137 vdev_prop_add_list(outnvl
, propname
, NULL
,
6138 vd
->vdev_stat
.vs_ops
[ZIO_TYPE_IOCTL
],
6141 case VDEV_PROP_BYTES_NULL
:
6142 vdev_prop_add_list(outnvl
, propname
, NULL
,
6143 vd
->vdev_stat
.vs_bytes
[ZIO_TYPE_NULL
],
6146 case VDEV_PROP_BYTES_READ
:
6147 vdev_prop_add_list(outnvl
, propname
, NULL
,
6148 vd
->vdev_stat
.vs_bytes
[ZIO_TYPE_READ
],
6151 case VDEV_PROP_BYTES_WRITE
:
6152 vdev_prop_add_list(outnvl
, propname
, NULL
,
6153 vd
->vdev_stat
.vs_bytes
[ZIO_TYPE_WRITE
],
6156 case VDEV_PROP_BYTES_FREE
:
6157 vdev_prop_add_list(outnvl
, propname
, NULL
,
6158 vd
->vdev_stat
.vs_bytes
[ZIO_TYPE_FREE
],
6161 case VDEV_PROP_BYTES_CLAIM
:
6162 vdev_prop_add_list(outnvl
, propname
, NULL
,
6163 vd
->vdev_stat
.vs_bytes
[ZIO_TYPE_CLAIM
],
6166 case VDEV_PROP_BYTES_TRIM
:
6168 * TRIM ops and bytes are reported to user
6169 * space as ZIO_TYPE_IOCTL. This is done to
6170 * preserve the vdev_stat_t structure layout
6173 vdev_prop_add_list(outnvl
, propname
, NULL
,
6174 vd
->vdev_stat
.vs_bytes
[ZIO_TYPE_IOCTL
],
6177 case VDEV_PROP_REMOVING
:
6178 vdev_prop_add_list(outnvl
, propname
, NULL
,
6179 vd
->vdev_removing
, ZPROP_SRC_NONE
);
6181 /* Numeric Properites */
6182 case VDEV_PROP_ALLOCATING
:
6183 /* Leaf vdevs cannot have this property */
6184 if (vd
->vdev_mg
== NULL
&&
6185 vd
->vdev_top
!= NULL
) {
6186 src
= ZPROP_SRC_NONE
;
6187 intval
= ZPROP_BOOLEAN_NA
;
6189 err
= vdev_prop_get_int(vd
, prop
,
6191 if (err
&& err
!= ENOENT
)
6195 vdev_prop_default_numeric(prop
))
6196 src
= ZPROP_SRC_DEFAULT
;
6198 src
= ZPROP_SRC_LOCAL
;
6201 vdev_prop_add_list(outnvl
, propname
, NULL
,
6204 case VDEV_PROP_FAILFAST
:
6205 src
= ZPROP_SRC_LOCAL
;
6208 err
= zap_lookup(mos
, objid
, nvpair_name(elem
),
6209 sizeof (uint64_t), 1, &intval
);
6210 if (err
== ENOENT
) {
6211 intval
= vdev_prop_default_numeric(
6217 if (intval
== vdev_prop_default_numeric(prop
))
6218 src
= ZPROP_SRC_DEFAULT
;
6220 vdev_prop_add_list(outnvl
, propname
, strval
,
6223 case VDEV_PROP_CHECKSUM_N
:
6224 case VDEV_PROP_CHECKSUM_T
:
6225 case VDEV_PROP_IO_N
:
6226 case VDEV_PROP_IO_T
:
6227 err
= vdev_prop_get_int(vd
, prop
, &intval
);
6228 if (err
&& err
!= ENOENT
)
6231 if (intval
== vdev_prop_default_numeric(prop
))
6232 src
= ZPROP_SRC_DEFAULT
;
6234 src
= ZPROP_SRC_LOCAL
;
6236 vdev_prop_add_list(outnvl
, propname
, NULL
,
6239 /* Text Properties */
6240 case VDEV_PROP_COMMENT
:
6241 /* Exists in the ZAP below */
6243 case VDEV_PROP_USERPROP
:
6244 /* User Properites */
6245 src
= ZPROP_SRC_LOCAL
;
6247 err
= zap_length(mos
, objid
, nvpair_name(elem
),
6248 &integer_size
, &num_integers
);
6252 switch (integer_size
) {
6254 /* User properties cannot be integers */
6258 /* string property */
6259 strval
= kmem_alloc(num_integers
,
6261 err
= zap_lookup(mos
, objid
,
6262 nvpair_name(elem
), 1,
6263 num_integers
, strval
);
6269 vdev_prop_add_list(outnvl
, propname
,
6271 kmem_free(strval
, num_integers
);
6284 * Get all properties from the MOS vdev property object.
6288 for (zap_cursor_init(&zc
, mos
, objid
);
6289 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
6290 zap_cursor_advance(&zc
)) {
6293 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
6294 propname
= za
.za_name
;
6296 switch (za
.za_integer_length
) {
6298 /* We do not allow integer user properties */
6299 /* This is likely an internal value */
6302 /* string property */
6303 strval
= kmem_alloc(za
.za_num_integers
,
6305 err
= zap_lookup(mos
, objid
, za
.za_name
, 1,
6306 za
.za_num_integers
, strval
);
6308 kmem_free(strval
, za
.za_num_integers
);
6311 vdev_prop_add_list(outnvl
, propname
, strval
, 0,
6313 kmem_free(strval
, za
.za_num_integers
);
6320 zap_cursor_fini(&zc
);
6323 mutex_exit(&spa
->spa_props_lock
);
6324 if (err
&& err
!= ENOENT
) {
6331 EXPORT_SYMBOL(vdev_fault
);
6332 EXPORT_SYMBOL(vdev_degrade
);
6333 EXPORT_SYMBOL(vdev_online
);
6334 EXPORT_SYMBOL(vdev_offline
);
6335 EXPORT_SYMBOL(vdev_clear
);
6337 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, default_ms_count
, UINT
, ZMOD_RW
,
6338 "Target number of metaslabs per top-level vdev");
6340 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, default_ms_shift
, UINT
, ZMOD_RW
,
6341 "Default lower limit for metaslab size");
6343 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, max_ms_shift
, UINT
, ZMOD_RW
,
6344 "Default upper limit for metaslab size");
6346 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, min_ms_count
, UINT
, ZMOD_RW
,
6347 "Minimum number of metaslabs per top-level vdev");
6349 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, ms_count_limit
, UINT
, ZMOD_RW
,
6350 "Practical upper limit of total metaslabs per top-level vdev");
6352 ZFS_MODULE_PARAM(zfs
, zfs_
, slow_io_events_per_second
, UINT
, ZMOD_RW
,
6353 "Rate limit slow IO (delay) events to this many per second");
6356 ZFS_MODULE_PARAM(zfs
, zfs_
, checksum_events_per_second
, UINT
, ZMOD_RW
,
6357 "Rate limit checksum events to this many checksum errors per second "
6358 "(do not set below ZED threshold).");
6361 ZFS_MODULE_PARAM(zfs
, zfs_
, scan_ignore_errors
, INT
, ZMOD_RW
,
6362 "Ignore errors during resilver/scrub");
6364 ZFS_MODULE_PARAM(zfs_vdev
, vdev_
, validate_skip
, INT
, ZMOD_RW
,
6365 "Bypass vdev_validate()");
6367 ZFS_MODULE_PARAM(zfs
, zfs_
, nocacheflush
, INT
, ZMOD_RW
,
6368 "Disable cache flushes");
6370 ZFS_MODULE_PARAM(zfs
, zfs_
, embedded_slog_min_ms
, UINT
, ZMOD_RW
,
6371 "Minimum number of metaslabs required to dedicate one for log blocks");
6374 ZFS_MODULE_PARAM_CALL(zfs_vdev
, zfs_vdev_
, min_auto_ashift
,
6375 param_set_min_auto_ashift
, param_get_uint
, ZMOD_RW
,
6376 "Minimum ashift used when creating new top-level vdevs");
6378 ZFS_MODULE_PARAM_CALL(zfs_vdev
, zfs_vdev_
, max_auto_ashift
,
6379 param_set_max_auto_ashift
, param_get_uint
, ZMOD_RW
,
6380 "Maximum ashift used when optimizing for logical -> physical sector "
6381 "size on new top-level vdevs");