4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright 2017 Nexenta Systems, Inc.
26 * Copyright (c) 2014 Integros [integros.com]
27 * Copyright 2016 Toomas Soome <tsoome@me.com>
28 * Copyright 2017 Joyent, Inc.
31 #include <sys/zfs_context.h>
32 #include <sys/fm/fs/zfs.h>
34 #include <sys/spa_impl.h>
35 #include <sys/bpobj.h>
37 #include <sys/dmu_tx.h>
38 #include <sys/dsl_dir.h>
39 #include <sys/vdev_impl.h>
40 #include <sys/uberblock_impl.h>
41 #include <sys/metaslab.h>
42 #include <sys/metaslab_impl.h>
43 #include <sys/space_map.h>
44 #include <sys/space_reftree.h>
47 #include <sys/fs/zfs.h>
50 #include <sys/dsl_scan.h>
53 #include <sys/zfs_ratelimit.h>
56 * When a vdev is added, it will be divided into approximately (but no
57 * more than) this number of metaslabs.
59 int metaslabs_per_vdev
= 200;
62 * Rate limit delay events to this many IO delays per second.
64 unsigned int zfs_delays_per_second
= 20;
67 * Rate limit checksum events after this many checksum errors per second.
69 unsigned int zfs_checksums_per_second
= 20;
72 * Ignore errors during scrub/resilver. Allows to work around resilver
73 * upon import when there are pool errors.
75 int zfs_scan_ignore_errors
= 0;
78 * Virtual device management.
81 static vdev_ops_t
*vdev_ops_table
[] = {
96 * Given a vdev type, return the appropriate ops vector.
99 vdev_getops(const char *type
)
101 vdev_ops_t
*ops
, **opspp
;
103 for (opspp
= vdev_ops_table
; (ops
= *opspp
) != NULL
; opspp
++)
104 if (strcmp(ops
->vdev_op_type
, type
) == 0)
111 * Default asize function: return the MAX of psize with the asize of
112 * all children. This is what's used by anything other than RAID-Z.
115 vdev_default_asize(vdev_t
*vd
, uint64_t psize
)
117 uint64_t asize
= P2ROUNDUP(psize
, 1ULL << vd
->vdev_top
->vdev_ashift
);
120 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
121 csize
= vdev_psize_to_asize(vd
->vdev_child
[c
], psize
);
122 asize
= MAX(asize
, csize
);
129 * Get the minimum allocatable size. We define the allocatable size as
130 * the vdev's asize rounded to the nearest metaslab. This allows us to
131 * replace or attach devices which don't have the same physical size but
132 * can still satisfy the same number of allocations.
135 vdev_get_min_asize(vdev_t
*vd
)
137 vdev_t
*pvd
= vd
->vdev_parent
;
140 * If our parent is NULL (inactive spare or cache) or is the root,
141 * just return our own asize.
144 return (vd
->vdev_asize
);
147 * The top-level vdev just returns the allocatable size rounded
148 * to the nearest metaslab.
150 if (vd
== vd
->vdev_top
)
151 return (P2ALIGN(vd
->vdev_asize
, 1ULL << vd
->vdev_ms_shift
));
154 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
155 * so each child must provide at least 1/Nth of its asize.
157 if (pvd
->vdev_ops
== &vdev_raidz_ops
)
158 return ((pvd
->vdev_min_asize
+ pvd
->vdev_children
- 1) /
161 return (pvd
->vdev_min_asize
);
165 vdev_set_min_asize(vdev_t
*vd
)
167 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
169 for (int c
= 0; c
< vd
->vdev_children
; c
++)
170 vdev_set_min_asize(vd
->vdev_child
[c
]);
174 vdev_lookup_top(spa_t
*spa
, uint64_t vdev
)
176 vdev_t
*rvd
= spa
->spa_root_vdev
;
178 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
180 if (vdev
< rvd
->vdev_children
) {
181 ASSERT(rvd
->vdev_child
[vdev
] != NULL
);
182 return (rvd
->vdev_child
[vdev
]);
189 vdev_lookup_by_guid(vdev_t
*vd
, uint64_t guid
)
193 if (vd
->vdev_guid
== guid
)
196 for (int c
= 0; c
< vd
->vdev_children
; c
++)
197 if ((mvd
= vdev_lookup_by_guid(vd
->vdev_child
[c
], guid
)) !=
205 vdev_count_leaves_impl(vdev_t
*vd
)
209 if (vd
->vdev_ops
->vdev_op_leaf
)
212 for (int c
= 0; c
< vd
->vdev_children
; c
++)
213 n
+= vdev_count_leaves_impl(vd
->vdev_child
[c
]);
219 vdev_count_leaves(spa_t
*spa
)
223 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
224 rc
= vdev_count_leaves_impl(spa
->spa_root_vdev
);
225 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
231 vdev_add_child(vdev_t
*pvd
, vdev_t
*cvd
)
233 size_t oldsize
, newsize
;
234 uint64_t id
= cvd
->vdev_id
;
237 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
238 ASSERT(cvd
->vdev_parent
== NULL
);
240 cvd
->vdev_parent
= pvd
;
245 ASSERT(id
>= pvd
->vdev_children
|| pvd
->vdev_child
[id
] == NULL
);
247 oldsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
248 pvd
->vdev_children
= MAX(pvd
->vdev_children
, id
+ 1);
249 newsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
251 newchild
= kmem_alloc(newsize
, KM_SLEEP
);
252 if (pvd
->vdev_child
!= NULL
) {
253 bcopy(pvd
->vdev_child
, newchild
, oldsize
);
254 kmem_free(pvd
->vdev_child
, oldsize
);
257 pvd
->vdev_child
= newchild
;
258 pvd
->vdev_child
[id
] = cvd
;
260 cvd
->vdev_top
= (pvd
->vdev_top
? pvd
->vdev_top
: cvd
);
261 ASSERT(cvd
->vdev_top
->vdev_parent
->vdev_parent
== NULL
);
264 * Walk up all ancestors to update guid sum.
266 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
267 pvd
->vdev_guid_sum
+= cvd
->vdev_guid_sum
;
271 vdev_remove_child(vdev_t
*pvd
, vdev_t
*cvd
)
274 uint_t id
= cvd
->vdev_id
;
276 ASSERT(cvd
->vdev_parent
== pvd
);
281 ASSERT(id
< pvd
->vdev_children
);
282 ASSERT(pvd
->vdev_child
[id
] == cvd
);
284 pvd
->vdev_child
[id
] = NULL
;
285 cvd
->vdev_parent
= NULL
;
287 for (c
= 0; c
< pvd
->vdev_children
; c
++)
288 if (pvd
->vdev_child
[c
])
291 if (c
== pvd
->vdev_children
) {
292 kmem_free(pvd
->vdev_child
, c
* sizeof (vdev_t
*));
293 pvd
->vdev_child
= NULL
;
294 pvd
->vdev_children
= 0;
298 * Walk up all ancestors to update guid sum.
300 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
301 pvd
->vdev_guid_sum
-= cvd
->vdev_guid_sum
;
305 * Remove any holes in the child array.
308 vdev_compact_children(vdev_t
*pvd
)
310 vdev_t
**newchild
, *cvd
;
311 int oldc
= pvd
->vdev_children
;
314 ASSERT(spa_config_held(pvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
319 for (int c
= newc
= 0; c
< oldc
; c
++)
320 if (pvd
->vdev_child
[c
])
324 newchild
= kmem_zalloc(newc
* sizeof (vdev_t
*), KM_SLEEP
);
326 for (int c
= newc
= 0; c
< oldc
; c
++) {
327 if ((cvd
= pvd
->vdev_child
[c
]) != NULL
) {
328 newchild
[newc
] = cvd
;
329 cvd
->vdev_id
= newc
++;
336 kmem_free(pvd
->vdev_child
, oldc
* sizeof (vdev_t
*));
337 pvd
->vdev_child
= newchild
;
338 pvd
->vdev_children
= newc
;
342 * Allocate and minimally initialize a vdev_t.
345 vdev_alloc_common(spa_t
*spa
, uint_t id
, uint64_t guid
, vdev_ops_t
*ops
)
348 vdev_indirect_config_t
*vic
;
350 vd
= kmem_zalloc(sizeof (vdev_t
), KM_SLEEP
);
351 vic
= &vd
->vdev_indirect_config
;
353 if (spa
->spa_root_vdev
== NULL
) {
354 ASSERT(ops
== &vdev_root_ops
);
355 spa
->spa_root_vdev
= vd
;
356 spa
->spa_load_guid
= spa_generate_guid(NULL
);
359 if (guid
== 0 && ops
!= &vdev_hole_ops
) {
360 if (spa
->spa_root_vdev
== vd
) {
362 * The root vdev's guid will also be the pool guid,
363 * which must be unique among all pools.
365 guid
= spa_generate_guid(NULL
);
368 * Any other vdev's guid must be unique within the pool.
370 guid
= spa_generate_guid(spa
);
372 ASSERT(!spa_guid_exists(spa_guid(spa
), guid
));
377 vd
->vdev_guid
= guid
;
378 vd
->vdev_guid_sum
= guid
;
380 vd
->vdev_state
= VDEV_STATE_CLOSED
;
381 vd
->vdev_ishole
= (ops
== &vdev_hole_ops
);
382 vic
->vic_prev_indirect_vdev
= UINT64_MAX
;
384 rw_init(&vd
->vdev_indirect_rwlock
, NULL
, RW_DEFAULT
, NULL
);
385 mutex_init(&vd
->vdev_obsolete_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
386 vd
->vdev_obsolete_segments
= range_tree_create(NULL
, NULL
);
389 * Initialize rate limit structs for events. We rate limit ZIO delay
390 * and checksum events so that we don't overwhelm ZED with thousands
391 * of events when a disk is acting up.
393 zfs_ratelimit_init(&vd
->vdev_delay_rl
, &zfs_delays_per_second
, 1);
394 zfs_ratelimit_init(&vd
->vdev_checksum_rl
, &zfs_checksums_per_second
, 1);
396 list_link_init(&vd
->vdev_config_dirty_node
);
397 list_link_init(&vd
->vdev_state_dirty_node
);
398 mutex_init(&vd
->vdev_dtl_lock
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
399 mutex_init(&vd
->vdev_stat_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
400 mutex_init(&vd
->vdev_probe_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
401 mutex_init(&vd
->vdev_queue_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
402 mutex_init(&vd
->vdev_scan_io_queue_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
404 for (int t
= 0; t
< DTL_TYPES
; t
++) {
405 vd
->vdev_dtl
[t
] = range_tree_create(NULL
, NULL
);
407 txg_list_create(&vd
->vdev_ms_list
, spa
,
408 offsetof(struct metaslab
, ms_txg_node
));
409 txg_list_create(&vd
->vdev_dtl_list
, spa
,
410 offsetof(struct vdev
, vdev_dtl_node
));
411 vd
->vdev_stat
.vs_timestamp
= gethrtime();
419 * Allocate a new vdev. The 'alloctype' is used to control whether we are
420 * creating a new vdev or loading an existing one - the behavior is slightly
421 * different for each case.
424 vdev_alloc(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
, uint_t id
,
429 uint64_t guid
= 0, islog
, nparity
;
431 vdev_indirect_config_t
*vic
;
435 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
437 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
438 return (SET_ERROR(EINVAL
));
440 if ((ops
= vdev_getops(type
)) == NULL
)
441 return (SET_ERROR(EINVAL
));
444 * If this is a load, get the vdev guid from the nvlist.
445 * Otherwise, vdev_alloc_common() will generate one for us.
447 if (alloctype
== VDEV_ALLOC_LOAD
) {
450 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
, &label_id
) ||
452 return (SET_ERROR(EINVAL
));
454 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
455 return (SET_ERROR(EINVAL
));
456 } else if (alloctype
== VDEV_ALLOC_SPARE
) {
457 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
458 return (SET_ERROR(EINVAL
));
459 } else if (alloctype
== VDEV_ALLOC_L2CACHE
) {
460 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
461 return (SET_ERROR(EINVAL
));
462 } else if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
463 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
464 return (SET_ERROR(EINVAL
));
468 * The first allocated vdev must be of type 'root'.
470 if (ops
!= &vdev_root_ops
&& spa
->spa_root_vdev
== NULL
)
471 return (SET_ERROR(EINVAL
));
474 * Determine whether we're a log vdev.
477 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &islog
);
478 if (islog
&& spa_version(spa
) < SPA_VERSION_SLOGS
)
479 return (SET_ERROR(ENOTSUP
));
481 if (ops
== &vdev_hole_ops
&& spa_version(spa
) < SPA_VERSION_HOLES
)
482 return (SET_ERROR(ENOTSUP
));
485 * Set the nparity property for RAID-Z vdevs.
488 if (ops
== &vdev_raidz_ops
) {
489 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
491 if (nparity
== 0 || nparity
> VDEV_RAIDZ_MAXPARITY
)
492 return (SET_ERROR(EINVAL
));
494 * Previous versions could only support 1 or 2 parity
498 spa_version(spa
) < SPA_VERSION_RAIDZ2
)
499 return (SET_ERROR(ENOTSUP
));
501 spa_version(spa
) < SPA_VERSION_RAIDZ3
)
502 return (SET_ERROR(ENOTSUP
));
505 * We require the parity to be specified for SPAs that
506 * support multiple parity levels.
508 if (spa_version(spa
) >= SPA_VERSION_RAIDZ2
)
509 return (SET_ERROR(EINVAL
));
511 * Otherwise, we default to 1 parity device for RAID-Z.
518 ASSERT(nparity
!= -1ULL);
520 vd
= vdev_alloc_common(spa
, id
, guid
, ops
);
521 vic
= &vd
->vdev_indirect_config
;
523 vd
->vdev_islog
= islog
;
524 vd
->vdev_nparity
= nparity
;
526 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &vd
->vdev_path
) == 0)
527 vd
->vdev_path
= spa_strdup(vd
->vdev_path
);
530 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
531 * fault on a vdev and want it to persist across imports (like with
534 rc
= nvlist_lookup_string(nv
, ZPOOL_CONFIG_AUX_STATE
, &tmp
);
535 if (rc
== 0 && tmp
!= NULL
&& strcmp(tmp
, "external") == 0) {
536 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
537 vd
->vdev_faulted
= 1;
538 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
541 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &vd
->vdev_devid
) == 0)
542 vd
->vdev_devid
= spa_strdup(vd
->vdev_devid
);
543 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PHYS_PATH
,
544 &vd
->vdev_physpath
) == 0)
545 vd
->vdev_physpath
= spa_strdup(vd
->vdev_physpath
);
547 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH
,
548 &vd
->vdev_enc_sysfs_path
) == 0)
549 vd
->vdev_enc_sysfs_path
= spa_strdup(vd
->vdev_enc_sysfs_path
);
551 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_FRU
, &vd
->vdev_fru
) == 0)
552 vd
->vdev_fru
= spa_strdup(vd
->vdev_fru
);
555 * Set the whole_disk property. If it's not specified, leave the value
558 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
559 &vd
->vdev_wholedisk
) != 0)
560 vd
->vdev_wholedisk
= -1ULL;
562 ASSERT0(vic
->vic_mapping_object
);
563 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_OBJECT
,
564 &vic
->vic_mapping_object
);
565 ASSERT0(vic
->vic_births_object
);
566 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_BIRTHS
,
567 &vic
->vic_births_object
);
568 ASSERT3U(vic
->vic_prev_indirect_vdev
, ==, UINT64_MAX
);
569 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_PREV_INDIRECT_VDEV
,
570 &vic
->vic_prev_indirect_vdev
);
573 * Look for the 'not present' flag. This will only be set if the device
574 * was not present at the time of import.
576 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
577 &vd
->vdev_not_present
);
580 * Get the alignment requirement.
582 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASHIFT
, &vd
->vdev_ashift
);
585 * Retrieve the vdev creation time.
587 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_CREATE_TXG
,
591 * If we're a top-level vdev, try to load the allocation parameters.
593 if (parent
&& !parent
->vdev_parent
&&
594 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
595 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_ARRAY
,
597 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_SHIFT
,
599 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASIZE
,
601 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVING
,
603 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_VDEV_TOP_ZAP
,
606 ASSERT0(vd
->vdev_top_zap
);
609 if (parent
&& !parent
->vdev_parent
&& alloctype
!= VDEV_ALLOC_ATTACH
) {
610 ASSERT(alloctype
== VDEV_ALLOC_LOAD
||
611 alloctype
== VDEV_ALLOC_ADD
||
612 alloctype
== VDEV_ALLOC_SPLIT
||
613 alloctype
== VDEV_ALLOC_ROOTPOOL
);
614 vd
->vdev_mg
= metaslab_group_create(islog
?
615 spa_log_class(spa
) : spa_normal_class(spa
), vd
);
618 if (vd
->vdev_ops
->vdev_op_leaf
&&
619 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
620 (void) nvlist_lookup_uint64(nv
,
621 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, &vd
->vdev_leaf_zap
);
623 ASSERT0(vd
->vdev_leaf_zap
);
627 * If we're a leaf vdev, try to load the DTL object and other state.
630 if (vd
->vdev_ops
->vdev_op_leaf
&&
631 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_L2CACHE
||
632 alloctype
== VDEV_ALLOC_ROOTPOOL
)) {
633 if (alloctype
== VDEV_ALLOC_LOAD
) {
634 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DTL
,
635 &vd
->vdev_dtl_object
);
636 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_UNSPARE
,
640 if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
643 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
644 &spare
) == 0 && spare
)
648 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
,
651 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_RESILVER_TXG
,
652 &vd
->vdev_resilver_txg
);
655 * In general, when importing a pool we want to ignore the
656 * persistent fault state, as the diagnosis made on another
657 * system may not be valid in the current context. The only
658 * exception is if we forced a vdev to a persistently faulted
659 * state with 'zpool offline -f'. The persistent fault will
660 * remain across imports until cleared.
662 * Local vdevs will remain in the faulted state.
664 if (spa_load_state(spa
) == SPA_LOAD_OPEN
||
665 spa_load_state(spa
) == SPA_LOAD_IMPORT
) {
666 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
,
668 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DEGRADED
,
670 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
,
673 if (vd
->vdev_faulted
|| vd
->vdev_degraded
) {
677 VDEV_AUX_ERR_EXCEEDED
;
678 if (nvlist_lookup_string(nv
,
679 ZPOOL_CONFIG_AUX_STATE
, &aux
) == 0 &&
680 strcmp(aux
, "external") == 0)
681 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
687 * Add ourselves to the parent's list of children.
689 vdev_add_child(parent
, vd
);
697 vdev_free(vdev_t
*vd
)
699 spa_t
*spa
= vd
->vdev_spa
;
702 * Scan queues are normally destroyed at the end of a scan. If the
703 * queue exists here, that implies the vdev is being removed while
704 * the scan is still running.
706 if (vd
->vdev_scan_io_queue
!= NULL
) {
707 mutex_enter(&vd
->vdev_scan_io_queue_lock
);
708 dsl_scan_io_queue_destroy(vd
->vdev_scan_io_queue
);
709 vd
->vdev_scan_io_queue
= NULL
;
710 mutex_exit(&vd
->vdev_scan_io_queue_lock
);
714 * vdev_free() implies closing the vdev first. This is simpler than
715 * trying to ensure complicated semantics for all callers.
719 ASSERT(!list_link_active(&vd
->vdev_config_dirty_node
));
720 ASSERT(!list_link_active(&vd
->vdev_state_dirty_node
));
725 for (int c
= 0; c
< vd
->vdev_children
; c
++)
726 vdev_free(vd
->vdev_child
[c
]);
728 ASSERT(vd
->vdev_child
== NULL
);
729 ASSERT(vd
->vdev_guid_sum
== vd
->vdev_guid
);
732 * Discard allocation state.
734 if (vd
->vdev_mg
!= NULL
) {
735 vdev_metaslab_fini(vd
);
736 metaslab_group_destroy(vd
->vdev_mg
);
739 ASSERT0(vd
->vdev_stat
.vs_space
);
740 ASSERT0(vd
->vdev_stat
.vs_dspace
);
741 ASSERT0(vd
->vdev_stat
.vs_alloc
);
744 * Remove this vdev from its parent's child list.
746 vdev_remove_child(vd
->vdev_parent
, vd
);
748 ASSERT(vd
->vdev_parent
== NULL
);
751 * Clean up vdev structure.
757 spa_strfree(vd
->vdev_path
);
759 spa_strfree(vd
->vdev_devid
);
760 if (vd
->vdev_physpath
)
761 spa_strfree(vd
->vdev_physpath
);
763 if (vd
->vdev_enc_sysfs_path
)
764 spa_strfree(vd
->vdev_enc_sysfs_path
);
767 spa_strfree(vd
->vdev_fru
);
769 if (vd
->vdev_isspare
)
770 spa_spare_remove(vd
);
771 if (vd
->vdev_isl2cache
)
772 spa_l2cache_remove(vd
);
774 txg_list_destroy(&vd
->vdev_ms_list
);
775 txg_list_destroy(&vd
->vdev_dtl_list
);
777 mutex_enter(&vd
->vdev_dtl_lock
);
778 space_map_close(vd
->vdev_dtl_sm
);
779 for (int t
= 0; t
< DTL_TYPES
; t
++) {
780 range_tree_vacate(vd
->vdev_dtl
[t
], NULL
, NULL
);
781 range_tree_destroy(vd
->vdev_dtl
[t
]);
783 mutex_exit(&vd
->vdev_dtl_lock
);
785 EQUIV(vd
->vdev_indirect_births
!= NULL
,
786 vd
->vdev_indirect_mapping
!= NULL
);
787 if (vd
->vdev_indirect_births
!= NULL
) {
788 vdev_indirect_mapping_close(vd
->vdev_indirect_mapping
);
789 vdev_indirect_births_close(vd
->vdev_indirect_births
);
792 if (vd
->vdev_obsolete_sm
!= NULL
) {
793 ASSERT(vd
->vdev_removing
||
794 vd
->vdev_ops
== &vdev_indirect_ops
);
795 space_map_close(vd
->vdev_obsolete_sm
);
796 vd
->vdev_obsolete_sm
= NULL
;
798 range_tree_destroy(vd
->vdev_obsolete_segments
);
799 rw_destroy(&vd
->vdev_indirect_rwlock
);
800 mutex_destroy(&vd
->vdev_obsolete_lock
);
802 mutex_destroy(&vd
->vdev_queue_lock
);
803 mutex_destroy(&vd
->vdev_dtl_lock
);
804 mutex_destroy(&vd
->vdev_stat_lock
);
805 mutex_destroy(&vd
->vdev_probe_lock
);
806 mutex_destroy(&vd
->vdev_scan_io_queue_lock
);
808 zfs_ratelimit_fini(&vd
->vdev_delay_rl
);
809 zfs_ratelimit_fini(&vd
->vdev_checksum_rl
);
811 if (vd
== spa
->spa_root_vdev
)
812 spa
->spa_root_vdev
= NULL
;
814 kmem_free(vd
, sizeof (vdev_t
));
818 * Transfer top-level vdev state from svd to tvd.
821 vdev_top_transfer(vdev_t
*svd
, vdev_t
*tvd
)
823 spa_t
*spa
= svd
->vdev_spa
;
828 ASSERT(tvd
== tvd
->vdev_top
);
830 tvd
->vdev_pending_fastwrite
= svd
->vdev_pending_fastwrite
;
831 tvd
->vdev_ms_array
= svd
->vdev_ms_array
;
832 tvd
->vdev_ms_shift
= svd
->vdev_ms_shift
;
833 tvd
->vdev_ms_count
= svd
->vdev_ms_count
;
834 tvd
->vdev_top_zap
= svd
->vdev_top_zap
;
836 svd
->vdev_ms_array
= 0;
837 svd
->vdev_ms_shift
= 0;
838 svd
->vdev_ms_count
= 0;
839 svd
->vdev_top_zap
= 0;
842 ASSERT3P(tvd
->vdev_mg
, ==, svd
->vdev_mg
);
843 tvd
->vdev_mg
= svd
->vdev_mg
;
844 tvd
->vdev_ms
= svd
->vdev_ms
;
849 if (tvd
->vdev_mg
!= NULL
)
850 tvd
->vdev_mg
->mg_vd
= tvd
;
852 tvd
->vdev_stat
.vs_alloc
= svd
->vdev_stat
.vs_alloc
;
853 tvd
->vdev_stat
.vs_space
= svd
->vdev_stat
.vs_space
;
854 tvd
->vdev_stat
.vs_dspace
= svd
->vdev_stat
.vs_dspace
;
856 svd
->vdev_stat
.vs_alloc
= 0;
857 svd
->vdev_stat
.vs_space
= 0;
858 svd
->vdev_stat
.vs_dspace
= 0;
861 * State which may be set on a top-level vdev that's in the
862 * process of being removed.
864 ASSERT0(tvd
->vdev_indirect_config
.vic_births_object
);
865 ASSERT0(tvd
->vdev_indirect_config
.vic_mapping_object
);
866 ASSERT3U(tvd
->vdev_indirect_config
.vic_prev_indirect_vdev
, ==, -1ULL);
867 ASSERT3P(tvd
->vdev_indirect_mapping
, ==, NULL
);
868 ASSERT3P(tvd
->vdev_indirect_births
, ==, NULL
);
869 ASSERT3P(tvd
->vdev_obsolete_sm
, ==, NULL
);
870 ASSERT0(tvd
->vdev_removing
);
871 tvd
->vdev_removing
= svd
->vdev_removing
;
872 tvd
->vdev_indirect_config
= svd
->vdev_indirect_config
;
873 tvd
->vdev_indirect_mapping
= svd
->vdev_indirect_mapping
;
874 tvd
->vdev_indirect_births
= svd
->vdev_indirect_births
;
875 range_tree_swap(&svd
->vdev_obsolete_segments
,
876 &tvd
->vdev_obsolete_segments
);
877 tvd
->vdev_obsolete_sm
= svd
->vdev_obsolete_sm
;
878 svd
->vdev_indirect_config
.vic_mapping_object
= 0;
879 svd
->vdev_indirect_config
.vic_births_object
= 0;
880 svd
->vdev_indirect_config
.vic_prev_indirect_vdev
= -1ULL;
881 svd
->vdev_indirect_mapping
= NULL
;
882 svd
->vdev_indirect_births
= NULL
;
883 svd
->vdev_obsolete_sm
= NULL
;
884 svd
->vdev_removing
= 0;
886 for (t
= 0; t
< TXG_SIZE
; t
++) {
887 while ((msp
= txg_list_remove(&svd
->vdev_ms_list
, t
)) != NULL
)
888 (void) txg_list_add(&tvd
->vdev_ms_list
, msp
, t
);
889 while ((vd
= txg_list_remove(&svd
->vdev_dtl_list
, t
)) != NULL
)
890 (void) txg_list_add(&tvd
->vdev_dtl_list
, vd
, t
);
891 if (txg_list_remove_this(&spa
->spa_vdev_txg_list
, svd
, t
))
892 (void) txg_list_add(&spa
->spa_vdev_txg_list
, tvd
, t
);
895 if (list_link_active(&svd
->vdev_config_dirty_node
)) {
896 vdev_config_clean(svd
);
897 vdev_config_dirty(tvd
);
900 if (list_link_active(&svd
->vdev_state_dirty_node
)) {
901 vdev_state_clean(svd
);
902 vdev_state_dirty(tvd
);
905 tvd
->vdev_deflate_ratio
= svd
->vdev_deflate_ratio
;
906 svd
->vdev_deflate_ratio
= 0;
908 tvd
->vdev_islog
= svd
->vdev_islog
;
911 dsl_scan_io_queue_vdev_xfer(svd
, tvd
);
915 vdev_top_update(vdev_t
*tvd
, vdev_t
*vd
)
922 for (int c
= 0; c
< vd
->vdev_children
; c
++)
923 vdev_top_update(tvd
, vd
->vdev_child
[c
]);
927 * Add a mirror/replacing vdev above an existing vdev.
930 vdev_add_parent(vdev_t
*cvd
, vdev_ops_t
*ops
)
932 spa_t
*spa
= cvd
->vdev_spa
;
933 vdev_t
*pvd
= cvd
->vdev_parent
;
936 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
938 mvd
= vdev_alloc_common(spa
, cvd
->vdev_id
, 0, ops
);
940 mvd
->vdev_asize
= cvd
->vdev_asize
;
941 mvd
->vdev_min_asize
= cvd
->vdev_min_asize
;
942 mvd
->vdev_max_asize
= cvd
->vdev_max_asize
;
943 mvd
->vdev_psize
= cvd
->vdev_psize
;
944 mvd
->vdev_ashift
= cvd
->vdev_ashift
;
945 mvd
->vdev_state
= cvd
->vdev_state
;
946 mvd
->vdev_crtxg
= cvd
->vdev_crtxg
;
948 vdev_remove_child(pvd
, cvd
);
949 vdev_add_child(pvd
, mvd
);
950 cvd
->vdev_id
= mvd
->vdev_children
;
951 vdev_add_child(mvd
, cvd
);
952 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
954 if (mvd
== mvd
->vdev_top
)
955 vdev_top_transfer(cvd
, mvd
);
961 * Remove a 1-way mirror/replacing vdev from the tree.
964 vdev_remove_parent(vdev_t
*cvd
)
966 vdev_t
*mvd
= cvd
->vdev_parent
;
967 vdev_t
*pvd
= mvd
->vdev_parent
;
969 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
971 ASSERT(mvd
->vdev_children
== 1);
972 ASSERT(mvd
->vdev_ops
== &vdev_mirror_ops
||
973 mvd
->vdev_ops
== &vdev_replacing_ops
||
974 mvd
->vdev_ops
== &vdev_spare_ops
);
975 cvd
->vdev_ashift
= mvd
->vdev_ashift
;
977 vdev_remove_child(mvd
, cvd
);
978 vdev_remove_child(pvd
, mvd
);
981 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
982 * Otherwise, we could have detached an offline device, and when we
983 * go to import the pool we'll think we have two top-level vdevs,
984 * instead of a different version of the same top-level vdev.
986 if (mvd
->vdev_top
== mvd
) {
987 uint64_t guid_delta
= mvd
->vdev_guid
- cvd
->vdev_guid
;
988 cvd
->vdev_orig_guid
= cvd
->vdev_guid
;
989 cvd
->vdev_guid
+= guid_delta
;
990 cvd
->vdev_guid_sum
+= guid_delta
;
993 * If pool not set for autoexpand, we need to also preserve
994 * mvd's asize to prevent automatic expansion of cvd.
995 * Otherwise if we are adjusting the mirror by attaching and
996 * detaching children of non-uniform sizes, the mirror could
997 * autoexpand, unexpectedly requiring larger devices to
998 * re-establish the mirror.
1000 if (!cvd
->vdev_spa
->spa_autoexpand
)
1001 cvd
->vdev_asize
= mvd
->vdev_asize
;
1003 cvd
->vdev_id
= mvd
->vdev_id
;
1004 vdev_add_child(pvd
, cvd
);
1005 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1007 if (cvd
== cvd
->vdev_top
)
1008 vdev_top_transfer(mvd
, cvd
);
1010 ASSERT(mvd
->vdev_children
== 0);
1015 vdev_metaslab_init(vdev_t
*vd
, uint64_t txg
)
1017 spa_t
*spa
= vd
->vdev_spa
;
1018 objset_t
*mos
= spa
->spa_meta_objset
;
1020 uint64_t oldc
= vd
->vdev_ms_count
;
1021 uint64_t newc
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
1025 ASSERT(txg
== 0 || spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1028 * This vdev is not being allocated from yet or is a hole.
1030 if (vd
->vdev_ms_shift
== 0)
1033 ASSERT(!vd
->vdev_ishole
);
1035 ASSERT(oldc
<= newc
);
1037 mspp
= vmem_zalloc(newc
* sizeof (*mspp
), KM_SLEEP
);
1040 bcopy(vd
->vdev_ms
, mspp
, oldc
* sizeof (*mspp
));
1041 vmem_free(vd
->vdev_ms
, oldc
* sizeof (*mspp
));
1045 vd
->vdev_ms_count
= newc
;
1047 for (m
= oldc
; m
< newc
; m
++) {
1048 uint64_t object
= 0;
1051 * vdev_ms_array may be 0 if we are creating the "fake"
1052 * metaslabs for an indirect vdev for zdb's leak detection.
1053 * See zdb_leak_init().
1055 if (txg
== 0 && vd
->vdev_ms_array
!= 0) {
1056 error
= dmu_read(mos
, vd
->vdev_ms_array
,
1057 m
* sizeof (uint64_t), sizeof (uint64_t), &object
,
1063 error
= metaslab_init(vd
->vdev_mg
, m
, object
, txg
,
1070 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_WRITER
);
1073 * If the vdev is being removed we don't activate
1074 * the metaslabs since we want to ensure that no new
1075 * allocations are performed on this device.
1077 if (oldc
== 0 && !vd
->vdev_removing
)
1078 metaslab_group_activate(vd
->vdev_mg
);
1081 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
1087 vdev_metaslab_fini(vdev_t
*vd
)
1089 if (vd
->vdev_ms
!= NULL
) {
1090 uint64_t count
= vd
->vdev_ms_count
;
1092 metaslab_group_passivate(vd
->vdev_mg
);
1093 for (uint64_t m
= 0; m
< count
; m
++) {
1094 metaslab_t
*msp
= vd
->vdev_ms
[m
];
1099 vmem_free(vd
->vdev_ms
, count
* sizeof (metaslab_t
*));
1102 vd
->vdev_ms_count
= 0;
1104 ASSERT0(vd
->vdev_ms_count
);
1105 ASSERT3U(vd
->vdev_pending_fastwrite
, ==, 0);
1108 typedef struct vdev_probe_stats
{
1109 boolean_t vps_readable
;
1110 boolean_t vps_writeable
;
1112 } vdev_probe_stats_t
;
1115 vdev_probe_done(zio_t
*zio
)
1117 spa_t
*spa
= zio
->io_spa
;
1118 vdev_t
*vd
= zio
->io_vd
;
1119 vdev_probe_stats_t
*vps
= zio
->io_private
;
1121 ASSERT(vd
->vdev_probe_zio
!= NULL
);
1123 if (zio
->io_type
== ZIO_TYPE_READ
) {
1124 if (zio
->io_error
== 0)
1125 vps
->vps_readable
= 1;
1126 if (zio
->io_error
== 0 && spa_writeable(spa
)) {
1127 zio_nowait(zio_write_phys(vd
->vdev_probe_zio
, vd
,
1128 zio
->io_offset
, zio
->io_size
, zio
->io_abd
,
1129 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1130 ZIO_PRIORITY_SYNC_WRITE
, vps
->vps_flags
, B_TRUE
));
1132 abd_free(zio
->io_abd
);
1134 } else if (zio
->io_type
== ZIO_TYPE_WRITE
) {
1135 if (zio
->io_error
== 0)
1136 vps
->vps_writeable
= 1;
1137 abd_free(zio
->io_abd
);
1138 } else if (zio
->io_type
== ZIO_TYPE_NULL
) {
1142 vd
->vdev_cant_read
|= !vps
->vps_readable
;
1143 vd
->vdev_cant_write
|= !vps
->vps_writeable
;
1145 if (vdev_readable(vd
) &&
1146 (vdev_writeable(vd
) || !spa_writeable(spa
))) {
1149 ASSERT(zio
->io_error
!= 0);
1150 zfs_dbgmsg("failed probe on vdev %llu",
1151 (longlong_t
)vd
->vdev_id
);
1152 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE
,
1153 spa
, vd
, NULL
, NULL
, 0, 0);
1154 zio
->io_error
= SET_ERROR(ENXIO
);
1157 mutex_enter(&vd
->vdev_probe_lock
);
1158 ASSERT(vd
->vdev_probe_zio
== zio
);
1159 vd
->vdev_probe_zio
= NULL
;
1160 mutex_exit(&vd
->vdev_probe_lock
);
1163 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
)
1164 if (!vdev_accessible(vd
, pio
))
1165 pio
->io_error
= SET_ERROR(ENXIO
);
1167 kmem_free(vps
, sizeof (*vps
));
1172 * Determine whether this device is accessible.
1174 * Read and write to several known locations: the pad regions of each
1175 * vdev label but the first, which we leave alone in case it contains
1179 vdev_probe(vdev_t
*vd
, zio_t
*zio
)
1181 spa_t
*spa
= vd
->vdev_spa
;
1182 vdev_probe_stats_t
*vps
= NULL
;
1185 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1188 * Don't probe the probe.
1190 if (zio
&& (zio
->io_flags
& ZIO_FLAG_PROBE
))
1194 * To prevent 'probe storms' when a device fails, we create
1195 * just one probe i/o at a time. All zios that want to probe
1196 * this vdev will become parents of the probe io.
1198 mutex_enter(&vd
->vdev_probe_lock
);
1200 if ((pio
= vd
->vdev_probe_zio
) == NULL
) {
1201 vps
= kmem_zalloc(sizeof (*vps
), KM_SLEEP
);
1203 vps
->vps_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_PROBE
|
1204 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_AGGREGATE
|
1207 if (spa_config_held(spa
, SCL_ZIO
, RW_WRITER
)) {
1209 * vdev_cant_read and vdev_cant_write can only
1210 * transition from TRUE to FALSE when we have the
1211 * SCL_ZIO lock as writer; otherwise they can only
1212 * transition from FALSE to TRUE. This ensures that
1213 * any zio looking at these values can assume that
1214 * failures persist for the life of the I/O. That's
1215 * important because when a device has intermittent
1216 * connectivity problems, we want to ensure that
1217 * they're ascribed to the device (ENXIO) and not
1220 * Since we hold SCL_ZIO as writer here, clear both
1221 * values so the probe can reevaluate from first
1224 vps
->vps_flags
|= ZIO_FLAG_CONFIG_WRITER
;
1225 vd
->vdev_cant_read
= B_FALSE
;
1226 vd
->vdev_cant_write
= B_FALSE
;
1229 vd
->vdev_probe_zio
= pio
= zio_null(NULL
, spa
, vd
,
1230 vdev_probe_done
, vps
,
1231 vps
->vps_flags
| ZIO_FLAG_DONT_PROPAGATE
);
1234 * We can't change the vdev state in this context, so we
1235 * kick off an async task to do it on our behalf.
1238 vd
->vdev_probe_wanted
= B_TRUE
;
1239 spa_async_request(spa
, SPA_ASYNC_PROBE
);
1244 zio_add_child(zio
, pio
);
1246 mutex_exit(&vd
->vdev_probe_lock
);
1249 ASSERT(zio
!= NULL
);
1253 for (int l
= 1; l
< VDEV_LABELS
; l
++) {
1254 zio_nowait(zio_read_phys(pio
, vd
,
1255 vdev_label_offset(vd
->vdev_psize
, l
,
1256 offsetof(vdev_label_t
, vl_pad2
)), VDEV_PAD_SIZE
,
1257 abd_alloc_for_io(VDEV_PAD_SIZE
, B_TRUE
),
1258 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1259 ZIO_PRIORITY_SYNC_READ
, vps
->vps_flags
, B_TRUE
));
1270 vdev_open_child(void *arg
)
1274 vd
->vdev_open_thread
= curthread
;
1275 vd
->vdev_open_error
= vdev_open(vd
);
1276 vd
->vdev_open_thread
= NULL
;
1280 vdev_uses_zvols(vdev_t
*vd
)
1283 if (zvol_is_zvol(vd
->vdev_path
))
1287 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1288 if (vdev_uses_zvols(vd
->vdev_child
[c
]))
1295 vdev_open_children(vdev_t
*vd
)
1298 int children
= vd
->vdev_children
;
1301 * in order to handle pools on top of zvols, do the opens
1302 * in a single thread so that the same thread holds the
1303 * spa_namespace_lock
1305 if (vdev_uses_zvols(vd
)) {
1307 for (int c
= 0; c
< children
; c
++)
1308 vd
->vdev_child
[c
]->vdev_open_error
=
1309 vdev_open(vd
->vdev_child
[c
]);
1311 tq
= taskq_create("vdev_open", children
, minclsyspri
,
1312 children
, children
, TASKQ_PREPOPULATE
);
1316 for (int c
= 0; c
< children
; c
++)
1317 VERIFY(taskq_dispatch(tq
, vdev_open_child
,
1318 vd
->vdev_child
[c
], TQ_SLEEP
) != TASKQID_INVALID
);
1323 vd
->vdev_nonrot
= B_TRUE
;
1325 for (int c
= 0; c
< children
; c
++)
1326 vd
->vdev_nonrot
&= vd
->vdev_child
[c
]->vdev_nonrot
;
1330 * Compute the raidz-deflation ratio. Note, we hard-code
1331 * in 128k (1 << 17) because it is the "typical" blocksize.
1332 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1333 * otherwise it would inconsistently account for existing bp's.
1336 vdev_set_deflate_ratio(vdev_t
*vd
)
1338 if (vd
== vd
->vdev_top
&& !vd
->vdev_ishole
&& vd
->vdev_ashift
!= 0) {
1339 vd
->vdev_deflate_ratio
= (1 << 17) /
1340 (vdev_psize_to_asize(vd
, 1 << 17) >> SPA_MINBLOCKSHIFT
);
1345 * Prepare a virtual device for access.
1348 vdev_open(vdev_t
*vd
)
1350 spa_t
*spa
= vd
->vdev_spa
;
1353 uint64_t max_osize
= 0;
1354 uint64_t asize
, max_asize
, psize
;
1355 uint64_t ashift
= 0;
1357 ASSERT(vd
->vdev_open_thread
== curthread
||
1358 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1359 ASSERT(vd
->vdev_state
== VDEV_STATE_CLOSED
||
1360 vd
->vdev_state
== VDEV_STATE_CANT_OPEN
||
1361 vd
->vdev_state
== VDEV_STATE_OFFLINE
);
1363 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1364 vd
->vdev_cant_read
= B_FALSE
;
1365 vd
->vdev_cant_write
= B_FALSE
;
1366 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
1369 * If this vdev is not removed, check its fault status. If it's
1370 * faulted, bail out of the open.
1372 if (!vd
->vdev_removed
&& vd
->vdev_faulted
) {
1373 ASSERT(vd
->vdev_children
== 0);
1374 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1375 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1376 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1377 vd
->vdev_label_aux
);
1378 return (SET_ERROR(ENXIO
));
1379 } else if (vd
->vdev_offline
) {
1380 ASSERT(vd
->vdev_children
== 0);
1381 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
, VDEV_AUX_NONE
);
1382 return (SET_ERROR(ENXIO
));
1385 error
= vd
->vdev_ops
->vdev_op_open(vd
, &osize
, &max_osize
, &ashift
);
1388 * Reset the vdev_reopening flag so that we actually close
1389 * the vdev on error.
1391 vd
->vdev_reopening
= B_FALSE
;
1392 if (zio_injection_enabled
&& error
== 0)
1393 error
= zio_handle_device_injection(vd
, NULL
, ENXIO
);
1396 if (vd
->vdev_removed
&&
1397 vd
->vdev_stat
.vs_aux
!= VDEV_AUX_OPEN_FAILED
)
1398 vd
->vdev_removed
= B_FALSE
;
1400 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1401 vd
->vdev_stat
.vs_aux
);
1405 vd
->vdev_removed
= B_FALSE
;
1408 * Recheck the faulted flag now that we have confirmed that
1409 * the vdev is accessible. If we're faulted, bail.
1411 if (vd
->vdev_faulted
) {
1412 ASSERT(vd
->vdev_children
== 0);
1413 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1414 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1415 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1416 vd
->vdev_label_aux
);
1417 return (SET_ERROR(ENXIO
));
1420 if (vd
->vdev_degraded
) {
1421 ASSERT(vd
->vdev_children
== 0);
1422 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1423 VDEV_AUX_ERR_EXCEEDED
);
1425 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_HEALTHY
, 0);
1429 * For hole or missing vdevs we just return success.
1431 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
)
1434 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
1435 if (vd
->vdev_child
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
) {
1436 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1442 osize
= P2ALIGN(osize
, (uint64_t)sizeof (vdev_label_t
));
1443 max_osize
= P2ALIGN(max_osize
, (uint64_t)sizeof (vdev_label_t
));
1445 if (vd
->vdev_children
== 0) {
1446 if (osize
< SPA_MINDEVSIZE
) {
1447 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1448 VDEV_AUX_TOO_SMALL
);
1449 return (SET_ERROR(EOVERFLOW
));
1452 asize
= osize
- (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
);
1453 max_asize
= max_osize
- (VDEV_LABEL_START_SIZE
+
1454 VDEV_LABEL_END_SIZE
);
1456 if (vd
->vdev_parent
!= NULL
&& osize
< SPA_MINDEVSIZE
-
1457 (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
)) {
1458 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1459 VDEV_AUX_TOO_SMALL
);
1460 return (SET_ERROR(EOVERFLOW
));
1464 max_asize
= max_osize
;
1468 * If the vdev was expanded, record this so that we can re-create the
1469 * uberblock rings in labels {2,3}, during the next sync.
1471 if ((psize
> vd
->vdev_psize
) && (vd
->vdev_psize
!= 0))
1472 vd
->vdev_copy_uberblocks
= B_TRUE
;
1474 vd
->vdev_psize
= psize
;
1477 * Make sure the allocatable size hasn't shrunk too much.
1479 if (asize
< vd
->vdev_min_asize
) {
1480 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1481 VDEV_AUX_BAD_LABEL
);
1482 return (SET_ERROR(EINVAL
));
1485 if (vd
->vdev_asize
== 0) {
1487 * This is the first-ever open, so use the computed values.
1488 * For compatibility, a different ashift can be requested.
1490 vd
->vdev_asize
= asize
;
1491 vd
->vdev_max_asize
= max_asize
;
1492 if (vd
->vdev_ashift
== 0) {
1493 vd
->vdev_ashift
= ashift
; /* use detected value */
1495 if (vd
->vdev_ashift
!= 0 && (vd
->vdev_ashift
< ASHIFT_MIN
||
1496 vd
->vdev_ashift
> ASHIFT_MAX
)) {
1497 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1498 VDEV_AUX_BAD_ASHIFT
);
1499 return (SET_ERROR(EDOM
));
1503 * Detect if the alignment requirement has increased.
1504 * We don't want to make the pool unavailable, just
1505 * post an event instead.
1507 if (ashift
> vd
->vdev_top
->vdev_ashift
&&
1508 vd
->vdev_ops
->vdev_op_leaf
) {
1509 zfs_ereport_post(FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
,
1510 spa
, vd
, NULL
, NULL
, 0, 0);
1513 vd
->vdev_max_asize
= max_asize
;
1517 * If all children are healthy we update asize if either:
1518 * The asize has increased, due to a device expansion caused by dynamic
1519 * LUN growth or vdev replacement, and automatic expansion is enabled;
1520 * making the additional space available.
1522 * The asize has decreased, due to a device shrink usually caused by a
1523 * vdev replace with a smaller device. This ensures that calculations
1524 * based of max_asize and asize e.g. esize are always valid. It's safe
1525 * to do this as we've already validated that asize is greater than
1528 if (vd
->vdev_state
== VDEV_STATE_HEALTHY
&&
1529 ((asize
> vd
->vdev_asize
&&
1530 (vd
->vdev_expanding
|| spa
->spa_autoexpand
)) ||
1531 (asize
< vd
->vdev_asize
)))
1532 vd
->vdev_asize
= asize
;
1534 vdev_set_min_asize(vd
);
1537 * Ensure we can issue some IO before declaring the
1538 * vdev open for business.
1540 if (vd
->vdev_ops
->vdev_op_leaf
&&
1541 (error
= zio_wait(vdev_probe(vd
, NULL
))) != 0) {
1542 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1543 VDEV_AUX_ERR_EXCEEDED
);
1547 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
1548 !vd
->vdev_isl2cache
&& !vd
->vdev_islog
) {
1549 if (vd
->vdev_ashift
> spa
->spa_max_ashift
)
1550 spa
->spa_max_ashift
= vd
->vdev_ashift
;
1551 if (vd
->vdev_ashift
< spa
->spa_min_ashift
)
1552 spa
->spa_min_ashift
= vd
->vdev_ashift
;
1556 * Track the min and max ashift values for normal data devices.
1558 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
1559 !vd
->vdev_islog
&& vd
->vdev_aux
== NULL
) {
1560 if (vd
->vdev_ashift
> spa
->spa_max_ashift
)
1561 spa
->spa_max_ashift
= vd
->vdev_ashift
;
1562 if (vd
->vdev_ashift
< spa
->spa_min_ashift
)
1563 spa
->spa_min_ashift
= vd
->vdev_ashift
;
1567 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1568 * resilver. But don't do this if we are doing a reopen for a scrub,
1569 * since this would just restart the scrub we are already doing.
1571 if (vd
->vdev_ops
->vdev_op_leaf
&& !spa
->spa_scrub_reopen
&&
1572 vdev_resilver_needed(vd
, NULL
, NULL
))
1573 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
1579 * Called once the vdevs are all opened, this routine validates the label
1580 * contents. This needs to be done before vdev_load() so that we don't
1581 * inadvertently do repair I/Os to the wrong device.
1583 * If 'strict' is false ignore the spa guid check. This is necessary because
1584 * if the machine crashed during a re-guid the new guid might have been written
1585 * to all of the vdev labels, but not the cached config. The strict check
1586 * will be performed when the pool is opened again using the mos config.
1588 * This function will only return failure if one of the vdevs indicates that it
1589 * has since been destroyed or exported. This is only possible if
1590 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1591 * will be updated but the function will return 0.
1594 vdev_validate(vdev_t
*vd
, boolean_t strict
)
1596 spa_t
*spa
= vd
->vdev_spa
;
1598 uint64_t guid
= 0, top_guid
;
1601 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1602 if (vdev_validate(vd
->vdev_child
[c
], strict
) != 0)
1603 return (SET_ERROR(EBADF
));
1606 * If the device has already failed, or was marked offline, don't do
1607 * any further validation. Otherwise, label I/O will fail and we will
1608 * overwrite the previous state.
1610 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_readable(vd
)) {
1611 uint64_t aux_guid
= 0;
1613 uint64_t txg
= spa_last_synced_txg(spa
) != 0 ?
1614 spa_last_synced_txg(spa
) : -1ULL;
1616 if ((label
= vdev_label_read_config(vd
, txg
)) == NULL
) {
1617 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1618 VDEV_AUX_BAD_LABEL
);
1623 * Determine if this vdev has been split off into another
1624 * pool. If so, then refuse to open it.
1626 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_SPLIT_GUID
,
1627 &aux_guid
) == 0 && aux_guid
== spa_guid(spa
)) {
1628 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1629 VDEV_AUX_SPLIT_POOL
);
1634 if (strict
&& (nvlist_lookup_uint64(label
,
1635 ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1636 guid
!= spa_guid(spa
))) {
1637 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1638 VDEV_AUX_CORRUPT_DATA
);
1643 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
)
1644 != 0 || nvlist_lookup_uint64(nvl
, ZPOOL_CONFIG_ORIG_GUID
,
1649 * If this vdev just became a top-level vdev because its
1650 * sibling was detached, it will have adopted the parent's
1651 * vdev guid -- but the label may or may not be on disk yet.
1652 * Fortunately, either version of the label will have the
1653 * same top guid, so if we're a top-level vdev, we can
1654 * safely compare to that instead.
1656 * If we split this vdev off instead, then we also check the
1657 * original pool's guid. We don't want to consider the vdev
1658 * corrupt if it is partway through a split operation.
1660 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
,
1662 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_TOP_GUID
,
1664 ((vd
->vdev_guid
!= guid
&& vd
->vdev_guid
!= aux_guid
) &&
1665 (vd
->vdev_guid
!= top_guid
|| vd
!= vd
->vdev_top
))) {
1666 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1667 VDEV_AUX_CORRUPT_DATA
);
1672 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
,
1674 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1675 VDEV_AUX_CORRUPT_DATA
);
1683 * If this is a verbatim import, no need to check the
1684 * state of the pool.
1686 if (!(spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) &&
1687 spa_load_state(spa
) == SPA_LOAD_OPEN
&&
1688 state
!= POOL_STATE_ACTIVE
)
1689 return (SET_ERROR(EBADF
));
1692 * If we were able to open and validate a vdev that was
1693 * previously marked permanently unavailable, clear that state
1696 if (vd
->vdev_not_present
)
1697 vd
->vdev_not_present
= 0;
1704 * Close a virtual device.
1707 vdev_close(vdev_t
*vd
)
1709 vdev_t
*pvd
= vd
->vdev_parent
;
1710 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
1712 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1715 * If our parent is reopening, then we are as well, unless we are
1718 if (pvd
!= NULL
&& pvd
->vdev_reopening
)
1719 vd
->vdev_reopening
= (pvd
->vdev_reopening
&& !vd
->vdev_offline
);
1721 vd
->vdev_ops
->vdev_op_close(vd
);
1723 vdev_cache_purge(vd
);
1726 * We record the previous state before we close it, so that if we are
1727 * doing a reopen(), we don't generate FMA ereports if we notice that
1728 * it's still faulted.
1730 vd
->vdev_prevstate
= vd
->vdev_state
;
1732 if (vd
->vdev_offline
)
1733 vd
->vdev_state
= VDEV_STATE_OFFLINE
;
1735 vd
->vdev_state
= VDEV_STATE_CLOSED
;
1736 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1740 vdev_hold(vdev_t
*vd
)
1742 spa_t
*spa
= vd
->vdev_spa
;
1744 ASSERT(spa_is_root(spa
));
1745 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1748 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1749 vdev_hold(vd
->vdev_child
[c
]);
1751 if (vd
->vdev_ops
->vdev_op_leaf
)
1752 vd
->vdev_ops
->vdev_op_hold(vd
);
1756 vdev_rele(vdev_t
*vd
)
1758 ASSERT(spa_is_root(vd
->vdev_spa
));
1759 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1760 vdev_rele(vd
->vdev_child
[c
]);
1762 if (vd
->vdev_ops
->vdev_op_leaf
)
1763 vd
->vdev_ops
->vdev_op_rele(vd
);
1767 * Reopen all interior vdevs and any unopened leaves. We don't actually
1768 * reopen leaf vdevs which had previously been opened as they might deadlock
1769 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
1770 * If the leaf has never been opened then open it, as usual.
1773 vdev_reopen(vdev_t
*vd
)
1775 spa_t
*spa
= vd
->vdev_spa
;
1777 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1779 /* set the reopening flag unless we're taking the vdev offline */
1780 vd
->vdev_reopening
= !vd
->vdev_offline
;
1782 (void) vdev_open(vd
);
1785 * Call vdev_validate() here to make sure we have the same device.
1786 * Otherwise, a device with an invalid label could be successfully
1787 * opened in response to vdev_reopen().
1790 (void) vdev_validate_aux(vd
);
1791 if (vdev_readable(vd
) && vdev_writeable(vd
) &&
1792 vd
->vdev_aux
== &spa
->spa_l2cache
&&
1793 !l2arc_vdev_present(vd
))
1794 l2arc_add_vdev(spa
, vd
);
1796 (void) vdev_validate(vd
, B_TRUE
);
1800 * Reassess parent vdev's health.
1802 vdev_propagate_state(vd
);
1806 vdev_create(vdev_t
*vd
, uint64_t txg
, boolean_t isreplacing
)
1811 * Normally, partial opens (e.g. of a mirror) are allowed.
1812 * For a create, however, we want to fail the request if
1813 * there are any components we can't open.
1815 error
= vdev_open(vd
);
1817 if (error
|| vd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
1819 return (error
? error
: ENXIO
);
1823 * Recursively load DTLs and initialize all labels.
1825 if ((error
= vdev_dtl_load(vd
)) != 0 ||
1826 (error
= vdev_label_init(vd
, txg
, isreplacing
?
1827 VDEV_LABEL_REPLACE
: VDEV_LABEL_CREATE
)) != 0) {
1836 vdev_metaslab_set_size(vdev_t
*vd
)
1839 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
1841 vd
->vdev_ms_shift
= highbit64(vd
->vdev_asize
/ metaslabs_per_vdev
);
1842 vd
->vdev_ms_shift
= MAX(vd
->vdev_ms_shift
, SPA_MAXBLOCKSHIFT
);
1846 vdev_dirty(vdev_t
*vd
, int flags
, void *arg
, uint64_t txg
)
1848 ASSERT(vd
== vd
->vdev_top
);
1849 /* indirect vdevs don't have metaslabs or dtls */
1850 ASSERT(vdev_is_concrete(vd
) || flags
== 0);
1851 ASSERT(ISP2(flags
));
1852 ASSERT(spa_writeable(vd
->vdev_spa
));
1854 if (flags
& VDD_METASLAB
)
1855 (void) txg_list_add(&vd
->vdev_ms_list
, arg
, txg
);
1857 if (flags
& VDD_DTL
)
1858 (void) txg_list_add(&vd
->vdev_dtl_list
, arg
, txg
);
1860 (void) txg_list_add(&vd
->vdev_spa
->spa_vdev_txg_list
, vd
, txg
);
1864 vdev_dirty_leaves(vdev_t
*vd
, int flags
, uint64_t txg
)
1866 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1867 vdev_dirty_leaves(vd
->vdev_child
[c
], flags
, txg
);
1869 if (vd
->vdev_ops
->vdev_op_leaf
)
1870 vdev_dirty(vd
->vdev_top
, flags
, vd
, txg
);
1876 * A vdev's DTL (dirty time log) is the set of transaction groups for which
1877 * the vdev has less than perfect replication. There are four kinds of DTL:
1879 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
1881 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
1883 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
1884 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
1885 * txgs that was scrubbed.
1887 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
1888 * persistent errors or just some device being offline.
1889 * Unlike the other three, the DTL_OUTAGE map is not generally
1890 * maintained; it's only computed when needed, typically to
1891 * determine whether a device can be detached.
1893 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
1894 * either has the data or it doesn't.
1896 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
1897 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
1898 * if any child is less than fully replicated, then so is its parent.
1899 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
1900 * comprising only those txgs which appear in 'maxfaults' or more children;
1901 * those are the txgs we don't have enough replication to read. For example,
1902 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
1903 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
1904 * two child DTL_MISSING maps.
1906 * It should be clear from the above that to compute the DTLs and outage maps
1907 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
1908 * Therefore, that is all we keep on disk. When loading the pool, or after
1909 * a configuration change, we generate all other DTLs from first principles.
1912 vdev_dtl_dirty(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
1914 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
1916 ASSERT(t
< DTL_TYPES
);
1917 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
1918 ASSERT(spa_writeable(vd
->vdev_spa
));
1920 mutex_enter(&vd
->vdev_dtl_lock
);
1921 if (!range_tree_contains(rt
, txg
, size
))
1922 range_tree_add(rt
, txg
, size
);
1923 mutex_exit(&vd
->vdev_dtl_lock
);
1927 vdev_dtl_contains(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
1929 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
1930 boolean_t dirty
= B_FALSE
;
1932 ASSERT(t
< DTL_TYPES
);
1933 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
1936 * While we are loading the pool, the DTLs have not been loaded yet.
1937 * Ignore the DTLs and try all devices. This avoids a recursive
1938 * mutex enter on the vdev_dtl_lock, and also makes us try hard
1939 * when loading the pool (relying on the checksum to ensure that
1940 * we get the right data -- note that we while loading, we are
1941 * only reading the MOS, which is always checksummed).
1943 if (vd
->vdev_spa
->spa_load_state
!= SPA_LOAD_NONE
)
1946 mutex_enter(&vd
->vdev_dtl_lock
);
1947 if (range_tree_space(rt
) != 0)
1948 dirty
= range_tree_contains(rt
, txg
, size
);
1949 mutex_exit(&vd
->vdev_dtl_lock
);
1955 vdev_dtl_empty(vdev_t
*vd
, vdev_dtl_type_t t
)
1957 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
1960 mutex_enter(&vd
->vdev_dtl_lock
);
1961 empty
= (range_tree_space(rt
) == 0);
1962 mutex_exit(&vd
->vdev_dtl_lock
);
1968 * Returns B_TRUE if vdev determines offset needs to be resilvered.
1971 vdev_dtl_need_resilver(vdev_t
*vd
, uint64_t offset
, size_t psize
)
1973 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
1975 if (vd
->vdev_ops
->vdev_op_need_resilver
== NULL
||
1976 vd
->vdev_ops
->vdev_op_leaf
)
1979 return (vd
->vdev_ops
->vdev_op_need_resilver(vd
, offset
, psize
));
1983 * Returns the lowest txg in the DTL range.
1986 vdev_dtl_min(vdev_t
*vd
)
1990 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
1991 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
1992 ASSERT0(vd
->vdev_children
);
1994 rs
= avl_first(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
1995 return (rs
->rs_start
- 1);
1999 * Returns the highest txg in the DTL.
2002 vdev_dtl_max(vdev_t
*vd
)
2006 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2007 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2008 ASSERT0(vd
->vdev_children
);
2010 rs
= avl_last(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
2011 return (rs
->rs_end
);
2015 * Determine if a resilvering vdev should remove any DTL entries from
2016 * its range. If the vdev was resilvering for the entire duration of the
2017 * scan then it should excise that range from its DTLs. Otherwise, this
2018 * vdev is considered partially resilvered and should leave its DTL
2019 * entries intact. The comment in vdev_dtl_reassess() describes how we
2023 vdev_dtl_should_excise(vdev_t
*vd
)
2025 spa_t
*spa
= vd
->vdev_spa
;
2026 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
2028 ASSERT0(scn
->scn_phys
.scn_errors
);
2029 ASSERT0(vd
->vdev_children
);
2031 if (vd
->vdev_state
< VDEV_STATE_DEGRADED
)
2034 if (vd
->vdev_resilver_txg
== 0 ||
2035 range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) == 0)
2039 * When a resilver is initiated the scan will assign the scn_max_txg
2040 * value to the highest txg value that exists in all DTLs. If this
2041 * device's max DTL is not part of this scan (i.e. it is not in
2042 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2045 if (vdev_dtl_max(vd
) <= scn
->scn_phys
.scn_max_txg
) {
2046 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <=, vdev_dtl_min(vd
));
2047 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <, vd
->vdev_resilver_txg
);
2048 ASSERT3U(vd
->vdev_resilver_txg
, <=, scn
->scn_phys
.scn_max_txg
);
2055 * Reassess DTLs after a config change or scrub completion.
2058 vdev_dtl_reassess(vdev_t
*vd
, uint64_t txg
, uint64_t scrub_txg
, int scrub_done
)
2060 spa_t
*spa
= vd
->vdev_spa
;
2064 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
2066 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2067 vdev_dtl_reassess(vd
->vdev_child
[c
], txg
,
2068 scrub_txg
, scrub_done
);
2070 if (vd
== spa
->spa_root_vdev
|| !vdev_is_concrete(vd
) || vd
->vdev_aux
)
2073 if (vd
->vdev_ops
->vdev_op_leaf
) {
2074 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
2076 mutex_enter(&vd
->vdev_dtl_lock
);
2079 * If requested, pretend the scan completed cleanly.
2081 if (zfs_scan_ignore_errors
&& scn
)
2082 scn
->scn_phys
.scn_errors
= 0;
2085 * If we've completed a scan cleanly then determine
2086 * if this vdev should remove any DTLs. We only want to
2087 * excise regions on vdevs that were available during
2088 * the entire duration of this scan.
2090 if (scrub_txg
!= 0 &&
2091 (spa
->spa_scrub_started
||
2092 (scn
!= NULL
&& scn
->scn_phys
.scn_errors
== 0)) &&
2093 vdev_dtl_should_excise(vd
)) {
2095 * We completed a scrub up to scrub_txg. If we
2096 * did it without rebooting, then the scrub dtl
2097 * will be valid, so excise the old region and
2098 * fold in the scrub dtl. Otherwise, leave the
2099 * dtl as-is if there was an error.
2101 * There's little trick here: to excise the beginning
2102 * of the DTL_MISSING map, we put it into a reference
2103 * tree and then add a segment with refcnt -1 that
2104 * covers the range [0, scrub_txg). This means
2105 * that each txg in that range has refcnt -1 or 0.
2106 * We then add DTL_SCRUB with a refcnt of 2, so that
2107 * entries in the range [0, scrub_txg) will have a
2108 * positive refcnt -- either 1 or 2. We then convert
2109 * the reference tree into the new DTL_MISSING map.
2111 space_reftree_create(&reftree
);
2112 space_reftree_add_map(&reftree
,
2113 vd
->vdev_dtl
[DTL_MISSING
], 1);
2114 space_reftree_add_seg(&reftree
, 0, scrub_txg
, -1);
2115 space_reftree_add_map(&reftree
,
2116 vd
->vdev_dtl
[DTL_SCRUB
], 2);
2117 space_reftree_generate_map(&reftree
,
2118 vd
->vdev_dtl
[DTL_MISSING
], 1);
2119 space_reftree_destroy(&reftree
);
2121 range_tree_vacate(vd
->vdev_dtl
[DTL_PARTIAL
], NULL
, NULL
);
2122 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2123 range_tree_add
, vd
->vdev_dtl
[DTL_PARTIAL
]);
2125 range_tree_vacate(vd
->vdev_dtl
[DTL_SCRUB
], NULL
, NULL
);
2126 range_tree_vacate(vd
->vdev_dtl
[DTL_OUTAGE
], NULL
, NULL
);
2127 if (!vdev_readable(vd
))
2128 range_tree_add(vd
->vdev_dtl
[DTL_OUTAGE
], 0, -1ULL);
2130 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2131 range_tree_add
, vd
->vdev_dtl
[DTL_OUTAGE
]);
2134 * If the vdev was resilvering and no longer has any
2135 * DTLs then reset its resilvering flag and dirty
2136 * the top level so that we persist the change.
2138 if (vd
->vdev_resilver_txg
!= 0 &&
2139 range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) == 0 &&
2140 range_tree_space(vd
->vdev_dtl
[DTL_OUTAGE
]) == 0) {
2141 vd
->vdev_resilver_txg
= 0;
2142 vdev_config_dirty(vd
->vdev_top
);
2145 mutex_exit(&vd
->vdev_dtl_lock
);
2148 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, txg
);
2152 mutex_enter(&vd
->vdev_dtl_lock
);
2153 for (int t
= 0; t
< DTL_TYPES
; t
++) {
2154 /* account for child's outage in parent's missing map */
2155 int s
= (t
== DTL_MISSING
) ? DTL_OUTAGE
: t
;
2157 continue; /* leaf vdevs only */
2158 if (t
== DTL_PARTIAL
)
2159 minref
= 1; /* i.e. non-zero */
2160 else if (vd
->vdev_nparity
!= 0)
2161 minref
= vd
->vdev_nparity
+ 1; /* RAID-Z */
2163 minref
= vd
->vdev_children
; /* any kind of mirror */
2164 space_reftree_create(&reftree
);
2165 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2166 vdev_t
*cvd
= vd
->vdev_child
[c
];
2167 mutex_enter(&cvd
->vdev_dtl_lock
);
2168 space_reftree_add_map(&reftree
, cvd
->vdev_dtl
[s
], 1);
2169 mutex_exit(&cvd
->vdev_dtl_lock
);
2171 space_reftree_generate_map(&reftree
, vd
->vdev_dtl
[t
], minref
);
2172 space_reftree_destroy(&reftree
);
2174 mutex_exit(&vd
->vdev_dtl_lock
);
2178 vdev_dtl_load(vdev_t
*vd
)
2180 spa_t
*spa
= vd
->vdev_spa
;
2181 objset_t
*mos
= spa
->spa_meta_objset
;
2184 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_dtl_object
!= 0) {
2185 ASSERT(vdev_is_concrete(vd
));
2187 error
= space_map_open(&vd
->vdev_dtl_sm
, mos
,
2188 vd
->vdev_dtl_object
, 0, -1ULL, 0);
2191 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2193 mutex_enter(&vd
->vdev_dtl_lock
);
2196 * Now that we've opened the space_map we need to update
2199 space_map_update(vd
->vdev_dtl_sm
);
2201 error
= space_map_load(vd
->vdev_dtl_sm
,
2202 vd
->vdev_dtl
[DTL_MISSING
], SM_ALLOC
);
2203 mutex_exit(&vd
->vdev_dtl_lock
);
2208 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2209 error
= vdev_dtl_load(vd
->vdev_child
[c
]);
2218 vdev_destroy_unlink_zap(vdev_t
*vd
, uint64_t zapobj
, dmu_tx_t
*tx
)
2220 spa_t
*spa
= vd
->vdev_spa
;
2222 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zapobj
, tx
));
2223 VERIFY0(zap_remove_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2228 vdev_create_link_zap(vdev_t
*vd
, dmu_tx_t
*tx
)
2230 spa_t
*spa
= vd
->vdev_spa
;
2231 uint64_t zap
= zap_create(spa
->spa_meta_objset
, DMU_OTN_ZAP_METADATA
,
2232 DMU_OT_NONE
, 0, tx
);
2235 VERIFY0(zap_add_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2242 vdev_construct_zaps(vdev_t
*vd
, dmu_tx_t
*tx
)
2244 if (vd
->vdev_ops
!= &vdev_hole_ops
&&
2245 vd
->vdev_ops
!= &vdev_missing_ops
&&
2246 vd
->vdev_ops
!= &vdev_root_ops
&&
2247 !vd
->vdev_top
->vdev_removing
) {
2248 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_leaf_zap
== 0) {
2249 vd
->vdev_leaf_zap
= vdev_create_link_zap(vd
, tx
);
2251 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
== 0) {
2252 vd
->vdev_top_zap
= vdev_create_link_zap(vd
, tx
);
2255 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2256 vdev_construct_zaps(vd
->vdev_child
[i
], tx
);
2261 vdev_dtl_sync(vdev_t
*vd
, uint64_t txg
)
2263 spa_t
*spa
= vd
->vdev_spa
;
2264 range_tree_t
*rt
= vd
->vdev_dtl
[DTL_MISSING
];
2265 objset_t
*mos
= spa
->spa_meta_objset
;
2266 range_tree_t
*rtsync
;
2268 uint64_t object
= space_map_object(vd
->vdev_dtl_sm
);
2270 ASSERT(vdev_is_concrete(vd
));
2271 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2273 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2275 if (vd
->vdev_detached
|| vd
->vdev_top
->vdev_removing
) {
2276 mutex_enter(&vd
->vdev_dtl_lock
);
2277 space_map_free(vd
->vdev_dtl_sm
, tx
);
2278 space_map_close(vd
->vdev_dtl_sm
);
2279 vd
->vdev_dtl_sm
= NULL
;
2280 mutex_exit(&vd
->vdev_dtl_lock
);
2283 * We only destroy the leaf ZAP for detached leaves or for
2284 * removed log devices. Removed data devices handle leaf ZAP
2285 * cleanup later, once cancellation is no longer possible.
2287 if (vd
->vdev_leaf_zap
!= 0 && (vd
->vdev_detached
||
2288 vd
->vdev_top
->vdev_islog
)) {
2289 vdev_destroy_unlink_zap(vd
, vd
->vdev_leaf_zap
, tx
);
2290 vd
->vdev_leaf_zap
= 0;
2297 if (vd
->vdev_dtl_sm
== NULL
) {
2298 uint64_t new_object
;
2300 new_object
= space_map_alloc(mos
, tx
);
2301 VERIFY3U(new_object
, !=, 0);
2303 VERIFY0(space_map_open(&vd
->vdev_dtl_sm
, mos
, new_object
,
2305 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2308 rtsync
= range_tree_create(NULL
, NULL
);
2310 mutex_enter(&vd
->vdev_dtl_lock
);
2311 range_tree_walk(rt
, range_tree_add
, rtsync
);
2312 mutex_exit(&vd
->vdev_dtl_lock
);
2314 space_map_truncate(vd
->vdev_dtl_sm
, tx
);
2315 space_map_write(vd
->vdev_dtl_sm
, rtsync
, SM_ALLOC
, tx
);
2316 range_tree_vacate(rtsync
, NULL
, NULL
);
2318 range_tree_destroy(rtsync
);
2321 * If the object for the space map has changed then dirty
2322 * the top level so that we update the config.
2324 if (object
!= space_map_object(vd
->vdev_dtl_sm
)) {
2325 zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, "
2326 "new object %llu", txg
, spa_name(spa
), object
,
2327 space_map_object(vd
->vdev_dtl_sm
));
2328 vdev_config_dirty(vd
->vdev_top
);
2333 mutex_enter(&vd
->vdev_dtl_lock
);
2334 space_map_update(vd
->vdev_dtl_sm
);
2335 mutex_exit(&vd
->vdev_dtl_lock
);
2339 * Determine whether the specified vdev can be offlined/detached/removed
2340 * without losing data.
2343 vdev_dtl_required(vdev_t
*vd
)
2345 spa_t
*spa
= vd
->vdev_spa
;
2346 vdev_t
*tvd
= vd
->vdev_top
;
2347 uint8_t cant_read
= vd
->vdev_cant_read
;
2350 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2352 if (vd
== spa
->spa_root_vdev
|| vd
== tvd
)
2356 * Temporarily mark the device as unreadable, and then determine
2357 * whether this results in any DTL outages in the top-level vdev.
2358 * If not, we can safely offline/detach/remove the device.
2360 vd
->vdev_cant_read
= B_TRUE
;
2361 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2362 required
= !vdev_dtl_empty(tvd
, DTL_OUTAGE
);
2363 vd
->vdev_cant_read
= cant_read
;
2364 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2366 if (!required
&& zio_injection_enabled
)
2367 required
= !!zio_handle_device_injection(vd
, NULL
, ECHILD
);
2373 * Determine if resilver is needed, and if so the txg range.
2376 vdev_resilver_needed(vdev_t
*vd
, uint64_t *minp
, uint64_t *maxp
)
2378 boolean_t needed
= B_FALSE
;
2379 uint64_t thismin
= UINT64_MAX
;
2380 uint64_t thismax
= 0;
2382 if (vd
->vdev_children
== 0) {
2383 mutex_enter(&vd
->vdev_dtl_lock
);
2384 if (range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]) != 0 &&
2385 vdev_writeable(vd
)) {
2387 thismin
= vdev_dtl_min(vd
);
2388 thismax
= vdev_dtl_max(vd
);
2391 mutex_exit(&vd
->vdev_dtl_lock
);
2393 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2394 vdev_t
*cvd
= vd
->vdev_child
[c
];
2395 uint64_t cmin
, cmax
;
2397 if (vdev_resilver_needed(cvd
, &cmin
, &cmax
)) {
2398 thismin
= MIN(thismin
, cmin
);
2399 thismax
= MAX(thismax
, cmax
);
2405 if (needed
&& minp
) {
2413 vdev_load(vdev_t
*vd
)
2418 * Recursively load all children.
2420 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2421 error
= vdev_load(vd
->vdev_child
[c
]);
2427 vdev_set_deflate_ratio(vd
);
2430 * If this is a top-level vdev, initialize its metaslabs.
2432 if (vd
== vd
->vdev_top
&& vdev_is_concrete(vd
)) {
2433 if (vd
->vdev_ashift
== 0 || vd
->vdev_asize
== 0) {
2434 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2435 VDEV_AUX_CORRUPT_DATA
);
2436 return (SET_ERROR(ENXIO
));
2437 } else if ((error
= vdev_metaslab_init(vd
, 0)) != 0) {
2438 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2439 VDEV_AUX_CORRUPT_DATA
);
2445 * If this is a leaf vdev, load its DTL.
2447 if (vd
->vdev_ops
->vdev_op_leaf
&& (error
= vdev_dtl_load(vd
)) != 0) {
2448 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2449 VDEV_AUX_CORRUPT_DATA
);
2453 uint64_t obsolete_sm_object
= vdev_obsolete_sm_object(vd
);
2454 if (obsolete_sm_object
!= 0) {
2455 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
2456 ASSERT(vd
->vdev_asize
!= 0);
2457 ASSERT(vd
->vdev_obsolete_sm
== NULL
);
2459 if ((error
= space_map_open(&vd
->vdev_obsolete_sm
, mos
,
2460 obsolete_sm_object
, 0, vd
->vdev_asize
, 0))) {
2461 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2462 VDEV_AUX_CORRUPT_DATA
);
2465 space_map_update(vd
->vdev_obsolete_sm
);
2472 * The special vdev case is used for hot spares and l2cache devices. Its
2473 * sole purpose it to set the vdev state for the associated vdev. To do this,
2474 * we make sure that we can open the underlying device, then try to read the
2475 * label, and make sure that the label is sane and that it hasn't been
2476 * repurposed to another pool.
2479 vdev_validate_aux(vdev_t
*vd
)
2482 uint64_t guid
, version
;
2485 if (!vdev_readable(vd
))
2488 if ((label
= vdev_label_read_config(vd
, -1ULL)) == NULL
) {
2489 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2490 VDEV_AUX_CORRUPT_DATA
);
2494 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_VERSION
, &version
) != 0 ||
2495 !SPA_VERSION_IS_SUPPORTED(version
) ||
2496 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0 ||
2497 guid
!= vd
->vdev_guid
||
2498 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
, &state
) != 0) {
2499 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
2500 VDEV_AUX_CORRUPT_DATA
);
2506 * We don't actually check the pool state here. If it's in fact in
2507 * use by another pool, we update this fact on the fly when requested.
2514 * Free the objects used to store this vdev's spacemaps, and the array
2515 * that points to them.
2518 vdev_destroy_spacemaps(vdev_t
*vd
, dmu_tx_t
*tx
)
2520 if (vd
->vdev_ms_array
== 0)
2523 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
2524 uint64_t array_count
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
2525 size_t array_bytes
= array_count
* sizeof (uint64_t);
2526 uint64_t *smobj_array
= kmem_alloc(array_bytes
, KM_SLEEP
);
2527 VERIFY0(dmu_read(mos
, vd
->vdev_ms_array
, 0,
2528 array_bytes
, smobj_array
, 0));
2530 for (uint64_t i
= 0; i
< array_count
; i
++) {
2531 uint64_t smobj
= smobj_array
[i
];
2535 space_map_free_obj(mos
, smobj
, tx
);
2538 kmem_free(smobj_array
, array_bytes
);
2539 VERIFY0(dmu_object_free(mos
, vd
->vdev_ms_array
, tx
));
2540 vd
->vdev_ms_array
= 0;
2544 vdev_remove_empty(vdev_t
*vd
, uint64_t txg
)
2546 spa_t
*spa
= vd
->vdev_spa
;
2549 ASSERT(vd
== vd
->vdev_top
);
2550 ASSERT3U(txg
, ==, spa_syncing_txg(spa
));
2552 if (vd
->vdev_ms
!= NULL
) {
2553 metaslab_group_t
*mg
= vd
->vdev_mg
;
2555 metaslab_group_histogram_verify(mg
);
2556 metaslab_class_histogram_verify(mg
->mg_class
);
2558 for (int m
= 0; m
< vd
->vdev_ms_count
; m
++) {
2559 metaslab_t
*msp
= vd
->vdev_ms
[m
];
2561 if (msp
== NULL
|| msp
->ms_sm
== NULL
)
2564 mutex_enter(&msp
->ms_lock
);
2566 * If the metaslab was not loaded when the vdev
2567 * was removed then the histogram accounting may
2568 * not be accurate. Update the histogram information
2569 * here so that we ensure that the metaslab group
2570 * and metaslab class are up-to-date.
2572 metaslab_group_histogram_remove(mg
, msp
);
2574 VERIFY0(space_map_allocated(msp
->ms_sm
));
2575 space_map_close(msp
->ms_sm
);
2577 mutex_exit(&msp
->ms_lock
);
2580 metaslab_group_histogram_verify(mg
);
2581 metaslab_class_histogram_verify(mg
->mg_class
);
2582 for (int i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++)
2583 ASSERT0(mg
->mg_histogram
[i
]);
2586 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
2587 vdev_destroy_spacemaps(vd
, tx
);
2589 if (vd
->vdev_islog
&& vd
->vdev_top_zap
!= 0) {
2590 vdev_destroy_unlink_zap(vd
, vd
->vdev_top_zap
, tx
);
2591 vd
->vdev_top_zap
= 0;
2597 vdev_sync_done(vdev_t
*vd
, uint64_t txg
)
2600 boolean_t reassess
= !txg_list_empty(&vd
->vdev_ms_list
, TXG_CLEAN(txg
));
2602 ASSERT(vdev_is_concrete(vd
));
2604 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, TXG_CLEAN(txg
))))
2605 metaslab_sync_done(msp
, txg
);
2608 metaslab_sync_reassess(vd
->vdev_mg
);
2612 vdev_sync(vdev_t
*vd
, uint64_t txg
)
2614 spa_t
*spa
= vd
->vdev_spa
;
2619 if (range_tree_space(vd
->vdev_obsolete_segments
) > 0) {
2622 ASSERT(vd
->vdev_removing
||
2623 vd
->vdev_ops
== &vdev_indirect_ops
);
2625 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2626 vdev_indirect_sync_obsolete(vd
, tx
);
2630 * If the vdev is indirect, it can't have dirty
2631 * metaslabs or DTLs.
2633 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
2634 ASSERT(txg_list_empty(&vd
->vdev_ms_list
, txg
));
2635 ASSERT(txg_list_empty(&vd
->vdev_dtl_list
, txg
));
2640 ASSERT(vdev_is_concrete(vd
));
2642 if (vd
->vdev_ms_array
== 0 && vd
->vdev_ms_shift
!= 0 &&
2643 !vd
->vdev_removing
) {
2644 ASSERT(vd
== vd
->vdev_top
);
2645 ASSERT0(vd
->vdev_indirect_config
.vic_mapping_object
);
2646 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2647 vd
->vdev_ms_array
= dmu_object_alloc(spa
->spa_meta_objset
,
2648 DMU_OT_OBJECT_ARRAY
, 0, DMU_OT_NONE
, 0, tx
);
2649 ASSERT(vd
->vdev_ms_array
!= 0);
2650 vdev_config_dirty(vd
);
2654 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, txg
)) != NULL
) {
2655 metaslab_sync(msp
, txg
);
2656 (void) txg_list_add(&vd
->vdev_ms_list
, msp
, TXG_CLEAN(txg
));
2659 while ((lvd
= txg_list_remove(&vd
->vdev_dtl_list
, txg
)) != NULL
)
2660 vdev_dtl_sync(lvd
, txg
);
2663 * Remove the metadata associated with this vdev once it's empty.
2664 * Note that this is typically used for log/cache device removal;
2665 * we don't empty toplevel vdevs when removing them. But if
2666 * a toplevel happens to be emptied, this is not harmful.
2668 if (vd
->vdev_stat
.vs_alloc
== 0 && vd
->vdev_removing
) {
2669 vdev_remove_empty(vd
, txg
);
2672 (void) txg_list_add(&spa
->spa_vdev_txg_list
, vd
, TXG_CLEAN(txg
));
2676 vdev_psize_to_asize(vdev_t
*vd
, uint64_t psize
)
2678 return (vd
->vdev_ops
->vdev_op_asize(vd
, psize
));
2682 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
2683 * not be opened, and no I/O is attempted.
2686 vdev_fault(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
2690 spa_vdev_state_enter(spa
, SCL_NONE
);
2692 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2693 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2695 if (!vd
->vdev_ops
->vdev_op_leaf
)
2696 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2701 * If user did a 'zpool offline -f' then make the fault persist across
2704 if (aux
== VDEV_AUX_EXTERNAL_PERSIST
) {
2706 * There are two kinds of forced faults: temporary and
2707 * persistent. Temporary faults go away at pool import, while
2708 * persistent faults stay set. Both types of faults can be
2709 * cleared with a zpool clear.
2711 * We tell if a vdev is persistently faulted by looking at the
2712 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
2713 * import then it's a persistent fault. Otherwise, it's
2714 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
2715 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
2716 * tells vdev_config_generate() (which gets run later) to set
2717 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
2719 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
2720 vd
->vdev_tmpoffline
= B_FALSE
;
2721 aux
= VDEV_AUX_EXTERNAL
;
2723 vd
->vdev_tmpoffline
= B_TRUE
;
2727 * We don't directly use the aux state here, but if we do a
2728 * vdev_reopen(), we need this value to be present to remember why we
2731 vd
->vdev_label_aux
= aux
;
2734 * Faulted state takes precedence over degraded.
2736 vd
->vdev_delayed_close
= B_FALSE
;
2737 vd
->vdev_faulted
= 1ULL;
2738 vd
->vdev_degraded
= 0ULL;
2739 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_FAULTED
, aux
);
2742 * If this device has the only valid copy of the data, then
2743 * back off and simply mark the vdev as degraded instead.
2745 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&& vdev_dtl_required(vd
)) {
2746 vd
->vdev_degraded
= 1ULL;
2747 vd
->vdev_faulted
= 0ULL;
2750 * If we reopen the device and it's not dead, only then do we
2755 if (vdev_readable(vd
))
2756 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
, aux
);
2759 return (spa_vdev_state_exit(spa
, vd
, 0));
2763 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
2764 * user that something is wrong. The vdev continues to operate as normal as far
2765 * as I/O is concerned.
2768 vdev_degrade(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
2772 spa_vdev_state_enter(spa
, SCL_NONE
);
2774 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2775 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2777 if (!vd
->vdev_ops
->vdev_op_leaf
)
2778 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2781 * If the vdev is already faulted, then don't do anything.
2783 if (vd
->vdev_faulted
|| vd
->vdev_degraded
)
2784 return (spa_vdev_state_exit(spa
, NULL
, 0));
2786 vd
->vdev_degraded
= 1ULL;
2787 if (!vdev_is_dead(vd
))
2788 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
,
2791 return (spa_vdev_state_exit(spa
, vd
, 0));
2795 * Online the given vdev.
2797 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
2798 * spare device should be detached when the device finishes resilvering.
2799 * Second, the online should be treated like a 'test' online case, so no FMA
2800 * events are generated if the device fails to open.
2803 vdev_online(spa_t
*spa
, uint64_t guid
, uint64_t flags
, vdev_state_t
*newstate
)
2805 vdev_t
*vd
, *tvd
, *pvd
, *rvd
= spa
->spa_root_vdev
;
2806 boolean_t wasoffline
;
2807 vdev_state_t oldstate
;
2809 spa_vdev_state_enter(spa
, SCL_NONE
);
2811 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2812 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2814 if (!vd
->vdev_ops
->vdev_op_leaf
)
2815 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2817 wasoffline
= (vd
->vdev_offline
|| vd
->vdev_tmpoffline
);
2818 oldstate
= vd
->vdev_state
;
2821 vd
->vdev_offline
= B_FALSE
;
2822 vd
->vdev_tmpoffline
= B_FALSE
;
2823 vd
->vdev_checkremove
= !!(flags
& ZFS_ONLINE_CHECKREMOVE
);
2824 vd
->vdev_forcefault
= !!(flags
& ZFS_ONLINE_FORCEFAULT
);
2826 /* XXX - L2ARC 1.0 does not support expansion */
2827 if (!vd
->vdev_aux
) {
2828 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
2829 pvd
->vdev_expanding
= !!(flags
& ZFS_ONLINE_EXPAND
);
2833 vd
->vdev_checkremove
= vd
->vdev_forcefault
= B_FALSE
;
2835 if (!vd
->vdev_aux
) {
2836 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
2837 pvd
->vdev_expanding
= B_FALSE
;
2841 *newstate
= vd
->vdev_state
;
2842 if ((flags
& ZFS_ONLINE_UNSPARE
) &&
2843 !vdev_is_dead(vd
) && vd
->vdev_parent
&&
2844 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
2845 vd
->vdev_parent
->vdev_child
[0] == vd
)
2846 vd
->vdev_unspare
= B_TRUE
;
2848 if ((flags
& ZFS_ONLINE_EXPAND
) || spa
->spa_autoexpand
) {
2850 /* XXX - L2ARC 1.0 does not support expansion */
2852 return (spa_vdev_state_exit(spa
, vd
, ENOTSUP
));
2853 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2857 (oldstate
< VDEV_STATE_DEGRADED
&&
2858 vd
->vdev_state
>= VDEV_STATE_DEGRADED
))
2859 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_ONLINE
);
2861 return (spa_vdev_state_exit(spa
, vd
, 0));
2865 vdev_offline_locked(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
2869 uint64_t generation
;
2870 metaslab_group_t
*mg
;
2873 spa_vdev_state_enter(spa
, SCL_ALLOC
);
2875 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
2876 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
2878 if (!vd
->vdev_ops
->vdev_op_leaf
)
2879 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
2883 generation
= spa
->spa_config_generation
+ 1;
2886 * If the device isn't already offline, try to offline it.
2888 if (!vd
->vdev_offline
) {
2890 * If this device has the only valid copy of some data,
2891 * don't allow it to be offlined. Log devices are always
2894 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
2895 vdev_dtl_required(vd
))
2896 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
2899 * If the top-level is a slog and it has had allocations
2900 * then proceed. We check that the vdev's metaslab group
2901 * is not NULL since it's possible that we may have just
2902 * added this vdev but not yet initialized its metaslabs.
2904 if (tvd
->vdev_islog
&& mg
!= NULL
) {
2906 * Prevent any future allocations.
2908 metaslab_group_passivate(mg
);
2909 (void) spa_vdev_state_exit(spa
, vd
, 0);
2911 error
= spa_reset_logs(spa
);
2913 spa_vdev_state_enter(spa
, SCL_ALLOC
);
2916 * Check to see if the config has changed.
2918 if (error
|| generation
!= spa
->spa_config_generation
) {
2919 metaslab_group_activate(mg
);
2921 return (spa_vdev_state_exit(spa
,
2923 (void) spa_vdev_state_exit(spa
, vd
, 0);
2926 ASSERT0(tvd
->vdev_stat
.vs_alloc
);
2930 * Offline this device and reopen its top-level vdev.
2931 * If the top-level vdev is a log device then just offline
2932 * it. Otherwise, if this action results in the top-level
2933 * vdev becoming unusable, undo it and fail the request.
2935 vd
->vdev_offline
= B_TRUE
;
2938 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
2939 vdev_is_dead(tvd
)) {
2940 vd
->vdev_offline
= B_FALSE
;
2942 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
2946 * Add the device back into the metaslab rotor so that
2947 * once we online the device it's open for business.
2949 if (tvd
->vdev_islog
&& mg
!= NULL
)
2950 metaslab_group_activate(mg
);
2953 vd
->vdev_tmpoffline
= !!(flags
& ZFS_OFFLINE_TEMPORARY
);
2955 return (spa_vdev_state_exit(spa
, vd
, 0));
2959 vdev_offline(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
2963 mutex_enter(&spa
->spa_vdev_top_lock
);
2964 error
= vdev_offline_locked(spa
, guid
, flags
);
2965 mutex_exit(&spa
->spa_vdev_top_lock
);
2971 * Clear the error counts associated with this vdev. Unlike vdev_online() and
2972 * vdev_offline(), we assume the spa config is locked. We also clear all
2973 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
2976 vdev_clear(spa_t
*spa
, vdev_t
*vd
)
2978 vdev_t
*rvd
= spa
->spa_root_vdev
;
2980 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2985 vd
->vdev_stat
.vs_read_errors
= 0;
2986 vd
->vdev_stat
.vs_write_errors
= 0;
2987 vd
->vdev_stat
.vs_checksum_errors
= 0;
2989 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2990 vdev_clear(spa
, vd
->vdev_child
[c
]);
2993 * It makes no sense to "clear" an indirect vdev.
2995 if (!vdev_is_concrete(vd
))
2999 * If we're in the FAULTED state or have experienced failed I/O, then
3000 * clear the persistent state and attempt to reopen the device. We
3001 * also mark the vdev config dirty, so that the new faulted state is
3002 * written out to disk.
3004 if (vd
->vdev_faulted
|| vd
->vdev_degraded
||
3005 !vdev_readable(vd
) || !vdev_writeable(vd
)) {
3007 * When reopening in response to a clear event, it may be due to
3008 * a fmadm repair request. In this case, if the device is
3009 * still broken, we want to still post the ereport again.
3011 vd
->vdev_forcefault
= B_TRUE
;
3013 vd
->vdev_faulted
= vd
->vdev_degraded
= 0ULL;
3014 vd
->vdev_cant_read
= B_FALSE
;
3015 vd
->vdev_cant_write
= B_FALSE
;
3016 vd
->vdev_stat
.vs_aux
= 0;
3018 vdev_reopen(vd
== rvd
? rvd
: vd
->vdev_top
);
3020 vd
->vdev_forcefault
= B_FALSE
;
3022 if (vd
!= rvd
&& vdev_writeable(vd
->vdev_top
))
3023 vdev_state_dirty(vd
->vdev_top
);
3025 if (vd
->vdev_aux
== NULL
&& !vdev_is_dead(vd
))
3026 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
3028 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_CLEAR
);
3032 * When clearing a FMA-diagnosed fault, we always want to
3033 * unspare the device, as we assume that the original spare was
3034 * done in response to the FMA fault.
3036 if (!vdev_is_dead(vd
) && vd
->vdev_parent
!= NULL
&&
3037 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3038 vd
->vdev_parent
->vdev_child
[0] == vd
)
3039 vd
->vdev_unspare
= B_TRUE
;
3043 vdev_is_dead(vdev_t
*vd
)
3046 * Holes and missing devices are always considered "dead".
3047 * This simplifies the code since we don't have to check for
3048 * these types of devices in the various code paths.
3049 * Instead we rely on the fact that we skip over dead devices
3050 * before issuing I/O to them.
3052 return (vd
->vdev_state
< VDEV_STATE_DEGRADED
||
3053 vd
->vdev_ops
== &vdev_hole_ops
||
3054 vd
->vdev_ops
== &vdev_missing_ops
);
3058 vdev_readable(vdev_t
*vd
)
3060 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_read
);
3064 vdev_writeable(vdev_t
*vd
)
3066 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_write
&&
3067 vdev_is_concrete(vd
));
3071 vdev_allocatable(vdev_t
*vd
)
3073 uint64_t state
= vd
->vdev_state
;
3076 * We currently allow allocations from vdevs which may be in the
3077 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3078 * fails to reopen then we'll catch it later when we're holding
3079 * the proper locks. Note that we have to get the vdev state
3080 * in a local variable because although it changes atomically,
3081 * we're asking two separate questions about it.
3083 return (!(state
< VDEV_STATE_DEGRADED
&& state
!= VDEV_STATE_CLOSED
) &&
3084 !vd
->vdev_cant_write
&& vdev_is_concrete(vd
) &&
3085 vd
->vdev_mg
->mg_initialized
);
3089 vdev_accessible(vdev_t
*vd
, zio_t
*zio
)
3091 ASSERT(zio
->io_vd
== vd
);
3093 if (vdev_is_dead(vd
) || vd
->vdev_remove_wanted
)
3096 if (zio
->io_type
== ZIO_TYPE_READ
)
3097 return (!vd
->vdev_cant_read
);
3099 if (zio
->io_type
== ZIO_TYPE_WRITE
)
3100 return (!vd
->vdev_cant_write
);
3106 vdev_get_child_stat(vdev_t
*cvd
, vdev_stat_t
*vs
, vdev_stat_t
*cvs
)
3109 for (t
= 0; t
< ZIO_TYPES
; t
++) {
3110 vs
->vs_ops
[t
] += cvs
->vs_ops
[t
];
3111 vs
->vs_bytes
[t
] += cvs
->vs_bytes
[t
];
3114 cvs
->vs_scan_removing
= cvd
->vdev_removing
;
3118 * Get extended stats
3121 vdev_get_child_stat_ex(vdev_t
*cvd
, vdev_stat_ex_t
*vsx
, vdev_stat_ex_t
*cvsx
)
3124 for (t
= 0; t
< ZIO_TYPES
; t
++) {
3125 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_disk_histo
[0]); b
++)
3126 vsx
->vsx_disk_histo
[t
][b
] += cvsx
->vsx_disk_histo
[t
][b
];
3128 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_total_histo
[0]); b
++) {
3129 vsx
->vsx_total_histo
[t
][b
] +=
3130 cvsx
->vsx_total_histo
[t
][b
];
3134 for (t
= 0; t
< ZIO_PRIORITY_NUM_QUEUEABLE
; t
++) {
3135 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_queue_histo
[0]); b
++) {
3136 vsx
->vsx_queue_histo
[t
][b
] +=
3137 cvsx
->vsx_queue_histo
[t
][b
];
3139 vsx
->vsx_active_queue
[t
] += cvsx
->vsx_active_queue
[t
];
3140 vsx
->vsx_pend_queue
[t
] += cvsx
->vsx_pend_queue
[t
];
3142 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_ind_histo
[0]); b
++)
3143 vsx
->vsx_ind_histo
[t
][b
] += cvsx
->vsx_ind_histo
[t
][b
];
3145 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_agg_histo
[0]); b
++)
3146 vsx
->vsx_agg_histo
[t
][b
] += cvsx
->vsx_agg_histo
[t
][b
];
3152 * Get statistics for the given vdev.
3155 vdev_get_stats_ex_impl(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
3159 * If we're getting stats on the root vdev, aggregate the I/O counts
3160 * over all top-level vdevs (i.e. the direct children of the root).
3162 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3164 memset(vs
->vs_ops
, 0, sizeof (vs
->vs_ops
));
3165 memset(vs
->vs_bytes
, 0, sizeof (vs
->vs_bytes
));
3168 memset(vsx
, 0, sizeof (*vsx
));
3170 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3171 vdev_t
*cvd
= vd
->vdev_child
[c
];
3172 vdev_stat_t
*cvs
= &cvd
->vdev_stat
;
3173 vdev_stat_ex_t
*cvsx
= &cvd
->vdev_stat_ex
;
3175 vdev_get_stats_ex_impl(cvd
, cvs
, cvsx
);
3177 vdev_get_child_stat(cvd
, vs
, cvs
);
3179 vdev_get_child_stat_ex(cvd
, vsx
, cvsx
);
3184 * We're a leaf. Just copy our ZIO active queue stats in. The
3185 * other leaf stats are updated in vdev_stat_update().
3190 memcpy(vsx
, &vd
->vdev_stat_ex
, sizeof (vd
->vdev_stat_ex
));
3192 for (t
= 0; t
< ARRAY_SIZE(vd
->vdev_queue
.vq_class
); t
++) {
3193 vsx
->vsx_active_queue
[t
] =
3194 vd
->vdev_queue
.vq_class
[t
].vqc_active
;
3195 vsx
->vsx_pend_queue
[t
] = avl_numnodes(
3196 &vd
->vdev_queue
.vq_class
[t
].vqc_queued_tree
);
3202 vdev_get_stats_ex(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
3204 vdev_t
*tvd
= vd
->vdev_top
;
3205 mutex_enter(&vd
->vdev_stat_lock
);
3207 bcopy(&vd
->vdev_stat
, vs
, sizeof (*vs
));
3208 vs
->vs_timestamp
= gethrtime() - vs
->vs_timestamp
;
3209 vs
->vs_state
= vd
->vdev_state
;
3210 vs
->vs_rsize
= vdev_get_min_asize(vd
);
3211 if (vd
->vdev_ops
->vdev_op_leaf
)
3212 vs
->vs_rsize
+= VDEV_LABEL_START_SIZE
+
3213 VDEV_LABEL_END_SIZE
;
3215 * Report expandable space on top-level, non-auxillary devices
3216 * only. The expandable space is reported in terms of metaslab
3217 * sized units since that determines how much space the pool
3220 if (vd
->vdev_aux
== NULL
&& tvd
!= NULL
) {
3221 vs
->vs_esize
= P2ALIGN(
3222 vd
->vdev_max_asize
- vd
->vdev_asize
,
3223 1ULL << tvd
->vdev_ms_shift
);
3225 vs
->vs_esize
= vd
->vdev_max_asize
- vd
->vdev_asize
;
3226 if (vd
->vdev_aux
== NULL
&& vd
== vd
->vdev_top
&&
3227 vdev_is_concrete(vd
)) {
3228 vs
->vs_fragmentation
= vd
->vdev_mg
->mg_fragmentation
;
3232 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_READER
) != 0);
3233 vdev_get_stats_ex_impl(vd
, vs
, vsx
);
3234 mutex_exit(&vd
->vdev_stat_lock
);
3238 vdev_get_stats(vdev_t
*vd
, vdev_stat_t
*vs
)
3240 return (vdev_get_stats_ex(vd
, vs
, NULL
));
3244 vdev_clear_stats(vdev_t
*vd
)
3246 mutex_enter(&vd
->vdev_stat_lock
);
3247 vd
->vdev_stat
.vs_space
= 0;
3248 vd
->vdev_stat
.vs_dspace
= 0;
3249 vd
->vdev_stat
.vs_alloc
= 0;
3250 mutex_exit(&vd
->vdev_stat_lock
);
3254 vdev_scan_stat_init(vdev_t
*vd
)
3256 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3258 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3259 vdev_scan_stat_init(vd
->vdev_child
[c
]);
3261 mutex_enter(&vd
->vdev_stat_lock
);
3262 vs
->vs_scan_processed
= 0;
3263 mutex_exit(&vd
->vdev_stat_lock
);
3267 vdev_stat_update(zio_t
*zio
, uint64_t psize
)
3269 spa_t
*spa
= zio
->io_spa
;
3270 vdev_t
*rvd
= spa
->spa_root_vdev
;
3271 vdev_t
*vd
= zio
->io_vd
? zio
->io_vd
: rvd
;
3273 uint64_t txg
= zio
->io_txg
;
3274 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3275 vdev_stat_ex_t
*vsx
= &vd
->vdev_stat_ex
;
3276 zio_type_t type
= zio
->io_type
;
3277 int flags
= zio
->io_flags
;
3280 * If this i/o is a gang leader, it didn't do any actual work.
3282 if (zio
->io_gang_tree
)
3285 if (zio
->io_error
== 0) {
3287 * If this is a root i/o, don't count it -- we've already
3288 * counted the top-level vdevs, and vdev_get_stats() will
3289 * aggregate them when asked. This reduces contention on
3290 * the root vdev_stat_lock and implicitly handles blocks
3291 * that compress away to holes, for which there is no i/o.
3292 * (Holes never create vdev children, so all the counters
3293 * remain zero, which is what we want.)
3295 * Note: this only applies to successful i/o (io_error == 0)
3296 * because unlike i/o counts, errors are not additive.
3297 * When reading a ditto block, for example, failure of
3298 * one top-level vdev does not imply a root-level error.
3303 ASSERT(vd
== zio
->io_vd
);
3305 if (flags
& ZIO_FLAG_IO_BYPASS
)
3308 mutex_enter(&vd
->vdev_stat_lock
);
3310 if (flags
& ZIO_FLAG_IO_REPAIR
) {
3311 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
3312 dsl_scan_phys_t
*scn_phys
=
3313 &spa
->spa_dsl_pool
->dp_scan
->scn_phys
;
3314 uint64_t *processed
= &scn_phys
->scn_processed
;
3317 if (vd
->vdev_ops
->vdev_op_leaf
)
3318 atomic_add_64(processed
, psize
);
3319 vs
->vs_scan_processed
+= psize
;
3322 if (flags
& ZIO_FLAG_SELF_HEAL
)
3323 vs
->vs_self_healed
+= psize
;
3327 * The bytes/ops/histograms are recorded at the leaf level and
3328 * aggregated into the higher level vdevs in vdev_get_stats().
3330 if (vd
->vdev_ops
->vdev_op_leaf
&&
3331 (zio
->io_priority
< ZIO_PRIORITY_NUM_QUEUEABLE
)) {
3334 vs
->vs_bytes
[type
] += psize
;
3336 if (flags
& ZIO_FLAG_DELEGATED
) {
3337 vsx
->vsx_agg_histo
[zio
->io_priority
]
3338 [RQ_HISTO(zio
->io_size
)]++;
3340 vsx
->vsx_ind_histo
[zio
->io_priority
]
3341 [RQ_HISTO(zio
->io_size
)]++;
3344 if (zio
->io_delta
&& zio
->io_delay
) {
3345 vsx
->vsx_queue_histo
[zio
->io_priority
]
3346 [L_HISTO(zio
->io_delta
- zio
->io_delay
)]++;
3347 vsx
->vsx_disk_histo
[type
]
3348 [L_HISTO(zio
->io_delay
)]++;
3349 vsx
->vsx_total_histo
[type
]
3350 [L_HISTO(zio
->io_delta
)]++;
3354 mutex_exit(&vd
->vdev_stat_lock
);
3358 if (flags
& ZIO_FLAG_SPECULATIVE
)
3362 * If this is an I/O error that is going to be retried, then ignore the
3363 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
3364 * hard errors, when in reality they can happen for any number of
3365 * innocuous reasons (bus resets, MPxIO link failure, etc).
3367 if (zio
->io_error
== EIO
&&
3368 !(zio
->io_flags
& ZIO_FLAG_IO_RETRY
))
3372 * Intent logs writes won't propagate their error to the root
3373 * I/O so don't mark these types of failures as pool-level
3376 if (zio
->io_vd
== NULL
&& (zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
3379 mutex_enter(&vd
->vdev_stat_lock
);
3380 if (type
== ZIO_TYPE_READ
&& !vdev_is_dead(vd
)) {
3381 if (zio
->io_error
== ECKSUM
)
3382 vs
->vs_checksum_errors
++;
3384 vs
->vs_read_errors
++;
3386 if (type
== ZIO_TYPE_WRITE
&& !vdev_is_dead(vd
))
3387 vs
->vs_write_errors
++;
3388 mutex_exit(&vd
->vdev_stat_lock
);
3390 if (spa
->spa_load_state
== SPA_LOAD_NONE
&&
3391 type
== ZIO_TYPE_WRITE
&& txg
!= 0 &&
3392 (!(flags
& ZIO_FLAG_IO_REPAIR
) ||
3393 (flags
& ZIO_FLAG_SCAN_THREAD
) ||
3394 spa
->spa_claiming
)) {
3396 * This is either a normal write (not a repair), or it's
3397 * a repair induced by the scrub thread, or it's a repair
3398 * made by zil_claim() during spa_load() in the first txg.
3399 * In the normal case, we commit the DTL change in the same
3400 * txg as the block was born. In the scrub-induced repair
3401 * case, we know that scrubs run in first-pass syncing context,
3402 * so we commit the DTL change in spa_syncing_txg(spa).
3403 * In the zil_claim() case, we commit in spa_first_txg(spa).
3405 * We currently do not make DTL entries for failed spontaneous
3406 * self-healing writes triggered by normal (non-scrubbing)
3407 * reads, because we have no transactional context in which to
3408 * do so -- and it's not clear that it'd be desirable anyway.
3410 if (vd
->vdev_ops
->vdev_op_leaf
) {
3411 uint64_t commit_txg
= txg
;
3412 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
3413 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
3414 ASSERT(spa_sync_pass(spa
) == 1);
3415 vdev_dtl_dirty(vd
, DTL_SCRUB
, txg
, 1);
3416 commit_txg
= spa_syncing_txg(spa
);
3417 } else if (spa
->spa_claiming
) {
3418 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
3419 commit_txg
= spa_first_txg(spa
);
3421 ASSERT(commit_txg
>= spa_syncing_txg(spa
));
3422 if (vdev_dtl_contains(vd
, DTL_MISSING
, txg
, 1))
3424 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3425 vdev_dtl_dirty(pvd
, DTL_PARTIAL
, txg
, 1);
3426 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, commit_txg
);
3429 vdev_dtl_dirty(vd
, DTL_MISSING
, txg
, 1);
3434 * Update the in-core space usage stats for this vdev, its metaslab class,
3435 * and the root vdev.
3438 vdev_space_update(vdev_t
*vd
, int64_t alloc_delta
, int64_t defer_delta
,
3439 int64_t space_delta
)
3441 int64_t dspace_delta
= space_delta
;
3442 spa_t
*spa
= vd
->vdev_spa
;
3443 vdev_t
*rvd
= spa
->spa_root_vdev
;
3444 metaslab_group_t
*mg
= vd
->vdev_mg
;
3445 metaslab_class_t
*mc
= mg
? mg
->mg_class
: NULL
;
3447 ASSERT(vd
== vd
->vdev_top
);
3450 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
3451 * factor. We must calculate this here and not at the root vdev
3452 * because the root vdev's psize-to-asize is simply the max of its
3453 * childrens', thus not accurate enough for us.
3455 ASSERT((dspace_delta
& (SPA_MINBLOCKSIZE
-1)) == 0);
3456 ASSERT(vd
->vdev_deflate_ratio
!= 0 || vd
->vdev_isl2cache
);
3457 dspace_delta
= (dspace_delta
>> SPA_MINBLOCKSHIFT
) *
3458 vd
->vdev_deflate_ratio
;
3460 mutex_enter(&vd
->vdev_stat_lock
);
3461 vd
->vdev_stat
.vs_alloc
+= alloc_delta
;
3462 vd
->vdev_stat
.vs_space
+= space_delta
;
3463 vd
->vdev_stat
.vs_dspace
+= dspace_delta
;
3464 mutex_exit(&vd
->vdev_stat_lock
);
3466 if (mc
== spa_normal_class(spa
)) {
3467 mutex_enter(&rvd
->vdev_stat_lock
);
3468 rvd
->vdev_stat
.vs_alloc
+= alloc_delta
;
3469 rvd
->vdev_stat
.vs_space
+= space_delta
;
3470 rvd
->vdev_stat
.vs_dspace
+= dspace_delta
;
3471 mutex_exit(&rvd
->vdev_stat_lock
);
3475 ASSERT(rvd
== vd
->vdev_parent
);
3476 ASSERT(vd
->vdev_ms_count
!= 0);
3478 metaslab_class_space_update(mc
,
3479 alloc_delta
, defer_delta
, space_delta
, dspace_delta
);
3484 * Mark a top-level vdev's config as dirty, placing it on the dirty list
3485 * so that it will be written out next time the vdev configuration is synced.
3486 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3489 vdev_config_dirty(vdev_t
*vd
)
3491 spa_t
*spa
= vd
->vdev_spa
;
3492 vdev_t
*rvd
= spa
->spa_root_vdev
;
3495 ASSERT(spa_writeable(spa
));
3498 * If this is an aux vdev (as with l2cache and spare devices), then we
3499 * update the vdev config manually and set the sync flag.
3501 if (vd
->vdev_aux
!= NULL
) {
3502 spa_aux_vdev_t
*sav
= vd
->vdev_aux
;
3506 for (c
= 0; c
< sav
->sav_count
; c
++) {
3507 if (sav
->sav_vdevs
[c
] == vd
)
3511 if (c
== sav
->sav_count
) {
3513 * We're being removed. There's nothing more to do.
3515 ASSERT(sav
->sav_sync
== B_TRUE
);
3519 sav
->sav_sync
= B_TRUE
;
3521 if (nvlist_lookup_nvlist_array(sav
->sav_config
,
3522 ZPOOL_CONFIG_L2CACHE
, &aux
, &naux
) != 0) {
3523 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
3524 ZPOOL_CONFIG_SPARES
, &aux
, &naux
) == 0);
3530 * Setting the nvlist in the middle if the array is a little
3531 * sketchy, but it will work.
3533 nvlist_free(aux
[c
]);
3534 aux
[c
] = vdev_config_generate(spa
, vd
, B_TRUE
, 0);
3540 * The dirty list is protected by the SCL_CONFIG lock. The caller
3541 * must either hold SCL_CONFIG as writer, or must be the sync thread
3542 * (which holds SCL_CONFIG as reader). There's only one sync thread,
3543 * so this is sufficient to ensure mutual exclusion.
3545 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
3546 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3547 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
3550 for (c
= 0; c
< rvd
->vdev_children
; c
++)
3551 vdev_config_dirty(rvd
->vdev_child
[c
]);
3553 ASSERT(vd
== vd
->vdev_top
);
3555 if (!list_link_active(&vd
->vdev_config_dirty_node
) &&
3556 vdev_is_concrete(vd
)) {
3557 list_insert_head(&spa
->spa_config_dirty_list
, vd
);
3563 vdev_config_clean(vdev_t
*vd
)
3565 spa_t
*spa
= vd
->vdev_spa
;
3567 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
3568 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3569 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
3571 ASSERT(list_link_active(&vd
->vdev_config_dirty_node
));
3572 list_remove(&spa
->spa_config_dirty_list
, vd
);
3576 * Mark a top-level vdev's state as dirty, so that the next pass of
3577 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3578 * the state changes from larger config changes because they require
3579 * much less locking, and are often needed for administrative actions.
3582 vdev_state_dirty(vdev_t
*vd
)
3584 spa_t
*spa
= vd
->vdev_spa
;
3586 ASSERT(spa_writeable(spa
));
3587 ASSERT(vd
== vd
->vdev_top
);
3590 * The state list is protected by the SCL_STATE lock. The caller
3591 * must either hold SCL_STATE as writer, or must be the sync thread
3592 * (which holds SCL_STATE as reader). There's only one sync thread,
3593 * so this is sufficient to ensure mutual exclusion.
3595 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
3596 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3597 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
3599 if (!list_link_active(&vd
->vdev_state_dirty_node
) &&
3600 vdev_is_concrete(vd
))
3601 list_insert_head(&spa
->spa_state_dirty_list
, vd
);
3605 vdev_state_clean(vdev_t
*vd
)
3607 spa_t
*spa
= vd
->vdev_spa
;
3609 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
3610 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
3611 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
3613 ASSERT(list_link_active(&vd
->vdev_state_dirty_node
));
3614 list_remove(&spa
->spa_state_dirty_list
, vd
);
3618 * Propagate vdev state up from children to parent.
3621 vdev_propagate_state(vdev_t
*vd
)
3623 spa_t
*spa
= vd
->vdev_spa
;
3624 vdev_t
*rvd
= spa
->spa_root_vdev
;
3625 int degraded
= 0, faulted
= 0;
3629 if (vd
->vdev_children
> 0) {
3630 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3631 child
= vd
->vdev_child
[c
];
3634 * Don't factor holes or indirect vdevs into the
3637 if (!vdev_is_concrete(child
))
3640 if (!vdev_readable(child
) ||
3641 (!vdev_writeable(child
) && spa_writeable(spa
))) {
3643 * Root special: if there is a top-level log
3644 * device, treat the root vdev as if it were
3647 if (child
->vdev_islog
&& vd
== rvd
)
3651 } else if (child
->vdev_state
<= VDEV_STATE_DEGRADED
) {
3655 if (child
->vdev_stat
.vs_aux
== VDEV_AUX_CORRUPT_DATA
)
3659 vd
->vdev_ops
->vdev_op_state_change(vd
, faulted
, degraded
);
3662 * Root special: if there is a top-level vdev that cannot be
3663 * opened due to corrupted metadata, then propagate the root
3664 * vdev's aux state as 'corrupt' rather than 'insufficient
3667 if (corrupted
&& vd
== rvd
&&
3668 rvd
->vdev_state
== VDEV_STATE_CANT_OPEN
)
3669 vdev_set_state(rvd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3670 VDEV_AUX_CORRUPT_DATA
);
3673 if (vd
->vdev_parent
)
3674 vdev_propagate_state(vd
->vdev_parent
);
3678 * Set a vdev's state. If this is during an open, we don't update the parent
3679 * state, because we're in the process of opening children depth-first.
3680 * Otherwise, we propagate the change to the parent.
3682 * If this routine places a device in a faulted state, an appropriate ereport is
3686 vdev_set_state(vdev_t
*vd
, boolean_t isopen
, vdev_state_t state
, vdev_aux_t aux
)
3688 uint64_t save_state
;
3689 spa_t
*spa
= vd
->vdev_spa
;
3691 if (state
== vd
->vdev_state
) {
3693 * Since vdev_offline() code path is already in an offline
3694 * state we can miss a statechange event to OFFLINE. Check
3695 * the previous state to catch this condition.
3697 if (vd
->vdev_ops
->vdev_op_leaf
&&
3698 (state
== VDEV_STATE_OFFLINE
) &&
3699 (vd
->vdev_prevstate
>= VDEV_STATE_FAULTED
)) {
3700 /* post an offline state change */
3701 zfs_post_state_change(spa
, vd
, vd
->vdev_prevstate
);
3703 vd
->vdev_stat
.vs_aux
= aux
;
3707 save_state
= vd
->vdev_state
;
3709 vd
->vdev_state
= state
;
3710 vd
->vdev_stat
.vs_aux
= aux
;
3713 * If we are setting the vdev state to anything but an open state, then
3714 * always close the underlying device unless the device has requested
3715 * a delayed close (i.e. we're about to remove or fault the device).
3716 * Otherwise, we keep accessible but invalid devices open forever.
3717 * We don't call vdev_close() itself, because that implies some extra
3718 * checks (offline, etc) that we don't want here. This is limited to
3719 * leaf devices, because otherwise closing the device will affect other
3722 if (!vd
->vdev_delayed_close
&& vdev_is_dead(vd
) &&
3723 vd
->vdev_ops
->vdev_op_leaf
)
3724 vd
->vdev_ops
->vdev_op_close(vd
);
3726 if (vd
->vdev_removed
&&
3727 state
== VDEV_STATE_CANT_OPEN
&&
3728 (aux
== VDEV_AUX_OPEN_FAILED
|| vd
->vdev_checkremove
)) {
3730 * If the previous state is set to VDEV_STATE_REMOVED, then this
3731 * device was previously marked removed and someone attempted to
3732 * reopen it. If this failed due to a nonexistent device, then
3733 * keep the device in the REMOVED state. We also let this be if
3734 * it is one of our special test online cases, which is only
3735 * attempting to online the device and shouldn't generate an FMA
3738 vd
->vdev_state
= VDEV_STATE_REMOVED
;
3739 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
3740 } else if (state
== VDEV_STATE_REMOVED
) {
3741 vd
->vdev_removed
= B_TRUE
;
3742 } else if (state
== VDEV_STATE_CANT_OPEN
) {
3744 * If we fail to open a vdev during an import or recovery, we
3745 * mark it as "not available", which signifies that it was
3746 * never there to begin with. Failure to open such a device
3747 * is not considered an error.
3749 if ((spa_load_state(spa
) == SPA_LOAD_IMPORT
||
3750 spa_load_state(spa
) == SPA_LOAD_RECOVER
) &&
3751 vd
->vdev_ops
->vdev_op_leaf
)
3752 vd
->vdev_not_present
= 1;
3755 * Post the appropriate ereport. If the 'prevstate' field is
3756 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3757 * that this is part of a vdev_reopen(). In this case, we don't
3758 * want to post the ereport if the device was already in the
3759 * CANT_OPEN state beforehand.
3761 * If the 'checkremove' flag is set, then this is an attempt to
3762 * online the device in response to an insertion event. If we
3763 * hit this case, then we have detected an insertion event for a
3764 * faulted or offline device that wasn't in the removed state.
3765 * In this scenario, we don't post an ereport because we are
3766 * about to replace the device, or attempt an online with
3767 * vdev_forcefault, which will generate the fault for us.
3769 if ((vd
->vdev_prevstate
!= state
|| vd
->vdev_forcefault
) &&
3770 !vd
->vdev_not_present
&& !vd
->vdev_checkremove
&&
3771 vd
!= spa
->spa_root_vdev
) {
3775 case VDEV_AUX_OPEN_FAILED
:
3776 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED
;
3778 case VDEV_AUX_CORRUPT_DATA
:
3779 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA
;
3781 case VDEV_AUX_NO_REPLICAS
:
3782 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS
;
3784 case VDEV_AUX_BAD_GUID_SUM
:
3785 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM
;
3787 case VDEV_AUX_TOO_SMALL
:
3788 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL
;
3790 case VDEV_AUX_BAD_LABEL
:
3791 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL
;
3793 case VDEV_AUX_BAD_ASHIFT
:
3794 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
;
3797 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN
;
3800 zfs_ereport_post(class, spa
, vd
, NULL
, NULL
,
3804 /* Erase any notion of persistent removed state */
3805 vd
->vdev_removed
= B_FALSE
;
3807 vd
->vdev_removed
= B_FALSE
;
3811 * Notify ZED of any significant state-change on a leaf vdev.
3814 if (vd
->vdev_ops
->vdev_op_leaf
) {
3815 /* preserve original state from a vdev_reopen() */
3816 if ((vd
->vdev_prevstate
!= VDEV_STATE_UNKNOWN
) &&
3817 (vd
->vdev_prevstate
!= vd
->vdev_state
) &&
3818 (save_state
<= VDEV_STATE_CLOSED
))
3819 save_state
= vd
->vdev_prevstate
;
3821 /* filter out state change due to initial vdev_open */
3822 if (save_state
> VDEV_STATE_CLOSED
)
3823 zfs_post_state_change(spa
, vd
, save_state
);
3826 if (!isopen
&& vd
->vdev_parent
)
3827 vdev_propagate_state(vd
->vdev_parent
);
3831 * Check the vdev configuration to ensure that it's capable of supporting
3832 * a root pool. We do not support partial configuration.
3835 vdev_is_bootable(vdev_t
*vd
)
3837 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3838 const char *vdev_type
= vd
->vdev_ops
->vdev_op_type
;
3840 if (strcmp(vdev_type
, VDEV_TYPE_MISSING
) == 0 ||
3841 strcmp(vdev_type
, VDEV_TYPE_INDIRECT
) == 0) {
3846 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3847 if (!vdev_is_bootable(vd
->vdev_child
[c
]))
3854 vdev_is_concrete(vdev_t
*vd
)
3856 vdev_ops_t
*ops
= vd
->vdev_ops
;
3857 if (ops
== &vdev_indirect_ops
|| ops
== &vdev_hole_ops
||
3858 ops
== &vdev_missing_ops
|| ops
== &vdev_root_ops
) {
3866 * Load the state from the original vdev tree (ovd) which
3867 * we've retrieved from the MOS config object. If the original
3868 * vdev was offline or faulted then we transfer that state to the
3869 * device in the current vdev tree (nvd).
3872 vdev_load_log_state(vdev_t
*nvd
, vdev_t
*ovd
)
3874 ASSERT(nvd
->vdev_top
->vdev_islog
);
3875 ASSERT(spa_config_held(nvd
->vdev_spa
,
3876 SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
3877 ASSERT3U(nvd
->vdev_guid
, ==, ovd
->vdev_guid
);
3879 for (int c
= 0; c
< nvd
->vdev_children
; c
++)
3880 vdev_load_log_state(nvd
->vdev_child
[c
], ovd
->vdev_child
[c
]);
3882 if (nvd
->vdev_ops
->vdev_op_leaf
) {
3884 * Restore the persistent vdev state
3886 nvd
->vdev_offline
= ovd
->vdev_offline
;
3887 nvd
->vdev_faulted
= ovd
->vdev_faulted
;
3888 nvd
->vdev_degraded
= ovd
->vdev_degraded
;
3889 nvd
->vdev_removed
= ovd
->vdev_removed
;
3894 * Determine if a log device has valid content. If the vdev was
3895 * removed or faulted in the MOS config then we know that
3896 * the content on the log device has already been written to the pool.
3899 vdev_log_state_valid(vdev_t
*vd
)
3901 if (vd
->vdev_ops
->vdev_op_leaf
&& !vd
->vdev_faulted
&&
3905 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3906 if (vdev_log_state_valid(vd
->vdev_child
[c
]))
3913 * Expand a vdev if possible.
3916 vdev_expand(vdev_t
*vd
, uint64_t txg
)
3918 ASSERT(vd
->vdev_top
== vd
);
3919 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3921 vdev_set_deflate_ratio(vd
);
3923 if ((vd
->vdev_asize
>> vd
->vdev_ms_shift
) > vd
->vdev_ms_count
&&
3924 vdev_is_concrete(vd
)) {
3925 VERIFY(vdev_metaslab_init(vd
, txg
) == 0);
3926 vdev_config_dirty(vd
);
3934 vdev_split(vdev_t
*vd
)
3936 vdev_t
*cvd
, *pvd
= vd
->vdev_parent
;
3938 vdev_remove_child(pvd
, vd
);
3939 vdev_compact_children(pvd
);
3941 cvd
= pvd
->vdev_child
[0];
3942 if (pvd
->vdev_children
== 1) {
3943 vdev_remove_parent(cvd
);
3944 cvd
->vdev_splitting
= B_TRUE
;
3946 vdev_propagate_state(cvd
);
3950 vdev_deadman(vdev_t
*vd
, char *tag
)
3952 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3953 vdev_t
*cvd
= vd
->vdev_child
[c
];
3955 vdev_deadman(cvd
, tag
);
3958 if (vd
->vdev_ops
->vdev_op_leaf
) {
3959 vdev_queue_t
*vq
= &vd
->vdev_queue
;
3961 mutex_enter(&vq
->vq_lock
);
3962 if (avl_numnodes(&vq
->vq_active_tree
) > 0) {
3963 spa_t
*spa
= vd
->vdev_spa
;
3967 zfs_dbgmsg("slow vdev: %s has %d active IOs",
3968 vd
->vdev_path
, avl_numnodes(&vq
->vq_active_tree
));
3971 * Look at the head of all the pending queues,
3972 * if any I/O has been outstanding for longer than
3973 * the spa_deadman_synctime invoke the deadman logic.
3975 fio
= avl_first(&vq
->vq_active_tree
);
3976 delta
= gethrtime() - fio
->io_timestamp
;
3977 if (delta
> spa_deadman_synctime(spa
))
3978 zio_deadman(fio
, tag
);
3980 mutex_exit(&vq
->vq_lock
);
3984 #if defined(_KERNEL) && defined(HAVE_SPL)
3985 EXPORT_SYMBOL(vdev_fault
);
3986 EXPORT_SYMBOL(vdev_degrade
);
3987 EXPORT_SYMBOL(vdev_online
);
3988 EXPORT_SYMBOL(vdev_offline
);
3989 EXPORT_SYMBOL(vdev_clear
);
3991 module_param(metaslabs_per_vdev
, int, 0644);
3992 MODULE_PARM_DESC(metaslabs_per_vdev
,
3993 "Divide added vdev into approximately (but no more than) this number "
3996 module_param(zfs_delays_per_second
, uint
, 0644);
3997 MODULE_PARM_DESC(zfs_delays_per_second
, "Rate limit delay events to this many "
3998 "IO delays per second");
4000 module_param(zfs_checksums_per_second
, uint
, 0644);
4001 MODULE_PARM_DESC(zfs_checksums_per_second
, "Rate limit checksum events "
4002 "to this many checksum errors per second (do not set below zed"
4005 module_param(zfs_scan_ignore_errors
, int, 0644);
4006 MODULE_PARM_DESC(zfs_scan_ignore_errors
,
4007 "Ignore errors during resilver/scrub");