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.
1696 if (spa
->spa_extreme_rewind
|| spa_last_synced_txg(spa
) == 0)
1699 txg
= spa_last_synced_txg(spa
);
1701 if ((label
= vdev_label_read_config(vd
, txg
)) == NULL
) {
1702 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1703 VDEV_AUX_BAD_LABEL
);
1704 vdev_dbgmsg(vd
, "vdev_validate: failed reading config");
1709 * Determine if this vdev has been split off into another
1710 * pool. If so, then refuse to open it.
1712 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_SPLIT_GUID
,
1713 &aux_guid
) == 0 && aux_guid
== spa_guid(spa
)) {
1714 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1715 VDEV_AUX_SPLIT_POOL
);
1717 vdev_dbgmsg(vd
, "vdev_validate: vdev split into other pool");
1721 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0) {
1722 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1723 VDEV_AUX_CORRUPT_DATA
);
1725 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1726 ZPOOL_CONFIG_POOL_GUID
);
1731 * If config is not trusted then ignore the spa guid check. This is
1732 * necessary because if the machine crashed during a re-guid the new
1733 * guid might have been written to all of the vdev labels, but not the
1734 * cached config. The check will be performed again once we have the
1735 * trusted config from the MOS.
1737 if (spa
->spa_trust_config
&& guid
!= spa_guid(spa
)) {
1738 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1739 VDEV_AUX_CORRUPT_DATA
);
1741 vdev_dbgmsg(vd
, "vdev_validate: vdev label pool_guid doesn't "
1742 "match config (%llu != %llu)", (u_longlong_t
)guid
,
1743 (u_longlong_t
)spa_guid(spa
));
1747 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
)
1748 != 0 || nvlist_lookup_uint64(nvl
, ZPOOL_CONFIG_ORIG_GUID
,
1752 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0) {
1753 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1754 VDEV_AUX_CORRUPT_DATA
);
1756 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1761 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_TOP_GUID
, &top_guid
)
1763 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1764 VDEV_AUX_CORRUPT_DATA
);
1766 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1767 ZPOOL_CONFIG_TOP_GUID
);
1772 * If this vdev just became a top-level vdev because its sibling was
1773 * detached, it will have adopted the parent's vdev guid -- but the
1774 * label may or may not be on disk yet. Fortunately, either version
1775 * of the label will have the same top guid, so if we're a top-level
1776 * vdev, we can safely compare to that instead.
1777 * However, if the config comes from a cachefile that failed to update
1778 * after the detach, a top-level vdev will appear as a non top-level
1779 * vdev in the config. Also relax the constraints if we perform an
1782 * If we split this vdev off instead, then we also check the
1783 * original pool's guid. We don't want to consider the vdev
1784 * corrupt if it is partway through a split operation.
1786 if (vd
->vdev_guid
!= guid
&& vd
->vdev_guid
!= aux_guid
) {
1787 boolean_t mismatch
= B_FALSE
;
1788 if (spa
->spa_trust_config
&& !spa
->spa_extreme_rewind
) {
1789 if (vd
!= vd
->vdev_top
|| vd
->vdev_guid
!= top_guid
)
1792 if (vd
->vdev_guid
!= top_guid
&&
1793 vd
->vdev_top
->vdev_guid
!= guid
)
1798 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1799 VDEV_AUX_CORRUPT_DATA
);
1801 vdev_dbgmsg(vd
, "vdev_validate: config guid "
1802 "doesn't match label guid");
1803 vdev_dbgmsg(vd
, "CONFIG: guid %llu, top_guid %llu",
1804 (u_longlong_t
)vd
->vdev_guid
,
1805 (u_longlong_t
)vd
->vdev_top
->vdev_guid
);
1806 vdev_dbgmsg(vd
, "LABEL: guid %llu, top_guid %llu, "
1807 "aux_guid %llu", (u_longlong_t
)guid
,
1808 (u_longlong_t
)top_guid
, (u_longlong_t
)aux_guid
);
1813 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
,
1815 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1816 VDEV_AUX_CORRUPT_DATA
);
1818 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1819 ZPOOL_CONFIG_POOL_STATE
);
1826 * If this is a verbatim import, no need to check the
1827 * state of the pool.
1829 if (!(spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) &&
1830 spa_load_state(spa
) == SPA_LOAD_OPEN
&&
1831 state
!= POOL_STATE_ACTIVE
) {
1832 vdev_dbgmsg(vd
, "vdev_validate: invalid pool state (%llu) "
1833 "for spa %s", (u_longlong_t
)state
, spa
->spa_name
);
1834 return (SET_ERROR(EBADF
));
1838 * If we were able to open and validate a vdev that was
1839 * previously marked permanently unavailable, clear that state
1842 if (vd
->vdev_not_present
)
1843 vd
->vdev_not_present
= 0;
1849 vdev_copy_path_impl(vdev_t
*svd
, vdev_t
*dvd
)
1851 if (svd
->vdev_path
!= NULL
&& dvd
->vdev_path
!= NULL
) {
1852 if (strcmp(svd
->vdev_path
, dvd
->vdev_path
) != 0) {
1853 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
1854 "from '%s' to '%s'", (u_longlong_t
)dvd
->vdev_guid
,
1855 dvd
->vdev_path
, svd
->vdev_path
);
1856 spa_strfree(dvd
->vdev_path
);
1857 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
1859 } else if (svd
->vdev_path
!= NULL
) {
1860 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
1861 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
1862 (u_longlong_t
)dvd
->vdev_guid
, dvd
->vdev_path
);
1867 * Recursively copy vdev paths from one vdev to another. Source and destination
1868 * vdev trees must have same geometry otherwise return error. Intended to copy
1869 * paths from userland config into MOS config.
1872 vdev_copy_path_strict(vdev_t
*svd
, vdev_t
*dvd
)
1874 if ((svd
->vdev_ops
== &vdev_missing_ops
) ||
1875 (svd
->vdev_ishole
&& dvd
->vdev_ishole
) ||
1876 (dvd
->vdev_ops
== &vdev_indirect_ops
))
1879 if (svd
->vdev_ops
!= dvd
->vdev_ops
) {
1880 vdev_dbgmsg(svd
, "vdev_copy_path: vdev type mismatch: %s != %s",
1881 svd
->vdev_ops
->vdev_op_type
, dvd
->vdev_ops
->vdev_op_type
);
1882 return (SET_ERROR(EINVAL
));
1885 if (svd
->vdev_guid
!= dvd
->vdev_guid
) {
1886 vdev_dbgmsg(svd
, "vdev_copy_path: guids mismatch (%llu != "
1887 "%llu)", (u_longlong_t
)svd
->vdev_guid
,
1888 (u_longlong_t
)dvd
->vdev_guid
);
1889 return (SET_ERROR(EINVAL
));
1892 if (svd
->vdev_children
!= dvd
->vdev_children
) {
1893 vdev_dbgmsg(svd
, "vdev_copy_path: children count mismatch: "
1894 "%llu != %llu", (u_longlong_t
)svd
->vdev_children
,
1895 (u_longlong_t
)dvd
->vdev_children
);
1896 return (SET_ERROR(EINVAL
));
1899 for (uint64_t i
= 0; i
< svd
->vdev_children
; i
++) {
1900 int error
= vdev_copy_path_strict(svd
->vdev_child
[i
],
1901 dvd
->vdev_child
[i
]);
1906 if (svd
->vdev_ops
->vdev_op_leaf
)
1907 vdev_copy_path_impl(svd
, dvd
);
1913 vdev_copy_path_search(vdev_t
*stvd
, vdev_t
*dvd
)
1915 ASSERT(stvd
->vdev_top
== stvd
);
1916 ASSERT3U(stvd
->vdev_id
, ==, dvd
->vdev_top
->vdev_id
);
1918 for (uint64_t i
= 0; i
< dvd
->vdev_children
; i
++) {
1919 vdev_copy_path_search(stvd
, dvd
->vdev_child
[i
]);
1922 if (!dvd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(dvd
))
1926 * The idea here is that while a vdev can shift positions within
1927 * a top vdev (when replacing, attaching mirror, etc.) it cannot
1928 * step outside of it.
1930 vdev_t
*vd
= vdev_lookup_by_guid(stvd
, dvd
->vdev_guid
);
1932 if (vd
== NULL
|| vd
->vdev_ops
!= dvd
->vdev_ops
)
1935 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1937 vdev_copy_path_impl(vd
, dvd
);
1941 * Recursively copy vdev paths from one root vdev to another. Source and
1942 * destination vdev trees may differ in geometry. For each destination leaf
1943 * vdev, search a vdev with the same guid and top vdev id in the source.
1944 * Intended to copy paths from userland config into MOS config.
1947 vdev_copy_path_relaxed(vdev_t
*srvd
, vdev_t
*drvd
)
1949 uint64_t children
= MIN(srvd
->vdev_children
, drvd
->vdev_children
);
1950 ASSERT(srvd
->vdev_ops
== &vdev_root_ops
);
1951 ASSERT(drvd
->vdev_ops
== &vdev_root_ops
);
1953 for (uint64_t i
= 0; i
< children
; i
++) {
1954 vdev_copy_path_search(srvd
->vdev_child
[i
],
1955 drvd
->vdev_child
[i
]);
1960 * Close a virtual device.
1963 vdev_close(vdev_t
*vd
)
1965 vdev_t
*pvd
= vd
->vdev_parent
;
1966 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
1968 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1971 * If our parent is reopening, then we are as well, unless we are
1974 if (pvd
!= NULL
&& pvd
->vdev_reopening
)
1975 vd
->vdev_reopening
= (pvd
->vdev_reopening
&& !vd
->vdev_offline
);
1977 vd
->vdev_ops
->vdev_op_close(vd
);
1979 vdev_cache_purge(vd
);
1982 * We record the previous state before we close it, so that if we are
1983 * doing a reopen(), we don't generate FMA ereports if we notice that
1984 * it's still faulted.
1986 vd
->vdev_prevstate
= vd
->vdev_state
;
1988 if (vd
->vdev_offline
)
1989 vd
->vdev_state
= VDEV_STATE_OFFLINE
;
1991 vd
->vdev_state
= VDEV_STATE_CLOSED
;
1992 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1996 vdev_hold(vdev_t
*vd
)
1998 spa_t
*spa
= vd
->vdev_spa
;
2000 ASSERT(spa_is_root(spa
));
2001 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
2004 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2005 vdev_hold(vd
->vdev_child
[c
]);
2007 if (vd
->vdev_ops
->vdev_op_leaf
)
2008 vd
->vdev_ops
->vdev_op_hold(vd
);
2012 vdev_rele(vdev_t
*vd
)
2014 ASSERT(spa_is_root(vd
->vdev_spa
));
2015 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2016 vdev_rele(vd
->vdev_child
[c
]);
2018 if (vd
->vdev_ops
->vdev_op_leaf
)
2019 vd
->vdev_ops
->vdev_op_rele(vd
);
2023 * Reopen all interior vdevs and any unopened leaves. We don't actually
2024 * reopen leaf vdevs which had previously been opened as they might deadlock
2025 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2026 * If the leaf has never been opened then open it, as usual.
2029 vdev_reopen(vdev_t
*vd
)
2031 spa_t
*spa
= vd
->vdev_spa
;
2033 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2035 /* set the reopening flag unless we're taking the vdev offline */
2036 vd
->vdev_reopening
= !vd
->vdev_offline
;
2038 (void) vdev_open(vd
);
2041 * Call vdev_validate() here to make sure we have the same device.
2042 * Otherwise, a device with an invalid label could be successfully
2043 * opened in response to vdev_reopen().
2046 (void) vdev_validate_aux(vd
);
2047 if (vdev_readable(vd
) && vdev_writeable(vd
) &&
2048 vd
->vdev_aux
== &spa
->spa_l2cache
&&
2049 !l2arc_vdev_present(vd
))
2050 l2arc_add_vdev(spa
, vd
);
2052 (void) vdev_validate(vd
);
2056 * Reassess parent vdev's health.
2058 vdev_propagate_state(vd
);
2062 vdev_create(vdev_t
*vd
, uint64_t txg
, boolean_t isreplacing
)
2067 * Normally, partial opens (e.g. of a mirror) are allowed.
2068 * For a create, however, we want to fail the request if
2069 * there are any components we can't open.
2071 error
= vdev_open(vd
);
2073 if (error
|| vd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
2075 return (error
? error
: ENXIO
);
2079 * Recursively load DTLs and initialize all labels.
2081 if ((error
= vdev_dtl_load(vd
)) != 0 ||
2082 (error
= vdev_label_init(vd
, txg
, isreplacing
?
2083 VDEV_LABEL_REPLACE
: VDEV_LABEL_CREATE
)) != 0) {
2092 vdev_metaslab_set_size(vdev_t
*vd
)
2095 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
2097 vd
->vdev_ms_shift
= highbit64(vd
->vdev_asize
/ metaslabs_per_vdev
);
2098 vd
->vdev_ms_shift
= MAX(vd
->vdev_ms_shift
, SPA_MAXBLOCKSHIFT
);
2102 vdev_dirty(vdev_t
*vd
, int flags
, void *arg
, uint64_t txg
)
2104 ASSERT(vd
== vd
->vdev_top
);
2105 /* indirect vdevs don't have metaslabs or dtls */
2106 ASSERT(vdev_is_concrete(vd
) || flags
== 0);
2107 ASSERT(ISP2(flags
));
2108 ASSERT(spa_writeable(vd
->vdev_spa
));
2110 if (flags
& VDD_METASLAB
)
2111 (void) txg_list_add(&vd
->vdev_ms_list
, arg
, txg
);
2113 if (flags
& VDD_DTL
)
2114 (void) txg_list_add(&vd
->vdev_dtl_list
, arg
, txg
);
2116 (void) txg_list_add(&vd
->vdev_spa
->spa_vdev_txg_list
, vd
, txg
);
2120 vdev_dirty_leaves(vdev_t
*vd
, int flags
, uint64_t txg
)
2122 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2123 vdev_dirty_leaves(vd
->vdev_child
[c
], flags
, txg
);
2125 if (vd
->vdev_ops
->vdev_op_leaf
)
2126 vdev_dirty(vd
->vdev_top
, flags
, vd
, txg
);
2132 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2133 * the vdev has less than perfect replication. There are four kinds of DTL:
2135 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2137 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2139 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2140 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2141 * txgs that was scrubbed.
2143 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2144 * persistent errors or just some device being offline.
2145 * Unlike the other three, the DTL_OUTAGE map is not generally
2146 * maintained; it's only computed when needed, typically to
2147 * determine whether a device can be detached.
2149 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2150 * either has the data or it doesn't.
2152 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2153 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2154 * if any child is less than fully replicated, then so is its parent.
2155 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2156 * comprising only those txgs which appear in 'maxfaults' or more children;
2157 * those are the txgs we don't have enough replication to read. For example,
2158 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2159 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2160 * two child DTL_MISSING maps.
2162 * It should be clear from the above that to compute the DTLs and outage maps
2163 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2164 * Therefore, that is all we keep on disk. When loading the pool, or after
2165 * a configuration change, we generate all other DTLs from first principles.
2168 vdev_dtl_dirty(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2170 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2172 ASSERT(t
< DTL_TYPES
);
2173 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2174 ASSERT(spa_writeable(vd
->vdev_spa
));
2176 mutex_enter(&vd
->vdev_dtl_lock
);
2177 if (!range_tree_contains(rt
, txg
, size
))
2178 range_tree_add(rt
, txg
, size
);
2179 mutex_exit(&vd
->vdev_dtl_lock
);
2183 vdev_dtl_contains(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2185 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2186 boolean_t dirty
= B_FALSE
;
2188 ASSERT(t
< DTL_TYPES
);
2189 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2192 * While we are loading the pool, the DTLs have not been loaded yet.
2193 * Ignore the DTLs and try all devices. This avoids a recursive
2194 * mutex enter on the vdev_dtl_lock, and also makes us try hard
2195 * when loading the pool (relying on the checksum to ensure that
2196 * we get the right data -- note that we while loading, we are
2197 * only reading the MOS, which is always checksummed).
2199 if (vd
->vdev_spa
->spa_load_state
!= SPA_LOAD_NONE
)
2202 mutex_enter(&vd
->vdev_dtl_lock
);
2203 if (range_tree_space(rt
) != 0)
2204 dirty
= range_tree_contains(rt
, txg
, size
);
2205 mutex_exit(&vd
->vdev_dtl_lock
);
2211 vdev_dtl_empty(vdev_t
*vd
, vdev_dtl_type_t t
)
2213 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2216 mutex_enter(&vd
->vdev_dtl_lock
);
2217 empty
= (range_tree_space(rt
) == 0);
2218 mutex_exit(&vd
->vdev_dtl_lock
);
2224 * Returns B_TRUE if vdev determines offset needs to be resilvered.
2227 vdev_dtl_need_resilver(vdev_t
*vd
, uint64_t offset
, size_t psize
)
2229 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2231 if (vd
->vdev_ops
->vdev_op_need_resilver
== NULL
||
2232 vd
->vdev_ops
->vdev_op_leaf
)
2235 return (vd
->vdev_ops
->vdev_op_need_resilver(vd
, offset
, psize
));
2239 * Returns the lowest txg in the DTL range.
2242 vdev_dtl_min(vdev_t
*vd
)
2246 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2247 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2248 ASSERT0(vd
->vdev_children
);
2250 rs
= avl_first(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
2251 return (rs
->rs_start
- 1);
2255 * Returns the highest txg in the DTL.
2258 vdev_dtl_max(vdev_t
*vd
)
2262 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2263 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2264 ASSERT0(vd
->vdev_children
);
2266 rs
= avl_last(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
2267 return (rs
->rs_end
);
2271 * Determine if a resilvering vdev should remove any DTL entries from
2272 * its range. If the vdev was resilvering for the entire duration of the
2273 * scan then it should excise that range from its DTLs. Otherwise, this
2274 * vdev is considered partially resilvered and should leave its DTL
2275 * entries intact. The comment in vdev_dtl_reassess() describes how we
2279 vdev_dtl_should_excise(vdev_t
*vd
)
2281 spa_t
*spa
= vd
->vdev_spa
;
2282 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
2284 ASSERT0(scn
->scn_phys
.scn_errors
);
2285 ASSERT0(vd
->vdev_children
);
2287 if (vd
->vdev_state
< VDEV_STATE_DEGRADED
)
2290 if (vd
->vdev_resilver_txg
== 0 ||
2291 range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) == 0)
2295 * When a resilver is initiated the scan will assign the scn_max_txg
2296 * value to the highest txg value that exists in all DTLs. If this
2297 * device's max DTL is not part of this scan (i.e. it is not in
2298 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2301 if (vdev_dtl_max(vd
) <= scn
->scn_phys
.scn_max_txg
) {
2302 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <=, vdev_dtl_min(vd
));
2303 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <, vd
->vdev_resilver_txg
);
2304 ASSERT3U(vd
->vdev_resilver_txg
, <=, scn
->scn_phys
.scn_max_txg
);
2311 * Reassess DTLs after a config change or scrub completion.
2314 vdev_dtl_reassess(vdev_t
*vd
, uint64_t txg
, uint64_t scrub_txg
, int scrub_done
)
2316 spa_t
*spa
= vd
->vdev_spa
;
2320 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
2322 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2323 vdev_dtl_reassess(vd
->vdev_child
[c
], txg
,
2324 scrub_txg
, scrub_done
);
2326 if (vd
== spa
->spa_root_vdev
|| !vdev_is_concrete(vd
) || vd
->vdev_aux
)
2329 if (vd
->vdev_ops
->vdev_op_leaf
) {
2330 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
2332 mutex_enter(&vd
->vdev_dtl_lock
);
2335 * If requested, pretend the scan completed cleanly.
2337 if (zfs_scan_ignore_errors
&& scn
)
2338 scn
->scn_phys
.scn_errors
= 0;
2341 * If we've completed a scan cleanly then determine
2342 * if this vdev should remove any DTLs. We only want to
2343 * excise regions on vdevs that were available during
2344 * the entire duration of this scan.
2346 if (scrub_txg
!= 0 &&
2347 (spa
->spa_scrub_started
||
2348 (scn
!= NULL
&& scn
->scn_phys
.scn_errors
== 0)) &&
2349 vdev_dtl_should_excise(vd
)) {
2351 * We completed a scrub up to scrub_txg. If we
2352 * did it without rebooting, then the scrub dtl
2353 * will be valid, so excise the old region and
2354 * fold in the scrub dtl. Otherwise, leave the
2355 * dtl as-is if there was an error.
2357 * There's little trick here: to excise the beginning
2358 * of the DTL_MISSING map, we put it into a reference
2359 * tree and then add a segment with refcnt -1 that
2360 * covers the range [0, scrub_txg). This means
2361 * that each txg in that range has refcnt -1 or 0.
2362 * We then add DTL_SCRUB with a refcnt of 2, so that
2363 * entries in the range [0, scrub_txg) will have a
2364 * positive refcnt -- either 1 or 2. We then convert
2365 * the reference tree into the new DTL_MISSING map.
2367 space_reftree_create(&reftree
);
2368 space_reftree_add_map(&reftree
,
2369 vd
->vdev_dtl
[DTL_MISSING
], 1);
2370 space_reftree_add_seg(&reftree
, 0, scrub_txg
, -1);
2371 space_reftree_add_map(&reftree
,
2372 vd
->vdev_dtl
[DTL_SCRUB
], 2);
2373 space_reftree_generate_map(&reftree
,
2374 vd
->vdev_dtl
[DTL_MISSING
], 1);
2375 space_reftree_destroy(&reftree
);
2377 range_tree_vacate(vd
->vdev_dtl
[DTL_PARTIAL
], NULL
, NULL
);
2378 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2379 range_tree_add
, vd
->vdev_dtl
[DTL_PARTIAL
]);
2381 range_tree_vacate(vd
->vdev_dtl
[DTL_SCRUB
], NULL
, NULL
);
2382 range_tree_vacate(vd
->vdev_dtl
[DTL_OUTAGE
], NULL
, NULL
);
2383 if (!vdev_readable(vd
))
2384 range_tree_add(vd
->vdev_dtl
[DTL_OUTAGE
], 0, -1ULL);
2386 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2387 range_tree_add
, vd
->vdev_dtl
[DTL_OUTAGE
]);
2390 * If the vdev was resilvering and no longer has any
2391 * DTLs then reset its resilvering flag and dirty
2392 * the top level so that we persist the change.
2394 if (vd
->vdev_resilver_txg
!= 0 &&
2395 range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) == 0 &&
2396 range_tree_space(vd
->vdev_dtl
[DTL_OUTAGE
]) == 0) {
2397 vd
->vdev_resilver_txg
= 0;
2398 vdev_config_dirty(vd
->vdev_top
);
2401 mutex_exit(&vd
->vdev_dtl_lock
);
2404 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, txg
);
2408 mutex_enter(&vd
->vdev_dtl_lock
);
2409 for (int t
= 0; t
< DTL_TYPES
; t
++) {
2410 /* account for child's outage in parent's missing map */
2411 int s
= (t
== DTL_MISSING
) ? DTL_OUTAGE
: t
;
2413 continue; /* leaf vdevs only */
2414 if (t
== DTL_PARTIAL
)
2415 minref
= 1; /* i.e. non-zero */
2416 else if (vd
->vdev_nparity
!= 0)
2417 minref
= vd
->vdev_nparity
+ 1; /* RAID-Z */
2419 minref
= vd
->vdev_children
; /* any kind of mirror */
2420 space_reftree_create(&reftree
);
2421 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2422 vdev_t
*cvd
= vd
->vdev_child
[c
];
2423 mutex_enter(&cvd
->vdev_dtl_lock
);
2424 space_reftree_add_map(&reftree
, cvd
->vdev_dtl
[s
], 1);
2425 mutex_exit(&cvd
->vdev_dtl_lock
);
2427 space_reftree_generate_map(&reftree
, vd
->vdev_dtl
[t
], minref
);
2428 space_reftree_destroy(&reftree
);
2430 mutex_exit(&vd
->vdev_dtl_lock
);
2434 vdev_dtl_load(vdev_t
*vd
)
2436 spa_t
*spa
= vd
->vdev_spa
;
2437 objset_t
*mos
= spa
->spa_meta_objset
;
2440 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_dtl_object
!= 0) {
2441 ASSERT(vdev_is_concrete(vd
));
2443 error
= space_map_open(&vd
->vdev_dtl_sm
, mos
,
2444 vd
->vdev_dtl_object
, 0, -1ULL, 0);
2447 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2449 mutex_enter(&vd
->vdev_dtl_lock
);
2452 * Now that we've opened the space_map we need to update
2455 space_map_update(vd
->vdev_dtl_sm
);
2457 error
= space_map_load(vd
->vdev_dtl_sm
,
2458 vd
->vdev_dtl
[DTL_MISSING
], SM_ALLOC
);
2459 mutex_exit(&vd
->vdev_dtl_lock
);
2464 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2465 error
= vdev_dtl_load(vd
->vdev_child
[c
]);
2474 vdev_destroy_unlink_zap(vdev_t
*vd
, uint64_t zapobj
, dmu_tx_t
*tx
)
2476 spa_t
*spa
= vd
->vdev_spa
;
2478 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zapobj
, tx
));
2479 VERIFY0(zap_remove_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2484 vdev_create_link_zap(vdev_t
*vd
, dmu_tx_t
*tx
)
2486 spa_t
*spa
= vd
->vdev_spa
;
2487 uint64_t zap
= zap_create(spa
->spa_meta_objset
, DMU_OTN_ZAP_METADATA
,
2488 DMU_OT_NONE
, 0, tx
);
2491 VERIFY0(zap_add_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2498 vdev_construct_zaps(vdev_t
*vd
, dmu_tx_t
*tx
)
2500 if (vd
->vdev_ops
!= &vdev_hole_ops
&&
2501 vd
->vdev_ops
!= &vdev_missing_ops
&&
2502 vd
->vdev_ops
!= &vdev_root_ops
&&
2503 !vd
->vdev_top
->vdev_removing
) {
2504 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_leaf_zap
== 0) {
2505 vd
->vdev_leaf_zap
= vdev_create_link_zap(vd
, tx
);
2507 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
== 0) {
2508 vd
->vdev_top_zap
= vdev_create_link_zap(vd
, tx
);
2511 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2512 vdev_construct_zaps(vd
->vdev_child
[i
], tx
);
2517 vdev_dtl_sync(vdev_t
*vd
, uint64_t txg
)
2519 spa_t
*spa
= vd
->vdev_spa
;
2520 range_tree_t
*rt
= vd
->vdev_dtl
[DTL_MISSING
];
2521 objset_t
*mos
= spa
->spa_meta_objset
;
2522 range_tree_t
*rtsync
;
2524 uint64_t object
= space_map_object(vd
->vdev_dtl_sm
);
2526 ASSERT(vdev_is_concrete(vd
));
2527 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2529 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2531 if (vd
->vdev_detached
|| vd
->vdev_top
->vdev_removing
) {
2532 mutex_enter(&vd
->vdev_dtl_lock
);
2533 space_map_free(vd
->vdev_dtl_sm
, tx
);
2534 space_map_close(vd
->vdev_dtl_sm
);
2535 vd
->vdev_dtl_sm
= NULL
;
2536 mutex_exit(&vd
->vdev_dtl_lock
);
2539 * We only destroy the leaf ZAP for detached leaves or for
2540 * removed log devices. Removed data devices handle leaf ZAP
2541 * cleanup later, once cancellation is no longer possible.
2543 if (vd
->vdev_leaf_zap
!= 0 && (vd
->vdev_detached
||
2544 vd
->vdev_top
->vdev_islog
)) {
2545 vdev_destroy_unlink_zap(vd
, vd
->vdev_leaf_zap
, tx
);
2546 vd
->vdev_leaf_zap
= 0;
2553 if (vd
->vdev_dtl_sm
== NULL
) {
2554 uint64_t new_object
;
2556 new_object
= space_map_alloc(mos
, tx
);
2557 VERIFY3U(new_object
, !=, 0);
2559 VERIFY0(space_map_open(&vd
->vdev_dtl_sm
, mos
, new_object
,
2561 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2564 rtsync
= range_tree_create(NULL
, NULL
);
2566 mutex_enter(&vd
->vdev_dtl_lock
);
2567 range_tree_walk(rt
, range_tree_add
, rtsync
);
2568 mutex_exit(&vd
->vdev_dtl_lock
);
2570 space_map_truncate(vd
->vdev_dtl_sm
, tx
);
2571 space_map_write(vd
->vdev_dtl_sm
, rtsync
, SM_ALLOC
, tx
);
2572 range_tree_vacate(rtsync
, NULL
, NULL
);
2574 range_tree_destroy(rtsync
);
2577 * If the object for the space map has changed then dirty
2578 * the top level so that we update the config.
2580 if (object
!= space_map_object(vd
->vdev_dtl_sm
)) {
2581 vdev_dbgmsg(vd
, "txg %llu, spa %s, DTL old object %llu, "
2582 "new object %llu", (u_longlong_t
)txg
, spa_name(spa
),
2583 (u_longlong_t
)object
,
2584 (u_longlong_t
)space_map_object(vd
->vdev_dtl_sm
));
2585 vdev_config_dirty(vd
->vdev_top
);
2590 mutex_enter(&vd
->vdev_dtl_lock
);
2591 space_map_update(vd
->vdev_dtl_sm
);
2592 mutex_exit(&vd
->vdev_dtl_lock
);
2596 * Determine whether the specified vdev can be offlined/detached/removed
2597 * without losing data.
2600 vdev_dtl_required(vdev_t
*vd
)
2602 spa_t
*spa
= vd
->vdev_spa
;
2603 vdev_t
*tvd
= vd
->vdev_top
;
2604 uint8_t cant_read
= vd
->vdev_cant_read
;
2607 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2609 if (vd
== spa
->spa_root_vdev
|| vd
== tvd
)
2613 * Temporarily mark the device as unreadable, and then determine
2614 * whether this results in any DTL outages in the top-level vdev.
2615 * If not, we can safely offline/detach/remove the device.
2617 vd
->vdev_cant_read
= B_TRUE
;
2618 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2619 required
= !vdev_dtl_empty(tvd
, DTL_OUTAGE
);
2620 vd
->vdev_cant_read
= cant_read
;
2621 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2623 if (!required
&& zio_injection_enabled
)
2624 required
= !!zio_handle_device_injection(vd
, NULL
, ECHILD
);
2630 * Determine if resilver is needed, and if so the txg range.
2633 vdev_resilver_needed(vdev_t
*vd
, uint64_t *minp
, uint64_t *maxp
)
2635 boolean_t needed
= B_FALSE
;
2636 uint64_t thismin
= UINT64_MAX
;
2637 uint64_t thismax
= 0;
2639 if (vd
->vdev_children
== 0) {
2640 mutex_enter(&vd
->vdev_dtl_lock
);
2641 if (range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) != 0 &&
2642 vdev_writeable(vd
)) {
2644 thismin
= vdev_dtl_min(vd
);
2645 thismax
= vdev_dtl_max(vd
);
2648 mutex_exit(&vd
->vdev_dtl_lock
);
2650 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2651 vdev_t
*cvd
= vd
->vdev_child
[c
];
2652 uint64_t cmin
, cmax
;
2654 if (vdev_resilver_needed(cvd
, &cmin
, &cmax
)) {
2655 thismin
= MIN(thismin
, cmin
);
2656 thismax
= MAX(thismax
, cmax
);
2662 if (needed
&& minp
) {
2670 vdev_load(vdev_t
*vd
)
2675 * Recursively load all children.
2677 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2678 error
= vdev_load(vd
->vdev_child
[c
]);
2684 vdev_set_deflate_ratio(vd
);
2687 * If this is a top-level vdev, initialize its metaslabs.
2689 if (vd
== vd
->vdev_top
&& vdev_is_concrete(vd
)) {
2690 if (vd
->vdev_ashift
== 0 || vd
->vdev_asize
== 0) {
2691 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2692 VDEV_AUX_CORRUPT_DATA
);
2693 vdev_dbgmsg(vd
, "vdev_load: invalid size. ashift=%llu, "
2694 "asize=%llu", (u_longlong_t
)vd
->vdev_ashift
,
2695 (u_longlong_t
)vd
->vdev_asize
);
2696 return (SET_ERROR(ENXIO
));
2697 } else if ((error
= vdev_metaslab_init(vd
, 0)) != 0) {
2698 vdev_dbgmsg(vd
, "vdev_load: metaslab_init failed "
2699 "[error=%d]", error
);
2700 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2701 VDEV_AUX_CORRUPT_DATA
);
2707 * If this is a leaf vdev, load its DTL.
2709 if (vd
->vdev_ops
->vdev_op_leaf
&& (error
= vdev_dtl_load(vd
)) != 0) {
2710 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2711 VDEV_AUX_CORRUPT_DATA
);
2712 vdev_dbgmsg(vd
, "vdev_load: vdev_dtl_load failed "
2713 "[error=%d]", error
);
2717 uint64_t obsolete_sm_object
= vdev_obsolete_sm_object(vd
);
2718 if (obsolete_sm_object
!= 0) {
2719 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
2720 ASSERT(vd
->vdev_asize
!= 0);
2721 ASSERT(vd
->vdev_obsolete_sm
== NULL
);
2723 if ((error
= space_map_open(&vd
->vdev_obsolete_sm
, mos
,
2724 obsolete_sm_object
, 0, vd
->vdev_asize
, 0))) {
2725 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2726 VDEV_AUX_CORRUPT_DATA
);
2727 vdev_dbgmsg(vd
, "vdev_load: space_map_open failed for "
2728 "obsolete spacemap (obj %llu) [error=%d]",
2729 (u_longlong_t
)obsolete_sm_object
, error
);
2732 space_map_update(vd
->vdev_obsolete_sm
);
2739 * The special vdev case is used for hot spares and l2cache devices. Its
2740 * sole purpose it to set the vdev state for the associated vdev. To do this,
2741 * we make sure that we can open the underlying device, then try to read the
2742 * label, and make sure that the label is sane and that it hasn't been
2743 * repurposed to another pool.
2746 vdev_validate_aux(vdev_t
*vd
)
2749 uint64_t guid
, version
;
2752 if (!vdev_readable(vd
))
2755 if ((label
= vdev_label_read_config(vd
, -1ULL)) == NULL
) {
2756 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2757 VDEV_AUX_CORRUPT_DATA
);
2761 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_VERSION
, &version
) != 0 ||
2762 !SPA_VERSION_IS_SUPPORTED(version
) ||
2763 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0 ||
2764 guid
!= vd
->vdev_guid
||
2765 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
, &state
) != 0) {
2766 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2767 VDEV_AUX_CORRUPT_DATA
);
2773 * We don't actually check the pool state here. If it's in fact in
2774 * use by another pool, we update this fact on the fly when requested.
2781 * Free the objects used to store this vdev's spacemaps, and the array
2782 * that points to them.
2785 vdev_destroy_spacemaps(vdev_t
*vd
, dmu_tx_t
*tx
)
2787 if (vd
->vdev_ms_array
== 0)
2790 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
2791 uint64_t array_count
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
2792 size_t array_bytes
= array_count
* sizeof (uint64_t);
2793 uint64_t *smobj_array
= kmem_alloc(array_bytes
, KM_SLEEP
);
2794 VERIFY0(dmu_read(mos
, vd
->vdev_ms_array
, 0,
2795 array_bytes
, smobj_array
, 0));
2797 for (uint64_t i
= 0; i
< array_count
; i
++) {
2798 uint64_t smobj
= smobj_array
[i
];
2802 space_map_free_obj(mos
, smobj
, tx
);
2805 kmem_free(smobj_array
, array_bytes
);
2806 VERIFY0(dmu_object_free(mos
, vd
->vdev_ms_array
, tx
));
2807 vd
->vdev_ms_array
= 0;
2811 vdev_remove_empty(vdev_t
*vd
, uint64_t txg
)
2813 spa_t
*spa
= vd
->vdev_spa
;
2816 ASSERT(vd
== vd
->vdev_top
);
2817 ASSERT3U(txg
, ==, spa_syncing_txg(spa
));
2819 if (vd
->vdev_ms
!= NULL
) {
2820 metaslab_group_t
*mg
= vd
->vdev_mg
;
2822 metaslab_group_histogram_verify(mg
);
2823 metaslab_class_histogram_verify(mg
->mg_class
);
2825 for (int m
= 0; m
< vd
->vdev_ms_count
; m
++) {
2826 metaslab_t
*msp
= vd
->vdev_ms
[m
];
2828 if (msp
== NULL
|| msp
->ms_sm
== NULL
)
2831 mutex_enter(&msp
->ms_lock
);
2833 * If the metaslab was not loaded when the vdev
2834 * was removed then the histogram accounting may
2835 * not be accurate. Update the histogram information
2836 * here so that we ensure that the metaslab group
2837 * and metaslab class are up-to-date.
2839 metaslab_group_histogram_remove(mg
, msp
);
2841 VERIFY0(space_map_allocated(msp
->ms_sm
));
2842 space_map_close(msp
->ms_sm
);
2844 mutex_exit(&msp
->ms_lock
);
2847 metaslab_group_histogram_verify(mg
);
2848 metaslab_class_histogram_verify(mg
->mg_class
);
2849 for (int i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++)
2850 ASSERT0(mg
->mg_histogram
[i
]);
2853 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
2854 vdev_destroy_spacemaps(vd
, tx
);
2856 if (vd
->vdev_islog
&& vd
->vdev_top_zap
!= 0) {
2857 vdev_destroy_unlink_zap(vd
, vd
->vdev_top_zap
, tx
);
2858 vd
->vdev_top_zap
= 0;
2864 vdev_sync_done(vdev_t
*vd
, uint64_t txg
)
2867 boolean_t reassess
= !txg_list_empty(&vd
->vdev_ms_list
, TXG_CLEAN(txg
));
2869 ASSERT(vdev_is_concrete(vd
));
2871 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, TXG_CLEAN(txg
))))
2872 metaslab_sync_done(msp
, txg
);
2875 metaslab_sync_reassess(vd
->vdev_mg
);
2879 vdev_sync(vdev_t
*vd
, uint64_t txg
)
2881 spa_t
*spa
= vd
->vdev_spa
;
2886 if (range_tree_space(vd
->vdev_obsolete_segments
) > 0) {
2889 ASSERT(vd
->vdev_removing
||
2890 vd
->vdev_ops
== &vdev_indirect_ops
);
2892 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2893 vdev_indirect_sync_obsolete(vd
, tx
);
2897 * If the vdev is indirect, it can't have dirty
2898 * metaslabs or DTLs.
2900 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
2901 ASSERT(txg_list_empty(&vd
->vdev_ms_list
, txg
));
2902 ASSERT(txg_list_empty(&vd
->vdev_dtl_list
, txg
));
2907 ASSERT(vdev_is_concrete(vd
));
2909 if (vd
->vdev_ms_array
== 0 && vd
->vdev_ms_shift
!= 0 &&
2910 !vd
->vdev_removing
) {
2911 ASSERT(vd
== vd
->vdev_top
);
2912 ASSERT0(vd
->vdev_indirect_config
.vic_mapping_object
);
2913 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2914 vd
->vdev_ms_array
= dmu_object_alloc(spa
->spa_meta_objset
,
2915 DMU_OT_OBJECT_ARRAY
, 0, DMU_OT_NONE
, 0, tx
);
2916 ASSERT(vd
->vdev_ms_array
!= 0);
2917 vdev_config_dirty(vd
);
2921 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, txg
)) != NULL
) {
2922 metaslab_sync(msp
, txg
);
2923 (void) txg_list_add(&vd
->vdev_ms_list
, msp
, TXG_CLEAN(txg
));
2926 while ((lvd
= txg_list_remove(&vd
->vdev_dtl_list
, txg
)) != NULL
)
2927 vdev_dtl_sync(lvd
, txg
);
2930 * Remove the metadata associated with this vdev once it's empty.
2931 * Note that this is typically used for log/cache device removal;
2932 * we don't empty toplevel vdevs when removing them. But if
2933 * a toplevel happens to be emptied, this is not harmful.
2935 if (vd
->vdev_stat
.vs_alloc
== 0 && vd
->vdev_removing
) {
2936 vdev_remove_empty(vd
, txg
);
2939 (void) txg_list_add(&spa
->spa_vdev_txg_list
, vd
, TXG_CLEAN(txg
));
2943 vdev_psize_to_asize(vdev_t
*vd
, uint64_t psize
)
2945 return (vd
->vdev_ops
->vdev_op_asize(vd
, psize
));
2949 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
2950 * not be opened, and no I/O is attempted.
2953 vdev_fault(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
2957 spa_vdev_state_enter(spa
, SCL_NONE
);
2959 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2960 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2962 if (!vd
->vdev_ops
->vdev_op_leaf
)
2963 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2968 * If user did a 'zpool offline -f' then make the fault persist across
2971 if (aux
== VDEV_AUX_EXTERNAL_PERSIST
) {
2973 * There are two kinds of forced faults: temporary and
2974 * persistent. Temporary faults go away at pool import, while
2975 * persistent faults stay set. Both types of faults can be
2976 * cleared with a zpool clear.
2978 * We tell if a vdev is persistently faulted by looking at the
2979 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
2980 * import then it's a persistent fault. Otherwise, it's
2981 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
2982 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
2983 * tells vdev_config_generate() (which gets run later) to set
2984 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
2986 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
2987 vd
->vdev_tmpoffline
= B_FALSE
;
2988 aux
= VDEV_AUX_EXTERNAL
;
2990 vd
->vdev_tmpoffline
= B_TRUE
;
2994 * We don't directly use the aux state here, but if we do a
2995 * vdev_reopen(), we need this value to be present to remember why we
2998 vd
->vdev_label_aux
= aux
;
3001 * Faulted state takes precedence over degraded.
3003 vd
->vdev_delayed_close
= B_FALSE
;
3004 vd
->vdev_faulted
= 1ULL;
3005 vd
->vdev_degraded
= 0ULL;
3006 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_FAULTED
, aux
);
3009 * If this device has the only valid copy of the data, then
3010 * back off and simply mark the vdev as degraded instead.
3012 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&& vdev_dtl_required(vd
)) {
3013 vd
->vdev_degraded
= 1ULL;
3014 vd
->vdev_faulted
= 0ULL;
3017 * If we reopen the device and it's not dead, only then do we
3022 if (vdev_readable(vd
))
3023 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
, aux
);
3026 return (spa_vdev_state_exit(spa
, vd
, 0));
3030 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
3031 * user that something is wrong. The vdev continues to operate as normal as far
3032 * as I/O is concerned.
3035 vdev_degrade(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
3039 spa_vdev_state_enter(spa
, SCL_NONE
);
3041 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3042 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
3044 if (!vd
->vdev_ops
->vdev_op_leaf
)
3045 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3048 * If the vdev is already faulted, then don't do anything.
3050 if (vd
->vdev_faulted
|| vd
->vdev_degraded
)
3051 return (spa_vdev_state_exit(spa
, NULL
, 0));
3053 vd
->vdev_degraded
= 1ULL;
3054 if (!vdev_is_dead(vd
))
3055 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
,
3058 return (spa_vdev_state_exit(spa
, vd
, 0));
3062 * Online the given vdev.
3064 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
3065 * spare device should be detached when the device finishes resilvering.
3066 * Second, the online should be treated like a 'test' online case, so no FMA
3067 * events are generated if the device fails to open.
3070 vdev_online(spa_t
*spa
, uint64_t guid
, uint64_t flags
, vdev_state_t
*newstate
)
3072 vdev_t
*vd
, *tvd
, *pvd
, *rvd
= spa
->spa_root_vdev
;
3073 boolean_t wasoffline
;
3074 vdev_state_t oldstate
;
3076 spa_vdev_state_enter(spa
, SCL_NONE
);
3078 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3079 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
3081 if (!vd
->vdev_ops
->vdev_op_leaf
)
3082 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3084 wasoffline
= (vd
->vdev_offline
|| vd
->vdev_tmpoffline
);
3085 oldstate
= vd
->vdev_state
;
3088 vd
->vdev_offline
= B_FALSE
;
3089 vd
->vdev_tmpoffline
= B_FALSE
;
3090 vd
->vdev_checkremove
= !!(flags
& ZFS_ONLINE_CHECKREMOVE
);
3091 vd
->vdev_forcefault
= !!(flags
& ZFS_ONLINE_FORCEFAULT
);
3093 /* XXX - L2ARC 1.0 does not support expansion */
3094 if (!vd
->vdev_aux
) {
3095 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3096 pvd
->vdev_expanding
= !!(flags
& ZFS_ONLINE_EXPAND
);
3100 vd
->vdev_checkremove
= vd
->vdev_forcefault
= B_FALSE
;
3102 if (!vd
->vdev_aux
) {
3103 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3104 pvd
->vdev_expanding
= B_FALSE
;
3108 *newstate
= vd
->vdev_state
;
3109 if ((flags
& ZFS_ONLINE_UNSPARE
) &&
3110 !vdev_is_dead(vd
) && vd
->vdev_parent
&&
3111 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3112 vd
->vdev_parent
->vdev_child
[0] == vd
)
3113 vd
->vdev_unspare
= B_TRUE
;
3115 if ((flags
& ZFS_ONLINE_EXPAND
) || spa
->spa_autoexpand
) {
3117 /* XXX - L2ARC 1.0 does not support expansion */
3119 return (spa_vdev_state_exit(spa
, vd
, ENOTSUP
));
3120 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3124 (oldstate
< VDEV_STATE_DEGRADED
&&
3125 vd
->vdev_state
>= VDEV_STATE_DEGRADED
))
3126 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_ONLINE
);
3128 return (spa_vdev_state_exit(spa
, vd
, 0));
3132 vdev_offline_locked(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
3136 uint64_t generation
;
3137 metaslab_group_t
*mg
;
3140 spa_vdev_state_enter(spa
, SCL_ALLOC
);
3142 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3143 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
3145 if (!vd
->vdev_ops
->vdev_op_leaf
)
3146 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3150 generation
= spa
->spa_config_generation
+ 1;
3153 * If the device isn't already offline, try to offline it.
3155 if (!vd
->vdev_offline
) {
3157 * If this device has the only valid copy of some data,
3158 * don't allow it to be offlined. Log devices are always
3161 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
3162 vdev_dtl_required(vd
))
3163 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
3166 * If the top-level is a slog and it has had allocations
3167 * then proceed. We check that the vdev's metaslab group
3168 * is not NULL since it's possible that we may have just
3169 * added this vdev but not yet initialized its metaslabs.
3171 if (tvd
->vdev_islog
&& mg
!= NULL
) {
3173 * Prevent any future allocations.
3175 metaslab_group_passivate(mg
);
3176 (void) spa_vdev_state_exit(spa
, vd
, 0);
3178 error
= spa_reset_logs(spa
);
3180 spa_vdev_state_enter(spa
, SCL_ALLOC
);
3183 * Check to see if the config has changed.
3185 if (error
|| generation
!= spa
->spa_config_generation
) {
3186 metaslab_group_activate(mg
);
3188 return (spa_vdev_state_exit(spa
,
3190 (void) spa_vdev_state_exit(spa
, vd
, 0);
3193 ASSERT0(tvd
->vdev_stat
.vs_alloc
);
3197 * Offline this device and reopen its top-level vdev.
3198 * If the top-level vdev is a log device then just offline
3199 * it. Otherwise, if this action results in the top-level
3200 * vdev becoming unusable, undo it and fail the request.
3202 vd
->vdev_offline
= B_TRUE
;
3205 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
3206 vdev_is_dead(tvd
)) {
3207 vd
->vdev_offline
= B_FALSE
;
3209 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
3213 * Add the device back into the metaslab rotor so that
3214 * once we online the device it's open for business.
3216 if (tvd
->vdev_islog
&& mg
!= NULL
)
3217 metaslab_group_activate(mg
);
3220 vd
->vdev_tmpoffline
= !!(flags
& ZFS_OFFLINE_TEMPORARY
);
3222 return (spa_vdev_state_exit(spa
, vd
, 0));
3226 vdev_offline(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
3230 mutex_enter(&spa
->spa_vdev_top_lock
);
3231 error
= vdev_offline_locked(spa
, guid
, flags
);
3232 mutex_exit(&spa
->spa_vdev_top_lock
);
3238 * Clear the error counts associated with this vdev. Unlike vdev_online() and
3239 * vdev_offline(), we assume the spa config is locked. We also clear all
3240 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
3243 vdev_clear(spa_t
*spa
, vdev_t
*vd
)
3245 vdev_t
*rvd
= spa
->spa_root_vdev
;
3247 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
3252 vd
->vdev_stat
.vs_read_errors
= 0;
3253 vd
->vdev_stat
.vs_write_errors
= 0;
3254 vd
->vdev_stat
.vs_checksum_errors
= 0;
3256 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3257 vdev_clear(spa
, vd
->vdev_child
[c
]);
3260 * It makes no sense to "clear" an indirect vdev.
3262 if (!vdev_is_concrete(vd
))
3266 * If we're in the FAULTED state or have experienced failed I/O, then
3267 * clear the persistent state and attempt to reopen the device. We
3268 * also mark the vdev config dirty, so that the new faulted state is
3269 * written out to disk.
3271 if (vd
->vdev_faulted
|| vd
->vdev_degraded
||
3272 !vdev_readable(vd
) || !vdev_writeable(vd
)) {
3274 * When reopening in response to a clear event, it may be due to
3275 * a fmadm repair request. In this case, if the device is
3276 * still broken, we want to still post the ereport again.
3278 vd
->vdev_forcefault
= B_TRUE
;
3280 vd
->vdev_faulted
= vd
->vdev_degraded
= 0ULL;
3281 vd
->vdev_cant_read
= B_FALSE
;
3282 vd
->vdev_cant_write
= B_FALSE
;
3283 vd
->vdev_stat
.vs_aux
= 0;
3285 vdev_reopen(vd
== rvd
? rvd
: vd
->vdev_top
);
3287 vd
->vdev_forcefault
= B_FALSE
;
3289 if (vd
!= rvd
&& vdev_writeable(vd
->vdev_top
))
3290 vdev_state_dirty(vd
->vdev_top
);
3292 if (vd
->vdev_aux
== NULL
&& !vdev_is_dead(vd
))
3293 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
3295 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_CLEAR
);
3299 * When clearing a FMA-diagnosed fault, we always want to
3300 * unspare the device, as we assume that the original spare was
3301 * done in response to the FMA fault.
3303 if (!vdev_is_dead(vd
) && vd
->vdev_parent
!= NULL
&&
3304 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3305 vd
->vdev_parent
->vdev_child
[0] == vd
)
3306 vd
->vdev_unspare
= B_TRUE
;
3310 vdev_is_dead(vdev_t
*vd
)
3313 * Holes and missing devices are always considered "dead".
3314 * This simplifies the code since we don't have to check for
3315 * these types of devices in the various code paths.
3316 * Instead we rely on the fact that we skip over dead devices
3317 * before issuing I/O to them.
3319 return (vd
->vdev_state
< VDEV_STATE_DEGRADED
||
3320 vd
->vdev_ops
== &vdev_hole_ops
||
3321 vd
->vdev_ops
== &vdev_missing_ops
);
3325 vdev_readable(vdev_t
*vd
)
3327 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_read
);
3331 vdev_writeable(vdev_t
*vd
)
3333 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_write
&&
3334 vdev_is_concrete(vd
));
3338 vdev_allocatable(vdev_t
*vd
)
3340 uint64_t state
= vd
->vdev_state
;
3343 * We currently allow allocations from vdevs which may be in the
3344 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3345 * fails to reopen then we'll catch it later when we're holding
3346 * the proper locks. Note that we have to get the vdev state
3347 * in a local variable because although it changes atomically,
3348 * we're asking two separate questions about it.
3350 return (!(state
< VDEV_STATE_DEGRADED
&& state
!= VDEV_STATE_CLOSED
) &&
3351 !vd
->vdev_cant_write
&& vdev_is_concrete(vd
) &&
3352 vd
->vdev_mg
->mg_initialized
);
3356 vdev_accessible(vdev_t
*vd
, zio_t
*zio
)
3358 ASSERT(zio
->io_vd
== vd
);
3360 if (vdev_is_dead(vd
) || vd
->vdev_remove_wanted
)
3363 if (zio
->io_type
== ZIO_TYPE_READ
)
3364 return (!vd
->vdev_cant_read
);
3366 if (zio
->io_type
== ZIO_TYPE_WRITE
)
3367 return (!vd
->vdev_cant_write
);
3373 vdev_get_child_stat(vdev_t
*cvd
, vdev_stat_t
*vs
, vdev_stat_t
*cvs
)
3376 for (t
= 0; t
< ZIO_TYPES
; t
++) {
3377 vs
->vs_ops
[t
] += cvs
->vs_ops
[t
];
3378 vs
->vs_bytes
[t
] += cvs
->vs_bytes
[t
];
3381 cvs
->vs_scan_removing
= cvd
->vdev_removing
;
3385 * Get extended stats
3388 vdev_get_child_stat_ex(vdev_t
*cvd
, vdev_stat_ex_t
*vsx
, vdev_stat_ex_t
*cvsx
)
3391 for (t
= 0; t
< ZIO_TYPES
; t
++) {
3392 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_disk_histo
[0]); b
++)
3393 vsx
->vsx_disk_histo
[t
][b
] += cvsx
->vsx_disk_histo
[t
][b
];
3395 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_total_histo
[0]); b
++) {
3396 vsx
->vsx_total_histo
[t
][b
] +=
3397 cvsx
->vsx_total_histo
[t
][b
];
3401 for (t
= 0; t
< ZIO_PRIORITY_NUM_QUEUEABLE
; t
++) {
3402 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_queue_histo
[0]); b
++) {
3403 vsx
->vsx_queue_histo
[t
][b
] +=
3404 cvsx
->vsx_queue_histo
[t
][b
];
3406 vsx
->vsx_active_queue
[t
] += cvsx
->vsx_active_queue
[t
];
3407 vsx
->vsx_pend_queue
[t
] += cvsx
->vsx_pend_queue
[t
];
3409 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_ind_histo
[0]); b
++)
3410 vsx
->vsx_ind_histo
[t
][b
] += cvsx
->vsx_ind_histo
[t
][b
];
3412 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_agg_histo
[0]); b
++)
3413 vsx
->vsx_agg_histo
[t
][b
] += cvsx
->vsx_agg_histo
[t
][b
];
3419 * Get statistics for the given vdev.
3422 vdev_get_stats_ex_impl(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
3426 * If we're getting stats on the root vdev, aggregate the I/O counts
3427 * over all top-level vdevs (i.e. the direct children of the root).
3429 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3431 memset(vs
->vs_ops
, 0, sizeof (vs
->vs_ops
));
3432 memset(vs
->vs_bytes
, 0, sizeof (vs
->vs_bytes
));
3435 memset(vsx
, 0, sizeof (*vsx
));
3437 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3438 vdev_t
*cvd
= vd
->vdev_child
[c
];
3439 vdev_stat_t
*cvs
= &cvd
->vdev_stat
;
3440 vdev_stat_ex_t
*cvsx
= &cvd
->vdev_stat_ex
;
3442 vdev_get_stats_ex_impl(cvd
, cvs
, cvsx
);
3444 vdev_get_child_stat(cvd
, vs
, cvs
);
3446 vdev_get_child_stat_ex(cvd
, vsx
, cvsx
);
3451 * We're a leaf. Just copy our ZIO active queue stats in. The
3452 * other leaf stats are updated in vdev_stat_update().
3457 memcpy(vsx
, &vd
->vdev_stat_ex
, sizeof (vd
->vdev_stat_ex
));
3459 for (t
= 0; t
< ARRAY_SIZE(vd
->vdev_queue
.vq_class
); t
++) {
3460 vsx
->vsx_active_queue
[t
] =
3461 vd
->vdev_queue
.vq_class
[t
].vqc_active
;
3462 vsx
->vsx_pend_queue
[t
] = avl_numnodes(
3463 &vd
->vdev_queue
.vq_class
[t
].vqc_queued_tree
);
3469 vdev_get_stats_ex(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
3471 vdev_t
*tvd
= vd
->vdev_top
;
3472 mutex_enter(&vd
->vdev_stat_lock
);
3474 bcopy(&vd
->vdev_stat
, vs
, sizeof (*vs
));
3475 vs
->vs_timestamp
= gethrtime() - vs
->vs_timestamp
;
3476 vs
->vs_state
= vd
->vdev_state
;
3477 vs
->vs_rsize
= vdev_get_min_asize(vd
);
3478 if (vd
->vdev_ops
->vdev_op_leaf
)
3479 vs
->vs_rsize
+= VDEV_LABEL_START_SIZE
+
3480 VDEV_LABEL_END_SIZE
;
3482 * Report expandable space on top-level, non-auxillary devices
3483 * only. The expandable space is reported in terms of metaslab
3484 * sized units since that determines how much space the pool
3487 if (vd
->vdev_aux
== NULL
&& tvd
!= NULL
) {
3488 vs
->vs_esize
= P2ALIGN(
3489 vd
->vdev_max_asize
- vd
->vdev_asize
,
3490 1ULL << tvd
->vdev_ms_shift
);
3492 vs
->vs_esize
= vd
->vdev_max_asize
- vd
->vdev_asize
;
3493 if (vd
->vdev_aux
== NULL
&& vd
== vd
->vdev_top
&&
3494 vdev_is_concrete(vd
)) {
3495 vs
->vs_fragmentation
= vd
->vdev_mg
->mg_fragmentation
;
3499 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_READER
) != 0);
3500 vdev_get_stats_ex_impl(vd
, vs
, vsx
);
3501 mutex_exit(&vd
->vdev_stat_lock
);
3505 vdev_get_stats(vdev_t
*vd
, vdev_stat_t
*vs
)
3507 return (vdev_get_stats_ex(vd
, vs
, NULL
));
3511 vdev_clear_stats(vdev_t
*vd
)
3513 mutex_enter(&vd
->vdev_stat_lock
);
3514 vd
->vdev_stat
.vs_space
= 0;
3515 vd
->vdev_stat
.vs_dspace
= 0;
3516 vd
->vdev_stat
.vs_alloc
= 0;
3517 mutex_exit(&vd
->vdev_stat_lock
);
3521 vdev_scan_stat_init(vdev_t
*vd
)
3523 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3525 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3526 vdev_scan_stat_init(vd
->vdev_child
[c
]);
3528 mutex_enter(&vd
->vdev_stat_lock
);
3529 vs
->vs_scan_processed
= 0;
3530 mutex_exit(&vd
->vdev_stat_lock
);
3534 vdev_stat_update(zio_t
*zio
, uint64_t psize
)
3536 spa_t
*spa
= zio
->io_spa
;
3537 vdev_t
*rvd
= spa
->spa_root_vdev
;
3538 vdev_t
*vd
= zio
->io_vd
? zio
->io_vd
: rvd
;
3540 uint64_t txg
= zio
->io_txg
;
3541 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3542 vdev_stat_ex_t
*vsx
= &vd
->vdev_stat_ex
;
3543 zio_type_t type
= zio
->io_type
;
3544 int flags
= zio
->io_flags
;
3547 * If this i/o is a gang leader, it didn't do any actual work.
3549 if (zio
->io_gang_tree
)
3552 if (zio
->io_error
== 0) {
3554 * If this is a root i/o, don't count it -- we've already
3555 * counted the top-level vdevs, and vdev_get_stats() will
3556 * aggregate them when asked. This reduces contention on
3557 * the root vdev_stat_lock and implicitly handles blocks
3558 * that compress away to holes, for which there is no i/o.
3559 * (Holes never create vdev children, so all the counters
3560 * remain zero, which is what we want.)
3562 * Note: this only applies to successful i/o (io_error == 0)
3563 * because unlike i/o counts, errors are not additive.
3564 * When reading a ditto block, for example, failure of
3565 * one top-level vdev does not imply a root-level error.
3570 ASSERT(vd
== zio
->io_vd
);
3572 if (flags
& ZIO_FLAG_IO_BYPASS
)
3575 mutex_enter(&vd
->vdev_stat_lock
);
3577 if (flags
& ZIO_FLAG_IO_REPAIR
) {
3578 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
3579 dsl_scan_phys_t
*scn_phys
=
3580 &spa
->spa_dsl_pool
->dp_scan
->scn_phys
;
3581 uint64_t *processed
= &scn_phys
->scn_processed
;
3584 if (vd
->vdev_ops
->vdev_op_leaf
)
3585 atomic_add_64(processed
, psize
);
3586 vs
->vs_scan_processed
+= psize
;
3589 if (flags
& ZIO_FLAG_SELF_HEAL
)
3590 vs
->vs_self_healed
+= psize
;
3594 * The bytes/ops/histograms are recorded at the leaf level and
3595 * aggregated into the higher level vdevs in vdev_get_stats().
3597 if (vd
->vdev_ops
->vdev_op_leaf
&&
3598 (zio
->io_priority
< ZIO_PRIORITY_NUM_QUEUEABLE
)) {
3601 vs
->vs_bytes
[type
] += psize
;
3603 if (flags
& ZIO_FLAG_DELEGATED
) {
3604 vsx
->vsx_agg_histo
[zio
->io_priority
]
3605 [RQ_HISTO(zio
->io_size
)]++;
3607 vsx
->vsx_ind_histo
[zio
->io_priority
]
3608 [RQ_HISTO(zio
->io_size
)]++;
3611 if (zio
->io_delta
&& zio
->io_delay
) {
3612 vsx
->vsx_queue_histo
[zio
->io_priority
]
3613 [L_HISTO(zio
->io_delta
- zio
->io_delay
)]++;
3614 vsx
->vsx_disk_histo
[type
]
3615 [L_HISTO(zio
->io_delay
)]++;
3616 vsx
->vsx_total_histo
[type
]
3617 [L_HISTO(zio
->io_delta
)]++;
3621 mutex_exit(&vd
->vdev_stat_lock
);
3625 if (flags
& ZIO_FLAG_SPECULATIVE
)
3629 * If this is an I/O error that is going to be retried, then ignore the
3630 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
3631 * hard errors, when in reality they can happen for any number of
3632 * innocuous reasons (bus resets, MPxIO link failure, etc).
3634 if (zio
->io_error
== EIO
&&
3635 !(zio
->io_flags
& ZIO_FLAG_IO_RETRY
))
3639 * Intent logs writes won't propagate their error to the root
3640 * I/O so don't mark these types of failures as pool-level
3643 if (zio
->io_vd
== NULL
&& (zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
3646 mutex_enter(&vd
->vdev_stat_lock
);
3647 if (type
== ZIO_TYPE_READ
&& !vdev_is_dead(vd
)) {
3648 if (zio
->io_error
== ECKSUM
)
3649 vs
->vs_checksum_errors
++;
3651 vs
->vs_read_errors
++;
3653 if (type
== ZIO_TYPE_WRITE
&& !vdev_is_dead(vd
))
3654 vs
->vs_write_errors
++;
3655 mutex_exit(&vd
->vdev_stat_lock
);
3657 if (spa
->spa_load_state
== SPA_LOAD_NONE
&&
3658 type
== ZIO_TYPE_WRITE
&& txg
!= 0 &&
3659 (!(flags
& ZIO_FLAG_IO_REPAIR
) ||
3660 (flags
& ZIO_FLAG_SCAN_THREAD
) ||
3661 spa
->spa_claiming
)) {
3663 * This is either a normal write (not a repair), or it's
3664 * a repair induced by the scrub thread, or it's a repair
3665 * made by zil_claim() during spa_load() in the first txg.
3666 * In the normal case, we commit the DTL change in the same
3667 * txg as the block was born. In the scrub-induced repair
3668 * case, we know that scrubs run in first-pass syncing context,
3669 * so we commit the DTL change in spa_syncing_txg(spa).
3670 * In the zil_claim() case, we commit in spa_first_txg(spa).
3672 * We currently do not make DTL entries for failed spontaneous
3673 * self-healing writes triggered by normal (non-scrubbing)
3674 * reads, because we have no transactional context in which to
3675 * do so -- and it's not clear that it'd be desirable anyway.
3677 if (vd
->vdev_ops
->vdev_op_leaf
) {
3678 uint64_t commit_txg
= txg
;
3679 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
3680 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
3681 ASSERT(spa_sync_pass(spa
) == 1);
3682 vdev_dtl_dirty(vd
, DTL_SCRUB
, txg
, 1);
3683 commit_txg
= spa_syncing_txg(spa
);
3684 } else if (spa
->spa_claiming
) {
3685 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
3686 commit_txg
= spa_first_txg(spa
);
3688 ASSERT(commit_txg
>= spa_syncing_txg(spa
));
3689 if (vdev_dtl_contains(vd
, DTL_MISSING
, txg
, 1))
3691 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3692 vdev_dtl_dirty(pvd
, DTL_PARTIAL
, txg
, 1);
3693 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, commit_txg
);
3696 vdev_dtl_dirty(vd
, DTL_MISSING
, txg
, 1);
3701 * Update the in-core space usage stats for this vdev, its metaslab class,
3702 * and the root vdev.
3705 vdev_space_update(vdev_t
*vd
, int64_t alloc_delta
, int64_t defer_delta
,
3706 int64_t space_delta
)
3708 int64_t dspace_delta
= space_delta
;
3709 spa_t
*spa
= vd
->vdev_spa
;
3710 vdev_t
*rvd
= spa
->spa_root_vdev
;
3711 metaslab_group_t
*mg
= vd
->vdev_mg
;
3712 metaslab_class_t
*mc
= mg
? mg
->mg_class
: NULL
;
3714 ASSERT(vd
== vd
->vdev_top
);
3717 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
3718 * factor. We must calculate this here and not at the root vdev
3719 * because the root vdev's psize-to-asize is simply the max of its
3720 * childrens', thus not accurate enough for us.
3722 ASSERT((dspace_delta
& (SPA_MINBLOCKSIZE
-1)) == 0);
3723 ASSERT(vd
->vdev_deflate_ratio
!= 0 || vd
->vdev_isl2cache
);
3724 dspace_delta
= (dspace_delta
>> SPA_MINBLOCKSHIFT
) *
3725 vd
->vdev_deflate_ratio
;
3727 mutex_enter(&vd
->vdev_stat_lock
);
3728 vd
->vdev_stat
.vs_alloc
+= alloc_delta
;
3729 vd
->vdev_stat
.vs_space
+= space_delta
;
3730 vd
->vdev_stat
.vs_dspace
+= dspace_delta
;
3731 mutex_exit(&vd
->vdev_stat_lock
);
3733 if (mc
== spa_normal_class(spa
)) {
3734 mutex_enter(&rvd
->vdev_stat_lock
);
3735 rvd
->vdev_stat
.vs_alloc
+= alloc_delta
;
3736 rvd
->vdev_stat
.vs_space
+= space_delta
;
3737 rvd
->vdev_stat
.vs_dspace
+= dspace_delta
;
3738 mutex_exit(&rvd
->vdev_stat_lock
);
3742 ASSERT(rvd
== vd
->vdev_parent
);
3743 ASSERT(vd
->vdev_ms_count
!= 0);
3745 metaslab_class_space_update(mc
,
3746 alloc_delta
, defer_delta
, space_delta
, dspace_delta
);
3751 * Mark a top-level vdev's config as dirty, placing it on the dirty list
3752 * so that it will be written out next time the vdev configuration is synced.
3753 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3756 vdev_config_dirty(vdev_t
*vd
)
3758 spa_t
*spa
= vd
->vdev_spa
;
3759 vdev_t
*rvd
= spa
->spa_root_vdev
;
3762 ASSERT(spa_writeable(spa
));
3765 * If this is an aux vdev (as with l2cache and spare devices), then we
3766 * update the vdev config manually and set the sync flag.
3768 if (vd
->vdev_aux
!= NULL
) {
3769 spa_aux_vdev_t
*sav
= vd
->vdev_aux
;
3773 for (c
= 0; c
< sav
->sav_count
; c
++) {
3774 if (sav
->sav_vdevs
[c
] == vd
)
3778 if (c
== sav
->sav_count
) {
3780 * We're being removed. There's nothing more to do.
3782 ASSERT(sav
->sav_sync
== B_TRUE
);
3786 sav
->sav_sync
= B_TRUE
;
3788 if (nvlist_lookup_nvlist_array(sav
->sav_config
,
3789 ZPOOL_CONFIG_L2CACHE
, &aux
, &naux
) != 0) {
3790 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
3791 ZPOOL_CONFIG_SPARES
, &aux
, &naux
) == 0);
3797 * Setting the nvlist in the middle if the array is a little
3798 * sketchy, but it will work.
3800 nvlist_free(aux
[c
]);
3801 aux
[c
] = vdev_config_generate(spa
, vd
, B_TRUE
, 0);
3807 * The dirty list is protected by the SCL_CONFIG lock. The caller
3808 * must either hold SCL_CONFIG as writer, or must be the sync thread
3809 * (which holds SCL_CONFIG as reader). There's only one sync thread,
3810 * so this is sufficient to ensure mutual exclusion.
3812 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
3813 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3814 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
3817 for (c
= 0; c
< rvd
->vdev_children
; c
++)
3818 vdev_config_dirty(rvd
->vdev_child
[c
]);
3820 ASSERT(vd
== vd
->vdev_top
);
3822 if (!list_link_active(&vd
->vdev_config_dirty_node
) &&
3823 vdev_is_concrete(vd
)) {
3824 list_insert_head(&spa
->spa_config_dirty_list
, vd
);
3830 vdev_config_clean(vdev_t
*vd
)
3832 spa_t
*spa
= vd
->vdev_spa
;
3834 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
3835 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3836 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
3838 ASSERT(list_link_active(&vd
->vdev_config_dirty_node
));
3839 list_remove(&spa
->spa_config_dirty_list
, vd
);
3843 * Mark a top-level vdev's state as dirty, so that the next pass of
3844 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3845 * the state changes from larger config changes because they require
3846 * much less locking, and are often needed for administrative actions.
3849 vdev_state_dirty(vdev_t
*vd
)
3851 spa_t
*spa
= vd
->vdev_spa
;
3853 ASSERT(spa_writeable(spa
));
3854 ASSERT(vd
== vd
->vdev_top
);
3857 * The state list is protected by the SCL_STATE lock. The caller
3858 * must either hold SCL_STATE as writer, or must be the sync thread
3859 * (which holds SCL_STATE as reader). There's only one sync thread,
3860 * so this is sufficient to ensure mutual exclusion.
3862 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
3863 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3864 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
3866 if (!list_link_active(&vd
->vdev_state_dirty_node
) &&
3867 vdev_is_concrete(vd
))
3868 list_insert_head(&spa
->spa_state_dirty_list
, vd
);
3872 vdev_state_clean(vdev_t
*vd
)
3874 spa_t
*spa
= vd
->vdev_spa
;
3876 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
3877 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3878 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
3880 ASSERT(list_link_active(&vd
->vdev_state_dirty_node
));
3881 list_remove(&spa
->spa_state_dirty_list
, vd
);
3885 * Propagate vdev state up from children to parent.
3888 vdev_propagate_state(vdev_t
*vd
)
3890 spa_t
*spa
= vd
->vdev_spa
;
3891 vdev_t
*rvd
= spa
->spa_root_vdev
;
3892 int degraded
= 0, faulted
= 0;
3896 if (vd
->vdev_children
> 0) {
3897 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3898 child
= vd
->vdev_child
[c
];
3901 * Don't factor holes or indirect vdevs into the
3904 if (!vdev_is_concrete(child
))
3907 if (!vdev_readable(child
) ||
3908 (!vdev_writeable(child
) && spa_writeable(spa
))) {
3910 * Root special: if there is a top-level log
3911 * device, treat the root vdev as if it were
3914 if (child
->vdev_islog
&& vd
== rvd
)
3918 } else if (child
->vdev_state
<= VDEV_STATE_DEGRADED
) {
3922 if (child
->vdev_stat
.vs_aux
== VDEV_AUX_CORRUPT_DATA
)
3926 vd
->vdev_ops
->vdev_op_state_change(vd
, faulted
, degraded
);
3929 * Root special: if there is a top-level vdev that cannot be
3930 * opened due to corrupted metadata, then propagate the root
3931 * vdev's aux state as 'corrupt' rather than 'insufficient
3934 if (corrupted
&& vd
== rvd
&&
3935 rvd
->vdev_state
== VDEV_STATE_CANT_OPEN
)
3936 vdev_set_state(rvd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3937 VDEV_AUX_CORRUPT_DATA
);
3940 if (vd
->vdev_parent
)
3941 vdev_propagate_state(vd
->vdev_parent
);
3945 * Set a vdev's state. If this is during an open, we don't update the parent
3946 * state, because we're in the process of opening children depth-first.
3947 * Otherwise, we propagate the change to the parent.
3949 * If this routine places a device in a faulted state, an appropriate ereport is
3953 vdev_set_state(vdev_t
*vd
, boolean_t isopen
, vdev_state_t state
, vdev_aux_t aux
)
3955 uint64_t save_state
;
3956 spa_t
*spa
= vd
->vdev_spa
;
3958 if (state
== vd
->vdev_state
) {
3960 * Since vdev_offline() code path is already in an offline
3961 * state we can miss a statechange event to OFFLINE. Check
3962 * the previous state to catch this condition.
3964 if (vd
->vdev_ops
->vdev_op_leaf
&&
3965 (state
== VDEV_STATE_OFFLINE
) &&
3966 (vd
->vdev_prevstate
>= VDEV_STATE_FAULTED
)) {
3967 /* post an offline state change */
3968 zfs_post_state_change(spa
, vd
, vd
->vdev_prevstate
);
3970 vd
->vdev_stat
.vs_aux
= aux
;
3974 save_state
= vd
->vdev_state
;
3976 vd
->vdev_state
= state
;
3977 vd
->vdev_stat
.vs_aux
= aux
;
3980 * If we are setting the vdev state to anything but an open state, then
3981 * always close the underlying device unless the device has requested
3982 * a delayed close (i.e. we're about to remove or fault the device).
3983 * Otherwise, we keep accessible but invalid devices open forever.
3984 * We don't call vdev_close() itself, because that implies some extra
3985 * checks (offline, etc) that we don't want here. This is limited to
3986 * leaf devices, because otherwise closing the device will affect other
3989 if (!vd
->vdev_delayed_close
&& vdev_is_dead(vd
) &&
3990 vd
->vdev_ops
->vdev_op_leaf
)
3991 vd
->vdev_ops
->vdev_op_close(vd
);
3993 if (vd
->vdev_removed
&&
3994 state
== VDEV_STATE_CANT_OPEN
&&
3995 (aux
== VDEV_AUX_OPEN_FAILED
|| vd
->vdev_checkremove
)) {
3997 * If the previous state is set to VDEV_STATE_REMOVED, then this
3998 * device was previously marked removed and someone attempted to
3999 * reopen it. If this failed due to a nonexistent device, then
4000 * keep the device in the REMOVED state. We also let this be if
4001 * it is one of our special test online cases, which is only
4002 * attempting to online the device and shouldn't generate an FMA
4005 vd
->vdev_state
= VDEV_STATE_REMOVED
;
4006 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
4007 } else if (state
== VDEV_STATE_REMOVED
) {
4008 vd
->vdev_removed
= B_TRUE
;
4009 } else if (state
== VDEV_STATE_CANT_OPEN
) {
4011 * If we fail to open a vdev during an import or recovery, we
4012 * mark it as "not available", which signifies that it was
4013 * never there to begin with. Failure to open such a device
4014 * is not considered an error.
4016 if ((spa_load_state(spa
) == SPA_LOAD_IMPORT
||
4017 spa_load_state(spa
) == SPA_LOAD_RECOVER
) &&
4018 vd
->vdev_ops
->vdev_op_leaf
)
4019 vd
->vdev_not_present
= 1;
4022 * Post the appropriate ereport. If the 'prevstate' field is
4023 * set to something other than VDEV_STATE_UNKNOWN, it indicates
4024 * that this is part of a vdev_reopen(). In this case, we don't
4025 * want to post the ereport if the device was already in the
4026 * CANT_OPEN state beforehand.
4028 * If the 'checkremove' flag is set, then this is an attempt to
4029 * online the device in response to an insertion event. If we
4030 * hit this case, then we have detected an insertion event for a
4031 * faulted or offline device that wasn't in the removed state.
4032 * In this scenario, we don't post an ereport because we are
4033 * about to replace the device, or attempt an online with
4034 * vdev_forcefault, which will generate the fault for us.
4036 if ((vd
->vdev_prevstate
!= state
|| vd
->vdev_forcefault
) &&
4037 !vd
->vdev_not_present
&& !vd
->vdev_checkremove
&&
4038 vd
!= spa
->spa_root_vdev
) {
4042 case VDEV_AUX_OPEN_FAILED
:
4043 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED
;
4045 case VDEV_AUX_CORRUPT_DATA
:
4046 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA
;
4048 case VDEV_AUX_NO_REPLICAS
:
4049 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS
;
4051 case VDEV_AUX_BAD_GUID_SUM
:
4052 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM
;
4054 case VDEV_AUX_TOO_SMALL
:
4055 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL
;
4057 case VDEV_AUX_BAD_LABEL
:
4058 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL
;
4060 case VDEV_AUX_BAD_ASHIFT
:
4061 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
;
4064 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN
;
4067 zfs_ereport_post(class, spa
, vd
, NULL
, NULL
,
4071 /* Erase any notion of persistent removed state */
4072 vd
->vdev_removed
= B_FALSE
;
4074 vd
->vdev_removed
= B_FALSE
;
4078 * Notify ZED of any significant state-change on a leaf vdev.
4081 if (vd
->vdev_ops
->vdev_op_leaf
) {
4082 /* preserve original state from a vdev_reopen() */
4083 if ((vd
->vdev_prevstate
!= VDEV_STATE_UNKNOWN
) &&
4084 (vd
->vdev_prevstate
!= vd
->vdev_state
) &&
4085 (save_state
<= VDEV_STATE_CLOSED
))
4086 save_state
= vd
->vdev_prevstate
;
4088 /* filter out state change due to initial vdev_open */
4089 if (save_state
> VDEV_STATE_CLOSED
)
4090 zfs_post_state_change(spa
, vd
, save_state
);
4093 if (!isopen
&& vd
->vdev_parent
)
4094 vdev_propagate_state(vd
->vdev_parent
);
4098 vdev_children_are_offline(vdev_t
*vd
)
4100 ASSERT(!vd
->vdev_ops
->vdev_op_leaf
);
4102 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
4103 if (vd
->vdev_child
[i
]->vdev_state
!= VDEV_STATE_OFFLINE
)
4111 * Check the vdev configuration to ensure that it's capable of supporting
4112 * a root pool. We do not support partial configuration.
4115 vdev_is_bootable(vdev_t
*vd
)
4117 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4118 const char *vdev_type
= vd
->vdev_ops
->vdev_op_type
;
4120 if (strcmp(vdev_type
, VDEV_TYPE_MISSING
) == 0 ||
4121 strcmp(vdev_type
, VDEV_TYPE_INDIRECT
) == 0) {
4126 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4127 if (!vdev_is_bootable(vd
->vdev_child
[c
]))
4134 vdev_is_concrete(vdev_t
*vd
)
4136 vdev_ops_t
*ops
= vd
->vdev_ops
;
4137 if (ops
== &vdev_indirect_ops
|| ops
== &vdev_hole_ops
||
4138 ops
== &vdev_missing_ops
|| ops
== &vdev_root_ops
) {
4146 * Determine if a log device has valid content. If the vdev was
4147 * removed or faulted in the MOS config then we know that
4148 * the content on the log device has already been written to the pool.
4151 vdev_log_state_valid(vdev_t
*vd
)
4153 if (vd
->vdev_ops
->vdev_op_leaf
&& !vd
->vdev_faulted
&&
4157 for (int c
= 0; c
< vd
->vdev_children
; c
++)
4158 if (vdev_log_state_valid(vd
->vdev_child
[c
]))
4165 * Expand a vdev if possible.
4168 vdev_expand(vdev_t
*vd
, uint64_t txg
)
4170 ASSERT(vd
->vdev_top
== vd
);
4171 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4173 vdev_set_deflate_ratio(vd
);
4175 if ((vd
->vdev_asize
>> vd
->vdev_ms_shift
) > vd
->vdev_ms_count
&&
4176 vdev_is_concrete(vd
)) {
4177 VERIFY(vdev_metaslab_init(vd
, txg
) == 0);
4178 vdev_config_dirty(vd
);
4186 vdev_split(vdev_t
*vd
)
4188 vdev_t
*cvd
, *pvd
= vd
->vdev_parent
;
4190 vdev_remove_child(pvd
, vd
);
4191 vdev_compact_children(pvd
);
4193 cvd
= pvd
->vdev_child
[0];
4194 if (pvd
->vdev_children
== 1) {
4195 vdev_remove_parent(cvd
);
4196 cvd
->vdev_splitting
= B_TRUE
;
4198 vdev_propagate_state(cvd
);
4202 vdev_deadman(vdev_t
*vd
, char *tag
)
4204 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4205 vdev_t
*cvd
= vd
->vdev_child
[c
];
4207 vdev_deadman(cvd
, tag
);
4210 if (vd
->vdev_ops
->vdev_op_leaf
) {
4211 vdev_queue_t
*vq
= &vd
->vdev_queue
;
4213 mutex_enter(&vq
->vq_lock
);
4214 if (avl_numnodes(&vq
->vq_active_tree
) > 0) {
4215 spa_t
*spa
= vd
->vdev_spa
;
4219 zfs_dbgmsg("slow vdev: %s has %d active IOs",
4220 vd
->vdev_path
, avl_numnodes(&vq
->vq_active_tree
));
4223 * Look at the head of all the pending queues,
4224 * if any I/O has been outstanding for longer than
4225 * the spa_deadman_synctime invoke the deadman logic.
4227 fio
= avl_first(&vq
->vq_active_tree
);
4228 delta
= gethrtime() - fio
->io_timestamp
;
4229 if (delta
> spa_deadman_synctime(spa
))
4230 zio_deadman(fio
, tag
);
4232 mutex_exit(&vq
->vq_lock
);
4236 #if defined(_KERNEL) && defined(HAVE_SPL)
4237 EXPORT_SYMBOL(vdev_fault
);
4238 EXPORT_SYMBOL(vdev_degrade
);
4239 EXPORT_SYMBOL(vdev_online
);
4240 EXPORT_SYMBOL(vdev_offline
);
4241 EXPORT_SYMBOL(vdev_clear
);
4243 module_param(metaslabs_per_vdev
, int, 0644);
4244 MODULE_PARM_DESC(metaslabs_per_vdev
,
4245 "Divide added vdev into approximately (but no more than) this number "
4248 module_param(zfs_delays_per_second
, uint
, 0644);
4249 MODULE_PARM_DESC(zfs_delays_per_second
, "Rate limit delay events to this many "
4250 "IO delays per second");
4252 module_param(zfs_checksums_per_second
, uint
, 0644);
4253 MODULE_PARM_DESC(zfs_checksums_per_second
, "Rate limit checksum events "
4254 "to this many checksum errors per second (do not set below zed"
4257 module_param(zfs_scan_ignore_errors
, int, 0644);
4258 MODULE_PARM_DESC(zfs_scan_ignore_errors
,
4259 "Ignore errors during resilver/scrub");
4261 module_param(vdev_validate_skip
, int, 0644);
4262 MODULE_PARM_DESC(vdev_validate_skip
,
4263 "Bypass vdev_validate()");