4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25 * Copyright 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.
33 #include <sys/zfs_context.h>
34 #include <sys/fm/fs/zfs.h>
36 #include <sys/spa_impl.h>
37 #include <sys/bpobj.h>
39 #include <sys/dmu_tx.h>
40 #include <sys/dsl_dir.h>
41 #include <sys/vdev_impl.h>
42 #include <sys/vdev_rebuild.h>
43 #include <sys/vdev_draid.h>
44 #include <sys/uberblock_impl.h>
45 #include <sys/metaslab.h>
46 #include <sys/metaslab_impl.h>
47 #include <sys/space_map.h>
48 #include <sys/space_reftree.h>
51 #include <sys/fs/zfs.h>
54 #include <sys/dsl_scan.h>
55 #include <sys/vdev_raidz.h>
57 #include <sys/vdev_initialize.h>
58 #include <sys/vdev_trim.h>
60 #include <sys/zfs_ratelimit.h>
62 /* default target for number of metaslabs per top-level vdev */
63 int zfs_vdev_default_ms_count
= 200;
65 /* minimum number of metaslabs per top-level vdev */
66 int zfs_vdev_min_ms_count
= 16;
68 /* practical upper limit of total metaslabs per top-level vdev */
69 int zfs_vdev_ms_count_limit
= 1ULL << 17;
71 /* lower limit for metaslab size (512M) */
72 int zfs_vdev_default_ms_shift
= 29;
74 /* upper limit for metaslab size (16G) */
75 int zfs_vdev_max_ms_shift
= 34;
77 int vdev_validate_skip
= B_FALSE
;
80 * Since the DTL space map of a vdev is not expected to have a lot of
81 * entries, we default its block size to 4K.
83 int zfs_vdev_dtl_sm_blksz
= (1 << 12);
86 * Rate limit slow IO (delay) events to this many per second.
88 unsigned int zfs_slow_io_events_per_second
= 20;
91 * Rate limit checksum events after this many checksum errors per second.
93 unsigned int zfs_checksum_events_per_second
= 20;
96 * Ignore errors during scrub/resilver. Allows to work around resilver
97 * upon import when there are pool errors.
99 int zfs_scan_ignore_errors
= 0;
102 * vdev-wide space maps that have lots of entries written to them at
103 * the end of each transaction can benefit from a higher I/O bandwidth
104 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
106 int zfs_vdev_standard_sm_blksz
= (1 << 17);
109 * Tunable parameter for debugging or performance analysis. Setting this
110 * will cause pool corruption on power loss if a volatile out-of-order
111 * write cache is enabled.
113 int zfs_nocacheflush
= 0;
115 uint64_t zfs_vdev_max_auto_ashift
= ASHIFT_MAX
;
116 uint64_t zfs_vdev_min_auto_ashift
= ASHIFT_MIN
;
120 vdev_dbgmsg(vdev_t
*vd
, const char *fmt
, ...)
126 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
129 if (vd
->vdev_path
!= NULL
) {
130 zfs_dbgmsg("%s vdev '%s': %s", vd
->vdev_ops
->vdev_op_type
,
133 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
134 vd
->vdev_ops
->vdev_op_type
,
135 (u_longlong_t
)vd
->vdev_id
,
136 (u_longlong_t
)vd
->vdev_guid
, buf
);
141 vdev_dbgmsg_print_tree(vdev_t
*vd
, int indent
)
145 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
) {
146 zfs_dbgmsg("%*svdev %u: %s", indent
, "", vd
->vdev_id
,
147 vd
->vdev_ops
->vdev_op_type
);
151 switch (vd
->vdev_state
) {
152 case VDEV_STATE_UNKNOWN
:
153 (void) snprintf(state
, sizeof (state
), "unknown");
155 case VDEV_STATE_CLOSED
:
156 (void) snprintf(state
, sizeof (state
), "closed");
158 case VDEV_STATE_OFFLINE
:
159 (void) snprintf(state
, sizeof (state
), "offline");
161 case VDEV_STATE_REMOVED
:
162 (void) snprintf(state
, sizeof (state
), "removed");
164 case VDEV_STATE_CANT_OPEN
:
165 (void) snprintf(state
, sizeof (state
), "can't open");
167 case VDEV_STATE_FAULTED
:
168 (void) snprintf(state
, sizeof (state
), "faulted");
170 case VDEV_STATE_DEGRADED
:
171 (void) snprintf(state
, sizeof (state
), "degraded");
173 case VDEV_STATE_HEALTHY
:
174 (void) snprintf(state
, sizeof (state
), "healthy");
177 (void) snprintf(state
, sizeof (state
), "<state %u>",
178 (uint_t
)vd
->vdev_state
);
181 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent
,
182 "", (int)vd
->vdev_id
, vd
->vdev_ops
->vdev_op_type
,
183 vd
->vdev_islog
? " (log)" : "",
184 (u_longlong_t
)vd
->vdev_guid
,
185 vd
->vdev_path
? vd
->vdev_path
: "N/A", state
);
187 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++)
188 vdev_dbgmsg_print_tree(vd
->vdev_child
[i
], indent
+ 2);
192 * Virtual device management.
195 static vdev_ops_t
*vdev_ops_table
[] = {
199 &vdev_draid_spare_ops
,
212 * Given a vdev type, return the appropriate ops vector.
215 vdev_getops(const char *type
)
217 vdev_ops_t
*ops
, **opspp
;
219 for (opspp
= vdev_ops_table
; (ops
= *opspp
) != NULL
; opspp
++)
220 if (strcmp(ops
->vdev_op_type
, type
) == 0)
228 vdev_default_xlate(vdev_t
*vd
, const range_seg64_t
*logical_rs
,
229 range_seg64_t
*physical_rs
, range_seg64_t
*remain_rs
)
231 physical_rs
->rs_start
= logical_rs
->rs_start
;
232 physical_rs
->rs_end
= logical_rs
->rs_end
;
236 * Derive the enumerated allocation bias from string input.
237 * String origin is either the per-vdev zap or zpool(8).
239 static vdev_alloc_bias_t
240 vdev_derive_alloc_bias(const char *bias
)
242 vdev_alloc_bias_t alloc_bias
= VDEV_BIAS_NONE
;
244 if (strcmp(bias
, VDEV_ALLOC_BIAS_LOG
) == 0)
245 alloc_bias
= VDEV_BIAS_LOG
;
246 else if (strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
247 alloc_bias
= VDEV_BIAS_SPECIAL
;
248 else if (strcmp(bias
, VDEV_ALLOC_BIAS_DEDUP
) == 0)
249 alloc_bias
= VDEV_BIAS_DEDUP
;
255 * Default asize function: return the MAX of psize with the asize of
256 * all children. This is what's used by anything other than RAID-Z.
259 vdev_default_asize(vdev_t
*vd
, uint64_t psize
)
261 uint64_t asize
= P2ROUNDUP(psize
, 1ULL << vd
->vdev_top
->vdev_ashift
);
264 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
265 csize
= vdev_psize_to_asize(vd
->vdev_child
[c
], psize
);
266 asize
= MAX(asize
, csize
);
273 vdev_default_min_asize(vdev_t
*vd
)
275 return (vd
->vdev_min_asize
);
279 * Get the minimum allocatable size. We define the allocatable size as
280 * the vdev's asize rounded to the nearest metaslab. This allows us to
281 * replace or attach devices which don't have the same physical size but
282 * can still satisfy the same number of allocations.
285 vdev_get_min_asize(vdev_t
*vd
)
287 vdev_t
*pvd
= vd
->vdev_parent
;
290 * If our parent is NULL (inactive spare or cache) or is the root,
291 * just return our own asize.
294 return (vd
->vdev_asize
);
297 * The top-level vdev just returns the allocatable size rounded
298 * to the nearest metaslab.
300 if (vd
== vd
->vdev_top
)
301 return (P2ALIGN(vd
->vdev_asize
, 1ULL << vd
->vdev_ms_shift
));
303 return (pvd
->vdev_ops
->vdev_op_min_asize(pvd
));
307 vdev_set_min_asize(vdev_t
*vd
)
309 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
311 for (int c
= 0; c
< vd
->vdev_children
; c
++)
312 vdev_set_min_asize(vd
->vdev_child
[c
]);
316 * Get the minimal allocation size for the top-level vdev.
319 vdev_get_min_alloc(vdev_t
*vd
)
321 uint64_t min_alloc
= 1ULL << vd
->vdev_ashift
;
323 if (vd
->vdev_ops
->vdev_op_min_alloc
!= NULL
)
324 min_alloc
= vd
->vdev_ops
->vdev_op_min_alloc(vd
);
330 * Get the parity level for a top-level vdev.
333 vdev_get_nparity(vdev_t
*vd
)
335 uint64_t nparity
= 0;
337 if (vd
->vdev_ops
->vdev_op_nparity
!= NULL
)
338 nparity
= vd
->vdev_ops
->vdev_op_nparity(vd
);
344 * Get the number of data disks for a top-level vdev.
347 vdev_get_ndisks(vdev_t
*vd
)
351 if (vd
->vdev_ops
->vdev_op_ndisks
!= NULL
)
352 ndisks
= vd
->vdev_ops
->vdev_op_ndisks(vd
);
358 vdev_lookup_top(spa_t
*spa
, uint64_t vdev
)
360 vdev_t
*rvd
= spa
->spa_root_vdev
;
362 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
364 if (vdev
< rvd
->vdev_children
) {
365 ASSERT(rvd
->vdev_child
[vdev
] != NULL
);
366 return (rvd
->vdev_child
[vdev
]);
373 vdev_lookup_by_guid(vdev_t
*vd
, uint64_t guid
)
377 if (vd
->vdev_guid
== guid
)
380 for (int c
= 0; c
< vd
->vdev_children
; c
++)
381 if ((mvd
= vdev_lookup_by_guid(vd
->vdev_child
[c
], guid
)) !=
389 vdev_count_leaves_impl(vdev_t
*vd
)
393 if (vd
->vdev_ops
->vdev_op_leaf
)
396 for (int c
= 0; c
< vd
->vdev_children
; c
++)
397 n
+= vdev_count_leaves_impl(vd
->vdev_child
[c
]);
403 vdev_count_leaves(spa_t
*spa
)
407 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
408 rc
= vdev_count_leaves_impl(spa
->spa_root_vdev
);
409 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
415 vdev_add_child(vdev_t
*pvd
, vdev_t
*cvd
)
417 size_t oldsize
, newsize
;
418 uint64_t id
= cvd
->vdev_id
;
421 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
422 ASSERT(cvd
->vdev_parent
== NULL
);
424 cvd
->vdev_parent
= pvd
;
429 ASSERT(id
>= pvd
->vdev_children
|| pvd
->vdev_child
[id
] == NULL
);
431 oldsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
432 pvd
->vdev_children
= MAX(pvd
->vdev_children
, id
+ 1);
433 newsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
435 newchild
= kmem_alloc(newsize
, KM_SLEEP
);
436 if (pvd
->vdev_child
!= NULL
) {
437 bcopy(pvd
->vdev_child
, newchild
, oldsize
);
438 kmem_free(pvd
->vdev_child
, oldsize
);
441 pvd
->vdev_child
= newchild
;
442 pvd
->vdev_child
[id
] = cvd
;
444 cvd
->vdev_top
= (pvd
->vdev_top
? pvd
->vdev_top
: cvd
);
445 ASSERT(cvd
->vdev_top
->vdev_parent
->vdev_parent
== NULL
);
448 * Walk up all ancestors to update guid sum.
450 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
451 pvd
->vdev_guid_sum
+= cvd
->vdev_guid_sum
;
453 if (cvd
->vdev_ops
->vdev_op_leaf
) {
454 list_insert_head(&cvd
->vdev_spa
->spa_leaf_list
, cvd
);
455 cvd
->vdev_spa
->spa_leaf_list_gen
++;
460 vdev_remove_child(vdev_t
*pvd
, vdev_t
*cvd
)
463 uint_t id
= cvd
->vdev_id
;
465 ASSERT(cvd
->vdev_parent
== pvd
);
470 ASSERT(id
< pvd
->vdev_children
);
471 ASSERT(pvd
->vdev_child
[id
] == cvd
);
473 pvd
->vdev_child
[id
] = NULL
;
474 cvd
->vdev_parent
= NULL
;
476 for (c
= 0; c
< pvd
->vdev_children
; c
++)
477 if (pvd
->vdev_child
[c
])
480 if (c
== pvd
->vdev_children
) {
481 kmem_free(pvd
->vdev_child
, c
* sizeof (vdev_t
*));
482 pvd
->vdev_child
= NULL
;
483 pvd
->vdev_children
= 0;
486 if (cvd
->vdev_ops
->vdev_op_leaf
) {
487 spa_t
*spa
= cvd
->vdev_spa
;
488 list_remove(&spa
->spa_leaf_list
, cvd
);
489 spa
->spa_leaf_list_gen
++;
493 * Walk up all ancestors to update guid sum.
495 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
496 pvd
->vdev_guid_sum
-= cvd
->vdev_guid_sum
;
500 * Remove any holes in the child array.
503 vdev_compact_children(vdev_t
*pvd
)
505 vdev_t
**newchild
, *cvd
;
506 int oldc
= pvd
->vdev_children
;
509 ASSERT(spa_config_held(pvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
514 for (int c
= newc
= 0; c
< oldc
; c
++)
515 if (pvd
->vdev_child
[c
])
519 newchild
= kmem_zalloc(newc
* sizeof (vdev_t
*), KM_SLEEP
);
521 for (int c
= newc
= 0; c
< oldc
; c
++) {
522 if ((cvd
= pvd
->vdev_child
[c
]) != NULL
) {
523 newchild
[newc
] = cvd
;
524 cvd
->vdev_id
= newc
++;
531 kmem_free(pvd
->vdev_child
, oldc
* sizeof (vdev_t
*));
532 pvd
->vdev_child
= newchild
;
533 pvd
->vdev_children
= newc
;
537 * Allocate and minimally initialize a vdev_t.
540 vdev_alloc_common(spa_t
*spa
, uint_t id
, uint64_t guid
, vdev_ops_t
*ops
)
543 vdev_indirect_config_t
*vic
;
545 vd
= kmem_zalloc(sizeof (vdev_t
), KM_SLEEP
);
546 vic
= &vd
->vdev_indirect_config
;
548 if (spa
->spa_root_vdev
== NULL
) {
549 ASSERT(ops
== &vdev_root_ops
);
550 spa
->spa_root_vdev
= vd
;
551 spa
->spa_load_guid
= spa_generate_guid(NULL
);
554 if (guid
== 0 && ops
!= &vdev_hole_ops
) {
555 if (spa
->spa_root_vdev
== vd
) {
557 * The root vdev's guid will also be the pool guid,
558 * which must be unique among all pools.
560 guid
= spa_generate_guid(NULL
);
563 * Any other vdev's guid must be unique within the pool.
565 guid
= spa_generate_guid(spa
);
567 ASSERT(!spa_guid_exists(spa_guid(spa
), guid
));
572 vd
->vdev_guid
= guid
;
573 vd
->vdev_guid_sum
= guid
;
575 vd
->vdev_state
= VDEV_STATE_CLOSED
;
576 vd
->vdev_ishole
= (ops
== &vdev_hole_ops
);
577 vic
->vic_prev_indirect_vdev
= UINT64_MAX
;
579 rw_init(&vd
->vdev_indirect_rwlock
, NULL
, RW_DEFAULT
, NULL
);
580 mutex_init(&vd
->vdev_obsolete_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
581 vd
->vdev_obsolete_segments
= range_tree_create(NULL
, RANGE_SEG64
, NULL
,
585 * Initialize rate limit structs for events. We rate limit ZIO delay
586 * and checksum events so that we don't overwhelm ZED with thousands
587 * of events when a disk is acting up.
589 zfs_ratelimit_init(&vd
->vdev_delay_rl
, &zfs_slow_io_events_per_second
,
591 zfs_ratelimit_init(&vd
->vdev_checksum_rl
,
592 &zfs_checksum_events_per_second
, 1);
594 list_link_init(&vd
->vdev_config_dirty_node
);
595 list_link_init(&vd
->vdev_state_dirty_node
);
596 list_link_init(&vd
->vdev_initialize_node
);
597 list_link_init(&vd
->vdev_leaf_node
);
598 list_link_init(&vd
->vdev_trim_node
);
600 mutex_init(&vd
->vdev_dtl_lock
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
601 mutex_init(&vd
->vdev_stat_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
602 mutex_init(&vd
->vdev_probe_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
603 mutex_init(&vd
->vdev_scan_io_queue_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
605 mutex_init(&vd
->vdev_initialize_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
606 mutex_init(&vd
->vdev_initialize_io_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
607 cv_init(&vd
->vdev_initialize_cv
, NULL
, CV_DEFAULT
, NULL
);
608 cv_init(&vd
->vdev_initialize_io_cv
, NULL
, CV_DEFAULT
, NULL
);
610 mutex_init(&vd
->vdev_trim_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
611 mutex_init(&vd
->vdev_autotrim_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
612 mutex_init(&vd
->vdev_trim_io_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
613 cv_init(&vd
->vdev_trim_cv
, NULL
, CV_DEFAULT
, NULL
);
614 cv_init(&vd
->vdev_autotrim_cv
, NULL
, CV_DEFAULT
, NULL
);
615 cv_init(&vd
->vdev_trim_io_cv
, NULL
, CV_DEFAULT
, NULL
);
617 mutex_init(&vd
->vdev_rebuild_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
618 cv_init(&vd
->vdev_rebuild_cv
, NULL
, CV_DEFAULT
, NULL
);
620 for (int t
= 0; t
< DTL_TYPES
; t
++) {
621 vd
->vdev_dtl
[t
] = range_tree_create(NULL
, RANGE_SEG64
, NULL
, 0,
625 txg_list_create(&vd
->vdev_ms_list
, spa
,
626 offsetof(struct metaslab
, ms_txg_node
));
627 txg_list_create(&vd
->vdev_dtl_list
, spa
,
628 offsetof(struct vdev
, vdev_dtl_node
));
629 vd
->vdev_stat
.vs_timestamp
= gethrtime();
637 * Allocate a new vdev. The 'alloctype' is used to control whether we are
638 * creating a new vdev or loading an existing one - the behavior is slightly
639 * different for each case.
642 vdev_alloc(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
, uint_t id
,
647 uint64_t guid
= 0, islog
;
649 vdev_indirect_config_t
*vic
;
652 vdev_alloc_bias_t alloc_bias
= VDEV_BIAS_NONE
;
653 boolean_t top_level
= (parent
&& !parent
->vdev_parent
);
655 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
657 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
658 return (SET_ERROR(EINVAL
));
660 if ((ops
= vdev_getops(type
)) == NULL
)
661 return (SET_ERROR(EINVAL
));
664 * If this is a load, get the vdev guid from the nvlist.
665 * Otherwise, vdev_alloc_common() will generate one for us.
667 if (alloctype
== VDEV_ALLOC_LOAD
) {
670 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
, &label_id
) ||
672 return (SET_ERROR(EINVAL
));
674 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
675 return (SET_ERROR(EINVAL
));
676 } else if (alloctype
== VDEV_ALLOC_SPARE
) {
677 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
678 return (SET_ERROR(EINVAL
));
679 } else if (alloctype
== VDEV_ALLOC_L2CACHE
) {
680 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
681 return (SET_ERROR(EINVAL
));
682 } else if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
683 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
684 return (SET_ERROR(EINVAL
));
688 * The first allocated vdev must be of type 'root'.
690 if (ops
!= &vdev_root_ops
&& spa
->spa_root_vdev
== NULL
)
691 return (SET_ERROR(EINVAL
));
694 * Determine whether we're a log vdev.
697 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &islog
);
698 if (islog
&& spa_version(spa
) < SPA_VERSION_SLOGS
)
699 return (SET_ERROR(ENOTSUP
));
701 if (ops
== &vdev_hole_ops
&& spa_version(spa
) < SPA_VERSION_HOLES
)
702 return (SET_ERROR(ENOTSUP
));
704 if (top_level
&& alloctype
== VDEV_ALLOC_ADD
) {
708 * If creating a top-level vdev, check for allocation
711 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_ALLOCATION_BIAS
,
713 alloc_bias
= vdev_derive_alloc_bias(bias
);
715 /* spa_vdev_add() expects feature to be enabled */
716 if (spa
->spa_load_state
!= SPA_LOAD_CREATE
&&
717 !spa_feature_is_enabled(spa
,
718 SPA_FEATURE_ALLOCATION_CLASSES
)) {
719 return (SET_ERROR(ENOTSUP
));
723 /* spa_vdev_add() expects feature to be enabled */
724 if (ops
== &vdev_draid_ops
&&
725 spa
->spa_load_state
!= SPA_LOAD_CREATE
&&
726 !spa_feature_is_enabled(spa
, SPA_FEATURE_DRAID
)) {
727 return (SET_ERROR(ENOTSUP
));
732 * Initialize the vdev specific data. This is done before calling
733 * vdev_alloc_common() since it may fail and this simplifies the
734 * error reporting and cleanup code paths.
737 if (ops
->vdev_op_init
!= NULL
) {
738 rc
= ops
->vdev_op_init(spa
, nv
, &tsd
);
744 vd
= vdev_alloc_common(spa
, id
, guid
, ops
);
746 vd
->vdev_islog
= islog
;
748 if (top_level
&& alloc_bias
!= VDEV_BIAS_NONE
)
749 vd
->vdev_alloc_bias
= alloc_bias
;
751 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &vd
->vdev_path
) == 0)
752 vd
->vdev_path
= spa_strdup(vd
->vdev_path
);
755 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
756 * fault on a vdev and want it to persist across imports (like with
759 rc
= nvlist_lookup_string(nv
, ZPOOL_CONFIG_AUX_STATE
, &tmp
);
760 if (rc
== 0 && tmp
!= NULL
&& strcmp(tmp
, "external") == 0) {
761 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
762 vd
->vdev_faulted
= 1;
763 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
766 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &vd
->vdev_devid
) == 0)
767 vd
->vdev_devid
= spa_strdup(vd
->vdev_devid
);
768 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PHYS_PATH
,
769 &vd
->vdev_physpath
) == 0)
770 vd
->vdev_physpath
= spa_strdup(vd
->vdev_physpath
);
772 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH
,
773 &vd
->vdev_enc_sysfs_path
) == 0)
774 vd
->vdev_enc_sysfs_path
= spa_strdup(vd
->vdev_enc_sysfs_path
);
776 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_FRU
, &vd
->vdev_fru
) == 0)
777 vd
->vdev_fru
= spa_strdup(vd
->vdev_fru
);
780 * Set the whole_disk property. If it's not specified, leave the value
783 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
784 &vd
->vdev_wholedisk
) != 0)
785 vd
->vdev_wholedisk
= -1ULL;
787 vic
= &vd
->vdev_indirect_config
;
789 ASSERT0(vic
->vic_mapping_object
);
790 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_OBJECT
,
791 &vic
->vic_mapping_object
);
792 ASSERT0(vic
->vic_births_object
);
793 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_BIRTHS
,
794 &vic
->vic_births_object
);
795 ASSERT3U(vic
->vic_prev_indirect_vdev
, ==, UINT64_MAX
);
796 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_PREV_INDIRECT_VDEV
,
797 &vic
->vic_prev_indirect_vdev
);
800 * Look for the 'not present' flag. This will only be set if the device
801 * was not present at the time of import.
803 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
804 &vd
->vdev_not_present
);
807 * Get the alignment requirement.
809 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASHIFT
, &vd
->vdev_ashift
);
812 * Retrieve the vdev creation time.
814 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_CREATE_TXG
,
818 * If we're a top-level vdev, try to load the allocation parameters.
821 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
822 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_ARRAY
,
824 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_SHIFT
,
826 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASIZE
,
828 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVING
,
830 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_VDEV_TOP_ZAP
,
833 ASSERT0(vd
->vdev_top_zap
);
836 if (top_level
&& alloctype
!= VDEV_ALLOC_ATTACH
) {
837 ASSERT(alloctype
== VDEV_ALLOC_LOAD
||
838 alloctype
== VDEV_ALLOC_ADD
||
839 alloctype
== VDEV_ALLOC_SPLIT
||
840 alloctype
== VDEV_ALLOC_ROOTPOOL
);
841 /* Note: metaslab_group_create() is now deferred */
844 if (vd
->vdev_ops
->vdev_op_leaf
&&
845 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
846 (void) nvlist_lookup_uint64(nv
,
847 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, &vd
->vdev_leaf_zap
);
849 ASSERT0(vd
->vdev_leaf_zap
);
853 * If we're a leaf vdev, try to load the DTL object and other state.
856 if (vd
->vdev_ops
->vdev_op_leaf
&&
857 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_L2CACHE
||
858 alloctype
== VDEV_ALLOC_ROOTPOOL
)) {
859 if (alloctype
== VDEV_ALLOC_LOAD
) {
860 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DTL
,
861 &vd
->vdev_dtl_object
);
862 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_UNSPARE
,
866 if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
869 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
870 &spare
) == 0 && spare
)
874 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
,
877 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_RESILVER_TXG
,
878 &vd
->vdev_resilver_txg
);
880 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REBUILD_TXG
,
881 &vd
->vdev_rebuild_txg
);
883 if (nvlist_exists(nv
, ZPOOL_CONFIG_RESILVER_DEFER
))
884 vdev_defer_resilver(vd
);
887 * In general, when importing a pool we want to ignore the
888 * persistent fault state, as the diagnosis made on another
889 * system may not be valid in the current context. The only
890 * exception is if we forced a vdev to a persistently faulted
891 * state with 'zpool offline -f'. The persistent fault will
892 * remain across imports until cleared.
894 * Local vdevs will remain in the faulted state.
896 if (spa_load_state(spa
) == SPA_LOAD_OPEN
||
897 spa_load_state(spa
) == SPA_LOAD_IMPORT
) {
898 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
,
900 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DEGRADED
,
902 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
,
905 if (vd
->vdev_faulted
|| vd
->vdev_degraded
) {
909 VDEV_AUX_ERR_EXCEEDED
;
910 if (nvlist_lookup_string(nv
,
911 ZPOOL_CONFIG_AUX_STATE
, &aux
) == 0 &&
912 strcmp(aux
, "external") == 0)
913 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
915 vd
->vdev_faulted
= 0ULL;
921 * Add ourselves to the parent's list of children.
923 vdev_add_child(parent
, vd
);
931 vdev_free(vdev_t
*vd
)
933 spa_t
*spa
= vd
->vdev_spa
;
935 ASSERT3P(vd
->vdev_initialize_thread
, ==, NULL
);
936 ASSERT3P(vd
->vdev_trim_thread
, ==, NULL
);
937 ASSERT3P(vd
->vdev_autotrim_thread
, ==, NULL
);
938 ASSERT3P(vd
->vdev_rebuild_thread
, ==, NULL
);
941 * Scan queues are normally destroyed at the end of a scan. If the
942 * queue exists here, that implies the vdev is being removed while
943 * the scan is still running.
945 if (vd
->vdev_scan_io_queue
!= NULL
) {
946 mutex_enter(&vd
->vdev_scan_io_queue_lock
);
947 dsl_scan_io_queue_destroy(vd
->vdev_scan_io_queue
);
948 vd
->vdev_scan_io_queue
= NULL
;
949 mutex_exit(&vd
->vdev_scan_io_queue_lock
);
953 * vdev_free() implies closing the vdev first. This is simpler than
954 * trying to ensure complicated semantics for all callers.
958 ASSERT(!list_link_active(&vd
->vdev_config_dirty_node
));
959 ASSERT(!list_link_active(&vd
->vdev_state_dirty_node
));
964 for (int c
= 0; c
< vd
->vdev_children
; c
++)
965 vdev_free(vd
->vdev_child
[c
]);
967 ASSERT(vd
->vdev_child
== NULL
);
968 ASSERT(vd
->vdev_guid_sum
== vd
->vdev_guid
);
970 if (vd
->vdev_ops
->vdev_op_fini
!= NULL
)
971 vd
->vdev_ops
->vdev_op_fini(vd
);
974 * Discard allocation state.
976 if (vd
->vdev_mg
!= NULL
) {
977 vdev_metaslab_fini(vd
);
978 metaslab_group_destroy(vd
->vdev_mg
);
982 ASSERT0(vd
->vdev_stat
.vs_space
);
983 ASSERT0(vd
->vdev_stat
.vs_dspace
);
984 ASSERT0(vd
->vdev_stat
.vs_alloc
);
987 * Remove this vdev from its parent's child list.
989 vdev_remove_child(vd
->vdev_parent
, vd
);
991 ASSERT(vd
->vdev_parent
== NULL
);
992 ASSERT(!list_link_active(&vd
->vdev_leaf_node
));
995 * Clean up vdev structure.
1001 spa_strfree(vd
->vdev_path
);
1003 spa_strfree(vd
->vdev_devid
);
1004 if (vd
->vdev_physpath
)
1005 spa_strfree(vd
->vdev_physpath
);
1007 if (vd
->vdev_enc_sysfs_path
)
1008 spa_strfree(vd
->vdev_enc_sysfs_path
);
1011 spa_strfree(vd
->vdev_fru
);
1013 if (vd
->vdev_isspare
)
1014 spa_spare_remove(vd
);
1015 if (vd
->vdev_isl2cache
)
1016 spa_l2cache_remove(vd
);
1018 txg_list_destroy(&vd
->vdev_ms_list
);
1019 txg_list_destroy(&vd
->vdev_dtl_list
);
1021 mutex_enter(&vd
->vdev_dtl_lock
);
1022 space_map_close(vd
->vdev_dtl_sm
);
1023 for (int t
= 0; t
< DTL_TYPES
; t
++) {
1024 range_tree_vacate(vd
->vdev_dtl
[t
], NULL
, NULL
);
1025 range_tree_destroy(vd
->vdev_dtl
[t
]);
1027 mutex_exit(&vd
->vdev_dtl_lock
);
1029 EQUIV(vd
->vdev_indirect_births
!= NULL
,
1030 vd
->vdev_indirect_mapping
!= NULL
);
1031 if (vd
->vdev_indirect_births
!= NULL
) {
1032 vdev_indirect_mapping_close(vd
->vdev_indirect_mapping
);
1033 vdev_indirect_births_close(vd
->vdev_indirect_births
);
1036 if (vd
->vdev_obsolete_sm
!= NULL
) {
1037 ASSERT(vd
->vdev_removing
||
1038 vd
->vdev_ops
== &vdev_indirect_ops
);
1039 space_map_close(vd
->vdev_obsolete_sm
);
1040 vd
->vdev_obsolete_sm
= NULL
;
1042 range_tree_destroy(vd
->vdev_obsolete_segments
);
1043 rw_destroy(&vd
->vdev_indirect_rwlock
);
1044 mutex_destroy(&vd
->vdev_obsolete_lock
);
1046 mutex_destroy(&vd
->vdev_dtl_lock
);
1047 mutex_destroy(&vd
->vdev_stat_lock
);
1048 mutex_destroy(&vd
->vdev_probe_lock
);
1049 mutex_destroy(&vd
->vdev_scan_io_queue_lock
);
1051 mutex_destroy(&vd
->vdev_initialize_lock
);
1052 mutex_destroy(&vd
->vdev_initialize_io_lock
);
1053 cv_destroy(&vd
->vdev_initialize_io_cv
);
1054 cv_destroy(&vd
->vdev_initialize_cv
);
1056 mutex_destroy(&vd
->vdev_trim_lock
);
1057 mutex_destroy(&vd
->vdev_autotrim_lock
);
1058 mutex_destroy(&vd
->vdev_trim_io_lock
);
1059 cv_destroy(&vd
->vdev_trim_cv
);
1060 cv_destroy(&vd
->vdev_autotrim_cv
);
1061 cv_destroy(&vd
->vdev_trim_io_cv
);
1063 mutex_destroy(&vd
->vdev_rebuild_lock
);
1064 cv_destroy(&vd
->vdev_rebuild_cv
);
1066 zfs_ratelimit_fini(&vd
->vdev_delay_rl
);
1067 zfs_ratelimit_fini(&vd
->vdev_checksum_rl
);
1069 if (vd
== spa
->spa_root_vdev
)
1070 spa
->spa_root_vdev
= NULL
;
1072 kmem_free(vd
, sizeof (vdev_t
));
1076 * Transfer top-level vdev state from svd to tvd.
1079 vdev_top_transfer(vdev_t
*svd
, vdev_t
*tvd
)
1081 spa_t
*spa
= svd
->vdev_spa
;
1086 ASSERT(tvd
== tvd
->vdev_top
);
1088 tvd
->vdev_pending_fastwrite
= svd
->vdev_pending_fastwrite
;
1089 tvd
->vdev_ms_array
= svd
->vdev_ms_array
;
1090 tvd
->vdev_ms_shift
= svd
->vdev_ms_shift
;
1091 tvd
->vdev_ms_count
= svd
->vdev_ms_count
;
1092 tvd
->vdev_top_zap
= svd
->vdev_top_zap
;
1094 svd
->vdev_ms_array
= 0;
1095 svd
->vdev_ms_shift
= 0;
1096 svd
->vdev_ms_count
= 0;
1097 svd
->vdev_top_zap
= 0;
1100 ASSERT3P(tvd
->vdev_mg
, ==, svd
->vdev_mg
);
1101 tvd
->vdev_mg
= svd
->vdev_mg
;
1102 tvd
->vdev_ms
= svd
->vdev_ms
;
1104 svd
->vdev_mg
= NULL
;
1105 svd
->vdev_ms
= NULL
;
1107 if (tvd
->vdev_mg
!= NULL
)
1108 tvd
->vdev_mg
->mg_vd
= tvd
;
1110 tvd
->vdev_checkpoint_sm
= svd
->vdev_checkpoint_sm
;
1111 svd
->vdev_checkpoint_sm
= NULL
;
1113 tvd
->vdev_alloc_bias
= svd
->vdev_alloc_bias
;
1114 svd
->vdev_alloc_bias
= VDEV_BIAS_NONE
;
1116 tvd
->vdev_stat
.vs_alloc
= svd
->vdev_stat
.vs_alloc
;
1117 tvd
->vdev_stat
.vs_space
= svd
->vdev_stat
.vs_space
;
1118 tvd
->vdev_stat
.vs_dspace
= svd
->vdev_stat
.vs_dspace
;
1120 svd
->vdev_stat
.vs_alloc
= 0;
1121 svd
->vdev_stat
.vs_space
= 0;
1122 svd
->vdev_stat
.vs_dspace
= 0;
1125 * State which may be set on a top-level vdev that's in the
1126 * process of being removed.
1128 ASSERT0(tvd
->vdev_indirect_config
.vic_births_object
);
1129 ASSERT0(tvd
->vdev_indirect_config
.vic_mapping_object
);
1130 ASSERT3U(tvd
->vdev_indirect_config
.vic_prev_indirect_vdev
, ==, -1ULL);
1131 ASSERT3P(tvd
->vdev_indirect_mapping
, ==, NULL
);
1132 ASSERT3P(tvd
->vdev_indirect_births
, ==, NULL
);
1133 ASSERT3P(tvd
->vdev_obsolete_sm
, ==, NULL
);
1134 ASSERT0(tvd
->vdev_removing
);
1135 ASSERT0(tvd
->vdev_rebuilding
);
1136 tvd
->vdev_removing
= svd
->vdev_removing
;
1137 tvd
->vdev_rebuilding
= svd
->vdev_rebuilding
;
1138 tvd
->vdev_rebuild_config
= svd
->vdev_rebuild_config
;
1139 tvd
->vdev_indirect_config
= svd
->vdev_indirect_config
;
1140 tvd
->vdev_indirect_mapping
= svd
->vdev_indirect_mapping
;
1141 tvd
->vdev_indirect_births
= svd
->vdev_indirect_births
;
1142 range_tree_swap(&svd
->vdev_obsolete_segments
,
1143 &tvd
->vdev_obsolete_segments
);
1144 tvd
->vdev_obsolete_sm
= svd
->vdev_obsolete_sm
;
1145 svd
->vdev_indirect_config
.vic_mapping_object
= 0;
1146 svd
->vdev_indirect_config
.vic_births_object
= 0;
1147 svd
->vdev_indirect_config
.vic_prev_indirect_vdev
= -1ULL;
1148 svd
->vdev_indirect_mapping
= NULL
;
1149 svd
->vdev_indirect_births
= NULL
;
1150 svd
->vdev_obsolete_sm
= NULL
;
1151 svd
->vdev_removing
= 0;
1152 svd
->vdev_rebuilding
= 0;
1154 for (t
= 0; t
< TXG_SIZE
; t
++) {
1155 while ((msp
= txg_list_remove(&svd
->vdev_ms_list
, t
)) != NULL
)
1156 (void) txg_list_add(&tvd
->vdev_ms_list
, msp
, t
);
1157 while ((vd
= txg_list_remove(&svd
->vdev_dtl_list
, t
)) != NULL
)
1158 (void) txg_list_add(&tvd
->vdev_dtl_list
, vd
, t
);
1159 if (txg_list_remove_this(&spa
->spa_vdev_txg_list
, svd
, t
))
1160 (void) txg_list_add(&spa
->spa_vdev_txg_list
, tvd
, t
);
1163 if (list_link_active(&svd
->vdev_config_dirty_node
)) {
1164 vdev_config_clean(svd
);
1165 vdev_config_dirty(tvd
);
1168 if (list_link_active(&svd
->vdev_state_dirty_node
)) {
1169 vdev_state_clean(svd
);
1170 vdev_state_dirty(tvd
);
1173 tvd
->vdev_deflate_ratio
= svd
->vdev_deflate_ratio
;
1174 svd
->vdev_deflate_ratio
= 0;
1176 tvd
->vdev_islog
= svd
->vdev_islog
;
1177 svd
->vdev_islog
= 0;
1179 dsl_scan_io_queue_vdev_xfer(svd
, tvd
);
1183 vdev_top_update(vdev_t
*tvd
, vdev_t
*vd
)
1190 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1191 vdev_top_update(tvd
, vd
->vdev_child
[c
]);
1195 * Add a mirror/replacing vdev above an existing vdev. There is no need to
1196 * call .vdev_op_init() since mirror/replacing vdevs do not have private state.
1199 vdev_add_parent(vdev_t
*cvd
, vdev_ops_t
*ops
)
1201 spa_t
*spa
= cvd
->vdev_spa
;
1202 vdev_t
*pvd
= cvd
->vdev_parent
;
1205 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1207 mvd
= vdev_alloc_common(spa
, cvd
->vdev_id
, 0, ops
);
1209 mvd
->vdev_asize
= cvd
->vdev_asize
;
1210 mvd
->vdev_min_asize
= cvd
->vdev_min_asize
;
1211 mvd
->vdev_max_asize
= cvd
->vdev_max_asize
;
1212 mvd
->vdev_psize
= cvd
->vdev_psize
;
1213 mvd
->vdev_ashift
= cvd
->vdev_ashift
;
1214 mvd
->vdev_logical_ashift
= cvd
->vdev_logical_ashift
;
1215 mvd
->vdev_physical_ashift
= cvd
->vdev_physical_ashift
;
1216 mvd
->vdev_state
= cvd
->vdev_state
;
1217 mvd
->vdev_crtxg
= cvd
->vdev_crtxg
;
1219 vdev_remove_child(pvd
, cvd
);
1220 vdev_add_child(pvd
, mvd
);
1221 cvd
->vdev_id
= mvd
->vdev_children
;
1222 vdev_add_child(mvd
, cvd
);
1223 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1225 if (mvd
== mvd
->vdev_top
)
1226 vdev_top_transfer(cvd
, mvd
);
1232 * Remove a 1-way mirror/replacing vdev from the tree.
1235 vdev_remove_parent(vdev_t
*cvd
)
1237 vdev_t
*mvd
= cvd
->vdev_parent
;
1238 vdev_t
*pvd
= mvd
->vdev_parent
;
1240 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1242 ASSERT(mvd
->vdev_children
== 1);
1243 ASSERT(mvd
->vdev_ops
== &vdev_mirror_ops
||
1244 mvd
->vdev_ops
== &vdev_replacing_ops
||
1245 mvd
->vdev_ops
== &vdev_spare_ops
);
1246 cvd
->vdev_ashift
= mvd
->vdev_ashift
;
1247 cvd
->vdev_logical_ashift
= mvd
->vdev_logical_ashift
;
1248 cvd
->vdev_physical_ashift
= mvd
->vdev_physical_ashift
;
1249 vdev_remove_child(mvd
, cvd
);
1250 vdev_remove_child(pvd
, mvd
);
1253 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1254 * Otherwise, we could have detached an offline device, and when we
1255 * go to import the pool we'll think we have two top-level vdevs,
1256 * instead of a different version of the same top-level vdev.
1258 if (mvd
->vdev_top
== mvd
) {
1259 uint64_t guid_delta
= mvd
->vdev_guid
- cvd
->vdev_guid
;
1260 cvd
->vdev_orig_guid
= cvd
->vdev_guid
;
1261 cvd
->vdev_guid
+= guid_delta
;
1262 cvd
->vdev_guid_sum
+= guid_delta
;
1265 * If pool not set for autoexpand, we need to also preserve
1266 * mvd's asize to prevent automatic expansion of cvd.
1267 * Otherwise if we are adjusting the mirror by attaching and
1268 * detaching children of non-uniform sizes, the mirror could
1269 * autoexpand, unexpectedly requiring larger devices to
1270 * re-establish the mirror.
1272 if (!cvd
->vdev_spa
->spa_autoexpand
)
1273 cvd
->vdev_asize
= mvd
->vdev_asize
;
1275 cvd
->vdev_id
= mvd
->vdev_id
;
1276 vdev_add_child(pvd
, cvd
);
1277 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1279 if (cvd
== cvd
->vdev_top
)
1280 vdev_top_transfer(mvd
, cvd
);
1282 ASSERT(mvd
->vdev_children
== 0);
1287 vdev_metaslab_group_create(vdev_t
*vd
)
1289 spa_t
*spa
= vd
->vdev_spa
;
1292 * metaslab_group_create was delayed until allocation bias was available
1294 if (vd
->vdev_mg
== NULL
) {
1295 metaslab_class_t
*mc
;
1297 if (vd
->vdev_islog
&& vd
->vdev_alloc_bias
== VDEV_BIAS_NONE
)
1298 vd
->vdev_alloc_bias
= VDEV_BIAS_LOG
;
1300 ASSERT3U(vd
->vdev_islog
, ==,
1301 (vd
->vdev_alloc_bias
== VDEV_BIAS_LOG
));
1303 switch (vd
->vdev_alloc_bias
) {
1305 mc
= spa_log_class(spa
);
1307 case VDEV_BIAS_SPECIAL
:
1308 mc
= spa_special_class(spa
);
1310 case VDEV_BIAS_DEDUP
:
1311 mc
= spa_dedup_class(spa
);
1314 mc
= spa_normal_class(spa
);
1317 vd
->vdev_mg
= metaslab_group_create(mc
, vd
,
1318 spa
->spa_alloc_count
);
1321 * The spa ashift min/max only apply for the normal metaslab
1322 * class. Class destination is late binding so ashift boundry
1323 * setting had to wait until now.
1325 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
1326 mc
== spa_normal_class(spa
) && vd
->vdev_aux
== NULL
) {
1327 if (vd
->vdev_ashift
> spa
->spa_max_ashift
)
1328 spa
->spa_max_ashift
= vd
->vdev_ashift
;
1329 if (vd
->vdev_ashift
< spa
->spa_min_ashift
)
1330 spa
->spa_min_ashift
= vd
->vdev_ashift
;
1332 uint64_t min_alloc
= vdev_get_min_alloc(vd
);
1333 if (min_alloc
< spa
->spa_min_alloc
)
1334 spa
->spa_min_alloc
= min_alloc
;
1340 vdev_metaslab_init(vdev_t
*vd
, uint64_t txg
)
1342 spa_t
*spa
= vd
->vdev_spa
;
1343 objset_t
*mos
= spa
->spa_meta_objset
;
1345 uint64_t oldc
= vd
->vdev_ms_count
;
1346 uint64_t newc
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
1349 boolean_t expanding
= (oldc
!= 0);
1351 ASSERT(txg
== 0 || spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1354 * This vdev is not being allocated from yet or is a hole.
1356 if (vd
->vdev_ms_shift
== 0)
1359 ASSERT(!vd
->vdev_ishole
);
1361 ASSERT(oldc
<= newc
);
1363 mspp
= vmem_zalloc(newc
* sizeof (*mspp
), KM_SLEEP
);
1366 bcopy(vd
->vdev_ms
, mspp
, oldc
* sizeof (*mspp
));
1367 vmem_free(vd
->vdev_ms
, oldc
* sizeof (*mspp
));
1371 vd
->vdev_ms_count
= newc
;
1372 for (m
= oldc
; m
< newc
; m
++) {
1373 uint64_t object
= 0;
1376 * vdev_ms_array may be 0 if we are creating the "fake"
1377 * metaslabs for an indirect vdev for zdb's leak detection.
1378 * See zdb_leak_init().
1380 if (txg
== 0 && vd
->vdev_ms_array
!= 0) {
1381 error
= dmu_read(mos
, vd
->vdev_ms_array
,
1382 m
* sizeof (uint64_t), sizeof (uint64_t), &object
,
1385 vdev_dbgmsg(vd
, "unable to read the metaslab "
1386 "array [error=%d]", error
);
1393 * To accommodate zdb_leak_init() fake indirect
1394 * metaslabs, we allocate a metaslab group for
1395 * indirect vdevs which normally don't have one.
1397 if (vd
->vdev_mg
== NULL
) {
1398 ASSERT0(vdev_is_concrete(vd
));
1399 vdev_metaslab_group_create(vd
);
1402 error
= metaslab_init(vd
->vdev_mg
, m
, object
, txg
,
1405 vdev_dbgmsg(vd
, "metaslab_init failed [error=%d]",
1412 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_WRITER
);
1415 * If the vdev is being removed we don't activate
1416 * the metaslabs since we want to ensure that no new
1417 * allocations are performed on this device.
1419 if (!expanding
&& !vd
->vdev_removing
) {
1420 metaslab_group_activate(vd
->vdev_mg
);
1424 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
1427 * Regardless whether this vdev was just added or it is being
1428 * expanded, the metaslab count has changed. Recalculate the
1431 spa_log_sm_set_blocklimit(spa
);
1437 vdev_metaslab_fini(vdev_t
*vd
)
1439 if (vd
->vdev_checkpoint_sm
!= NULL
) {
1440 ASSERT(spa_feature_is_active(vd
->vdev_spa
,
1441 SPA_FEATURE_POOL_CHECKPOINT
));
1442 space_map_close(vd
->vdev_checkpoint_sm
);
1444 * Even though we close the space map, we need to set its
1445 * pointer to NULL. The reason is that vdev_metaslab_fini()
1446 * may be called multiple times for certain operations
1447 * (i.e. when destroying a pool) so we need to ensure that
1448 * this clause never executes twice. This logic is similar
1449 * to the one used for the vdev_ms clause below.
1451 vd
->vdev_checkpoint_sm
= NULL
;
1454 if (vd
->vdev_ms
!= NULL
) {
1455 metaslab_group_t
*mg
= vd
->vdev_mg
;
1456 metaslab_group_passivate(mg
);
1458 uint64_t count
= vd
->vdev_ms_count
;
1459 for (uint64_t m
= 0; m
< count
; m
++) {
1460 metaslab_t
*msp
= vd
->vdev_ms
[m
];
1464 vmem_free(vd
->vdev_ms
, count
* sizeof (metaslab_t
*));
1467 vd
->vdev_ms_count
= 0;
1469 for (int i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++)
1470 ASSERT0(mg
->mg_histogram
[i
]);
1472 ASSERT0(vd
->vdev_ms_count
);
1473 ASSERT3U(vd
->vdev_pending_fastwrite
, ==, 0);
1476 typedef struct vdev_probe_stats
{
1477 boolean_t vps_readable
;
1478 boolean_t vps_writeable
;
1480 } vdev_probe_stats_t
;
1483 vdev_probe_done(zio_t
*zio
)
1485 spa_t
*spa
= zio
->io_spa
;
1486 vdev_t
*vd
= zio
->io_vd
;
1487 vdev_probe_stats_t
*vps
= zio
->io_private
;
1489 ASSERT(vd
->vdev_probe_zio
!= NULL
);
1491 if (zio
->io_type
== ZIO_TYPE_READ
) {
1492 if (zio
->io_error
== 0)
1493 vps
->vps_readable
= 1;
1494 if (zio
->io_error
== 0 && spa_writeable(spa
)) {
1495 zio_nowait(zio_write_phys(vd
->vdev_probe_zio
, vd
,
1496 zio
->io_offset
, zio
->io_size
, zio
->io_abd
,
1497 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1498 ZIO_PRIORITY_SYNC_WRITE
, vps
->vps_flags
, B_TRUE
));
1500 abd_free(zio
->io_abd
);
1502 } else if (zio
->io_type
== ZIO_TYPE_WRITE
) {
1503 if (zio
->io_error
== 0)
1504 vps
->vps_writeable
= 1;
1505 abd_free(zio
->io_abd
);
1506 } else if (zio
->io_type
== ZIO_TYPE_NULL
) {
1510 vd
->vdev_cant_read
|= !vps
->vps_readable
;
1511 vd
->vdev_cant_write
|= !vps
->vps_writeable
;
1513 if (vdev_readable(vd
) &&
1514 (vdev_writeable(vd
) || !spa_writeable(spa
))) {
1517 ASSERT(zio
->io_error
!= 0);
1518 vdev_dbgmsg(vd
, "failed probe");
1519 (void) zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE
,
1520 spa
, vd
, NULL
, NULL
, 0);
1521 zio
->io_error
= SET_ERROR(ENXIO
);
1524 mutex_enter(&vd
->vdev_probe_lock
);
1525 ASSERT(vd
->vdev_probe_zio
== zio
);
1526 vd
->vdev_probe_zio
= NULL
;
1527 mutex_exit(&vd
->vdev_probe_lock
);
1530 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
)
1531 if (!vdev_accessible(vd
, pio
))
1532 pio
->io_error
= SET_ERROR(ENXIO
);
1534 kmem_free(vps
, sizeof (*vps
));
1539 * Determine whether this device is accessible.
1541 * Read and write to several known locations: the pad regions of each
1542 * vdev label but the first, which we leave alone in case it contains
1546 vdev_probe(vdev_t
*vd
, zio_t
*zio
)
1548 spa_t
*spa
= vd
->vdev_spa
;
1549 vdev_probe_stats_t
*vps
= NULL
;
1552 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1555 * Don't probe the probe.
1557 if (zio
&& (zio
->io_flags
& ZIO_FLAG_PROBE
))
1561 * To prevent 'probe storms' when a device fails, we create
1562 * just one probe i/o at a time. All zios that want to probe
1563 * this vdev will become parents of the probe io.
1565 mutex_enter(&vd
->vdev_probe_lock
);
1567 if ((pio
= vd
->vdev_probe_zio
) == NULL
) {
1568 vps
= kmem_zalloc(sizeof (*vps
), KM_SLEEP
);
1570 vps
->vps_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_PROBE
|
1571 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_AGGREGATE
|
1574 if (spa_config_held(spa
, SCL_ZIO
, RW_WRITER
)) {
1576 * vdev_cant_read and vdev_cant_write can only
1577 * transition from TRUE to FALSE when we have the
1578 * SCL_ZIO lock as writer; otherwise they can only
1579 * transition from FALSE to TRUE. This ensures that
1580 * any zio looking at these values can assume that
1581 * failures persist for the life of the I/O. That's
1582 * important because when a device has intermittent
1583 * connectivity problems, we want to ensure that
1584 * they're ascribed to the device (ENXIO) and not
1587 * Since we hold SCL_ZIO as writer here, clear both
1588 * values so the probe can reevaluate from first
1591 vps
->vps_flags
|= ZIO_FLAG_CONFIG_WRITER
;
1592 vd
->vdev_cant_read
= B_FALSE
;
1593 vd
->vdev_cant_write
= B_FALSE
;
1596 vd
->vdev_probe_zio
= pio
= zio_null(NULL
, spa
, vd
,
1597 vdev_probe_done
, vps
,
1598 vps
->vps_flags
| ZIO_FLAG_DONT_PROPAGATE
);
1601 * We can't change the vdev state in this context, so we
1602 * kick off an async task to do it on our behalf.
1605 vd
->vdev_probe_wanted
= B_TRUE
;
1606 spa_async_request(spa
, SPA_ASYNC_PROBE
);
1611 zio_add_child(zio
, pio
);
1613 mutex_exit(&vd
->vdev_probe_lock
);
1616 ASSERT(zio
!= NULL
);
1620 for (int l
= 1; l
< VDEV_LABELS
; l
++) {
1621 zio_nowait(zio_read_phys(pio
, vd
,
1622 vdev_label_offset(vd
->vdev_psize
, l
,
1623 offsetof(vdev_label_t
, vl_be
)), VDEV_PAD_SIZE
,
1624 abd_alloc_for_io(VDEV_PAD_SIZE
, B_TRUE
),
1625 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1626 ZIO_PRIORITY_SYNC_READ
, vps
->vps_flags
, B_TRUE
));
1637 vdev_open_child(void *arg
)
1641 vd
->vdev_open_thread
= curthread
;
1642 vd
->vdev_open_error
= vdev_open(vd
);
1643 vd
->vdev_open_thread
= NULL
;
1647 vdev_uses_zvols(vdev_t
*vd
)
1650 if (zvol_is_zvol(vd
->vdev_path
))
1654 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1655 if (vdev_uses_zvols(vd
->vdev_child
[c
]))
1662 * Returns B_TRUE if the passed child should be opened.
1665 vdev_default_open_children_func(vdev_t
*vd
)
1671 * Open the requested child vdevs. If any of the leaf vdevs are using
1672 * a ZFS volume then do the opens in a single thread. This avoids a
1673 * deadlock when the current thread is holding the spa_namespace_lock.
1676 vdev_open_children_impl(vdev_t
*vd
, vdev_open_children_func_t
*open_func
)
1678 int children
= vd
->vdev_children
;
1680 taskq_t
*tq
= taskq_create("vdev_open", children
, minclsyspri
,
1681 children
, children
, TASKQ_PREPOPULATE
);
1682 vd
->vdev_nonrot
= B_TRUE
;
1684 for (int c
= 0; c
< children
; c
++) {
1685 vdev_t
*cvd
= vd
->vdev_child
[c
];
1687 if (open_func(cvd
) == B_FALSE
)
1690 if (tq
== NULL
|| vdev_uses_zvols(vd
)) {
1691 cvd
->vdev_open_error
= vdev_open(cvd
);
1693 VERIFY(taskq_dispatch(tq
, vdev_open_child
,
1694 cvd
, TQ_SLEEP
) != TASKQID_INVALID
);
1697 vd
->vdev_nonrot
&= cvd
->vdev_nonrot
;
1707 * Open all child vdevs.
1710 vdev_open_children(vdev_t
*vd
)
1712 vdev_open_children_impl(vd
, vdev_default_open_children_func
);
1716 * Conditionally open a subset of child vdevs.
1719 vdev_open_children_subset(vdev_t
*vd
, vdev_open_children_func_t
*open_func
)
1721 vdev_open_children_impl(vd
, open_func
);
1725 * Compute the raidz-deflation ratio. Note, we hard-code
1726 * in 128k (1 << 17) because it is the "typical" blocksize.
1727 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1728 * otherwise it would inconsistently account for existing bp's.
1731 vdev_set_deflate_ratio(vdev_t
*vd
)
1733 if (vd
== vd
->vdev_top
&& !vd
->vdev_ishole
&& vd
->vdev_ashift
!= 0) {
1734 vd
->vdev_deflate_ratio
= (1 << 17) /
1735 (vdev_psize_to_asize(vd
, 1 << 17) >> SPA_MINBLOCKSHIFT
);
1740 * Maximize performance by inflating the configured ashift for top level
1741 * vdevs to be as close to the physical ashift as possible while maintaining
1742 * administrator defined limits and ensuring it doesn't go below the
1746 vdev_ashift_optimize(vdev_t
*vd
)
1748 ASSERT(vd
== vd
->vdev_top
);
1750 if (vd
->vdev_ashift
< vd
->vdev_physical_ashift
) {
1751 vd
->vdev_ashift
= MIN(
1752 MAX(zfs_vdev_max_auto_ashift
, vd
->vdev_ashift
),
1753 MAX(zfs_vdev_min_auto_ashift
,
1754 vd
->vdev_physical_ashift
));
1757 * If the logical and physical ashifts are the same, then
1758 * we ensure that the top-level vdev's ashift is not smaller
1759 * than our minimum ashift value. For the unusual case
1760 * where logical ashift > physical ashift, we can't cap
1761 * the calculated ashift based on max ashift as that
1762 * would cause failures.
1763 * We still check if we need to increase it to match
1766 vd
->vdev_ashift
= MAX(zfs_vdev_min_auto_ashift
,
1772 * Prepare a virtual device for access.
1775 vdev_open(vdev_t
*vd
)
1777 spa_t
*spa
= vd
->vdev_spa
;
1780 uint64_t max_osize
= 0;
1781 uint64_t asize
, max_asize
, psize
;
1782 uint64_t logical_ashift
= 0;
1783 uint64_t physical_ashift
= 0;
1785 ASSERT(vd
->vdev_open_thread
== curthread
||
1786 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1787 ASSERT(vd
->vdev_state
== VDEV_STATE_CLOSED
||
1788 vd
->vdev_state
== VDEV_STATE_CANT_OPEN
||
1789 vd
->vdev_state
== VDEV_STATE_OFFLINE
);
1791 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1792 vd
->vdev_cant_read
= B_FALSE
;
1793 vd
->vdev_cant_write
= B_FALSE
;
1794 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
1797 * If this vdev is not removed, check its fault status. If it's
1798 * faulted, bail out of the open.
1800 if (!vd
->vdev_removed
&& vd
->vdev_faulted
) {
1801 ASSERT(vd
->vdev_children
== 0);
1802 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1803 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1804 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1805 vd
->vdev_label_aux
);
1806 return (SET_ERROR(ENXIO
));
1807 } else if (vd
->vdev_offline
) {
1808 ASSERT(vd
->vdev_children
== 0);
1809 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
, VDEV_AUX_NONE
);
1810 return (SET_ERROR(ENXIO
));
1813 error
= vd
->vdev_ops
->vdev_op_open(vd
, &osize
, &max_osize
,
1814 &logical_ashift
, &physical_ashift
);
1816 * Physical volume size should never be larger than its max size, unless
1817 * the disk has shrunk while we were reading it or the device is buggy
1818 * or damaged: either way it's not safe for use, bail out of the open.
1820 if (osize
> max_osize
) {
1821 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1822 VDEV_AUX_OPEN_FAILED
);
1823 return (SET_ERROR(ENXIO
));
1827 * Reset the vdev_reopening flag so that we actually close
1828 * the vdev on error.
1830 vd
->vdev_reopening
= B_FALSE
;
1831 if (zio_injection_enabled
&& error
== 0)
1832 error
= zio_handle_device_injection(vd
, NULL
, SET_ERROR(ENXIO
));
1835 if (vd
->vdev_removed
&&
1836 vd
->vdev_stat
.vs_aux
!= VDEV_AUX_OPEN_FAILED
)
1837 vd
->vdev_removed
= B_FALSE
;
1839 if (vd
->vdev_stat
.vs_aux
== VDEV_AUX_CHILDREN_OFFLINE
) {
1840 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
,
1841 vd
->vdev_stat
.vs_aux
);
1843 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1844 vd
->vdev_stat
.vs_aux
);
1849 vd
->vdev_removed
= B_FALSE
;
1852 * Recheck the faulted flag now that we have confirmed that
1853 * the vdev is accessible. If we're faulted, bail.
1855 if (vd
->vdev_faulted
) {
1856 ASSERT(vd
->vdev_children
== 0);
1857 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1858 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1859 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1860 vd
->vdev_label_aux
);
1861 return (SET_ERROR(ENXIO
));
1864 if (vd
->vdev_degraded
) {
1865 ASSERT(vd
->vdev_children
== 0);
1866 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1867 VDEV_AUX_ERR_EXCEEDED
);
1869 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_HEALTHY
, 0);
1873 * For hole or missing vdevs we just return success.
1875 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
)
1878 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
1879 if (vd
->vdev_child
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
) {
1880 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1886 osize
= P2ALIGN(osize
, (uint64_t)sizeof (vdev_label_t
));
1887 max_osize
= P2ALIGN(max_osize
, (uint64_t)sizeof (vdev_label_t
));
1889 if (vd
->vdev_children
== 0) {
1890 if (osize
< SPA_MINDEVSIZE
) {
1891 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1892 VDEV_AUX_TOO_SMALL
);
1893 return (SET_ERROR(EOVERFLOW
));
1896 asize
= osize
- (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
);
1897 max_asize
= max_osize
- (VDEV_LABEL_START_SIZE
+
1898 VDEV_LABEL_END_SIZE
);
1900 if (vd
->vdev_parent
!= NULL
&& osize
< SPA_MINDEVSIZE
-
1901 (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
)) {
1902 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1903 VDEV_AUX_TOO_SMALL
);
1904 return (SET_ERROR(EOVERFLOW
));
1908 max_asize
= max_osize
;
1912 * If the vdev was expanded, record this so that we can re-create the
1913 * uberblock rings in labels {2,3}, during the next sync.
1915 if ((psize
> vd
->vdev_psize
) && (vd
->vdev_psize
!= 0))
1916 vd
->vdev_copy_uberblocks
= B_TRUE
;
1918 vd
->vdev_psize
= psize
;
1921 * Make sure the allocatable size hasn't shrunk too much.
1923 if (asize
< vd
->vdev_min_asize
) {
1924 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1925 VDEV_AUX_BAD_LABEL
);
1926 return (SET_ERROR(EINVAL
));
1930 * We can always set the logical/physical ashift members since
1931 * their values are only used to calculate the vdev_ashift when
1932 * the device is first added to the config. These values should
1933 * not be used for anything else since they may change whenever
1934 * the device is reopened and we don't store them in the label.
1936 vd
->vdev_physical_ashift
=
1937 MAX(physical_ashift
, vd
->vdev_physical_ashift
);
1938 vd
->vdev_logical_ashift
= MAX(logical_ashift
,
1939 vd
->vdev_logical_ashift
);
1941 if (vd
->vdev_asize
== 0) {
1943 * This is the first-ever open, so use the computed values.
1944 * For compatibility, a different ashift can be requested.
1946 vd
->vdev_asize
= asize
;
1947 vd
->vdev_max_asize
= max_asize
;
1950 * If the vdev_ashift was not overriden at creation time,
1951 * then set it the logical ashift and optimize the ashift.
1953 if (vd
->vdev_ashift
== 0) {
1954 vd
->vdev_ashift
= vd
->vdev_logical_ashift
;
1956 if (vd
->vdev_logical_ashift
> ASHIFT_MAX
) {
1957 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1958 VDEV_AUX_ASHIFT_TOO_BIG
);
1959 return (SET_ERROR(EDOM
));
1962 if (vd
->vdev_top
== vd
) {
1963 vdev_ashift_optimize(vd
);
1966 if (vd
->vdev_ashift
!= 0 && (vd
->vdev_ashift
< ASHIFT_MIN
||
1967 vd
->vdev_ashift
> ASHIFT_MAX
)) {
1968 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1969 VDEV_AUX_BAD_ASHIFT
);
1970 return (SET_ERROR(EDOM
));
1974 * Make sure the alignment required hasn't increased.
1976 if (vd
->vdev_ashift
> vd
->vdev_top
->vdev_ashift
&&
1977 vd
->vdev_ops
->vdev_op_leaf
) {
1978 (void) zfs_ereport_post(
1979 FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
,
1980 spa
, vd
, NULL
, NULL
, 0);
1981 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1982 VDEV_AUX_BAD_LABEL
);
1983 return (SET_ERROR(EDOM
));
1985 vd
->vdev_max_asize
= max_asize
;
1989 * If all children are healthy we update asize if either:
1990 * The asize has increased, due to a device expansion caused by dynamic
1991 * LUN growth or vdev replacement, and automatic expansion is enabled;
1992 * making the additional space available.
1994 * The asize has decreased, due to a device shrink usually caused by a
1995 * vdev replace with a smaller device. This ensures that calculations
1996 * based of max_asize and asize e.g. esize are always valid. It's safe
1997 * to do this as we've already validated that asize is greater than
2000 if (vd
->vdev_state
== VDEV_STATE_HEALTHY
&&
2001 ((asize
> vd
->vdev_asize
&&
2002 (vd
->vdev_expanding
|| spa
->spa_autoexpand
)) ||
2003 (asize
< vd
->vdev_asize
)))
2004 vd
->vdev_asize
= asize
;
2006 vdev_set_min_asize(vd
);
2009 * Ensure we can issue some IO before declaring the
2010 * vdev open for business.
2012 if (vd
->vdev_ops
->vdev_op_leaf
&&
2013 (error
= zio_wait(vdev_probe(vd
, NULL
))) != 0) {
2014 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
2015 VDEV_AUX_ERR_EXCEEDED
);
2020 * Track the the minimum allocation size.
2022 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
2023 vd
->vdev_islog
== 0 && vd
->vdev_aux
== NULL
) {
2024 uint64_t min_alloc
= vdev_get_min_alloc(vd
);
2025 if (min_alloc
< spa
->spa_min_alloc
)
2026 spa
->spa_min_alloc
= min_alloc
;
2030 * If this is a leaf vdev, assess whether a resilver is needed.
2031 * But don't do this if we are doing a reopen for a scrub, since
2032 * this would just restart the scrub we are already doing.
2034 if (vd
->vdev_ops
->vdev_op_leaf
&& !spa
->spa_scrub_reopen
)
2035 dsl_scan_assess_vdev(spa
->spa_dsl_pool
, vd
);
2041 * Called once the vdevs are all opened, this routine validates the label
2042 * contents. This needs to be done before vdev_load() so that we don't
2043 * inadvertently do repair I/Os to the wrong device.
2045 * This function will only return failure if one of the vdevs indicates that it
2046 * has since been destroyed or exported. This is only possible if
2047 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
2048 * will be updated but the function will return 0.
2051 vdev_validate(vdev_t
*vd
)
2053 spa_t
*spa
= vd
->vdev_spa
;
2055 uint64_t guid
= 0, aux_guid
= 0, top_guid
;
2060 if (vdev_validate_skip
)
2063 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
2064 if (vdev_validate(vd
->vdev_child
[c
]) != 0)
2065 return (SET_ERROR(EBADF
));
2068 * If the device has already failed, or was marked offline, don't do
2069 * any further validation. Otherwise, label I/O will fail and we will
2070 * overwrite the previous state.
2072 if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_readable(vd
))
2076 * If we are performing an extreme rewind, we allow for a label that
2077 * was modified at a point after the current txg.
2078 * If config lock is not held do not check for the txg. spa_sync could
2079 * be updating the vdev's label before updating spa_last_synced_txg.
2081 if (spa
->spa_extreme_rewind
|| spa_last_synced_txg(spa
) == 0 ||
2082 spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) != SCL_CONFIG
)
2085 txg
= spa_last_synced_txg(spa
);
2087 if ((label
= vdev_label_read_config(vd
, txg
)) == NULL
) {
2088 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2089 VDEV_AUX_BAD_LABEL
);
2090 vdev_dbgmsg(vd
, "vdev_validate: failed reading config for "
2091 "txg %llu", (u_longlong_t
)txg
);
2096 * Determine if this vdev has been split off into another
2097 * pool. If so, then refuse to open it.
2099 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_SPLIT_GUID
,
2100 &aux_guid
) == 0 && aux_guid
== spa_guid(spa
)) {
2101 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2102 VDEV_AUX_SPLIT_POOL
);
2104 vdev_dbgmsg(vd
, "vdev_validate: vdev split into other pool");
2108 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0) {
2109 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2110 VDEV_AUX_CORRUPT_DATA
);
2112 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2113 ZPOOL_CONFIG_POOL_GUID
);
2118 * If config is not trusted then ignore the spa guid check. This is
2119 * necessary because if the machine crashed during a re-guid the new
2120 * guid might have been written to all of the vdev labels, but not the
2121 * cached config. The check will be performed again once we have the
2122 * trusted config from the MOS.
2124 if (spa
->spa_trust_config
&& guid
!= spa_guid(spa
)) {
2125 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2126 VDEV_AUX_CORRUPT_DATA
);
2128 vdev_dbgmsg(vd
, "vdev_validate: vdev label pool_guid doesn't "
2129 "match config (%llu != %llu)", (u_longlong_t
)guid
,
2130 (u_longlong_t
)spa_guid(spa
));
2134 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
)
2135 != 0 || nvlist_lookup_uint64(nvl
, ZPOOL_CONFIG_ORIG_GUID
,
2139 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0) {
2140 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2141 VDEV_AUX_CORRUPT_DATA
);
2143 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2148 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_TOP_GUID
, &top_guid
)
2150 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2151 VDEV_AUX_CORRUPT_DATA
);
2153 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2154 ZPOOL_CONFIG_TOP_GUID
);
2159 * If this vdev just became a top-level vdev because its sibling was
2160 * detached, it will have adopted the parent's vdev guid -- but the
2161 * label may or may not be on disk yet. Fortunately, either version
2162 * of the label will have the same top guid, so if we're a top-level
2163 * vdev, we can safely compare to that instead.
2164 * However, if the config comes from a cachefile that failed to update
2165 * after the detach, a top-level vdev will appear as a non top-level
2166 * vdev in the config. Also relax the constraints if we perform an
2169 * If we split this vdev off instead, then we also check the
2170 * original pool's guid. We don't want to consider the vdev
2171 * corrupt if it is partway through a split operation.
2173 if (vd
->vdev_guid
!= guid
&& vd
->vdev_guid
!= aux_guid
) {
2174 boolean_t mismatch
= B_FALSE
;
2175 if (spa
->spa_trust_config
&& !spa
->spa_extreme_rewind
) {
2176 if (vd
!= vd
->vdev_top
|| vd
->vdev_guid
!= top_guid
)
2179 if (vd
->vdev_guid
!= top_guid
&&
2180 vd
->vdev_top
->vdev_guid
!= guid
)
2185 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2186 VDEV_AUX_CORRUPT_DATA
);
2188 vdev_dbgmsg(vd
, "vdev_validate: config guid "
2189 "doesn't match label guid");
2190 vdev_dbgmsg(vd
, "CONFIG: guid %llu, top_guid %llu",
2191 (u_longlong_t
)vd
->vdev_guid
,
2192 (u_longlong_t
)vd
->vdev_top
->vdev_guid
);
2193 vdev_dbgmsg(vd
, "LABEL: guid %llu, top_guid %llu, "
2194 "aux_guid %llu", (u_longlong_t
)guid
,
2195 (u_longlong_t
)top_guid
, (u_longlong_t
)aux_guid
);
2200 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
,
2202 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2203 VDEV_AUX_CORRUPT_DATA
);
2205 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2206 ZPOOL_CONFIG_POOL_STATE
);
2213 * If this is a verbatim import, no need to check the
2214 * state of the pool.
2216 if (!(spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) &&
2217 spa_load_state(spa
) == SPA_LOAD_OPEN
&&
2218 state
!= POOL_STATE_ACTIVE
) {
2219 vdev_dbgmsg(vd
, "vdev_validate: invalid pool state (%llu) "
2220 "for spa %s", (u_longlong_t
)state
, spa
->spa_name
);
2221 return (SET_ERROR(EBADF
));
2225 * If we were able to open and validate a vdev that was
2226 * previously marked permanently unavailable, clear that state
2229 if (vd
->vdev_not_present
)
2230 vd
->vdev_not_present
= 0;
2236 vdev_copy_path_impl(vdev_t
*svd
, vdev_t
*dvd
)
2238 if (svd
->vdev_path
!= NULL
&& dvd
->vdev_path
!= NULL
) {
2239 if (strcmp(svd
->vdev_path
, dvd
->vdev_path
) != 0) {
2240 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
2241 "from '%s' to '%s'", (u_longlong_t
)dvd
->vdev_guid
,
2242 dvd
->vdev_path
, svd
->vdev_path
);
2243 spa_strfree(dvd
->vdev_path
);
2244 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
2246 } else if (svd
->vdev_path
!= NULL
) {
2247 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
2248 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
2249 (u_longlong_t
)dvd
->vdev_guid
, dvd
->vdev_path
);
2254 * Recursively copy vdev paths from one vdev to another. Source and destination
2255 * vdev trees must have same geometry otherwise return error. Intended to copy
2256 * paths from userland config into MOS config.
2259 vdev_copy_path_strict(vdev_t
*svd
, vdev_t
*dvd
)
2261 if ((svd
->vdev_ops
== &vdev_missing_ops
) ||
2262 (svd
->vdev_ishole
&& dvd
->vdev_ishole
) ||
2263 (dvd
->vdev_ops
== &vdev_indirect_ops
))
2266 if (svd
->vdev_ops
!= dvd
->vdev_ops
) {
2267 vdev_dbgmsg(svd
, "vdev_copy_path: vdev type mismatch: %s != %s",
2268 svd
->vdev_ops
->vdev_op_type
, dvd
->vdev_ops
->vdev_op_type
);
2269 return (SET_ERROR(EINVAL
));
2272 if (svd
->vdev_guid
!= dvd
->vdev_guid
) {
2273 vdev_dbgmsg(svd
, "vdev_copy_path: guids mismatch (%llu != "
2274 "%llu)", (u_longlong_t
)svd
->vdev_guid
,
2275 (u_longlong_t
)dvd
->vdev_guid
);
2276 return (SET_ERROR(EINVAL
));
2279 if (svd
->vdev_children
!= dvd
->vdev_children
) {
2280 vdev_dbgmsg(svd
, "vdev_copy_path: children count mismatch: "
2281 "%llu != %llu", (u_longlong_t
)svd
->vdev_children
,
2282 (u_longlong_t
)dvd
->vdev_children
);
2283 return (SET_ERROR(EINVAL
));
2286 for (uint64_t i
= 0; i
< svd
->vdev_children
; i
++) {
2287 int error
= vdev_copy_path_strict(svd
->vdev_child
[i
],
2288 dvd
->vdev_child
[i
]);
2293 if (svd
->vdev_ops
->vdev_op_leaf
)
2294 vdev_copy_path_impl(svd
, dvd
);
2300 vdev_copy_path_search(vdev_t
*stvd
, vdev_t
*dvd
)
2302 ASSERT(stvd
->vdev_top
== stvd
);
2303 ASSERT3U(stvd
->vdev_id
, ==, dvd
->vdev_top
->vdev_id
);
2305 for (uint64_t i
= 0; i
< dvd
->vdev_children
; i
++) {
2306 vdev_copy_path_search(stvd
, dvd
->vdev_child
[i
]);
2309 if (!dvd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(dvd
))
2313 * The idea here is that while a vdev can shift positions within
2314 * a top vdev (when replacing, attaching mirror, etc.) it cannot
2315 * step outside of it.
2317 vdev_t
*vd
= vdev_lookup_by_guid(stvd
, dvd
->vdev_guid
);
2319 if (vd
== NULL
|| vd
->vdev_ops
!= dvd
->vdev_ops
)
2322 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2324 vdev_copy_path_impl(vd
, dvd
);
2328 * Recursively copy vdev paths from one root vdev to another. Source and
2329 * destination vdev trees may differ in geometry. For each destination leaf
2330 * vdev, search a vdev with the same guid and top vdev id in the source.
2331 * Intended to copy paths from userland config into MOS config.
2334 vdev_copy_path_relaxed(vdev_t
*srvd
, vdev_t
*drvd
)
2336 uint64_t children
= MIN(srvd
->vdev_children
, drvd
->vdev_children
);
2337 ASSERT(srvd
->vdev_ops
== &vdev_root_ops
);
2338 ASSERT(drvd
->vdev_ops
== &vdev_root_ops
);
2340 for (uint64_t i
= 0; i
< children
; i
++) {
2341 vdev_copy_path_search(srvd
->vdev_child
[i
],
2342 drvd
->vdev_child
[i
]);
2347 * Close a virtual device.
2350 vdev_close(vdev_t
*vd
)
2352 vdev_t
*pvd
= vd
->vdev_parent
;
2353 spa_t
*spa __maybe_unused
= vd
->vdev_spa
;
2356 ASSERT(vd
->vdev_open_thread
== curthread
||
2357 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2360 * If our parent is reopening, then we are as well, unless we are
2363 if (pvd
!= NULL
&& pvd
->vdev_reopening
)
2364 vd
->vdev_reopening
= (pvd
->vdev_reopening
&& !vd
->vdev_offline
);
2366 vd
->vdev_ops
->vdev_op_close(vd
);
2368 vdev_cache_purge(vd
);
2371 * We record the previous state before we close it, so that if we are
2372 * doing a reopen(), we don't generate FMA ereports if we notice that
2373 * it's still faulted.
2375 vd
->vdev_prevstate
= vd
->vdev_state
;
2377 if (vd
->vdev_offline
)
2378 vd
->vdev_state
= VDEV_STATE_OFFLINE
;
2380 vd
->vdev_state
= VDEV_STATE_CLOSED
;
2381 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
2385 vdev_hold(vdev_t
*vd
)
2387 spa_t
*spa
= vd
->vdev_spa
;
2389 ASSERT(spa_is_root(spa
));
2390 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
2393 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2394 vdev_hold(vd
->vdev_child
[c
]);
2396 if (vd
->vdev_ops
->vdev_op_leaf
)
2397 vd
->vdev_ops
->vdev_op_hold(vd
);
2401 vdev_rele(vdev_t
*vd
)
2403 ASSERT(spa_is_root(vd
->vdev_spa
));
2404 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2405 vdev_rele(vd
->vdev_child
[c
]);
2407 if (vd
->vdev_ops
->vdev_op_leaf
)
2408 vd
->vdev_ops
->vdev_op_rele(vd
);
2412 * Reopen all interior vdevs and any unopened leaves. We don't actually
2413 * reopen leaf vdevs which had previously been opened as they might deadlock
2414 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2415 * If the leaf has never been opened then open it, as usual.
2418 vdev_reopen(vdev_t
*vd
)
2420 spa_t
*spa
= vd
->vdev_spa
;
2422 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2424 /* set the reopening flag unless we're taking the vdev offline */
2425 vd
->vdev_reopening
= !vd
->vdev_offline
;
2427 (void) vdev_open(vd
);
2430 * Call vdev_validate() here to make sure we have the same device.
2431 * Otherwise, a device with an invalid label could be successfully
2432 * opened in response to vdev_reopen().
2435 (void) vdev_validate_aux(vd
);
2436 if (vdev_readable(vd
) && vdev_writeable(vd
) &&
2437 vd
->vdev_aux
== &spa
->spa_l2cache
) {
2439 * In case the vdev is present we should evict all ARC
2440 * buffers and pointers to log blocks and reclaim their
2441 * space before restoring its contents to L2ARC.
2443 if (l2arc_vdev_present(vd
)) {
2444 l2arc_rebuild_vdev(vd
, B_TRUE
);
2446 l2arc_add_vdev(spa
, vd
);
2448 spa_async_request(spa
, SPA_ASYNC_L2CACHE_REBUILD
);
2449 spa_async_request(spa
, SPA_ASYNC_L2CACHE_TRIM
);
2452 (void) vdev_validate(vd
);
2456 * Reassess parent vdev's health.
2458 vdev_propagate_state(vd
);
2462 vdev_create(vdev_t
*vd
, uint64_t txg
, boolean_t isreplacing
)
2467 * Normally, partial opens (e.g. of a mirror) are allowed.
2468 * For a create, however, we want to fail the request if
2469 * there are any components we can't open.
2471 error
= vdev_open(vd
);
2473 if (error
|| vd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
2475 return (error
? error
: SET_ERROR(ENXIO
));
2479 * Recursively load DTLs and initialize all labels.
2481 if ((error
= vdev_dtl_load(vd
)) != 0 ||
2482 (error
= vdev_label_init(vd
, txg
, isreplacing
?
2483 VDEV_LABEL_REPLACE
: VDEV_LABEL_CREATE
)) != 0) {
2492 vdev_metaslab_set_size(vdev_t
*vd
)
2494 uint64_t asize
= vd
->vdev_asize
;
2495 uint64_t ms_count
= asize
>> zfs_vdev_default_ms_shift
;
2499 * There are two dimensions to the metaslab sizing calculation:
2500 * the size of the metaslab and the count of metaslabs per vdev.
2502 * The default values used below are a good balance between memory
2503 * usage (larger metaslab size means more memory needed for loaded
2504 * metaslabs; more metaslabs means more memory needed for the
2505 * metaslab_t structs), metaslab load time (larger metaslabs take
2506 * longer to load), and metaslab sync time (more metaslabs means
2507 * more time spent syncing all of them).
2509 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2510 * The range of the dimensions are as follows:
2512 * 2^29 <= ms_size <= 2^34
2513 * 16 <= ms_count <= 131,072
2515 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2516 * at least 512MB (2^29) to minimize fragmentation effects when
2517 * testing with smaller devices. However, the count constraint
2518 * of at least 16 metaslabs will override this minimum size goal.
2520 * On the upper end of vdev sizes, we aim for a maximum metaslab
2521 * size of 16GB. However, we will cap the total count to 2^17
2522 * metaslabs to keep our memory footprint in check and let the
2523 * metaslab size grow from there if that limit is hit.
2525 * The net effect of applying above constrains is summarized below.
2527 * vdev size metaslab count
2528 * --------------|-----------------
2530 * 8GB - 100GB one per 512MB
2532 * 3TB - 2PB one per 16GB
2534 * --------------------------------
2536 * Finally, note that all of the above calculate the initial
2537 * number of metaslabs. Expanding a top-level vdev will result
2538 * in additional metaslabs being allocated making it possible
2539 * to exceed the zfs_vdev_ms_count_limit.
2542 if (ms_count
< zfs_vdev_min_ms_count
)
2543 ms_shift
= highbit64(asize
/ zfs_vdev_min_ms_count
);
2544 else if (ms_count
> zfs_vdev_default_ms_count
)
2545 ms_shift
= highbit64(asize
/ zfs_vdev_default_ms_count
);
2547 ms_shift
= zfs_vdev_default_ms_shift
;
2549 if (ms_shift
< SPA_MAXBLOCKSHIFT
) {
2550 ms_shift
= SPA_MAXBLOCKSHIFT
;
2551 } else if (ms_shift
> zfs_vdev_max_ms_shift
) {
2552 ms_shift
= zfs_vdev_max_ms_shift
;
2553 /* cap the total count to constrain memory footprint */
2554 if ((asize
>> ms_shift
) > zfs_vdev_ms_count_limit
)
2555 ms_shift
= highbit64(asize
/ zfs_vdev_ms_count_limit
);
2558 vd
->vdev_ms_shift
= ms_shift
;
2559 ASSERT3U(vd
->vdev_ms_shift
, >=, SPA_MAXBLOCKSHIFT
);
2563 vdev_dirty(vdev_t
*vd
, int flags
, void *arg
, uint64_t txg
)
2565 ASSERT(vd
== vd
->vdev_top
);
2566 /* indirect vdevs don't have metaslabs or dtls */
2567 ASSERT(vdev_is_concrete(vd
) || flags
== 0);
2568 ASSERT(ISP2(flags
));
2569 ASSERT(spa_writeable(vd
->vdev_spa
));
2571 if (flags
& VDD_METASLAB
)
2572 (void) txg_list_add(&vd
->vdev_ms_list
, arg
, txg
);
2574 if (flags
& VDD_DTL
)
2575 (void) txg_list_add(&vd
->vdev_dtl_list
, arg
, txg
);
2577 (void) txg_list_add(&vd
->vdev_spa
->spa_vdev_txg_list
, vd
, txg
);
2581 vdev_dirty_leaves(vdev_t
*vd
, int flags
, uint64_t txg
)
2583 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2584 vdev_dirty_leaves(vd
->vdev_child
[c
], flags
, txg
);
2586 if (vd
->vdev_ops
->vdev_op_leaf
)
2587 vdev_dirty(vd
->vdev_top
, flags
, vd
, txg
);
2593 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2594 * the vdev has less than perfect replication. There are four kinds of DTL:
2596 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2598 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2600 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2601 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2602 * txgs that was scrubbed.
2604 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2605 * persistent errors or just some device being offline.
2606 * Unlike the other three, the DTL_OUTAGE map is not generally
2607 * maintained; it's only computed when needed, typically to
2608 * determine whether a device can be detached.
2610 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2611 * either has the data or it doesn't.
2613 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2614 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2615 * if any child is less than fully replicated, then so is its parent.
2616 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2617 * comprising only those txgs which appear in 'maxfaults' or more children;
2618 * those are the txgs we don't have enough replication to read. For example,
2619 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2620 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2621 * two child DTL_MISSING maps.
2623 * It should be clear from the above that to compute the DTLs and outage maps
2624 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2625 * Therefore, that is all we keep on disk. When loading the pool, or after
2626 * a configuration change, we generate all other DTLs from first principles.
2629 vdev_dtl_dirty(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2631 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2633 ASSERT(t
< DTL_TYPES
);
2634 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2635 ASSERT(spa_writeable(vd
->vdev_spa
));
2637 mutex_enter(&vd
->vdev_dtl_lock
);
2638 if (!range_tree_contains(rt
, txg
, size
))
2639 range_tree_add(rt
, txg
, size
);
2640 mutex_exit(&vd
->vdev_dtl_lock
);
2644 vdev_dtl_contains(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2646 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2647 boolean_t dirty
= B_FALSE
;
2649 ASSERT(t
< DTL_TYPES
);
2650 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2653 * While we are loading the pool, the DTLs have not been loaded yet.
2654 * This isn't a problem but it can result in devices being tried
2655 * which are known to not have the data. In which case, the import
2656 * is relying on the checksum to ensure that we get the right data.
2657 * Note that while importing we are only reading the MOS, which is
2658 * always checksummed.
2660 mutex_enter(&vd
->vdev_dtl_lock
);
2661 if (!range_tree_is_empty(rt
))
2662 dirty
= range_tree_contains(rt
, txg
, size
);
2663 mutex_exit(&vd
->vdev_dtl_lock
);
2669 vdev_dtl_empty(vdev_t
*vd
, vdev_dtl_type_t t
)
2671 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2674 mutex_enter(&vd
->vdev_dtl_lock
);
2675 empty
= range_tree_is_empty(rt
);
2676 mutex_exit(&vd
->vdev_dtl_lock
);
2682 * Check if the txg falls within the range which must be
2683 * resilvered. DVAs outside this range can always be skipped.
2686 vdev_default_need_resilver(vdev_t
*vd
, const dva_t
*dva
, size_t psize
,
2687 uint64_t phys_birth
)
2689 /* Set by sequential resilver. */
2690 if (phys_birth
== TXG_UNKNOWN
)
2693 return (vdev_dtl_contains(vd
, DTL_PARTIAL
, phys_birth
, 1));
2697 * Returns B_TRUE if the vdev determines the DVA needs to be resilvered.
2700 vdev_dtl_need_resilver(vdev_t
*vd
, const dva_t
*dva
, size_t psize
,
2701 uint64_t phys_birth
)
2703 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2705 if (vd
->vdev_ops
->vdev_op_need_resilver
== NULL
||
2706 vd
->vdev_ops
->vdev_op_leaf
)
2709 return (vd
->vdev_ops
->vdev_op_need_resilver(vd
, dva
, psize
,
2714 * Returns the lowest txg in the DTL range.
2717 vdev_dtl_min(vdev_t
*vd
)
2719 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2720 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2721 ASSERT0(vd
->vdev_children
);
2723 return (range_tree_min(vd
->vdev_dtl
[DTL_MISSING
]) - 1);
2727 * Returns the highest txg in the DTL.
2730 vdev_dtl_max(vdev_t
*vd
)
2732 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2733 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2734 ASSERT0(vd
->vdev_children
);
2736 return (range_tree_max(vd
->vdev_dtl
[DTL_MISSING
]));
2740 * Determine if a resilvering vdev should remove any DTL entries from
2741 * its range. If the vdev was resilvering for the entire duration of the
2742 * scan then it should excise that range from its DTLs. Otherwise, this
2743 * vdev is considered partially resilvered and should leave its DTL
2744 * entries intact. The comment in vdev_dtl_reassess() describes how we
2748 vdev_dtl_should_excise(vdev_t
*vd
, boolean_t rebuild_done
)
2750 ASSERT0(vd
->vdev_children
);
2752 if (vd
->vdev_state
< VDEV_STATE_DEGRADED
)
2755 if (vd
->vdev_resilver_deferred
)
2758 if (range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]))
2762 vdev_rebuild_t
*vr
= &vd
->vdev_top
->vdev_rebuild_config
;
2763 vdev_rebuild_phys_t
*vrp
= &vr
->vr_rebuild_phys
;
2765 /* Rebuild not initiated by attach */
2766 if (vd
->vdev_rebuild_txg
== 0)
2770 * When a rebuild completes without error then all missing data
2771 * up to the rebuild max txg has been reconstructed and the DTL
2772 * is eligible for excision.
2774 if (vrp
->vrp_rebuild_state
== VDEV_REBUILD_COMPLETE
&&
2775 vdev_dtl_max(vd
) <= vrp
->vrp_max_txg
) {
2776 ASSERT3U(vrp
->vrp_min_txg
, <=, vdev_dtl_min(vd
));
2777 ASSERT3U(vrp
->vrp_min_txg
, <, vd
->vdev_rebuild_txg
);
2778 ASSERT3U(vd
->vdev_rebuild_txg
, <=, vrp
->vrp_max_txg
);
2782 dsl_scan_t
*scn
= vd
->vdev_spa
->spa_dsl_pool
->dp_scan
;
2783 dsl_scan_phys_t
*scnp __maybe_unused
= &scn
->scn_phys
;
2785 /* Resilver not initiated by attach */
2786 if (vd
->vdev_resilver_txg
== 0)
2790 * When a resilver is initiated the scan will assign the
2791 * scn_max_txg value to the highest txg value that exists
2792 * in all DTLs. If this device's max DTL is not part of this
2793 * scan (i.e. it is not in the range (scn_min_txg, scn_max_txg]
2794 * then it is not eligible for excision.
2796 if (vdev_dtl_max(vd
) <= scn
->scn_phys
.scn_max_txg
) {
2797 ASSERT3U(scnp
->scn_min_txg
, <=, vdev_dtl_min(vd
));
2798 ASSERT3U(scnp
->scn_min_txg
, <, vd
->vdev_resilver_txg
);
2799 ASSERT3U(vd
->vdev_resilver_txg
, <=, scnp
->scn_max_txg
);
2808 * Reassess DTLs after a config change or scrub completion. If txg == 0 no
2809 * write operations will be issued to the pool.
2812 vdev_dtl_reassess(vdev_t
*vd
, uint64_t txg
, uint64_t scrub_txg
,
2813 boolean_t scrub_done
, boolean_t rebuild_done
)
2815 spa_t
*spa
= vd
->vdev_spa
;
2819 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
2821 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2822 vdev_dtl_reassess(vd
->vdev_child
[c
], txg
,
2823 scrub_txg
, scrub_done
, rebuild_done
);
2825 if (vd
== spa
->spa_root_vdev
|| !vdev_is_concrete(vd
) || vd
->vdev_aux
)
2828 if (vd
->vdev_ops
->vdev_op_leaf
) {
2829 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
2830 vdev_rebuild_t
*vr
= &vd
->vdev_top
->vdev_rebuild_config
;
2831 boolean_t check_excise
= B_FALSE
;
2832 boolean_t wasempty
= B_TRUE
;
2834 mutex_enter(&vd
->vdev_dtl_lock
);
2837 * If requested, pretend the scan or rebuild completed cleanly.
2839 if (zfs_scan_ignore_errors
) {
2841 scn
->scn_phys
.scn_errors
= 0;
2843 vr
->vr_rebuild_phys
.vrp_errors
= 0;
2846 if (scrub_txg
!= 0 &&
2847 !range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
])) {
2849 zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d "
2850 "dtl:%llu/%llu errors:%llu",
2851 (u_longlong_t
)vd
->vdev_guid
, (u_longlong_t
)txg
,
2852 (u_longlong_t
)scrub_txg
, spa
->spa_scrub_started
,
2853 (u_longlong_t
)vdev_dtl_min(vd
),
2854 (u_longlong_t
)vdev_dtl_max(vd
),
2855 (u_longlong_t
)(scn
? scn
->scn_phys
.scn_errors
: 0));
2859 * If we've completed a scrub/resilver or a rebuild cleanly
2860 * then determine if this vdev should remove any DTLs. We
2861 * only want to excise regions on vdevs that were available
2862 * during the entire duration of this scan.
2865 vr
!= NULL
&& vr
->vr_rebuild_phys
.vrp_errors
== 0) {
2866 check_excise
= B_TRUE
;
2868 if (spa
->spa_scrub_started
||
2869 (scn
!= NULL
&& scn
->scn_phys
.scn_errors
== 0)) {
2870 check_excise
= B_TRUE
;
2874 if (scrub_txg
&& check_excise
&&
2875 vdev_dtl_should_excise(vd
, rebuild_done
)) {
2877 * We completed a scrub, resilver or rebuild up to
2878 * scrub_txg. If we did it without rebooting, then
2879 * the scrub dtl will be valid, so excise the old
2880 * region and fold in the scrub dtl. Otherwise,
2881 * leave the dtl as-is if there was an error.
2883 * There's little trick here: to excise the beginning
2884 * of the DTL_MISSING map, we put it into a reference
2885 * tree and then add a segment with refcnt -1 that
2886 * covers the range [0, scrub_txg). This means
2887 * that each txg in that range has refcnt -1 or 0.
2888 * We then add DTL_SCRUB with a refcnt of 2, so that
2889 * entries in the range [0, scrub_txg) will have a
2890 * positive refcnt -- either 1 or 2. We then convert
2891 * the reference tree into the new DTL_MISSING map.
2893 space_reftree_create(&reftree
);
2894 space_reftree_add_map(&reftree
,
2895 vd
->vdev_dtl
[DTL_MISSING
], 1);
2896 space_reftree_add_seg(&reftree
, 0, scrub_txg
, -1);
2897 space_reftree_add_map(&reftree
,
2898 vd
->vdev_dtl
[DTL_SCRUB
], 2);
2899 space_reftree_generate_map(&reftree
,
2900 vd
->vdev_dtl
[DTL_MISSING
], 1);
2901 space_reftree_destroy(&reftree
);
2903 if (!range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
])) {
2904 zfs_dbgmsg("update DTL_MISSING:%llu/%llu",
2905 (u_longlong_t
)vdev_dtl_min(vd
),
2906 (u_longlong_t
)vdev_dtl_max(vd
));
2907 } else if (!wasempty
) {
2908 zfs_dbgmsg("DTL_MISSING is now empty");
2911 range_tree_vacate(vd
->vdev_dtl
[DTL_PARTIAL
], NULL
, NULL
);
2912 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2913 range_tree_add
, vd
->vdev_dtl
[DTL_PARTIAL
]);
2915 range_tree_vacate(vd
->vdev_dtl
[DTL_SCRUB
], NULL
, NULL
);
2916 range_tree_vacate(vd
->vdev_dtl
[DTL_OUTAGE
], NULL
, NULL
);
2917 if (!vdev_readable(vd
))
2918 range_tree_add(vd
->vdev_dtl
[DTL_OUTAGE
], 0, -1ULL);
2920 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2921 range_tree_add
, vd
->vdev_dtl
[DTL_OUTAGE
]);
2924 * If the vdev was resilvering or rebuilding and no longer
2925 * has any DTLs then reset the appropriate flag and dirty
2926 * the top level so that we persist the change.
2929 range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]) &&
2930 range_tree_is_empty(vd
->vdev_dtl
[DTL_OUTAGE
])) {
2931 if (vd
->vdev_rebuild_txg
!= 0) {
2932 vd
->vdev_rebuild_txg
= 0;
2933 vdev_config_dirty(vd
->vdev_top
);
2934 } else if (vd
->vdev_resilver_txg
!= 0) {
2935 vd
->vdev_resilver_txg
= 0;
2936 vdev_config_dirty(vd
->vdev_top
);
2940 mutex_exit(&vd
->vdev_dtl_lock
);
2943 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, txg
);
2947 mutex_enter(&vd
->vdev_dtl_lock
);
2948 for (int t
= 0; t
< DTL_TYPES
; t
++) {
2949 /* account for child's outage in parent's missing map */
2950 int s
= (t
== DTL_MISSING
) ? DTL_OUTAGE
: t
;
2952 continue; /* leaf vdevs only */
2953 if (t
== DTL_PARTIAL
)
2954 minref
= 1; /* i.e. non-zero */
2955 else if (vdev_get_nparity(vd
) != 0)
2956 minref
= vdev_get_nparity(vd
) + 1; /* RAID-Z, dRAID */
2958 minref
= vd
->vdev_children
; /* any kind of mirror */
2959 space_reftree_create(&reftree
);
2960 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2961 vdev_t
*cvd
= vd
->vdev_child
[c
];
2962 mutex_enter(&cvd
->vdev_dtl_lock
);
2963 space_reftree_add_map(&reftree
, cvd
->vdev_dtl
[s
], 1);
2964 mutex_exit(&cvd
->vdev_dtl_lock
);
2966 space_reftree_generate_map(&reftree
, vd
->vdev_dtl
[t
], minref
);
2967 space_reftree_destroy(&reftree
);
2969 mutex_exit(&vd
->vdev_dtl_lock
);
2973 vdev_dtl_load(vdev_t
*vd
)
2975 spa_t
*spa
= vd
->vdev_spa
;
2976 objset_t
*mos
= spa
->spa_meta_objset
;
2980 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_dtl_object
!= 0) {
2981 ASSERT(vdev_is_concrete(vd
));
2983 error
= space_map_open(&vd
->vdev_dtl_sm
, mos
,
2984 vd
->vdev_dtl_object
, 0, -1ULL, 0);
2987 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2989 rt
= range_tree_create(NULL
, RANGE_SEG64
, NULL
, 0, 0);
2990 error
= space_map_load(vd
->vdev_dtl_sm
, rt
, SM_ALLOC
);
2992 mutex_enter(&vd
->vdev_dtl_lock
);
2993 range_tree_walk(rt
, range_tree_add
,
2994 vd
->vdev_dtl
[DTL_MISSING
]);
2995 mutex_exit(&vd
->vdev_dtl_lock
);
2998 range_tree_vacate(rt
, NULL
, NULL
);
2999 range_tree_destroy(rt
);
3004 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3005 error
= vdev_dtl_load(vd
->vdev_child
[c
]);
3014 vdev_zap_allocation_data(vdev_t
*vd
, dmu_tx_t
*tx
)
3016 spa_t
*spa
= vd
->vdev_spa
;
3017 objset_t
*mos
= spa
->spa_meta_objset
;
3018 vdev_alloc_bias_t alloc_bias
= vd
->vdev_alloc_bias
;
3021 ASSERT(alloc_bias
!= VDEV_BIAS_NONE
);
3024 (alloc_bias
== VDEV_BIAS_LOG
) ? VDEV_ALLOC_BIAS_LOG
:
3025 (alloc_bias
== VDEV_BIAS_SPECIAL
) ? VDEV_ALLOC_BIAS_SPECIAL
:
3026 (alloc_bias
== VDEV_BIAS_DEDUP
) ? VDEV_ALLOC_BIAS_DEDUP
: NULL
;
3028 ASSERT(string
!= NULL
);
3029 VERIFY0(zap_add(mos
, vd
->vdev_top_zap
, VDEV_TOP_ZAP_ALLOCATION_BIAS
,
3030 1, strlen(string
) + 1, string
, tx
));
3032 if (alloc_bias
== VDEV_BIAS_SPECIAL
|| alloc_bias
== VDEV_BIAS_DEDUP
) {
3033 spa_activate_allocation_classes(spa
, tx
);
3038 vdev_destroy_unlink_zap(vdev_t
*vd
, uint64_t zapobj
, dmu_tx_t
*tx
)
3040 spa_t
*spa
= vd
->vdev_spa
;
3042 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zapobj
, tx
));
3043 VERIFY0(zap_remove_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
3048 vdev_create_link_zap(vdev_t
*vd
, dmu_tx_t
*tx
)
3050 spa_t
*spa
= vd
->vdev_spa
;
3051 uint64_t zap
= zap_create(spa
->spa_meta_objset
, DMU_OTN_ZAP_METADATA
,
3052 DMU_OT_NONE
, 0, tx
);
3055 VERIFY0(zap_add_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
3062 vdev_construct_zaps(vdev_t
*vd
, dmu_tx_t
*tx
)
3064 if (vd
->vdev_ops
!= &vdev_hole_ops
&&
3065 vd
->vdev_ops
!= &vdev_missing_ops
&&
3066 vd
->vdev_ops
!= &vdev_root_ops
&&
3067 !vd
->vdev_top
->vdev_removing
) {
3068 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_leaf_zap
== 0) {
3069 vd
->vdev_leaf_zap
= vdev_create_link_zap(vd
, tx
);
3071 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
== 0) {
3072 vd
->vdev_top_zap
= vdev_create_link_zap(vd
, tx
);
3073 if (vd
->vdev_alloc_bias
!= VDEV_BIAS_NONE
)
3074 vdev_zap_allocation_data(vd
, tx
);
3078 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
3079 vdev_construct_zaps(vd
->vdev_child
[i
], tx
);
3084 vdev_dtl_sync(vdev_t
*vd
, uint64_t txg
)
3086 spa_t
*spa
= vd
->vdev_spa
;
3087 range_tree_t
*rt
= vd
->vdev_dtl
[DTL_MISSING
];
3088 objset_t
*mos
= spa
->spa_meta_objset
;
3089 range_tree_t
*rtsync
;
3091 uint64_t object
= space_map_object(vd
->vdev_dtl_sm
);
3093 ASSERT(vdev_is_concrete(vd
));
3094 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3096 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
3098 if (vd
->vdev_detached
|| vd
->vdev_top
->vdev_removing
) {
3099 mutex_enter(&vd
->vdev_dtl_lock
);
3100 space_map_free(vd
->vdev_dtl_sm
, tx
);
3101 space_map_close(vd
->vdev_dtl_sm
);
3102 vd
->vdev_dtl_sm
= NULL
;
3103 mutex_exit(&vd
->vdev_dtl_lock
);
3106 * We only destroy the leaf ZAP for detached leaves or for
3107 * removed log devices. Removed data devices handle leaf ZAP
3108 * cleanup later, once cancellation is no longer possible.
3110 if (vd
->vdev_leaf_zap
!= 0 && (vd
->vdev_detached
||
3111 vd
->vdev_top
->vdev_islog
)) {
3112 vdev_destroy_unlink_zap(vd
, vd
->vdev_leaf_zap
, tx
);
3113 vd
->vdev_leaf_zap
= 0;
3120 if (vd
->vdev_dtl_sm
== NULL
) {
3121 uint64_t new_object
;
3123 new_object
= space_map_alloc(mos
, zfs_vdev_dtl_sm_blksz
, tx
);
3124 VERIFY3U(new_object
, !=, 0);
3126 VERIFY0(space_map_open(&vd
->vdev_dtl_sm
, mos
, new_object
,
3128 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
3131 rtsync
= range_tree_create(NULL
, RANGE_SEG64
, NULL
, 0, 0);
3133 mutex_enter(&vd
->vdev_dtl_lock
);
3134 range_tree_walk(rt
, range_tree_add
, rtsync
);
3135 mutex_exit(&vd
->vdev_dtl_lock
);
3137 space_map_truncate(vd
->vdev_dtl_sm
, zfs_vdev_dtl_sm_blksz
, tx
);
3138 space_map_write(vd
->vdev_dtl_sm
, rtsync
, SM_ALLOC
, SM_NO_VDEVID
, tx
);
3139 range_tree_vacate(rtsync
, NULL
, NULL
);
3141 range_tree_destroy(rtsync
);
3144 * If the object for the space map has changed then dirty
3145 * the top level so that we update the config.
3147 if (object
!= space_map_object(vd
->vdev_dtl_sm
)) {
3148 vdev_dbgmsg(vd
, "txg %llu, spa %s, DTL old object %llu, "
3149 "new object %llu", (u_longlong_t
)txg
, spa_name(spa
),
3150 (u_longlong_t
)object
,
3151 (u_longlong_t
)space_map_object(vd
->vdev_dtl_sm
));
3152 vdev_config_dirty(vd
->vdev_top
);
3159 * Determine whether the specified vdev can be offlined/detached/removed
3160 * without losing data.
3163 vdev_dtl_required(vdev_t
*vd
)
3165 spa_t
*spa
= vd
->vdev_spa
;
3166 vdev_t
*tvd
= vd
->vdev_top
;
3167 uint8_t cant_read
= vd
->vdev_cant_read
;
3170 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
3172 if (vd
== spa
->spa_root_vdev
|| vd
== tvd
)
3176 * Temporarily mark the device as unreadable, and then determine
3177 * whether this results in any DTL outages in the top-level vdev.
3178 * If not, we can safely offline/detach/remove the device.
3180 vd
->vdev_cant_read
= B_TRUE
;
3181 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
, B_FALSE
);
3182 required
= !vdev_dtl_empty(tvd
, DTL_OUTAGE
);
3183 vd
->vdev_cant_read
= cant_read
;
3184 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
, B_FALSE
);
3186 if (!required
&& zio_injection_enabled
) {
3187 required
= !!zio_handle_device_injection(vd
, NULL
,
3195 * Determine if resilver is needed, and if so the txg range.
3198 vdev_resilver_needed(vdev_t
*vd
, uint64_t *minp
, uint64_t *maxp
)
3200 boolean_t needed
= B_FALSE
;
3201 uint64_t thismin
= UINT64_MAX
;
3202 uint64_t thismax
= 0;
3204 if (vd
->vdev_children
== 0) {
3205 mutex_enter(&vd
->vdev_dtl_lock
);
3206 if (!range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]) &&
3207 vdev_writeable(vd
)) {
3209 thismin
= vdev_dtl_min(vd
);
3210 thismax
= vdev_dtl_max(vd
);
3213 mutex_exit(&vd
->vdev_dtl_lock
);
3215 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3216 vdev_t
*cvd
= vd
->vdev_child
[c
];
3217 uint64_t cmin
, cmax
;
3219 if (vdev_resilver_needed(cvd
, &cmin
, &cmax
)) {
3220 thismin
= MIN(thismin
, cmin
);
3221 thismax
= MAX(thismax
, cmax
);
3227 if (needed
&& minp
) {
3235 * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj
3236 * will contain either the checkpoint spacemap object or zero if none exists.
3237 * All other errors are returned to the caller.
3240 vdev_checkpoint_sm_object(vdev_t
*vd
, uint64_t *sm_obj
)
3242 ASSERT0(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
));
3244 if (vd
->vdev_top_zap
== 0) {
3249 int error
= zap_lookup(spa_meta_objset(vd
->vdev_spa
), vd
->vdev_top_zap
,
3250 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM
, sizeof (uint64_t), 1, sm_obj
);
3251 if (error
== ENOENT
) {
3260 vdev_load(vdev_t
*vd
)
3265 * Recursively load all children.
3267 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3268 error
= vdev_load(vd
->vdev_child
[c
]);
3274 vdev_set_deflate_ratio(vd
);
3277 * On spa_load path, grab the allocation bias from our zap
3279 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
!= 0) {
3280 spa_t
*spa
= vd
->vdev_spa
;
3283 error
= zap_lookup(spa
->spa_meta_objset
, vd
->vdev_top_zap
,
3284 VDEV_TOP_ZAP_ALLOCATION_BIAS
, 1, sizeof (bias_str
),
3287 ASSERT(vd
->vdev_alloc_bias
== VDEV_BIAS_NONE
);
3288 vd
->vdev_alloc_bias
= vdev_derive_alloc_bias(bias_str
);
3289 } else if (error
!= ENOENT
) {
3290 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3291 VDEV_AUX_CORRUPT_DATA
);
3292 vdev_dbgmsg(vd
, "vdev_load: zap_lookup(top_zap=%llu) "
3293 "failed [error=%d]", vd
->vdev_top_zap
, error
);
3299 * Load any rebuild state from the top-level vdev zap.
3301 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
!= 0) {
3302 error
= vdev_rebuild_load(vd
);
3303 if (error
&& error
!= ENOTSUP
) {
3304 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3305 VDEV_AUX_CORRUPT_DATA
);
3306 vdev_dbgmsg(vd
, "vdev_load: vdev_rebuild_load "
3307 "failed [error=%d]", error
);
3313 * If this is a top-level vdev, initialize its metaslabs.
3315 if (vd
== vd
->vdev_top
&& vdev_is_concrete(vd
)) {
3316 vdev_metaslab_group_create(vd
);
3318 if (vd
->vdev_ashift
== 0 || vd
->vdev_asize
== 0) {
3319 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3320 VDEV_AUX_CORRUPT_DATA
);
3321 vdev_dbgmsg(vd
, "vdev_load: invalid size. ashift=%llu, "
3322 "asize=%llu", (u_longlong_t
)vd
->vdev_ashift
,
3323 (u_longlong_t
)vd
->vdev_asize
);
3324 return (SET_ERROR(ENXIO
));
3327 error
= vdev_metaslab_init(vd
, 0);
3329 vdev_dbgmsg(vd
, "vdev_load: metaslab_init failed "
3330 "[error=%d]", error
);
3331 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3332 VDEV_AUX_CORRUPT_DATA
);
3336 uint64_t checkpoint_sm_obj
;
3337 error
= vdev_checkpoint_sm_object(vd
, &checkpoint_sm_obj
);
3338 if (error
== 0 && checkpoint_sm_obj
!= 0) {
3339 objset_t
*mos
= spa_meta_objset(vd
->vdev_spa
);
3340 ASSERT(vd
->vdev_asize
!= 0);
3341 ASSERT3P(vd
->vdev_checkpoint_sm
, ==, NULL
);
3343 error
= space_map_open(&vd
->vdev_checkpoint_sm
,
3344 mos
, checkpoint_sm_obj
, 0, vd
->vdev_asize
,
3347 vdev_dbgmsg(vd
, "vdev_load: space_map_open "
3348 "failed for checkpoint spacemap (obj %llu) "
3350 (u_longlong_t
)checkpoint_sm_obj
, error
);
3353 ASSERT3P(vd
->vdev_checkpoint_sm
, !=, NULL
);
3356 * Since the checkpoint_sm contains free entries
3357 * exclusively we can use space_map_allocated() to
3358 * indicate the cumulative checkpointed space that
3361 vd
->vdev_stat
.vs_checkpoint_space
=
3362 -space_map_allocated(vd
->vdev_checkpoint_sm
);
3363 vd
->vdev_spa
->spa_checkpoint_info
.sci_dspace
+=
3364 vd
->vdev_stat
.vs_checkpoint_space
;
3365 } else if (error
!= 0) {
3366 vdev_dbgmsg(vd
, "vdev_load: failed to retrieve "
3367 "checkpoint space map object from vdev ZAP "
3368 "[error=%d]", error
);
3374 * If this is a leaf vdev, load its DTL.
3376 if (vd
->vdev_ops
->vdev_op_leaf
&& (error
= vdev_dtl_load(vd
)) != 0) {
3377 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3378 VDEV_AUX_CORRUPT_DATA
);
3379 vdev_dbgmsg(vd
, "vdev_load: vdev_dtl_load failed "
3380 "[error=%d]", error
);
3384 uint64_t obsolete_sm_object
;
3385 error
= vdev_obsolete_sm_object(vd
, &obsolete_sm_object
);
3386 if (error
== 0 && obsolete_sm_object
!= 0) {
3387 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
3388 ASSERT(vd
->vdev_asize
!= 0);
3389 ASSERT3P(vd
->vdev_obsolete_sm
, ==, NULL
);
3391 if ((error
= space_map_open(&vd
->vdev_obsolete_sm
, mos
,
3392 obsolete_sm_object
, 0, vd
->vdev_asize
, 0))) {
3393 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3394 VDEV_AUX_CORRUPT_DATA
);
3395 vdev_dbgmsg(vd
, "vdev_load: space_map_open failed for "
3396 "obsolete spacemap (obj %llu) [error=%d]",
3397 (u_longlong_t
)obsolete_sm_object
, error
);
3400 } else if (error
!= 0) {
3401 vdev_dbgmsg(vd
, "vdev_load: failed to retrieve obsolete "
3402 "space map object from vdev ZAP [error=%d]", error
);
3410 * The special vdev case is used for hot spares and l2cache devices. Its
3411 * sole purpose it to set the vdev state for the associated vdev. To do this,
3412 * we make sure that we can open the underlying device, then try to read the
3413 * label, and make sure that the label is sane and that it hasn't been
3414 * repurposed to another pool.
3417 vdev_validate_aux(vdev_t
*vd
)
3420 uint64_t guid
, version
;
3423 if (!vdev_readable(vd
))
3426 if ((label
= vdev_label_read_config(vd
, -1ULL)) == NULL
) {
3427 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
3428 VDEV_AUX_CORRUPT_DATA
);
3432 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_VERSION
, &version
) != 0 ||
3433 !SPA_VERSION_IS_SUPPORTED(version
) ||
3434 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0 ||
3435 guid
!= vd
->vdev_guid
||
3436 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
, &state
) != 0) {
3437 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
3438 VDEV_AUX_CORRUPT_DATA
);
3444 * We don't actually check the pool state here. If it's in fact in
3445 * use by another pool, we update this fact on the fly when requested.
3452 vdev_destroy_ms_flush_data(vdev_t
*vd
, dmu_tx_t
*tx
)
3454 objset_t
*mos
= spa_meta_objset(vd
->vdev_spa
);
3456 if (vd
->vdev_top_zap
== 0)
3459 uint64_t object
= 0;
3460 int err
= zap_lookup(mos
, vd
->vdev_top_zap
,
3461 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS
, sizeof (uint64_t), 1, &object
);
3466 VERIFY0(dmu_object_free(mos
, object
, tx
));
3467 VERIFY0(zap_remove(mos
, vd
->vdev_top_zap
,
3468 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS
, tx
));
3472 * Free the objects used to store this vdev's spacemaps, and the array
3473 * that points to them.
3476 vdev_destroy_spacemaps(vdev_t
*vd
, dmu_tx_t
*tx
)
3478 if (vd
->vdev_ms_array
== 0)
3481 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
3482 uint64_t array_count
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
3483 size_t array_bytes
= array_count
* sizeof (uint64_t);
3484 uint64_t *smobj_array
= kmem_alloc(array_bytes
, KM_SLEEP
);
3485 VERIFY0(dmu_read(mos
, vd
->vdev_ms_array
, 0,
3486 array_bytes
, smobj_array
, 0));
3488 for (uint64_t i
= 0; i
< array_count
; i
++) {
3489 uint64_t smobj
= smobj_array
[i
];
3493 space_map_free_obj(mos
, smobj
, tx
);
3496 kmem_free(smobj_array
, array_bytes
);
3497 VERIFY0(dmu_object_free(mos
, vd
->vdev_ms_array
, tx
));
3498 vdev_destroy_ms_flush_data(vd
, tx
);
3499 vd
->vdev_ms_array
= 0;
3503 vdev_remove_empty_log(vdev_t
*vd
, uint64_t txg
)
3505 spa_t
*spa
= vd
->vdev_spa
;
3507 ASSERT(vd
->vdev_islog
);
3508 ASSERT(vd
== vd
->vdev_top
);
3509 ASSERT3U(txg
, ==, spa_syncing_txg(spa
));
3511 dmu_tx_t
*tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
3513 vdev_destroy_spacemaps(vd
, tx
);
3514 if (vd
->vdev_top_zap
!= 0) {
3515 vdev_destroy_unlink_zap(vd
, vd
->vdev_top_zap
, tx
);
3516 vd
->vdev_top_zap
= 0;
3523 vdev_sync_done(vdev_t
*vd
, uint64_t txg
)
3526 boolean_t reassess
= !txg_list_empty(&vd
->vdev_ms_list
, TXG_CLEAN(txg
));
3528 ASSERT(vdev_is_concrete(vd
));
3530 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, TXG_CLEAN(txg
)))
3532 metaslab_sync_done(msp
, txg
);
3535 metaslab_sync_reassess(vd
->vdev_mg
);
3539 vdev_sync(vdev_t
*vd
, uint64_t txg
)
3541 spa_t
*spa
= vd
->vdev_spa
;
3545 ASSERT3U(txg
, ==, spa
->spa_syncing_txg
);
3546 dmu_tx_t
*tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
3547 if (range_tree_space(vd
->vdev_obsolete_segments
) > 0) {
3548 ASSERT(vd
->vdev_removing
||
3549 vd
->vdev_ops
== &vdev_indirect_ops
);
3551 vdev_indirect_sync_obsolete(vd
, tx
);
3554 * If the vdev is indirect, it can't have dirty
3555 * metaslabs or DTLs.
3557 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
3558 ASSERT(txg_list_empty(&vd
->vdev_ms_list
, txg
));
3559 ASSERT(txg_list_empty(&vd
->vdev_dtl_list
, txg
));
3565 ASSERT(vdev_is_concrete(vd
));
3567 if (vd
->vdev_ms_array
== 0 && vd
->vdev_ms_shift
!= 0 &&
3568 !vd
->vdev_removing
) {
3569 ASSERT(vd
== vd
->vdev_top
);
3570 ASSERT0(vd
->vdev_indirect_config
.vic_mapping_object
);
3571 vd
->vdev_ms_array
= dmu_object_alloc(spa
->spa_meta_objset
,
3572 DMU_OT_OBJECT_ARRAY
, 0, DMU_OT_NONE
, 0, tx
);
3573 ASSERT(vd
->vdev_ms_array
!= 0);
3574 vdev_config_dirty(vd
);
3577 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, txg
)) != NULL
) {
3578 metaslab_sync(msp
, txg
);
3579 (void) txg_list_add(&vd
->vdev_ms_list
, msp
, TXG_CLEAN(txg
));
3582 while ((lvd
= txg_list_remove(&vd
->vdev_dtl_list
, txg
)) != NULL
)
3583 vdev_dtl_sync(lvd
, txg
);
3586 * If this is an empty log device being removed, destroy the
3587 * metadata associated with it.
3589 if (vd
->vdev_islog
&& vd
->vdev_stat
.vs_alloc
== 0 && vd
->vdev_removing
)
3590 vdev_remove_empty_log(vd
, txg
);
3592 (void) txg_list_add(&spa
->spa_vdev_txg_list
, vd
, TXG_CLEAN(txg
));
3597 vdev_psize_to_asize(vdev_t
*vd
, uint64_t psize
)
3599 return (vd
->vdev_ops
->vdev_op_asize(vd
, psize
));
3603 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
3604 * not be opened, and no I/O is attempted.
3607 vdev_fault(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
3611 spa_vdev_state_enter(spa
, SCL_NONE
);
3613 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3614 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
3616 if (!vd
->vdev_ops
->vdev_op_leaf
)
3617 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
3622 * If user did a 'zpool offline -f' then make the fault persist across
3625 if (aux
== VDEV_AUX_EXTERNAL_PERSIST
) {
3627 * There are two kinds of forced faults: temporary and
3628 * persistent. Temporary faults go away at pool import, while
3629 * persistent faults stay set. Both types of faults can be
3630 * cleared with a zpool clear.
3632 * We tell if a vdev is persistently faulted by looking at the
3633 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
3634 * import then it's a persistent fault. Otherwise, it's
3635 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
3636 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
3637 * tells vdev_config_generate() (which gets run later) to set
3638 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
3640 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
3641 vd
->vdev_tmpoffline
= B_FALSE
;
3642 aux
= VDEV_AUX_EXTERNAL
;
3644 vd
->vdev_tmpoffline
= B_TRUE
;
3648 * We don't directly use the aux state here, but if we do a
3649 * vdev_reopen(), we need this value to be present to remember why we
3652 vd
->vdev_label_aux
= aux
;
3655 * Faulted state takes precedence over degraded.
3657 vd
->vdev_delayed_close
= B_FALSE
;
3658 vd
->vdev_faulted
= 1ULL;
3659 vd
->vdev_degraded
= 0ULL;
3660 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_FAULTED
, aux
);
3663 * If this device has the only valid copy of the data, then
3664 * back off and simply mark the vdev as degraded instead.
3666 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&& vdev_dtl_required(vd
)) {
3667 vd
->vdev_degraded
= 1ULL;
3668 vd
->vdev_faulted
= 0ULL;
3671 * If we reopen the device and it's not dead, only then do we
3676 if (vdev_readable(vd
))
3677 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
, aux
);
3680 return (spa_vdev_state_exit(spa
, vd
, 0));
3684 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
3685 * user that something is wrong. The vdev continues to operate as normal as far
3686 * as I/O is concerned.
3689 vdev_degrade(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
3693 spa_vdev_state_enter(spa
, SCL_NONE
);
3695 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3696 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
3698 if (!vd
->vdev_ops
->vdev_op_leaf
)
3699 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
3702 * If the vdev is already faulted, then don't do anything.
3704 if (vd
->vdev_faulted
|| vd
->vdev_degraded
)
3705 return (spa_vdev_state_exit(spa
, NULL
, 0));
3707 vd
->vdev_degraded
= 1ULL;
3708 if (!vdev_is_dead(vd
))
3709 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
,
3712 return (spa_vdev_state_exit(spa
, vd
, 0));
3716 * Online the given vdev.
3718 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
3719 * spare device should be detached when the device finishes resilvering.
3720 * Second, the online should be treated like a 'test' online case, so no FMA
3721 * events are generated if the device fails to open.
3724 vdev_online(spa_t
*spa
, uint64_t guid
, uint64_t flags
, vdev_state_t
*newstate
)
3726 vdev_t
*vd
, *tvd
, *pvd
, *rvd
= spa
->spa_root_vdev
;
3727 boolean_t wasoffline
;
3728 vdev_state_t oldstate
;
3730 spa_vdev_state_enter(spa
, SCL_NONE
);
3732 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3733 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
3735 if (!vd
->vdev_ops
->vdev_op_leaf
)
3736 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
3738 wasoffline
= (vd
->vdev_offline
|| vd
->vdev_tmpoffline
);
3739 oldstate
= vd
->vdev_state
;
3742 vd
->vdev_offline
= B_FALSE
;
3743 vd
->vdev_tmpoffline
= B_FALSE
;
3744 vd
->vdev_checkremove
= !!(flags
& ZFS_ONLINE_CHECKREMOVE
);
3745 vd
->vdev_forcefault
= !!(flags
& ZFS_ONLINE_FORCEFAULT
);
3747 /* XXX - L2ARC 1.0 does not support expansion */
3748 if (!vd
->vdev_aux
) {
3749 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3750 pvd
->vdev_expanding
= !!((flags
& ZFS_ONLINE_EXPAND
) ||
3751 spa
->spa_autoexpand
);
3752 vd
->vdev_expansion_time
= gethrestime_sec();
3756 vd
->vdev_checkremove
= vd
->vdev_forcefault
= B_FALSE
;
3758 if (!vd
->vdev_aux
) {
3759 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3760 pvd
->vdev_expanding
= B_FALSE
;
3764 *newstate
= vd
->vdev_state
;
3765 if ((flags
& ZFS_ONLINE_UNSPARE
) &&
3766 !vdev_is_dead(vd
) && vd
->vdev_parent
&&
3767 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3768 vd
->vdev_parent
->vdev_child
[0] == vd
)
3769 vd
->vdev_unspare
= B_TRUE
;
3771 if ((flags
& ZFS_ONLINE_EXPAND
) || spa
->spa_autoexpand
) {
3773 /* XXX - L2ARC 1.0 does not support expansion */
3775 return (spa_vdev_state_exit(spa
, vd
, ENOTSUP
));
3776 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3779 /* Restart initializing if necessary */
3780 mutex_enter(&vd
->vdev_initialize_lock
);
3781 if (vdev_writeable(vd
) &&
3782 vd
->vdev_initialize_thread
== NULL
&&
3783 vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
3784 (void) vdev_initialize(vd
);
3786 mutex_exit(&vd
->vdev_initialize_lock
);
3789 * Restart trimming if necessary. We do not restart trimming for cache
3790 * devices here. This is triggered by l2arc_rebuild_vdev()
3791 * asynchronously for the whole device or in l2arc_evict() as it evicts
3792 * space for upcoming writes.
3794 mutex_enter(&vd
->vdev_trim_lock
);
3795 if (vdev_writeable(vd
) && !vd
->vdev_isl2cache
&&
3796 vd
->vdev_trim_thread
== NULL
&&
3797 vd
->vdev_trim_state
== VDEV_TRIM_ACTIVE
) {
3798 (void) vdev_trim(vd
, vd
->vdev_trim_rate
, vd
->vdev_trim_partial
,
3799 vd
->vdev_trim_secure
);
3801 mutex_exit(&vd
->vdev_trim_lock
);
3804 (oldstate
< VDEV_STATE_DEGRADED
&&
3805 vd
->vdev_state
>= VDEV_STATE_DEGRADED
))
3806 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_ONLINE
);
3808 return (spa_vdev_state_exit(spa
, vd
, 0));
3812 vdev_offline_locked(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
3816 uint64_t generation
;
3817 metaslab_group_t
*mg
;
3820 spa_vdev_state_enter(spa
, SCL_ALLOC
);
3822 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3823 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
3825 if (!vd
->vdev_ops
->vdev_op_leaf
)
3826 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
3828 if (vd
->vdev_ops
== &vdev_draid_spare_ops
)
3829 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3833 generation
= spa
->spa_config_generation
+ 1;
3836 * If the device isn't already offline, try to offline it.
3838 if (!vd
->vdev_offline
) {
3840 * If this device has the only valid copy of some data,
3841 * don't allow it to be offlined. Log devices are always
3844 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
3845 vdev_dtl_required(vd
))
3846 return (spa_vdev_state_exit(spa
, NULL
,
3850 * If the top-level is a slog and it has had allocations
3851 * then proceed. We check that the vdev's metaslab group
3852 * is not NULL since it's possible that we may have just
3853 * added this vdev but not yet initialized its metaslabs.
3855 if (tvd
->vdev_islog
&& mg
!= NULL
) {
3857 * Prevent any future allocations.
3859 metaslab_group_passivate(mg
);
3860 (void) spa_vdev_state_exit(spa
, vd
, 0);
3862 error
= spa_reset_logs(spa
);
3865 * If the log device was successfully reset but has
3866 * checkpointed data, do not offline it.
3869 tvd
->vdev_checkpoint_sm
!= NULL
) {
3870 ASSERT3U(space_map_allocated(
3871 tvd
->vdev_checkpoint_sm
), !=, 0);
3872 error
= ZFS_ERR_CHECKPOINT_EXISTS
;
3875 spa_vdev_state_enter(spa
, SCL_ALLOC
);
3878 * Check to see if the config has changed.
3880 if (error
|| generation
!= spa
->spa_config_generation
) {
3881 metaslab_group_activate(mg
);
3883 return (spa_vdev_state_exit(spa
,
3885 (void) spa_vdev_state_exit(spa
, vd
, 0);
3888 ASSERT0(tvd
->vdev_stat
.vs_alloc
);
3892 * Offline this device and reopen its top-level vdev.
3893 * If the top-level vdev is a log device then just offline
3894 * it. Otherwise, if this action results in the top-level
3895 * vdev becoming unusable, undo it and fail the request.
3897 vd
->vdev_offline
= B_TRUE
;
3900 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
3901 vdev_is_dead(tvd
)) {
3902 vd
->vdev_offline
= B_FALSE
;
3904 return (spa_vdev_state_exit(spa
, NULL
,
3909 * Add the device back into the metaslab rotor so that
3910 * once we online the device it's open for business.
3912 if (tvd
->vdev_islog
&& mg
!= NULL
)
3913 metaslab_group_activate(mg
);
3916 vd
->vdev_tmpoffline
= !!(flags
& ZFS_OFFLINE_TEMPORARY
);
3918 return (spa_vdev_state_exit(spa
, vd
, 0));
3922 vdev_offline(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
3926 mutex_enter(&spa
->spa_vdev_top_lock
);
3927 error
= vdev_offline_locked(spa
, guid
, flags
);
3928 mutex_exit(&spa
->spa_vdev_top_lock
);
3934 * Clear the error counts associated with this vdev. Unlike vdev_online() and
3935 * vdev_offline(), we assume the spa config is locked. We also clear all
3936 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
3939 vdev_clear(spa_t
*spa
, vdev_t
*vd
)
3941 vdev_t
*rvd
= spa
->spa_root_vdev
;
3943 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
3948 vd
->vdev_stat
.vs_read_errors
= 0;
3949 vd
->vdev_stat
.vs_write_errors
= 0;
3950 vd
->vdev_stat
.vs_checksum_errors
= 0;
3951 vd
->vdev_stat
.vs_slow_ios
= 0;
3953 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3954 vdev_clear(spa
, vd
->vdev_child
[c
]);
3957 * It makes no sense to "clear" an indirect vdev.
3959 if (!vdev_is_concrete(vd
))
3963 * If we're in the FAULTED state or have experienced failed I/O, then
3964 * clear the persistent state and attempt to reopen the device. We
3965 * also mark the vdev config dirty, so that the new faulted state is
3966 * written out to disk.
3968 if (vd
->vdev_faulted
|| vd
->vdev_degraded
||
3969 !vdev_readable(vd
) || !vdev_writeable(vd
)) {
3971 * When reopening in response to a clear event, it may be due to
3972 * a fmadm repair request. In this case, if the device is
3973 * still broken, we want to still post the ereport again.
3975 vd
->vdev_forcefault
= B_TRUE
;
3977 vd
->vdev_faulted
= vd
->vdev_degraded
= 0ULL;
3978 vd
->vdev_cant_read
= B_FALSE
;
3979 vd
->vdev_cant_write
= B_FALSE
;
3980 vd
->vdev_stat
.vs_aux
= 0;
3982 vdev_reopen(vd
== rvd
? rvd
: vd
->vdev_top
);
3984 vd
->vdev_forcefault
= B_FALSE
;
3986 if (vd
!= rvd
&& vdev_writeable(vd
->vdev_top
))
3987 vdev_state_dirty(vd
->vdev_top
);
3989 /* If a resilver isn't required, check if vdevs can be culled */
3990 if (vd
->vdev_aux
== NULL
&& !vdev_is_dead(vd
) &&
3991 !dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
3992 !dsl_scan_resilver_scheduled(spa
->spa_dsl_pool
))
3993 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
3995 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_CLEAR
);
3999 * When clearing a FMA-diagnosed fault, we always want to
4000 * unspare the device, as we assume that the original spare was
4001 * done in response to the FMA fault.
4003 if (!vdev_is_dead(vd
) && vd
->vdev_parent
!= NULL
&&
4004 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
4005 vd
->vdev_parent
->vdev_child
[0] == vd
)
4006 vd
->vdev_unspare
= B_TRUE
;
4010 vdev_is_dead(vdev_t
*vd
)
4013 * Holes and missing devices are always considered "dead".
4014 * This simplifies the code since we don't have to check for
4015 * these types of devices in the various code paths.
4016 * Instead we rely on the fact that we skip over dead devices
4017 * before issuing I/O to them.
4019 return (vd
->vdev_state
< VDEV_STATE_DEGRADED
||
4020 vd
->vdev_ops
== &vdev_hole_ops
||
4021 vd
->vdev_ops
== &vdev_missing_ops
);
4025 vdev_readable(vdev_t
*vd
)
4027 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_read
);
4031 vdev_writeable(vdev_t
*vd
)
4033 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_write
&&
4034 vdev_is_concrete(vd
));
4038 vdev_allocatable(vdev_t
*vd
)
4040 uint64_t state
= vd
->vdev_state
;
4043 * We currently allow allocations from vdevs which may be in the
4044 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
4045 * fails to reopen then we'll catch it later when we're holding
4046 * the proper locks. Note that we have to get the vdev state
4047 * in a local variable because although it changes atomically,
4048 * we're asking two separate questions about it.
4050 return (!(state
< VDEV_STATE_DEGRADED
&& state
!= VDEV_STATE_CLOSED
) &&
4051 !vd
->vdev_cant_write
&& vdev_is_concrete(vd
) &&
4052 vd
->vdev_mg
->mg_initialized
);
4056 vdev_accessible(vdev_t
*vd
, zio_t
*zio
)
4058 ASSERT(zio
->io_vd
== vd
);
4060 if (vdev_is_dead(vd
) || vd
->vdev_remove_wanted
)
4063 if (zio
->io_type
== ZIO_TYPE_READ
)
4064 return (!vd
->vdev_cant_read
);
4066 if (zio
->io_type
== ZIO_TYPE_WRITE
)
4067 return (!vd
->vdev_cant_write
);
4073 vdev_get_child_stat(vdev_t
*cvd
, vdev_stat_t
*vs
, vdev_stat_t
*cvs
)
4076 * Exclude the dRAID spare when aggregating to avoid double counting
4077 * the ops and bytes. These IOs are counted by the physical leaves.
4079 if (cvd
->vdev_ops
== &vdev_draid_spare_ops
)
4082 for (int t
= 0; t
< VS_ZIO_TYPES
; t
++) {
4083 vs
->vs_ops
[t
] += cvs
->vs_ops
[t
];
4084 vs
->vs_bytes
[t
] += cvs
->vs_bytes
[t
];
4087 cvs
->vs_scan_removing
= cvd
->vdev_removing
;
4091 * Get extended stats
4094 vdev_get_child_stat_ex(vdev_t
*cvd
, vdev_stat_ex_t
*vsx
, vdev_stat_ex_t
*cvsx
)
4097 for (t
= 0; t
< ZIO_TYPES
; t
++) {
4098 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_disk_histo
[0]); b
++)
4099 vsx
->vsx_disk_histo
[t
][b
] += cvsx
->vsx_disk_histo
[t
][b
];
4101 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_total_histo
[0]); b
++) {
4102 vsx
->vsx_total_histo
[t
][b
] +=
4103 cvsx
->vsx_total_histo
[t
][b
];
4107 for (t
= 0; t
< ZIO_PRIORITY_NUM_QUEUEABLE
; t
++) {
4108 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_queue_histo
[0]); b
++) {
4109 vsx
->vsx_queue_histo
[t
][b
] +=
4110 cvsx
->vsx_queue_histo
[t
][b
];
4112 vsx
->vsx_active_queue
[t
] += cvsx
->vsx_active_queue
[t
];
4113 vsx
->vsx_pend_queue
[t
] += cvsx
->vsx_pend_queue
[t
];
4115 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_ind_histo
[0]); b
++)
4116 vsx
->vsx_ind_histo
[t
][b
] += cvsx
->vsx_ind_histo
[t
][b
];
4118 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_agg_histo
[0]); b
++)
4119 vsx
->vsx_agg_histo
[t
][b
] += cvsx
->vsx_agg_histo
[t
][b
];
4125 vdev_is_spacemap_addressable(vdev_t
*vd
)
4127 if (spa_feature_is_active(vd
->vdev_spa
, SPA_FEATURE_SPACEMAP_V2
))
4131 * If double-word space map entries are not enabled we assume
4132 * 47 bits of the space map entry are dedicated to the entry's
4133 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
4134 * to calculate the maximum address that can be described by a
4135 * space map entry for the given device.
4137 uint64_t shift
= vd
->vdev_ashift
+ SM_OFFSET_BITS
;
4139 if (shift
>= 63) /* detect potential overflow */
4142 return (vd
->vdev_asize
< (1ULL << shift
));
4146 * Get statistics for the given vdev.
4149 vdev_get_stats_ex_impl(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
4153 * If we're getting stats on the root vdev, aggregate the I/O counts
4154 * over all top-level vdevs (i.e. the direct children of the root).
4156 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4158 memset(vs
->vs_ops
, 0, sizeof (vs
->vs_ops
));
4159 memset(vs
->vs_bytes
, 0, sizeof (vs
->vs_bytes
));
4162 memset(vsx
, 0, sizeof (*vsx
));
4164 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4165 vdev_t
*cvd
= vd
->vdev_child
[c
];
4166 vdev_stat_t
*cvs
= &cvd
->vdev_stat
;
4167 vdev_stat_ex_t
*cvsx
= &cvd
->vdev_stat_ex
;
4169 vdev_get_stats_ex_impl(cvd
, cvs
, cvsx
);
4171 vdev_get_child_stat(cvd
, vs
, cvs
);
4173 vdev_get_child_stat_ex(cvd
, vsx
, cvsx
);
4177 * We're a leaf. Just copy our ZIO active queue stats in. The
4178 * other leaf stats are updated in vdev_stat_update().
4183 memcpy(vsx
, &vd
->vdev_stat_ex
, sizeof (vd
->vdev_stat_ex
));
4185 for (t
= 0; t
< ARRAY_SIZE(vd
->vdev_queue
.vq_class
); t
++) {
4186 vsx
->vsx_active_queue
[t
] =
4187 vd
->vdev_queue
.vq_class
[t
].vqc_active
;
4188 vsx
->vsx_pend_queue
[t
] = avl_numnodes(
4189 &vd
->vdev_queue
.vq_class
[t
].vqc_queued_tree
);
4195 vdev_get_stats_ex(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
4197 vdev_t
*tvd
= vd
->vdev_top
;
4198 mutex_enter(&vd
->vdev_stat_lock
);
4200 bcopy(&vd
->vdev_stat
, vs
, sizeof (*vs
));
4201 vs
->vs_timestamp
= gethrtime() - vs
->vs_timestamp
;
4202 vs
->vs_state
= vd
->vdev_state
;
4203 vs
->vs_rsize
= vdev_get_min_asize(vd
);
4205 if (vd
->vdev_ops
->vdev_op_leaf
) {
4206 vs
->vs_rsize
+= VDEV_LABEL_START_SIZE
+
4207 VDEV_LABEL_END_SIZE
;
4209 * Report initializing progress. Since we don't
4210 * have the initializing locks held, this is only
4211 * an estimate (although a fairly accurate one).
4213 vs
->vs_initialize_bytes_done
=
4214 vd
->vdev_initialize_bytes_done
;
4215 vs
->vs_initialize_bytes_est
=
4216 vd
->vdev_initialize_bytes_est
;
4217 vs
->vs_initialize_state
= vd
->vdev_initialize_state
;
4218 vs
->vs_initialize_action_time
=
4219 vd
->vdev_initialize_action_time
;
4222 * Report manual TRIM progress. Since we don't have
4223 * the manual TRIM locks held, this is only an
4224 * estimate (although fairly accurate one).
4226 vs
->vs_trim_notsup
= !vd
->vdev_has_trim
;
4227 vs
->vs_trim_bytes_done
= vd
->vdev_trim_bytes_done
;
4228 vs
->vs_trim_bytes_est
= vd
->vdev_trim_bytes_est
;
4229 vs
->vs_trim_state
= vd
->vdev_trim_state
;
4230 vs
->vs_trim_action_time
= vd
->vdev_trim_action_time
;
4232 /* Set when there is a deferred resilver. */
4233 vs
->vs_resilver_deferred
= vd
->vdev_resilver_deferred
;
4237 * Report expandable space on top-level, non-auxiliary devices
4238 * only. The expandable space is reported in terms of metaslab
4239 * sized units since that determines how much space the pool
4242 if (vd
->vdev_aux
== NULL
&& tvd
!= NULL
) {
4243 vs
->vs_esize
= P2ALIGN(
4244 vd
->vdev_max_asize
- vd
->vdev_asize
,
4245 1ULL << tvd
->vdev_ms_shift
);
4248 vs
->vs_configured_ashift
= vd
->vdev_top
!= NULL
4249 ? vd
->vdev_top
->vdev_ashift
: vd
->vdev_ashift
;
4250 vs
->vs_logical_ashift
= vd
->vdev_logical_ashift
;
4251 vs
->vs_physical_ashift
= vd
->vdev_physical_ashift
;
4254 * Report fragmentation and rebuild progress for top-level,
4255 * non-auxiliary, concrete devices.
4257 if (vd
->vdev_aux
== NULL
&& vd
== vd
->vdev_top
&&
4258 vdev_is_concrete(vd
)) {
4259 vs
->vs_fragmentation
= (vd
->vdev_mg
!= NULL
) ?
4260 vd
->vdev_mg
->mg_fragmentation
: 0;
4264 vdev_get_stats_ex_impl(vd
, vs
, vsx
);
4265 mutex_exit(&vd
->vdev_stat_lock
);
4269 vdev_get_stats(vdev_t
*vd
, vdev_stat_t
*vs
)
4271 return (vdev_get_stats_ex(vd
, vs
, NULL
));
4275 vdev_clear_stats(vdev_t
*vd
)
4277 mutex_enter(&vd
->vdev_stat_lock
);
4278 vd
->vdev_stat
.vs_space
= 0;
4279 vd
->vdev_stat
.vs_dspace
= 0;
4280 vd
->vdev_stat
.vs_alloc
= 0;
4281 mutex_exit(&vd
->vdev_stat_lock
);
4285 vdev_scan_stat_init(vdev_t
*vd
)
4287 vdev_stat_t
*vs
= &vd
->vdev_stat
;
4289 for (int c
= 0; c
< vd
->vdev_children
; c
++)
4290 vdev_scan_stat_init(vd
->vdev_child
[c
]);
4292 mutex_enter(&vd
->vdev_stat_lock
);
4293 vs
->vs_scan_processed
= 0;
4294 mutex_exit(&vd
->vdev_stat_lock
);
4298 vdev_stat_update(zio_t
*zio
, uint64_t psize
)
4300 spa_t
*spa
= zio
->io_spa
;
4301 vdev_t
*rvd
= spa
->spa_root_vdev
;
4302 vdev_t
*vd
= zio
->io_vd
? zio
->io_vd
: rvd
;
4304 uint64_t txg
= zio
->io_txg
;
4305 vdev_stat_t
*vs
= &vd
->vdev_stat
;
4306 vdev_stat_ex_t
*vsx
= &vd
->vdev_stat_ex
;
4307 zio_type_t type
= zio
->io_type
;
4308 int flags
= zio
->io_flags
;
4311 * If this i/o is a gang leader, it didn't do any actual work.
4313 if (zio
->io_gang_tree
)
4316 if (zio
->io_error
== 0) {
4318 * If this is a root i/o, don't count it -- we've already
4319 * counted the top-level vdevs, and vdev_get_stats() will
4320 * aggregate them when asked. This reduces contention on
4321 * the root vdev_stat_lock and implicitly handles blocks
4322 * that compress away to holes, for which there is no i/o.
4323 * (Holes never create vdev children, so all the counters
4324 * remain zero, which is what we want.)
4326 * Note: this only applies to successful i/o (io_error == 0)
4327 * because unlike i/o counts, errors are not additive.
4328 * When reading a ditto block, for example, failure of
4329 * one top-level vdev does not imply a root-level error.
4334 ASSERT(vd
== zio
->io_vd
);
4336 if (flags
& ZIO_FLAG_IO_BYPASS
)
4339 mutex_enter(&vd
->vdev_stat_lock
);
4341 if (flags
& ZIO_FLAG_IO_REPAIR
) {
4343 * Repair is the result of a resilver issued by the
4344 * scan thread (spa_sync).
4346 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
4347 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
4348 dsl_scan_phys_t
*scn_phys
= &scn
->scn_phys
;
4349 uint64_t *processed
= &scn_phys
->scn_processed
;
4351 if (vd
->vdev_ops
->vdev_op_leaf
)
4352 atomic_add_64(processed
, psize
);
4353 vs
->vs_scan_processed
+= psize
;
4357 * Repair is the result of a rebuild issued by the
4358 * rebuild thread (vdev_rebuild_thread). To avoid
4359 * double counting repaired bytes the virtual dRAID
4360 * spare vdev is excluded from the processed bytes.
4362 if (zio
->io_priority
== ZIO_PRIORITY_REBUILD
) {
4363 vdev_t
*tvd
= vd
->vdev_top
;
4364 vdev_rebuild_t
*vr
= &tvd
->vdev_rebuild_config
;
4365 vdev_rebuild_phys_t
*vrp
= &vr
->vr_rebuild_phys
;
4366 uint64_t *rebuilt
= &vrp
->vrp_bytes_rebuilt
;
4368 if (vd
->vdev_ops
->vdev_op_leaf
&&
4369 vd
->vdev_ops
!= &vdev_draid_spare_ops
) {
4370 atomic_add_64(rebuilt
, psize
);
4372 vs
->vs_rebuild_processed
+= psize
;
4375 if (flags
& ZIO_FLAG_SELF_HEAL
)
4376 vs
->vs_self_healed
+= psize
;
4380 * The bytes/ops/histograms are recorded at the leaf level and
4381 * aggregated into the higher level vdevs in vdev_get_stats().
4383 if (vd
->vdev_ops
->vdev_op_leaf
&&
4384 (zio
->io_priority
< ZIO_PRIORITY_NUM_QUEUEABLE
)) {
4385 zio_type_t vs_type
= type
;
4386 zio_priority_t priority
= zio
->io_priority
;
4389 * TRIM ops and bytes are reported to user space as
4390 * ZIO_TYPE_IOCTL. This is done to preserve the
4391 * vdev_stat_t structure layout for user space.
4393 if (type
== ZIO_TYPE_TRIM
)
4394 vs_type
= ZIO_TYPE_IOCTL
;
4397 * Solely for the purposes of 'zpool iostat -lqrw'
4398 * reporting use the priority to catagorize the IO.
4399 * Only the following are reported to user space:
4401 * ZIO_PRIORITY_SYNC_READ,
4402 * ZIO_PRIORITY_SYNC_WRITE,
4403 * ZIO_PRIORITY_ASYNC_READ,
4404 * ZIO_PRIORITY_ASYNC_WRITE,
4405 * ZIO_PRIORITY_SCRUB,
4406 * ZIO_PRIORITY_TRIM.
4408 if (priority
== ZIO_PRIORITY_REBUILD
) {
4409 priority
= ((type
== ZIO_TYPE_WRITE
) ?
4410 ZIO_PRIORITY_ASYNC_WRITE
:
4411 ZIO_PRIORITY_SCRUB
);
4412 } else if (priority
== ZIO_PRIORITY_INITIALIZING
) {
4413 ASSERT3U(type
, ==, ZIO_TYPE_WRITE
);
4414 priority
= ZIO_PRIORITY_ASYNC_WRITE
;
4415 } else if (priority
== ZIO_PRIORITY_REMOVAL
) {
4416 priority
= ((type
== ZIO_TYPE_WRITE
) ?
4417 ZIO_PRIORITY_ASYNC_WRITE
:
4418 ZIO_PRIORITY_ASYNC_READ
);
4421 vs
->vs_ops
[vs_type
]++;
4422 vs
->vs_bytes
[vs_type
] += psize
;
4424 if (flags
& ZIO_FLAG_DELEGATED
) {
4425 vsx
->vsx_agg_histo
[priority
]
4426 [RQ_HISTO(zio
->io_size
)]++;
4428 vsx
->vsx_ind_histo
[priority
]
4429 [RQ_HISTO(zio
->io_size
)]++;
4432 if (zio
->io_delta
&& zio
->io_delay
) {
4433 vsx
->vsx_queue_histo
[priority
]
4434 [L_HISTO(zio
->io_delta
- zio
->io_delay
)]++;
4435 vsx
->vsx_disk_histo
[type
]
4436 [L_HISTO(zio
->io_delay
)]++;
4437 vsx
->vsx_total_histo
[type
]
4438 [L_HISTO(zio
->io_delta
)]++;
4442 mutex_exit(&vd
->vdev_stat_lock
);
4446 if (flags
& ZIO_FLAG_SPECULATIVE
)
4450 * If this is an I/O error that is going to be retried, then ignore the
4451 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
4452 * hard errors, when in reality they can happen for any number of
4453 * innocuous reasons (bus resets, MPxIO link failure, etc).
4455 if (zio
->io_error
== EIO
&&
4456 !(zio
->io_flags
& ZIO_FLAG_IO_RETRY
))
4460 * Intent logs writes won't propagate their error to the root
4461 * I/O so don't mark these types of failures as pool-level
4464 if (zio
->io_vd
== NULL
&& (zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
4467 if (type
== ZIO_TYPE_WRITE
&& txg
!= 0 &&
4468 (!(flags
& ZIO_FLAG_IO_REPAIR
) ||
4469 (flags
& ZIO_FLAG_SCAN_THREAD
) ||
4470 spa
->spa_claiming
)) {
4472 * This is either a normal write (not a repair), or it's
4473 * a repair induced by the scrub thread, or it's a repair
4474 * made by zil_claim() during spa_load() in the first txg.
4475 * In the normal case, we commit the DTL change in the same
4476 * txg as the block was born. In the scrub-induced repair
4477 * case, we know that scrubs run in first-pass syncing context,
4478 * so we commit the DTL change in spa_syncing_txg(spa).
4479 * In the zil_claim() case, we commit in spa_first_txg(spa).
4481 * We currently do not make DTL entries for failed spontaneous
4482 * self-healing writes triggered by normal (non-scrubbing)
4483 * reads, because we have no transactional context in which to
4484 * do so -- and it's not clear that it'd be desirable anyway.
4486 if (vd
->vdev_ops
->vdev_op_leaf
) {
4487 uint64_t commit_txg
= txg
;
4488 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
4489 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
4490 ASSERT(spa_sync_pass(spa
) == 1);
4491 vdev_dtl_dirty(vd
, DTL_SCRUB
, txg
, 1);
4492 commit_txg
= spa_syncing_txg(spa
);
4493 } else if (spa
->spa_claiming
) {
4494 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
4495 commit_txg
= spa_first_txg(spa
);
4497 ASSERT(commit_txg
>= spa_syncing_txg(spa
));
4498 if (vdev_dtl_contains(vd
, DTL_MISSING
, txg
, 1))
4500 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
4501 vdev_dtl_dirty(pvd
, DTL_PARTIAL
, txg
, 1);
4502 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, commit_txg
);
4505 vdev_dtl_dirty(vd
, DTL_MISSING
, txg
, 1);
4510 vdev_deflated_space(vdev_t
*vd
, int64_t space
)
4512 ASSERT((space
& (SPA_MINBLOCKSIZE
-1)) == 0);
4513 ASSERT(vd
->vdev_deflate_ratio
!= 0 || vd
->vdev_isl2cache
);
4515 return ((space
>> SPA_MINBLOCKSHIFT
) * vd
->vdev_deflate_ratio
);
4519 * Update the in-core space usage stats for this vdev, its metaslab class,
4520 * and the root vdev.
4523 vdev_space_update(vdev_t
*vd
, int64_t alloc_delta
, int64_t defer_delta
,
4524 int64_t space_delta
)
4526 int64_t dspace_delta
;
4527 spa_t
*spa
= vd
->vdev_spa
;
4528 vdev_t
*rvd
= spa
->spa_root_vdev
;
4530 ASSERT(vd
== vd
->vdev_top
);
4533 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
4534 * factor. We must calculate this here and not at the root vdev
4535 * because the root vdev's psize-to-asize is simply the max of its
4536 * children's, thus not accurate enough for us.
4538 dspace_delta
= vdev_deflated_space(vd
, space_delta
);
4540 mutex_enter(&vd
->vdev_stat_lock
);
4541 /* ensure we won't underflow */
4542 if (alloc_delta
< 0) {
4543 ASSERT3U(vd
->vdev_stat
.vs_alloc
, >=, -alloc_delta
);
4546 vd
->vdev_stat
.vs_alloc
+= alloc_delta
;
4547 vd
->vdev_stat
.vs_space
+= space_delta
;
4548 vd
->vdev_stat
.vs_dspace
+= dspace_delta
;
4549 mutex_exit(&vd
->vdev_stat_lock
);
4551 /* every class but log contributes to root space stats */
4552 if (vd
->vdev_mg
!= NULL
&& !vd
->vdev_islog
) {
4553 ASSERT(!vd
->vdev_isl2cache
);
4554 mutex_enter(&rvd
->vdev_stat_lock
);
4555 rvd
->vdev_stat
.vs_alloc
+= alloc_delta
;
4556 rvd
->vdev_stat
.vs_space
+= space_delta
;
4557 rvd
->vdev_stat
.vs_dspace
+= dspace_delta
;
4558 mutex_exit(&rvd
->vdev_stat_lock
);
4560 /* Note: metaslab_class_space_update moved to metaslab_space_update */
4564 * Mark a top-level vdev's config as dirty, placing it on the dirty list
4565 * so that it will be written out next time the vdev configuration is synced.
4566 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
4569 vdev_config_dirty(vdev_t
*vd
)
4571 spa_t
*spa
= vd
->vdev_spa
;
4572 vdev_t
*rvd
= spa
->spa_root_vdev
;
4575 ASSERT(spa_writeable(spa
));
4578 * If this is an aux vdev (as with l2cache and spare devices), then we
4579 * update the vdev config manually and set the sync flag.
4581 if (vd
->vdev_aux
!= NULL
) {
4582 spa_aux_vdev_t
*sav
= vd
->vdev_aux
;
4586 for (c
= 0; c
< sav
->sav_count
; c
++) {
4587 if (sav
->sav_vdevs
[c
] == vd
)
4591 if (c
== sav
->sav_count
) {
4593 * We're being removed. There's nothing more to do.
4595 ASSERT(sav
->sav_sync
== B_TRUE
);
4599 sav
->sav_sync
= B_TRUE
;
4601 if (nvlist_lookup_nvlist_array(sav
->sav_config
,
4602 ZPOOL_CONFIG_L2CACHE
, &aux
, &naux
) != 0) {
4603 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
4604 ZPOOL_CONFIG_SPARES
, &aux
, &naux
) == 0);
4610 * Setting the nvlist in the middle if the array is a little
4611 * sketchy, but it will work.
4613 nvlist_free(aux
[c
]);
4614 aux
[c
] = vdev_config_generate(spa
, vd
, B_TRUE
, 0);
4620 * The dirty list is protected by the SCL_CONFIG lock. The caller
4621 * must either hold SCL_CONFIG as writer, or must be the sync thread
4622 * (which holds SCL_CONFIG as reader). There's only one sync thread,
4623 * so this is sufficient to ensure mutual exclusion.
4625 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
4626 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
4627 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
4630 for (c
= 0; c
< rvd
->vdev_children
; c
++)
4631 vdev_config_dirty(rvd
->vdev_child
[c
]);
4633 ASSERT(vd
== vd
->vdev_top
);
4635 if (!list_link_active(&vd
->vdev_config_dirty_node
) &&
4636 vdev_is_concrete(vd
)) {
4637 list_insert_head(&spa
->spa_config_dirty_list
, vd
);
4643 vdev_config_clean(vdev_t
*vd
)
4645 spa_t
*spa
= vd
->vdev_spa
;
4647 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
4648 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
4649 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
4651 ASSERT(list_link_active(&vd
->vdev_config_dirty_node
));
4652 list_remove(&spa
->spa_config_dirty_list
, vd
);
4656 * Mark a top-level vdev's state as dirty, so that the next pass of
4657 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
4658 * the state changes from larger config changes because they require
4659 * much less locking, and are often needed for administrative actions.
4662 vdev_state_dirty(vdev_t
*vd
)
4664 spa_t
*spa
= vd
->vdev_spa
;
4666 ASSERT(spa_writeable(spa
));
4667 ASSERT(vd
== vd
->vdev_top
);
4670 * The state list is protected by the SCL_STATE lock. The caller
4671 * must either hold SCL_STATE as writer, or must be the sync thread
4672 * (which holds SCL_STATE as reader). There's only one sync thread,
4673 * so this is sufficient to ensure mutual exclusion.
4675 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
4676 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
4677 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
4679 if (!list_link_active(&vd
->vdev_state_dirty_node
) &&
4680 vdev_is_concrete(vd
))
4681 list_insert_head(&spa
->spa_state_dirty_list
, vd
);
4685 vdev_state_clean(vdev_t
*vd
)
4687 spa_t
*spa
= vd
->vdev_spa
;
4689 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
4690 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
4691 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
4693 ASSERT(list_link_active(&vd
->vdev_state_dirty_node
));
4694 list_remove(&spa
->spa_state_dirty_list
, vd
);
4698 * Propagate vdev state up from children to parent.
4701 vdev_propagate_state(vdev_t
*vd
)
4703 spa_t
*spa
= vd
->vdev_spa
;
4704 vdev_t
*rvd
= spa
->spa_root_vdev
;
4705 int degraded
= 0, faulted
= 0;
4709 if (vd
->vdev_children
> 0) {
4710 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4711 child
= vd
->vdev_child
[c
];
4714 * Don't factor holes or indirect vdevs into the
4717 if (!vdev_is_concrete(child
))
4720 if (!vdev_readable(child
) ||
4721 (!vdev_writeable(child
) && spa_writeable(spa
))) {
4723 * Root special: if there is a top-level log
4724 * device, treat the root vdev as if it were
4727 if (child
->vdev_islog
&& vd
== rvd
)
4731 } else if (child
->vdev_state
<= VDEV_STATE_DEGRADED
) {
4735 if (child
->vdev_stat
.vs_aux
== VDEV_AUX_CORRUPT_DATA
)
4739 vd
->vdev_ops
->vdev_op_state_change(vd
, faulted
, degraded
);
4742 * Root special: if there is a top-level vdev that cannot be
4743 * opened due to corrupted metadata, then propagate the root
4744 * vdev's aux state as 'corrupt' rather than 'insufficient
4747 if (corrupted
&& vd
== rvd
&&
4748 rvd
->vdev_state
== VDEV_STATE_CANT_OPEN
)
4749 vdev_set_state(rvd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
4750 VDEV_AUX_CORRUPT_DATA
);
4753 if (vd
->vdev_parent
)
4754 vdev_propagate_state(vd
->vdev_parent
);
4758 * Set a vdev's state. If this is during an open, we don't update the parent
4759 * state, because we're in the process of opening children depth-first.
4760 * Otherwise, we propagate the change to the parent.
4762 * If this routine places a device in a faulted state, an appropriate ereport is
4766 vdev_set_state(vdev_t
*vd
, boolean_t isopen
, vdev_state_t state
, vdev_aux_t aux
)
4768 uint64_t save_state
;
4769 spa_t
*spa
= vd
->vdev_spa
;
4771 if (state
== vd
->vdev_state
) {
4773 * Since vdev_offline() code path is already in an offline
4774 * state we can miss a statechange event to OFFLINE. Check
4775 * the previous state to catch this condition.
4777 if (vd
->vdev_ops
->vdev_op_leaf
&&
4778 (state
== VDEV_STATE_OFFLINE
) &&
4779 (vd
->vdev_prevstate
>= VDEV_STATE_FAULTED
)) {
4780 /* post an offline state change */
4781 zfs_post_state_change(spa
, vd
, vd
->vdev_prevstate
);
4783 vd
->vdev_stat
.vs_aux
= aux
;
4787 save_state
= vd
->vdev_state
;
4789 vd
->vdev_state
= state
;
4790 vd
->vdev_stat
.vs_aux
= aux
;
4793 * If we are setting the vdev state to anything but an open state, then
4794 * always close the underlying device unless the device has requested
4795 * a delayed close (i.e. we're about to remove or fault the device).
4796 * Otherwise, we keep accessible but invalid devices open forever.
4797 * We don't call vdev_close() itself, because that implies some extra
4798 * checks (offline, etc) that we don't want here. This is limited to
4799 * leaf devices, because otherwise closing the device will affect other
4802 if (!vd
->vdev_delayed_close
&& vdev_is_dead(vd
) &&
4803 vd
->vdev_ops
->vdev_op_leaf
)
4804 vd
->vdev_ops
->vdev_op_close(vd
);
4806 if (vd
->vdev_removed
&&
4807 state
== VDEV_STATE_CANT_OPEN
&&
4808 (aux
== VDEV_AUX_OPEN_FAILED
|| vd
->vdev_checkremove
)) {
4810 * If the previous state is set to VDEV_STATE_REMOVED, then this
4811 * device was previously marked removed and someone attempted to
4812 * reopen it. If this failed due to a nonexistent device, then
4813 * keep the device in the REMOVED state. We also let this be if
4814 * it is one of our special test online cases, which is only
4815 * attempting to online the device and shouldn't generate an FMA
4818 vd
->vdev_state
= VDEV_STATE_REMOVED
;
4819 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
4820 } else if (state
== VDEV_STATE_REMOVED
) {
4821 vd
->vdev_removed
= B_TRUE
;
4822 } else if (state
== VDEV_STATE_CANT_OPEN
) {
4824 * If we fail to open a vdev during an import or recovery, we
4825 * mark it as "not available", which signifies that it was
4826 * never there to begin with. Failure to open such a device
4827 * is not considered an error.
4829 if ((spa_load_state(spa
) == SPA_LOAD_IMPORT
||
4830 spa_load_state(spa
) == SPA_LOAD_RECOVER
) &&
4831 vd
->vdev_ops
->vdev_op_leaf
)
4832 vd
->vdev_not_present
= 1;
4835 * Post the appropriate ereport. If the 'prevstate' field is
4836 * set to something other than VDEV_STATE_UNKNOWN, it indicates
4837 * that this is part of a vdev_reopen(). In this case, we don't
4838 * want to post the ereport if the device was already in the
4839 * CANT_OPEN state beforehand.
4841 * If the 'checkremove' flag is set, then this is an attempt to
4842 * online the device in response to an insertion event. If we
4843 * hit this case, then we have detected an insertion event for a
4844 * faulted or offline device that wasn't in the removed state.
4845 * In this scenario, we don't post an ereport because we are
4846 * about to replace the device, or attempt an online with
4847 * vdev_forcefault, which will generate the fault for us.
4849 if ((vd
->vdev_prevstate
!= state
|| vd
->vdev_forcefault
) &&
4850 !vd
->vdev_not_present
&& !vd
->vdev_checkremove
&&
4851 vd
!= spa
->spa_root_vdev
) {
4855 case VDEV_AUX_OPEN_FAILED
:
4856 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED
;
4858 case VDEV_AUX_CORRUPT_DATA
:
4859 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA
;
4861 case VDEV_AUX_NO_REPLICAS
:
4862 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS
;
4864 case VDEV_AUX_BAD_GUID_SUM
:
4865 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM
;
4867 case VDEV_AUX_TOO_SMALL
:
4868 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL
;
4870 case VDEV_AUX_BAD_LABEL
:
4871 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL
;
4873 case VDEV_AUX_BAD_ASHIFT
:
4874 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
;
4877 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN
;
4880 (void) zfs_ereport_post(class, spa
, vd
, NULL
, NULL
,
4884 /* Erase any notion of persistent removed state */
4885 vd
->vdev_removed
= B_FALSE
;
4887 vd
->vdev_removed
= B_FALSE
;
4891 * Notify ZED of any significant state-change on a leaf vdev.
4894 if (vd
->vdev_ops
->vdev_op_leaf
) {
4895 /* preserve original state from a vdev_reopen() */
4896 if ((vd
->vdev_prevstate
!= VDEV_STATE_UNKNOWN
) &&
4897 (vd
->vdev_prevstate
!= vd
->vdev_state
) &&
4898 (save_state
<= VDEV_STATE_CLOSED
))
4899 save_state
= vd
->vdev_prevstate
;
4901 /* filter out state change due to initial vdev_open */
4902 if (save_state
> VDEV_STATE_CLOSED
)
4903 zfs_post_state_change(spa
, vd
, save_state
);
4906 if (!isopen
&& vd
->vdev_parent
)
4907 vdev_propagate_state(vd
->vdev_parent
);
4911 vdev_children_are_offline(vdev_t
*vd
)
4913 ASSERT(!vd
->vdev_ops
->vdev_op_leaf
);
4915 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
4916 if (vd
->vdev_child
[i
]->vdev_state
!= VDEV_STATE_OFFLINE
)
4924 * Check the vdev configuration to ensure that it's capable of supporting
4925 * a root pool. We do not support partial configuration.
4928 vdev_is_bootable(vdev_t
*vd
)
4930 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4931 const char *vdev_type
= vd
->vdev_ops
->vdev_op_type
;
4933 if (strcmp(vdev_type
, VDEV_TYPE_MISSING
) == 0 ||
4934 strcmp(vdev_type
, VDEV_TYPE_INDIRECT
) == 0) {
4939 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4940 if (!vdev_is_bootable(vd
->vdev_child
[c
]))
4947 vdev_is_concrete(vdev_t
*vd
)
4949 vdev_ops_t
*ops
= vd
->vdev_ops
;
4950 if (ops
== &vdev_indirect_ops
|| ops
== &vdev_hole_ops
||
4951 ops
== &vdev_missing_ops
|| ops
== &vdev_root_ops
) {
4959 * Determine if a log device has valid content. If the vdev was
4960 * removed or faulted in the MOS config then we know that
4961 * the content on the log device has already been written to the pool.
4964 vdev_log_state_valid(vdev_t
*vd
)
4966 if (vd
->vdev_ops
->vdev_op_leaf
&& !vd
->vdev_faulted
&&
4970 for (int c
= 0; c
< vd
->vdev_children
; c
++)
4971 if (vdev_log_state_valid(vd
->vdev_child
[c
]))
4978 * Expand a vdev if possible.
4981 vdev_expand(vdev_t
*vd
, uint64_t txg
)
4983 ASSERT(vd
->vdev_top
== vd
);
4984 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4985 ASSERT(vdev_is_concrete(vd
));
4987 vdev_set_deflate_ratio(vd
);
4989 if ((vd
->vdev_asize
>> vd
->vdev_ms_shift
) > vd
->vdev_ms_count
&&
4990 vdev_is_concrete(vd
)) {
4991 vdev_metaslab_group_create(vd
);
4992 VERIFY(vdev_metaslab_init(vd
, txg
) == 0);
4993 vdev_config_dirty(vd
);
5001 vdev_split(vdev_t
*vd
)
5003 vdev_t
*cvd
, *pvd
= vd
->vdev_parent
;
5005 vdev_remove_child(pvd
, vd
);
5006 vdev_compact_children(pvd
);
5008 cvd
= pvd
->vdev_child
[0];
5009 if (pvd
->vdev_children
== 1) {
5010 vdev_remove_parent(cvd
);
5011 cvd
->vdev_splitting
= B_TRUE
;
5013 vdev_propagate_state(cvd
);
5017 vdev_deadman(vdev_t
*vd
, char *tag
)
5019 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5020 vdev_t
*cvd
= vd
->vdev_child
[c
];
5022 vdev_deadman(cvd
, tag
);
5025 if (vd
->vdev_ops
->vdev_op_leaf
) {
5026 vdev_queue_t
*vq
= &vd
->vdev_queue
;
5028 mutex_enter(&vq
->vq_lock
);
5029 if (avl_numnodes(&vq
->vq_active_tree
) > 0) {
5030 spa_t
*spa
= vd
->vdev_spa
;
5034 zfs_dbgmsg("slow vdev: %s has %d active IOs",
5035 vd
->vdev_path
, avl_numnodes(&vq
->vq_active_tree
));
5038 * Look at the head of all the pending queues,
5039 * if any I/O has been outstanding for longer than
5040 * the spa_deadman_synctime invoke the deadman logic.
5042 fio
= avl_first(&vq
->vq_active_tree
);
5043 delta
= gethrtime() - fio
->io_timestamp
;
5044 if (delta
> spa_deadman_synctime(spa
))
5045 zio_deadman(fio
, tag
);
5047 mutex_exit(&vq
->vq_lock
);
5052 vdev_defer_resilver(vdev_t
*vd
)
5054 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
5056 vd
->vdev_resilver_deferred
= B_TRUE
;
5057 vd
->vdev_spa
->spa_resilver_deferred
= B_TRUE
;
5061 * Clears the resilver deferred flag on all leaf devs under vd. Returns
5062 * B_TRUE if we have devices that need to be resilvered and are available to
5063 * accept resilver I/Os.
5066 vdev_clear_resilver_deferred(vdev_t
*vd
, dmu_tx_t
*tx
)
5068 boolean_t resilver_needed
= B_FALSE
;
5069 spa_t
*spa
= vd
->vdev_spa
;
5071 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5072 vdev_t
*cvd
= vd
->vdev_child
[c
];
5073 resilver_needed
|= vdev_clear_resilver_deferred(cvd
, tx
);
5076 if (vd
== spa
->spa_root_vdev
&&
5077 spa_feature_is_active(spa
, SPA_FEATURE_RESILVER_DEFER
)) {
5078 spa_feature_decr(spa
, SPA_FEATURE_RESILVER_DEFER
, tx
);
5079 vdev_config_dirty(vd
);
5080 spa
->spa_resilver_deferred
= B_FALSE
;
5081 return (resilver_needed
);
5084 if (!vdev_is_concrete(vd
) || vd
->vdev_aux
||
5085 !vd
->vdev_ops
->vdev_op_leaf
)
5086 return (resilver_needed
);
5088 vd
->vdev_resilver_deferred
= B_FALSE
;
5090 return (!vdev_is_dead(vd
) && !vd
->vdev_offline
&&
5091 vdev_resilver_needed(vd
, NULL
, NULL
));
5095 vdev_xlate_is_empty(range_seg64_t
*rs
)
5097 return (rs
->rs_start
== rs
->rs_end
);
5101 * Translate a logical range to the first contiguous physical range for the
5102 * specified vdev_t. This function is initially called with a leaf vdev and
5103 * will walk each parent vdev until it reaches a top-level vdev. Once the
5104 * top-level is reached the physical range is initialized and the recursive
5105 * function begins to unwind. As it unwinds it calls the parent's vdev
5106 * specific translation function to do the real conversion.
5109 vdev_xlate(vdev_t
*vd
, const range_seg64_t
*logical_rs
,
5110 range_seg64_t
*physical_rs
, range_seg64_t
*remain_rs
)
5113 * Walk up the vdev tree
5115 if (vd
!= vd
->vdev_top
) {
5116 vdev_xlate(vd
->vdev_parent
, logical_rs
, physical_rs
,
5120 * We've reached the top-level vdev, initialize the physical
5121 * range to the logical range and set an empty remaining
5122 * range then start to unwind.
5124 physical_rs
->rs_start
= logical_rs
->rs_start
;
5125 physical_rs
->rs_end
= logical_rs
->rs_end
;
5127 remain_rs
->rs_start
= logical_rs
->rs_start
;
5128 remain_rs
->rs_end
= logical_rs
->rs_start
;
5133 vdev_t
*pvd
= vd
->vdev_parent
;
5134 ASSERT3P(pvd
, !=, NULL
);
5135 ASSERT3P(pvd
->vdev_ops
->vdev_op_xlate
, !=, NULL
);
5138 * As this recursive function unwinds, translate the logical
5139 * range into its physical and any remaining components by calling
5140 * the vdev specific translate function.
5142 range_seg64_t intermediate
= { 0 };
5143 pvd
->vdev_ops
->vdev_op_xlate(vd
, physical_rs
, &intermediate
, remain_rs
);
5145 physical_rs
->rs_start
= intermediate
.rs_start
;
5146 physical_rs
->rs_end
= intermediate
.rs_end
;
5150 vdev_xlate_walk(vdev_t
*vd
, const range_seg64_t
*logical_rs
,
5151 vdev_xlate_func_t
*func
, void *arg
)
5153 range_seg64_t iter_rs
= *logical_rs
;
5154 range_seg64_t physical_rs
;
5155 range_seg64_t remain_rs
;
5157 while (!vdev_xlate_is_empty(&iter_rs
)) {
5159 vdev_xlate(vd
, &iter_rs
, &physical_rs
, &remain_rs
);
5162 * With raidz and dRAID, it's possible that the logical range
5163 * does not live on this leaf vdev. Only when there is a non-
5164 * zero physical size call the provided function.
5166 if (!vdev_xlate_is_empty(&physical_rs
))
5167 func(arg
, &physical_rs
);
5169 iter_rs
= remain_rs
;
5174 * Look at the vdev tree and determine whether any devices are currently being
5178 vdev_replace_in_progress(vdev_t
*vdev
)
5180 ASSERT(spa_config_held(vdev
->vdev_spa
, SCL_ALL
, RW_READER
) != 0);
5182 if (vdev
->vdev_ops
== &vdev_replacing_ops
)
5186 * A 'spare' vdev indicates that we have a replace in progress, unless
5187 * it has exactly two children, and the second, the hot spare, has
5188 * finished being resilvered.
5190 if (vdev
->vdev_ops
== &vdev_spare_ops
&& (vdev
->vdev_children
> 2 ||
5191 !vdev_dtl_empty(vdev
->vdev_child
[1], DTL_MISSING
)))
5194 for (int i
= 0; i
< vdev
->vdev_children
; i
++) {
5195 if (vdev_replace_in_progress(vdev
->vdev_child
[i
]))
5202 EXPORT_SYMBOL(vdev_fault
);
5203 EXPORT_SYMBOL(vdev_degrade
);
5204 EXPORT_SYMBOL(vdev_online
);
5205 EXPORT_SYMBOL(vdev_offline
);
5206 EXPORT_SYMBOL(vdev_clear
);
5209 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, default_ms_count
, INT
, ZMOD_RW
,
5210 "Target number of metaslabs per top-level vdev");
5212 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, default_ms_shift
, INT
, ZMOD_RW
,
5213 "Default limit for metaslab size");
5215 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, min_ms_count
, INT
, ZMOD_RW
,
5216 "Minimum number of metaslabs per top-level vdev");
5218 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, ms_count_limit
, INT
, ZMOD_RW
,
5219 "Practical upper limit of total metaslabs per top-level vdev");
5221 ZFS_MODULE_PARAM(zfs
, zfs_
, slow_io_events_per_second
, UINT
, ZMOD_RW
,
5222 "Rate limit slow IO (delay) events to this many per second");
5224 ZFS_MODULE_PARAM(zfs
, zfs_
, checksum_events_per_second
, UINT
, ZMOD_RW
,
5225 "Rate limit checksum events to this many checksum errors per second "
5226 "(do not set below zed threshold).");
5228 ZFS_MODULE_PARAM(zfs
, zfs_
, scan_ignore_errors
, INT
, ZMOD_RW
,
5229 "Ignore errors during resilver/scrub");
5231 ZFS_MODULE_PARAM(zfs_vdev
, vdev_
, validate_skip
, INT
, ZMOD_RW
,
5232 "Bypass vdev_validate()");
5234 ZFS_MODULE_PARAM(zfs
, zfs_
, nocacheflush
, INT
, ZMOD_RW
,
5235 "Disable cache flushes");
5237 ZFS_MODULE_PARAM_CALL(zfs_vdev
, zfs_vdev_
, min_auto_ashift
,
5238 param_set_min_auto_ashift
, param_get_ulong
, ZMOD_RW
,
5239 "Minimum ashift used when creating new top-level vdevs");
5241 ZFS_MODULE_PARAM_CALL(zfs_vdev
, zfs_vdev_
, max_auto_ashift
,
5242 param_set_max_auto_ashift
, param_get_ulong
, ZMOD_RW
,
5243 "Maximum ashift used when optimizing for logical -> physical sector "
5244 "size on new top-level vdevs");