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 * Rate limit delay events to this many IO delays per second.
62 unsigned int zfs_delays_per_second
= 20;
65 * Rate limit checksum events after this many checksum errors per second.
67 unsigned int zfs_checksums_per_second
= 20;
70 * Virtual device management.
73 static vdev_ops_t
*vdev_ops_table
[] = {
87 * Given a vdev type, return the appropriate ops vector.
90 vdev_getops(const char *type
)
92 vdev_ops_t
*ops
, **opspp
;
94 for (opspp
= vdev_ops_table
; (ops
= *opspp
) != NULL
; opspp
++)
95 if (strcmp(ops
->vdev_op_type
, type
) == 0)
102 * Default asize function: return the MAX of psize with the asize of
103 * all children. This is what's used by anything other than RAID-Z.
106 vdev_default_asize(vdev_t
*vd
, uint64_t psize
)
108 uint64_t asize
= P2ROUNDUP(psize
, 1ULL << vd
->vdev_top
->vdev_ashift
);
111 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
112 csize
= vdev_psize_to_asize(vd
->vdev_child
[c
], psize
);
113 asize
= MAX(asize
, csize
);
120 * Get the minimum allocatable size. We define the allocatable size as
121 * the vdev's asize rounded to the nearest metaslab. This allows us to
122 * replace or attach devices which don't have the same physical size but
123 * can still satisfy the same number of allocations.
126 vdev_get_min_asize(vdev_t
*vd
)
128 vdev_t
*pvd
= vd
->vdev_parent
;
131 * If our parent is NULL (inactive spare or cache) or is the root,
132 * just return our own asize.
135 return (vd
->vdev_asize
);
138 * The top-level vdev just returns the allocatable size rounded
139 * to the nearest metaslab.
141 if (vd
== vd
->vdev_top
)
142 return (P2ALIGN(vd
->vdev_asize
, 1ULL << vd
->vdev_ms_shift
));
145 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
146 * so each child must provide at least 1/Nth of its asize.
148 if (pvd
->vdev_ops
== &vdev_raidz_ops
)
149 return ((pvd
->vdev_min_asize
+ pvd
->vdev_children
- 1) /
152 return (pvd
->vdev_min_asize
);
156 vdev_set_min_asize(vdev_t
*vd
)
158 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
160 for (int c
= 0; c
< vd
->vdev_children
; c
++)
161 vdev_set_min_asize(vd
->vdev_child
[c
]);
165 vdev_lookup_top(spa_t
*spa
, uint64_t vdev
)
167 vdev_t
*rvd
= spa
->spa_root_vdev
;
169 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
171 if (vdev
< rvd
->vdev_children
) {
172 ASSERT(rvd
->vdev_child
[vdev
] != NULL
);
173 return (rvd
->vdev_child
[vdev
]);
180 vdev_lookup_by_guid(vdev_t
*vd
, uint64_t guid
)
184 if (vd
->vdev_guid
== guid
)
187 for (int c
= 0; c
< vd
->vdev_children
; c
++)
188 if ((mvd
= vdev_lookup_by_guid(vd
->vdev_child
[c
], guid
)) !=
196 vdev_count_leaves_impl(vdev_t
*vd
)
200 if (vd
->vdev_ops
->vdev_op_leaf
)
203 for (int c
= 0; c
< vd
->vdev_children
; c
++)
204 n
+= vdev_count_leaves_impl(vd
->vdev_child
[c
]);
210 vdev_count_leaves(spa_t
*spa
)
212 return (vdev_count_leaves_impl(spa
->spa_root_vdev
));
216 vdev_add_child(vdev_t
*pvd
, vdev_t
*cvd
)
218 size_t oldsize
, newsize
;
219 uint64_t id
= cvd
->vdev_id
;
222 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
223 ASSERT(cvd
->vdev_parent
== NULL
);
225 cvd
->vdev_parent
= pvd
;
230 ASSERT(id
>= pvd
->vdev_children
|| pvd
->vdev_child
[id
] == NULL
);
232 oldsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
233 pvd
->vdev_children
= MAX(pvd
->vdev_children
, id
+ 1);
234 newsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
236 newchild
= kmem_alloc(newsize
, KM_SLEEP
);
237 if (pvd
->vdev_child
!= NULL
) {
238 bcopy(pvd
->vdev_child
, newchild
, oldsize
);
239 kmem_free(pvd
->vdev_child
, oldsize
);
242 pvd
->vdev_child
= newchild
;
243 pvd
->vdev_child
[id
] = cvd
;
245 cvd
->vdev_top
= (pvd
->vdev_top
? pvd
->vdev_top
: cvd
);
246 ASSERT(cvd
->vdev_top
->vdev_parent
->vdev_parent
== NULL
);
249 * Walk up all ancestors to update guid sum.
251 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
252 pvd
->vdev_guid_sum
+= cvd
->vdev_guid_sum
;
256 vdev_remove_child(vdev_t
*pvd
, vdev_t
*cvd
)
259 uint_t id
= cvd
->vdev_id
;
261 ASSERT(cvd
->vdev_parent
== pvd
);
266 ASSERT(id
< pvd
->vdev_children
);
267 ASSERT(pvd
->vdev_child
[id
] == cvd
);
269 pvd
->vdev_child
[id
] = NULL
;
270 cvd
->vdev_parent
= NULL
;
272 for (c
= 0; c
< pvd
->vdev_children
; c
++)
273 if (pvd
->vdev_child
[c
])
276 if (c
== pvd
->vdev_children
) {
277 kmem_free(pvd
->vdev_child
, c
* sizeof (vdev_t
*));
278 pvd
->vdev_child
= NULL
;
279 pvd
->vdev_children
= 0;
283 * Walk up all ancestors to update guid sum.
285 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
286 pvd
->vdev_guid_sum
-= cvd
->vdev_guid_sum
;
290 * Remove any holes in the child array.
293 vdev_compact_children(vdev_t
*pvd
)
295 vdev_t
**newchild
, *cvd
;
296 int oldc
= pvd
->vdev_children
;
299 ASSERT(spa_config_held(pvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
301 for (int c
= newc
= 0; c
< oldc
; c
++)
302 if (pvd
->vdev_child
[c
])
305 newchild
= kmem_zalloc(newc
* sizeof (vdev_t
*), KM_SLEEP
);
307 for (int c
= newc
= 0; c
< oldc
; c
++) {
308 if ((cvd
= pvd
->vdev_child
[c
]) != NULL
) {
309 newchild
[newc
] = cvd
;
310 cvd
->vdev_id
= newc
++;
314 kmem_free(pvd
->vdev_child
, oldc
* sizeof (vdev_t
*));
315 pvd
->vdev_child
= newchild
;
316 pvd
->vdev_children
= newc
;
320 * Allocate and minimally initialize a vdev_t.
323 vdev_alloc_common(spa_t
*spa
, uint_t id
, uint64_t guid
, vdev_ops_t
*ops
)
327 vd
= kmem_zalloc(sizeof (vdev_t
), KM_SLEEP
);
329 if (spa
->spa_root_vdev
== NULL
) {
330 ASSERT(ops
== &vdev_root_ops
);
331 spa
->spa_root_vdev
= vd
;
332 spa
->spa_load_guid
= spa_generate_guid(NULL
);
335 if (guid
== 0 && ops
!= &vdev_hole_ops
) {
336 if (spa
->spa_root_vdev
== vd
) {
338 * The root vdev's guid will also be the pool guid,
339 * which must be unique among all pools.
341 guid
= spa_generate_guid(NULL
);
344 * Any other vdev's guid must be unique within the pool.
346 guid
= spa_generate_guid(spa
);
348 ASSERT(!spa_guid_exists(spa_guid(spa
), guid
));
353 vd
->vdev_guid
= guid
;
354 vd
->vdev_guid_sum
= guid
;
356 vd
->vdev_state
= VDEV_STATE_CLOSED
;
357 vd
->vdev_ishole
= (ops
== &vdev_hole_ops
);
360 * Initialize rate limit structs for events. We rate limit ZIO delay
361 * and checksum events so that we don't overwhelm ZED with thousands
362 * of events when a disk is acting up.
364 zfs_ratelimit_init(&vd
->vdev_delay_rl
, &zfs_delays_per_second
, 1);
365 zfs_ratelimit_init(&vd
->vdev_checksum_rl
, &zfs_checksums_per_second
, 1);
367 list_link_init(&vd
->vdev_config_dirty_node
);
368 list_link_init(&vd
->vdev_state_dirty_node
);
369 mutex_init(&vd
->vdev_dtl_lock
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
370 mutex_init(&vd
->vdev_stat_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
371 mutex_init(&vd
->vdev_probe_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
372 mutex_init(&vd
->vdev_queue_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
373 mutex_init(&vd
->vdev_scan_io_queue_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
375 for (int t
= 0; t
< DTL_TYPES
; t
++) {
376 vd
->vdev_dtl
[t
] = range_tree_create(NULL
, NULL
,
379 txg_list_create(&vd
->vdev_ms_list
, spa
,
380 offsetof(struct metaslab
, ms_txg_node
));
381 txg_list_create(&vd
->vdev_dtl_list
, spa
,
382 offsetof(struct vdev
, vdev_dtl_node
));
383 vd
->vdev_stat
.vs_timestamp
= gethrtime();
391 * Allocate a new vdev. The 'alloctype' is used to control whether we are
392 * creating a new vdev or loading an existing one - the behavior is slightly
393 * different for each case.
396 vdev_alloc(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
, uint_t id
,
401 uint64_t guid
= 0, islog
, nparity
;
406 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
408 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
409 return (SET_ERROR(EINVAL
));
411 if ((ops
= vdev_getops(type
)) == NULL
)
412 return (SET_ERROR(EINVAL
));
415 * If this is a load, get the vdev guid from the nvlist.
416 * Otherwise, vdev_alloc_common() will generate one for us.
418 if (alloctype
== VDEV_ALLOC_LOAD
) {
421 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
, &label_id
) ||
423 return (SET_ERROR(EINVAL
));
425 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
426 return (SET_ERROR(EINVAL
));
427 } else if (alloctype
== VDEV_ALLOC_SPARE
) {
428 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
429 return (SET_ERROR(EINVAL
));
430 } else if (alloctype
== VDEV_ALLOC_L2CACHE
) {
431 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
432 return (SET_ERROR(EINVAL
));
433 } else if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
434 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
435 return (SET_ERROR(EINVAL
));
439 * The first allocated vdev must be of type 'root'.
441 if (ops
!= &vdev_root_ops
&& spa
->spa_root_vdev
== NULL
)
442 return (SET_ERROR(EINVAL
));
445 * Determine whether we're a log vdev.
448 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &islog
);
449 if (islog
&& spa_version(spa
) < SPA_VERSION_SLOGS
)
450 return (SET_ERROR(ENOTSUP
));
452 if (ops
== &vdev_hole_ops
&& spa_version(spa
) < SPA_VERSION_HOLES
)
453 return (SET_ERROR(ENOTSUP
));
456 * Set the nparity property for RAID-Z vdevs.
459 if (ops
== &vdev_raidz_ops
) {
460 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
462 if (nparity
== 0 || nparity
> VDEV_RAIDZ_MAXPARITY
)
463 return (SET_ERROR(EINVAL
));
465 * Previous versions could only support 1 or 2 parity
469 spa_version(spa
) < SPA_VERSION_RAIDZ2
)
470 return (SET_ERROR(ENOTSUP
));
472 spa_version(spa
) < SPA_VERSION_RAIDZ3
)
473 return (SET_ERROR(ENOTSUP
));
476 * We require the parity to be specified for SPAs that
477 * support multiple parity levels.
479 if (spa_version(spa
) >= SPA_VERSION_RAIDZ2
)
480 return (SET_ERROR(EINVAL
));
482 * Otherwise, we default to 1 parity device for RAID-Z.
489 ASSERT(nparity
!= -1ULL);
491 vd
= vdev_alloc_common(spa
, id
, guid
, ops
);
493 vd
->vdev_islog
= islog
;
494 vd
->vdev_nparity
= nparity
;
496 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &vd
->vdev_path
) == 0)
497 vd
->vdev_path
= spa_strdup(vd
->vdev_path
);
500 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
501 * fault on a vdev and want it to persist across imports (like with
504 rc
= nvlist_lookup_string(nv
, ZPOOL_CONFIG_AUX_STATE
, &tmp
);
505 if (rc
== 0 && tmp
!= NULL
&& strcmp(tmp
, "external") == 0) {
506 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
507 vd
->vdev_faulted
= 1;
508 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
511 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &vd
->vdev_devid
) == 0)
512 vd
->vdev_devid
= spa_strdup(vd
->vdev_devid
);
513 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PHYS_PATH
,
514 &vd
->vdev_physpath
) == 0)
515 vd
->vdev_physpath
= spa_strdup(vd
->vdev_physpath
);
517 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH
,
518 &vd
->vdev_enc_sysfs_path
) == 0)
519 vd
->vdev_enc_sysfs_path
= spa_strdup(vd
->vdev_enc_sysfs_path
);
521 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_FRU
, &vd
->vdev_fru
) == 0)
522 vd
->vdev_fru
= spa_strdup(vd
->vdev_fru
);
525 * Set the whole_disk property. If it's not specified, leave the value
528 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
529 &vd
->vdev_wholedisk
) != 0)
530 vd
->vdev_wholedisk
= -1ULL;
533 * Look for the 'not present' flag. This will only be set if the device
534 * was not present at the time of import.
536 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
537 &vd
->vdev_not_present
);
540 * Get the alignment requirement.
542 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASHIFT
, &vd
->vdev_ashift
);
545 * Retrieve the vdev creation time.
547 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_CREATE_TXG
,
551 * If we're a top-level vdev, try to load the allocation parameters.
553 if (parent
&& !parent
->vdev_parent
&&
554 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
555 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_ARRAY
,
557 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_SHIFT
,
559 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASIZE
,
561 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVING
,
563 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_VDEV_TOP_ZAP
,
566 ASSERT0(vd
->vdev_top_zap
);
569 if (parent
&& !parent
->vdev_parent
&& alloctype
!= VDEV_ALLOC_ATTACH
) {
570 ASSERT(alloctype
== VDEV_ALLOC_LOAD
||
571 alloctype
== VDEV_ALLOC_ADD
||
572 alloctype
== VDEV_ALLOC_SPLIT
||
573 alloctype
== VDEV_ALLOC_ROOTPOOL
);
574 vd
->vdev_mg
= metaslab_group_create(islog
?
575 spa_log_class(spa
) : spa_normal_class(spa
), vd
);
578 if (vd
->vdev_ops
->vdev_op_leaf
&&
579 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
580 (void) nvlist_lookup_uint64(nv
,
581 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, &vd
->vdev_leaf_zap
);
583 ASSERT0(vd
->vdev_leaf_zap
);
587 * If we're a leaf vdev, try to load the DTL object and other state.
590 if (vd
->vdev_ops
->vdev_op_leaf
&&
591 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_L2CACHE
||
592 alloctype
== VDEV_ALLOC_ROOTPOOL
)) {
593 if (alloctype
== VDEV_ALLOC_LOAD
) {
594 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DTL
,
595 &vd
->vdev_dtl_object
);
596 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_UNSPARE
,
600 if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
603 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
604 &spare
) == 0 && spare
)
608 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
,
611 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_RESILVER_TXG
,
612 &vd
->vdev_resilver_txg
);
615 * In general, when importing a pool we want to ignore the
616 * persistent fault state, as the diagnosis made on another
617 * system may not be valid in the current context. The only
618 * exception is if we forced a vdev to a persistently faulted
619 * state with 'zpool offline -f'. The persistent fault will
620 * remain across imports until cleared.
622 * Local vdevs will remain in the faulted state.
624 if (spa_load_state(spa
) == SPA_LOAD_OPEN
||
625 spa_load_state(spa
) == SPA_LOAD_IMPORT
) {
626 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
,
628 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DEGRADED
,
630 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
,
633 if (vd
->vdev_faulted
|| vd
->vdev_degraded
) {
637 VDEV_AUX_ERR_EXCEEDED
;
638 if (nvlist_lookup_string(nv
,
639 ZPOOL_CONFIG_AUX_STATE
, &aux
) == 0 &&
640 strcmp(aux
, "external") == 0)
641 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
647 * Add ourselves to the parent's list of children.
649 vdev_add_child(parent
, vd
);
657 vdev_free(vdev_t
*vd
)
659 spa_t
*spa
= vd
->vdev_spa
;
662 * Scan queues are normally destroyed at the end of a scan. If the
663 * queue exists here, that implies the vdev is being removed while
664 * the scan is still running.
666 if (vd
->vdev_scan_io_queue
!= NULL
) {
667 mutex_enter(&vd
->vdev_scan_io_queue_lock
);
668 dsl_scan_io_queue_destroy(vd
->vdev_scan_io_queue
);
669 vd
->vdev_scan_io_queue
= NULL
;
670 mutex_exit(&vd
->vdev_scan_io_queue_lock
);
674 * vdev_free() implies closing the vdev first. This is simpler than
675 * trying to ensure complicated semantics for all callers.
679 ASSERT(!list_link_active(&vd
->vdev_config_dirty_node
));
680 ASSERT(!list_link_active(&vd
->vdev_state_dirty_node
));
685 for (int c
= 0; c
< vd
->vdev_children
; c
++)
686 vdev_free(vd
->vdev_child
[c
]);
688 ASSERT(vd
->vdev_child
== NULL
);
689 ASSERT(vd
->vdev_guid_sum
== vd
->vdev_guid
);
692 * Discard allocation state.
694 if (vd
->vdev_mg
!= NULL
) {
695 vdev_metaslab_fini(vd
);
696 metaslab_group_destroy(vd
->vdev_mg
);
699 ASSERT0(vd
->vdev_stat
.vs_space
);
700 ASSERT0(vd
->vdev_stat
.vs_dspace
);
701 ASSERT0(vd
->vdev_stat
.vs_alloc
);
704 * Remove this vdev from its parent's child list.
706 vdev_remove_child(vd
->vdev_parent
, vd
);
708 ASSERT(vd
->vdev_parent
== NULL
);
711 * Clean up vdev structure.
717 spa_strfree(vd
->vdev_path
);
719 spa_strfree(vd
->vdev_devid
);
720 if (vd
->vdev_physpath
)
721 spa_strfree(vd
->vdev_physpath
);
723 if (vd
->vdev_enc_sysfs_path
)
724 spa_strfree(vd
->vdev_enc_sysfs_path
);
727 spa_strfree(vd
->vdev_fru
);
729 if (vd
->vdev_isspare
)
730 spa_spare_remove(vd
);
731 if (vd
->vdev_isl2cache
)
732 spa_l2cache_remove(vd
);
734 txg_list_destroy(&vd
->vdev_ms_list
);
735 txg_list_destroy(&vd
->vdev_dtl_list
);
737 mutex_enter(&vd
->vdev_dtl_lock
);
738 space_map_close(vd
->vdev_dtl_sm
);
739 for (int t
= 0; t
< DTL_TYPES
; t
++) {
740 range_tree_vacate(vd
->vdev_dtl
[t
], NULL
, NULL
);
741 range_tree_destroy(vd
->vdev_dtl
[t
]);
743 mutex_exit(&vd
->vdev_dtl_lock
);
745 mutex_destroy(&vd
->vdev_queue_lock
);
746 mutex_destroy(&vd
->vdev_dtl_lock
);
747 mutex_destroy(&vd
->vdev_stat_lock
);
748 mutex_destroy(&vd
->vdev_probe_lock
);
749 mutex_destroy(&vd
->vdev_scan_io_queue_lock
);
751 zfs_ratelimit_fini(&vd
->vdev_delay_rl
);
752 zfs_ratelimit_fini(&vd
->vdev_checksum_rl
);
754 if (vd
== spa
->spa_root_vdev
)
755 spa
->spa_root_vdev
= NULL
;
757 kmem_free(vd
, sizeof (vdev_t
));
761 * Transfer top-level vdev state from svd to tvd.
764 vdev_top_transfer(vdev_t
*svd
, vdev_t
*tvd
)
766 spa_t
*spa
= svd
->vdev_spa
;
771 ASSERT(tvd
== tvd
->vdev_top
);
773 tvd
->vdev_pending_fastwrite
= svd
->vdev_pending_fastwrite
;
774 tvd
->vdev_ms_array
= svd
->vdev_ms_array
;
775 tvd
->vdev_ms_shift
= svd
->vdev_ms_shift
;
776 tvd
->vdev_ms_count
= svd
->vdev_ms_count
;
777 tvd
->vdev_top_zap
= svd
->vdev_top_zap
;
779 svd
->vdev_ms_array
= 0;
780 svd
->vdev_ms_shift
= 0;
781 svd
->vdev_ms_count
= 0;
782 svd
->vdev_top_zap
= 0;
785 ASSERT3P(tvd
->vdev_mg
, ==, svd
->vdev_mg
);
786 tvd
->vdev_mg
= svd
->vdev_mg
;
787 tvd
->vdev_ms
= svd
->vdev_ms
;
792 if (tvd
->vdev_mg
!= NULL
)
793 tvd
->vdev_mg
->mg_vd
= tvd
;
795 tvd
->vdev_stat
.vs_alloc
= svd
->vdev_stat
.vs_alloc
;
796 tvd
->vdev_stat
.vs_space
= svd
->vdev_stat
.vs_space
;
797 tvd
->vdev_stat
.vs_dspace
= svd
->vdev_stat
.vs_dspace
;
799 svd
->vdev_stat
.vs_alloc
= 0;
800 svd
->vdev_stat
.vs_space
= 0;
801 svd
->vdev_stat
.vs_dspace
= 0;
803 for (t
= 0; t
< TXG_SIZE
; t
++) {
804 while ((msp
= txg_list_remove(&svd
->vdev_ms_list
, t
)) != NULL
)
805 (void) txg_list_add(&tvd
->vdev_ms_list
, msp
, t
);
806 while ((vd
= txg_list_remove(&svd
->vdev_dtl_list
, t
)) != NULL
)
807 (void) txg_list_add(&tvd
->vdev_dtl_list
, vd
, t
);
808 if (txg_list_remove_this(&spa
->spa_vdev_txg_list
, svd
, t
))
809 (void) txg_list_add(&spa
->spa_vdev_txg_list
, tvd
, t
);
812 if (list_link_active(&svd
->vdev_config_dirty_node
)) {
813 vdev_config_clean(svd
);
814 vdev_config_dirty(tvd
);
817 if (list_link_active(&svd
->vdev_state_dirty_node
)) {
818 vdev_state_clean(svd
);
819 vdev_state_dirty(tvd
);
822 tvd
->vdev_deflate_ratio
= svd
->vdev_deflate_ratio
;
823 svd
->vdev_deflate_ratio
= 0;
825 tvd
->vdev_islog
= svd
->vdev_islog
;
828 dsl_scan_io_queue_vdev_xfer(svd
, tvd
);
832 vdev_top_update(vdev_t
*tvd
, vdev_t
*vd
)
839 for (int c
= 0; c
< vd
->vdev_children
; c
++)
840 vdev_top_update(tvd
, vd
->vdev_child
[c
]);
844 * Add a mirror/replacing vdev above an existing vdev.
847 vdev_add_parent(vdev_t
*cvd
, vdev_ops_t
*ops
)
849 spa_t
*spa
= cvd
->vdev_spa
;
850 vdev_t
*pvd
= cvd
->vdev_parent
;
853 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
855 mvd
= vdev_alloc_common(spa
, cvd
->vdev_id
, 0, ops
);
857 mvd
->vdev_asize
= cvd
->vdev_asize
;
858 mvd
->vdev_min_asize
= cvd
->vdev_min_asize
;
859 mvd
->vdev_max_asize
= cvd
->vdev_max_asize
;
860 mvd
->vdev_ashift
= cvd
->vdev_ashift
;
861 mvd
->vdev_state
= cvd
->vdev_state
;
862 mvd
->vdev_crtxg
= cvd
->vdev_crtxg
;
864 vdev_remove_child(pvd
, cvd
);
865 vdev_add_child(pvd
, mvd
);
866 cvd
->vdev_id
= mvd
->vdev_children
;
867 vdev_add_child(mvd
, cvd
);
868 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
870 if (mvd
== mvd
->vdev_top
)
871 vdev_top_transfer(cvd
, mvd
);
877 * Remove a 1-way mirror/replacing vdev from the tree.
880 vdev_remove_parent(vdev_t
*cvd
)
882 vdev_t
*mvd
= cvd
->vdev_parent
;
883 vdev_t
*pvd
= mvd
->vdev_parent
;
885 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
887 ASSERT(mvd
->vdev_children
== 1);
888 ASSERT(mvd
->vdev_ops
== &vdev_mirror_ops
||
889 mvd
->vdev_ops
== &vdev_replacing_ops
||
890 mvd
->vdev_ops
== &vdev_spare_ops
);
891 cvd
->vdev_ashift
= mvd
->vdev_ashift
;
893 vdev_remove_child(mvd
, cvd
);
894 vdev_remove_child(pvd
, mvd
);
897 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
898 * Otherwise, we could have detached an offline device, and when we
899 * go to import the pool we'll think we have two top-level vdevs,
900 * instead of a different version of the same top-level vdev.
902 if (mvd
->vdev_top
== mvd
) {
903 uint64_t guid_delta
= mvd
->vdev_guid
- cvd
->vdev_guid
;
904 cvd
->vdev_orig_guid
= cvd
->vdev_guid
;
905 cvd
->vdev_guid
+= guid_delta
;
906 cvd
->vdev_guid_sum
+= guid_delta
;
909 * If pool not set for autoexpand, we need to also preserve
910 * mvd's asize to prevent automatic expansion of cvd.
911 * Otherwise if we are adjusting the mirror by attaching and
912 * detaching children of non-uniform sizes, the mirror could
913 * autoexpand, unexpectedly requiring larger devices to
914 * re-establish the mirror.
916 if (!cvd
->vdev_spa
->spa_autoexpand
)
917 cvd
->vdev_asize
= mvd
->vdev_asize
;
919 cvd
->vdev_id
= mvd
->vdev_id
;
920 vdev_add_child(pvd
, cvd
);
921 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
923 if (cvd
== cvd
->vdev_top
)
924 vdev_top_transfer(mvd
, cvd
);
926 ASSERT(mvd
->vdev_children
== 0);
931 vdev_metaslab_init(vdev_t
*vd
, uint64_t txg
)
933 spa_t
*spa
= vd
->vdev_spa
;
934 objset_t
*mos
= spa
->spa_meta_objset
;
936 uint64_t oldc
= vd
->vdev_ms_count
;
937 uint64_t newc
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
941 ASSERT(txg
== 0 || spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
944 * This vdev is not being allocated from yet or is a hole.
946 if (vd
->vdev_ms_shift
== 0)
949 ASSERT(!vd
->vdev_ishole
);
952 * Compute the raidz-deflation ratio. Note, we hard-code
953 * in 128k (1 << 17) because it is the "typical" blocksize.
954 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
955 * otherwise it would inconsistently account for existing bp's.
957 vd
->vdev_deflate_ratio
= (1 << 17) /
958 (vdev_psize_to_asize(vd
, 1 << 17) >> SPA_MINBLOCKSHIFT
);
960 ASSERT(oldc
<= newc
);
962 mspp
= vmem_zalloc(newc
* sizeof (*mspp
), KM_SLEEP
);
965 bcopy(vd
->vdev_ms
, mspp
, oldc
* sizeof (*mspp
));
966 vmem_free(vd
->vdev_ms
, oldc
* sizeof (*mspp
));
970 vd
->vdev_ms_count
= newc
;
972 for (m
= oldc
; m
< newc
; m
++) {
976 error
= dmu_read(mos
, vd
->vdev_ms_array
,
977 m
* sizeof (uint64_t), sizeof (uint64_t), &object
,
983 error
= metaslab_init(vd
->vdev_mg
, m
, object
, txg
,
990 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_WRITER
);
993 * If the vdev is being removed we don't activate
994 * the metaslabs since we want to ensure that no new
995 * allocations are performed on this device.
997 if (oldc
== 0 && !vd
->vdev_removing
)
998 metaslab_group_activate(vd
->vdev_mg
);
1001 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
1007 vdev_metaslab_fini(vdev_t
*vd
)
1010 uint64_t count
= vd
->vdev_ms_count
;
1012 if (vd
->vdev_ms
!= NULL
) {
1013 metaslab_group_passivate(vd
->vdev_mg
);
1014 for (m
= 0; m
< count
; m
++) {
1015 metaslab_t
*msp
= vd
->vdev_ms
[m
];
1020 vmem_free(vd
->vdev_ms
, count
* sizeof (metaslab_t
*));
1024 ASSERT3U(vd
->vdev_pending_fastwrite
, ==, 0);
1027 typedef struct vdev_probe_stats
{
1028 boolean_t vps_readable
;
1029 boolean_t vps_writeable
;
1031 } vdev_probe_stats_t
;
1034 vdev_probe_done(zio_t
*zio
)
1036 spa_t
*spa
= zio
->io_spa
;
1037 vdev_t
*vd
= zio
->io_vd
;
1038 vdev_probe_stats_t
*vps
= zio
->io_private
;
1040 ASSERT(vd
->vdev_probe_zio
!= NULL
);
1042 if (zio
->io_type
== ZIO_TYPE_READ
) {
1043 if (zio
->io_error
== 0)
1044 vps
->vps_readable
= 1;
1045 if (zio
->io_error
== 0 && spa_writeable(spa
)) {
1046 zio_nowait(zio_write_phys(vd
->vdev_probe_zio
, vd
,
1047 zio
->io_offset
, zio
->io_size
, zio
->io_abd
,
1048 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1049 ZIO_PRIORITY_SYNC_WRITE
, vps
->vps_flags
, B_TRUE
));
1051 abd_free(zio
->io_abd
);
1053 } else if (zio
->io_type
== ZIO_TYPE_WRITE
) {
1054 if (zio
->io_error
== 0)
1055 vps
->vps_writeable
= 1;
1056 abd_free(zio
->io_abd
);
1057 } else if (zio
->io_type
== ZIO_TYPE_NULL
) {
1061 vd
->vdev_cant_read
|= !vps
->vps_readable
;
1062 vd
->vdev_cant_write
|= !vps
->vps_writeable
;
1064 if (vdev_readable(vd
) &&
1065 (vdev_writeable(vd
) || !spa_writeable(spa
))) {
1068 ASSERT(zio
->io_error
!= 0);
1069 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE
,
1070 spa
, vd
, NULL
, NULL
, 0, 0);
1071 zio
->io_error
= SET_ERROR(ENXIO
);
1074 mutex_enter(&vd
->vdev_probe_lock
);
1075 ASSERT(vd
->vdev_probe_zio
== zio
);
1076 vd
->vdev_probe_zio
= NULL
;
1077 mutex_exit(&vd
->vdev_probe_lock
);
1080 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
)
1081 if (!vdev_accessible(vd
, pio
))
1082 pio
->io_error
= SET_ERROR(ENXIO
);
1084 kmem_free(vps
, sizeof (*vps
));
1089 * Determine whether this device is accessible.
1091 * Read and write to several known locations: the pad regions of each
1092 * vdev label but the first, which we leave alone in case it contains
1096 vdev_probe(vdev_t
*vd
, zio_t
*zio
)
1098 spa_t
*spa
= vd
->vdev_spa
;
1099 vdev_probe_stats_t
*vps
= NULL
;
1102 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1105 * Don't probe the probe.
1107 if (zio
&& (zio
->io_flags
& ZIO_FLAG_PROBE
))
1111 * To prevent 'probe storms' when a device fails, we create
1112 * just one probe i/o at a time. All zios that want to probe
1113 * this vdev will become parents of the probe io.
1115 mutex_enter(&vd
->vdev_probe_lock
);
1117 if ((pio
= vd
->vdev_probe_zio
) == NULL
) {
1118 vps
= kmem_zalloc(sizeof (*vps
), KM_SLEEP
);
1120 vps
->vps_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_PROBE
|
1121 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_AGGREGATE
|
1124 if (spa_config_held(spa
, SCL_ZIO
, RW_WRITER
)) {
1126 * vdev_cant_read and vdev_cant_write can only
1127 * transition from TRUE to FALSE when we have the
1128 * SCL_ZIO lock as writer; otherwise they can only
1129 * transition from FALSE to TRUE. This ensures that
1130 * any zio looking at these values can assume that
1131 * failures persist for the life of the I/O. That's
1132 * important because when a device has intermittent
1133 * connectivity problems, we want to ensure that
1134 * they're ascribed to the device (ENXIO) and not
1137 * Since we hold SCL_ZIO as writer here, clear both
1138 * values so the probe can reevaluate from first
1141 vps
->vps_flags
|= ZIO_FLAG_CONFIG_WRITER
;
1142 vd
->vdev_cant_read
= B_FALSE
;
1143 vd
->vdev_cant_write
= B_FALSE
;
1146 vd
->vdev_probe_zio
= pio
= zio_null(NULL
, spa
, vd
,
1147 vdev_probe_done
, vps
,
1148 vps
->vps_flags
| ZIO_FLAG_DONT_PROPAGATE
);
1151 * We can't change the vdev state in this context, so we
1152 * kick off an async task to do it on our behalf.
1155 vd
->vdev_probe_wanted
= B_TRUE
;
1156 spa_async_request(spa
, SPA_ASYNC_PROBE
);
1161 zio_add_child(zio
, pio
);
1163 mutex_exit(&vd
->vdev_probe_lock
);
1166 ASSERT(zio
!= NULL
);
1170 for (int l
= 1; l
< VDEV_LABELS
; l
++) {
1171 zio_nowait(zio_read_phys(pio
, vd
,
1172 vdev_label_offset(vd
->vdev_psize
, l
,
1173 offsetof(vdev_label_t
, vl_pad2
)), VDEV_PAD_SIZE
,
1174 abd_alloc_for_io(VDEV_PAD_SIZE
, B_TRUE
),
1175 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1176 ZIO_PRIORITY_SYNC_READ
, vps
->vps_flags
, B_TRUE
));
1187 vdev_open_child(void *arg
)
1191 vd
->vdev_open_thread
= curthread
;
1192 vd
->vdev_open_error
= vdev_open(vd
);
1193 vd
->vdev_open_thread
= NULL
;
1197 vdev_uses_zvols(vdev_t
*vd
)
1200 if (zvol_is_zvol(vd
->vdev_path
))
1204 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1205 if (vdev_uses_zvols(vd
->vdev_child
[c
]))
1212 vdev_open_children(vdev_t
*vd
)
1215 int children
= vd
->vdev_children
;
1218 * in order to handle pools on top of zvols, do the opens
1219 * in a single thread so that the same thread holds the
1220 * spa_namespace_lock
1222 if (vdev_uses_zvols(vd
)) {
1224 for (int c
= 0; c
< children
; c
++)
1225 vd
->vdev_child
[c
]->vdev_open_error
=
1226 vdev_open(vd
->vdev_child
[c
]);
1228 tq
= taskq_create("vdev_open", children
, minclsyspri
,
1229 children
, children
, TASKQ_PREPOPULATE
);
1233 for (int c
= 0; c
< children
; c
++)
1234 VERIFY(taskq_dispatch(tq
, vdev_open_child
,
1235 vd
->vdev_child
[c
], TQ_SLEEP
) != TASKQID_INVALID
);
1240 vd
->vdev_nonrot
= B_TRUE
;
1242 for (int c
= 0; c
< children
; c
++)
1243 vd
->vdev_nonrot
&= vd
->vdev_child
[c
]->vdev_nonrot
;
1247 * Prepare a virtual device for access.
1250 vdev_open(vdev_t
*vd
)
1252 spa_t
*spa
= vd
->vdev_spa
;
1255 uint64_t max_osize
= 0;
1256 uint64_t asize
, max_asize
, psize
;
1257 uint64_t ashift
= 0;
1259 ASSERT(vd
->vdev_open_thread
== curthread
||
1260 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1261 ASSERT(vd
->vdev_state
== VDEV_STATE_CLOSED
||
1262 vd
->vdev_state
== VDEV_STATE_CANT_OPEN
||
1263 vd
->vdev_state
== VDEV_STATE_OFFLINE
);
1265 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1266 vd
->vdev_cant_read
= B_FALSE
;
1267 vd
->vdev_cant_write
= B_FALSE
;
1268 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
1271 * If this vdev is not removed, check its fault status. If it's
1272 * faulted, bail out of the open.
1274 if (!vd
->vdev_removed
&& vd
->vdev_faulted
) {
1275 ASSERT(vd
->vdev_children
== 0);
1276 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1277 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1278 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1279 vd
->vdev_label_aux
);
1280 return (SET_ERROR(ENXIO
));
1281 } else if (vd
->vdev_offline
) {
1282 ASSERT(vd
->vdev_children
== 0);
1283 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
, VDEV_AUX_NONE
);
1284 return (SET_ERROR(ENXIO
));
1287 error
= vd
->vdev_ops
->vdev_op_open(vd
, &osize
, &max_osize
, &ashift
);
1290 * Reset the vdev_reopening flag so that we actually close
1291 * the vdev on error.
1293 vd
->vdev_reopening
= B_FALSE
;
1294 if (zio_injection_enabled
&& error
== 0)
1295 error
= zio_handle_device_injection(vd
, NULL
, ENXIO
);
1298 if (vd
->vdev_removed
&&
1299 vd
->vdev_stat
.vs_aux
!= VDEV_AUX_OPEN_FAILED
)
1300 vd
->vdev_removed
= B_FALSE
;
1302 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1303 vd
->vdev_stat
.vs_aux
);
1307 vd
->vdev_removed
= B_FALSE
;
1310 * Recheck the faulted flag now that we have confirmed that
1311 * the vdev is accessible. If we're faulted, bail.
1313 if (vd
->vdev_faulted
) {
1314 ASSERT(vd
->vdev_children
== 0);
1315 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1316 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1317 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1318 vd
->vdev_label_aux
);
1319 return (SET_ERROR(ENXIO
));
1322 if (vd
->vdev_degraded
) {
1323 ASSERT(vd
->vdev_children
== 0);
1324 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1325 VDEV_AUX_ERR_EXCEEDED
);
1327 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_HEALTHY
, 0);
1331 * For hole or missing vdevs we just return success.
1333 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
)
1336 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
1337 if (vd
->vdev_child
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
) {
1338 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1344 osize
= P2ALIGN(osize
, (uint64_t)sizeof (vdev_label_t
));
1345 max_osize
= P2ALIGN(max_osize
, (uint64_t)sizeof (vdev_label_t
));
1347 if (vd
->vdev_children
== 0) {
1348 if (osize
< SPA_MINDEVSIZE
) {
1349 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1350 VDEV_AUX_TOO_SMALL
);
1351 return (SET_ERROR(EOVERFLOW
));
1354 asize
= osize
- (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
);
1355 max_asize
= max_osize
- (VDEV_LABEL_START_SIZE
+
1356 VDEV_LABEL_END_SIZE
);
1358 if (vd
->vdev_parent
!= NULL
&& osize
< SPA_MINDEVSIZE
-
1359 (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
)) {
1360 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1361 VDEV_AUX_TOO_SMALL
);
1362 return (SET_ERROR(EOVERFLOW
));
1366 max_asize
= max_osize
;
1370 * If the vdev was expanded, record this so that we can re-create the
1371 * uberblock rings in labels {2,3}, during the next sync.
1373 if ((psize
> vd
->vdev_psize
) && (vd
->vdev_psize
!= 0))
1374 vd
->vdev_copy_uberblocks
= B_TRUE
;
1376 vd
->vdev_psize
= psize
;
1379 * Make sure the allocatable size hasn't shrunk too much.
1381 if (asize
< vd
->vdev_min_asize
) {
1382 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1383 VDEV_AUX_BAD_LABEL
);
1384 return (SET_ERROR(EINVAL
));
1387 if (vd
->vdev_asize
== 0) {
1389 * This is the first-ever open, so use the computed values.
1390 * For compatibility, a different ashift can be requested.
1392 vd
->vdev_asize
= asize
;
1393 vd
->vdev_max_asize
= max_asize
;
1394 if (vd
->vdev_ashift
== 0) {
1395 vd
->vdev_ashift
= ashift
; /* use detected value */
1397 if (vd
->vdev_ashift
!= 0 && (vd
->vdev_ashift
< ASHIFT_MIN
||
1398 vd
->vdev_ashift
> ASHIFT_MAX
)) {
1399 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1400 VDEV_AUX_BAD_ASHIFT
);
1401 return (SET_ERROR(EDOM
));
1405 * Detect if the alignment requirement has increased.
1406 * We don't want to make the pool unavailable, just
1407 * post an event instead.
1409 if (ashift
> vd
->vdev_top
->vdev_ashift
&&
1410 vd
->vdev_ops
->vdev_op_leaf
) {
1411 zfs_ereport_post(FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
,
1412 spa
, vd
, NULL
, NULL
, 0, 0);
1415 vd
->vdev_max_asize
= max_asize
;
1419 * If all children are healthy we update asize if either:
1420 * The asize has increased, due to a device expansion caused by dynamic
1421 * LUN growth or vdev replacement, and automatic expansion is enabled;
1422 * making the additional space available.
1424 * The asize has decreased, due to a device shrink usually caused by a
1425 * vdev replace with a smaller device. This ensures that calculations
1426 * based of max_asize and asize e.g. esize are always valid. It's safe
1427 * to do this as we've already validated that asize is greater than
1430 if (vd
->vdev_state
== VDEV_STATE_HEALTHY
&&
1431 ((asize
> vd
->vdev_asize
&&
1432 (vd
->vdev_expanding
|| spa
->spa_autoexpand
)) ||
1433 (asize
< vd
->vdev_asize
)))
1434 vd
->vdev_asize
= asize
;
1436 vdev_set_min_asize(vd
);
1439 * Ensure we can issue some IO before declaring the
1440 * vdev open for business.
1442 if (vd
->vdev_ops
->vdev_op_leaf
&&
1443 (error
= zio_wait(vdev_probe(vd
, NULL
))) != 0) {
1444 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1445 VDEV_AUX_ERR_EXCEEDED
);
1450 * Track the min and max ashift values for normal data devices.
1452 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
1453 !vd
->vdev_islog
&& vd
->vdev_aux
== NULL
) {
1454 if (vd
->vdev_ashift
> spa
->spa_max_ashift
)
1455 spa
->spa_max_ashift
= vd
->vdev_ashift
;
1456 if (vd
->vdev_ashift
< spa
->spa_min_ashift
)
1457 spa
->spa_min_ashift
= vd
->vdev_ashift
;
1461 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1462 * resilver. But don't do this if we are doing a reopen for a scrub,
1463 * since this would just restart the scrub we are already doing.
1465 if (vd
->vdev_ops
->vdev_op_leaf
&& !spa
->spa_scrub_reopen
&&
1466 vdev_resilver_needed(vd
, NULL
, NULL
))
1467 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
1473 * Called once the vdevs are all opened, this routine validates the label
1474 * contents. This needs to be done before vdev_load() so that we don't
1475 * inadvertently do repair I/Os to the wrong device.
1477 * If 'strict' is false ignore the spa guid check. This is necessary because
1478 * if the machine crashed during a re-guid the new guid might have been written
1479 * to all of the vdev labels, but not the cached config. The strict check
1480 * will be performed when the pool is opened again using the mos config.
1482 * This function will only return failure if one of the vdevs indicates that it
1483 * has since been destroyed or exported. This is only possible if
1484 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1485 * will be updated but the function will return 0.
1488 vdev_validate(vdev_t
*vd
, boolean_t strict
)
1490 spa_t
*spa
= vd
->vdev_spa
;
1492 uint64_t guid
= 0, top_guid
;
1495 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1496 if (vdev_validate(vd
->vdev_child
[c
], strict
) != 0)
1497 return (SET_ERROR(EBADF
));
1500 * If the device has already failed, or was marked offline, don't do
1501 * any further validation. Otherwise, label I/O will fail and we will
1502 * overwrite the previous state.
1504 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_readable(vd
)) {
1505 uint64_t aux_guid
= 0;
1507 uint64_t txg
= spa_last_synced_txg(spa
) != 0 ?
1508 spa_last_synced_txg(spa
) : -1ULL;
1510 if ((label
= vdev_label_read_config(vd
, txg
)) == NULL
) {
1511 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1512 VDEV_AUX_BAD_LABEL
);
1517 * Determine if this vdev has been split off into another
1518 * pool. If so, then refuse to open it.
1520 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_SPLIT_GUID
,
1521 &aux_guid
) == 0 && aux_guid
== spa_guid(spa
)) {
1522 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1523 VDEV_AUX_SPLIT_POOL
);
1528 if (strict
&& (nvlist_lookup_uint64(label
,
1529 ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1530 guid
!= spa_guid(spa
))) {
1531 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1532 VDEV_AUX_CORRUPT_DATA
);
1537 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
)
1538 != 0 || nvlist_lookup_uint64(nvl
, ZPOOL_CONFIG_ORIG_GUID
,
1543 * If this vdev just became a top-level vdev because its
1544 * sibling was detached, it will have adopted the parent's
1545 * vdev guid -- but the label may or may not be on disk yet.
1546 * Fortunately, either version of the label will have the
1547 * same top guid, so if we're a top-level vdev, we can
1548 * safely compare to that instead.
1550 * If we split this vdev off instead, then we also check the
1551 * original pool's guid. We don't want to consider the vdev
1552 * corrupt if it is partway through a split operation.
1554 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
,
1556 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_TOP_GUID
,
1558 ((vd
->vdev_guid
!= guid
&& vd
->vdev_guid
!= aux_guid
) &&
1559 (vd
->vdev_guid
!= top_guid
|| vd
!= vd
->vdev_top
))) {
1560 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1561 VDEV_AUX_CORRUPT_DATA
);
1566 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
,
1568 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1569 VDEV_AUX_CORRUPT_DATA
);
1577 * If this is a verbatim import, no need to check the
1578 * state of the pool.
1580 if (!(spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) &&
1581 spa_load_state(spa
) == SPA_LOAD_OPEN
&&
1582 state
!= POOL_STATE_ACTIVE
)
1583 return (SET_ERROR(EBADF
));
1586 * If we were able to open and validate a vdev that was
1587 * previously marked permanently unavailable, clear that state
1590 if (vd
->vdev_not_present
)
1591 vd
->vdev_not_present
= 0;
1598 * Close a virtual device.
1601 vdev_close(vdev_t
*vd
)
1603 vdev_t
*pvd
= vd
->vdev_parent
;
1604 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
1606 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1609 * If our parent is reopening, then we are as well, unless we are
1612 if (pvd
!= NULL
&& pvd
->vdev_reopening
)
1613 vd
->vdev_reopening
= (pvd
->vdev_reopening
&& !vd
->vdev_offline
);
1615 vd
->vdev_ops
->vdev_op_close(vd
);
1617 vdev_cache_purge(vd
);
1620 * We record the previous state before we close it, so that if we are
1621 * doing a reopen(), we don't generate FMA ereports if we notice that
1622 * it's still faulted.
1624 vd
->vdev_prevstate
= vd
->vdev_state
;
1626 if (vd
->vdev_offline
)
1627 vd
->vdev_state
= VDEV_STATE_OFFLINE
;
1629 vd
->vdev_state
= VDEV_STATE_CLOSED
;
1630 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1634 vdev_hold(vdev_t
*vd
)
1636 spa_t
*spa
= vd
->vdev_spa
;
1638 ASSERT(spa_is_root(spa
));
1639 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1642 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1643 vdev_hold(vd
->vdev_child
[c
]);
1645 if (vd
->vdev_ops
->vdev_op_leaf
)
1646 vd
->vdev_ops
->vdev_op_hold(vd
);
1650 vdev_rele(vdev_t
*vd
)
1652 ASSERT(spa_is_root(vd
->vdev_spa
));
1653 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1654 vdev_rele(vd
->vdev_child
[c
]);
1656 if (vd
->vdev_ops
->vdev_op_leaf
)
1657 vd
->vdev_ops
->vdev_op_rele(vd
);
1661 * Reopen all interior vdevs and any unopened leaves. We don't actually
1662 * reopen leaf vdevs which had previously been opened as they might deadlock
1663 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
1664 * If the leaf has never been opened then open it, as usual.
1667 vdev_reopen(vdev_t
*vd
)
1669 spa_t
*spa
= vd
->vdev_spa
;
1671 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1673 /* set the reopening flag unless we're taking the vdev offline */
1674 vd
->vdev_reopening
= !vd
->vdev_offline
;
1676 (void) vdev_open(vd
);
1679 * Call vdev_validate() here to make sure we have the same device.
1680 * Otherwise, a device with an invalid label could be successfully
1681 * opened in response to vdev_reopen().
1684 (void) vdev_validate_aux(vd
);
1685 if (vdev_readable(vd
) && vdev_writeable(vd
) &&
1686 vd
->vdev_aux
== &spa
->spa_l2cache
&&
1687 !l2arc_vdev_present(vd
))
1688 l2arc_add_vdev(spa
, vd
);
1690 (void) vdev_validate(vd
, B_TRUE
);
1694 * Reassess parent vdev's health.
1696 vdev_propagate_state(vd
);
1700 vdev_create(vdev_t
*vd
, uint64_t txg
, boolean_t isreplacing
)
1705 * Normally, partial opens (e.g. of a mirror) are allowed.
1706 * For a create, however, we want to fail the request if
1707 * there are any components we can't open.
1709 error
= vdev_open(vd
);
1711 if (error
|| vd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
1713 return (error
? error
: ENXIO
);
1717 * Recursively load DTLs and initialize all labels.
1719 if ((error
= vdev_dtl_load(vd
)) != 0 ||
1720 (error
= vdev_label_init(vd
, txg
, isreplacing
?
1721 VDEV_LABEL_REPLACE
: VDEV_LABEL_CREATE
)) != 0) {
1730 vdev_metaslab_set_size(vdev_t
*vd
)
1733 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
1735 vd
->vdev_ms_shift
= highbit64(vd
->vdev_asize
/ metaslabs_per_vdev
);
1736 vd
->vdev_ms_shift
= MAX(vd
->vdev_ms_shift
, SPA_MAXBLOCKSHIFT
);
1740 vdev_dirty(vdev_t
*vd
, int flags
, void *arg
, uint64_t txg
)
1742 ASSERT(vd
== vd
->vdev_top
);
1743 ASSERT(!vd
->vdev_ishole
);
1744 ASSERT(ISP2(flags
));
1745 ASSERT(spa_writeable(vd
->vdev_spa
));
1747 if (flags
& VDD_METASLAB
)
1748 (void) txg_list_add(&vd
->vdev_ms_list
, arg
, txg
);
1750 if (flags
& VDD_DTL
)
1751 (void) txg_list_add(&vd
->vdev_dtl_list
, arg
, txg
);
1753 (void) txg_list_add(&vd
->vdev_spa
->spa_vdev_txg_list
, vd
, txg
);
1757 vdev_dirty_leaves(vdev_t
*vd
, int flags
, uint64_t txg
)
1759 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1760 vdev_dirty_leaves(vd
->vdev_child
[c
], flags
, txg
);
1762 if (vd
->vdev_ops
->vdev_op_leaf
)
1763 vdev_dirty(vd
->vdev_top
, flags
, vd
, txg
);
1769 * A vdev's DTL (dirty time log) is the set of transaction groups for which
1770 * the vdev has less than perfect replication. There are four kinds of DTL:
1772 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
1774 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
1776 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
1777 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
1778 * txgs that was scrubbed.
1780 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
1781 * persistent errors or just some device being offline.
1782 * Unlike the other three, the DTL_OUTAGE map is not generally
1783 * maintained; it's only computed when needed, typically to
1784 * determine whether a device can be detached.
1786 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
1787 * either has the data or it doesn't.
1789 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
1790 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
1791 * if any child is less than fully replicated, then so is its parent.
1792 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
1793 * comprising only those txgs which appear in 'maxfaults' or more children;
1794 * those are the txgs we don't have enough replication to read. For example,
1795 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
1796 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
1797 * two child DTL_MISSING maps.
1799 * It should be clear from the above that to compute the DTLs and outage maps
1800 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
1801 * Therefore, that is all we keep on disk. When loading the pool, or after
1802 * a configuration change, we generate all other DTLs from first principles.
1805 vdev_dtl_dirty(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
1807 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
1809 ASSERT(t
< DTL_TYPES
);
1810 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
1811 ASSERT(spa_writeable(vd
->vdev_spa
));
1813 mutex_enter(rt
->rt_lock
);
1814 if (!range_tree_contains(rt
, txg
, size
))
1815 range_tree_add(rt
, txg
, size
);
1816 mutex_exit(rt
->rt_lock
);
1820 vdev_dtl_contains(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
1822 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
1823 boolean_t dirty
= B_FALSE
;
1825 ASSERT(t
< DTL_TYPES
);
1826 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
1828 mutex_enter(rt
->rt_lock
);
1829 if (range_tree_space(rt
) != 0)
1830 dirty
= range_tree_contains(rt
, txg
, size
);
1831 mutex_exit(rt
->rt_lock
);
1837 vdev_dtl_empty(vdev_t
*vd
, vdev_dtl_type_t t
)
1839 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
1842 mutex_enter(rt
->rt_lock
);
1843 empty
= (range_tree_space(rt
) == 0);
1844 mutex_exit(rt
->rt_lock
);
1850 * Returns B_TRUE if vdev determines offset needs to be resilvered.
1853 vdev_dtl_need_resilver(vdev_t
*vd
, uint64_t offset
, size_t psize
)
1855 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
1857 if (vd
->vdev_ops
->vdev_op_need_resilver
== NULL
||
1858 vd
->vdev_ops
->vdev_op_leaf
)
1861 return (vd
->vdev_ops
->vdev_op_need_resilver(vd
, offset
, psize
));
1865 * Returns the lowest txg in the DTL range.
1868 vdev_dtl_min(vdev_t
*vd
)
1872 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
1873 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
1874 ASSERT0(vd
->vdev_children
);
1876 rs
= avl_first(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
1877 return (rs
->rs_start
- 1);
1881 * Returns the highest txg in the DTL.
1884 vdev_dtl_max(vdev_t
*vd
)
1888 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
1889 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
1890 ASSERT0(vd
->vdev_children
);
1892 rs
= avl_last(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
1893 return (rs
->rs_end
);
1897 * Determine if a resilvering vdev should remove any DTL entries from
1898 * its range. If the vdev was resilvering for the entire duration of the
1899 * scan then it should excise that range from its DTLs. Otherwise, this
1900 * vdev is considered partially resilvered and should leave its DTL
1901 * entries intact. The comment in vdev_dtl_reassess() describes how we
1905 vdev_dtl_should_excise(vdev_t
*vd
)
1907 spa_t
*spa
= vd
->vdev_spa
;
1908 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
1910 ASSERT0(scn
->scn_phys
.scn_errors
);
1911 ASSERT0(vd
->vdev_children
);
1913 if (vd
->vdev_state
< VDEV_STATE_DEGRADED
)
1916 if (vd
->vdev_resilver_txg
== 0 ||
1917 range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) == 0)
1921 * When a resilver is initiated the scan will assign the scn_max_txg
1922 * value to the highest txg value that exists in all DTLs. If this
1923 * device's max DTL is not part of this scan (i.e. it is not in
1924 * the range (scn_min_txg, scn_max_txg] then it is not eligible
1927 if (vdev_dtl_max(vd
) <= scn
->scn_phys
.scn_max_txg
) {
1928 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <=, vdev_dtl_min(vd
));
1929 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <, vd
->vdev_resilver_txg
);
1930 ASSERT3U(vd
->vdev_resilver_txg
, <=, scn
->scn_phys
.scn_max_txg
);
1937 * Reassess DTLs after a config change or scrub completion.
1940 vdev_dtl_reassess(vdev_t
*vd
, uint64_t txg
, uint64_t scrub_txg
, int scrub_done
)
1942 spa_t
*spa
= vd
->vdev_spa
;
1946 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1948 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1949 vdev_dtl_reassess(vd
->vdev_child
[c
], txg
,
1950 scrub_txg
, scrub_done
);
1952 if (vd
== spa
->spa_root_vdev
|| vd
->vdev_ishole
|| vd
->vdev_aux
)
1955 if (vd
->vdev_ops
->vdev_op_leaf
) {
1956 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
1958 mutex_enter(&vd
->vdev_dtl_lock
);
1961 * If we've completed a scan cleanly then determine
1962 * if this vdev should remove any DTLs. We only want to
1963 * excise regions on vdevs that were available during
1964 * the entire duration of this scan.
1966 if (scrub_txg
!= 0 &&
1967 (spa
->spa_scrub_started
||
1968 (scn
!= NULL
&& scn
->scn_phys
.scn_errors
== 0)) &&
1969 vdev_dtl_should_excise(vd
)) {
1971 * We completed a scrub up to scrub_txg. If we
1972 * did it without rebooting, then the scrub dtl
1973 * will be valid, so excise the old region and
1974 * fold in the scrub dtl. Otherwise, leave the
1975 * dtl as-is if there was an error.
1977 * There's little trick here: to excise the beginning
1978 * of the DTL_MISSING map, we put it into a reference
1979 * tree and then add a segment with refcnt -1 that
1980 * covers the range [0, scrub_txg). This means
1981 * that each txg in that range has refcnt -1 or 0.
1982 * We then add DTL_SCRUB with a refcnt of 2, so that
1983 * entries in the range [0, scrub_txg) will have a
1984 * positive refcnt -- either 1 or 2. We then convert
1985 * the reference tree into the new DTL_MISSING map.
1987 space_reftree_create(&reftree
);
1988 space_reftree_add_map(&reftree
,
1989 vd
->vdev_dtl
[DTL_MISSING
], 1);
1990 space_reftree_add_seg(&reftree
, 0, scrub_txg
, -1);
1991 space_reftree_add_map(&reftree
,
1992 vd
->vdev_dtl
[DTL_SCRUB
], 2);
1993 space_reftree_generate_map(&reftree
,
1994 vd
->vdev_dtl
[DTL_MISSING
], 1);
1995 space_reftree_destroy(&reftree
);
1997 range_tree_vacate(vd
->vdev_dtl
[DTL_PARTIAL
], NULL
, NULL
);
1998 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
1999 range_tree_add
, vd
->vdev_dtl
[DTL_PARTIAL
]);
2001 range_tree_vacate(vd
->vdev_dtl
[DTL_SCRUB
], NULL
, NULL
);
2002 range_tree_vacate(vd
->vdev_dtl
[DTL_OUTAGE
], NULL
, NULL
);
2003 if (!vdev_readable(vd
))
2004 range_tree_add(vd
->vdev_dtl
[DTL_OUTAGE
], 0, -1ULL);
2006 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2007 range_tree_add
, vd
->vdev_dtl
[DTL_OUTAGE
]);
2010 * If the vdev was resilvering and no longer has any
2011 * DTLs then reset its resilvering flag and dirty
2012 * the top level so that we persist the change.
2014 if (vd
->vdev_resilver_txg
!= 0 &&
2015 range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) == 0 &&
2016 range_tree_space(vd
->vdev_dtl
[DTL_OUTAGE
]) == 0) {
2017 vd
->vdev_resilver_txg
= 0;
2018 vdev_config_dirty(vd
->vdev_top
);
2021 mutex_exit(&vd
->vdev_dtl_lock
);
2024 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, txg
);
2028 mutex_enter(&vd
->vdev_dtl_lock
);
2029 for (int t
= 0; t
< DTL_TYPES
; t
++) {
2030 /* account for child's outage in parent's missing map */
2031 int s
= (t
== DTL_MISSING
) ? DTL_OUTAGE
: t
;
2033 continue; /* leaf vdevs only */
2034 if (t
== DTL_PARTIAL
)
2035 minref
= 1; /* i.e. non-zero */
2036 else if (vd
->vdev_nparity
!= 0)
2037 minref
= vd
->vdev_nparity
+ 1; /* RAID-Z */
2039 minref
= vd
->vdev_children
; /* any kind of mirror */
2040 space_reftree_create(&reftree
);
2041 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2042 vdev_t
*cvd
= vd
->vdev_child
[c
];
2043 mutex_enter(&cvd
->vdev_dtl_lock
);
2044 space_reftree_add_map(&reftree
, cvd
->vdev_dtl
[s
], 1);
2045 mutex_exit(&cvd
->vdev_dtl_lock
);
2047 space_reftree_generate_map(&reftree
, vd
->vdev_dtl
[t
], minref
);
2048 space_reftree_destroy(&reftree
);
2050 mutex_exit(&vd
->vdev_dtl_lock
);
2054 vdev_dtl_load(vdev_t
*vd
)
2056 spa_t
*spa
= vd
->vdev_spa
;
2057 objset_t
*mos
= spa
->spa_meta_objset
;
2060 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_dtl_object
!= 0) {
2061 ASSERT(!vd
->vdev_ishole
);
2063 error
= space_map_open(&vd
->vdev_dtl_sm
, mos
,
2064 vd
->vdev_dtl_object
, 0, -1ULL, 0, &vd
->vdev_dtl_lock
);
2067 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2069 mutex_enter(&vd
->vdev_dtl_lock
);
2072 * Now that we've opened the space_map we need to update
2075 space_map_update(vd
->vdev_dtl_sm
);
2077 error
= space_map_load(vd
->vdev_dtl_sm
,
2078 vd
->vdev_dtl
[DTL_MISSING
], SM_ALLOC
);
2079 mutex_exit(&vd
->vdev_dtl_lock
);
2084 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2085 error
= vdev_dtl_load(vd
->vdev_child
[c
]);
2094 vdev_destroy_unlink_zap(vdev_t
*vd
, uint64_t zapobj
, dmu_tx_t
*tx
)
2096 spa_t
*spa
= vd
->vdev_spa
;
2098 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zapobj
, tx
));
2099 VERIFY0(zap_remove_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2104 vdev_create_link_zap(vdev_t
*vd
, dmu_tx_t
*tx
)
2106 spa_t
*spa
= vd
->vdev_spa
;
2107 uint64_t zap
= zap_create(spa
->spa_meta_objset
, DMU_OTN_ZAP_METADATA
,
2108 DMU_OT_NONE
, 0, tx
);
2111 VERIFY0(zap_add_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2118 vdev_construct_zaps(vdev_t
*vd
, dmu_tx_t
*tx
)
2120 if (vd
->vdev_ops
!= &vdev_hole_ops
&&
2121 vd
->vdev_ops
!= &vdev_missing_ops
&&
2122 vd
->vdev_ops
!= &vdev_root_ops
&&
2123 !vd
->vdev_top
->vdev_removing
) {
2124 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_leaf_zap
== 0) {
2125 vd
->vdev_leaf_zap
= vdev_create_link_zap(vd
, tx
);
2127 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
== 0) {
2128 vd
->vdev_top_zap
= vdev_create_link_zap(vd
, tx
);
2131 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2132 vdev_construct_zaps(vd
->vdev_child
[i
], tx
);
2137 vdev_dtl_sync(vdev_t
*vd
, uint64_t txg
)
2139 spa_t
*spa
= vd
->vdev_spa
;
2140 range_tree_t
*rt
= vd
->vdev_dtl
[DTL_MISSING
];
2141 objset_t
*mos
= spa
->spa_meta_objset
;
2142 range_tree_t
*rtsync
;
2145 uint64_t object
= space_map_object(vd
->vdev_dtl_sm
);
2147 ASSERT(!vd
->vdev_ishole
);
2148 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2150 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2152 if (vd
->vdev_detached
|| vd
->vdev_top
->vdev_removing
) {
2153 mutex_enter(&vd
->vdev_dtl_lock
);
2154 space_map_free(vd
->vdev_dtl_sm
, tx
);
2155 space_map_close(vd
->vdev_dtl_sm
);
2156 vd
->vdev_dtl_sm
= NULL
;
2157 mutex_exit(&vd
->vdev_dtl_lock
);
2160 * We only destroy the leaf ZAP for detached leaves or for
2161 * removed log devices. Removed data devices handle leaf ZAP
2162 * cleanup later, once cancellation is no longer possible.
2164 if (vd
->vdev_leaf_zap
!= 0 && (vd
->vdev_detached
||
2165 vd
->vdev_top
->vdev_islog
)) {
2166 vdev_destroy_unlink_zap(vd
, vd
->vdev_leaf_zap
, tx
);
2167 vd
->vdev_leaf_zap
= 0;
2174 if (vd
->vdev_dtl_sm
== NULL
) {
2175 uint64_t new_object
;
2177 new_object
= space_map_alloc(mos
, tx
);
2178 VERIFY3U(new_object
, !=, 0);
2180 VERIFY0(space_map_open(&vd
->vdev_dtl_sm
, mos
, new_object
,
2181 0, -1ULL, 0, &vd
->vdev_dtl_lock
));
2182 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2185 mutex_init(&rtlock
, NULL
, MUTEX_DEFAULT
, NULL
);
2187 rtsync
= range_tree_create(NULL
, NULL
, &rtlock
);
2189 mutex_enter(&rtlock
);
2191 mutex_enter(&vd
->vdev_dtl_lock
);
2192 range_tree_walk(rt
, range_tree_add
, rtsync
);
2193 mutex_exit(&vd
->vdev_dtl_lock
);
2195 space_map_truncate(vd
->vdev_dtl_sm
, tx
);
2196 space_map_write(vd
->vdev_dtl_sm
, rtsync
, SM_ALLOC
, tx
);
2197 range_tree_vacate(rtsync
, NULL
, NULL
);
2199 range_tree_destroy(rtsync
);
2201 mutex_exit(&rtlock
);
2202 mutex_destroy(&rtlock
);
2205 * If the object for the space map has changed then dirty
2206 * the top level so that we update the config.
2208 if (object
!= space_map_object(vd
->vdev_dtl_sm
)) {
2209 zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, "
2210 "new object %llu", txg
, spa_name(spa
), object
,
2211 space_map_object(vd
->vdev_dtl_sm
));
2212 vdev_config_dirty(vd
->vdev_top
);
2217 mutex_enter(&vd
->vdev_dtl_lock
);
2218 space_map_update(vd
->vdev_dtl_sm
);
2219 mutex_exit(&vd
->vdev_dtl_lock
);
2223 * Determine whether the specified vdev can be offlined/detached/removed
2224 * without losing data.
2227 vdev_dtl_required(vdev_t
*vd
)
2229 spa_t
*spa
= vd
->vdev_spa
;
2230 vdev_t
*tvd
= vd
->vdev_top
;
2231 uint8_t cant_read
= vd
->vdev_cant_read
;
2234 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2236 if (vd
== spa
->spa_root_vdev
|| vd
== tvd
)
2240 * Temporarily mark the device as unreadable, and then determine
2241 * whether this results in any DTL outages in the top-level vdev.
2242 * If not, we can safely offline/detach/remove the device.
2244 vd
->vdev_cant_read
= B_TRUE
;
2245 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2246 required
= !vdev_dtl_empty(tvd
, DTL_OUTAGE
);
2247 vd
->vdev_cant_read
= cant_read
;
2248 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2250 if (!required
&& zio_injection_enabled
)
2251 required
= !!zio_handle_device_injection(vd
, NULL
, ECHILD
);
2257 * Determine if resilver is needed, and if so the txg range.
2260 vdev_resilver_needed(vdev_t
*vd
, uint64_t *minp
, uint64_t *maxp
)
2262 boolean_t needed
= B_FALSE
;
2263 uint64_t thismin
= UINT64_MAX
;
2264 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 (int 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
)
2300 * Recursively load all children.
2302 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2303 vdev_load(vd
->vdev_child
[c
]);
2306 * If this is a top-level vdev, initialize its metaslabs.
2308 if (vd
== vd
->vdev_top
&& !vd
->vdev_ishole
&&
2309 (vd
->vdev_ashift
== 0 || vd
->vdev_asize
== 0 ||
2310 vdev_metaslab_init(vd
, 0) != 0))
2311 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2312 VDEV_AUX_CORRUPT_DATA
);
2314 * If this is a leaf vdev, load its DTL.
2316 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_dtl_load(vd
) != 0)
2317 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2318 VDEV_AUX_CORRUPT_DATA
);
2322 * The special vdev case is used for hot spares and l2cache devices. Its
2323 * sole purpose it to set the vdev state for the associated vdev. To do this,
2324 * we make sure that we can open the underlying device, then try to read the
2325 * label, and make sure that the label is sane and that it hasn't been
2326 * repurposed to another pool.
2329 vdev_validate_aux(vdev_t
*vd
)
2332 uint64_t guid
, version
;
2335 if (!vdev_readable(vd
))
2338 if ((label
= vdev_label_read_config(vd
, -1ULL)) == NULL
) {
2339 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2340 VDEV_AUX_CORRUPT_DATA
);
2344 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_VERSION
, &version
) != 0 ||
2345 !SPA_VERSION_IS_SUPPORTED(version
) ||
2346 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0 ||
2347 guid
!= vd
->vdev_guid
||
2348 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
, &state
) != 0) {
2349 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2350 VDEV_AUX_CORRUPT_DATA
);
2356 * We don't actually check the pool state here. If it's in fact in
2357 * use by another pool, we update this fact on the fly when requested.
2364 vdev_remove(vdev_t
*vd
, uint64_t txg
)
2366 spa_t
*spa
= vd
->vdev_spa
;
2367 objset_t
*mos
= spa
->spa_meta_objset
;
2370 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
2371 ASSERT(vd
== vd
->vdev_top
);
2372 ASSERT3U(txg
, ==, spa_syncing_txg(spa
));
2374 if (vd
->vdev_ms
!= NULL
) {
2375 metaslab_group_t
*mg
= vd
->vdev_mg
;
2377 metaslab_group_histogram_verify(mg
);
2378 metaslab_class_histogram_verify(mg
->mg_class
);
2380 for (int m
= 0; m
< vd
->vdev_ms_count
; m
++) {
2381 metaslab_t
*msp
= vd
->vdev_ms
[m
];
2383 if (msp
== NULL
|| msp
->ms_sm
== NULL
)
2386 mutex_enter(&msp
->ms_lock
);
2388 * If the metaslab was not loaded when the vdev
2389 * was removed then the histogram accounting may
2390 * not be accurate. Update the histogram information
2391 * here so that we ensure that the metaslab group
2392 * and metaslab class are up-to-date.
2394 metaslab_group_histogram_remove(mg
, msp
);
2396 VERIFY0(space_map_allocated(msp
->ms_sm
));
2397 space_map_free(msp
->ms_sm
, tx
);
2398 space_map_close(msp
->ms_sm
);
2400 mutex_exit(&msp
->ms_lock
);
2403 metaslab_group_histogram_verify(mg
);
2404 metaslab_class_histogram_verify(mg
->mg_class
);
2405 for (int i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++)
2406 ASSERT0(mg
->mg_histogram
[i
]);
2410 if (vd
->vdev_ms_array
) {
2411 (void) dmu_object_free(mos
, vd
->vdev_ms_array
, tx
);
2412 vd
->vdev_ms_array
= 0;
2415 if (vd
->vdev_islog
&& vd
->vdev_top_zap
!= 0) {
2416 vdev_destroy_unlink_zap(vd
, vd
->vdev_top_zap
, tx
);
2417 vd
->vdev_top_zap
= 0;
2423 vdev_sync_done(vdev_t
*vd
, uint64_t txg
)
2426 boolean_t reassess
= !txg_list_empty(&vd
->vdev_ms_list
, TXG_CLEAN(txg
));
2428 ASSERT(!vd
->vdev_ishole
);
2430 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, TXG_CLEAN(txg
))))
2431 metaslab_sync_done(msp
, txg
);
2434 metaslab_sync_reassess(vd
->vdev_mg
);
2438 vdev_sync(vdev_t
*vd
, uint64_t txg
)
2440 spa_t
*spa
= vd
->vdev_spa
;
2445 ASSERT(!vd
->vdev_ishole
);
2447 if (vd
->vdev_ms_array
== 0 && vd
->vdev_ms_shift
!= 0) {
2448 ASSERT(vd
== vd
->vdev_top
);
2449 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2450 vd
->vdev_ms_array
= dmu_object_alloc(spa
->spa_meta_objset
,
2451 DMU_OT_OBJECT_ARRAY
, 0, DMU_OT_NONE
, 0, tx
);
2452 ASSERT(vd
->vdev_ms_array
!= 0);
2453 vdev_config_dirty(vd
);
2458 * Remove the metadata associated with this vdev once it's empty.
2460 if (vd
->vdev_stat
.vs_alloc
== 0 && vd
->vdev_removing
)
2461 vdev_remove(vd
, txg
);
2463 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, txg
)) != NULL
) {
2464 metaslab_sync(msp
, txg
);
2465 (void) txg_list_add(&vd
->vdev_ms_list
, msp
, TXG_CLEAN(txg
));
2468 while ((lvd
= txg_list_remove(&vd
->vdev_dtl_list
, txg
)) != NULL
)
2469 vdev_dtl_sync(lvd
, txg
);
2471 (void) txg_list_add(&spa
->spa_vdev_txg_list
, vd
, TXG_CLEAN(txg
));
2475 vdev_psize_to_asize(vdev_t
*vd
, uint64_t psize
)
2477 return (vd
->vdev_ops
->vdev_op_asize(vd
, psize
));
2481 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
2482 * not be opened, and no I/O is attempted.
2485 vdev_fault(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
2489 spa_vdev_state_enter(spa
, SCL_NONE
);
2491 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2492 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2494 if (!vd
->vdev_ops
->vdev_op_leaf
)
2495 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2500 * If user did a 'zpool offline -f' then make the fault persist across
2503 if (aux
== VDEV_AUX_EXTERNAL_PERSIST
) {
2505 * There are two kinds of forced faults: temporary and
2506 * persistent. Temporary faults go away at pool import, while
2507 * persistent faults stay set. Both types of faults can be
2508 * cleared with a zpool clear.
2510 * We tell if a vdev is persistently faulted by looking at the
2511 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
2512 * import then it's a persistent fault. Otherwise, it's
2513 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
2514 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
2515 * tells vdev_config_generate() (which gets run later) to set
2516 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
2518 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
2519 vd
->vdev_tmpoffline
= B_FALSE
;
2520 aux
= VDEV_AUX_EXTERNAL
;
2522 vd
->vdev_tmpoffline
= B_TRUE
;
2526 * We don't directly use the aux state here, but if we do a
2527 * vdev_reopen(), we need this value to be present to remember why we
2530 vd
->vdev_label_aux
= aux
;
2533 * Faulted state takes precedence over degraded.
2535 vd
->vdev_delayed_close
= B_FALSE
;
2536 vd
->vdev_faulted
= 1ULL;
2537 vd
->vdev_degraded
= 0ULL;
2538 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_FAULTED
, aux
);
2541 * If this device has the only valid copy of the data, then
2542 * back off and simply mark the vdev as degraded instead.
2544 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&& vdev_dtl_required(vd
)) {
2545 vd
->vdev_degraded
= 1ULL;
2546 vd
->vdev_faulted
= 0ULL;
2549 * If we reopen the device and it's not dead, only then do we
2554 if (vdev_readable(vd
))
2555 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
, aux
);
2558 return (spa_vdev_state_exit(spa
, vd
, 0));
2562 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
2563 * user that something is wrong. The vdev continues to operate as normal as far
2564 * as I/O is concerned.
2567 vdev_degrade(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
2571 spa_vdev_state_enter(spa
, SCL_NONE
);
2573 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2574 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2576 if (!vd
->vdev_ops
->vdev_op_leaf
)
2577 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2580 * If the vdev is already faulted, then don't do anything.
2582 if (vd
->vdev_faulted
|| vd
->vdev_degraded
)
2583 return (spa_vdev_state_exit(spa
, NULL
, 0));
2585 vd
->vdev_degraded
= 1ULL;
2586 if (!vdev_is_dead(vd
))
2587 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
,
2590 return (spa_vdev_state_exit(spa
, vd
, 0));
2594 * Online the given vdev.
2596 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
2597 * spare device should be detached when the device finishes resilvering.
2598 * Second, the online should be treated like a 'test' online case, so no FMA
2599 * events are generated if the device fails to open.
2602 vdev_online(spa_t
*spa
, uint64_t guid
, uint64_t flags
, vdev_state_t
*newstate
)
2604 vdev_t
*vd
, *tvd
, *pvd
, *rvd
= spa
->spa_root_vdev
;
2605 boolean_t wasoffline
;
2606 vdev_state_t oldstate
;
2608 spa_vdev_state_enter(spa
, SCL_NONE
);
2610 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2611 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2613 if (!vd
->vdev_ops
->vdev_op_leaf
)
2614 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2616 wasoffline
= (vd
->vdev_offline
|| vd
->vdev_tmpoffline
);
2617 oldstate
= vd
->vdev_state
;
2620 vd
->vdev_offline
= B_FALSE
;
2621 vd
->vdev_tmpoffline
= B_FALSE
;
2622 vd
->vdev_checkremove
= !!(flags
& ZFS_ONLINE_CHECKREMOVE
);
2623 vd
->vdev_forcefault
= !!(flags
& ZFS_ONLINE_FORCEFAULT
);
2625 /* XXX - L2ARC 1.0 does not support expansion */
2626 if (!vd
->vdev_aux
) {
2627 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
2628 pvd
->vdev_expanding
= !!(flags
& ZFS_ONLINE_EXPAND
);
2632 vd
->vdev_checkremove
= vd
->vdev_forcefault
= B_FALSE
;
2634 if (!vd
->vdev_aux
) {
2635 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
2636 pvd
->vdev_expanding
= B_FALSE
;
2640 *newstate
= vd
->vdev_state
;
2641 if ((flags
& ZFS_ONLINE_UNSPARE
) &&
2642 !vdev_is_dead(vd
) && vd
->vdev_parent
&&
2643 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
2644 vd
->vdev_parent
->vdev_child
[0] == vd
)
2645 vd
->vdev_unspare
= B_TRUE
;
2647 if ((flags
& ZFS_ONLINE_EXPAND
) || spa
->spa_autoexpand
) {
2649 /* XXX - L2ARC 1.0 does not support expansion */
2651 return (spa_vdev_state_exit(spa
, vd
, ENOTSUP
));
2652 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2656 (oldstate
< VDEV_STATE_DEGRADED
&&
2657 vd
->vdev_state
>= VDEV_STATE_DEGRADED
))
2658 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_ONLINE
);
2660 return (spa_vdev_state_exit(spa
, vd
, 0));
2664 vdev_offline_locked(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
2668 uint64_t generation
;
2669 metaslab_group_t
*mg
;
2672 spa_vdev_state_enter(spa
, SCL_ALLOC
);
2674 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2675 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2677 if (!vd
->vdev_ops
->vdev_op_leaf
)
2678 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2682 generation
= spa
->spa_config_generation
+ 1;
2685 * If the device isn't already offline, try to offline it.
2687 if (!vd
->vdev_offline
) {
2689 * If this device has the only valid copy of some data,
2690 * don't allow it to be offlined. Log devices are always
2693 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
2694 vdev_dtl_required(vd
))
2695 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
2698 * If the top-level is a slog and it has had allocations
2699 * then proceed. We check that the vdev's metaslab group
2700 * is not NULL since it's possible that we may have just
2701 * added this vdev but not yet initialized its metaslabs.
2703 if (tvd
->vdev_islog
&& mg
!= NULL
) {
2705 * Prevent any future allocations.
2707 metaslab_group_passivate(mg
);
2708 (void) spa_vdev_state_exit(spa
, vd
, 0);
2710 error
= spa_offline_log(spa
);
2712 spa_vdev_state_enter(spa
, SCL_ALLOC
);
2715 * Check to see if the config has changed.
2717 if (error
|| generation
!= spa
->spa_config_generation
) {
2718 metaslab_group_activate(mg
);
2720 return (spa_vdev_state_exit(spa
,
2722 (void) spa_vdev_state_exit(spa
, vd
, 0);
2725 ASSERT0(tvd
->vdev_stat
.vs_alloc
);
2729 * Offline this device and reopen its top-level vdev.
2730 * If the top-level vdev is a log device then just offline
2731 * it. Otherwise, if this action results in the top-level
2732 * vdev becoming unusable, undo it and fail the request.
2734 vd
->vdev_offline
= B_TRUE
;
2737 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
2738 vdev_is_dead(tvd
)) {
2739 vd
->vdev_offline
= B_FALSE
;
2741 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
2745 * Add the device back into the metaslab rotor so that
2746 * once we online the device it's open for business.
2748 if (tvd
->vdev_islog
&& mg
!= NULL
)
2749 metaslab_group_activate(mg
);
2752 vd
->vdev_tmpoffline
= !!(flags
& ZFS_OFFLINE_TEMPORARY
);
2754 return (spa_vdev_state_exit(spa
, vd
, 0));
2758 vdev_offline(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
2762 mutex_enter(&spa
->spa_vdev_top_lock
);
2763 error
= vdev_offline_locked(spa
, guid
, flags
);
2764 mutex_exit(&spa
->spa_vdev_top_lock
);
2770 * Clear the error counts associated with this vdev. Unlike vdev_online() and
2771 * vdev_offline(), we assume the spa config is locked. We also clear all
2772 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
2775 vdev_clear(spa_t
*spa
, vdev_t
*vd
)
2777 vdev_t
*rvd
= spa
->spa_root_vdev
;
2779 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2784 vd
->vdev_stat
.vs_read_errors
= 0;
2785 vd
->vdev_stat
.vs_write_errors
= 0;
2786 vd
->vdev_stat
.vs_checksum_errors
= 0;
2788 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2789 vdev_clear(spa
, vd
->vdev_child
[c
]);
2792 * If we're in the FAULTED state or have experienced failed I/O, then
2793 * clear the persistent state and attempt to reopen the device. We
2794 * also mark the vdev config dirty, so that the new faulted state is
2795 * written out to disk.
2797 if (vd
->vdev_faulted
|| vd
->vdev_degraded
||
2798 !vdev_readable(vd
) || !vdev_writeable(vd
)) {
2800 * When reopening in response to a clear event, it may be due to
2801 * a fmadm repair request. In this case, if the device is
2802 * still broken, we want to still post the ereport again.
2804 vd
->vdev_forcefault
= B_TRUE
;
2806 vd
->vdev_faulted
= vd
->vdev_degraded
= 0ULL;
2807 vd
->vdev_cant_read
= B_FALSE
;
2808 vd
->vdev_cant_write
= B_FALSE
;
2809 vd
->vdev_stat
.vs_aux
= 0;
2811 vdev_reopen(vd
== rvd
? rvd
: vd
->vdev_top
);
2813 vd
->vdev_forcefault
= B_FALSE
;
2815 if (vd
!= rvd
&& vdev_writeable(vd
->vdev_top
))
2816 vdev_state_dirty(vd
->vdev_top
);
2818 if (vd
->vdev_aux
== NULL
&& !vdev_is_dead(vd
))
2819 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2821 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_CLEAR
);
2825 * When clearing a FMA-diagnosed fault, we always want to
2826 * unspare the device, as we assume that the original spare was
2827 * done in response to the FMA fault.
2829 if (!vdev_is_dead(vd
) && vd
->vdev_parent
!= NULL
&&
2830 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
2831 vd
->vdev_parent
->vdev_child
[0] == vd
)
2832 vd
->vdev_unspare
= B_TRUE
;
2836 vdev_is_dead(vdev_t
*vd
)
2839 * Holes and missing devices are always considered "dead".
2840 * This simplifies the code since we don't have to check for
2841 * these types of devices in the various code paths.
2842 * Instead we rely on the fact that we skip over dead devices
2843 * before issuing I/O to them.
2845 return (vd
->vdev_state
< VDEV_STATE_DEGRADED
|| vd
->vdev_ishole
||
2846 vd
->vdev_ops
== &vdev_missing_ops
);
2850 vdev_readable(vdev_t
*vd
)
2852 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_read
);
2856 vdev_writeable(vdev_t
*vd
)
2858 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_write
);
2862 vdev_allocatable(vdev_t
*vd
)
2864 uint64_t state
= vd
->vdev_state
;
2867 * We currently allow allocations from vdevs which may be in the
2868 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
2869 * fails to reopen then we'll catch it later when we're holding
2870 * the proper locks. Note that we have to get the vdev state
2871 * in a local variable because although it changes atomically,
2872 * we're asking two separate questions about it.
2874 return (!(state
< VDEV_STATE_DEGRADED
&& state
!= VDEV_STATE_CLOSED
) &&
2875 !vd
->vdev_cant_write
&& !vd
->vdev_ishole
&&
2876 vd
->vdev_mg
->mg_initialized
);
2880 vdev_accessible(vdev_t
*vd
, zio_t
*zio
)
2882 ASSERT(zio
->io_vd
== vd
);
2884 if (vdev_is_dead(vd
) || vd
->vdev_remove_wanted
)
2887 if (zio
->io_type
== ZIO_TYPE_READ
)
2888 return (!vd
->vdev_cant_read
);
2890 if (zio
->io_type
== ZIO_TYPE_WRITE
)
2891 return (!vd
->vdev_cant_write
);
2897 vdev_get_child_stat(vdev_t
*cvd
, vdev_stat_t
*vs
, vdev_stat_t
*cvs
)
2900 for (t
= 0; t
< ZIO_TYPES
; t
++) {
2901 vs
->vs_ops
[t
] += cvs
->vs_ops
[t
];
2902 vs
->vs_bytes
[t
] += cvs
->vs_bytes
[t
];
2905 cvs
->vs_scan_removing
= cvd
->vdev_removing
;
2909 * Get extended stats
2912 vdev_get_child_stat_ex(vdev_t
*cvd
, vdev_stat_ex_t
*vsx
, vdev_stat_ex_t
*cvsx
)
2915 for (t
= 0; t
< ZIO_TYPES
; t
++) {
2916 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_disk_histo
[0]); b
++)
2917 vsx
->vsx_disk_histo
[t
][b
] += cvsx
->vsx_disk_histo
[t
][b
];
2919 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_total_histo
[0]); b
++) {
2920 vsx
->vsx_total_histo
[t
][b
] +=
2921 cvsx
->vsx_total_histo
[t
][b
];
2925 for (t
= 0; t
< ZIO_PRIORITY_NUM_QUEUEABLE
; t
++) {
2926 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_queue_histo
[0]); b
++) {
2927 vsx
->vsx_queue_histo
[t
][b
] +=
2928 cvsx
->vsx_queue_histo
[t
][b
];
2930 vsx
->vsx_active_queue
[t
] += cvsx
->vsx_active_queue
[t
];
2931 vsx
->vsx_pend_queue
[t
] += cvsx
->vsx_pend_queue
[t
];
2933 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_ind_histo
[0]); b
++)
2934 vsx
->vsx_ind_histo
[t
][b
] += cvsx
->vsx_ind_histo
[t
][b
];
2936 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_agg_histo
[0]); b
++)
2937 vsx
->vsx_agg_histo
[t
][b
] += cvsx
->vsx_agg_histo
[t
][b
];
2943 * Get statistics for the given vdev.
2946 vdev_get_stats_ex_impl(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
2950 * If we're getting stats on the root vdev, aggregate the I/O counts
2951 * over all top-level vdevs (i.e. the direct children of the root).
2953 if (!vd
->vdev_ops
->vdev_op_leaf
) {
2955 memset(vs
->vs_ops
, 0, sizeof (vs
->vs_ops
));
2956 memset(vs
->vs_bytes
, 0, sizeof (vs
->vs_bytes
));
2959 memset(vsx
, 0, sizeof (*vsx
));
2961 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2962 vdev_t
*cvd
= vd
->vdev_child
[c
];
2963 vdev_stat_t
*cvs
= &cvd
->vdev_stat
;
2964 vdev_stat_ex_t
*cvsx
= &cvd
->vdev_stat_ex
;
2966 vdev_get_stats_ex_impl(cvd
, cvs
, cvsx
);
2968 vdev_get_child_stat(cvd
, vs
, cvs
);
2970 vdev_get_child_stat_ex(cvd
, vsx
, cvsx
);
2975 * We're a leaf. Just copy our ZIO active queue stats in. The
2976 * other leaf stats are updated in vdev_stat_update().
2981 memcpy(vsx
, &vd
->vdev_stat_ex
, sizeof (vd
->vdev_stat_ex
));
2983 for (t
= 0; t
< ARRAY_SIZE(vd
->vdev_queue
.vq_class
); t
++) {
2984 vsx
->vsx_active_queue
[t
] =
2985 vd
->vdev_queue
.vq_class
[t
].vqc_active
;
2986 vsx
->vsx_pend_queue
[t
] = avl_numnodes(
2987 &vd
->vdev_queue
.vq_class
[t
].vqc_queued_tree
);
2993 vdev_get_stats_ex(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
2995 vdev_t
*tvd
= vd
->vdev_top
;
2996 mutex_enter(&vd
->vdev_stat_lock
);
2998 bcopy(&vd
->vdev_stat
, vs
, sizeof (*vs
));
2999 vs
->vs_timestamp
= gethrtime() - vs
->vs_timestamp
;
3000 vs
->vs_state
= vd
->vdev_state
;
3001 vs
->vs_rsize
= vdev_get_min_asize(vd
);
3002 if (vd
->vdev_ops
->vdev_op_leaf
)
3003 vs
->vs_rsize
+= VDEV_LABEL_START_SIZE
+
3004 VDEV_LABEL_END_SIZE
;
3006 * Report expandable space on top-level, non-auxillary devices
3007 * only. The expandable space is reported in terms of metaslab
3008 * sized units since that determines how much space the pool
3011 if (vd
->vdev_aux
== NULL
&& tvd
!= NULL
) {
3012 vs
->vs_esize
= P2ALIGN(
3013 vd
->vdev_max_asize
- vd
->vdev_asize
,
3014 1ULL << tvd
->vdev_ms_shift
);
3016 vs
->vs_esize
= vd
->vdev_max_asize
- vd
->vdev_asize
;
3017 if (vd
->vdev_aux
== NULL
&& vd
== vd
->vdev_top
&&
3019 vs
->vs_fragmentation
= vd
->vdev_mg
->mg_fragmentation
;
3023 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_READER
) != 0);
3024 vdev_get_stats_ex_impl(vd
, vs
, vsx
);
3025 mutex_exit(&vd
->vdev_stat_lock
);
3029 vdev_get_stats(vdev_t
*vd
, vdev_stat_t
*vs
)
3031 return (vdev_get_stats_ex(vd
, vs
, NULL
));
3035 vdev_clear_stats(vdev_t
*vd
)
3037 mutex_enter(&vd
->vdev_stat_lock
);
3038 vd
->vdev_stat
.vs_space
= 0;
3039 vd
->vdev_stat
.vs_dspace
= 0;
3040 vd
->vdev_stat
.vs_alloc
= 0;
3041 mutex_exit(&vd
->vdev_stat_lock
);
3045 vdev_scan_stat_init(vdev_t
*vd
)
3047 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3049 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3050 vdev_scan_stat_init(vd
->vdev_child
[c
]);
3052 mutex_enter(&vd
->vdev_stat_lock
);
3053 vs
->vs_scan_processed
= 0;
3054 mutex_exit(&vd
->vdev_stat_lock
);
3058 vdev_stat_update(zio_t
*zio
, uint64_t psize
)
3060 spa_t
*spa
= zio
->io_spa
;
3061 vdev_t
*rvd
= spa
->spa_root_vdev
;
3062 vdev_t
*vd
= zio
->io_vd
? zio
->io_vd
: rvd
;
3064 uint64_t txg
= zio
->io_txg
;
3065 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3066 vdev_stat_ex_t
*vsx
= &vd
->vdev_stat_ex
;
3067 zio_type_t type
= zio
->io_type
;
3068 int flags
= zio
->io_flags
;
3071 * If this i/o is a gang leader, it didn't do any actual work.
3073 if (zio
->io_gang_tree
)
3076 if (zio
->io_error
== 0) {
3078 * If this is a root i/o, don't count it -- we've already
3079 * counted the top-level vdevs, and vdev_get_stats() will
3080 * aggregate them when asked. This reduces contention on
3081 * the root vdev_stat_lock and implicitly handles blocks
3082 * that compress away to holes, for which there is no i/o.
3083 * (Holes never create vdev children, so all the counters
3084 * remain zero, which is what we want.)
3086 * Note: this only applies to successful i/o (io_error == 0)
3087 * because unlike i/o counts, errors are not additive.
3088 * When reading a ditto block, for example, failure of
3089 * one top-level vdev does not imply a root-level error.
3094 ASSERT(vd
== zio
->io_vd
);
3096 if (flags
& ZIO_FLAG_IO_BYPASS
)
3099 mutex_enter(&vd
->vdev_stat_lock
);
3101 if (flags
& ZIO_FLAG_IO_REPAIR
) {
3102 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
3103 dsl_scan_phys_t
*scn_phys
=
3104 &spa
->spa_dsl_pool
->dp_scan
->scn_phys
;
3105 uint64_t *processed
= &scn_phys
->scn_processed
;
3108 if (vd
->vdev_ops
->vdev_op_leaf
)
3109 atomic_add_64(processed
, psize
);
3110 vs
->vs_scan_processed
+= psize
;
3113 if (flags
& ZIO_FLAG_SELF_HEAL
)
3114 vs
->vs_self_healed
+= psize
;
3118 * The bytes/ops/histograms are recorded at the leaf level and
3119 * aggregated into the higher level vdevs in vdev_get_stats().
3121 if (vd
->vdev_ops
->vdev_op_leaf
&&
3122 (zio
->io_priority
< ZIO_PRIORITY_NUM_QUEUEABLE
)) {
3125 vs
->vs_bytes
[type
] += psize
;
3127 if (flags
& ZIO_FLAG_DELEGATED
) {
3128 vsx
->vsx_agg_histo
[zio
->io_priority
]
3129 [RQ_HISTO(zio
->io_size
)]++;
3131 vsx
->vsx_ind_histo
[zio
->io_priority
]
3132 [RQ_HISTO(zio
->io_size
)]++;
3135 if (zio
->io_delta
&& zio
->io_delay
) {
3136 vsx
->vsx_queue_histo
[zio
->io_priority
]
3137 [L_HISTO(zio
->io_delta
- zio
->io_delay
)]++;
3138 vsx
->vsx_disk_histo
[type
]
3139 [L_HISTO(zio
->io_delay
)]++;
3140 vsx
->vsx_total_histo
[type
]
3141 [L_HISTO(zio
->io_delta
)]++;
3145 mutex_exit(&vd
->vdev_stat_lock
);
3149 if (flags
& ZIO_FLAG_SPECULATIVE
)
3153 * If this is an I/O error that is going to be retried, then ignore the
3154 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
3155 * hard errors, when in reality they can happen for any number of
3156 * innocuous reasons (bus resets, MPxIO link failure, etc).
3158 if (zio
->io_error
== EIO
&&
3159 !(zio
->io_flags
& ZIO_FLAG_IO_RETRY
))
3163 * Intent logs writes won't propagate their error to the root
3164 * I/O so don't mark these types of failures as pool-level
3167 if (zio
->io_vd
== NULL
&& (zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
3170 mutex_enter(&vd
->vdev_stat_lock
);
3171 if (type
== ZIO_TYPE_READ
&& !vdev_is_dead(vd
)) {
3172 if (zio
->io_error
== ECKSUM
)
3173 vs
->vs_checksum_errors
++;
3175 vs
->vs_read_errors
++;
3177 if (type
== ZIO_TYPE_WRITE
&& !vdev_is_dead(vd
))
3178 vs
->vs_write_errors
++;
3179 mutex_exit(&vd
->vdev_stat_lock
);
3181 if (type
== ZIO_TYPE_WRITE
&& txg
!= 0 &&
3182 (!(flags
& ZIO_FLAG_IO_REPAIR
) ||
3183 (flags
& ZIO_FLAG_SCAN_THREAD
) ||
3184 spa
->spa_claiming
)) {
3186 * This is either a normal write (not a repair), or it's
3187 * a repair induced by the scrub thread, or it's a repair
3188 * made by zil_claim() during spa_load() in the first txg.
3189 * In the normal case, we commit the DTL change in the same
3190 * txg as the block was born. In the scrub-induced repair
3191 * case, we know that scrubs run in first-pass syncing context,
3192 * so we commit the DTL change in spa_syncing_txg(spa).
3193 * In the zil_claim() case, we commit in spa_first_txg(spa).
3195 * We currently do not make DTL entries for failed spontaneous
3196 * self-healing writes triggered by normal (non-scrubbing)
3197 * reads, because we have no transactional context in which to
3198 * do so -- and it's not clear that it'd be desirable anyway.
3200 if (vd
->vdev_ops
->vdev_op_leaf
) {
3201 uint64_t commit_txg
= txg
;
3202 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
3203 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
3204 ASSERT(spa_sync_pass(spa
) == 1);
3205 vdev_dtl_dirty(vd
, DTL_SCRUB
, txg
, 1);
3206 commit_txg
= spa_syncing_txg(spa
);
3207 } else if (spa
->spa_claiming
) {
3208 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
3209 commit_txg
= spa_first_txg(spa
);
3211 ASSERT(commit_txg
>= spa_syncing_txg(spa
));
3212 if (vdev_dtl_contains(vd
, DTL_MISSING
, txg
, 1))
3214 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3215 vdev_dtl_dirty(pvd
, DTL_PARTIAL
, txg
, 1);
3216 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, commit_txg
);
3219 vdev_dtl_dirty(vd
, DTL_MISSING
, txg
, 1);
3224 * Update the in-core space usage stats for this vdev, its metaslab class,
3225 * and the root vdev.
3228 vdev_space_update(vdev_t
*vd
, int64_t alloc_delta
, int64_t defer_delta
,
3229 int64_t space_delta
)
3231 int64_t dspace_delta
= space_delta
;
3232 spa_t
*spa
= vd
->vdev_spa
;
3233 vdev_t
*rvd
= spa
->spa_root_vdev
;
3234 metaslab_group_t
*mg
= vd
->vdev_mg
;
3235 metaslab_class_t
*mc
= mg
? mg
->mg_class
: NULL
;
3237 ASSERT(vd
== vd
->vdev_top
);
3240 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
3241 * factor. We must calculate this here and not at the root vdev
3242 * because the root vdev's psize-to-asize is simply the max of its
3243 * childrens', thus not accurate enough for us.
3245 ASSERT((dspace_delta
& (SPA_MINBLOCKSIZE
-1)) == 0);
3246 ASSERT(vd
->vdev_deflate_ratio
!= 0 || vd
->vdev_isl2cache
);
3247 dspace_delta
= (dspace_delta
>> SPA_MINBLOCKSHIFT
) *
3248 vd
->vdev_deflate_ratio
;
3250 mutex_enter(&vd
->vdev_stat_lock
);
3251 vd
->vdev_stat
.vs_alloc
+= alloc_delta
;
3252 vd
->vdev_stat
.vs_space
+= space_delta
;
3253 vd
->vdev_stat
.vs_dspace
+= dspace_delta
;
3254 mutex_exit(&vd
->vdev_stat_lock
);
3256 if (mc
== spa_normal_class(spa
)) {
3257 mutex_enter(&rvd
->vdev_stat_lock
);
3258 rvd
->vdev_stat
.vs_alloc
+= alloc_delta
;
3259 rvd
->vdev_stat
.vs_space
+= space_delta
;
3260 rvd
->vdev_stat
.vs_dspace
+= dspace_delta
;
3261 mutex_exit(&rvd
->vdev_stat_lock
);
3265 ASSERT(rvd
== vd
->vdev_parent
);
3266 ASSERT(vd
->vdev_ms_count
!= 0);
3268 metaslab_class_space_update(mc
,
3269 alloc_delta
, defer_delta
, space_delta
, dspace_delta
);
3274 * Mark a top-level vdev's config as dirty, placing it on the dirty list
3275 * so that it will be written out next time the vdev configuration is synced.
3276 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3279 vdev_config_dirty(vdev_t
*vd
)
3281 spa_t
*spa
= vd
->vdev_spa
;
3282 vdev_t
*rvd
= spa
->spa_root_vdev
;
3285 ASSERT(spa_writeable(spa
));
3288 * If this is an aux vdev (as with l2cache and spare devices), then we
3289 * update the vdev config manually and set the sync flag.
3291 if (vd
->vdev_aux
!= NULL
) {
3292 spa_aux_vdev_t
*sav
= vd
->vdev_aux
;
3296 for (c
= 0; c
< sav
->sav_count
; c
++) {
3297 if (sav
->sav_vdevs
[c
] == vd
)
3301 if (c
== sav
->sav_count
) {
3303 * We're being removed. There's nothing more to do.
3305 ASSERT(sav
->sav_sync
== B_TRUE
);
3309 sav
->sav_sync
= B_TRUE
;
3311 if (nvlist_lookup_nvlist_array(sav
->sav_config
,
3312 ZPOOL_CONFIG_L2CACHE
, &aux
, &naux
) != 0) {
3313 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
3314 ZPOOL_CONFIG_SPARES
, &aux
, &naux
) == 0);
3320 * Setting the nvlist in the middle if the array is a little
3321 * sketchy, but it will work.
3323 nvlist_free(aux
[c
]);
3324 aux
[c
] = vdev_config_generate(spa
, vd
, B_TRUE
, 0);
3330 * The dirty list is protected by the SCL_CONFIG lock. The caller
3331 * must either hold SCL_CONFIG as writer, or must be the sync thread
3332 * (which holds SCL_CONFIG as reader). There's only one sync thread,
3333 * so this is sufficient to ensure mutual exclusion.
3335 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
3336 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3337 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
3340 for (c
= 0; c
< rvd
->vdev_children
; c
++)
3341 vdev_config_dirty(rvd
->vdev_child
[c
]);
3343 ASSERT(vd
== vd
->vdev_top
);
3345 if (!list_link_active(&vd
->vdev_config_dirty_node
) &&
3347 list_insert_head(&spa
->spa_config_dirty_list
, vd
);
3352 vdev_config_clean(vdev_t
*vd
)
3354 spa_t
*spa
= vd
->vdev_spa
;
3356 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
3357 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3358 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
3360 ASSERT(list_link_active(&vd
->vdev_config_dirty_node
));
3361 list_remove(&spa
->spa_config_dirty_list
, vd
);
3365 * Mark a top-level vdev's state as dirty, so that the next pass of
3366 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3367 * the state changes from larger config changes because they require
3368 * much less locking, and are often needed for administrative actions.
3371 vdev_state_dirty(vdev_t
*vd
)
3373 spa_t
*spa
= vd
->vdev_spa
;
3375 ASSERT(spa_writeable(spa
));
3376 ASSERT(vd
== vd
->vdev_top
);
3379 * The state list is protected by the SCL_STATE lock. The caller
3380 * must either hold SCL_STATE as writer, or must be the sync thread
3381 * (which holds SCL_STATE as reader). There's only one sync thread,
3382 * so this is sufficient to ensure mutual exclusion.
3384 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
3385 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3386 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
3388 if (!list_link_active(&vd
->vdev_state_dirty_node
) && !vd
->vdev_ishole
)
3389 list_insert_head(&spa
->spa_state_dirty_list
, vd
);
3393 vdev_state_clean(vdev_t
*vd
)
3395 spa_t
*spa
= vd
->vdev_spa
;
3397 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
3398 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3399 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
3401 ASSERT(list_link_active(&vd
->vdev_state_dirty_node
));
3402 list_remove(&spa
->spa_state_dirty_list
, vd
);
3406 * Propagate vdev state up from children to parent.
3409 vdev_propagate_state(vdev_t
*vd
)
3411 spa_t
*spa
= vd
->vdev_spa
;
3412 vdev_t
*rvd
= spa
->spa_root_vdev
;
3413 int degraded
= 0, faulted
= 0;
3417 if (vd
->vdev_children
> 0) {
3418 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3419 child
= vd
->vdev_child
[c
];
3422 * Don't factor holes into the decision.
3424 if (child
->vdev_ishole
)
3427 if (!vdev_readable(child
) ||
3428 (!vdev_writeable(child
) && spa_writeable(spa
))) {
3430 * Root special: if there is a top-level log
3431 * device, treat the root vdev as if it were
3434 if (child
->vdev_islog
&& vd
== rvd
)
3438 } else if (child
->vdev_state
<= VDEV_STATE_DEGRADED
) {
3442 if (child
->vdev_stat
.vs_aux
== VDEV_AUX_CORRUPT_DATA
)
3446 vd
->vdev_ops
->vdev_op_state_change(vd
, faulted
, degraded
);
3449 * Root special: if there is a top-level vdev that cannot be
3450 * opened due to corrupted metadata, then propagate the root
3451 * vdev's aux state as 'corrupt' rather than 'insufficient
3454 if (corrupted
&& vd
== rvd
&&
3455 rvd
->vdev_state
== VDEV_STATE_CANT_OPEN
)
3456 vdev_set_state(rvd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3457 VDEV_AUX_CORRUPT_DATA
);
3460 if (vd
->vdev_parent
)
3461 vdev_propagate_state(vd
->vdev_parent
);
3465 * Set a vdev's state. If this is during an open, we don't update the parent
3466 * state, because we're in the process of opening children depth-first.
3467 * Otherwise, we propagate the change to the parent.
3469 * If this routine places a device in a faulted state, an appropriate ereport is
3473 vdev_set_state(vdev_t
*vd
, boolean_t isopen
, vdev_state_t state
, vdev_aux_t aux
)
3475 uint64_t save_state
;
3476 spa_t
*spa
= vd
->vdev_spa
;
3478 if (state
== vd
->vdev_state
) {
3480 * Since vdev_offline() code path is already in an offline
3481 * state we can miss a statechange event to OFFLINE. Check
3482 * the previous state to catch this condition.
3484 if (vd
->vdev_ops
->vdev_op_leaf
&&
3485 (state
== VDEV_STATE_OFFLINE
) &&
3486 (vd
->vdev_prevstate
>= VDEV_STATE_FAULTED
)) {
3487 /* post an offline state change */
3488 zfs_post_state_change(spa
, vd
, vd
->vdev_prevstate
);
3490 vd
->vdev_stat
.vs_aux
= aux
;
3494 save_state
= vd
->vdev_state
;
3496 vd
->vdev_state
= state
;
3497 vd
->vdev_stat
.vs_aux
= aux
;
3500 * If we are setting the vdev state to anything but an open state, then
3501 * always close the underlying device unless the device has requested
3502 * a delayed close (i.e. we're about to remove or fault the device).
3503 * Otherwise, we keep accessible but invalid devices open forever.
3504 * We don't call vdev_close() itself, because that implies some extra
3505 * checks (offline, etc) that we don't want here. This is limited to
3506 * leaf devices, because otherwise closing the device will affect other
3509 if (!vd
->vdev_delayed_close
&& vdev_is_dead(vd
) &&
3510 vd
->vdev_ops
->vdev_op_leaf
)
3511 vd
->vdev_ops
->vdev_op_close(vd
);
3513 if (vd
->vdev_removed
&&
3514 state
== VDEV_STATE_CANT_OPEN
&&
3515 (aux
== VDEV_AUX_OPEN_FAILED
|| vd
->vdev_checkremove
)) {
3517 * If the previous state is set to VDEV_STATE_REMOVED, then this
3518 * device was previously marked removed and someone attempted to
3519 * reopen it. If this failed due to a nonexistent device, then
3520 * keep the device in the REMOVED state. We also let this be if
3521 * it is one of our special test online cases, which is only
3522 * attempting to online the device and shouldn't generate an FMA
3525 vd
->vdev_state
= VDEV_STATE_REMOVED
;
3526 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
3527 } else if (state
== VDEV_STATE_REMOVED
) {
3528 vd
->vdev_removed
= B_TRUE
;
3529 } else if (state
== VDEV_STATE_CANT_OPEN
) {
3531 * If we fail to open a vdev during an import or recovery, we
3532 * mark it as "not available", which signifies that it was
3533 * never there to begin with. Failure to open such a device
3534 * is not considered an error.
3536 if ((spa_load_state(spa
) == SPA_LOAD_IMPORT
||
3537 spa_load_state(spa
) == SPA_LOAD_RECOVER
) &&
3538 vd
->vdev_ops
->vdev_op_leaf
)
3539 vd
->vdev_not_present
= 1;
3542 * Post the appropriate ereport. If the 'prevstate' field is
3543 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3544 * that this is part of a vdev_reopen(). In this case, we don't
3545 * want to post the ereport if the device was already in the
3546 * CANT_OPEN state beforehand.
3548 * If the 'checkremove' flag is set, then this is an attempt to
3549 * online the device in response to an insertion event. If we
3550 * hit this case, then we have detected an insertion event for a
3551 * faulted or offline device that wasn't in the removed state.
3552 * In this scenario, we don't post an ereport because we are
3553 * about to replace the device, or attempt an online with
3554 * vdev_forcefault, which will generate the fault for us.
3556 if ((vd
->vdev_prevstate
!= state
|| vd
->vdev_forcefault
) &&
3557 !vd
->vdev_not_present
&& !vd
->vdev_checkremove
&&
3558 vd
!= spa
->spa_root_vdev
) {
3562 case VDEV_AUX_OPEN_FAILED
:
3563 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED
;
3565 case VDEV_AUX_CORRUPT_DATA
:
3566 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA
;
3568 case VDEV_AUX_NO_REPLICAS
:
3569 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS
;
3571 case VDEV_AUX_BAD_GUID_SUM
:
3572 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM
;
3574 case VDEV_AUX_TOO_SMALL
:
3575 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL
;
3577 case VDEV_AUX_BAD_LABEL
:
3578 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL
;
3580 case VDEV_AUX_BAD_ASHIFT
:
3581 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
;
3584 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN
;
3587 zfs_ereport_post(class, spa
, vd
, NULL
, NULL
,
3591 /* Erase any notion of persistent removed state */
3592 vd
->vdev_removed
= B_FALSE
;
3594 vd
->vdev_removed
= B_FALSE
;
3598 * Notify ZED of any significant state-change on a leaf vdev.
3601 if (vd
->vdev_ops
->vdev_op_leaf
) {
3602 /* preserve original state from a vdev_reopen() */
3603 if ((vd
->vdev_prevstate
!= VDEV_STATE_UNKNOWN
) &&
3604 (vd
->vdev_prevstate
!= vd
->vdev_state
) &&
3605 (save_state
<= VDEV_STATE_CLOSED
))
3606 save_state
= vd
->vdev_prevstate
;
3608 /* filter out state change due to initial vdev_open */
3609 if (save_state
> VDEV_STATE_CLOSED
)
3610 zfs_post_state_change(spa
, vd
, save_state
);
3613 if (!isopen
&& vd
->vdev_parent
)
3614 vdev_propagate_state(vd
->vdev_parent
);
3618 * Check the vdev configuration to ensure that it's capable of supporting
3619 * a root pool. We do not support partial configuration.
3622 vdev_is_bootable(vdev_t
*vd
)
3624 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3625 const char *vdev_type
= vd
->vdev_ops
->vdev_op_type
;
3627 if (strcmp(vdev_type
, VDEV_TYPE_MISSING
) == 0)
3631 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3632 if (!vdev_is_bootable(vd
->vdev_child
[c
]))
3639 * Load the state from the original vdev tree (ovd) which
3640 * we've retrieved from the MOS config object. If the original
3641 * vdev was offline or faulted then we transfer that state to the
3642 * device in the current vdev tree (nvd).
3645 vdev_load_log_state(vdev_t
*nvd
, vdev_t
*ovd
)
3647 ASSERT(nvd
->vdev_top
->vdev_islog
);
3648 ASSERT(spa_config_held(nvd
->vdev_spa
,
3649 SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
3650 ASSERT3U(nvd
->vdev_guid
, ==, ovd
->vdev_guid
);
3652 for (int c
= 0; c
< nvd
->vdev_children
; c
++)
3653 vdev_load_log_state(nvd
->vdev_child
[c
], ovd
->vdev_child
[c
]);
3655 if (nvd
->vdev_ops
->vdev_op_leaf
) {
3657 * Restore the persistent vdev state
3659 nvd
->vdev_offline
= ovd
->vdev_offline
;
3660 nvd
->vdev_faulted
= ovd
->vdev_faulted
;
3661 nvd
->vdev_degraded
= ovd
->vdev_degraded
;
3662 nvd
->vdev_removed
= ovd
->vdev_removed
;
3667 * Determine if a log device has valid content. If the vdev was
3668 * removed or faulted in the MOS config then we know that
3669 * the content on the log device has already been written to the pool.
3672 vdev_log_state_valid(vdev_t
*vd
)
3674 if (vd
->vdev_ops
->vdev_op_leaf
&& !vd
->vdev_faulted
&&
3678 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3679 if (vdev_log_state_valid(vd
->vdev_child
[c
]))
3686 * Expand a vdev if possible.
3689 vdev_expand(vdev_t
*vd
, uint64_t txg
)
3691 ASSERT(vd
->vdev_top
== vd
);
3692 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3694 if ((vd
->vdev_asize
>> vd
->vdev_ms_shift
) > vd
->vdev_ms_count
) {
3695 VERIFY(vdev_metaslab_init(vd
, txg
) == 0);
3696 vdev_config_dirty(vd
);
3704 vdev_split(vdev_t
*vd
)
3706 vdev_t
*cvd
, *pvd
= vd
->vdev_parent
;
3708 vdev_remove_child(pvd
, vd
);
3709 vdev_compact_children(pvd
);
3711 cvd
= pvd
->vdev_child
[0];
3712 if (pvd
->vdev_children
== 1) {
3713 vdev_remove_parent(cvd
);
3714 cvd
->vdev_splitting
= B_TRUE
;
3716 vdev_propagate_state(cvd
);
3720 vdev_deadman(vdev_t
*vd
, char *tag
)
3722 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3723 vdev_t
*cvd
= vd
->vdev_child
[c
];
3725 vdev_deadman(cvd
, tag
);
3728 if (vd
->vdev_ops
->vdev_op_leaf
) {
3729 vdev_queue_t
*vq
= &vd
->vdev_queue
;
3731 mutex_enter(&vq
->vq_lock
);
3732 if (avl_numnodes(&vq
->vq_active_tree
) > 0) {
3733 spa_t
*spa
= vd
->vdev_spa
;
3737 zfs_dbgmsg("slow vdev: %s has %d active IOs",
3738 vd
->vdev_path
, avl_numnodes(&vq
->vq_active_tree
));
3741 * Look at the head of all the pending queues,
3742 * if any I/O has been outstanding for longer than
3743 * the spa_deadman_synctime invoke the deadman logic.
3745 fio
= avl_first(&vq
->vq_active_tree
);
3746 delta
= gethrtime() - fio
->io_timestamp
;
3747 if (delta
> spa_deadman_synctime(spa
))
3748 zio_deadman(fio
, tag
);
3750 mutex_exit(&vq
->vq_lock
);
3754 #if defined(_KERNEL) && defined(HAVE_SPL)
3755 EXPORT_SYMBOL(vdev_fault
);
3756 EXPORT_SYMBOL(vdev_degrade
);
3757 EXPORT_SYMBOL(vdev_online
);
3758 EXPORT_SYMBOL(vdev_offline
);
3759 EXPORT_SYMBOL(vdev_clear
);
3761 module_param(metaslabs_per_vdev
, int, 0644);
3762 MODULE_PARM_DESC(metaslabs_per_vdev
,
3763 "Divide added vdev into approximately (but no more than) this number "
3766 module_param(zfs_delays_per_second
, uint
, 0644);
3767 MODULE_PARM_DESC(zfs_delays_per_second
, "Rate limit delay events to this many "
3768 "IO delays per second");
3770 module_param(zfs_checksums_per_second
, uint
, 0644);
3771 MODULE_PARM_DESC(zfs_checksums_per_second
, "Rate limit checksum events "
3772 "to this many checksum errors per second (do not set below zed"