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, 2018 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.
29 * Copyright (c) 2017, Intel Corporation.
32 #include <sys/zfs_context.h>
33 #include <sys/fm/fs/zfs.h>
35 #include <sys/spa_impl.h>
36 #include <sys/bpobj.h>
38 #include <sys/dmu_tx.h>
39 #include <sys/dsl_dir.h>
40 #include <sys/vdev_impl.h>
41 #include <sys/uberblock_impl.h>
42 #include <sys/metaslab.h>
43 #include <sys/metaslab_impl.h>
44 #include <sys/space_map.h>
45 #include <sys/space_reftree.h>
48 #include <sys/fs/zfs.h>
51 #include <sys/dsl_scan.h>
54 #include <sys/zfs_ratelimit.h>
56 /* target number of metaslabs per top-level vdev */
57 int vdev_max_ms_count
= 200;
59 /* minimum number of metaslabs per top-level vdev */
60 int vdev_min_ms_count
= 16;
62 /* practical upper limit of total metaslabs per top-level vdev */
63 int vdev_ms_count_limit
= 1ULL << 17;
65 /* lower limit for metaslab size (512M) */
66 int vdev_default_ms_shift
= 29;
68 /* upper limit for metaslab size (256G) */
69 int vdev_max_ms_shift
= 38;
71 int vdev_validate_skip
= B_FALSE
;
74 * Since the DTL space map of a vdev is not expected to have a lot of
75 * entries, we default its block size to 4K.
77 int vdev_dtl_sm_blksz
= (1 << 12);
80 * Rate limit delay events to this many IO delays per second.
82 unsigned int zfs_delays_per_second
= 20;
85 * Rate limit checksum events after this many checksum errors per second.
87 unsigned int zfs_checksums_per_second
= 20;
90 * Ignore errors during scrub/resilver. Allows to work around resilver
91 * upon import when there are pool errors.
93 int zfs_scan_ignore_errors
= 0;
96 * vdev-wide space maps that have lots of entries written to them at
97 * the end of each transaction can benefit from a higher I/O bandwidth
98 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
100 int vdev_standard_sm_blksz
= (1 << 17);
104 vdev_dbgmsg(vdev_t
*vd
, const char *fmt
, ...)
110 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
113 if (vd
->vdev_path
!= NULL
) {
114 zfs_dbgmsg("%s vdev '%s': %s", vd
->vdev_ops
->vdev_op_type
,
117 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
118 vd
->vdev_ops
->vdev_op_type
,
119 (u_longlong_t
)vd
->vdev_id
,
120 (u_longlong_t
)vd
->vdev_guid
, buf
);
125 vdev_dbgmsg_print_tree(vdev_t
*vd
, int indent
)
129 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
) {
130 zfs_dbgmsg("%*svdev %u: %s", indent
, "", vd
->vdev_id
,
131 vd
->vdev_ops
->vdev_op_type
);
135 switch (vd
->vdev_state
) {
136 case VDEV_STATE_UNKNOWN
:
137 (void) snprintf(state
, sizeof (state
), "unknown");
139 case VDEV_STATE_CLOSED
:
140 (void) snprintf(state
, sizeof (state
), "closed");
142 case VDEV_STATE_OFFLINE
:
143 (void) snprintf(state
, sizeof (state
), "offline");
145 case VDEV_STATE_REMOVED
:
146 (void) snprintf(state
, sizeof (state
), "removed");
148 case VDEV_STATE_CANT_OPEN
:
149 (void) snprintf(state
, sizeof (state
), "can't open");
151 case VDEV_STATE_FAULTED
:
152 (void) snprintf(state
, sizeof (state
), "faulted");
154 case VDEV_STATE_DEGRADED
:
155 (void) snprintf(state
, sizeof (state
), "degraded");
157 case VDEV_STATE_HEALTHY
:
158 (void) snprintf(state
, sizeof (state
), "healthy");
161 (void) snprintf(state
, sizeof (state
), "<state %u>",
162 (uint_t
)vd
->vdev_state
);
165 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent
,
166 "", (int)vd
->vdev_id
, vd
->vdev_ops
->vdev_op_type
,
167 vd
->vdev_islog
? " (log)" : "",
168 (u_longlong_t
)vd
->vdev_guid
,
169 vd
->vdev_path
? vd
->vdev_path
: "N/A", state
);
171 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++)
172 vdev_dbgmsg_print_tree(vd
->vdev_child
[i
], indent
+ 2);
176 * Virtual device management.
179 static vdev_ops_t
*vdev_ops_table
[] = {
194 * Given a vdev type, return the appropriate ops vector.
197 vdev_getops(const char *type
)
199 vdev_ops_t
*ops
, **opspp
;
201 for (opspp
= vdev_ops_table
; (ops
= *opspp
) != NULL
; opspp
++)
202 if (strcmp(ops
->vdev_op_type
, type
) == 0)
209 * Derive the enumerated alloction bias from string input.
210 * String origin is either the per-vdev zap or zpool(1M).
212 static vdev_alloc_bias_t
213 vdev_derive_alloc_bias(const char *bias
)
215 vdev_alloc_bias_t alloc_bias
= VDEV_BIAS_NONE
;
217 if (strcmp(bias
, VDEV_ALLOC_BIAS_LOG
) == 0)
218 alloc_bias
= VDEV_BIAS_LOG
;
219 else if (strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
220 alloc_bias
= VDEV_BIAS_SPECIAL
;
221 else if (strcmp(bias
, VDEV_ALLOC_BIAS_DEDUP
) == 0)
222 alloc_bias
= VDEV_BIAS_DEDUP
;
228 * Default asize function: return the MAX of psize with the asize of
229 * all children. This is what's used by anything other than RAID-Z.
232 vdev_default_asize(vdev_t
*vd
, uint64_t psize
)
234 uint64_t asize
= P2ROUNDUP(psize
, 1ULL << vd
->vdev_top
->vdev_ashift
);
237 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
238 csize
= vdev_psize_to_asize(vd
->vdev_child
[c
], psize
);
239 asize
= MAX(asize
, csize
);
246 * Get the minimum allocatable size. We define the allocatable size as
247 * the vdev's asize rounded to the nearest metaslab. This allows us to
248 * replace or attach devices which don't have the same physical size but
249 * can still satisfy the same number of allocations.
252 vdev_get_min_asize(vdev_t
*vd
)
254 vdev_t
*pvd
= vd
->vdev_parent
;
257 * If our parent is NULL (inactive spare or cache) or is the root,
258 * just return our own asize.
261 return (vd
->vdev_asize
);
264 * The top-level vdev just returns the allocatable size rounded
265 * to the nearest metaslab.
267 if (vd
== vd
->vdev_top
)
268 return (P2ALIGN(vd
->vdev_asize
, 1ULL << vd
->vdev_ms_shift
));
271 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
272 * so each child must provide at least 1/Nth of its asize.
274 if (pvd
->vdev_ops
== &vdev_raidz_ops
)
275 return ((pvd
->vdev_min_asize
+ pvd
->vdev_children
- 1) /
278 return (pvd
->vdev_min_asize
);
282 vdev_set_min_asize(vdev_t
*vd
)
284 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
286 for (int c
= 0; c
< vd
->vdev_children
; c
++)
287 vdev_set_min_asize(vd
->vdev_child
[c
]);
291 vdev_lookup_top(spa_t
*spa
, uint64_t vdev
)
293 vdev_t
*rvd
= spa
->spa_root_vdev
;
295 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
297 if (vdev
< rvd
->vdev_children
) {
298 ASSERT(rvd
->vdev_child
[vdev
] != NULL
);
299 return (rvd
->vdev_child
[vdev
]);
306 vdev_lookup_by_guid(vdev_t
*vd
, uint64_t guid
)
310 if (vd
->vdev_guid
== guid
)
313 for (int c
= 0; c
< vd
->vdev_children
; c
++)
314 if ((mvd
= vdev_lookup_by_guid(vd
->vdev_child
[c
], guid
)) !=
322 vdev_count_leaves_impl(vdev_t
*vd
)
326 if (vd
->vdev_ops
->vdev_op_leaf
)
329 for (int c
= 0; c
< vd
->vdev_children
; c
++)
330 n
+= vdev_count_leaves_impl(vd
->vdev_child
[c
]);
336 vdev_count_leaves(spa_t
*spa
)
340 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
341 rc
= vdev_count_leaves_impl(spa
->spa_root_vdev
);
342 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
348 vdev_add_child(vdev_t
*pvd
, vdev_t
*cvd
)
350 size_t oldsize
, newsize
;
351 uint64_t id
= cvd
->vdev_id
;
354 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
355 ASSERT(cvd
->vdev_parent
== NULL
);
357 cvd
->vdev_parent
= pvd
;
362 ASSERT(id
>= pvd
->vdev_children
|| pvd
->vdev_child
[id
] == NULL
);
364 oldsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
365 pvd
->vdev_children
= MAX(pvd
->vdev_children
, id
+ 1);
366 newsize
= pvd
->vdev_children
* sizeof (vdev_t
*);
368 newchild
= kmem_alloc(newsize
, KM_SLEEP
);
369 if (pvd
->vdev_child
!= NULL
) {
370 bcopy(pvd
->vdev_child
, newchild
, oldsize
);
371 kmem_free(pvd
->vdev_child
, oldsize
);
374 pvd
->vdev_child
= newchild
;
375 pvd
->vdev_child
[id
] = cvd
;
377 cvd
->vdev_top
= (pvd
->vdev_top
? pvd
->vdev_top
: cvd
);
378 ASSERT(cvd
->vdev_top
->vdev_parent
->vdev_parent
== NULL
);
381 * Walk up all ancestors to update guid sum.
383 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
384 pvd
->vdev_guid_sum
+= cvd
->vdev_guid_sum
;
388 vdev_remove_child(vdev_t
*pvd
, vdev_t
*cvd
)
391 uint_t id
= cvd
->vdev_id
;
393 ASSERT(cvd
->vdev_parent
== pvd
);
398 ASSERT(id
< pvd
->vdev_children
);
399 ASSERT(pvd
->vdev_child
[id
] == cvd
);
401 pvd
->vdev_child
[id
] = NULL
;
402 cvd
->vdev_parent
= NULL
;
404 for (c
= 0; c
< pvd
->vdev_children
; c
++)
405 if (pvd
->vdev_child
[c
])
408 if (c
== pvd
->vdev_children
) {
409 kmem_free(pvd
->vdev_child
, c
* sizeof (vdev_t
*));
410 pvd
->vdev_child
= NULL
;
411 pvd
->vdev_children
= 0;
415 * Walk up all ancestors to update guid sum.
417 for (; pvd
!= NULL
; pvd
= pvd
->vdev_parent
)
418 pvd
->vdev_guid_sum
-= cvd
->vdev_guid_sum
;
422 * Remove any holes in the child array.
425 vdev_compact_children(vdev_t
*pvd
)
427 vdev_t
**newchild
, *cvd
;
428 int oldc
= pvd
->vdev_children
;
431 ASSERT(spa_config_held(pvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
436 for (int c
= newc
= 0; c
< oldc
; c
++)
437 if (pvd
->vdev_child
[c
])
441 newchild
= kmem_zalloc(newc
* sizeof (vdev_t
*), KM_SLEEP
);
443 for (int c
= newc
= 0; c
< oldc
; c
++) {
444 if ((cvd
= pvd
->vdev_child
[c
]) != NULL
) {
445 newchild
[newc
] = cvd
;
446 cvd
->vdev_id
= newc
++;
453 kmem_free(pvd
->vdev_child
, oldc
* sizeof (vdev_t
*));
454 pvd
->vdev_child
= newchild
;
455 pvd
->vdev_children
= newc
;
459 * Allocate and minimally initialize a vdev_t.
462 vdev_alloc_common(spa_t
*spa
, uint_t id
, uint64_t guid
, vdev_ops_t
*ops
)
465 vdev_indirect_config_t
*vic
;
467 vd
= kmem_zalloc(sizeof (vdev_t
), KM_SLEEP
);
468 vic
= &vd
->vdev_indirect_config
;
470 if (spa
->spa_root_vdev
== NULL
) {
471 ASSERT(ops
== &vdev_root_ops
);
472 spa
->spa_root_vdev
= vd
;
473 spa
->spa_load_guid
= spa_generate_guid(NULL
);
476 if (guid
== 0 && ops
!= &vdev_hole_ops
) {
477 if (spa
->spa_root_vdev
== vd
) {
479 * The root vdev's guid will also be the pool guid,
480 * which must be unique among all pools.
482 guid
= spa_generate_guid(NULL
);
485 * Any other vdev's guid must be unique within the pool.
487 guid
= spa_generate_guid(spa
);
489 ASSERT(!spa_guid_exists(spa_guid(spa
), guid
));
494 vd
->vdev_guid
= guid
;
495 vd
->vdev_guid_sum
= guid
;
497 vd
->vdev_state
= VDEV_STATE_CLOSED
;
498 vd
->vdev_ishole
= (ops
== &vdev_hole_ops
);
499 vic
->vic_prev_indirect_vdev
= UINT64_MAX
;
501 rw_init(&vd
->vdev_indirect_rwlock
, NULL
, RW_DEFAULT
, NULL
);
502 mutex_init(&vd
->vdev_obsolete_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
503 vd
->vdev_obsolete_segments
= range_tree_create(NULL
, NULL
);
506 * Initialize rate limit structs for events. We rate limit ZIO delay
507 * and checksum events so that we don't overwhelm ZED with thousands
508 * of events when a disk is acting up.
510 zfs_ratelimit_init(&vd
->vdev_delay_rl
, &zfs_delays_per_second
, 1);
511 zfs_ratelimit_init(&vd
->vdev_checksum_rl
, &zfs_checksums_per_second
, 1);
513 list_link_init(&vd
->vdev_config_dirty_node
);
514 list_link_init(&vd
->vdev_state_dirty_node
);
515 mutex_init(&vd
->vdev_dtl_lock
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
516 mutex_init(&vd
->vdev_stat_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
517 mutex_init(&vd
->vdev_probe_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
518 mutex_init(&vd
->vdev_queue_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
519 mutex_init(&vd
->vdev_scan_io_queue_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
521 for (int t
= 0; t
< DTL_TYPES
; t
++) {
522 vd
->vdev_dtl
[t
] = range_tree_create(NULL
, NULL
);
524 txg_list_create(&vd
->vdev_ms_list
, spa
,
525 offsetof(struct metaslab
, ms_txg_node
));
526 txg_list_create(&vd
->vdev_dtl_list
, spa
,
527 offsetof(struct vdev
, vdev_dtl_node
));
528 vd
->vdev_stat
.vs_timestamp
= gethrtime();
536 * Allocate a new vdev. The 'alloctype' is used to control whether we are
537 * creating a new vdev or loading an existing one - the behavior is slightly
538 * different for each case.
541 vdev_alloc(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
, uint_t id
,
546 uint64_t guid
= 0, islog
, nparity
;
548 vdev_indirect_config_t
*vic
;
551 vdev_alloc_bias_t alloc_bias
= VDEV_BIAS_NONE
;
552 boolean_t top_level
= (parent
&& !parent
->vdev_parent
);
554 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
556 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
557 return (SET_ERROR(EINVAL
));
559 if ((ops
= vdev_getops(type
)) == NULL
)
560 return (SET_ERROR(EINVAL
));
563 * If this is a load, get the vdev guid from the nvlist.
564 * Otherwise, vdev_alloc_common() will generate one for us.
566 if (alloctype
== VDEV_ALLOC_LOAD
) {
569 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
, &label_id
) ||
571 return (SET_ERROR(EINVAL
));
573 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
574 return (SET_ERROR(EINVAL
));
575 } else if (alloctype
== VDEV_ALLOC_SPARE
) {
576 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
577 return (SET_ERROR(EINVAL
));
578 } else if (alloctype
== VDEV_ALLOC_L2CACHE
) {
579 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
580 return (SET_ERROR(EINVAL
));
581 } else if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
582 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &guid
) != 0)
583 return (SET_ERROR(EINVAL
));
587 * The first allocated vdev must be of type 'root'.
589 if (ops
!= &vdev_root_ops
&& spa
->spa_root_vdev
== NULL
)
590 return (SET_ERROR(EINVAL
));
593 * Determine whether we're a log vdev.
596 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &islog
);
597 if (islog
&& spa_version(spa
) < SPA_VERSION_SLOGS
)
598 return (SET_ERROR(ENOTSUP
));
600 if (ops
== &vdev_hole_ops
&& spa_version(spa
) < SPA_VERSION_HOLES
)
601 return (SET_ERROR(ENOTSUP
));
604 * Set the nparity property for RAID-Z vdevs.
607 if (ops
== &vdev_raidz_ops
) {
608 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
610 if (nparity
== 0 || nparity
> VDEV_RAIDZ_MAXPARITY
)
611 return (SET_ERROR(EINVAL
));
613 * Previous versions could only support 1 or 2 parity
617 spa_version(spa
) < SPA_VERSION_RAIDZ2
)
618 return (SET_ERROR(ENOTSUP
));
620 spa_version(spa
) < SPA_VERSION_RAIDZ3
)
621 return (SET_ERROR(ENOTSUP
));
624 * We require the parity to be specified for SPAs that
625 * support multiple parity levels.
627 if (spa_version(spa
) >= SPA_VERSION_RAIDZ2
)
628 return (SET_ERROR(EINVAL
));
630 * Otherwise, we default to 1 parity device for RAID-Z.
637 ASSERT(nparity
!= -1ULL);
640 * If creating a top-level vdev, check for allocation classes input
642 if (top_level
&& alloctype
== VDEV_ALLOC_ADD
) {
645 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_ALLOCATION_BIAS
,
647 alloc_bias
= vdev_derive_alloc_bias(bias
);
649 /* spa_vdev_add() expects feature to be enabled */
650 if (spa
->spa_load_state
!= SPA_LOAD_CREATE
&&
651 !spa_feature_is_enabled(spa
,
652 SPA_FEATURE_ALLOCATION_CLASSES
)) {
653 return (SET_ERROR(ENOTSUP
));
658 vd
= vdev_alloc_common(spa
, id
, guid
, ops
);
659 vic
= &vd
->vdev_indirect_config
;
661 vd
->vdev_islog
= islog
;
662 vd
->vdev_nparity
= nparity
;
663 if (top_level
&& alloc_bias
!= VDEV_BIAS_NONE
)
664 vd
->vdev_alloc_bias
= alloc_bias
;
666 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &vd
->vdev_path
) == 0)
667 vd
->vdev_path
= spa_strdup(vd
->vdev_path
);
670 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
671 * fault on a vdev and want it to persist across imports (like with
674 rc
= nvlist_lookup_string(nv
, ZPOOL_CONFIG_AUX_STATE
, &tmp
);
675 if (rc
== 0 && tmp
!= NULL
&& strcmp(tmp
, "external") == 0) {
676 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
677 vd
->vdev_faulted
= 1;
678 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
681 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &vd
->vdev_devid
) == 0)
682 vd
->vdev_devid
= spa_strdup(vd
->vdev_devid
);
683 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PHYS_PATH
,
684 &vd
->vdev_physpath
) == 0)
685 vd
->vdev_physpath
= spa_strdup(vd
->vdev_physpath
);
687 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH
,
688 &vd
->vdev_enc_sysfs_path
) == 0)
689 vd
->vdev_enc_sysfs_path
= spa_strdup(vd
->vdev_enc_sysfs_path
);
691 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_FRU
, &vd
->vdev_fru
) == 0)
692 vd
->vdev_fru
= spa_strdup(vd
->vdev_fru
);
695 * Set the whole_disk property. If it's not specified, leave the value
698 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
699 &vd
->vdev_wholedisk
) != 0)
700 vd
->vdev_wholedisk
= -1ULL;
702 ASSERT0(vic
->vic_mapping_object
);
703 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_OBJECT
,
704 &vic
->vic_mapping_object
);
705 ASSERT0(vic
->vic_births_object
);
706 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_INDIRECT_BIRTHS
,
707 &vic
->vic_births_object
);
708 ASSERT3U(vic
->vic_prev_indirect_vdev
, ==, UINT64_MAX
);
709 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_PREV_INDIRECT_VDEV
,
710 &vic
->vic_prev_indirect_vdev
);
713 * Look for the 'not present' flag. This will only be set if the device
714 * was not present at the time of import.
716 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
717 &vd
->vdev_not_present
);
720 * Get the alignment requirement.
722 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASHIFT
, &vd
->vdev_ashift
);
725 * Retrieve the vdev creation time.
727 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_CREATE_TXG
,
731 * If we're a top-level vdev, try to load the allocation parameters.
734 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
735 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_ARRAY
,
737 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_METASLAB_SHIFT
,
739 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ASIZE
,
741 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVING
,
743 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_VDEV_TOP_ZAP
,
746 ASSERT0(vd
->vdev_top_zap
);
749 if (top_level
&& alloctype
!= VDEV_ALLOC_ATTACH
) {
750 ASSERT(alloctype
== VDEV_ALLOC_LOAD
||
751 alloctype
== VDEV_ALLOC_ADD
||
752 alloctype
== VDEV_ALLOC_SPLIT
||
753 alloctype
== VDEV_ALLOC_ROOTPOOL
);
754 /* Note: metaslab_group_create() is now deferred */
757 if (vd
->vdev_ops
->vdev_op_leaf
&&
758 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_SPLIT
)) {
759 (void) nvlist_lookup_uint64(nv
,
760 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, &vd
->vdev_leaf_zap
);
762 ASSERT0(vd
->vdev_leaf_zap
);
766 * If we're a leaf vdev, try to load the DTL object and other state.
769 if (vd
->vdev_ops
->vdev_op_leaf
&&
770 (alloctype
== VDEV_ALLOC_LOAD
|| alloctype
== VDEV_ALLOC_L2CACHE
||
771 alloctype
== VDEV_ALLOC_ROOTPOOL
)) {
772 if (alloctype
== VDEV_ALLOC_LOAD
) {
773 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DTL
,
774 &vd
->vdev_dtl_object
);
775 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_UNSPARE
,
779 if (alloctype
== VDEV_ALLOC_ROOTPOOL
) {
782 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
783 &spare
) == 0 && spare
)
787 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
,
790 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_RESILVER_TXG
,
791 &vd
->vdev_resilver_txg
);
794 * In general, when importing a pool we want to ignore the
795 * persistent fault state, as the diagnosis made on another
796 * system may not be valid in the current context. The only
797 * exception is if we forced a vdev to a persistently faulted
798 * state with 'zpool offline -f'. The persistent fault will
799 * remain across imports until cleared.
801 * Local vdevs will remain in the faulted state.
803 if (spa_load_state(spa
) == SPA_LOAD_OPEN
||
804 spa_load_state(spa
) == SPA_LOAD_IMPORT
) {
805 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
,
807 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_DEGRADED
,
809 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
,
812 if (vd
->vdev_faulted
|| vd
->vdev_degraded
) {
816 VDEV_AUX_ERR_EXCEEDED
;
817 if (nvlist_lookup_string(nv
,
818 ZPOOL_CONFIG_AUX_STATE
, &aux
) == 0 &&
819 strcmp(aux
, "external") == 0)
820 vd
->vdev_label_aux
= VDEV_AUX_EXTERNAL
;
822 vd
->vdev_faulted
= 0ULL;
828 * Add ourselves to the parent's list of children.
830 vdev_add_child(parent
, vd
);
838 vdev_free(vdev_t
*vd
)
840 spa_t
*spa
= vd
->vdev_spa
;
843 * Scan queues are normally destroyed at the end of a scan. If the
844 * queue exists here, that implies the vdev is being removed while
845 * the scan is still running.
847 if (vd
->vdev_scan_io_queue
!= NULL
) {
848 mutex_enter(&vd
->vdev_scan_io_queue_lock
);
849 dsl_scan_io_queue_destroy(vd
->vdev_scan_io_queue
);
850 vd
->vdev_scan_io_queue
= NULL
;
851 mutex_exit(&vd
->vdev_scan_io_queue_lock
);
855 * vdev_free() implies closing the vdev first. This is simpler than
856 * trying to ensure complicated semantics for all callers.
860 ASSERT(!list_link_active(&vd
->vdev_config_dirty_node
));
861 ASSERT(!list_link_active(&vd
->vdev_state_dirty_node
));
866 for (int c
= 0; c
< vd
->vdev_children
; c
++)
867 vdev_free(vd
->vdev_child
[c
]);
869 ASSERT(vd
->vdev_child
== NULL
);
870 ASSERT(vd
->vdev_guid_sum
== vd
->vdev_guid
);
873 * Discard allocation state.
875 if (vd
->vdev_mg
!= NULL
) {
876 vdev_metaslab_fini(vd
);
877 metaslab_group_destroy(vd
->vdev_mg
);
880 ASSERT0(vd
->vdev_stat
.vs_space
);
881 ASSERT0(vd
->vdev_stat
.vs_dspace
);
882 ASSERT0(vd
->vdev_stat
.vs_alloc
);
885 * Remove this vdev from its parent's child list.
887 vdev_remove_child(vd
->vdev_parent
, vd
);
889 ASSERT(vd
->vdev_parent
== NULL
);
892 * Clean up vdev structure.
898 spa_strfree(vd
->vdev_path
);
900 spa_strfree(vd
->vdev_devid
);
901 if (vd
->vdev_physpath
)
902 spa_strfree(vd
->vdev_physpath
);
904 if (vd
->vdev_enc_sysfs_path
)
905 spa_strfree(vd
->vdev_enc_sysfs_path
);
908 spa_strfree(vd
->vdev_fru
);
910 if (vd
->vdev_isspare
)
911 spa_spare_remove(vd
);
912 if (vd
->vdev_isl2cache
)
913 spa_l2cache_remove(vd
);
915 txg_list_destroy(&vd
->vdev_ms_list
);
916 txg_list_destroy(&vd
->vdev_dtl_list
);
918 mutex_enter(&vd
->vdev_dtl_lock
);
919 space_map_close(vd
->vdev_dtl_sm
);
920 for (int t
= 0; t
< DTL_TYPES
; t
++) {
921 range_tree_vacate(vd
->vdev_dtl
[t
], NULL
, NULL
);
922 range_tree_destroy(vd
->vdev_dtl
[t
]);
924 mutex_exit(&vd
->vdev_dtl_lock
);
926 EQUIV(vd
->vdev_indirect_births
!= NULL
,
927 vd
->vdev_indirect_mapping
!= NULL
);
928 if (vd
->vdev_indirect_births
!= NULL
) {
929 vdev_indirect_mapping_close(vd
->vdev_indirect_mapping
);
930 vdev_indirect_births_close(vd
->vdev_indirect_births
);
933 if (vd
->vdev_obsolete_sm
!= NULL
) {
934 ASSERT(vd
->vdev_removing
||
935 vd
->vdev_ops
== &vdev_indirect_ops
);
936 space_map_close(vd
->vdev_obsolete_sm
);
937 vd
->vdev_obsolete_sm
= NULL
;
939 range_tree_destroy(vd
->vdev_obsolete_segments
);
940 rw_destroy(&vd
->vdev_indirect_rwlock
);
941 mutex_destroy(&vd
->vdev_obsolete_lock
);
943 mutex_destroy(&vd
->vdev_queue_lock
);
944 mutex_destroy(&vd
->vdev_dtl_lock
);
945 mutex_destroy(&vd
->vdev_stat_lock
);
946 mutex_destroy(&vd
->vdev_probe_lock
);
947 mutex_destroy(&vd
->vdev_scan_io_queue_lock
);
949 zfs_ratelimit_fini(&vd
->vdev_delay_rl
);
950 zfs_ratelimit_fini(&vd
->vdev_checksum_rl
);
952 if (vd
== spa
->spa_root_vdev
)
953 spa
->spa_root_vdev
= NULL
;
955 kmem_free(vd
, sizeof (vdev_t
));
959 * Transfer top-level vdev state from svd to tvd.
962 vdev_top_transfer(vdev_t
*svd
, vdev_t
*tvd
)
964 spa_t
*spa
= svd
->vdev_spa
;
969 ASSERT(tvd
== tvd
->vdev_top
);
971 tvd
->vdev_pending_fastwrite
= svd
->vdev_pending_fastwrite
;
972 tvd
->vdev_ms_array
= svd
->vdev_ms_array
;
973 tvd
->vdev_ms_shift
= svd
->vdev_ms_shift
;
974 tvd
->vdev_ms_count
= svd
->vdev_ms_count
;
975 tvd
->vdev_top_zap
= svd
->vdev_top_zap
;
977 svd
->vdev_ms_array
= 0;
978 svd
->vdev_ms_shift
= 0;
979 svd
->vdev_ms_count
= 0;
980 svd
->vdev_top_zap
= 0;
983 ASSERT3P(tvd
->vdev_mg
, ==, svd
->vdev_mg
);
984 tvd
->vdev_mg
= svd
->vdev_mg
;
985 tvd
->vdev_ms
= svd
->vdev_ms
;
990 if (tvd
->vdev_mg
!= NULL
)
991 tvd
->vdev_mg
->mg_vd
= tvd
;
993 tvd
->vdev_checkpoint_sm
= svd
->vdev_checkpoint_sm
;
994 svd
->vdev_checkpoint_sm
= NULL
;
996 tvd
->vdev_alloc_bias
= svd
->vdev_alloc_bias
;
997 svd
->vdev_alloc_bias
= VDEV_BIAS_NONE
;
999 tvd
->vdev_stat
.vs_alloc
= svd
->vdev_stat
.vs_alloc
;
1000 tvd
->vdev_stat
.vs_space
= svd
->vdev_stat
.vs_space
;
1001 tvd
->vdev_stat
.vs_dspace
= svd
->vdev_stat
.vs_dspace
;
1003 svd
->vdev_stat
.vs_alloc
= 0;
1004 svd
->vdev_stat
.vs_space
= 0;
1005 svd
->vdev_stat
.vs_dspace
= 0;
1008 * State which may be set on a top-level vdev that's in the
1009 * process of being removed.
1011 ASSERT0(tvd
->vdev_indirect_config
.vic_births_object
);
1012 ASSERT0(tvd
->vdev_indirect_config
.vic_mapping_object
);
1013 ASSERT3U(tvd
->vdev_indirect_config
.vic_prev_indirect_vdev
, ==, -1ULL);
1014 ASSERT3P(tvd
->vdev_indirect_mapping
, ==, NULL
);
1015 ASSERT3P(tvd
->vdev_indirect_births
, ==, NULL
);
1016 ASSERT3P(tvd
->vdev_obsolete_sm
, ==, NULL
);
1017 ASSERT0(tvd
->vdev_removing
);
1018 tvd
->vdev_removing
= svd
->vdev_removing
;
1019 tvd
->vdev_indirect_config
= svd
->vdev_indirect_config
;
1020 tvd
->vdev_indirect_mapping
= svd
->vdev_indirect_mapping
;
1021 tvd
->vdev_indirect_births
= svd
->vdev_indirect_births
;
1022 range_tree_swap(&svd
->vdev_obsolete_segments
,
1023 &tvd
->vdev_obsolete_segments
);
1024 tvd
->vdev_obsolete_sm
= svd
->vdev_obsolete_sm
;
1025 svd
->vdev_indirect_config
.vic_mapping_object
= 0;
1026 svd
->vdev_indirect_config
.vic_births_object
= 0;
1027 svd
->vdev_indirect_config
.vic_prev_indirect_vdev
= -1ULL;
1028 svd
->vdev_indirect_mapping
= NULL
;
1029 svd
->vdev_indirect_births
= NULL
;
1030 svd
->vdev_obsolete_sm
= NULL
;
1031 svd
->vdev_removing
= 0;
1033 for (t
= 0; t
< TXG_SIZE
; t
++) {
1034 while ((msp
= txg_list_remove(&svd
->vdev_ms_list
, t
)) != NULL
)
1035 (void) txg_list_add(&tvd
->vdev_ms_list
, msp
, t
);
1036 while ((vd
= txg_list_remove(&svd
->vdev_dtl_list
, t
)) != NULL
)
1037 (void) txg_list_add(&tvd
->vdev_dtl_list
, vd
, t
);
1038 if (txg_list_remove_this(&spa
->spa_vdev_txg_list
, svd
, t
))
1039 (void) txg_list_add(&spa
->spa_vdev_txg_list
, tvd
, t
);
1042 if (list_link_active(&svd
->vdev_config_dirty_node
)) {
1043 vdev_config_clean(svd
);
1044 vdev_config_dirty(tvd
);
1047 if (list_link_active(&svd
->vdev_state_dirty_node
)) {
1048 vdev_state_clean(svd
);
1049 vdev_state_dirty(tvd
);
1052 tvd
->vdev_deflate_ratio
= svd
->vdev_deflate_ratio
;
1053 svd
->vdev_deflate_ratio
= 0;
1055 tvd
->vdev_islog
= svd
->vdev_islog
;
1056 svd
->vdev_islog
= 0;
1058 dsl_scan_io_queue_vdev_xfer(svd
, tvd
);
1062 vdev_top_update(vdev_t
*tvd
, vdev_t
*vd
)
1069 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1070 vdev_top_update(tvd
, vd
->vdev_child
[c
]);
1074 * Add a mirror/replacing vdev above an existing vdev.
1077 vdev_add_parent(vdev_t
*cvd
, vdev_ops_t
*ops
)
1079 spa_t
*spa
= cvd
->vdev_spa
;
1080 vdev_t
*pvd
= cvd
->vdev_parent
;
1083 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1085 mvd
= vdev_alloc_common(spa
, cvd
->vdev_id
, 0, ops
);
1087 mvd
->vdev_asize
= cvd
->vdev_asize
;
1088 mvd
->vdev_min_asize
= cvd
->vdev_min_asize
;
1089 mvd
->vdev_max_asize
= cvd
->vdev_max_asize
;
1090 mvd
->vdev_psize
= cvd
->vdev_psize
;
1091 mvd
->vdev_ashift
= cvd
->vdev_ashift
;
1092 mvd
->vdev_state
= cvd
->vdev_state
;
1093 mvd
->vdev_crtxg
= cvd
->vdev_crtxg
;
1095 vdev_remove_child(pvd
, cvd
);
1096 vdev_add_child(pvd
, mvd
);
1097 cvd
->vdev_id
= mvd
->vdev_children
;
1098 vdev_add_child(mvd
, cvd
);
1099 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1101 if (mvd
== mvd
->vdev_top
)
1102 vdev_top_transfer(cvd
, mvd
);
1108 * Remove a 1-way mirror/replacing vdev from the tree.
1111 vdev_remove_parent(vdev_t
*cvd
)
1113 vdev_t
*mvd
= cvd
->vdev_parent
;
1114 vdev_t
*pvd
= mvd
->vdev_parent
;
1116 ASSERT(spa_config_held(cvd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1118 ASSERT(mvd
->vdev_children
== 1);
1119 ASSERT(mvd
->vdev_ops
== &vdev_mirror_ops
||
1120 mvd
->vdev_ops
== &vdev_replacing_ops
||
1121 mvd
->vdev_ops
== &vdev_spare_ops
);
1122 cvd
->vdev_ashift
= mvd
->vdev_ashift
;
1124 vdev_remove_child(mvd
, cvd
);
1125 vdev_remove_child(pvd
, mvd
);
1128 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1129 * Otherwise, we could have detached an offline device, and when we
1130 * go to import the pool we'll think we have two top-level vdevs,
1131 * instead of a different version of the same top-level vdev.
1133 if (mvd
->vdev_top
== mvd
) {
1134 uint64_t guid_delta
= mvd
->vdev_guid
- cvd
->vdev_guid
;
1135 cvd
->vdev_orig_guid
= cvd
->vdev_guid
;
1136 cvd
->vdev_guid
+= guid_delta
;
1137 cvd
->vdev_guid_sum
+= guid_delta
;
1140 * If pool not set for autoexpand, we need to also preserve
1141 * mvd's asize to prevent automatic expansion of cvd.
1142 * Otherwise if we are adjusting the mirror by attaching and
1143 * detaching children of non-uniform sizes, the mirror could
1144 * autoexpand, unexpectedly requiring larger devices to
1145 * re-establish the mirror.
1147 if (!cvd
->vdev_spa
->spa_autoexpand
)
1148 cvd
->vdev_asize
= mvd
->vdev_asize
;
1150 cvd
->vdev_id
= mvd
->vdev_id
;
1151 vdev_add_child(pvd
, cvd
);
1152 vdev_top_update(cvd
->vdev_top
, cvd
->vdev_top
);
1154 if (cvd
== cvd
->vdev_top
)
1155 vdev_top_transfer(mvd
, cvd
);
1157 ASSERT(mvd
->vdev_children
== 0);
1162 vdev_metaslab_group_create(vdev_t
*vd
)
1164 spa_t
*spa
= vd
->vdev_spa
;
1167 * metaslab_group_create was delayed until allocation bias was available
1169 if (vd
->vdev_mg
== NULL
) {
1170 metaslab_class_t
*mc
;
1172 if (vd
->vdev_islog
&& vd
->vdev_alloc_bias
== VDEV_BIAS_NONE
)
1173 vd
->vdev_alloc_bias
= VDEV_BIAS_LOG
;
1175 ASSERT3U(vd
->vdev_islog
, ==,
1176 (vd
->vdev_alloc_bias
== VDEV_BIAS_LOG
));
1178 switch (vd
->vdev_alloc_bias
) {
1180 mc
= spa_log_class(spa
);
1182 case VDEV_BIAS_SPECIAL
:
1183 mc
= spa_special_class(spa
);
1185 case VDEV_BIAS_DEDUP
:
1186 mc
= spa_dedup_class(spa
);
1189 mc
= spa_normal_class(spa
);
1192 vd
->vdev_mg
= metaslab_group_create(mc
, vd
,
1193 spa
->spa_alloc_count
);
1196 * The spa ashift values currently only reflect the
1197 * general vdev classes. Class destination is late
1198 * binding so ashift checking had to wait until now
1200 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
1201 mc
== spa_normal_class(spa
) && vd
->vdev_aux
== NULL
) {
1202 if (vd
->vdev_ashift
> spa
->spa_max_ashift
)
1203 spa
->spa_max_ashift
= vd
->vdev_ashift
;
1204 if (vd
->vdev_ashift
< spa
->spa_min_ashift
)
1205 spa
->spa_min_ashift
= vd
->vdev_ashift
;
1211 vdev_metaslab_init(vdev_t
*vd
, uint64_t txg
)
1213 spa_t
*spa
= vd
->vdev_spa
;
1214 objset_t
*mos
= spa
->spa_meta_objset
;
1216 uint64_t oldc
= vd
->vdev_ms_count
;
1217 uint64_t newc
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
1220 boolean_t expanding
= (oldc
!= 0);
1222 ASSERT(txg
== 0 || spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1225 * This vdev is not being allocated from yet or is a hole.
1227 if (vd
->vdev_ms_shift
== 0)
1230 ASSERT(!vd
->vdev_ishole
);
1232 ASSERT(oldc
<= newc
);
1234 mspp
= vmem_zalloc(newc
* sizeof (*mspp
), KM_SLEEP
);
1237 bcopy(vd
->vdev_ms
, mspp
, oldc
* sizeof (*mspp
));
1238 vmem_free(vd
->vdev_ms
, oldc
* sizeof (*mspp
));
1242 vd
->vdev_ms_count
= newc
;
1243 for (m
= oldc
; m
< newc
; m
++) {
1244 uint64_t object
= 0;
1247 * vdev_ms_array may be 0 if we are creating the "fake"
1248 * metaslabs for an indirect vdev for zdb's leak detection.
1249 * See zdb_leak_init().
1251 if (txg
== 0 && vd
->vdev_ms_array
!= 0) {
1252 error
= dmu_read(mos
, vd
->vdev_ms_array
,
1253 m
* sizeof (uint64_t), sizeof (uint64_t), &object
,
1256 vdev_dbgmsg(vd
, "unable to read the metaslab "
1257 "array [error=%d]", error
);
1264 * To accomodate zdb_leak_init() fake indirect
1265 * metaslabs, we allocate a metaslab group for
1266 * indirect vdevs which normally don't have one.
1268 if (vd
->vdev_mg
== NULL
) {
1269 ASSERT0(vdev_is_concrete(vd
));
1270 vdev_metaslab_group_create(vd
);
1273 error
= metaslab_init(vd
->vdev_mg
, m
, object
, txg
,
1276 vdev_dbgmsg(vd
, "metaslab_init failed [error=%d]",
1283 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_WRITER
);
1286 * If the vdev is being removed we don't activate
1287 * the metaslabs since we want to ensure that no new
1288 * allocations are performed on this device.
1290 if (!expanding
&& !vd
->vdev_removing
) {
1291 metaslab_group_activate(vd
->vdev_mg
);
1295 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
1301 vdev_metaslab_fini(vdev_t
*vd
)
1303 if (vd
->vdev_checkpoint_sm
!= NULL
) {
1304 ASSERT(spa_feature_is_active(vd
->vdev_spa
,
1305 SPA_FEATURE_POOL_CHECKPOINT
));
1306 space_map_close(vd
->vdev_checkpoint_sm
);
1308 * Even though we close the space map, we need to set its
1309 * pointer to NULL. The reason is that vdev_metaslab_fini()
1310 * may be called multiple times for certain operations
1311 * (i.e. when destroying a pool) so we need to ensure that
1312 * this clause never executes twice. This logic is similar
1313 * to the one used for the vdev_ms clause below.
1315 vd
->vdev_checkpoint_sm
= NULL
;
1318 if (vd
->vdev_ms
!= NULL
) {
1319 uint64_t count
= vd
->vdev_ms_count
;
1321 metaslab_group_passivate(vd
->vdev_mg
);
1322 for (uint64_t m
= 0; m
< count
; m
++) {
1323 metaslab_t
*msp
= vd
->vdev_ms
[m
];
1328 vmem_free(vd
->vdev_ms
, count
* sizeof (metaslab_t
*));
1331 vd
->vdev_ms_count
= 0;
1333 ASSERT0(vd
->vdev_ms_count
);
1334 ASSERT3U(vd
->vdev_pending_fastwrite
, ==, 0);
1337 typedef struct vdev_probe_stats
{
1338 boolean_t vps_readable
;
1339 boolean_t vps_writeable
;
1341 } vdev_probe_stats_t
;
1344 vdev_probe_done(zio_t
*zio
)
1346 spa_t
*spa
= zio
->io_spa
;
1347 vdev_t
*vd
= zio
->io_vd
;
1348 vdev_probe_stats_t
*vps
= zio
->io_private
;
1350 ASSERT(vd
->vdev_probe_zio
!= NULL
);
1352 if (zio
->io_type
== ZIO_TYPE_READ
) {
1353 if (zio
->io_error
== 0)
1354 vps
->vps_readable
= 1;
1355 if (zio
->io_error
== 0 && spa_writeable(spa
)) {
1356 zio_nowait(zio_write_phys(vd
->vdev_probe_zio
, vd
,
1357 zio
->io_offset
, zio
->io_size
, zio
->io_abd
,
1358 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1359 ZIO_PRIORITY_SYNC_WRITE
, vps
->vps_flags
, B_TRUE
));
1361 abd_free(zio
->io_abd
);
1363 } else if (zio
->io_type
== ZIO_TYPE_WRITE
) {
1364 if (zio
->io_error
== 0)
1365 vps
->vps_writeable
= 1;
1366 abd_free(zio
->io_abd
);
1367 } else if (zio
->io_type
== ZIO_TYPE_NULL
) {
1371 vd
->vdev_cant_read
|= !vps
->vps_readable
;
1372 vd
->vdev_cant_write
|= !vps
->vps_writeable
;
1374 if (vdev_readable(vd
) &&
1375 (vdev_writeable(vd
) || !spa_writeable(spa
))) {
1378 ASSERT(zio
->io_error
!= 0);
1379 vdev_dbgmsg(vd
, "failed probe");
1380 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE
,
1381 spa
, vd
, NULL
, NULL
, 0, 0);
1382 zio
->io_error
= SET_ERROR(ENXIO
);
1385 mutex_enter(&vd
->vdev_probe_lock
);
1386 ASSERT(vd
->vdev_probe_zio
== zio
);
1387 vd
->vdev_probe_zio
= NULL
;
1388 mutex_exit(&vd
->vdev_probe_lock
);
1391 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
)
1392 if (!vdev_accessible(vd
, pio
))
1393 pio
->io_error
= SET_ERROR(ENXIO
);
1395 kmem_free(vps
, sizeof (*vps
));
1400 * Determine whether this device is accessible.
1402 * Read and write to several known locations: the pad regions of each
1403 * vdev label but the first, which we leave alone in case it contains
1407 vdev_probe(vdev_t
*vd
, zio_t
*zio
)
1409 spa_t
*spa
= vd
->vdev_spa
;
1410 vdev_probe_stats_t
*vps
= NULL
;
1413 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
1416 * Don't probe the probe.
1418 if (zio
&& (zio
->io_flags
& ZIO_FLAG_PROBE
))
1422 * To prevent 'probe storms' when a device fails, we create
1423 * just one probe i/o at a time. All zios that want to probe
1424 * this vdev will become parents of the probe io.
1426 mutex_enter(&vd
->vdev_probe_lock
);
1428 if ((pio
= vd
->vdev_probe_zio
) == NULL
) {
1429 vps
= kmem_zalloc(sizeof (*vps
), KM_SLEEP
);
1431 vps
->vps_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_PROBE
|
1432 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_AGGREGATE
|
1435 if (spa_config_held(spa
, SCL_ZIO
, RW_WRITER
)) {
1437 * vdev_cant_read and vdev_cant_write can only
1438 * transition from TRUE to FALSE when we have the
1439 * SCL_ZIO lock as writer; otherwise they can only
1440 * transition from FALSE to TRUE. This ensures that
1441 * any zio looking at these values can assume that
1442 * failures persist for the life of the I/O. That's
1443 * important because when a device has intermittent
1444 * connectivity problems, we want to ensure that
1445 * they're ascribed to the device (ENXIO) and not
1448 * Since we hold SCL_ZIO as writer here, clear both
1449 * values so the probe can reevaluate from first
1452 vps
->vps_flags
|= ZIO_FLAG_CONFIG_WRITER
;
1453 vd
->vdev_cant_read
= B_FALSE
;
1454 vd
->vdev_cant_write
= B_FALSE
;
1457 vd
->vdev_probe_zio
= pio
= zio_null(NULL
, spa
, vd
,
1458 vdev_probe_done
, vps
,
1459 vps
->vps_flags
| ZIO_FLAG_DONT_PROPAGATE
);
1462 * We can't change the vdev state in this context, so we
1463 * kick off an async task to do it on our behalf.
1466 vd
->vdev_probe_wanted
= B_TRUE
;
1467 spa_async_request(spa
, SPA_ASYNC_PROBE
);
1472 zio_add_child(zio
, pio
);
1474 mutex_exit(&vd
->vdev_probe_lock
);
1477 ASSERT(zio
!= NULL
);
1481 for (int l
= 1; l
< VDEV_LABELS
; l
++) {
1482 zio_nowait(zio_read_phys(pio
, vd
,
1483 vdev_label_offset(vd
->vdev_psize
, l
,
1484 offsetof(vdev_label_t
, vl_pad2
)), VDEV_PAD_SIZE
,
1485 abd_alloc_for_io(VDEV_PAD_SIZE
, B_TRUE
),
1486 ZIO_CHECKSUM_OFF
, vdev_probe_done
, vps
,
1487 ZIO_PRIORITY_SYNC_READ
, vps
->vps_flags
, B_TRUE
));
1498 vdev_open_child(void *arg
)
1502 vd
->vdev_open_thread
= curthread
;
1503 vd
->vdev_open_error
= vdev_open(vd
);
1504 vd
->vdev_open_thread
= NULL
;
1508 vdev_uses_zvols(vdev_t
*vd
)
1511 if (zvol_is_zvol(vd
->vdev_path
))
1515 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1516 if (vdev_uses_zvols(vd
->vdev_child
[c
]))
1523 vdev_open_children(vdev_t
*vd
)
1526 int children
= vd
->vdev_children
;
1529 * in order to handle pools on top of zvols, do the opens
1530 * in a single thread so that the same thread holds the
1531 * spa_namespace_lock
1533 if (vdev_uses_zvols(vd
)) {
1535 for (int c
= 0; c
< children
; c
++)
1536 vd
->vdev_child
[c
]->vdev_open_error
=
1537 vdev_open(vd
->vdev_child
[c
]);
1539 tq
= taskq_create("vdev_open", children
, minclsyspri
,
1540 children
, children
, TASKQ_PREPOPULATE
);
1544 for (int c
= 0; c
< children
; c
++)
1545 VERIFY(taskq_dispatch(tq
, vdev_open_child
,
1546 vd
->vdev_child
[c
], TQ_SLEEP
) != TASKQID_INVALID
);
1551 vd
->vdev_nonrot
= B_TRUE
;
1553 for (int c
= 0; c
< children
; c
++)
1554 vd
->vdev_nonrot
&= vd
->vdev_child
[c
]->vdev_nonrot
;
1558 * Compute the raidz-deflation ratio. Note, we hard-code
1559 * in 128k (1 << 17) because it is the "typical" blocksize.
1560 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1561 * otherwise it would inconsistently account for existing bp's.
1564 vdev_set_deflate_ratio(vdev_t
*vd
)
1566 if (vd
== vd
->vdev_top
&& !vd
->vdev_ishole
&& vd
->vdev_ashift
!= 0) {
1567 vd
->vdev_deflate_ratio
= (1 << 17) /
1568 (vdev_psize_to_asize(vd
, 1 << 17) >> SPA_MINBLOCKSHIFT
);
1573 * Prepare a virtual device for access.
1576 vdev_open(vdev_t
*vd
)
1578 spa_t
*spa
= vd
->vdev_spa
;
1581 uint64_t max_osize
= 0;
1582 uint64_t asize
, max_asize
, psize
;
1583 uint64_t ashift
= 0;
1585 ASSERT(vd
->vdev_open_thread
== curthread
||
1586 spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
1587 ASSERT(vd
->vdev_state
== VDEV_STATE_CLOSED
||
1588 vd
->vdev_state
== VDEV_STATE_CANT_OPEN
||
1589 vd
->vdev_state
== VDEV_STATE_OFFLINE
);
1591 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
1592 vd
->vdev_cant_read
= B_FALSE
;
1593 vd
->vdev_cant_write
= B_FALSE
;
1594 vd
->vdev_min_asize
= vdev_get_min_asize(vd
);
1597 * If this vdev is not removed, check its fault status. If it's
1598 * faulted, bail out of the open.
1600 if (!vd
->vdev_removed
&& vd
->vdev_faulted
) {
1601 ASSERT(vd
->vdev_children
== 0);
1602 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1603 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1604 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1605 vd
->vdev_label_aux
);
1606 return (SET_ERROR(ENXIO
));
1607 } else if (vd
->vdev_offline
) {
1608 ASSERT(vd
->vdev_children
== 0);
1609 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
, VDEV_AUX_NONE
);
1610 return (SET_ERROR(ENXIO
));
1613 error
= vd
->vdev_ops
->vdev_op_open(vd
, &osize
, &max_osize
, &ashift
);
1616 * Reset the vdev_reopening flag so that we actually close
1617 * the vdev on error.
1619 vd
->vdev_reopening
= B_FALSE
;
1620 if (zio_injection_enabled
&& error
== 0)
1621 error
= zio_handle_device_injection(vd
, NULL
, ENXIO
);
1624 if (vd
->vdev_removed
&&
1625 vd
->vdev_stat
.vs_aux
!= VDEV_AUX_OPEN_FAILED
)
1626 vd
->vdev_removed
= B_FALSE
;
1628 if (vd
->vdev_stat
.vs_aux
== VDEV_AUX_CHILDREN_OFFLINE
) {
1629 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_OFFLINE
,
1630 vd
->vdev_stat
.vs_aux
);
1632 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1633 vd
->vdev_stat
.vs_aux
);
1638 vd
->vdev_removed
= B_FALSE
;
1641 * Recheck the faulted flag now that we have confirmed that
1642 * the vdev is accessible. If we're faulted, bail.
1644 if (vd
->vdev_faulted
) {
1645 ASSERT(vd
->vdev_children
== 0);
1646 ASSERT(vd
->vdev_label_aux
== VDEV_AUX_ERR_EXCEEDED
||
1647 vd
->vdev_label_aux
== VDEV_AUX_EXTERNAL
);
1648 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1649 vd
->vdev_label_aux
);
1650 return (SET_ERROR(ENXIO
));
1653 if (vd
->vdev_degraded
) {
1654 ASSERT(vd
->vdev_children
== 0);
1655 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1656 VDEV_AUX_ERR_EXCEEDED
);
1658 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_HEALTHY
, 0);
1662 * For hole or missing vdevs we just return success.
1664 if (vd
->vdev_ishole
|| vd
->vdev_ops
== &vdev_missing_ops
)
1667 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
1668 if (vd
->vdev_child
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
) {
1669 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_DEGRADED
,
1675 osize
= P2ALIGN(osize
, (uint64_t)sizeof (vdev_label_t
));
1676 max_osize
= P2ALIGN(max_osize
, (uint64_t)sizeof (vdev_label_t
));
1678 if (vd
->vdev_children
== 0) {
1679 if (osize
< SPA_MINDEVSIZE
) {
1680 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1681 VDEV_AUX_TOO_SMALL
);
1682 return (SET_ERROR(EOVERFLOW
));
1685 asize
= osize
- (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
);
1686 max_asize
= max_osize
- (VDEV_LABEL_START_SIZE
+
1687 VDEV_LABEL_END_SIZE
);
1689 if (vd
->vdev_parent
!= NULL
&& osize
< SPA_MINDEVSIZE
-
1690 (VDEV_LABEL_START_SIZE
+ VDEV_LABEL_END_SIZE
)) {
1691 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1692 VDEV_AUX_TOO_SMALL
);
1693 return (SET_ERROR(EOVERFLOW
));
1697 max_asize
= max_osize
;
1701 * If the vdev was expanded, record this so that we can re-create the
1702 * uberblock rings in labels {2,3}, during the next sync.
1704 if ((psize
> vd
->vdev_psize
) && (vd
->vdev_psize
!= 0))
1705 vd
->vdev_copy_uberblocks
= B_TRUE
;
1707 vd
->vdev_psize
= psize
;
1710 * Make sure the allocatable size hasn't shrunk too much.
1712 if (asize
< vd
->vdev_min_asize
) {
1713 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1714 VDEV_AUX_BAD_LABEL
);
1715 return (SET_ERROR(EINVAL
));
1718 if (vd
->vdev_asize
== 0) {
1720 * This is the first-ever open, so use the computed values.
1721 * For compatibility, a different ashift can be requested.
1723 vd
->vdev_asize
= asize
;
1724 vd
->vdev_max_asize
= max_asize
;
1725 if (vd
->vdev_ashift
== 0) {
1726 vd
->vdev_ashift
= ashift
; /* use detected value */
1728 if (vd
->vdev_ashift
!= 0 && (vd
->vdev_ashift
< ASHIFT_MIN
||
1729 vd
->vdev_ashift
> ASHIFT_MAX
)) {
1730 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1731 VDEV_AUX_BAD_ASHIFT
);
1732 return (SET_ERROR(EDOM
));
1736 * Detect if the alignment requirement has increased.
1737 * We don't want to make the pool unavailable, just
1738 * post an event instead.
1740 if (ashift
> vd
->vdev_top
->vdev_ashift
&&
1741 vd
->vdev_ops
->vdev_op_leaf
) {
1742 zfs_ereport_post(FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
,
1743 spa
, vd
, NULL
, NULL
, 0, 0);
1746 vd
->vdev_max_asize
= max_asize
;
1750 * If all children are healthy we update asize if either:
1751 * The asize has increased, due to a device expansion caused by dynamic
1752 * LUN growth or vdev replacement, and automatic expansion is enabled;
1753 * making the additional space available.
1755 * The asize has decreased, due to a device shrink usually caused by a
1756 * vdev replace with a smaller device. This ensures that calculations
1757 * based of max_asize and asize e.g. esize are always valid. It's safe
1758 * to do this as we've already validated that asize is greater than
1761 if (vd
->vdev_state
== VDEV_STATE_HEALTHY
&&
1762 ((asize
> vd
->vdev_asize
&&
1763 (vd
->vdev_expanding
|| spa
->spa_autoexpand
)) ||
1764 (asize
< vd
->vdev_asize
)))
1765 vd
->vdev_asize
= asize
;
1767 vdev_set_min_asize(vd
);
1770 * Ensure we can issue some IO before declaring the
1771 * vdev open for business.
1773 if (vd
->vdev_ops
->vdev_op_leaf
&&
1774 (error
= zio_wait(vdev_probe(vd
, NULL
))) != 0) {
1775 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
1776 VDEV_AUX_ERR_EXCEEDED
);
1781 * Track the min and max ashift values for normal data devices.
1783 * DJB - TBD these should perhaps be tracked per allocation class
1784 * (e.g. spa_min_ashift is used to round up post compression buffers)
1786 if (vd
->vdev_top
== vd
&& vd
->vdev_ashift
!= 0 &&
1787 vd
->vdev_alloc_bias
== VDEV_BIAS_NONE
&&
1788 vd
->vdev_aux
== NULL
) {
1789 if (vd
->vdev_ashift
> spa
->spa_max_ashift
)
1790 spa
->spa_max_ashift
= vd
->vdev_ashift
;
1791 if (vd
->vdev_ashift
< spa
->spa_min_ashift
)
1792 spa
->spa_min_ashift
= vd
->vdev_ashift
;
1796 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1797 * resilver. But don't do this if we are doing a reopen for a scrub,
1798 * since this would just restart the scrub we are already doing.
1800 if (vd
->vdev_ops
->vdev_op_leaf
&& !spa
->spa_scrub_reopen
&&
1801 vdev_resilver_needed(vd
, NULL
, NULL
))
1802 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
1808 * Called once the vdevs are all opened, this routine validates the label
1809 * contents. This needs to be done before vdev_load() so that we don't
1810 * inadvertently do repair I/Os to the wrong device.
1812 * This function will only return failure if one of the vdevs indicates that it
1813 * has since been destroyed or exported. This is only possible if
1814 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1815 * will be updated but the function will return 0.
1818 vdev_validate(vdev_t
*vd
)
1820 spa_t
*spa
= vd
->vdev_spa
;
1822 uint64_t guid
= 0, aux_guid
= 0, top_guid
;
1827 if (vdev_validate_skip
)
1830 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
1831 if (vdev_validate(vd
->vdev_child
[c
]) != 0)
1832 return (SET_ERROR(EBADF
));
1835 * If the device has already failed, or was marked offline, don't do
1836 * any further validation. Otherwise, label I/O will fail and we will
1837 * overwrite the previous state.
1839 if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_readable(vd
))
1843 * If we are performing an extreme rewind, we allow for a label that
1844 * was modified at a point after the current txg.
1845 * If config lock is not held do not check for the txg. spa_sync could
1846 * be updating the vdev's label before updating spa_last_synced_txg.
1848 if (spa
->spa_extreme_rewind
|| spa_last_synced_txg(spa
) == 0 ||
1849 spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) != SCL_CONFIG
)
1852 txg
= spa_last_synced_txg(spa
);
1854 if ((label
= vdev_label_read_config(vd
, txg
)) == NULL
) {
1855 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
1856 VDEV_AUX_BAD_LABEL
);
1857 vdev_dbgmsg(vd
, "vdev_validate: failed reading config for "
1858 "txg %llu", (u_longlong_t
)txg
);
1863 * Determine if this vdev has been split off into another
1864 * pool. If so, then refuse to open it.
1866 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_SPLIT_GUID
,
1867 &aux_guid
) == 0 && aux_guid
== spa_guid(spa
)) {
1868 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1869 VDEV_AUX_SPLIT_POOL
);
1871 vdev_dbgmsg(vd
, "vdev_validate: vdev split into other pool");
1875 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0) {
1876 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1877 VDEV_AUX_CORRUPT_DATA
);
1879 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1880 ZPOOL_CONFIG_POOL_GUID
);
1885 * If config is not trusted then ignore the spa guid check. This is
1886 * necessary because if the machine crashed during a re-guid the new
1887 * guid might have been written to all of the vdev labels, but not the
1888 * cached config. The check will be performed again once we have the
1889 * trusted config from the MOS.
1891 if (spa
->spa_trust_config
&& guid
!= spa_guid(spa
)) {
1892 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1893 VDEV_AUX_CORRUPT_DATA
);
1895 vdev_dbgmsg(vd
, "vdev_validate: vdev label pool_guid doesn't "
1896 "match config (%llu != %llu)", (u_longlong_t
)guid
,
1897 (u_longlong_t
)spa_guid(spa
));
1901 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
)
1902 != 0 || nvlist_lookup_uint64(nvl
, ZPOOL_CONFIG_ORIG_GUID
,
1906 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0) {
1907 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1908 VDEV_AUX_CORRUPT_DATA
);
1910 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1915 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_TOP_GUID
, &top_guid
)
1917 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1918 VDEV_AUX_CORRUPT_DATA
);
1920 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1921 ZPOOL_CONFIG_TOP_GUID
);
1926 * If this vdev just became a top-level vdev because its sibling was
1927 * detached, it will have adopted the parent's vdev guid -- but the
1928 * label may or may not be on disk yet. Fortunately, either version
1929 * of the label will have the same top guid, so if we're a top-level
1930 * vdev, we can safely compare to that instead.
1931 * However, if the config comes from a cachefile that failed to update
1932 * after the detach, a top-level vdev will appear as a non top-level
1933 * vdev in the config. Also relax the constraints if we perform an
1936 * If we split this vdev off instead, then we also check the
1937 * original pool's guid. We don't want to consider the vdev
1938 * corrupt if it is partway through a split operation.
1940 if (vd
->vdev_guid
!= guid
&& vd
->vdev_guid
!= aux_guid
) {
1941 boolean_t mismatch
= B_FALSE
;
1942 if (spa
->spa_trust_config
&& !spa
->spa_extreme_rewind
) {
1943 if (vd
!= vd
->vdev_top
|| vd
->vdev_guid
!= top_guid
)
1946 if (vd
->vdev_guid
!= top_guid
&&
1947 vd
->vdev_top
->vdev_guid
!= guid
)
1952 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1953 VDEV_AUX_CORRUPT_DATA
);
1955 vdev_dbgmsg(vd
, "vdev_validate: config guid "
1956 "doesn't match label guid");
1957 vdev_dbgmsg(vd
, "CONFIG: guid %llu, top_guid %llu",
1958 (u_longlong_t
)vd
->vdev_guid
,
1959 (u_longlong_t
)vd
->vdev_top
->vdev_guid
);
1960 vdev_dbgmsg(vd
, "LABEL: guid %llu, top_guid %llu, "
1961 "aux_guid %llu", (u_longlong_t
)guid
,
1962 (u_longlong_t
)top_guid
, (u_longlong_t
)aux_guid
);
1967 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
,
1969 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
1970 VDEV_AUX_CORRUPT_DATA
);
1972 vdev_dbgmsg(vd
, "vdev_validate: '%s' missing from label",
1973 ZPOOL_CONFIG_POOL_STATE
);
1980 * If this is a verbatim import, no need to check the
1981 * state of the pool.
1983 if (!(spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) &&
1984 spa_load_state(spa
) == SPA_LOAD_OPEN
&&
1985 state
!= POOL_STATE_ACTIVE
) {
1986 vdev_dbgmsg(vd
, "vdev_validate: invalid pool state (%llu) "
1987 "for spa %s", (u_longlong_t
)state
, spa
->spa_name
);
1988 return (SET_ERROR(EBADF
));
1992 * If we were able to open and validate a vdev that was
1993 * previously marked permanently unavailable, clear that state
1996 if (vd
->vdev_not_present
)
1997 vd
->vdev_not_present
= 0;
2003 vdev_copy_path_impl(vdev_t
*svd
, vdev_t
*dvd
)
2005 if (svd
->vdev_path
!= NULL
&& dvd
->vdev_path
!= NULL
) {
2006 if (strcmp(svd
->vdev_path
, dvd
->vdev_path
) != 0) {
2007 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
2008 "from '%s' to '%s'", (u_longlong_t
)dvd
->vdev_guid
,
2009 dvd
->vdev_path
, svd
->vdev_path
);
2010 spa_strfree(dvd
->vdev_path
);
2011 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
2013 } else if (svd
->vdev_path
!= NULL
) {
2014 dvd
->vdev_path
= spa_strdup(svd
->vdev_path
);
2015 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
2016 (u_longlong_t
)dvd
->vdev_guid
, dvd
->vdev_path
);
2021 * Recursively copy vdev paths from one vdev to another. Source and destination
2022 * vdev trees must have same geometry otherwise return error. Intended to copy
2023 * paths from userland config into MOS config.
2026 vdev_copy_path_strict(vdev_t
*svd
, vdev_t
*dvd
)
2028 if ((svd
->vdev_ops
== &vdev_missing_ops
) ||
2029 (svd
->vdev_ishole
&& dvd
->vdev_ishole
) ||
2030 (dvd
->vdev_ops
== &vdev_indirect_ops
))
2033 if (svd
->vdev_ops
!= dvd
->vdev_ops
) {
2034 vdev_dbgmsg(svd
, "vdev_copy_path: vdev type mismatch: %s != %s",
2035 svd
->vdev_ops
->vdev_op_type
, dvd
->vdev_ops
->vdev_op_type
);
2036 return (SET_ERROR(EINVAL
));
2039 if (svd
->vdev_guid
!= dvd
->vdev_guid
) {
2040 vdev_dbgmsg(svd
, "vdev_copy_path: guids mismatch (%llu != "
2041 "%llu)", (u_longlong_t
)svd
->vdev_guid
,
2042 (u_longlong_t
)dvd
->vdev_guid
);
2043 return (SET_ERROR(EINVAL
));
2046 if (svd
->vdev_children
!= dvd
->vdev_children
) {
2047 vdev_dbgmsg(svd
, "vdev_copy_path: children count mismatch: "
2048 "%llu != %llu", (u_longlong_t
)svd
->vdev_children
,
2049 (u_longlong_t
)dvd
->vdev_children
);
2050 return (SET_ERROR(EINVAL
));
2053 for (uint64_t i
= 0; i
< svd
->vdev_children
; i
++) {
2054 int error
= vdev_copy_path_strict(svd
->vdev_child
[i
],
2055 dvd
->vdev_child
[i
]);
2060 if (svd
->vdev_ops
->vdev_op_leaf
)
2061 vdev_copy_path_impl(svd
, dvd
);
2067 vdev_copy_path_search(vdev_t
*stvd
, vdev_t
*dvd
)
2069 ASSERT(stvd
->vdev_top
== stvd
);
2070 ASSERT3U(stvd
->vdev_id
, ==, dvd
->vdev_top
->vdev_id
);
2072 for (uint64_t i
= 0; i
< dvd
->vdev_children
; i
++) {
2073 vdev_copy_path_search(stvd
, dvd
->vdev_child
[i
]);
2076 if (!dvd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(dvd
))
2080 * The idea here is that while a vdev can shift positions within
2081 * a top vdev (when replacing, attaching mirror, etc.) it cannot
2082 * step outside of it.
2084 vdev_t
*vd
= vdev_lookup_by_guid(stvd
, dvd
->vdev_guid
);
2086 if (vd
== NULL
|| vd
->vdev_ops
!= dvd
->vdev_ops
)
2089 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2091 vdev_copy_path_impl(vd
, dvd
);
2095 * Recursively copy vdev paths from one root vdev to another. Source and
2096 * destination vdev trees may differ in geometry. For each destination leaf
2097 * vdev, search a vdev with the same guid and top vdev id in the source.
2098 * Intended to copy paths from userland config into MOS config.
2101 vdev_copy_path_relaxed(vdev_t
*srvd
, vdev_t
*drvd
)
2103 uint64_t children
= MIN(srvd
->vdev_children
, drvd
->vdev_children
);
2104 ASSERT(srvd
->vdev_ops
== &vdev_root_ops
);
2105 ASSERT(drvd
->vdev_ops
== &vdev_root_ops
);
2107 for (uint64_t i
= 0; i
< children
; i
++) {
2108 vdev_copy_path_search(srvd
->vdev_child
[i
],
2109 drvd
->vdev_child
[i
]);
2114 * Close a virtual device.
2117 vdev_close(vdev_t
*vd
)
2119 vdev_t
*pvd
= vd
->vdev_parent
;
2120 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
2122 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2125 * If our parent is reopening, then we are as well, unless we are
2128 if (pvd
!= NULL
&& pvd
->vdev_reopening
)
2129 vd
->vdev_reopening
= (pvd
->vdev_reopening
&& !vd
->vdev_offline
);
2131 vd
->vdev_ops
->vdev_op_close(vd
);
2133 vdev_cache_purge(vd
);
2136 * We record the previous state before we close it, so that if we are
2137 * doing a reopen(), we don't generate FMA ereports if we notice that
2138 * it's still faulted.
2140 vd
->vdev_prevstate
= vd
->vdev_state
;
2142 if (vd
->vdev_offline
)
2143 vd
->vdev_state
= VDEV_STATE_OFFLINE
;
2145 vd
->vdev_state
= VDEV_STATE_CLOSED
;
2146 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
2150 vdev_hold(vdev_t
*vd
)
2152 spa_t
*spa
= vd
->vdev_spa
;
2154 ASSERT(spa_is_root(spa
));
2155 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
2158 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2159 vdev_hold(vd
->vdev_child
[c
]);
2161 if (vd
->vdev_ops
->vdev_op_leaf
)
2162 vd
->vdev_ops
->vdev_op_hold(vd
);
2166 vdev_rele(vdev_t
*vd
)
2168 ASSERT(spa_is_root(vd
->vdev_spa
));
2169 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2170 vdev_rele(vd
->vdev_child
[c
]);
2172 if (vd
->vdev_ops
->vdev_op_leaf
)
2173 vd
->vdev_ops
->vdev_op_rele(vd
);
2177 * Reopen all interior vdevs and any unopened leaves. We don't actually
2178 * reopen leaf vdevs which had previously been opened as they might deadlock
2179 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2180 * If the leaf has never been opened then open it, as usual.
2183 vdev_reopen(vdev_t
*vd
)
2185 spa_t
*spa
= vd
->vdev_spa
;
2187 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2189 /* set the reopening flag unless we're taking the vdev offline */
2190 vd
->vdev_reopening
= !vd
->vdev_offline
;
2192 (void) vdev_open(vd
);
2195 * Call vdev_validate() here to make sure we have the same device.
2196 * Otherwise, a device with an invalid label could be successfully
2197 * opened in response to vdev_reopen().
2200 (void) vdev_validate_aux(vd
);
2201 if (vdev_readable(vd
) && vdev_writeable(vd
) &&
2202 vd
->vdev_aux
== &spa
->spa_l2cache
&&
2203 !l2arc_vdev_present(vd
))
2204 l2arc_add_vdev(spa
, vd
);
2206 (void) vdev_validate(vd
);
2210 * Reassess parent vdev's health.
2212 vdev_propagate_state(vd
);
2216 vdev_create(vdev_t
*vd
, uint64_t txg
, boolean_t isreplacing
)
2221 * Normally, partial opens (e.g. of a mirror) are allowed.
2222 * For a create, however, we want to fail the request if
2223 * there are any components we can't open.
2225 error
= vdev_open(vd
);
2227 if (error
|| vd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
2229 return (error
? error
: ENXIO
);
2233 * Recursively load DTLs and initialize all labels.
2235 if ((error
= vdev_dtl_load(vd
)) != 0 ||
2236 (error
= vdev_label_init(vd
, txg
, isreplacing
?
2237 VDEV_LABEL_REPLACE
: VDEV_LABEL_CREATE
)) != 0) {
2246 vdev_metaslab_set_size(vdev_t
*vd
)
2248 uint64_t asize
= vd
->vdev_asize
;
2249 uint64_t ms_count
= asize
>> vdev_default_ms_shift
;
2253 * There are two dimensions to the metaslab sizing calculation:
2254 * the size of the metaslab and the count of metaslabs per vdev.
2255 * In general, we aim for vdev_max_ms_count (200) metaslabs. The
2256 * range of the dimensions are as follows:
2258 * 2^29 <= ms_size <= 2^38
2259 * 16 <= ms_count <= 131,072
2261 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2262 * at least 512MB (2^29) to minimize fragmentation effects when
2263 * testing with smaller devices. However, the count constraint
2264 * of at least 16 metaslabs will override this minimum size goal.
2266 * On the upper end of vdev sizes, we aim for a maximum metaslab
2267 * size of 256GB. However, we will cap the total count to 2^17
2268 * metaslabs to keep our memory footprint in check.
2270 * The net effect of applying above constrains is summarized below.
2272 * vdev size metaslab count
2273 * -------------|-----------------
2275 * 8GB - 100GB one per 512MB
2277 * 50TB - 32PB one per 256GB
2279 * -------------------------------
2282 if (ms_count
< vdev_min_ms_count
)
2283 ms_shift
= highbit64(asize
/ vdev_min_ms_count
);
2284 else if (ms_count
> vdev_max_ms_count
)
2285 ms_shift
= highbit64(asize
/ vdev_max_ms_count
);
2287 ms_shift
= vdev_default_ms_shift
;
2289 if (ms_shift
< SPA_MAXBLOCKSHIFT
) {
2290 ms_shift
= SPA_MAXBLOCKSHIFT
;
2291 } else if (ms_shift
> vdev_max_ms_shift
) {
2292 ms_shift
= vdev_max_ms_shift
;
2293 /* cap the total count to constrain memory footprint */
2294 if ((asize
>> ms_shift
) > vdev_ms_count_limit
)
2295 ms_shift
= highbit64(asize
/ vdev_ms_count_limit
);
2298 vd
->vdev_ms_shift
= ms_shift
;
2299 ASSERT3U(vd
->vdev_ms_shift
, >=, SPA_MAXBLOCKSHIFT
);
2303 vdev_dirty(vdev_t
*vd
, int flags
, void *arg
, uint64_t txg
)
2305 ASSERT(vd
== vd
->vdev_top
);
2306 /* indirect vdevs don't have metaslabs or dtls */
2307 ASSERT(vdev_is_concrete(vd
) || flags
== 0);
2308 ASSERT(ISP2(flags
));
2309 ASSERT(spa_writeable(vd
->vdev_spa
));
2311 if (flags
& VDD_METASLAB
)
2312 (void) txg_list_add(&vd
->vdev_ms_list
, arg
, txg
);
2314 if (flags
& VDD_DTL
)
2315 (void) txg_list_add(&vd
->vdev_dtl_list
, arg
, txg
);
2317 (void) txg_list_add(&vd
->vdev_spa
->spa_vdev_txg_list
, vd
, txg
);
2321 vdev_dirty_leaves(vdev_t
*vd
, int flags
, uint64_t txg
)
2323 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2324 vdev_dirty_leaves(vd
->vdev_child
[c
], flags
, txg
);
2326 if (vd
->vdev_ops
->vdev_op_leaf
)
2327 vdev_dirty(vd
->vdev_top
, flags
, vd
, txg
);
2333 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2334 * the vdev has less than perfect replication. There are four kinds of DTL:
2336 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2338 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2340 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2341 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2342 * txgs that was scrubbed.
2344 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2345 * persistent errors or just some device being offline.
2346 * Unlike the other three, the DTL_OUTAGE map is not generally
2347 * maintained; it's only computed when needed, typically to
2348 * determine whether a device can be detached.
2350 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2351 * either has the data or it doesn't.
2353 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2354 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2355 * if any child is less than fully replicated, then so is its parent.
2356 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2357 * comprising only those txgs which appear in 'maxfaults' or more children;
2358 * those are the txgs we don't have enough replication to read. For example,
2359 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2360 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2361 * two child DTL_MISSING maps.
2363 * It should be clear from the above that to compute the DTLs and outage maps
2364 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2365 * Therefore, that is all we keep on disk. When loading the pool, or after
2366 * a configuration change, we generate all other DTLs from first principles.
2369 vdev_dtl_dirty(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2371 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2373 ASSERT(t
< DTL_TYPES
);
2374 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2375 ASSERT(spa_writeable(vd
->vdev_spa
));
2377 mutex_enter(&vd
->vdev_dtl_lock
);
2378 if (!range_tree_contains(rt
, txg
, size
))
2379 range_tree_add(rt
, txg
, size
);
2380 mutex_exit(&vd
->vdev_dtl_lock
);
2384 vdev_dtl_contains(vdev_t
*vd
, vdev_dtl_type_t t
, uint64_t txg
, uint64_t size
)
2386 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2387 boolean_t dirty
= B_FALSE
;
2389 ASSERT(t
< DTL_TYPES
);
2390 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2393 * While we are loading the pool, the DTLs have not been loaded yet.
2394 * Ignore the DTLs and try all devices. This avoids a recursive
2395 * mutex enter on the vdev_dtl_lock, and also makes us try hard
2396 * when loading the pool (relying on the checksum to ensure that
2397 * we get the right data -- note that we while loading, we are
2398 * only reading the MOS, which is always checksummed).
2400 if (vd
->vdev_spa
->spa_load_state
!= SPA_LOAD_NONE
)
2403 mutex_enter(&vd
->vdev_dtl_lock
);
2404 if (!range_tree_is_empty(rt
))
2405 dirty
= range_tree_contains(rt
, txg
, size
);
2406 mutex_exit(&vd
->vdev_dtl_lock
);
2412 vdev_dtl_empty(vdev_t
*vd
, vdev_dtl_type_t t
)
2414 range_tree_t
*rt
= vd
->vdev_dtl
[t
];
2417 mutex_enter(&vd
->vdev_dtl_lock
);
2418 empty
= range_tree_is_empty(rt
);
2419 mutex_exit(&vd
->vdev_dtl_lock
);
2425 * Returns B_TRUE if vdev determines offset needs to be resilvered.
2428 vdev_dtl_need_resilver(vdev_t
*vd
, uint64_t offset
, size_t psize
)
2430 ASSERT(vd
!= vd
->vdev_spa
->spa_root_vdev
);
2432 if (vd
->vdev_ops
->vdev_op_need_resilver
== NULL
||
2433 vd
->vdev_ops
->vdev_op_leaf
)
2436 return (vd
->vdev_ops
->vdev_op_need_resilver(vd
, offset
, psize
));
2440 * Returns the lowest txg in the DTL range.
2443 vdev_dtl_min(vdev_t
*vd
)
2447 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2448 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2449 ASSERT0(vd
->vdev_children
);
2451 rs
= avl_first(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
2452 return (rs
->rs_start
- 1);
2456 * Returns the highest txg in the DTL.
2459 vdev_dtl_max(vdev_t
*vd
)
2463 ASSERT(MUTEX_HELD(&vd
->vdev_dtl_lock
));
2464 ASSERT3U(range_tree_space(vd
->vdev_dtl
[DTL_MISSING
]), !=, 0);
2465 ASSERT0(vd
->vdev_children
);
2467 rs
= avl_last(&vd
->vdev_dtl
[DTL_MISSING
]->rt_root
);
2468 return (rs
->rs_end
);
2472 * Determine if a resilvering vdev should remove any DTL entries from
2473 * its range. If the vdev was resilvering for the entire duration of the
2474 * scan then it should excise that range from its DTLs. Otherwise, this
2475 * vdev is considered partially resilvered and should leave its DTL
2476 * entries intact. The comment in vdev_dtl_reassess() describes how we
2480 vdev_dtl_should_excise(vdev_t
*vd
)
2482 spa_t
*spa
= vd
->vdev_spa
;
2483 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
2485 ASSERT0(scn
->scn_phys
.scn_errors
);
2486 ASSERT0(vd
->vdev_children
);
2488 if (vd
->vdev_state
< VDEV_STATE_DEGRADED
)
2491 if (vd
->vdev_resilver_txg
== 0 ||
2492 range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]))
2496 * When a resilver is initiated the scan will assign the scn_max_txg
2497 * value to the highest txg value that exists in all DTLs. If this
2498 * device's max DTL is not part of this scan (i.e. it is not in
2499 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2502 if (vdev_dtl_max(vd
) <= scn
->scn_phys
.scn_max_txg
) {
2503 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <=, vdev_dtl_min(vd
));
2504 ASSERT3U(scn
->scn_phys
.scn_min_txg
, <, vd
->vdev_resilver_txg
);
2505 ASSERT3U(vd
->vdev_resilver_txg
, <=, scn
->scn_phys
.scn_max_txg
);
2512 * Reassess DTLs after a config change or scrub completion.
2515 vdev_dtl_reassess(vdev_t
*vd
, uint64_t txg
, uint64_t scrub_txg
, int scrub_done
)
2517 spa_t
*spa
= vd
->vdev_spa
;
2521 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
2523 for (int c
= 0; c
< vd
->vdev_children
; c
++)
2524 vdev_dtl_reassess(vd
->vdev_child
[c
], txg
,
2525 scrub_txg
, scrub_done
);
2527 if (vd
== spa
->spa_root_vdev
|| !vdev_is_concrete(vd
) || vd
->vdev_aux
)
2530 if (vd
->vdev_ops
->vdev_op_leaf
) {
2531 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
2533 mutex_enter(&vd
->vdev_dtl_lock
);
2536 * If requested, pretend the scan completed cleanly.
2538 if (zfs_scan_ignore_errors
&& scn
)
2539 scn
->scn_phys
.scn_errors
= 0;
2542 * If we've completed a scan cleanly then determine
2543 * if this vdev should remove any DTLs. We only want to
2544 * excise regions on vdevs that were available during
2545 * the entire duration of this scan.
2547 if (scrub_txg
!= 0 &&
2548 (spa
->spa_scrub_started
||
2549 (scn
!= NULL
&& scn
->scn_phys
.scn_errors
== 0)) &&
2550 vdev_dtl_should_excise(vd
)) {
2552 * We completed a scrub up to scrub_txg. If we
2553 * did it without rebooting, then the scrub dtl
2554 * will be valid, so excise the old region and
2555 * fold in the scrub dtl. Otherwise, leave the
2556 * dtl as-is if there was an error.
2558 * There's little trick here: to excise the beginning
2559 * of the DTL_MISSING map, we put it into a reference
2560 * tree and then add a segment with refcnt -1 that
2561 * covers the range [0, scrub_txg). This means
2562 * that each txg in that range has refcnt -1 or 0.
2563 * We then add DTL_SCRUB with a refcnt of 2, so that
2564 * entries in the range [0, scrub_txg) will have a
2565 * positive refcnt -- either 1 or 2. We then convert
2566 * the reference tree into the new DTL_MISSING map.
2568 space_reftree_create(&reftree
);
2569 space_reftree_add_map(&reftree
,
2570 vd
->vdev_dtl
[DTL_MISSING
], 1);
2571 space_reftree_add_seg(&reftree
, 0, scrub_txg
, -1);
2572 space_reftree_add_map(&reftree
,
2573 vd
->vdev_dtl
[DTL_SCRUB
], 2);
2574 space_reftree_generate_map(&reftree
,
2575 vd
->vdev_dtl
[DTL_MISSING
], 1);
2576 space_reftree_destroy(&reftree
);
2578 range_tree_vacate(vd
->vdev_dtl
[DTL_PARTIAL
], NULL
, NULL
);
2579 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2580 range_tree_add
, vd
->vdev_dtl
[DTL_PARTIAL
]);
2582 range_tree_vacate(vd
->vdev_dtl
[DTL_SCRUB
], NULL
, NULL
);
2583 range_tree_vacate(vd
->vdev_dtl
[DTL_OUTAGE
], NULL
, NULL
);
2584 if (!vdev_readable(vd
))
2585 range_tree_add(vd
->vdev_dtl
[DTL_OUTAGE
], 0, -1ULL);
2587 range_tree_walk(vd
->vdev_dtl
[DTL_MISSING
],
2588 range_tree_add
, vd
->vdev_dtl
[DTL_OUTAGE
]);
2591 * If the vdev was resilvering and no longer has any
2592 * DTLs then reset its resilvering flag and dirty
2593 * the top level so that we persist the change.
2595 if (vd
->vdev_resilver_txg
!= 0 &&
2596 range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]) &&
2597 range_tree_is_empty(vd
->vdev_dtl
[DTL_OUTAGE
])) {
2598 vd
->vdev_resilver_txg
= 0;
2599 vdev_config_dirty(vd
->vdev_top
);
2602 mutex_exit(&vd
->vdev_dtl_lock
);
2605 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, txg
);
2609 mutex_enter(&vd
->vdev_dtl_lock
);
2610 for (int t
= 0; t
< DTL_TYPES
; t
++) {
2611 /* account for child's outage in parent's missing map */
2612 int s
= (t
== DTL_MISSING
) ? DTL_OUTAGE
: t
;
2614 continue; /* leaf vdevs only */
2615 if (t
== DTL_PARTIAL
)
2616 minref
= 1; /* i.e. non-zero */
2617 else if (vd
->vdev_nparity
!= 0)
2618 minref
= vd
->vdev_nparity
+ 1; /* RAID-Z */
2620 minref
= vd
->vdev_children
; /* any kind of mirror */
2621 space_reftree_create(&reftree
);
2622 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2623 vdev_t
*cvd
= vd
->vdev_child
[c
];
2624 mutex_enter(&cvd
->vdev_dtl_lock
);
2625 space_reftree_add_map(&reftree
, cvd
->vdev_dtl
[s
], 1);
2626 mutex_exit(&cvd
->vdev_dtl_lock
);
2628 space_reftree_generate_map(&reftree
, vd
->vdev_dtl
[t
], minref
);
2629 space_reftree_destroy(&reftree
);
2631 mutex_exit(&vd
->vdev_dtl_lock
);
2635 vdev_dtl_load(vdev_t
*vd
)
2637 spa_t
*spa
= vd
->vdev_spa
;
2638 objset_t
*mos
= spa
->spa_meta_objset
;
2641 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_dtl_object
!= 0) {
2642 ASSERT(vdev_is_concrete(vd
));
2644 error
= space_map_open(&vd
->vdev_dtl_sm
, mos
,
2645 vd
->vdev_dtl_object
, 0, -1ULL, 0);
2648 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2650 mutex_enter(&vd
->vdev_dtl_lock
);
2653 * Now that we've opened the space_map we need to update
2656 space_map_update(vd
->vdev_dtl_sm
);
2658 error
= space_map_load(vd
->vdev_dtl_sm
,
2659 vd
->vdev_dtl
[DTL_MISSING
], SM_ALLOC
);
2660 mutex_exit(&vd
->vdev_dtl_lock
);
2665 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2666 error
= vdev_dtl_load(vd
->vdev_child
[c
]);
2675 vdev_zap_allocation_data(vdev_t
*vd
, dmu_tx_t
*tx
)
2677 spa_t
*spa
= vd
->vdev_spa
;
2678 objset_t
*mos
= spa
->spa_meta_objset
;
2679 vdev_alloc_bias_t alloc_bias
= vd
->vdev_alloc_bias
;
2682 ASSERT(alloc_bias
!= VDEV_BIAS_NONE
);
2685 (alloc_bias
== VDEV_BIAS_LOG
) ? VDEV_ALLOC_BIAS_LOG
:
2686 (alloc_bias
== VDEV_BIAS_SPECIAL
) ? VDEV_ALLOC_BIAS_SPECIAL
:
2687 (alloc_bias
== VDEV_BIAS_DEDUP
) ? VDEV_ALLOC_BIAS_DEDUP
: NULL
;
2689 ASSERT(string
!= NULL
);
2690 VERIFY0(zap_add(mos
, vd
->vdev_top_zap
, VDEV_TOP_ZAP_ALLOCATION_BIAS
,
2691 1, strlen(string
) + 1, string
, tx
));
2693 if (alloc_bias
== VDEV_BIAS_SPECIAL
|| alloc_bias
== VDEV_BIAS_DEDUP
) {
2694 spa_activate_allocation_classes(spa
, tx
);
2699 vdev_destroy_unlink_zap(vdev_t
*vd
, uint64_t zapobj
, dmu_tx_t
*tx
)
2701 spa_t
*spa
= vd
->vdev_spa
;
2703 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zapobj
, tx
));
2704 VERIFY0(zap_remove_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2709 vdev_create_link_zap(vdev_t
*vd
, dmu_tx_t
*tx
)
2711 spa_t
*spa
= vd
->vdev_spa
;
2712 uint64_t zap
= zap_create(spa
->spa_meta_objset
, DMU_OTN_ZAP_METADATA
,
2713 DMU_OT_NONE
, 0, tx
);
2716 VERIFY0(zap_add_int(spa
->spa_meta_objset
, spa
->spa_all_vdev_zaps
,
2723 vdev_construct_zaps(vdev_t
*vd
, dmu_tx_t
*tx
)
2725 if (vd
->vdev_ops
!= &vdev_hole_ops
&&
2726 vd
->vdev_ops
!= &vdev_missing_ops
&&
2727 vd
->vdev_ops
!= &vdev_root_ops
&&
2728 !vd
->vdev_top
->vdev_removing
) {
2729 if (vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_leaf_zap
== 0) {
2730 vd
->vdev_leaf_zap
= vdev_create_link_zap(vd
, tx
);
2732 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
== 0) {
2733 vd
->vdev_top_zap
= vdev_create_link_zap(vd
, tx
);
2734 if (vd
->vdev_alloc_bias
!= VDEV_BIAS_NONE
)
2735 vdev_zap_allocation_data(vd
, tx
);
2739 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2740 vdev_construct_zaps(vd
->vdev_child
[i
], tx
);
2745 vdev_dtl_sync(vdev_t
*vd
, uint64_t txg
)
2747 spa_t
*spa
= vd
->vdev_spa
;
2748 range_tree_t
*rt
= vd
->vdev_dtl
[DTL_MISSING
];
2749 objset_t
*mos
= spa
->spa_meta_objset
;
2750 range_tree_t
*rtsync
;
2752 uint64_t object
= space_map_object(vd
->vdev_dtl_sm
);
2754 ASSERT(vdev_is_concrete(vd
));
2755 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
2757 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
2759 if (vd
->vdev_detached
|| vd
->vdev_top
->vdev_removing
) {
2760 mutex_enter(&vd
->vdev_dtl_lock
);
2761 space_map_free(vd
->vdev_dtl_sm
, tx
);
2762 space_map_close(vd
->vdev_dtl_sm
);
2763 vd
->vdev_dtl_sm
= NULL
;
2764 mutex_exit(&vd
->vdev_dtl_lock
);
2767 * We only destroy the leaf ZAP for detached leaves or for
2768 * removed log devices. Removed data devices handle leaf ZAP
2769 * cleanup later, once cancellation is no longer possible.
2771 if (vd
->vdev_leaf_zap
!= 0 && (vd
->vdev_detached
||
2772 vd
->vdev_top
->vdev_islog
)) {
2773 vdev_destroy_unlink_zap(vd
, vd
->vdev_leaf_zap
, tx
);
2774 vd
->vdev_leaf_zap
= 0;
2781 if (vd
->vdev_dtl_sm
== NULL
) {
2782 uint64_t new_object
;
2784 new_object
= space_map_alloc(mos
, vdev_dtl_sm_blksz
, tx
);
2785 VERIFY3U(new_object
, !=, 0);
2787 VERIFY0(space_map_open(&vd
->vdev_dtl_sm
, mos
, new_object
,
2789 ASSERT(vd
->vdev_dtl_sm
!= NULL
);
2792 rtsync
= range_tree_create(NULL
, NULL
);
2794 mutex_enter(&vd
->vdev_dtl_lock
);
2795 range_tree_walk(rt
, range_tree_add
, rtsync
);
2796 mutex_exit(&vd
->vdev_dtl_lock
);
2798 space_map_truncate(vd
->vdev_dtl_sm
, vdev_dtl_sm_blksz
, tx
);
2799 space_map_write(vd
->vdev_dtl_sm
, rtsync
, SM_ALLOC
, SM_NO_VDEVID
, tx
);
2800 range_tree_vacate(rtsync
, NULL
, NULL
);
2802 range_tree_destroy(rtsync
);
2805 * If the object for the space map has changed then dirty
2806 * the top level so that we update the config.
2808 if (object
!= space_map_object(vd
->vdev_dtl_sm
)) {
2809 vdev_dbgmsg(vd
, "txg %llu, spa %s, DTL old object %llu, "
2810 "new object %llu", (u_longlong_t
)txg
, spa_name(spa
),
2811 (u_longlong_t
)object
,
2812 (u_longlong_t
)space_map_object(vd
->vdev_dtl_sm
));
2813 vdev_config_dirty(vd
->vdev_top
);
2818 mutex_enter(&vd
->vdev_dtl_lock
);
2819 space_map_update(vd
->vdev_dtl_sm
);
2820 mutex_exit(&vd
->vdev_dtl_lock
);
2824 * Determine whether the specified vdev can be offlined/detached/removed
2825 * without losing data.
2828 vdev_dtl_required(vdev_t
*vd
)
2830 spa_t
*spa
= vd
->vdev_spa
;
2831 vdev_t
*tvd
= vd
->vdev_top
;
2832 uint8_t cant_read
= vd
->vdev_cant_read
;
2835 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
2837 if (vd
== spa
->spa_root_vdev
|| vd
== tvd
)
2841 * Temporarily mark the device as unreadable, and then determine
2842 * whether this results in any DTL outages in the top-level vdev.
2843 * If not, we can safely offline/detach/remove the device.
2845 vd
->vdev_cant_read
= B_TRUE
;
2846 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2847 required
= !vdev_dtl_empty(tvd
, DTL_OUTAGE
);
2848 vd
->vdev_cant_read
= cant_read
;
2849 vdev_dtl_reassess(tvd
, 0, 0, B_FALSE
);
2851 if (!required
&& zio_injection_enabled
)
2852 required
= !!zio_handle_device_injection(vd
, NULL
, ECHILD
);
2858 * Determine if resilver is needed, and if so the txg range.
2861 vdev_resilver_needed(vdev_t
*vd
, uint64_t *minp
, uint64_t *maxp
)
2863 boolean_t needed
= B_FALSE
;
2864 uint64_t thismin
= UINT64_MAX
;
2865 uint64_t thismax
= 0;
2867 if (vd
->vdev_children
== 0) {
2868 mutex_enter(&vd
->vdev_dtl_lock
);
2869 if (!range_tree_is_empty(vd
->vdev_dtl
[DTL_MISSING
]) &&
2870 vdev_writeable(vd
)) {
2872 thismin
= vdev_dtl_min(vd
);
2873 thismax
= vdev_dtl_max(vd
);
2876 mutex_exit(&vd
->vdev_dtl_lock
);
2878 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2879 vdev_t
*cvd
= vd
->vdev_child
[c
];
2880 uint64_t cmin
, cmax
;
2882 if (vdev_resilver_needed(cvd
, &cmin
, &cmax
)) {
2883 thismin
= MIN(thismin
, cmin
);
2884 thismax
= MAX(thismax
, cmax
);
2890 if (needed
&& minp
) {
2898 * Gets the checkpoint space map object from the vdev's ZAP.
2899 * Returns the spacemap object, or 0 if it wasn't in the ZAP,
2900 * the ZAP doesn't exist yet, or the ZAP is damaged.
2903 vdev_checkpoint_sm_object(vdev_t
*vd
)
2905 ASSERT0(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
));
2906 if (vd
->vdev_top_zap
== 0) {
2910 uint64_t sm_obj
= 0;
2911 int err
= zap_lookup(spa_meta_objset(vd
->vdev_spa
), vd
->vdev_top_zap
,
2912 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM
, sizeof (uint64_t), 1, &sm_obj
);
2914 if (err
!= 0 && err
!= ENOENT
) {
2915 vdev_dbgmsg(vd
, "vdev_load: vdev_checkpoint_sm_objset "
2916 "failed to retrieve checkpoint space map object from "
2917 "vdev ZAP [error=%d]", err
);
2918 ASSERT3S(err
, ==, ECKSUM
);
2925 vdev_load(vdev_t
*vd
)
2930 * Recursively load all children.
2932 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
2933 error
= vdev_load(vd
->vdev_child
[c
]);
2939 vdev_set_deflate_ratio(vd
);
2942 * On spa_load path, grab the allocation bias from our zap
2944 if (vd
== vd
->vdev_top
&& vd
->vdev_top_zap
!= 0) {
2945 spa_t
*spa
= vd
->vdev_spa
;
2948 if (zap_lookup(spa
->spa_meta_objset
, vd
->vdev_top_zap
,
2949 VDEV_TOP_ZAP_ALLOCATION_BIAS
, 1, sizeof (bias_str
),
2951 ASSERT(vd
->vdev_alloc_bias
== VDEV_BIAS_NONE
);
2952 vd
->vdev_alloc_bias
= vdev_derive_alloc_bias(bias_str
);
2957 * If this is a top-level vdev, initialize its metaslabs.
2959 if (vd
== vd
->vdev_top
&& vdev_is_concrete(vd
)) {
2960 vdev_metaslab_group_create(vd
);
2962 if (vd
->vdev_ashift
== 0 || vd
->vdev_asize
== 0) {
2963 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2964 VDEV_AUX_CORRUPT_DATA
);
2965 vdev_dbgmsg(vd
, "vdev_load: invalid size. ashift=%llu, "
2966 "asize=%llu", (u_longlong_t
)vd
->vdev_ashift
,
2967 (u_longlong_t
)vd
->vdev_asize
);
2968 return (SET_ERROR(ENXIO
));
2969 } else if ((error
= vdev_metaslab_init(vd
, 0)) != 0) {
2970 vdev_dbgmsg(vd
, "vdev_load: metaslab_init failed "
2971 "[error=%d]", error
);
2972 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
2973 VDEV_AUX_CORRUPT_DATA
);
2977 uint64_t checkpoint_sm_obj
= vdev_checkpoint_sm_object(vd
);
2978 if (checkpoint_sm_obj
!= 0) {
2979 objset_t
*mos
= spa_meta_objset(vd
->vdev_spa
);
2980 ASSERT(vd
->vdev_asize
!= 0);
2981 ASSERT3P(vd
->vdev_checkpoint_sm
, ==, NULL
);
2983 if ((error
= space_map_open(&vd
->vdev_checkpoint_sm
,
2984 mos
, checkpoint_sm_obj
, 0, vd
->vdev_asize
,
2985 vd
->vdev_ashift
))) {
2986 vdev_dbgmsg(vd
, "vdev_load: space_map_open "
2987 "failed for checkpoint spacemap (obj %llu) "
2989 (u_longlong_t
)checkpoint_sm_obj
, error
);
2992 ASSERT3P(vd
->vdev_checkpoint_sm
, !=, NULL
);
2993 space_map_update(vd
->vdev_checkpoint_sm
);
2996 * Since the checkpoint_sm contains free entries
2997 * exclusively we can use sm_alloc to indicate the
2998 * culmulative checkpointed space that has been freed.
3000 vd
->vdev_stat
.vs_checkpoint_space
=
3001 -vd
->vdev_checkpoint_sm
->sm_alloc
;
3002 vd
->vdev_spa
->spa_checkpoint_info
.sci_dspace
+=
3003 vd
->vdev_stat
.vs_checkpoint_space
;
3008 * If this is a leaf vdev, load its DTL.
3010 if (vd
->vdev_ops
->vdev_op_leaf
&& (error
= vdev_dtl_load(vd
)) != 0) {
3011 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3012 VDEV_AUX_CORRUPT_DATA
);
3013 vdev_dbgmsg(vd
, "vdev_load: vdev_dtl_load failed "
3014 "[error=%d]", error
);
3018 uint64_t obsolete_sm_object
= vdev_obsolete_sm_object(vd
);
3019 if (obsolete_sm_object
!= 0) {
3020 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
3021 ASSERT(vd
->vdev_asize
!= 0);
3022 ASSERT3P(vd
->vdev_obsolete_sm
, ==, NULL
);
3024 if ((error
= space_map_open(&vd
->vdev_obsolete_sm
, mos
,
3025 obsolete_sm_object
, 0, vd
->vdev_asize
, 0))) {
3026 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
3027 VDEV_AUX_CORRUPT_DATA
);
3028 vdev_dbgmsg(vd
, "vdev_load: space_map_open failed for "
3029 "obsolete spacemap (obj %llu) [error=%d]",
3030 (u_longlong_t
)obsolete_sm_object
, error
);
3033 space_map_update(vd
->vdev_obsolete_sm
);
3040 * The special vdev case is used for hot spares and l2cache devices. Its
3041 * sole purpose it to set the vdev state for the associated vdev. To do this,
3042 * we make sure that we can open the underlying device, then try to read the
3043 * label, and make sure that the label is sane and that it hasn't been
3044 * repurposed to another pool.
3047 vdev_validate_aux(vdev_t
*vd
)
3050 uint64_t guid
, version
;
3053 if (!vdev_readable(vd
))
3056 if ((label
= vdev_label_read_config(vd
, -1ULL)) == NULL
) {
3057 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
3058 VDEV_AUX_CORRUPT_DATA
);
3062 if (nvlist_lookup_uint64(label
, ZPOOL_CONFIG_VERSION
, &version
) != 0 ||
3063 !SPA_VERSION_IS_SUPPORTED(version
) ||
3064 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) != 0 ||
3065 guid
!= vd
->vdev_guid
||
3066 nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_STATE
, &state
) != 0) {
3067 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_CANT_OPEN
,
3068 VDEV_AUX_CORRUPT_DATA
);
3074 * We don't actually check the pool state here. If it's in fact in
3075 * use by another pool, we update this fact on the fly when requested.
3082 * Free the objects used to store this vdev's spacemaps, and the array
3083 * that points to them.
3086 vdev_destroy_spacemaps(vdev_t
*vd
, dmu_tx_t
*tx
)
3088 if (vd
->vdev_ms_array
== 0)
3091 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
3092 uint64_t array_count
= vd
->vdev_asize
>> vd
->vdev_ms_shift
;
3093 size_t array_bytes
= array_count
* sizeof (uint64_t);
3094 uint64_t *smobj_array
= kmem_alloc(array_bytes
, KM_SLEEP
);
3095 VERIFY0(dmu_read(mos
, vd
->vdev_ms_array
, 0,
3096 array_bytes
, smobj_array
, 0));
3098 for (uint64_t i
= 0; i
< array_count
; i
++) {
3099 uint64_t smobj
= smobj_array
[i
];
3103 space_map_free_obj(mos
, smobj
, tx
);
3106 kmem_free(smobj_array
, array_bytes
);
3107 VERIFY0(dmu_object_free(mos
, vd
->vdev_ms_array
, tx
));
3108 vd
->vdev_ms_array
= 0;
3112 vdev_remove_empty(vdev_t
*vd
, uint64_t txg
)
3114 spa_t
*spa
= vd
->vdev_spa
;
3117 ASSERT(vd
== vd
->vdev_top
);
3118 ASSERT3U(txg
, ==, spa_syncing_txg(spa
));
3120 if (vd
->vdev_ms
!= NULL
) {
3121 metaslab_group_t
*mg
= vd
->vdev_mg
;
3123 metaslab_group_histogram_verify(mg
);
3124 metaslab_class_histogram_verify(mg
->mg_class
);
3126 for (int m
= 0; m
< vd
->vdev_ms_count
; m
++) {
3127 metaslab_t
*msp
= vd
->vdev_ms
[m
];
3129 if (msp
== NULL
|| msp
->ms_sm
== NULL
)
3132 mutex_enter(&msp
->ms_lock
);
3134 * If the metaslab was not loaded when the vdev
3135 * was removed then the histogram accounting may
3136 * not be accurate. Update the histogram information
3137 * here so that we ensure that the metaslab group
3138 * and metaslab class are up-to-date.
3140 metaslab_group_histogram_remove(mg
, msp
);
3142 VERIFY0(space_map_allocated(msp
->ms_sm
));
3143 space_map_close(msp
->ms_sm
);
3145 mutex_exit(&msp
->ms_lock
);
3148 if (vd
->vdev_checkpoint_sm
!= NULL
) {
3149 ASSERT(spa_has_checkpoint(spa
));
3150 space_map_close(vd
->vdev_checkpoint_sm
);
3151 vd
->vdev_checkpoint_sm
= NULL
;
3154 metaslab_group_histogram_verify(mg
);
3155 metaslab_class_histogram_verify(mg
->mg_class
);
3157 for (int i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++)
3158 ASSERT0(mg
->mg_histogram
[i
]);
3161 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
3162 vdev_destroy_spacemaps(vd
, tx
);
3164 if (vd
->vdev_islog
&& vd
->vdev_top_zap
!= 0) {
3165 vdev_destroy_unlink_zap(vd
, vd
->vdev_top_zap
, tx
);
3166 vd
->vdev_top_zap
= 0;
3172 vdev_sync_done(vdev_t
*vd
, uint64_t txg
)
3175 boolean_t reassess
= !txg_list_empty(&vd
->vdev_ms_list
, TXG_CLEAN(txg
));
3177 ASSERT(vdev_is_concrete(vd
));
3179 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, TXG_CLEAN(txg
))))
3180 metaslab_sync_done(msp
, txg
);
3183 metaslab_sync_reassess(vd
->vdev_mg
);
3187 vdev_sync(vdev_t
*vd
, uint64_t txg
)
3189 spa_t
*spa
= vd
->vdev_spa
;
3194 if (range_tree_space(vd
->vdev_obsolete_segments
) > 0) {
3197 ASSERT(vd
->vdev_removing
||
3198 vd
->vdev_ops
== &vdev_indirect_ops
);
3200 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
3201 vdev_indirect_sync_obsolete(vd
, tx
);
3205 * If the vdev is indirect, it can't have dirty
3206 * metaslabs or DTLs.
3208 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
3209 ASSERT(txg_list_empty(&vd
->vdev_ms_list
, txg
));
3210 ASSERT(txg_list_empty(&vd
->vdev_dtl_list
, txg
));
3215 ASSERT(vdev_is_concrete(vd
));
3217 if (vd
->vdev_ms_array
== 0 && vd
->vdev_ms_shift
!= 0 &&
3218 !vd
->vdev_removing
) {
3219 ASSERT(vd
== vd
->vdev_top
);
3220 ASSERT0(vd
->vdev_indirect_config
.vic_mapping_object
);
3221 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
3222 vd
->vdev_ms_array
= dmu_object_alloc(spa
->spa_meta_objset
,
3223 DMU_OT_OBJECT_ARRAY
, 0, DMU_OT_NONE
, 0, tx
);
3224 ASSERT(vd
->vdev_ms_array
!= 0);
3225 vdev_config_dirty(vd
);
3229 while ((msp
= txg_list_remove(&vd
->vdev_ms_list
, txg
)) != NULL
) {
3230 metaslab_sync(msp
, txg
);
3231 (void) txg_list_add(&vd
->vdev_ms_list
, msp
, TXG_CLEAN(txg
));
3234 while ((lvd
= txg_list_remove(&vd
->vdev_dtl_list
, txg
)) != NULL
)
3235 vdev_dtl_sync(lvd
, txg
);
3238 * Remove the metadata associated with this vdev once it's empty.
3239 * Note that this is typically used for log/cache device removal;
3240 * we don't empty toplevel vdevs when removing them. But if
3241 * a toplevel happens to be emptied, this is not harmful.
3243 if (vd
->vdev_stat
.vs_alloc
== 0 && vd
->vdev_removing
) {
3244 vdev_remove_empty(vd
, txg
);
3247 (void) txg_list_add(&spa
->spa_vdev_txg_list
, vd
, TXG_CLEAN(txg
));
3251 vdev_psize_to_asize(vdev_t
*vd
, uint64_t psize
)
3253 return (vd
->vdev_ops
->vdev_op_asize(vd
, psize
));
3257 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
3258 * not be opened, and no I/O is attempted.
3261 vdev_fault(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
3265 spa_vdev_state_enter(spa
, SCL_NONE
);
3267 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3268 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
3270 if (!vd
->vdev_ops
->vdev_op_leaf
)
3271 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3276 * If user did a 'zpool offline -f' then make the fault persist across
3279 if (aux
== VDEV_AUX_EXTERNAL_PERSIST
) {
3281 * There are two kinds of forced faults: temporary and
3282 * persistent. Temporary faults go away at pool import, while
3283 * persistent faults stay set. Both types of faults can be
3284 * cleared with a zpool clear.
3286 * We tell if a vdev is persistently faulted by looking at the
3287 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
3288 * import then it's a persistent fault. Otherwise, it's
3289 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
3290 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
3291 * tells vdev_config_generate() (which gets run later) to set
3292 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
3294 vd
->vdev_stat
.vs_aux
= VDEV_AUX_EXTERNAL
;
3295 vd
->vdev_tmpoffline
= B_FALSE
;
3296 aux
= VDEV_AUX_EXTERNAL
;
3298 vd
->vdev_tmpoffline
= B_TRUE
;
3302 * We don't directly use the aux state here, but if we do a
3303 * vdev_reopen(), we need this value to be present to remember why we
3306 vd
->vdev_label_aux
= aux
;
3309 * Faulted state takes precedence over degraded.
3311 vd
->vdev_delayed_close
= B_FALSE
;
3312 vd
->vdev_faulted
= 1ULL;
3313 vd
->vdev_degraded
= 0ULL;
3314 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_FAULTED
, aux
);
3317 * If this device has the only valid copy of the data, then
3318 * back off and simply mark the vdev as degraded instead.
3320 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&& vdev_dtl_required(vd
)) {
3321 vd
->vdev_degraded
= 1ULL;
3322 vd
->vdev_faulted
= 0ULL;
3325 * If we reopen the device and it's not dead, only then do we
3330 if (vdev_readable(vd
))
3331 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
, aux
);
3334 return (spa_vdev_state_exit(spa
, vd
, 0));
3338 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
3339 * user that something is wrong. The vdev continues to operate as normal as far
3340 * as I/O is concerned.
3343 vdev_degrade(spa_t
*spa
, uint64_t guid
, vdev_aux_t aux
)
3347 spa_vdev_state_enter(spa
, SCL_NONE
);
3349 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3350 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
3352 if (!vd
->vdev_ops
->vdev_op_leaf
)
3353 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3356 * If the vdev is already faulted, then don't do anything.
3358 if (vd
->vdev_faulted
|| vd
->vdev_degraded
)
3359 return (spa_vdev_state_exit(spa
, NULL
, 0));
3361 vd
->vdev_degraded
= 1ULL;
3362 if (!vdev_is_dead(vd
))
3363 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
,
3366 return (spa_vdev_state_exit(spa
, vd
, 0));
3370 * Online the given vdev.
3372 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
3373 * spare device should be detached when the device finishes resilvering.
3374 * Second, the online should be treated like a 'test' online case, so no FMA
3375 * events are generated if the device fails to open.
3378 vdev_online(spa_t
*spa
, uint64_t guid
, uint64_t flags
, vdev_state_t
*newstate
)
3380 vdev_t
*vd
, *tvd
, *pvd
, *rvd
= spa
->spa_root_vdev
;
3381 boolean_t wasoffline
;
3382 vdev_state_t oldstate
;
3384 spa_vdev_state_enter(spa
, SCL_NONE
);
3386 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3387 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
3389 if (!vd
->vdev_ops
->vdev_op_leaf
)
3390 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3392 wasoffline
= (vd
->vdev_offline
|| vd
->vdev_tmpoffline
);
3393 oldstate
= vd
->vdev_state
;
3396 vd
->vdev_offline
= B_FALSE
;
3397 vd
->vdev_tmpoffline
= B_FALSE
;
3398 vd
->vdev_checkremove
= !!(flags
& ZFS_ONLINE_CHECKREMOVE
);
3399 vd
->vdev_forcefault
= !!(flags
& ZFS_ONLINE_FORCEFAULT
);
3401 /* XXX - L2ARC 1.0 does not support expansion */
3402 if (!vd
->vdev_aux
) {
3403 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3404 pvd
->vdev_expanding
= !!((flags
& ZFS_ONLINE_EXPAND
) ||
3405 spa
->spa_autoexpand
);
3409 vd
->vdev_checkremove
= vd
->vdev_forcefault
= B_FALSE
;
3411 if (!vd
->vdev_aux
) {
3412 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
3413 pvd
->vdev_expanding
= B_FALSE
;
3417 *newstate
= vd
->vdev_state
;
3418 if ((flags
& ZFS_ONLINE_UNSPARE
) &&
3419 !vdev_is_dead(vd
) && vd
->vdev_parent
&&
3420 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3421 vd
->vdev_parent
->vdev_child
[0] == vd
)
3422 vd
->vdev_unspare
= B_TRUE
;
3424 if ((flags
& ZFS_ONLINE_EXPAND
) || spa
->spa_autoexpand
) {
3426 /* XXX - L2ARC 1.0 does not support expansion */
3428 return (spa_vdev_state_exit(spa
, vd
, ENOTSUP
));
3429 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3433 (oldstate
< VDEV_STATE_DEGRADED
&&
3434 vd
->vdev_state
>= VDEV_STATE_DEGRADED
))
3435 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_ONLINE
);
3437 return (spa_vdev_state_exit(spa
, vd
, 0));
3441 vdev_offline_locked(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
3445 uint64_t generation
;
3446 metaslab_group_t
*mg
;
3449 spa_vdev_state_enter(spa
, SCL_ALLOC
);
3451 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
3452 return (spa_vdev_state_exit(spa
, NULL
, ENODEV
));
3454 if (!vd
->vdev_ops
->vdev_op_leaf
)
3455 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
3459 generation
= spa
->spa_config_generation
+ 1;
3462 * If the device isn't already offline, try to offline it.
3464 if (!vd
->vdev_offline
) {
3466 * If this device has the only valid copy of some data,
3467 * don't allow it to be offlined. Log devices are always
3470 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
3471 vdev_dtl_required(vd
))
3472 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
3475 * If the top-level is a slog and it has had allocations
3476 * then proceed. We check that the vdev's metaslab group
3477 * is not NULL since it's possible that we may have just
3478 * added this vdev but not yet initialized its metaslabs.
3480 if (tvd
->vdev_islog
&& mg
!= NULL
) {
3482 * Prevent any future allocations.
3484 metaslab_group_passivate(mg
);
3485 (void) spa_vdev_state_exit(spa
, vd
, 0);
3487 error
= spa_reset_logs(spa
);
3490 * If the log device was successfully reset but has
3491 * checkpointed data, do not offline it.
3494 tvd
->vdev_checkpoint_sm
!= NULL
) {
3495 ASSERT3U(tvd
->vdev_checkpoint_sm
->sm_alloc
,
3497 error
= ZFS_ERR_CHECKPOINT_EXISTS
;
3500 spa_vdev_state_enter(spa
, SCL_ALLOC
);
3503 * Check to see if the config has changed.
3505 if (error
|| generation
!= spa
->spa_config_generation
) {
3506 metaslab_group_activate(mg
);
3508 return (spa_vdev_state_exit(spa
,
3510 (void) spa_vdev_state_exit(spa
, vd
, 0);
3513 ASSERT0(tvd
->vdev_stat
.vs_alloc
);
3517 * Offline this device and reopen its top-level vdev.
3518 * If the top-level vdev is a log device then just offline
3519 * it. Otherwise, if this action results in the top-level
3520 * vdev becoming unusable, undo it and fail the request.
3522 vd
->vdev_offline
= B_TRUE
;
3525 if (!tvd
->vdev_islog
&& vd
->vdev_aux
== NULL
&&
3526 vdev_is_dead(tvd
)) {
3527 vd
->vdev_offline
= B_FALSE
;
3529 return (spa_vdev_state_exit(spa
, NULL
, EBUSY
));
3533 * Add the device back into the metaslab rotor so that
3534 * once we online the device it's open for business.
3536 if (tvd
->vdev_islog
&& mg
!= NULL
)
3537 metaslab_group_activate(mg
);
3540 vd
->vdev_tmpoffline
= !!(flags
& ZFS_OFFLINE_TEMPORARY
);
3542 return (spa_vdev_state_exit(spa
, vd
, 0));
3546 vdev_offline(spa_t
*spa
, uint64_t guid
, uint64_t flags
)
3550 mutex_enter(&spa
->spa_vdev_top_lock
);
3551 error
= vdev_offline_locked(spa
, guid
, flags
);
3552 mutex_exit(&spa
->spa_vdev_top_lock
);
3558 * Clear the error counts associated with this vdev. Unlike vdev_online() and
3559 * vdev_offline(), we assume the spa config is locked. We also clear all
3560 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
3563 vdev_clear(spa_t
*spa
, vdev_t
*vd
)
3565 vdev_t
*rvd
= spa
->spa_root_vdev
;
3567 ASSERT(spa_config_held(spa
, SCL_STATE_ALL
, RW_WRITER
) == SCL_STATE_ALL
);
3572 vd
->vdev_stat
.vs_read_errors
= 0;
3573 vd
->vdev_stat
.vs_write_errors
= 0;
3574 vd
->vdev_stat
.vs_checksum_errors
= 0;
3576 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3577 vdev_clear(spa
, vd
->vdev_child
[c
]);
3580 * It makes no sense to "clear" an indirect vdev.
3582 if (!vdev_is_concrete(vd
))
3586 * If we're in the FAULTED state or have experienced failed I/O, then
3587 * clear the persistent state and attempt to reopen the device. We
3588 * also mark the vdev config dirty, so that the new faulted state is
3589 * written out to disk.
3591 if (vd
->vdev_faulted
|| vd
->vdev_degraded
||
3592 !vdev_readable(vd
) || !vdev_writeable(vd
)) {
3594 * When reopening in response to a clear event, it may be due to
3595 * a fmadm repair request. In this case, if the device is
3596 * still broken, we want to still post the ereport again.
3598 vd
->vdev_forcefault
= B_TRUE
;
3600 vd
->vdev_faulted
= vd
->vdev_degraded
= 0ULL;
3601 vd
->vdev_cant_read
= B_FALSE
;
3602 vd
->vdev_cant_write
= B_FALSE
;
3603 vd
->vdev_stat
.vs_aux
= 0;
3605 vdev_reopen(vd
== rvd
? rvd
: vd
->vdev_top
);
3607 vd
->vdev_forcefault
= B_FALSE
;
3609 if (vd
!= rvd
&& vdev_writeable(vd
->vdev_top
))
3610 vdev_state_dirty(vd
->vdev_top
);
3612 if (vd
->vdev_aux
== NULL
&& !vdev_is_dead(vd
))
3613 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
3615 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_CLEAR
);
3619 * When clearing a FMA-diagnosed fault, we always want to
3620 * unspare the device, as we assume that the original spare was
3621 * done in response to the FMA fault.
3623 if (!vdev_is_dead(vd
) && vd
->vdev_parent
!= NULL
&&
3624 vd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3625 vd
->vdev_parent
->vdev_child
[0] == vd
)
3626 vd
->vdev_unspare
= B_TRUE
;
3630 vdev_is_dead(vdev_t
*vd
)
3633 * Holes and missing devices are always considered "dead".
3634 * This simplifies the code since we don't have to check for
3635 * these types of devices in the various code paths.
3636 * Instead we rely on the fact that we skip over dead devices
3637 * before issuing I/O to them.
3639 return (vd
->vdev_state
< VDEV_STATE_DEGRADED
||
3640 vd
->vdev_ops
== &vdev_hole_ops
||
3641 vd
->vdev_ops
== &vdev_missing_ops
);
3645 vdev_readable(vdev_t
*vd
)
3647 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_read
);
3651 vdev_writeable(vdev_t
*vd
)
3653 return (!vdev_is_dead(vd
) && !vd
->vdev_cant_write
&&
3654 vdev_is_concrete(vd
));
3658 vdev_allocatable(vdev_t
*vd
)
3660 uint64_t state
= vd
->vdev_state
;
3663 * We currently allow allocations from vdevs which may be in the
3664 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3665 * fails to reopen then we'll catch it later when we're holding
3666 * the proper locks. Note that we have to get the vdev state
3667 * in a local variable because although it changes atomically,
3668 * we're asking two separate questions about it.
3670 return (!(state
< VDEV_STATE_DEGRADED
&& state
!= VDEV_STATE_CLOSED
) &&
3671 !vd
->vdev_cant_write
&& vdev_is_concrete(vd
) &&
3672 vd
->vdev_mg
->mg_initialized
);
3676 vdev_accessible(vdev_t
*vd
, zio_t
*zio
)
3678 ASSERT(zio
->io_vd
== vd
);
3680 if (vdev_is_dead(vd
) || vd
->vdev_remove_wanted
)
3683 if (zio
->io_type
== ZIO_TYPE_READ
)
3684 return (!vd
->vdev_cant_read
);
3686 if (zio
->io_type
== ZIO_TYPE_WRITE
)
3687 return (!vd
->vdev_cant_write
);
3693 vdev_get_child_stat(vdev_t
*cvd
, vdev_stat_t
*vs
, vdev_stat_t
*cvs
)
3696 for (t
= 0; t
< ZIO_TYPES
; t
++) {
3697 vs
->vs_ops
[t
] += cvs
->vs_ops
[t
];
3698 vs
->vs_bytes
[t
] += cvs
->vs_bytes
[t
];
3701 cvs
->vs_scan_removing
= cvd
->vdev_removing
;
3705 * Get extended stats
3708 vdev_get_child_stat_ex(vdev_t
*cvd
, vdev_stat_ex_t
*vsx
, vdev_stat_ex_t
*cvsx
)
3711 for (t
= 0; t
< ZIO_TYPES
; t
++) {
3712 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_disk_histo
[0]); b
++)
3713 vsx
->vsx_disk_histo
[t
][b
] += cvsx
->vsx_disk_histo
[t
][b
];
3715 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_total_histo
[0]); b
++) {
3716 vsx
->vsx_total_histo
[t
][b
] +=
3717 cvsx
->vsx_total_histo
[t
][b
];
3721 for (t
= 0; t
< ZIO_PRIORITY_NUM_QUEUEABLE
; t
++) {
3722 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_queue_histo
[0]); b
++) {
3723 vsx
->vsx_queue_histo
[t
][b
] +=
3724 cvsx
->vsx_queue_histo
[t
][b
];
3726 vsx
->vsx_active_queue
[t
] += cvsx
->vsx_active_queue
[t
];
3727 vsx
->vsx_pend_queue
[t
] += cvsx
->vsx_pend_queue
[t
];
3729 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_ind_histo
[0]); b
++)
3730 vsx
->vsx_ind_histo
[t
][b
] += cvsx
->vsx_ind_histo
[t
][b
];
3732 for (b
= 0; b
< ARRAY_SIZE(vsx
->vsx_agg_histo
[0]); b
++)
3733 vsx
->vsx_agg_histo
[t
][b
] += cvsx
->vsx_agg_histo
[t
][b
];
3739 vdev_is_spacemap_addressable(vdev_t
*vd
)
3742 * Assuming 47 bits of the space map entry dedicated for the entry's
3743 * offset (see description in space_map.h), we calculate the maximum
3744 * address that can be described by a space map entry for the given
3747 uint64_t shift
= vd
->vdev_ashift
+ 47;
3749 if (shift
>= 63) /* detect potential overflow */
3752 return (vd
->vdev_asize
< (1ULL << shift
));
3756 * Get statistics for the given vdev.
3759 vdev_get_stats_ex_impl(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
3763 * If we're getting stats on the root vdev, aggregate the I/O counts
3764 * over all top-level vdevs (i.e. the direct children of the root).
3766 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3768 memset(vs
->vs_ops
, 0, sizeof (vs
->vs_ops
));
3769 memset(vs
->vs_bytes
, 0, sizeof (vs
->vs_bytes
));
3772 memset(vsx
, 0, sizeof (*vsx
));
3774 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
3775 vdev_t
*cvd
= vd
->vdev_child
[c
];
3776 vdev_stat_t
*cvs
= &cvd
->vdev_stat
;
3777 vdev_stat_ex_t
*cvsx
= &cvd
->vdev_stat_ex
;
3779 vdev_get_stats_ex_impl(cvd
, cvs
, cvsx
);
3781 vdev_get_child_stat(cvd
, vs
, cvs
);
3783 vdev_get_child_stat_ex(cvd
, vsx
, cvsx
);
3788 * We're a leaf. Just copy our ZIO active queue stats in. The
3789 * other leaf stats are updated in vdev_stat_update().
3794 memcpy(vsx
, &vd
->vdev_stat_ex
, sizeof (vd
->vdev_stat_ex
));
3796 for (t
= 0; t
< ARRAY_SIZE(vd
->vdev_queue
.vq_class
); t
++) {
3797 vsx
->vsx_active_queue
[t
] =
3798 vd
->vdev_queue
.vq_class
[t
].vqc_active
;
3799 vsx
->vsx_pend_queue
[t
] = avl_numnodes(
3800 &vd
->vdev_queue
.vq_class
[t
].vqc_queued_tree
);
3806 vdev_get_stats_ex(vdev_t
*vd
, vdev_stat_t
*vs
, vdev_stat_ex_t
*vsx
)
3808 vdev_t
*tvd
= vd
->vdev_top
;
3809 mutex_enter(&vd
->vdev_stat_lock
);
3811 bcopy(&vd
->vdev_stat
, vs
, sizeof (*vs
));
3812 vs
->vs_timestamp
= gethrtime() - vs
->vs_timestamp
;
3813 vs
->vs_state
= vd
->vdev_state
;
3814 vs
->vs_rsize
= vdev_get_min_asize(vd
);
3815 if (vd
->vdev_ops
->vdev_op_leaf
)
3816 vs
->vs_rsize
+= VDEV_LABEL_START_SIZE
+
3817 VDEV_LABEL_END_SIZE
;
3819 * Report expandable space on top-level, non-auxillary devices
3820 * only. The expandable space is reported in terms of metaslab
3821 * sized units since that determines how much space the pool
3824 if (vd
->vdev_aux
== NULL
&& tvd
!= NULL
) {
3825 vs
->vs_esize
= P2ALIGN(
3826 vd
->vdev_max_asize
- vd
->vdev_asize
,
3827 1ULL << tvd
->vdev_ms_shift
);
3829 if (vd
->vdev_aux
== NULL
&& vd
== vd
->vdev_top
&&
3830 vdev_is_concrete(vd
)) {
3831 vs
->vs_fragmentation
= (vd
->vdev_mg
!= NULL
) ?
3832 vd
->vdev_mg
->mg_fragmentation
: 0;
3836 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_READER
) != 0);
3837 vdev_get_stats_ex_impl(vd
, vs
, vsx
);
3838 mutex_exit(&vd
->vdev_stat_lock
);
3842 vdev_get_stats(vdev_t
*vd
, vdev_stat_t
*vs
)
3844 return (vdev_get_stats_ex(vd
, vs
, NULL
));
3848 vdev_clear_stats(vdev_t
*vd
)
3850 mutex_enter(&vd
->vdev_stat_lock
);
3851 vd
->vdev_stat
.vs_space
= 0;
3852 vd
->vdev_stat
.vs_dspace
= 0;
3853 vd
->vdev_stat
.vs_alloc
= 0;
3854 mutex_exit(&vd
->vdev_stat_lock
);
3858 vdev_scan_stat_init(vdev_t
*vd
)
3860 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3862 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3863 vdev_scan_stat_init(vd
->vdev_child
[c
]);
3865 mutex_enter(&vd
->vdev_stat_lock
);
3866 vs
->vs_scan_processed
= 0;
3867 mutex_exit(&vd
->vdev_stat_lock
);
3871 vdev_stat_update(zio_t
*zio
, uint64_t psize
)
3873 spa_t
*spa
= zio
->io_spa
;
3874 vdev_t
*rvd
= spa
->spa_root_vdev
;
3875 vdev_t
*vd
= zio
->io_vd
? zio
->io_vd
: rvd
;
3877 uint64_t txg
= zio
->io_txg
;
3878 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3879 vdev_stat_ex_t
*vsx
= &vd
->vdev_stat_ex
;
3880 zio_type_t type
= zio
->io_type
;
3881 int flags
= zio
->io_flags
;
3884 * If this i/o is a gang leader, it didn't do any actual work.
3886 if (zio
->io_gang_tree
)
3889 if (zio
->io_error
== 0) {
3891 * If this is a root i/o, don't count it -- we've already
3892 * counted the top-level vdevs, and vdev_get_stats() will
3893 * aggregate them when asked. This reduces contention on
3894 * the root vdev_stat_lock and implicitly handles blocks
3895 * that compress away to holes, for which there is no i/o.
3896 * (Holes never create vdev children, so all the counters
3897 * remain zero, which is what we want.)
3899 * Note: this only applies to successful i/o (io_error == 0)
3900 * because unlike i/o counts, errors are not additive.
3901 * When reading a ditto block, for example, failure of
3902 * one top-level vdev does not imply a root-level error.
3907 ASSERT(vd
== zio
->io_vd
);
3909 if (flags
& ZIO_FLAG_IO_BYPASS
)
3912 mutex_enter(&vd
->vdev_stat_lock
);
3914 if (flags
& ZIO_FLAG_IO_REPAIR
) {
3915 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
3916 dsl_scan_phys_t
*scn_phys
=
3917 &spa
->spa_dsl_pool
->dp_scan
->scn_phys
;
3918 uint64_t *processed
= &scn_phys
->scn_processed
;
3921 if (vd
->vdev_ops
->vdev_op_leaf
)
3922 atomic_add_64(processed
, psize
);
3923 vs
->vs_scan_processed
+= psize
;
3926 if (flags
& ZIO_FLAG_SELF_HEAL
)
3927 vs
->vs_self_healed
+= psize
;
3931 * The bytes/ops/histograms are recorded at the leaf level and
3932 * aggregated into the higher level vdevs in vdev_get_stats().
3934 if (vd
->vdev_ops
->vdev_op_leaf
&&
3935 (zio
->io_priority
< ZIO_PRIORITY_NUM_QUEUEABLE
)) {
3938 vs
->vs_bytes
[type
] += psize
;
3940 if (flags
& ZIO_FLAG_DELEGATED
) {
3941 vsx
->vsx_agg_histo
[zio
->io_priority
]
3942 [RQ_HISTO(zio
->io_size
)]++;
3944 vsx
->vsx_ind_histo
[zio
->io_priority
]
3945 [RQ_HISTO(zio
->io_size
)]++;
3948 if (zio
->io_delta
&& zio
->io_delay
) {
3949 vsx
->vsx_queue_histo
[zio
->io_priority
]
3950 [L_HISTO(zio
->io_delta
- zio
->io_delay
)]++;
3951 vsx
->vsx_disk_histo
[type
]
3952 [L_HISTO(zio
->io_delay
)]++;
3953 vsx
->vsx_total_histo
[type
]
3954 [L_HISTO(zio
->io_delta
)]++;
3958 mutex_exit(&vd
->vdev_stat_lock
);
3962 if (flags
& ZIO_FLAG_SPECULATIVE
)
3966 * If this is an I/O error that is going to be retried, then ignore the
3967 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
3968 * hard errors, when in reality they can happen for any number of
3969 * innocuous reasons (bus resets, MPxIO link failure, etc).
3971 if (zio
->io_error
== EIO
&&
3972 !(zio
->io_flags
& ZIO_FLAG_IO_RETRY
))
3976 * Intent logs writes won't propagate their error to the root
3977 * I/O so don't mark these types of failures as pool-level
3980 if (zio
->io_vd
== NULL
&& (zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
))
3983 mutex_enter(&vd
->vdev_stat_lock
);
3984 if (type
== ZIO_TYPE_READ
&& !vdev_is_dead(vd
)) {
3985 if (zio
->io_error
== ECKSUM
)
3986 vs
->vs_checksum_errors
++;
3988 vs
->vs_read_errors
++;
3990 if (type
== ZIO_TYPE_WRITE
&& !vdev_is_dead(vd
))
3991 vs
->vs_write_errors
++;
3992 mutex_exit(&vd
->vdev_stat_lock
);
3994 if (spa
->spa_load_state
== SPA_LOAD_NONE
&&
3995 type
== ZIO_TYPE_WRITE
&& txg
!= 0 &&
3996 (!(flags
& ZIO_FLAG_IO_REPAIR
) ||
3997 (flags
& ZIO_FLAG_SCAN_THREAD
) ||
3998 spa
->spa_claiming
)) {
4000 * This is either a normal write (not a repair), or it's
4001 * a repair induced by the scrub thread, or it's a repair
4002 * made by zil_claim() during spa_load() in the first txg.
4003 * In the normal case, we commit the DTL change in the same
4004 * txg as the block was born. In the scrub-induced repair
4005 * case, we know that scrubs run in first-pass syncing context,
4006 * so we commit the DTL change in spa_syncing_txg(spa).
4007 * In the zil_claim() case, we commit in spa_first_txg(spa).
4009 * We currently do not make DTL entries for failed spontaneous
4010 * self-healing writes triggered by normal (non-scrubbing)
4011 * reads, because we have no transactional context in which to
4012 * do so -- and it's not clear that it'd be desirable anyway.
4014 if (vd
->vdev_ops
->vdev_op_leaf
) {
4015 uint64_t commit_txg
= txg
;
4016 if (flags
& ZIO_FLAG_SCAN_THREAD
) {
4017 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
4018 ASSERT(spa_sync_pass(spa
) == 1);
4019 vdev_dtl_dirty(vd
, DTL_SCRUB
, txg
, 1);
4020 commit_txg
= spa_syncing_txg(spa
);
4021 } else if (spa
->spa_claiming
) {
4022 ASSERT(flags
& ZIO_FLAG_IO_REPAIR
);
4023 commit_txg
= spa_first_txg(spa
);
4025 ASSERT(commit_txg
>= spa_syncing_txg(spa
));
4026 if (vdev_dtl_contains(vd
, DTL_MISSING
, txg
, 1))
4028 for (pvd
= vd
; pvd
!= rvd
; pvd
= pvd
->vdev_parent
)
4029 vdev_dtl_dirty(pvd
, DTL_PARTIAL
, txg
, 1);
4030 vdev_dirty(vd
->vdev_top
, VDD_DTL
, vd
, commit_txg
);
4033 vdev_dtl_dirty(vd
, DTL_MISSING
, txg
, 1);
4038 vdev_deflated_space(vdev_t
*vd
, int64_t space
)
4040 ASSERT((space
& (SPA_MINBLOCKSIZE
-1)) == 0);
4041 ASSERT(vd
->vdev_deflate_ratio
!= 0 || vd
->vdev_isl2cache
);
4043 return ((space
>> SPA_MINBLOCKSHIFT
) * vd
->vdev_deflate_ratio
);
4047 * Update the in-core space usage stats for this vdev and the root vdev.
4050 vdev_space_update(vdev_t
*vd
, int64_t alloc_delta
, int64_t defer_delta
,
4051 int64_t space_delta
)
4053 int64_t dspace_delta
;
4054 spa_t
*spa
= vd
->vdev_spa
;
4055 vdev_t
*rvd
= spa
->spa_root_vdev
;
4057 ASSERT(vd
== vd
->vdev_top
);
4060 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
4061 * factor. We must calculate this here and not at the root vdev
4062 * because the root vdev's psize-to-asize is simply the max of its
4063 * childrens', thus not accurate enough for us.
4065 dspace_delta
= vdev_deflated_space(vd
, space_delta
);
4067 mutex_enter(&vd
->vdev_stat_lock
);
4068 vd
->vdev_stat
.vs_alloc
+= alloc_delta
;
4069 vd
->vdev_stat
.vs_space
+= space_delta
;
4070 vd
->vdev_stat
.vs_dspace
+= dspace_delta
;
4071 mutex_exit(&vd
->vdev_stat_lock
);
4073 /* every class but log contributes to root space stats */
4074 if (vd
->vdev_mg
!= NULL
&& !vd
->vdev_islog
) {
4075 mutex_enter(&rvd
->vdev_stat_lock
);
4076 rvd
->vdev_stat
.vs_alloc
+= alloc_delta
;
4077 rvd
->vdev_stat
.vs_space
+= space_delta
;
4078 rvd
->vdev_stat
.vs_dspace
+= dspace_delta
;
4079 mutex_exit(&rvd
->vdev_stat_lock
);
4081 /* Note: metaslab_class_space_update moved to metaslab_space_update */
4085 * Mark a top-level vdev's config as dirty, placing it on the dirty list
4086 * so that it will be written out next time the vdev configuration is synced.
4087 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
4090 vdev_config_dirty(vdev_t
*vd
)
4092 spa_t
*spa
= vd
->vdev_spa
;
4093 vdev_t
*rvd
= spa
->spa_root_vdev
;
4096 ASSERT(spa_writeable(spa
));
4099 * If this is an aux vdev (as with l2cache and spare devices), then we
4100 * update the vdev config manually and set the sync flag.
4102 if (vd
->vdev_aux
!= NULL
) {
4103 spa_aux_vdev_t
*sav
= vd
->vdev_aux
;
4107 for (c
= 0; c
< sav
->sav_count
; c
++) {
4108 if (sav
->sav_vdevs
[c
] == vd
)
4112 if (c
== sav
->sav_count
) {
4114 * We're being removed. There's nothing more to do.
4116 ASSERT(sav
->sav_sync
== B_TRUE
);
4120 sav
->sav_sync
= B_TRUE
;
4122 if (nvlist_lookup_nvlist_array(sav
->sav_config
,
4123 ZPOOL_CONFIG_L2CACHE
, &aux
, &naux
) != 0) {
4124 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
4125 ZPOOL_CONFIG_SPARES
, &aux
, &naux
) == 0);
4131 * Setting the nvlist in the middle if the array is a little
4132 * sketchy, but it will work.
4134 nvlist_free(aux
[c
]);
4135 aux
[c
] = vdev_config_generate(spa
, vd
, B_TRUE
, 0);
4141 * The dirty list is protected by the SCL_CONFIG lock. The caller
4142 * must either hold SCL_CONFIG as writer, or must be the sync thread
4143 * (which holds SCL_CONFIG as reader). There's only one sync thread,
4144 * so this is sufficient to ensure mutual exclusion.
4146 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
4147 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
4148 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
4151 for (c
= 0; c
< rvd
->vdev_children
; c
++)
4152 vdev_config_dirty(rvd
->vdev_child
[c
]);
4154 ASSERT(vd
== vd
->vdev_top
);
4156 if (!list_link_active(&vd
->vdev_config_dirty_node
) &&
4157 vdev_is_concrete(vd
)) {
4158 list_insert_head(&spa
->spa_config_dirty_list
, vd
);
4164 vdev_config_clean(vdev_t
*vd
)
4166 spa_t
*spa
= vd
->vdev_spa
;
4168 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_WRITER
) ||
4169 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
4170 spa_config_held(spa
, SCL_CONFIG
, RW_READER
)));
4172 ASSERT(list_link_active(&vd
->vdev_config_dirty_node
));
4173 list_remove(&spa
->spa_config_dirty_list
, vd
);
4177 * Mark a top-level vdev's state as dirty, so that the next pass of
4178 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
4179 * the state changes from larger config changes because they require
4180 * much less locking, and are often needed for administrative actions.
4183 vdev_state_dirty(vdev_t
*vd
)
4185 spa_t
*spa
= vd
->vdev_spa
;
4187 ASSERT(spa_writeable(spa
));
4188 ASSERT(vd
== vd
->vdev_top
);
4191 * The state list is protected by the SCL_STATE lock. The caller
4192 * must either hold SCL_STATE as writer, or must be the sync thread
4193 * (which holds SCL_STATE as reader). There's only one sync thread,
4194 * so this is sufficient to ensure mutual exclusion.
4196 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
4197 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
4198 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
4200 if (!list_link_active(&vd
->vdev_state_dirty_node
) &&
4201 vdev_is_concrete(vd
))
4202 list_insert_head(&spa
->spa_state_dirty_list
, vd
);
4206 vdev_state_clean(vdev_t
*vd
)
4208 spa_t
*spa
= vd
->vdev_spa
;
4210 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_WRITER
) ||
4211 (dsl_pool_sync_context(spa_get_dsl(spa
)) &&
4212 spa_config_held(spa
, SCL_STATE
, RW_READER
)));
4214 ASSERT(list_link_active(&vd
->vdev_state_dirty_node
));
4215 list_remove(&spa
->spa_state_dirty_list
, vd
);
4219 * Propagate vdev state up from children to parent.
4222 vdev_propagate_state(vdev_t
*vd
)
4224 spa_t
*spa
= vd
->vdev_spa
;
4225 vdev_t
*rvd
= spa
->spa_root_vdev
;
4226 int degraded
= 0, faulted
= 0;
4230 if (vd
->vdev_children
> 0) {
4231 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4232 child
= vd
->vdev_child
[c
];
4235 * Don't factor holes or indirect vdevs into the
4238 if (!vdev_is_concrete(child
))
4241 if (!vdev_readable(child
) ||
4242 (!vdev_writeable(child
) && spa_writeable(spa
))) {
4244 * Root special: if there is a top-level log
4245 * device, treat the root vdev as if it were
4248 if (child
->vdev_islog
&& vd
== rvd
)
4252 } else if (child
->vdev_state
<= VDEV_STATE_DEGRADED
) {
4256 if (child
->vdev_stat
.vs_aux
== VDEV_AUX_CORRUPT_DATA
)
4260 vd
->vdev_ops
->vdev_op_state_change(vd
, faulted
, degraded
);
4263 * Root special: if there is a top-level vdev that cannot be
4264 * opened due to corrupted metadata, then propagate the root
4265 * vdev's aux state as 'corrupt' rather than 'insufficient
4268 if (corrupted
&& vd
== rvd
&&
4269 rvd
->vdev_state
== VDEV_STATE_CANT_OPEN
)
4270 vdev_set_state(rvd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
4271 VDEV_AUX_CORRUPT_DATA
);
4274 if (vd
->vdev_parent
)
4275 vdev_propagate_state(vd
->vdev_parent
);
4279 * Set a vdev's state. If this is during an open, we don't update the parent
4280 * state, because we're in the process of opening children depth-first.
4281 * Otherwise, we propagate the change to the parent.
4283 * If this routine places a device in a faulted state, an appropriate ereport is
4287 vdev_set_state(vdev_t
*vd
, boolean_t isopen
, vdev_state_t state
, vdev_aux_t aux
)
4289 uint64_t save_state
;
4290 spa_t
*spa
= vd
->vdev_spa
;
4292 if (state
== vd
->vdev_state
) {
4294 * Since vdev_offline() code path is already in an offline
4295 * state we can miss a statechange event to OFFLINE. Check
4296 * the previous state to catch this condition.
4298 if (vd
->vdev_ops
->vdev_op_leaf
&&
4299 (state
== VDEV_STATE_OFFLINE
) &&
4300 (vd
->vdev_prevstate
>= VDEV_STATE_FAULTED
)) {
4301 /* post an offline state change */
4302 zfs_post_state_change(spa
, vd
, vd
->vdev_prevstate
);
4304 vd
->vdev_stat
.vs_aux
= aux
;
4308 save_state
= vd
->vdev_state
;
4310 vd
->vdev_state
= state
;
4311 vd
->vdev_stat
.vs_aux
= aux
;
4314 * If we are setting the vdev state to anything but an open state, then
4315 * always close the underlying device unless the device has requested
4316 * a delayed close (i.e. we're about to remove or fault the device).
4317 * Otherwise, we keep accessible but invalid devices open forever.
4318 * We don't call vdev_close() itself, because that implies some extra
4319 * checks (offline, etc) that we don't want here. This is limited to
4320 * leaf devices, because otherwise closing the device will affect other
4323 if (!vd
->vdev_delayed_close
&& vdev_is_dead(vd
) &&
4324 vd
->vdev_ops
->vdev_op_leaf
)
4325 vd
->vdev_ops
->vdev_op_close(vd
);
4327 if (vd
->vdev_removed
&&
4328 state
== VDEV_STATE_CANT_OPEN
&&
4329 (aux
== VDEV_AUX_OPEN_FAILED
|| vd
->vdev_checkremove
)) {
4331 * If the previous state is set to VDEV_STATE_REMOVED, then this
4332 * device was previously marked removed and someone attempted to
4333 * reopen it. If this failed due to a nonexistent device, then
4334 * keep the device in the REMOVED state. We also let this be if
4335 * it is one of our special test online cases, which is only
4336 * attempting to online the device and shouldn't generate an FMA
4339 vd
->vdev_state
= VDEV_STATE_REMOVED
;
4340 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NONE
;
4341 } else if (state
== VDEV_STATE_REMOVED
) {
4342 vd
->vdev_removed
= B_TRUE
;
4343 } else if (state
== VDEV_STATE_CANT_OPEN
) {
4345 * If we fail to open a vdev during an import or recovery, we
4346 * mark it as "not available", which signifies that it was
4347 * never there to begin with. Failure to open such a device
4348 * is not considered an error.
4350 if ((spa_load_state(spa
) == SPA_LOAD_IMPORT
||
4351 spa_load_state(spa
) == SPA_LOAD_RECOVER
) &&
4352 vd
->vdev_ops
->vdev_op_leaf
)
4353 vd
->vdev_not_present
= 1;
4356 * Post the appropriate ereport. If the 'prevstate' field is
4357 * set to something other than VDEV_STATE_UNKNOWN, it indicates
4358 * that this is part of a vdev_reopen(). In this case, we don't
4359 * want to post the ereport if the device was already in the
4360 * CANT_OPEN state beforehand.
4362 * If the 'checkremove' flag is set, then this is an attempt to
4363 * online the device in response to an insertion event. If we
4364 * hit this case, then we have detected an insertion event for a
4365 * faulted or offline device that wasn't in the removed state.
4366 * In this scenario, we don't post an ereport because we are
4367 * about to replace the device, or attempt an online with
4368 * vdev_forcefault, which will generate the fault for us.
4370 if ((vd
->vdev_prevstate
!= state
|| vd
->vdev_forcefault
) &&
4371 !vd
->vdev_not_present
&& !vd
->vdev_checkremove
&&
4372 vd
!= spa
->spa_root_vdev
) {
4376 case VDEV_AUX_OPEN_FAILED
:
4377 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED
;
4379 case VDEV_AUX_CORRUPT_DATA
:
4380 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA
;
4382 case VDEV_AUX_NO_REPLICAS
:
4383 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS
;
4385 case VDEV_AUX_BAD_GUID_SUM
:
4386 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM
;
4388 case VDEV_AUX_TOO_SMALL
:
4389 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL
;
4391 case VDEV_AUX_BAD_LABEL
:
4392 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL
;
4394 case VDEV_AUX_BAD_ASHIFT
:
4395 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT
;
4398 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN
;
4401 zfs_ereport_post(class, spa
, vd
, NULL
, NULL
,
4405 /* Erase any notion of persistent removed state */
4406 vd
->vdev_removed
= B_FALSE
;
4408 vd
->vdev_removed
= B_FALSE
;
4412 * Notify ZED of any significant state-change on a leaf vdev.
4415 if (vd
->vdev_ops
->vdev_op_leaf
) {
4416 /* preserve original state from a vdev_reopen() */
4417 if ((vd
->vdev_prevstate
!= VDEV_STATE_UNKNOWN
) &&
4418 (vd
->vdev_prevstate
!= vd
->vdev_state
) &&
4419 (save_state
<= VDEV_STATE_CLOSED
))
4420 save_state
= vd
->vdev_prevstate
;
4422 /* filter out state change due to initial vdev_open */
4423 if (save_state
> VDEV_STATE_CLOSED
)
4424 zfs_post_state_change(spa
, vd
, save_state
);
4427 if (!isopen
&& vd
->vdev_parent
)
4428 vdev_propagate_state(vd
->vdev_parent
);
4432 vdev_children_are_offline(vdev_t
*vd
)
4434 ASSERT(!vd
->vdev_ops
->vdev_op_leaf
);
4436 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
4437 if (vd
->vdev_child
[i
]->vdev_state
!= VDEV_STATE_OFFLINE
)
4445 * Check the vdev configuration to ensure that it's capable of supporting
4446 * a root pool. We do not support partial configuration.
4449 vdev_is_bootable(vdev_t
*vd
)
4451 if (!vd
->vdev_ops
->vdev_op_leaf
) {
4452 const char *vdev_type
= vd
->vdev_ops
->vdev_op_type
;
4454 if (strcmp(vdev_type
, VDEV_TYPE_MISSING
) == 0 ||
4455 strcmp(vdev_type
, VDEV_TYPE_INDIRECT
) == 0) {
4460 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4461 if (!vdev_is_bootable(vd
->vdev_child
[c
]))
4468 vdev_is_concrete(vdev_t
*vd
)
4470 vdev_ops_t
*ops
= vd
->vdev_ops
;
4471 if (ops
== &vdev_indirect_ops
|| ops
== &vdev_hole_ops
||
4472 ops
== &vdev_missing_ops
|| ops
== &vdev_root_ops
) {
4480 * Determine if a log device has valid content. If the vdev was
4481 * removed or faulted in the MOS config then we know that
4482 * the content on the log device has already been written to the pool.
4485 vdev_log_state_valid(vdev_t
*vd
)
4487 if (vd
->vdev_ops
->vdev_op_leaf
&& !vd
->vdev_faulted
&&
4491 for (int c
= 0; c
< vd
->vdev_children
; c
++)
4492 if (vdev_log_state_valid(vd
->vdev_child
[c
]))
4499 * Expand a vdev if possible.
4502 vdev_expand(vdev_t
*vd
, uint64_t txg
)
4504 ASSERT(vd
->vdev_top
== vd
);
4505 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
4506 ASSERT(vdev_is_concrete(vd
));
4508 vdev_set_deflate_ratio(vd
);
4510 if ((vd
->vdev_asize
>> vd
->vdev_ms_shift
) > vd
->vdev_ms_count
&&
4511 vdev_is_concrete(vd
)) {
4512 vdev_metaslab_group_create(vd
);
4513 VERIFY(vdev_metaslab_init(vd
, txg
) == 0);
4514 vdev_config_dirty(vd
);
4522 vdev_split(vdev_t
*vd
)
4524 vdev_t
*cvd
, *pvd
= vd
->vdev_parent
;
4526 vdev_remove_child(pvd
, vd
);
4527 vdev_compact_children(pvd
);
4529 cvd
= pvd
->vdev_child
[0];
4530 if (pvd
->vdev_children
== 1) {
4531 vdev_remove_parent(cvd
);
4532 cvd
->vdev_splitting
= B_TRUE
;
4534 vdev_propagate_state(cvd
);
4538 vdev_deadman(vdev_t
*vd
, char *tag
)
4540 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4541 vdev_t
*cvd
= vd
->vdev_child
[c
];
4543 vdev_deadman(cvd
, tag
);
4546 if (vd
->vdev_ops
->vdev_op_leaf
) {
4547 vdev_queue_t
*vq
= &vd
->vdev_queue
;
4549 mutex_enter(&vq
->vq_lock
);
4550 if (avl_numnodes(&vq
->vq_active_tree
) > 0) {
4551 spa_t
*spa
= vd
->vdev_spa
;
4555 zfs_dbgmsg("slow vdev: %s has %d active IOs",
4556 vd
->vdev_path
, avl_numnodes(&vq
->vq_active_tree
));
4559 * Look at the head of all the pending queues,
4560 * if any I/O has been outstanding for longer than
4561 * the spa_deadman_synctime invoke the deadman logic.
4563 fio
= avl_first(&vq
->vq_active_tree
);
4564 delta
= gethrtime() - fio
->io_timestamp
;
4565 if (delta
> spa_deadman_synctime(spa
))
4566 zio_deadman(fio
, tag
);
4568 mutex_exit(&vq
->vq_lock
);
4572 #if defined(_KERNEL)
4573 EXPORT_SYMBOL(vdev_fault
);
4574 EXPORT_SYMBOL(vdev_degrade
);
4575 EXPORT_SYMBOL(vdev_online
);
4576 EXPORT_SYMBOL(vdev_offline
);
4577 EXPORT_SYMBOL(vdev_clear
);
4579 module_param(vdev_max_ms_count
, int, 0644);
4580 MODULE_PARM_DESC(vdev_max_ms_count
,
4581 "Target number of metaslabs per top-level vdev");
4583 module_param(vdev_min_ms_count
, int, 0644);
4584 MODULE_PARM_DESC(vdev_min_ms_count
,
4585 "Minimum number of metaslabs per top-level vdev");
4587 module_param(vdev_ms_count_limit
, int, 0644);
4588 MODULE_PARM_DESC(vdev_ms_count_limit
,
4589 "Practical upper limit of total metaslabs per top-level vdev");
4591 module_param(zfs_delays_per_second
, uint
, 0644);
4592 MODULE_PARM_DESC(zfs_delays_per_second
, "Rate limit delay events to this many "
4593 "IO delays per second");
4595 module_param(zfs_checksums_per_second
, uint
, 0644);
4596 MODULE_PARM_DESC(zfs_checksums_per_second
, "Rate limit checksum events "
4597 "to this many checksum errors per second (do not set below zed"
4600 module_param(zfs_scan_ignore_errors
, int, 0644);
4601 MODULE_PARM_DESC(zfs_scan_ignore_errors
,
4602 "Ignore errors during resilver/scrub");
4604 module_param(vdev_validate_skip
, int, 0644);
4605 MODULE_PARM_DESC(vdev_validate_skip
,
4606 "Bypass vdev_validate()");