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, 2018 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.
31 #include <sys/zfs_context.h>
32 #include <sys/fm/fs/zfs.h>
34 #include <sys/spa_impl.h>
35 #include <sys/bpobj.h>
37 #include <sys/dmu_tx.h>
38 #include <sys/dsl_dir.h>
39 #include <sys/vdev_impl.h>
40 #include <sys/uberblock_impl.h>
41 #include <sys/metaslab.h>
42 #include <sys/metaslab_impl.h>
43 #include <sys/space_map.h>
44 #include <sys/space_reftree.h>
47 #include <sys/fs/zfs.h>
50 #include <sys/dsl_scan.h>
52 #include <sys/vdev_initialize.h>
55 * Virtual device management.
58 static vdev_ops_t
*vdev_ops_table
[] = {
72 /* maximum scrub/resilver I/O queue per leaf vdev */
73 int zfs_scrub_limit
= 10;
75 /* target number of metaslabs per top-level vdev */
76 int vdev_max_ms_count
= 200;
78 /* minimum number of metaslabs per top-level vdev */
79 int vdev_min_ms_count
= 16;
81 /* practical upper limit of total metaslabs per top-level vdev */
82 int vdev_ms_count_limit
= 1ULL << 17;
84 /* lower limit for metaslab size (512M) */
85 int vdev_default_ms_shift
= 29;
87 /* upper limit for metaslab size (256G) */
88 int vdev_max_ms_shift
= 38;
90 boolean_t vdev_validate_skip
= B_FALSE
;
93 * Since the DTL space map of a vdev is not expected to have a lot of
94 * entries, we default its block size to 4K.
96 int vdev_dtl_sm_blksz
= (1 << 12);
99 * vdev-wide space maps that have lots of entries written to them at
100 * the end of each transaction can benefit from a higher I/O bandwidth
101 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
103 int vdev_standard_sm_blksz
= (1 << 17);
109 vdev_dbgmsg(vdev_t
*vd
, const char *fmt
, ...)
115 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
118 if (vd
->vdev_path
!= NULL
) {
119 zfs_dbgmsg("%s vdev '%s': %s", vd
->vdev_ops
->vdev_op_type
,
122 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
123 vd
->vdev_ops
->vdev_op_type
,
124 (u_longlong_t
)vd
->vdev_id
,
125 (u_longlong_t
)vd
->vdev_guid
, buf
);
130 vdev_dbgmsg_print_tree(vdev_t
*vd
, int indent
)
134 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
) {
135 zfs_dbgmsg("%*svdev %u: %s", indent
, "", vd
->vdev_id
,
136 vd
->vdev_ops
->vdev_op_type
);
140 switch (vd
->vdev_state
) {
141 case VDEV_STATE_UNKNOWN
:
142 (void) snprintf(state
, sizeof (state
), "unknown");
144 case VDEV_STATE_CLOSED
:
145 (void) snprintf(state
, sizeof (state
), "closed");
147 case VDEV_STATE_OFFLINE
:
148 (void) snprintf(state
, sizeof (state
), "offline");
150 case VDEV_STATE_REMOVED
:
151 (void) snprintf(state
, sizeof (state
), "removed");
153 case VDEV_STATE_CANT_OPEN
:
154 (void) snprintf(state
, sizeof (state
), "can't open");
156 case VDEV_STATE_FAULTED
:
157 (void) snprintf(state
, sizeof (state
), "faulted");
159 case VDEV_STATE_DEGRADED
:
160 (void) snprintf(state
, sizeof (state
), "degraded");
162 case VDEV_STATE_HEALTHY
:
163 (void) snprintf(state
, sizeof (state
), "healthy");
166 (void) snprintf(state
, sizeof (state
), "<state %u>",
167 (uint_t
)vd
->vdev_state
);
170 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent
,
171 "", (int)vd
->vdev_id
, vd
->vdev_ops
->vdev_op_type
,
172 vd
->vdev_islog
? " (log)" : "",
173 (u_longlong_t
)vd
->vdev_guid
,
174 vd
->vdev_path
? vd
->vdev_path
: "N/A", state
);
176 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++)
177 vdev_dbgmsg_print_tree(vd
->vdev_child
[i
], indent
+ 2);
181 * Given a vdev type, return the appropriate ops vector.
184 vdev_getops(const char *type
)
186 vdev_ops_t
*ops
, **opspp
;
188 for (opspp
= vdev_ops_table
; (ops
= *opspp
) != NULL
; opspp
++)
189 if (strcmp(ops
->vdev_op_type
, type
) == 0)
197 vdev_default_xlate(vdev_t
*vd
, const range_seg_t
*in
, range_seg_t
*res
)
199 res
->rs_start
= in
->rs_start
;
200 res
->rs_end
= in
->rs_end
;
204 * Default asize function: return the MAX of psize with the asize of
205 * all children. This is what's used by anything other than RAID-Z.
208 vdev_default_asize(vdev_t
*vd
, uint64_t psize
)
210 uint64_t asize
= P2ROUNDUP(psize
, 1ULL << vd
->vdev_top
->vdev_ashift
);
213 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
214 csize
= vdev_psize_to_asize(vd
->vdev_child
[c
], psize
);
215 asize
= MAX(asize
, csize
);
222 * Get the minimum allocatable size. We define the allocatable size as
223 * the vdev's asize rounded to the nearest metaslab. This allows us to
224 * replace or attach devices which don't have the same physical size but
225 * can still satisfy the same number of allocations.
228 vdev_get_min_asize(vdev_t
*vd
)
230 vdev_t
*pvd
= vd
->vdev_parent
;
233 * If our parent is NULL (inactive spare or cache) or is the root,
234 * just return our own asize.
237 return (vd
->vdev_asize
);
240 * The top-level vdev just returns the allocatable size rounded
241 * to the nearest metaslab.
243 if (vd
== vd
->vdev_top
)
244 return (P2ALIGN(vd
->vdev_asize
, 1ULL << vd
->vdev_ms_shift
));
247 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
248 * so each child must provide at least 1/Nth of its asize.
250 if (pvd
->vdev_ops
== &vdev_raidz_ops
)
251 return ((pvd
->vdev_min_asize
+ pvd
->vdev_children
- 1) /
254 return (pvd
->vdev_min_asize
);
258 vdev_set_min_asize(vdev_t
*vd
)
260 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
262 for (int c
= 0; c
< vd
->vdev_children
; c
++)
263 vdev_set_min_asize(vd
->vdev_child
[c
]);
267 vdev_lookup_top(spa_t
*spa
, uint64_t vdev
)
269 vdev_t
*rvd
= spa
->spa_root_vdev
;
271 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
273 if (vdev
< rvd
->vdev_children
) {
274 ASSERT(rvd
->vdev_child
[vdev
] != NULL
);
275 return (rvd
->vdev_child
[vdev
]);
282 vdev_lookup_by_guid(vdev_t
*vd
, uint64_t guid
)
286 if (vd
->vdev_guid
== guid
)
289 for (int c
= 0; c
< vd
->vdev_children
; c
++)
290 if ((mvd
= vdev_lookup_by_guid(vd
->vdev_child
[c
], guid
)) !=
298 vdev_count_leaves_impl(vdev_t
*vd
)
302 if (vd
->vdev_ops
->vdev_op_leaf
)
305 for (int c
= 0; c
< vd
->vdev_children
; c
++)
306 n
+= vdev_count_leaves_impl(vd
->vdev_child
[c
]);
312 vdev_count_leaves(spa_t
*spa
)
314 return (vdev_count_leaves_impl(spa
->spa_root_vdev
));
318 vdev_add_child(vdev_t
*pvd
, vdev_t
*cvd
)
320 size_t oldsize
, newsize
;
321 uint64_t id
= cvd
->vdev_id
;
323 spa_t
*spa
= cvd
->vdev_spa
;
325 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
326 ASSERT(cvd
->vdev_parent
== NULL
);
328 cvd
->vdev_parent
= pvd
;
333 ASSERT(id
>= pvd
->vdev_children
|| pvd
->vdev_child
[id
] == NULL
);
335 oldsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
336 pvd
->vdev_children
= MAX(pvd
->vdev_children
, id
+ 1);
337 newsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
339 newchild
= kmem_zalloc(newsize
, KM_SLEEP
);
340 if (pvd
->vdev_child
!= NULL
) {
341 bcopy(pvd
->vdev_child
, newchild
, oldsize
);
342 kmem_free(pvd
->vdev_child
, oldsize
);
345 pvd
->vdev_child
= newchild
;
346 pvd
->vdev_child
[id
] = cvd
;
348 cvd
->vdev_top
= (pvd
->vdev_top
? pvd
->vdev_top
: cvd
);
349 ASSERT(cvd
->vdev_top
->vdev_parent
->vdev_parent
== NULL
);
352 * Walk up all ancestors to update guid sum.
354 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
355 pvd
->vdev_guid_sum
+= cvd
->vdev_guid_sum
;
359 vdev_remove_child(vdev_t
*pvd
, vdev_t
*cvd
)
362 uint_t id
= cvd
->vdev_id
;
364 ASSERT(cvd
->vdev_parent
== pvd
);
369 ASSERT(id
< pvd
->vdev_children
);
370 ASSERT(pvd
->vdev_child
[id
] == cvd
);
372 pvd
->vdev_child
[id
] = NULL
;
373 cvd
->vdev_parent
= NULL
;
375 for (c
= 0; c
< pvd
->vdev_children
; c
++)
376 if (pvd
->vdev_child
[c
])
379 if (c
== pvd
->vdev_children
) {
380 kmem_free(pvd
->vdev_child
, c
* sizeof (vdev_t
*));
381 pvd
->vdev_child
= NULL
;
382 pvd
->vdev_children
= 0;
386 * Walk up all ancestors to update guid sum.
388 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
389 pvd
->vdev_guid_sum
-= cvd
->vdev_guid_sum
;
393 * Remove any holes in the child array.
396 vdev_compact_children(vdev_t
*pvd
)
398 vdev_t
**newchild
, *cvd
;
399 int oldc
= pvd
->vdev_children
;
402 ASSERT(spa_config_held(pvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
404 for (int c
= newc
= 0; c
< oldc
; c
++)
405 if (pvd
->vdev_child
[c
])
408 newchild
= kmem_alloc(newc
* sizeof (vdev_t
*), KM_SLEEP
);
410 for (int c
= newc
= 0; c
< oldc
; c
++) {
411 if ((cvd
= pvd
->vdev_child
[c
]) != NULL
) {
412 newchild
[newc
] = cvd
;
413 cvd
->vdev_id
= newc
++;
417 kmem_free(pvd
->vdev_child
, oldc
* sizeof (vdev_t
*));
418 pvd
->vdev_child
= newchild
;
419 pvd
->vdev_children
= newc
;
423 * Allocate and minimally initialize a vdev_t.
426 vdev_alloc_common(spa_t
*spa
, uint_t id
, uint64_t guid
, vdev_ops_t
*ops
)
429 vdev_indirect_config_t
*vic
;
431 vd
= kmem_zalloc(sizeof (vdev_t
), KM_SLEEP
);
432 vic
= &vd
->vdev_indirect_config
;
434 if (spa
->spa_root_vdev
== NULL
) {
435 ASSERT(ops
== &vdev_root_ops
);
436 spa
->spa_root_vdev
= vd
;
437 spa
->spa_load_guid
= spa_generate_guid(NULL
);
440 if (guid
== 0 && ops
!= &vdev_hole_ops
) {
441 if (spa
->spa_root_vdev
== vd
) {
443 * The root vdev's guid will also be the pool guid,
444 * which must be unique among all pools.
446 guid
= spa_generate_guid(NULL
);
449 * Any other vdev's guid must be unique within the pool.
451 guid
= spa_generate_guid(spa
);
453 ASSERT(!spa_guid_exists(spa_guid(spa
), guid
));
458 vd
->vdev_guid
= guid
;
459 vd
->vdev_guid_sum
= guid
;
461 vd
->vdev_state
= VDEV_STATE_CLOSED
;
462 vd
->vdev_ishole
= (ops
== &vdev_hole_ops
);
463 vic
->vic_prev_indirect_vdev
= UINT64_MAX
;
465 rw_init(&vd
->vdev_indirect_rwlock
, NULL
, RW_DEFAULT
, NULL
);
466 mutex_init(&vd
->vdev_obsolete_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
467 vd
->vdev_obsolete_segments
= range_tree_create(NULL
, NULL
);
469 mutex_init(&vd
->vdev_dtl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
470 mutex_init(&vd
->vdev_stat_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
471 mutex_init(&vd
->vdev_probe_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
472 mutex_init(&vd
->vdev_queue_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
473 mutex_init(&vd
->vdev_initialize_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
474 mutex_init(&vd
->vdev_initialize_io_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
475 cv_init(&vd
->vdev_initialize_cv
, NULL
, CV_DEFAULT
, NULL
);
476 cv_init(&vd
->vdev_initialize_io_cv
, NULL
, CV_DEFAULT
, NULL
);
478 for (int t
= 0; t
< DTL_TYPES
; t
++) {
479 vd
->vdev_dtl
[t
] = range_tree_create(NULL
, NULL
);
481 txg_list_create(&vd
->vdev_ms_list
, spa
,
482 offsetof(struct metaslab
, ms_txg_node
));
483 txg_list_create(&vd
->vdev_dtl_list
, spa
,
484 offsetof(struct vdev
, vdev_dtl_node
));
485 vd
->vdev_stat
.vs_timestamp
= gethrtime();
493 * Allocate a new vdev. The 'alloctype' is used to control whether we are
494 * creating a new vdev or loading an existing one - the behavior is slightly
495 * different for each case.
498 vdev_alloc(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
, uint_t id
,
503 uint64_t guid
= 0, islog
, nparity
;
505 vdev_indirect_config_t
*vic
;
507 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
509 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
510 return (SET_ERROR(EINVAL
));
512 if ((ops
= vdev_getops(type
)) == NULL
)
513 return (SET_ERROR(EINVAL
));
516 * If this is a load, get the vdev guid from the nvlist.
517 * Otherwise, vdev_alloc_common() will generate one for us.
519 if (alloctype
== VDEV_ALLOC_LOAD
) {
522 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
, &label_id
) ||
524 return (SET_ERROR(EINVAL
));
526 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
527 return (SET_ERROR(EINVAL
));
528 } else if (alloctype
== VDEV_ALLOC_SPARE
) {
529 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
530 return (SET_ERROR(EINVAL
));
531 } else if (alloctype
== VDEV_ALLOC_L2CACHE
) {
532 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
533 return (SET_ERROR(EINVAL
));
534 } else if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
535 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
536 return (SET_ERROR(EINVAL
));
540 * The first allocated vdev must be of type 'root'.
542 if (ops
!= &vdev_root_ops
&& spa
->spa_root_vdev
== NULL
)
543 return (SET_ERROR(EINVAL
));
546 * Determine whether we're a log vdev.
549 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &islog
);
550 if (islog
&& spa_version(spa
) < SPA_VERSION_SLOGS
)
551 return (SET_ERROR(ENOTSUP
));
553 if (ops
== &vdev_hole_ops
&& spa_version(spa
) < SPA_VERSION_HOLES
)
554 return (SET_ERROR(ENOTSUP
));
557 * Set the nparity property for RAID-Z vdevs.
560 if (ops
== &vdev_raidz_ops
) {
561 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
563 if (nparity
== 0 || nparity
> VDEV_RAIDZ_MAXPARITY
)
564 return (SET_ERROR(EINVAL
));
566 * Previous versions could only support 1 or 2 parity
570 spa_version(spa
) < SPA_VERSION_RAIDZ2
)
571 return (SET_ERROR(ENOTSUP
));
573 spa_version(spa
) < SPA_VERSION_RAIDZ3
)
574 return (SET_ERROR(ENOTSUP
));
577 * We require the parity to be specified for SPAs that
578 * support multiple parity levels.
580 if (spa_version(spa
) >= SPA_VERSION_RAIDZ2
)
581 return (SET_ERROR(EINVAL
));
583 * Otherwise, we default to 1 parity device for RAID-Z.
590 ASSERT(nparity
!= -1ULL);
592 vd
= vdev_alloc_common(spa
, id
, guid
, ops
);
593 vic
= &vd
->vdev_indirect_config
;
595 vd
->vdev_islog
= islog
;
596 vd
->vdev_nparity
= nparity
;
598 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &vd
->vdev_path
) == 0)
599 vd
->vdev_path
= spa_strdup(vd
->vdev_path
);
600 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &vd
->vdev_devid
) == 0)
601 vd
->vdev_devid
= spa_strdup(vd
->vdev_devid
);
602 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PHYS_PATH
,
603 &vd
->vdev_physpath
) == 0)
604 vd
->vdev_physpath
= spa_strdup(vd
->vdev_physpath
);
605 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_FRU
, &vd
->vdev_fru
) == 0)
606 vd
->vdev_fru
= spa_strdup(vd
->vdev_fru
);
609 * Set the whole_disk property. If it's not specified, leave the value
612 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
613 &vd
->vdev_wholedisk
) != 0)
614 vd
->vdev_wholedisk
= -1ULL;
616 ASSERT0(vic
->vic_mapping_object
);
617 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_OBJECT
,
618 &vic
->vic_mapping_object
);
619 ASSERT0(vic
->vic_births_object
);
620 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_BIRTHS
,
621 &vic
->vic_births_object
);
622 ASSERT3U(vic
->vic_prev_indirect_vdev
, ==, UINT64_MAX
);
623 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_PREV_INDIRECT_VDEV
,
624 &vic
->vic_prev_indirect_vdev
);
627 * Look for the 'not present' flag. This will only be set if the device
628 * was not present at the time of import.
630 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
631 &vd
->vdev_not_present
);
634 * Get the alignment requirement.
636 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASHIFT
, &vd
->vdev_ashift
);
639 * Retrieve the vdev creation time.
641 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_CREATE_TXG
,
645 * If we're a top-level vdev, try to load the allocation parameters.
647 if (parent
&& !parent
->vdev_parent
&&
648 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
649 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_ARRAY
,
651 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_SHIFT
,
653 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASIZE
,
655 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVING
,
657 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_VDEV_TOP_ZAP
,
660 ASSERT0(vd
->vdev_top_zap
);
663 if (parent
&& !parent
->vdev_parent
&& alloctype
!= VDEV_ALLOC_ATTACH
) {
664 ASSERT(alloctype
== VDEV_ALLOC_LOAD
||
665 alloctype
== VDEV_ALLOC_ADD
||
666 alloctype
== VDEV_ALLOC_SPLIT
||
667 alloctype
== VDEV_ALLOC_ROOTPOOL
);
668 vd
->vdev_mg
= metaslab_group_create(islog
?
669 spa_log_class(spa
) : spa_normal_class(spa
), vd
,
670 spa
->spa_alloc_count
);
673 if (vd
->vdev_ops
->vdev_op_leaf
&&
674 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
675 (void) nvlist_lookup_uint64(nv
,
676 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, &vd
->vdev_leaf_zap
);
678 ASSERT0(vd
->vdev_leaf_zap
);
682 * If we're a leaf vdev, try to load the DTL object and other state.
685 if (vd
->vdev_ops
->vdev_op_leaf
&&
686 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_L2CACHE
||
687 alloctype
== VDEV_ALLOC_ROOTPOOL
)) {
688 if (alloctype
== VDEV_ALLOC_LOAD
) {
689 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DTL
,
690 &vd
->vdev_dtl_object
);
691 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_UNSPARE
,
695 if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
698 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
699 &spare
) == 0 && spare
)
703 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
,
706 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_RESILVER_TXG
,
707 &vd
->vdev_resilver_txg
);
710 * When importing a pool, we want to ignore the persistent fault
711 * state, as the diagnosis made on another system may not be
712 * valid in the current context. Local vdevs will
713 * remain in the faulted state.
715 if (spa_load_state(spa
) == SPA_LOAD_OPEN
) {
716 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
,
718 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DEGRADED
,
720 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
,
723 if (vd
->vdev_faulted
|| vd
->vdev_degraded
) {
727 VDEV_AUX_ERR_EXCEEDED
;
728 if (nvlist_lookup_string(nv
,
729 ZPOOL_CONFIG_AUX_STATE
, &aux
) == 0 &&
730 strcmp(aux
, "external") == 0)
731 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
737 * Add ourselves to the parent's list of children.
739 vdev_add_child(parent
, vd
);
747 vdev_free(vdev_t
*vd
)
749 spa_t
*spa
= vd
->vdev_spa
;
750 ASSERT3P(vd
->vdev_initialize_thread
, ==, NULL
);
753 * vdev_free() implies closing the vdev first. This is simpler than
754 * trying to ensure complicated semantics for all callers.
758 ASSERT(!list_link_active(&vd
->vdev_config_dirty_node
));
759 ASSERT(!list_link_active(&vd
->vdev_state_dirty_node
));
764 for (int c
= 0; c
< vd
->vdev_children
; c
++)
765 vdev_free(vd
->vdev_child
[c
]);
767 ASSERT(vd
->vdev_child
== NULL
);
768 ASSERT(vd
->vdev_guid_sum
== vd
->vdev_guid
);
769 ASSERT(vd
->vdev_initialize_thread
== NULL
);
772 * Discard allocation state.
774 if (vd
->vdev_mg
!= NULL
) {
775 vdev_metaslab_fini(vd
);
776 metaslab_group_destroy(vd
->vdev_mg
);
779 ASSERT0(vd
->vdev_stat
.vs_space
);
780 ASSERT0(vd
->vdev_stat
.vs_dspace
);
781 ASSERT0(vd
->vdev_stat
.vs_alloc
);
784 * Remove this vdev from its parent's child list.
786 vdev_remove_child(vd
->vdev_parent
, vd
);
788 ASSERT(vd
->vdev_parent
== NULL
);
791 * Clean up vdev structure.
797 spa_strfree(vd
->vdev_path
);
799 spa_strfree(vd
->vdev_devid
);
800 if (vd
->vdev_physpath
)
801 spa_strfree(vd
->vdev_physpath
);
803 spa_strfree(vd
->vdev_fru
);
805 if (vd
->vdev_isspare
)
806 spa_spare_remove(vd
);
807 if (vd
->vdev_isl2cache
)
808 spa_l2cache_remove(vd
);
810 txg_list_destroy(&vd
->vdev_ms_list
);
811 txg_list_destroy(&vd
->vdev_dtl_list
);
813 mutex_enter(&vd
->vdev_dtl_lock
);
814 space_map_close(vd
->vdev_dtl_sm
);
815 for (int t
= 0; t
< DTL_TYPES
; t
++) {
816 range_tree_vacate(vd
->vdev_dtl
[t
], NULL
, NULL
);
817 range_tree_destroy(vd
->vdev_dtl
[t
]);
819 mutex_exit(&vd
->vdev_dtl_lock
);
821 EQUIV(vd
->vdev_indirect_births
!= NULL
,
822 vd
->vdev_indirect_mapping
!= NULL
);
823 if (vd
->vdev_indirect_births
!= NULL
) {
824 vdev_indirect_mapping_close(vd
->vdev_indirect_mapping
);
825 vdev_indirect_births_close(vd
->vdev_indirect_births
);
828 if (vd
->vdev_obsolete_sm
!= NULL
) {
829 ASSERT(vd
->vdev_removing
||
830 vd
->vdev_ops
== &vdev_indirect_ops
);
831 space_map_close(vd
->vdev_obsolete_sm
);
832 vd
->vdev_obsolete_sm
= NULL
;
834 range_tree_destroy(vd
->vdev_obsolete_segments
);
835 rw_destroy(&vd
->vdev_indirect_rwlock
);
836 mutex_destroy(&vd
->vdev_obsolete_lock
);
838 mutex_destroy(&vd
->vdev_queue_lock
);
839 mutex_destroy(&vd
->vdev_dtl_lock
);
840 mutex_destroy(&vd
->vdev_stat_lock
);
841 mutex_destroy(&vd
->vdev_probe_lock
);
842 mutex_destroy(&vd
->vdev_initialize_lock
);
843 mutex_destroy(&vd
->vdev_initialize_io_lock
);
844 cv_destroy(&vd
->vdev_initialize_io_cv
);
845 cv_destroy(&vd
->vdev_initialize_cv
);
847 if (vd
== spa
->spa_root_vdev
)
848 spa
->spa_root_vdev
= NULL
;
850 kmem_free(vd
, sizeof (vdev_t
));
854 * Transfer top-level vdev state from svd to tvd.
857 vdev_top_transfer(vdev_t
*svd
, vdev_t
*tvd
)
859 spa_t
*spa
= svd
->vdev_spa
;
864 ASSERT(tvd
== tvd
->vdev_top
);
866 tvd
->vdev_ms_array
= svd
->vdev_ms_array
;
867 tvd
->vdev_ms_shift
= svd
->vdev_ms_shift
;
868 tvd
->vdev_ms_count
= svd
->vdev_ms_count
;
869 tvd
->vdev_top_zap
= svd
->vdev_top_zap
;
871 svd
->vdev_ms_array
= 0;
872 svd
->vdev_ms_shift
= 0;
873 svd
->vdev_ms_count
= 0;
874 svd
->vdev_top_zap
= 0;
877 ASSERT3P(tvd
->vdev_mg
, ==, svd
->vdev_mg
);
878 tvd
->vdev_mg
= svd
->vdev_mg
;
879 tvd
->vdev_ms
= svd
->vdev_ms
;
884 if (tvd
->vdev_mg
!= NULL
)
885 tvd
->vdev_mg
->mg_vd
= tvd
;
887 tvd
->vdev_checkpoint_sm
= svd
->vdev_checkpoint_sm
;
888 svd
->vdev_checkpoint_sm
= NULL
;
890 tvd
->vdev_stat
.vs_alloc
= svd
->vdev_stat
.vs_alloc
;
891 tvd
->vdev_stat
.vs_space
= svd
->vdev_stat
.vs_space
;
892 tvd
->vdev_stat
.vs_dspace
= svd
->vdev_stat
.vs_dspace
;
894 svd
->vdev_stat
.vs_alloc
= 0;
895 svd
->vdev_stat
.vs_space
= 0;
896 svd
->vdev_stat
.vs_dspace
= 0;
899 * State which may be set on a top-level vdev that's in the
900 * process of being removed.
902 ASSERT0(tvd
->vdev_indirect_config
.vic_births_object
);
903 ASSERT0(tvd
->vdev_indirect_config
.vic_mapping_object
);
904 ASSERT3U(tvd
->vdev_indirect_config
.vic_prev_indirect_vdev
, ==, -1ULL);
905 ASSERT3P(tvd
->vdev_indirect_mapping
, ==, NULL
);
906 ASSERT3P(tvd
->vdev_indirect_births
, ==, NULL
);
907 ASSERT3P(tvd
->vdev_obsolete_sm
, ==, NULL
);
908 ASSERT0(tvd
->vdev_removing
);
909 tvd
->vdev_removing
= svd
->vdev_removing
;
910 tvd
->vdev_indirect_config
= svd
->vdev_indirect_config
;
911 tvd
->vdev_indirect_mapping
= svd
->vdev_indirect_mapping
;
912 tvd
->vdev_indirect_births
= svd
->vdev_indirect_births
;
913 range_tree_swap(&svd
->vdev_obsolete_segments
,
914 &tvd
->vdev_obsolete_segments
);
915 tvd
->vdev_obsolete_sm
= svd
->vdev_obsolete_sm
;
916 svd
->vdev_indirect_config
.vic_mapping_object
= 0;
917 svd
->vdev_indirect_config
.vic_births_object
= 0;
918 svd
->vdev_indirect_config
.vic_prev_indirect_vdev
= -1ULL;
919 svd
->vdev_indirect_mapping
= NULL
;
920 svd
->vdev_indirect_births
= NULL
;
921 svd
->vdev_obsolete_sm
= NULL
;
922 svd
->vdev_removing
= 0;
924 for (t
= 0; t
< TXG_SIZE
; t
++) {
925 while ((msp
= txg_list_remove(&svd
->vdev_ms_list
, t
)) != NULL
)
926 (void) txg_list_add(&tvd
->vdev_ms_list
, msp
, t
);
927 while ((vd
= txg_list_remove(&svd
->vdev_dtl_list
, t
)) != NULL
)
928 (void) txg_list_add(&tvd
->vdev_dtl_list
, vd
, t
);
929 if (txg_list_remove_this(&spa
->spa_vdev_txg_list
, svd
, t
))
930 (void) txg_list_add(&spa
->spa_vdev_txg_list
, tvd
, t
);
933 if (list_link_active(&svd
->vdev_config_dirty_node
)) {
934 vdev_config_clean(svd
);
935 vdev_config_dirty(tvd
);
938 if (list_link_active(&svd
->vdev_state_dirty_node
)) {
939 vdev_state_clean(svd
);
940 vdev_state_dirty(tvd
);
943 tvd
->vdev_deflate_ratio
= svd
->vdev_deflate_ratio
;
944 svd
->vdev_deflate_ratio
= 0;
946 tvd
->vdev_islog
= svd
->vdev_islog
;
951 vdev_top_update(vdev_t
*tvd
, vdev_t
*vd
)
958 for (int c
= 0; c
< vd
->vdev_children
; c
++)
959 vdev_top_update(tvd
, vd
->vdev_child
[c
]);
963 * Add a mirror/replacing vdev above an existing vdev.
966 vdev_add_parent(vdev_t
*cvd
, vdev_ops_t
*ops
)
968 spa_t
*spa
= cvd
->vdev_spa
;
969 vdev_t
*pvd
= cvd
->vdev_parent
;
972 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
974 mvd
= vdev_alloc_common(spa
, cvd
->vdev_id
, 0, ops
);
976 mvd
->vdev_asize
= cvd
->vdev_asize
;
977 mvd
->vdev_min_asize
= cvd
->vdev_min_asize
;
978 mvd
->vdev_max_asize
= cvd
->vdev_max_asize
;
979 mvd
->vdev_psize
= cvd
->vdev_psize
;
980 mvd
->vdev_ashift
= cvd
->vdev_ashift
;
981 mvd
->vdev_state
= cvd
->vdev_state
;
982 mvd
->vdev_crtxg
= cvd
->vdev_crtxg
;
984 vdev_remove_child(pvd
, cvd
);
985 vdev_add_child(pvd
, mvd
);
986 cvd
->vdev_id
= mvd
->vdev_children
;
987 vdev_add_child(mvd
, cvd
);
988 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
990 if (mvd
== mvd
->vdev_top
)
991 vdev_top_transfer(cvd
, mvd
);
997 * Remove a 1-way mirror/replacing vdev from the tree.
1000 vdev_remove_parent(vdev_t
*cvd
)
1002 vdev_t
*mvd
= cvd
->vdev_parent
;
1003 vdev_t
*pvd
= mvd
->vdev_parent
;
1005 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1007 ASSERT(mvd
->vdev_children
== 1);
1008 ASSERT(mvd
->vdev_ops
== &vdev_mirror_ops
||
1009 mvd
->vdev_ops
== &vdev_replacing_ops
||
1010 mvd
->vdev_ops
== &vdev_spare_ops
);
1011 cvd
->vdev_ashift
= mvd
->vdev_ashift
;
1013 vdev_remove_child(mvd
, cvd
);
1014 vdev_remove_child(pvd
, mvd
);
1017 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1018 * Otherwise, we could have detached an offline device, and when we
1019 * go to import the pool we'll think we have two top-level vdevs,
1020 * instead of a different version of the same top-level vdev.
1022 if (mvd
->vdev_top
== mvd
) {
1023 uint64_t guid_delta
= mvd
->vdev_guid
- cvd
->vdev_guid
;
1024 cvd
->vdev_orig_guid
= cvd
->vdev_guid
;
1025 cvd
->vdev_guid
+= guid_delta
;
1026 cvd
->vdev_guid_sum
+= guid_delta
;
1028 cvd
->vdev_id
= mvd
->vdev_id
;
1029 vdev_add_child(pvd
, cvd
);
1030 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1032 if (cvd
== cvd
->vdev_top
)
1033 vdev_top_transfer(mvd
, cvd
);
1035 ASSERT(mvd
->vdev_children
== 0);
1040 vdev_metaslab_init(vdev_t
*vd
, uint64_t txg
)
1042 spa_t
*spa
= vd
->vdev_spa
;
1043 objset_t
*mos
= spa
->spa_meta_objset
;
1045 uint64_t oldc
= vd
->vdev_ms_count
;
1046 uint64_t newc
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
1050 ASSERT(txg
== 0 || spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1053 * This vdev is not being allocated from yet or is a hole.
1055 if (vd
->vdev_ms_shift
== 0)
1058 ASSERT(!vd
->vdev_ishole
);
1060 ASSERT(oldc
<= newc
);
1062 mspp
= kmem_zalloc(newc
* sizeof (*mspp
), KM_SLEEP
);
1065 bcopy(vd
->vdev_ms
, mspp
, oldc
* sizeof (*mspp
));
1066 kmem_free(vd
->vdev_ms
, oldc
* sizeof (*mspp
));
1070 vd
->vdev_ms_count
= newc
;
1071 for (m
= oldc
; m
< newc
; m
++) {
1072 uint64_t object
= 0;
1075 * vdev_ms_array may be 0 if we are creating the "fake"
1076 * metaslabs for an indirect vdev for zdb's leak detection.
1077 * See zdb_leak_init().
1079 if (txg
== 0 && vd
->vdev_ms_array
!= 0) {
1080 error
= dmu_read(mos
, vd
->vdev_ms_array
,
1081 m
* sizeof (uint64_t), sizeof (uint64_t), &object
,
1084 vdev_dbgmsg(vd
, "unable to read the metaslab "
1085 "array [error=%d]", error
);
1090 error
= metaslab_init(vd
->vdev_mg
, m
, object
, txg
,
1093 vdev_dbgmsg(vd
, "metaslab_init failed [error=%d]",
1100 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_WRITER
);
1103 * If the vdev is being removed we don't activate
1104 * the metaslabs since we want to ensure that no new
1105 * allocations are performed on this device.
1107 if (oldc
== 0 && !vd
->vdev_removing
)
1108 metaslab_group_activate(vd
->vdev_mg
);
1111 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
1117 vdev_metaslab_fini(vdev_t
*vd
)
1119 if (vd
->vdev_checkpoint_sm
!= NULL
) {
1120 ASSERT(spa_feature_is_active(vd
->vdev_spa
,
1121 SPA_FEATURE_POOL_CHECKPOINT
));
1122 space_map_close(vd
->vdev_checkpoint_sm
);
1124 * Even though we close the space map, we need to set its
1125 * pointer to NULL. The reason is that vdev_metaslab_fini()
1126 * may be called multiple times for certain operations
1127 * (i.e. when destroying a pool) so we need to ensure that
1128 * this clause never executes twice. This logic is similar
1129 * to the one used for the vdev_ms clause below.
1131 vd
->vdev_checkpoint_sm
= NULL
;
1134 if (vd
->vdev_ms
!= NULL
) {
1135 uint64_t count
= vd
->vdev_ms_count
;
1137 metaslab_group_passivate(vd
->vdev_mg
);
1138 for (uint64_t m
= 0; m
< count
; m
++) {
1139 metaslab_t
*msp
= vd
->vdev_ms
[m
];
1144 kmem_free(vd
->vdev_ms
, count
* sizeof (metaslab_t
*));
1147 vd
->vdev_ms_count
= 0;
1149 ASSERT0(vd
->vdev_ms_count
);
1152 typedef struct vdev_probe_stats
{
1153 boolean_t vps_readable
;
1154 boolean_t vps_writeable
;
1156 } vdev_probe_stats_t
;
1159 vdev_probe_done(zio_t
*zio
)
1161 spa_t
*spa
= zio
->io_spa
;
1162 vdev_t
*vd
= zio
->io_vd
;
1163 vdev_probe_stats_t
*vps
= zio
->io_private
;
1165 ASSERT(vd
->vdev_probe_zio
!= NULL
);
1167 if (zio
->io_type
== ZIO_TYPE_READ
) {
1168 if (zio
->io_error
== 0)
1169 vps
->vps_readable
= 1;
1170 if (zio
->io_error
== 0 && spa_writeable(spa
)) {
1171 zio_nowait(zio_write_phys(vd
->vdev_probe_zio
, vd
,
1172 zio
->io_offset
, zio
->io_size
, zio
->io_abd
,
1173 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1174 ZIO_PRIORITY_SYNC_WRITE
, vps
->vps_flags
, B_TRUE
));
1176 abd_free(zio
->io_abd
);
1178 } else if (zio
->io_type
== ZIO_TYPE_WRITE
) {
1179 if (zio
->io_error
== 0)
1180 vps
->vps_writeable
= 1;
1181 abd_free(zio
->io_abd
);
1182 } else if (zio
->io_type
== ZIO_TYPE_NULL
) {
1185 vd
->vdev_cant_read
|= !vps
->vps_readable
;
1186 vd
->vdev_cant_write
|= !vps
->vps_writeable
;
1188 if (vdev_readable(vd
) &&
1189 (vdev_writeable(vd
) || !spa_writeable(spa
))) {
1192 ASSERT(zio
->io_error
!= 0);
1193 vdev_dbgmsg(vd
, "failed probe");
1194 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE
,
1195 spa
, vd
, NULL
, 0, 0);
1196 zio
->io_error
= SET_ERROR(ENXIO
);
1199 mutex_enter(&vd
->vdev_probe_lock
);
1200 ASSERT(vd
->vdev_probe_zio
== zio
);
1201 vd
->vdev_probe_zio
= NULL
;
1202 mutex_exit(&vd
->vdev_probe_lock
);
1204 zio_link_t
*zl
= NULL
;
1205 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
)
1206 if (!vdev_accessible(vd
, pio
))
1207 pio
->io_error
= SET_ERROR(ENXIO
);
1209 kmem_free(vps
, sizeof (*vps
));
1214 * Determine whether this device is accessible.
1216 * Read and write to several known locations: the pad regions of each
1217 * vdev label but the first, which we leave alone in case it contains
1221 vdev_probe(vdev_t
*vd
, zio_t
*zio
)
1223 spa_t
*spa
= vd
->vdev_spa
;
1224 vdev_probe_stats_t
*vps
= NULL
;
1227 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1230 * Don't probe the probe.
1232 if (zio
&& (zio
->io_flags
& ZIO_FLAG_PROBE
))
1236 * To prevent 'probe storms' when a device fails, we create
1237 * just one probe i/o at a time. All zios that want to probe
1238 * this vdev will become parents of the probe io.
1240 mutex_enter(&vd
->vdev_probe_lock
);
1242 if ((pio
= vd
->vdev_probe_zio
) == NULL
) {
1243 vps
= kmem_zalloc(sizeof (*vps
), KM_SLEEP
);
1245 vps
->vps_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_PROBE
|
1246 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_AGGREGATE
|
1249 if (spa_config_held(spa
, SCL_ZIO
, RW_WRITER
)) {
1251 * vdev_cant_read and vdev_cant_write can only
1252 * transition from TRUE to FALSE when we have the
1253 * SCL_ZIO lock as writer; otherwise they can only
1254 * transition from FALSE to TRUE. This ensures that
1255 * any zio looking at these values can assume that
1256 * failures persist for the life of the I/O. That's
1257 * important because when a device has intermittent
1258 * connectivity problems, we want to ensure that
1259 * they're ascribed to the device (ENXIO) and not
1262 * Since we hold SCL_ZIO as writer here, clear both
1263 * values so the probe can reevaluate from first
1266 vps
->vps_flags
|= ZIO_FLAG_CONFIG_WRITER
;
1267 vd
->vdev_cant_read
= B_FALSE
;
1268 vd
->vdev_cant_write
= B_FALSE
;
1271 vd
->vdev_probe_zio
= pio
= zio_null(NULL
, spa
, vd
,
1272 vdev_probe_done
, vps
,
1273 vps
->vps_flags
| ZIO_FLAG_DONT_PROPAGATE
);
1276 * We can't change the vdev state in this context, so we
1277 * kick off an async task to do it on our behalf.
1280 vd
->vdev_probe_wanted
= B_TRUE
;
1281 spa_async_request(spa
, SPA_ASYNC_PROBE
);
1286 zio_add_child(zio
, pio
);
1288 mutex_exit(&vd
->vdev_probe_lock
);
1291 ASSERT(zio
!= NULL
);
1295 for (int l
= 1; l
< VDEV_LABELS
; l
++) {
1296 zio_nowait(zio_read_phys(pio
, vd
,
1297 vdev_label_offset(vd
->vdev_psize
, l
,
1298 offsetof(vdev_label_t
, vl_pad2
)), VDEV_PAD_SIZE
,
1299 abd_alloc_for_io(VDEV_PAD_SIZE
, B_TRUE
),
1300 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1301 ZIO_PRIORITY_SYNC_READ
, vps
->vps_flags
, B_TRUE
));
1312 vdev_open_child(void *arg
)
1316 vd
->vdev_open_thread
= curthread
;
1317 vd
->vdev_open_error
= vdev_open(vd
);
1318 vd
->vdev_open_thread
= NULL
;
1322 vdev_uses_zvols(vdev_t
*vd
)
1324 if (vd
->vdev_path
&& strncmp(vd
->vdev_path
, ZVOL_DIR
,
1325 strlen(ZVOL_DIR
)) == 0)
1327 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1328 if (vdev_uses_zvols(vd
->vdev_child
[c
]))
1334 vdev_open_children(vdev_t
*vd
)
1337 int children
= vd
->vdev_children
;
1340 * in order to handle pools on top of zvols, do the opens
1341 * in a single thread so that the same thread holds the
1342 * spa_namespace_lock
1344 if (vdev_uses_zvols(vd
)) {
1345 for (int c
= 0; c
< children
; c
++)
1346 vd
->vdev_child
[c
]->vdev_open_error
=
1347 vdev_open(vd
->vdev_child
[c
]);
1350 tq
= taskq_create("vdev_open", children
, minclsyspri
,
1351 children
, children
, TASKQ_PREPOPULATE
);
1353 for (int c
= 0; c
< children
; c
++)
1354 VERIFY(taskq_dispatch(tq
, vdev_open_child
, vd
->vdev_child
[c
],
1361 * Compute the raidz-deflation ratio. Note, we hard-code
1362 * in 128k (1 << 17) because it is the "typical" blocksize.
1363 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1364 * otherwise it would inconsistently account for existing bp's.
1367 vdev_set_deflate_ratio(vdev_t
*vd
)
1369 if (vd
== vd
->vdev_top
&& !vd
->vdev_ishole
&& vd
->vdev_ashift
!= 0) {
1370 vd
->vdev_deflate_ratio
= (1 << 17) /
1371 (vdev_psize_to_asize(vd
, 1 << 17) >> SPA_MINBLOCKSHIFT
);
1376 * Prepare a virtual device for access.
1379 vdev_open(vdev_t
*vd
)
1381 spa_t
*spa
= vd
->vdev_spa
;
1384 uint64_t max_osize
= 0;
1385 uint64_t asize
, max_asize
, psize
;
1386 uint64_t ashift
= 0;
1388 ASSERT(vd
->vdev_open_thread
== curthread
||
1389 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1390 ASSERT(vd
->vdev_state
== VDEV_STATE_CLOSED
||
1391 vd
->vdev_state
== VDEV_STATE_CANT_OPEN
||
1392 vd
->vdev_state
== VDEV_STATE_OFFLINE
);
1394 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1395 vd
->vdev_cant_read
= B_FALSE
;
1396 vd
->vdev_cant_write
= B_FALSE
;
1397 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
1400 * If this vdev is not removed, check its fault status. If it's
1401 * faulted, bail out of the open.
1403 if (!vd
->vdev_removed
&& vd
->vdev_faulted
) {
1404 ASSERT(vd
->vdev_children
== 0);
1405 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1406 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1407 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1408 vd
->vdev_label_aux
);
1409 return (SET_ERROR(ENXIO
));
1410 } else if (vd
->vdev_offline
) {
1411 ASSERT(vd
->vdev_children
== 0);
1412 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
, VDEV_AUX_NONE
);
1413 return (SET_ERROR(ENXIO
));
1416 error
= vd
->vdev_ops
->vdev_op_open(vd
, &osize
, &max_osize
, &ashift
);
1419 * Reset the vdev_reopening flag so that we actually close
1420 * the vdev on error.
1422 vd
->vdev_reopening
= B_FALSE
;
1423 if (zio_injection_enabled
&& error
== 0)
1424 error
= zio_handle_device_injection(vd
, NULL
, ENXIO
);
1427 if (vd
->vdev_removed
&&
1428 vd
->vdev_stat
.vs_aux
!= VDEV_AUX_OPEN_FAILED
)
1429 vd
->vdev_removed
= B_FALSE
;
1431 if (vd
->vdev_stat
.vs_aux
== VDEV_AUX_CHILDREN_OFFLINE
) {
1432 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
,
1433 vd
->vdev_stat
.vs_aux
);
1435 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1436 vd
->vdev_stat
.vs_aux
);
1441 vd
->vdev_removed
= B_FALSE
;
1444 * Recheck the faulted flag now that we have confirmed that
1445 * the vdev is accessible. If we're faulted, bail.
1447 if (vd
->vdev_faulted
) {
1448 ASSERT(vd
->vdev_children
== 0);
1449 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1450 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1451 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1452 vd
->vdev_label_aux
);
1453 return (SET_ERROR(ENXIO
));
1456 if (vd
->vdev_degraded
) {
1457 ASSERT(vd
->vdev_children
== 0);
1458 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1459 VDEV_AUX_ERR_EXCEEDED
);
1461 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_HEALTHY
, 0);
1465 * For hole or missing vdevs we just return success.
1467 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
)
1470 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
1471 if (vd
->vdev_child
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
) {
1472 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1478 osize
= P2ALIGN(osize
, (uint64_t)sizeof (vdev_label_t
));
1479 max_osize
= P2ALIGN(max_osize
, (uint64_t)sizeof (vdev_label_t
));
1481 if (vd
->vdev_children
== 0) {
1482 if (osize
< SPA_MINDEVSIZE
) {
1483 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1484 VDEV_AUX_TOO_SMALL
);
1485 return (SET_ERROR(EOVERFLOW
));
1488 asize
= osize
- (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
);
1489 max_asize
= max_osize
- (VDEV_LABEL_START_SIZE
+
1490 VDEV_LABEL_END_SIZE
);
1492 if (vd
->vdev_parent
!= NULL
&& osize
< SPA_MINDEVSIZE
-
1493 (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
)) {
1494 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1495 VDEV_AUX_TOO_SMALL
);
1496 return (SET_ERROR(EOVERFLOW
));
1500 max_asize
= max_osize
;
1503 vd
->vdev_psize
= psize
;
1506 * Make sure the allocatable size hasn't shrunk too much.
1508 if (asize
< vd
->vdev_min_asize
) {
1509 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1510 VDEV_AUX_BAD_LABEL
);
1511 return (SET_ERROR(EINVAL
));
1514 if (vd
->vdev_asize
== 0) {
1516 * This is the first-ever open, so use the computed values.
1517 * For testing purposes, a higher ashift can be requested.
1519 vd
->vdev_asize
= asize
;
1520 vd
->vdev_max_asize
= max_asize
;
1521 vd
->vdev_ashift
= MAX(ashift
, vd
->vdev_ashift
);
1522 vd
->vdev_ashift
= MAX(zfs_ashift_min
, vd
->vdev_ashift
);
1525 * Detect if the alignment requirement has increased.
1526 * We don't want to make the pool unavailable, just
1527 * issue a warning instead.
1529 if (ashift
> vd
->vdev_top
->vdev_ashift
&&
1530 vd
->vdev_ops
->vdev_op_leaf
) {
1532 "Disk, '%s', has a block alignment that is "
1533 "larger than the pool's alignment\n",
1536 vd
->vdev_max_asize
= max_asize
;
1540 * If all children are healthy we update asize if either:
1541 * The asize has increased, due to a device expansion caused by dynamic
1542 * LUN growth or vdev replacement, and automatic expansion is enabled;
1543 * making the additional space available.
1545 * The asize has decreased, due to a device shrink usually caused by a
1546 * vdev replace with a smaller device. This ensures that calculations
1547 * based of max_asize and asize e.g. esize are always valid. It's safe
1548 * to do this as we've already validated that asize is greater than
1551 if (vd
->vdev_state
== VDEV_STATE_HEALTHY
&&
1552 ((asize
> vd
->vdev_asize
&&
1553 (vd
->vdev_expanding
|| spa
->spa_autoexpand
)) ||
1554 (asize
< vd
->vdev_asize
)))
1555 vd
->vdev_asize
= asize
;
1557 vdev_set_min_asize(vd
);
1560 * Ensure we can issue some IO before declaring the
1561 * vdev open for business.
1563 if (vd
->vdev_ops
->vdev_op_leaf
&&
1564 (error
= zio_wait(vdev_probe(vd
, NULL
))) != 0) {
1565 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1566 VDEV_AUX_ERR_EXCEEDED
);
1571 * Track the min and max ashift values for normal data devices.
1573 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
1574 !vd
->vdev_islog
&& vd
->vdev_aux
== NULL
) {
1575 if (vd
->vdev_ashift
> spa
->spa_max_ashift
)
1576 spa
->spa_max_ashift
= vd
->vdev_ashift
;
1577 if (vd
->vdev_ashift
< spa
->spa_min_ashift
)
1578 spa
->spa_min_ashift
= vd
->vdev_ashift
;
1582 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1583 * resilver. But don't do this if we are doing a reopen for a scrub,
1584 * since this would just restart the scrub we are already doing.
1586 if (vd
->vdev_ops
->vdev_op_leaf
&& !spa
->spa_scrub_reopen
&&
1587 vdev_resilver_needed(vd
, NULL
, NULL
))
1588 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
1594 * Called once the vdevs are all opened, this routine validates the label
1595 * contents. This needs to be done before vdev_load() so that we don't
1596 * inadvertently do repair I/Os to the wrong device.
1598 * This function will only return failure if one of the vdevs indicates that it
1599 * has since been destroyed or exported. This is only possible if
1600 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1601 * will be updated but the function will return 0.
1604 vdev_validate(vdev_t
*vd
)
1606 spa_t
*spa
= vd
->vdev_spa
;
1608 uint64_t guid
= 0, aux_guid
= 0, top_guid
;
1613 if (vdev_validate_skip
)
1616 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
1617 if (vdev_validate(vd
->vdev_child
[c
]) != 0)
1618 return (SET_ERROR(EBADF
));
1621 * If the device has already failed, or was marked offline, don't do
1622 * any further validation. Otherwise, label I/O will fail and we will
1623 * overwrite the previous state.
1625 if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_readable(vd
))
1629 * If we are performing an extreme rewind, we allow for a label that
1630 * was modified at a point after the current txg.
1631 * If config lock is not held do not check for the txg. spa_sync could
1632 * be updating the vdev's label before updating spa_last_synced_txg.
1634 if (spa
->spa_extreme_rewind
|| spa_last_synced_txg(spa
) == 0 ||
1635 spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) != SCL_CONFIG
)
1638 txg
= spa_last_synced_txg(spa
);
1640 if ((label
= vdev_label_read_config(vd
, txg
)) == NULL
) {
1641 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1642 VDEV_AUX_BAD_LABEL
);
1643 vdev_dbgmsg(vd
, "vdev_validate: failed reading config for "
1644 "txg %llu", (u_longlong_t
)txg
);
1649 * Determine if this vdev has been split off into another
1650 * pool. If so, then refuse to open it.
1652 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_SPLIT_GUID
,
1653 &aux_guid
) == 0 && aux_guid
== spa_guid(spa
)) {
1654 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1655 VDEV_AUX_SPLIT_POOL
);
1657 vdev_dbgmsg(vd
, "vdev_validate: vdev split into other pool");
1661 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0) {
1662 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1663 VDEV_AUX_CORRUPT_DATA
);
1665 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1666 ZPOOL_CONFIG_POOL_GUID
);
1671 * If config is not trusted then ignore the spa guid check. This is
1672 * necessary because if the machine crashed during a re-guid the new
1673 * guid might have been written to all of the vdev labels, but not the
1674 * cached config. The check will be performed again once we have the
1675 * trusted config from the MOS.
1677 if (spa
->spa_trust_config
&& guid
!= spa_guid(spa
)) {
1678 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1679 VDEV_AUX_CORRUPT_DATA
);
1681 vdev_dbgmsg(vd
, "vdev_validate: vdev label pool_guid doesn't "
1682 "match config (%llu != %llu)", (u_longlong_t
)guid
,
1683 (u_longlong_t
)spa_guid(spa
));
1687 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
)
1688 != 0 || nvlist_lookup_uint64(nvl
, ZPOOL_CONFIG_ORIG_GUID
,
1692 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0) {
1693 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1694 VDEV_AUX_CORRUPT_DATA
);
1696 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1701 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_TOP_GUID
, &top_guid
)
1703 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1704 VDEV_AUX_CORRUPT_DATA
);
1706 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1707 ZPOOL_CONFIG_TOP_GUID
);
1712 * If this vdev just became a top-level vdev because its sibling was
1713 * detached, it will have adopted the parent's vdev guid -- but the
1714 * label may or may not be on disk yet. Fortunately, either version
1715 * of the label will have the same top guid, so if we're a top-level
1716 * vdev, we can safely compare to that instead.
1717 * However, if the config comes from a cachefile that failed to update
1718 * after the detach, a top-level vdev will appear as a non top-level
1719 * vdev in the config. Also relax the constraints if we perform an
1722 * If we split this vdev off instead, then we also check the
1723 * original pool's guid. We don't want to consider the vdev
1724 * corrupt if it is partway through a split operation.
1726 if (vd
->vdev_guid
!= guid
&& vd
->vdev_guid
!= aux_guid
) {
1727 boolean_t mismatch
= B_FALSE
;
1728 if (spa
->spa_trust_config
&& !spa
->spa_extreme_rewind
) {
1729 if (vd
!= vd
->vdev_top
|| vd
->vdev_guid
!= top_guid
)
1732 if (vd
->vdev_guid
!= top_guid
&&
1733 vd
->vdev_top
->vdev_guid
!= guid
)
1738 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1739 VDEV_AUX_CORRUPT_DATA
);
1741 vdev_dbgmsg(vd
, "vdev_validate: config guid "
1742 "doesn't match label guid");
1743 vdev_dbgmsg(vd
, "CONFIG: guid %llu, top_guid %llu",
1744 (u_longlong_t
)vd
->vdev_guid
,
1745 (u_longlong_t
)vd
->vdev_top
->vdev_guid
);
1746 vdev_dbgmsg(vd
, "LABEL: guid %llu, top_guid %llu, "
1747 "aux_guid %llu", (u_longlong_t
)guid
,
1748 (u_longlong_t
)top_guid
, (u_longlong_t
)aux_guid
);
1753 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
,
1755 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1756 VDEV_AUX_CORRUPT_DATA
);
1758 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1759 ZPOOL_CONFIG_POOL_STATE
);
1766 * If this is a verbatim import, no need to check the
1767 * state of the pool.
1769 if (!(spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) &&
1770 spa_load_state(spa
) == SPA_LOAD_OPEN
&&
1771 state
!= POOL_STATE_ACTIVE
) {
1772 vdev_dbgmsg(vd
, "vdev_validate: invalid pool state (%llu) "
1773 "for spa %s", (u_longlong_t
)state
, spa
->spa_name
);
1774 return (SET_ERROR(EBADF
));
1778 * If we were able to open and validate a vdev that was
1779 * previously marked permanently unavailable, clear that state
1782 if (vd
->vdev_not_present
)
1783 vd
->vdev_not_present
= 0;
1789 vdev_copy_path_impl(vdev_t
*svd
, vdev_t
*dvd
)
1791 if (svd
->vdev_path
!= NULL
&& dvd
->vdev_path
!= NULL
) {
1792 if (strcmp(svd
->vdev_path
, dvd
->vdev_path
) != 0) {
1793 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
1794 "from '%s' to '%s'", (u_longlong_t
)dvd
->vdev_guid
,
1795 dvd
->vdev_path
, svd
->vdev_path
);
1796 spa_strfree(dvd
->vdev_path
);
1797 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
1799 } else if (svd
->vdev_path
!= NULL
) {
1800 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
1801 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
1802 (u_longlong_t
)dvd
->vdev_guid
, dvd
->vdev_path
);
1807 * Recursively copy vdev paths from one vdev to another. Source and destination
1808 * vdev trees must have same geometry otherwise return error. Intended to copy
1809 * paths from userland config into MOS config.
1812 vdev_copy_path_strict(vdev_t
*svd
, vdev_t
*dvd
)
1814 if ((svd
->vdev_ops
== &vdev_missing_ops
) ||
1815 (svd
->vdev_ishole
&& dvd
->vdev_ishole
) ||
1816 (dvd
->vdev_ops
== &vdev_indirect_ops
))
1819 if (svd
->vdev_ops
!= dvd
->vdev_ops
) {
1820 vdev_dbgmsg(svd
, "vdev_copy_path: vdev type mismatch: %s != %s",
1821 svd
->vdev_ops
->vdev_op_type
, dvd
->vdev_ops
->vdev_op_type
);
1822 return (SET_ERROR(EINVAL
));
1825 if (svd
->vdev_guid
!= dvd
->vdev_guid
) {
1826 vdev_dbgmsg(svd
, "vdev_copy_path: guids mismatch (%llu != "
1827 "%llu)", (u_longlong_t
)svd
->vdev_guid
,
1828 (u_longlong_t
)dvd
->vdev_guid
);
1829 return (SET_ERROR(EINVAL
));
1832 if (svd
->vdev_children
!= dvd
->vdev_children
) {
1833 vdev_dbgmsg(svd
, "vdev_copy_path: children count mismatch: "
1834 "%llu != %llu", (u_longlong_t
)svd
->vdev_children
,
1835 (u_longlong_t
)dvd
->vdev_children
);
1836 return (SET_ERROR(EINVAL
));
1839 for (uint64_t i
= 0; i
< svd
->vdev_children
; i
++) {
1840 int error
= vdev_copy_path_strict(svd
->vdev_child
[i
],
1841 dvd
->vdev_child
[i
]);
1846 if (svd
->vdev_ops
->vdev_op_leaf
)
1847 vdev_copy_path_impl(svd
, dvd
);
1853 vdev_copy_path_search(vdev_t
*stvd
, vdev_t
*dvd
)
1855 ASSERT(stvd
->vdev_top
== stvd
);
1856 ASSERT3U(stvd
->vdev_id
, ==, dvd
->vdev_top
->vdev_id
);
1858 for (uint64_t i
= 0; i
< dvd
->vdev_children
; i
++) {
1859 vdev_copy_path_search(stvd
, dvd
->vdev_child
[i
]);
1862 if (!dvd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(dvd
))
1866 * The idea here is that while a vdev can shift positions within
1867 * a top vdev (when replacing, attaching mirror, etc.) it cannot
1868 * step outside of it.
1870 vdev_t
*vd
= vdev_lookup_by_guid(stvd
, dvd
->vdev_guid
);
1872 if (vd
== NULL
|| vd
->vdev_ops
!= dvd
->vdev_ops
)
1875 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1877 vdev_copy_path_impl(vd
, dvd
);
1881 * Recursively copy vdev paths from one root vdev to another. Source and
1882 * destination vdev trees may differ in geometry. For each destination leaf
1883 * vdev, search a vdev with the same guid and top vdev id in the source.
1884 * Intended to copy paths from userland config into MOS config.
1887 vdev_copy_path_relaxed(vdev_t
*srvd
, vdev_t
*drvd
)
1889 uint64_t children
= MIN(srvd
->vdev_children
, drvd
->vdev_children
);
1890 ASSERT(srvd
->vdev_ops
== &vdev_root_ops
);
1891 ASSERT(drvd
->vdev_ops
== &vdev_root_ops
);
1893 for (uint64_t i
= 0; i
< children
; i
++) {
1894 vdev_copy_path_search(srvd
->vdev_child
[i
],
1895 drvd
->vdev_child
[i
]);
1900 * Close a virtual device.
1903 vdev_close(vdev_t
*vd
)
1905 spa_t
*spa
= vd
->vdev_spa
;
1906 vdev_t
*pvd
= vd
->vdev_parent
;
1908 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1911 * If our parent is reopening, then we are as well, unless we are
1914 if (pvd
!= NULL
&& pvd
->vdev_reopening
)
1915 vd
->vdev_reopening
= (pvd
->vdev_reopening
&& !vd
->vdev_offline
);
1917 vd
->vdev_ops
->vdev_op_close(vd
);
1919 vdev_cache_purge(vd
);
1922 * We record the previous state before we close it, so that if we are
1923 * doing a reopen(), we don't generate FMA ereports if we notice that
1924 * it's still faulted.
1926 vd
->vdev_prevstate
= vd
->vdev_state
;
1928 if (vd
->vdev_offline
)
1929 vd
->vdev_state
= VDEV_STATE_OFFLINE
;
1931 vd
->vdev_state
= VDEV_STATE_CLOSED
;
1932 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1936 vdev_hold(vdev_t
*vd
)
1938 spa_t
*spa
= vd
->vdev_spa
;
1940 ASSERT(spa_is_root(spa
));
1941 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1944 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1945 vdev_hold(vd
->vdev_child
[c
]);
1947 if (vd
->vdev_ops
->vdev_op_leaf
)
1948 vd
->vdev_ops
->vdev_op_hold(vd
);
1952 vdev_rele(vdev_t
*vd
)
1954 spa_t
*spa
= vd
->vdev_spa
;
1956 ASSERT(spa_is_root(spa
));
1957 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1958 vdev_rele(vd
->vdev_child
[c
]);
1960 if (vd
->vdev_ops
->vdev_op_leaf
)
1961 vd
->vdev_ops
->vdev_op_rele(vd
);
1965 * Reopen all interior vdevs and any unopened leaves. We don't actually
1966 * reopen leaf vdevs which had previously been opened as they might deadlock
1967 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
1968 * If the leaf has never been opened then open it, as usual.
1971 vdev_reopen(vdev_t
*vd
)
1973 spa_t
*spa
= vd
->vdev_spa
;
1975 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1977 /* set the reopening flag unless we're taking the vdev offline */
1978 vd
->vdev_reopening
= !vd
->vdev_offline
;
1980 (void) vdev_open(vd
);
1983 * Call vdev_validate() here to make sure we have the same device.
1984 * Otherwise, a device with an invalid label could be successfully
1985 * opened in response to vdev_reopen().
1988 (void) vdev_validate_aux(vd
);
1989 if (vdev_readable(vd
) && vdev_writeable(vd
) &&
1990 vd
->vdev_aux
== &spa
->spa_l2cache
&&
1991 !l2arc_vdev_present(vd
))
1992 l2arc_add_vdev(spa
, vd
);
1994 (void) vdev_validate(vd
);
1998 * Reassess parent vdev's health.
2000 vdev_propagate_state(vd
);
2004 vdev_create(vdev_t
*vd
, uint64_t txg
, boolean_t isreplacing
)
2009 * Normally, partial opens (e.g. of a mirror) are allowed.
2010 * For a create, however, we want to fail the request if
2011 * there are any components we can't open.
2013 error
= vdev_open(vd
);
2015 if (error
|| vd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
2017 return (error
? error
: ENXIO
);
2021 * Recursively load DTLs and initialize all labels.
2023 if ((error
= vdev_dtl_load(vd
)) != 0 ||
2024 (error
= vdev_label_init(vd
, txg
, isreplacing
?
2025 VDEV_LABEL_REPLACE
: VDEV_LABEL_CREATE
)) != 0) {
2034 vdev_metaslab_set_size(vdev_t
*vd
)
2036 uint64_t asize
= vd
->vdev_asize
;
2037 uint64_t ms_count
= asize
>> vdev_default_ms_shift
;
2041 * There are two dimensions to the metaslab sizing calculation:
2042 * the size of the metaslab and the count of metaslabs per vdev.
2043 * In general, we aim for vdev_max_ms_count (200) metaslabs. The
2044 * range of the dimensions are as follows:
2046 * 2^29 <= ms_size <= 2^38
2047 * 16 <= ms_count <= 131,072
2049 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2050 * at least 512MB (2^29) to minimize fragmentation effects when
2051 * testing with smaller devices. However, the count constraint
2052 * of at least 16 metaslabs will override this minimum size goal.
2054 * On the upper end of vdev sizes, we aim for a maximum metaslab
2055 * size of 256GB. However, we will cap the total count to 2^17
2056 * metaslabs to keep our memory footprint in check.
2058 * The net effect of applying above constrains is summarized below.
2060 * vdev size metaslab count
2061 * -------------|-----------------
2063 * 8GB - 100GB one per 512MB
2065 * 50TB - 32PB one per 256GB
2067 * -------------------------------
2070 if (ms_count
< vdev_min_ms_count
)
2071 ms_shift
= highbit64(asize
/ vdev_min_ms_count
);
2072 else if (ms_count
> vdev_max_ms_count
)
2073 ms_shift
= highbit64(asize
/ vdev_max_ms_count
);
2075 ms_shift
= vdev_default_ms_shift
;
2077 if (ms_shift
< SPA_MAXBLOCKSHIFT
) {
2078 ms_shift
= SPA_MAXBLOCKSHIFT
;
2079 } else if (ms_shift
> vdev_max_ms_shift
) {
2080 ms_shift
= vdev_max_ms_shift
;
2081 /* cap the total count to constrain memory footprint */
2082 if ((asize
>> ms_shift
) > vdev_ms_count_limit
)
2083 ms_shift
= highbit64(asize
/ vdev_ms_count_limit
);
2086 vd
->vdev_ms_shift
= ms_shift
;
2087 ASSERT3U(vd
->vdev_ms_shift
, >=, SPA_MAXBLOCKSHIFT
);
2091 vdev_dirty(vdev_t
*vd
, int flags
, void *arg
, uint64_t txg
)
2093 ASSERT(vd
== vd
->vdev_top
);
2094 /* indirect vdevs don't have metaslabs or dtls */
2095 ASSERT(vdev_is_concrete(vd
) || flags
== 0);
2096 ASSERT(ISP2(flags
));
2097 ASSERT(spa_writeable(vd
->vdev_spa
));
2099 if (flags
& VDD_METASLAB
)
2100 (void) txg_list_add(&vd
->vdev_ms_list
, arg
, txg
);
2102 if (flags
& VDD_DTL
)
2103 (void) txg_list_add(&vd
->vdev_dtl_list
, arg
, txg
);
2105 (void) txg_list_add(&vd
->vdev_spa
->spa_vdev_txg_list
, vd
, txg
);
2109 vdev_dirty_leaves(vdev_t
*vd
, int flags
, uint64_t txg
)
2111 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2112 vdev_dirty_leaves(vd
->vdev_child
[c
], flags
, txg
);
2114 if (vd
->vdev_ops
->vdev_op_leaf
)
2115 vdev_dirty(vd
->vdev_top
, flags
, vd
, txg
);
2121 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2122 * the vdev has less than perfect replication. There are four kinds of DTL:
2124 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2126 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2128 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2129 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2130 * txgs that was scrubbed.
2132 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2133 * persistent errors or just some device being offline.
2134 * Unlike the other three, the DTL_OUTAGE map is not generally
2135 * maintained; it's only computed when needed, typically to
2136 * determine whether a device can be detached.
2138 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2139 * either has the data or it doesn't.
2141 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2142 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2143 * if any child is less than fully replicated, then so is its parent.
2144 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2145 * comprising only those txgs which appear in 'maxfaults' or more children;
2146 * those are the txgs we don't have enough replication to read. For example,
2147 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2148 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2149 * two child DTL_MISSING maps.
2151 * It should be clear from the above that to compute the DTLs and outage maps
2152 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2153 * Therefore, that is all we keep on disk. When loading the pool, or after
2154 * a configuration change, we generate all other DTLs from first principles.
2157 vdev_dtl_dirty(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2159 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2161 ASSERT(t
< DTL_TYPES
);
2162 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2163 ASSERT(spa_writeable(vd
->vdev_spa
));
2165 mutex_enter(&vd
->vdev_dtl_lock
);
2166 if (!range_tree_contains(rt
, txg
, size
))
2167 range_tree_add(rt
, txg
, size
);
2168 mutex_exit(&vd
->vdev_dtl_lock
);
2172 vdev_dtl_contains(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2174 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2175 boolean_t dirty
= B_FALSE
;
2177 ASSERT(t
< DTL_TYPES
);
2178 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2181 * While we are loading the pool, the DTLs have not been loaded yet.
2182 * Ignore the DTLs and try all devices. This avoids a recursive
2183 * mutex enter on the vdev_dtl_lock, and also makes us try hard
2184 * when loading the pool (relying on the checksum to ensure that
2185 * we get the right data -- note that we while loading, we are
2186 * only reading the MOS, which is always checksummed).
2188 if (vd
->vdev_spa
->spa_load_state
!= SPA_LOAD_NONE
)
2191 mutex_enter(&vd
->vdev_dtl_lock
);
2192 if (!range_tree_is_empty(rt
))
2193 dirty
= range_tree_contains(rt
, txg
, size
);
2194 mutex_exit(&vd
->vdev_dtl_lock
);
2200 vdev_dtl_empty(vdev_t
*vd
, vdev_dtl_type_t t
)
2202 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2205 mutex_enter(&vd
->vdev_dtl_lock
);
2206 empty
= range_tree_is_empty(rt
);
2207 mutex_exit(&vd
->vdev_dtl_lock
);
2213 * Returns the lowest txg in the DTL range.
2216 vdev_dtl_min(vdev_t
*vd
)
2220 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2221 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2222 ASSERT0(vd
->vdev_children
);
2224 rs
= avl_first(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
2225 return (rs
->rs_start
- 1);
2229 * Returns the highest txg in the DTL.
2232 vdev_dtl_max(vdev_t
*vd
)
2236 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2237 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2238 ASSERT0(vd
->vdev_children
);
2240 rs
= avl_last(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
2241 return (rs
->rs_end
);
2245 * Determine if a resilvering vdev should remove any DTL entries from
2246 * its range. If the vdev was resilvering for the entire duration of the
2247 * scan then it should excise that range from its DTLs. Otherwise, this
2248 * vdev is considered partially resilvered and should leave its DTL
2249 * entries intact. The comment in vdev_dtl_reassess() describes how we
2253 vdev_dtl_should_excise(vdev_t
*vd
)
2255 spa_t
*spa
= vd
->vdev_spa
;
2256 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
2258 ASSERT0(scn
->scn_phys
.scn_errors
);
2259 ASSERT0(vd
->vdev_children
);
2261 if (vd
->vdev_state
< VDEV_STATE_DEGRADED
)
2264 if (vd
->vdev_resilver_txg
== 0 ||
2265 range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]))
2269 * When a resilver is initiated the scan will assign the scn_max_txg
2270 * value to the highest txg value that exists in all DTLs. If this
2271 * device's max DTL is not part of this scan (i.e. it is not in
2272 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2275 if (vdev_dtl_max(vd
) <= scn
->scn_phys
.scn_max_txg
) {
2276 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <=, vdev_dtl_min(vd
));
2277 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <, vd
->vdev_resilver_txg
);
2278 ASSERT3U(vd
->vdev_resilver_txg
, <=, scn
->scn_phys
.scn_max_txg
);
2285 * Reassess DTLs after a config change or scrub completion.
2288 vdev_dtl_reassess(vdev_t
*vd
, uint64_t txg
, uint64_t scrub_txg
, int scrub_done
)
2290 spa_t
*spa
= vd
->vdev_spa
;
2294 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
2296 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2297 vdev_dtl_reassess(vd
->vdev_child
[c
], txg
,
2298 scrub_txg
, scrub_done
);
2300 if (vd
== spa
->spa_root_vdev
|| !vdev_is_concrete(vd
) || vd
->vdev_aux
)
2303 if (vd
->vdev_ops
->vdev_op_leaf
) {
2304 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
2306 mutex_enter(&vd
->vdev_dtl_lock
);
2309 * If we've completed a scan cleanly then determine
2310 * if this vdev should remove any DTLs. We only want to
2311 * excise regions on vdevs that were available during
2312 * the entire duration of this scan.
2314 if (scrub_txg
!= 0 &&
2315 (spa
->spa_scrub_started
||
2316 (scn
!= NULL
&& scn
->scn_phys
.scn_errors
== 0)) &&
2317 vdev_dtl_should_excise(vd
)) {
2319 * We completed a scrub up to scrub_txg. If we
2320 * did it without rebooting, then the scrub dtl
2321 * will be valid, so excise the old region and
2322 * fold in the scrub dtl. Otherwise, leave the
2323 * dtl as-is if there was an error.
2325 * There's little trick here: to excise the beginning
2326 * of the DTL_MISSING map, we put it into a reference
2327 * tree and then add a segment with refcnt -1 that
2328 * covers the range [0, scrub_txg). This means
2329 * that each txg in that range has refcnt -1 or 0.
2330 * We then add DTL_SCRUB with a refcnt of 2, so that
2331 * entries in the range [0, scrub_txg) will have a
2332 * positive refcnt -- either 1 or 2. We then convert
2333 * the reference tree into the new DTL_MISSING map.
2335 space_reftree_create(&reftree
);
2336 space_reftree_add_map(&reftree
,
2337 vd
->vdev_dtl
[DTL_MISSING
], 1);
2338 space_reftree_add_seg(&reftree
, 0, scrub_txg
, -1);
2339 space_reftree_add_map(&reftree
,
2340 vd
->vdev_dtl
[DTL_SCRUB
], 2);
2341 space_reftree_generate_map(&reftree
,
2342 vd
->vdev_dtl
[DTL_MISSING
], 1);
2343 space_reftree_destroy(&reftree
);
2345 range_tree_vacate(vd
->vdev_dtl
[DTL_PARTIAL
], NULL
, NULL
);
2346 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2347 range_tree_add
, vd
->vdev_dtl
[DTL_PARTIAL
]);
2349 range_tree_vacate(vd
->vdev_dtl
[DTL_SCRUB
], NULL
, NULL
);
2350 range_tree_vacate(vd
->vdev_dtl
[DTL_OUTAGE
], NULL
, NULL
);
2351 if (!vdev_readable(vd
))
2352 range_tree_add(vd
->vdev_dtl
[DTL_OUTAGE
], 0, -1ULL);
2354 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2355 range_tree_add
, vd
->vdev_dtl
[DTL_OUTAGE
]);
2358 * If the vdev was resilvering and no longer has any
2359 * DTLs then reset its resilvering flag.
2361 if (vd
->vdev_resilver_txg
!= 0 &&
2362 range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]) &&
2363 range_tree_is_empty(vd
->vdev_dtl
[DTL_OUTAGE
]))
2364 vd
->vdev_resilver_txg
= 0;
2366 mutex_exit(&vd
->vdev_dtl_lock
);
2369 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, txg
);
2373 mutex_enter(&vd
->vdev_dtl_lock
);
2374 for (int t
= 0; t
< DTL_TYPES
; t
++) {
2375 /* account for child's outage in parent's missing map */
2376 int s
= (t
== DTL_MISSING
) ? DTL_OUTAGE
: t
;
2378 continue; /* leaf vdevs only */
2379 if (t
== DTL_PARTIAL
)
2380 minref
= 1; /* i.e. non-zero */
2381 else if (vd
->vdev_nparity
!= 0)
2382 minref
= vd
->vdev_nparity
+ 1; /* RAID-Z */
2384 minref
= vd
->vdev_children
; /* any kind of mirror */
2385 space_reftree_create(&reftree
);
2386 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2387 vdev_t
*cvd
= vd
->vdev_child
[c
];
2388 mutex_enter(&cvd
->vdev_dtl_lock
);
2389 space_reftree_add_map(&reftree
, cvd
->vdev_dtl
[s
], 1);
2390 mutex_exit(&cvd
->vdev_dtl_lock
);
2392 space_reftree_generate_map(&reftree
, vd
->vdev_dtl
[t
], minref
);
2393 space_reftree_destroy(&reftree
);
2395 mutex_exit(&vd
->vdev_dtl_lock
);
2399 vdev_dtl_load(vdev_t
*vd
)
2401 spa_t
*spa
= vd
->vdev_spa
;
2402 objset_t
*mos
= spa
->spa_meta_objset
;
2405 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_dtl_object
!= 0) {
2406 ASSERT(vdev_is_concrete(vd
));
2408 error
= space_map_open(&vd
->vdev_dtl_sm
, mos
,
2409 vd
->vdev_dtl_object
, 0, -1ULL, 0);
2412 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2414 mutex_enter(&vd
->vdev_dtl_lock
);
2417 * Now that we've opened the space_map we need to update
2420 space_map_update(vd
->vdev_dtl_sm
);
2422 error
= space_map_load(vd
->vdev_dtl_sm
,
2423 vd
->vdev_dtl
[DTL_MISSING
], SM_ALLOC
);
2424 mutex_exit(&vd
->vdev_dtl_lock
);
2429 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2430 error
= vdev_dtl_load(vd
->vdev_child
[c
]);
2439 vdev_destroy_unlink_zap(vdev_t
*vd
, uint64_t zapobj
, dmu_tx_t
*tx
)
2441 spa_t
*spa
= vd
->vdev_spa
;
2443 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zapobj
, tx
));
2444 VERIFY0(zap_remove_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2449 vdev_create_link_zap(vdev_t
*vd
, dmu_tx_t
*tx
)
2451 spa_t
*spa
= vd
->vdev_spa
;
2452 uint64_t zap
= zap_create(spa
->spa_meta_objset
, DMU_OTN_ZAP_METADATA
,
2453 DMU_OT_NONE
, 0, tx
);
2456 VERIFY0(zap_add_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2463 vdev_construct_zaps(vdev_t
*vd
, dmu_tx_t
*tx
)
2465 if (vd
->vdev_ops
!= &vdev_hole_ops
&&
2466 vd
->vdev_ops
!= &vdev_missing_ops
&&
2467 vd
->vdev_ops
!= &vdev_root_ops
&&
2468 !vd
->vdev_top
->vdev_removing
) {
2469 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_leaf_zap
== 0) {
2470 vd
->vdev_leaf_zap
= vdev_create_link_zap(vd
, tx
);
2472 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
== 0) {
2473 vd
->vdev_top_zap
= vdev_create_link_zap(vd
, tx
);
2476 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2477 vdev_construct_zaps(vd
->vdev_child
[i
], tx
);
2482 vdev_dtl_sync(vdev_t
*vd
, uint64_t txg
)
2484 spa_t
*spa
= vd
->vdev_spa
;
2485 range_tree_t
*rt
= vd
->vdev_dtl
[DTL_MISSING
];
2486 objset_t
*mos
= spa
->spa_meta_objset
;
2487 range_tree_t
*rtsync
;
2489 uint64_t object
= space_map_object(vd
->vdev_dtl_sm
);
2491 ASSERT(vdev_is_concrete(vd
));
2492 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2494 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2496 if (vd
->vdev_detached
|| vd
->vdev_top
->vdev_removing
) {
2497 mutex_enter(&vd
->vdev_dtl_lock
);
2498 space_map_free(vd
->vdev_dtl_sm
, tx
);
2499 space_map_close(vd
->vdev_dtl_sm
);
2500 vd
->vdev_dtl_sm
= NULL
;
2501 mutex_exit(&vd
->vdev_dtl_lock
);
2504 * We only destroy the leaf ZAP for detached leaves or for
2505 * removed log devices. Removed data devices handle leaf ZAP
2506 * cleanup later, once cancellation is no longer possible.
2508 if (vd
->vdev_leaf_zap
!= 0 && (vd
->vdev_detached
||
2509 vd
->vdev_top
->vdev_islog
)) {
2510 vdev_destroy_unlink_zap(vd
, vd
->vdev_leaf_zap
, tx
);
2511 vd
->vdev_leaf_zap
= 0;
2518 if (vd
->vdev_dtl_sm
== NULL
) {
2519 uint64_t new_object
;
2521 new_object
= space_map_alloc(mos
, vdev_dtl_sm_blksz
, tx
);
2522 VERIFY3U(new_object
, !=, 0);
2524 VERIFY0(space_map_open(&vd
->vdev_dtl_sm
, mos
, new_object
,
2526 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2529 rtsync
= range_tree_create(NULL
, NULL
);
2531 mutex_enter(&vd
->vdev_dtl_lock
);
2532 range_tree_walk(rt
, range_tree_add
, rtsync
);
2533 mutex_exit(&vd
->vdev_dtl_lock
);
2535 space_map_truncate(vd
->vdev_dtl_sm
, vdev_dtl_sm_blksz
, tx
);
2536 space_map_write(vd
->vdev_dtl_sm
, rtsync
, SM_ALLOC
, SM_NO_VDEVID
, tx
);
2537 range_tree_vacate(rtsync
, NULL
, NULL
);
2539 range_tree_destroy(rtsync
);
2542 * If the object for the space map has changed then dirty
2543 * the top level so that we update the config.
2545 if (object
!= space_map_object(vd
->vdev_dtl_sm
)) {
2546 vdev_dbgmsg(vd
, "txg %llu, spa %s, DTL old object %llu, "
2547 "new object %llu", (u_longlong_t
)txg
, spa_name(spa
),
2548 (u_longlong_t
)object
,
2549 (u_longlong_t
)space_map_object(vd
->vdev_dtl_sm
));
2550 vdev_config_dirty(vd
->vdev_top
);
2555 mutex_enter(&vd
->vdev_dtl_lock
);
2556 space_map_update(vd
->vdev_dtl_sm
);
2557 mutex_exit(&vd
->vdev_dtl_lock
);
2561 * Determine whether the specified vdev can be offlined/detached/removed
2562 * without losing data.
2565 vdev_dtl_required(vdev_t
*vd
)
2567 spa_t
*spa
= vd
->vdev_spa
;
2568 vdev_t
*tvd
= vd
->vdev_top
;
2569 uint8_t cant_read
= vd
->vdev_cant_read
;
2572 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2574 if (vd
== spa
->spa_root_vdev
|| vd
== tvd
)
2578 * Temporarily mark the device as unreadable, and then determine
2579 * whether this results in any DTL outages in the top-level vdev.
2580 * If not, we can safely offline/detach/remove the device.
2582 vd
->vdev_cant_read
= B_TRUE
;
2583 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2584 required
= !vdev_dtl_empty(tvd
, DTL_OUTAGE
);
2585 vd
->vdev_cant_read
= cant_read
;
2586 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2588 if (!required
&& zio_injection_enabled
)
2589 required
= !!zio_handle_device_injection(vd
, NULL
, ECHILD
);
2595 * Determine if resilver is needed, and if so the txg range.
2598 vdev_resilver_needed(vdev_t
*vd
, uint64_t *minp
, uint64_t *maxp
)
2600 boolean_t needed
= B_FALSE
;
2601 uint64_t thismin
= UINT64_MAX
;
2602 uint64_t thismax
= 0;
2604 if (vd
->vdev_children
== 0) {
2605 mutex_enter(&vd
->vdev_dtl_lock
);
2606 if (!range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]) &&
2607 vdev_writeable(vd
)) {
2609 thismin
= vdev_dtl_min(vd
);
2610 thismax
= vdev_dtl_max(vd
);
2613 mutex_exit(&vd
->vdev_dtl_lock
);
2615 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2616 vdev_t
*cvd
= vd
->vdev_child
[c
];
2617 uint64_t cmin
, cmax
;
2619 if (vdev_resilver_needed(cvd
, &cmin
, &cmax
)) {
2620 thismin
= MIN(thismin
, cmin
);
2621 thismax
= MAX(thismax
, cmax
);
2627 if (needed
&& minp
) {
2635 * Gets the checkpoint space map object from the vdev's ZAP.
2636 * Returns the spacemap object, or 0 if it wasn't in the ZAP
2637 * or the ZAP doesn't exist yet.
2640 vdev_checkpoint_sm_object(vdev_t
*vd
)
2642 ASSERT0(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
));
2643 if (vd
->vdev_top_zap
== 0) {
2647 uint64_t sm_obj
= 0;
2648 int err
= zap_lookup(spa_meta_objset(vd
->vdev_spa
), vd
->vdev_top_zap
,
2649 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM
, sizeof (uint64_t), 1, &sm_obj
);
2651 ASSERT(err
== 0 || err
== ENOENT
);
2657 vdev_load(vdev_t
*vd
)
2661 * Recursively load all children.
2663 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2664 error
= vdev_load(vd
->vdev_child
[c
]);
2670 vdev_set_deflate_ratio(vd
);
2673 * If this is a top-level vdev, initialize its metaslabs.
2675 if (vd
== vd
->vdev_top
&& vdev_is_concrete(vd
)) {
2676 if (vd
->vdev_ashift
== 0 || vd
->vdev_asize
== 0) {
2677 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2678 VDEV_AUX_CORRUPT_DATA
);
2679 vdev_dbgmsg(vd
, "vdev_load: invalid size. ashift=%llu, "
2680 "asize=%llu", (u_longlong_t
)vd
->vdev_ashift
,
2681 (u_longlong_t
)vd
->vdev_asize
);
2682 return (SET_ERROR(ENXIO
));
2683 } else if ((error
= vdev_metaslab_init(vd
, 0)) != 0) {
2684 vdev_dbgmsg(vd
, "vdev_load: metaslab_init failed "
2685 "[error=%d]", error
);
2686 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2687 VDEV_AUX_CORRUPT_DATA
);
2691 uint64_t checkpoint_sm_obj
= vdev_checkpoint_sm_object(vd
);
2692 if (checkpoint_sm_obj
!= 0) {
2693 objset_t
*mos
= spa_meta_objset(vd
->vdev_spa
);
2694 ASSERT(vd
->vdev_asize
!= 0);
2695 ASSERT3P(vd
->vdev_checkpoint_sm
, ==, NULL
);
2697 if ((error
= space_map_open(&vd
->vdev_checkpoint_sm
,
2698 mos
, checkpoint_sm_obj
, 0, vd
->vdev_asize
,
2699 vd
->vdev_ashift
))) {
2700 vdev_dbgmsg(vd
, "vdev_load: space_map_open "
2701 "failed for checkpoint spacemap (obj %llu) "
2703 (u_longlong_t
)checkpoint_sm_obj
, error
);
2706 ASSERT3P(vd
->vdev_checkpoint_sm
, !=, NULL
);
2707 space_map_update(vd
->vdev_checkpoint_sm
);
2710 * Since the checkpoint_sm contains free entries
2711 * exclusively we can use sm_alloc to indicate the
2712 * culmulative checkpointed space that has been freed.
2714 vd
->vdev_stat
.vs_checkpoint_space
=
2715 -vd
->vdev_checkpoint_sm
->sm_alloc
;
2716 vd
->vdev_spa
->spa_checkpoint_info
.sci_dspace
+=
2717 vd
->vdev_stat
.vs_checkpoint_space
;
2722 * If this is a leaf vdev, load its DTL.
2724 if (vd
->vdev_ops
->vdev_op_leaf
&& (error
= vdev_dtl_load(vd
)) != 0) {
2725 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2726 VDEV_AUX_CORRUPT_DATA
);
2727 vdev_dbgmsg(vd
, "vdev_load: vdev_dtl_load failed "
2728 "[error=%d]", error
);
2732 uint64_t obsolete_sm_object
= vdev_obsolete_sm_object(vd
);
2733 if (obsolete_sm_object
!= 0) {
2734 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
2735 ASSERT(vd
->vdev_asize
!= 0);
2736 ASSERT3P(vd
->vdev_obsolete_sm
, ==, NULL
);
2738 if ((error
= space_map_open(&vd
->vdev_obsolete_sm
, mos
,
2739 obsolete_sm_object
, 0, vd
->vdev_asize
, 0))) {
2740 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2741 VDEV_AUX_CORRUPT_DATA
);
2742 vdev_dbgmsg(vd
, "vdev_load: space_map_open failed for "
2743 "obsolete spacemap (obj %llu) [error=%d]",
2744 (u_longlong_t
)obsolete_sm_object
, error
);
2747 space_map_update(vd
->vdev_obsolete_sm
);
2754 * The special vdev case is used for hot spares and l2cache devices. Its
2755 * sole purpose it to set the vdev state for the associated vdev. To do this,
2756 * we make sure that we can open the underlying device, then try to read the
2757 * label, and make sure that the label is sane and that it hasn't been
2758 * repurposed to another pool.
2761 vdev_validate_aux(vdev_t
*vd
)
2764 uint64_t guid
, version
;
2767 if (!vdev_readable(vd
))
2770 if ((label
= vdev_label_read_config(vd
, -1ULL)) == NULL
) {
2771 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2772 VDEV_AUX_CORRUPT_DATA
);
2776 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_VERSION
, &version
) != 0 ||
2777 !SPA_VERSION_IS_SUPPORTED(version
) ||
2778 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0 ||
2779 guid
!= vd
->vdev_guid
||
2780 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
, &state
) != 0) {
2781 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2782 VDEV_AUX_CORRUPT_DATA
);
2788 * We don't actually check the pool state here. If it's in fact in
2789 * use by another pool, we update this fact on the fly when requested.
2796 * Free the objects used to store this vdev's spacemaps, and the array
2797 * that points to them.
2800 vdev_destroy_spacemaps(vdev_t
*vd
, dmu_tx_t
*tx
)
2802 if (vd
->vdev_ms_array
== 0)
2805 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
2806 uint64_t array_count
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
2807 size_t array_bytes
= array_count
* sizeof (uint64_t);
2808 uint64_t *smobj_array
= kmem_alloc(array_bytes
, KM_SLEEP
);
2809 VERIFY0(dmu_read(mos
, vd
->vdev_ms_array
, 0,
2810 array_bytes
, smobj_array
, 0));
2812 for (uint64_t i
= 0; i
< array_count
; i
++) {
2813 uint64_t smobj
= smobj_array
[i
];
2817 space_map_free_obj(mos
, smobj
, tx
);
2820 kmem_free(smobj_array
, array_bytes
);
2821 VERIFY0(dmu_object_free(mos
, vd
->vdev_ms_array
, tx
));
2822 vd
->vdev_ms_array
= 0;
2826 vdev_remove_empty(vdev_t
*vd
, uint64_t txg
)
2828 spa_t
*spa
= vd
->vdev_spa
;
2831 ASSERT(vd
== vd
->vdev_top
);
2832 ASSERT3U(txg
, ==, spa_syncing_txg(spa
));
2834 if (vd
->vdev_ms
!= NULL
) {
2835 metaslab_group_t
*mg
= vd
->vdev_mg
;
2837 metaslab_group_histogram_verify(mg
);
2838 metaslab_class_histogram_verify(mg
->mg_class
);
2840 for (int m
= 0; m
< vd
->vdev_ms_count
; m
++) {
2841 metaslab_t
*msp
= vd
->vdev_ms
[m
];
2843 if (msp
== NULL
|| msp
->ms_sm
== NULL
)
2846 mutex_enter(&msp
->ms_lock
);
2848 * If the metaslab was not loaded when the vdev
2849 * was removed then the histogram accounting may
2850 * not be accurate. Update the histogram information
2851 * here so that we ensure that the metaslab group
2852 * and metaslab class are up-to-date.
2854 metaslab_group_histogram_remove(mg
, msp
);
2856 VERIFY0(space_map_allocated(msp
->ms_sm
));
2857 space_map_close(msp
->ms_sm
);
2859 mutex_exit(&msp
->ms_lock
);
2862 if (vd
->vdev_checkpoint_sm
!= NULL
) {
2863 ASSERT(spa_has_checkpoint(spa
));
2864 space_map_close(vd
->vdev_checkpoint_sm
);
2865 vd
->vdev_checkpoint_sm
= NULL
;
2868 metaslab_group_histogram_verify(mg
);
2869 metaslab_class_histogram_verify(mg
->mg_class
);
2870 for (int i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++)
2871 ASSERT0(mg
->mg_histogram
[i
]);
2874 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
2875 vdev_destroy_spacemaps(vd
, tx
);
2877 if (vd
->vdev_islog
&& vd
->vdev_top_zap
!= 0) {
2878 vdev_destroy_unlink_zap(vd
, vd
->vdev_top_zap
, tx
);
2879 vd
->vdev_top_zap
= 0;
2885 vdev_sync_done(vdev_t
*vd
, uint64_t txg
)
2888 boolean_t reassess
= !txg_list_empty(&vd
->vdev_ms_list
, TXG_CLEAN(txg
));
2890 ASSERT(vdev_is_concrete(vd
));
2892 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, TXG_CLEAN(txg
)))
2894 metaslab_sync_done(msp
, txg
);
2897 metaslab_sync_reassess(vd
->vdev_mg
);
2901 vdev_sync(vdev_t
*vd
, uint64_t txg
)
2903 spa_t
*spa
= vd
->vdev_spa
;
2908 if (range_tree_space(vd
->vdev_obsolete_segments
) > 0) {
2911 ASSERT(vd
->vdev_removing
||
2912 vd
->vdev_ops
== &vdev_indirect_ops
);
2914 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2915 vdev_indirect_sync_obsolete(vd
, tx
);
2919 * If the vdev is indirect, it can't have dirty
2920 * metaslabs or DTLs.
2922 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
2923 ASSERT(txg_list_empty(&vd
->vdev_ms_list
, txg
));
2924 ASSERT(txg_list_empty(&vd
->vdev_dtl_list
, txg
));
2929 ASSERT(vdev_is_concrete(vd
));
2931 if (vd
->vdev_ms_array
== 0 && vd
->vdev_ms_shift
!= 0 &&
2932 !vd
->vdev_removing
) {
2933 ASSERT(vd
== vd
->vdev_top
);
2934 ASSERT0(vd
->vdev_indirect_config
.vic_mapping_object
);
2935 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2936 vd
->vdev_ms_array
= dmu_object_alloc(spa
->spa_meta_objset
,
2937 DMU_OT_OBJECT_ARRAY
, 0, DMU_OT_NONE
, 0, tx
);
2938 ASSERT(vd
->vdev_ms_array
!= 0);
2939 vdev_config_dirty(vd
);
2943 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, txg
)) != NULL
) {
2944 metaslab_sync(msp
, txg
);
2945 (void) txg_list_add(&vd
->vdev_ms_list
, msp
, TXG_CLEAN(txg
));
2948 while ((lvd
= txg_list_remove(&vd
->vdev_dtl_list
, txg
)) != NULL
)
2949 vdev_dtl_sync(lvd
, txg
);
2952 * Remove the metadata associated with this vdev once it's empty.
2953 * Note that this is typically used for log/cache device removal;
2954 * we don't empty toplevel vdevs when removing them. But if
2955 * a toplevel happens to be emptied, this is not harmful.
2957 if (vd
->vdev_stat
.vs_alloc
== 0 && vd
->vdev_removing
) {
2958 vdev_remove_empty(vd
, txg
);
2961 (void) txg_list_add(&spa
->spa_vdev_txg_list
, vd
, TXG_CLEAN(txg
));
2965 vdev_psize_to_asize(vdev_t
*vd
, uint64_t psize
)
2967 return (vd
->vdev_ops
->vdev_op_asize(vd
, psize
));
2971 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
2972 * not be opened, and no I/O is attempted.
2975 vdev_fault(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
2979 spa_vdev_state_enter(spa
, SCL_NONE
);
2981 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2982 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2984 if (!vd
->vdev_ops
->vdev_op_leaf
)
2985 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2990 * We don't directly use the aux state here, but if we do a
2991 * vdev_reopen(), we need this value to be present to remember why we
2994 vd
->vdev_label_aux
= aux
;
2997 * Faulted state takes precedence over degraded.
2999 vd
->vdev_delayed_close
= B_FALSE
;
3000 vd
->vdev_faulted
= 1ULL;
3001 vd
->vdev_degraded
= 0ULL;
3002 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_FAULTED
, aux
);
3005 * If this device has the only valid copy of the data, then
3006 * back off and simply mark the vdev as degraded instead.
3008 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&& vdev_dtl_required(vd
)) {
3009 vd
->vdev_degraded
= 1ULL;
3010 vd
->vdev_faulted
= 0ULL;
3013 * If we reopen the device and it's not dead, only then do we
3018 if (vdev_readable(vd
))
3019 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
, aux
);
3022 return (spa_vdev_state_exit(spa
, vd
, 0));
3026 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
3027 * user that something is wrong. The vdev continues to operate as normal as far
3028 * as I/O is concerned.
3031 vdev_degrade(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
3035 spa_vdev_state_enter(spa
, SCL_NONE
);
3037 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3038 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
3040 if (!vd
->vdev_ops
->vdev_op_leaf
)
3041 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3044 * If the vdev is already faulted, then don't do anything.
3046 if (vd
->vdev_faulted
|| vd
->vdev_degraded
)
3047 return (spa_vdev_state_exit(spa
, NULL
, 0));
3049 vd
->vdev_degraded
= 1ULL;
3050 if (!vdev_is_dead(vd
))
3051 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
,
3054 return (spa_vdev_state_exit(spa
, vd
, 0));
3058 * Online the given vdev.
3060 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
3061 * spare device should be detached when the device finishes resilvering.
3062 * Second, the online should be treated like a 'test' online case, so no FMA
3063 * events are generated if the device fails to open.
3066 vdev_online(spa_t
*spa
, uint64_t guid
, uint64_t flags
, vdev_state_t
*newstate
)
3068 vdev_t
*vd
, *tvd
, *pvd
, *rvd
= spa
->spa_root_vdev
;
3069 boolean_t wasoffline
;
3070 vdev_state_t oldstate
;
3072 spa_vdev_state_enter(spa
, SCL_NONE
);
3074 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3075 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
3077 if (!vd
->vdev_ops
->vdev_op_leaf
)
3078 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3080 wasoffline
= (vd
->vdev_offline
|| vd
->vdev_tmpoffline
);
3081 oldstate
= vd
->vdev_state
;
3084 vd
->vdev_offline
= B_FALSE
;
3085 vd
->vdev_tmpoffline
= B_FALSE
;
3086 vd
->vdev_checkremove
= !!(flags
& ZFS_ONLINE_CHECKREMOVE
);
3087 vd
->vdev_forcefault
= !!(flags
& ZFS_ONLINE_FORCEFAULT
);
3089 /* XXX - L2ARC 1.0 does not support expansion */
3090 if (!vd
->vdev_aux
) {
3091 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3092 pvd
->vdev_expanding
= !!(flags
& ZFS_ONLINE_EXPAND
);
3096 vd
->vdev_checkremove
= vd
->vdev_forcefault
= B_FALSE
;
3098 if (!vd
->vdev_aux
) {
3099 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3100 pvd
->vdev_expanding
= B_FALSE
;
3104 *newstate
= vd
->vdev_state
;
3105 if ((flags
& ZFS_ONLINE_UNSPARE
) &&
3106 !vdev_is_dead(vd
) && vd
->vdev_parent
&&
3107 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3108 vd
->vdev_parent
->vdev_child
[0] == vd
)
3109 vd
->vdev_unspare
= B_TRUE
;
3111 if ((flags
& ZFS_ONLINE_EXPAND
) || spa
->spa_autoexpand
) {
3113 /* XXX - L2ARC 1.0 does not support expansion */
3115 return (spa_vdev_state_exit(spa
, vd
, ENOTSUP
));
3116 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3119 /* Restart initializing if necessary */
3120 mutex_enter(&vd
->vdev_initialize_lock
);
3121 if (vdev_writeable(vd
) &&
3122 vd
->vdev_initialize_thread
== NULL
&&
3123 vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
) {
3124 (void) vdev_initialize(vd
);
3126 mutex_exit(&vd
->vdev_initialize_lock
);
3129 (oldstate
< VDEV_STATE_DEGRADED
&&
3130 vd
->vdev_state
>= VDEV_STATE_DEGRADED
))
3131 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_ONLINE
);
3133 return (spa_vdev_state_exit(spa
, vd
, 0));
3137 vdev_offline_locked(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
3141 uint64_t generation
;
3142 metaslab_group_t
*mg
;
3145 spa_vdev_state_enter(spa
, SCL_ALLOC
);
3147 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3148 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
3150 if (!vd
->vdev_ops
->vdev_op_leaf
)
3151 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3155 generation
= spa
->spa_config_generation
+ 1;
3158 * If the device isn't already offline, try to offline it.
3160 if (!vd
->vdev_offline
) {
3162 * If this device has the only valid copy of some data,
3163 * don't allow it to be offlined. Log devices are always
3166 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
3167 vdev_dtl_required(vd
))
3168 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
3171 * If the top-level is a slog and it has had allocations
3172 * then proceed. We check that the vdev's metaslab group
3173 * is not NULL since it's possible that we may have just
3174 * added this vdev but not yet initialized its metaslabs.
3176 if (tvd
->vdev_islog
&& mg
!= NULL
) {
3178 * Prevent any future allocations.
3180 metaslab_group_passivate(mg
);
3181 (void) spa_vdev_state_exit(spa
, vd
, 0);
3183 error
= spa_reset_logs(spa
);
3186 * If the log device was successfully reset but has
3187 * checkpointed data, do not offline it.
3190 tvd
->vdev_checkpoint_sm
!= NULL
) {
3191 ASSERT3U(tvd
->vdev_checkpoint_sm
->sm_alloc
,
3193 error
= ZFS_ERR_CHECKPOINT_EXISTS
;
3196 spa_vdev_state_enter(spa
, SCL_ALLOC
);
3199 * Check to see if the config has changed.
3201 if (error
|| generation
!= spa
->spa_config_generation
) {
3202 metaslab_group_activate(mg
);
3204 return (spa_vdev_state_exit(spa
,
3206 (void) spa_vdev_state_exit(spa
, vd
, 0);
3209 ASSERT0(tvd
->vdev_stat
.vs_alloc
);
3213 * Offline this device and reopen its top-level vdev.
3214 * If the top-level vdev is a log device then just offline
3215 * it. Otherwise, if this action results in the top-level
3216 * vdev becoming unusable, undo it and fail the request.
3218 vd
->vdev_offline
= B_TRUE
;
3221 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
3222 vdev_is_dead(tvd
)) {
3223 vd
->vdev_offline
= B_FALSE
;
3225 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
3229 * Add the device back into the metaslab rotor so that
3230 * once we online the device it's open for business.
3232 if (tvd
->vdev_islog
&& mg
!= NULL
)
3233 metaslab_group_activate(mg
);
3236 vd
->vdev_tmpoffline
= !!(flags
& ZFS_OFFLINE_TEMPORARY
);
3238 return (spa_vdev_state_exit(spa
, vd
, 0));
3242 vdev_offline(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
3246 mutex_enter(&spa
->spa_vdev_top_lock
);
3247 error
= vdev_offline_locked(spa
, guid
, flags
);
3248 mutex_exit(&spa
->spa_vdev_top_lock
);
3254 * Clear the error counts associated with this vdev. Unlike vdev_online() and
3255 * vdev_offline(), we assume the spa config is locked. We also clear all
3256 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
3259 vdev_clear(spa_t
*spa
, vdev_t
*vd
)
3261 vdev_t
*rvd
= spa
->spa_root_vdev
;
3263 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
3268 vd
->vdev_stat
.vs_read_errors
= 0;
3269 vd
->vdev_stat
.vs_write_errors
= 0;
3270 vd
->vdev_stat
.vs_checksum_errors
= 0;
3272 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3273 vdev_clear(spa
, vd
->vdev_child
[c
]);
3276 * It makes no sense to "clear" an indirect vdev.
3278 if (!vdev_is_concrete(vd
))
3282 * If we're in the FAULTED state or have experienced failed I/O, then
3283 * clear the persistent state and attempt to reopen the device. We
3284 * also mark the vdev config dirty, so that the new faulted state is
3285 * written out to disk.
3287 if (vd
->vdev_faulted
|| vd
->vdev_degraded
||
3288 !vdev_readable(vd
) || !vdev_writeable(vd
)) {
3291 * When reopening in reponse to a clear event, it may be due to
3292 * a fmadm repair request. In this case, if the device is
3293 * still broken, we want to still post the ereport again.
3295 vd
->vdev_forcefault
= B_TRUE
;
3297 vd
->vdev_faulted
= vd
->vdev_degraded
= 0ULL;
3298 vd
->vdev_cant_read
= B_FALSE
;
3299 vd
->vdev_cant_write
= B_FALSE
;
3301 vdev_reopen(vd
== rvd
? rvd
: vd
->vdev_top
);
3303 vd
->vdev_forcefault
= B_FALSE
;
3305 if (vd
!= rvd
&& vdev_writeable(vd
->vdev_top
))
3306 vdev_state_dirty(vd
->vdev_top
);
3308 if (vd
->vdev_aux
== NULL
&& !vdev_is_dead(vd
))
3309 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
3311 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_CLEAR
);
3315 * When clearing a FMA-diagnosed fault, we always want to
3316 * unspare the device, as we assume that the original spare was
3317 * done in response to the FMA fault.
3319 if (!vdev_is_dead(vd
) && vd
->vdev_parent
!= NULL
&&
3320 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3321 vd
->vdev_parent
->vdev_child
[0] == vd
)
3322 vd
->vdev_unspare
= B_TRUE
;
3326 vdev_is_dead(vdev_t
*vd
)
3329 * Holes and missing devices are always considered "dead".
3330 * This simplifies the code since we don't have to check for
3331 * these types of devices in the various code paths.
3332 * Instead we rely on the fact that we skip over dead devices
3333 * before issuing I/O to them.
3335 return (vd
->vdev_state
< VDEV_STATE_DEGRADED
||
3336 vd
->vdev_ops
== &vdev_hole_ops
||
3337 vd
->vdev_ops
== &vdev_missing_ops
);
3341 vdev_readable(vdev_t
*vd
)
3343 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_read
);
3347 vdev_writeable(vdev_t
*vd
)
3349 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_write
&&
3350 vdev_is_concrete(vd
));
3354 vdev_allocatable(vdev_t
*vd
)
3356 uint64_t state
= vd
->vdev_state
;
3359 * We currently allow allocations from vdevs which may be in the
3360 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3361 * fails to reopen then we'll catch it later when we're holding
3362 * the proper locks. Note that we have to get the vdev state
3363 * in a local variable because although it changes atomically,
3364 * we're asking two separate questions about it.
3366 return (!(state
< VDEV_STATE_DEGRADED
&& state
!= VDEV_STATE_CLOSED
) &&
3367 !vd
->vdev_cant_write
&& vdev_is_concrete(vd
) &&
3368 vd
->vdev_mg
->mg_initialized
);
3372 vdev_accessible(vdev_t
*vd
, zio_t
*zio
)
3374 ASSERT(zio
->io_vd
== vd
);
3376 if (vdev_is_dead(vd
) || vd
->vdev_remove_wanted
)
3379 if (zio
->io_type
== ZIO_TYPE_READ
)
3380 return (!vd
->vdev_cant_read
);
3382 if (zio
->io_type
== ZIO_TYPE_WRITE
)
3383 return (!vd
->vdev_cant_write
);
3389 vdev_is_spacemap_addressable(vdev_t
*vd
)
3392 * Assuming 47 bits of the space map entry dedicated for the entry's
3393 * offset (see description in space_map.h), we calculate the maximum
3394 * address that can be described by a space map entry for the given
3397 uint64_t shift
= vd
->vdev_ashift
+ 47;
3399 if (shift
>= 63) /* detect potential overflow */
3402 return (vd
->vdev_asize
< (1ULL << shift
));
3406 * Get statistics for the given vdev.
3409 vdev_get_stats(vdev_t
*vd
, vdev_stat_t
*vs
)
3411 spa_t
*spa
= vd
->vdev_spa
;
3412 vdev_t
*rvd
= spa
->spa_root_vdev
;
3413 vdev_t
*tvd
= vd
->vdev_top
;
3415 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
3417 mutex_enter(&vd
->vdev_stat_lock
);
3418 bcopy(&vd
->vdev_stat
, vs
, sizeof (*vs
));
3419 vs
->vs_timestamp
= gethrtime() - vs
->vs_timestamp
;
3420 vs
->vs_state
= vd
->vdev_state
;
3421 vs
->vs_rsize
= vdev_get_min_asize(vd
);
3422 if (vd
->vdev_ops
->vdev_op_leaf
) {
3423 vs
->vs_rsize
+= VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
;
3425 * Report intializing progress. Since we don't have the
3426 * initializing locks held, this is only an estimate (although a
3427 * fairly accurate one).
3429 vs
->vs_initialize_bytes_done
= vd
->vdev_initialize_bytes_done
;
3430 vs
->vs_initialize_bytes_est
= vd
->vdev_initialize_bytes_est
;
3431 vs
->vs_initialize_state
= vd
->vdev_initialize_state
;
3432 vs
->vs_initialize_action_time
= vd
->vdev_initialize_action_time
;
3435 * Report expandable space on top-level, non-auxillary devices only.
3436 * The expandable space is reported in terms of metaslab sized units
3437 * since that determines how much space the pool can expand.
3439 if (vd
->vdev_aux
== NULL
&& tvd
!= NULL
) {
3440 vs
->vs_esize
= P2ALIGN(vd
->vdev_max_asize
- vd
->vdev_asize
-
3441 spa
->spa_bootsize
, 1ULL << tvd
->vdev_ms_shift
);
3443 if (vd
->vdev_aux
== NULL
&& vd
== vd
->vdev_top
&&
3444 vdev_is_concrete(vd
)) {
3445 vs
->vs_fragmentation
= vd
->vdev_mg
->mg_fragmentation
;
3449 * If we're getting stats on the root vdev, aggregate the I/O counts
3450 * over all top-level vdevs (i.e. the direct children of the root).
3453 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
3454 vdev_t
*cvd
= rvd
->vdev_child
[c
];
3455 vdev_stat_t
*cvs
= &cvd
->vdev_stat
;
3457 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
3458 vs
->vs_ops
[t
] += cvs
->vs_ops
[t
];
3459 vs
->vs_bytes
[t
] += cvs
->vs_bytes
[t
];
3461 cvs
->vs_scan_removing
= cvd
->vdev_removing
;
3464 mutex_exit(&vd
->vdev_stat_lock
);
3468 vdev_clear_stats(vdev_t
*vd
)
3470 mutex_enter(&vd
->vdev_stat_lock
);
3471 vd
->vdev_stat
.vs_space
= 0;
3472 vd
->vdev_stat
.vs_dspace
= 0;
3473 vd
->vdev_stat
.vs_alloc
= 0;
3474 mutex_exit(&vd
->vdev_stat_lock
);
3478 vdev_scan_stat_init(vdev_t
*vd
)
3480 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3482 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3483 vdev_scan_stat_init(vd
->vdev_child
[c
]);
3485 mutex_enter(&vd
->vdev_stat_lock
);
3486 vs
->vs_scan_processed
= 0;
3487 mutex_exit(&vd
->vdev_stat_lock
);
3491 vdev_stat_update(zio_t
*zio
, uint64_t psize
)
3493 spa_t
*spa
= zio
->io_spa
;
3494 vdev_t
*rvd
= spa
->spa_root_vdev
;
3495 vdev_t
*vd
= zio
->io_vd
? zio
->io_vd
: rvd
;
3497 uint64_t txg
= zio
->io_txg
;
3498 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3499 zio_type_t type
= zio
->io_type
;
3500 int flags
= zio
->io_flags
;
3503 * If this i/o is a gang leader, it didn't do any actual work.
3505 if (zio
->io_gang_tree
)
3508 if (zio
->io_error
== 0) {
3510 * If this is a root i/o, don't count it -- we've already
3511 * counted the top-level vdevs, and vdev_get_stats() will
3512 * aggregate them when asked. This reduces contention on
3513 * the root vdev_stat_lock and implicitly handles blocks
3514 * that compress away to holes, for which there is no i/o.
3515 * (Holes never create vdev children, so all the counters
3516 * remain zero, which is what we want.)
3518 * Note: this only applies to successful i/o (io_error == 0)
3519 * because unlike i/o counts, errors are not additive.
3520 * When reading a ditto block, for example, failure of
3521 * one top-level vdev does not imply a root-level error.
3526 ASSERT(vd
== zio
->io_vd
);
3528 if (flags
& ZIO_FLAG_IO_BYPASS
)
3531 mutex_enter(&vd
->vdev_stat_lock
);
3533 if (flags
& ZIO_FLAG_IO_REPAIR
) {
3534 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
3535 dsl_scan_phys_t
*scn_phys
=
3536 &spa
->spa_dsl_pool
->dp_scan
->scn_phys
;
3537 uint64_t *processed
= &scn_phys
->scn_processed
;
3540 if (vd
->vdev_ops
->vdev_op_leaf
)
3541 atomic_add_64(processed
, psize
);
3542 vs
->vs_scan_processed
+= psize
;
3545 if (flags
& ZIO_FLAG_SELF_HEAL
)
3546 vs
->vs_self_healed
+= psize
;
3550 vs
->vs_bytes
[type
] += psize
;
3552 mutex_exit(&vd
->vdev_stat_lock
);
3556 if (flags
& ZIO_FLAG_SPECULATIVE
)
3560 * If this is an I/O error that is going to be retried, then ignore the
3561 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
3562 * hard errors, when in reality they can happen for any number of
3563 * innocuous reasons (bus resets, MPxIO link failure, etc).
3565 if (zio
->io_error
== EIO
&&
3566 !(zio
->io_flags
& ZIO_FLAG_IO_RETRY
))
3570 * Intent logs writes won't propagate their error to the root
3571 * I/O so don't mark these types of failures as pool-level
3574 if (zio
->io_vd
== NULL
&& (zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
3577 mutex_enter(&vd
->vdev_stat_lock
);
3578 if (type
== ZIO_TYPE_READ
&& !vdev_is_dead(vd
)) {
3579 if (zio
->io_error
== ECKSUM
)
3580 vs
->vs_checksum_errors
++;
3582 vs
->vs_read_errors
++;
3584 if (type
== ZIO_TYPE_WRITE
&& !vdev_is_dead(vd
))
3585 vs
->vs_write_errors
++;
3586 mutex_exit(&vd
->vdev_stat_lock
);
3588 if (spa
->spa_load_state
== SPA_LOAD_NONE
&&
3589 type
== ZIO_TYPE_WRITE
&& txg
!= 0 &&
3590 (!(flags
& ZIO_FLAG_IO_REPAIR
) ||
3591 (flags
& ZIO_FLAG_SCAN_THREAD
) ||
3592 spa
->spa_claiming
)) {
3594 * This is either a normal write (not a repair), or it's
3595 * a repair induced by the scrub thread, or it's a repair
3596 * made by zil_claim() during spa_load() in the first txg.
3597 * In the normal case, we commit the DTL change in the same
3598 * txg as the block was born. In the scrub-induced repair
3599 * case, we know that scrubs run in first-pass syncing context,
3600 * so we commit the DTL change in spa_syncing_txg(spa).
3601 * In the zil_claim() case, we commit in spa_first_txg(spa).
3603 * We currently do not make DTL entries for failed spontaneous
3604 * self-healing writes triggered by normal (non-scrubbing)
3605 * reads, because we have no transactional context in which to
3606 * do so -- and it's not clear that it'd be desirable anyway.
3608 if (vd
->vdev_ops
->vdev_op_leaf
) {
3609 uint64_t commit_txg
= txg
;
3610 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
3611 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
3612 ASSERT(spa_sync_pass(spa
) == 1);
3613 vdev_dtl_dirty(vd
, DTL_SCRUB
, txg
, 1);
3614 commit_txg
= spa_syncing_txg(spa
);
3615 } else if (spa
->spa_claiming
) {
3616 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
3617 commit_txg
= spa_first_txg(spa
);
3619 ASSERT(commit_txg
>= spa_syncing_txg(spa
));
3620 if (vdev_dtl_contains(vd
, DTL_MISSING
, txg
, 1))
3622 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3623 vdev_dtl_dirty(pvd
, DTL_PARTIAL
, txg
, 1);
3624 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, commit_txg
);
3627 vdev_dtl_dirty(vd
, DTL_MISSING
, txg
, 1);
3632 * Update the in-core space usage stats for this vdev, its metaslab class,
3633 * and the root vdev.
3636 vdev_space_update(vdev_t
*vd
, int64_t alloc_delta
, int64_t defer_delta
,
3637 int64_t space_delta
)
3639 int64_t dspace_delta
= space_delta
;
3640 spa_t
*spa
= vd
->vdev_spa
;
3641 vdev_t
*rvd
= spa
->spa_root_vdev
;
3642 metaslab_group_t
*mg
= vd
->vdev_mg
;
3643 metaslab_class_t
*mc
= mg
? mg
->mg_class
: NULL
;
3645 ASSERT(vd
== vd
->vdev_top
);
3648 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
3649 * factor. We must calculate this here and not at the root vdev
3650 * because the root vdev's psize-to-asize is simply the max of its
3651 * childrens', thus not accurate enough for us.
3653 ASSERT((dspace_delta
& (SPA_MINBLOCKSIZE
-1)) == 0);
3654 ASSERT(vd
->vdev_deflate_ratio
!= 0 || vd
->vdev_isl2cache
);
3655 dspace_delta
= (dspace_delta
>> SPA_MINBLOCKSHIFT
) *
3656 vd
->vdev_deflate_ratio
;
3658 mutex_enter(&vd
->vdev_stat_lock
);
3659 vd
->vdev_stat
.vs_alloc
+= alloc_delta
;
3660 vd
->vdev_stat
.vs_space
+= space_delta
;
3661 vd
->vdev_stat
.vs_dspace
+= dspace_delta
;
3662 mutex_exit(&vd
->vdev_stat_lock
);
3664 if (mc
== spa_normal_class(spa
)) {
3665 mutex_enter(&rvd
->vdev_stat_lock
);
3666 rvd
->vdev_stat
.vs_alloc
+= alloc_delta
;
3667 rvd
->vdev_stat
.vs_space
+= space_delta
;
3668 rvd
->vdev_stat
.vs_dspace
+= dspace_delta
;
3669 mutex_exit(&rvd
->vdev_stat_lock
);
3673 ASSERT(rvd
== vd
->vdev_parent
);
3674 ASSERT(vd
->vdev_ms_count
!= 0);
3676 metaslab_class_space_update(mc
,
3677 alloc_delta
, defer_delta
, space_delta
, dspace_delta
);
3682 * Mark a top-level vdev's config as dirty, placing it on the dirty list
3683 * so that it will be written out next time the vdev configuration is synced.
3684 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3687 vdev_config_dirty(vdev_t
*vd
)
3689 spa_t
*spa
= vd
->vdev_spa
;
3690 vdev_t
*rvd
= spa
->spa_root_vdev
;
3693 ASSERT(spa_writeable(spa
));
3696 * If this is an aux vdev (as with l2cache and spare devices), then we
3697 * update the vdev config manually and set the sync flag.
3699 if (vd
->vdev_aux
!= NULL
) {
3700 spa_aux_vdev_t
*sav
= vd
->vdev_aux
;
3704 for (c
= 0; c
< sav
->sav_count
; c
++) {
3705 if (sav
->sav_vdevs
[c
] == vd
)
3709 if (c
== sav
->sav_count
) {
3711 * We're being removed. There's nothing more to do.
3713 ASSERT(sav
->sav_sync
== B_TRUE
);
3717 sav
->sav_sync
= B_TRUE
;
3719 if (nvlist_lookup_nvlist_array(sav
->sav_config
,
3720 ZPOOL_CONFIG_L2CACHE
, &aux
, &naux
) != 0) {
3721 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
3722 ZPOOL_CONFIG_SPARES
, &aux
, &naux
) == 0);
3728 * Setting the nvlist in the middle if the array is a little
3729 * sketchy, but it will work.
3731 nvlist_free(aux
[c
]);
3732 aux
[c
] = vdev_config_generate(spa
, vd
, B_TRUE
, 0);
3738 * The dirty list is protected by the SCL_CONFIG lock. The caller
3739 * must either hold SCL_CONFIG as writer, or must be the sync thread
3740 * (which holds SCL_CONFIG as reader). There's only one sync thread,
3741 * so this is sufficient to ensure mutual exclusion.
3743 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
3744 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3745 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
3748 for (c
= 0; c
< rvd
->vdev_children
; c
++)
3749 vdev_config_dirty(rvd
->vdev_child
[c
]);
3751 ASSERT(vd
== vd
->vdev_top
);
3753 if (!list_link_active(&vd
->vdev_config_dirty_node
) &&
3754 vdev_is_concrete(vd
)) {
3755 list_insert_head(&spa
->spa_config_dirty_list
, vd
);
3761 vdev_config_clean(vdev_t
*vd
)
3763 spa_t
*spa
= vd
->vdev_spa
;
3765 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
3766 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3767 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
3769 ASSERT(list_link_active(&vd
->vdev_config_dirty_node
));
3770 list_remove(&spa
->spa_config_dirty_list
, vd
);
3774 * Mark a top-level vdev's state as dirty, so that the next pass of
3775 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3776 * the state changes from larger config changes because they require
3777 * much less locking, and are often needed for administrative actions.
3780 vdev_state_dirty(vdev_t
*vd
)
3782 spa_t
*spa
= vd
->vdev_spa
;
3784 ASSERT(spa_writeable(spa
));
3785 ASSERT(vd
== vd
->vdev_top
);
3788 * The state list is protected by the SCL_STATE lock. The caller
3789 * must either hold SCL_STATE as writer, or must be the sync thread
3790 * (which holds SCL_STATE as reader). There's only one sync thread,
3791 * so this is sufficient to ensure mutual exclusion.
3793 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
3794 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3795 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
3797 if (!list_link_active(&vd
->vdev_state_dirty_node
) &&
3798 vdev_is_concrete(vd
))
3799 list_insert_head(&spa
->spa_state_dirty_list
, vd
);
3803 vdev_state_clean(vdev_t
*vd
)
3805 spa_t
*spa
= vd
->vdev_spa
;
3807 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
3808 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3809 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
3811 ASSERT(list_link_active(&vd
->vdev_state_dirty_node
));
3812 list_remove(&spa
->spa_state_dirty_list
, vd
);
3816 * Propagate vdev state up from children to parent.
3819 vdev_propagate_state(vdev_t
*vd
)
3821 spa_t
*spa
= vd
->vdev_spa
;
3822 vdev_t
*rvd
= spa
->spa_root_vdev
;
3823 int degraded
= 0, faulted
= 0;
3827 if (vd
->vdev_children
> 0) {
3828 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3829 child
= vd
->vdev_child
[c
];
3832 * Don't factor holes or indirect vdevs into the
3835 if (!vdev_is_concrete(child
))
3838 if (!vdev_readable(child
) ||
3839 (!vdev_writeable(child
) && spa_writeable(spa
))) {
3841 * Root special: if there is a top-level log
3842 * device, treat the root vdev as if it were
3845 if (child
->vdev_islog
&& vd
== rvd
)
3849 } else if (child
->vdev_state
<= VDEV_STATE_DEGRADED
) {
3853 if (child
->vdev_stat
.vs_aux
== VDEV_AUX_CORRUPT_DATA
)
3857 vd
->vdev_ops
->vdev_op_state_change(vd
, faulted
, degraded
);
3860 * Root special: if there is a top-level vdev that cannot be
3861 * opened due to corrupted metadata, then propagate the root
3862 * vdev's aux state as 'corrupt' rather than 'insufficient
3865 if (corrupted
&& vd
== rvd
&&
3866 rvd
->vdev_state
== VDEV_STATE_CANT_OPEN
)
3867 vdev_set_state(rvd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3868 VDEV_AUX_CORRUPT_DATA
);
3871 if (vd
->vdev_parent
)
3872 vdev_propagate_state(vd
->vdev_parent
);
3876 * Set a vdev's state. If this is during an open, we don't update the parent
3877 * state, because we're in the process of opening children depth-first.
3878 * Otherwise, we propagate the change to the parent.
3880 * If this routine places a device in a faulted state, an appropriate ereport is
3884 vdev_set_state(vdev_t
*vd
, boolean_t isopen
, vdev_state_t state
, vdev_aux_t aux
)
3886 uint64_t save_state
;
3887 spa_t
*spa
= vd
->vdev_spa
;
3889 if (state
== vd
->vdev_state
) {
3890 vd
->vdev_stat
.vs_aux
= aux
;
3894 save_state
= vd
->vdev_state
;
3896 vd
->vdev_state
= state
;
3897 vd
->vdev_stat
.vs_aux
= aux
;
3900 * If we are setting the vdev state to anything but an open state, then
3901 * always close the underlying device unless the device has requested
3902 * a delayed close (i.e. we're about to remove or fault the device).
3903 * Otherwise, we keep accessible but invalid devices open forever.
3904 * We don't call vdev_close() itself, because that implies some extra
3905 * checks (offline, etc) that we don't want here. This is limited to
3906 * leaf devices, because otherwise closing the device will affect other
3909 if (!vd
->vdev_delayed_close
&& vdev_is_dead(vd
) &&
3910 vd
->vdev_ops
->vdev_op_leaf
)
3911 vd
->vdev_ops
->vdev_op_close(vd
);
3914 * If we have brought this vdev back into service, we need
3915 * to notify fmd so that it can gracefully repair any outstanding
3916 * cases due to a missing device. We do this in all cases, even those
3917 * that probably don't correlate to a repaired fault. This is sure to
3918 * catch all cases, and we let the zfs-retire agent sort it out. If
3919 * this is a transient state it's OK, as the retire agent will
3920 * double-check the state of the vdev before repairing it.
3922 if (state
== VDEV_STATE_HEALTHY
&& vd
->vdev_ops
->vdev_op_leaf
&&
3923 vd
->vdev_prevstate
!= state
)
3924 zfs_post_state_change(spa
, vd
);
3926 if (vd
->vdev_removed
&&
3927 state
== VDEV_STATE_CANT_OPEN
&&
3928 (aux
== VDEV_AUX_OPEN_FAILED
|| vd
->vdev_checkremove
)) {
3930 * If the previous state is set to VDEV_STATE_REMOVED, then this
3931 * device was previously marked removed and someone attempted to
3932 * reopen it. If this failed due to a nonexistent device, then
3933 * keep the device in the REMOVED state. We also let this be if
3934 * it is one of our special test online cases, which is only
3935 * attempting to online the device and shouldn't generate an FMA
3938 vd
->vdev_state
= VDEV_STATE_REMOVED
;
3939 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
3940 } else if (state
== VDEV_STATE_REMOVED
) {
3941 vd
->vdev_removed
= B_TRUE
;
3942 } else if (state
== VDEV_STATE_CANT_OPEN
) {
3944 * If we fail to open a vdev during an import or recovery, we
3945 * mark it as "not available", which signifies that it was
3946 * never there to begin with. Failure to open such a device
3947 * is not considered an error.
3949 if ((spa_load_state(spa
) == SPA_LOAD_IMPORT
||
3950 spa_load_state(spa
) == SPA_LOAD_RECOVER
) &&
3951 vd
->vdev_ops
->vdev_op_leaf
)
3952 vd
->vdev_not_present
= 1;
3955 * Post the appropriate ereport. If the 'prevstate' field is
3956 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3957 * that this is part of a vdev_reopen(). In this case, we don't
3958 * want to post the ereport if the device was already in the
3959 * CANT_OPEN state beforehand.
3961 * If the 'checkremove' flag is set, then this is an attempt to
3962 * online the device in response to an insertion event. If we
3963 * hit this case, then we have detected an insertion event for a
3964 * faulted or offline device that wasn't in the removed state.
3965 * In this scenario, we don't post an ereport because we are
3966 * about to replace the device, or attempt an online with
3967 * vdev_forcefault, which will generate the fault for us.
3969 if ((vd
->vdev_prevstate
!= state
|| vd
->vdev_forcefault
) &&
3970 !vd
->vdev_not_present
&& !vd
->vdev_checkremove
&&
3971 vd
!= spa
->spa_root_vdev
) {
3975 case VDEV_AUX_OPEN_FAILED
:
3976 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED
;
3978 case VDEV_AUX_CORRUPT_DATA
:
3979 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA
;
3981 case VDEV_AUX_NO_REPLICAS
:
3982 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS
;
3984 case VDEV_AUX_BAD_GUID_SUM
:
3985 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM
;
3987 case VDEV_AUX_TOO_SMALL
:
3988 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL
;
3990 case VDEV_AUX_BAD_LABEL
:
3991 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL
;
3994 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN
;
3997 zfs_ereport_post(class, spa
, vd
, NULL
, save_state
, 0);
4000 /* Erase any notion of persistent removed state */
4001 vd
->vdev_removed
= B_FALSE
;
4003 vd
->vdev_removed
= B_FALSE
;
4006 if (!isopen
&& vd
->vdev_parent
)
4007 vdev_propagate_state(vd
->vdev_parent
);
4011 vdev_children_are_offline(vdev_t
*vd
)
4013 ASSERT(!vd
->vdev_ops
->vdev_op_leaf
);
4015 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
4016 if (vd
->vdev_child
[i
]->vdev_state
!= VDEV_STATE_OFFLINE
)
4024 * Check the vdev configuration to ensure that it's capable of supporting
4025 * a root pool. We do not support partial configuration.
4026 * In addition, only a single top-level vdev is allowed.
4029 vdev_is_bootable(vdev_t
*vd
)
4031 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4032 char *vdev_type
= vd
->vdev_ops
->vdev_op_type
;
4034 if (strcmp(vdev_type
, VDEV_TYPE_ROOT
) == 0 &&
4035 vd
->vdev_children
> 1) {
4037 } else if (strcmp(vdev_type
, VDEV_TYPE_MISSING
) == 0 ||
4038 strcmp(vdev_type
, VDEV_TYPE_INDIRECT
) == 0) {
4043 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4044 if (!vdev_is_bootable(vd
->vdev_child
[c
]))
4051 vdev_is_concrete(vdev_t
*vd
)
4053 vdev_ops_t
*ops
= vd
->vdev_ops
;
4054 if (ops
== &vdev_indirect_ops
|| ops
== &vdev_hole_ops
||
4055 ops
== &vdev_missing_ops
|| ops
== &vdev_root_ops
) {
4063 * Determine if a log device has valid content. If the vdev was
4064 * removed or faulted in the MOS config then we know that
4065 * the content on the log device has already been written to the pool.
4068 vdev_log_state_valid(vdev_t
*vd
)
4070 if (vd
->vdev_ops
->vdev_op_leaf
&& !vd
->vdev_faulted
&&
4074 for (int c
= 0; c
< vd
->vdev_children
; c
++)
4075 if (vdev_log_state_valid(vd
->vdev_child
[c
]))
4082 * Expand a vdev if possible.
4085 vdev_expand(vdev_t
*vd
, uint64_t txg
)
4087 ASSERT(vd
->vdev_top
== vd
);
4088 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4090 vdev_set_deflate_ratio(vd
);
4092 if ((vd
->vdev_asize
>> vd
->vdev_ms_shift
) > vd
->vdev_ms_count
&&
4093 vdev_is_concrete(vd
)) {
4094 VERIFY(vdev_metaslab_init(vd
, txg
) == 0);
4095 vdev_config_dirty(vd
);
4103 vdev_split(vdev_t
*vd
)
4105 vdev_t
*cvd
, *pvd
= vd
->vdev_parent
;
4107 vdev_remove_child(pvd
, vd
);
4108 vdev_compact_children(pvd
);
4110 cvd
= pvd
->vdev_child
[0];
4111 if (pvd
->vdev_children
== 1) {
4112 vdev_remove_parent(cvd
);
4113 cvd
->vdev_splitting
= B_TRUE
;
4115 vdev_propagate_state(cvd
);
4119 vdev_deadman(vdev_t
*vd
)
4121 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4122 vdev_t
*cvd
= vd
->vdev_child
[c
];
4127 if (vd
->vdev_ops
->vdev_op_leaf
) {
4128 vdev_queue_t
*vq
= &vd
->vdev_queue
;
4130 mutex_enter(&vq
->vq_lock
);
4131 if (avl_numnodes(&vq
->vq_active_tree
) > 0) {
4132 spa_t
*spa
= vd
->vdev_spa
;
4137 * Look at the head of all the pending queues,
4138 * if any I/O has been outstanding for longer than
4139 * the spa_deadman_synctime we panic the system.
4141 fio
= avl_first(&vq
->vq_active_tree
);
4142 delta
= gethrtime() - fio
->io_timestamp
;
4143 if (delta
> spa_deadman_synctime(spa
)) {
4144 vdev_dbgmsg(vd
, "SLOW IO: zio timestamp "
4145 "%lluns, delta %lluns, last io %lluns",
4146 fio
->io_timestamp
, (u_longlong_t
)delta
,
4147 vq
->vq_io_complete_ts
);
4148 fm_panic("I/O to pool '%s' appears to be "
4149 "hung.", spa_name(spa
));
4152 mutex_exit(&vq
->vq_lock
);