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>
63 * One metaslab from each (normal-class) vdev is used by the ZIL. These are
64 * called "embedded slog metaslabs", are referenced by vdev_log_mg, and are
65 * part of the spa_embedded_log_class. The metaslab with the most free space
66 * in each vdev is selected for this purpose when the pool is opened (or a
67 * vdev is added). See vdev_metaslab_init().
69 * Log blocks can be allocated from the following locations. Each one is tried
70 * in order until the allocation succeeds:
71 * 1. dedicated log vdevs, aka "slog" (spa_log_class)
72 * 2. embedded slog metaslabs (spa_embedded_log_class)
73 * 3. other metaslabs in normal vdevs (spa_normal_class)
75 * zfs_embedded_slog_min_ms disables the embedded slog if there are fewer
76 * than this number of metaslabs in the vdev. This ensures that we don't set
77 * aside an unreasonable amount of space for the ZIL. If set to less than
78 * 1 << (spa_slop_shift + 1), on small pools the usable space may be reduced
79 * (by more than 1<<spa_slop_shift) due to the embedded slog metaslab.
81 int zfs_embedded_slog_min_ms
= 64;
83 /* default target for number of metaslabs per top-level vdev */
84 int zfs_vdev_default_ms_count
= 200;
86 /* minimum number of metaslabs per top-level vdev */
87 int zfs_vdev_min_ms_count
= 16;
89 /* practical upper limit of total metaslabs per top-level vdev */
90 int zfs_vdev_ms_count_limit
= 1ULL << 17;
92 /* lower limit for metaslab size (512M) */
93 int zfs_vdev_default_ms_shift
= 29;
95 /* upper limit for metaslab size (16G) */
96 int zfs_vdev_max_ms_shift
= 34;
98 int vdev_validate_skip
= B_FALSE
;
101 * Since the DTL space map of a vdev is not expected to have a lot of
102 * entries, we default its block size to 4K.
104 int zfs_vdev_dtl_sm_blksz
= (1 << 12);
107 * Rate limit slow IO (delay) events to this many per second.
109 unsigned int zfs_slow_io_events_per_second
= 20;
112 * Rate limit checksum events after this many checksum errors per second.
114 unsigned int zfs_checksum_events_per_second
= 20;
117 * Ignore errors during scrub/resilver. Allows to work around resilver
118 * upon import when there are pool errors.
120 int zfs_scan_ignore_errors
= 0;
123 * vdev-wide space maps that have lots of entries written to them at
124 * the end of each transaction can benefit from a higher I/O bandwidth
125 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
127 int zfs_vdev_standard_sm_blksz
= (1 << 17);
130 * Tunable parameter for debugging or performance analysis. Setting this
131 * will cause pool corruption on power loss if a volatile out-of-order
132 * write cache is enabled.
134 int zfs_nocacheflush
= 0;
136 uint64_t zfs_vdev_max_auto_ashift
= ASHIFT_MAX
;
137 uint64_t zfs_vdev_min_auto_ashift
= ASHIFT_MIN
;
141 vdev_dbgmsg(vdev_t
*vd
, const char *fmt
, ...)
147 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
150 if (vd
->vdev_path
!= NULL
) {
151 zfs_dbgmsg("%s vdev '%s': %s", vd
->vdev_ops
->vdev_op_type
,
154 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
155 vd
->vdev_ops
->vdev_op_type
,
156 (u_longlong_t
)vd
->vdev_id
,
157 (u_longlong_t
)vd
->vdev_guid
, buf
);
162 vdev_dbgmsg_print_tree(vdev_t
*vd
, int indent
)
166 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
) {
167 zfs_dbgmsg("%*svdev %u: %s", indent
, "", vd
->vdev_id
,
168 vd
->vdev_ops
->vdev_op_type
);
172 switch (vd
->vdev_state
) {
173 case VDEV_STATE_UNKNOWN
:
174 (void) snprintf(state
, sizeof (state
), "unknown");
176 case VDEV_STATE_CLOSED
:
177 (void) snprintf(state
, sizeof (state
), "closed");
179 case VDEV_STATE_OFFLINE
:
180 (void) snprintf(state
, sizeof (state
), "offline");
182 case VDEV_STATE_REMOVED
:
183 (void) snprintf(state
, sizeof (state
), "removed");
185 case VDEV_STATE_CANT_OPEN
:
186 (void) snprintf(state
, sizeof (state
), "can't open");
188 case VDEV_STATE_FAULTED
:
189 (void) snprintf(state
, sizeof (state
), "faulted");
191 case VDEV_STATE_DEGRADED
:
192 (void) snprintf(state
, sizeof (state
), "degraded");
194 case VDEV_STATE_HEALTHY
:
195 (void) snprintf(state
, sizeof (state
), "healthy");
198 (void) snprintf(state
, sizeof (state
), "<state %u>",
199 (uint_t
)vd
->vdev_state
);
202 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent
,
203 "", (int)vd
->vdev_id
, vd
->vdev_ops
->vdev_op_type
,
204 vd
->vdev_islog
? " (log)" : "",
205 (u_longlong_t
)vd
->vdev_guid
,
206 vd
->vdev_path
? vd
->vdev_path
: "N/A", state
);
208 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++)
209 vdev_dbgmsg_print_tree(vd
->vdev_child
[i
], indent
+ 2);
213 * Virtual device management.
216 static vdev_ops_t
*vdev_ops_table
[] = {
220 &vdev_draid_spare_ops
,
233 * Given a vdev type, return the appropriate ops vector.
236 vdev_getops(const char *type
)
238 vdev_ops_t
*ops
, **opspp
;
240 for (opspp
= vdev_ops_table
; (ops
= *opspp
) != NULL
; opspp
++)
241 if (strcmp(ops
->vdev_op_type
, type
) == 0)
248 * Given a vdev and a metaslab class, find which metaslab group we're
249 * interested in. All vdevs may belong to two different metaslab classes.
250 * Dedicated slog devices use only the primary metaslab group, rather than a
251 * separate log group. For embedded slogs, the vdev_log_mg will be non-NULL.
254 vdev_get_mg(vdev_t
*vd
, metaslab_class_t
*mc
)
256 if (mc
== spa_embedded_log_class(vd
->vdev_spa
) &&
257 vd
->vdev_log_mg
!= NULL
)
258 return (vd
->vdev_log_mg
);
260 return (vd
->vdev_mg
);
265 vdev_default_xlate(vdev_t
*vd
, const range_seg64_t
*logical_rs
,
266 range_seg64_t
*physical_rs
, range_seg64_t
*remain_rs
)
268 physical_rs
->rs_start
= logical_rs
->rs_start
;
269 physical_rs
->rs_end
= logical_rs
->rs_end
;
273 * Derive the enumerated allocation bias from string input.
274 * String origin is either the per-vdev zap or zpool(8).
276 static vdev_alloc_bias_t
277 vdev_derive_alloc_bias(const char *bias
)
279 vdev_alloc_bias_t alloc_bias
= VDEV_BIAS_NONE
;
281 if (strcmp(bias
, VDEV_ALLOC_BIAS_LOG
) == 0)
282 alloc_bias
= VDEV_BIAS_LOG
;
283 else if (strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
284 alloc_bias
= VDEV_BIAS_SPECIAL
;
285 else if (strcmp(bias
, VDEV_ALLOC_BIAS_DEDUP
) == 0)
286 alloc_bias
= VDEV_BIAS_DEDUP
;
292 * Default asize function: return the MAX of psize with the asize of
293 * all children. This is what's used by anything other than RAID-Z.
296 vdev_default_asize(vdev_t
*vd
, uint64_t psize
)
298 uint64_t asize
= P2ROUNDUP(psize
, 1ULL << vd
->vdev_top
->vdev_ashift
);
301 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
302 csize
= vdev_psize_to_asize(vd
->vdev_child
[c
], psize
);
303 asize
= MAX(asize
, csize
);
310 vdev_default_min_asize(vdev_t
*vd
)
312 return (vd
->vdev_min_asize
);
316 * Get the minimum allocatable size. We define the allocatable size as
317 * the vdev's asize rounded to the nearest metaslab. This allows us to
318 * replace or attach devices which don't have the same physical size but
319 * can still satisfy the same number of allocations.
322 vdev_get_min_asize(vdev_t
*vd
)
324 vdev_t
*pvd
= vd
->vdev_parent
;
327 * If our parent is NULL (inactive spare or cache) or is the root,
328 * just return our own asize.
331 return (vd
->vdev_asize
);
334 * The top-level vdev just returns the allocatable size rounded
335 * to the nearest metaslab.
337 if (vd
== vd
->vdev_top
)
338 return (P2ALIGN(vd
->vdev_asize
, 1ULL << vd
->vdev_ms_shift
));
340 return (pvd
->vdev_ops
->vdev_op_min_asize(pvd
));
344 vdev_set_min_asize(vdev_t
*vd
)
346 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
348 for (int c
= 0; c
< vd
->vdev_children
; c
++)
349 vdev_set_min_asize(vd
->vdev_child
[c
]);
353 * Get the minimal allocation size for the top-level vdev.
356 vdev_get_min_alloc(vdev_t
*vd
)
358 uint64_t min_alloc
= 1ULL << vd
->vdev_ashift
;
360 if (vd
->vdev_ops
->vdev_op_min_alloc
!= NULL
)
361 min_alloc
= vd
->vdev_ops
->vdev_op_min_alloc(vd
);
367 * Get the parity level for a top-level vdev.
370 vdev_get_nparity(vdev_t
*vd
)
372 uint64_t nparity
= 0;
374 if (vd
->vdev_ops
->vdev_op_nparity
!= NULL
)
375 nparity
= vd
->vdev_ops
->vdev_op_nparity(vd
);
381 * Get the number of data disks for a top-level vdev.
384 vdev_get_ndisks(vdev_t
*vd
)
388 if (vd
->vdev_ops
->vdev_op_ndisks
!= NULL
)
389 ndisks
= vd
->vdev_ops
->vdev_op_ndisks(vd
);
395 vdev_lookup_top(spa_t
*spa
, uint64_t vdev
)
397 vdev_t
*rvd
= spa
->spa_root_vdev
;
399 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
401 if (vdev
< rvd
->vdev_children
) {
402 ASSERT(rvd
->vdev_child
[vdev
] != NULL
);
403 return (rvd
->vdev_child
[vdev
]);
410 vdev_lookup_by_guid(vdev_t
*vd
, uint64_t guid
)
414 if (vd
->vdev_guid
== guid
)
417 for (int c
= 0; c
< vd
->vdev_children
; c
++)
418 if ((mvd
= vdev_lookup_by_guid(vd
->vdev_child
[c
], guid
)) !=
426 vdev_count_leaves_impl(vdev_t
*vd
)
430 if (vd
->vdev_ops
->vdev_op_leaf
)
433 for (int c
= 0; c
< vd
->vdev_children
; c
++)
434 n
+= vdev_count_leaves_impl(vd
->vdev_child
[c
]);
440 vdev_count_leaves(spa_t
*spa
)
444 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
445 rc
= vdev_count_leaves_impl(spa
->spa_root_vdev
);
446 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
452 vdev_add_child(vdev_t
*pvd
, vdev_t
*cvd
)
454 size_t oldsize
, newsize
;
455 uint64_t id
= cvd
->vdev_id
;
458 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
459 ASSERT(cvd
->vdev_parent
== NULL
);
461 cvd
->vdev_parent
= pvd
;
466 ASSERT(id
>= pvd
->vdev_children
|| pvd
->vdev_child
[id
] == NULL
);
468 oldsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
469 pvd
->vdev_children
= MAX(pvd
->vdev_children
, id
+ 1);
470 newsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
472 newchild
= kmem_alloc(newsize
, KM_SLEEP
);
473 if (pvd
->vdev_child
!= NULL
) {
474 bcopy(pvd
->vdev_child
, newchild
, oldsize
);
475 kmem_free(pvd
->vdev_child
, oldsize
);
478 pvd
->vdev_child
= newchild
;
479 pvd
->vdev_child
[id
] = cvd
;
481 cvd
->vdev_top
= (pvd
->vdev_top
? pvd
->vdev_top
: cvd
);
482 ASSERT(cvd
->vdev_top
->vdev_parent
->vdev_parent
== NULL
);
485 * Walk up all ancestors to update guid sum.
487 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
488 pvd
->vdev_guid_sum
+= cvd
->vdev_guid_sum
;
490 if (cvd
->vdev_ops
->vdev_op_leaf
) {
491 list_insert_head(&cvd
->vdev_spa
->spa_leaf_list
, cvd
);
492 cvd
->vdev_spa
->spa_leaf_list_gen
++;
497 vdev_remove_child(vdev_t
*pvd
, vdev_t
*cvd
)
500 uint_t id
= cvd
->vdev_id
;
502 ASSERT(cvd
->vdev_parent
== pvd
);
507 ASSERT(id
< pvd
->vdev_children
);
508 ASSERT(pvd
->vdev_child
[id
] == cvd
);
510 pvd
->vdev_child
[id
] = NULL
;
511 cvd
->vdev_parent
= NULL
;
513 for (c
= 0; c
< pvd
->vdev_children
; c
++)
514 if (pvd
->vdev_child
[c
])
517 if (c
== pvd
->vdev_children
) {
518 kmem_free(pvd
->vdev_child
, c
* sizeof (vdev_t
*));
519 pvd
->vdev_child
= NULL
;
520 pvd
->vdev_children
= 0;
523 if (cvd
->vdev_ops
->vdev_op_leaf
) {
524 spa_t
*spa
= cvd
->vdev_spa
;
525 list_remove(&spa
->spa_leaf_list
, cvd
);
526 spa
->spa_leaf_list_gen
++;
530 * Walk up all ancestors to update guid sum.
532 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
533 pvd
->vdev_guid_sum
-= cvd
->vdev_guid_sum
;
537 * Remove any holes in the child array.
540 vdev_compact_children(vdev_t
*pvd
)
542 vdev_t
**newchild
, *cvd
;
543 int oldc
= pvd
->vdev_children
;
546 ASSERT(spa_config_held(pvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
551 for (int c
= newc
= 0; c
< oldc
; c
++)
552 if (pvd
->vdev_child
[c
])
556 newchild
= kmem_zalloc(newc
* sizeof (vdev_t
*), KM_SLEEP
);
558 for (int c
= newc
= 0; c
< oldc
; c
++) {
559 if ((cvd
= pvd
->vdev_child
[c
]) != NULL
) {
560 newchild
[newc
] = cvd
;
561 cvd
->vdev_id
= newc
++;
568 kmem_free(pvd
->vdev_child
, oldc
* sizeof (vdev_t
*));
569 pvd
->vdev_child
= newchild
;
570 pvd
->vdev_children
= newc
;
574 * Allocate and minimally initialize a vdev_t.
577 vdev_alloc_common(spa_t
*spa
, uint_t id
, uint64_t guid
, vdev_ops_t
*ops
)
580 vdev_indirect_config_t
*vic
;
582 vd
= kmem_zalloc(sizeof (vdev_t
), KM_SLEEP
);
583 vic
= &vd
->vdev_indirect_config
;
585 if (spa
->spa_root_vdev
== NULL
) {
586 ASSERT(ops
== &vdev_root_ops
);
587 spa
->spa_root_vdev
= vd
;
588 spa
->spa_load_guid
= spa_generate_guid(NULL
);
591 if (guid
== 0 && ops
!= &vdev_hole_ops
) {
592 if (spa
->spa_root_vdev
== vd
) {
594 * The root vdev's guid will also be the pool guid,
595 * which must be unique among all pools.
597 guid
= spa_generate_guid(NULL
);
600 * Any other vdev's guid must be unique within the pool.
602 guid
= spa_generate_guid(spa
);
604 ASSERT(!spa_guid_exists(spa_guid(spa
), guid
));
609 vd
->vdev_guid
= guid
;
610 vd
->vdev_guid_sum
= guid
;
612 vd
->vdev_state
= VDEV_STATE_CLOSED
;
613 vd
->vdev_ishole
= (ops
== &vdev_hole_ops
);
614 vic
->vic_prev_indirect_vdev
= UINT64_MAX
;
616 rw_init(&vd
->vdev_indirect_rwlock
, NULL
, RW_DEFAULT
, NULL
);
617 mutex_init(&vd
->vdev_obsolete_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
618 vd
->vdev_obsolete_segments
= range_tree_create(NULL
, RANGE_SEG64
, NULL
,
622 * Initialize rate limit structs for events. We rate limit ZIO delay
623 * and checksum events so that we don't overwhelm ZED with thousands
624 * of events when a disk is acting up.
626 zfs_ratelimit_init(&vd
->vdev_delay_rl
, &zfs_slow_io_events_per_second
,
628 zfs_ratelimit_init(&vd
->vdev_checksum_rl
,
629 &zfs_checksum_events_per_second
, 1);
631 list_link_init(&vd
->vdev_config_dirty_node
);
632 list_link_init(&vd
->vdev_state_dirty_node
);
633 list_link_init(&vd
->vdev_initialize_node
);
634 list_link_init(&vd
->vdev_leaf_node
);
635 list_link_init(&vd
->vdev_trim_node
);
637 mutex_init(&vd
->vdev_dtl_lock
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
638 mutex_init(&vd
->vdev_stat_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
639 mutex_init(&vd
->vdev_probe_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
640 mutex_init(&vd
->vdev_scan_io_queue_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
642 mutex_init(&vd
->vdev_initialize_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
643 mutex_init(&vd
->vdev_initialize_io_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
644 cv_init(&vd
->vdev_initialize_cv
, NULL
, CV_DEFAULT
, NULL
);
645 cv_init(&vd
->vdev_initialize_io_cv
, NULL
, CV_DEFAULT
, NULL
);
647 mutex_init(&vd
->vdev_trim_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
648 mutex_init(&vd
->vdev_autotrim_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
649 mutex_init(&vd
->vdev_trim_io_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
650 cv_init(&vd
->vdev_trim_cv
, NULL
, CV_DEFAULT
, NULL
);
651 cv_init(&vd
->vdev_autotrim_cv
, NULL
, CV_DEFAULT
, NULL
);
652 cv_init(&vd
->vdev_trim_io_cv
, NULL
, CV_DEFAULT
, NULL
);
654 mutex_init(&vd
->vdev_rebuild_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
655 cv_init(&vd
->vdev_rebuild_cv
, NULL
, CV_DEFAULT
, NULL
);
657 for (int t
= 0; t
< DTL_TYPES
; t
++) {
658 vd
->vdev_dtl
[t
] = range_tree_create(NULL
, RANGE_SEG64
, NULL
, 0,
662 txg_list_create(&vd
->vdev_ms_list
, spa
,
663 offsetof(struct metaslab
, ms_txg_node
));
664 txg_list_create(&vd
->vdev_dtl_list
, spa
,
665 offsetof(struct vdev
, vdev_dtl_node
));
666 vd
->vdev_stat
.vs_timestamp
= gethrtime();
674 * Allocate a new vdev. The 'alloctype' is used to control whether we are
675 * creating a new vdev or loading an existing one - the behavior is slightly
676 * different for each case.
679 vdev_alloc(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
, uint_t id
,
684 uint64_t guid
= 0, islog
;
686 vdev_indirect_config_t
*vic
;
689 vdev_alloc_bias_t alloc_bias
= VDEV_BIAS_NONE
;
690 boolean_t top_level
= (parent
&& !parent
->vdev_parent
);
692 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
694 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
695 return (SET_ERROR(EINVAL
));
697 if ((ops
= vdev_getops(type
)) == NULL
)
698 return (SET_ERROR(EINVAL
));
701 * If this is a load, get the vdev guid from the nvlist.
702 * Otherwise, vdev_alloc_common() will generate one for us.
704 if (alloctype
== VDEV_ALLOC_LOAD
) {
707 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
, &label_id
) ||
709 return (SET_ERROR(EINVAL
));
711 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
712 return (SET_ERROR(EINVAL
));
713 } else if (alloctype
== VDEV_ALLOC_SPARE
) {
714 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
715 return (SET_ERROR(EINVAL
));
716 } else if (alloctype
== VDEV_ALLOC_L2CACHE
) {
717 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
718 return (SET_ERROR(EINVAL
));
719 } else if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
720 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
721 return (SET_ERROR(EINVAL
));
725 * The first allocated vdev must be of type 'root'.
727 if (ops
!= &vdev_root_ops
&& spa
->spa_root_vdev
== NULL
)
728 return (SET_ERROR(EINVAL
));
731 * Determine whether we're a log vdev.
734 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &islog
);
735 if (islog
&& spa_version(spa
) < SPA_VERSION_SLOGS
)
736 return (SET_ERROR(ENOTSUP
));
738 if (ops
== &vdev_hole_ops
&& spa_version(spa
) < SPA_VERSION_HOLES
)
739 return (SET_ERROR(ENOTSUP
));
741 if (top_level
&& alloctype
== VDEV_ALLOC_ADD
) {
745 * If creating a top-level vdev, check for allocation
748 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_ALLOCATION_BIAS
,
750 alloc_bias
= vdev_derive_alloc_bias(bias
);
752 /* spa_vdev_add() expects feature to be enabled */
753 if (spa
->spa_load_state
!= SPA_LOAD_CREATE
&&
754 !spa_feature_is_enabled(spa
,
755 SPA_FEATURE_ALLOCATION_CLASSES
)) {
756 return (SET_ERROR(ENOTSUP
));
760 /* spa_vdev_add() expects feature to be enabled */
761 if (ops
== &vdev_draid_ops
&&
762 spa
->spa_load_state
!= SPA_LOAD_CREATE
&&
763 !spa_feature_is_enabled(spa
, SPA_FEATURE_DRAID
)) {
764 return (SET_ERROR(ENOTSUP
));
769 * Initialize the vdev specific data. This is done before calling
770 * vdev_alloc_common() since it may fail and this simplifies the
771 * error reporting and cleanup code paths.
774 if (ops
->vdev_op_init
!= NULL
) {
775 rc
= ops
->vdev_op_init(spa
, nv
, &tsd
);
781 vd
= vdev_alloc_common(spa
, id
, guid
, ops
);
783 vd
->vdev_islog
= islog
;
785 if (top_level
&& alloc_bias
!= VDEV_BIAS_NONE
)
786 vd
->vdev_alloc_bias
= alloc_bias
;
788 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &vd
->vdev_path
) == 0)
789 vd
->vdev_path
= spa_strdup(vd
->vdev_path
);
792 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
793 * fault on a vdev and want it to persist across imports (like with
796 rc
= nvlist_lookup_string(nv
, ZPOOL_CONFIG_AUX_STATE
, &tmp
);
797 if (rc
== 0 && tmp
!= NULL
&& strcmp(tmp
, "external") == 0) {
798 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
799 vd
->vdev_faulted
= 1;
800 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
803 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &vd
->vdev_devid
) == 0)
804 vd
->vdev_devid
= spa_strdup(vd
->vdev_devid
);
805 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PHYS_PATH
,
806 &vd
->vdev_physpath
) == 0)
807 vd
->vdev_physpath
= spa_strdup(vd
->vdev_physpath
);
809 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH
,
810 &vd
->vdev_enc_sysfs_path
) == 0)
811 vd
->vdev_enc_sysfs_path
= spa_strdup(vd
->vdev_enc_sysfs_path
);
813 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_FRU
, &vd
->vdev_fru
) == 0)
814 vd
->vdev_fru
= spa_strdup(vd
->vdev_fru
);
817 * Set the whole_disk property. If it's not specified, leave the value
820 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
821 &vd
->vdev_wholedisk
) != 0)
822 vd
->vdev_wholedisk
= -1ULL;
824 vic
= &vd
->vdev_indirect_config
;
826 ASSERT0(vic
->vic_mapping_object
);
827 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_OBJECT
,
828 &vic
->vic_mapping_object
);
829 ASSERT0(vic
->vic_births_object
);
830 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_BIRTHS
,
831 &vic
->vic_births_object
);
832 ASSERT3U(vic
->vic_prev_indirect_vdev
, ==, UINT64_MAX
);
833 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_PREV_INDIRECT_VDEV
,
834 &vic
->vic_prev_indirect_vdev
);
837 * Look for the 'not present' flag. This will only be set if the device
838 * was not present at the time of import.
840 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
841 &vd
->vdev_not_present
);
844 * Get the alignment requirement.
846 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASHIFT
, &vd
->vdev_ashift
);
849 * Retrieve the vdev creation time.
851 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_CREATE_TXG
,
855 * If we're a top-level vdev, try to load the allocation parameters.
858 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
859 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_ARRAY
,
861 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_SHIFT
,
863 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASIZE
,
865 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVING
,
867 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_VDEV_TOP_ZAP
,
870 ASSERT0(vd
->vdev_top_zap
);
873 if (top_level
&& alloctype
!= VDEV_ALLOC_ATTACH
) {
874 ASSERT(alloctype
== VDEV_ALLOC_LOAD
||
875 alloctype
== VDEV_ALLOC_ADD
||
876 alloctype
== VDEV_ALLOC_SPLIT
||
877 alloctype
== VDEV_ALLOC_ROOTPOOL
);
878 /* Note: metaslab_group_create() is now deferred */
881 if (vd
->vdev_ops
->vdev_op_leaf
&&
882 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
883 (void) nvlist_lookup_uint64(nv
,
884 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, &vd
->vdev_leaf_zap
);
886 ASSERT0(vd
->vdev_leaf_zap
);
890 * If we're a leaf vdev, try to load the DTL object and other state.
893 if (vd
->vdev_ops
->vdev_op_leaf
&&
894 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_L2CACHE
||
895 alloctype
== VDEV_ALLOC_ROOTPOOL
)) {
896 if (alloctype
== VDEV_ALLOC_LOAD
) {
897 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DTL
,
898 &vd
->vdev_dtl_object
);
899 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_UNSPARE
,
903 if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
906 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
907 &spare
) == 0 && spare
)
911 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
,
914 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_RESILVER_TXG
,
915 &vd
->vdev_resilver_txg
);
917 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REBUILD_TXG
,
918 &vd
->vdev_rebuild_txg
);
920 if (nvlist_exists(nv
, ZPOOL_CONFIG_RESILVER_DEFER
))
921 vdev_defer_resilver(vd
);
924 * In general, when importing a pool we want to ignore the
925 * persistent fault state, as the diagnosis made on another
926 * system may not be valid in the current context. The only
927 * exception is if we forced a vdev to a persistently faulted
928 * state with 'zpool offline -f'. The persistent fault will
929 * remain across imports until cleared.
931 * Local vdevs will remain in the faulted state.
933 if (spa_load_state(spa
) == SPA_LOAD_OPEN
||
934 spa_load_state(spa
) == SPA_LOAD_IMPORT
) {
935 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
,
937 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DEGRADED
,
939 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
,
942 if (vd
->vdev_faulted
|| vd
->vdev_degraded
) {
946 VDEV_AUX_ERR_EXCEEDED
;
947 if (nvlist_lookup_string(nv
,
948 ZPOOL_CONFIG_AUX_STATE
, &aux
) == 0 &&
949 strcmp(aux
, "external") == 0)
950 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
952 vd
->vdev_faulted
= 0ULL;
958 * Add ourselves to the parent's list of children.
960 vdev_add_child(parent
, vd
);
968 vdev_free(vdev_t
*vd
)
970 spa_t
*spa
= vd
->vdev_spa
;
972 ASSERT3P(vd
->vdev_initialize_thread
, ==, NULL
);
973 ASSERT3P(vd
->vdev_trim_thread
, ==, NULL
);
974 ASSERT3P(vd
->vdev_autotrim_thread
, ==, NULL
);
975 ASSERT3P(vd
->vdev_rebuild_thread
, ==, NULL
);
978 * Scan queues are normally destroyed at the end of a scan. If the
979 * queue exists here, that implies the vdev is being removed while
980 * the scan is still running.
982 if (vd
->vdev_scan_io_queue
!= NULL
) {
983 mutex_enter(&vd
->vdev_scan_io_queue_lock
);
984 dsl_scan_io_queue_destroy(vd
->vdev_scan_io_queue
);
985 vd
->vdev_scan_io_queue
= NULL
;
986 mutex_exit(&vd
->vdev_scan_io_queue_lock
);
990 * vdev_free() implies closing the vdev first. This is simpler than
991 * trying to ensure complicated semantics for all callers.
995 ASSERT(!list_link_active(&vd
->vdev_config_dirty_node
));
996 ASSERT(!list_link_active(&vd
->vdev_state_dirty_node
));
1001 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1002 vdev_free(vd
->vdev_child
[c
]);
1004 ASSERT(vd
->vdev_child
== NULL
);
1005 ASSERT(vd
->vdev_guid_sum
== vd
->vdev_guid
);
1007 if (vd
->vdev_ops
->vdev_op_fini
!= NULL
)
1008 vd
->vdev_ops
->vdev_op_fini(vd
);
1011 * Discard allocation state.
1013 if (vd
->vdev_mg
!= NULL
) {
1014 vdev_metaslab_fini(vd
);
1015 metaslab_group_destroy(vd
->vdev_mg
);
1018 if (vd
->vdev_log_mg
!= NULL
) {
1019 ASSERT0(vd
->vdev_ms_count
);
1020 metaslab_group_destroy(vd
->vdev_log_mg
);
1021 vd
->vdev_log_mg
= NULL
;
1024 ASSERT0(vd
->vdev_stat
.vs_space
);
1025 ASSERT0(vd
->vdev_stat
.vs_dspace
);
1026 ASSERT0(vd
->vdev_stat
.vs_alloc
);
1029 * Remove this vdev from its parent's child list.
1031 vdev_remove_child(vd
->vdev_parent
, vd
);
1033 ASSERT(vd
->vdev_parent
== NULL
);
1034 ASSERT(!list_link_active(&vd
->vdev_leaf_node
));
1037 * Clean up vdev structure.
1039 vdev_queue_fini(vd
);
1040 vdev_cache_fini(vd
);
1043 spa_strfree(vd
->vdev_path
);
1045 spa_strfree(vd
->vdev_devid
);
1046 if (vd
->vdev_physpath
)
1047 spa_strfree(vd
->vdev_physpath
);
1049 if (vd
->vdev_enc_sysfs_path
)
1050 spa_strfree(vd
->vdev_enc_sysfs_path
);
1053 spa_strfree(vd
->vdev_fru
);
1055 if (vd
->vdev_isspare
)
1056 spa_spare_remove(vd
);
1057 if (vd
->vdev_isl2cache
)
1058 spa_l2cache_remove(vd
);
1060 txg_list_destroy(&vd
->vdev_ms_list
);
1061 txg_list_destroy(&vd
->vdev_dtl_list
);
1063 mutex_enter(&vd
->vdev_dtl_lock
);
1064 space_map_close(vd
->vdev_dtl_sm
);
1065 for (int t
= 0; t
< DTL_TYPES
; t
++) {
1066 range_tree_vacate(vd
->vdev_dtl
[t
], NULL
, NULL
);
1067 range_tree_destroy(vd
->vdev_dtl
[t
]);
1069 mutex_exit(&vd
->vdev_dtl_lock
);
1071 EQUIV(vd
->vdev_indirect_births
!= NULL
,
1072 vd
->vdev_indirect_mapping
!= NULL
);
1073 if (vd
->vdev_indirect_births
!= NULL
) {
1074 vdev_indirect_mapping_close(vd
->vdev_indirect_mapping
);
1075 vdev_indirect_births_close(vd
->vdev_indirect_births
);
1078 if (vd
->vdev_obsolete_sm
!= NULL
) {
1079 ASSERT(vd
->vdev_removing
||
1080 vd
->vdev_ops
== &vdev_indirect_ops
);
1081 space_map_close(vd
->vdev_obsolete_sm
);
1082 vd
->vdev_obsolete_sm
= NULL
;
1084 range_tree_destroy(vd
->vdev_obsolete_segments
);
1085 rw_destroy(&vd
->vdev_indirect_rwlock
);
1086 mutex_destroy(&vd
->vdev_obsolete_lock
);
1088 mutex_destroy(&vd
->vdev_dtl_lock
);
1089 mutex_destroy(&vd
->vdev_stat_lock
);
1090 mutex_destroy(&vd
->vdev_probe_lock
);
1091 mutex_destroy(&vd
->vdev_scan_io_queue_lock
);
1093 mutex_destroy(&vd
->vdev_initialize_lock
);
1094 mutex_destroy(&vd
->vdev_initialize_io_lock
);
1095 cv_destroy(&vd
->vdev_initialize_io_cv
);
1096 cv_destroy(&vd
->vdev_initialize_cv
);
1098 mutex_destroy(&vd
->vdev_trim_lock
);
1099 mutex_destroy(&vd
->vdev_autotrim_lock
);
1100 mutex_destroy(&vd
->vdev_trim_io_lock
);
1101 cv_destroy(&vd
->vdev_trim_cv
);
1102 cv_destroy(&vd
->vdev_autotrim_cv
);
1103 cv_destroy(&vd
->vdev_trim_io_cv
);
1105 mutex_destroy(&vd
->vdev_rebuild_lock
);
1106 cv_destroy(&vd
->vdev_rebuild_cv
);
1108 zfs_ratelimit_fini(&vd
->vdev_delay_rl
);
1109 zfs_ratelimit_fini(&vd
->vdev_checksum_rl
);
1111 if (vd
== spa
->spa_root_vdev
)
1112 spa
->spa_root_vdev
= NULL
;
1114 kmem_free(vd
, sizeof (vdev_t
));
1118 * Transfer top-level vdev state from svd to tvd.
1121 vdev_top_transfer(vdev_t
*svd
, vdev_t
*tvd
)
1123 spa_t
*spa
= svd
->vdev_spa
;
1128 ASSERT(tvd
== tvd
->vdev_top
);
1130 tvd
->vdev_pending_fastwrite
= svd
->vdev_pending_fastwrite
;
1131 tvd
->vdev_ms_array
= svd
->vdev_ms_array
;
1132 tvd
->vdev_ms_shift
= svd
->vdev_ms_shift
;
1133 tvd
->vdev_ms_count
= svd
->vdev_ms_count
;
1134 tvd
->vdev_top_zap
= svd
->vdev_top_zap
;
1136 svd
->vdev_ms_array
= 0;
1137 svd
->vdev_ms_shift
= 0;
1138 svd
->vdev_ms_count
= 0;
1139 svd
->vdev_top_zap
= 0;
1142 ASSERT3P(tvd
->vdev_mg
, ==, svd
->vdev_mg
);
1143 if (tvd
->vdev_log_mg
)
1144 ASSERT3P(tvd
->vdev_log_mg
, ==, svd
->vdev_log_mg
);
1145 tvd
->vdev_mg
= svd
->vdev_mg
;
1146 tvd
->vdev_log_mg
= svd
->vdev_log_mg
;
1147 tvd
->vdev_ms
= svd
->vdev_ms
;
1149 svd
->vdev_mg
= NULL
;
1150 svd
->vdev_log_mg
= NULL
;
1151 svd
->vdev_ms
= NULL
;
1153 if (tvd
->vdev_mg
!= NULL
)
1154 tvd
->vdev_mg
->mg_vd
= tvd
;
1155 if (tvd
->vdev_log_mg
!= NULL
)
1156 tvd
->vdev_log_mg
->mg_vd
= tvd
;
1158 tvd
->vdev_checkpoint_sm
= svd
->vdev_checkpoint_sm
;
1159 svd
->vdev_checkpoint_sm
= NULL
;
1161 tvd
->vdev_alloc_bias
= svd
->vdev_alloc_bias
;
1162 svd
->vdev_alloc_bias
= VDEV_BIAS_NONE
;
1164 tvd
->vdev_stat
.vs_alloc
= svd
->vdev_stat
.vs_alloc
;
1165 tvd
->vdev_stat
.vs_space
= svd
->vdev_stat
.vs_space
;
1166 tvd
->vdev_stat
.vs_dspace
= svd
->vdev_stat
.vs_dspace
;
1168 svd
->vdev_stat
.vs_alloc
= 0;
1169 svd
->vdev_stat
.vs_space
= 0;
1170 svd
->vdev_stat
.vs_dspace
= 0;
1173 * State which may be set on a top-level vdev that's in the
1174 * process of being removed.
1176 ASSERT0(tvd
->vdev_indirect_config
.vic_births_object
);
1177 ASSERT0(tvd
->vdev_indirect_config
.vic_mapping_object
);
1178 ASSERT3U(tvd
->vdev_indirect_config
.vic_prev_indirect_vdev
, ==, -1ULL);
1179 ASSERT3P(tvd
->vdev_indirect_mapping
, ==, NULL
);
1180 ASSERT3P(tvd
->vdev_indirect_births
, ==, NULL
);
1181 ASSERT3P(tvd
->vdev_obsolete_sm
, ==, NULL
);
1182 ASSERT0(tvd
->vdev_removing
);
1183 ASSERT0(tvd
->vdev_rebuilding
);
1184 tvd
->vdev_removing
= svd
->vdev_removing
;
1185 tvd
->vdev_rebuilding
= svd
->vdev_rebuilding
;
1186 tvd
->vdev_rebuild_config
= svd
->vdev_rebuild_config
;
1187 tvd
->vdev_indirect_config
= svd
->vdev_indirect_config
;
1188 tvd
->vdev_indirect_mapping
= svd
->vdev_indirect_mapping
;
1189 tvd
->vdev_indirect_births
= svd
->vdev_indirect_births
;
1190 range_tree_swap(&svd
->vdev_obsolete_segments
,
1191 &tvd
->vdev_obsolete_segments
);
1192 tvd
->vdev_obsolete_sm
= svd
->vdev_obsolete_sm
;
1193 svd
->vdev_indirect_config
.vic_mapping_object
= 0;
1194 svd
->vdev_indirect_config
.vic_births_object
= 0;
1195 svd
->vdev_indirect_config
.vic_prev_indirect_vdev
= -1ULL;
1196 svd
->vdev_indirect_mapping
= NULL
;
1197 svd
->vdev_indirect_births
= NULL
;
1198 svd
->vdev_obsolete_sm
= NULL
;
1199 svd
->vdev_removing
= 0;
1200 svd
->vdev_rebuilding
= 0;
1202 for (t
= 0; t
< TXG_SIZE
; t
++) {
1203 while ((msp
= txg_list_remove(&svd
->vdev_ms_list
, t
)) != NULL
)
1204 (void) txg_list_add(&tvd
->vdev_ms_list
, msp
, t
);
1205 while ((vd
= txg_list_remove(&svd
->vdev_dtl_list
, t
)) != NULL
)
1206 (void) txg_list_add(&tvd
->vdev_dtl_list
, vd
, t
);
1207 if (txg_list_remove_this(&spa
->spa_vdev_txg_list
, svd
, t
))
1208 (void) txg_list_add(&spa
->spa_vdev_txg_list
, tvd
, t
);
1211 if (list_link_active(&svd
->vdev_config_dirty_node
)) {
1212 vdev_config_clean(svd
);
1213 vdev_config_dirty(tvd
);
1216 if (list_link_active(&svd
->vdev_state_dirty_node
)) {
1217 vdev_state_clean(svd
);
1218 vdev_state_dirty(tvd
);
1221 tvd
->vdev_deflate_ratio
= svd
->vdev_deflate_ratio
;
1222 svd
->vdev_deflate_ratio
= 0;
1224 tvd
->vdev_islog
= svd
->vdev_islog
;
1225 svd
->vdev_islog
= 0;
1227 dsl_scan_io_queue_vdev_xfer(svd
, tvd
);
1231 vdev_top_update(vdev_t
*tvd
, vdev_t
*vd
)
1238 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1239 vdev_top_update(tvd
, vd
->vdev_child
[c
]);
1243 * Add a mirror/replacing vdev above an existing vdev. There is no need to
1244 * call .vdev_op_init() since mirror/replacing vdevs do not have private state.
1247 vdev_add_parent(vdev_t
*cvd
, vdev_ops_t
*ops
)
1249 spa_t
*spa
= cvd
->vdev_spa
;
1250 vdev_t
*pvd
= cvd
->vdev_parent
;
1253 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1255 mvd
= vdev_alloc_common(spa
, cvd
->vdev_id
, 0, ops
);
1257 mvd
->vdev_asize
= cvd
->vdev_asize
;
1258 mvd
->vdev_min_asize
= cvd
->vdev_min_asize
;
1259 mvd
->vdev_max_asize
= cvd
->vdev_max_asize
;
1260 mvd
->vdev_psize
= cvd
->vdev_psize
;
1261 mvd
->vdev_ashift
= cvd
->vdev_ashift
;
1262 mvd
->vdev_logical_ashift
= cvd
->vdev_logical_ashift
;
1263 mvd
->vdev_physical_ashift
= cvd
->vdev_physical_ashift
;
1264 mvd
->vdev_state
= cvd
->vdev_state
;
1265 mvd
->vdev_crtxg
= cvd
->vdev_crtxg
;
1267 vdev_remove_child(pvd
, cvd
);
1268 vdev_add_child(pvd
, mvd
);
1269 cvd
->vdev_id
= mvd
->vdev_children
;
1270 vdev_add_child(mvd
, cvd
);
1271 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1273 if (mvd
== mvd
->vdev_top
)
1274 vdev_top_transfer(cvd
, mvd
);
1280 * Remove a 1-way mirror/replacing vdev from the tree.
1283 vdev_remove_parent(vdev_t
*cvd
)
1285 vdev_t
*mvd
= cvd
->vdev_parent
;
1286 vdev_t
*pvd
= mvd
->vdev_parent
;
1288 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1290 ASSERT(mvd
->vdev_children
== 1);
1291 ASSERT(mvd
->vdev_ops
== &vdev_mirror_ops
||
1292 mvd
->vdev_ops
== &vdev_replacing_ops
||
1293 mvd
->vdev_ops
== &vdev_spare_ops
);
1294 cvd
->vdev_ashift
= mvd
->vdev_ashift
;
1295 cvd
->vdev_logical_ashift
= mvd
->vdev_logical_ashift
;
1296 cvd
->vdev_physical_ashift
= mvd
->vdev_physical_ashift
;
1297 vdev_remove_child(mvd
, cvd
);
1298 vdev_remove_child(pvd
, mvd
);
1301 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1302 * Otherwise, we could have detached an offline device, and when we
1303 * go to import the pool we'll think we have two top-level vdevs,
1304 * instead of a different version of the same top-level vdev.
1306 if (mvd
->vdev_top
== mvd
) {
1307 uint64_t guid_delta
= mvd
->vdev_guid
- cvd
->vdev_guid
;
1308 cvd
->vdev_orig_guid
= cvd
->vdev_guid
;
1309 cvd
->vdev_guid
+= guid_delta
;
1310 cvd
->vdev_guid_sum
+= guid_delta
;
1313 * If pool not set for autoexpand, we need to also preserve
1314 * mvd's asize to prevent automatic expansion of cvd.
1315 * Otherwise if we are adjusting the mirror by attaching and
1316 * detaching children of non-uniform sizes, the mirror could
1317 * autoexpand, unexpectedly requiring larger devices to
1318 * re-establish the mirror.
1320 if (!cvd
->vdev_spa
->spa_autoexpand
)
1321 cvd
->vdev_asize
= mvd
->vdev_asize
;
1323 cvd
->vdev_id
= mvd
->vdev_id
;
1324 vdev_add_child(pvd
, cvd
);
1325 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1327 if (cvd
== cvd
->vdev_top
)
1328 vdev_top_transfer(mvd
, cvd
);
1330 ASSERT(mvd
->vdev_children
== 0);
1335 vdev_metaslab_group_create(vdev_t
*vd
)
1337 spa_t
*spa
= vd
->vdev_spa
;
1340 * metaslab_group_create was delayed until allocation bias was available
1342 if (vd
->vdev_mg
== NULL
) {
1343 metaslab_class_t
*mc
;
1345 if (vd
->vdev_islog
&& vd
->vdev_alloc_bias
== VDEV_BIAS_NONE
)
1346 vd
->vdev_alloc_bias
= VDEV_BIAS_LOG
;
1348 ASSERT3U(vd
->vdev_islog
, ==,
1349 (vd
->vdev_alloc_bias
== VDEV_BIAS_LOG
));
1351 switch (vd
->vdev_alloc_bias
) {
1353 mc
= spa_log_class(spa
);
1355 case VDEV_BIAS_SPECIAL
:
1356 mc
= spa_special_class(spa
);
1358 case VDEV_BIAS_DEDUP
:
1359 mc
= spa_dedup_class(spa
);
1362 mc
= spa_normal_class(spa
);
1365 vd
->vdev_mg
= metaslab_group_create(mc
, vd
,
1366 spa
->spa_alloc_count
);
1368 if (!vd
->vdev_islog
) {
1369 vd
->vdev_log_mg
= metaslab_group_create(
1370 spa_embedded_log_class(spa
), vd
, 1);
1374 * The spa ashift min/max only apply for the normal metaslab
1375 * class. Class destination is late binding so ashift boundry
1376 * setting had to wait until now.
1378 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
1379 mc
== spa_normal_class(spa
) && vd
->vdev_aux
== NULL
) {
1380 if (vd
->vdev_ashift
> spa
->spa_max_ashift
)
1381 spa
->spa_max_ashift
= vd
->vdev_ashift
;
1382 if (vd
->vdev_ashift
< spa
->spa_min_ashift
)
1383 spa
->spa_min_ashift
= vd
->vdev_ashift
;
1385 uint64_t min_alloc
= vdev_get_min_alloc(vd
);
1386 if (min_alloc
< spa
->spa_min_alloc
)
1387 spa
->spa_min_alloc
= min_alloc
;
1393 vdev_metaslab_init(vdev_t
*vd
, uint64_t txg
)
1395 spa_t
*spa
= vd
->vdev_spa
;
1396 uint64_t oldc
= vd
->vdev_ms_count
;
1397 uint64_t newc
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
1400 boolean_t expanding
= (oldc
!= 0);
1402 ASSERT(txg
== 0 || spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1405 * This vdev is not being allocated from yet or is a hole.
1407 if (vd
->vdev_ms_shift
== 0)
1410 ASSERT(!vd
->vdev_ishole
);
1412 ASSERT(oldc
<= newc
);
1414 mspp
= vmem_zalloc(newc
* sizeof (*mspp
), KM_SLEEP
);
1417 bcopy(vd
->vdev_ms
, mspp
, oldc
* sizeof (*mspp
));
1418 vmem_free(vd
->vdev_ms
, oldc
* sizeof (*mspp
));
1422 vd
->vdev_ms_count
= newc
;
1424 for (uint64_t m
= oldc
; m
< newc
; m
++) {
1425 uint64_t object
= 0;
1427 * vdev_ms_array may be 0 if we are creating the "fake"
1428 * metaslabs for an indirect vdev for zdb's leak detection.
1429 * See zdb_leak_init().
1431 if (txg
== 0 && vd
->vdev_ms_array
!= 0) {
1432 error
= dmu_read(spa
->spa_meta_objset
,
1434 m
* sizeof (uint64_t), sizeof (uint64_t), &object
,
1437 vdev_dbgmsg(vd
, "unable to read the metaslab "
1438 "array [error=%d]", error
);
1443 error
= metaslab_init(vd
->vdev_mg
, m
, object
, txg
,
1446 vdev_dbgmsg(vd
, "metaslab_init failed [error=%d]",
1453 * Find the emptiest metaslab on the vdev and mark it for use for
1454 * embedded slog by moving it from the regular to the log metaslab
1457 if (vd
->vdev_mg
->mg_class
== spa_normal_class(spa
) &&
1458 vd
->vdev_ms_count
> zfs_embedded_slog_min_ms
&&
1459 avl_is_empty(&vd
->vdev_log_mg
->mg_metaslab_tree
)) {
1460 uint64_t slog_msid
= 0;
1461 uint64_t smallest
= UINT64_MAX
;
1464 * Note, we only search the new metaslabs, because the old
1465 * (pre-existing) ones may be active (e.g. have non-empty
1466 * range_tree's), and we don't move them to the new
1469 for (uint64_t m
= oldc
; m
< newc
; m
++) {
1471 space_map_allocated(vd
->vdev_ms
[m
]->ms_sm
);
1472 if (alloc
< smallest
) {
1477 metaslab_t
*slog_ms
= vd
->vdev_ms
[slog_msid
];
1479 * The metaslab was marked as dirty at the end of
1480 * metaslab_init(). Remove it from the dirty list so that we
1481 * can uninitialize and reinitialize it to the new class.
1484 (void) txg_list_remove_this(&vd
->vdev_ms_list
,
1487 uint64_t sm_obj
= space_map_object(slog_ms
->ms_sm
);
1488 metaslab_fini(slog_ms
);
1489 VERIFY0(metaslab_init(vd
->vdev_log_mg
, slog_msid
, sm_obj
, txg
,
1490 &vd
->vdev_ms
[slog_msid
]));
1494 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_WRITER
);
1497 * If the vdev is being removed we don't activate
1498 * the metaslabs since we want to ensure that no new
1499 * allocations are performed on this device.
1501 if (!expanding
&& !vd
->vdev_removing
) {
1502 metaslab_group_activate(vd
->vdev_mg
);
1503 if (vd
->vdev_log_mg
!= NULL
)
1504 metaslab_group_activate(vd
->vdev_log_mg
);
1508 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
1511 * Regardless whether this vdev was just added or it is being
1512 * expanded, the metaslab count has changed. Recalculate the
1515 spa_log_sm_set_blocklimit(spa
);
1521 vdev_metaslab_fini(vdev_t
*vd
)
1523 if (vd
->vdev_checkpoint_sm
!= NULL
) {
1524 ASSERT(spa_feature_is_active(vd
->vdev_spa
,
1525 SPA_FEATURE_POOL_CHECKPOINT
));
1526 space_map_close(vd
->vdev_checkpoint_sm
);
1528 * Even though we close the space map, we need to set its
1529 * pointer to NULL. The reason is that vdev_metaslab_fini()
1530 * may be called multiple times for certain operations
1531 * (i.e. when destroying a pool) so we need to ensure that
1532 * this clause never executes twice. This logic is similar
1533 * to the one used for the vdev_ms clause below.
1535 vd
->vdev_checkpoint_sm
= NULL
;
1538 if (vd
->vdev_ms
!= NULL
) {
1539 metaslab_group_t
*mg
= vd
->vdev_mg
;
1541 metaslab_group_passivate(mg
);
1542 if (vd
->vdev_log_mg
!= NULL
) {
1543 ASSERT(!vd
->vdev_islog
);
1544 metaslab_group_passivate(vd
->vdev_log_mg
);
1547 uint64_t count
= vd
->vdev_ms_count
;
1548 for (uint64_t m
= 0; m
< count
; m
++) {
1549 metaslab_t
*msp
= vd
->vdev_ms
[m
];
1553 vmem_free(vd
->vdev_ms
, count
* sizeof (metaslab_t
*));
1555 vd
->vdev_ms_count
= 0;
1557 for (int i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++) {
1558 ASSERT0(mg
->mg_histogram
[i
]);
1559 if (vd
->vdev_log_mg
!= NULL
)
1560 ASSERT0(vd
->vdev_log_mg
->mg_histogram
[i
]);
1563 ASSERT0(vd
->vdev_ms_count
);
1564 ASSERT3U(vd
->vdev_pending_fastwrite
, ==, 0);
1567 typedef struct vdev_probe_stats
{
1568 boolean_t vps_readable
;
1569 boolean_t vps_writeable
;
1571 } vdev_probe_stats_t
;
1574 vdev_probe_done(zio_t
*zio
)
1576 spa_t
*spa
= zio
->io_spa
;
1577 vdev_t
*vd
= zio
->io_vd
;
1578 vdev_probe_stats_t
*vps
= zio
->io_private
;
1580 ASSERT(vd
->vdev_probe_zio
!= NULL
);
1582 if (zio
->io_type
== ZIO_TYPE_READ
) {
1583 if (zio
->io_error
== 0)
1584 vps
->vps_readable
= 1;
1585 if (zio
->io_error
== 0 && spa_writeable(spa
)) {
1586 zio_nowait(zio_write_phys(vd
->vdev_probe_zio
, vd
,
1587 zio
->io_offset
, zio
->io_size
, zio
->io_abd
,
1588 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1589 ZIO_PRIORITY_SYNC_WRITE
, vps
->vps_flags
, B_TRUE
));
1591 abd_free(zio
->io_abd
);
1593 } else if (zio
->io_type
== ZIO_TYPE_WRITE
) {
1594 if (zio
->io_error
== 0)
1595 vps
->vps_writeable
= 1;
1596 abd_free(zio
->io_abd
);
1597 } else if (zio
->io_type
== ZIO_TYPE_NULL
) {
1601 vd
->vdev_cant_read
|= !vps
->vps_readable
;
1602 vd
->vdev_cant_write
|= !vps
->vps_writeable
;
1604 if (vdev_readable(vd
) &&
1605 (vdev_writeable(vd
) || !spa_writeable(spa
))) {
1608 ASSERT(zio
->io_error
!= 0);
1609 vdev_dbgmsg(vd
, "failed probe");
1610 (void) zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE
,
1611 spa
, vd
, NULL
, NULL
, 0);
1612 zio
->io_error
= SET_ERROR(ENXIO
);
1615 mutex_enter(&vd
->vdev_probe_lock
);
1616 ASSERT(vd
->vdev_probe_zio
== zio
);
1617 vd
->vdev_probe_zio
= NULL
;
1618 mutex_exit(&vd
->vdev_probe_lock
);
1621 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
)
1622 if (!vdev_accessible(vd
, pio
))
1623 pio
->io_error
= SET_ERROR(ENXIO
);
1625 kmem_free(vps
, sizeof (*vps
));
1630 * Determine whether this device is accessible.
1632 * Read and write to several known locations: the pad regions of each
1633 * vdev label but the first, which we leave alone in case it contains
1637 vdev_probe(vdev_t
*vd
, zio_t
*zio
)
1639 spa_t
*spa
= vd
->vdev_spa
;
1640 vdev_probe_stats_t
*vps
= NULL
;
1643 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1646 * Don't probe the probe.
1648 if (zio
&& (zio
->io_flags
& ZIO_FLAG_PROBE
))
1652 * To prevent 'probe storms' when a device fails, we create
1653 * just one probe i/o at a time. All zios that want to probe
1654 * this vdev will become parents of the probe io.
1656 mutex_enter(&vd
->vdev_probe_lock
);
1658 if ((pio
= vd
->vdev_probe_zio
) == NULL
) {
1659 vps
= kmem_zalloc(sizeof (*vps
), KM_SLEEP
);
1661 vps
->vps_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_PROBE
|
1662 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_AGGREGATE
|
1665 if (spa_config_held(spa
, SCL_ZIO
, RW_WRITER
)) {
1667 * vdev_cant_read and vdev_cant_write can only
1668 * transition from TRUE to FALSE when we have the
1669 * SCL_ZIO lock as writer; otherwise they can only
1670 * transition from FALSE to TRUE. This ensures that
1671 * any zio looking at these values can assume that
1672 * failures persist for the life of the I/O. That's
1673 * important because when a device has intermittent
1674 * connectivity problems, we want to ensure that
1675 * they're ascribed to the device (ENXIO) and not
1678 * Since we hold SCL_ZIO as writer here, clear both
1679 * values so the probe can reevaluate from first
1682 vps
->vps_flags
|= ZIO_FLAG_CONFIG_WRITER
;
1683 vd
->vdev_cant_read
= B_FALSE
;
1684 vd
->vdev_cant_write
= B_FALSE
;
1687 vd
->vdev_probe_zio
= pio
= zio_null(NULL
, spa
, vd
,
1688 vdev_probe_done
, vps
,
1689 vps
->vps_flags
| ZIO_FLAG_DONT_PROPAGATE
);
1692 * We can't change the vdev state in this context, so we
1693 * kick off an async task to do it on our behalf.
1696 vd
->vdev_probe_wanted
= B_TRUE
;
1697 spa_async_request(spa
, SPA_ASYNC_PROBE
);
1702 zio_add_child(zio
, pio
);
1704 mutex_exit(&vd
->vdev_probe_lock
);
1707 ASSERT(zio
!= NULL
);
1711 for (int l
= 1; l
< VDEV_LABELS
; l
++) {
1712 zio_nowait(zio_read_phys(pio
, vd
,
1713 vdev_label_offset(vd
->vdev_psize
, l
,
1714 offsetof(vdev_label_t
, vl_be
)), VDEV_PAD_SIZE
,
1715 abd_alloc_for_io(VDEV_PAD_SIZE
, B_TRUE
),
1716 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1717 ZIO_PRIORITY_SYNC_READ
, vps
->vps_flags
, B_TRUE
));
1728 vdev_open_child(void *arg
)
1732 vd
->vdev_open_thread
= curthread
;
1733 vd
->vdev_open_error
= vdev_open(vd
);
1734 vd
->vdev_open_thread
= NULL
;
1738 vdev_uses_zvols(vdev_t
*vd
)
1741 if (zvol_is_zvol(vd
->vdev_path
))
1745 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1746 if (vdev_uses_zvols(vd
->vdev_child
[c
]))
1753 * Returns B_TRUE if the passed child should be opened.
1756 vdev_default_open_children_func(vdev_t
*vd
)
1762 * Open the requested child vdevs. If any of the leaf vdevs are using
1763 * a ZFS volume then do the opens in a single thread. This avoids a
1764 * deadlock when the current thread is holding the spa_namespace_lock.
1767 vdev_open_children_impl(vdev_t
*vd
, vdev_open_children_func_t
*open_func
)
1769 int children
= vd
->vdev_children
;
1771 taskq_t
*tq
= taskq_create("vdev_open", children
, minclsyspri
,
1772 children
, children
, TASKQ_PREPOPULATE
);
1773 vd
->vdev_nonrot
= B_TRUE
;
1775 for (int c
= 0; c
< children
; c
++) {
1776 vdev_t
*cvd
= vd
->vdev_child
[c
];
1778 if (open_func(cvd
) == B_FALSE
)
1781 if (tq
== NULL
|| vdev_uses_zvols(vd
)) {
1782 cvd
->vdev_open_error
= vdev_open(cvd
);
1784 VERIFY(taskq_dispatch(tq
, vdev_open_child
,
1785 cvd
, TQ_SLEEP
) != TASKQID_INVALID
);
1788 vd
->vdev_nonrot
&= cvd
->vdev_nonrot
;
1798 * Open all child vdevs.
1801 vdev_open_children(vdev_t
*vd
)
1803 vdev_open_children_impl(vd
, vdev_default_open_children_func
);
1807 * Conditionally open a subset of child vdevs.
1810 vdev_open_children_subset(vdev_t
*vd
, vdev_open_children_func_t
*open_func
)
1812 vdev_open_children_impl(vd
, open_func
);
1816 * Compute the raidz-deflation ratio. Note, we hard-code
1817 * in 128k (1 << 17) because it is the "typical" blocksize.
1818 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1819 * otherwise it would inconsistently account for existing bp's.
1822 vdev_set_deflate_ratio(vdev_t
*vd
)
1824 if (vd
== vd
->vdev_top
&& !vd
->vdev_ishole
&& vd
->vdev_ashift
!= 0) {
1825 vd
->vdev_deflate_ratio
= (1 << 17) /
1826 (vdev_psize_to_asize(vd
, 1 << 17) >> SPA_MINBLOCKSHIFT
);
1831 * Maximize performance by inflating the configured ashift for top level
1832 * vdevs to be as close to the physical ashift as possible while maintaining
1833 * administrator defined limits and ensuring it doesn't go below the
1837 vdev_ashift_optimize(vdev_t
*vd
)
1839 ASSERT(vd
== vd
->vdev_top
);
1841 if (vd
->vdev_ashift
< vd
->vdev_physical_ashift
) {
1842 vd
->vdev_ashift
= MIN(
1843 MAX(zfs_vdev_max_auto_ashift
, vd
->vdev_ashift
),
1844 MAX(zfs_vdev_min_auto_ashift
,
1845 vd
->vdev_physical_ashift
));
1848 * If the logical and physical ashifts are the same, then
1849 * we ensure that the top-level vdev's ashift is not smaller
1850 * than our minimum ashift value. For the unusual case
1851 * where logical ashift > physical ashift, we can't cap
1852 * the calculated ashift based on max ashift as that
1853 * would cause failures.
1854 * We still check if we need to increase it to match
1857 vd
->vdev_ashift
= MAX(zfs_vdev_min_auto_ashift
,
1863 * Prepare a virtual device for access.
1866 vdev_open(vdev_t
*vd
)
1868 spa_t
*spa
= vd
->vdev_spa
;
1871 uint64_t max_osize
= 0;
1872 uint64_t asize
, max_asize
, psize
;
1873 uint64_t logical_ashift
= 0;
1874 uint64_t physical_ashift
= 0;
1876 ASSERT(vd
->vdev_open_thread
== curthread
||
1877 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1878 ASSERT(vd
->vdev_state
== VDEV_STATE_CLOSED
||
1879 vd
->vdev_state
== VDEV_STATE_CANT_OPEN
||
1880 vd
->vdev_state
== VDEV_STATE_OFFLINE
);
1882 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1883 vd
->vdev_cant_read
= B_FALSE
;
1884 vd
->vdev_cant_write
= B_FALSE
;
1885 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
1888 * If this vdev is not removed, check its fault status. If it's
1889 * faulted, bail out of the open.
1891 if (!vd
->vdev_removed
&& vd
->vdev_faulted
) {
1892 ASSERT(vd
->vdev_children
== 0);
1893 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1894 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1895 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1896 vd
->vdev_label_aux
);
1897 return (SET_ERROR(ENXIO
));
1898 } else if (vd
->vdev_offline
) {
1899 ASSERT(vd
->vdev_children
== 0);
1900 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
, VDEV_AUX_NONE
);
1901 return (SET_ERROR(ENXIO
));
1904 error
= vd
->vdev_ops
->vdev_op_open(vd
, &osize
, &max_osize
,
1905 &logical_ashift
, &physical_ashift
);
1907 * Physical volume size should never be larger than its max size, unless
1908 * the disk has shrunk while we were reading it or the device is buggy
1909 * or damaged: either way it's not safe for use, bail out of the open.
1911 if (osize
> max_osize
) {
1912 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1913 VDEV_AUX_OPEN_FAILED
);
1914 return (SET_ERROR(ENXIO
));
1918 * Reset the vdev_reopening flag so that we actually close
1919 * the vdev on error.
1921 vd
->vdev_reopening
= B_FALSE
;
1922 if (zio_injection_enabled
&& error
== 0)
1923 error
= zio_handle_device_injection(vd
, NULL
, SET_ERROR(ENXIO
));
1926 if (vd
->vdev_removed
&&
1927 vd
->vdev_stat
.vs_aux
!= VDEV_AUX_OPEN_FAILED
)
1928 vd
->vdev_removed
= B_FALSE
;
1930 if (vd
->vdev_stat
.vs_aux
== VDEV_AUX_CHILDREN_OFFLINE
) {
1931 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
,
1932 vd
->vdev_stat
.vs_aux
);
1934 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1935 vd
->vdev_stat
.vs_aux
);
1940 vd
->vdev_removed
= B_FALSE
;
1943 * Recheck the faulted flag now that we have confirmed that
1944 * the vdev is accessible. If we're faulted, bail.
1946 if (vd
->vdev_faulted
) {
1947 ASSERT(vd
->vdev_children
== 0);
1948 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1949 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1950 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1951 vd
->vdev_label_aux
);
1952 return (SET_ERROR(ENXIO
));
1955 if (vd
->vdev_degraded
) {
1956 ASSERT(vd
->vdev_children
== 0);
1957 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1958 VDEV_AUX_ERR_EXCEEDED
);
1960 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_HEALTHY
, 0);
1964 * For hole or missing vdevs we just return success.
1966 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
)
1969 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
1970 if (vd
->vdev_child
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
) {
1971 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1977 osize
= P2ALIGN(osize
, (uint64_t)sizeof (vdev_label_t
));
1978 max_osize
= P2ALIGN(max_osize
, (uint64_t)sizeof (vdev_label_t
));
1980 if (vd
->vdev_children
== 0) {
1981 if (osize
< SPA_MINDEVSIZE
) {
1982 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1983 VDEV_AUX_TOO_SMALL
);
1984 return (SET_ERROR(EOVERFLOW
));
1987 asize
= osize
- (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
);
1988 max_asize
= max_osize
- (VDEV_LABEL_START_SIZE
+
1989 VDEV_LABEL_END_SIZE
);
1991 if (vd
->vdev_parent
!= NULL
&& osize
< SPA_MINDEVSIZE
-
1992 (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
)) {
1993 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1994 VDEV_AUX_TOO_SMALL
);
1995 return (SET_ERROR(EOVERFLOW
));
1999 max_asize
= max_osize
;
2003 * If the vdev was expanded, record this so that we can re-create the
2004 * uberblock rings in labels {2,3}, during the next sync.
2006 if ((psize
> vd
->vdev_psize
) && (vd
->vdev_psize
!= 0))
2007 vd
->vdev_copy_uberblocks
= B_TRUE
;
2009 vd
->vdev_psize
= psize
;
2012 * Make sure the allocatable size hasn't shrunk too much.
2014 if (asize
< vd
->vdev_min_asize
) {
2015 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2016 VDEV_AUX_BAD_LABEL
);
2017 return (SET_ERROR(EINVAL
));
2021 * We can always set the logical/physical ashift members since
2022 * their values are only used to calculate the vdev_ashift when
2023 * the device is first added to the config. These values should
2024 * not be used for anything else since they may change whenever
2025 * the device is reopened and we don't store them in the label.
2027 vd
->vdev_physical_ashift
=
2028 MAX(physical_ashift
, vd
->vdev_physical_ashift
);
2029 vd
->vdev_logical_ashift
= MAX(logical_ashift
,
2030 vd
->vdev_logical_ashift
);
2032 if (vd
->vdev_asize
== 0) {
2034 * This is the first-ever open, so use the computed values.
2035 * For compatibility, a different ashift can be requested.
2037 vd
->vdev_asize
= asize
;
2038 vd
->vdev_max_asize
= max_asize
;
2041 * If the vdev_ashift was not overriden at creation time,
2042 * then set it the logical ashift and optimize the ashift.
2044 if (vd
->vdev_ashift
== 0) {
2045 vd
->vdev_ashift
= vd
->vdev_logical_ashift
;
2047 if (vd
->vdev_logical_ashift
> ASHIFT_MAX
) {
2048 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2049 VDEV_AUX_ASHIFT_TOO_BIG
);
2050 return (SET_ERROR(EDOM
));
2053 if (vd
->vdev_top
== vd
) {
2054 vdev_ashift_optimize(vd
);
2057 if (vd
->vdev_ashift
!= 0 && (vd
->vdev_ashift
< ASHIFT_MIN
||
2058 vd
->vdev_ashift
> ASHIFT_MAX
)) {
2059 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2060 VDEV_AUX_BAD_ASHIFT
);
2061 return (SET_ERROR(EDOM
));
2065 * Make sure the alignment required hasn't increased.
2067 if (vd
->vdev_ashift
> vd
->vdev_top
->vdev_ashift
&&
2068 vd
->vdev_ops
->vdev_op_leaf
) {
2069 (void) zfs_ereport_post(
2070 FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
,
2071 spa
, vd
, NULL
, NULL
, 0);
2072 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2073 VDEV_AUX_BAD_LABEL
);
2074 return (SET_ERROR(EDOM
));
2076 vd
->vdev_max_asize
= max_asize
;
2080 * If all children are healthy we update asize if either:
2081 * The asize has increased, due to a device expansion caused by dynamic
2082 * LUN growth or vdev replacement, and automatic expansion is enabled;
2083 * making the additional space available.
2085 * The asize has decreased, due to a device shrink usually caused by a
2086 * vdev replace with a smaller device. This ensures that calculations
2087 * based of max_asize and asize e.g. esize are always valid. It's safe
2088 * to do this as we've already validated that asize is greater than
2091 if (vd
->vdev_state
== VDEV_STATE_HEALTHY
&&
2092 ((asize
> vd
->vdev_asize
&&
2093 (vd
->vdev_expanding
|| spa
->spa_autoexpand
)) ||
2094 (asize
< vd
->vdev_asize
)))
2095 vd
->vdev_asize
= asize
;
2097 vdev_set_min_asize(vd
);
2100 * Ensure we can issue some IO before declaring the
2101 * vdev open for business.
2103 if (vd
->vdev_ops
->vdev_op_leaf
&&
2104 (error
= zio_wait(vdev_probe(vd
, NULL
))) != 0) {
2105 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
2106 VDEV_AUX_ERR_EXCEEDED
);
2111 * Track the the minimum allocation size.
2113 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
2114 vd
->vdev_islog
== 0 && vd
->vdev_aux
== NULL
) {
2115 uint64_t min_alloc
= vdev_get_min_alloc(vd
);
2116 if (min_alloc
< spa
->spa_min_alloc
)
2117 spa
->spa_min_alloc
= min_alloc
;
2121 * If this is a leaf vdev, assess whether a resilver is needed.
2122 * But don't do this if we are doing a reopen for a scrub, since
2123 * this would just restart the scrub we are already doing.
2125 if (vd
->vdev_ops
->vdev_op_leaf
&& !spa
->spa_scrub_reopen
)
2126 dsl_scan_assess_vdev(spa
->spa_dsl_pool
, vd
);
2132 * Called once the vdevs are all opened, this routine validates the label
2133 * contents. This needs to be done before vdev_load() so that we don't
2134 * inadvertently do repair I/Os to the wrong device.
2136 * This function will only return failure if one of the vdevs indicates that it
2137 * has since been destroyed or exported. This is only possible if
2138 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
2139 * will be updated but the function will return 0.
2142 vdev_validate(vdev_t
*vd
)
2144 spa_t
*spa
= vd
->vdev_spa
;
2146 uint64_t guid
= 0, aux_guid
= 0, top_guid
;
2151 if (vdev_validate_skip
)
2154 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
2155 if (vdev_validate(vd
->vdev_child
[c
]) != 0)
2156 return (SET_ERROR(EBADF
));
2159 * If the device has already failed, or was marked offline, don't do
2160 * any further validation. Otherwise, label I/O will fail and we will
2161 * overwrite the previous state.
2163 if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_readable(vd
))
2167 * If we are performing an extreme rewind, we allow for a label that
2168 * was modified at a point after the current txg.
2169 * If config lock is not held do not check for the txg. spa_sync could
2170 * be updating the vdev's label before updating spa_last_synced_txg.
2172 if (spa
->spa_extreme_rewind
|| spa_last_synced_txg(spa
) == 0 ||
2173 spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) != SCL_CONFIG
)
2176 txg
= spa_last_synced_txg(spa
);
2178 if ((label
= vdev_label_read_config(vd
, txg
)) == NULL
) {
2179 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2180 VDEV_AUX_BAD_LABEL
);
2181 vdev_dbgmsg(vd
, "vdev_validate: failed reading config for "
2182 "txg %llu", (u_longlong_t
)txg
);
2187 * Determine if this vdev has been split off into another
2188 * pool. If so, then refuse to open it.
2190 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_SPLIT_GUID
,
2191 &aux_guid
) == 0 && aux_guid
== spa_guid(spa
)) {
2192 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2193 VDEV_AUX_SPLIT_POOL
);
2195 vdev_dbgmsg(vd
, "vdev_validate: vdev split into other pool");
2199 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0) {
2200 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2201 VDEV_AUX_CORRUPT_DATA
);
2203 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2204 ZPOOL_CONFIG_POOL_GUID
);
2209 * If config is not trusted then ignore the spa guid check. This is
2210 * necessary because if the machine crashed during a re-guid the new
2211 * guid might have been written to all of the vdev labels, but not the
2212 * cached config. The check will be performed again once we have the
2213 * trusted config from the MOS.
2215 if (spa
->spa_trust_config
&& guid
!= spa_guid(spa
)) {
2216 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2217 VDEV_AUX_CORRUPT_DATA
);
2219 vdev_dbgmsg(vd
, "vdev_validate: vdev label pool_guid doesn't "
2220 "match config (%llu != %llu)", (u_longlong_t
)guid
,
2221 (u_longlong_t
)spa_guid(spa
));
2225 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
)
2226 != 0 || nvlist_lookup_uint64(nvl
, ZPOOL_CONFIG_ORIG_GUID
,
2230 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0) {
2231 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2232 VDEV_AUX_CORRUPT_DATA
);
2234 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2239 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_TOP_GUID
, &top_guid
)
2241 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2242 VDEV_AUX_CORRUPT_DATA
);
2244 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2245 ZPOOL_CONFIG_TOP_GUID
);
2250 * If this vdev just became a top-level vdev because its sibling was
2251 * detached, it will have adopted the parent's vdev guid -- but the
2252 * label may or may not be on disk yet. Fortunately, either version
2253 * of the label will have the same top guid, so if we're a top-level
2254 * vdev, we can safely compare to that instead.
2255 * However, if the config comes from a cachefile that failed to update
2256 * after the detach, a top-level vdev will appear as a non top-level
2257 * vdev in the config. Also relax the constraints if we perform an
2260 * If we split this vdev off instead, then we also check the
2261 * original pool's guid. We don't want to consider the vdev
2262 * corrupt if it is partway through a split operation.
2264 if (vd
->vdev_guid
!= guid
&& vd
->vdev_guid
!= aux_guid
) {
2265 boolean_t mismatch
= B_FALSE
;
2266 if (spa
->spa_trust_config
&& !spa
->spa_extreme_rewind
) {
2267 if (vd
!= vd
->vdev_top
|| vd
->vdev_guid
!= top_guid
)
2270 if (vd
->vdev_guid
!= top_guid
&&
2271 vd
->vdev_top
->vdev_guid
!= guid
)
2276 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2277 VDEV_AUX_CORRUPT_DATA
);
2279 vdev_dbgmsg(vd
, "vdev_validate: config guid "
2280 "doesn't match label guid");
2281 vdev_dbgmsg(vd
, "CONFIG: guid %llu, top_guid %llu",
2282 (u_longlong_t
)vd
->vdev_guid
,
2283 (u_longlong_t
)vd
->vdev_top
->vdev_guid
);
2284 vdev_dbgmsg(vd
, "LABEL: guid %llu, top_guid %llu, "
2285 "aux_guid %llu", (u_longlong_t
)guid
,
2286 (u_longlong_t
)top_guid
, (u_longlong_t
)aux_guid
);
2291 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
,
2293 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2294 VDEV_AUX_CORRUPT_DATA
);
2296 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
2297 ZPOOL_CONFIG_POOL_STATE
);
2304 * If this is a verbatim import, no need to check the
2305 * state of the pool.
2307 if (!(spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) &&
2308 spa_load_state(spa
) == SPA_LOAD_OPEN
&&
2309 state
!= POOL_STATE_ACTIVE
) {
2310 vdev_dbgmsg(vd
, "vdev_validate: invalid pool state (%llu) "
2311 "for spa %s", (u_longlong_t
)state
, spa
->spa_name
);
2312 return (SET_ERROR(EBADF
));
2316 * If we were able to open and validate a vdev that was
2317 * previously marked permanently unavailable, clear that state
2320 if (vd
->vdev_not_present
)
2321 vd
->vdev_not_present
= 0;
2327 vdev_copy_path_impl(vdev_t
*svd
, vdev_t
*dvd
)
2329 if (svd
->vdev_path
!= NULL
&& dvd
->vdev_path
!= NULL
) {
2330 if (strcmp(svd
->vdev_path
, dvd
->vdev_path
) != 0) {
2331 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
2332 "from '%s' to '%s'", (u_longlong_t
)dvd
->vdev_guid
,
2333 dvd
->vdev_path
, svd
->vdev_path
);
2334 spa_strfree(dvd
->vdev_path
);
2335 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
2337 } else if (svd
->vdev_path
!= NULL
) {
2338 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
2339 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
2340 (u_longlong_t
)dvd
->vdev_guid
, dvd
->vdev_path
);
2345 * Recursively copy vdev paths from one vdev to another. Source and destination
2346 * vdev trees must have same geometry otherwise return error. Intended to copy
2347 * paths from userland config into MOS config.
2350 vdev_copy_path_strict(vdev_t
*svd
, vdev_t
*dvd
)
2352 if ((svd
->vdev_ops
== &vdev_missing_ops
) ||
2353 (svd
->vdev_ishole
&& dvd
->vdev_ishole
) ||
2354 (dvd
->vdev_ops
== &vdev_indirect_ops
))
2357 if (svd
->vdev_ops
!= dvd
->vdev_ops
) {
2358 vdev_dbgmsg(svd
, "vdev_copy_path: vdev type mismatch: %s != %s",
2359 svd
->vdev_ops
->vdev_op_type
, dvd
->vdev_ops
->vdev_op_type
);
2360 return (SET_ERROR(EINVAL
));
2363 if (svd
->vdev_guid
!= dvd
->vdev_guid
) {
2364 vdev_dbgmsg(svd
, "vdev_copy_path: guids mismatch (%llu != "
2365 "%llu)", (u_longlong_t
)svd
->vdev_guid
,
2366 (u_longlong_t
)dvd
->vdev_guid
);
2367 return (SET_ERROR(EINVAL
));
2370 if (svd
->vdev_children
!= dvd
->vdev_children
) {
2371 vdev_dbgmsg(svd
, "vdev_copy_path: children count mismatch: "
2372 "%llu != %llu", (u_longlong_t
)svd
->vdev_children
,
2373 (u_longlong_t
)dvd
->vdev_children
);
2374 return (SET_ERROR(EINVAL
));
2377 for (uint64_t i
= 0; i
< svd
->vdev_children
; i
++) {
2378 int error
= vdev_copy_path_strict(svd
->vdev_child
[i
],
2379 dvd
->vdev_child
[i
]);
2384 if (svd
->vdev_ops
->vdev_op_leaf
)
2385 vdev_copy_path_impl(svd
, dvd
);
2391 vdev_copy_path_search(vdev_t
*stvd
, vdev_t
*dvd
)
2393 ASSERT(stvd
->vdev_top
== stvd
);
2394 ASSERT3U(stvd
->vdev_id
, ==, dvd
->vdev_top
->vdev_id
);
2396 for (uint64_t i
= 0; i
< dvd
->vdev_children
; i
++) {
2397 vdev_copy_path_search(stvd
, dvd
->vdev_child
[i
]);
2400 if (!dvd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(dvd
))
2404 * The idea here is that while a vdev can shift positions within
2405 * a top vdev (when replacing, attaching mirror, etc.) it cannot
2406 * step outside of it.
2408 vdev_t
*vd
= vdev_lookup_by_guid(stvd
, dvd
->vdev_guid
);
2410 if (vd
== NULL
|| vd
->vdev_ops
!= dvd
->vdev_ops
)
2413 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2415 vdev_copy_path_impl(vd
, dvd
);
2419 * Recursively copy vdev paths from one root vdev to another. Source and
2420 * destination vdev trees may differ in geometry. For each destination leaf
2421 * vdev, search a vdev with the same guid and top vdev id in the source.
2422 * Intended to copy paths from userland config into MOS config.
2425 vdev_copy_path_relaxed(vdev_t
*srvd
, vdev_t
*drvd
)
2427 uint64_t children
= MIN(srvd
->vdev_children
, drvd
->vdev_children
);
2428 ASSERT(srvd
->vdev_ops
== &vdev_root_ops
);
2429 ASSERT(drvd
->vdev_ops
== &vdev_root_ops
);
2431 for (uint64_t i
= 0; i
< children
; i
++) {
2432 vdev_copy_path_search(srvd
->vdev_child
[i
],
2433 drvd
->vdev_child
[i
]);
2438 * Close a virtual device.
2441 vdev_close(vdev_t
*vd
)
2443 vdev_t
*pvd
= vd
->vdev_parent
;
2444 spa_t
*spa __maybe_unused
= vd
->vdev_spa
;
2447 ASSERT(vd
->vdev_open_thread
== curthread
||
2448 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2451 * If our parent is reopening, then we are as well, unless we are
2454 if (pvd
!= NULL
&& pvd
->vdev_reopening
)
2455 vd
->vdev_reopening
= (pvd
->vdev_reopening
&& !vd
->vdev_offline
);
2457 vd
->vdev_ops
->vdev_op_close(vd
);
2459 vdev_cache_purge(vd
);
2462 * We record the previous state before we close it, so that if we are
2463 * doing a reopen(), we don't generate FMA ereports if we notice that
2464 * it's still faulted.
2466 vd
->vdev_prevstate
= vd
->vdev_state
;
2468 if (vd
->vdev_offline
)
2469 vd
->vdev_state
= VDEV_STATE_OFFLINE
;
2471 vd
->vdev_state
= VDEV_STATE_CLOSED
;
2472 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
2476 vdev_hold(vdev_t
*vd
)
2478 spa_t
*spa
= vd
->vdev_spa
;
2480 ASSERT(spa_is_root(spa
));
2481 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
2484 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2485 vdev_hold(vd
->vdev_child
[c
]);
2487 if (vd
->vdev_ops
->vdev_op_leaf
)
2488 vd
->vdev_ops
->vdev_op_hold(vd
);
2492 vdev_rele(vdev_t
*vd
)
2494 ASSERT(spa_is_root(vd
->vdev_spa
));
2495 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2496 vdev_rele(vd
->vdev_child
[c
]);
2498 if (vd
->vdev_ops
->vdev_op_leaf
)
2499 vd
->vdev_ops
->vdev_op_rele(vd
);
2503 * Reopen all interior vdevs and any unopened leaves. We don't actually
2504 * reopen leaf vdevs which had previously been opened as they might deadlock
2505 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2506 * If the leaf has never been opened then open it, as usual.
2509 vdev_reopen(vdev_t
*vd
)
2511 spa_t
*spa
= vd
->vdev_spa
;
2513 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2515 /* set the reopening flag unless we're taking the vdev offline */
2516 vd
->vdev_reopening
= !vd
->vdev_offline
;
2518 (void) vdev_open(vd
);
2521 * Call vdev_validate() here to make sure we have the same device.
2522 * Otherwise, a device with an invalid label could be successfully
2523 * opened in response to vdev_reopen().
2526 (void) vdev_validate_aux(vd
);
2527 if (vdev_readable(vd
) && vdev_writeable(vd
) &&
2528 vd
->vdev_aux
== &spa
->spa_l2cache
) {
2530 * In case the vdev is present we should evict all ARC
2531 * buffers and pointers to log blocks and reclaim their
2532 * space before restoring its contents to L2ARC.
2534 if (l2arc_vdev_present(vd
)) {
2535 l2arc_rebuild_vdev(vd
, B_TRUE
);
2537 l2arc_add_vdev(spa
, vd
);
2539 spa_async_request(spa
, SPA_ASYNC_L2CACHE_REBUILD
);
2540 spa_async_request(spa
, SPA_ASYNC_L2CACHE_TRIM
);
2543 (void) vdev_validate(vd
);
2547 * Reassess parent vdev's health.
2549 vdev_propagate_state(vd
);
2553 vdev_create(vdev_t
*vd
, uint64_t txg
, boolean_t isreplacing
)
2558 * Normally, partial opens (e.g. of a mirror) are allowed.
2559 * For a create, however, we want to fail the request if
2560 * there are any components we can't open.
2562 error
= vdev_open(vd
);
2564 if (error
|| vd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
2566 return (error
? error
: SET_ERROR(ENXIO
));
2570 * Recursively load DTLs and initialize all labels.
2572 if ((error
= vdev_dtl_load(vd
)) != 0 ||
2573 (error
= vdev_label_init(vd
, txg
, isreplacing
?
2574 VDEV_LABEL_REPLACE
: VDEV_LABEL_CREATE
)) != 0) {
2583 vdev_metaslab_set_size(vdev_t
*vd
)
2585 uint64_t asize
= vd
->vdev_asize
;
2586 uint64_t ms_count
= asize
>> zfs_vdev_default_ms_shift
;
2590 * There are two dimensions to the metaslab sizing calculation:
2591 * the size of the metaslab and the count of metaslabs per vdev.
2593 * The default values used below are a good balance between memory
2594 * usage (larger metaslab size means more memory needed for loaded
2595 * metaslabs; more metaslabs means more memory needed for the
2596 * metaslab_t structs), metaslab load time (larger metaslabs take
2597 * longer to load), and metaslab sync time (more metaslabs means
2598 * more time spent syncing all of them).
2600 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2601 * The range of the dimensions are as follows:
2603 * 2^29 <= ms_size <= 2^34
2604 * 16 <= ms_count <= 131,072
2606 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2607 * at least 512MB (2^29) to minimize fragmentation effects when
2608 * testing with smaller devices. However, the count constraint
2609 * of at least 16 metaslabs will override this minimum size goal.
2611 * On the upper end of vdev sizes, we aim for a maximum metaslab
2612 * size of 16GB. However, we will cap the total count to 2^17
2613 * metaslabs to keep our memory footprint in check and let the
2614 * metaslab size grow from there if that limit is hit.
2616 * The net effect of applying above constrains is summarized below.
2618 * vdev size metaslab count
2619 * --------------|-----------------
2621 * 8GB - 100GB one per 512MB
2623 * 3TB - 2PB one per 16GB
2625 * --------------------------------
2627 * Finally, note that all of the above calculate the initial
2628 * number of metaslabs. Expanding a top-level vdev will result
2629 * in additional metaslabs being allocated making it possible
2630 * to exceed the zfs_vdev_ms_count_limit.
2633 if (ms_count
< zfs_vdev_min_ms_count
)
2634 ms_shift
= highbit64(asize
/ zfs_vdev_min_ms_count
);
2635 else if (ms_count
> zfs_vdev_default_ms_count
)
2636 ms_shift
= highbit64(asize
/ zfs_vdev_default_ms_count
);
2638 ms_shift
= zfs_vdev_default_ms_shift
;
2640 if (ms_shift
< SPA_MAXBLOCKSHIFT
) {
2641 ms_shift
= SPA_MAXBLOCKSHIFT
;
2642 } else if (ms_shift
> zfs_vdev_max_ms_shift
) {
2643 ms_shift
= zfs_vdev_max_ms_shift
;
2644 /* cap the total count to constrain memory footprint */
2645 if ((asize
>> ms_shift
) > zfs_vdev_ms_count_limit
)
2646 ms_shift
= highbit64(asize
/ zfs_vdev_ms_count_limit
);
2649 vd
->vdev_ms_shift
= ms_shift
;
2650 ASSERT3U(vd
->vdev_ms_shift
, >=, SPA_MAXBLOCKSHIFT
);
2654 vdev_dirty(vdev_t
*vd
, int flags
, void *arg
, uint64_t txg
)
2656 ASSERT(vd
== vd
->vdev_top
);
2657 /* indirect vdevs don't have metaslabs or dtls */
2658 ASSERT(vdev_is_concrete(vd
) || flags
== 0);
2659 ASSERT(ISP2(flags
));
2660 ASSERT(spa_writeable(vd
->vdev_spa
));
2662 if (flags
& VDD_METASLAB
)
2663 (void) txg_list_add(&vd
->vdev_ms_list
, arg
, txg
);
2665 if (flags
& VDD_DTL
)
2666 (void) txg_list_add(&vd
->vdev_dtl_list
, arg
, txg
);
2668 (void) txg_list_add(&vd
->vdev_spa
->spa_vdev_txg_list
, vd
, txg
);
2672 vdev_dirty_leaves(vdev_t
*vd
, int flags
, uint64_t txg
)
2674 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2675 vdev_dirty_leaves(vd
->vdev_child
[c
], flags
, txg
);
2677 if (vd
->vdev_ops
->vdev_op_leaf
)
2678 vdev_dirty(vd
->vdev_top
, flags
, vd
, txg
);
2684 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2685 * the vdev has less than perfect replication. There are four kinds of DTL:
2687 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2689 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2691 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2692 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2693 * txgs that was scrubbed.
2695 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2696 * persistent errors or just some device being offline.
2697 * Unlike the other three, the DTL_OUTAGE map is not generally
2698 * maintained; it's only computed when needed, typically to
2699 * determine whether a device can be detached.
2701 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2702 * either has the data or it doesn't.
2704 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2705 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2706 * if any child is less than fully replicated, then so is its parent.
2707 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2708 * comprising only those txgs which appear in 'maxfaults' or more children;
2709 * those are the txgs we don't have enough replication to read. For example,
2710 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2711 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2712 * two child DTL_MISSING maps.
2714 * It should be clear from the above that to compute the DTLs and outage maps
2715 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2716 * Therefore, that is all we keep on disk. When loading the pool, or after
2717 * a configuration change, we generate all other DTLs from first principles.
2720 vdev_dtl_dirty(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2722 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2724 ASSERT(t
< DTL_TYPES
);
2725 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2726 ASSERT(spa_writeable(vd
->vdev_spa
));
2728 mutex_enter(&vd
->vdev_dtl_lock
);
2729 if (!range_tree_contains(rt
, txg
, size
))
2730 range_tree_add(rt
, txg
, size
);
2731 mutex_exit(&vd
->vdev_dtl_lock
);
2735 vdev_dtl_contains(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2737 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2738 boolean_t dirty
= B_FALSE
;
2740 ASSERT(t
< DTL_TYPES
);
2741 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2744 * While we are loading the pool, the DTLs have not been loaded yet.
2745 * This isn't a problem but it can result in devices being tried
2746 * which are known to not have the data. In which case, the import
2747 * is relying on the checksum to ensure that we get the right data.
2748 * Note that while importing we are only reading the MOS, which is
2749 * always checksummed.
2751 mutex_enter(&vd
->vdev_dtl_lock
);
2752 if (!range_tree_is_empty(rt
))
2753 dirty
= range_tree_contains(rt
, txg
, size
);
2754 mutex_exit(&vd
->vdev_dtl_lock
);
2760 vdev_dtl_empty(vdev_t
*vd
, vdev_dtl_type_t t
)
2762 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2765 mutex_enter(&vd
->vdev_dtl_lock
);
2766 empty
= range_tree_is_empty(rt
);
2767 mutex_exit(&vd
->vdev_dtl_lock
);
2773 * Check if the txg falls within the range which must be
2774 * resilvered. DVAs outside this range can always be skipped.
2777 vdev_default_need_resilver(vdev_t
*vd
, const dva_t
*dva
, size_t psize
,
2778 uint64_t phys_birth
)
2780 /* Set by sequential resilver. */
2781 if (phys_birth
== TXG_UNKNOWN
)
2784 return (vdev_dtl_contains(vd
, DTL_PARTIAL
, phys_birth
, 1));
2788 * Returns B_TRUE if the vdev determines the DVA needs to be resilvered.
2791 vdev_dtl_need_resilver(vdev_t
*vd
, const dva_t
*dva
, size_t psize
,
2792 uint64_t phys_birth
)
2794 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2796 if (vd
->vdev_ops
->vdev_op_need_resilver
== NULL
||
2797 vd
->vdev_ops
->vdev_op_leaf
)
2800 return (vd
->vdev_ops
->vdev_op_need_resilver(vd
, dva
, psize
,
2805 * Returns the lowest txg in the DTL range.
2808 vdev_dtl_min(vdev_t
*vd
)
2810 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2811 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2812 ASSERT0(vd
->vdev_children
);
2814 return (range_tree_min(vd
->vdev_dtl
[DTL_MISSING
]) - 1);
2818 * Returns the highest txg in the DTL.
2821 vdev_dtl_max(vdev_t
*vd
)
2823 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2824 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2825 ASSERT0(vd
->vdev_children
);
2827 return (range_tree_max(vd
->vdev_dtl
[DTL_MISSING
]));
2831 * Determine if a resilvering vdev should remove any DTL entries from
2832 * its range. If the vdev was resilvering for the entire duration of the
2833 * scan then it should excise that range from its DTLs. Otherwise, this
2834 * vdev is considered partially resilvered and should leave its DTL
2835 * entries intact. The comment in vdev_dtl_reassess() describes how we
2839 vdev_dtl_should_excise(vdev_t
*vd
, boolean_t rebuild_done
)
2841 ASSERT0(vd
->vdev_children
);
2843 if (vd
->vdev_state
< VDEV_STATE_DEGRADED
)
2846 if (vd
->vdev_resilver_deferred
)
2849 if (range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]))
2853 vdev_rebuild_t
*vr
= &vd
->vdev_top
->vdev_rebuild_config
;
2854 vdev_rebuild_phys_t
*vrp
= &vr
->vr_rebuild_phys
;
2856 /* Rebuild not initiated by attach */
2857 if (vd
->vdev_rebuild_txg
== 0)
2861 * When a rebuild completes without error then all missing data
2862 * up to the rebuild max txg has been reconstructed and the DTL
2863 * is eligible for excision.
2865 if (vrp
->vrp_rebuild_state
== VDEV_REBUILD_COMPLETE
&&
2866 vdev_dtl_max(vd
) <= vrp
->vrp_max_txg
) {
2867 ASSERT3U(vrp
->vrp_min_txg
, <=, vdev_dtl_min(vd
));
2868 ASSERT3U(vrp
->vrp_min_txg
, <, vd
->vdev_rebuild_txg
);
2869 ASSERT3U(vd
->vdev_rebuild_txg
, <=, vrp
->vrp_max_txg
);
2873 dsl_scan_t
*scn
= vd
->vdev_spa
->spa_dsl_pool
->dp_scan
;
2874 dsl_scan_phys_t
*scnp __maybe_unused
= &scn
->scn_phys
;
2876 /* Resilver not initiated by attach */
2877 if (vd
->vdev_resilver_txg
== 0)
2881 * When a resilver is initiated the scan will assign the
2882 * scn_max_txg value to the highest txg value that exists
2883 * in all DTLs. If this device's max DTL is not part of this
2884 * scan (i.e. it is not in the range (scn_min_txg, scn_max_txg]
2885 * then it is not eligible for excision.
2887 if (vdev_dtl_max(vd
) <= scn
->scn_phys
.scn_max_txg
) {
2888 ASSERT3U(scnp
->scn_min_txg
, <=, vdev_dtl_min(vd
));
2889 ASSERT3U(scnp
->scn_min_txg
, <, vd
->vdev_resilver_txg
);
2890 ASSERT3U(vd
->vdev_resilver_txg
, <=, scnp
->scn_max_txg
);
2899 * Reassess DTLs after a config change or scrub completion. If txg == 0 no
2900 * write operations will be issued to the pool.
2903 vdev_dtl_reassess(vdev_t
*vd
, uint64_t txg
, uint64_t scrub_txg
,
2904 boolean_t scrub_done
, boolean_t rebuild_done
)
2906 spa_t
*spa
= vd
->vdev_spa
;
2910 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
2912 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2913 vdev_dtl_reassess(vd
->vdev_child
[c
], txg
,
2914 scrub_txg
, scrub_done
, rebuild_done
);
2916 if (vd
== spa
->spa_root_vdev
|| !vdev_is_concrete(vd
) || vd
->vdev_aux
)
2919 if (vd
->vdev_ops
->vdev_op_leaf
) {
2920 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
2921 vdev_rebuild_t
*vr
= &vd
->vdev_top
->vdev_rebuild_config
;
2922 boolean_t check_excise
= B_FALSE
;
2923 boolean_t wasempty
= B_TRUE
;
2925 mutex_enter(&vd
->vdev_dtl_lock
);
2928 * If requested, pretend the scan or rebuild completed cleanly.
2930 if (zfs_scan_ignore_errors
) {
2932 scn
->scn_phys
.scn_errors
= 0;
2934 vr
->vr_rebuild_phys
.vrp_errors
= 0;
2937 if (scrub_txg
!= 0 &&
2938 !range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
])) {
2940 zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d "
2941 "dtl:%llu/%llu errors:%llu",
2942 (u_longlong_t
)vd
->vdev_guid
, (u_longlong_t
)txg
,
2943 (u_longlong_t
)scrub_txg
, spa
->spa_scrub_started
,
2944 (u_longlong_t
)vdev_dtl_min(vd
),
2945 (u_longlong_t
)vdev_dtl_max(vd
),
2946 (u_longlong_t
)(scn
? scn
->scn_phys
.scn_errors
: 0));
2950 * If we've completed a scrub/resilver or a rebuild cleanly
2951 * then determine if this vdev should remove any DTLs. We
2952 * only want to excise regions on vdevs that were available
2953 * during the entire duration of this scan.
2956 vr
!= NULL
&& vr
->vr_rebuild_phys
.vrp_errors
== 0) {
2957 check_excise
= B_TRUE
;
2959 if (spa
->spa_scrub_started
||
2960 (scn
!= NULL
&& scn
->scn_phys
.scn_errors
== 0)) {
2961 check_excise
= B_TRUE
;
2965 if (scrub_txg
&& check_excise
&&
2966 vdev_dtl_should_excise(vd
, rebuild_done
)) {
2968 * We completed a scrub, resilver or rebuild up to
2969 * scrub_txg. If we did it without rebooting, then
2970 * the scrub dtl will be valid, so excise the old
2971 * region and fold in the scrub dtl. Otherwise,
2972 * leave the dtl as-is if there was an error.
2974 * There's little trick here: to excise the beginning
2975 * of the DTL_MISSING map, we put it into a reference
2976 * tree and then add a segment with refcnt -1 that
2977 * covers the range [0, scrub_txg). This means
2978 * that each txg in that range has refcnt -1 or 0.
2979 * We then add DTL_SCRUB with a refcnt of 2, so that
2980 * entries in the range [0, scrub_txg) will have a
2981 * positive refcnt -- either 1 or 2. We then convert
2982 * the reference tree into the new DTL_MISSING map.
2984 space_reftree_create(&reftree
);
2985 space_reftree_add_map(&reftree
,
2986 vd
->vdev_dtl
[DTL_MISSING
], 1);
2987 space_reftree_add_seg(&reftree
, 0, scrub_txg
, -1);
2988 space_reftree_add_map(&reftree
,
2989 vd
->vdev_dtl
[DTL_SCRUB
], 2);
2990 space_reftree_generate_map(&reftree
,
2991 vd
->vdev_dtl
[DTL_MISSING
], 1);
2992 space_reftree_destroy(&reftree
);
2994 if (!range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
])) {
2995 zfs_dbgmsg("update DTL_MISSING:%llu/%llu",
2996 (u_longlong_t
)vdev_dtl_min(vd
),
2997 (u_longlong_t
)vdev_dtl_max(vd
));
2998 } else if (!wasempty
) {
2999 zfs_dbgmsg("DTL_MISSING is now empty");
3002 range_tree_vacate(vd
->vdev_dtl
[DTL_PARTIAL
], NULL
, NULL
);
3003 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
3004 range_tree_add
, vd
->vdev_dtl
[DTL_PARTIAL
]);
3006 range_tree_vacate(vd
->vdev_dtl
[DTL_SCRUB
], NULL
, NULL
);
3007 range_tree_vacate(vd
->vdev_dtl
[DTL_OUTAGE
], NULL
, NULL
);
3008 if (!vdev_readable(vd
))
3009 range_tree_add(vd
->vdev_dtl
[DTL_OUTAGE
], 0, -1ULL);
3011 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
3012 range_tree_add
, vd
->vdev_dtl
[DTL_OUTAGE
]);
3015 * If the vdev was resilvering or rebuilding and no longer
3016 * has any DTLs then reset the appropriate flag and dirty
3017 * the top level so that we persist the change.
3020 range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]) &&
3021 range_tree_is_empty(vd
->vdev_dtl
[DTL_OUTAGE
])) {
3022 if (vd
->vdev_rebuild_txg
!= 0) {
3023 vd
->vdev_rebuild_txg
= 0;
3024 vdev_config_dirty(vd
->vdev_top
);
3025 } else if (vd
->vdev_resilver_txg
!= 0) {
3026 vd
->vdev_resilver_txg
= 0;
3027 vdev_config_dirty(vd
->vdev_top
);
3031 mutex_exit(&vd
->vdev_dtl_lock
);
3034 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, txg
);
3038 mutex_enter(&vd
->vdev_dtl_lock
);
3039 for (int t
= 0; t
< DTL_TYPES
; t
++) {
3040 /* account for child's outage in parent's missing map */
3041 int s
= (t
== DTL_MISSING
) ? DTL_OUTAGE
: t
;
3043 continue; /* leaf vdevs only */
3044 if (t
== DTL_PARTIAL
)
3045 minref
= 1; /* i.e. non-zero */
3046 else if (vdev_get_nparity(vd
) != 0)
3047 minref
= vdev_get_nparity(vd
) + 1; /* RAID-Z, dRAID */
3049 minref
= vd
->vdev_children
; /* any kind of mirror */
3050 space_reftree_create(&reftree
);
3051 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3052 vdev_t
*cvd
= vd
->vdev_child
[c
];
3053 mutex_enter(&cvd
->vdev_dtl_lock
);
3054 space_reftree_add_map(&reftree
, cvd
->vdev_dtl
[s
], 1);
3055 mutex_exit(&cvd
->vdev_dtl_lock
);
3057 space_reftree_generate_map(&reftree
, vd
->vdev_dtl
[t
], minref
);
3058 space_reftree_destroy(&reftree
);
3060 mutex_exit(&vd
->vdev_dtl_lock
);
3064 vdev_dtl_load(vdev_t
*vd
)
3066 spa_t
*spa
= vd
->vdev_spa
;
3067 objset_t
*mos
= spa
->spa_meta_objset
;
3071 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_dtl_object
!= 0) {
3072 ASSERT(vdev_is_concrete(vd
));
3074 error
= space_map_open(&vd
->vdev_dtl_sm
, mos
,
3075 vd
->vdev_dtl_object
, 0, -1ULL, 0);
3078 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
3080 rt
= range_tree_create(NULL
, RANGE_SEG64
, NULL
, 0, 0);
3081 error
= space_map_load(vd
->vdev_dtl_sm
, rt
, SM_ALLOC
);
3083 mutex_enter(&vd
->vdev_dtl_lock
);
3084 range_tree_walk(rt
, range_tree_add
,
3085 vd
->vdev_dtl
[DTL_MISSING
]);
3086 mutex_exit(&vd
->vdev_dtl_lock
);
3089 range_tree_vacate(rt
, NULL
, NULL
);
3090 range_tree_destroy(rt
);
3095 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3096 error
= vdev_dtl_load(vd
->vdev_child
[c
]);
3105 vdev_zap_allocation_data(vdev_t
*vd
, dmu_tx_t
*tx
)
3107 spa_t
*spa
= vd
->vdev_spa
;
3108 objset_t
*mos
= spa
->spa_meta_objset
;
3109 vdev_alloc_bias_t alloc_bias
= vd
->vdev_alloc_bias
;
3112 ASSERT(alloc_bias
!= VDEV_BIAS_NONE
);
3115 (alloc_bias
== VDEV_BIAS_LOG
) ? VDEV_ALLOC_BIAS_LOG
:
3116 (alloc_bias
== VDEV_BIAS_SPECIAL
) ? VDEV_ALLOC_BIAS_SPECIAL
:
3117 (alloc_bias
== VDEV_BIAS_DEDUP
) ? VDEV_ALLOC_BIAS_DEDUP
: NULL
;
3119 ASSERT(string
!= NULL
);
3120 VERIFY0(zap_add(mos
, vd
->vdev_top_zap
, VDEV_TOP_ZAP_ALLOCATION_BIAS
,
3121 1, strlen(string
) + 1, string
, tx
));
3123 if (alloc_bias
== VDEV_BIAS_SPECIAL
|| alloc_bias
== VDEV_BIAS_DEDUP
) {
3124 spa_activate_allocation_classes(spa
, tx
);
3129 vdev_destroy_unlink_zap(vdev_t
*vd
, uint64_t zapobj
, dmu_tx_t
*tx
)
3131 spa_t
*spa
= vd
->vdev_spa
;
3133 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zapobj
, tx
));
3134 VERIFY0(zap_remove_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
3139 vdev_create_link_zap(vdev_t
*vd
, dmu_tx_t
*tx
)
3141 spa_t
*spa
= vd
->vdev_spa
;
3142 uint64_t zap
= zap_create(spa
->spa_meta_objset
, DMU_OTN_ZAP_METADATA
,
3143 DMU_OT_NONE
, 0, tx
);
3146 VERIFY0(zap_add_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
3153 vdev_construct_zaps(vdev_t
*vd
, dmu_tx_t
*tx
)
3155 if (vd
->vdev_ops
!= &vdev_hole_ops
&&
3156 vd
->vdev_ops
!= &vdev_missing_ops
&&
3157 vd
->vdev_ops
!= &vdev_root_ops
&&
3158 !vd
->vdev_top
->vdev_removing
) {
3159 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_leaf_zap
== 0) {
3160 vd
->vdev_leaf_zap
= vdev_create_link_zap(vd
, tx
);
3162 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
== 0) {
3163 vd
->vdev_top_zap
= vdev_create_link_zap(vd
, tx
);
3164 if (vd
->vdev_alloc_bias
!= VDEV_BIAS_NONE
)
3165 vdev_zap_allocation_data(vd
, tx
);
3169 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
3170 vdev_construct_zaps(vd
->vdev_child
[i
], tx
);
3175 vdev_dtl_sync(vdev_t
*vd
, uint64_t txg
)
3177 spa_t
*spa
= vd
->vdev_spa
;
3178 range_tree_t
*rt
= vd
->vdev_dtl
[DTL_MISSING
];
3179 objset_t
*mos
= spa
->spa_meta_objset
;
3180 range_tree_t
*rtsync
;
3182 uint64_t object
= space_map_object(vd
->vdev_dtl_sm
);
3184 ASSERT(vdev_is_concrete(vd
));
3185 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3187 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
3189 if (vd
->vdev_detached
|| vd
->vdev_top
->vdev_removing
) {
3190 mutex_enter(&vd
->vdev_dtl_lock
);
3191 space_map_free(vd
->vdev_dtl_sm
, tx
);
3192 space_map_close(vd
->vdev_dtl_sm
);
3193 vd
->vdev_dtl_sm
= NULL
;
3194 mutex_exit(&vd
->vdev_dtl_lock
);
3197 * We only destroy the leaf ZAP for detached leaves or for
3198 * removed log devices. Removed data devices handle leaf ZAP
3199 * cleanup later, once cancellation is no longer possible.
3201 if (vd
->vdev_leaf_zap
!= 0 && (vd
->vdev_detached
||
3202 vd
->vdev_top
->vdev_islog
)) {
3203 vdev_destroy_unlink_zap(vd
, vd
->vdev_leaf_zap
, tx
);
3204 vd
->vdev_leaf_zap
= 0;
3211 if (vd
->vdev_dtl_sm
== NULL
) {
3212 uint64_t new_object
;
3214 new_object
= space_map_alloc(mos
, zfs_vdev_dtl_sm_blksz
, tx
);
3215 VERIFY3U(new_object
, !=, 0);
3217 VERIFY0(space_map_open(&vd
->vdev_dtl_sm
, mos
, new_object
,
3219 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
3222 rtsync
= range_tree_create(NULL
, RANGE_SEG64
, NULL
, 0, 0);
3224 mutex_enter(&vd
->vdev_dtl_lock
);
3225 range_tree_walk(rt
, range_tree_add
, rtsync
);
3226 mutex_exit(&vd
->vdev_dtl_lock
);
3228 space_map_truncate(vd
->vdev_dtl_sm
, zfs_vdev_dtl_sm_blksz
, tx
);
3229 space_map_write(vd
->vdev_dtl_sm
, rtsync
, SM_ALLOC
, SM_NO_VDEVID
, tx
);
3230 range_tree_vacate(rtsync
, NULL
, NULL
);
3232 range_tree_destroy(rtsync
);
3235 * If the object for the space map has changed then dirty
3236 * the top level so that we update the config.
3238 if (object
!= space_map_object(vd
->vdev_dtl_sm
)) {
3239 vdev_dbgmsg(vd
, "txg %llu, spa %s, DTL old object %llu, "
3240 "new object %llu", (u_longlong_t
)txg
, spa_name(spa
),
3241 (u_longlong_t
)object
,
3242 (u_longlong_t
)space_map_object(vd
->vdev_dtl_sm
));
3243 vdev_config_dirty(vd
->vdev_top
);
3250 * Determine whether the specified vdev can be offlined/detached/removed
3251 * without losing data.
3254 vdev_dtl_required(vdev_t
*vd
)
3256 spa_t
*spa
= vd
->vdev_spa
;
3257 vdev_t
*tvd
= vd
->vdev_top
;
3258 uint8_t cant_read
= vd
->vdev_cant_read
;
3261 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
3263 if (vd
== spa
->spa_root_vdev
|| vd
== tvd
)
3267 * Temporarily mark the device as unreadable, and then determine
3268 * whether this results in any DTL outages in the top-level vdev.
3269 * If not, we can safely offline/detach/remove the device.
3271 vd
->vdev_cant_read
= B_TRUE
;
3272 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
, B_FALSE
);
3273 required
= !vdev_dtl_empty(tvd
, DTL_OUTAGE
);
3274 vd
->vdev_cant_read
= cant_read
;
3275 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
, B_FALSE
);
3277 if (!required
&& zio_injection_enabled
) {
3278 required
= !!zio_handle_device_injection(vd
, NULL
,
3286 * Determine if resilver is needed, and if so the txg range.
3289 vdev_resilver_needed(vdev_t
*vd
, uint64_t *minp
, uint64_t *maxp
)
3291 boolean_t needed
= B_FALSE
;
3292 uint64_t thismin
= UINT64_MAX
;
3293 uint64_t thismax
= 0;
3295 if (vd
->vdev_children
== 0) {
3296 mutex_enter(&vd
->vdev_dtl_lock
);
3297 if (!range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]) &&
3298 vdev_writeable(vd
)) {
3300 thismin
= vdev_dtl_min(vd
);
3301 thismax
= vdev_dtl_max(vd
);
3304 mutex_exit(&vd
->vdev_dtl_lock
);
3306 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3307 vdev_t
*cvd
= vd
->vdev_child
[c
];
3308 uint64_t cmin
, cmax
;
3310 if (vdev_resilver_needed(cvd
, &cmin
, &cmax
)) {
3311 thismin
= MIN(thismin
, cmin
);
3312 thismax
= MAX(thismax
, cmax
);
3318 if (needed
&& minp
) {
3326 * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj
3327 * will contain either the checkpoint spacemap object or zero if none exists.
3328 * All other errors are returned to the caller.
3331 vdev_checkpoint_sm_object(vdev_t
*vd
, uint64_t *sm_obj
)
3333 ASSERT0(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
));
3335 if (vd
->vdev_top_zap
== 0) {
3340 int error
= zap_lookup(spa_meta_objset(vd
->vdev_spa
), vd
->vdev_top_zap
,
3341 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM
, sizeof (uint64_t), 1, sm_obj
);
3342 if (error
== ENOENT
) {
3351 vdev_load(vdev_t
*vd
)
3356 * Recursively load all children.
3358 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3359 error
= vdev_load(vd
->vdev_child
[c
]);
3365 vdev_set_deflate_ratio(vd
);
3368 * On spa_load path, grab the allocation bias from our zap
3370 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
!= 0) {
3371 spa_t
*spa
= vd
->vdev_spa
;
3374 error
= zap_lookup(spa
->spa_meta_objset
, vd
->vdev_top_zap
,
3375 VDEV_TOP_ZAP_ALLOCATION_BIAS
, 1, sizeof (bias_str
),
3378 ASSERT(vd
->vdev_alloc_bias
== VDEV_BIAS_NONE
);
3379 vd
->vdev_alloc_bias
= vdev_derive_alloc_bias(bias_str
);
3380 } else if (error
!= ENOENT
) {
3381 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3382 VDEV_AUX_CORRUPT_DATA
);
3383 vdev_dbgmsg(vd
, "vdev_load: zap_lookup(top_zap=%llu) "
3384 "failed [error=%d]", vd
->vdev_top_zap
, error
);
3390 * Load any rebuild state from the top-level vdev zap.
3392 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
!= 0) {
3393 error
= vdev_rebuild_load(vd
);
3394 if (error
&& error
!= ENOTSUP
) {
3395 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3396 VDEV_AUX_CORRUPT_DATA
);
3397 vdev_dbgmsg(vd
, "vdev_load: vdev_rebuild_load "
3398 "failed [error=%d]", error
);
3404 * If this is a top-level vdev, initialize its metaslabs.
3406 if (vd
== vd
->vdev_top
&& vdev_is_concrete(vd
)) {
3407 vdev_metaslab_group_create(vd
);
3409 if (vd
->vdev_ashift
== 0 || vd
->vdev_asize
== 0) {
3410 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3411 VDEV_AUX_CORRUPT_DATA
);
3412 vdev_dbgmsg(vd
, "vdev_load: invalid size. ashift=%llu, "
3413 "asize=%llu", (u_longlong_t
)vd
->vdev_ashift
,
3414 (u_longlong_t
)vd
->vdev_asize
);
3415 return (SET_ERROR(ENXIO
));
3418 error
= vdev_metaslab_init(vd
, 0);
3420 vdev_dbgmsg(vd
, "vdev_load: metaslab_init failed "
3421 "[error=%d]", error
);
3422 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3423 VDEV_AUX_CORRUPT_DATA
);
3427 uint64_t checkpoint_sm_obj
;
3428 error
= vdev_checkpoint_sm_object(vd
, &checkpoint_sm_obj
);
3429 if (error
== 0 && checkpoint_sm_obj
!= 0) {
3430 objset_t
*mos
= spa_meta_objset(vd
->vdev_spa
);
3431 ASSERT(vd
->vdev_asize
!= 0);
3432 ASSERT3P(vd
->vdev_checkpoint_sm
, ==, NULL
);
3434 error
= space_map_open(&vd
->vdev_checkpoint_sm
,
3435 mos
, checkpoint_sm_obj
, 0, vd
->vdev_asize
,
3438 vdev_dbgmsg(vd
, "vdev_load: space_map_open "
3439 "failed for checkpoint spacemap (obj %llu) "
3441 (u_longlong_t
)checkpoint_sm_obj
, error
);
3444 ASSERT3P(vd
->vdev_checkpoint_sm
, !=, NULL
);
3447 * Since the checkpoint_sm contains free entries
3448 * exclusively we can use space_map_allocated() to
3449 * indicate the cumulative checkpointed space that
3452 vd
->vdev_stat
.vs_checkpoint_space
=
3453 -space_map_allocated(vd
->vdev_checkpoint_sm
);
3454 vd
->vdev_spa
->spa_checkpoint_info
.sci_dspace
+=
3455 vd
->vdev_stat
.vs_checkpoint_space
;
3456 } else if (error
!= 0) {
3457 vdev_dbgmsg(vd
, "vdev_load: failed to retrieve "
3458 "checkpoint space map object from vdev ZAP "
3459 "[error=%d]", error
);
3465 * If this is a leaf vdev, load its DTL.
3467 if (vd
->vdev_ops
->vdev_op_leaf
&& (error
= vdev_dtl_load(vd
)) != 0) {
3468 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3469 VDEV_AUX_CORRUPT_DATA
);
3470 vdev_dbgmsg(vd
, "vdev_load: vdev_dtl_load failed "
3471 "[error=%d]", error
);
3475 uint64_t obsolete_sm_object
;
3476 error
= vdev_obsolete_sm_object(vd
, &obsolete_sm_object
);
3477 if (error
== 0 && obsolete_sm_object
!= 0) {
3478 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
3479 ASSERT(vd
->vdev_asize
!= 0);
3480 ASSERT3P(vd
->vdev_obsolete_sm
, ==, NULL
);
3482 if ((error
= space_map_open(&vd
->vdev_obsolete_sm
, mos
,
3483 obsolete_sm_object
, 0, vd
->vdev_asize
, 0))) {
3484 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3485 VDEV_AUX_CORRUPT_DATA
);
3486 vdev_dbgmsg(vd
, "vdev_load: space_map_open failed for "
3487 "obsolete spacemap (obj %llu) [error=%d]",
3488 (u_longlong_t
)obsolete_sm_object
, error
);
3491 } else if (error
!= 0) {
3492 vdev_dbgmsg(vd
, "vdev_load: failed to retrieve obsolete "
3493 "space map object from vdev ZAP [error=%d]", error
);
3501 * The special vdev case is used for hot spares and l2cache devices. Its
3502 * sole purpose it to set the vdev state for the associated vdev. To do this,
3503 * we make sure that we can open the underlying device, then try to read the
3504 * label, and make sure that the label is sane and that it hasn't been
3505 * repurposed to another pool.
3508 vdev_validate_aux(vdev_t
*vd
)
3511 uint64_t guid
, version
;
3514 if (!vdev_readable(vd
))
3517 if ((label
= vdev_label_read_config(vd
, -1ULL)) == NULL
) {
3518 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
3519 VDEV_AUX_CORRUPT_DATA
);
3523 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_VERSION
, &version
) != 0 ||
3524 !SPA_VERSION_IS_SUPPORTED(version
) ||
3525 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0 ||
3526 guid
!= vd
->vdev_guid
||
3527 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
, &state
) != 0) {
3528 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
3529 VDEV_AUX_CORRUPT_DATA
);
3535 * We don't actually check the pool state here. If it's in fact in
3536 * use by another pool, we update this fact on the fly when requested.
3543 vdev_destroy_ms_flush_data(vdev_t
*vd
, dmu_tx_t
*tx
)
3545 objset_t
*mos
= spa_meta_objset(vd
->vdev_spa
);
3547 if (vd
->vdev_top_zap
== 0)
3550 uint64_t object
= 0;
3551 int err
= zap_lookup(mos
, vd
->vdev_top_zap
,
3552 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS
, sizeof (uint64_t), 1, &object
);
3557 VERIFY0(dmu_object_free(mos
, object
, tx
));
3558 VERIFY0(zap_remove(mos
, vd
->vdev_top_zap
,
3559 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS
, tx
));
3563 * Free the objects used to store this vdev's spacemaps, and the array
3564 * that points to them.
3567 vdev_destroy_spacemaps(vdev_t
*vd
, dmu_tx_t
*tx
)
3569 if (vd
->vdev_ms_array
== 0)
3572 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
3573 uint64_t array_count
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
3574 size_t array_bytes
= array_count
* sizeof (uint64_t);
3575 uint64_t *smobj_array
= kmem_alloc(array_bytes
, KM_SLEEP
);
3576 VERIFY0(dmu_read(mos
, vd
->vdev_ms_array
, 0,
3577 array_bytes
, smobj_array
, 0));
3579 for (uint64_t i
= 0; i
< array_count
; i
++) {
3580 uint64_t smobj
= smobj_array
[i
];
3584 space_map_free_obj(mos
, smobj
, tx
);
3587 kmem_free(smobj_array
, array_bytes
);
3588 VERIFY0(dmu_object_free(mos
, vd
->vdev_ms_array
, tx
));
3589 vdev_destroy_ms_flush_data(vd
, tx
);
3590 vd
->vdev_ms_array
= 0;
3594 vdev_remove_empty_log(vdev_t
*vd
, uint64_t txg
)
3596 spa_t
*spa
= vd
->vdev_spa
;
3598 ASSERT(vd
->vdev_islog
);
3599 ASSERT(vd
== vd
->vdev_top
);
3600 ASSERT3U(txg
, ==, spa_syncing_txg(spa
));
3602 dmu_tx_t
*tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
3604 vdev_destroy_spacemaps(vd
, tx
);
3605 if (vd
->vdev_top_zap
!= 0) {
3606 vdev_destroy_unlink_zap(vd
, vd
->vdev_top_zap
, tx
);
3607 vd
->vdev_top_zap
= 0;
3614 vdev_sync_done(vdev_t
*vd
, uint64_t txg
)
3617 boolean_t reassess
= !txg_list_empty(&vd
->vdev_ms_list
, TXG_CLEAN(txg
));
3619 ASSERT(vdev_is_concrete(vd
));
3621 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, TXG_CLEAN(txg
)))
3623 metaslab_sync_done(msp
, txg
);
3626 metaslab_sync_reassess(vd
->vdev_mg
);
3627 if (vd
->vdev_log_mg
!= NULL
)
3628 metaslab_sync_reassess(vd
->vdev_log_mg
);
3633 vdev_sync(vdev_t
*vd
, uint64_t txg
)
3635 spa_t
*spa
= vd
->vdev_spa
;
3639 ASSERT3U(txg
, ==, spa
->spa_syncing_txg
);
3640 dmu_tx_t
*tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
3641 if (range_tree_space(vd
->vdev_obsolete_segments
) > 0) {
3642 ASSERT(vd
->vdev_removing
||
3643 vd
->vdev_ops
== &vdev_indirect_ops
);
3645 vdev_indirect_sync_obsolete(vd
, tx
);
3648 * If the vdev is indirect, it can't have dirty
3649 * metaslabs or DTLs.
3651 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
3652 ASSERT(txg_list_empty(&vd
->vdev_ms_list
, txg
));
3653 ASSERT(txg_list_empty(&vd
->vdev_dtl_list
, txg
));
3659 ASSERT(vdev_is_concrete(vd
));
3661 if (vd
->vdev_ms_array
== 0 && vd
->vdev_ms_shift
!= 0 &&
3662 !vd
->vdev_removing
) {
3663 ASSERT(vd
== vd
->vdev_top
);
3664 ASSERT0(vd
->vdev_indirect_config
.vic_mapping_object
);
3665 vd
->vdev_ms_array
= dmu_object_alloc(spa
->spa_meta_objset
,
3666 DMU_OT_OBJECT_ARRAY
, 0, DMU_OT_NONE
, 0, tx
);
3667 ASSERT(vd
->vdev_ms_array
!= 0);
3668 vdev_config_dirty(vd
);
3671 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, txg
)) != NULL
) {
3672 metaslab_sync(msp
, txg
);
3673 (void) txg_list_add(&vd
->vdev_ms_list
, msp
, TXG_CLEAN(txg
));
3676 while ((lvd
= txg_list_remove(&vd
->vdev_dtl_list
, txg
)) != NULL
)
3677 vdev_dtl_sync(lvd
, txg
);
3680 * If this is an empty log device being removed, destroy the
3681 * metadata associated with it.
3683 if (vd
->vdev_islog
&& vd
->vdev_stat
.vs_alloc
== 0 && vd
->vdev_removing
)
3684 vdev_remove_empty_log(vd
, txg
);
3686 (void) txg_list_add(&spa
->spa_vdev_txg_list
, vd
, TXG_CLEAN(txg
));
3691 vdev_psize_to_asize(vdev_t
*vd
, uint64_t psize
)
3693 return (vd
->vdev_ops
->vdev_op_asize(vd
, psize
));
3697 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
3698 * not be opened, and no I/O is attempted.
3701 vdev_fault(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
3705 spa_vdev_state_enter(spa
, SCL_NONE
);
3707 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3708 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
3710 if (!vd
->vdev_ops
->vdev_op_leaf
)
3711 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
3716 * If user did a 'zpool offline -f' then make the fault persist across
3719 if (aux
== VDEV_AUX_EXTERNAL_PERSIST
) {
3721 * There are two kinds of forced faults: temporary and
3722 * persistent. Temporary faults go away at pool import, while
3723 * persistent faults stay set. Both types of faults can be
3724 * cleared with a zpool clear.
3726 * We tell if a vdev is persistently faulted by looking at the
3727 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
3728 * import then it's a persistent fault. Otherwise, it's
3729 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
3730 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
3731 * tells vdev_config_generate() (which gets run later) to set
3732 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
3734 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
3735 vd
->vdev_tmpoffline
= B_FALSE
;
3736 aux
= VDEV_AUX_EXTERNAL
;
3738 vd
->vdev_tmpoffline
= B_TRUE
;
3742 * We don't directly use the aux state here, but if we do a
3743 * vdev_reopen(), we need this value to be present to remember why we
3746 vd
->vdev_label_aux
= aux
;
3749 * Faulted state takes precedence over degraded.
3751 vd
->vdev_delayed_close
= B_FALSE
;
3752 vd
->vdev_faulted
= 1ULL;
3753 vd
->vdev_degraded
= 0ULL;
3754 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_FAULTED
, aux
);
3757 * If this device has the only valid copy of the data, then
3758 * back off and simply mark the vdev as degraded instead.
3760 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&& vdev_dtl_required(vd
)) {
3761 vd
->vdev_degraded
= 1ULL;
3762 vd
->vdev_faulted
= 0ULL;
3765 * If we reopen the device and it's not dead, only then do we
3770 if (vdev_readable(vd
))
3771 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
, aux
);
3774 return (spa_vdev_state_exit(spa
, vd
, 0));
3778 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
3779 * user that something is wrong. The vdev continues to operate as normal as far
3780 * as I/O is concerned.
3783 vdev_degrade(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
3787 spa_vdev_state_enter(spa
, SCL_NONE
);
3789 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3790 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
3792 if (!vd
->vdev_ops
->vdev_op_leaf
)
3793 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
3796 * If the vdev is already faulted, then don't do anything.
3798 if (vd
->vdev_faulted
|| vd
->vdev_degraded
)
3799 return (spa_vdev_state_exit(spa
, NULL
, 0));
3801 vd
->vdev_degraded
= 1ULL;
3802 if (!vdev_is_dead(vd
))
3803 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
,
3806 return (spa_vdev_state_exit(spa
, vd
, 0));
3810 * Online the given vdev.
3812 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
3813 * spare device should be detached when the device finishes resilvering.
3814 * Second, the online should be treated like a 'test' online case, so no FMA
3815 * events are generated if the device fails to open.
3818 vdev_online(spa_t
*spa
, uint64_t guid
, uint64_t flags
, vdev_state_t
*newstate
)
3820 vdev_t
*vd
, *tvd
, *pvd
, *rvd
= spa
->spa_root_vdev
;
3821 boolean_t wasoffline
;
3822 vdev_state_t oldstate
;
3824 spa_vdev_state_enter(spa
, SCL_NONE
);
3826 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3827 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
3829 if (!vd
->vdev_ops
->vdev_op_leaf
)
3830 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
3832 wasoffline
= (vd
->vdev_offline
|| vd
->vdev_tmpoffline
);
3833 oldstate
= vd
->vdev_state
;
3836 vd
->vdev_offline
= B_FALSE
;
3837 vd
->vdev_tmpoffline
= B_FALSE
;
3838 vd
->vdev_checkremove
= !!(flags
& ZFS_ONLINE_CHECKREMOVE
);
3839 vd
->vdev_forcefault
= !!(flags
& ZFS_ONLINE_FORCEFAULT
);
3841 /* XXX - L2ARC 1.0 does not support expansion */
3842 if (!vd
->vdev_aux
) {
3843 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3844 pvd
->vdev_expanding
= !!((flags
& ZFS_ONLINE_EXPAND
) ||
3845 spa
->spa_autoexpand
);
3846 vd
->vdev_expansion_time
= gethrestime_sec();
3850 vd
->vdev_checkremove
= vd
->vdev_forcefault
= B_FALSE
;
3852 if (!vd
->vdev_aux
) {
3853 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3854 pvd
->vdev_expanding
= B_FALSE
;
3858 *newstate
= vd
->vdev_state
;
3859 if ((flags
& ZFS_ONLINE_UNSPARE
) &&
3860 !vdev_is_dead(vd
) && vd
->vdev_parent
&&
3861 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3862 vd
->vdev_parent
->vdev_child
[0] == vd
)
3863 vd
->vdev_unspare
= B_TRUE
;
3865 if ((flags
& ZFS_ONLINE_EXPAND
) || spa
->spa_autoexpand
) {
3867 /* XXX - L2ARC 1.0 does not support expansion */
3869 return (spa_vdev_state_exit(spa
, vd
, ENOTSUP
));
3870 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3873 /* Restart initializing if necessary */
3874 mutex_enter(&vd
->vdev_initialize_lock
);
3875 if (vdev_writeable(vd
) &&
3876 vd
->vdev_initialize_thread
== NULL
&&
3877 vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
3878 (void) vdev_initialize(vd
);
3880 mutex_exit(&vd
->vdev_initialize_lock
);
3883 * Restart trimming if necessary. We do not restart trimming for cache
3884 * devices here. This is triggered by l2arc_rebuild_vdev()
3885 * asynchronously for the whole device or in l2arc_evict() as it evicts
3886 * space for upcoming writes.
3888 mutex_enter(&vd
->vdev_trim_lock
);
3889 if (vdev_writeable(vd
) && !vd
->vdev_isl2cache
&&
3890 vd
->vdev_trim_thread
== NULL
&&
3891 vd
->vdev_trim_state
== VDEV_TRIM_ACTIVE
) {
3892 (void) vdev_trim(vd
, vd
->vdev_trim_rate
, vd
->vdev_trim_partial
,
3893 vd
->vdev_trim_secure
);
3895 mutex_exit(&vd
->vdev_trim_lock
);
3898 (oldstate
< VDEV_STATE_DEGRADED
&&
3899 vd
->vdev_state
>= VDEV_STATE_DEGRADED
))
3900 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_ONLINE
);
3902 return (spa_vdev_state_exit(spa
, vd
, 0));
3906 vdev_offline_locked(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
3910 uint64_t generation
;
3911 metaslab_group_t
*mg
;
3914 spa_vdev_state_enter(spa
, SCL_ALLOC
);
3916 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3917 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENODEV
)));
3919 if (!vd
->vdev_ops
->vdev_op_leaf
)
3920 return (spa_vdev_state_exit(spa
, NULL
, SET_ERROR(ENOTSUP
)));
3922 if (vd
->vdev_ops
== &vdev_draid_spare_ops
)
3923 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3927 generation
= spa
->spa_config_generation
+ 1;
3930 * If the device isn't already offline, try to offline it.
3932 if (!vd
->vdev_offline
) {
3934 * If this device has the only valid copy of some data,
3935 * don't allow it to be offlined. Log devices are always
3938 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
3939 vdev_dtl_required(vd
))
3940 return (spa_vdev_state_exit(spa
, NULL
,
3944 * If the top-level is a slog and it has had allocations
3945 * then proceed. We check that the vdev's metaslab group
3946 * is not NULL since it's possible that we may have just
3947 * added this vdev but not yet initialized its metaslabs.
3949 if (tvd
->vdev_islog
&& mg
!= NULL
) {
3951 * Prevent any future allocations.
3953 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
3954 metaslab_group_passivate(mg
);
3955 (void) spa_vdev_state_exit(spa
, vd
, 0);
3957 error
= spa_reset_logs(spa
);
3960 * If the log device was successfully reset but has
3961 * checkpointed data, do not offline it.
3964 tvd
->vdev_checkpoint_sm
!= NULL
) {
3965 ASSERT3U(space_map_allocated(
3966 tvd
->vdev_checkpoint_sm
), !=, 0);
3967 error
= ZFS_ERR_CHECKPOINT_EXISTS
;
3970 spa_vdev_state_enter(spa
, SCL_ALLOC
);
3973 * Check to see if the config has changed.
3975 if (error
|| generation
!= spa
->spa_config_generation
) {
3976 metaslab_group_activate(mg
);
3978 return (spa_vdev_state_exit(spa
,
3980 (void) spa_vdev_state_exit(spa
, vd
, 0);
3983 ASSERT0(tvd
->vdev_stat
.vs_alloc
);
3987 * Offline this device and reopen its top-level vdev.
3988 * If the top-level vdev is a log device then just offline
3989 * it. Otherwise, if this action results in the top-level
3990 * vdev becoming unusable, undo it and fail the request.
3992 vd
->vdev_offline
= B_TRUE
;
3995 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
3996 vdev_is_dead(tvd
)) {
3997 vd
->vdev_offline
= B_FALSE
;
3999 return (spa_vdev_state_exit(spa
, NULL
,
4004 * Add the device back into the metaslab rotor so that
4005 * once we online the device it's open for business.
4007 if (tvd
->vdev_islog
&& mg
!= NULL
)
4008 metaslab_group_activate(mg
);
4011 vd
->vdev_tmpoffline
= !!(flags
& ZFS_OFFLINE_TEMPORARY
);
4013 return (spa_vdev_state_exit(spa
, vd
, 0));
4017 vdev_offline(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
4021 mutex_enter(&spa
->spa_vdev_top_lock
);
4022 error
= vdev_offline_locked(spa
, guid
, flags
);
4023 mutex_exit(&spa
->spa_vdev_top_lock
);
4029 * Clear the error counts associated with this vdev. Unlike vdev_online() and
4030 * vdev_offline(), we assume the spa config is locked. We also clear all
4031 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
4034 vdev_clear(spa_t
*spa
, vdev_t
*vd
)
4036 vdev_t
*rvd
= spa
->spa_root_vdev
;
4038 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
4043 vd
->vdev_stat
.vs_read_errors
= 0;
4044 vd
->vdev_stat
.vs_write_errors
= 0;
4045 vd
->vdev_stat
.vs_checksum_errors
= 0;
4046 vd
->vdev_stat
.vs_slow_ios
= 0;
4048 for (int c
= 0; c
< vd
->vdev_children
; c
++)
4049 vdev_clear(spa
, vd
->vdev_child
[c
]);
4052 * It makes no sense to "clear" an indirect vdev.
4054 if (!vdev_is_concrete(vd
))
4058 * If we're in the FAULTED state or have experienced failed I/O, then
4059 * clear the persistent state and attempt to reopen the device. We
4060 * also mark the vdev config dirty, so that the new faulted state is
4061 * written out to disk.
4063 if (vd
->vdev_faulted
|| vd
->vdev_degraded
||
4064 !vdev_readable(vd
) || !vdev_writeable(vd
)) {
4066 * When reopening in response to a clear event, it may be due to
4067 * a fmadm repair request. In this case, if the device is
4068 * still broken, we want to still post the ereport again.
4070 vd
->vdev_forcefault
= B_TRUE
;
4072 vd
->vdev_faulted
= vd
->vdev_degraded
= 0ULL;
4073 vd
->vdev_cant_read
= B_FALSE
;
4074 vd
->vdev_cant_write
= B_FALSE
;
4075 vd
->vdev_stat
.vs_aux
= 0;
4077 vdev_reopen(vd
== rvd
? rvd
: vd
->vdev_top
);
4079 vd
->vdev_forcefault
= B_FALSE
;
4081 if (vd
!= rvd
&& vdev_writeable(vd
->vdev_top
))
4082 vdev_state_dirty(vd
->vdev_top
);
4084 /* If a resilver isn't required, check if vdevs can be culled */
4085 if (vd
->vdev_aux
== NULL
&& !vdev_is_dead(vd
) &&
4086 !dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
4087 !dsl_scan_resilver_scheduled(spa
->spa_dsl_pool
))
4088 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
4090 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_CLEAR
);
4094 * When clearing a FMA-diagnosed fault, we always want to
4095 * unspare the device, as we assume that the original spare was
4096 * done in response to the FMA fault.
4098 if (!vdev_is_dead(vd
) && vd
->vdev_parent
!= NULL
&&
4099 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
4100 vd
->vdev_parent
->vdev_child
[0] == vd
)
4101 vd
->vdev_unspare
= B_TRUE
;
4105 vdev_is_dead(vdev_t
*vd
)
4108 * Holes and missing devices are always considered "dead".
4109 * This simplifies the code since we don't have to check for
4110 * these types of devices in the various code paths.
4111 * Instead we rely on the fact that we skip over dead devices
4112 * before issuing I/O to them.
4114 return (vd
->vdev_state
< VDEV_STATE_DEGRADED
||
4115 vd
->vdev_ops
== &vdev_hole_ops
||
4116 vd
->vdev_ops
== &vdev_missing_ops
);
4120 vdev_readable(vdev_t
*vd
)
4122 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_read
);
4126 vdev_writeable(vdev_t
*vd
)
4128 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_write
&&
4129 vdev_is_concrete(vd
));
4133 vdev_allocatable(vdev_t
*vd
)
4135 uint64_t state
= vd
->vdev_state
;
4138 * We currently allow allocations from vdevs which may be in the
4139 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
4140 * fails to reopen then we'll catch it later when we're holding
4141 * the proper locks. Note that we have to get the vdev state
4142 * in a local variable because although it changes atomically,
4143 * we're asking two separate questions about it.
4145 return (!(state
< VDEV_STATE_DEGRADED
&& state
!= VDEV_STATE_CLOSED
) &&
4146 !vd
->vdev_cant_write
&& vdev_is_concrete(vd
) &&
4147 vd
->vdev_mg
->mg_initialized
);
4151 vdev_accessible(vdev_t
*vd
, zio_t
*zio
)
4153 ASSERT(zio
->io_vd
== vd
);
4155 if (vdev_is_dead(vd
) || vd
->vdev_remove_wanted
)
4158 if (zio
->io_type
== ZIO_TYPE_READ
)
4159 return (!vd
->vdev_cant_read
);
4161 if (zio
->io_type
== ZIO_TYPE_WRITE
)
4162 return (!vd
->vdev_cant_write
);
4168 vdev_get_child_stat(vdev_t
*cvd
, vdev_stat_t
*vs
, vdev_stat_t
*cvs
)
4171 * Exclude the dRAID spare when aggregating to avoid double counting
4172 * the ops and bytes. These IOs are counted by the physical leaves.
4174 if (cvd
->vdev_ops
== &vdev_draid_spare_ops
)
4177 for (int t
= 0; t
< VS_ZIO_TYPES
; t
++) {
4178 vs
->vs_ops
[t
] += cvs
->vs_ops
[t
];
4179 vs
->vs_bytes
[t
] += cvs
->vs_bytes
[t
];
4182 cvs
->vs_scan_removing
= cvd
->vdev_removing
;
4186 * Get extended stats
4189 vdev_get_child_stat_ex(vdev_t
*cvd
, vdev_stat_ex_t
*vsx
, vdev_stat_ex_t
*cvsx
)
4192 for (t
= 0; t
< ZIO_TYPES
; t
++) {
4193 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_disk_histo
[0]); b
++)
4194 vsx
->vsx_disk_histo
[t
][b
] += cvsx
->vsx_disk_histo
[t
][b
];
4196 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_total_histo
[0]); b
++) {
4197 vsx
->vsx_total_histo
[t
][b
] +=
4198 cvsx
->vsx_total_histo
[t
][b
];
4202 for (t
= 0; t
< ZIO_PRIORITY_NUM_QUEUEABLE
; t
++) {
4203 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_queue_histo
[0]); b
++) {
4204 vsx
->vsx_queue_histo
[t
][b
] +=
4205 cvsx
->vsx_queue_histo
[t
][b
];
4207 vsx
->vsx_active_queue
[t
] += cvsx
->vsx_active_queue
[t
];
4208 vsx
->vsx_pend_queue
[t
] += cvsx
->vsx_pend_queue
[t
];
4210 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_ind_histo
[0]); b
++)
4211 vsx
->vsx_ind_histo
[t
][b
] += cvsx
->vsx_ind_histo
[t
][b
];
4213 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_agg_histo
[0]); b
++)
4214 vsx
->vsx_agg_histo
[t
][b
] += cvsx
->vsx_agg_histo
[t
][b
];
4220 vdev_is_spacemap_addressable(vdev_t
*vd
)
4222 if (spa_feature_is_active(vd
->vdev_spa
, SPA_FEATURE_SPACEMAP_V2
))
4226 * If double-word space map entries are not enabled we assume
4227 * 47 bits of the space map entry are dedicated to the entry's
4228 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
4229 * to calculate the maximum address that can be described by a
4230 * space map entry for the given device.
4232 uint64_t shift
= vd
->vdev_ashift
+ SM_OFFSET_BITS
;
4234 if (shift
>= 63) /* detect potential overflow */
4237 return (vd
->vdev_asize
< (1ULL << shift
));
4241 * Get statistics for the given vdev.
4244 vdev_get_stats_ex_impl(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
4248 * If we're getting stats on the root vdev, aggregate the I/O counts
4249 * over all top-level vdevs (i.e. the direct children of the root).
4251 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4253 memset(vs
->vs_ops
, 0, sizeof (vs
->vs_ops
));
4254 memset(vs
->vs_bytes
, 0, sizeof (vs
->vs_bytes
));
4257 memset(vsx
, 0, sizeof (*vsx
));
4259 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4260 vdev_t
*cvd
= vd
->vdev_child
[c
];
4261 vdev_stat_t
*cvs
= &cvd
->vdev_stat
;
4262 vdev_stat_ex_t
*cvsx
= &cvd
->vdev_stat_ex
;
4264 vdev_get_stats_ex_impl(cvd
, cvs
, cvsx
);
4266 vdev_get_child_stat(cvd
, vs
, cvs
);
4268 vdev_get_child_stat_ex(cvd
, vsx
, cvsx
);
4272 * We're a leaf. Just copy our ZIO active queue stats in. The
4273 * other leaf stats are updated in vdev_stat_update().
4278 memcpy(vsx
, &vd
->vdev_stat_ex
, sizeof (vd
->vdev_stat_ex
));
4280 for (t
= 0; t
< ARRAY_SIZE(vd
->vdev_queue
.vq_class
); t
++) {
4281 vsx
->vsx_active_queue
[t
] =
4282 vd
->vdev_queue
.vq_class
[t
].vqc_active
;
4283 vsx
->vsx_pend_queue
[t
] = avl_numnodes(
4284 &vd
->vdev_queue
.vq_class
[t
].vqc_queued_tree
);
4290 vdev_get_stats_ex(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
4292 vdev_t
*tvd
= vd
->vdev_top
;
4293 mutex_enter(&vd
->vdev_stat_lock
);
4295 bcopy(&vd
->vdev_stat
, vs
, sizeof (*vs
));
4296 vs
->vs_timestamp
= gethrtime() - vs
->vs_timestamp
;
4297 vs
->vs_state
= vd
->vdev_state
;
4298 vs
->vs_rsize
= vdev_get_min_asize(vd
);
4300 if (vd
->vdev_ops
->vdev_op_leaf
) {
4301 vs
->vs_rsize
+= VDEV_LABEL_START_SIZE
+
4302 VDEV_LABEL_END_SIZE
;
4304 * Report initializing progress. Since we don't
4305 * have the initializing locks held, this is only
4306 * an estimate (although a fairly accurate one).
4308 vs
->vs_initialize_bytes_done
=
4309 vd
->vdev_initialize_bytes_done
;
4310 vs
->vs_initialize_bytes_est
=
4311 vd
->vdev_initialize_bytes_est
;
4312 vs
->vs_initialize_state
= vd
->vdev_initialize_state
;
4313 vs
->vs_initialize_action_time
=
4314 vd
->vdev_initialize_action_time
;
4317 * Report manual TRIM progress. Since we don't have
4318 * the manual TRIM locks held, this is only an
4319 * estimate (although fairly accurate one).
4321 vs
->vs_trim_notsup
= !vd
->vdev_has_trim
;
4322 vs
->vs_trim_bytes_done
= vd
->vdev_trim_bytes_done
;
4323 vs
->vs_trim_bytes_est
= vd
->vdev_trim_bytes_est
;
4324 vs
->vs_trim_state
= vd
->vdev_trim_state
;
4325 vs
->vs_trim_action_time
= vd
->vdev_trim_action_time
;
4327 /* Set when there is a deferred resilver. */
4328 vs
->vs_resilver_deferred
= vd
->vdev_resilver_deferred
;
4332 * Report expandable space on top-level, non-auxiliary devices
4333 * only. The expandable space is reported in terms of metaslab
4334 * sized units since that determines how much space the pool
4337 if (vd
->vdev_aux
== NULL
&& tvd
!= NULL
) {
4338 vs
->vs_esize
= P2ALIGN(
4339 vd
->vdev_max_asize
- vd
->vdev_asize
,
4340 1ULL << tvd
->vdev_ms_shift
);
4343 vs
->vs_configured_ashift
= vd
->vdev_top
!= NULL
4344 ? vd
->vdev_top
->vdev_ashift
: vd
->vdev_ashift
;
4345 vs
->vs_logical_ashift
= vd
->vdev_logical_ashift
;
4346 vs
->vs_physical_ashift
= vd
->vdev_physical_ashift
;
4349 * Report fragmentation and rebuild progress for top-level,
4350 * non-auxiliary, concrete devices.
4352 if (vd
->vdev_aux
== NULL
&& vd
== vd
->vdev_top
&&
4353 vdev_is_concrete(vd
)) {
4355 * The vdev fragmentation rating doesn't take into
4356 * account the embedded slog metaslab (vdev_log_mg).
4357 * Since it's only one metaslab, it would have a tiny
4358 * impact on the overall fragmentation.
4360 vs
->vs_fragmentation
= (vd
->vdev_mg
!= NULL
) ?
4361 vd
->vdev_mg
->mg_fragmentation
: 0;
4365 vdev_get_stats_ex_impl(vd
, vs
, vsx
);
4366 mutex_exit(&vd
->vdev_stat_lock
);
4370 vdev_get_stats(vdev_t
*vd
, vdev_stat_t
*vs
)
4372 return (vdev_get_stats_ex(vd
, vs
, NULL
));
4376 vdev_clear_stats(vdev_t
*vd
)
4378 mutex_enter(&vd
->vdev_stat_lock
);
4379 vd
->vdev_stat
.vs_space
= 0;
4380 vd
->vdev_stat
.vs_dspace
= 0;
4381 vd
->vdev_stat
.vs_alloc
= 0;
4382 mutex_exit(&vd
->vdev_stat_lock
);
4386 vdev_scan_stat_init(vdev_t
*vd
)
4388 vdev_stat_t
*vs
= &vd
->vdev_stat
;
4390 for (int c
= 0; c
< vd
->vdev_children
; c
++)
4391 vdev_scan_stat_init(vd
->vdev_child
[c
]);
4393 mutex_enter(&vd
->vdev_stat_lock
);
4394 vs
->vs_scan_processed
= 0;
4395 mutex_exit(&vd
->vdev_stat_lock
);
4399 vdev_stat_update(zio_t
*zio
, uint64_t psize
)
4401 spa_t
*spa
= zio
->io_spa
;
4402 vdev_t
*rvd
= spa
->spa_root_vdev
;
4403 vdev_t
*vd
= zio
->io_vd
? zio
->io_vd
: rvd
;
4405 uint64_t txg
= zio
->io_txg
;
4406 vdev_stat_t
*vs
= &vd
->vdev_stat
;
4407 vdev_stat_ex_t
*vsx
= &vd
->vdev_stat_ex
;
4408 zio_type_t type
= zio
->io_type
;
4409 int flags
= zio
->io_flags
;
4412 * If this i/o is a gang leader, it didn't do any actual work.
4414 if (zio
->io_gang_tree
)
4417 if (zio
->io_error
== 0) {
4419 * If this is a root i/o, don't count it -- we've already
4420 * counted the top-level vdevs, and vdev_get_stats() will
4421 * aggregate them when asked. This reduces contention on
4422 * the root vdev_stat_lock and implicitly handles blocks
4423 * that compress away to holes, for which there is no i/o.
4424 * (Holes never create vdev children, so all the counters
4425 * remain zero, which is what we want.)
4427 * Note: this only applies to successful i/o (io_error == 0)
4428 * because unlike i/o counts, errors are not additive.
4429 * When reading a ditto block, for example, failure of
4430 * one top-level vdev does not imply a root-level error.
4435 ASSERT(vd
== zio
->io_vd
);
4437 if (flags
& ZIO_FLAG_IO_BYPASS
)
4440 mutex_enter(&vd
->vdev_stat_lock
);
4442 if (flags
& ZIO_FLAG_IO_REPAIR
) {
4444 * Repair is the result of a resilver issued by the
4445 * scan thread (spa_sync).
4447 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
4448 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
4449 dsl_scan_phys_t
*scn_phys
= &scn
->scn_phys
;
4450 uint64_t *processed
= &scn_phys
->scn_processed
;
4452 if (vd
->vdev_ops
->vdev_op_leaf
)
4453 atomic_add_64(processed
, psize
);
4454 vs
->vs_scan_processed
+= psize
;
4458 * Repair is the result of a rebuild issued by the
4459 * rebuild thread (vdev_rebuild_thread). To avoid
4460 * double counting repaired bytes the virtual dRAID
4461 * spare vdev is excluded from the processed bytes.
4463 if (zio
->io_priority
== ZIO_PRIORITY_REBUILD
) {
4464 vdev_t
*tvd
= vd
->vdev_top
;
4465 vdev_rebuild_t
*vr
= &tvd
->vdev_rebuild_config
;
4466 vdev_rebuild_phys_t
*vrp
= &vr
->vr_rebuild_phys
;
4467 uint64_t *rebuilt
= &vrp
->vrp_bytes_rebuilt
;
4469 if (vd
->vdev_ops
->vdev_op_leaf
&&
4470 vd
->vdev_ops
!= &vdev_draid_spare_ops
) {
4471 atomic_add_64(rebuilt
, psize
);
4473 vs
->vs_rebuild_processed
+= psize
;
4476 if (flags
& ZIO_FLAG_SELF_HEAL
)
4477 vs
->vs_self_healed
+= psize
;
4481 * The bytes/ops/histograms are recorded at the leaf level and
4482 * aggregated into the higher level vdevs in vdev_get_stats().
4484 if (vd
->vdev_ops
->vdev_op_leaf
&&
4485 (zio
->io_priority
< ZIO_PRIORITY_NUM_QUEUEABLE
)) {
4486 zio_type_t vs_type
= type
;
4487 zio_priority_t priority
= zio
->io_priority
;
4490 * TRIM ops and bytes are reported to user space as
4491 * ZIO_TYPE_IOCTL. This is done to preserve the
4492 * vdev_stat_t structure layout for user space.
4494 if (type
== ZIO_TYPE_TRIM
)
4495 vs_type
= ZIO_TYPE_IOCTL
;
4498 * Solely for the purposes of 'zpool iostat -lqrw'
4499 * reporting use the priority to catagorize the IO.
4500 * Only the following are reported to user space:
4502 * ZIO_PRIORITY_SYNC_READ,
4503 * ZIO_PRIORITY_SYNC_WRITE,
4504 * ZIO_PRIORITY_ASYNC_READ,
4505 * ZIO_PRIORITY_ASYNC_WRITE,
4506 * ZIO_PRIORITY_SCRUB,
4507 * ZIO_PRIORITY_TRIM.
4509 if (priority
== ZIO_PRIORITY_REBUILD
) {
4510 priority
= ((type
== ZIO_TYPE_WRITE
) ?
4511 ZIO_PRIORITY_ASYNC_WRITE
:
4512 ZIO_PRIORITY_SCRUB
);
4513 } else if (priority
== ZIO_PRIORITY_INITIALIZING
) {
4514 ASSERT3U(type
, ==, ZIO_TYPE_WRITE
);
4515 priority
= ZIO_PRIORITY_ASYNC_WRITE
;
4516 } else if (priority
== ZIO_PRIORITY_REMOVAL
) {
4517 priority
= ((type
== ZIO_TYPE_WRITE
) ?
4518 ZIO_PRIORITY_ASYNC_WRITE
:
4519 ZIO_PRIORITY_ASYNC_READ
);
4522 vs
->vs_ops
[vs_type
]++;
4523 vs
->vs_bytes
[vs_type
] += psize
;
4525 if (flags
& ZIO_FLAG_DELEGATED
) {
4526 vsx
->vsx_agg_histo
[priority
]
4527 [RQ_HISTO(zio
->io_size
)]++;
4529 vsx
->vsx_ind_histo
[priority
]
4530 [RQ_HISTO(zio
->io_size
)]++;
4533 if (zio
->io_delta
&& zio
->io_delay
) {
4534 vsx
->vsx_queue_histo
[priority
]
4535 [L_HISTO(zio
->io_delta
- zio
->io_delay
)]++;
4536 vsx
->vsx_disk_histo
[type
]
4537 [L_HISTO(zio
->io_delay
)]++;
4538 vsx
->vsx_total_histo
[type
]
4539 [L_HISTO(zio
->io_delta
)]++;
4543 mutex_exit(&vd
->vdev_stat_lock
);
4547 if (flags
& ZIO_FLAG_SPECULATIVE
)
4551 * If this is an I/O error that is going to be retried, then ignore the
4552 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
4553 * hard errors, when in reality they can happen for any number of
4554 * innocuous reasons (bus resets, MPxIO link failure, etc).
4556 if (zio
->io_error
== EIO
&&
4557 !(zio
->io_flags
& ZIO_FLAG_IO_RETRY
))
4561 * Intent logs writes won't propagate their error to the root
4562 * I/O so don't mark these types of failures as pool-level
4565 if (zio
->io_vd
== NULL
&& (zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
4568 if (type
== ZIO_TYPE_WRITE
&& txg
!= 0 &&
4569 (!(flags
& ZIO_FLAG_IO_REPAIR
) ||
4570 (flags
& ZIO_FLAG_SCAN_THREAD
) ||
4571 spa
->spa_claiming
)) {
4573 * This is either a normal write (not a repair), or it's
4574 * a repair induced by the scrub thread, or it's a repair
4575 * made by zil_claim() during spa_load() in the first txg.
4576 * In the normal case, we commit the DTL change in the same
4577 * txg as the block was born. In the scrub-induced repair
4578 * case, we know that scrubs run in first-pass syncing context,
4579 * so we commit the DTL change in spa_syncing_txg(spa).
4580 * In the zil_claim() case, we commit in spa_first_txg(spa).
4582 * We currently do not make DTL entries for failed spontaneous
4583 * self-healing writes triggered by normal (non-scrubbing)
4584 * reads, because we have no transactional context in which to
4585 * do so -- and it's not clear that it'd be desirable anyway.
4587 if (vd
->vdev_ops
->vdev_op_leaf
) {
4588 uint64_t commit_txg
= txg
;
4589 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
4590 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
4591 ASSERT(spa_sync_pass(spa
) == 1);
4592 vdev_dtl_dirty(vd
, DTL_SCRUB
, txg
, 1);
4593 commit_txg
= spa_syncing_txg(spa
);
4594 } else if (spa
->spa_claiming
) {
4595 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
4596 commit_txg
= spa_first_txg(spa
);
4598 ASSERT(commit_txg
>= spa_syncing_txg(spa
));
4599 if (vdev_dtl_contains(vd
, DTL_MISSING
, txg
, 1))
4601 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
4602 vdev_dtl_dirty(pvd
, DTL_PARTIAL
, txg
, 1);
4603 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, commit_txg
);
4606 vdev_dtl_dirty(vd
, DTL_MISSING
, txg
, 1);
4611 vdev_deflated_space(vdev_t
*vd
, int64_t space
)
4613 ASSERT((space
& (SPA_MINBLOCKSIZE
-1)) == 0);
4614 ASSERT(vd
->vdev_deflate_ratio
!= 0 || vd
->vdev_isl2cache
);
4616 return ((space
>> SPA_MINBLOCKSHIFT
) * vd
->vdev_deflate_ratio
);
4620 * Update the in-core space usage stats for this vdev, its metaslab class,
4621 * and the root vdev.
4624 vdev_space_update(vdev_t
*vd
, int64_t alloc_delta
, int64_t defer_delta
,
4625 int64_t space_delta
)
4627 int64_t dspace_delta
;
4628 spa_t
*spa
= vd
->vdev_spa
;
4629 vdev_t
*rvd
= spa
->spa_root_vdev
;
4631 ASSERT(vd
== vd
->vdev_top
);
4634 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
4635 * factor. We must calculate this here and not at the root vdev
4636 * because the root vdev's psize-to-asize is simply the max of its
4637 * children's, thus not accurate enough for us.
4639 dspace_delta
= vdev_deflated_space(vd
, space_delta
);
4641 mutex_enter(&vd
->vdev_stat_lock
);
4642 /* ensure we won't underflow */
4643 if (alloc_delta
< 0) {
4644 ASSERT3U(vd
->vdev_stat
.vs_alloc
, >=, -alloc_delta
);
4647 vd
->vdev_stat
.vs_alloc
+= alloc_delta
;
4648 vd
->vdev_stat
.vs_space
+= space_delta
;
4649 vd
->vdev_stat
.vs_dspace
+= dspace_delta
;
4650 mutex_exit(&vd
->vdev_stat_lock
);
4652 /* every class but log contributes to root space stats */
4653 if (vd
->vdev_mg
!= NULL
&& !vd
->vdev_islog
) {
4654 ASSERT(!vd
->vdev_isl2cache
);
4655 mutex_enter(&rvd
->vdev_stat_lock
);
4656 rvd
->vdev_stat
.vs_alloc
+= alloc_delta
;
4657 rvd
->vdev_stat
.vs_space
+= space_delta
;
4658 rvd
->vdev_stat
.vs_dspace
+= dspace_delta
;
4659 mutex_exit(&rvd
->vdev_stat_lock
);
4661 /* Note: metaslab_class_space_update moved to metaslab_space_update */
4665 * Mark a top-level vdev's config as dirty, placing it on the dirty list
4666 * so that it will be written out next time the vdev configuration is synced.
4667 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
4670 vdev_config_dirty(vdev_t
*vd
)
4672 spa_t
*spa
= vd
->vdev_spa
;
4673 vdev_t
*rvd
= spa
->spa_root_vdev
;
4676 ASSERT(spa_writeable(spa
));
4679 * If this is an aux vdev (as with l2cache and spare devices), then we
4680 * update the vdev config manually and set the sync flag.
4682 if (vd
->vdev_aux
!= NULL
) {
4683 spa_aux_vdev_t
*sav
= vd
->vdev_aux
;
4687 for (c
= 0; c
< sav
->sav_count
; c
++) {
4688 if (sav
->sav_vdevs
[c
] == vd
)
4692 if (c
== sav
->sav_count
) {
4694 * We're being removed. There's nothing more to do.
4696 ASSERT(sav
->sav_sync
== B_TRUE
);
4700 sav
->sav_sync
= B_TRUE
;
4702 if (nvlist_lookup_nvlist_array(sav
->sav_config
,
4703 ZPOOL_CONFIG_L2CACHE
, &aux
, &naux
) != 0) {
4704 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
4705 ZPOOL_CONFIG_SPARES
, &aux
, &naux
) == 0);
4711 * Setting the nvlist in the middle if the array is a little
4712 * sketchy, but it will work.
4714 nvlist_free(aux
[c
]);
4715 aux
[c
] = vdev_config_generate(spa
, vd
, B_TRUE
, 0);
4721 * The dirty list is protected by the SCL_CONFIG lock. The caller
4722 * must either hold SCL_CONFIG as writer, or must be the sync thread
4723 * (which holds SCL_CONFIG as reader). There's only one sync thread,
4724 * so this is sufficient to ensure mutual exclusion.
4726 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
4727 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
4728 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
4731 for (c
= 0; c
< rvd
->vdev_children
; c
++)
4732 vdev_config_dirty(rvd
->vdev_child
[c
]);
4734 ASSERT(vd
== vd
->vdev_top
);
4736 if (!list_link_active(&vd
->vdev_config_dirty_node
) &&
4737 vdev_is_concrete(vd
)) {
4738 list_insert_head(&spa
->spa_config_dirty_list
, vd
);
4744 vdev_config_clean(vdev_t
*vd
)
4746 spa_t
*spa
= vd
->vdev_spa
;
4748 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
4749 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
4750 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
4752 ASSERT(list_link_active(&vd
->vdev_config_dirty_node
));
4753 list_remove(&spa
->spa_config_dirty_list
, vd
);
4757 * Mark a top-level vdev's state as dirty, so that the next pass of
4758 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
4759 * the state changes from larger config changes because they require
4760 * much less locking, and are often needed for administrative actions.
4763 vdev_state_dirty(vdev_t
*vd
)
4765 spa_t
*spa
= vd
->vdev_spa
;
4767 ASSERT(spa_writeable(spa
));
4768 ASSERT(vd
== vd
->vdev_top
);
4771 * The state list is protected by the SCL_STATE lock. The caller
4772 * must either hold SCL_STATE as writer, or must be the sync thread
4773 * (which holds SCL_STATE as reader). There's only one sync thread,
4774 * so this is sufficient to ensure mutual exclusion.
4776 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
4777 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
4778 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
4780 if (!list_link_active(&vd
->vdev_state_dirty_node
) &&
4781 vdev_is_concrete(vd
))
4782 list_insert_head(&spa
->spa_state_dirty_list
, vd
);
4786 vdev_state_clean(vdev_t
*vd
)
4788 spa_t
*spa
= vd
->vdev_spa
;
4790 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
4791 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
4792 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
4794 ASSERT(list_link_active(&vd
->vdev_state_dirty_node
));
4795 list_remove(&spa
->spa_state_dirty_list
, vd
);
4799 * Propagate vdev state up from children to parent.
4802 vdev_propagate_state(vdev_t
*vd
)
4804 spa_t
*spa
= vd
->vdev_spa
;
4805 vdev_t
*rvd
= spa
->spa_root_vdev
;
4806 int degraded
= 0, faulted
= 0;
4810 if (vd
->vdev_children
> 0) {
4811 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4812 child
= vd
->vdev_child
[c
];
4815 * Don't factor holes or indirect vdevs into the
4818 if (!vdev_is_concrete(child
))
4821 if (!vdev_readable(child
) ||
4822 (!vdev_writeable(child
) && spa_writeable(spa
))) {
4824 * Root special: if there is a top-level log
4825 * device, treat the root vdev as if it were
4828 if (child
->vdev_islog
&& vd
== rvd
)
4832 } else if (child
->vdev_state
<= VDEV_STATE_DEGRADED
) {
4836 if (child
->vdev_stat
.vs_aux
== VDEV_AUX_CORRUPT_DATA
)
4840 vd
->vdev_ops
->vdev_op_state_change(vd
, faulted
, degraded
);
4843 * Root special: if there is a top-level vdev that cannot be
4844 * opened due to corrupted metadata, then propagate the root
4845 * vdev's aux state as 'corrupt' rather than 'insufficient
4848 if (corrupted
&& vd
== rvd
&&
4849 rvd
->vdev_state
== VDEV_STATE_CANT_OPEN
)
4850 vdev_set_state(rvd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
4851 VDEV_AUX_CORRUPT_DATA
);
4854 if (vd
->vdev_parent
)
4855 vdev_propagate_state(vd
->vdev_parent
);
4859 * Set a vdev's state. If this is during an open, we don't update the parent
4860 * state, because we're in the process of opening children depth-first.
4861 * Otherwise, we propagate the change to the parent.
4863 * If this routine places a device in a faulted state, an appropriate ereport is
4867 vdev_set_state(vdev_t
*vd
, boolean_t isopen
, vdev_state_t state
, vdev_aux_t aux
)
4869 uint64_t save_state
;
4870 spa_t
*spa
= vd
->vdev_spa
;
4872 if (state
== vd
->vdev_state
) {
4874 * Since vdev_offline() code path is already in an offline
4875 * state we can miss a statechange event to OFFLINE. Check
4876 * the previous state to catch this condition.
4878 if (vd
->vdev_ops
->vdev_op_leaf
&&
4879 (state
== VDEV_STATE_OFFLINE
) &&
4880 (vd
->vdev_prevstate
>= VDEV_STATE_FAULTED
)) {
4881 /* post an offline state change */
4882 zfs_post_state_change(spa
, vd
, vd
->vdev_prevstate
);
4884 vd
->vdev_stat
.vs_aux
= aux
;
4888 save_state
= vd
->vdev_state
;
4890 vd
->vdev_state
= state
;
4891 vd
->vdev_stat
.vs_aux
= aux
;
4894 * If we are setting the vdev state to anything but an open state, then
4895 * always close the underlying device unless the device has requested
4896 * a delayed close (i.e. we're about to remove or fault the device).
4897 * Otherwise, we keep accessible but invalid devices open forever.
4898 * We don't call vdev_close() itself, because that implies some extra
4899 * checks (offline, etc) that we don't want here. This is limited to
4900 * leaf devices, because otherwise closing the device will affect other
4903 if (!vd
->vdev_delayed_close
&& vdev_is_dead(vd
) &&
4904 vd
->vdev_ops
->vdev_op_leaf
)
4905 vd
->vdev_ops
->vdev_op_close(vd
);
4907 if (vd
->vdev_removed
&&
4908 state
== VDEV_STATE_CANT_OPEN
&&
4909 (aux
== VDEV_AUX_OPEN_FAILED
|| vd
->vdev_checkremove
)) {
4911 * If the previous state is set to VDEV_STATE_REMOVED, then this
4912 * device was previously marked removed and someone attempted to
4913 * reopen it. If this failed due to a nonexistent device, then
4914 * keep the device in the REMOVED state. We also let this be if
4915 * it is one of our special test online cases, which is only
4916 * attempting to online the device and shouldn't generate an FMA
4919 vd
->vdev_state
= VDEV_STATE_REMOVED
;
4920 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
4921 } else if (state
== VDEV_STATE_REMOVED
) {
4922 vd
->vdev_removed
= B_TRUE
;
4923 } else if (state
== VDEV_STATE_CANT_OPEN
) {
4925 * If we fail to open a vdev during an import or recovery, we
4926 * mark it as "not available", which signifies that it was
4927 * never there to begin with. Failure to open such a device
4928 * is not considered an error.
4930 if ((spa_load_state(spa
) == SPA_LOAD_IMPORT
||
4931 spa_load_state(spa
) == SPA_LOAD_RECOVER
) &&
4932 vd
->vdev_ops
->vdev_op_leaf
)
4933 vd
->vdev_not_present
= 1;
4936 * Post the appropriate ereport. If the 'prevstate' field is
4937 * set to something other than VDEV_STATE_UNKNOWN, it indicates
4938 * that this is part of a vdev_reopen(). In this case, we don't
4939 * want to post the ereport if the device was already in the
4940 * CANT_OPEN state beforehand.
4942 * If the 'checkremove' flag is set, then this is an attempt to
4943 * online the device in response to an insertion event. If we
4944 * hit this case, then we have detected an insertion event for a
4945 * faulted or offline device that wasn't in the removed state.
4946 * In this scenario, we don't post an ereport because we are
4947 * about to replace the device, or attempt an online with
4948 * vdev_forcefault, which will generate the fault for us.
4950 if ((vd
->vdev_prevstate
!= state
|| vd
->vdev_forcefault
) &&
4951 !vd
->vdev_not_present
&& !vd
->vdev_checkremove
&&
4952 vd
!= spa
->spa_root_vdev
) {
4956 case VDEV_AUX_OPEN_FAILED
:
4957 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED
;
4959 case VDEV_AUX_CORRUPT_DATA
:
4960 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA
;
4962 case VDEV_AUX_NO_REPLICAS
:
4963 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS
;
4965 case VDEV_AUX_BAD_GUID_SUM
:
4966 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM
;
4968 case VDEV_AUX_TOO_SMALL
:
4969 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL
;
4971 case VDEV_AUX_BAD_LABEL
:
4972 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL
;
4974 case VDEV_AUX_BAD_ASHIFT
:
4975 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
;
4978 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN
;
4981 (void) zfs_ereport_post(class, spa
, vd
, NULL
, NULL
,
4985 /* Erase any notion of persistent removed state */
4986 vd
->vdev_removed
= B_FALSE
;
4988 vd
->vdev_removed
= B_FALSE
;
4992 * Notify ZED of any significant state-change on a leaf vdev.
4995 if (vd
->vdev_ops
->vdev_op_leaf
) {
4996 /* preserve original state from a vdev_reopen() */
4997 if ((vd
->vdev_prevstate
!= VDEV_STATE_UNKNOWN
) &&
4998 (vd
->vdev_prevstate
!= vd
->vdev_state
) &&
4999 (save_state
<= VDEV_STATE_CLOSED
))
5000 save_state
= vd
->vdev_prevstate
;
5002 /* filter out state change due to initial vdev_open */
5003 if (save_state
> VDEV_STATE_CLOSED
)
5004 zfs_post_state_change(spa
, vd
, save_state
);
5007 if (!isopen
&& vd
->vdev_parent
)
5008 vdev_propagate_state(vd
->vdev_parent
);
5012 vdev_children_are_offline(vdev_t
*vd
)
5014 ASSERT(!vd
->vdev_ops
->vdev_op_leaf
);
5016 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
5017 if (vd
->vdev_child
[i
]->vdev_state
!= VDEV_STATE_OFFLINE
)
5025 * Check the vdev configuration to ensure that it's capable of supporting
5026 * a root pool. We do not support partial configuration.
5029 vdev_is_bootable(vdev_t
*vd
)
5031 if (!vd
->vdev_ops
->vdev_op_leaf
) {
5032 const char *vdev_type
= vd
->vdev_ops
->vdev_op_type
;
5034 if (strcmp(vdev_type
, VDEV_TYPE_MISSING
) == 0 ||
5035 strcmp(vdev_type
, VDEV_TYPE_INDIRECT
) == 0) {
5040 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5041 if (!vdev_is_bootable(vd
->vdev_child
[c
]))
5048 vdev_is_concrete(vdev_t
*vd
)
5050 vdev_ops_t
*ops
= vd
->vdev_ops
;
5051 if (ops
== &vdev_indirect_ops
|| ops
== &vdev_hole_ops
||
5052 ops
== &vdev_missing_ops
|| ops
== &vdev_root_ops
) {
5060 * Determine if a log device has valid content. If the vdev was
5061 * removed or faulted in the MOS config then we know that
5062 * the content on the log device has already been written to the pool.
5065 vdev_log_state_valid(vdev_t
*vd
)
5067 if (vd
->vdev_ops
->vdev_op_leaf
&& !vd
->vdev_faulted
&&
5071 for (int c
= 0; c
< vd
->vdev_children
; c
++)
5072 if (vdev_log_state_valid(vd
->vdev_child
[c
]))
5079 * Expand a vdev if possible.
5082 vdev_expand(vdev_t
*vd
, uint64_t txg
)
5084 ASSERT(vd
->vdev_top
== vd
);
5085 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5086 ASSERT(vdev_is_concrete(vd
));
5088 vdev_set_deflate_ratio(vd
);
5090 if ((vd
->vdev_asize
>> vd
->vdev_ms_shift
) > vd
->vdev_ms_count
&&
5091 vdev_is_concrete(vd
)) {
5092 vdev_metaslab_group_create(vd
);
5093 VERIFY(vdev_metaslab_init(vd
, txg
) == 0);
5094 vdev_config_dirty(vd
);
5102 vdev_split(vdev_t
*vd
)
5104 vdev_t
*cvd
, *pvd
= vd
->vdev_parent
;
5106 vdev_remove_child(pvd
, vd
);
5107 vdev_compact_children(pvd
);
5109 cvd
= pvd
->vdev_child
[0];
5110 if (pvd
->vdev_children
== 1) {
5111 vdev_remove_parent(cvd
);
5112 cvd
->vdev_splitting
= B_TRUE
;
5114 vdev_propagate_state(cvd
);
5118 vdev_deadman(vdev_t
*vd
, char *tag
)
5120 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5121 vdev_t
*cvd
= vd
->vdev_child
[c
];
5123 vdev_deadman(cvd
, tag
);
5126 if (vd
->vdev_ops
->vdev_op_leaf
) {
5127 vdev_queue_t
*vq
= &vd
->vdev_queue
;
5129 mutex_enter(&vq
->vq_lock
);
5130 if (avl_numnodes(&vq
->vq_active_tree
) > 0) {
5131 spa_t
*spa
= vd
->vdev_spa
;
5135 zfs_dbgmsg("slow vdev: %s has %d active IOs",
5136 vd
->vdev_path
, avl_numnodes(&vq
->vq_active_tree
));
5139 * Look at the head of all the pending queues,
5140 * if any I/O has been outstanding for longer than
5141 * the spa_deadman_synctime invoke the deadman logic.
5143 fio
= avl_first(&vq
->vq_active_tree
);
5144 delta
= gethrtime() - fio
->io_timestamp
;
5145 if (delta
> spa_deadman_synctime(spa
))
5146 zio_deadman(fio
, tag
);
5148 mutex_exit(&vq
->vq_lock
);
5153 vdev_defer_resilver(vdev_t
*vd
)
5155 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
5157 vd
->vdev_resilver_deferred
= B_TRUE
;
5158 vd
->vdev_spa
->spa_resilver_deferred
= B_TRUE
;
5162 * Clears the resilver deferred flag on all leaf devs under vd. Returns
5163 * B_TRUE if we have devices that need to be resilvered and are available to
5164 * accept resilver I/Os.
5167 vdev_clear_resilver_deferred(vdev_t
*vd
, dmu_tx_t
*tx
)
5169 boolean_t resilver_needed
= B_FALSE
;
5170 spa_t
*spa
= vd
->vdev_spa
;
5172 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5173 vdev_t
*cvd
= vd
->vdev_child
[c
];
5174 resilver_needed
|= vdev_clear_resilver_deferred(cvd
, tx
);
5177 if (vd
== spa
->spa_root_vdev
&&
5178 spa_feature_is_active(spa
, SPA_FEATURE_RESILVER_DEFER
)) {
5179 spa_feature_decr(spa
, SPA_FEATURE_RESILVER_DEFER
, tx
);
5180 vdev_config_dirty(vd
);
5181 spa
->spa_resilver_deferred
= B_FALSE
;
5182 return (resilver_needed
);
5185 if (!vdev_is_concrete(vd
) || vd
->vdev_aux
||
5186 !vd
->vdev_ops
->vdev_op_leaf
)
5187 return (resilver_needed
);
5189 vd
->vdev_resilver_deferred
= B_FALSE
;
5191 return (!vdev_is_dead(vd
) && !vd
->vdev_offline
&&
5192 vdev_resilver_needed(vd
, NULL
, NULL
));
5196 vdev_xlate_is_empty(range_seg64_t
*rs
)
5198 return (rs
->rs_start
== rs
->rs_end
);
5202 * Translate a logical range to the first contiguous physical range for the
5203 * specified vdev_t. This function is initially called with a leaf vdev and
5204 * will walk each parent vdev until it reaches a top-level vdev. Once the
5205 * top-level is reached the physical range is initialized and the recursive
5206 * function begins to unwind. As it unwinds it calls the parent's vdev
5207 * specific translation function to do the real conversion.
5210 vdev_xlate(vdev_t
*vd
, const range_seg64_t
*logical_rs
,
5211 range_seg64_t
*physical_rs
, range_seg64_t
*remain_rs
)
5214 * Walk up the vdev tree
5216 if (vd
!= vd
->vdev_top
) {
5217 vdev_xlate(vd
->vdev_parent
, logical_rs
, physical_rs
,
5221 * We've reached the top-level vdev, initialize the physical
5222 * range to the logical range and set an empty remaining
5223 * range then start to unwind.
5225 physical_rs
->rs_start
= logical_rs
->rs_start
;
5226 physical_rs
->rs_end
= logical_rs
->rs_end
;
5228 remain_rs
->rs_start
= logical_rs
->rs_start
;
5229 remain_rs
->rs_end
= logical_rs
->rs_start
;
5234 vdev_t
*pvd
= vd
->vdev_parent
;
5235 ASSERT3P(pvd
, !=, NULL
);
5236 ASSERT3P(pvd
->vdev_ops
->vdev_op_xlate
, !=, NULL
);
5239 * As this recursive function unwinds, translate the logical
5240 * range into its physical and any remaining components by calling
5241 * the vdev specific translate function.
5243 range_seg64_t intermediate
= { 0 };
5244 pvd
->vdev_ops
->vdev_op_xlate(vd
, physical_rs
, &intermediate
, remain_rs
);
5246 physical_rs
->rs_start
= intermediate
.rs_start
;
5247 physical_rs
->rs_end
= intermediate
.rs_end
;
5251 vdev_xlate_walk(vdev_t
*vd
, const range_seg64_t
*logical_rs
,
5252 vdev_xlate_func_t
*func
, void *arg
)
5254 range_seg64_t iter_rs
= *logical_rs
;
5255 range_seg64_t physical_rs
;
5256 range_seg64_t remain_rs
;
5258 while (!vdev_xlate_is_empty(&iter_rs
)) {
5260 vdev_xlate(vd
, &iter_rs
, &physical_rs
, &remain_rs
);
5263 * With raidz and dRAID, it's possible that the logical range
5264 * does not live on this leaf vdev. Only when there is a non-
5265 * zero physical size call the provided function.
5267 if (!vdev_xlate_is_empty(&physical_rs
))
5268 func(arg
, &physical_rs
);
5270 iter_rs
= remain_rs
;
5275 * Look at the vdev tree and determine whether any devices are currently being
5279 vdev_replace_in_progress(vdev_t
*vdev
)
5281 ASSERT(spa_config_held(vdev
->vdev_spa
, SCL_ALL
, RW_READER
) != 0);
5283 if (vdev
->vdev_ops
== &vdev_replacing_ops
)
5287 * A 'spare' vdev indicates that we have a replace in progress, unless
5288 * it has exactly two children, and the second, the hot spare, has
5289 * finished being resilvered.
5291 if (vdev
->vdev_ops
== &vdev_spare_ops
&& (vdev
->vdev_children
> 2 ||
5292 !vdev_dtl_empty(vdev
->vdev_child
[1], DTL_MISSING
)))
5295 for (int i
= 0; i
< vdev
->vdev_children
; i
++) {
5296 if (vdev_replace_in_progress(vdev
->vdev_child
[i
]))
5303 EXPORT_SYMBOL(vdev_fault
);
5304 EXPORT_SYMBOL(vdev_degrade
);
5305 EXPORT_SYMBOL(vdev_online
);
5306 EXPORT_SYMBOL(vdev_offline
);
5307 EXPORT_SYMBOL(vdev_clear
);
5310 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, default_ms_count
, INT
, ZMOD_RW
,
5311 "Target number of metaslabs per top-level vdev");
5313 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, default_ms_shift
, INT
, ZMOD_RW
,
5314 "Default limit for metaslab size");
5316 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, min_ms_count
, INT
, ZMOD_RW
,
5317 "Minimum number of metaslabs per top-level vdev");
5319 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, ms_count_limit
, INT
, ZMOD_RW
,
5320 "Practical upper limit of total metaslabs per top-level vdev");
5322 ZFS_MODULE_PARAM(zfs
, zfs_
, slow_io_events_per_second
, UINT
, ZMOD_RW
,
5323 "Rate limit slow IO (delay) events to this many per second");
5325 ZFS_MODULE_PARAM(zfs
, zfs_
, checksum_events_per_second
, UINT
, ZMOD_RW
,
5326 "Rate limit checksum events to this many checksum errors per second "
5327 "(do not set below zed threshold).");
5329 ZFS_MODULE_PARAM(zfs
, zfs_
, scan_ignore_errors
, INT
, ZMOD_RW
,
5330 "Ignore errors during resilver/scrub");
5332 ZFS_MODULE_PARAM(zfs_vdev
, vdev_
, validate_skip
, INT
, ZMOD_RW
,
5333 "Bypass vdev_validate()");
5335 ZFS_MODULE_PARAM(zfs
, zfs_
, nocacheflush
, INT
, ZMOD_RW
,
5336 "Disable cache flushes");
5338 ZFS_MODULE_PARAM(zfs
, zfs_
, embedded_slog_min_ms
, INT
, ZMOD_RW
,
5339 "Minimum number of metaslabs required to dedicate one for log blocks");
5341 ZFS_MODULE_PARAM_CALL(zfs_vdev
, zfs_vdev_
, min_auto_ashift
,
5342 param_set_min_auto_ashift
, param_get_ulong
, ZMOD_RW
,
5343 "Minimum ashift used when creating new top-level vdevs");
5345 ZFS_MODULE_PARAM_CALL(zfs_vdev
, zfs_vdev_
, max_auto_ashift
,
5346 param_set_max_auto_ashift
, param_get_ulong
, ZMOD_RW
,
5347 "Maximum ashift used when optimizing for logical -> physical sector "
5348 "size on new top-level vdevs");