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, 2015 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>
53 #include <sys/zfs_ratelimit.h>
56 * When a vdev is added, it will be divided into approximately (but no
57 * more than) this number of metaslabs.
59 int metaslabs_per_vdev
= 200;
62 * Rate limit delay events to this many IO delays per second.
64 unsigned int zfs_delays_per_second
= 20;
67 * Rate limit checksum events after this many checksum errors per second.
69 unsigned int zfs_checksums_per_second
= 20;
72 * Ignore errors during scrub/resilver. Allows to work around resilver
73 * upon import when there are pool errors.
75 int zfs_scan_ignore_errors
= 0;
77 int vdev_validate_skip
= B_FALSE
;
81 vdev_dbgmsg(vdev_t
*vd
, const char *fmt
, ...)
87 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
90 if (vd
->vdev_path
!= NULL
) {
91 zfs_dbgmsg("%s vdev '%s': %s", vd
->vdev_ops
->vdev_op_type
,
94 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
95 vd
->vdev_ops
->vdev_op_type
,
96 (u_longlong_t
)vd
->vdev_id
,
97 (u_longlong_t
)vd
->vdev_guid
, buf
);
102 vdev_dbgmsg_print_tree(vdev_t
*vd
, int indent
)
106 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
) {
107 zfs_dbgmsg("%*svdev %u: %s", indent
, "", vd
->vdev_id
,
108 vd
->vdev_ops
->vdev_op_type
);
112 switch (vd
->vdev_state
) {
113 case VDEV_STATE_UNKNOWN
:
114 (void) snprintf(state
, sizeof (state
), "unknown");
116 case VDEV_STATE_CLOSED
:
117 (void) snprintf(state
, sizeof (state
), "closed");
119 case VDEV_STATE_OFFLINE
:
120 (void) snprintf(state
, sizeof (state
), "offline");
122 case VDEV_STATE_REMOVED
:
123 (void) snprintf(state
, sizeof (state
), "removed");
125 case VDEV_STATE_CANT_OPEN
:
126 (void) snprintf(state
, sizeof (state
), "can't open");
128 case VDEV_STATE_FAULTED
:
129 (void) snprintf(state
, sizeof (state
), "faulted");
131 case VDEV_STATE_DEGRADED
:
132 (void) snprintf(state
, sizeof (state
), "degraded");
134 case VDEV_STATE_HEALTHY
:
135 (void) snprintf(state
, sizeof (state
), "healthy");
138 (void) snprintf(state
, sizeof (state
), "<state %u>",
139 (uint_t
)vd
->vdev_state
);
142 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent
,
143 "", vd
->vdev_id
, vd
->vdev_ops
->vdev_op_type
,
144 vd
->vdev_islog
? " (log)" : "",
145 (u_longlong_t
)vd
->vdev_guid
,
146 vd
->vdev_path
? vd
->vdev_path
: "N/A", state
);
148 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++)
149 vdev_dbgmsg_print_tree(vd
->vdev_child
[i
], indent
+ 2);
153 * Virtual device management.
156 static vdev_ops_t
*vdev_ops_table
[] = {
171 * Given a vdev type, return the appropriate ops vector.
174 vdev_getops(const char *type
)
176 vdev_ops_t
*ops
, **opspp
;
178 for (opspp
= vdev_ops_table
; (ops
= *opspp
) != NULL
; opspp
++)
179 if (strcmp(ops
->vdev_op_type
, type
) == 0)
186 * Default asize function: return the MAX of psize with the asize of
187 * all children. This is what's used by anything other than RAID-Z.
190 vdev_default_asize(vdev_t
*vd
, uint64_t psize
)
192 uint64_t asize
= P2ROUNDUP(psize
, 1ULL << vd
->vdev_top
->vdev_ashift
);
195 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
196 csize
= vdev_psize_to_asize(vd
->vdev_child
[c
], psize
);
197 asize
= MAX(asize
, csize
);
204 * Get the minimum allocatable size. We define the allocatable size as
205 * the vdev's asize rounded to the nearest metaslab. This allows us to
206 * replace or attach devices which don't have the same physical size but
207 * can still satisfy the same number of allocations.
210 vdev_get_min_asize(vdev_t
*vd
)
212 vdev_t
*pvd
= vd
->vdev_parent
;
215 * If our parent is NULL (inactive spare or cache) or is the root,
216 * just return our own asize.
219 return (vd
->vdev_asize
);
222 * The top-level vdev just returns the allocatable size rounded
223 * to the nearest metaslab.
225 if (vd
== vd
->vdev_top
)
226 return (P2ALIGN(vd
->vdev_asize
, 1ULL << vd
->vdev_ms_shift
));
229 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
230 * so each child must provide at least 1/Nth of its asize.
232 if (pvd
->vdev_ops
== &vdev_raidz_ops
)
233 return ((pvd
->vdev_min_asize
+ pvd
->vdev_children
- 1) /
236 return (pvd
->vdev_min_asize
);
240 vdev_set_min_asize(vdev_t
*vd
)
242 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
244 for (int c
= 0; c
< vd
->vdev_children
; c
++)
245 vdev_set_min_asize(vd
->vdev_child
[c
]);
249 vdev_lookup_top(spa_t
*spa
, uint64_t vdev
)
251 vdev_t
*rvd
= spa
->spa_root_vdev
;
253 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
255 if (vdev
< rvd
->vdev_children
) {
256 ASSERT(rvd
->vdev_child
[vdev
] != NULL
);
257 return (rvd
->vdev_child
[vdev
]);
264 vdev_lookup_by_guid(vdev_t
*vd
, uint64_t guid
)
268 if (vd
->vdev_guid
== guid
)
271 for (int c
= 0; c
< vd
->vdev_children
; c
++)
272 if ((mvd
= vdev_lookup_by_guid(vd
->vdev_child
[c
], guid
)) !=
280 vdev_count_leaves_impl(vdev_t
*vd
)
284 if (vd
->vdev_ops
->vdev_op_leaf
)
287 for (int c
= 0; c
< vd
->vdev_children
; c
++)
288 n
+= vdev_count_leaves_impl(vd
->vdev_child
[c
]);
294 vdev_count_leaves(spa_t
*spa
)
298 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
299 rc
= vdev_count_leaves_impl(spa
->spa_root_vdev
);
300 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
306 vdev_add_child(vdev_t
*pvd
, vdev_t
*cvd
)
308 size_t oldsize
, newsize
;
309 uint64_t id
= cvd
->vdev_id
;
312 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
313 ASSERT(cvd
->vdev_parent
== NULL
);
315 cvd
->vdev_parent
= pvd
;
320 ASSERT(id
>= pvd
->vdev_children
|| pvd
->vdev_child
[id
] == NULL
);
322 oldsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
323 pvd
->vdev_children
= MAX(pvd
->vdev_children
, id
+ 1);
324 newsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
326 newchild
= kmem_alloc(newsize
, KM_SLEEP
);
327 if (pvd
->vdev_child
!= NULL
) {
328 bcopy(pvd
->vdev_child
, newchild
, oldsize
);
329 kmem_free(pvd
->vdev_child
, oldsize
);
332 pvd
->vdev_child
= newchild
;
333 pvd
->vdev_child
[id
] = cvd
;
335 cvd
->vdev_top
= (pvd
->vdev_top
? pvd
->vdev_top
: cvd
);
336 ASSERT(cvd
->vdev_top
->vdev_parent
->vdev_parent
== NULL
);
339 * Walk up all ancestors to update guid sum.
341 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
342 pvd
->vdev_guid_sum
+= cvd
->vdev_guid_sum
;
346 vdev_remove_child(vdev_t
*pvd
, vdev_t
*cvd
)
349 uint_t id
= cvd
->vdev_id
;
351 ASSERT(cvd
->vdev_parent
== pvd
);
356 ASSERT(id
< pvd
->vdev_children
);
357 ASSERT(pvd
->vdev_child
[id
] == cvd
);
359 pvd
->vdev_child
[id
] = NULL
;
360 cvd
->vdev_parent
= NULL
;
362 for (c
= 0; c
< pvd
->vdev_children
; c
++)
363 if (pvd
->vdev_child
[c
])
366 if (c
== pvd
->vdev_children
) {
367 kmem_free(pvd
->vdev_child
, c
* sizeof (vdev_t
*));
368 pvd
->vdev_child
= NULL
;
369 pvd
->vdev_children
= 0;
373 * Walk up all ancestors to update guid sum.
375 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
376 pvd
->vdev_guid_sum
-= cvd
->vdev_guid_sum
;
380 * Remove any holes in the child array.
383 vdev_compact_children(vdev_t
*pvd
)
385 vdev_t
**newchild
, *cvd
;
386 int oldc
= pvd
->vdev_children
;
389 ASSERT(spa_config_held(pvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
394 for (int c
= newc
= 0; c
< oldc
; c
++)
395 if (pvd
->vdev_child
[c
])
399 newchild
= kmem_zalloc(newc
* sizeof (vdev_t
*), KM_SLEEP
);
401 for (int c
= newc
= 0; c
< oldc
; c
++) {
402 if ((cvd
= pvd
->vdev_child
[c
]) != NULL
) {
403 newchild
[newc
] = cvd
;
404 cvd
->vdev_id
= newc
++;
411 kmem_free(pvd
->vdev_child
, oldc
* sizeof (vdev_t
*));
412 pvd
->vdev_child
= newchild
;
413 pvd
->vdev_children
= newc
;
417 * Allocate and minimally initialize a vdev_t.
420 vdev_alloc_common(spa_t
*spa
, uint_t id
, uint64_t guid
, vdev_ops_t
*ops
)
423 vdev_indirect_config_t
*vic
;
425 vd
= kmem_zalloc(sizeof (vdev_t
), KM_SLEEP
);
426 vic
= &vd
->vdev_indirect_config
;
428 if (spa
->spa_root_vdev
== NULL
) {
429 ASSERT(ops
== &vdev_root_ops
);
430 spa
->spa_root_vdev
= vd
;
431 spa
->spa_load_guid
= spa_generate_guid(NULL
);
434 if (guid
== 0 && ops
!= &vdev_hole_ops
) {
435 if (spa
->spa_root_vdev
== vd
) {
437 * The root vdev's guid will also be the pool guid,
438 * which must be unique among all pools.
440 guid
= spa_generate_guid(NULL
);
443 * Any other vdev's guid must be unique within the pool.
445 guid
= spa_generate_guid(spa
);
447 ASSERT(!spa_guid_exists(spa_guid(spa
), guid
));
452 vd
->vdev_guid
= guid
;
453 vd
->vdev_guid_sum
= guid
;
455 vd
->vdev_state
= VDEV_STATE_CLOSED
;
456 vd
->vdev_ishole
= (ops
== &vdev_hole_ops
);
457 vic
->vic_prev_indirect_vdev
= UINT64_MAX
;
459 rw_init(&vd
->vdev_indirect_rwlock
, NULL
, RW_DEFAULT
, NULL
);
460 mutex_init(&vd
->vdev_obsolete_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
461 vd
->vdev_obsolete_segments
= range_tree_create(NULL
, NULL
);
464 * Initialize rate limit structs for events. We rate limit ZIO delay
465 * and checksum events so that we don't overwhelm ZED with thousands
466 * of events when a disk is acting up.
468 zfs_ratelimit_init(&vd
->vdev_delay_rl
, &zfs_delays_per_second
, 1);
469 zfs_ratelimit_init(&vd
->vdev_checksum_rl
, &zfs_checksums_per_second
, 1);
471 list_link_init(&vd
->vdev_config_dirty_node
);
472 list_link_init(&vd
->vdev_state_dirty_node
);
473 mutex_init(&vd
->vdev_dtl_lock
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
474 mutex_init(&vd
->vdev_stat_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
475 mutex_init(&vd
->vdev_probe_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
476 mutex_init(&vd
->vdev_queue_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
477 mutex_init(&vd
->vdev_scan_io_queue_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
479 for (int t
= 0; t
< DTL_TYPES
; t
++) {
480 vd
->vdev_dtl
[t
] = range_tree_create(NULL
, NULL
);
482 txg_list_create(&vd
->vdev_ms_list
, spa
,
483 offsetof(struct metaslab
, ms_txg_node
));
484 txg_list_create(&vd
->vdev_dtl_list
, spa
,
485 offsetof(struct vdev
, vdev_dtl_node
));
486 vd
->vdev_stat
.vs_timestamp
= gethrtime();
494 * Allocate a new vdev. The 'alloctype' is used to control whether we are
495 * creating a new vdev or loading an existing one - the behavior is slightly
496 * different for each case.
499 vdev_alloc(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
, uint_t id
,
504 uint64_t guid
= 0, islog
, nparity
;
506 vdev_indirect_config_t
*vic
;
510 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
512 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
513 return (SET_ERROR(EINVAL
));
515 if ((ops
= vdev_getops(type
)) == NULL
)
516 return (SET_ERROR(EINVAL
));
519 * If this is a load, get the vdev guid from the nvlist.
520 * Otherwise, vdev_alloc_common() will generate one for us.
522 if (alloctype
== VDEV_ALLOC_LOAD
) {
525 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
, &label_id
) ||
527 return (SET_ERROR(EINVAL
));
529 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
530 return (SET_ERROR(EINVAL
));
531 } else if (alloctype
== VDEV_ALLOC_SPARE
) {
532 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
533 return (SET_ERROR(EINVAL
));
534 } else if (alloctype
== VDEV_ALLOC_L2CACHE
) {
535 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
536 return (SET_ERROR(EINVAL
));
537 } else if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
538 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
539 return (SET_ERROR(EINVAL
));
543 * The first allocated vdev must be of type 'root'.
545 if (ops
!= &vdev_root_ops
&& spa
->spa_root_vdev
== NULL
)
546 return (SET_ERROR(EINVAL
));
549 * Determine whether we're a log vdev.
552 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &islog
);
553 if (islog
&& spa_version(spa
) < SPA_VERSION_SLOGS
)
554 return (SET_ERROR(ENOTSUP
));
556 if (ops
== &vdev_hole_ops
&& spa_version(spa
) < SPA_VERSION_HOLES
)
557 return (SET_ERROR(ENOTSUP
));
560 * Set the nparity property for RAID-Z vdevs.
563 if (ops
== &vdev_raidz_ops
) {
564 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
566 if (nparity
== 0 || nparity
> VDEV_RAIDZ_MAXPARITY
)
567 return (SET_ERROR(EINVAL
));
569 * Previous versions could only support 1 or 2 parity
573 spa_version(spa
) < SPA_VERSION_RAIDZ2
)
574 return (SET_ERROR(ENOTSUP
));
576 spa_version(spa
) < SPA_VERSION_RAIDZ3
)
577 return (SET_ERROR(ENOTSUP
));
580 * We require the parity to be specified for SPAs that
581 * support multiple parity levels.
583 if (spa_version(spa
) >= SPA_VERSION_RAIDZ2
)
584 return (SET_ERROR(EINVAL
));
586 * Otherwise, we default to 1 parity device for RAID-Z.
593 ASSERT(nparity
!= -1ULL);
595 vd
= vdev_alloc_common(spa
, id
, guid
, ops
);
596 vic
= &vd
->vdev_indirect_config
;
598 vd
->vdev_islog
= islog
;
599 vd
->vdev_nparity
= nparity
;
601 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &vd
->vdev_path
) == 0)
602 vd
->vdev_path
= spa_strdup(vd
->vdev_path
);
605 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
606 * fault on a vdev and want it to persist across imports (like with
609 rc
= nvlist_lookup_string(nv
, ZPOOL_CONFIG_AUX_STATE
, &tmp
);
610 if (rc
== 0 && tmp
!= NULL
&& strcmp(tmp
, "external") == 0) {
611 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
612 vd
->vdev_faulted
= 1;
613 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
616 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &vd
->vdev_devid
) == 0)
617 vd
->vdev_devid
= spa_strdup(vd
->vdev_devid
);
618 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PHYS_PATH
,
619 &vd
->vdev_physpath
) == 0)
620 vd
->vdev_physpath
= spa_strdup(vd
->vdev_physpath
);
622 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH
,
623 &vd
->vdev_enc_sysfs_path
) == 0)
624 vd
->vdev_enc_sysfs_path
= spa_strdup(vd
->vdev_enc_sysfs_path
);
626 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_FRU
, &vd
->vdev_fru
) == 0)
627 vd
->vdev_fru
= spa_strdup(vd
->vdev_fru
);
630 * Set the whole_disk property. If it's not specified, leave the value
633 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
634 &vd
->vdev_wholedisk
) != 0)
635 vd
->vdev_wholedisk
= -1ULL;
637 ASSERT0(vic
->vic_mapping_object
);
638 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_OBJECT
,
639 &vic
->vic_mapping_object
);
640 ASSERT0(vic
->vic_births_object
);
641 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_BIRTHS
,
642 &vic
->vic_births_object
);
643 ASSERT3U(vic
->vic_prev_indirect_vdev
, ==, UINT64_MAX
);
644 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_PREV_INDIRECT_VDEV
,
645 &vic
->vic_prev_indirect_vdev
);
648 * Look for the 'not present' flag. This will only be set if the device
649 * was not present at the time of import.
651 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
652 &vd
->vdev_not_present
);
655 * Get the alignment requirement.
657 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASHIFT
, &vd
->vdev_ashift
);
660 * Retrieve the vdev creation time.
662 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_CREATE_TXG
,
666 * If we're a top-level vdev, try to load the allocation parameters.
668 if (parent
&& !parent
->vdev_parent
&&
669 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
670 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_ARRAY
,
672 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_SHIFT
,
674 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASIZE
,
676 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVING
,
678 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_VDEV_TOP_ZAP
,
681 ASSERT0(vd
->vdev_top_zap
);
684 if (parent
&& !parent
->vdev_parent
&& alloctype
!= VDEV_ALLOC_ATTACH
) {
685 ASSERT(alloctype
== VDEV_ALLOC_LOAD
||
686 alloctype
== VDEV_ALLOC_ADD
||
687 alloctype
== VDEV_ALLOC_SPLIT
||
688 alloctype
== VDEV_ALLOC_ROOTPOOL
);
689 vd
->vdev_mg
= metaslab_group_create(islog
?
690 spa_log_class(spa
) : spa_normal_class(spa
), vd
);
693 if (vd
->vdev_ops
->vdev_op_leaf
&&
694 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
695 (void) nvlist_lookup_uint64(nv
,
696 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, &vd
->vdev_leaf_zap
);
698 ASSERT0(vd
->vdev_leaf_zap
);
702 * If we're a leaf vdev, try to load the DTL object and other state.
705 if (vd
->vdev_ops
->vdev_op_leaf
&&
706 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_L2CACHE
||
707 alloctype
== VDEV_ALLOC_ROOTPOOL
)) {
708 if (alloctype
== VDEV_ALLOC_LOAD
) {
709 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DTL
,
710 &vd
->vdev_dtl_object
);
711 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_UNSPARE
,
715 if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
718 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
719 &spare
) == 0 && spare
)
723 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
,
726 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_RESILVER_TXG
,
727 &vd
->vdev_resilver_txg
);
730 * In general, when importing a pool we want to ignore the
731 * persistent fault state, as the diagnosis made on another
732 * system may not be valid in the current context. The only
733 * exception is if we forced a vdev to a persistently faulted
734 * state with 'zpool offline -f'. The persistent fault will
735 * remain across imports until cleared.
737 * Local vdevs will remain in the faulted state.
739 if (spa_load_state(spa
) == SPA_LOAD_OPEN
||
740 spa_load_state(spa
) == SPA_LOAD_IMPORT
) {
741 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
,
743 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DEGRADED
,
745 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
,
748 if (vd
->vdev_faulted
|| vd
->vdev_degraded
) {
752 VDEV_AUX_ERR_EXCEEDED
;
753 if (nvlist_lookup_string(nv
,
754 ZPOOL_CONFIG_AUX_STATE
, &aux
) == 0 &&
755 strcmp(aux
, "external") == 0)
756 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
758 vd
->vdev_faulted
= 0ULL;
764 * Add ourselves to the parent's list of children.
766 vdev_add_child(parent
, vd
);
774 vdev_free(vdev_t
*vd
)
776 spa_t
*spa
= vd
->vdev_spa
;
779 * Scan queues are normally destroyed at the end of a scan. If the
780 * queue exists here, that implies the vdev is being removed while
781 * the scan is still running.
783 if (vd
->vdev_scan_io_queue
!= NULL
) {
784 mutex_enter(&vd
->vdev_scan_io_queue_lock
);
785 dsl_scan_io_queue_destroy(vd
->vdev_scan_io_queue
);
786 vd
->vdev_scan_io_queue
= NULL
;
787 mutex_exit(&vd
->vdev_scan_io_queue_lock
);
791 * vdev_free() implies closing the vdev first. This is simpler than
792 * trying to ensure complicated semantics for all callers.
796 ASSERT(!list_link_active(&vd
->vdev_config_dirty_node
));
797 ASSERT(!list_link_active(&vd
->vdev_state_dirty_node
));
802 for (int c
= 0; c
< vd
->vdev_children
; c
++)
803 vdev_free(vd
->vdev_child
[c
]);
805 ASSERT(vd
->vdev_child
== NULL
);
806 ASSERT(vd
->vdev_guid_sum
== vd
->vdev_guid
);
809 * Discard allocation state.
811 if (vd
->vdev_mg
!= NULL
) {
812 vdev_metaslab_fini(vd
);
813 metaslab_group_destroy(vd
->vdev_mg
);
816 ASSERT0(vd
->vdev_stat
.vs_space
);
817 ASSERT0(vd
->vdev_stat
.vs_dspace
);
818 ASSERT0(vd
->vdev_stat
.vs_alloc
);
821 * Remove this vdev from its parent's child list.
823 vdev_remove_child(vd
->vdev_parent
, vd
);
825 ASSERT(vd
->vdev_parent
== NULL
);
828 * Clean up vdev structure.
834 spa_strfree(vd
->vdev_path
);
836 spa_strfree(vd
->vdev_devid
);
837 if (vd
->vdev_physpath
)
838 spa_strfree(vd
->vdev_physpath
);
840 if (vd
->vdev_enc_sysfs_path
)
841 spa_strfree(vd
->vdev_enc_sysfs_path
);
844 spa_strfree(vd
->vdev_fru
);
846 if (vd
->vdev_isspare
)
847 spa_spare_remove(vd
);
848 if (vd
->vdev_isl2cache
)
849 spa_l2cache_remove(vd
);
851 txg_list_destroy(&vd
->vdev_ms_list
);
852 txg_list_destroy(&vd
->vdev_dtl_list
);
854 mutex_enter(&vd
->vdev_dtl_lock
);
855 space_map_close(vd
->vdev_dtl_sm
);
856 for (int t
= 0; t
< DTL_TYPES
; t
++) {
857 range_tree_vacate(vd
->vdev_dtl
[t
], NULL
, NULL
);
858 range_tree_destroy(vd
->vdev_dtl
[t
]);
860 mutex_exit(&vd
->vdev_dtl_lock
);
862 EQUIV(vd
->vdev_indirect_births
!= NULL
,
863 vd
->vdev_indirect_mapping
!= NULL
);
864 if (vd
->vdev_indirect_births
!= NULL
) {
865 vdev_indirect_mapping_close(vd
->vdev_indirect_mapping
);
866 vdev_indirect_births_close(vd
->vdev_indirect_births
);
869 if (vd
->vdev_obsolete_sm
!= NULL
) {
870 ASSERT(vd
->vdev_removing
||
871 vd
->vdev_ops
== &vdev_indirect_ops
);
872 space_map_close(vd
->vdev_obsolete_sm
);
873 vd
->vdev_obsolete_sm
= NULL
;
875 range_tree_destroy(vd
->vdev_obsolete_segments
);
876 rw_destroy(&vd
->vdev_indirect_rwlock
);
877 mutex_destroy(&vd
->vdev_obsolete_lock
);
879 mutex_destroy(&vd
->vdev_queue_lock
);
880 mutex_destroy(&vd
->vdev_dtl_lock
);
881 mutex_destroy(&vd
->vdev_stat_lock
);
882 mutex_destroy(&vd
->vdev_probe_lock
);
883 mutex_destroy(&vd
->vdev_scan_io_queue_lock
);
885 zfs_ratelimit_fini(&vd
->vdev_delay_rl
);
886 zfs_ratelimit_fini(&vd
->vdev_checksum_rl
);
888 if (vd
== spa
->spa_root_vdev
)
889 spa
->spa_root_vdev
= NULL
;
891 kmem_free(vd
, sizeof (vdev_t
));
895 * Transfer top-level vdev state from svd to tvd.
898 vdev_top_transfer(vdev_t
*svd
, vdev_t
*tvd
)
900 spa_t
*spa
= svd
->vdev_spa
;
905 ASSERT(tvd
== tvd
->vdev_top
);
907 tvd
->vdev_pending_fastwrite
= svd
->vdev_pending_fastwrite
;
908 tvd
->vdev_ms_array
= svd
->vdev_ms_array
;
909 tvd
->vdev_ms_shift
= svd
->vdev_ms_shift
;
910 tvd
->vdev_ms_count
= svd
->vdev_ms_count
;
911 tvd
->vdev_top_zap
= svd
->vdev_top_zap
;
913 svd
->vdev_ms_array
= 0;
914 svd
->vdev_ms_shift
= 0;
915 svd
->vdev_ms_count
= 0;
916 svd
->vdev_top_zap
= 0;
919 ASSERT3P(tvd
->vdev_mg
, ==, svd
->vdev_mg
);
920 tvd
->vdev_mg
= svd
->vdev_mg
;
921 tvd
->vdev_ms
= svd
->vdev_ms
;
926 if (tvd
->vdev_mg
!= NULL
)
927 tvd
->vdev_mg
->mg_vd
= tvd
;
929 tvd
->vdev_stat
.vs_alloc
= svd
->vdev_stat
.vs_alloc
;
930 tvd
->vdev_stat
.vs_space
= svd
->vdev_stat
.vs_space
;
931 tvd
->vdev_stat
.vs_dspace
= svd
->vdev_stat
.vs_dspace
;
933 svd
->vdev_stat
.vs_alloc
= 0;
934 svd
->vdev_stat
.vs_space
= 0;
935 svd
->vdev_stat
.vs_dspace
= 0;
938 * State which may be set on a top-level vdev that's in the
939 * process of being removed.
941 ASSERT0(tvd
->vdev_indirect_config
.vic_births_object
);
942 ASSERT0(tvd
->vdev_indirect_config
.vic_mapping_object
);
943 ASSERT3U(tvd
->vdev_indirect_config
.vic_prev_indirect_vdev
, ==, -1ULL);
944 ASSERT3P(tvd
->vdev_indirect_mapping
, ==, NULL
);
945 ASSERT3P(tvd
->vdev_indirect_births
, ==, NULL
);
946 ASSERT3P(tvd
->vdev_obsolete_sm
, ==, NULL
);
947 ASSERT0(tvd
->vdev_removing
);
948 tvd
->vdev_removing
= svd
->vdev_removing
;
949 tvd
->vdev_indirect_config
= svd
->vdev_indirect_config
;
950 tvd
->vdev_indirect_mapping
= svd
->vdev_indirect_mapping
;
951 tvd
->vdev_indirect_births
= svd
->vdev_indirect_births
;
952 range_tree_swap(&svd
->vdev_obsolete_segments
,
953 &tvd
->vdev_obsolete_segments
);
954 tvd
->vdev_obsolete_sm
= svd
->vdev_obsolete_sm
;
955 svd
->vdev_indirect_config
.vic_mapping_object
= 0;
956 svd
->vdev_indirect_config
.vic_births_object
= 0;
957 svd
->vdev_indirect_config
.vic_prev_indirect_vdev
= -1ULL;
958 svd
->vdev_indirect_mapping
= NULL
;
959 svd
->vdev_indirect_births
= NULL
;
960 svd
->vdev_obsolete_sm
= NULL
;
961 svd
->vdev_removing
= 0;
963 for (t
= 0; t
< TXG_SIZE
; t
++) {
964 while ((msp
= txg_list_remove(&svd
->vdev_ms_list
, t
)) != NULL
)
965 (void) txg_list_add(&tvd
->vdev_ms_list
, msp
, t
);
966 while ((vd
= txg_list_remove(&svd
->vdev_dtl_list
, t
)) != NULL
)
967 (void) txg_list_add(&tvd
->vdev_dtl_list
, vd
, t
);
968 if (txg_list_remove_this(&spa
->spa_vdev_txg_list
, svd
, t
))
969 (void) txg_list_add(&spa
->spa_vdev_txg_list
, tvd
, t
);
972 if (list_link_active(&svd
->vdev_config_dirty_node
)) {
973 vdev_config_clean(svd
);
974 vdev_config_dirty(tvd
);
977 if (list_link_active(&svd
->vdev_state_dirty_node
)) {
978 vdev_state_clean(svd
);
979 vdev_state_dirty(tvd
);
982 tvd
->vdev_deflate_ratio
= svd
->vdev_deflate_ratio
;
983 svd
->vdev_deflate_ratio
= 0;
985 tvd
->vdev_islog
= svd
->vdev_islog
;
988 dsl_scan_io_queue_vdev_xfer(svd
, tvd
);
992 vdev_top_update(vdev_t
*tvd
, vdev_t
*vd
)
999 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1000 vdev_top_update(tvd
, vd
->vdev_child
[c
]);
1004 * Add a mirror/replacing vdev above an existing vdev.
1007 vdev_add_parent(vdev_t
*cvd
, vdev_ops_t
*ops
)
1009 spa_t
*spa
= cvd
->vdev_spa
;
1010 vdev_t
*pvd
= cvd
->vdev_parent
;
1013 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1015 mvd
= vdev_alloc_common(spa
, cvd
->vdev_id
, 0, ops
);
1017 mvd
->vdev_asize
= cvd
->vdev_asize
;
1018 mvd
->vdev_min_asize
= cvd
->vdev_min_asize
;
1019 mvd
->vdev_max_asize
= cvd
->vdev_max_asize
;
1020 mvd
->vdev_psize
= cvd
->vdev_psize
;
1021 mvd
->vdev_ashift
= cvd
->vdev_ashift
;
1022 mvd
->vdev_state
= cvd
->vdev_state
;
1023 mvd
->vdev_crtxg
= cvd
->vdev_crtxg
;
1025 vdev_remove_child(pvd
, cvd
);
1026 vdev_add_child(pvd
, mvd
);
1027 cvd
->vdev_id
= mvd
->vdev_children
;
1028 vdev_add_child(mvd
, cvd
);
1029 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1031 if (mvd
== mvd
->vdev_top
)
1032 vdev_top_transfer(cvd
, mvd
);
1038 * Remove a 1-way mirror/replacing vdev from the tree.
1041 vdev_remove_parent(vdev_t
*cvd
)
1043 vdev_t
*mvd
= cvd
->vdev_parent
;
1044 vdev_t
*pvd
= mvd
->vdev_parent
;
1046 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1048 ASSERT(mvd
->vdev_children
== 1);
1049 ASSERT(mvd
->vdev_ops
== &vdev_mirror_ops
||
1050 mvd
->vdev_ops
== &vdev_replacing_ops
||
1051 mvd
->vdev_ops
== &vdev_spare_ops
);
1052 cvd
->vdev_ashift
= mvd
->vdev_ashift
;
1054 vdev_remove_child(mvd
, cvd
);
1055 vdev_remove_child(pvd
, mvd
);
1058 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1059 * Otherwise, we could have detached an offline device, and when we
1060 * go to import the pool we'll think we have two top-level vdevs,
1061 * instead of a different version of the same top-level vdev.
1063 if (mvd
->vdev_top
== mvd
) {
1064 uint64_t guid_delta
= mvd
->vdev_guid
- cvd
->vdev_guid
;
1065 cvd
->vdev_orig_guid
= cvd
->vdev_guid
;
1066 cvd
->vdev_guid
+= guid_delta
;
1067 cvd
->vdev_guid_sum
+= guid_delta
;
1070 * If pool not set for autoexpand, we need to also preserve
1071 * mvd's asize to prevent automatic expansion of cvd.
1072 * Otherwise if we are adjusting the mirror by attaching and
1073 * detaching children of non-uniform sizes, the mirror could
1074 * autoexpand, unexpectedly requiring larger devices to
1075 * re-establish the mirror.
1077 if (!cvd
->vdev_spa
->spa_autoexpand
)
1078 cvd
->vdev_asize
= mvd
->vdev_asize
;
1080 cvd
->vdev_id
= mvd
->vdev_id
;
1081 vdev_add_child(pvd
, cvd
);
1082 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1084 if (cvd
== cvd
->vdev_top
)
1085 vdev_top_transfer(mvd
, cvd
);
1087 ASSERT(mvd
->vdev_children
== 0);
1092 vdev_metaslab_init(vdev_t
*vd
, uint64_t txg
)
1094 spa_t
*spa
= vd
->vdev_spa
;
1095 objset_t
*mos
= spa
->spa_meta_objset
;
1097 uint64_t oldc
= vd
->vdev_ms_count
;
1098 uint64_t newc
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
1102 ASSERT(txg
== 0 || spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1105 * This vdev is not being allocated from yet or is a hole.
1107 if (vd
->vdev_ms_shift
== 0)
1110 ASSERT(!vd
->vdev_ishole
);
1112 ASSERT(oldc
<= newc
);
1114 mspp
= vmem_zalloc(newc
* sizeof (*mspp
), KM_SLEEP
);
1117 bcopy(vd
->vdev_ms
, mspp
, oldc
* sizeof (*mspp
));
1118 vmem_free(vd
->vdev_ms
, oldc
* sizeof (*mspp
));
1122 vd
->vdev_ms_count
= newc
;
1124 for (m
= oldc
; m
< newc
; m
++) {
1125 uint64_t object
= 0;
1128 * vdev_ms_array may be 0 if we are creating the "fake"
1129 * metaslabs for an indirect vdev for zdb's leak detection.
1130 * See zdb_leak_init().
1132 if (txg
== 0 && vd
->vdev_ms_array
!= 0) {
1133 error
= dmu_read(mos
, vd
->vdev_ms_array
,
1134 m
* sizeof (uint64_t), sizeof (uint64_t), &object
,
1137 vdev_dbgmsg(vd
, "unable to read the metaslab "
1138 "array [error=%d]", error
);
1143 error
= metaslab_init(vd
->vdev_mg
, m
, object
, txg
,
1146 vdev_dbgmsg(vd
, "metaslab_init failed [error=%d]",
1153 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_WRITER
);
1156 * If the vdev is being removed we don't activate
1157 * the metaslabs since we want to ensure that no new
1158 * allocations are performed on this device.
1160 if (oldc
== 0 && !vd
->vdev_removing
)
1161 metaslab_group_activate(vd
->vdev_mg
);
1164 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
1170 vdev_metaslab_fini(vdev_t
*vd
)
1172 if (vd
->vdev_ms
!= NULL
) {
1173 uint64_t count
= vd
->vdev_ms_count
;
1175 metaslab_group_passivate(vd
->vdev_mg
);
1176 for (uint64_t m
= 0; m
< count
; m
++) {
1177 metaslab_t
*msp
= vd
->vdev_ms
[m
];
1182 vmem_free(vd
->vdev_ms
, count
* sizeof (metaslab_t
*));
1185 vd
->vdev_ms_count
= 0;
1187 ASSERT0(vd
->vdev_ms_count
);
1188 ASSERT3U(vd
->vdev_pending_fastwrite
, ==, 0);
1191 typedef struct vdev_probe_stats
{
1192 boolean_t vps_readable
;
1193 boolean_t vps_writeable
;
1195 } vdev_probe_stats_t
;
1198 vdev_probe_done(zio_t
*zio
)
1200 spa_t
*spa
= zio
->io_spa
;
1201 vdev_t
*vd
= zio
->io_vd
;
1202 vdev_probe_stats_t
*vps
= zio
->io_private
;
1204 ASSERT(vd
->vdev_probe_zio
!= NULL
);
1206 if (zio
->io_type
== ZIO_TYPE_READ
) {
1207 if (zio
->io_error
== 0)
1208 vps
->vps_readable
= 1;
1209 if (zio
->io_error
== 0 && spa_writeable(spa
)) {
1210 zio_nowait(zio_write_phys(vd
->vdev_probe_zio
, vd
,
1211 zio
->io_offset
, zio
->io_size
, zio
->io_abd
,
1212 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1213 ZIO_PRIORITY_SYNC_WRITE
, vps
->vps_flags
, B_TRUE
));
1215 abd_free(zio
->io_abd
);
1217 } else if (zio
->io_type
== ZIO_TYPE_WRITE
) {
1218 if (zio
->io_error
== 0)
1219 vps
->vps_writeable
= 1;
1220 abd_free(zio
->io_abd
);
1221 } else if (zio
->io_type
== ZIO_TYPE_NULL
) {
1225 vd
->vdev_cant_read
|= !vps
->vps_readable
;
1226 vd
->vdev_cant_write
|= !vps
->vps_writeable
;
1228 if (vdev_readable(vd
) &&
1229 (vdev_writeable(vd
) || !spa_writeable(spa
))) {
1232 ASSERT(zio
->io_error
!= 0);
1233 vdev_dbgmsg(vd
, "failed probe");
1234 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE
,
1235 spa
, vd
, NULL
, NULL
, 0, 0);
1236 zio
->io_error
= SET_ERROR(ENXIO
);
1239 mutex_enter(&vd
->vdev_probe_lock
);
1240 ASSERT(vd
->vdev_probe_zio
== zio
);
1241 vd
->vdev_probe_zio
= NULL
;
1242 mutex_exit(&vd
->vdev_probe_lock
);
1245 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
)
1246 if (!vdev_accessible(vd
, pio
))
1247 pio
->io_error
= SET_ERROR(ENXIO
);
1249 kmem_free(vps
, sizeof (*vps
));
1254 * Determine whether this device is accessible.
1256 * Read and write to several known locations: the pad regions of each
1257 * vdev label but the first, which we leave alone in case it contains
1261 vdev_probe(vdev_t
*vd
, zio_t
*zio
)
1263 spa_t
*spa
= vd
->vdev_spa
;
1264 vdev_probe_stats_t
*vps
= NULL
;
1267 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1270 * Don't probe the probe.
1272 if (zio
&& (zio
->io_flags
& ZIO_FLAG_PROBE
))
1276 * To prevent 'probe storms' when a device fails, we create
1277 * just one probe i/o at a time. All zios that want to probe
1278 * this vdev will become parents of the probe io.
1280 mutex_enter(&vd
->vdev_probe_lock
);
1282 if ((pio
= vd
->vdev_probe_zio
) == NULL
) {
1283 vps
= kmem_zalloc(sizeof (*vps
), KM_SLEEP
);
1285 vps
->vps_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_PROBE
|
1286 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_AGGREGATE
|
1289 if (spa_config_held(spa
, SCL_ZIO
, RW_WRITER
)) {
1291 * vdev_cant_read and vdev_cant_write can only
1292 * transition from TRUE to FALSE when we have the
1293 * SCL_ZIO lock as writer; otherwise they can only
1294 * transition from FALSE to TRUE. This ensures that
1295 * any zio looking at these values can assume that
1296 * failures persist for the life of the I/O. That's
1297 * important because when a device has intermittent
1298 * connectivity problems, we want to ensure that
1299 * they're ascribed to the device (ENXIO) and not
1302 * Since we hold SCL_ZIO as writer here, clear both
1303 * values so the probe can reevaluate from first
1306 vps
->vps_flags
|= ZIO_FLAG_CONFIG_WRITER
;
1307 vd
->vdev_cant_read
= B_FALSE
;
1308 vd
->vdev_cant_write
= B_FALSE
;
1311 vd
->vdev_probe_zio
= pio
= zio_null(NULL
, spa
, vd
,
1312 vdev_probe_done
, vps
,
1313 vps
->vps_flags
| ZIO_FLAG_DONT_PROPAGATE
);
1316 * We can't change the vdev state in this context, so we
1317 * kick off an async task to do it on our behalf.
1320 vd
->vdev_probe_wanted
= B_TRUE
;
1321 spa_async_request(spa
, SPA_ASYNC_PROBE
);
1326 zio_add_child(zio
, pio
);
1328 mutex_exit(&vd
->vdev_probe_lock
);
1331 ASSERT(zio
!= NULL
);
1335 for (int l
= 1; l
< VDEV_LABELS
; l
++) {
1336 zio_nowait(zio_read_phys(pio
, vd
,
1337 vdev_label_offset(vd
->vdev_psize
, l
,
1338 offsetof(vdev_label_t
, vl_pad2
)), VDEV_PAD_SIZE
,
1339 abd_alloc_for_io(VDEV_PAD_SIZE
, B_TRUE
),
1340 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1341 ZIO_PRIORITY_SYNC_READ
, vps
->vps_flags
, B_TRUE
));
1352 vdev_open_child(void *arg
)
1356 vd
->vdev_open_thread
= curthread
;
1357 vd
->vdev_open_error
= vdev_open(vd
);
1358 vd
->vdev_open_thread
= NULL
;
1362 vdev_uses_zvols(vdev_t
*vd
)
1365 if (zvol_is_zvol(vd
->vdev_path
))
1369 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1370 if (vdev_uses_zvols(vd
->vdev_child
[c
]))
1377 vdev_open_children(vdev_t
*vd
)
1380 int children
= vd
->vdev_children
;
1383 * in order to handle pools on top of zvols, do the opens
1384 * in a single thread so that the same thread holds the
1385 * spa_namespace_lock
1387 if (vdev_uses_zvols(vd
)) {
1389 for (int c
= 0; c
< children
; c
++)
1390 vd
->vdev_child
[c
]->vdev_open_error
=
1391 vdev_open(vd
->vdev_child
[c
]);
1393 tq
= taskq_create("vdev_open", children
, minclsyspri
,
1394 children
, children
, TASKQ_PREPOPULATE
);
1398 for (int c
= 0; c
< children
; c
++)
1399 VERIFY(taskq_dispatch(tq
, vdev_open_child
,
1400 vd
->vdev_child
[c
], TQ_SLEEP
) != TASKQID_INVALID
);
1405 vd
->vdev_nonrot
= B_TRUE
;
1407 for (int c
= 0; c
< children
; c
++)
1408 vd
->vdev_nonrot
&= vd
->vdev_child
[c
]->vdev_nonrot
;
1412 * Compute the raidz-deflation ratio. Note, we hard-code
1413 * in 128k (1 << 17) because it is the "typical" blocksize.
1414 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1415 * otherwise it would inconsistently account for existing bp's.
1418 vdev_set_deflate_ratio(vdev_t
*vd
)
1420 if (vd
== vd
->vdev_top
&& !vd
->vdev_ishole
&& vd
->vdev_ashift
!= 0) {
1421 vd
->vdev_deflate_ratio
= (1 << 17) /
1422 (vdev_psize_to_asize(vd
, 1 << 17) >> SPA_MINBLOCKSHIFT
);
1427 * Prepare a virtual device for access.
1430 vdev_open(vdev_t
*vd
)
1432 spa_t
*spa
= vd
->vdev_spa
;
1435 uint64_t max_osize
= 0;
1436 uint64_t asize
, max_asize
, psize
;
1437 uint64_t ashift
= 0;
1439 ASSERT(vd
->vdev_open_thread
== curthread
||
1440 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1441 ASSERT(vd
->vdev_state
== VDEV_STATE_CLOSED
||
1442 vd
->vdev_state
== VDEV_STATE_CANT_OPEN
||
1443 vd
->vdev_state
== VDEV_STATE_OFFLINE
);
1445 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1446 vd
->vdev_cant_read
= B_FALSE
;
1447 vd
->vdev_cant_write
= B_FALSE
;
1448 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
1451 * If this vdev is not removed, check its fault status. If it's
1452 * faulted, bail out of the open.
1454 if (!vd
->vdev_removed
&& vd
->vdev_faulted
) {
1455 ASSERT(vd
->vdev_children
== 0);
1456 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1457 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1458 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1459 vd
->vdev_label_aux
);
1460 return (SET_ERROR(ENXIO
));
1461 } else if (vd
->vdev_offline
) {
1462 ASSERT(vd
->vdev_children
== 0);
1463 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
, VDEV_AUX_NONE
);
1464 return (SET_ERROR(ENXIO
));
1467 error
= vd
->vdev_ops
->vdev_op_open(vd
, &osize
, &max_osize
, &ashift
);
1470 * Reset the vdev_reopening flag so that we actually close
1471 * the vdev on error.
1473 vd
->vdev_reopening
= B_FALSE
;
1474 if (zio_injection_enabled
&& error
== 0)
1475 error
= zio_handle_device_injection(vd
, NULL
, ENXIO
);
1478 if (vd
->vdev_removed
&&
1479 vd
->vdev_stat
.vs_aux
!= VDEV_AUX_OPEN_FAILED
)
1480 vd
->vdev_removed
= B_FALSE
;
1482 if (vd
->vdev_stat
.vs_aux
== VDEV_AUX_CHILDREN_OFFLINE
) {
1483 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
,
1484 vd
->vdev_stat
.vs_aux
);
1486 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1487 vd
->vdev_stat
.vs_aux
);
1492 vd
->vdev_removed
= B_FALSE
;
1495 * Recheck the faulted flag now that we have confirmed that
1496 * the vdev is accessible. If we're faulted, bail.
1498 if (vd
->vdev_faulted
) {
1499 ASSERT(vd
->vdev_children
== 0);
1500 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1501 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1502 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1503 vd
->vdev_label_aux
);
1504 return (SET_ERROR(ENXIO
));
1507 if (vd
->vdev_degraded
) {
1508 ASSERT(vd
->vdev_children
== 0);
1509 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1510 VDEV_AUX_ERR_EXCEEDED
);
1512 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_HEALTHY
, 0);
1516 * For hole or missing vdevs we just return success.
1518 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
)
1521 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
1522 if (vd
->vdev_child
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
) {
1523 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1529 osize
= P2ALIGN(osize
, (uint64_t)sizeof (vdev_label_t
));
1530 max_osize
= P2ALIGN(max_osize
, (uint64_t)sizeof (vdev_label_t
));
1532 if (vd
->vdev_children
== 0) {
1533 if (osize
< SPA_MINDEVSIZE
) {
1534 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1535 VDEV_AUX_TOO_SMALL
);
1536 return (SET_ERROR(EOVERFLOW
));
1539 asize
= osize
- (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
);
1540 max_asize
= max_osize
- (VDEV_LABEL_START_SIZE
+
1541 VDEV_LABEL_END_SIZE
);
1543 if (vd
->vdev_parent
!= NULL
&& osize
< SPA_MINDEVSIZE
-
1544 (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
)) {
1545 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1546 VDEV_AUX_TOO_SMALL
);
1547 return (SET_ERROR(EOVERFLOW
));
1551 max_asize
= max_osize
;
1555 * If the vdev was expanded, record this so that we can re-create the
1556 * uberblock rings in labels {2,3}, during the next sync.
1558 if ((psize
> vd
->vdev_psize
) && (vd
->vdev_psize
!= 0))
1559 vd
->vdev_copy_uberblocks
= B_TRUE
;
1561 vd
->vdev_psize
= psize
;
1564 * Make sure the allocatable size hasn't shrunk too much.
1566 if (asize
< vd
->vdev_min_asize
) {
1567 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1568 VDEV_AUX_BAD_LABEL
);
1569 return (SET_ERROR(EINVAL
));
1572 if (vd
->vdev_asize
== 0) {
1574 * This is the first-ever open, so use the computed values.
1575 * For compatibility, a different ashift can be requested.
1577 vd
->vdev_asize
= asize
;
1578 vd
->vdev_max_asize
= max_asize
;
1579 if (vd
->vdev_ashift
== 0) {
1580 vd
->vdev_ashift
= ashift
; /* use detected value */
1582 if (vd
->vdev_ashift
!= 0 && (vd
->vdev_ashift
< ASHIFT_MIN
||
1583 vd
->vdev_ashift
> ASHIFT_MAX
)) {
1584 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1585 VDEV_AUX_BAD_ASHIFT
);
1586 return (SET_ERROR(EDOM
));
1590 * Detect if the alignment requirement has increased.
1591 * We don't want to make the pool unavailable, just
1592 * post an event instead.
1594 if (ashift
> vd
->vdev_top
->vdev_ashift
&&
1595 vd
->vdev_ops
->vdev_op_leaf
) {
1596 zfs_ereport_post(FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
,
1597 spa
, vd
, NULL
, NULL
, 0, 0);
1600 vd
->vdev_max_asize
= max_asize
;
1604 * If all children are healthy we update asize if either:
1605 * The asize has increased, due to a device expansion caused by dynamic
1606 * LUN growth or vdev replacement, and automatic expansion is enabled;
1607 * making the additional space available.
1609 * The asize has decreased, due to a device shrink usually caused by a
1610 * vdev replace with a smaller device. This ensures that calculations
1611 * based of max_asize and asize e.g. esize are always valid. It's safe
1612 * to do this as we've already validated that asize is greater than
1615 if (vd
->vdev_state
== VDEV_STATE_HEALTHY
&&
1616 ((asize
> vd
->vdev_asize
&&
1617 (vd
->vdev_expanding
|| spa
->spa_autoexpand
)) ||
1618 (asize
< vd
->vdev_asize
)))
1619 vd
->vdev_asize
= asize
;
1621 vdev_set_min_asize(vd
);
1624 * Ensure we can issue some IO before declaring the
1625 * vdev open for business.
1627 if (vd
->vdev_ops
->vdev_op_leaf
&&
1628 (error
= zio_wait(vdev_probe(vd
, NULL
))) != 0) {
1629 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1630 VDEV_AUX_ERR_EXCEEDED
);
1635 * Track the min and max ashift values for normal data devices.
1637 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
1638 !vd
->vdev_islog
&& vd
->vdev_aux
== NULL
) {
1639 if (vd
->vdev_ashift
> spa
->spa_max_ashift
)
1640 spa
->spa_max_ashift
= vd
->vdev_ashift
;
1641 if (vd
->vdev_ashift
< spa
->spa_min_ashift
)
1642 spa
->spa_min_ashift
= vd
->vdev_ashift
;
1646 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1647 * resilver. But don't do this if we are doing a reopen for a scrub,
1648 * since this would just restart the scrub we are already doing.
1650 if (vd
->vdev_ops
->vdev_op_leaf
&& !spa
->spa_scrub_reopen
&&
1651 vdev_resilver_needed(vd
, NULL
, NULL
))
1652 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
1658 * Called once the vdevs are all opened, this routine validates the label
1659 * contents. This needs to be done before vdev_load() so that we don't
1660 * inadvertently do repair I/Os to the wrong device.
1662 * This function will only return failure if one of the vdevs indicates that it
1663 * has since been destroyed or exported. This is only possible if
1664 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1665 * will be updated but the function will return 0.
1668 vdev_validate(vdev_t
*vd
)
1670 spa_t
*spa
= vd
->vdev_spa
;
1672 uint64_t guid
= 0, aux_guid
= 0, top_guid
;
1677 if (vdev_validate_skip
)
1680 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
1681 if (vdev_validate(vd
->vdev_child
[c
]) != 0)
1682 return (SET_ERROR(EBADF
));
1685 * If the device has already failed, or was marked offline, don't do
1686 * any further validation. Otherwise, label I/O will fail and we will
1687 * overwrite the previous state.
1689 if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_readable(vd
))
1693 * If we are performing an extreme rewind, we allow for a label that
1694 * was modified at a point after the current txg.
1695 * If config lock is not held do not check for the txg. spa_sync could
1696 * be updating the vdev's label before updating spa_last_synced_txg.
1698 if (spa
->spa_extreme_rewind
|| spa_last_synced_txg(spa
) == 0 ||
1699 spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) != SCL_CONFIG
)
1702 txg
= spa_last_synced_txg(spa
);
1704 if ((label
= vdev_label_read_config(vd
, txg
)) == NULL
) {
1705 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1706 VDEV_AUX_BAD_LABEL
);
1707 vdev_dbgmsg(vd
, "vdev_validate: failed reading config for "
1708 "txg %llu", (u_longlong_t
)txg
);
1713 * Determine if this vdev has been split off into another
1714 * pool. If so, then refuse to open it.
1716 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_SPLIT_GUID
,
1717 &aux_guid
) == 0 && aux_guid
== spa_guid(spa
)) {
1718 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1719 VDEV_AUX_SPLIT_POOL
);
1721 vdev_dbgmsg(vd
, "vdev_validate: vdev split into other pool");
1725 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0) {
1726 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1727 VDEV_AUX_CORRUPT_DATA
);
1729 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1730 ZPOOL_CONFIG_POOL_GUID
);
1735 * If config is not trusted then ignore the spa guid check. This is
1736 * necessary because if the machine crashed during a re-guid the new
1737 * guid might have been written to all of the vdev labels, but not the
1738 * cached config. The check will be performed again once we have the
1739 * trusted config from the MOS.
1741 if (spa
->spa_trust_config
&& guid
!= spa_guid(spa
)) {
1742 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1743 VDEV_AUX_CORRUPT_DATA
);
1745 vdev_dbgmsg(vd
, "vdev_validate: vdev label pool_guid doesn't "
1746 "match config (%llu != %llu)", (u_longlong_t
)guid
,
1747 (u_longlong_t
)spa_guid(spa
));
1751 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
)
1752 != 0 || nvlist_lookup_uint64(nvl
, ZPOOL_CONFIG_ORIG_GUID
,
1756 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0) {
1757 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1758 VDEV_AUX_CORRUPT_DATA
);
1760 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1765 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_TOP_GUID
, &top_guid
)
1767 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1768 VDEV_AUX_CORRUPT_DATA
);
1770 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1771 ZPOOL_CONFIG_TOP_GUID
);
1776 * If this vdev just became a top-level vdev because its sibling was
1777 * detached, it will have adopted the parent's vdev guid -- but the
1778 * label may or may not be on disk yet. Fortunately, either version
1779 * of the label will have the same top guid, so if we're a top-level
1780 * vdev, we can safely compare to that instead.
1781 * However, if the config comes from a cachefile that failed to update
1782 * after the detach, a top-level vdev will appear as a non top-level
1783 * vdev in the config. Also relax the constraints if we perform an
1786 * If we split this vdev off instead, then we also check the
1787 * original pool's guid. We don't want to consider the vdev
1788 * corrupt if it is partway through a split operation.
1790 if (vd
->vdev_guid
!= guid
&& vd
->vdev_guid
!= aux_guid
) {
1791 boolean_t mismatch
= B_FALSE
;
1792 if (spa
->spa_trust_config
&& !spa
->spa_extreme_rewind
) {
1793 if (vd
!= vd
->vdev_top
|| vd
->vdev_guid
!= top_guid
)
1796 if (vd
->vdev_guid
!= top_guid
&&
1797 vd
->vdev_top
->vdev_guid
!= guid
)
1802 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1803 VDEV_AUX_CORRUPT_DATA
);
1805 vdev_dbgmsg(vd
, "vdev_validate: config guid "
1806 "doesn't match label guid");
1807 vdev_dbgmsg(vd
, "CONFIG: guid %llu, top_guid %llu",
1808 (u_longlong_t
)vd
->vdev_guid
,
1809 (u_longlong_t
)vd
->vdev_top
->vdev_guid
);
1810 vdev_dbgmsg(vd
, "LABEL: guid %llu, top_guid %llu, "
1811 "aux_guid %llu", (u_longlong_t
)guid
,
1812 (u_longlong_t
)top_guid
, (u_longlong_t
)aux_guid
);
1817 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
,
1819 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1820 VDEV_AUX_CORRUPT_DATA
);
1822 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1823 ZPOOL_CONFIG_POOL_STATE
);
1830 * If this is a verbatim import, no need to check the
1831 * state of the pool.
1833 if (!(spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) &&
1834 spa_load_state(spa
) == SPA_LOAD_OPEN
&&
1835 state
!= POOL_STATE_ACTIVE
) {
1836 vdev_dbgmsg(vd
, "vdev_validate: invalid pool state (%llu) "
1837 "for spa %s", (u_longlong_t
)state
, spa
->spa_name
);
1838 return (SET_ERROR(EBADF
));
1842 * If we were able to open and validate a vdev that was
1843 * previously marked permanently unavailable, clear that state
1846 if (vd
->vdev_not_present
)
1847 vd
->vdev_not_present
= 0;
1853 vdev_copy_path_impl(vdev_t
*svd
, vdev_t
*dvd
)
1855 if (svd
->vdev_path
!= NULL
&& dvd
->vdev_path
!= NULL
) {
1856 if (strcmp(svd
->vdev_path
, dvd
->vdev_path
) != 0) {
1857 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
1858 "from '%s' to '%s'", (u_longlong_t
)dvd
->vdev_guid
,
1859 dvd
->vdev_path
, svd
->vdev_path
);
1860 spa_strfree(dvd
->vdev_path
);
1861 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
1863 } else if (svd
->vdev_path
!= NULL
) {
1864 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
1865 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
1866 (u_longlong_t
)dvd
->vdev_guid
, dvd
->vdev_path
);
1871 * Recursively copy vdev paths from one vdev to another. Source and destination
1872 * vdev trees must have same geometry otherwise return error. Intended to copy
1873 * paths from userland config into MOS config.
1876 vdev_copy_path_strict(vdev_t
*svd
, vdev_t
*dvd
)
1878 if ((svd
->vdev_ops
== &vdev_missing_ops
) ||
1879 (svd
->vdev_ishole
&& dvd
->vdev_ishole
) ||
1880 (dvd
->vdev_ops
== &vdev_indirect_ops
))
1883 if (svd
->vdev_ops
!= dvd
->vdev_ops
) {
1884 vdev_dbgmsg(svd
, "vdev_copy_path: vdev type mismatch: %s != %s",
1885 svd
->vdev_ops
->vdev_op_type
, dvd
->vdev_ops
->vdev_op_type
);
1886 return (SET_ERROR(EINVAL
));
1889 if (svd
->vdev_guid
!= dvd
->vdev_guid
) {
1890 vdev_dbgmsg(svd
, "vdev_copy_path: guids mismatch (%llu != "
1891 "%llu)", (u_longlong_t
)svd
->vdev_guid
,
1892 (u_longlong_t
)dvd
->vdev_guid
);
1893 return (SET_ERROR(EINVAL
));
1896 if (svd
->vdev_children
!= dvd
->vdev_children
) {
1897 vdev_dbgmsg(svd
, "vdev_copy_path: children count mismatch: "
1898 "%llu != %llu", (u_longlong_t
)svd
->vdev_children
,
1899 (u_longlong_t
)dvd
->vdev_children
);
1900 return (SET_ERROR(EINVAL
));
1903 for (uint64_t i
= 0; i
< svd
->vdev_children
; i
++) {
1904 int error
= vdev_copy_path_strict(svd
->vdev_child
[i
],
1905 dvd
->vdev_child
[i
]);
1910 if (svd
->vdev_ops
->vdev_op_leaf
)
1911 vdev_copy_path_impl(svd
, dvd
);
1917 vdev_copy_path_search(vdev_t
*stvd
, vdev_t
*dvd
)
1919 ASSERT(stvd
->vdev_top
== stvd
);
1920 ASSERT3U(stvd
->vdev_id
, ==, dvd
->vdev_top
->vdev_id
);
1922 for (uint64_t i
= 0; i
< dvd
->vdev_children
; i
++) {
1923 vdev_copy_path_search(stvd
, dvd
->vdev_child
[i
]);
1926 if (!dvd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(dvd
))
1930 * The idea here is that while a vdev can shift positions within
1931 * a top vdev (when replacing, attaching mirror, etc.) it cannot
1932 * step outside of it.
1934 vdev_t
*vd
= vdev_lookup_by_guid(stvd
, dvd
->vdev_guid
);
1936 if (vd
== NULL
|| vd
->vdev_ops
!= dvd
->vdev_ops
)
1939 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1941 vdev_copy_path_impl(vd
, dvd
);
1945 * Recursively copy vdev paths from one root vdev to another. Source and
1946 * destination vdev trees may differ in geometry. For each destination leaf
1947 * vdev, search a vdev with the same guid and top vdev id in the source.
1948 * Intended to copy paths from userland config into MOS config.
1951 vdev_copy_path_relaxed(vdev_t
*srvd
, vdev_t
*drvd
)
1953 uint64_t children
= MIN(srvd
->vdev_children
, drvd
->vdev_children
);
1954 ASSERT(srvd
->vdev_ops
== &vdev_root_ops
);
1955 ASSERT(drvd
->vdev_ops
== &vdev_root_ops
);
1957 for (uint64_t i
= 0; i
< children
; i
++) {
1958 vdev_copy_path_search(srvd
->vdev_child
[i
],
1959 drvd
->vdev_child
[i
]);
1964 * Close a virtual device.
1967 vdev_close(vdev_t
*vd
)
1969 vdev_t
*pvd
= vd
->vdev_parent
;
1970 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
1972 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1975 * If our parent is reopening, then we are as well, unless we are
1978 if (pvd
!= NULL
&& pvd
->vdev_reopening
)
1979 vd
->vdev_reopening
= (pvd
->vdev_reopening
&& !vd
->vdev_offline
);
1981 vd
->vdev_ops
->vdev_op_close(vd
);
1983 vdev_cache_purge(vd
);
1986 * We record the previous state before we close it, so that if we are
1987 * doing a reopen(), we don't generate FMA ereports if we notice that
1988 * it's still faulted.
1990 vd
->vdev_prevstate
= vd
->vdev_state
;
1992 if (vd
->vdev_offline
)
1993 vd
->vdev_state
= VDEV_STATE_OFFLINE
;
1995 vd
->vdev_state
= VDEV_STATE_CLOSED
;
1996 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
2000 vdev_hold(vdev_t
*vd
)
2002 spa_t
*spa
= vd
->vdev_spa
;
2004 ASSERT(spa_is_root(spa
));
2005 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
2008 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2009 vdev_hold(vd
->vdev_child
[c
]);
2011 if (vd
->vdev_ops
->vdev_op_leaf
)
2012 vd
->vdev_ops
->vdev_op_hold(vd
);
2016 vdev_rele(vdev_t
*vd
)
2018 ASSERT(spa_is_root(vd
->vdev_spa
));
2019 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2020 vdev_rele(vd
->vdev_child
[c
]);
2022 if (vd
->vdev_ops
->vdev_op_leaf
)
2023 vd
->vdev_ops
->vdev_op_rele(vd
);
2027 * Reopen all interior vdevs and any unopened leaves. We don't actually
2028 * reopen leaf vdevs which had previously been opened as they might deadlock
2029 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2030 * If the leaf has never been opened then open it, as usual.
2033 vdev_reopen(vdev_t
*vd
)
2035 spa_t
*spa
= vd
->vdev_spa
;
2037 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2039 /* set the reopening flag unless we're taking the vdev offline */
2040 vd
->vdev_reopening
= !vd
->vdev_offline
;
2042 (void) vdev_open(vd
);
2045 * Call vdev_validate() here to make sure we have the same device.
2046 * Otherwise, a device with an invalid label could be successfully
2047 * opened in response to vdev_reopen().
2050 (void) vdev_validate_aux(vd
);
2051 if (vdev_readable(vd
) && vdev_writeable(vd
) &&
2052 vd
->vdev_aux
== &spa
->spa_l2cache
&&
2053 !l2arc_vdev_present(vd
))
2054 l2arc_add_vdev(spa
, vd
);
2056 (void) vdev_validate(vd
);
2060 * Reassess parent vdev's health.
2062 vdev_propagate_state(vd
);
2066 vdev_create(vdev_t
*vd
, uint64_t txg
, boolean_t isreplacing
)
2071 * Normally, partial opens (e.g. of a mirror) are allowed.
2072 * For a create, however, we want to fail the request if
2073 * there are any components we can't open.
2075 error
= vdev_open(vd
);
2077 if (error
|| vd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
2079 return (error
? error
: ENXIO
);
2083 * Recursively load DTLs and initialize all labels.
2085 if ((error
= vdev_dtl_load(vd
)) != 0 ||
2086 (error
= vdev_label_init(vd
, txg
, isreplacing
?
2087 VDEV_LABEL_REPLACE
: VDEV_LABEL_CREATE
)) != 0) {
2096 vdev_metaslab_set_size(vdev_t
*vd
)
2099 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
2101 vd
->vdev_ms_shift
= highbit64(vd
->vdev_asize
/ metaslabs_per_vdev
);
2102 vd
->vdev_ms_shift
= MAX(vd
->vdev_ms_shift
, SPA_MAXBLOCKSHIFT
);
2106 vdev_dirty(vdev_t
*vd
, int flags
, void *arg
, uint64_t txg
)
2108 ASSERT(vd
== vd
->vdev_top
);
2109 /* indirect vdevs don't have metaslabs or dtls */
2110 ASSERT(vdev_is_concrete(vd
) || flags
== 0);
2111 ASSERT(ISP2(flags
));
2112 ASSERT(spa_writeable(vd
->vdev_spa
));
2114 if (flags
& VDD_METASLAB
)
2115 (void) txg_list_add(&vd
->vdev_ms_list
, arg
, txg
);
2117 if (flags
& VDD_DTL
)
2118 (void) txg_list_add(&vd
->vdev_dtl_list
, arg
, txg
);
2120 (void) txg_list_add(&vd
->vdev_spa
->spa_vdev_txg_list
, vd
, txg
);
2124 vdev_dirty_leaves(vdev_t
*vd
, int flags
, uint64_t txg
)
2126 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2127 vdev_dirty_leaves(vd
->vdev_child
[c
], flags
, txg
);
2129 if (vd
->vdev_ops
->vdev_op_leaf
)
2130 vdev_dirty(vd
->vdev_top
, flags
, vd
, txg
);
2136 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2137 * the vdev has less than perfect replication. There are four kinds of DTL:
2139 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2141 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2143 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2144 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2145 * txgs that was scrubbed.
2147 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2148 * persistent errors or just some device being offline.
2149 * Unlike the other three, the DTL_OUTAGE map is not generally
2150 * maintained; it's only computed when needed, typically to
2151 * determine whether a device can be detached.
2153 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2154 * either has the data or it doesn't.
2156 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2157 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2158 * if any child is less than fully replicated, then so is its parent.
2159 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2160 * comprising only those txgs which appear in 'maxfaults' or more children;
2161 * those are the txgs we don't have enough replication to read. For example,
2162 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2163 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2164 * two child DTL_MISSING maps.
2166 * It should be clear from the above that to compute the DTLs and outage maps
2167 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2168 * Therefore, that is all we keep on disk. When loading the pool, or after
2169 * a configuration change, we generate all other DTLs from first principles.
2172 vdev_dtl_dirty(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2174 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2176 ASSERT(t
< DTL_TYPES
);
2177 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2178 ASSERT(spa_writeable(vd
->vdev_spa
));
2180 mutex_enter(&vd
->vdev_dtl_lock
);
2181 if (!range_tree_contains(rt
, txg
, size
))
2182 range_tree_add(rt
, txg
, size
);
2183 mutex_exit(&vd
->vdev_dtl_lock
);
2187 vdev_dtl_contains(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2189 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2190 boolean_t dirty
= B_FALSE
;
2192 ASSERT(t
< DTL_TYPES
);
2193 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2196 * While we are loading the pool, the DTLs have not been loaded yet.
2197 * Ignore the DTLs and try all devices. This avoids a recursive
2198 * mutex enter on the vdev_dtl_lock, and also makes us try hard
2199 * when loading the pool (relying on the checksum to ensure that
2200 * we get the right data -- note that we while loading, we are
2201 * only reading the MOS, which is always checksummed).
2203 if (vd
->vdev_spa
->spa_load_state
!= SPA_LOAD_NONE
)
2206 mutex_enter(&vd
->vdev_dtl_lock
);
2207 if (range_tree_space(rt
) != 0)
2208 dirty
= range_tree_contains(rt
, txg
, size
);
2209 mutex_exit(&vd
->vdev_dtl_lock
);
2215 vdev_dtl_empty(vdev_t
*vd
, vdev_dtl_type_t t
)
2217 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2220 mutex_enter(&vd
->vdev_dtl_lock
);
2221 empty
= (range_tree_space(rt
) == 0);
2222 mutex_exit(&vd
->vdev_dtl_lock
);
2228 * Returns B_TRUE if vdev determines offset needs to be resilvered.
2231 vdev_dtl_need_resilver(vdev_t
*vd
, uint64_t offset
, size_t psize
)
2233 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2235 if (vd
->vdev_ops
->vdev_op_need_resilver
== NULL
||
2236 vd
->vdev_ops
->vdev_op_leaf
)
2239 return (vd
->vdev_ops
->vdev_op_need_resilver(vd
, offset
, psize
));
2243 * Returns the lowest txg in the DTL range.
2246 vdev_dtl_min(vdev_t
*vd
)
2250 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2251 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2252 ASSERT0(vd
->vdev_children
);
2254 rs
= avl_first(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
2255 return (rs
->rs_start
- 1);
2259 * Returns the highest txg in the DTL.
2262 vdev_dtl_max(vdev_t
*vd
)
2266 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2267 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2268 ASSERT0(vd
->vdev_children
);
2270 rs
= avl_last(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
2271 return (rs
->rs_end
);
2275 * Determine if a resilvering vdev should remove any DTL entries from
2276 * its range. If the vdev was resilvering for the entire duration of the
2277 * scan then it should excise that range from its DTLs. Otherwise, this
2278 * vdev is considered partially resilvered and should leave its DTL
2279 * entries intact. The comment in vdev_dtl_reassess() describes how we
2283 vdev_dtl_should_excise(vdev_t
*vd
)
2285 spa_t
*spa
= vd
->vdev_spa
;
2286 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
2288 ASSERT0(scn
->scn_phys
.scn_errors
);
2289 ASSERT0(vd
->vdev_children
);
2291 if (vd
->vdev_state
< VDEV_STATE_DEGRADED
)
2294 if (vd
->vdev_resilver_txg
== 0 ||
2295 range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) == 0)
2299 * When a resilver is initiated the scan will assign the scn_max_txg
2300 * value to the highest txg value that exists in all DTLs. If this
2301 * device's max DTL is not part of this scan (i.e. it is not in
2302 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2305 if (vdev_dtl_max(vd
) <= scn
->scn_phys
.scn_max_txg
) {
2306 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <=, vdev_dtl_min(vd
));
2307 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <, vd
->vdev_resilver_txg
);
2308 ASSERT3U(vd
->vdev_resilver_txg
, <=, scn
->scn_phys
.scn_max_txg
);
2315 * Reassess DTLs after a config change or scrub completion.
2318 vdev_dtl_reassess(vdev_t
*vd
, uint64_t txg
, uint64_t scrub_txg
, int scrub_done
)
2320 spa_t
*spa
= vd
->vdev_spa
;
2324 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
2326 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2327 vdev_dtl_reassess(vd
->vdev_child
[c
], txg
,
2328 scrub_txg
, scrub_done
);
2330 if (vd
== spa
->spa_root_vdev
|| !vdev_is_concrete(vd
) || vd
->vdev_aux
)
2333 if (vd
->vdev_ops
->vdev_op_leaf
) {
2334 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
2336 mutex_enter(&vd
->vdev_dtl_lock
);
2339 * If requested, pretend the scan completed cleanly.
2341 if (zfs_scan_ignore_errors
&& scn
)
2342 scn
->scn_phys
.scn_errors
= 0;
2345 * If we've completed a scan cleanly then determine
2346 * if this vdev should remove any DTLs. We only want to
2347 * excise regions on vdevs that were available during
2348 * the entire duration of this scan.
2350 if (scrub_txg
!= 0 &&
2351 (spa
->spa_scrub_started
||
2352 (scn
!= NULL
&& scn
->scn_phys
.scn_errors
== 0)) &&
2353 vdev_dtl_should_excise(vd
)) {
2355 * We completed a scrub up to scrub_txg. If we
2356 * did it without rebooting, then the scrub dtl
2357 * will be valid, so excise the old region and
2358 * fold in the scrub dtl. Otherwise, leave the
2359 * dtl as-is if there was an error.
2361 * There's little trick here: to excise the beginning
2362 * of the DTL_MISSING map, we put it into a reference
2363 * tree and then add a segment with refcnt -1 that
2364 * covers the range [0, scrub_txg). This means
2365 * that each txg in that range has refcnt -1 or 0.
2366 * We then add DTL_SCRUB with a refcnt of 2, so that
2367 * entries in the range [0, scrub_txg) will have a
2368 * positive refcnt -- either 1 or 2. We then convert
2369 * the reference tree into the new DTL_MISSING map.
2371 space_reftree_create(&reftree
);
2372 space_reftree_add_map(&reftree
,
2373 vd
->vdev_dtl
[DTL_MISSING
], 1);
2374 space_reftree_add_seg(&reftree
, 0, scrub_txg
, -1);
2375 space_reftree_add_map(&reftree
,
2376 vd
->vdev_dtl
[DTL_SCRUB
], 2);
2377 space_reftree_generate_map(&reftree
,
2378 vd
->vdev_dtl
[DTL_MISSING
], 1);
2379 space_reftree_destroy(&reftree
);
2381 range_tree_vacate(vd
->vdev_dtl
[DTL_PARTIAL
], NULL
, NULL
);
2382 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2383 range_tree_add
, vd
->vdev_dtl
[DTL_PARTIAL
]);
2385 range_tree_vacate(vd
->vdev_dtl
[DTL_SCRUB
], NULL
, NULL
);
2386 range_tree_vacate(vd
->vdev_dtl
[DTL_OUTAGE
], NULL
, NULL
);
2387 if (!vdev_readable(vd
))
2388 range_tree_add(vd
->vdev_dtl
[DTL_OUTAGE
], 0, -1ULL);
2390 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2391 range_tree_add
, vd
->vdev_dtl
[DTL_OUTAGE
]);
2394 * If the vdev was resilvering and no longer has any
2395 * DTLs then reset its resilvering flag and dirty
2396 * the top level so that we persist the change.
2398 if (vd
->vdev_resilver_txg
!= 0 &&
2399 range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) == 0 &&
2400 range_tree_space(vd
->vdev_dtl
[DTL_OUTAGE
]) == 0) {
2401 vd
->vdev_resilver_txg
= 0;
2402 vdev_config_dirty(vd
->vdev_top
);
2405 mutex_exit(&vd
->vdev_dtl_lock
);
2408 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, txg
);
2412 mutex_enter(&vd
->vdev_dtl_lock
);
2413 for (int t
= 0; t
< DTL_TYPES
; t
++) {
2414 /* account for child's outage in parent's missing map */
2415 int s
= (t
== DTL_MISSING
) ? DTL_OUTAGE
: t
;
2417 continue; /* leaf vdevs only */
2418 if (t
== DTL_PARTIAL
)
2419 minref
= 1; /* i.e. non-zero */
2420 else if (vd
->vdev_nparity
!= 0)
2421 minref
= vd
->vdev_nparity
+ 1; /* RAID-Z */
2423 minref
= vd
->vdev_children
; /* any kind of mirror */
2424 space_reftree_create(&reftree
);
2425 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2426 vdev_t
*cvd
= vd
->vdev_child
[c
];
2427 mutex_enter(&cvd
->vdev_dtl_lock
);
2428 space_reftree_add_map(&reftree
, cvd
->vdev_dtl
[s
], 1);
2429 mutex_exit(&cvd
->vdev_dtl_lock
);
2431 space_reftree_generate_map(&reftree
, vd
->vdev_dtl
[t
], minref
);
2432 space_reftree_destroy(&reftree
);
2434 mutex_exit(&vd
->vdev_dtl_lock
);
2438 vdev_dtl_load(vdev_t
*vd
)
2440 spa_t
*spa
= vd
->vdev_spa
;
2441 objset_t
*mos
= spa
->spa_meta_objset
;
2444 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_dtl_object
!= 0) {
2445 ASSERT(vdev_is_concrete(vd
));
2447 error
= space_map_open(&vd
->vdev_dtl_sm
, mos
,
2448 vd
->vdev_dtl_object
, 0, -1ULL, 0);
2451 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2453 mutex_enter(&vd
->vdev_dtl_lock
);
2456 * Now that we've opened the space_map we need to update
2459 space_map_update(vd
->vdev_dtl_sm
);
2461 error
= space_map_load(vd
->vdev_dtl_sm
,
2462 vd
->vdev_dtl
[DTL_MISSING
], SM_ALLOC
);
2463 mutex_exit(&vd
->vdev_dtl_lock
);
2468 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2469 error
= vdev_dtl_load(vd
->vdev_child
[c
]);
2478 vdev_destroy_unlink_zap(vdev_t
*vd
, uint64_t zapobj
, dmu_tx_t
*tx
)
2480 spa_t
*spa
= vd
->vdev_spa
;
2482 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zapobj
, tx
));
2483 VERIFY0(zap_remove_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2488 vdev_create_link_zap(vdev_t
*vd
, dmu_tx_t
*tx
)
2490 spa_t
*spa
= vd
->vdev_spa
;
2491 uint64_t zap
= zap_create(spa
->spa_meta_objset
, DMU_OTN_ZAP_METADATA
,
2492 DMU_OT_NONE
, 0, tx
);
2495 VERIFY0(zap_add_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2502 vdev_construct_zaps(vdev_t
*vd
, dmu_tx_t
*tx
)
2504 if (vd
->vdev_ops
!= &vdev_hole_ops
&&
2505 vd
->vdev_ops
!= &vdev_missing_ops
&&
2506 vd
->vdev_ops
!= &vdev_root_ops
&&
2507 !vd
->vdev_top
->vdev_removing
) {
2508 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_leaf_zap
== 0) {
2509 vd
->vdev_leaf_zap
= vdev_create_link_zap(vd
, tx
);
2511 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
== 0) {
2512 vd
->vdev_top_zap
= vdev_create_link_zap(vd
, tx
);
2515 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2516 vdev_construct_zaps(vd
->vdev_child
[i
], tx
);
2521 vdev_dtl_sync(vdev_t
*vd
, uint64_t txg
)
2523 spa_t
*spa
= vd
->vdev_spa
;
2524 range_tree_t
*rt
= vd
->vdev_dtl
[DTL_MISSING
];
2525 objset_t
*mos
= spa
->spa_meta_objset
;
2526 range_tree_t
*rtsync
;
2528 uint64_t object
= space_map_object(vd
->vdev_dtl_sm
);
2530 ASSERT(vdev_is_concrete(vd
));
2531 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2533 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2535 if (vd
->vdev_detached
|| vd
->vdev_top
->vdev_removing
) {
2536 mutex_enter(&vd
->vdev_dtl_lock
);
2537 space_map_free(vd
->vdev_dtl_sm
, tx
);
2538 space_map_close(vd
->vdev_dtl_sm
);
2539 vd
->vdev_dtl_sm
= NULL
;
2540 mutex_exit(&vd
->vdev_dtl_lock
);
2543 * We only destroy the leaf ZAP for detached leaves or for
2544 * removed log devices. Removed data devices handle leaf ZAP
2545 * cleanup later, once cancellation is no longer possible.
2547 if (vd
->vdev_leaf_zap
!= 0 && (vd
->vdev_detached
||
2548 vd
->vdev_top
->vdev_islog
)) {
2549 vdev_destroy_unlink_zap(vd
, vd
->vdev_leaf_zap
, tx
);
2550 vd
->vdev_leaf_zap
= 0;
2557 if (vd
->vdev_dtl_sm
== NULL
) {
2558 uint64_t new_object
;
2560 new_object
= space_map_alloc(mos
, tx
);
2561 VERIFY3U(new_object
, !=, 0);
2563 VERIFY0(space_map_open(&vd
->vdev_dtl_sm
, mos
, new_object
,
2565 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2568 rtsync
= range_tree_create(NULL
, NULL
);
2570 mutex_enter(&vd
->vdev_dtl_lock
);
2571 range_tree_walk(rt
, range_tree_add
, rtsync
);
2572 mutex_exit(&vd
->vdev_dtl_lock
);
2574 space_map_truncate(vd
->vdev_dtl_sm
, tx
);
2575 space_map_write(vd
->vdev_dtl_sm
, rtsync
, SM_ALLOC
, tx
);
2576 range_tree_vacate(rtsync
, NULL
, NULL
);
2578 range_tree_destroy(rtsync
);
2581 * If the object for the space map has changed then dirty
2582 * the top level so that we update the config.
2584 if (object
!= space_map_object(vd
->vdev_dtl_sm
)) {
2585 vdev_dbgmsg(vd
, "txg %llu, spa %s, DTL old object %llu, "
2586 "new object %llu", (u_longlong_t
)txg
, spa_name(spa
),
2587 (u_longlong_t
)object
,
2588 (u_longlong_t
)space_map_object(vd
->vdev_dtl_sm
));
2589 vdev_config_dirty(vd
->vdev_top
);
2594 mutex_enter(&vd
->vdev_dtl_lock
);
2595 space_map_update(vd
->vdev_dtl_sm
);
2596 mutex_exit(&vd
->vdev_dtl_lock
);
2600 * Determine whether the specified vdev can be offlined/detached/removed
2601 * without losing data.
2604 vdev_dtl_required(vdev_t
*vd
)
2606 spa_t
*spa
= vd
->vdev_spa
;
2607 vdev_t
*tvd
= vd
->vdev_top
;
2608 uint8_t cant_read
= vd
->vdev_cant_read
;
2611 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2613 if (vd
== spa
->spa_root_vdev
|| vd
== tvd
)
2617 * Temporarily mark the device as unreadable, and then determine
2618 * whether this results in any DTL outages in the top-level vdev.
2619 * If not, we can safely offline/detach/remove the device.
2621 vd
->vdev_cant_read
= B_TRUE
;
2622 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2623 required
= !vdev_dtl_empty(tvd
, DTL_OUTAGE
);
2624 vd
->vdev_cant_read
= cant_read
;
2625 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2627 if (!required
&& zio_injection_enabled
)
2628 required
= !!zio_handle_device_injection(vd
, NULL
, ECHILD
);
2634 * Determine if resilver is needed, and if so the txg range.
2637 vdev_resilver_needed(vdev_t
*vd
, uint64_t *minp
, uint64_t *maxp
)
2639 boolean_t needed
= B_FALSE
;
2640 uint64_t thismin
= UINT64_MAX
;
2641 uint64_t thismax
= 0;
2643 if (vd
->vdev_children
== 0) {
2644 mutex_enter(&vd
->vdev_dtl_lock
);
2645 if (range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) != 0 &&
2646 vdev_writeable(vd
)) {
2648 thismin
= vdev_dtl_min(vd
);
2649 thismax
= vdev_dtl_max(vd
);
2652 mutex_exit(&vd
->vdev_dtl_lock
);
2654 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2655 vdev_t
*cvd
= vd
->vdev_child
[c
];
2656 uint64_t cmin
, cmax
;
2658 if (vdev_resilver_needed(cvd
, &cmin
, &cmax
)) {
2659 thismin
= MIN(thismin
, cmin
);
2660 thismax
= MAX(thismax
, cmax
);
2666 if (needed
&& minp
) {
2674 vdev_load(vdev_t
*vd
)
2679 * Recursively load all children.
2681 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2682 error
= vdev_load(vd
->vdev_child
[c
]);
2688 vdev_set_deflate_ratio(vd
);
2691 * If this is a top-level vdev, initialize its metaslabs.
2693 if (vd
== vd
->vdev_top
&& vdev_is_concrete(vd
)) {
2694 if (vd
->vdev_ashift
== 0 || vd
->vdev_asize
== 0) {
2695 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2696 VDEV_AUX_CORRUPT_DATA
);
2697 vdev_dbgmsg(vd
, "vdev_load: invalid size. ashift=%llu, "
2698 "asize=%llu", (u_longlong_t
)vd
->vdev_ashift
,
2699 (u_longlong_t
)vd
->vdev_asize
);
2700 return (SET_ERROR(ENXIO
));
2701 } else if ((error
= vdev_metaslab_init(vd
, 0)) != 0) {
2702 vdev_dbgmsg(vd
, "vdev_load: metaslab_init failed "
2703 "[error=%d]", error
);
2704 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2705 VDEV_AUX_CORRUPT_DATA
);
2711 * If this is a leaf vdev, load its DTL.
2713 if (vd
->vdev_ops
->vdev_op_leaf
&& (error
= vdev_dtl_load(vd
)) != 0) {
2714 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2715 VDEV_AUX_CORRUPT_DATA
);
2716 vdev_dbgmsg(vd
, "vdev_load: vdev_dtl_load failed "
2717 "[error=%d]", error
);
2721 uint64_t obsolete_sm_object
= vdev_obsolete_sm_object(vd
);
2722 if (obsolete_sm_object
!= 0) {
2723 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
2724 ASSERT(vd
->vdev_asize
!= 0);
2725 ASSERT(vd
->vdev_obsolete_sm
== NULL
);
2727 if ((error
= space_map_open(&vd
->vdev_obsolete_sm
, mos
,
2728 obsolete_sm_object
, 0, vd
->vdev_asize
, 0))) {
2729 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2730 VDEV_AUX_CORRUPT_DATA
);
2731 vdev_dbgmsg(vd
, "vdev_load: space_map_open failed for "
2732 "obsolete spacemap (obj %llu) [error=%d]",
2733 (u_longlong_t
)obsolete_sm_object
, error
);
2736 space_map_update(vd
->vdev_obsolete_sm
);
2743 * The special vdev case is used for hot spares and l2cache devices. Its
2744 * sole purpose it to set the vdev state for the associated vdev. To do this,
2745 * we make sure that we can open the underlying device, then try to read the
2746 * label, and make sure that the label is sane and that it hasn't been
2747 * repurposed to another pool.
2750 vdev_validate_aux(vdev_t
*vd
)
2753 uint64_t guid
, version
;
2756 if (!vdev_readable(vd
))
2759 if ((label
= vdev_label_read_config(vd
, -1ULL)) == NULL
) {
2760 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2761 VDEV_AUX_CORRUPT_DATA
);
2765 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_VERSION
, &version
) != 0 ||
2766 !SPA_VERSION_IS_SUPPORTED(version
) ||
2767 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0 ||
2768 guid
!= vd
->vdev_guid
||
2769 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
, &state
) != 0) {
2770 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2771 VDEV_AUX_CORRUPT_DATA
);
2777 * We don't actually check the pool state here. If it's in fact in
2778 * use by another pool, we update this fact on the fly when requested.
2785 * Free the objects used to store this vdev's spacemaps, and the array
2786 * that points to them.
2789 vdev_destroy_spacemaps(vdev_t
*vd
, dmu_tx_t
*tx
)
2791 if (vd
->vdev_ms_array
== 0)
2794 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
2795 uint64_t array_count
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
2796 size_t array_bytes
= array_count
* sizeof (uint64_t);
2797 uint64_t *smobj_array
= kmem_alloc(array_bytes
, KM_SLEEP
);
2798 VERIFY0(dmu_read(mos
, vd
->vdev_ms_array
, 0,
2799 array_bytes
, smobj_array
, 0));
2801 for (uint64_t i
= 0; i
< array_count
; i
++) {
2802 uint64_t smobj
= smobj_array
[i
];
2806 space_map_free_obj(mos
, smobj
, tx
);
2809 kmem_free(smobj_array
, array_bytes
);
2810 VERIFY0(dmu_object_free(mos
, vd
->vdev_ms_array
, tx
));
2811 vd
->vdev_ms_array
= 0;
2815 vdev_remove_empty(vdev_t
*vd
, uint64_t txg
)
2817 spa_t
*spa
= vd
->vdev_spa
;
2820 ASSERT(vd
== vd
->vdev_top
);
2821 ASSERT3U(txg
, ==, spa_syncing_txg(spa
));
2823 if (vd
->vdev_ms
!= NULL
) {
2824 metaslab_group_t
*mg
= vd
->vdev_mg
;
2826 metaslab_group_histogram_verify(mg
);
2827 metaslab_class_histogram_verify(mg
->mg_class
);
2829 for (int m
= 0; m
< vd
->vdev_ms_count
; m
++) {
2830 metaslab_t
*msp
= vd
->vdev_ms
[m
];
2832 if (msp
== NULL
|| msp
->ms_sm
== NULL
)
2835 mutex_enter(&msp
->ms_lock
);
2837 * If the metaslab was not loaded when the vdev
2838 * was removed then the histogram accounting may
2839 * not be accurate. Update the histogram information
2840 * here so that we ensure that the metaslab group
2841 * and metaslab class are up-to-date.
2843 metaslab_group_histogram_remove(mg
, msp
);
2845 VERIFY0(space_map_allocated(msp
->ms_sm
));
2846 space_map_close(msp
->ms_sm
);
2848 mutex_exit(&msp
->ms_lock
);
2851 metaslab_group_histogram_verify(mg
);
2852 metaslab_class_histogram_verify(mg
->mg_class
);
2853 for (int i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++)
2854 ASSERT0(mg
->mg_histogram
[i
]);
2857 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
2858 vdev_destroy_spacemaps(vd
, tx
);
2860 if (vd
->vdev_islog
&& vd
->vdev_top_zap
!= 0) {
2861 vdev_destroy_unlink_zap(vd
, vd
->vdev_top_zap
, tx
);
2862 vd
->vdev_top_zap
= 0;
2868 vdev_sync_done(vdev_t
*vd
, uint64_t txg
)
2871 boolean_t reassess
= !txg_list_empty(&vd
->vdev_ms_list
, TXG_CLEAN(txg
));
2873 ASSERT(vdev_is_concrete(vd
));
2875 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, TXG_CLEAN(txg
))))
2876 metaslab_sync_done(msp
, txg
);
2879 metaslab_sync_reassess(vd
->vdev_mg
);
2883 vdev_sync(vdev_t
*vd
, uint64_t txg
)
2885 spa_t
*spa
= vd
->vdev_spa
;
2890 if (range_tree_space(vd
->vdev_obsolete_segments
) > 0) {
2893 ASSERT(vd
->vdev_removing
||
2894 vd
->vdev_ops
== &vdev_indirect_ops
);
2896 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2897 vdev_indirect_sync_obsolete(vd
, tx
);
2901 * If the vdev is indirect, it can't have dirty
2902 * metaslabs or DTLs.
2904 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
2905 ASSERT(txg_list_empty(&vd
->vdev_ms_list
, txg
));
2906 ASSERT(txg_list_empty(&vd
->vdev_dtl_list
, txg
));
2911 ASSERT(vdev_is_concrete(vd
));
2913 if (vd
->vdev_ms_array
== 0 && vd
->vdev_ms_shift
!= 0 &&
2914 !vd
->vdev_removing
) {
2915 ASSERT(vd
== vd
->vdev_top
);
2916 ASSERT0(vd
->vdev_indirect_config
.vic_mapping_object
);
2917 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2918 vd
->vdev_ms_array
= dmu_object_alloc(spa
->spa_meta_objset
,
2919 DMU_OT_OBJECT_ARRAY
, 0, DMU_OT_NONE
, 0, tx
);
2920 ASSERT(vd
->vdev_ms_array
!= 0);
2921 vdev_config_dirty(vd
);
2925 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, txg
)) != NULL
) {
2926 metaslab_sync(msp
, txg
);
2927 (void) txg_list_add(&vd
->vdev_ms_list
, msp
, TXG_CLEAN(txg
));
2930 while ((lvd
= txg_list_remove(&vd
->vdev_dtl_list
, txg
)) != NULL
)
2931 vdev_dtl_sync(lvd
, txg
);
2934 * Remove the metadata associated with this vdev once it's empty.
2935 * Note that this is typically used for log/cache device removal;
2936 * we don't empty toplevel vdevs when removing them. But if
2937 * a toplevel happens to be emptied, this is not harmful.
2939 if (vd
->vdev_stat
.vs_alloc
== 0 && vd
->vdev_removing
) {
2940 vdev_remove_empty(vd
, txg
);
2943 (void) txg_list_add(&spa
->spa_vdev_txg_list
, vd
, TXG_CLEAN(txg
));
2947 vdev_psize_to_asize(vdev_t
*vd
, uint64_t psize
)
2949 return (vd
->vdev_ops
->vdev_op_asize(vd
, psize
));
2953 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
2954 * not be opened, and no I/O is attempted.
2957 vdev_fault(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
2961 spa_vdev_state_enter(spa
, SCL_NONE
);
2963 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2964 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2966 if (!vd
->vdev_ops
->vdev_op_leaf
)
2967 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2972 * If user did a 'zpool offline -f' then make the fault persist across
2975 if (aux
== VDEV_AUX_EXTERNAL_PERSIST
) {
2977 * There are two kinds of forced faults: temporary and
2978 * persistent. Temporary faults go away at pool import, while
2979 * persistent faults stay set. Both types of faults can be
2980 * cleared with a zpool clear.
2982 * We tell if a vdev is persistently faulted by looking at the
2983 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
2984 * import then it's a persistent fault. Otherwise, it's
2985 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
2986 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
2987 * tells vdev_config_generate() (which gets run later) to set
2988 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
2990 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
2991 vd
->vdev_tmpoffline
= B_FALSE
;
2992 aux
= VDEV_AUX_EXTERNAL
;
2994 vd
->vdev_tmpoffline
= B_TRUE
;
2998 * We don't directly use the aux state here, but if we do a
2999 * vdev_reopen(), we need this value to be present to remember why we
3002 vd
->vdev_label_aux
= aux
;
3005 * Faulted state takes precedence over degraded.
3007 vd
->vdev_delayed_close
= B_FALSE
;
3008 vd
->vdev_faulted
= 1ULL;
3009 vd
->vdev_degraded
= 0ULL;
3010 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_FAULTED
, aux
);
3013 * If this device has the only valid copy of the data, then
3014 * back off and simply mark the vdev as degraded instead.
3016 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&& vdev_dtl_required(vd
)) {
3017 vd
->vdev_degraded
= 1ULL;
3018 vd
->vdev_faulted
= 0ULL;
3021 * If we reopen the device and it's not dead, only then do we
3026 if (vdev_readable(vd
))
3027 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
, aux
);
3030 return (spa_vdev_state_exit(spa
, vd
, 0));
3034 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
3035 * user that something is wrong. The vdev continues to operate as normal as far
3036 * as I/O is concerned.
3039 vdev_degrade(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
3043 spa_vdev_state_enter(spa
, SCL_NONE
);
3045 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3046 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
3048 if (!vd
->vdev_ops
->vdev_op_leaf
)
3049 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3052 * If the vdev is already faulted, then don't do anything.
3054 if (vd
->vdev_faulted
|| vd
->vdev_degraded
)
3055 return (spa_vdev_state_exit(spa
, NULL
, 0));
3057 vd
->vdev_degraded
= 1ULL;
3058 if (!vdev_is_dead(vd
))
3059 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
,
3062 return (spa_vdev_state_exit(spa
, vd
, 0));
3066 * Online the given vdev.
3068 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
3069 * spare device should be detached when the device finishes resilvering.
3070 * Second, the online should be treated like a 'test' online case, so no FMA
3071 * events are generated if the device fails to open.
3074 vdev_online(spa_t
*spa
, uint64_t guid
, uint64_t flags
, vdev_state_t
*newstate
)
3076 vdev_t
*vd
, *tvd
, *pvd
, *rvd
= spa
->spa_root_vdev
;
3077 boolean_t wasoffline
;
3078 vdev_state_t oldstate
;
3080 spa_vdev_state_enter(spa
, SCL_NONE
);
3082 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3083 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
3085 if (!vd
->vdev_ops
->vdev_op_leaf
)
3086 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3088 wasoffline
= (vd
->vdev_offline
|| vd
->vdev_tmpoffline
);
3089 oldstate
= vd
->vdev_state
;
3092 vd
->vdev_offline
= B_FALSE
;
3093 vd
->vdev_tmpoffline
= B_FALSE
;
3094 vd
->vdev_checkremove
= !!(flags
& ZFS_ONLINE_CHECKREMOVE
);
3095 vd
->vdev_forcefault
= !!(flags
& ZFS_ONLINE_FORCEFAULT
);
3097 /* XXX - L2ARC 1.0 does not support expansion */
3098 if (!vd
->vdev_aux
) {
3099 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3100 pvd
->vdev_expanding
= !!(flags
& ZFS_ONLINE_EXPAND
);
3104 vd
->vdev_checkremove
= vd
->vdev_forcefault
= B_FALSE
;
3106 if (!vd
->vdev_aux
) {
3107 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3108 pvd
->vdev_expanding
= B_FALSE
;
3112 *newstate
= vd
->vdev_state
;
3113 if ((flags
& ZFS_ONLINE_UNSPARE
) &&
3114 !vdev_is_dead(vd
) && vd
->vdev_parent
&&
3115 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3116 vd
->vdev_parent
->vdev_child
[0] == vd
)
3117 vd
->vdev_unspare
= B_TRUE
;
3119 if ((flags
& ZFS_ONLINE_EXPAND
) || spa
->spa_autoexpand
) {
3121 /* XXX - L2ARC 1.0 does not support expansion */
3123 return (spa_vdev_state_exit(spa
, vd
, ENOTSUP
));
3124 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3128 (oldstate
< VDEV_STATE_DEGRADED
&&
3129 vd
->vdev_state
>= VDEV_STATE_DEGRADED
))
3130 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_ONLINE
);
3132 return (spa_vdev_state_exit(spa
, vd
, 0));
3136 vdev_offline_locked(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
3140 uint64_t generation
;
3141 metaslab_group_t
*mg
;
3144 spa_vdev_state_enter(spa
, SCL_ALLOC
);
3146 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3147 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
3149 if (!vd
->vdev_ops
->vdev_op_leaf
)
3150 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3154 generation
= spa
->spa_config_generation
+ 1;
3157 * If the device isn't already offline, try to offline it.
3159 if (!vd
->vdev_offline
) {
3161 * If this device has the only valid copy of some data,
3162 * don't allow it to be offlined. Log devices are always
3165 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
3166 vdev_dtl_required(vd
))
3167 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
3170 * If the top-level is a slog and it has had allocations
3171 * then proceed. We check that the vdev's metaslab group
3172 * is not NULL since it's possible that we may have just
3173 * added this vdev but not yet initialized its metaslabs.
3175 if (tvd
->vdev_islog
&& mg
!= NULL
) {
3177 * Prevent any future allocations.
3179 metaslab_group_passivate(mg
);
3180 (void) spa_vdev_state_exit(spa
, vd
, 0);
3182 error
= spa_reset_logs(spa
);
3184 spa_vdev_state_enter(spa
, SCL_ALLOC
);
3187 * Check to see if the config has changed.
3189 if (error
|| generation
!= spa
->spa_config_generation
) {
3190 metaslab_group_activate(mg
);
3192 return (spa_vdev_state_exit(spa
,
3194 (void) spa_vdev_state_exit(spa
, vd
, 0);
3197 ASSERT0(tvd
->vdev_stat
.vs_alloc
);
3201 * Offline this device and reopen its top-level vdev.
3202 * If the top-level vdev is a log device then just offline
3203 * it. Otherwise, if this action results in the top-level
3204 * vdev becoming unusable, undo it and fail the request.
3206 vd
->vdev_offline
= B_TRUE
;
3209 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
3210 vdev_is_dead(tvd
)) {
3211 vd
->vdev_offline
= B_FALSE
;
3213 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
3217 * Add the device back into the metaslab rotor so that
3218 * once we online the device it's open for business.
3220 if (tvd
->vdev_islog
&& mg
!= NULL
)
3221 metaslab_group_activate(mg
);
3224 vd
->vdev_tmpoffline
= !!(flags
& ZFS_OFFLINE_TEMPORARY
);
3226 return (spa_vdev_state_exit(spa
, vd
, 0));
3230 vdev_offline(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
3234 mutex_enter(&spa
->spa_vdev_top_lock
);
3235 error
= vdev_offline_locked(spa
, guid
, flags
);
3236 mutex_exit(&spa
->spa_vdev_top_lock
);
3242 * Clear the error counts associated with this vdev. Unlike vdev_online() and
3243 * vdev_offline(), we assume the spa config is locked. We also clear all
3244 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
3247 vdev_clear(spa_t
*spa
, vdev_t
*vd
)
3249 vdev_t
*rvd
= spa
->spa_root_vdev
;
3251 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
3256 vd
->vdev_stat
.vs_read_errors
= 0;
3257 vd
->vdev_stat
.vs_write_errors
= 0;
3258 vd
->vdev_stat
.vs_checksum_errors
= 0;
3260 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3261 vdev_clear(spa
, vd
->vdev_child
[c
]);
3264 * It makes no sense to "clear" an indirect vdev.
3266 if (!vdev_is_concrete(vd
))
3270 * If we're in the FAULTED state or have experienced failed I/O, then
3271 * clear the persistent state and attempt to reopen the device. We
3272 * also mark the vdev config dirty, so that the new faulted state is
3273 * written out to disk.
3275 if (vd
->vdev_faulted
|| vd
->vdev_degraded
||
3276 !vdev_readable(vd
) || !vdev_writeable(vd
)) {
3278 * When reopening in response to a clear event, it may be due to
3279 * a fmadm repair request. In this case, if the device is
3280 * still broken, we want to still post the ereport again.
3282 vd
->vdev_forcefault
= B_TRUE
;
3284 vd
->vdev_faulted
= vd
->vdev_degraded
= 0ULL;
3285 vd
->vdev_cant_read
= B_FALSE
;
3286 vd
->vdev_cant_write
= B_FALSE
;
3287 vd
->vdev_stat
.vs_aux
= 0;
3289 vdev_reopen(vd
== rvd
? rvd
: vd
->vdev_top
);
3291 vd
->vdev_forcefault
= B_FALSE
;
3293 if (vd
!= rvd
&& vdev_writeable(vd
->vdev_top
))
3294 vdev_state_dirty(vd
->vdev_top
);
3296 if (vd
->vdev_aux
== NULL
&& !vdev_is_dead(vd
))
3297 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
3299 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_CLEAR
);
3303 * When clearing a FMA-diagnosed fault, we always want to
3304 * unspare the device, as we assume that the original spare was
3305 * done in response to the FMA fault.
3307 if (!vdev_is_dead(vd
) && vd
->vdev_parent
!= NULL
&&
3308 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3309 vd
->vdev_parent
->vdev_child
[0] == vd
)
3310 vd
->vdev_unspare
= B_TRUE
;
3314 vdev_is_dead(vdev_t
*vd
)
3317 * Holes and missing devices are always considered "dead".
3318 * This simplifies the code since we don't have to check for
3319 * these types of devices in the various code paths.
3320 * Instead we rely on the fact that we skip over dead devices
3321 * before issuing I/O to them.
3323 return (vd
->vdev_state
< VDEV_STATE_DEGRADED
||
3324 vd
->vdev_ops
== &vdev_hole_ops
||
3325 vd
->vdev_ops
== &vdev_missing_ops
);
3329 vdev_readable(vdev_t
*vd
)
3331 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_read
);
3335 vdev_writeable(vdev_t
*vd
)
3337 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_write
&&
3338 vdev_is_concrete(vd
));
3342 vdev_allocatable(vdev_t
*vd
)
3344 uint64_t state
= vd
->vdev_state
;
3347 * We currently allow allocations from vdevs which may be in the
3348 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3349 * fails to reopen then we'll catch it later when we're holding
3350 * the proper locks. Note that we have to get the vdev state
3351 * in a local variable because although it changes atomically,
3352 * we're asking two separate questions about it.
3354 return (!(state
< VDEV_STATE_DEGRADED
&& state
!= VDEV_STATE_CLOSED
) &&
3355 !vd
->vdev_cant_write
&& vdev_is_concrete(vd
) &&
3356 vd
->vdev_mg
->mg_initialized
);
3360 vdev_accessible(vdev_t
*vd
, zio_t
*zio
)
3362 ASSERT(zio
->io_vd
== vd
);
3364 if (vdev_is_dead(vd
) || vd
->vdev_remove_wanted
)
3367 if (zio
->io_type
== ZIO_TYPE_READ
)
3368 return (!vd
->vdev_cant_read
);
3370 if (zio
->io_type
== ZIO_TYPE_WRITE
)
3371 return (!vd
->vdev_cant_write
);
3377 vdev_get_child_stat(vdev_t
*cvd
, vdev_stat_t
*vs
, vdev_stat_t
*cvs
)
3380 for (t
= 0; t
< ZIO_TYPES
; t
++) {
3381 vs
->vs_ops
[t
] += cvs
->vs_ops
[t
];
3382 vs
->vs_bytes
[t
] += cvs
->vs_bytes
[t
];
3385 cvs
->vs_scan_removing
= cvd
->vdev_removing
;
3389 * Get extended stats
3392 vdev_get_child_stat_ex(vdev_t
*cvd
, vdev_stat_ex_t
*vsx
, vdev_stat_ex_t
*cvsx
)
3395 for (t
= 0; t
< ZIO_TYPES
; t
++) {
3396 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_disk_histo
[0]); b
++)
3397 vsx
->vsx_disk_histo
[t
][b
] += cvsx
->vsx_disk_histo
[t
][b
];
3399 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_total_histo
[0]); b
++) {
3400 vsx
->vsx_total_histo
[t
][b
] +=
3401 cvsx
->vsx_total_histo
[t
][b
];
3405 for (t
= 0; t
< ZIO_PRIORITY_NUM_QUEUEABLE
; t
++) {
3406 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_queue_histo
[0]); b
++) {
3407 vsx
->vsx_queue_histo
[t
][b
] +=
3408 cvsx
->vsx_queue_histo
[t
][b
];
3410 vsx
->vsx_active_queue
[t
] += cvsx
->vsx_active_queue
[t
];
3411 vsx
->vsx_pend_queue
[t
] += cvsx
->vsx_pend_queue
[t
];
3413 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_ind_histo
[0]); b
++)
3414 vsx
->vsx_ind_histo
[t
][b
] += cvsx
->vsx_ind_histo
[t
][b
];
3416 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_agg_histo
[0]); b
++)
3417 vsx
->vsx_agg_histo
[t
][b
] += cvsx
->vsx_agg_histo
[t
][b
];
3423 * Get statistics for the given vdev.
3426 vdev_get_stats_ex_impl(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
3430 * If we're getting stats on the root vdev, aggregate the I/O counts
3431 * over all top-level vdevs (i.e. the direct children of the root).
3433 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3435 memset(vs
->vs_ops
, 0, sizeof (vs
->vs_ops
));
3436 memset(vs
->vs_bytes
, 0, sizeof (vs
->vs_bytes
));
3439 memset(vsx
, 0, sizeof (*vsx
));
3441 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3442 vdev_t
*cvd
= vd
->vdev_child
[c
];
3443 vdev_stat_t
*cvs
= &cvd
->vdev_stat
;
3444 vdev_stat_ex_t
*cvsx
= &cvd
->vdev_stat_ex
;
3446 vdev_get_stats_ex_impl(cvd
, cvs
, cvsx
);
3448 vdev_get_child_stat(cvd
, vs
, cvs
);
3450 vdev_get_child_stat_ex(cvd
, vsx
, cvsx
);
3455 * We're a leaf. Just copy our ZIO active queue stats in. The
3456 * other leaf stats are updated in vdev_stat_update().
3461 memcpy(vsx
, &vd
->vdev_stat_ex
, sizeof (vd
->vdev_stat_ex
));
3463 for (t
= 0; t
< ARRAY_SIZE(vd
->vdev_queue
.vq_class
); t
++) {
3464 vsx
->vsx_active_queue
[t
] =
3465 vd
->vdev_queue
.vq_class
[t
].vqc_active
;
3466 vsx
->vsx_pend_queue
[t
] = avl_numnodes(
3467 &vd
->vdev_queue
.vq_class
[t
].vqc_queued_tree
);
3473 vdev_get_stats_ex(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
3475 vdev_t
*tvd
= vd
->vdev_top
;
3476 mutex_enter(&vd
->vdev_stat_lock
);
3478 bcopy(&vd
->vdev_stat
, vs
, sizeof (*vs
));
3479 vs
->vs_timestamp
= gethrtime() - vs
->vs_timestamp
;
3480 vs
->vs_state
= vd
->vdev_state
;
3481 vs
->vs_rsize
= vdev_get_min_asize(vd
);
3482 if (vd
->vdev_ops
->vdev_op_leaf
)
3483 vs
->vs_rsize
+= VDEV_LABEL_START_SIZE
+
3484 VDEV_LABEL_END_SIZE
;
3486 * Report expandable space on top-level, non-auxillary devices
3487 * only. The expandable space is reported in terms of metaslab
3488 * sized units since that determines how much space the pool
3491 if (vd
->vdev_aux
== NULL
&& tvd
!= NULL
) {
3492 vs
->vs_esize
= P2ALIGN(
3493 vd
->vdev_max_asize
- vd
->vdev_asize
,
3494 1ULL << tvd
->vdev_ms_shift
);
3496 vs
->vs_esize
= vd
->vdev_max_asize
- vd
->vdev_asize
;
3497 if (vd
->vdev_aux
== NULL
&& vd
== vd
->vdev_top
&&
3498 vdev_is_concrete(vd
)) {
3499 vs
->vs_fragmentation
= vd
->vdev_mg
->mg_fragmentation
;
3503 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_READER
) != 0);
3504 vdev_get_stats_ex_impl(vd
, vs
, vsx
);
3505 mutex_exit(&vd
->vdev_stat_lock
);
3509 vdev_get_stats(vdev_t
*vd
, vdev_stat_t
*vs
)
3511 return (vdev_get_stats_ex(vd
, vs
, NULL
));
3515 vdev_clear_stats(vdev_t
*vd
)
3517 mutex_enter(&vd
->vdev_stat_lock
);
3518 vd
->vdev_stat
.vs_space
= 0;
3519 vd
->vdev_stat
.vs_dspace
= 0;
3520 vd
->vdev_stat
.vs_alloc
= 0;
3521 mutex_exit(&vd
->vdev_stat_lock
);
3525 vdev_scan_stat_init(vdev_t
*vd
)
3527 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3529 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3530 vdev_scan_stat_init(vd
->vdev_child
[c
]);
3532 mutex_enter(&vd
->vdev_stat_lock
);
3533 vs
->vs_scan_processed
= 0;
3534 mutex_exit(&vd
->vdev_stat_lock
);
3538 vdev_stat_update(zio_t
*zio
, uint64_t psize
)
3540 spa_t
*spa
= zio
->io_spa
;
3541 vdev_t
*rvd
= spa
->spa_root_vdev
;
3542 vdev_t
*vd
= zio
->io_vd
? zio
->io_vd
: rvd
;
3544 uint64_t txg
= zio
->io_txg
;
3545 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3546 vdev_stat_ex_t
*vsx
= &vd
->vdev_stat_ex
;
3547 zio_type_t type
= zio
->io_type
;
3548 int flags
= zio
->io_flags
;
3551 * If this i/o is a gang leader, it didn't do any actual work.
3553 if (zio
->io_gang_tree
)
3556 if (zio
->io_error
== 0) {
3558 * If this is a root i/o, don't count it -- we've already
3559 * counted the top-level vdevs, and vdev_get_stats() will
3560 * aggregate them when asked. This reduces contention on
3561 * the root vdev_stat_lock and implicitly handles blocks
3562 * that compress away to holes, for which there is no i/o.
3563 * (Holes never create vdev children, so all the counters
3564 * remain zero, which is what we want.)
3566 * Note: this only applies to successful i/o (io_error == 0)
3567 * because unlike i/o counts, errors are not additive.
3568 * When reading a ditto block, for example, failure of
3569 * one top-level vdev does not imply a root-level error.
3574 ASSERT(vd
== zio
->io_vd
);
3576 if (flags
& ZIO_FLAG_IO_BYPASS
)
3579 mutex_enter(&vd
->vdev_stat_lock
);
3581 if (flags
& ZIO_FLAG_IO_REPAIR
) {
3582 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
3583 dsl_scan_phys_t
*scn_phys
=
3584 &spa
->spa_dsl_pool
->dp_scan
->scn_phys
;
3585 uint64_t *processed
= &scn_phys
->scn_processed
;
3588 if (vd
->vdev_ops
->vdev_op_leaf
)
3589 atomic_add_64(processed
, psize
);
3590 vs
->vs_scan_processed
+= psize
;
3593 if (flags
& ZIO_FLAG_SELF_HEAL
)
3594 vs
->vs_self_healed
+= psize
;
3598 * The bytes/ops/histograms are recorded at the leaf level and
3599 * aggregated into the higher level vdevs in vdev_get_stats().
3601 if (vd
->vdev_ops
->vdev_op_leaf
&&
3602 (zio
->io_priority
< ZIO_PRIORITY_NUM_QUEUEABLE
)) {
3605 vs
->vs_bytes
[type
] += psize
;
3607 if (flags
& ZIO_FLAG_DELEGATED
) {
3608 vsx
->vsx_agg_histo
[zio
->io_priority
]
3609 [RQ_HISTO(zio
->io_size
)]++;
3611 vsx
->vsx_ind_histo
[zio
->io_priority
]
3612 [RQ_HISTO(zio
->io_size
)]++;
3615 if (zio
->io_delta
&& zio
->io_delay
) {
3616 vsx
->vsx_queue_histo
[zio
->io_priority
]
3617 [L_HISTO(zio
->io_delta
- zio
->io_delay
)]++;
3618 vsx
->vsx_disk_histo
[type
]
3619 [L_HISTO(zio
->io_delay
)]++;
3620 vsx
->vsx_total_histo
[type
]
3621 [L_HISTO(zio
->io_delta
)]++;
3625 mutex_exit(&vd
->vdev_stat_lock
);
3629 if (flags
& ZIO_FLAG_SPECULATIVE
)
3633 * If this is an I/O error that is going to be retried, then ignore the
3634 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
3635 * hard errors, when in reality they can happen for any number of
3636 * innocuous reasons (bus resets, MPxIO link failure, etc).
3638 if (zio
->io_error
== EIO
&&
3639 !(zio
->io_flags
& ZIO_FLAG_IO_RETRY
))
3643 * Intent logs writes won't propagate their error to the root
3644 * I/O so don't mark these types of failures as pool-level
3647 if (zio
->io_vd
== NULL
&& (zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
3650 mutex_enter(&vd
->vdev_stat_lock
);
3651 if (type
== ZIO_TYPE_READ
&& !vdev_is_dead(vd
)) {
3652 if (zio
->io_error
== ECKSUM
)
3653 vs
->vs_checksum_errors
++;
3655 vs
->vs_read_errors
++;
3657 if (type
== ZIO_TYPE_WRITE
&& !vdev_is_dead(vd
))
3658 vs
->vs_write_errors
++;
3659 mutex_exit(&vd
->vdev_stat_lock
);
3661 if (spa
->spa_load_state
== SPA_LOAD_NONE
&&
3662 type
== ZIO_TYPE_WRITE
&& txg
!= 0 &&
3663 (!(flags
& ZIO_FLAG_IO_REPAIR
) ||
3664 (flags
& ZIO_FLAG_SCAN_THREAD
) ||
3665 spa
->spa_claiming
)) {
3667 * This is either a normal write (not a repair), or it's
3668 * a repair induced by the scrub thread, or it's a repair
3669 * made by zil_claim() during spa_load() in the first txg.
3670 * In the normal case, we commit the DTL change in the same
3671 * txg as the block was born. In the scrub-induced repair
3672 * case, we know that scrubs run in first-pass syncing context,
3673 * so we commit the DTL change in spa_syncing_txg(spa).
3674 * In the zil_claim() case, we commit in spa_first_txg(spa).
3676 * We currently do not make DTL entries for failed spontaneous
3677 * self-healing writes triggered by normal (non-scrubbing)
3678 * reads, because we have no transactional context in which to
3679 * do so -- and it's not clear that it'd be desirable anyway.
3681 if (vd
->vdev_ops
->vdev_op_leaf
) {
3682 uint64_t commit_txg
= txg
;
3683 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
3684 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
3685 ASSERT(spa_sync_pass(spa
) == 1);
3686 vdev_dtl_dirty(vd
, DTL_SCRUB
, txg
, 1);
3687 commit_txg
= spa_syncing_txg(spa
);
3688 } else if (spa
->spa_claiming
) {
3689 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
3690 commit_txg
= spa_first_txg(spa
);
3692 ASSERT(commit_txg
>= spa_syncing_txg(spa
));
3693 if (vdev_dtl_contains(vd
, DTL_MISSING
, txg
, 1))
3695 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3696 vdev_dtl_dirty(pvd
, DTL_PARTIAL
, txg
, 1);
3697 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, commit_txg
);
3700 vdev_dtl_dirty(vd
, DTL_MISSING
, txg
, 1);
3705 * Update the in-core space usage stats for this vdev, its metaslab class,
3706 * and the root vdev.
3709 vdev_space_update(vdev_t
*vd
, int64_t alloc_delta
, int64_t defer_delta
,
3710 int64_t space_delta
)
3712 int64_t dspace_delta
= space_delta
;
3713 spa_t
*spa
= vd
->vdev_spa
;
3714 vdev_t
*rvd
= spa
->spa_root_vdev
;
3715 metaslab_group_t
*mg
= vd
->vdev_mg
;
3716 metaslab_class_t
*mc
= mg
? mg
->mg_class
: NULL
;
3718 ASSERT(vd
== vd
->vdev_top
);
3721 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
3722 * factor. We must calculate this here and not at the root vdev
3723 * because the root vdev's psize-to-asize is simply the max of its
3724 * childrens', thus not accurate enough for us.
3726 ASSERT((dspace_delta
& (SPA_MINBLOCKSIZE
-1)) == 0);
3727 ASSERT(vd
->vdev_deflate_ratio
!= 0 || vd
->vdev_isl2cache
);
3728 dspace_delta
= (dspace_delta
>> SPA_MINBLOCKSHIFT
) *
3729 vd
->vdev_deflate_ratio
;
3731 mutex_enter(&vd
->vdev_stat_lock
);
3732 vd
->vdev_stat
.vs_alloc
+= alloc_delta
;
3733 vd
->vdev_stat
.vs_space
+= space_delta
;
3734 vd
->vdev_stat
.vs_dspace
+= dspace_delta
;
3735 mutex_exit(&vd
->vdev_stat_lock
);
3737 if (mc
== spa_normal_class(spa
)) {
3738 mutex_enter(&rvd
->vdev_stat_lock
);
3739 rvd
->vdev_stat
.vs_alloc
+= alloc_delta
;
3740 rvd
->vdev_stat
.vs_space
+= space_delta
;
3741 rvd
->vdev_stat
.vs_dspace
+= dspace_delta
;
3742 mutex_exit(&rvd
->vdev_stat_lock
);
3746 ASSERT(rvd
== vd
->vdev_parent
);
3747 ASSERT(vd
->vdev_ms_count
!= 0);
3749 metaslab_class_space_update(mc
,
3750 alloc_delta
, defer_delta
, space_delta
, dspace_delta
);
3755 * Mark a top-level vdev's config as dirty, placing it on the dirty list
3756 * so that it will be written out next time the vdev configuration is synced.
3757 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3760 vdev_config_dirty(vdev_t
*vd
)
3762 spa_t
*spa
= vd
->vdev_spa
;
3763 vdev_t
*rvd
= spa
->spa_root_vdev
;
3766 ASSERT(spa_writeable(spa
));
3769 * If this is an aux vdev (as with l2cache and spare devices), then we
3770 * update the vdev config manually and set the sync flag.
3772 if (vd
->vdev_aux
!= NULL
) {
3773 spa_aux_vdev_t
*sav
= vd
->vdev_aux
;
3777 for (c
= 0; c
< sav
->sav_count
; c
++) {
3778 if (sav
->sav_vdevs
[c
] == vd
)
3782 if (c
== sav
->sav_count
) {
3784 * We're being removed. There's nothing more to do.
3786 ASSERT(sav
->sav_sync
== B_TRUE
);
3790 sav
->sav_sync
= B_TRUE
;
3792 if (nvlist_lookup_nvlist_array(sav
->sav_config
,
3793 ZPOOL_CONFIG_L2CACHE
, &aux
, &naux
) != 0) {
3794 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
3795 ZPOOL_CONFIG_SPARES
, &aux
, &naux
) == 0);
3801 * Setting the nvlist in the middle if the array is a little
3802 * sketchy, but it will work.
3804 nvlist_free(aux
[c
]);
3805 aux
[c
] = vdev_config_generate(spa
, vd
, B_TRUE
, 0);
3811 * The dirty list is protected by the SCL_CONFIG lock. The caller
3812 * must either hold SCL_CONFIG as writer, or must be the sync thread
3813 * (which holds SCL_CONFIG as reader). There's only one sync thread,
3814 * so this is sufficient to ensure mutual exclusion.
3816 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
3817 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3818 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
3821 for (c
= 0; c
< rvd
->vdev_children
; c
++)
3822 vdev_config_dirty(rvd
->vdev_child
[c
]);
3824 ASSERT(vd
== vd
->vdev_top
);
3826 if (!list_link_active(&vd
->vdev_config_dirty_node
) &&
3827 vdev_is_concrete(vd
)) {
3828 list_insert_head(&spa
->spa_config_dirty_list
, vd
);
3834 vdev_config_clean(vdev_t
*vd
)
3836 spa_t
*spa
= vd
->vdev_spa
;
3838 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
3839 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3840 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
3842 ASSERT(list_link_active(&vd
->vdev_config_dirty_node
));
3843 list_remove(&spa
->spa_config_dirty_list
, vd
);
3847 * Mark a top-level vdev's state as dirty, so that the next pass of
3848 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3849 * the state changes from larger config changes because they require
3850 * much less locking, and are often needed for administrative actions.
3853 vdev_state_dirty(vdev_t
*vd
)
3855 spa_t
*spa
= vd
->vdev_spa
;
3857 ASSERT(spa_writeable(spa
));
3858 ASSERT(vd
== vd
->vdev_top
);
3861 * The state list is protected by the SCL_STATE lock. The caller
3862 * must either hold SCL_STATE as writer, or must be the sync thread
3863 * (which holds SCL_STATE as reader). There's only one sync thread,
3864 * so this is sufficient to ensure mutual exclusion.
3866 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
3867 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3868 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
3870 if (!list_link_active(&vd
->vdev_state_dirty_node
) &&
3871 vdev_is_concrete(vd
))
3872 list_insert_head(&spa
->spa_state_dirty_list
, vd
);
3876 vdev_state_clean(vdev_t
*vd
)
3878 spa_t
*spa
= vd
->vdev_spa
;
3880 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
3881 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3882 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
3884 ASSERT(list_link_active(&vd
->vdev_state_dirty_node
));
3885 list_remove(&spa
->spa_state_dirty_list
, vd
);
3889 * Propagate vdev state up from children to parent.
3892 vdev_propagate_state(vdev_t
*vd
)
3894 spa_t
*spa
= vd
->vdev_spa
;
3895 vdev_t
*rvd
= spa
->spa_root_vdev
;
3896 int degraded
= 0, faulted
= 0;
3900 if (vd
->vdev_children
> 0) {
3901 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3902 child
= vd
->vdev_child
[c
];
3905 * Don't factor holes or indirect vdevs into the
3908 if (!vdev_is_concrete(child
))
3911 if (!vdev_readable(child
) ||
3912 (!vdev_writeable(child
) && spa_writeable(spa
))) {
3914 * Root special: if there is a top-level log
3915 * device, treat the root vdev as if it were
3918 if (child
->vdev_islog
&& vd
== rvd
)
3922 } else if (child
->vdev_state
<= VDEV_STATE_DEGRADED
) {
3926 if (child
->vdev_stat
.vs_aux
== VDEV_AUX_CORRUPT_DATA
)
3930 vd
->vdev_ops
->vdev_op_state_change(vd
, faulted
, degraded
);
3933 * Root special: if there is a top-level vdev that cannot be
3934 * opened due to corrupted metadata, then propagate the root
3935 * vdev's aux state as 'corrupt' rather than 'insufficient
3938 if (corrupted
&& vd
== rvd
&&
3939 rvd
->vdev_state
== VDEV_STATE_CANT_OPEN
)
3940 vdev_set_state(rvd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3941 VDEV_AUX_CORRUPT_DATA
);
3944 if (vd
->vdev_parent
)
3945 vdev_propagate_state(vd
->vdev_parent
);
3949 * Set a vdev's state. If this is during an open, we don't update the parent
3950 * state, because we're in the process of opening children depth-first.
3951 * Otherwise, we propagate the change to the parent.
3953 * If this routine places a device in a faulted state, an appropriate ereport is
3957 vdev_set_state(vdev_t
*vd
, boolean_t isopen
, vdev_state_t state
, vdev_aux_t aux
)
3959 uint64_t save_state
;
3960 spa_t
*spa
= vd
->vdev_spa
;
3962 if (state
== vd
->vdev_state
) {
3964 * Since vdev_offline() code path is already in an offline
3965 * state we can miss a statechange event to OFFLINE. Check
3966 * the previous state to catch this condition.
3968 if (vd
->vdev_ops
->vdev_op_leaf
&&
3969 (state
== VDEV_STATE_OFFLINE
) &&
3970 (vd
->vdev_prevstate
>= VDEV_STATE_FAULTED
)) {
3971 /* post an offline state change */
3972 zfs_post_state_change(spa
, vd
, vd
->vdev_prevstate
);
3974 vd
->vdev_stat
.vs_aux
= aux
;
3978 save_state
= vd
->vdev_state
;
3980 vd
->vdev_state
= state
;
3981 vd
->vdev_stat
.vs_aux
= aux
;
3984 * If we are setting the vdev state to anything but an open state, then
3985 * always close the underlying device unless the device has requested
3986 * a delayed close (i.e. we're about to remove or fault the device).
3987 * Otherwise, we keep accessible but invalid devices open forever.
3988 * We don't call vdev_close() itself, because that implies some extra
3989 * checks (offline, etc) that we don't want here. This is limited to
3990 * leaf devices, because otherwise closing the device will affect other
3993 if (!vd
->vdev_delayed_close
&& vdev_is_dead(vd
) &&
3994 vd
->vdev_ops
->vdev_op_leaf
)
3995 vd
->vdev_ops
->vdev_op_close(vd
);
3997 if (vd
->vdev_removed
&&
3998 state
== VDEV_STATE_CANT_OPEN
&&
3999 (aux
== VDEV_AUX_OPEN_FAILED
|| vd
->vdev_checkremove
)) {
4001 * If the previous state is set to VDEV_STATE_REMOVED, then this
4002 * device was previously marked removed and someone attempted to
4003 * reopen it. If this failed due to a nonexistent device, then
4004 * keep the device in the REMOVED state. We also let this be if
4005 * it is one of our special test online cases, which is only
4006 * attempting to online the device and shouldn't generate an FMA
4009 vd
->vdev_state
= VDEV_STATE_REMOVED
;
4010 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
4011 } else if (state
== VDEV_STATE_REMOVED
) {
4012 vd
->vdev_removed
= B_TRUE
;
4013 } else if (state
== VDEV_STATE_CANT_OPEN
) {
4015 * If we fail to open a vdev during an import or recovery, we
4016 * mark it as "not available", which signifies that it was
4017 * never there to begin with. Failure to open such a device
4018 * is not considered an error.
4020 if ((spa_load_state(spa
) == SPA_LOAD_IMPORT
||
4021 spa_load_state(spa
) == SPA_LOAD_RECOVER
) &&
4022 vd
->vdev_ops
->vdev_op_leaf
)
4023 vd
->vdev_not_present
= 1;
4026 * Post the appropriate ereport. If the 'prevstate' field is
4027 * set to something other than VDEV_STATE_UNKNOWN, it indicates
4028 * that this is part of a vdev_reopen(). In this case, we don't
4029 * want to post the ereport if the device was already in the
4030 * CANT_OPEN state beforehand.
4032 * If the 'checkremove' flag is set, then this is an attempt to
4033 * online the device in response to an insertion event. If we
4034 * hit this case, then we have detected an insertion event for a
4035 * faulted or offline device that wasn't in the removed state.
4036 * In this scenario, we don't post an ereport because we are
4037 * about to replace the device, or attempt an online with
4038 * vdev_forcefault, which will generate the fault for us.
4040 if ((vd
->vdev_prevstate
!= state
|| vd
->vdev_forcefault
) &&
4041 !vd
->vdev_not_present
&& !vd
->vdev_checkremove
&&
4042 vd
!= spa
->spa_root_vdev
) {
4046 case VDEV_AUX_OPEN_FAILED
:
4047 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED
;
4049 case VDEV_AUX_CORRUPT_DATA
:
4050 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA
;
4052 case VDEV_AUX_NO_REPLICAS
:
4053 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS
;
4055 case VDEV_AUX_BAD_GUID_SUM
:
4056 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM
;
4058 case VDEV_AUX_TOO_SMALL
:
4059 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL
;
4061 case VDEV_AUX_BAD_LABEL
:
4062 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL
;
4064 case VDEV_AUX_BAD_ASHIFT
:
4065 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
;
4068 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN
;
4071 zfs_ereport_post(class, spa
, vd
, NULL
, NULL
,
4075 /* Erase any notion of persistent removed state */
4076 vd
->vdev_removed
= B_FALSE
;
4078 vd
->vdev_removed
= B_FALSE
;
4082 * Notify ZED of any significant state-change on a leaf vdev.
4085 if (vd
->vdev_ops
->vdev_op_leaf
) {
4086 /* preserve original state from a vdev_reopen() */
4087 if ((vd
->vdev_prevstate
!= VDEV_STATE_UNKNOWN
) &&
4088 (vd
->vdev_prevstate
!= vd
->vdev_state
) &&
4089 (save_state
<= VDEV_STATE_CLOSED
))
4090 save_state
= vd
->vdev_prevstate
;
4092 /* filter out state change due to initial vdev_open */
4093 if (save_state
> VDEV_STATE_CLOSED
)
4094 zfs_post_state_change(spa
, vd
, save_state
);
4097 if (!isopen
&& vd
->vdev_parent
)
4098 vdev_propagate_state(vd
->vdev_parent
);
4102 vdev_children_are_offline(vdev_t
*vd
)
4104 ASSERT(!vd
->vdev_ops
->vdev_op_leaf
);
4106 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
4107 if (vd
->vdev_child
[i
]->vdev_state
!= VDEV_STATE_OFFLINE
)
4115 * Check the vdev configuration to ensure that it's capable of supporting
4116 * a root pool. We do not support partial configuration.
4119 vdev_is_bootable(vdev_t
*vd
)
4121 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4122 const char *vdev_type
= vd
->vdev_ops
->vdev_op_type
;
4124 if (strcmp(vdev_type
, VDEV_TYPE_MISSING
) == 0 ||
4125 strcmp(vdev_type
, VDEV_TYPE_INDIRECT
) == 0) {
4130 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4131 if (!vdev_is_bootable(vd
->vdev_child
[c
]))
4138 vdev_is_concrete(vdev_t
*vd
)
4140 vdev_ops_t
*ops
= vd
->vdev_ops
;
4141 if (ops
== &vdev_indirect_ops
|| ops
== &vdev_hole_ops
||
4142 ops
== &vdev_missing_ops
|| ops
== &vdev_root_ops
) {
4150 * Determine if a log device has valid content. If the vdev was
4151 * removed or faulted in the MOS config then we know that
4152 * the content on the log device has already been written to the pool.
4155 vdev_log_state_valid(vdev_t
*vd
)
4157 if (vd
->vdev_ops
->vdev_op_leaf
&& !vd
->vdev_faulted
&&
4161 for (int c
= 0; c
< vd
->vdev_children
; c
++)
4162 if (vdev_log_state_valid(vd
->vdev_child
[c
]))
4169 * Expand a vdev if possible.
4172 vdev_expand(vdev_t
*vd
, uint64_t txg
)
4174 ASSERT(vd
->vdev_top
== vd
);
4175 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4177 vdev_set_deflate_ratio(vd
);
4179 if ((vd
->vdev_asize
>> vd
->vdev_ms_shift
) > vd
->vdev_ms_count
&&
4180 vdev_is_concrete(vd
)) {
4181 VERIFY(vdev_metaslab_init(vd
, txg
) == 0);
4182 vdev_config_dirty(vd
);
4190 vdev_split(vdev_t
*vd
)
4192 vdev_t
*cvd
, *pvd
= vd
->vdev_parent
;
4194 vdev_remove_child(pvd
, vd
);
4195 vdev_compact_children(pvd
);
4197 cvd
= pvd
->vdev_child
[0];
4198 if (pvd
->vdev_children
== 1) {
4199 vdev_remove_parent(cvd
);
4200 cvd
->vdev_splitting
= B_TRUE
;
4202 vdev_propagate_state(cvd
);
4206 vdev_deadman(vdev_t
*vd
, char *tag
)
4208 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4209 vdev_t
*cvd
= vd
->vdev_child
[c
];
4211 vdev_deadman(cvd
, tag
);
4214 if (vd
->vdev_ops
->vdev_op_leaf
) {
4215 vdev_queue_t
*vq
= &vd
->vdev_queue
;
4217 mutex_enter(&vq
->vq_lock
);
4218 if (avl_numnodes(&vq
->vq_active_tree
) > 0) {
4219 spa_t
*spa
= vd
->vdev_spa
;
4223 zfs_dbgmsg("slow vdev: %s has %d active IOs",
4224 vd
->vdev_path
, avl_numnodes(&vq
->vq_active_tree
));
4227 * Look at the head of all the pending queues,
4228 * if any I/O has been outstanding for longer than
4229 * the spa_deadman_synctime invoke the deadman logic.
4231 fio
= avl_first(&vq
->vq_active_tree
);
4232 delta
= gethrtime() - fio
->io_timestamp
;
4233 if (delta
> spa_deadman_synctime(spa
))
4234 zio_deadman(fio
, tag
);
4236 mutex_exit(&vq
->vq_lock
);
4240 #if defined(_KERNEL)
4241 EXPORT_SYMBOL(vdev_fault
);
4242 EXPORT_SYMBOL(vdev_degrade
);
4243 EXPORT_SYMBOL(vdev_online
);
4244 EXPORT_SYMBOL(vdev_offline
);
4245 EXPORT_SYMBOL(vdev_clear
);
4247 module_param(metaslabs_per_vdev
, int, 0644);
4248 MODULE_PARM_DESC(metaslabs_per_vdev
,
4249 "Divide added vdev into approximately (but no more than) this number "
4252 module_param(zfs_delays_per_second
, uint
, 0644);
4253 MODULE_PARM_DESC(zfs_delays_per_second
, "Rate limit delay events to this many "
4254 "IO delays per second");
4256 module_param(zfs_checksums_per_second
, uint
, 0644);
4257 MODULE_PARM_DESC(zfs_checksums_per_second
, "Rate limit checksum events "
4258 "to this many checksum errors per second (do not set below zed"
4261 module_param(zfs_scan_ignore_errors
, int, 0644);
4262 MODULE_PARM_DESC(zfs_scan_ignore_errors
,
4263 "Ignore errors during resilver/scrub");
4265 module_param(vdev_validate_skip
, int, 0644);
4266 MODULE_PARM_DESC(vdev_validate_skip
,
4267 "Bypass vdev_validate()");