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>
36 #include <sys/dmu_tx.h>
37 #include <sys/vdev_impl.h>
38 #include <sys/uberblock_impl.h>
39 #include <sys/metaslab.h>
40 #include <sys/metaslab_impl.h>
41 #include <sys/space_map.h>
42 #include <sys/space_reftree.h>
45 #include <sys/fs/zfs.h>
48 #include <sys/dsl_scan.h>
51 #include <sys/zfs_ratelimit.h>
54 * When a vdev is added, it will be divided into approximately (but no
55 * more than) this number of metaslabs.
57 int metaslabs_per_vdev
= 200;
60 * Virtual device management.
63 static vdev_ops_t
*vdev_ops_table
[] = {
77 * Given a vdev type, return the appropriate ops vector.
80 vdev_getops(const char *type
)
82 vdev_ops_t
*ops
, **opspp
;
84 for (opspp
= vdev_ops_table
; (ops
= *opspp
) != NULL
; opspp
++)
85 if (strcmp(ops
->vdev_op_type
, type
) == 0)
92 * Default asize function: return the MAX of psize with the asize of
93 * all children. This is what's used by anything other than RAID-Z.
96 vdev_default_asize(vdev_t
*vd
, uint64_t psize
)
98 uint64_t asize
= P2ROUNDUP(psize
, 1ULL << vd
->vdev_top
->vdev_ashift
);
102 for (c
= 0; c
< vd
->vdev_children
; c
++) {
103 csize
= vdev_psize_to_asize(vd
->vdev_child
[c
], psize
);
104 asize
= MAX(asize
, csize
);
111 * Get the minimum allocatable size. We define the allocatable size as
112 * the vdev's asize rounded to the nearest metaslab. This allows us to
113 * replace or attach devices which don't have the same physical size but
114 * can still satisfy the same number of allocations.
117 vdev_get_min_asize(vdev_t
*vd
)
119 vdev_t
*pvd
= vd
->vdev_parent
;
122 * If our parent is NULL (inactive spare or cache) or is the root,
123 * just return our own asize.
126 return (vd
->vdev_asize
);
129 * The top-level vdev just returns the allocatable size rounded
130 * to the nearest metaslab.
132 if (vd
== vd
->vdev_top
)
133 return (P2ALIGN(vd
->vdev_asize
, 1ULL << vd
->vdev_ms_shift
));
136 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
137 * so each child must provide at least 1/Nth of its asize.
139 if (pvd
->vdev_ops
== &vdev_raidz_ops
)
140 return ((pvd
->vdev_min_asize
+ pvd
->vdev_children
- 1) /
143 return (pvd
->vdev_min_asize
);
147 vdev_set_min_asize(vdev_t
*vd
)
150 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
152 for (c
= 0; c
< vd
->vdev_children
; c
++)
153 vdev_set_min_asize(vd
->vdev_child
[c
]);
157 vdev_lookup_top(spa_t
*spa
, uint64_t vdev
)
159 vdev_t
*rvd
= spa
->spa_root_vdev
;
161 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
163 if (vdev
< rvd
->vdev_children
) {
164 ASSERT(rvd
->vdev_child
[vdev
] != NULL
);
165 return (rvd
->vdev_child
[vdev
]);
172 vdev_lookup_by_guid(vdev_t
*vd
, uint64_t guid
)
177 if (vd
->vdev_guid
== guid
)
180 for (c
= 0; c
< vd
->vdev_children
; c
++)
181 if ((mvd
= vdev_lookup_by_guid(vd
->vdev_child
[c
], guid
)) !=
189 vdev_count_leaves_impl(vdev_t
*vd
)
194 if (vd
->vdev_ops
->vdev_op_leaf
)
197 for (c
= 0; c
< vd
->vdev_children
; c
++)
198 n
+= vdev_count_leaves_impl(vd
->vdev_child
[c
]);
204 vdev_count_leaves(spa_t
*spa
)
206 return (vdev_count_leaves_impl(spa
->spa_root_vdev
));
210 vdev_add_child(vdev_t
*pvd
, vdev_t
*cvd
)
212 size_t oldsize
, newsize
;
213 uint64_t id
= cvd
->vdev_id
;
216 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
217 ASSERT(cvd
->vdev_parent
== NULL
);
219 cvd
->vdev_parent
= pvd
;
224 ASSERT(id
>= pvd
->vdev_children
|| pvd
->vdev_child
[id
] == NULL
);
226 oldsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
227 pvd
->vdev_children
= MAX(pvd
->vdev_children
, id
+ 1);
228 newsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
230 newchild
= kmem_alloc(newsize
, KM_SLEEP
);
231 if (pvd
->vdev_child
!= NULL
) {
232 bcopy(pvd
->vdev_child
, newchild
, oldsize
);
233 kmem_free(pvd
->vdev_child
, oldsize
);
236 pvd
->vdev_child
= newchild
;
237 pvd
->vdev_child
[id
] = cvd
;
239 cvd
->vdev_top
= (pvd
->vdev_top
? pvd
->vdev_top
: cvd
);
240 ASSERT(cvd
->vdev_top
->vdev_parent
->vdev_parent
== NULL
);
243 * Walk up all ancestors to update guid sum.
245 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
246 pvd
->vdev_guid_sum
+= cvd
->vdev_guid_sum
;
250 vdev_remove_child(vdev_t
*pvd
, vdev_t
*cvd
)
253 uint_t id
= cvd
->vdev_id
;
255 ASSERT(cvd
->vdev_parent
== pvd
);
260 ASSERT(id
< pvd
->vdev_children
);
261 ASSERT(pvd
->vdev_child
[id
] == cvd
);
263 pvd
->vdev_child
[id
] = NULL
;
264 cvd
->vdev_parent
= NULL
;
266 for (c
= 0; c
< pvd
->vdev_children
; c
++)
267 if (pvd
->vdev_child
[c
])
270 if (c
== pvd
->vdev_children
) {
271 kmem_free(pvd
->vdev_child
, c
* sizeof (vdev_t
*));
272 pvd
->vdev_child
= NULL
;
273 pvd
->vdev_children
= 0;
277 * Walk up all ancestors to update guid sum.
279 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
280 pvd
->vdev_guid_sum
-= cvd
->vdev_guid_sum
;
284 * Remove any holes in the child array.
287 vdev_compact_children(vdev_t
*pvd
)
289 vdev_t
**newchild
, *cvd
;
290 int oldc
= pvd
->vdev_children
;
294 ASSERT(spa_config_held(pvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
296 for (c
= newc
= 0; c
< oldc
; c
++)
297 if (pvd
->vdev_child
[c
])
300 newchild
= kmem_zalloc(newc
* sizeof (vdev_t
*), KM_SLEEP
);
302 for (c
= newc
= 0; c
< oldc
; c
++) {
303 if ((cvd
= pvd
->vdev_child
[c
]) != NULL
) {
304 newchild
[newc
] = cvd
;
305 cvd
->vdev_id
= newc
++;
309 kmem_free(pvd
->vdev_child
, oldc
* sizeof (vdev_t
*));
310 pvd
->vdev_child
= newchild
;
311 pvd
->vdev_children
= newc
;
315 * Allocate and minimally initialize a vdev_t.
318 vdev_alloc_common(spa_t
*spa
, uint_t id
, uint64_t guid
, vdev_ops_t
*ops
)
323 vd
= kmem_zalloc(sizeof (vdev_t
), KM_SLEEP
);
325 if (spa
->spa_root_vdev
== NULL
) {
326 ASSERT(ops
== &vdev_root_ops
);
327 spa
->spa_root_vdev
= vd
;
328 spa
->spa_load_guid
= spa_generate_guid(NULL
);
331 if (guid
== 0 && ops
!= &vdev_hole_ops
) {
332 if (spa
->spa_root_vdev
== vd
) {
334 * The root vdev's guid will also be the pool guid,
335 * which must be unique among all pools.
337 guid
= spa_generate_guid(NULL
);
340 * Any other vdev's guid must be unique within the pool.
342 guid
= spa_generate_guid(spa
);
344 ASSERT(!spa_guid_exists(spa_guid(spa
), guid
));
349 vd
->vdev_guid
= guid
;
350 vd
->vdev_guid_sum
= guid
;
352 vd
->vdev_state
= VDEV_STATE_CLOSED
;
353 vd
->vdev_ishole
= (ops
== &vdev_hole_ops
);
356 * Initialize rate limit structs for events. We rate limit ZIO delay
357 * and checksum events so that we don't overwhelm ZED with thousands
358 * of events when a disk is acting up.
360 zfs_ratelimit_init(&vd
->vdev_delay_rl
, DELAYS_PER_SECOND
, 1);
361 zfs_ratelimit_init(&vd
->vdev_checksum_rl
, CHECKSUMS_PER_SECOND
, 1);
363 list_link_init(&vd
->vdev_config_dirty_node
);
364 list_link_init(&vd
->vdev_state_dirty_node
);
365 mutex_init(&vd
->vdev_dtl_lock
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
366 mutex_init(&vd
->vdev_stat_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
367 mutex_init(&vd
->vdev_probe_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
368 mutex_init(&vd
->vdev_queue_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
370 for (t
= 0; t
< DTL_TYPES
; t
++) {
371 vd
->vdev_dtl
[t
] = range_tree_create(NULL
, NULL
,
374 txg_list_create(&vd
->vdev_ms_list
, spa
,
375 offsetof(struct metaslab
, ms_txg_node
));
376 txg_list_create(&vd
->vdev_dtl_list
, spa
,
377 offsetof(struct vdev
, vdev_dtl_node
));
378 vd
->vdev_stat
.vs_timestamp
= gethrtime();
386 * Allocate a new vdev. The 'alloctype' is used to control whether we are
387 * creating a new vdev or loading an existing one - the behavior is slightly
388 * different for each case.
391 vdev_alloc(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
, uint_t id
,
396 uint64_t guid
= 0, islog
, nparity
;
401 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
403 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
404 return (SET_ERROR(EINVAL
));
406 if ((ops
= vdev_getops(type
)) == NULL
)
407 return (SET_ERROR(EINVAL
));
410 * If this is a load, get the vdev guid from the nvlist.
411 * Otherwise, vdev_alloc_common() will generate one for us.
413 if (alloctype
== VDEV_ALLOC_LOAD
) {
416 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
, &label_id
) ||
418 return (SET_ERROR(EINVAL
));
420 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
421 return (SET_ERROR(EINVAL
));
422 } else if (alloctype
== VDEV_ALLOC_SPARE
) {
423 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
424 return (SET_ERROR(EINVAL
));
425 } else if (alloctype
== VDEV_ALLOC_L2CACHE
) {
426 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
427 return (SET_ERROR(EINVAL
));
428 } else if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
429 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
430 return (SET_ERROR(EINVAL
));
434 * The first allocated vdev must be of type 'root'.
436 if (ops
!= &vdev_root_ops
&& spa
->spa_root_vdev
== NULL
)
437 return (SET_ERROR(EINVAL
));
440 * Determine whether we're a log vdev.
443 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &islog
);
444 if (islog
&& spa_version(spa
) < SPA_VERSION_SLOGS
)
445 return (SET_ERROR(ENOTSUP
));
447 if (ops
== &vdev_hole_ops
&& spa_version(spa
) < SPA_VERSION_HOLES
)
448 return (SET_ERROR(ENOTSUP
));
451 * Set the nparity property for RAID-Z vdevs.
454 if (ops
== &vdev_raidz_ops
) {
455 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
457 if (nparity
== 0 || nparity
> VDEV_RAIDZ_MAXPARITY
)
458 return (SET_ERROR(EINVAL
));
460 * Previous versions could only support 1 or 2 parity
464 spa_version(spa
) < SPA_VERSION_RAIDZ2
)
465 return (SET_ERROR(ENOTSUP
));
467 spa_version(spa
) < SPA_VERSION_RAIDZ3
)
468 return (SET_ERROR(ENOTSUP
));
471 * We require the parity to be specified for SPAs that
472 * support multiple parity levels.
474 if (spa_version(spa
) >= SPA_VERSION_RAIDZ2
)
475 return (SET_ERROR(EINVAL
));
477 * Otherwise, we default to 1 parity device for RAID-Z.
484 ASSERT(nparity
!= -1ULL);
486 vd
= vdev_alloc_common(spa
, id
, guid
, ops
);
488 vd
->vdev_islog
= islog
;
489 vd
->vdev_nparity
= nparity
;
491 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &vd
->vdev_path
) == 0)
492 vd
->vdev_path
= spa_strdup(vd
->vdev_path
);
495 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
496 * fault on a vdev and want it to persist across imports (like with
499 rc
= nvlist_lookup_string(nv
, ZPOOL_CONFIG_AUX_STATE
, &tmp
);
500 if (rc
== 0 && tmp
!= NULL
&& strcmp(tmp
, "external") == 0) {
501 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
502 vd
->vdev_faulted
= 1;
503 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
506 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &vd
->vdev_devid
) == 0)
507 vd
->vdev_devid
= spa_strdup(vd
->vdev_devid
);
508 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PHYS_PATH
,
509 &vd
->vdev_physpath
) == 0)
510 vd
->vdev_physpath
= spa_strdup(vd
->vdev_physpath
);
512 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH
,
513 &vd
->vdev_enc_sysfs_path
) == 0)
514 vd
->vdev_enc_sysfs_path
= spa_strdup(vd
->vdev_enc_sysfs_path
);
516 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_FRU
, &vd
->vdev_fru
) == 0)
517 vd
->vdev_fru
= spa_strdup(vd
->vdev_fru
);
520 * Set the whole_disk property. If it's not specified, leave the value
523 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
524 &vd
->vdev_wholedisk
) != 0)
525 vd
->vdev_wholedisk
= -1ULL;
528 * Look for the 'not present' flag. This will only be set if the device
529 * was not present at the time of import.
531 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
532 &vd
->vdev_not_present
);
535 * Get the alignment requirement.
537 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASHIFT
, &vd
->vdev_ashift
);
540 * Retrieve the vdev creation time.
542 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_CREATE_TXG
,
546 * If we're a top-level vdev, try to load the allocation parameters.
548 if (parent
&& !parent
->vdev_parent
&&
549 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
550 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_ARRAY
,
552 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_SHIFT
,
554 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASIZE
,
556 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVING
,
558 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_VDEV_TOP_ZAP
,
561 ASSERT0(vd
->vdev_top_zap
);
564 if (parent
&& !parent
->vdev_parent
&& alloctype
!= VDEV_ALLOC_ATTACH
) {
565 ASSERT(alloctype
== VDEV_ALLOC_LOAD
||
566 alloctype
== VDEV_ALLOC_ADD
||
567 alloctype
== VDEV_ALLOC_SPLIT
||
568 alloctype
== VDEV_ALLOC_ROOTPOOL
);
569 vd
->vdev_mg
= metaslab_group_create(islog
?
570 spa_log_class(spa
) : spa_normal_class(spa
), vd
);
573 if (vd
->vdev_ops
->vdev_op_leaf
&&
574 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
575 (void) nvlist_lookup_uint64(nv
,
576 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, &vd
->vdev_leaf_zap
);
578 ASSERT0(vd
->vdev_leaf_zap
);
582 * If we're a leaf vdev, try to load the DTL object and other state.
585 if (vd
->vdev_ops
->vdev_op_leaf
&&
586 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_L2CACHE
||
587 alloctype
== VDEV_ALLOC_ROOTPOOL
)) {
588 if (alloctype
== VDEV_ALLOC_LOAD
) {
589 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DTL
,
590 &vd
->vdev_dtl_object
);
591 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_UNSPARE
,
595 if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
598 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
599 &spare
) == 0 && spare
)
603 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
,
606 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_RESILVER_TXG
,
607 &vd
->vdev_resilver_txg
);
610 * In general, when importing a pool we want to ignore the
611 * persistent fault state, as the diagnosis made on another
612 * system may not be valid in the current context. The only
613 * exception is if we forced a vdev to a persistently faulted
614 * state with 'zpool offline -f'. The persistent fault will
615 * remain across imports until cleared.
617 * Local vdevs will remain in the faulted state.
619 if (spa_load_state(spa
) == SPA_LOAD_OPEN
||
620 spa_load_state(spa
) == SPA_LOAD_IMPORT
) {
621 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
,
623 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DEGRADED
,
625 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
,
628 if (vd
->vdev_faulted
|| vd
->vdev_degraded
) {
632 VDEV_AUX_ERR_EXCEEDED
;
633 if (nvlist_lookup_string(nv
,
634 ZPOOL_CONFIG_AUX_STATE
, &aux
) == 0 &&
635 strcmp(aux
, "external") == 0)
636 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
642 * Add ourselves to the parent's list of children.
644 vdev_add_child(parent
, vd
);
652 vdev_free(vdev_t
*vd
)
655 spa_t
*spa
= vd
->vdev_spa
;
658 * vdev_free() implies closing the vdev first. This is simpler than
659 * trying to ensure complicated semantics for all callers.
663 ASSERT(!list_link_active(&vd
->vdev_config_dirty_node
));
664 ASSERT(!list_link_active(&vd
->vdev_state_dirty_node
));
669 for (c
= 0; c
< vd
->vdev_children
; c
++)
670 vdev_free(vd
->vdev_child
[c
]);
672 ASSERT(vd
->vdev_child
== NULL
);
673 ASSERT(vd
->vdev_guid_sum
== vd
->vdev_guid
);
676 * Discard allocation state.
678 if (vd
->vdev_mg
!= NULL
) {
679 vdev_metaslab_fini(vd
);
680 metaslab_group_destroy(vd
->vdev_mg
);
683 ASSERT0(vd
->vdev_stat
.vs_space
);
684 ASSERT0(vd
->vdev_stat
.vs_dspace
);
685 ASSERT0(vd
->vdev_stat
.vs_alloc
);
688 * Remove this vdev from its parent's child list.
690 vdev_remove_child(vd
->vdev_parent
, vd
);
692 ASSERT(vd
->vdev_parent
== NULL
);
695 * Clean up vdev structure.
701 spa_strfree(vd
->vdev_path
);
703 spa_strfree(vd
->vdev_devid
);
704 if (vd
->vdev_physpath
)
705 spa_strfree(vd
->vdev_physpath
);
707 if (vd
->vdev_enc_sysfs_path
)
708 spa_strfree(vd
->vdev_enc_sysfs_path
);
711 spa_strfree(vd
->vdev_fru
);
713 if (vd
->vdev_isspare
)
714 spa_spare_remove(vd
);
715 if (vd
->vdev_isl2cache
)
716 spa_l2cache_remove(vd
);
718 txg_list_destroy(&vd
->vdev_ms_list
);
719 txg_list_destroy(&vd
->vdev_dtl_list
);
721 mutex_enter(&vd
->vdev_dtl_lock
);
722 space_map_close(vd
->vdev_dtl_sm
);
723 for (t
= 0; t
< DTL_TYPES
; t
++) {
724 range_tree_vacate(vd
->vdev_dtl
[t
], NULL
, NULL
);
725 range_tree_destroy(vd
->vdev_dtl
[t
]);
727 mutex_exit(&vd
->vdev_dtl_lock
);
729 mutex_destroy(&vd
->vdev_queue_lock
);
730 mutex_destroy(&vd
->vdev_dtl_lock
);
731 mutex_destroy(&vd
->vdev_stat_lock
);
732 mutex_destroy(&vd
->vdev_probe_lock
);
734 zfs_ratelimit_fini(&vd
->vdev_delay_rl
);
735 zfs_ratelimit_fini(&vd
->vdev_checksum_rl
);
737 if (vd
== spa
->spa_root_vdev
)
738 spa
->spa_root_vdev
= NULL
;
740 kmem_free(vd
, sizeof (vdev_t
));
744 * Transfer top-level vdev state from svd to tvd.
747 vdev_top_transfer(vdev_t
*svd
, vdev_t
*tvd
)
749 spa_t
*spa
= svd
->vdev_spa
;
754 ASSERT(tvd
== tvd
->vdev_top
);
756 tvd
->vdev_pending_fastwrite
= svd
->vdev_pending_fastwrite
;
757 tvd
->vdev_ms_array
= svd
->vdev_ms_array
;
758 tvd
->vdev_ms_shift
= svd
->vdev_ms_shift
;
759 tvd
->vdev_ms_count
= svd
->vdev_ms_count
;
760 tvd
->vdev_top_zap
= svd
->vdev_top_zap
;
762 svd
->vdev_ms_array
= 0;
763 svd
->vdev_ms_shift
= 0;
764 svd
->vdev_ms_count
= 0;
765 svd
->vdev_top_zap
= 0;
768 ASSERT3P(tvd
->vdev_mg
, ==, svd
->vdev_mg
);
769 tvd
->vdev_mg
= svd
->vdev_mg
;
770 tvd
->vdev_ms
= svd
->vdev_ms
;
775 if (tvd
->vdev_mg
!= NULL
)
776 tvd
->vdev_mg
->mg_vd
= tvd
;
778 tvd
->vdev_stat
.vs_alloc
= svd
->vdev_stat
.vs_alloc
;
779 tvd
->vdev_stat
.vs_space
= svd
->vdev_stat
.vs_space
;
780 tvd
->vdev_stat
.vs_dspace
= svd
->vdev_stat
.vs_dspace
;
782 svd
->vdev_stat
.vs_alloc
= 0;
783 svd
->vdev_stat
.vs_space
= 0;
784 svd
->vdev_stat
.vs_dspace
= 0;
786 for (t
= 0; t
< TXG_SIZE
; t
++) {
787 while ((msp
= txg_list_remove(&svd
->vdev_ms_list
, t
)) != NULL
)
788 (void) txg_list_add(&tvd
->vdev_ms_list
, msp
, t
);
789 while ((vd
= txg_list_remove(&svd
->vdev_dtl_list
, t
)) != NULL
)
790 (void) txg_list_add(&tvd
->vdev_dtl_list
, vd
, t
);
791 if (txg_list_remove_this(&spa
->spa_vdev_txg_list
, svd
, t
))
792 (void) txg_list_add(&spa
->spa_vdev_txg_list
, tvd
, t
);
795 if (list_link_active(&svd
->vdev_config_dirty_node
)) {
796 vdev_config_clean(svd
);
797 vdev_config_dirty(tvd
);
800 if (list_link_active(&svd
->vdev_state_dirty_node
)) {
801 vdev_state_clean(svd
);
802 vdev_state_dirty(tvd
);
805 tvd
->vdev_deflate_ratio
= svd
->vdev_deflate_ratio
;
806 svd
->vdev_deflate_ratio
= 0;
808 tvd
->vdev_islog
= svd
->vdev_islog
;
813 vdev_top_update(vdev_t
*tvd
, vdev_t
*vd
)
822 for (c
= 0; c
< vd
->vdev_children
; c
++)
823 vdev_top_update(tvd
, vd
->vdev_child
[c
]);
827 * Add a mirror/replacing vdev above an existing vdev.
830 vdev_add_parent(vdev_t
*cvd
, vdev_ops_t
*ops
)
832 spa_t
*spa
= cvd
->vdev_spa
;
833 vdev_t
*pvd
= cvd
->vdev_parent
;
836 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
838 mvd
= vdev_alloc_common(spa
, cvd
->vdev_id
, 0, ops
);
840 mvd
->vdev_asize
= cvd
->vdev_asize
;
841 mvd
->vdev_min_asize
= cvd
->vdev_min_asize
;
842 mvd
->vdev_max_asize
= cvd
->vdev_max_asize
;
843 mvd
->vdev_ashift
= cvd
->vdev_ashift
;
844 mvd
->vdev_state
= cvd
->vdev_state
;
845 mvd
->vdev_crtxg
= cvd
->vdev_crtxg
;
847 vdev_remove_child(pvd
, cvd
);
848 vdev_add_child(pvd
, mvd
);
849 cvd
->vdev_id
= mvd
->vdev_children
;
850 vdev_add_child(mvd
, cvd
);
851 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
853 if (mvd
== mvd
->vdev_top
)
854 vdev_top_transfer(cvd
, mvd
);
860 * Remove a 1-way mirror/replacing vdev from the tree.
863 vdev_remove_parent(vdev_t
*cvd
)
865 vdev_t
*mvd
= cvd
->vdev_parent
;
866 vdev_t
*pvd
= mvd
->vdev_parent
;
868 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
870 ASSERT(mvd
->vdev_children
== 1);
871 ASSERT(mvd
->vdev_ops
== &vdev_mirror_ops
||
872 mvd
->vdev_ops
== &vdev_replacing_ops
||
873 mvd
->vdev_ops
== &vdev_spare_ops
);
874 cvd
->vdev_ashift
= mvd
->vdev_ashift
;
876 vdev_remove_child(mvd
, cvd
);
877 vdev_remove_child(pvd
, mvd
);
880 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
881 * Otherwise, we could have detached an offline device, and when we
882 * go to import the pool we'll think we have two top-level vdevs,
883 * instead of a different version of the same top-level vdev.
885 if (mvd
->vdev_top
== mvd
) {
886 uint64_t guid_delta
= mvd
->vdev_guid
- cvd
->vdev_guid
;
887 cvd
->vdev_orig_guid
= cvd
->vdev_guid
;
888 cvd
->vdev_guid
+= guid_delta
;
889 cvd
->vdev_guid_sum
+= guid_delta
;
892 * If pool not set for autoexpand, we need to also preserve
893 * mvd's asize to prevent automatic expansion of cvd.
894 * Otherwise if we are adjusting the mirror by attaching and
895 * detaching children of non-uniform sizes, the mirror could
896 * autoexpand, unexpectedly requiring larger devices to
897 * re-establish the mirror.
899 if (!cvd
->vdev_spa
->spa_autoexpand
)
900 cvd
->vdev_asize
= mvd
->vdev_asize
;
902 cvd
->vdev_id
= mvd
->vdev_id
;
903 vdev_add_child(pvd
, cvd
);
904 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
906 if (cvd
== cvd
->vdev_top
)
907 vdev_top_transfer(mvd
, cvd
);
909 ASSERT(mvd
->vdev_children
== 0);
914 vdev_metaslab_init(vdev_t
*vd
, uint64_t txg
)
916 spa_t
*spa
= vd
->vdev_spa
;
917 objset_t
*mos
= spa
->spa_meta_objset
;
919 uint64_t oldc
= vd
->vdev_ms_count
;
920 uint64_t newc
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
924 ASSERT(txg
== 0 || spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
927 * This vdev is not being allocated from yet or is a hole.
929 if (vd
->vdev_ms_shift
== 0)
932 ASSERT(!vd
->vdev_ishole
);
935 * Compute the raidz-deflation ratio. Note, we hard-code
936 * in 128k (1 << 17) because it is the "typical" blocksize.
937 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
938 * otherwise it would inconsistently account for existing bp's.
940 vd
->vdev_deflate_ratio
= (1 << 17) /
941 (vdev_psize_to_asize(vd
, 1 << 17) >> SPA_MINBLOCKSHIFT
);
943 ASSERT(oldc
<= newc
);
945 mspp
= vmem_zalloc(newc
* sizeof (*mspp
), KM_SLEEP
);
948 bcopy(vd
->vdev_ms
, mspp
, oldc
* sizeof (*mspp
));
949 vmem_free(vd
->vdev_ms
, oldc
* sizeof (*mspp
));
953 vd
->vdev_ms_count
= newc
;
955 for (m
= oldc
; m
< newc
; m
++) {
959 error
= dmu_read(mos
, vd
->vdev_ms_array
,
960 m
* sizeof (uint64_t), sizeof (uint64_t), &object
,
966 error
= metaslab_init(vd
->vdev_mg
, m
, object
, txg
,
973 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_WRITER
);
976 * If the vdev is being removed we don't activate
977 * the metaslabs since we want to ensure that no new
978 * allocations are performed on this device.
980 if (oldc
== 0 && !vd
->vdev_removing
)
981 metaslab_group_activate(vd
->vdev_mg
);
984 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
990 vdev_metaslab_fini(vdev_t
*vd
)
993 uint64_t count
= vd
->vdev_ms_count
;
995 if (vd
->vdev_ms
!= NULL
) {
996 metaslab_group_passivate(vd
->vdev_mg
);
997 for (m
= 0; m
< count
; m
++) {
998 metaslab_t
*msp
= vd
->vdev_ms
[m
];
1003 vmem_free(vd
->vdev_ms
, count
* sizeof (metaslab_t
*));
1007 ASSERT3U(vd
->vdev_pending_fastwrite
, ==, 0);
1010 typedef struct vdev_probe_stats
{
1011 boolean_t vps_readable
;
1012 boolean_t vps_writeable
;
1014 } vdev_probe_stats_t
;
1017 vdev_probe_done(zio_t
*zio
)
1019 spa_t
*spa
= zio
->io_spa
;
1020 vdev_t
*vd
= zio
->io_vd
;
1021 vdev_probe_stats_t
*vps
= zio
->io_private
;
1023 ASSERT(vd
->vdev_probe_zio
!= NULL
);
1025 if (zio
->io_type
== ZIO_TYPE_READ
) {
1026 if (zio
->io_error
== 0)
1027 vps
->vps_readable
= 1;
1028 if (zio
->io_error
== 0 && spa_writeable(spa
)) {
1029 zio_nowait(zio_write_phys(vd
->vdev_probe_zio
, vd
,
1030 zio
->io_offset
, zio
->io_size
, zio
->io_abd
,
1031 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1032 ZIO_PRIORITY_SYNC_WRITE
, vps
->vps_flags
, B_TRUE
));
1034 abd_free(zio
->io_abd
);
1036 } else if (zio
->io_type
== ZIO_TYPE_WRITE
) {
1037 if (zio
->io_error
== 0)
1038 vps
->vps_writeable
= 1;
1039 abd_free(zio
->io_abd
);
1040 } else if (zio
->io_type
== ZIO_TYPE_NULL
) {
1044 vd
->vdev_cant_read
|= !vps
->vps_readable
;
1045 vd
->vdev_cant_write
|= !vps
->vps_writeable
;
1047 if (vdev_readable(vd
) &&
1048 (vdev_writeable(vd
) || !spa_writeable(spa
))) {
1051 ASSERT(zio
->io_error
!= 0);
1052 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE
,
1053 spa
, vd
, NULL
, NULL
, 0, 0);
1054 zio
->io_error
= SET_ERROR(ENXIO
);
1057 mutex_enter(&vd
->vdev_probe_lock
);
1058 ASSERT(vd
->vdev_probe_zio
== zio
);
1059 vd
->vdev_probe_zio
= NULL
;
1060 mutex_exit(&vd
->vdev_probe_lock
);
1063 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
)
1064 if (!vdev_accessible(vd
, pio
))
1065 pio
->io_error
= SET_ERROR(ENXIO
);
1067 kmem_free(vps
, sizeof (*vps
));
1072 * Determine whether this device is accessible.
1074 * Read and write to several known locations: the pad regions of each
1075 * vdev label but the first, which we leave alone in case it contains
1079 vdev_probe(vdev_t
*vd
, zio_t
*zio
)
1081 spa_t
*spa
= vd
->vdev_spa
;
1082 vdev_probe_stats_t
*vps
= NULL
;
1086 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1089 * Don't probe the probe.
1091 if (zio
&& (zio
->io_flags
& ZIO_FLAG_PROBE
))
1095 * To prevent 'probe storms' when a device fails, we create
1096 * just one probe i/o at a time. All zios that want to probe
1097 * this vdev will become parents of the probe io.
1099 mutex_enter(&vd
->vdev_probe_lock
);
1101 if ((pio
= vd
->vdev_probe_zio
) == NULL
) {
1102 vps
= kmem_zalloc(sizeof (*vps
), KM_SLEEP
);
1104 vps
->vps_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_PROBE
|
1105 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_AGGREGATE
|
1108 if (spa_config_held(spa
, SCL_ZIO
, RW_WRITER
)) {
1110 * vdev_cant_read and vdev_cant_write can only
1111 * transition from TRUE to FALSE when we have the
1112 * SCL_ZIO lock as writer; otherwise they can only
1113 * transition from FALSE to TRUE. This ensures that
1114 * any zio looking at these values can assume that
1115 * failures persist for the life of the I/O. That's
1116 * important because when a device has intermittent
1117 * connectivity problems, we want to ensure that
1118 * they're ascribed to the device (ENXIO) and not
1121 * Since we hold SCL_ZIO as writer here, clear both
1122 * values so the probe can reevaluate from first
1125 vps
->vps_flags
|= ZIO_FLAG_CONFIG_WRITER
;
1126 vd
->vdev_cant_read
= B_FALSE
;
1127 vd
->vdev_cant_write
= B_FALSE
;
1130 vd
->vdev_probe_zio
= pio
= zio_null(NULL
, spa
, vd
,
1131 vdev_probe_done
, vps
,
1132 vps
->vps_flags
| ZIO_FLAG_DONT_PROPAGATE
);
1135 * We can't change the vdev state in this context, so we
1136 * kick off an async task to do it on our behalf.
1139 vd
->vdev_probe_wanted
= B_TRUE
;
1140 spa_async_request(spa
, SPA_ASYNC_PROBE
);
1145 zio_add_child(zio
, pio
);
1147 mutex_exit(&vd
->vdev_probe_lock
);
1150 ASSERT(zio
!= NULL
);
1154 for (l
= 1; l
< VDEV_LABELS
; l
++) {
1155 zio_nowait(zio_read_phys(pio
, vd
,
1156 vdev_label_offset(vd
->vdev_psize
, l
,
1157 offsetof(vdev_label_t
, vl_pad2
)), VDEV_PAD_SIZE
,
1158 abd_alloc_for_io(VDEV_PAD_SIZE
, B_TRUE
),
1159 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1160 ZIO_PRIORITY_SYNC_READ
, vps
->vps_flags
, B_TRUE
));
1171 vdev_open_child(void *arg
)
1175 vd
->vdev_open_thread
= curthread
;
1176 vd
->vdev_open_error
= vdev_open(vd
);
1177 vd
->vdev_open_thread
= NULL
;
1181 vdev_uses_zvols(vdev_t
*vd
)
1186 if (zvol_is_zvol(vd
->vdev_path
))
1190 for (c
= 0; c
< vd
->vdev_children
; c
++)
1191 if (vdev_uses_zvols(vd
->vdev_child
[c
]))
1198 vdev_open_children(vdev_t
*vd
)
1201 int children
= vd
->vdev_children
;
1205 * in order to handle pools on top of zvols, do the opens
1206 * in a single thread so that the same thread holds the
1207 * spa_namespace_lock
1209 if (vdev_uses_zvols(vd
)) {
1211 for (c
= 0; c
< children
; c
++)
1212 vd
->vdev_child
[c
]->vdev_open_error
=
1213 vdev_open(vd
->vdev_child
[c
]);
1215 tq
= taskq_create("vdev_open", children
, minclsyspri
,
1216 children
, children
, TASKQ_PREPOPULATE
);
1220 for (c
= 0; c
< children
; c
++)
1221 VERIFY(taskq_dispatch(tq
, vdev_open_child
,
1222 vd
->vdev_child
[c
], TQ_SLEEP
) != TASKQID_INVALID
);
1227 vd
->vdev_nonrot
= B_TRUE
;
1229 for (c
= 0; c
< children
; c
++)
1230 vd
->vdev_nonrot
&= vd
->vdev_child
[c
]->vdev_nonrot
;
1234 * Prepare a virtual device for access.
1237 vdev_open(vdev_t
*vd
)
1239 spa_t
*spa
= vd
->vdev_spa
;
1242 uint64_t max_osize
= 0;
1243 uint64_t asize
, max_asize
, psize
;
1244 uint64_t ashift
= 0;
1247 ASSERT(vd
->vdev_open_thread
== curthread
||
1248 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1249 ASSERT(vd
->vdev_state
== VDEV_STATE_CLOSED
||
1250 vd
->vdev_state
== VDEV_STATE_CANT_OPEN
||
1251 vd
->vdev_state
== VDEV_STATE_OFFLINE
);
1253 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1254 vd
->vdev_cant_read
= B_FALSE
;
1255 vd
->vdev_cant_write
= B_FALSE
;
1256 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
1259 * If this vdev is not removed, check its fault status. If it's
1260 * faulted, bail out of the open.
1262 if (!vd
->vdev_removed
&& vd
->vdev_faulted
) {
1263 ASSERT(vd
->vdev_children
== 0);
1264 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1265 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1266 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1267 vd
->vdev_label_aux
);
1268 return (SET_ERROR(ENXIO
));
1269 } else if (vd
->vdev_offline
) {
1270 ASSERT(vd
->vdev_children
== 0);
1271 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
, VDEV_AUX_NONE
);
1272 return (SET_ERROR(ENXIO
));
1275 error
= vd
->vdev_ops
->vdev_op_open(vd
, &osize
, &max_osize
, &ashift
);
1278 * Reset the vdev_reopening flag so that we actually close
1279 * the vdev on error.
1281 vd
->vdev_reopening
= B_FALSE
;
1282 if (zio_injection_enabled
&& error
== 0)
1283 error
= zio_handle_device_injection(vd
, NULL
, ENXIO
);
1286 if (vd
->vdev_removed
&&
1287 vd
->vdev_stat
.vs_aux
!= VDEV_AUX_OPEN_FAILED
)
1288 vd
->vdev_removed
= B_FALSE
;
1290 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1291 vd
->vdev_stat
.vs_aux
);
1295 vd
->vdev_removed
= B_FALSE
;
1298 * Recheck the faulted flag now that we have confirmed that
1299 * the vdev is accessible. If we're faulted, bail.
1301 if (vd
->vdev_faulted
) {
1302 ASSERT(vd
->vdev_children
== 0);
1303 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1304 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1305 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1306 vd
->vdev_label_aux
);
1307 return (SET_ERROR(ENXIO
));
1310 if (vd
->vdev_degraded
) {
1311 ASSERT(vd
->vdev_children
== 0);
1312 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1313 VDEV_AUX_ERR_EXCEEDED
);
1315 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_HEALTHY
, 0);
1319 * For hole or missing vdevs we just return success.
1321 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
)
1324 for (c
= 0; c
< vd
->vdev_children
; c
++) {
1325 if (vd
->vdev_child
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
) {
1326 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1332 osize
= P2ALIGN(osize
, (uint64_t)sizeof (vdev_label_t
));
1333 max_osize
= P2ALIGN(max_osize
, (uint64_t)sizeof (vdev_label_t
));
1335 if (vd
->vdev_children
== 0) {
1336 if (osize
< SPA_MINDEVSIZE
) {
1337 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1338 VDEV_AUX_TOO_SMALL
);
1339 return (SET_ERROR(EOVERFLOW
));
1342 asize
= osize
- (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
);
1343 max_asize
= max_osize
- (VDEV_LABEL_START_SIZE
+
1344 VDEV_LABEL_END_SIZE
);
1346 if (vd
->vdev_parent
!= NULL
&& osize
< SPA_MINDEVSIZE
-
1347 (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
)) {
1348 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1349 VDEV_AUX_TOO_SMALL
);
1350 return (SET_ERROR(EOVERFLOW
));
1354 max_asize
= max_osize
;
1358 * If the vdev was expanded, record this so that we can re-create the
1359 * uberblock rings in labels {2,3}, during the next sync.
1361 if ((psize
> vd
->vdev_psize
) && (vd
->vdev_psize
!= 0))
1362 vd
->vdev_copy_uberblocks
= B_TRUE
;
1364 vd
->vdev_psize
= psize
;
1367 * Make sure the allocatable size hasn't shrunk too much.
1369 if (asize
< vd
->vdev_min_asize
) {
1370 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1371 VDEV_AUX_BAD_LABEL
);
1372 return (SET_ERROR(EINVAL
));
1375 if (vd
->vdev_asize
== 0) {
1377 * This is the first-ever open, so use the computed values.
1378 * For compatibility, a different ashift can be requested.
1380 vd
->vdev_asize
= asize
;
1381 vd
->vdev_max_asize
= max_asize
;
1382 if (vd
->vdev_ashift
== 0) {
1383 vd
->vdev_ashift
= ashift
; /* use detected value */
1385 if (vd
->vdev_ashift
!= 0 && (vd
->vdev_ashift
< ASHIFT_MIN
||
1386 vd
->vdev_ashift
> ASHIFT_MAX
)) {
1387 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1388 VDEV_AUX_BAD_ASHIFT
);
1389 return (SET_ERROR(EDOM
));
1393 * Detect if the alignment requirement has increased.
1394 * We don't want to make the pool unavailable, just
1395 * post an event instead.
1397 if (ashift
> vd
->vdev_top
->vdev_ashift
&&
1398 vd
->vdev_ops
->vdev_op_leaf
) {
1399 zfs_ereport_post(FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
,
1400 spa
, vd
, NULL
, NULL
, 0, 0);
1403 vd
->vdev_max_asize
= max_asize
;
1407 * If all children are healthy we update asize if either:
1408 * The asize has increased, due to a device expansion caused by dynamic
1409 * LUN growth or vdev replacement, and automatic expansion is enabled;
1410 * making the additional space available.
1412 * The asize has decreased, due to a device shrink usually caused by a
1413 * vdev replace with a smaller device. This ensures that calculations
1414 * based of max_asize and asize e.g. esize are always valid. It's safe
1415 * to do this as we've already validated that asize is greater than
1418 if (vd
->vdev_state
== VDEV_STATE_HEALTHY
&&
1419 ((asize
> vd
->vdev_asize
&&
1420 (vd
->vdev_expanding
|| spa
->spa_autoexpand
)) ||
1421 (asize
< vd
->vdev_asize
)))
1422 vd
->vdev_asize
= asize
;
1424 vdev_set_min_asize(vd
);
1427 * Ensure we can issue some IO before declaring the
1428 * vdev open for business.
1430 if (vd
->vdev_ops
->vdev_op_leaf
&&
1431 (error
= zio_wait(vdev_probe(vd
, NULL
))) != 0) {
1432 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1433 VDEV_AUX_ERR_EXCEEDED
);
1438 * Track the min and max ashift values for normal data devices.
1440 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
1441 !vd
->vdev_islog
&& vd
->vdev_aux
== NULL
) {
1442 if (vd
->vdev_ashift
> spa
->spa_max_ashift
)
1443 spa
->spa_max_ashift
= vd
->vdev_ashift
;
1444 if (vd
->vdev_ashift
< spa
->spa_min_ashift
)
1445 spa
->spa_min_ashift
= vd
->vdev_ashift
;
1449 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1450 * resilver. But don't do this if we are doing a reopen for a scrub,
1451 * since this would just restart the scrub we are already doing.
1453 if (vd
->vdev_ops
->vdev_op_leaf
&& !spa
->spa_scrub_reopen
&&
1454 vdev_resilver_needed(vd
, NULL
, NULL
))
1455 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
1461 * Called once the vdevs are all opened, this routine validates the label
1462 * contents. This needs to be done before vdev_load() so that we don't
1463 * inadvertently do repair I/Os to the wrong device.
1465 * If 'strict' is false ignore the spa guid check. This is necessary because
1466 * if the machine crashed during a re-guid the new guid might have been written
1467 * to all of the vdev labels, but not the cached config. The strict check
1468 * will be performed when the pool is opened again using the mos config.
1470 * This function will only return failure if one of the vdevs indicates that it
1471 * has since been destroyed or exported. This is only possible if
1472 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1473 * will be updated but the function will return 0.
1476 vdev_validate(vdev_t
*vd
, boolean_t strict
)
1478 spa_t
*spa
= vd
->vdev_spa
;
1480 uint64_t guid
= 0, top_guid
;
1484 for (c
= 0; c
< vd
->vdev_children
; c
++)
1485 if (vdev_validate(vd
->vdev_child
[c
], strict
) != 0)
1486 return (SET_ERROR(EBADF
));
1489 * If the device has already failed, or was marked offline, don't do
1490 * any further validation. Otherwise, label I/O will fail and we will
1491 * overwrite the previous state.
1493 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_readable(vd
)) {
1494 uint64_t aux_guid
= 0;
1496 uint64_t txg
= spa_last_synced_txg(spa
) != 0 ?
1497 spa_last_synced_txg(spa
) : -1ULL;
1499 if ((label
= vdev_label_read_config(vd
, txg
)) == NULL
) {
1500 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1501 VDEV_AUX_BAD_LABEL
);
1506 * Determine if this vdev has been split off into another
1507 * pool. If so, then refuse to open it.
1509 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_SPLIT_GUID
,
1510 &aux_guid
) == 0 && aux_guid
== spa_guid(spa
)) {
1511 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1512 VDEV_AUX_SPLIT_POOL
);
1517 if (strict
&& (nvlist_lookup_uint64(label
,
1518 ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1519 guid
!= spa_guid(spa
))) {
1520 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1521 VDEV_AUX_CORRUPT_DATA
);
1526 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
)
1527 != 0 || nvlist_lookup_uint64(nvl
, ZPOOL_CONFIG_ORIG_GUID
,
1532 * If this vdev just became a top-level vdev because its
1533 * sibling was detached, it will have adopted the parent's
1534 * vdev guid -- but the label may or may not be on disk yet.
1535 * Fortunately, either version of the label will have the
1536 * same top guid, so if we're a top-level vdev, we can
1537 * safely compare to that instead.
1539 * If we split this vdev off instead, then we also check the
1540 * original pool's guid. We don't want to consider the vdev
1541 * corrupt if it is partway through a split operation.
1543 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
,
1545 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_TOP_GUID
,
1547 ((vd
->vdev_guid
!= guid
&& vd
->vdev_guid
!= aux_guid
) &&
1548 (vd
->vdev_guid
!= top_guid
|| vd
!= vd
->vdev_top
))) {
1549 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1550 VDEV_AUX_CORRUPT_DATA
);
1555 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
,
1557 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1558 VDEV_AUX_CORRUPT_DATA
);
1566 * If this is a verbatim import, no need to check the
1567 * state of the pool.
1569 if (!(spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) &&
1570 spa_load_state(spa
) == SPA_LOAD_OPEN
&&
1571 state
!= POOL_STATE_ACTIVE
)
1572 return (SET_ERROR(EBADF
));
1575 * If we were able to open and validate a vdev that was
1576 * previously marked permanently unavailable, clear that state
1579 if (vd
->vdev_not_present
)
1580 vd
->vdev_not_present
= 0;
1587 * Close a virtual device.
1590 vdev_close(vdev_t
*vd
)
1592 vdev_t
*pvd
= vd
->vdev_parent
;
1593 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
1595 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1598 * If our parent is reopening, then we are as well, unless we are
1601 if (pvd
!= NULL
&& pvd
->vdev_reopening
)
1602 vd
->vdev_reopening
= (pvd
->vdev_reopening
&& !vd
->vdev_offline
);
1604 vd
->vdev_ops
->vdev_op_close(vd
);
1606 vdev_cache_purge(vd
);
1609 * We record the previous state before we close it, so that if we are
1610 * doing a reopen(), we don't generate FMA ereports if we notice that
1611 * it's still faulted.
1613 vd
->vdev_prevstate
= vd
->vdev_state
;
1615 if (vd
->vdev_offline
)
1616 vd
->vdev_state
= VDEV_STATE_OFFLINE
;
1618 vd
->vdev_state
= VDEV_STATE_CLOSED
;
1619 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1623 vdev_hold(vdev_t
*vd
)
1625 spa_t
*spa
= vd
->vdev_spa
;
1628 ASSERT(spa_is_root(spa
));
1629 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1632 for (c
= 0; c
< vd
->vdev_children
; c
++)
1633 vdev_hold(vd
->vdev_child
[c
]);
1635 if (vd
->vdev_ops
->vdev_op_leaf
)
1636 vd
->vdev_ops
->vdev_op_hold(vd
);
1640 vdev_rele(vdev_t
*vd
)
1644 ASSERT(spa_is_root(vd
->vdev_spa
));
1645 for (c
= 0; c
< vd
->vdev_children
; c
++)
1646 vdev_rele(vd
->vdev_child
[c
]);
1648 if (vd
->vdev_ops
->vdev_op_leaf
)
1649 vd
->vdev_ops
->vdev_op_rele(vd
);
1653 * Reopen all interior vdevs and any unopened leaves. We don't actually
1654 * reopen leaf vdevs which had previously been opened as they might deadlock
1655 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
1656 * If the leaf has never been opened then open it, as usual.
1659 vdev_reopen(vdev_t
*vd
)
1661 spa_t
*spa
= vd
->vdev_spa
;
1663 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1665 /* set the reopening flag unless we're taking the vdev offline */
1666 vd
->vdev_reopening
= !vd
->vdev_offline
;
1668 (void) vdev_open(vd
);
1671 * Call vdev_validate() here to make sure we have the same device.
1672 * Otherwise, a device with an invalid label could be successfully
1673 * opened in response to vdev_reopen().
1676 (void) vdev_validate_aux(vd
);
1677 if (vdev_readable(vd
) && vdev_writeable(vd
) &&
1678 vd
->vdev_aux
== &spa
->spa_l2cache
&&
1679 !l2arc_vdev_present(vd
))
1680 l2arc_add_vdev(spa
, vd
);
1682 (void) vdev_validate(vd
, B_TRUE
);
1686 * Reassess parent vdev's health.
1688 vdev_propagate_state(vd
);
1692 vdev_create(vdev_t
*vd
, uint64_t txg
, boolean_t isreplacing
)
1697 * Normally, partial opens (e.g. of a mirror) are allowed.
1698 * For a create, however, we want to fail the request if
1699 * there are any components we can't open.
1701 error
= vdev_open(vd
);
1703 if (error
|| vd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
1705 return (error
? error
: ENXIO
);
1709 * Recursively load DTLs and initialize all labels.
1711 if ((error
= vdev_dtl_load(vd
)) != 0 ||
1712 (error
= vdev_label_init(vd
, txg
, isreplacing
?
1713 VDEV_LABEL_REPLACE
: VDEV_LABEL_CREATE
)) != 0) {
1722 vdev_metaslab_set_size(vdev_t
*vd
)
1725 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
1727 vd
->vdev_ms_shift
= highbit64(vd
->vdev_asize
/ metaslabs_per_vdev
);
1728 vd
->vdev_ms_shift
= MAX(vd
->vdev_ms_shift
, SPA_MAXBLOCKSHIFT
);
1732 vdev_dirty(vdev_t
*vd
, int flags
, void *arg
, uint64_t txg
)
1734 ASSERT(vd
== vd
->vdev_top
);
1735 ASSERT(!vd
->vdev_ishole
);
1736 ASSERT(ISP2(flags
));
1737 ASSERT(spa_writeable(vd
->vdev_spa
));
1739 if (flags
& VDD_METASLAB
)
1740 (void) txg_list_add(&vd
->vdev_ms_list
, arg
, txg
);
1742 if (flags
& VDD_DTL
)
1743 (void) txg_list_add(&vd
->vdev_dtl_list
, arg
, txg
);
1745 (void) txg_list_add(&vd
->vdev_spa
->spa_vdev_txg_list
, vd
, txg
);
1749 vdev_dirty_leaves(vdev_t
*vd
, int flags
, uint64_t txg
)
1753 for (c
= 0; c
< vd
->vdev_children
; c
++)
1754 vdev_dirty_leaves(vd
->vdev_child
[c
], flags
, txg
);
1756 if (vd
->vdev_ops
->vdev_op_leaf
)
1757 vdev_dirty(vd
->vdev_top
, flags
, vd
, txg
);
1763 * A vdev's DTL (dirty time log) is the set of transaction groups for which
1764 * the vdev has less than perfect replication. There are four kinds of DTL:
1766 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
1768 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
1770 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
1771 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
1772 * txgs that was scrubbed.
1774 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
1775 * persistent errors or just some device being offline.
1776 * Unlike the other three, the DTL_OUTAGE map is not generally
1777 * maintained; it's only computed when needed, typically to
1778 * determine whether a device can be detached.
1780 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
1781 * either has the data or it doesn't.
1783 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
1784 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
1785 * if any child is less than fully replicated, then so is its parent.
1786 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
1787 * comprising only those txgs which appear in 'maxfaults' or more children;
1788 * those are the txgs we don't have enough replication to read. For example,
1789 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
1790 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
1791 * two child DTL_MISSING maps.
1793 * It should be clear from the above that to compute the DTLs and outage maps
1794 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
1795 * Therefore, that is all we keep on disk. When loading the pool, or after
1796 * a configuration change, we generate all other DTLs from first principles.
1799 vdev_dtl_dirty(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
1801 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
1803 ASSERT(t
< DTL_TYPES
);
1804 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
1805 ASSERT(spa_writeable(vd
->vdev_spa
));
1807 mutex_enter(rt
->rt_lock
);
1808 if (!range_tree_contains(rt
, txg
, size
))
1809 range_tree_add(rt
, txg
, size
);
1810 mutex_exit(rt
->rt_lock
);
1814 vdev_dtl_contains(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
1816 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
1817 boolean_t dirty
= B_FALSE
;
1819 ASSERT(t
< DTL_TYPES
);
1820 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
1822 mutex_enter(rt
->rt_lock
);
1823 if (range_tree_space(rt
) != 0)
1824 dirty
= range_tree_contains(rt
, txg
, size
);
1825 mutex_exit(rt
->rt_lock
);
1831 vdev_dtl_empty(vdev_t
*vd
, vdev_dtl_type_t t
)
1833 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
1836 mutex_enter(rt
->rt_lock
);
1837 empty
= (range_tree_space(rt
) == 0);
1838 mutex_exit(rt
->rt_lock
);
1844 * Returns B_TRUE if vdev determines offset needs to be resilvered.
1847 vdev_dtl_need_resilver(vdev_t
*vd
, uint64_t offset
, size_t psize
)
1849 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
1851 if (vd
->vdev_ops
->vdev_op_need_resilver
== NULL
||
1852 vd
->vdev_ops
->vdev_op_leaf
)
1855 return (vd
->vdev_ops
->vdev_op_need_resilver(vd
, offset
, psize
));
1859 * Returns the lowest txg in the DTL range.
1862 vdev_dtl_min(vdev_t
*vd
)
1866 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
1867 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
1868 ASSERT0(vd
->vdev_children
);
1870 rs
= avl_first(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
1871 return (rs
->rs_start
- 1);
1875 * Returns the highest txg in the DTL.
1878 vdev_dtl_max(vdev_t
*vd
)
1882 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
1883 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
1884 ASSERT0(vd
->vdev_children
);
1886 rs
= avl_last(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
1887 return (rs
->rs_end
);
1891 * Determine if a resilvering vdev should remove any DTL entries from
1892 * its range. If the vdev was resilvering for the entire duration of the
1893 * scan then it should excise that range from its DTLs. Otherwise, this
1894 * vdev is considered partially resilvered and should leave its DTL
1895 * entries intact. The comment in vdev_dtl_reassess() describes how we
1899 vdev_dtl_should_excise(vdev_t
*vd
)
1901 spa_t
*spa
= vd
->vdev_spa
;
1902 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
1904 ASSERT0(scn
->scn_phys
.scn_errors
);
1905 ASSERT0(vd
->vdev_children
);
1907 if (vd
->vdev_state
< VDEV_STATE_DEGRADED
)
1910 if (vd
->vdev_resilver_txg
== 0 ||
1911 range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) == 0)
1915 * When a resilver is initiated the scan will assign the scn_max_txg
1916 * value to the highest txg value that exists in all DTLs. If this
1917 * device's max DTL is not part of this scan (i.e. it is not in
1918 * the range (scn_min_txg, scn_max_txg] then it is not eligible
1921 if (vdev_dtl_max(vd
) <= scn
->scn_phys
.scn_max_txg
) {
1922 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <=, vdev_dtl_min(vd
));
1923 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <, vd
->vdev_resilver_txg
);
1924 ASSERT3U(vd
->vdev_resilver_txg
, <=, scn
->scn_phys
.scn_max_txg
);
1931 * Reassess DTLs after a config change or scrub completion.
1934 vdev_dtl_reassess(vdev_t
*vd
, uint64_t txg
, uint64_t scrub_txg
, int scrub_done
)
1936 spa_t
*spa
= vd
->vdev_spa
;
1940 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1942 for (c
= 0; c
< vd
->vdev_children
; c
++)
1943 vdev_dtl_reassess(vd
->vdev_child
[c
], txg
,
1944 scrub_txg
, scrub_done
);
1946 if (vd
== spa
->spa_root_vdev
|| vd
->vdev_ishole
|| vd
->vdev_aux
)
1949 if (vd
->vdev_ops
->vdev_op_leaf
) {
1950 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
1952 mutex_enter(&vd
->vdev_dtl_lock
);
1955 * If we've completed a scan cleanly then determine
1956 * if this vdev should remove any DTLs. We only want to
1957 * excise regions on vdevs that were available during
1958 * the entire duration of this scan.
1960 if (scrub_txg
!= 0 &&
1961 (spa
->spa_scrub_started
||
1962 (scn
!= NULL
&& scn
->scn_phys
.scn_errors
== 0)) &&
1963 vdev_dtl_should_excise(vd
)) {
1965 * We completed a scrub up to scrub_txg. If we
1966 * did it without rebooting, then the scrub dtl
1967 * will be valid, so excise the old region and
1968 * fold in the scrub dtl. Otherwise, leave the
1969 * dtl as-is if there was an error.
1971 * There's little trick here: to excise the beginning
1972 * of the DTL_MISSING map, we put it into a reference
1973 * tree and then add a segment with refcnt -1 that
1974 * covers the range [0, scrub_txg). This means
1975 * that each txg in that range has refcnt -1 or 0.
1976 * We then add DTL_SCRUB with a refcnt of 2, so that
1977 * entries in the range [0, scrub_txg) will have a
1978 * positive refcnt -- either 1 or 2. We then convert
1979 * the reference tree into the new DTL_MISSING map.
1981 space_reftree_create(&reftree
);
1982 space_reftree_add_map(&reftree
,
1983 vd
->vdev_dtl
[DTL_MISSING
], 1);
1984 space_reftree_add_seg(&reftree
, 0, scrub_txg
, -1);
1985 space_reftree_add_map(&reftree
,
1986 vd
->vdev_dtl
[DTL_SCRUB
], 2);
1987 space_reftree_generate_map(&reftree
,
1988 vd
->vdev_dtl
[DTL_MISSING
], 1);
1989 space_reftree_destroy(&reftree
);
1991 range_tree_vacate(vd
->vdev_dtl
[DTL_PARTIAL
], NULL
, NULL
);
1992 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
1993 range_tree_add
, vd
->vdev_dtl
[DTL_PARTIAL
]);
1995 range_tree_vacate(vd
->vdev_dtl
[DTL_SCRUB
], NULL
, NULL
);
1996 range_tree_vacate(vd
->vdev_dtl
[DTL_OUTAGE
], NULL
, NULL
);
1997 if (!vdev_readable(vd
))
1998 range_tree_add(vd
->vdev_dtl
[DTL_OUTAGE
], 0, -1ULL);
2000 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2001 range_tree_add
, vd
->vdev_dtl
[DTL_OUTAGE
]);
2004 * If the vdev was resilvering and no longer has any
2005 * DTLs then reset its resilvering flag and dirty
2006 * the top level so that we persist the change.
2008 if (vd
->vdev_resilver_txg
!= 0 &&
2009 range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) == 0 &&
2010 range_tree_space(vd
->vdev_dtl
[DTL_OUTAGE
]) == 0) {
2011 vd
->vdev_resilver_txg
= 0;
2012 vdev_config_dirty(vd
->vdev_top
);
2015 mutex_exit(&vd
->vdev_dtl_lock
);
2018 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, txg
);
2022 mutex_enter(&vd
->vdev_dtl_lock
);
2023 for (t
= 0; t
< DTL_TYPES
; t
++) {
2026 /* account for child's outage in parent's missing map */
2027 int s
= (t
== DTL_MISSING
) ? DTL_OUTAGE
: t
;
2029 continue; /* leaf vdevs only */
2030 if (t
== DTL_PARTIAL
)
2031 minref
= 1; /* i.e. non-zero */
2032 else if (vd
->vdev_nparity
!= 0)
2033 minref
= vd
->vdev_nparity
+ 1; /* RAID-Z */
2035 minref
= vd
->vdev_children
; /* any kind of mirror */
2036 space_reftree_create(&reftree
);
2037 for (c
= 0; c
< vd
->vdev_children
; c
++) {
2038 vdev_t
*cvd
= vd
->vdev_child
[c
];
2039 mutex_enter(&cvd
->vdev_dtl_lock
);
2040 space_reftree_add_map(&reftree
, cvd
->vdev_dtl
[s
], 1);
2041 mutex_exit(&cvd
->vdev_dtl_lock
);
2043 space_reftree_generate_map(&reftree
, vd
->vdev_dtl
[t
], minref
);
2044 space_reftree_destroy(&reftree
);
2046 mutex_exit(&vd
->vdev_dtl_lock
);
2050 vdev_dtl_load(vdev_t
*vd
)
2052 spa_t
*spa
= vd
->vdev_spa
;
2053 objset_t
*mos
= spa
->spa_meta_objset
;
2057 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_dtl_object
!= 0) {
2058 ASSERT(!vd
->vdev_ishole
);
2060 error
= space_map_open(&vd
->vdev_dtl_sm
, mos
,
2061 vd
->vdev_dtl_object
, 0, -1ULL, 0, &vd
->vdev_dtl_lock
);
2064 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2066 mutex_enter(&vd
->vdev_dtl_lock
);
2069 * Now that we've opened the space_map we need to update
2072 space_map_update(vd
->vdev_dtl_sm
);
2074 error
= space_map_load(vd
->vdev_dtl_sm
,
2075 vd
->vdev_dtl
[DTL_MISSING
], SM_ALLOC
);
2076 mutex_exit(&vd
->vdev_dtl_lock
);
2081 for (c
= 0; c
< vd
->vdev_children
; c
++) {
2082 error
= vdev_dtl_load(vd
->vdev_child
[c
]);
2091 vdev_destroy_unlink_zap(vdev_t
*vd
, uint64_t zapobj
, dmu_tx_t
*tx
)
2093 spa_t
*spa
= vd
->vdev_spa
;
2095 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zapobj
, tx
));
2096 VERIFY0(zap_remove_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2101 vdev_create_link_zap(vdev_t
*vd
, dmu_tx_t
*tx
)
2103 spa_t
*spa
= vd
->vdev_spa
;
2104 uint64_t zap
= zap_create(spa
->spa_meta_objset
, DMU_OTN_ZAP_METADATA
,
2105 DMU_OT_NONE
, 0, tx
);
2108 VERIFY0(zap_add_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2115 vdev_construct_zaps(vdev_t
*vd
, dmu_tx_t
*tx
)
2119 if (vd
->vdev_ops
!= &vdev_hole_ops
&&
2120 vd
->vdev_ops
!= &vdev_missing_ops
&&
2121 vd
->vdev_ops
!= &vdev_root_ops
&&
2122 !vd
->vdev_top
->vdev_removing
) {
2123 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_leaf_zap
== 0) {
2124 vd
->vdev_leaf_zap
= vdev_create_link_zap(vd
, tx
);
2126 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
== 0) {
2127 vd
->vdev_top_zap
= vdev_create_link_zap(vd
, tx
);
2130 for (i
= 0; i
< vd
->vdev_children
; i
++) {
2131 vdev_construct_zaps(vd
->vdev_child
[i
], tx
);
2136 vdev_dtl_sync(vdev_t
*vd
, uint64_t txg
)
2138 spa_t
*spa
= vd
->vdev_spa
;
2139 range_tree_t
*rt
= vd
->vdev_dtl
[DTL_MISSING
];
2140 objset_t
*mos
= spa
->spa_meta_objset
;
2141 range_tree_t
*rtsync
;
2144 uint64_t object
= space_map_object(vd
->vdev_dtl_sm
);
2146 ASSERT(!vd
->vdev_ishole
);
2147 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2149 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2151 if (vd
->vdev_detached
|| vd
->vdev_top
->vdev_removing
) {
2152 mutex_enter(&vd
->vdev_dtl_lock
);
2153 space_map_free(vd
->vdev_dtl_sm
, tx
);
2154 space_map_close(vd
->vdev_dtl_sm
);
2155 vd
->vdev_dtl_sm
= NULL
;
2156 mutex_exit(&vd
->vdev_dtl_lock
);
2159 * We only destroy the leaf ZAP for detached leaves or for
2160 * removed log devices. Removed data devices handle leaf ZAP
2161 * cleanup later, once cancellation is no longer possible.
2163 if (vd
->vdev_leaf_zap
!= 0 && (vd
->vdev_detached
||
2164 vd
->vdev_top
->vdev_islog
)) {
2165 vdev_destroy_unlink_zap(vd
, vd
->vdev_leaf_zap
, tx
);
2166 vd
->vdev_leaf_zap
= 0;
2173 if (vd
->vdev_dtl_sm
== NULL
) {
2174 uint64_t new_object
;
2176 new_object
= space_map_alloc(mos
, tx
);
2177 VERIFY3U(new_object
, !=, 0);
2179 VERIFY0(space_map_open(&vd
->vdev_dtl_sm
, mos
, new_object
,
2180 0, -1ULL, 0, &vd
->vdev_dtl_lock
));
2181 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2184 mutex_init(&rtlock
, NULL
, MUTEX_DEFAULT
, NULL
);
2186 rtsync
= range_tree_create(NULL
, NULL
, &rtlock
);
2188 mutex_enter(&rtlock
);
2190 mutex_enter(&vd
->vdev_dtl_lock
);
2191 range_tree_walk(rt
, range_tree_add
, rtsync
);
2192 mutex_exit(&vd
->vdev_dtl_lock
);
2194 space_map_truncate(vd
->vdev_dtl_sm
, tx
);
2195 space_map_write(vd
->vdev_dtl_sm
, rtsync
, SM_ALLOC
, tx
);
2196 range_tree_vacate(rtsync
, NULL
, NULL
);
2198 range_tree_destroy(rtsync
);
2200 mutex_exit(&rtlock
);
2201 mutex_destroy(&rtlock
);
2204 * If the object for the space map has changed then dirty
2205 * the top level so that we update the config.
2207 if (object
!= space_map_object(vd
->vdev_dtl_sm
)) {
2208 zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, "
2209 "new object %llu", txg
, spa_name(spa
), object
,
2210 space_map_object(vd
->vdev_dtl_sm
));
2211 vdev_config_dirty(vd
->vdev_top
);
2216 mutex_enter(&vd
->vdev_dtl_lock
);
2217 space_map_update(vd
->vdev_dtl_sm
);
2218 mutex_exit(&vd
->vdev_dtl_lock
);
2222 * Determine whether the specified vdev can be offlined/detached/removed
2223 * without losing data.
2226 vdev_dtl_required(vdev_t
*vd
)
2228 spa_t
*spa
= vd
->vdev_spa
;
2229 vdev_t
*tvd
= vd
->vdev_top
;
2230 uint8_t cant_read
= vd
->vdev_cant_read
;
2233 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2235 if (vd
== spa
->spa_root_vdev
|| vd
== tvd
)
2239 * Temporarily mark the device as unreadable, and then determine
2240 * whether this results in any DTL outages in the top-level vdev.
2241 * If not, we can safely offline/detach/remove the device.
2243 vd
->vdev_cant_read
= B_TRUE
;
2244 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2245 required
= !vdev_dtl_empty(tvd
, DTL_OUTAGE
);
2246 vd
->vdev_cant_read
= cant_read
;
2247 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2249 if (!required
&& zio_injection_enabled
)
2250 required
= !!zio_handle_device_injection(vd
, NULL
, ECHILD
);
2256 * Determine if resilver is needed, and if so the txg range.
2259 vdev_resilver_needed(vdev_t
*vd
, uint64_t *minp
, uint64_t *maxp
)
2261 boolean_t needed
= B_FALSE
;
2262 uint64_t thismin
= UINT64_MAX
;
2263 uint64_t thismax
= 0;
2266 if (vd
->vdev_children
== 0) {
2267 mutex_enter(&vd
->vdev_dtl_lock
);
2268 if (range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) != 0 &&
2269 vdev_writeable(vd
)) {
2271 thismin
= vdev_dtl_min(vd
);
2272 thismax
= vdev_dtl_max(vd
);
2275 mutex_exit(&vd
->vdev_dtl_lock
);
2277 for (c
= 0; c
< vd
->vdev_children
; c
++) {
2278 vdev_t
*cvd
= vd
->vdev_child
[c
];
2279 uint64_t cmin
, cmax
;
2281 if (vdev_resilver_needed(cvd
, &cmin
, &cmax
)) {
2282 thismin
= MIN(thismin
, cmin
);
2283 thismax
= MAX(thismax
, cmax
);
2289 if (needed
&& minp
) {
2297 vdev_load(vdev_t
*vd
)
2302 * Recursively load all children.
2304 for (c
= 0; c
< vd
->vdev_children
; c
++)
2305 vdev_load(vd
->vdev_child
[c
]);
2308 * If this is a top-level vdev, initialize its metaslabs.
2310 if (vd
== vd
->vdev_top
&& !vd
->vdev_ishole
&&
2311 (vd
->vdev_ashift
== 0 || vd
->vdev_asize
== 0 ||
2312 vdev_metaslab_init(vd
, 0) != 0))
2313 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2314 VDEV_AUX_CORRUPT_DATA
);
2316 * If this is a leaf vdev, load its DTL.
2318 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_dtl_load(vd
) != 0)
2319 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2320 VDEV_AUX_CORRUPT_DATA
);
2324 * The special vdev case is used for hot spares and l2cache devices. Its
2325 * sole purpose it to set the vdev state for the associated vdev. To do this,
2326 * we make sure that we can open the underlying device, then try to read the
2327 * label, and make sure that the label is sane and that it hasn't been
2328 * repurposed to another pool.
2331 vdev_validate_aux(vdev_t
*vd
)
2334 uint64_t guid
, version
;
2337 if (!vdev_readable(vd
))
2340 if ((label
= vdev_label_read_config(vd
, -1ULL)) == NULL
) {
2341 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2342 VDEV_AUX_CORRUPT_DATA
);
2346 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_VERSION
, &version
) != 0 ||
2347 !SPA_VERSION_IS_SUPPORTED(version
) ||
2348 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0 ||
2349 guid
!= vd
->vdev_guid
||
2350 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
, &state
) != 0) {
2351 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2352 VDEV_AUX_CORRUPT_DATA
);
2358 * We don't actually check the pool state here. If it's in fact in
2359 * use by another pool, we update this fact on the fly when requested.
2366 vdev_remove(vdev_t
*vd
, uint64_t txg
)
2368 spa_t
*spa
= vd
->vdev_spa
;
2369 objset_t
*mos
= spa
->spa_meta_objset
;
2373 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
2374 ASSERT(vd
== vd
->vdev_top
);
2375 ASSERT3U(txg
, ==, spa_syncing_txg(spa
));
2377 if (vd
->vdev_ms
!= NULL
) {
2378 metaslab_group_t
*mg
= vd
->vdev_mg
;
2380 metaslab_group_histogram_verify(mg
);
2381 metaslab_class_histogram_verify(mg
->mg_class
);
2383 for (m
= 0; m
< vd
->vdev_ms_count
; m
++) {
2384 metaslab_t
*msp
= vd
->vdev_ms
[m
];
2386 if (msp
== NULL
|| msp
->ms_sm
== NULL
)
2389 mutex_enter(&msp
->ms_lock
);
2391 * If the metaslab was not loaded when the vdev
2392 * was removed then the histogram accounting may
2393 * not be accurate. Update the histogram information
2394 * here so that we ensure that the metaslab group
2395 * and metaslab class are up-to-date.
2397 metaslab_group_histogram_remove(mg
, msp
);
2399 VERIFY0(space_map_allocated(msp
->ms_sm
));
2400 space_map_free(msp
->ms_sm
, tx
);
2401 space_map_close(msp
->ms_sm
);
2403 mutex_exit(&msp
->ms_lock
);
2406 metaslab_group_histogram_verify(mg
);
2407 metaslab_class_histogram_verify(mg
->mg_class
);
2408 for (i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++)
2409 ASSERT0(mg
->mg_histogram
[i
]);
2413 if (vd
->vdev_ms_array
) {
2414 (void) dmu_object_free(mos
, vd
->vdev_ms_array
, tx
);
2415 vd
->vdev_ms_array
= 0;
2418 if (vd
->vdev_islog
&& vd
->vdev_top_zap
!= 0) {
2419 vdev_destroy_unlink_zap(vd
, vd
->vdev_top_zap
, tx
);
2420 vd
->vdev_top_zap
= 0;
2426 vdev_sync_done(vdev_t
*vd
, uint64_t txg
)
2429 boolean_t reassess
= !txg_list_empty(&vd
->vdev_ms_list
, TXG_CLEAN(txg
));
2431 ASSERT(!vd
->vdev_ishole
);
2433 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, TXG_CLEAN(txg
))))
2434 metaslab_sync_done(msp
, txg
);
2437 metaslab_sync_reassess(vd
->vdev_mg
);
2441 vdev_sync(vdev_t
*vd
, uint64_t txg
)
2443 spa_t
*spa
= vd
->vdev_spa
;
2448 ASSERT(!vd
->vdev_ishole
);
2450 if (vd
->vdev_ms_array
== 0 && vd
->vdev_ms_shift
!= 0) {
2451 ASSERT(vd
== vd
->vdev_top
);
2452 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2453 vd
->vdev_ms_array
= dmu_object_alloc(spa
->spa_meta_objset
,
2454 DMU_OT_OBJECT_ARRAY
, 0, DMU_OT_NONE
, 0, tx
);
2455 ASSERT(vd
->vdev_ms_array
!= 0);
2456 vdev_config_dirty(vd
);
2461 * Remove the metadata associated with this vdev once it's empty.
2463 if (vd
->vdev_stat
.vs_alloc
== 0 && vd
->vdev_removing
)
2464 vdev_remove(vd
, txg
);
2466 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, txg
)) != NULL
) {
2467 metaslab_sync(msp
, txg
);
2468 (void) txg_list_add(&vd
->vdev_ms_list
, msp
, TXG_CLEAN(txg
));
2471 while ((lvd
= txg_list_remove(&vd
->vdev_dtl_list
, txg
)) != NULL
)
2472 vdev_dtl_sync(lvd
, txg
);
2474 (void) txg_list_add(&spa
->spa_vdev_txg_list
, vd
, TXG_CLEAN(txg
));
2478 vdev_psize_to_asize(vdev_t
*vd
, uint64_t psize
)
2480 return (vd
->vdev_ops
->vdev_op_asize(vd
, psize
));
2484 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
2485 * not be opened, and no I/O is attempted.
2488 vdev_fault(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
2492 spa_vdev_state_enter(spa
, SCL_NONE
);
2494 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2495 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2497 if (!vd
->vdev_ops
->vdev_op_leaf
)
2498 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2503 * If user did a 'zpool offline -f' then make the fault persist across
2506 if (aux
== VDEV_AUX_EXTERNAL_PERSIST
) {
2508 * There are two kinds of forced faults: temporary and
2509 * persistent. Temporary faults go away at pool import, while
2510 * persistent faults stay set. Both types of faults can be
2511 * cleared with a zpool clear.
2513 * We tell if a vdev is persistently faulted by looking at the
2514 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
2515 * import then it's a persistent fault. Otherwise, it's
2516 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
2517 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
2518 * tells vdev_config_generate() (which gets run later) to set
2519 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
2521 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
2522 vd
->vdev_tmpoffline
= B_FALSE
;
2523 aux
= VDEV_AUX_EXTERNAL
;
2525 vd
->vdev_tmpoffline
= B_TRUE
;
2529 * We don't directly use the aux state here, but if we do a
2530 * vdev_reopen(), we need this value to be present to remember why we
2533 vd
->vdev_label_aux
= aux
;
2536 * Faulted state takes precedence over degraded.
2538 vd
->vdev_delayed_close
= B_FALSE
;
2539 vd
->vdev_faulted
= 1ULL;
2540 vd
->vdev_degraded
= 0ULL;
2541 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_FAULTED
, aux
);
2544 * If this device has the only valid copy of the data, then
2545 * back off and simply mark the vdev as degraded instead.
2547 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&& vdev_dtl_required(vd
)) {
2548 vd
->vdev_degraded
= 1ULL;
2549 vd
->vdev_faulted
= 0ULL;
2552 * If we reopen the device and it's not dead, only then do we
2557 if (vdev_readable(vd
))
2558 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
, aux
);
2561 return (spa_vdev_state_exit(spa
, vd
, 0));
2565 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
2566 * user that something is wrong. The vdev continues to operate as normal as far
2567 * as I/O is concerned.
2570 vdev_degrade(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
2574 spa_vdev_state_enter(spa
, SCL_NONE
);
2576 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2577 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2579 if (!vd
->vdev_ops
->vdev_op_leaf
)
2580 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2583 * If the vdev is already faulted, then don't do anything.
2585 if (vd
->vdev_faulted
|| vd
->vdev_degraded
)
2586 return (spa_vdev_state_exit(spa
, NULL
, 0));
2588 vd
->vdev_degraded
= 1ULL;
2589 if (!vdev_is_dead(vd
))
2590 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
,
2593 return (spa_vdev_state_exit(spa
, vd
, 0));
2597 * Online the given vdev.
2599 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
2600 * spare device should be detached when the device finishes resilvering.
2601 * Second, the online should be treated like a 'test' online case, so no FMA
2602 * events are generated if the device fails to open.
2605 vdev_online(spa_t
*spa
, uint64_t guid
, uint64_t flags
, vdev_state_t
*newstate
)
2607 vdev_t
*vd
, *tvd
, *pvd
, *rvd
= spa
->spa_root_vdev
;
2608 boolean_t wasoffline
;
2609 vdev_state_t oldstate
;
2611 spa_vdev_state_enter(spa
, SCL_NONE
);
2613 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2614 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2616 if (!vd
->vdev_ops
->vdev_op_leaf
)
2617 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2619 wasoffline
= (vd
->vdev_offline
|| vd
->vdev_tmpoffline
);
2620 oldstate
= vd
->vdev_state
;
2623 vd
->vdev_offline
= B_FALSE
;
2624 vd
->vdev_tmpoffline
= B_FALSE
;
2625 vd
->vdev_checkremove
= !!(flags
& ZFS_ONLINE_CHECKREMOVE
);
2626 vd
->vdev_forcefault
= !!(flags
& ZFS_ONLINE_FORCEFAULT
);
2628 /* XXX - L2ARC 1.0 does not support expansion */
2629 if (!vd
->vdev_aux
) {
2630 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
2631 pvd
->vdev_expanding
= !!(flags
& ZFS_ONLINE_EXPAND
);
2635 vd
->vdev_checkremove
= vd
->vdev_forcefault
= B_FALSE
;
2637 if (!vd
->vdev_aux
) {
2638 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
2639 pvd
->vdev_expanding
= B_FALSE
;
2643 *newstate
= vd
->vdev_state
;
2644 if ((flags
& ZFS_ONLINE_UNSPARE
) &&
2645 !vdev_is_dead(vd
) && vd
->vdev_parent
&&
2646 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
2647 vd
->vdev_parent
->vdev_child
[0] == vd
)
2648 vd
->vdev_unspare
= B_TRUE
;
2650 if ((flags
& ZFS_ONLINE_EXPAND
) || spa
->spa_autoexpand
) {
2652 /* XXX - L2ARC 1.0 does not support expansion */
2654 return (spa_vdev_state_exit(spa
, vd
, ENOTSUP
));
2655 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2659 (oldstate
< VDEV_STATE_DEGRADED
&&
2660 vd
->vdev_state
>= VDEV_STATE_DEGRADED
))
2661 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_ONLINE
);
2663 return (spa_vdev_state_exit(spa
, vd
, 0));
2667 vdev_offline_locked(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
2671 uint64_t generation
;
2672 metaslab_group_t
*mg
;
2675 spa_vdev_state_enter(spa
, SCL_ALLOC
);
2677 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2678 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2680 if (!vd
->vdev_ops
->vdev_op_leaf
)
2681 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2685 generation
= spa
->spa_config_generation
+ 1;
2688 * If the device isn't already offline, try to offline it.
2690 if (!vd
->vdev_offline
) {
2692 * If this device has the only valid copy of some data,
2693 * don't allow it to be offlined. Log devices are always
2696 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
2697 vdev_dtl_required(vd
))
2698 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
2701 * If the top-level is a slog and it has had allocations
2702 * then proceed. We check that the vdev's metaslab group
2703 * is not NULL since it's possible that we may have just
2704 * added this vdev but not yet initialized its metaslabs.
2706 if (tvd
->vdev_islog
&& mg
!= NULL
) {
2708 * Prevent any future allocations.
2710 metaslab_group_passivate(mg
);
2711 (void) spa_vdev_state_exit(spa
, vd
, 0);
2713 error
= spa_offline_log(spa
);
2715 spa_vdev_state_enter(spa
, SCL_ALLOC
);
2718 * Check to see if the config has changed.
2720 if (error
|| generation
!= spa
->spa_config_generation
) {
2721 metaslab_group_activate(mg
);
2723 return (spa_vdev_state_exit(spa
,
2725 (void) spa_vdev_state_exit(spa
, vd
, 0);
2728 ASSERT0(tvd
->vdev_stat
.vs_alloc
);
2732 * Offline this device and reopen its top-level vdev.
2733 * If the top-level vdev is a log device then just offline
2734 * it. Otherwise, if this action results in the top-level
2735 * vdev becoming unusable, undo it and fail the request.
2737 vd
->vdev_offline
= B_TRUE
;
2740 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
2741 vdev_is_dead(tvd
)) {
2742 vd
->vdev_offline
= B_FALSE
;
2744 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
2748 * Add the device back into the metaslab rotor so that
2749 * once we online the device it's open for business.
2751 if (tvd
->vdev_islog
&& mg
!= NULL
)
2752 metaslab_group_activate(mg
);
2755 vd
->vdev_tmpoffline
= !!(flags
& ZFS_OFFLINE_TEMPORARY
);
2757 return (spa_vdev_state_exit(spa
, vd
, 0));
2761 vdev_offline(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
2765 mutex_enter(&spa
->spa_vdev_top_lock
);
2766 error
= vdev_offline_locked(spa
, guid
, flags
);
2767 mutex_exit(&spa
->spa_vdev_top_lock
);
2773 * Clear the error counts associated with this vdev. Unlike vdev_online() and
2774 * vdev_offline(), we assume the spa config is locked. We also clear all
2775 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
2778 vdev_clear(spa_t
*spa
, vdev_t
*vd
)
2780 vdev_t
*rvd
= spa
->spa_root_vdev
;
2783 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2788 vd
->vdev_stat
.vs_read_errors
= 0;
2789 vd
->vdev_stat
.vs_write_errors
= 0;
2790 vd
->vdev_stat
.vs_checksum_errors
= 0;
2792 for (c
= 0; c
< vd
->vdev_children
; c
++)
2793 vdev_clear(spa
, vd
->vdev_child
[c
]);
2796 * If we're in the FAULTED state or have experienced failed I/O, then
2797 * clear the persistent state and attempt to reopen the device. We
2798 * also mark the vdev config dirty, so that the new faulted state is
2799 * written out to disk.
2801 if (vd
->vdev_faulted
|| vd
->vdev_degraded
||
2802 !vdev_readable(vd
) || !vdev_writeable(vd
)) {
2804 * When reopening in response to a clear event, it may be due to
2805 * a fmadm repair request. In this case, if the device is
2806 * still broken, we want to still post the ereport again.
2808 vd
->vdev_forcefault
= B_TRUE
;
2810 vd
->vdev_faulted
= vd
->vdev_degraded
= 0ULL;
2811 vd
->vdev_cant_read
= B_FALSE
;
2812 vd
->vdev_cant_write
= B_FALSE
;
2813 vd
->vdev_stat
.vs_aux
= 0;
2815 vdev_reopen(vd
== rvd
? rvd
: vd
->vdev_top
);
2817 vd
->vdev_forcefault
= B_FALSE
;
2819 if (vd
!= rvd
&& vdev_writeable(vd
->vdev_top
))
2820 vdev_state_dirty(vd
->vdev_top
);
2822 if (vd
->vdev_aux
== NULL
&& !vdev_is_dead(vd
))
2823 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2825 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_CLEAR
);
2829 * When clearing a FMA-diagnosed fault, we always want to
2830 * unspare the device, as we assume that the original spare was
2831 * done in response to the FMA fault.
2833 if (!vdev_is_dead(vd
) && vd
->vdev_parent
!= NULL
&&
2834 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
2835 vd
->vdev_parent
->vdev_child
[0] == vd
)
2836 vd
->vdev_unspare
= B_TRUE
;
2840 vdev_is_dead(vdev_t
*vd
)
2843 * Holes and missing devices are always considered "dead".
2844 * This simplifies the code since we don't have to check for
2845 * these types of devices in the various code paths.
2846 * Instead we rely on the fact that we skip over dead devices
2847 * before issuing I/O to them.
2849 return (vd
->vdev_state
< VDEV_STATE_DEGRADED
|| vd
->vdev_ishole
||
2850 vd
->vdev_ops
== &vdev_missing_ops
);
2854 vdev_readable(vdev_t
*vd
)
2856 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_read
);
2860 vdev_writeable(vdev_t
*vd
)
2862 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_write
);
2866 vdev_allocatable(vdev_t
*vd
)
2868 uint64_t state
= vd
->vdev_state
;
2871 * We currently allow allocations from vdevs which may be in the
2872 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
2873 * fails to reopen then we'll catch it later when we're holding
2874 * the proper locks. Note that we have to get the vdev state
2875 * in a local variable because although it changes atomically,
2876 * we're asking two separate questions about it.
2878 return (!(state
< VDEV_STATE_DEGRADED
&& state
!= VDEV_STATE_CLOSED
) &&
2879 !vd
->vdev_cant_write
&& !vd
->vdev_ishole
&&
2880 vd
->vdev_mg
->mg_initialized
);
2884 vdev_accessible(vdev_t
*vd
, zio_t
*zio
)
2886 ASSERT(zio
->io_vd
== vd
);
2888 if (vdev_is_dead(vd
) || vd
->vdev_remove_wanted
)
2891 if (zio
->io_type
== ZIO_TYPE_READ
)
2892 return (!vd
->vdev_cant_read
);
2894 if (zio
->io_type
== ZIO_TYPE_WRITE
)
2895 return (!vd
->vdev_cant_write
);
2901 vdev_get_child_stat(vdev_t
*cvd
, vdev_stat_t
*vs
, vdev_stat_t
*cvs
)
2904 for (t
= 0; t
< ZIO_TYPES
; t
++) {
2905 vs
->vs_ops
[t
] += cvs
->vs_ops
[t
];
2906 vs
->vs_bytes
[t
] += cvs
->vs_bytes
[t
];
2909 cvs
->vs_scan_removing
= cvd
->vdev_removing
;
2913 * Get extended stats
2916 vdev_get_child_stat_ex(vdev_t
*cvd
, vdev_stat_ex_t
*vsx
, vdev_stat_ex_t
*cvsx
)
2919 for (t
= 0; t
< ZIO_TYPES
; t
++) {
2920 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_disk_histo
[0]); b
++)
2921 vsx
->vsx_disk_histo
[t
][b
] += cvsx
->vsx_disk_histo
[t
][b
];
2923 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_total_histo
[0]); b
++) {
2924 vsx
->vsx_total_histo
[t
][b
] +=
2925 cvsx
->vsx_total_histo
[t
][b
];
2929 for (t
= 0; t
< ZIO_PRIORITY_NUM_QUEUEABLE
; t
++) {
2930 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_queue_histo
[0]); b
++) {
2931 vsx
->vsx_queue_histo
[t
][b
] +=
2932 cvsx
->vsx_queue_histo
[t
][b
];
2934 vsx
->vsx_active_queue
[t
] += cvsx
->vsx_active_queue
[t
];
2935 vsx
->vsx_pend_queue
[t
] += cvsx
->vsx_pend_queue
[t
];
2937 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_ind_histo
[0]); b
++)
2938 vsx
->vsx_ind_histo
[t
][b
] += cvsx
->vsx_ind_histo
[t
][b
];
2940 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_agg_histo
[0]); b
++)
2941 vsx
->vsx_agg_histo
[t
][b
] += cvsx
->vsx_agg_histo
[t
][b
];
2947 * Get statistics for the given vdev.
2950 vdev_get_stats_ex_impl(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
2954 * If we're getting stats on the root vdev, aggregate the I/O counts
2955 * over all top-level vdevs (i.e. the direct children of the root).
2957 if (!vd
->vdev_ops
->vdev_op_leaf
) {
2959 memset(vs
->vs_ops
, 0, sizeof (vs
->vs_ops
));
2960 memset(vs
->vs_bytes
, 0, sizeof (vs
->vs_bytes
));
2963 memset(vsx
, 0, sizeof (*vsx
));
2965 for (c
= 0; c
< vd
->vdev_children
; c
++) {
2966 vdev_t
*cvd
= vd
->vdev_child
[c
];
2967 vdev_stat_t
*cvs
= &cvd
->vdev_stat
;
2968 vdev_stat_ex_t
*cvsx
= &cvd
->vdev_stat_ex
;
2970 vdev_get_stats_ex_impl(cvd
, cvs
, cvsx
);
2972 vdev_get_child_stat(cvd
, vs
, cvs
);
2974 vdev_get_child_stat_ex(cvd
, vsx
, cvsx
);
2979 * We're a leaf. Just copy our ZIO active queue stats in. The
2980 * other leaf stats are updated in vdev_stat_update().
2985 memcpy(vsx
, &vd
->vdev_stat_ex
, sizeof (vd
->vdev_stat_ex
));
2987 for (t
= 0; t
< ARRAY_SIZE(vd
->vdev_queue
.vq_class
); t
++) {
2988 vsx
->vsx_active_queue
[t
] =
2989 vd
->vdev_queue
.vq_class
[t
].vqc_active
;
2990 vsx
->vsx_pend_queue
[t
] = avl_numnodes(
2991 &vd
->vdev_queue
.vq_class
[t
].vqc_queued_tree
);
2997 vdev_get_stats_ex(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
2999 vdev_t
*tvd
= vd
->vdev_top
;
3000 mutex_enter(&vd
->vdev_stat_lock
);
3002 bcopy(&vd
->vdev_stat
, vs
, sizeof (*vs
));
3003 vs
->vs_timestamp
= gethrtime() - vs
->vs_timestamp
;
3004 vs
->vs_state
= vd
->vdev_state
;
3005 vs
->vs_rsize
= vdev_get_min_asize(vd
);
3006 if (vd
->vdev_ops
->vdev_op_leaf
)
3007 vs
->vs_rsize
+= VDEV_LABEL_START_SIZE
+
3008 VDEV_LABEL_END_SIZE
;
3010 * Report expandable space on top-level, non-auxillary devices
3011 * only. The expandable space is reported in terms of metaslab
3012 * sized units since that determines how much space the pool
3015 if (vd
->vdev_aux
== NULL
&& tvd
!= NULL
) {
3016 vs
->vs_esize
= P2ALIGN(
3017 vd
->vdev_max_asize
- vd
->vdev_asize
,
3018 1ULL << tvd
->vdev_ms_shift
);
3020 vs
->vs_esize
= vd
->vdev_max_asize
- vd
->vdev_asize
;
3021 if (vd
->vdev_aux
== NULL
&& vd
== vd
->vdev_top
&&
3023 vs
->vs_fragmentation
= vd
->vdev_mg
->mg_fragmentation
;
3027 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_READER
) != 0);
3028 vdev_get_stats_ex_impl(vd
, vs
, vsx
);
3029 mutex_exit(&vd
->vdev_stat_lock
);
3033 vdev_get_stats(vdev_t
*vd
, vdev_stat_t
*vs
)
3035 return (vdev_get_stats_ex(vd
, vs
, NULL
));
3039 vdev_clear_stats(vdev_t
*vd
)
3041 mutex_enter(&vd
->vdev_stat_lock
);
3042 vd
->vdev_stat
.vs_space
= 0;
3043 vd
->vdev_stat
.vs_dspace
= 0;
3044 vd
->vdev_stat
.vs_alloc
= 0;
3045 mutex_exit(&vd
->vdev_stat_lock
);
3049 vdev_scan_stat_init(vdev_t
*vd
)
3051 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3054 for (c
= 0; c
< vd
->vdev_children
; c
++)
3055 vdev_scan_stat_init(vd
->vdev_child
[c
]);
3057 mutex_enter(&vd
->vdev_stat_lock
);
3058 vs
->vs_scan_processed
= 0;
3059 mutex_exit(&vd
->vdev_stat_lock
);
3063 vdev_stat_update(zio_t
*zio
, uint64_t psize
)
3065 spa_t
*spa
= zio
->io_spa
;
3066 vdev_t
*rvd
= spa
->spa_root_vdev
;
3067 vdev_t
*vd
= zio
->io_vd
? zio
->io_vd
: rvd
;
3069 uint64_t txg
= zio
->io_txg
;
3070 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3071 vdev_stat_ex_t
*vsx
= &vd
->vdev_stat_ex
;
3072 zio_type_t type
= zio
->io_type
;
3073 int flags
= zio
->io_flags
;
3076 * If this i/o is a gang leader, it didn't do any actual work.
3078 if (zio
->io_gang_tree
)
3081 if (zio
->io_error
== 0) {
3083 * If this is a root i/o, don't count it -- we've already
3084 * counted the top-level vdevs, and vdev_get_stats() will
3085 * aggregate them when asked. This reduces contention on
3086 * the root vdev_stat_lock and implicitly handles blocks
3087 * that compress away to holes, for which there is no i/o.
3088 * (Holes never create vdev children, so all the counters
3089 * remain zero, which is what we want.)
3091 * Note: this only applies to successful i/o (io_error == 0)
3092 * because unlike i/o counts, errors are not additive.
3093 * When reading a ditto block, for example, failure of
3094 * one top-level vdev does not imply a root-level error.
3099 ASSERT(vd
== zio
->io_vd
);
3101 if (flags
& ZIO_FLAG_IO_BYPASS
)
3104 mutex_enter(&vd
->vdev_stat_lock
);
3106 if (flags
& ZIO_FLAG_IO_REPAIR
) {
3107 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
3108 dsl_scan_phys_t
*scn_phys
=
3109 &spa
->spa_dsl_pool
->dp_scan
->scn_phys
;
3110 uint64_t *processed
= &scn_phys
->scn_processed
;
3113 if (vd
->vdev_ops
->vdev_op_leaf
)
3114 atomic_add_64(processed
, psize
);
3115 vs
->vs_scan_processed
+= psize
;
3118 if (flags
& ZIO_FLAG_SELF_HEAL
)
3119 vs
->vs_self_healed
+= psize
;
3123 * The bytes/ops/histograms are recorded at the leaf level and
3124 * aggregated into the higher level vdevs in vdev_get_stats().
3126 if (vd
->vdev_ops
->vdev_op_leaf
&&
3127 (zio
->io_priority
< ZIO_PRIORITY_NUM_QUEUEABLE
)) {
3130 vs
->vs_bytes
[type
] += psize
;
3132 if (flags
& ZIO_FLAG_DELEGATED
) {
3133 vsx
->vsx_agg_histo
[zio
->io_priority
]
3134 [RQ_HISTO(zio
->io_size
)]++;
3136 vsx
->vsx_ind_histo
[zio
->io_priority
]
3137 [RQ_HISTO(zio
->io_size
)]++;
3140 if (zio
->io_delta
&& zio
->io_delay
) {
3141 vsx
->vsx_queue_histo
[zio
->io_priority
]
3142 [L_HISTO(zio
->io_delta
- zio
->io_delay
)]++;
3143 vsx
->vsx_disk_histo
[type
]
3144 [L_HISTO(zio
->io_delay
)]++;
3145 vsx
->vsx_total_histo
[type
]
3146 [L_HISTO(zio
->io_delta
)]++;
3150 mutex_exit(&vd
->vdev_stat_lock
);
3154 if (flags
& ZIO_FLAG_SPECULATIVE
)
3158 * If this is an I/O error that is going to be retried, then ignore the
3159 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
3160 * hard errors, when in reality they can happen for any number of
3161 * innocuous reasons (bus resets, MPxIO link failure, etc).
3163 if (zio
->io_error
== EIO
&&
3164 !(zio
->io_flags
& ZIO_FLAG_IO_RETRY
))
3168 * Intent logs writes won't propagate their error to the root
3169 * I/O so don't mark these types of failures as pool-level
3172 if (zio
->io_vd
== NULL
&& (zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
3175 mutex_enter(&vd
->vdev_stat_lock
);
3176 if (type
== ZIO_TYPE_READ
&& !vdev_is_dead(vd
)) {
3177 if (zio
->io_error
== ECKSUM
)
3178 vs
->vs_checksum_errors
++;
3180 vs
->vs_read_errors
++;
3182 if (type
== ZIO_TYPE_WRITE
&& !vdev_is_dead(vd
))
3183 vs
->vs_write_errors
++;
3184 mutex_exit(&vd
->vdev_stat_lock
);
3186 if (type
== ZIO_TYPE_WRITE
&& txg
!= 0 &&
3187 (!(flags
& ZIO_FLAG_IO_REPAIR
) ||
3188 (flags
& ZIO_FLAG_SCAN_THREAD
) ||
3189 spa
->spa_claiming
)) {
3191 * This is either a normal write (not a repair), or it's
3192 * a repair induced by the scrub thread, or it's a repair
3193 * made by zil_claim() during spa_load() in the first txg.
3194 * In the normal case, we commit the DTL change in the same
3195 * txg as the block was born. In the scrub-induced repair
3196 * case, we know that scrubs run in first-pass syncing context,
3197 * so we commit the DTL change in spa_syncing_txg(spa).
3198 * In the zil_claim() case, we commit in spa_first_txg(spa).
3200 * We currently do not make DTL entries for failed spontaneous
3201 * self-healing writes triggered by normal (non-scrubbing)
3202 * reads, because we have no transactional context in which to
3203 * do so -- and it's not clear that it'd be desirable anyway.
3205 if (vd
->vdev_ops
->vdev_op_leaf
) {
3206 uint64_t commit_txg
= txg
;
3207 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
3208 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
3209 ASSERT(spa_sync_pass(spa
) == 1);
3210 vdev_dtl_dirty(vd
, DTL_SCRUB
, txg
, 1);
3211 commit_txg
= spa_syncing_txg(spa
);
3212 } else if (spa
->spa_claiming
) {
3213 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
3214 commit_txg
= spa_first_txg(spa
);
3216 ASSERT(commit_txg
>= spa_syncing_txg(spa
));
3217 if (vdev_dtl_contains(vd
, DTL_MISSING
, txg
, 1))
3219 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3220 vdev_dtl_dirty(pvd
, DTL_PARTIAL
, txg
, 1);
3221 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, commit_txg
);
3224 vdev_dtl_dirty(vd
, DTL_MISSING
, txg
, 1);
3229 * Update the in-core space usage stats for this vdev, its metaslab class,
3230 * and the root vdev.
3233 vdev_space_update(vdev_t
*vd
, int64_t alloc_delta
, int64_t defer_delta
,
3234 int64_t space_delta
)
3236 int64_t dspace_delta
= space_delta
;
3237 spa_t
*spa
= vd
->vdev_spa
;
3238 vdev_t
*rvd
= spa
->spa_root_vdev
;
3239 metaslab_group_t
*mg
= vd
->vdev_mg
;
3240 metaslab_class_t
*mc
= mg
? mg
->mg_class
: NULL
;
3242 ASSERT(vd
== vd
->vdev_top
);
3245 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
3246 * factor. We must calculate this here and not at the root vdev
3247 * because the root vdev's psize-to-asize is simply the max of its
3248 * childrens', thus not accurate enough for us.
3250 ASSERT((dspace_delta
& (SPA_MINBLOCKSIZE
-1)) == 0);
3251 ASSERT(vd
->vdev_deflate_ratio
!= 0 || vd
->vdev_isl2cache
);
3252 dspace_delta
= (dspace_delta
>> SPA_MINBLOCKSHIFT
) *
3253 vd
->vdev_deflate_ratio
;
3255 mutex_enter(&vd
->vdev_stat_lock
);
3256 vd
->vdev_stat
.vs_alloc
+= alloc_delta
;
3257 vd
->vdev_stat
.vs_space
+= space_delta
;
3258 vd
->vdev_stat
.vs_dspace
+= dspace_delta
;
3259 mutex_exit(&vd
->vdev_stat_lock
);
3261 if (mc
== spa_normal_class(spa
)) {
3262 mutex_enter(&rvd
->vdev_stat_lock
);
3263 rvd
->vdev_stat
.vs_alloc
+= alloc_delta
;
3264 rvd
->vdev_stat
.vs_space
+= space_delta
;
3265 rvd
->vdev_stat
.vs_dspace
+= dspace_delta
;
3266 mutex_exit(&rvd
->vdev_stat_lock
);
3270 ASSERT(rvd
== vd
->vdev_parent
);
3271 ASSERT(vd
->vdev_ms_count
!= 0);
3273 metaslab_class_space_update(mc
,
3274 alloc_delta
, defer_delta
, space_delta
, dspace_delta
);
3279 * Mark a top-level vdev's config as dirty, placing it on the dirty list
3280 * so that it will be written out next time the vdev configuration is synced.
3281 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3284 vdev_config_dirty(vdev_t
*vd
)
3286 spa_t
*spa
= vd
->vdev_spa
;
3287 vdev_t
*rvd
= spa
->spa_root_vdev
;
3290 ASSERT(spa_writeable(spa
));
3293 * If this is an aux vdev (as with l2cache and spare devices), then we
3294 * update the vdev config manually and set the sync flag.
3296 if (vd
->vdev_aux
!= NULL
) {
3297 spa_aux_vdev_t
*sav
= vd
->vdev_aux
;
3301 for (c
= 0; c
< sav
->sav_count
; c
++) {
3302 if (sav
->sav_vdevs
[c
] == vd
)
3306 if (c
== sav
->sav_count
) {
3308 * We're being removed. There's nothing more to do.
3310 ASSERT(sav
->sav_sync
== B_TRUE
);
3314 sav
->sav_sync
= B_TRUE
;
3316 if (nvlist_lookup_nvlist_array(sav
->sav_config
,
3317 ZPOOL_CONFIG_L2CACHE
, &aux
, &naux
) != 0) {
3318 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
3319 ZPOOL_CONFIG_SPARES
, &aux
, &naux
) == 0);
3325 * Setting the nvlist in the middle if the array is a little
3326 * sketchy, but it will work.
3328 nvlist_free(aux
[c
]);
3329 aux
[c
] = vdev_config_generate(spa
, vd
, B_TRUE
, 0);
3335 * The dirty list is protected by the SCL_CONFIG lock. The caller
3336 * must either hold SCL_CONFIG as writer, or must be the sync thread
3337 * (which holds SCL_CONFIG as reader). There's only one sync thread,
3338 * so this is sufficient to ensure mutual exclusion.
3340 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
3341 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3342 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
3345 for (c
= 0; c
< rvd
->vdev_children
; c
++)
3346 vdev_config_dirty(rvd
->vdev_child
[c
]);
3348 ASSERT(vd
== vd
->vdev_top
);
3350 if (!list_link_active(&vd
->vdev_config_dirty_node
) &&
3352 list_insert_head(&spa
->spa_config_dirty_list
, vd
);
3357 vdev_config_clean(vdev_t
*vd
)
3359 spa_t
*spa
= vd
->vdev_spa
;
3361 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
3362 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3363 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
3365 ASSERT(list_link_active(&vd
->vdev_config_dirty_node
));
3366 list_remove(&spa
->spa_config_dirty_list
, vd
);
3370 * Mark a top-level vdev's state as dirty, so that the next pass of
3371 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3372 * the state changes from larger config changes because they require
3373 * much less locking, and are often needed for administrative actions.
3376 vdev_state_dirty(vdev_t
*vd
)
3378 spa_t
*spa
= vd
->vdev_spa
;
3380 ASSERT(spa_writeable(spa
));
3381 ASSERT(vd
== vd
->vdev_top
);
3384 * The state list is protected by the SCL_STATE lock. The caller
3385 * must either hold SCL_STATE as writer, or must be the sync thread
3386 * (which holds SCL_STATE as reader). There's only one sync thread,
3387 * so this is sufficient to ensure mutual exclusion.
3389 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
3390 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3391 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
3393 if (!list_link_active(&vd
->vdev_state_dirty_node
) && !vd
->vdev_ishole
)
3394 list_insert_head(&spa
->spa_state_dirty_list
, vd
);
3398 vdev_state_clean(vdev_t
*vd
)
3400 spa_t
*spa
= vd
->vdev_spa
;
3402 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
3403 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3404 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
3406 ASSERT(list_link_active(&vd
->vdev_state_dirty_node
));
3407 list_remove(&spa
->spa_state_dirty_list
, vd
);
3411 * Propagate vdev state up from children to parent.
3414 vdev_propagate_state(vdev_t
*vd
)
3416 spa_t
*spa
= vd
->vdev_spa
;
3417 vdev_t
*rvd
= spa
->spa_root_vdev
;
3418 int degraded
= 0, faulted
= 0;
3423 if (vd
->vdev_children
> 0) {
3424 for (c
= 0; c
< vd
->vdev_children
; c
++) {
3425 child
= vd
->vdev_child
[c
];
3428 * Don't factor holes into the decision.
3430 if (child
->vdev_ishole
)
3433 if (!vdev_readable(child
) ||
3434 (!vdev_writeable(child
) && spa_writeable(spa
))) {
3436 * Root special: if there is a top-level log
3437 * device, treat the root vdev as if it were
3440 if (child
->vdev_islog
&& vd
== rvd
)
3444 } else if (child
->vdev_state
<= VDEV_STATE_DEGRADED
) {
3448 if (child
->vdev_stat
.vs_aux
== VDEV_AUX_CORRUPT_DATA
)
3452 vd
->vdev_ops
->vdev_op_state_change(vd
, faulted
, degraded
);
3455 * Root special: if there is a top-level vdev that cannot be
3456 * opened due to corrupted metadata, then propagate the root
3457 * vdev's aux state as 'corrupt' rather than 'insufficient
3460 if (corrupted
&& vd
== rvd
&&
3461 rvd
->vdev_state
== VDEV_STATE_CANT_OPEN
)
3462 vdev_set_state(rvd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3463 VDEV_AUX_CORRUPT_DATA
);
3466 if (vd
->vdev_parent
)
3467 vdev_propagate_state(vd
->vdev_parent
);
3471 * Set a vdev's state. If this is during an open, we don't update the parent
3472 * state, because we're in the process of opening children depth-first.
3473 * Otherwise, we propagate the change to the parent.
3475 * If this routine places a device in a faulted state, an appropriate ereport is
3479 vdev_set_state(vdev_t
*vd
, boolean_t isopen
, vdev_state_t state
, vdev_aux_t aux
)
3481 uint64_t save_state
;
3482 spa_t
*spa
= vd
->vdev_spa
;
3484 if (state
== vd
->vdev_state
) {
3486 * Since vdev_offline() code path is already in an offline
3487 * state we can miss a statechange event to OFFLINE. Check
3488 * the previous state to catch this condition.
3490 if (vd
->vdev_ops
->vdev_op_leaf
&&
3491 (state
== VDEV_STATE_OFFLINE
) &&
3492 (vd
->vdev_prevstate
>= VDEV_STATE_FAULTED
)) {
3493 /* post an offline state change */
3494 zfs_post_state_change(spa
, vd
, vd
->vdev_prevstate
);
3496 vd
->vdev_stat
.vs_aux
= aux
;
3500 save_state
= vd
->vdev_state
;
3502 vd
->vdev_state
= state
;
3503 vd
->vdev_stat
.vs_aux
= aux
;
3506 * If we are setting the vdev state to anything but an open state, then
3507 * always close the underlying device unless the device has requested
3508 * a delayed close (i.e. we're about to remove or fault the device).
3509 * Otherwise, we keep accessible but invalid devices open forever.
3510 * We don't call vdev_close() itself, because that implies some extra
3511 * checks (offline, etc) that we don't want here. This is limited to
3512 * leaf devices, because otherwise closing the device will affect other
3515 if (!vd
->vdev_delayed_close
&& vdev_is_dead(vd
) &&
3516 vd
->vdev_ops
->vdev_op_leaf
)
3517 vd
->vdev_ops
->vdev_op_close(vd
);
3519 if (vd
->vdev_removed
&&
3520 state
== VDEV_STATE_CANT_OPEN
&&
3521 (aux
== VDEV_AUX_OPEN_FAILED
|| vd
->vdev_checkremove
)) {
3523 * If the previous state is set to VDEV_STATE_REMOVED, then this
3524 * device was previously marked removed and someone attempted to
3525 * reopen it. If this failed due to a nonexistent device, then
3526 * keep the device in the REMOVED state. We also let this be if
3527 * it is one of our special test online cases, which is only
3528 * attempting to online the device and shouldn't generate an FMA
3531 vd
->vdev_state
= VDEV_STATE_REMOVED
;
3532 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
3533 } else if (state
== VDEV_STATE_REMOVED
) {
3534 vd
->vdev_removed
= B_TRUE
;
3535 } else if (state
== VDEV_STATE_CANT_OPEN
) {
3537 * If we fail to open a vdev during an import or recovery, we
3538 * mark it as "not available", which signifies that it was
3539 * never there to begin with. Failure to open such a device
3540 * is not considered an error.
3542 if ((spa_load_state(spa
) == SPA_LOAD_IMPORT
||
3543 spa_load_state(spa
) == SPA_LOAD_RECOVER
) &&
3544 vd
->vdev_ops
->vdev_op_leaf
)
3545 vd
->vdev_not_present
= 1;
3548 * Post the appropriate ereport. If the 'prevstate' field is
3549 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3550 * that this is part of a vdev_reopen(). In this case, we don't
3551 * want to post the ereport if the device was already in the
3552 * CANT_OPEN state beforehand.
3554 * If the 'checkremove' flag is set, then this is an attempt to
3555 * online the device in response to an insertion event. If we
3556 * hit this case, then we have detected an insertion event for a
3557 * faulted or offline device that wasn't in the removed state.
3558 * In this scenario, we don't post an ereport because we are
3559 * about to replace the device, or attempt an online with
3560 * vdev_forcefault, which will generate the fault for us.
3562 if ((vd
->vdev_prevstate
!= state
|| vd
->vdev_forcefault
) &&
3563 !vd
->vdev_not_present
&& !vd
->vdev_checkremove
&&
3564 vd
!= spa
->spa_root_vdev
) {
3568 case VDEV_AUX_OPEN_FAILED
:
3569 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED
;
3571 case VDEV_AUX_CORRUPT_DATA
:
3572 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA
;
3574 case VDEV_AUX_NO_REPLICAS
:
3575 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS
;
3577 case VDEV_AUX_BAD_GUID_SUM
:
3578 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM
;
3580 case VDEV_AUX_TOO_SMALL
:
3581 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL
;
3583 case VDEV_AUX_BAD_LABEL
:
3584 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL
;
3586 case VDEV_AUX_BAD_ASHIFT
:
3587 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
;
3590 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN
;
3593 zfs_ereport_post(class, spa
, vd
, NULL
, NULL
,
3597 /* Erase any notion of persistent removed state */
3598 vd
->vdev_removed
= B_FALSE
;
3600 vd
->vdev_removed
= B_FALSE
;
3604 * Notify ZED of any significant state-change on a leaf vdev.
3607 if (vd
->vdev_ops
->vdev_op_leaf
) {
3608 /* preserve original state from a vdev_reopen() */
3609 if ((vd
->vdev_prevstate
!= VDEV_STATE_UNKNOWN
) &&
3610 (vd
->vdev_prevstate
!= vd
->vdev_state
) &&
3611 (save_state
<= VDEV_STATE_CLOSED
))
3612 save_state
= vd
->vdev_prevstate
;
3614 /* filter out state change due to initial vdev_open */
3615 if (save_state
> VDEV_STATE_CLOSED
)
3616 zfs_post_state_change(spa
, vd
, save_state
);
3619 if (!isopen
&& vd
->vdev_parent
)
3620 vdev_propagate_state(vd
->vdev_parent
);
3624 * Check the vdev configuration to ensure that it's capable of supporting
3625 * a root pool. We do not support partial configuration.
3628 vdev_is_bootable(vdev_t
*vd
)
3630 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3631 const char *vdev_type
= vd
->vdev_ops
->vdev_op_type
;
3633 if (strcmp(vdev_type
, VDEV_TYPE_MISSING
) == 0)
3637 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3638 if (!vdev_is_bootable(vd
->vdev_child
[c
]))
3645 * Load the state from the original vdev tree (ovd) which
3646 * we've retrieved from the MOS config object. If the original
3647 * vdev was offline or faulted then we transfer that state to the
3648 * device in the current vdev tree (nvd).
3651 vdev_load_log_state(vdev_t
*nvd
, vdev_t
*ovd
)
3655 ASSERT(nvd
->vdev_top
->vdev_islog
);
3656 ASSERT(spa_config_held(nvd
->vdev_spa
,
3657 SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
3658 ASSERT3U(nvd
->vdev_guid
, ==, ovd
->vdev_guid
);
3660 for (c
= 0; c
< nvd
->vdev_children
; c
++)
3661 vdev_load_log_state(nvd
->vdev_child
[c
], ovd
->vdev_child
[c
]);
3663 if (nvd
->vdev_ops
->vdev_op_leaf
) {
3665 * Restore the persistent vdev state
3667 nvd
->vdev_offline
= ovd
->vdev_offline
;
3668 nvd
->vdev_faulted
= ovd
->vdev_faulted
;
3669 nvd
->vdev_degraded
= ovd
->vdev_degraded
;
3670 nvd
->vdev_removed
= ovd
->vdev_removed
;
3675 * Determine if a log device has valid content. If the vdev was
3676 * removed or faulted in the MOS config then we know that
3677 * the content on the log device has already been written to the pool.
3680 vdev_log_state_valid(vdev_t
*vd
)
3684 if (vd
->vdev_ops
->vdev_op_leaf
&& !vd
->vdev_faulted
&&
3688 for (c
= 0; c
< vd
->vdev_children
; c
++)
3689 if (vdev_log_state_valid(vd
->vdev_child
[c
]))
3696 * Expand a vdev if possible.
3699 vdev_expand(vdev_t
*vd
, uint64_t txg
)
3701 ASSERT(vd
->vdev_top
== vd
);
3702 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3704 if ((vd
->vdev_asize
>> vd
->vdev_ms_shift
) > vd
->vdev_ms_count
) {
3705 VERIFY(vdev_metaslab_init(vd
, txg
) == 0);
3706 vdev_config_dirty(vd
);
3714 vdev_split(vdev_t
*vd
)
3716 vdev_t
*cvd
, *pvd
= vd
->vdev_parent
;
3718 vdev_remove_child(pvd
, vd
);
3719 vdev_compact_children(pvd
);
3721 cvd
= pvd
->vdev_child
[0];
3722 if (pvd
->vdev_children
== 1) {
3723 vdev_remove_parent(cvd
);
3724 cvd
->vdev_splitting
= B_TRUE
;
3726 vdev_propagate_state(cvd
);
3730 vdev_deadman(vdev_t
*vd
)
3734 for (c
= 0; c
< vd
->vdev_children
; c
++) {
3735 vdev_t
*cvd
= vd
->vdev_child
[c
];
3740 if (vd
->vdev_ops
->vdev_op_leaf
) {
3741 vdev_queue_t
*vq
= &vd
->vdev_queue
;
3743 mutex_enter(&vq
->vq_lock
);
3744 if (avl_numnodes(&vq
->vq_active_tree
) > 0) {
3745 spa_t
*spa
= vd
->vdev_spa
;
3750 * Look at the head of all the pending queues,
3751 * if any I/O has been outstanding for longer than
3752 * the spa_deadman_synctime we log a zevent.
3754 fio
= avl_first(&vq
->vq_active_tree
);
3755 delta
= gethrtime() - fio
->io_timestamp
;
3756 if (delta
> spa_deadman_synctime(spa
)) {
3757 zfs_dbgmsg("SLOW IO: zio timestamp %lluns, "
3758 "delta %lluns, last io %lluns",
3759 fio
->io_timestamp
, delta
,
3760 vq
->vq_io_complete_ts
);
3761 zfs_ereport_post(FM_EREPORT_ZFS_DELAY
,
3762 spa
, vd
, &fio
->io_bookmark
, fio
, 0, 0);
3765 mutex_exit(&vq
->vq_lock
);
3769 #if defined(_KERNEL) && defined(HAVE_SPL)
3770 EXPORT_SYMBOL(vdev_fault
);
3771 EXPORT_SYMBOL(vdev_degrade
);
3772 EXPORT_SYMBOL(vdev_online
);
3773 EXPORT_SYMBOL(vdev_offline
);
3774 EXPORT_SYMBOL(vdev_clear
);
3776 module_param(metaslabs_per_vdev
, int, 0644);
3777 MODULE_PARM_DESC(metaslabs_per_vdev
,
3778 "Divide added vdev into approximately (but no more than) this number "