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>
53 #include <sys/vdev_initialize.h>
54 #include <sys/vdev_trim.h>
56 #include <sys/zfs_ratelimit.h>
58 /* default target for number of metaslabs per top-level vdev */
59 int zfs_vdev_default_ms_count = 200;
61 /* minimum number of metaslabs per top-level vdev */
62 int zfs_vdev_min_ms_count = 16;
64 /* practical upper limit of total metaslabs per top-level vdev */
65 int zfs_vdev_ms_count_limit = 1ULL << 17;
67 /* lower limit for metaslab size (512M) */
68 int zfs_vdev_default_ms_shift = 29;
70 /* upper limit for metaslab size (16G) */
71 int zfs_vdev_max_ms_shift = 34;
73 int vdev_validate_skip = B_FALSE;
76 * Since the DTL space map of a vdev is not expected to have a lot of
77 * entries, we default its block size to 4K.
79 int vdev_dtl_sm_blksz = (1 << 12);
82 * Rate limit slow IO (delay) events to this many per second.
84 unsigned int zfs_slow_io_events_per_second = 20;
87 * Rate limit checksum events after this many checksum errors per second.
89 unsigned int zfs_checksum_events_per_second = 20;
92 * Ignore errors during scrub/resilver. Allows to work around resilver
93 * upon import when there are pool errors.
95 int zfs_scan_ignore_errors = 0;
98 * vdev-wide space maps that have lots of entries written to them at
99 * the end of each transaction can benefit from a higher I/O bandwidth
100 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
102 int vdev_standard_sm_blksz = (1 << 17);
105 * Tunable parameter for debugging or performance analysis. Setting this
106 * will cause pool corruption on power loss if a volatile out-of-order
107 * write cache is enabled.
109 int zfs_nocacheflush = 0;
113 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
119 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
122 if (vd->vdev_path != NULL) {
123 zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
126 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
127 vd->vdev_ops->vdev_op_type,
128 (u_longlong_t)vd->vdev_id,
129 (u_longlong_t)vd->vdev_guid, buf);
134 vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
138 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
139 zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id,
140 vd->vdev_ops->vdev_op_type);
144 switch (vd->vdev_state) {
145 case VDEV_STATE_UNKNOWN:
146 (void) snprintf(state, sizeof (state), "unknown");
148 case VDEV_STATE_CLOSED:
149 (void) snprintf(state, sizeof (state), "closed");
151 case VDEV_STATE_OFFLINE:
152 (void) snprintf(state, sizeof (state), "offline");
154 case VDEV_STATE_REMOVED:
155 (void) snprintf(state, sizeof (state), "removed");
157 case VDEV_STATE_CANT_OPEN:
158 (void) snprintf(state, sizeof (state), "can't open");
160 case VDEV_STATE_FAULTED:
161 (void) snprintf(state, sizeof (state), "faulted");
163 case VDEV_STATE_DEGRADED:
164 (void) snprintf(state, sizeof (state), "degraded");
166 case VDEV_STATE_HEALTHY:
167 (void) snprintf(state, sizeof (state), "healthy");
170 (void) snprintf(state, sizeof (state), "<state %u>",
171 (uint_t)vd->vdev_state);
174 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
175 "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type,
176 vd->vdev_islog ? " (log)" : "",
177 (u_longlong_t)vd->vdev_guid,
178 vd->vdev_path ? vd->vdev_path : "N/A", state);
180 for (uint64_t i = 0; i < vd->vdev_children; i++)
181 vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
185 * Virtual device management.
188 static vdev_ops_t *vdev_ops_table[] = {
203 * Given a vdev type, return the appropriate ops vector.
206 vdev_getops(const char *type)
208 vdev_ops_t *ops, **opspp;
210 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
211 if (strcmp(ops->vdev_op_type, type) == 0)
219 vdev_default_xlate(vdev_t *vd, const range_seg_t *in, range_seg_t *res)
221 res->rs_start = in->rs_start;
222 res->rs_end = in->rs_end;
226 * Derive the enumerated alloction bias from string input.
227 * String origin is either the per-vdev zap or zpool(1M).
229 static vdev_alloc_bias_t
230 vdev_derive_alloc_bias(const char *bias)
232 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
234 if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0)
235 alloc_bias = VDEV_BIAS_LOG;
236 else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
237 alloc_bias = VDEV_BIAS_SPECIAL;
238 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
239 alloc_bias = VDEV_BIAS_DEDUP;
245 * Default asize function: return the MAX of psize with the asize of
246 * all children. This is what's used by anything other than RAID-Z.
249 vdev_default_asize(vdev_t *vd, uint64_t psize)
251 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
254 for (int c = 0; c < vd->vdev_children; c++) {
255 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
256 asize = MAX(asize, csize);
263 * Get the minimum allocatable size. We define the allocatable size as
264 * the vdev's asize rounded to the nearest metaslab. This allows us to
265 * replace or attach devices which don't have the same physical size but
266 * can still satisfy the same number of allocations.
269 vdev_get_min_asize(vdev_t *vd)
271 vdev_t *pvd = vd->vdev_parent;
274 * If our parent is NULL (inactive spare or cache) or is the root,
275 * just return our own asize.
278 return (vd->vdev_asize);
281 * The top-level vdev just returns the allocatable size rounded
282 * to the nearest metaslab.
284 if (vd == vd->vdev_top)
285 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
288 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
289 * so each child must provide at least 1/Nth of its asize.
291 if (pvd->vdev_ops == &vdev_raidz_ops)
292 return ((pvd->vdev_min_asize + pvd->vdev_children - 1) /
295 return (pvd->vdev_min_asize);
299 vdev_set_min_asize(vdev_t *vd)
301 vd->vdev_min_asize = vdev_get_min_asize(vd);
303 for (int c = 0; c < vd->vdev_children; c++)
304 vdev_set_min_asize(vd->vdev_child[c]);
308 vdev_lookup_top(spa_t *spa, uint64_t vdev)
310 vdev_t *rvd = spa->spa_root_vdev;
312 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
314 if (vdev < rvd->vdev_children) {
315 ASSERT(rvd->vdev_child[vdev] != NULL);
316 return (rvd->vdev_child[vdev]);
323 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
327 if (vd->vdev_guid == guid)
330 for (int c = 0; c < vd->vdev_children; c++)
331 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
339 vdev_count_leaves_impl(vdev_t *vd)
343 if (vd->vdev_ops->vdev_op_leaf)
346 for (int c = 0; c < vd->vdev_children; c++)
347 n += vdev_count_leaves_impl(vd->vdev_child[c]);
353 vdev_count_leaves(spa_t *spa)
357 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
358 rc = vdev_count_leaves_impl(spa->spa_root_vdev);
359 spa_config_exit(spa, SCL_VDEV, FTAG);
365 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
367 size_t oldsize, newsize;
368 uint64_t id = cvd->vdev_id;
371 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
372 ASSERT(cvd->vdev_parent == NULL);
374 cvd->vdev_parent = pvd;
379 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
381 oldsize = pvd->vdev_children * sizeof (vdev_t *);
382 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
383 newsize = pvd->vdev_children * sizeof (vdev_t *);
385 newchild = kmem_alloc(newsize, KM_SLEEP);
386 if (pvd->vdev_child != NULL) {
387 bcopy(pvd->vdev_child, newchild, oldsize);
388 kmem_free(pvd->vdev_child, oldsize);
391 pvd->vdev_child = newchild;
392 pvd->vdev_child[id] = cvd;
394 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
395 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
398 * Walk up all ancestors to update guid sum.
400 for (; pvd != NULL; pvd = pvd->vdev_parent)
401 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
403 if (cvd->vdev_ops->vdev_op_leaf) {
404 list_insert_head(&cvd->vdev_spa->spa_leaf_list, cvd);
405 cvd->vdev_spa->spa_leaf_list_gen++;
410 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
413 uint_t id = cvd->vdev_id;
415 ASSERT(cvd->vdev_parent == pvd);
420 ASSERT(id < pvd->vdev_children);
421 ASSERT(pvd->vdev_child[id] == cvd);
423 pvd->vdev_child[id] = NULL;
424 cvd->vdev_parent = NULL;
426 for (c = 0; c < pvd->vdev_children; c++)
427 if (pvd->vdev_child[c])
430 if (c == pvd->vdev_children) {
431 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
432 pvd->vdev_child = NULL;
433 pvd->vdev_children = 0;
436 if (cvd->vdev_ops->vdev_op_leaf) {
437 spa_t *spa = cvd->vdev_spa;
438 list_remove(&spa->spa_leaf_list, cvd);
439 spa->spa_leaf_list_gen++;
443 * Walk up all ancestors to update guid sum.
445 for (; pvd != NULL; pvd = pvd->vdev_parent)
446 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
450 * Remove any holes in the child array.
453 vdev_compact_children(vdev_t *pvd)
455 vdev_t **newchild, *cvd;
456 int oldc = pvd->vdev_children;
459 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
464 for (int c = newc = 0; c < oldc; c++)
465 if (pvd->vdev_child[c])
469 newchild = kmem_zalloc(newc * sizeof (vdev_t *), KM_SLEEP);
471 for (int c = newc = 0; c < oldc; c++) {
472 if ((cvd = pvd->vdev_child[c]) != NULL) {
473 newchild[newc] = cvd;
474 cvd->vdev_id = newc++;
481 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
482 pvd->vdev_child = newchild;
483 pvd->vdev_children = newc;
487 * Allocate and minimally initialize a vdev_t.
490 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
493 vdev_indirect_config_t *vic;
495 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
496 vic = &vd->vdev_indirect_config;
498 if (spa->spa_root_vdev == NULL) {
499 ASSERT(ops == &vdev_root_ops);
500 spa->spa_root_vdev = vd;
501 spa->spa_load_guid = spa_generate_guid(NULL);
504 if (guid == 0 && ops != &vdev_hole_ops) {
505 if (spa->spa_root_vdev == vd) {
507 * The root vdev's guid will also be the pool guid,
508 * which must be unique among all pools.
510 guid = spa_generate_guid(NULL);
513 * Any other vdev's guid must be unique within the pool.
515 guid = spa_generate_guid(spa);
517 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
522 vd->vdev_guid = guid;
523 vd->vdev_guid_sum = guid;
525 vd->vdev_state = VDEV_STATE_CLOSED;
526 vd->vdev_ishole = (ops == &vdev_hole_ops);
527 vic->vic_prev_indirect_vdev = UINT64_MAX;
529 rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
530 mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
531 vd->vdev_obsolete_segments = range_tree_create(NULL, NULL);
534 * Initialize rate limit structs for events. We rate limit ZIO delay
535 * and checksum events so that we don't overwhelm ZED with thousands
536 * of events when a disk is acting up.
538 zfs_ratelimit_init(&vd->vdev_delay_rl, &zfs_slow_io_events_per_second,
540 zfs_ratelimit_init(&vd->vdev_checksum_rl,
541 &zfs_checksum_events_per_second, 1);
543 list_link_init(&vd->vdev_config_dirty_node);
544 list_link_init(&vd->vdev_state_dirty_node);
545 list_link_init(&vd->vdev_initialize_node);
546 list_link_init(&vd->vdev_leaf_node);
547 list_link_init(&vd->vdev_trim_node);
548 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_NOLOCKDEP, NULL);
549 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
550 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
551 mutex_init(&vd->vdev_scan_io_queue_lock, NULL, MUTEX_DEFAULT, NULL);
552 mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL);
553 mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL);
554 cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL);
555 cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL);
556 mutex_init(&vd->vdev_trim_lock, NULL, MUTEX_DEFAULT, NULL);
557 mutex_init(&vd->vdev_autotrim_lock, NULL, MUTEX_DEFAULT, NULL);
558 mutex_init(&vd->vdev_trim_io_lock, NULL, MUTEX_DEFAULT, NULL);
559 cv_init(&vd->vdev_trim_cv, NULL, CV_DEFAULT, NULL);
560 cv_init(&vd->vdev_autotrim_cv, NULL, CV_DEFAULT, NULL);
561 cv_init(&vd->vdev_trim_io_cv, NULL, CV_DEFAULT, NULL);
563 for (int t = 0; t < DTL_TYPES; t++) {
564 vd->vdev_dtl[t] = range_tree_create(NULL, NULL);
566 txg_list_create(&vd->vdev_ms_list, spa,
567 offsetof(struct metaslab, ms_txg_node));
568 txg_list_create(&vd->vdev_dtl_list, spa,
569 offsetof(struct vdev, vdev_dtl_node));
570 vd->vdev_stat.vs_timestamp = gethrtime();
578 * Allocate a new vdev. The 'alloctype' is used to control whether we are
579 * creating a new vdev or loading an existing one - the behavior is slightly
580 * different for each case.
583 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
588 uint64_t guid = 0, islog, nparity;
590 vdev_indirect_config_t *vic;
593 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
594 boolean_t top_level = (parent && !parent->vdev_parent);
596 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
598 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
599 return (SET_ERROR(EINVAL));
601 if ((ops = vdev_getops(type)) == NULL)
602 return (SET_ERROR(EINVAL));
605 * If this is a load, get the vdev guid from the nvlist.
606 * Otherwise, vdev_alloc_common() will generate one for us.
608 if (alloctype == VDEV_ALLOC_LOAD) {
611 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
613 return (SET_ERROR(EINVAL));
615 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
616 return (SET_ERROR(EINVAL));
617 } else if (alloctype == VDEV_ALLOC_SPARE) {
618 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
619 return (SET_ERROR(EINVAL));
620 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
621 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
622 return (SET_ERROR(EINVAL));
623 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
624 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
625 return (SET_ERROR(EINVAL));
629 * The first allocated vdev must be of type 'root'.
631 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
632 return (SET_ERROR(EINVAL));
635 * Determine whether we're a log vdev.
638 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
639 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
640 return (SET_ERROR(ENOTSUP));
642 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
643 return (SET_ERROR(ENOTSUP));
646 * Set the nparity property for RAID-Z vdevs.
649 if (ops == &vdev_raidz_ops) {
650 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
652 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
653 return (SET_ERROR(EINVAL));
655 * Previous versions could only support 1 or 2 parity
659 spa_version(spa) < SPA_VERSION_RAIDZ2)
660 return (SET_ERROR(ENOTSUP));
662 spa_version(spa) < SPA_VERSION_RAIDZ3)
663 return (SET_ERROR(ENOTSUP));
666 * We require the parity to be specified for SPAs that
667 * support multiple parity levels.
669 if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
670 return (SET_ERROR(EINVAL));
672 * Otherwise, we default to 1 parity device for RAID-Z.
679 ASSERT(nparity != -1ULL);
682 * If creating a top-level vdev, check for allocation classes input
684 if (top_level && alloctype == VDEV_ALLOC_ADD) {
687 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
689 alloc_bias = vdev_derive_alloc_bias(bias);
691 /* spa_vdev_add() expects feature to be enabled */
692 if (spa->spa_load_state != SPA_LOAD_CREATE &&
693 !spa_feature_is_enabled(spa,
694 SPA_FEATURE_ALLOCATION_CLASSES)) {
695 return (SET_ERROR(ENOTSUP));
700 vd = vdev_alloc_common(spa, id, guid, ops);
701 vic = &vd->vdev_indirect_config;
703 vd->vdev_islog = islog;
704 vd->vdev_nparity = nparity;
705 if (top_level && alloc_bias != VDEV_BIAS_NONE)
706 vd->vdev_alloc_bias = alloc_bias;
708 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
709 vd->vdev_path = spa_strdup(vd->vdev_path);
712 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
713 * fault on a vdev and want it to persist across imports (like with
716 rc = nvlist_lookup_string(nv, ZPOOL_CONFIG_AUX_STATE, &tmp);
717 if (rc == 0 && tmp != NULL && strcmp(tmp, "external") == 0) {
718 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL;
719 vd->vdev_faulted = 1;
720 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
723 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
724 vd->vdev_devid = spa_strdup(vd->vdev_devid);
725 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
726 &vd->vdev_physpath) == 0)
727 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
729 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
730 &vd->vdev_enc_sysfs_path) == 0)
731 vd->vdev_enc_sysfs_path = spa_strdup(vd->vdev_enc_sysfs_path);
733 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
734 vd->vdev_fru = spa_strdup(vd->vdev_fru);
737 * Set the whole_disk property. If it's not specified, leave the value
740 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
741 &vd->vdev_wholedisk) != 0)
742 vd->vdev_wholedisk = -1ULL;
744 ASSERT0(vic->vic_mapping_object);
745 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
746 &vic->vic_mapping_object);
747 ASSERT0(vic->vic_births_object);
748 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
749 &vic->vic_births_object);
750 ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
751 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
752 &vic->vic_prev_indirect_vdev);
755 * Look for the 'not present' flag. This will only be set if the device
756 * was not present at the time of import.
758 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
759 &vd->vdev_not_present);
762 * Get the alignment requirement.
764 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
767 * Retrieve the vdev creation time.
769 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
773 * If we're a top-level vdev, try to load the allocation parameters.
776 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
777 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
779 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
781 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
783 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
785 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
788 ASSERT0(vd->vdev_top_zap);
791 if (top_level && alloctype != VDEV_ALLOC_ATTACH) {
792 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
793 alloctype == VDEV_ALLOC_ADD ||
794 alloctype == VDEV_ALLOC_SPLIT ||
795 alloctype == VDEV_ALLOC_ROOTPOOL);
796 /* Note: metaslab_group_create() is now deferred */
799 if (vd->vdev_ops->vdev_op_leaf &&
800 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
801 (void) nvlist_lookup_uint64(nv,
802 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
804 ASSERT0(vd->vdev_leaf_zap);
808 * If we're a leaf vdev, try to load the DTL object and other state.
811 if (vd->vdev_ops->vdev_op_leaf &&
812 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
813 alloctype == VDEV_ALLOC_ROOTPOOL)) {
814 if (alloctype == VDEV_ALLOC_LOAD) {
815 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
816 &vd->vdev_dtl_object);
817 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
821 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
824 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
825 &spare) == 0 && spare)
829 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
832 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
833 &vd->vdev_resilver_txg);
835 if (nvlist_exists(nv, ZPOOL_CONFIG_RESILVER_DEFER))
836 vdev_set_deferred_resilver(spa, vd);
839 * In general, when importing a pool we want to ignore the
840 * persistent fault state, as the diagnosis made on another
841 * system may not be valid in the current context. The only
842 * exception is if we forced a vdev to a persistently faulted
843 * state with 'zpool offline -f'. The persistent fault will
844 * remain across imports until cleared.
846 * Local vdevs will remain in the faulted state.
848 if (spa_load_state(spa) == SPA_LOAD_OPEN ||
849 spa_load_state(spa) == SPA_LOAD_IMPORT) {
850 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
852 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
854 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
857 if (vd->vdev_faulted || vd->vdev_degraded) {
861 VDEV_AUX_ERR_EXCEEDED;
862 if (nvlist_lookup_string(nv,
863 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
864 strcmp(aux, "external") == 0)
865 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
867 vd->vdev_faulted = 0ULL;
873 * Add ourselves to the parent's list of children.
875 vdev_add_child(parent, vd);
883 vdev_free(vdev_t *vd)
885 spa_t *spa = vd->vdev_spa;
887 ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
888 ASSERT3P(vd->vdev_trim_thread, ==, NULL);
889 ASSERT3P(vd->vdev_autotrim_thread, ==, NULL);
892 * Scan queues are normally destroyed at the end of a scan. If the
893 * queue exists here, that implies the vdev is being removed while
894 * the scan is still running.
896 if (vd->vdev_scan_io_queue != NULL) {
897 mutex_enter(&vd->vdev_scan_io_queue_lock);
898 dsl_scan_io_queue_destroy(vd->vdev_scan_io_queue);
899 vd->vdev_scan_io_queue = NULL;
900 mutex_exit(&vd->vdev_scan_io_queue_lock);
904 * vdev_free() implies closing the vdev first. This is simpler than
905 * trying to ensure complicated semantics for all callers.
909 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
910 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
915 for (int c = 0; c < vd->vdev_children; c++)
916 vdev_free(vd->vdev_child[c]);
918 ASSERT(vd->vdev_child == NULL);
919 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
922 * Discard allocation state.
924 if (vd->vdev_mg != NULL) {
925 vdev_metaslab_fini(vd);
926 metaslab_group_destroy(vd->vdev_mg);
929 ASSERT0(vd->vdev_stat.vs_space);
930 ASSERT0(vd->vdev_stat.vs_dspace);
931 ASSERT0(vd->vdev_stat.vs_alloc);
934 * Remove this vdev from its parent's child list.
936 vdev_remove_child(vd->vdev_parent, vd);
938 ASSERT(vd->vdev_parent == NULL);
939 ASSERT(!list_link_active(&vd->vdev_leaf_node));
942 * Clean up vdev structure.
948 spa_strfree(vd->vdev_path);
950 spa_strfree(vd->vdev_devid);
951 if (vd->vdev_physpath)
952 spa_strfree(vd->vdev_physpath);
954 if (vd->vdev_enc_sysfs_path)
955 spa_strfree(vd->vdev_enc_sysfs_path);
958 spa_strfree(vd->vdev_fru);
960 if (vd->vdev_isspare)
961 spa_spare_remove(vd);
962 if (vd->vdev_isl2cache)
963 spa_l2cache_remove(vd);
965 txg_list_destroy(&vd->vdev_ms_list);
966 txg_list_destroy(&vd->vdev_dtl_list);
968 mutex_enter(&vd->vdev_dtl_lock);
969 space_map_close(vd->vdev_dtl_sm);
970 for (int t = 0; t < DTL_TYPES; t++) {
971 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
972 range_tree_destroy(vd->vdev_dtl[t]);
974 mutex_exit(&vd->vdev_dtl_lock);
976 EQUIV(vd->vdev_indirect_births != NULL,
977 vd->vdev_indirect_mapping != NULL);
978 if (vd->vdev_indirect_births != NULL) {
979 vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
980 vdev_indirect_births_close(vd->vdev_indirect_births);
983 if (vd->vdev_obsolete_sm != NULL) {
984 ASSERT(vd->vdev_removing ||
985 vd->vdev_ops == &vdev_indirect_ops);
986 space_map_close(vd->vdev_obsolete_sm);
987 vd->vdev_obsolete_sm = NULL;
989 range_tree_destroy(vd->vdev_obsolete_segments);
990 rw_destroy(&vd->vdev_indirect_rwlock);
991 mutex_destroy(&vd->vdev_obsolete_lock);
993 mutex_destroy(&vd->vdev_dtl_lock);
994 mutex_destroy(&vd->vdev_stat_lock);
995 mutex_destroy(&vd->vdev_probe_lock);
996 mutex_destroy(&vd->vdev_scan_io_queue_lock);
997 mutex_destroy(&vd->vdev_initialize_lock);
998 mutex_destroy(&vd->vdev_initialize_io_lock);
999 cv_destroy(&vd->vdev_initialize_io_cv);
1000 cv_destroy(&vd->vdev_initialize_cv);
1001 mutex_destroy(&vd->vdev_trim_lock);
1002 mutex_destroy(&vd->vdev_autotrim_lock);
1003 mutex_destroy(&vd->vdev_trim_io_lock);
1004 cv_destroy(&vd->vdev_trim_cv);
1005 cv_destroy(&vd->vdev_autotrim_cv);
1006 cv_destroy(&vd->vdev_trim_io_cv);
1008 zfs_ratelimit_fini(&vd->vdev_delay_rl);
1009 zfs_ratelimit_fini(&vd->vdev_checksum_rl);
1011 if (vd == spa->spa_root_vdev)
1012 spa->spa_root_vdev = NULL;
1014 kmem_free(vd, sizeof (vdev_t));
1018 * Transfer top-level vdev state from svd to tvd.
1021 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
1023 spa_t *spa = svd->vdev_spa;
1028 ASSERT(tvd == tvd->vdev_top);
1030 tvd->vdev_pending_fastwrite = svd->vdev_pending_fastwrite;
1031 tvd->vdev_ms_array = svd->vdev_ms_array;
1032 tvd->vdev_ms_shift = svd->vdev_ms_shift;
1033 tvd->vdev_ms_count = svd->vdev_ms_count;
1034 tvd->vdev_top_zap = svd->vdev_top_zap;
1036 svd->vdev_ms_array = 0;
1037 svd->vdev_ms_shift = 0;
1038 svd->vdev_ms_count = 0;
1039 svd->vdev_top_zap = 0;
1042 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
1043 tvd->vdev_mg = svd->vdev_mg;
1044 tvd->vdev_ms = svd->vdev_ms;
1046 svd->vdev_mg = NULL;
1047 svd->vdev_ms = NULL;
1049 if (tvd->vdev_mg != NULL)
1050 tvd->vdev_mg->mg_vd = tvd;
1052 tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm;
1053 svd->vdev_checkpoint_sm = NULL;
1055 tvd->vdev_alloc_bias = svd->vdev_alloc_bias;
1056 svd->vdev_alloc_bias = VDEV_BIAS_NONE;
1058 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
1059 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
1060 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
1062 svd->vdev_stat.vs_alloc = 0;
1063 svd->vdev_stat.vs_space = 0;
1064 svd->vdev_stat.vs_dspace = 0;
1067 * State which may be set on a top-level vdev that's in the
1068 * process of being removed.
1070 ASSERT0(tvd->vdev_indirect_config.vic_births_object);
1071 ASSERT0(tvd->vdev_indirect_config.vic_mapping_object);
1072 ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL);
1073 ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL);
1074 ASSERT3P(tvd->vdev_indirect_births, ==, NULL);
1075 ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL);
1076 ASSERT0(tvd->vdev_removing);
1077 tvd->vdev_removing = svd->vdev_removing;
1078 tvd->vdev_indirect_config = svd->vdev_indirect_config;
1079 tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping;
1080 tvd->vdev_indirect_births = svd->vdev_indirect_births;
1081 range_tree_swap(&svd->vdev_obsolete_segments,
1082 &tvd->vdev_obsolete_segments);
1083 tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm;
1084 svd->vdev_indirect_config.vic_mapping_object = 0;
1085 svd->vdev_indirect_config.vic_births_object = 0;
1086 svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL;
1087 svd->vdev_indirect_mapping = NULL;
1088 svd->vdev_indirect_births = NULL;
1089 svd->vdev_obsolete_sm = NULL;
1090 svd->vdev_removing = 0;
1092 for (t = 0; t < TXG_SIZE; t++) {
1093 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
1094 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
1095 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
1096 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
1097 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
1098 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
1101 if (list_link_active(&svd->vdev_config_dirty_node)) {
1102 vdev_config_clean(svd);
1103 vdev_config_dirty(tvd);
1106 if (list_link_active(&svd->vdev_state_dirty_node)) {
1107 vdev_state_clean(svd);
1108 vdev_state_dirty(tvd);
1111 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
1112 svd->vdev_deflate_ratio = 0;
1114 tvd->vdev_islog = svd->vdev_islog;
1115 svd->vdev_islog = 0;
1117 dsl_scan_io_queue_vdev_xfer(svd, tvd);
1121 vdev_top_update(vdev_t *tvd, vdev_t *vd)
1128 for (int c = 0; c < vd->vdev_children; c++)
1129 vdev_top_update(tvd, vd->vdev_child[c]);
1133 * Add a mirror/replacing vdev above an existing vdev.
1136 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
1138 spa_t *spa = cvd->vdev_spa;
1139 vdev_t *pvd = cvd->vdev_parent;
1142 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1144 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
1146 mvd->vdev_asize = cvd->vdev_asize;
1147 mvd->vdev_min_asize = cvd->vdev_min_asize;
1148 mvd->vdev_max_asize = cvd->vdev_max_asize;
1149 mvd->vdev_psize = cvd->vdev_psize;
1150 mvd->vdev_ashift = cvd->vdev_ashift;
1151 mvd->vdev_state = cvd->vdev_state;
1152 mvd->vdev_crtxg = cvd->vdev_crtxg;
1154 vdev_remove_child(pvd, cvd);
1155 vdev_add_child(pvd, mvd);
1156 cvd->vdev_id = mvd->vdev_children;
1157 vdev_add_child(mvd, cvd);
1158 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1160 if (mvd == mvd->vdev_top)
1161 vdev_top_transfer(cvd, mvd);
1167 * Remove a 1-way mirror/replacing vdev from the tree.
1170 vdev_remove_parent(vdev_t *cvd)
1172 vdev_t *mvd = cvd->vdev_parent;
1173 vdev_t *pvd = mvd->vdev_parent;
1175 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1177 ASSERT(mvd->vdev_children == 1);
1178 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
1179 mvd->vdev_ops == &vdev_replacing_ops ||
1180 mvd->vdev_ops == &vdev_spare_ops);
1181 cvd->vdev_ashift = mvd->vdev_ashift;
1183 vdev_remove_child(mvd, cvd);
1184 vdev_remove_child(pvd, mvd);
1187 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1188 * Otherwise, we could have detached an offline device, and when we
1189 * go to import the pool we'll think we have two top-level vdevs,
1190 * instead of a different version of the same top-level vdev.
1192 if (mvd->vdev_top == mvd) {
1193 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
1194 cvd->vdev_orig_guid = cvd->vdev_guid;
1195 cvd->vdev_guid += guid_delta;
1196 cvd->vdev_guid_sum += guid_delta;
1199 * If pool not set for autoexpand, we need to also preserve
1200 * mvd's asize to prevent automatic expansion of cvd.
1201 * Otherwise if we are adjusting the mirror by attaching and
1202 * detaching children of non-uniform sizes, the mirror could
1203 * autoexpand, unexpectedly requiring larger devices to
1204 * re-establish the mirror.
1206 if (!cvd->vdev_spa->spa_autoexpand)
1207 cvd->vdev_asize = mvd->vdev_asize;
1209 cvd->vdev_id = mvd->vdev_id;
1210 vdev_add_child(pvd, cvd);
1211 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1213 if (cvd == cvd->vdev_top)
1214 vdev_top_transfer(mvd, cvd);
1216 ASSERT(mvd->vdev_children == 0);
1221 vdev_metaslab_group_create(vdev_t *vd)
1223 spa_t *spa = vd->vdev_spa;
1226 * metaslab_group_create was delayed until allocation bias was available
1228 if (vd->vdev_mg == NULL) {
1229 metaslab_class_t *mc;
1231 if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE)
1232 vd->vdev_alloc_bias = VDEV_BIAS_LOG;
1234 ASSERT3U(vd->vdev_islog, ==,
1235 (vd->vdev_alloc_bias == VDEV_BIAS_LOG));
1237 switch (vd->vdev_alloc_bias) {
1239 mc = spa_log_class(spa);
1241 case VDEV_BIAS_SPECIAL:
1242 mc = spa_special_class(spa);
1244 case VDEV_BIAS_DEDUP:
1245 mc = spa_dedup_class(spa);
1248 mc = spa_normal_class(spa);
1251 vd->vdev_mg = metaslab_group_create(mc, vd,
1252 spa->spa_alloc_count);
1255 * The spa ashift values currently only reflect the
1256 * general vdev classes. Class destination is late
1257 * binding so ashift checking had to wait until now
1259 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1260 mc == spa_normal_class(spa) && vd->vdev_aux == NULL) {
1261 if (vd->vdev_ashift > spa->spa_max_ashift)
1262 spa->spa_max_ashift = vd->vdev_ashift;
1263 if (vd->vdev_ashift < spa->spa_min_ashift)
1264 spa->spa_min_ashift = vd->vdev_ashift;
1270 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
1272 spa_t *spa = vd->vdev_spa;
1273 objset_t *mos = spa->spa_meta_objset;
1275 uint64_t oldc = vd->vdev_ms_count;
1276 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
1279 boolean_t expanding = (oldc != 0);
1281 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1284 * This vdev is not being allocated from yet or is a hole.
1286 if (vd->vdev_ms_shift == 0)
1289 ASSERT(!vd->vdev_ishole);
1291 ASSERT(oldc <= newc);
1293 mspp = vmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1296 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
1297 vmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1301 vd->vdev_ms_count = newc;
1302 for (m = oldc; m < newc; m++) {
1303 uint64_t object = 0;
1306 * vdev_ms_array may be 0 if we are creating the "fake"
1307 * metaslabs for an indirect vdev for zdb's leak detection.
1308 * See zdb_leak_init().
1310 if (txg == 0 && vd->vdev_ms_array != 0) {
1311 error = dmu_read(mos, vd->vdev_ms_array,
1312 m * sizeof (uint64_t), sizeof (uint64_t), &object,
1315 vdev_dbgmsg(vd, "unable to read the metaslab "
1316 "array [error=%d]", error);
1323 * To accomodate zdb_leak_init() fake indirect
1324 * metaslabs, we allocate a metaslab group for
1325 * indirect vdevs which normally don't have one.
1327 if (vd->vdev_mg == NULL) {
1328 ASSERT0(vdev_is_concrete(vd));
1329 vdev_metaslab_group_create(vd);
1332 error = metaslab_init(vd->vdev_mg, m, object, txg,
1335 vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
1342 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1345 * If the vdev is being removed we don't activate
1346 * the metaslabs since we want to ensure that no new
1347 * allocations are performed on this device.
1349 if (!expanding && !vd->vdev_removing) {
1350 metaslab_group_activate(vd->vdev_mg);
1354 spa_config_exit(spa, SCL_ALLOC, FTAG);
1360 vdev_metaslab_fini(vdev_t *vd)
1362 if (vd->vdev_checkpoint_sm != NULL) {
1363 ASSERT(spa_feature_is_active(vd->vdev_spa,
1364 SPA_FEATURE_POOL_CHECKPOINT));
1365 space_map_close(vd->vdev_checkpoint_sm);
1367 * Even though we close the space map, we need to set its
1368 * pointer to NULL. The reason is that vdev_metaslab_fini()
1369 * may be called multiple times for certain operations
1370 * (i.e. when destroying a pool) so we need to ensure that
1371 * this clause never executes twice. This logic is similar
1372 * to the one used for the vdev_ms clause below.
1374 vd->vdev_checkpoint_sm = NULL;
1377 if (vd->vdev_ms != NULL) {
1378 metaslab_group_t *mg = vd->vdev_mg;
1379 metaslab_group_passivate(mg);
1381 uint64_t count = vd->vdev_ms_count;
1382 for (uint64_t m = 0; m < count; m++) {
1383 metaslab_t *msp = vd->vdev_ms[m];
1387 vmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1390 vd->vdev_ms_count = 0;
1392 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
1393 ASSERT0(mg->mg_histogram[i]);
1395 ASSERT0(vd->vdev_ms_count);
1396 ASSERT3U(vd->vdev_pending_fastwrite, ==, 0);
1399 typedef struct vdev_probe_stats {
1400 boolean_t vps_readable;
1401 boolean_t vps_writeable;
1403 } vdev_probe_stats_t;
1406 vdev_probe_done(zio_t *zio)
1408 spa_t *spa = zio->io_spa;
1409 vdev_t *vd = zio->io_vd;
1410 vdev_probe_stats_t *vps = zio->io_private;
1412 ASSERT(vd->vdev_probe_zio != NULL);
1414 if (zio->io_type == ZIO_TYPE_READ) {
1415 if (zio->io_error == 0)
1416 vps->vps_readable = 1;
1417 if (zio->io_error == 0 && spa_writeable(spa)) {
1418 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1419 zio->io_offset, zio->io_size, zio->io_abd,
1420 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1421 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1423 abd_free(zio->io_abd);
1425 } else if (zio->io_type == ZIO_TYPE_WRITE) {
1426 if (zio->io_error == 0)
1427 vps->vps_writeable = 1;
1428 abd_free(zio->io_abd);
1429 } else if (zio->io_type == ZIO_TYPE_NULL) {
1433 vd->vdev_cant_read |= !vps->vps_readable;
1434 vd->vdev_cant_write |= !vps->vps_writeable;
1436 if (vdev_readable(vd) &&
1437 (vdev_writeable(vd) || !spa_writeable(spa))) {
1440 ASSERT(zio->io_error != 0);
1441 vdev_dbgmsg(vd, "failed probe");
1442 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
1443 spa, vd, NULL, NULL, 0, 0);
1444 zio->io_error = SET_ERROR(ENXIO);
1447 mutex_enter(&vd->vdev_probe_lock);
1448 ASSERT(vd->vdev_probe_zio == zio);
1449 vd->vdev_probe_zio = NULL;
1450 mutex_exit(&vd->vdev_probe_lock);
1453 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1454 if (!vdev_accessible(vd, pio))
1455 pio->io_error = SET_ERROR(ENXIO);
1457 kmem_free(vps, sizeof (*vps));
1462 * Determine whether this device is accessible.
1464 * Read and write to several known locations: the pad regions of each
1465 * vdev label but the first, which we leave alone in case it contains
1469 vdev_probe(vdev_t *vd, zio_t *zio)
1471 spa_t *spa = vd->vdev_spa;
1472 vdev_probe_stats_t *vps = NULL;
1475 ASSERT(vd->vdev_ops->vdev_op_leaf);
1478 * Don't probe the probe.
1480 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1484 * To prevent 'probe storms' when a device fails, we create
1485 * just one probe i/o at a time. All zios that want to probe
1486 * this vdev will become parents of the probe io.
1488 mutex_enter(&vd->vdev_probe_lock);
1490 if ((pio = vd->vdev_probe_zio) == NULL) {
1491 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1493 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1494 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
1497 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1499 * vdev_cant_read and vdev_cant_write can only
1500 * transition from TRUE to FALSE when we have the
1501 * SCL_ZIO lock as writer; otherwise they can only
1502 * transition from FALSE to TRUE. This ensures that
1503 * any zio looking at these values can assume that
1504 * failures persist for the life of the I/O. That's
1505 * important because when a device has intermittent
1506 * connectivity problems, we want to ensure that
1507 * they're ascribed to the device (ENXIO) and not
1510 * Since we hold SCL_ZIO as writer here, clear both
1511 * values so the probe can reevaluate from first
1514 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1515 vd->vdev_cant_read = B_FALSE;
1516 vd->vdev_cant_write = B_FALSE;
1519 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1520 vdev_probe_done, vps,
1521 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1524 * We can't change the vdev state in this context, so we
1525 * kick off an async task to do it on our behalf.
1528 vd->vdev_probe_wanted = B_TRUE;
1529 spa_async_request(spa, SPA_ASYNC_PROBE);
1534 zio_add_child(zio, pio);
1536 mutex_exit(&vd->vdev_probe_lock);
1539 ASSERT(zio != NULL);
1543 for (int l = 1; l < VDEV_LABELS; l++) {
1544 zio_nowait(zio_read_phys(pio, vd,
1545 vdev_label_offset(vd->vdev_psize, l,
1546 offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE,
1547 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1548 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1549 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1560 vdev_open_child(void *arg)
1564 vd->vdev_open_thread = curthread;
1565 vd->vdev_open_error = vdev_open(vd);
1566 vd->vdev_open_thread = NULL;
1570 vdev_uses_zvols(vdev_t *vd)
1573 if (zvol_is_zvol(vd->vdev_path))
1577 for (int c = 0; c < vd->vdev_children; c++)
1578 if (vdev_uses_zvols(vd->vdev_child[c]))
1585 vdev_open_children(vdev_t *vd)
1588 int children = vd->vdev_children;
1591 * in order to handle pools on top of zvols, do the opens
1592 * in a single thread so that the same thread holds the
1593 * spa_namespace_lock
1595 if (vdev_uses_zvols(vd)) {
1597 for (int c = 0; c < children; c++)
1598 vd->vdev_child[c]->vdev_open_error =
1599 vdev_open(vd->vdev_child[c]);
1601 tq = taskq_create("vdev_open", children, minclsyspri,
1602 children, children, TASKQ_PREPOPULATE);
1606 for (int c = 0; c < children; c++)
1607 VERIFY(taskq_dispatch(tq, vdev_open_child,
1608 vd->vdev_child[c], TQ_SLEEP) != TASKQID_INVALID);
1613 vd->vdev_nonrot = B_TRUE;
1615 for (int c = 0; c < children; c++)
1616 vd->vdev_nonrot &= vd->vdev_child[c]->vdev_nonrot;
1620 * Compute the raidz-deflation ratio. Note, we hard-code
1621 * in 128k (1 << 17) because it is the "typical" blocksize.
1622 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1623 * otherwise it would inconsistently account for existing bp's.
1626 vdev_set_deflate_ratio(vdev_t *vd)
1628 if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
1629 vd->vdev_deflate_ratio = (1 << 17) /
1630 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
1635 * Prepare a virtual device for access.
1638 vdev_open(vdev_t *vd)
1640 spa_t *spa = vd->vdev_spa;
1643 uint64_t max_osize = 0;
1644 uint64_t asize, max_asize, psize;
1645 uint64_t ashift = 0;
1647 ASSERT(vd->vdev_open_thread == curthread ||
1648 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1649 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1650 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1651 vd->vdev_state == VDEV_STATE_OFFLINE);
1653 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1654 vd->vdev_cant_read = B_FALSE;
1655 vd->vdev_cant_write = B_FALSE;
1656 vd->vdev_min_asize = vdev_get_min_asize(vd);
1659 * If this vdev is not removed, check its fault status. If it's
1660 * faulted, bail out of the open.
1662 if (!vd->vdev_removed && vd->vdev_faulted) {
1663 ASSERT(vd->vdev_children == 0);
1664 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1665 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1666 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1667 vd->vdev_label_aux);
1668 return (SET_ERROR(ENXIO));
1669 } else if (vd->vdev_offline) {
1670 ASSERT(vd->vdev_children == 0);
1671 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1672 return (SET_ERROR(ENXIO));
1675 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
1678 * Physical volume size should never be larger than its max size, unless
1679 * the disk has shrunk while we were reading it or the device is buggy
1680 * or damaged: either way it's not safe for use, bail out of the open.
1682 if (osize > max_osize) {
1683 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1684 VDEV_AUX_OPEN_FAILED);
1685 return (SET_ERROR(ENXIO));
1689 * Reset the vdev_reopening flag so that we actually close
1690 * the vdev on error.
1692 vd->vdev_reopening = B_FALSE;
1693 if (zio_injection_enabled && error == 0)
1694 error = zio_handle_device_injection(vd, NULL, ENXIO);
1697 if (vd->vdev_removed &&
1698 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
1699 vd->vdev_removed = B_FALSE;
1701 if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
1702 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
1703 vd->vdev_stat.vs_aux);
1705 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1706 vd->vdev_stat.vs_aux);
1711 vd->vdev_removed = B_FALSE;
1714 * Recheck the faulted flag now that we have confirmed that
1715 * the vdev is accessible. If we're faulted, bail.
1717 if (vd->vdev_faulted) {
1718 ASSERT(vd->vdev_children == 0);
1719 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1720 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1721 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1722 vd->vdev_label_aux);
1723 return (SET_ERROR(ENXIO));
1726 if (vd->vdev_degraded) {
1727 ASSERT(vd->vdev_children == 0);
1728 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1729 VDEV_AUX_ERR_EXCEEDED);
1731 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1735 * For hole or missing vdevs we just return success.
1737 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
1740 for (int c = 0; c < vd->vdev_children; c++) {
1741 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1742 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1748 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1749 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1751 if (vd->vdev_children == 0) {
1752 if (osize < SPA_MINDEVSIZE) {
1753 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1754 VDEV_AUX_TOO_SMALL);
1755 return (SET_ERROR(EOVERFLOW));
1758 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1759 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
1760 VDEV_LABEL_END_SIZE);
1762 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1763 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1764 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1765 VDEV_AUX_TOO_SMALL);
1766 return (SET_ERROR(EOVERFLOW));
1770 max_asize = max_osize;
1774 * If the vdev was expanded, record this so that we can re-create the
1775 * uberblock rings in labels {2,3}, during the next sync.
1777 if ((psize > vd->vdev_psize) && (vd->vdev_psize != 0))
1778 vd->vdev_copy_uberblocks = B_TRUE;
1780 vd->vdev_psize = psize;
1783 * Make sure the allocatable size hasn't shrunk too much.
1785 if (asize < vd->vdev_min_asize) {
1786 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1787 VDEV_AUX_BAD_LABEL);
1788 return (SET_ERROR(EINVAL));
1791 if (vd->vdev_asize == 0) {
1793 * This is the first-ever open, so use the computed values.
1794 * For compatibility, a different ashift can be requested.
1796 vd->vdev_asize = asize;
1797 vd->vdev_max_asize = max_asize;
1798 if (vd->vdev_ashift == 0) {
1799 vd->vdev_ashift = ashift; /* use detected value */
1801 if (vd->vdev_ashift != 0 && (vd->vdev_ashift < ASHIFT_MIN ||
1802 vd->vdev_ashift > ASHIFT_MAX)) {
1803 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1804 VDEV_AUX_BAD_ASHIFT);
1805 return (SET_ERROR(EDOM));
1809 * Detect if the alignment requirement has increased.
1810 * We don't want to make the pool unavailable, just
1811 * post an event instead.
1813 if (ashift > vd->vdev_top->vdev_ashift &&
1814 vd->vdev_ops->vdev_op_leaf) {
1815 zfs_ereport_post(FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT,
1816 spa, vd, NULL, NULL, 0, 0);
1819 vd->vdev_max_asize = max_asize;
1823 * If all children are healthy we update asize if either:
1824 * The asize has increased, due to a device expansion caused by dynamic
1825 * LUN growth or vdev replacement, and automatic expansion is enabled;
1826 * making the additional space available.
1828 * The asize has decreased, due to a device shrink usually caused by a
1829 * vdev replace with a smaller device. This ensures that calculations
1830 * based of max_asize and asize e.g. esize are always valid. It's safe
1831 * to do this as we've already validated that asize is greater than
1834 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1835 ((asize > vd->vdev_asize &&
1836 (vd->vdev_expanding || spa->spa_autoexpand)) ||
1837 (asize < vd->vdev_asize)))
1838 vd->vdev_asize = asize;
1840 vdev_set_min_asize(vd);
1843 * Ensure we can issue some IO before declaring the
1844 * vdev open for business.
1846 if (vd->vdev_ops->vdev_op_leaf &&
1847 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1848 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1849 VDEV_AUX_ERR_EXCEEDED);
1854 * Track the min and max ashift values for normal data devices.
1856 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1857 vd->vdev_alloc_bias == VDEV_BIAS_NONE &&
1858 vd->vdev_islog == 0 && vd->vdev_aux == NULL) {
1859 if (vd->vdev_ashift > spa->spa_max_ashift)
1860 spa->spa_max_ashift = vd->vdev_ashift;
1861 if (vd->vdev_ashift < spa->spa_min_ashift)
1862 spa->spa_min_ashift = vd->vdev_ashift;
1866 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1867 * resilver. But don't do this if we are doing a reopen for a scrub,
1868 * since this would just restart the scrub we are already doing.
1870 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
1871 vdev_resilver_needed(vd, NULL, NULL)) {
1872 if (dsl_scan_resilvering(spa->spa_dsl_pool) &&
1873 spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER))
1874 vdev_set_deferred_resilver(spa, vd);
1876 spa_async_request(spa, SPA_ASYNC_RESILVER);
1883 * Called once the vdevs are all opened, this routine validates the label
1884 * contents. This needs to be done before vdev_load() so that we don't
1885 * inadvertently do repair I/Os to the wrong device.
1887 * This function will only return failure if one of the vdevs indicates that it
1888 * has since been destroyed or exported. This is only possible if
1889 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1890 * will be updated but the function will return 0.
1893 vdev_validate(vdev_t *vd)
1895 spa_t *spa = vd->vdev_spa;
1897 uint64_t guid = 0, aux_guid = 0, top_guid;
1902 if (vdev_validate_skip)
1905 for (uint64_t c = 0; c < vd->vdev_children; c++)
1906 if (vdev_validate(vd->vdev_child[c]) != 0)
1907 return (SET_ERROR(EBADF));
1910 * If the device has already failed, or was marked offline, don't do
1911 * any further validation. Otherwise, label I/O will fail and we will
1912 * overwrite the previous state.
1914 if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
1918 * If we are performing an extreme rewind, we allow for a label that
1919 * was modified at a point after the current txg.
1920 * If config lock is not held do not check for the txg. spa_sync could
1921 * be updating the vdev's label before updating spa_last_synced_txg.
1923 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 ||
1924 spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG)
1927 txg = spa_last_synced_txg(spa);
1929 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1930 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1931 VDEV_AUX_BAD_LABEL);
1932 vdev_dbgmsg(vd, "vdev_validate: failed reading config for "
1933 "txg %llu", (u_longlong_t)txg);
1938 * Determine if this vdev has been split off into another
1939 * pool. If so, then refuse to open it.
1941 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
1942 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
1943 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1944 VDEV_AUX_SPLIT_POOL);
1946 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
1950 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
1951 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1952 VDEV_AUX_CORRUPT_DATA);
1954 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1955 ZPOOL_CONFIG_POOL_GUID);
1960 * If config is not trusted then ignore the spa guid check. This is
1961 * necessary because if the machine crashed during a re-guid the new
1962 * guid might have been written to all of the vdev labels, but not the
1963 * cached config. The check will be performed again once we have the
1964 * trusted config from the MOS.
1966 if (spa->spa_trust_config && guid != spa_guid(spa)) {
1967 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1968 VDEV_AUX_CORRUPT_DATA);
1970 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
1971 "match config (%llu != %llu)", (u_longlong_t)guid,
1972 (u_longlong_t)spa_guid(spa));
1976 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
1977 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
1981 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
1982 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1983 VDEV_AUX_CORRUPT_DATA);
1985 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1990 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
1992 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1993 VDEV_AUX_CORRUPT_DATA);
1995 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1996 ZPOOL_CONFIG_TOP_GUID);
2001 * If this vdev just became a top-level vdev because its sibling was
2002 * detached, it will have adopted the parent's vdev guid -- but the
2003 * label may or may not be on disk yet. Fortunately, either version
2004 * of the label will have the same top guid, so if we're a top-level
2005 * vdev, we can safely compare to that instead.
2006 * However, if the config comes from a cachefile that failed to update
2007 * after the detach, a top-level vdev will appear as a non top-level
2008 * vdev in the config. Also relax the constraints if we perform an
2011 * If we split this vdev off instead, then we also check the
2012 * original pool's guid. We don't want to consider the vdev
2013 * corrupt if it is partway through a split operation.
2015 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
2016 boolean_t mismatch = B_FALSE;
2017 if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
2018 if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
2021 if (vd->vdev_guid != top_guid &&
2022 vd->vdev_top->vdev_guid != guid)
2027 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2028 VDEV_AUX_CORRUPT_DATA);
2030 vdev_dbgmsg(vd, "vdev_validate: config guid "
2031 "doesn't match label guid");
2032 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
2033 (u_longlong_t)vd->vdev_guid,
2034 (u_longlong_t)vd->vdev_top->vdev_guid);
2035 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
2036 "aux_guid %llu", (u_longlong_t)guid,
2037 (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
2042 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
2044 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2045 VDEV_AUX_CORRUPT_DATA);
2047 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2048 ZPOOL_CONFIG_POOL_STATE);
2055 * If this is a verbatim import, no need to check the
2056 * state of the pool.
2058 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
2059 spa_load_state(spa) == SPA_LOAD_OPEN &&
2060 state != POOL_STATE_ACTIVE) {
2061 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
2062 "for spa %s", (u_longlong_t)state, spa->spa_name);
2063 return (SET_ERROR(EBADF));
2067 * If we were able to open and validate a vdev that was
2068 * previously marked permanently unavailable, clear that state
2071 if (vd->vdev_not_present)
2072 vd->vdev_not_present = 0;
2078 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
2080 if (svd->vdev_path != NULL && dvd->vdev_path != NULL) {
2081 if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) {
2082 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
2083 "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
2084 dvd->vdev_path, svd->vdev_path);
2085 spa_strfree(dvd->vdev_path);
2086 dvd->vdev_path = spa_strdup(svd->vdev_path);
2088 } else if (svd->vdev_path != NULL) {
2089 dvd->vdev_path = spa_strdup(svd->vdev_path);
2090 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
2091 (u_longlong_t)dvd->vdev_guid, dvd->vdev_path);
2096 * Recursively copy vdev paths from one vdev to another. Source and destination
2097 * vdev trees must have same geometry otherwise return error. Intended to copy
2098 * paths from userland config into MOS config.
2101 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
2103 if ((svd->vdev_ops == &vdev_missing_ops) ||
2104 (svd->vdev_ishole && dvd->vdev_ishole) ||
2105 (dvd->vdev_ops == &vdev_indirect_ops))
2108 if (svd->vdev_ops != dvd->vdev_ops) {
2109 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
2110 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
2111 return (SET_ERROR(EINVAL));
2114 if (svd->vdev_guid != dvd->vdev_guid) {
2115 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
2116 "%llu)", (u_longlong_t)svd->vdev_guid,
2117 (u_longlong_t)dvd->vdev_guid);
2118 return (SET_ERROR(EINVAL));
2121 if (svd->vdev_children != dvd->vdev_children) {
2122 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
2123 "%llu != %llu", (u_longlong_t)svd->vdev_children,
2124 (u_longlong_t)dvd->vdev_children);
2125 return (SET_ERROR(EINVAL));
2128 for (uint64_t i = 0; i < svd->vdev_children; i++) {
2129 int error = vdev_copy_path_strict(svd->vdev_child[i],
2130 dvd->vdev_child[i]);
2135 if (svd->vdev_ops->vdev_op_leaf)
2136 vdev_copy_path_impl(svd, dvd);
2142 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
2144 ASSERT(stvd->vdev_top == stvd);
2145 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
2147 for (uint64_t i = 0; i < dvd->vdev_children; i++) {
2148 vdev_copy_path_search(stvd, dvd->vdev_child[i]);
2151 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
2155 * The idea here is that while a vdev can shift positions within
2156 * a top vdev (when replacing, attaching mirror, etc.) it cannot
2157 * step outside of it.
2159 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
2161 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
2164 ASSERT(vd->vdev_ops->vdev_op_leaf);
2166 vdev_copy_path_impl(vd, dvd);
2170 * Recursively copy vdev paths from one root vdev to another. Source and
2171 * destination vdev trees may differ in geometry. For each destination leaf
2172 * vdev, search a vdev with the same guid and top vdev id in the source.
2173 * Intended to copy paths from userland config into MOS config.
2176 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
2178 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
2179 ASSERT(srvd->vdev_ops == &vdev_root_ops);
2180 ASSERT(drvd->vdev_ops == &vdev_root_ops);
2182 for (uint64_t i = 0; i < children; i++) {
2183 vdev_copy_path_search(srvd->vdev_child[i],
2184 drvd->vdev_child[i]);
2189 * Close a virtual device.
2192 vdev_close(vdev_t *vd)
2194 vdev_t *pvd = vd->vdev_parent;
2195 ASSERTV(spa_t *spa = vd->vdev_spa);
2197 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2200 * If our parent is reopening, then we are as well, unless we are
2203 if (pvd != NULL && pvd->vdev_reopening)
2204 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
2206 vd->vdev_ops->vdev_op_close(vd);
2208 vdev_cache_purge(vd);
2211 * We record the previous state before we close it, so that if we are
2212 * doing a reopen(), we don't generate FMA ereports if we notice that
2213 * it's still faulted.
2215 vd->vdev_prevstate = vd->vdev_state;
2217 if (vd->vdev_offline)
2218 vd->vdev_state = VDEV_STATE_OFFLINE;
2220 vd->vdev_state = VDEV_STATE_CLOSED;
2221 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2225 vdev_hold(vdev_t *vd)
2227 spa_t *spa = vd->vdev_spa;
2229 ASSERT(spa_is_root(spa));
2230 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
2233 for (int c = 0; c < vd->vdev_children; c++)
2234 vdev_hold(vd->vdev_child[c]);
2236 if (vd->vdev_ops->vdev_op_leaf)
2237 vd->vdev_ops->vdev_op_hold(vd);
2241 vdev_rele(vdev_t *vd)
2243 ASSERT(spa_is_root(vd->vdev_spa));
2244 for (int c = 0; c < vd->vdev_children; c++)
2245 vdev_rele(vd->vdev_child[c]);
2247 if (vd->vdev_ops->vdev_op_leaf)
2248 vd->vdev_ops->vdev_op_rele(vd);
2252 * Reopen all interior vdevs and any unopened leaves. We don't actually
2253 * reopen leaf vdevs which had previously been opened as they might deadlock
2254 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2255 * If the leaf has never been opened then open it, as usual.
2258 vdev_reopen(vdev_t *vd)
2260 spa_t *spa = vd->vdev_spa;
2262 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2264 /* set the reopening flag unless we're taking the vdev offline */
2265 vd->vdev_reopening = !vd->vdev_offline;
2267 (void) vdev_open(vd);
2270 * Call vdev_validate() here to make sure we have the same device.
2271 * Otherwise, a device with an invalid label could be successfully
2272 * opened in response to vdev_reopen().
2275 (void) vdev_validate_aux(vd);
2276 if (vdev_readable(vd) && vdev_writeable(vd) &&
2277 vd->vdev_aux == &spa->spa_l2cache &&
2278 !l2arc_vdev_present(vd))
2279 l2arc_add_vdev(spa, vd);
2281 (void) vdev_validate(vd);
2285 * Reassess parent vdev's health.
2287 vdev_propagate_state(vd);
2291 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
2296 * Normally, partial opens (e.g. of a mirror) are allowed.
2297 * For a create, however, we want to fail the request if
2298 * there are any components we can't open.
2300 error = vdev_open(vd);
2302 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
2304 return (error ? error : ENXIO);
2308 * Recursively load DTLs and initialize all labels.
2310 if ((error = vdev_dtl_load(vd)) != 0 ||
2311 (error = vdev_label_init(vd, txg, isreplacing ?
2312 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
2321 vdev_metaslab_set_size(vdev_t *vd)
2323 uint64_t asize = vd->vdev_asize;
2324 uint64_t ms_count = asize >> zfs_vdev_default_ms_shift;
2328 * There are two dimensions to the metaslab sizing calculation:
2329 * the size of the metaslab and the count of metaslabs per vdev.
2331 * The default values used below are a good balance between memory
2332 * usage (larger metaslab size means more memory needed for loaded
2333 * metaslabs; more metaslabs means more memory needed for the
2334 * metaslab_t structs), metaslab load time (larger metaslabs take
2335 * longer to load), and metaslab sync time (more metaslabs means
2336 * more time spent syncing all of them).
2338 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2339 * The range of the dimensions are as follows:
2341 * 2^29 <= ms_size <= 2^34
2342 * 16 <= ms_count <= 131,072
2344 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2345 * at least 512MB (2^29) to minimize fragmentation effects when
2346 * testing with smaller devices. However, the count constraint
2347 * of at least 16 metaslabs will override this minimum size goal.
2349 * On the upper end of vdev sizes, we aim for a maximum metaslab
2350 * size of 16GB. However, we will cap the total count to 2^17
2351 * metaslabs to keep our memory footprint in check and let the
2352 * metaslab size grow from there if that limit is hit.
2354 * The net effect of applying above constrains is summarized below.
2356 * vdev size metaslab count
2357 * --------------|-----------------
2359 * 8GB - 100GB one per 512MB
2361 * 3TB - 2PB one per 16GB
2363 * --------------------------------
2365 * Finally, note that all of the above calculate the initial
2366 * number of metaslabs. Expanding a top-level vdev will result
2367 * in additional metaslabs being allocated making it possible
2368 * to exceed the zfs_vdev_ms_count_limit.
2371 if (ms_count < zfs_vdev_min_ms_count)
2372 ms_shift = highbit64(asize / zfs_vdev_min_ms_count);
2373 else if (ms_count > zfs_vdev_default_ms_count)
2374 ms_shift = highbit64(asize / zfs_vdev_default_ms_count);
2376 ms_shift = zfs_vdev_default_ms_shift;
2378 if (ms_shift < SPA_MAXBLOCKSHIFT) {
2379 ms_shift = SPA_MAXBLOCKSHIFT;
2380 } else if (ms_shift > zfs_vdev_max_ms_shift) {
2381 ms_shift = zfs_vdev_max_ms_shift;
2382 /* cap the total count to constrain memory footprint */
2383 if ((asize >> ms_shift) > zfs_vdev_ms_count_limit)
2384 ms_shift = highbit64(asize / zfs_vdev_ms_count_limit);
2387 vd->vdev_ms_shift = ms_shift;
2388 ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT);
2392 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2394 ASSERT(vd == vd->vdev_top);
2395 /* indirect vdevs don't have metaslabs or dtls */
2396 ASSERT(vdev_is_concrete(vd) || flags == 0);
2397 ASSERT(ISP2(flags));
2398 ASSERT(spa_writeable(vd->vdev_spa));
2400 if (flags & VDD_METASLAB)
2401 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2403 if (flags & VDD_DTL)
2404 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2406 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2410 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
2412 for (int c = 0; c < vd->vdev_children; c++)
2413 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
2415 if (vd->vdev_ops->vdev_op_leaf)
2416 vdev_dirty(vd->vdev_top, flags, vd, txg);
2422 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2423 * the vdev has less than perfect replication. There are four kinds of DTL:
2425 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2427 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2429 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2430 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2431 * txgs that was scrubbed.
2433 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2434 * persistent errors or just some device being offline.
2435 * Unlike the other three, the DTL_OUTAGE map is not generally
2436 * maintained; it's only computed when needed, typically to
2437 * determine whether a device can be detached.
2439 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2440 * either has the data or it doesn't.
2442 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2443 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2444 * if any child is less than fully replicated, then so is its parent.
2445 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2446 * comprising only those txgs which appear in 'maxfaults' or more children;
2447 * those are the txgs we don't have enough replication to read. For example,
2448 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2449 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2450 * two child DTL_MISSING maps.
2452 * It should be clear from the above that to compute the DTLs and outage maps
2453 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2454 * Therefore, that is all we keep on disk. When loading the pool, or after
2455 * a configuration change, we generate all other DTLs from first principles.
2458 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2460 range_tree_t *rt = vd->vdev_dtl[t];
2462 ASSERT(t < DTL_TYPES);
2463 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2464 ASSERT(spa_writeable(vd->vdev_spa));
2466 mutex_enter(&vd->vdev_dtl_lock);
2467 if (!range_tree_contains(rt, txg, size))
2468 range_tree_add(rt, txg, size);
2469 mutex_exit(&vd->vdev_dtl_lock);
2473 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2475 range_tree_t *rt = vd->vdev_dtl[t];
2476 boolean_t dirty = B_FALSE;
2478 ASSERT(t < DTL_TYPES);
2479 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2482 * While we are loading the pool, the DTLs have not been loaded yet.
2483 * Ignore the DTLs and try all devices. This avoids a recursive
2484 * mutex enter on the vdev_dtl_lock, and also makes us try hard
2485 * when loading the pool (relying on the checksum to ensure that
2486 * we get the right data -- note that we while loading, we are
2487 * only reading the MOS, which is always checksummed).
2489 if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE)
2492 mutex_enter(&vd->vdev_dtl_lock);
2493 if (!range_tree_is_empty(rt))
2494 dirty = range_tree_contains(rt, txg, size);
2495 mutex_exit(&vd->vdev_dtl_lock);
2501 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
2503 range_tree_t *rt = vd->vdev_dtl[t];
2506 mutex_enter(&vd->vdev_dtl_lock);
2507 empty = range_tree_is_empty(rt);
2508 mutex_exit(&vd->vdev_dtl_lock);
2514 * Returns B_TRUE if vdev determines offset needs to be resilvered.
2517 vdev_dtl_need_resilver(vdev_t *vd, uint64_t offset, size_t psize)
2519 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2521 if (vd->vdev_ops->vdev_op_need_resilver == NULL ||
2522 vd->vdev_ops->vdev_op_leaf)
2525 return (vd->vdev_ops->vdev_op_need_resilver(vd, offset, psize));
2529 * Returns the lowest txg in the DTL range.
2532 vdev_dtl_min(vdev_t *vd)
2536 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2537 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2538 ASSERT0(vd->vdev_children);
2540 rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
2541 return (rs->rs_start - 1);
2545 * Returns the highest txg in the DTL.
2548 vdev_dtl_max(vdev_t *vd)
2552 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2553 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2554 ASSERT0(vd->vdev_children);
2556 rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
2557 return (rs->rs_end);
2561 * Determine if a resilvering vdev should remove any DTL entries from
2562 * its range. If the vdev was resilvering for the entire duration of the
2563 * scan then it should excise that range from its DTLs. Otherwise, this
2564 * vdev is considered partially resilvered and should leave its DTL
2565 * entries intact. The comment in vdev_dtl_reassess() describes how we
2569 vdev_dtl_should_excise(vdev_t *vd)
2571 spa_t *spa = vd->vdev_spa;
2572 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2574 ASSERT0(scn->scn_phys.scn_errors);
2575 ASSERT0(vd->vdev_children);
2577 if (vd->vdev_state < VDEV_STATE_DEGRADED)
2580 if (vd->vdev_resilver_deferred)
2583 if (vd->vdev_resilver_txg == 0 ||
2584 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]))
2588 * When a resilver is initiated the scan will assign the scn_max_txg
2589 * value to the highest txg value that exists in all DTLs. If this
2590 * device's max DTL is not part of this scan (i.e. it is not in
2591 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2594 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
2595 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
2596 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
2597 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
2604 * Reassess DTLs after a config change or scrub completion. If txg == 0 no
2605 * write operations will be issued to the pool.
2608 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
2610 spa_t *spa = vd->vdev_spa;
2614 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2616 for (int c = 0; c < vd->vdev_children; c++)
2617 vdev_dtl_reassess(vd->vdev_child[c], txg,
2618 scrub_txg, scrub_done);
2620 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
2623 if (vd->vdev_ops->vdev_op_leaf) {
2624 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2626 mutex_enter(&vd->vdev_dtl_lock);
2629 * If requested, pretend the scan completed cleanly.
2631 if (zfs_scan_ignore_errors && scn)
2632 scn->scn_phys.scn_errors = 0;
2635 * If we've completed a scan cleanly then determine
2636 * if this vdev should remove any DTLs. We only want to
2637 * excise regions on vdevs that were available during
2638 * the entire duration of this scan.
2640 if (scrub_txg != 0 &&
2641 (spa->spa_scrub_started ||
2642 (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
2643 vdev_dtl_should_excise(vd)) {
2645 * We completed a scrub up to scrub_txg. If we
2646 * did it without rebooting, then the scrub dtl
2647 * will be valid, so excise the old region and
2648 * fold in the scrub dtl. Otherwise, leave the
2649 * dtl as-is if there was an error.
2651 * There's little trick here: to excise the beginning
2652 * of the DTL_MISSING map, we put it into a reference
2653 * tree and then add a segment with refcnt -1 that
2654 * covers the range [0, scrub_txg). This means
2655 * that each txg in that range has refcnt -1 or 0.
2656 * We then add DTL_SCRUB with a refcnt of 2, so that
2657 * entries in the range [0, scrub_txg) will have a
2658 * positive refcnt -- either 1 or 2. We then convert
2659 * the reference tree into the new DTL_MISSING map.
2661 space_reftree_create(&reftree);
2662 space_reftree_add_map(&reftree,
2663 vd->vdev_dtl[DTL_MISSING], 1);
2664 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
2665 space_reftree_add_map(&reftree,
2666 vd->vdev_dtl[DTL_SCRUB], 2);
2667 space_reftree_generate_map(&reftree,
2668 vd->vdev_dtl[DTL_MISSING], 1);
2669 space_reftree_destroy(&reftree);
2671 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
2672 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
2673 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
2675 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
2676 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
2677 if (!vdev_readable(vd))
2678 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
2680 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
2681 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
2684 * If the vdev was resilvering and no longer has any
2685 * DTLs then reset its resilvering flag and dirty
2686 * the top level so that we persist the change.
2688 if (txg != 0 && vd->vdev_resilver_txg != 0 &&
2689 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
2690 range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) {
2691 vd->vdev_resilver_txg = 0;
2692 vdev_config_dirty(vd->vdev_top);
2695 mutex_exit(&vd->vdev_dtl_lock);
2698 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2702 mutex_enter(&vd->vdev_dtl_lock);
2703 for (int t = 0; t < DTL_TYPES; t++) {
2704 /* account for child's outage in parent's missing map */
2705 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
2707 continue; /* leaf vdevs only */
2708 if (t == DTL_PARTIAL)
2709 minref = 1; /* i.e. non-zero */
2710 else if (vd->vdev_nparity != 0)
2711 minref = vd->vdev_nparity + 1; /* RAID-Z */
2713 minref = vd->vdev_children; /* any kind of mirror */
2714 space_reftree_create(&reftree);
2715 for (int c = 0; c < vd->vdev_children; c++) {
2716 vdev_t *cvd = vd->vdev_child[c];
2717 mutex_enter(&cvd->vdev_dtl_lock);
2718 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
2719 mutex_exit(&cvd->vdev_dtl_lock);
2721 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
2722 space_reftree_destroy(&reftree);
2724 mutex_exit(&vd->vdev_dtl_lock);
2728 vdev_dtl_load(vdev_t *vd)
2730 spa_t *spa = vd->vdev_spa;
2731 objset_t *mos = spa->spa_meta_objset;
2734 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
2735 ASSERT(vdev_is_concrete(vd));
2737 error = space_map_open(&vd->vdev_dtl_sm, mos,
2738 vd->vdev_dtl_object, 0, -1ULL, 0);
2741 ASSERT(vd->vdev_dtl_sm != NULL);
2743 mutex_enter(&vd->vdev_dtl_lock);
2744 error = space_map_load(vd->vdev_dtl_sm,
2745 vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
2746 mutex_exit(&vd->vdev_dtl_lock);
2751 for (int c = 0; c < vd->vdev_children; c++) {
2752 error = vdev_dtl_load(vd->vdev_child[c]);
2761 vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx)
2763 spa_t *spa = vd->vdev_spa;
2764 objset_t *mos = spa->spa_meta_objset;
2765 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
2768 ASSERT(alloc_bias != VDEV_BIAS_NONE);
2771 (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG :
2772 (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL :
2773 (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL;
2775 ASSERT(string != NULL);
2776 VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS,
2777 1, strlen(string) + 1, string, tx));
2779 if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) {
2780 spa_activate_allocation_classes(spa, tx);
2785 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
2787 spa_t *spa = vd->vdev_spa;
2789 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
2790 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2795 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
2797 spa_t *spa = vd->vdev_spa;
2798 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
2799 DMU_OT_NONE, 0, tx);
2802 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2809 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
2811 if (vd->vdev_ops != &vdev_hole_ops &&
2812 vd->vdev_ops != &vdev_missing_ops &&
2813 vd->vdev_ops != &vdev_root_ops &&
2814 !vd->vdev_top->vdev_removing) {
2815 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
2816 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
2818 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
2819 vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
2820 if (vd->vdev_alloc_bias != VDEV_BIAS_NONE)
2821 vdev_zap_allocation_data(vd, tx);
2825 for (uint64_t i = 0; i < vd->vdev_children; i++) {
2826 vdev_construct_zaps(vd->vdev_child[i], tx);
2831 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
2833 spa_t *spa = vd->vdev_spa;
2834 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2835 objset_t *mos = spa->spa_meta_objset;
2836 range_tree_t *rtsync;
2838 uint64_t object = space_map_object(vd->vdev_dtl_sm);
2840 ASSERT(vdev_is_concrete(vd));
2841 ASSERT(vd->vdev_ops->vdev_op_leaf);
2843 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2845 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
2846 mutex_enter(&vd->vdev_dtl_lock);
2847 space_map_free(vd->vdev_dtl_sm, tx);
2848 space_map_close(vd->vdev_dtl_sm);
2849 vd->vdev_dtl_sm = NULL;
2850 mutex_exit(&vd->vdev_dtl_lock);
2853 * We only destroy the leaf ZAP for detached leaves or for
2854 * removed log devices. Removed data devices handle leaf ZAP
2855 * cleanup later, once cancellation is no longer possible.
2857 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2858 vd->vdev_top->vdev_islog)) {
2859 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2860 vd->vdev_leaf_zap = 0;
2867 if (vd->vdev_dtl_sm == NULL) {
2868 uint64_t new_object;
2870 new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx);
2871 VERIFY3U(new_object, !=, 0);
2873 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
2875 ASSERT(vd->vdev_dtl_sm != NULL);
2878 rtsync = range_tree_create(NULL, NULL);
2880 mutex_enter(&vd->vdev_dtl_lock);
2881 range_tree_walk(rt, range_tree_add, rtsync);
2882 mutex_exit(&vd->vdev_dtl_lock);
2884 space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx);
2885 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx);
2886 range_tree_vacate(rtsync, NULL, NULL);
2888 range_tree_destroy(rtsync);
2891 * If the object for the space map has changed then dirty
2892 * the top level so that we update the config.
2894 if (object != space_map_object(vd->vdev_dtl_sm)) {
2895 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
2896 "new object %llu", (u_longlong_t)txg, spa_name(spa),
2897 (u_longlong_t)object,
2898 (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
2899 vdev_config_dirty(vd->vdev_top);
2906 * Determine whether the specified vdev can be offlined/detached/removed
2907 * without losing data.
2910 vdev_dtl_required(vdev_t *vd)
2912 spa_t *spa = vd->vdev_spa;
2913 vdev_t *tvd = vd->vdev_top;
2914 uint8_t cant_read = vd->vdev_cant_read;
2917 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2919 if (vd == spa->spa_root_vdev || vd == tvd)
2923 * Temporarily mark the device as unreadable, and then determine
2924 * whether this results in any DTL outages in the top-level vdev.
2925 * If not, we can safely offline/detach/remove the device.
2927 vd->vdev_cant_read = B_TRUE;
2928 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2929 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
2930 vd->vdev_cant_read = cant_read;
2931 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2933 if (!required && zio_injection_enabled)
2934 required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2940 * Determine if resilver is needed, and if so the txg range.
2943 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2945 boolean_t needed = B_FALSE;
2946 uint64_t thismin = UINT64_MAX;
2947 uint64_t thismax = 0;
2949 if (vd->vdev_children == 0) {
2950 mutex_enter(&vd->vdev_dtl_lock);
2951 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
2952 vdev_writeable(vd)) {
2954 thismin = vdev_dtl_min(vd);
2955 thismax = vdev_dtl_max(vd);
2958 mutex_exit(&vd->vdev_dtl_lock);
2960 for (int c = 0; c < vd->vdev_children; c++) {
2961 vdev_t *cvd = vd->vdev_child[c];
2962 uint64_t cmin, cmax;
2964 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2965 thismin = MIN(thismin, cmin);
2966 thismax = MAX(thismax, cmax);
2972 if (needed && minp) {
2980 * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj
2981 * will contain either the checkpoint spacemap object or zero if none exists.
2982 * All other errors are returned to the caller.
2985 vdev_checkpoint_sm_object(vdev_t *vd, uint64_t *sm_obj)
2987 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
2989 if (vd->vdev_top_zap == 0) {
2994 int error = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap,
2995 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, sm_obj);
2996 if (error == ENOENT) {
3005 vdev_load(vdev_t *vd)
3010 * Recursively load all children.
3012 for (int c = 0; c < vd->vdev_children; c++) {
3013 error = vdev_load(vd->vdev_child[c]);
3019 vdev_set_deflate_ratio(vd);
3022 * On spa_load path, grab the allocation bias from our zap
3024 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3025 spa_t *spa = vd->vdev_spa;
3028 if (zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
3029 VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str),
3031 ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE);
3032 vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str);
3037 * If this is a top-level vdev, initialize its metaslabs.
3039 if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
3040 vdev_metaslab_group_create(vd);
3042 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
3043 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3044 VDEV_AUX_CORRUPT_DATA);
3045 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
3046 "asize=%llu", (u_longlong_t)vd->vdev_ashift,
3047 (u_longlong_t)vd->vdev_asize);
3048 return (SET_ERROR(ENXIO));
3051 error = vdev_metaslab_init(vd, 0);
3053 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
3054 "[error=%d]", error);
3055 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3056 VDEV_AUX_CORRUPT_DATA);
3060 uint64_t checkpoint_sm_obj;
3061 error = vdev_checkpoint_sm_object(vd, &checkpoint_sm_obj);
3062 if (error == 0 && checkpoint_sm_obj != 0) {
3063 objset_t *mos = spa_meta_objset(vd->vdev_spa);
3064 ASSERT(vd->vdev_asize != 0);
3065 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL);
3067 error = space_map_open(&vd->vdev_checkpoint_sm,
3068 mos, checkpoint_sm_obj, 0, vd->vdev_asize,
3071 vdev_dbgmsg(vd, "vdev_load: space_map_open "
3072 "failed for checkpoint spacemap (obj %llu) "
3074 (u_longlong_t)checkpoint_sm_obj, error);
3077 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
3080 * Since the checkpoint_sm contains free entries
3081 * exclusively we can use space_map_allocated() to
3082 * indicate the cumulative checkpointed space that
3085 vd->vdev_stat.vs_checkpoint_space =
3086 -space_map_allocated(vd->vdev_checkpoint_sm);
3087 vd->vdev_spa->spa_checkpoint_info.sci_dspace +=
3088 vd->vdev_stat.vs_checkpoint_space;
3089 } else if (error != 0) {
3090 vdev_dbgmsg(vd, "vdev_load: failed to retrieve "
3091 "checkpoint space map object from vdev ZAP "
3092 "[error=%d]", error);
3098 * If this is a leaf vdev, load its DTL.
3100 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
3101 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3102 VDEV_AUX_CORRUPT_DATA);
3103 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
3104 "[error=%d]", error);
3108 uint64_t obsolete_sm_object;
3109 error = vdev_obsolete_sm_object(vd, &obsolete_sm_object);
3110 if (error == 0 && obsolete_sm_object != 0) {
3111 objset_t *mos = vd->vdev_spa->spa_meta_objset;
3112 ASSERT(vd->vdev_asize != 0);
3113 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
3115 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
3116 obsolete_sm_object, 0, vd->vdev_asize, 0))) {
3117 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3118 VDEV_AUX_CORRUPT_DATA);
3119 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
3120 "obsolete spacemap (obj %llu) [error=%d]",
3121 (u_longlong_t)obsolete_sm_object, error);
3124 } else if (error != 0) {
3125 vdev_dbgmsg(vd, "vdev_load: failed to retrieve obsolete "
3126 "space map object from vdev ZAP [error=%d]", error);
3134 * The special vdev case is used for hot spares and l2cache devices. Its
3135 * sole purpose it to set the vdev state for the associated vdev. To do this,
3136 * we make sure that we can open the underlying device, then try to read the
3137 * label, and make sure that the label is sane and that it hasn't been
3138 * repurposed to another pool.
3141 vdev_validate_aux(vdev_t *vd)
3144 uint64_t guid, version;
3147 if (!vdev_readable(vd))
3150 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
3151 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3152 VDEV_AUX_CORRUPT_DATA);
3156 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
3157 !SPA_VERSION_IS_SUPPORTED(version) ||
3158 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
3159 guid != vd->vdev_guid ||
3160 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
3161 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3162 VDEV_AUX_CORRUPT_DATA);
3168 * We don't actually check the pool state here. If it's in fact in
3169 * use by another pool, we update this fact on the fly when requested.
3176 * Free the objects used to store this vdev's spacemaps, and the array
3177 * that points to them.
3180 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
3182 if (vd->vdev_ms_array == 0)
3185 objset_t *mos = vd->vdev_spa->spa_meta_objset;
3186 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
3187 size_t array_bytes = array_count * sizeof (uint64_t);
3188 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
3189 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
3190 array_bytes, smobj_array, 0));
3192 for (uint64_t i = 0; i < array_count; i++) {
3193 uint64_t smobj = smobj_array[i];
3197 space_map_free_obj(mos, smobj, tx);
3200 kmem_free(smobj_array, array_bytes);
3201 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
3202 vd->vdev_ms_array = 0;
3206 vdev_remove_empty_log(vdev_t *vd, uint64_t txg)
3208 spa_t *spa = vd->vdev_spa;
3210 ASSERT(vd->vdev_islog);
3211 ASSERT(vd == vd->vdev_top);
3212 ASSERT3U(txg, ==, spa_syncing_txg(spa));
3214 dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
3216 vdev_destroy_spacemaps(vd, tx);
3217 if (vd->vdev_top_zap != 0) {
3218 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
3219 vd->vdev_top_zap = 0;
3226 vdev_sync_done(vdev_t *vd, uint64_t txg)
3229 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
3231 ASSERT(vdev_is_concrete(vd));
3233 while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
3235 metaslab_sync_done(msp, txg);
3238 * Because this function is only called on dirty vdevs, it's possible
3239 * we won't consider all metaslabs for unloading on every
3240 * txg. However, unless the system is largely idle it is likely that
3241 * we will dirty all vdevs within a few txgs.
3243 for (int i = 0; i < vd->vdev_ms_count; i++) {
3244 msp = vd->vdev_ms[i];
3245 mutex_enter(&msp->ms_lock);
3246 if (msp->ms_sm != NULL)
3247 metaslab_potentially_unload(msp, txg);
3248 mutex_exit(&msp->ms_lock);
3252 metaslab_sync_reassess(vd->vdev_mg);
3256 vdev_sync(vdev_t *vd, uint64_t txg)
3258 spa_t *spa = vd->vdev_spa;
3262 ASSERT3U(txg, ==, spa->spa_syncing_txg);
3263 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3264 if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
3265 ASSERT(vd->vdev_removing ||
3266 vd->vdev_ops == &vdev_indirect_ops);
3268 vdev_indirect_sync_obsolete(vd, tx);
3271 * If the vdev is indirect, it can't have dirty
3272 * metaslabs or DTLs.
3274 if (vd->vdev_ops == &vdev_indirect_ops) {
3275 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
3276 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
3282 ASSERT(vdev_is_concrete(vd));
3284 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
3285 !vd->vdev_removing) {
3286 ASSERT(vd == vd->vdev_top);
3287 ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
3288 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
3289 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
3290 ASSERT(vd->vdev_ms_array != 0);
3291 vdev_config_dirty(vd);
3294 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
3295 metaslab_sync(msp, txg);
3296 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
3299 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
3300 vdev_dtl_sync(lvd, txg);
3303 * If this is an empty log device being removed, destroy the
3304 * metadata associated with it.
3306 if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
3307 vdev_remove_empty_log(vd, txg);
3309 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
3314 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
3316 return (vd->vdev_ops->vdev_op_asize(vd, psize));
3320 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
3321 * not be opened, and no I/O is attempted.
3324 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
3328 spa_vdev_state_enter(spa, SCL_NONE);
3330 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3331 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3333 if (!vd->vdev_ops->vdev_op_leaf)
3334 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3339 * If user did a 'zpool offline -f' then make the fault persist across
3342 if (aux == VDEV_AUX_EXTERNAL_PERSIST) {
3344 * There are two kinds of forced faults: temporary and
3345 * persistent. Temporary faults go away at pool import, while
3346 * persistent faults stay set. Both types of faults can be
3347 * cleared with a zpool clear.
3349 * We tell if a vdev is persistently faulted by looking at the
3350 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
3351 * import then it's a persistent fault. Otherwise, it's
3352 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
3353 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
3354 * tells vdev_config_generate() (which gets run later) to set
3355 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
3357 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL;
3358 vd->vdev_tmpoffline = B_FALSE;
3359 aux = VDEV_AUX_EXTERNAL;
3361 vd->vdev_tmpoffline = B_TRUE;
3365 * We don't directly use the aux state here, but if we do a
3366 * vdev_reopen(), we need this value to be present to remember why we
3369 vd->vdev_label_aux = aux;
3372 * Faulted state takes precedence over degraded.
3374 vd->vdev_delayed_close = B_FALSE;
3375 vd->vdev_faulted = 1ULL;
3376 vd->vdev_degraded = 0ULL;
3377 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
3380 * If this device has the only valid copy of the data, then
3381 * back off and simply mark the vdev as degraded instead.
3383 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
3384 vd->vdev_degraded = 1ULL;
3385 vd->vdev_faulted = 0ULL;
3388 * If we reopen the device and it's not dead, only then do we
3393 if (vdev_readable(vd))
3394 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
3397 return (spa_vdev_state_exit(spa, vd, 0));
3401 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
3402 * user that something is wrong. The vdev continues to operate as normal as far
3403 * as I/O is concerned.
3406 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
3410 spa_vdev_state_enter(spa, SCL_NONE);
3412 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3413 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3415 if (!vd->vdev_ops->vdev_op_leaf)
3416 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3419 * If the vdev is already faulted, then don't do anything.
3421 if (vd->vdev_faulted || vd->vdev_degraded)
3422 return (spa_vdev_state_exit(spa, NULL, 0));
3424 vd->vdev_degraded = 1ULL;
3425 if (!vdev_is_dead(vd))
3426 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
3429 return (spa_vdev_state_exit(spa, vd, 0));
3433 * Online the given vdev.
3435 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
3436 * spare device should be detached when the device finishes resilvering.
3437 * Second, the online should be treated like a 'test' online case, so no FMA
3438 * events are generated if the device fails to open.
3441 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
3443 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
3444 boolean_t wasoffline;
3445 vdev_state_t oldstate;
3447 spa_vdev_state_enter(spa, SCL_NONE);
3449 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3450 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3452 if (!vd->vdev_ops->vdev_op_leaf)
3453 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3455 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
3456 oldstate = vd->vdev_state;
3459 vd->vdev_offline = B_FALSE;
3460 vd->vdev_tmpoffline = B_FALSE;
3461 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
3462 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
3464 /* XXX - L2ARC 1.0 does not support expansion */
3465 if (!vd->vdev_aux) {
3466 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3467 pvd->vdev_expanding = !!((flags & ZFS_ONLINE_EXPAND) ||
3468 spa->spa_autoexpand);
3469 vd->vdev_expansion_time = gethrestime_sec();
3473 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
3475 if (!vd->vdev_aux) {
3476 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3477 pvd->vdev_expanding = B_FALSE;
3481 *newstate = vd->vdev_state;
3482 if ((flags & ZFS_ONLINE_UNSPARE) &&
3483 !vdev_is_dead(vd) && vd->vdev_parent &&
3484 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3485 vd->vdev_parent->vdev_child[0] == vd)
3486 vd->vdev_unspare = B_TRUE;
3488 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
3490 /* XXX - L2ARC 1.0 does not support expansion */
3492 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
3493 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
3496 /* Restart initializing if necessary */
3497 mutex_enter(&vd->vdev_initialize_lock);
3498 if (vdev_writeable(vd) &&
3499 vd->vdev_initialize_thread == NULL &&
3500 vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) {
3501 (void) vdev_initialize(vd);
3503 mutex_exit(&vd->vdev_initialize_lock);
3505 /* Restart trimming if necessary */
3506 mutex_enter(&vd->vdev_trim_lock);
3507 if (vdev_writeable(vd) &&
3508 vd->vdev_trim_thread == NULL &&
3509 vd->vdev_trim_state == VDEV_TRIM_ACTIVE) {
3510 (void) vdev_trim(vd, vd->vdev_trim_rate, vd->vdev_trim_partial,
3511 vd->vdev_trim_secure);
3513 mutex_exit(&vd->vdev_trim_lock);
3516 (oldstate < VDEV_STATE_DEGRADED &&
3517 vd->vdev_state >= VDEV_STATE_DEGRADED))
3518 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
3520 return (spa_vdev_state_exit(spa, vd, 0));
3524 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
3528 uint64_t generation;
3529 metaslab_group_t *mg;
3532 spa_vdev_state_enter(spa, SCL_ALLOC);
3534 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3535 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3537 if (!vd->vdev_ops->vdev_op_leaf)
3538 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3542 generation = spa->spa_config_generation + 1;
3545 * If the device isn't already offline, try to offline it.
3547 if (!vd->vdev_offline) {
3549 * If this device has the only valid copy of some data,
3550 * don't allow it to be offlined. Log devices are always
3553 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3554 vdev_dtl_required(vd))
3555 return (spa_vdev_state_exit(spa, NULL, EBUSY));
3558 * If the top-level is a slog and it has had allocations
3559 * then proceed. We check that the vdev's metaslab group
3560 * is not NULL since it's possible that we may have just
3561 * added this vdev but not yet initialized its metaslabs.
3563 if (tvd->vdev_islog && mg != NULL) {
3565 * Prevent any future allocations.
3567 metaslab_group_passivate(mg);
3568 (void) spa_vdev_state_exit(spa, vd, 0);
3570 error = spa_reset_logs(spa);
3573 * If the log device was successfully reset but has
3574 * checkpointed data, do not offline it.
3577 tvd->vdev_checkpoint_sm != NULL) {
3578 ASSERT3U(space_map_allocated(
3579 tvd->vdev_checkpoint_sm), !=, 0);
3580 error = ZFS_ERR_CHECKPOINT_EXISTS;
3583 spa_vdev_state_enter(spa, SCL_ALLOC);
3586 * Check to see if the config has changed.
3588 if (error || generation != spa->spa_config_generation) {
3589 metaslab_group_activate(mg);
3591 return (spa_vdev_state_exit(spa,
3593 (void) spa_vdev_state_exit(spa, vd, 0);
3596 ASSERT0(tvd->vdev_stat.vs_alloc);
3600 * Offline this device and reopen its top-level vdev.
3601 * If the top-level vdev is a log device then just offline
3602 * it. Otherwise, if this action results in the top-level
3603 * vdev becoming unusable, undo it and fail the request.
3605 vd->vdev_offline = B_TRUE;
3608 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3609 vdev_is_dead(tvd)) {
3610 vd->vdev_offline = B_FALSE;
3612 return (spa_vdev_state_exit(spa, NULL, EBUSY));
3616 * Add the device back into the metaslab rotor so that
3617 * once we online the device it's open for business.
3619 if (tvd->vdev_islog && mg != NULL)
3620 metaslab_group_activate(mg);
3623 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
3625 return (spa_vdev_state_exit(spa, vd, 0));
3629 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
3633 mutex_enter(&spa->spa_vdev_top_lock);
3634 error = vdev_offline_locked(spa, guid, flags);
3635 mutex_exit(&spa->spa_vdev_top_lock);
3641 * Clear the error counts associated with this vdev. Unlike vdev_online() and
3642 * vdev_offline(), we assume the spa config is locked. We also clear all
3643 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
3646 vdev_clear(spa_t *spa, vdev_t *vd)
3648 vdev_t *rvd = spa->spa_root_vdev;
3650 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3655 vd->vdev_stat.vs_read_errors = 0;
3656 vd->vdev_stat.vs_write_errors = 0;
3657 vd->vdev_stat.vs_checksum_errors = 0;
3658 vd->vdev_stat.vs_slow_ios = 0;
3660 for (int c = 0; c < vd->vdev_children; c++)
3661 vdev_clear(spa, vd->vdev_child[c]);
3664 * It makes no sense to "clear" an indirect vdev.
3666 if (!vdev_is_concrete(vd))
3670 * If we're in the FAULTED state or have experienced failed I/O, then
3671 * clear the persistent state and attempt to reopen the device. We
3672 * also mark the vdev config dirty, so that the new faulted state is
3673 * written out to disk.
3675 if (vd->vdev_faulted || vd->vdev_degraded ||
3676 !vdev_readable(vd) || !vdev_writeable(vd)) {
3678 * When reopening in response to a clear event, it may be due to
3679 * a fmadm repair request. In this case, if the device is
3680 * still broken, we want to still post the ereport again.
3682 vd->vdev_forcefault = B_TRUE;
3684 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
3685 vd->vdev_cant_read = B_FALSE;
3686 vd->vdev_cant_write = B_FALSE;
3687 vd->vdev_stat.vs_aux = 0;
3689 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
3691 vd->vdev_forcefault = B_FALSE;
3693 if (vd != rvd && vdev_writeable(vd->vdev_top))
3694 vdev_state_dirty(vd->vdev_top);
3696 if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) {
3697 if (dsl_scan_resilvering(spa->spa_dsl_pool) &&
3698 spa_feature_is_enabled(spa,
3699 SPA_FEATURE_RESILVER_DEFER))
3700 vdev_set_deferred_resilver(spa, vd);
3702 spa_async_request(spa, SPA_ASYNC_RESILVER);
3705 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
3709 * When clearing a FMA-diagnosed fault, we always want to
3710 * unspare the device, as we assume that the original spare was
3711 * done in response to the FMA fault.
3713 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
3714 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3715 vd->vdev_parent->vdev_child[0] == vd)
3716 vd->vdev_unspare = B_TRUE;
3720 vdev_is_dead(vdev_t *vd)
3723 * Holes and missing devices are always considered "dead".
3724 * This simplifies the code since we don't have to check for
3725 * these types of devices in the various code paths.
3726 * Instead we rely on the fact that we skip over dead devices
3727 * before issuing I/O to them.
3729 return (vd->vdev_state < VDEV_STATE_DEGRADED ||
3730 vd->vdev_ops == &vdev_hole_ops ||
3731 vd->vdev_ops == &vdev_missing_ops);
3735 vdev_readable(vdev_t *vd)
3737 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
3741 vdev_writeable(vdev_t *vd)
3743 return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
3744 vdev_is_concrete(vd));
3748 vdev_allocatable(vdev_t *vd)
3750 uint64_t state = vd->vdev_state;
3753 * We currently allow allocations from vdevs which may be in the
3754 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3755 * fails to reopen then we'll catch it later when we're holding
3756 * the proper locks. Note that we have to get the vdev state
3757 * in a local variable because although it changes atomically,
3758 * we're asking two separate questions about it.
3760 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
3761 !vd->vdev_cant_write && vdev_is_concrete(vd) &&
3762 vd->vdev_mg->mg_initialized);
3766 vdev_accessible(vdev_t *vd, zio_t *zio)
3768 ASSERT(zio->io_vd == vd);
3770 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
3773 if (zio->io_type == ZIO_TYPE_READ)
3774 return (!vd->vdev_cant_read);
3776 if (zio->io_type == ZIO_TYPE_WRITE)
3777 return (!vd->vdev_cant_write);
3783 vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs)
3785 for (int t = 0; t < VS_ZIO_TYPES; t++) {
3786 vs->vs_ops[t] += cvs->vs_ops[t];
3787 vs->vs_bytes[t] += cvs->vs_bytes[t];
3790 cvs->vs_scan_removing = cvd->vdev_removing;
3794 * Get extended stats
3797 vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx)
3800 for (t = 0; t < ZIO_TYPES; t++) {
3801 for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++)
3802 vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b];
3804 for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) {
3805 vsx->vsx_total_histo[t][b] +=
3806 cvsx->vsx_total_histo[t][b];
3810 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
3811 for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) {
3812 vsx->vsx_queue_histo[t][b] +=
3813 cvsx->vsx_queue_histo[t][b];
3815 vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t];
3816 vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t];
3818 for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++)
3819 vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b];
3821 for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++)
3822 vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b];
3828 vdev_is_spacemap_addressable(vdev_t *vd)
3830 if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2))
3834 * If double-word space map entries are not enabled we assume
3835 * 47 bits of the space map entry are dedicated to the entry's
3836 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
3837 * to calculate the maximum address that can be described by a
3838 * space map entry for the given device.
3840 uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS;
3842 if (shift >= 63) /* detect potential overflow */
3845 return (vd->vdev_asize < (1ULL << shift));
3849 * Get statistics for the given vdev.
3852 vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
3856 * If we're getting stats on the root vdev, aggregate the I/O counts
3857 * over all top-level vdevs (i.e. the direct children of the root).
3859 if (!vd->vdev_ops->vdev_op_leaf) {
3861 memset(vs->vs_ops, 0, sizeof (vs->vs_ops));
3862 memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes));
3865 memset(vsx, 0, sizeof (*vsx));
3867 for (int c = 0; c < vd->vdev_children; c++) {
3868 vdev_t *cvd = vd->vdev_child[c];
3869 vdev_stat_t *cvs = &cvd->vdev_stat;
3870 vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex;
3872 vdev_get_stats_ex_impl(cvd, cvs, cvsx);
3874 vdev_get_child_stat(cvd, vs, cvs);
3876 vdev_get_child_stat_ex(cvd, vsx, cvsx);
3881 * We're a leaf. Just copy our ZIO active queue stats in. The
3882 * other leaf stats are updated in vdev_stat_update().
3887 memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex));
3889 for (t = 0; t < ARRAY_SIZE(vd->vdev_queue.vq_class); t++) {
3890 vsx->vsx_active_queue[t] =
3891 vd->vdev_queue.vq_class[t].vqc_active;
3892 vsx->vsx_pend_queue[t] = avl_numnodes(
3893 &vd->vdev_queue.vq_class[t].vqc_queued_tree);
3899 vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
3901 vdev_t *tvd = vd->vdev_top;
3902 mutex_enter(&vd->vdev_stat_lock);
3904 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
3905 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
3906 vs->vs_state = vd->vdev_state;
3907 vs->vs_rsize = vdev_get_min_asize(vd);
3908 if (vd->vdev_ops->vdev_op_leaf) {
3909 vs->vs_rsize += VDEV_LABEL_START_SIZE +
3910 VDEV_LABEL_END_SIZE;
3912 * Report initializing progress. Since we don't
3913 * have the initializing locks held, this is only
3914 * an estimate (although a fairly accurate one).
3916 vs->vs_initialize_bytes_done =
3917 vd->vdev_initialize_bytes_done;
3918 vs->vs_initialize_bytes_est =
3919 vd->vdev_initialize_bytes_est;
3920 vs->vs_initialize_state = vd->vdev_initialize_state;
3921 vs->vs_initialize_action_time =
3922 vd->vdev_initialize_action_time;
3925 * Report manual TRIM progress. Since we don't have
3926 * the manual TRIM locks held, this is only an
3927 * estimate (although fairly accurate one).
3929 vs->vs_trim_notsup = !vd->vdev_has_trim;
3930 vs->vs_trim_bytes_done = vd->vdev_trim_bytes_done;
3931 vs->vs_trim_bytes_est = vd->vdev_trim_bytes_est;
3932 vs->vs_trim_state = vd->vdev_trim_state;
3933 vs->vs_trim_action_time = vd->vdev_trim_action_time;
3936 * Report expandable space on top-level, non-auxiliary devices
3937 * only. The expandable space is reported in terms of metaslab
3938 * sized units since that determines how much space the pool
3941 if (vd->vdev_aux == NULL && tvd != NULL) {
3942 vs->vs_esize = P2ALIGN(
3943 vd->vdev_max_asize - vd->vdev_asize,
3944 1ULL << tvd->vdev_ms_shift);
3946 if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
3947 vdev_is_concrete(vd)) {
3948 vs->vs_fragmentation = (vd->vdev_mg != NULL) ?
3949 vd->vdev_mg->mg_fragmentation : 0;
3951 if (vd->vdev_ops->vdev_op_leaf)
3952 vs->vs_resilver_deferred = vd->vdev_resilver_deferred;
3955 vdev_get_stats_ex_impl(vd, vs, vsx);
3956 mutex_exit(&vd->vdev_stat_lock);
3960 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
3962 return (vdev_get_stats_ex(vd, vs, NULL));
3966 vdev_clear_stats(vdev_t *vd)
3968 mutex_enter(&vd->vdev_stat_lock);
3969 vd->vdev_stat.vs_space = 0;
3970 vd->vdev_stat.vs_dspace = 0;
3971 vd->vdev_stat.vs_alloc = 0;
3972 mutex_exit(&vd->vdev_stat_lock);
3976 vdev_scan_stat_init(vdev_t *vd)
3978 vdev_stat_t *vs = &vd->vdev_stat;
3980 for (int c = 0; c < vd->vdev_children; c++)
3981 vdev_scan_stat_init(vd->vdev_child[c]);
3983 mutex_enter(&vd->vdev_stat_lock);
3984 vs->vs_scan_processed = 0;
3985 mutex_exit(&vd->vdev_stat_lock);
3989 vdev_stat_update(zio_t *zio, uint64_t psize)
3991 spa_t *spa = zio->io_spa;
3992 vdev_t *rvd = spa->spa_root_vdev;
3993 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
3995 uint64_t txg = zio->io_txg;
3996 vdev_stat_t *vs = &vd->vdev_stat;
3997 vdev_stat_ex_t *vsx = &vd->vdev_stat_ex;
3998 zio_type_t type = zio->io_type;
3999 int flags = zio->io_flags;
4002 * If this i/o is a gang leader, it didn't do any actual work.
4004 if (zio->io_gang_tree)
4007 if (zio->io_error == 0) {
4009 * If this is a root i/o, don't count it -- we've already
4010 * counted the top-level vdevs, and vdev_get_stats() will
4011 * aggregate them when asked. This reduces contention on
4012 * the root vdev_stat_lock and implicitly handles blocks
4013 * that compress away to holes, for which there is no i/o.
4014 * (Holes never create vdev children, so all the counters
4015 * remain zero, which is what we want.)
4017 * Note: this only applies to successful i/o (io_error == 0)
4018 * because unlike i/o counts, errors are not additive.
4019 * When reading a ditto block, for example, failure of
4020 * one top-level vdev does not imply a root-level error.
4025 ASSERT(vd == zio->io_vd);
4027 if (flags & ZIO_FLAG_IO_BYPASS)
4030 mutex_enter(&vd->vdev_stat_lock);
4032 if (flags & ZIO_FLAG_IO_REPAIR) {
4033 if (flags & ZIO_FLAG_SCAN_THREAD) {
4034 dsl_scan_phys_t *scn_phys =
4035 &spa->spa_dsl_pool->dp_scan->scn_phys;
4036 uint64_t *processed = &scn_phys->scn_processed;
4039 if (vd->vdev_ops->vdev_op_leaf)
4040 atomic_add_64(processed, psize);
4041 vs->vs_scan_processed += psize;
4044 if (flags & ZIO_FLAG_SELF_HEAL)
4045 vs->vs_self_healed += psize;
4049 * The bytes/ops/histograms are recorded at the leaf level and
4050 * aggregated into the higher level vdevs in vdev_get_stats().
4052 if (vd->vdev_ops->vdev_op_leaf &&
4053 (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) {
4054 zio_type_t vs_type = type;
4057 * TRIM ops and bytes are reported to user space as
4058 * ZIO_TYPE_IOCTL. This is done to preserve the
4059 * vdev_stat_t structure layout for user space.
4061 if (type == ZIO_TYPE_TRIM)
4062 vs_type = ZIO_TYPE_IOCTL;
4064 vs->vs_ops[vs_type]++;
4065 vs->vs_bytes[vs_type] += psize;
4067 if (flags & ZIO_FLAG_DELEGATED) {
4068 vsx->vsx_agg_histo[zio->io_priority]
4069 [RQ_HISTO(zio->io_size)]++;
4071 vsx->vsx_ind_histo[zio->io_priority]
4072 [RQ_HISTO(zio->io_size)]++;
4075 if (zio->io_delta && zio->io_delay) {
4076 vsx->vsx_queue_histo[zio->io_priority]
4077 [L_HISTO(zio->io_delta - zio->io_delay)]++;
4078 vsx->vsx_disk_histo[type]
4079 [L_HISTO(zio->io_delay)]++;
4080 vsx->vsx_total_histo[type]
4081 [L_HISTO(zio->io_delta)]++;
4085 mutex_exit(&vd->vdev_stat_lock);
4089 if (flags & ZIO_FLAG_SPECULATIVE)
4093 * If this is an I/O error that is going to be retried, then ignore the
4094 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
4095 * hard errors, when in reality they can happen for any number of
4096 * innocuous reasons (bus resets, MPxIO link failure, etc).
4098 if (zio->io_error == EIO &&
4099 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
4103 * Intent logs writes won't propagate their error to the root
4104 * I/O so don't mark these types of failures as pool-level
4107 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
4110 if (spa->spa_load_state == SPA_LOAD_NONE &&
4111 type == ZIO_TYPE_WRITE && txg != 0 &&
4112 (!(flags & ZIO_FLAG_IO_REPAIR) ||
4113 (flags & ZIO_FLAG_SCAN_THREAD) ||
4114 spa->spa_claiming)) {
4116 * This is either a normal write (not a repair), or it's
4117 * a repair induced by the scrub thread, or it's a repair
4118 * made by zil_claim() during spa_load() in the first txg.
4119 * In the normal case, we commit the DTL change in the same
4120 * txg as the block was born. In the scrub-induced repair
4121 * case, we know that scrubs run in first-pass syncing context,
4122 * so we commit the DTL change in spa_syncing_txg(spa).
4123 * In the zil_claim() case, we commit in spa_first_txg(spa).
4125 * We currently do not make DTL entries for failed spontaneous
4126 * self-healing writes triggered by normal (non-scrubbing)
4127 * reads, because we have no transactional context in which to
4128 * do so -- and it's not clear that it'd be desirable anyway.
4130 if (vd->vdev_ops->vdev_op_leaf) {
4131 uint64_t commit_txg = txg;
4132 if (flags & ZIO_FLAG_SCAN_THREAD) {
4133 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
4134 ASSERT(spa_sync_pass(spa) == 1);
4135 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
4136 commit_txg = spa_syncing_txg(spa);
4137 } else if (spa->spa_claiming) {
4138 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
4139 commit_txg = spa_first_txg(spa);
4141 ASSERT(commit_txg >= spa_syncing_txg(spa));
4142 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
4144 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
4145 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
4146 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
4149 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
4154 vdev_deflated_space(vdev_t *vd, int64_t space)
4156 ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0);
4157 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
4159 return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio);
4163 * Update the in-core space usage stats for this vdev, its metaslab class,
4164 * and the root vdev.
4167 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
4168 int64_t space_delta)
4170 int64_t dspace_delta;
4171 spa_t *spa = vd->vdev_spa;
4172 vdev_t *rvd = spa->spa_root_vdev;
4174 ASSERT(vd == vd->vdev_top);
4177 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
4178 * factor. We must calculate this here and not at the root vdev
4179 * because the root vdev's psize-to-asize is simply the max of its
4180 * childrens', thus not accurate enough for us.
4182 dspace_delta = vdev_deflated_space(vd, space_delta);
4184 mutex_enter(&vd->vdev_stat_lock);
4185 /* ensure we won't underflow */
4186 if (alloc_delta < 0) {
4187 ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta);
4190 vd->vdev_stat.vs_alloc += alloc_delta;
4191 vd->vdev_stat.vs_space += space_delta;
4192 vd->vdev_stat.vs_dspace += dspace_delta;
4193 mutex_exit(&vd->vdev_stat_lock);
4195 /* every class but log contributes to root space stats */
4196 if (vd->vdev_mg != NULL && !vd->vdev_islog) {
4197 ASSERT(!vd->vdev_isl2cache);
4198 mutex_enter(&rvd->vdev_stat_lock);
4199 rvd->vdev_stat.vs_alloc += alloc_delta;
4200 rvd->vdev_stat.vs_space += space_delta;
4201 rvd->vdev_stat.vs_dspace += dspace_delta;
4202 mutex_exit(&rvd->vdev_stat_lock);
4204 /* Note: metaslab_class_space_update moved to metaslab_space_update */
4208 * Mark a top-level vdev's config as dirty, placing it on the dirty list
4209 * so that it will be written out next time the vdev configuration is synced.
4210 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
4213 vdev_config_dirty(vdev_t *vd)
4215 spa_t *spa = vd->vdev_spa;
4216 vdev_t *rvd = spa->spa_root_vdev;
4219 ASSERT(spa_writeable(spa));
4222 * If this is an aux vdev (as with l2cache and spare devices), then we
4223 * update the vdev config manually and set the sync flag.
4225 if (vd->vdev_aux != NULL) {
4226 spa_aux_vdev_t *sav = vd->vdev_aux;
4230 for (c = 0; c < sav->sav_count; c++) {
4231 if (sav->sav_vdevs[c] == vd)
4235 if (c == sav->sav_count) {
4237 * We're being removed. There's nothing more to do.
4239 ASSERT(sav->sav_sync == B_TRUE);
4243 sav->sav_sync = B_TRUE;
4245 if (nvlist_lookup_nvlist_array(sav->sav_config,
4246 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
4247 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
4248 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
4254 * Setting the nvlist in the middle if the array is a little
4255 * sketchy, but it will work.
4257 nvlist_free(aux[c]);
4258 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
4264 * The dirty list is protected by the SCL_CONFIG lock. The caller
4265 * must either hold SCL_CONFIG as writer, or must be the sync thread
4266 * (which holds SCL_CONFIG as reader). There's only one sync thread,
4267 * so this is sufficient to ensure mutual exclusion.
4269 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
4270 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4271 spa_config_held(spa, SCL_CONFIG, RW_READER)));
4274 for (c = 0; c < rvd->vdev_children; c++)
4275 vdev_config_dirty(rvd->vdev_child[c]);
4277 ASSERT(vd == vd->vdev_top);
4279 if (!list_link_active(&vd->vdev_config_dirty_node) &&
4280 vdev_is_concrete(vd)) {
4281 list_insert_head(&spa->spa_config_dirty_list, vd);
4287 vdev_config_clean(vdev_t *vd)
4289 spa_t *spa = vd->vdev_spa;
4291 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
4292 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4293 spa_config_held(spa, SCL_CONFIG, RW_READER)));
4295 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
4296 list_remove(&spa->spa_config_dirty_list, vd);
4300 * Mark a top-level vdev's state as dirty, so that the next pass of
4301 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
4302 * the state changes from larger config changes because they require
4303 * much less locking, and are often needed for administrative actions.
4306 vdev_state_dirty(vdev_t *vd)
4308 spa_t *spa = vd->vdev_spa;
4310 ASSERT(spa_writeable(spa));
4311 ASSERT(vd == vd->vdev_top);
4314 * The state list is protected by the SCL_STATE lock. The caller
4315 * must either hold SCL_STATE as writer, or must be the sync thread
4316 * (which holds SCL_STATE as reader). There's only one sync thread,
4317 * so this is sufficient to ensure mutual exclusion.
4319 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
4320 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4321 spa_config_held(spa, SCL_STATE, RW_READER)));
4323 if (!list_link_active(&vd->vdev_state_dirty_node) &&
4324 vdev_is_concrete(vd))
4325 list_insert_head(&spa->spa_state_dirty_list, vd);
4329 vdev_state_clean(vdev_t *vd)
4331 spa_t *spa = vd->vdev_spa;
4333 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
4334 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4335 spa_config_held(spa, SCL_STATE, RW_READER)));
4337 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
4338 list_remove(&spa->spa_state_dirty_list, vd);
4342 * Propagate vdev state up from children to parent.
4345 vdev_propagate_state(vdev_t *vd)
4347 spa_t *spa = vd->vdev_spa;
4348 vdev_t *rvd = spa->spa_root_vdev;
4349 int degraded = 0, faulted = 0;
4353 if (vd->vdev_children > 0) {
4354 for (int c = 0; c < vd->vdev_children; c++) {
4355 child = vd->vdev_child[c];
4358 * Don't factor holes or indirect vdevs into the
4361 if (!vdev_is_concrete(child))
4364 if (!vdev_readable(child) ||
4365 (!vdev_writeable(child) && spa_writeable(spa))) {
4367 * Root special: if there is a top-level log
4368 * device, treat the root vdev as if it were
4371 if (child->vdev_islog && vd == rvd)
4375 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
4379 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
4383 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
4386 * Root special: if there is a top-level vdev that cannot be
4387 * opened due to corrupted metadata, then propagate the root
4388 * vdev's aux state as 'corrupt' rather than 'insufficient
4391 if (corrupted && vd == rvd &&
4392 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
4393 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
4394 VDEV_AUX_CORRUPT_DATA);
4397 if (vd->vdev_parent)
4398 vdev_propagate_state(vd->vdev_parent);
4402 * Set a vdev's state. If this is during an open, we don't update the parent
4403 * state, because we're in the process of opening children depth-first.
4404 * Otherwise, we propagate the change to the parent.
4406 * If this routine places a device in a faulted state, an appropriate ereport is
4410 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
4412 uint64_t save_state;
4413 spa_t *spa = vd->vdev_spa;
4415 if (state == vd->vdev_state) {
4417 * Since vdev_offline() code path is already in an offline
4418 * state we can miss a statechange event to OFFLINE. Check
4419 * the previous state to catch this condition.
4421 if (vd->vdev_ops->vdev_op_leaf &&
4422 (state == VDEV_STATE_OFFLINE) &&
4423 (vd->vdev_prevstate >= VDEV_STATE_FAULTED)) {
4424 /* post an offline state change */
4425 zfs_post_state_change(spa, vd, vd->vdev_prevstate);
4427 vd->vdev_stat.vs_aux = aux;
4431 save_state = vd->vdev_state;
4433 vd->vdev_state = state;
4434 vd->vdev_stat.vs_aux = aux;
4437 * If we are setting the vdev state to anything but an open state, then
4438 * always close the underlying device unless the device has requested
4439 * a delayed close (i.e. we're about to remove or fault the device).
4440 * Otherwise, we keep accessible but invalid devices open forever.
4441 * We don't call vdev_close() itself, because that implies some extra
4442 * checks (offline, etc) that we don't want here. This is limited to
4443 * leaf devices, because otherwise closing the device will affect other
4446 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
4447 vd->vdev_ops->vdev_op_leaf)
4448 vd->vdev_ops->vdev_op_close(vd);
4450 if (vd->vdev_removed &&
4451 state == VDEV_STATE_CANT_OPEN &&
4452 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
4454 * If the previous state is set to VDEV_STATE_REMOVED, then this
4455 * device was previously marked removed and someone attempted to
4456 * reopen it. If this failed due to a nonexistent device, then
4457 * keep the device in the REMOVED state. We also let this be if
4458 * it is one of our special test online cases, which is only
4459 * attempting to online the device and shouldn't generate an FMA
4462 vd->vdev_state = VDEV_STATE_REMOVED;
4463 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
4464 } else if (state == VDEV_STATE_REMOVED) {
4465 vd->vdev_removed = B_TRUE;
4466 } else if (state == VDEV_STATE_CANT_OPEN) {
4468 * If we fail to open a vdev during an import or recovery, we
4469 * mark it as "not available", which signifies that it was
4470 * never there to begin with. Failure to open such a device
4471 * is not considered an error.
4473 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
4474 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
4475 vd->vdev_ops->vdev_op_leaf)
4476 vd->vdev_not_present = 1;
4479 * Post the appropriate ereport. If the 'prevstate' field is
4480 * set to something other than VDEV_STATE_UNKNOWN, it indicates
4481 * that this is part of a vdev_reopen(). In this case, we don't
4482 * want to post the ereport if the device was already in the
4483 * CANT_OPEN state beforehand.
4485 * If the 'checkremove' flag is set, then this is an attempt to
4486 * online the device in response to an insertion event. If we
4487 * hit this case, then we have detected an insertion event for a
4488 * faulted or offline device that wasn't in the removed state.
4489 * In this scenario, we don't post an ereport because we are
4490 * about to replace the device, or attempt an online with
4491 * vdev_forcefault, which will generate the fault for us.
4493 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
4494 !vd->vdev_not_present && !vd->vdev_checkremove &&
4495 vd != spa->spa_root_vdev) {
4499 case VDEV_AUX_OPEN_FAILED:
4500 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
4502 case VDEV_AUX_CORRUPT_DATA:
4503 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
4505 case VDEV_AUX_NO_REPLICAS:
4506 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
4508 case VDEV_AUX_BAD_GUID_SUM:
4509 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
4511 case VDEV_AUX_TOO_SMALL:
4512 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
4514 case VDEV_AUX_BAD_LABEL:
4515 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
4517 case VDEV_AUX_BAD_ASHIFT:
4518 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT;
4521 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
4524 zfs_ereport_post(class, spa, vd, NULL, NULL,
4528 /* Erase any notion of persistent removed state */
4529 vd->vdev_removed = B_FALSE;
4531 vd->vdev_removed = B_FALSE;
4535 * Notify ZED of any significant state-change on a leaf vdev.
4538 if (vd->vdev_ops->vdev_op_leaf) {
4539 /* preserve original state from a vdev_reopen() */
4540 if ((vd->vdev_prevstate != VDEV_STATE_UNKNOWN) &&
4541 (vd->vdev_prevstate != vd->vdev_state) &&
4542 (save_state <= VDEV_STATE_CLOSED))
4543 save_state = vd->vdev_prevstate;
4545 /* filter out state change due to initial vdev_open */
4546 if (save_state > VDEV_STATE_CLOSED)
4547 zfs_post_state_change(spa, vd, save_state);
4550 if (!isopen && vd->vdev_parent)
4551 vdev_propagate_state(vd->vdev_parent);
4555 vdev_children_are_offline(vdev_t *vd)
4557 ASSERT(!vd->vdev_ops->vdev_op_leaf);
4559 for (uint64_t i = 0; i < vd->vdev_children; i++) {
4560 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
4568 * Check the vdev configuration to ensure that it's capable of supporting
4569 * a root pool. We do not support partial configuration.
4572 vdev_is_bootable(vdev_t *vd)
4574 if (!vd->vdev_ops->vdev_op_leaf) {
4575 const char *vdev_type = vd->vdev_ops->vdev_op_type;
4577 if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 ||
4578 strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) {
4583 for (int c = 0; c < vd->vdev_children; c++) {
4584 if (!vdev_is_bootable(vd->vdev_child[c]))
4591 vdev_is_concrete(vdev_t *vd)
4593 vdev_ops_t *ops = vd->vdev_ops;
4594 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
4595 ops == &vdev_missing_ops || ops == &vdev_root_ops) {
4603 * Determine if a log device has valid content. If the vdev was
4604 * removed or faulted in the MOS config then we know that
4605 * the content on the log device has already been written to the pool.
4608 vdev_log_state_valid(vdev_t *vd)
4610 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
4614 for (int c = 0; c < vd->vdev_children; c++)
4615 if (vdev_log_state_valid(vd->vdev_child[c]))
4622 * Expand a vdev if possible.
4625 vdev_expand(vdev_t *vd, uint64_t txg)
4627 ASSERT(vd->vdev_top == vd);
4628 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4629 ASSERT(vdev_is_concrete(vd));
4631 vdev_set_deflate_ratio(vd);
4633 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
4634 vdev_is_concrete(vd)) {
4635 vdev_metaslab_group_create(vd);
4636 VERIFY(vdev_metaslab_init(vd, txg) == 0);
4637 vdev_config_dirty(vd);
4645 vdev_split(vdev_t *vd)
4647 vdev_t *cvd, *pvd = vd->vdev_parent;
4649 vdev_remove_child(pvd, vd);
4650 vdev_compact_children(pvd);
4652 cvd = pvd->vdev_child[0];
4653 if (pvd->vdev_children == 1) {
4654 vdev_remove_parent(cvd);
4655 cvd->vdev_splitting = B_TRUE;
4657 vdev_propagate_state(cvd);
4661 vdev_deadman(vdev_t *vd, char *tag)
4663 for (int c = 0; c < vd->vdev_children; c++) {
4664 vdev_t *cvd = vd->vdev_child[c];
4666 vdev_deadman(cvd, tag);
4669 if (vd->vdev_ops->vdev_op_leaf) {
4670 vdev_queue_t *vq = &vd->vdev_queue;
4672 mutex_enter(&vq->vq_lock);
4673 if (avl_numnodes(&vq->vq_active_tree) > 0) {
4674 spa_t *spa = vd->vdev_spa;
4678 zfs_dbgmsg("slow vdev: %s has %d active IOs",
4679 vd->vdev_path, avl_numnodes(&vq->vq_active_tree));
4682 * Look at the head of all the pending queues,
4683 * if any I/O has been outstanding for longer than
4684 * the spa_deadman_synctime invoke the deadman logic.
4686 fio = avl_first(&vq->vq_active_tree);
4687 delta = gethrtime() - fio->io_timestamp;
4688 if (delta > spa_deadman_synctime(spa))
4689 zio_deadman(fio, tag);
4691 mutex_exit(&vq->vq_lock);
4696 vdev_set_deferred_resilver(spa_t *spa, vdev_t *vd)
4698 for (uint64_t i = 0; i < vd->vdev_children; i++)
4699 vdev_set_deferred_resilver(spa, vd->vdev_child[i]);
4701 if (!vd->vdev_ops->vdev_op_leaf || !vdev_writeable(vd) ||
4702 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
4706 vd->vdev_resilver_deferred = B_TRUE;
4707 spa->spa_resilver_deferred = B_TRUE;
4711 * Translate a logical range to the physical range for the specified vdev_t.
4712 * This function is initially called with a leaf vdev and will walk each
4713 * parent vdev until it reaches a top-level vdev. Once the top-level is
4714 * reached the physical range is initialized and the recursive function
4715 * begins to unwind. As it unwinds it calls the parent's vdev specific
4716 * translation function to do the real conversion.
4719 vdev_xlate(vdev_t *vd, const range_seg_t *logical_rs, range_seg_t *physical_rs)
4722 * Walk up the vdev tree
4724 if (vd != vd->vdev_top) {
4725 vdev_xlate(vd->vdev_parent, logical_rs, physical_rs);
4728 * We've reached the top-level vdev, initialize the
4729 * physical range to the logical range and start to
4732 physical_rs->rs_start = logical_rs->rs_start;
4733 physical_rs->rs_end = logical_rs->rs_end;
4737 vdev_t *pvd = vd->vdev_parent;
4738 ASSERT3P(pvd, !=, NULL);
4739 ASSERT3P(pvd->vdev_ops->vdev_op_xlate, !=, NULL);
4742 * As this recursive function unwinds, translate the logical
4743 * range into its physical components by calling the
4744 * vdev specific translate function.
4746 range_seg_t intermediate = { { { 0, 0 } } };
4747 pvd->vdev_ops->vdev_op_xlate(vd, physical_rs, &intermediate);
4749 physical_rs->rs_start = intermediate.rs_start;
4750 physical_rs->rs_end = intermediate.rs_end;
4753 #if defined(_KERNEL)
4754 EXPORT_SYMBOL(vdev_fault);
4755 EXPORT_SYMBOL(vdev_degrade);
4756 EXPORT_SYMBOL(vdev_online);
4757 EXPORT_SYMBOL(vdev_offline);
4758 EXPORT_SYMBOL(vdev_clear);
4761 module_param(zfs_vdev_default_ms_count, int, 0644);
4762 MODULE_PARM_DESC(zfs_vdev_default_ms_count,
4763 "Target number of metaslabs per top-level vdev");
4765 module_param(zfs_vdev_min_ms_count, int, 0644);
4766 MODULE_PARM_DESC(zfs_vdev_min_ms_count,
4767 "Minimum number of metaslabs per top-level vdev");
4769 module_param(zfs_vdev_ms_count_limit, int, 0644);
4770 MODULE_PARM_DESC(zfs_vdev_ms_count_limit,
4771 "Practical upper limit of total metaslabs per top-level vdev");
4773 module_param(zfs_slow_io_events_per_second, uint, 0644);
4774 MODULE_PARM_DESC(zfs_slow_io_events_per_second,
4775 "Rate limit slow IO (delay) events to this many per second");
4777 module_param(zfs_checksum_events_per_second, uint, 0644);
4778 MODULE_PARM_DESC(zfs_checksum_events_per_second, "Rate limit checksum events "
4779 "to this many checksum errors per second (do not set below zed"
4782 module_param(zfs_scan_ignore_errors, int, 0644);
4783 MODULE_PARM_DESC(zfs_scan_ignore_errors,
4784 "Ignore errors during resilver/scrub");
4786 module_param(vdev_validate_skip, int, 0644);
4787 MODULE_PARM_DESC(vdev_validate_skip,
4788 "Bypass vdev_validate()");
4790 module_param(zfs_nocacheflush, int, 0644);
4791 MODULE_PARM_DESC(zfs_nocacheflush, "Disable cache flushes");