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 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
27 * Copyright (c) 2014 Integros [integros.com]
28 * Copyright 2016 Toomas Soome <tsoome@me.com>
29 * Copyright 2017 Joyent, Inc.
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/trim_map.h>
55 SYSCTL_DECL(_vfs_zfs);
56 SYSCTL_NODE(_vfs_zfs, OID_AUTO, vdev, CTLFLAG_RW, 0, "ZFS VDEV");
59 * Virtual device management.
63 * The limit for ZFS to automatically increase a top-level vdev's ashift
64 * from logical ashift to physical ashift.
66 * Example: one or more 512B emulation child vdevs
67 * child->vdev_ashift = 9 (512 bytes)
68 * child->vdev_physical_ashift = 12 (4096 bytes)
69 * zfs_max_auto_ashift = 11 (2048 bytes)
70 * zfs_min_auto_ashift = 9 (512 bytes)
72 * On pool creation or the addition of a new top-level vdev, ZFS will
73 * increase the ashift of the top-level vdev to 2048 as limited by
74 * zfs_max_auto_ashift.
76 * Example: one or more 512B emulation child vdevs
77 * child->vdev_ashift = 9 (512 bytes)
78 * child->vdev_physical_ashift = 12 (4096 bytes)
79 * zfs_max_auto_ashift = 13 (8192 bytes)
80 * zfs_min_auto_ashift = 9 (512 bytes)
82 * On pool creation or the addition of a new top-level vdev, ZFS will
83 * increase the ashift of the top-level vdev to 4096 to match the
84 * max vdev_physical_ashift.
86 * Example: one or more 512B emulation child vdevs
87 * child->vdev_ashift = 9 (512 bytes)
88 * child->vdev_physical_ashift = 9 (512 bytes)
89 * zfs_max_auto_ashift = 13 (8192 bytes)
90 * zfs_min_auto_ashift = 12 (4096 bytes)
92 * On pool creation or the addition of a new top-level vdev, ZFS will
93 * increase the ashift of the top-level vdev to 4096 to match the
94 * zfs_min_auto_ashift.
96 static uint64_t zfs_max_auto_ashift = SPA_MAXASHIFT;
97 static uint64_t zfs_min_auto_ashift = SPA_MINASHIFT;
100 sysctl_vfs_zfs_max_auto_ashift(SYSCTL_HANDLER_ARGS)
105 val = zfs_max_auto_ashift;
106 err = sysctl_handle_64(oidp, &val, 0, req);
107 if (err != 0 || req->newptr == NULL)
110 if (val > SPA_MAXASHIFT || val < zfs_min_auto_ashift)
113 zfs_max_auto_ashift = val;
117 SYSCTL_PROC(_vfs_zfs, OID_AUTO, max_auto_ashift,
118 CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t),
119 sysctl_vfs_zfs_max_auto_ashift, "QU",
120 "Max ashift used when optimising for logical -> physical sectors size on "
121 "new top-level vdevs.");
124 sysctl_vfs_zfs_min_auto_ashift(SYSCTL_HANDLER_ARGS)
129 val = zfs_min_auto_ashift;
130 err = sysctl_handle_64(oidp, &val, 0, req);
131 if (err != 0 || req->newptr == NULL)
134 if (val < SPA_MINASHIFT || val > zfs_max_auto_ashift)
137 zfs_min_auto_ashift = val;
141 SYSCTL_PROC(_vfs_zfs, OID_AUTO, min_auto_ashift,
142 CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t),
143 sysctl_vfs_zfs_min_auto_ashift, "QU",
144 "Min ashift used when creating new top-level vdevs.");
146 static vdev_ops_t *vdev_ops_table[] = {
165 /* maximum number of metaslabs per top-level vdev */
166 int vdev_max_ms_count = 200;
167 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, max_ms_count, CTLFLAG_RDTUN,
168 &vdev_max_ms_count, 0,
169 "Maximum number of metaslabs per top-level vdev");
171 /* minimum amount of metaslabs per top-level vdev */
172 int vdev_min_ms_count = 16;
173 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, min_ms_count, CTLFLAG_RDTUN,
174 &vdev_min_ms_count, 0,
175 "Minimum number of metaslabs per top-level vdev");
177 /* see comment in vdev_metaslab_set_size() */
178 int vdev_default_ms_shift = 29;
179 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, default_ms_shift, CTLFLAG_RDTUN,
180 &vdev_default_ms_shift, 0,
181 "Shift between vdev size and number of metaslabs");
183 boolean_t vdev_validate_skip = B_FALSE;
186 * Since the DTL space map of a vdev is not expected to have a lot of
187 * entries, we default its block size to 4K.
189 int vdev_dtl_sm_blksz = (1 << 12);
190 SYSCTL_INT(_vfs_zfs, OID_AUTO, dtl_sm_blksz, CTLFLAG_RDTUN,
191 &vdev_dtl_sm_blksz, 0,
192 "Block size for DTL space map. Power of 2 and greater than 4096.");
195 * vdev-wide space maps that have lots of entries written to them at
196 * the end of each transaction can benefit from a higher I/O bandwidth
197 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
199 int vdev_standard_sm_blksz = (1 << 17);
200 SYSCTL_INT(_vfs_zfs, OID_AUTO, standard_sm_blksz, CTLFLAG_RDTUN,
201 &vdev_standard_sm_blksz, 0,
202 "Block size for standard space map. Power of 2 and greater than 4096.");
206 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
212 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
215 if (vd->vdev_path != NULL) {
216 zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
219 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
220 vd->vdev_ops->vdev_op_type,
221 (u_longlong_t)vd->vdev_id,
222 (u_longlong_t)vd->vdev_guid, buf);
227 vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
231 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
232 zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id,
233 vd->vdev_ops->vdev_op_type);
237 switch (vd->vdev_state) {
238 case VDEV_STATE_UNKNOWN:
239 (void) snprintf(state, sizeof (state), "unknown");
241 case VDEV_STATE_CLOSED:
242 (void) snprintf(state, sizeof (state), "closed");
244 case VDEV_STATE_OFFLINE:
245 (void) snprintf(state, sizeof (state), "offline");
247 case VDEV_STATE_REMOVED:
248 (void) snprintf(state, sizeof (state), "removed");
250 case VDEV_STATE_CANT_OPEN:
251 (void) snprintf(state, sizeof (state), "can't open");
253 case VDEV_STATE_FAULTED:
254 (void) snprintf(state, sizeof (state), "faulted");
256 case VDEV_STATE_DEGRADED:
257 (void) snprintf(state, sizeof (state), "degraded");
259 case VDEV_STATE_HEALTHY:
260 (void) snprintf(state, sizeof (state), "healthy");
263 (void) snprintf(state, sizeof (state), "<state %u>",
264 (uint_t)vd->vdev_state);
267 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
268 "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type,
269 vd->vdev_islog ? " (log)" : "",
270 (u_longlong_t)vd->vdev_guid,
271 vd->vdev_path ? vd->vdev_path : "N/A", state);
273 for (uint64_t i = 0; i < vd->vdev_children; i++)
274 vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
278 * Given a vdev type, return the appropriate ops vector.
281 vdev_getops(const char *type)
283 vdev_ops_t *ops, **opspp;
285 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
286 if (strcmp(ops->vdev_op_type, type) == 0)
293 * Default asize function: return the MAX of psize with the asize of
294 * all children. This is what's used by anything other than RAID-Z.
297 vdev_default_asize(vdev_t *vd, uint64_t psize)
299 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
302 for (int c = 0; c < vd->vdev_children; c++) {
303 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
304 asize = MAX(asize, csize);
311 * Get the minimum allocatable size. We define the allocatable size as
312 * the vdev's asize rounded to the nearest metaslab. This allows us to
313 * replace or attach devices which don't have the same physical size but
314 * can still satisfy the same number of allocations.
317 vdev_get_min_asize(vdev_t *vd)
319 vdev_t *pvd = vd->vdev_parent;
322 * If our parent is NULL (inactive spare or cache) or is the root,
323 * just return our own asize.
326 return (vd->vdev_asize);
329 * The top-level vdev just returns the allocatable size rounded
330 * to the nearest metaslab.
332 if (vd == vd->vdev_top)
333 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
336 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
337 * so each child must provide at least 1/Nth of its asize.
339 if (pvd->vdev_ops == &vdev_raidz_ops)
340 return ((pvd->vdev_min_asize + pvd->vdev_children - 1) /
343 return (pvd->vdev_min_asize);
347 vdev_set_min_asize(vdev_t *vd)
349 vd->vdev_min_asize = vdev_get_min_asize(vd);
351 for (int c = 0; c < vd->vdev_children; c++)
352 vdev_set_min_asize(vd->vdev_child[c]);
356 vdev_lookup_top(spa_t *spa, uint64_t vdev)
358 vdev_t *rvd = spa->spa_root_vdev;
360 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
362 if (vdev < rvd->vdev_children) {
363 ASSERT(rvd->vdev_child[vdev] != NULL);
364 return (rvd->vdev_child[vdev]);
371 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
375 if (vd->vdev_guid == guid)
378 for (int c = 0; c < vd->vdev_children; c++)
379 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
387 vdev_count_leaves_impl(vdev_t *vd)
391 if (vd->vdev_ops->vdev_op_leaf)
394 for (int c = 0; c < vd->vdev_children; c++)
395 n += vdev_count_leaves_impl(vd->vdev_child[c]);
401 vdev_count_leaves(spa_t *spa)
403 return (vdev_count_leaves_impl(spa->spa_root_vdev));
407 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
409 size_t oldsize, newsize;
410 uint64_t id = cvd->vdev_id;
412 spa_t *spa = cvd->vdev_spa;
414 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
415 ASSERT(cvd->vdev_parent == NULL);
417 cvd->vdev_parent = pvd;
422 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
424 oldsize = pvd->vdev_children * sizeof (vdev_t *);
425 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
426 newsize = pvd->vdev_children * sizeof (vdev_t *);
428 newchild = kmem_zalloc(newsize, KM_SLEEP);
429 if (pvd->vdev_child != NULL) {
430 bcopy(pvd->vdev_child, newchild, oldsize);
431 kmem_free(pvd->vdev_child, oldsize);
434 pvd->vdev_child = newchild;
435 pvd->vdev_child[id] = cvd;
437 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
438 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
441 * Walk up all ancestors to update guid sum.
443 for (; pvd != NULL; pvd = pvd->vdev_parent)
444 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
448 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
451 uint_t id = cvd->vdev_id;
453 ASSERT(cvd->vdev_parent == pvd);
458 ASSERT(id < pvd->vdev_children);
459 ASSERT(pvd->vdev_child[id] == cvd);
461 pvd->vdev_child[id] = NULL;
462 cvd->vdev_parent = NULL;
464 for (c = 0; c < pvd->vdev_children; c++)
465 if (pvd->vdev_child[c])
468 if (c == pvd->vdev_children) {
469 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
470 pvd->vdev_child = NULL;
471 pvd->vdev_children = 0;
475 * Walk up all ancestors to update guid sum.
477 for (; pvd != NULL; pvd = pvd->vdev_parent)
478 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
482 * Remove any holes in the child array.
485 vdev_compact_children(vdev_t *pvd)
487 vdev_t **newchild, *cvd;
488 int oldc = pvd->vdev_children;
491 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
493 for (int c = newc = 0; c < oldc; c++)
494 if (pvd->vdev_child[c])
497 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
499 for (int c = newc = 0; c < oldc; c++) {
500 if ((cvd = pvd->vdev_child[c]) != NULL) {
501 newchild[newc] = cvd;
502 cvd->vdev_id = newc++;
506 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
507 pvd->vdev_child = newchild;
508 pvd->vdev_children = newc;
512 * Allocate and minimally initialize a vdev_t.
515 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
518 vdev_indirect_config_t *vic;
520 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
521 vic = &vd->vdev_indirect_config;
523 if (spa->spa_root_vdev == NULL) {
524 ASSERT(ops == &vdev_root_ops);
525 spa->spa_root_vdev = vd;
526 spa->spa_load_guid = spa_generate_guid(NULL);
529 if (guid == 0 && ops != &vdev_hole_ops) {
530 if (spa->spa_root_vdev == vd) {
532 * The root vdev's guid will also be the pool guid,
533 * which must be unique among all pools.
535 guid = spa_generate_guid(NULL);
538 * Any other vdev's guid must be unique within the pool.
540 guid = spa_generate_guid(spa);
542 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
547 vd->vdev_guid = guid;
548 vd->vdev_guid_sum = guid;
550 vd->vdev_state = VDEV_STATE_CLOSED;
551 vd->vdev_ishole = (ops == &vdev_hole_ops);
552 vic->vic_prev_indirect_vdev = UINT64_MAX;
554 rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
555 mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
556 vd->vdev_obsolete_segments = range_tree_create(NULL, NULL);
558 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
559 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
560 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
561 mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL);
562 for (int t = 0; t < DTL_TYPES; t++) {
563 vd->vdev_dtl[t] = range_tree_create(NULL, NULL);
565 txg_list_create(&vd->vdev_ms_list, spa,
566 offsetof(struct metaslab, ms_txg_node));
567 txg_list_create(&vd->vdev_dtl_list, spa,
568 offsetof(struct vdev, vdev_dtl_node));
569 vd->vdev_stat.vs_timestamp = gethrtime();
577 * Allocate a new vdev. The 'alloctype' is used to control whether we are
578 * creating a new vdev or loading an existing one - the behavior is slightly
579 * different for each case.
582 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
587 uint64_t guid = 0, islog, nparity;
589 vdev_indirect_config_t *vic;
591 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
593 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
594 return (SET_ERROR(EINVAL));
596 if ((ops = vdev_getops(type)) == NULL)
597 return (SET_ERROR(EINVAL));
600 * If this is a load, get the vdev guid from the nvlist.
601 * Otherwise, vdev_alloc_common() will generate one for us.
603 if (alloctype == VDEV_ALLOC_LOAD) {
606 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
608 return (SET_ERROR(EINVAL));
610 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
611 return (SET_ERROR(EINVAL));
612 } else if (alloctype == VDEV_ALLOC_SPARE) {
613 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
614 return (SET_ERROR(EINVAL));
615 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
616 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
617 return (SET_ERROR(EINVAL));
618 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
619 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
620 return (SET_ERROR(EINVAL));
624 * The first allocated vdev must be of type 'root'.
626 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
627 return (SET_ERROR(EINVAL));
630 * Determine whether we're a log vdev.
633 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
634 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
635 return (SET_ERROR(ENOTSUP));
637 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
638 return (SET_ERROR(ENOTSUP));
641 * Set the nparity property for RAID-Z vdevs.
644 if (ops == &vdev_raidz_ops) {
645 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
647 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
648 return (SET_ERROR(EINVAL));
650 * Previous versions could only support 1 or 2 parity
654 spa_version(spa) < SPA_VERSION_RAIDZ2)
655 return (SET_ERROR(ENOTSUP));
657 spa_version(spa) < SPA_VERSION_RAIDZ3)
658 return (SET_ERROR(ENOTSUP));
661 * We require the parity to be specified for SPAs that
662 * support multiple parity levels.
664 if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
665 return (SET_ERROR(EINVAL));
667 * Otherwise, we default to 1 parity device for RAID-Z.
674 ASSERT(nparity != -1ULL);
676 vd = vdev_alloc_common(spa, id, guid, ops);
677 vic = &vd->vdev_indirect_config;
679 vd->vdev_islog = islog;
680 vd->vdev_nparity = nparity;
682 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
683 vd->vdev_path = spa_strdup(vd->vdev_path);
684 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
685 vd->vdev_devid = spa_strdup(vd->vdev_devid);
686 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
687 &vd->vdev_physpath) == 0)
688 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
689 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
690 vd->vdev_fru = spa_strdup(vd->vdev_fru);
693 * Set the whole_disk property. If it's not specified, leave the value
696 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
697 &vd->vdev_wholedisk) != 0)
698 vd->vdev_wholedisk = -1ULL;
700 ASSERT0(vic->vic_mapping_object);
701 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
702 &vic->vic_mapping_object);
703 ASSERT0(vic->vic_births_object);
704 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
705 &vic->vic_births_object);
706 ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
707 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
708 &vic->vic_prev_indirect_vdev);
711 * Look for the 'not present' flag. This will only be set if the device
712 * was not present at the time of import.
714 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
715 &vd->vdev_not_present);
718 * Get the alignment requirement.
720 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
723 * Retrieve the vdev creation time.
725 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
729 * If we're a top-level vdev, try to load the allocation parameters.
731 if (parent && !parent->vdev_parent &&
732 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
733 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
735 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
737 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
739 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
741 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
744 ASSERT0(vd->vdev_top_zap);
747 if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
748 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
749 alloctype == VDEV_ALLOC_ADD ||
750 alloctype == VDEV_ALLOC_SPLIT ||
751 alloctype == VDEV_ALLOC_ROOTPOOL);
752 vd->vdev_mg = metaslab_group_create(islog ?
753 spa_log_class(spa) : spa_normal_class(spa), vd);
756 if (vd->vdev_ops->vdev_op_leaf &&
757 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
758 (void) nvlist_lookup_uint64(nv,
759 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
761 ASSERT0(vd->vdev_leaf_zap);
765 * If we're a leaf vdev, try to load the DTL object and other state.
768 if (vd->vdev_ops->vdev_op_leaf &&
769 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
770 alloctype == VDEV_ALLOC_ROOTPOOL)) {
771 if (alloctype == VDEV_ALLOC_LOAD) {
772 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
773 &vd->vdev_dtl_object);
774 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
778 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
781 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
782 &spare) == 0 && spare)
786 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
789 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
790 &vd->vdev_resilver_txg);
793 * When importing a pool, we want to ignore the persistent fault
794 * state, as the diagnosis made on another system may not be
795 * valid in the current context. Local vdevs will
796 * remain in the faulted state.
798 if (spa_load_state(spa) == SPA_LOAD_OPEN) {
799 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
801 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
803 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
806 if (vd->vdev_faulted || vd->vdev_degraded) {
810 VDEV_AUX_ERR_EXCEEDED;
811 if (nvlist_lookup_string(nv,
812 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
813 strcmp(aux, "external") == 0)
814 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
820 * Add ourselves to the parent's list of children.
822 vdev_add_child(parent, vd);
830 vdev_free(vdev_t *vd)
832 spa_t *spa = vd->vdev_spa;
835 * vdev_free() implies closing the vdev first. This is simpler than
836 * trying to ensure complicated semantics for all callers.
840 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
841 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
846 for (int c = 0; c < vd->vdev_children; c++)
847 vdev_free(vd->vdev_child[c]);
849 ASSERT(vd->vdev_child == NULL);
850 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
853 * Discard allocation state.
855 if (vd->vdev_mg != NULL) {
856 vdev_metaslab_fini(vd);
857 metaslab_group_destroy(vd->vdev_mg);
860 ASSERT0(vd->vdev_stat.vs_space);
861 ASSERT0(vd->vdev_stat.vs_dspace);
862 ASSERT0(vd->vdev_stat.vs_alloc);
865 * Remove this vdev from its parent's child list.
867 vdev_remove_child(vd->vdev_parent, vd);
869 ASSERT(vd->vdev_parent == NULL);
872 * Clean up vdev structure.
878 spa_strfree(vd->vdev_path);
880 spa_strfree(vd->vdev_devid);
881 if (vd->vdev_physpath)
882 spa_strfree(vd->vdev_physpath);
884 spa_strfree(vd->vdev_fru);
886 if (vd->vdev_isspare)
887 spa_spare_remove(vd);
888 if (vd->vdev_isl2cache)
889 spa_l2cache_remove(vd);
891 txg_list_destroy(&vd->vdev_ms_list);
892 txg_list_destroy(&vd->vdev_dtl_list);
894 mutex_enter(&vd->vdev_dtl_lock);
895 space_map_close(vd->vdev_dtl_sm);
896 for (int t = 0; t < DTL_TYPES; t++) {
897 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
898 range_tree_destroy(vd->vdev_dtl[t]);
900 mutex_exit(&vd->vdev_dtl_lock);
902 EQUIV(vd->vdev_indirect_births != NULL,
903 vd->vdev_indirect_mapping != NULL);
904 if (vd->vdev_indirect_births != NULL) {
905 vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
906 vdev_indirect_births_close(vd->vdev_indirect_births);
909 if (vd->vdev_obsolete_sm != NULL) {
910 ASSERT(vd->vdev_removing ||
911 vd->vdev_ops == &vdev_indirect_ops);
912 space_map_close(vd->vdev_obsolete_sm);
913 vd->vdev_obsolete_sm = NULL;
915 range_tree_destroy(vd->vdev_obsolete_segments);
916 rw_destroy(&vd->vdev_indirect_rwlock);
917 mutex_destroy(&vd->vdev_obsolete_lock);
919 mutex_destroy(&vd->vdev_queue_lock);
920 mutex_destroy(&vd->vdev_dtl_lock);
921 mutex_destroy(&vd->vdev_stat_lock);
922 mutex_destroy(&vd->vdev_probe_lock);
924 if (vd == spa->spa_root_vdev)
925 spa->spa_root_vdev = NULL;
927 kmem_free(vd, sizeof (vdev_t));
931 * Transfer top-level vdev state from svd to tvd.
934 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
936 spa_t *spa = svd->vdev_spa;
941 ASSERT(tvd == tvd->vdev_top);
943 tvd->vdev_ms_array = svd->vdev_ms_array;
944 tvd->vdev_ms_shift = svd->vdev_ms_shift;
945 tvd->vdev_ms_count = svd->vdev_ms_count;
946 tvd->vdev_top_zap = svd->vdev_top_zap;
948 svd->vdev_ms_array = 0;
949 svd->vdev_ms_shift = 0;
950 svd->vdev_ms_count = 0;
951 svd->vdev_top_zap = 0;
954 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
955 tvd->vdev_mg = svd->vdev_mg;
956 tvd->vdev_ms = svd->vdev_ms;
961 if (tvd->vdev_mg != NULL)
962 tvd->vdev_mg->mg_vd = tvd;
964 tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm;
965 svd->vdev_checkpoint_sm = NULL;
967 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
968 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
969 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
971 svd->vdev_stat.vs_alloc = 0;
972 svd->vdev_stat.vs_space = 0;
973 svd->vdev_stat.vs_dspace = 0;
975 for (t = 0; t < TXG_SIZE; t++) {
976 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
977 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
978 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
979 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
980 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
981 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
984 if (list_link_active(&svd->vdev_config_dirty_node)) {
985 vdev_config_clean(svd);
986 vdev_config_dirty(tvd);
989 if (list_link_active(&svd->vdev_state_dirty_node)) {
990 vdev_state_clean(svd);
991 vdev_state_dirty(tvd);
994 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
995 svd->vdev_deflate_ratio = 0;
997 tvd->vdev_islog = svd->vdev_islog;
1002 vdev_top_update(vdev_t *tvd, vdev_t *vd)
1009 for (int c = 0; c < vd->vdev_children; c++)
1010 vdev_top_update(tvd, vd->vdev_child[c]);
1014 * Add a mirror/replacing vdev above an existing vdev.
1017 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
1019 spa_t *spa = cvd->vdev_spa;
1020 vdev_t *pvd = cvd->vdev_parent;
1023 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1025 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
1027 mvd->vdev_asize = cvd->vdev_asize;
1028 mvd->vdev_min_asize = cvd->vdev_min_asize;
1029 mvd->vdev_max_asize = cvd->vdev_max_asize;
1030 mvd->vdev_psize = cvd->vdev_psize;
1031 mvd->vdev_ashift = cvd->vdev_ashift;
1032 mvd->vdev_logical_ashift = cvd->vdev_logical_ashift;
1033 mvd->vdev_physical_ashift = cvd->vdev_physical_ashift;
1034 mvd->vdev_state = cvd->vdev_state;
1035 mvd->vdev_crtxg = cvd->vdev_crtxg;
1037 vdev_remove_child(pvd, cvd);
1038 vdev_add_child(pvd, mvd);
1039 cvd->vdev_id = mvd->vdev_children;
1040 vdev_add_child(mvd, cvd);
1041 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1043 if (mvd == mvd->vdev_top)
1044 vdev_top_transfer(cvd, mvd);
1050 * Remove a 1-way mirror/replacing vdev from the tree.
1053 vdev_remove_parent(vdev_t *cvd)
1055 vdev_t *mvd = cvd->vdev_parent;
1056 vdev_t *pvd = mvd->vdev_parent;
1058 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1060 ASSERT(mvd->vdev_children == 1);
1061 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
1062 mvd->vdev_ops == &vdev_replacing_ops ||
1063 mvd->vdev_ops == &vdev_spare_ops);
1064 cvd->vdev_ashift = mvd->vdev_ashift;
1065 cvd->vdev_logical_ashift = mvd->vdev_logical_ashift;
1066 cvd->vdev_physical_ashift = mvd->vdev_physical_ashift;
1068 vdev_remove_child(mvd, cvd);
1069 vdev_remove_child(pvd, mvd);
1072 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1073 * Otherwise, we could have detached an offline device, and when we
1074 * go to import the pool we'll think we have two top-level vdevs,
1075 * instead of a different version of the same top-level vdev.
1077 if (mvd->vdev_top == mvd) {
1078 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
1079 cvd->vdev_orig_guid = cvd->vdev_guid;
1080 cvd->vdev_guid += guid_delta;
1081 cvd->vdev_guid_sum += guid_delta;
1083 cvd->vdev_id = mvd->vdev_id;
1084 vdev_add_child(pvd, cvd);
1085 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1087 if (cvd == cvd->vdev_top)
1088 vdev_top_transfer(mvd, cvd);
1090 ASSERT(mvd->vdev_children == 0);
1095 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
1097 spa_t *spa = vd->vdev_spa;
1098 objset_t *mos = spa->spa_meta_objset;
1100 uint64_t oldc = vd->vdev_ms_count;
1101 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
1105 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1108 * This vdev is not being allocated from yet or is a hole.
1110 if (vd->vdev_ms_shift == 0)
1113 ASSERT(!vd->vdev_ishole);
1115 ASSERT(oldc <= newc);
1117 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1120 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
1121 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1125 vd->vdev_ms_count = newc;
1127 for (m = oldc; m < newc; m++) {
1128 uint64_t object = 0;
1131 * vdev_ms_array may be 0 if we are creating the "fake"
1132 * metaslabs for an indirect vdev for zdb's leak detection.
1133 * See zdb_leak_init().
1135 if (txg == 0 && vd->vdev_ms_array != 0) {
1136 error = dmu_read(mos, vd->vdev_ms_array,
1137 m * sizeof (uint64_t), sizeof (uint64_t), &object,
1140 vdev_dbgmsg(vd, "unable to read the metaslab "
1141 "array [error=%d]", error);
1146 error = metaslab_init(vd->vdev_mg, m, object, txg,
1149 vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
1156 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1159 * If the vdev is being removed we don't activate
1160 * the metaslabs since we want to ensure that no new
1161 * allocations are performed on this device.
1163 if (oldc == 0 && !vd->vdev_removing)
1164 metaslab_group_activate(vd->vdev_mg);
1167 spa_config_exit(spa, SCL_ALLOC, FTAG);
1173 vdev_metaslab_fini(vdev_t *vd)
1175 if (vd->vdev_checkpoint_sm != NULL) {
1176 ASSERT(spa_feature_is_active(vd->vdev_spa,
1177 SPA_FEATURE_POOL_CHECKPOINT));
1178 space_map_close(vd->vdev_checkpoint_sm);
1180 * Even though we close the space map, we need to set its
1181 * pointer to NULL. The reason is that vdev_metaslab_fini()
1182 * may be called multiple times for certain operations
1183 * (i.e. when destroying a pool) so we need to ensure that
1184 * this clause never executes twice. This logic is similar
1185 * to the one used for the vdev_ms clause below.
1187 vd->vdev_checkpoint_sm = NULL;
1190 if (vd->vdev_ms != NULL) {
1191 uint64_t count = vd->vdev_ms_count;
1193 metaslab_group_passivate(vd->vdev_mg);
1194 for (uint64_t m = 0; m < count; m++) {
1195 metaslab_t *msp = vd->vdev_ms[m];
1200 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1203 vd->vdev_ms_count = 0;
1205 ASSERT0(vd->vdev_ms_count);
1208 typedef struct vdev_probe_stats {
1209 boolean_t vps_readable;
1210 boolean_t vps_writeable;
1212 } vdev_probe_stats_t;
1215 vdev_probe_done(zio_t *zio)
1217 spa_t *spa = zio->io_spa;
1218 vdev_t *vd = zio->io_vd;
1219 vdev_probe_stats_t *vps = zio->io_private;
1221 ASSERT(vd->vdev_probe_zio != NULL);
1223 if (zio->io_type == ZIO_TYPE_READ) {
1224 if (zio->io_error == 0)
1225 vps->vps_readable = 1;
1226 if (zio->io_error == 0 && spa_writeable(spa)) {
1227 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1228 zio->io_offset, zio->io_size, zio->io_abd,
1229 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1230 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1232 abd_free(zio->io_abd);
1234 } else if (zio->io_type == ZIO_TYPE_WRITE) {
1235 if (zio->io_error == 0)
1236 vps->vps_writeable = 1;
1237 abd_free(zio->io_abd);
1238 } else if (zio->io_type == ZIO_TYPE_NULL) {
1241 vd->vdev_cant_read |= !vps->vps_readable;
1242 vd->vdev_cant_write |= !vps->vps_writeable;
1244 if (vdev_readable(vd) &&
1245 (vdev_writeable(vd) || !spa_writeable(spa))) {
1248 ASSERT(zio->io_error != 0);
1249 vdev_dbgmsg(vd, "failed probe");
1250 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
1251 spa, vd, NULL, 0, 0);
1252 zio->io_error = SET_ERROR(ENXIO);
1255 mutex_enter(&vd->vdev_probe_lock);
1256 ASSERT(vd->vdev_probe_zio == zio);
1257 vd->vdev_probe_zio = NULL;
1258 mutex_exit(&vd->vdev_probe_lock);
1260 zio_link_t *zl = NULL;
1261 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1262 if (!vdev_accessible(vd, pio))
1263 pio->io_error = SET_ERROR(ENXIO);
1265 kmem_free(vps, sizeof (*vps));
1270 * Determine whether this device is accessible.
1272 * Read and write to several known locations: the pad regions of each
1273 * vdev label but the first, which we leave alone in case it contains
1277 vdev_probe(vdev_t *vd, zio_t *zio)
1279 spa_t *spa = vd->vdev_spa;
1280 vdev_probe_stats_t *vps = NULL;
1283 ASSERT(vd->vdev_ops->vdev_op_leaf);
1286 * Don't probe the probe.
1288 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1292 * To prevent 'probe storms' when a device fails, we create
1293 * just one probe i/o at a time. All zios that want to probe
1294 * this vdev will become parents of the probe io.
1296 mutex_enter(&vd->vdev_probe_lock);
1298 if ((pio = vd->vdev_probe_zio) == NULL) {
1299 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1301 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1302 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
1305 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1307 * vdev_cant_read and vdev_cant_write can only
1308 * transition from TRUE to FALSE when we have the
1309 * SCL_ZIO lock as writer; otherwise they can only
1310 * transition from FALSE to TRUE. This ensures that
1311 * any zio looking at these values can assume that
1312 * failures persist for the life of the I/O. That's
1313 * important because when a device has intermittent
1314 * connectivity problems, we want to ensure that
1315 * they're ascribed to the device (ENXIO) and not
1318 * Since we hold SCL_ZIO as writer here, clear both
1319 * values so the probe can reevaluate from first
1322 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1323 vd->vdev_cant_read = B_FALSE;
1324 vd->vdev_cant_write = B_FALSE;
1327 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1328 vdev_probe_done, vps,
1329 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1332 * We can't change the vdev state in this context, so we
1333 * kick off an async task to do it on our behalf.
1336 vd->vdev_probe_wanted = B_TRUE;
1337 spa_async_request(spa, SPA_ASYNC_PROBE);
1342 zio_add_child(zio, pio);
1344 mutex_exit(&vd->vdev_probe_lock);
1347 ASSERT(zio != NULL);
1351 for (int l = 1; l < VDEV_LABELS; l++) {
1352 zio_nowait(zio_read_phys(pio, vd,
1353 vdev_label_offset(vd->vdev_psize, l,
1354 offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE,
1355 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1356 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1357 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1368 vdev_open_child(void *arg)
1372 vd->vdev_open_thread = curthread;
1373 vd->vdev_open_error = vdev_open(vd);
1374 vd->vdev_open_thread = NULL;
1378 vdev_uses_zvols(vdev_t *vd)
1380 if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1381 strlen(ZVOL_DIR)) == 0)
1383 for (int c = 0; c < vd->vdev_children; c++)
1384 if (vdev_uses_zvols(vd->vdev_child[c]))
1390 vdev_open_children(vdev_t *vd)
1393 int children = vd->vdev_children;
1396 * in order to handle pools on top of zvols, do the opens
1397 * in a single thread so that the same thread holds the
1398 * spa_namespace_lock
1400 if (B_TRUE || vdev_uses_zvols(vd)) {
1401 for (int c = 0; c < children; c++)
1402 vd->vdev_child[c]->vdev_open_error =
1403 vdev_open(vd->vdev_child[c]);
1406 tq = taskq_create("vdev_open", children, minclsyspri,
1407 children, children, TASKQ_PREPOPULATE);
1409 for (int c = 0; c < children; c++)
1410 VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1417 * Compute the raidz-deflation ratio. Note, we hard-code
1418 * in 128k (1 << 17) because it is the "typical" blocksize.
1419 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1420 * otherwise it would inconsistently account for existing bp's.
1423 vdev_set_deflate_ratio(vdev_t *vd)
1425 if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
1426 vd->vdev_deflate_ratio = (1 << 17) /
1427 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
1432 * Prepare a virtual device for access.
1435 vdev_open(vdev_t *vd)
1437 spa_t *spa = vd->vdev_spa;
1440 uint64_t max_osize = 0;
1441 uint64_t asize, max_asize, psize;
1442 uint64_t logical_ashift = 0;
1443 uint64_t physical_ashift = 0;
1445 ASSERT(vd->vdev_open_thread == curthread ||
1446 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1447 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1448 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1449 vd->vdev_state == VDEV_STATE_OFFLINE);
1451 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1452 vd->vdev_cant_read = B_FALSE;
1453 vd->vdev_cant_write = B_FALSE;
1454 vd->vdev_notrim = B_FALSE;
1455 vd->vdev_min_asize = vdev_get_min_asize(vd);
1458 * If this vdev is not removed, check its fault status. If it's
1459 * faulted, bail out of the open.
1461 if (!vd->vdev_removed && vd->vdev_faulted) {
1462 ASSERT(vd->vdev_children == 0);
1463 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1464 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1465 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1466 vd->vdev_label_aux);
1467 return (SET_ERROR(ENXIO));
1468 } else if (vd->vdev_offline) {
1469 ASSERT(vd->vdev_children == 0);
1470 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1471 return (SET_ERROR(ENXIO));
1474 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize,
1475 &logical_ashift, &physical_ashift);
1478 * Reset the vdev_reopening flag so that we actually close
1479 * the vdev on error.
1481 vd->vdev_reopening = B_FALSE;
1482 if (zio_injection_enabled && error == 0)
1483 error = zio_handle_device_injection(vd, NULL, ENXIO);
1486 if (vd->vdev_removed &&
1487 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
1488 vd->vdev_removed = B_FALSE;
1490 if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
1491 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
1492 vd->vdev_stat.vs_aux);
1494 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1495 vd->vdev_stat.vs_aux);
1500 vd->vdev_removed = B_FALSE;
1503 * Recheck the faulted flag now that we have confirmed that
1504 * the vdev is accessible. If we're faulted, bail.
1506 if (vd->vdev_faulted) {
1507 ASSERT(vd->vdev_children == 0);
1508 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1509 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1510 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1511 vd->vdev_label_aux);
1512 return (SET_ERROR(ENXIO));
1515 if (vd->vdev_degraded) {
1516 ASSERT(vd->vdev_children == 0);
1517 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1518 VDEV_AUX_ERR_EXCEEDED);
1520 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1524 * For hole or missing vdevs we just return success.
1526 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
1529 if (zfs_trim_enabled && !vd->vdev_notrim && vd->vdev_ops->vdev_op_leaf)
1530 trim_map_create(vd);
1532 for (int c = 0; c < vd->vdev_children; c++) {
1533 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1534 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1540 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1541 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1543 if (vd->vdev_children == 0) {
1544 if (osize < SPA_MINDEVSIZE) {
1545 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1546 VDEV_AUX_TOO_SMALL);
1547 return (SET_ERROR(EOVERFLOW));
1550 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1551 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
1552 VDEV_LABEL_END_SIZE);
1554 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1555 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1556 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1557 VDEV_AUX_TOO_SMALL);
1558 return (SET_ERROR(EOVERFLOW));
1562 max_asize = max_osize;
1565 vd->vdev_psize = psize;
1568 * Make sure the allocatable size hasn't shrunk too much.
1570 if (asize < vd->vdev_min_asize) {
1571 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1572 VDEV_AUX_BAD_LABEL);
1573 return (SET_ERROR(EINVAL));
1576 vd->vdev_physical_ashift =
1577 MAX(physical_ashift, vd->vdev_physical_ashift);
1578 vd->vdev_logical_ashift = MAX(logical_ashift, vd->vdev_logical_ashift);
1579 vd->vdev_ashift = MAX(vd->vdev_logical_ashift, vd->vdev_ashift);
1581 if (vd->vdev_logical_ashift > SPA_MAXASHIFT) {
1582 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1583 VDEV_AUX_ASHIFT_TOO_BIG);
1587 if (vd->vdev_asize == 0) {
1589 * This is the first-ever open, so use the computed values.
1590 * For testing purposes, a higher ashift can be requested.
1592 vd->vdev_asize = asize;
1593 vd->vdev_max_asize = max_asize;
1596 * Make sure the alignment requirement hasn't increased.
1598 if (vd->vdev_ashift > vd->vdev_top->vdev_ashift &&
1599 vd->vdev_ops->vdev_op_leaf) {
1600 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1601 VDEV_AUX_BAD_LABEL);
1604 vd->vdev_max_asize = max_asize;
1608 * If all children are healthy we update asize if either:
1609 * The asize has increased, due to a device expansion caused by dynamic
1610 * LUN growth or vdev replacement, and automatic expansion is enabled;
1611 * making the additional space available.
1613 * The asize has decreased, due to a device shrink usually caused by a
1614 * vdev replace with a smaller device. This ensures that calculations
1615 * based of max_asize and asize e.g. esize are always valid. It's safe
1616 * to do this as we've already validated that asize is greater than
1619 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1620 ((asize > vd->vdev_asize &&
1621 (vd->vdev_expanding || spa->spa_autoexpand)) ||
1622 (asize < vd->vdev_asize)))
1623 vd->vdev_asize = asize;
1625 vdev_set_min_asize(vd);
1628 * Ensure we can issue some IO before declaring the
1629 * vdev open for business.
1631 if (vd->vdev_ops->vdev_op_leaf &&
1632 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1633 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1634 VDEV_AUX_ERR_EXCEEDED);
1639 * Track the min and max ashift values for normal data devices.
1641 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1642 !vd->vdev_islog && vd->vdev_aux == NULL) {
1643 if (vd->vdev_ashift > spa->spa_max_ashift)
1644 spa->spa_max_ashift = vd->vdev_ashift;
1645 if (vd->vdev_ashift < spa->spa_min_ashift)
1646 spa->spa_min_ashift = vd->vdev_ashift;
1650 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1651 * resilver. But don't do this if we are doing a reopen for a scrub,
1652 * since this would just restart the scrub we are already doing.
1654 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
1655 vdev_resilver_needed(vd, NULL, NULL))
1656 spa_async_request(spa, SPA_ASYNC_RESILVER);
1662 * Called once the vdevs are all opened, this routine validates the label
1663 * contents. This needs to be done before vdev_load() so that we don't
1664 * inadvertently do repair I/Os to the wrong device.
1666 * This function will only return failure if one of the vdevs indicates that it
1667 * has since been destroyed or exported. This is only possible if
1668 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1669 * will be updated but the function will return 0.
1672 vdev_validate(vdev_t *vd)
1674 spa_t *spa = vd->vdev_spa;
1676 uint64_t guid = 0, aux_guid = 0, top_guid;
1681 if (vdev_validate_skip)
1684 for (uint64_t c = 0; c < vd->vdev_children; c++)
1685 if (vdev_validate(vd->vdev_child[c]) != 0)
1686 return (SET_ERROR(EBADF));
1689 * If the device has already failed, or was marked offline, don't do
1690 * any further validation. Otherwise, label I/O will fail and we will
1691 * overwrite the previous state.
1693 if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
1697 * If we are performing an extreme rewind, we allow for a label that
1698 * was modified at a point after the current txg.
1699 * If config lock is not held do not check for the txg. spa_sync could
1700 * be updating the vdev's label before updating spa_last_synced_txg.
1702 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 ||
1703 spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG)
1706 txg = spa_last_synced_txg(spa);
1708 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1709 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1710 VDEV_AUX_BAD_LABEL);
1711 vdev_dbgmsg(vd, "vdev_validate: failed reading config");
1716 * Determine if this vdev has been split off into another
1717 * pool. If so, then refuse to open it.
1719 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
1720 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
1721 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1722 VDEV_AUX_SPLIT_POOL);
1724 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
1728 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
1729 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1730 VDEV_AUX_CORRUPT_DATA);
1732 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1733 ZPOOL_CONFIG_POOL_GUID);
1738 * If config is not trusted then ignore the spa guid check. This is
1739 * necessary because if the machine crashed during a re-guid the new
1740 * guid might have been written to all of the vdev labels, but not the
1741 * cached config. The check will be performed again once we have the
1742 * trusted config from the MOS.
1744 if (spa->spa_trust_config && guid != spa_guid(spa)) {
1745 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1746 VDEV_AUX_CORRUPT_DATA);
1748 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
1749 "match config (%llu != %llu)", (u_longlong_t)guid,
1750 (u_longlong_t)spa_guid(spa));
1754 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
1755 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
1759 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
1760 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1761 VDEV_AUX_CORRUPT_DATA);
1763 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1768 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
1770 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1771 VDEV_AUX_CORRUPT_DATA);
1773 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1774 ZPOOL_CONFIG_TOP_GUID);
1779 * If this vdev just became a top-level vdev because its sibling was
1780 * detached, it will have adopted the parent's vdev guid -- but the
1781 * label may or may not be on disk yet. Fortunately, either version
1782 * of the label will have the same top guid, so if we're a top-level
1783 * vdev, we can safely compare to that instead.
1784 * However, if the config comes from a cachefile that failed to update
1785 * after the detach, a top-level vdev will appear as a non top-level
1786 * vdev in the config. Also relax the constraints if we perform an
1789 * If we split this vdev off instead, then we also check the
1790 * original pool's guid. We don't want to consider the vdev
1791 * corrupt if it is partway through a split operation.
1793 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
1794 boolean_t mismatch = B_FALSE;
1795 if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
1796 if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
1799 if (vd->vdev_guid != top_guid &&
1800 vd->vdev_top->vdev_guid != guid)
1805 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1806 VDEV_AUX_CORRUPT_DATA);
1808 vdev_dbgmsg(vd, "vdev_validate: config guid "
1809 "doesn't match label guid");
1810 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
1811 (u_longlong_t)vd->vdev_guid,
1812 (u_longlong_t)vd->vdev_top->vdev_guid);
1813 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
1814 "aux_guid %llu", (u_longlong_t)guid,
1815 (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
1820 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1822 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1823 VDEV_AUX_CORRUPT_DATA);
1825 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1826 ZPOOL_CONFIG_POOL_STATE);
1833 * If this is a verbatim import, no need to check the
1834 * state of the pool.
1836 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1837 spa_load_state(spa) == SPA_LOAD_OPEN &&
1838 state != POOL_STATE_ACTIVE) {
1839 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
1840 "for spa %s", (u_longlong_t)state, spa->spa_name);
1841 return (SET_ERROR(EBADF));
1845 * If we were able to open and validate a vdev that was
1846 * previously marked permanently unavailable, clear that state
1849 if (vd->vdev_not_present)
1850 vd->vdev_not_present = 0;
1856 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
1858 if (svd->vdev_path != NULL && dvd->vdev_path != NULL) {
1859 if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) {
1860 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
1861 "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
1862 dvd->vdev_path, svd->vdev_path);
1863 spa_strfree(dvd->vdev_path);
1864 dvd->vdev_path = spa_strdup(svd->vdev_path);
1866 } else if (svd->vdev_path != NULL) {
1867 dvd->vdev_path = spa_strdup(svd->vdev_path);
1868 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
1869 (u_longlong_t)dvd->vdev_guid, dvd->vdev_path);
1874 * Recursively copy vdev paths from one vdev to another. Source and destination
1875 * vdev trees must have same geometry otherwise return error. Intended to copy
1876 * paths from userland config into MOS config.
1879 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
1881 if ((svd->vdev_ops == &vdev_missing_ops) ||
1882 (svd->vdev_ishole && dvd->vdev_ishole) ||
1883 (dvd->vdev_ops == &vdev_indirect_ops))
1886 if (svd->vdev_ops != dvd->vdev_ops) {
1887 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
1888 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
1889 return (SET_ERROR(EINVAL));
1892 if (svd->vdev_guid != dvd->vdev_guid) {
1893 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
1894 "%llu)", (u_longlong_t)svd->vdev_guid,
1895 (u_longlong_t)dvd->vdev_guid);
1896 return (SET_ERROR(EINVAL));
1899 if (svd->vdev_children != dvd->vdev_children) {
1900 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
1901 "%llu != %llu", (u_longlong_t)svd->vdev_children,
1902 (u_longlong_t)dvd->vdev_children);
1903 return (SET_ERROR(EINVAL));
1906 for (uint64_t i = 0; i < svd->vdev_children; i++) {
1907 int error = vdev_copy_path_strict(svd->vdev_child[i],
1908 dvd->vdev_child[i]);
1913 if (svd->vdev_ops->vdev_op_leaf)
1914 vdev_copy_path_impl(svd, dvd);
1920 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
1922 ASSERT(stvd->vdev_top == stvd);
1923 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
1925 for (uint64_t i = 0; i < dvd->vdev_children; i++) {
1926 vdev_copy_path_search(stvd, dvd->vdev_child[i]);
1929 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
1933 * The idea here is that while a vdev can shift positions within
1934 * a top vdev (when replacing, attaching mirror, etc.) it cannot
1935 * step outside of it.
1937 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
1939 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
1942 ASSERT(vd->vdev_ops->vdev_op_leaf);
1944 vdev_copy_path_impl(vd, dvd);
1948 * Recursively copy vdev paths from one root vdev to another. Source and
1949 * destination vdev trees may differ in geometry. For each destination leaf
1950 * vdev, search a vdev with the same guid and top vdev id in the source.
1951 * Intended to copy paths from userland config into MOS config.
1954 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
1956 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
1957 ASSERT(srvd->vdev_ops == &vdev_root_ops);
1958 ASSERT(drvd->vdev_ops == &vdev_root_ops);
1960 for (uint64_t i = 0; i < children; i++) {
1961 vdev_copy_path_search(srvd->vdev_child[i],
1962 drvd->vdev_child[i]);
1967 * Close a virtual device.
1970 vdev_close(vdev_t *vd)
1972 spa_t *spa = vd->vdev_spa;
1973 vdev_t *pvd = vd->vdev_parent;
1975 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1978 * If our parent is reopening, then we are as well, unless we are
1981 if (pvd != NULL && pvd->vdev_reopening)
1982 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1984 vd->vdev_ops->vdev_op_close(vd);
1986 vdev_cache_purge(vd);
1988 if (vd->vdev_ops->vdev_op_leaf)
1989 trim_map_destroy(vd);
1992 * We record the previous state before we close it, so that if we are
1993 * doing a reopen(), we don't generate FMA ereports if we notice that
1994 * it's still faulted.
1996 vd->vdev_prevstate = vd->vdev_state;
1998 if (vd->vdev_offline)
1999 vd->vdev_state = VDEV_STATE_OFFLINE;
2001 vd->vdev_state = VDEV_STATE_CLOSED;
2002 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2006 vdev_hold(vdev_t *vd)
2008 spa_t *spa = vd->vdev_spa;
2010 ASSERT(spa_is_root(spa));
2011 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
2014 for (int c = 0; c < vd->vdev_children; c++)
2015 vdev_hold(vd->vdev_child[c]);
2017 if (vd->vdev_ops->vdev_op_leaf)
2018 vd->vdev_ops->vdev_op_hold(vd);
2022 vdev_rele(vdev_t *vd)
2024 spa_t *spa = vd->vdev_spa;
2026 ASSERT(spa_is_root(spa));
2027 for (int c = 0; c < vd->vdev_children; c++)
2028 vdev_rele(vd->vdev_child[c]);
2030 if (vd->vdev_ops->vdev_op_leaf)
2031 vd->vdev_ops->vdev_op_rele(vd);
2035 * Reopen all interior vdevs and any unopened leaves. We don't actually
2036 * reopen leaf vdevs which had previously been opened as they might deadlock
2037 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2038 * If the leaf has never been opened then open it, as usual.
2041 vdev_reopen(vdev_t *vd)
2043 spa_t *spa = vd->vdev_spa;
2045 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2047 /* set the reopening flag unless we're taking the vdev offline */
2048 vd->vdev_reopening = !vd->vdev_offline;
2050 (void) vdev_open(vd);
2053 * Call vdev_validate() here to make sure we have the same device.
2054 * Otherwise, a device with an invalid label could be successfully
2055 * opened in response to vdev_reopen().
2058 (void) vdev_validate_aux(vd);
2059 if (vdev_readable(vd) && vdev_writeable(vd) &&
2060 vd->vdev_aux == &spa->spa_l2cache &&
2061 !l2arc_vdev_present(vd))
2062 l2arc_add_vdev(spa, vd);
2064 (void) vdev_validate(vd);
2068 * Reassess parent vdev's health.
2070 vdev_propagate_state(vd);
2074 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
2079 * Normally, partial opens (e.g. of a mirror) are allowed.
2080 * For a create, however, we want to fail the request if
2081 * there are any components we can't open.
2083 error = vdev_open(vd);
2085 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
2087 return (error ? error : ENXIO);
2091 * Recursively load DTLs and initialize all labels.
2093 if ((error = vdev_dtl_load(vd)) != 0 ||
2094 (error = vdev_label_init(vd, txg, isreplacing ?
2095 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
2104 vdev_metaslab_set_size(vdev_t *vd)
2106 uint64_t asize = vd->vdev_asize;
2107 uint64_t ms_shift = 0;
2110 * For vdevs that are bigger than 8G the metaslab size varies in
2111 * a way that the number of metaslabs increases in powers of two,
2112 * linearly in terms of vdev_asize, starting from 16 metaslabs.
2113 * So for vdev_asize of 8G we get 16 metaslabs, for 16G, we get 32,
2114 * and so on, until we hit the maximum metaslab count limit
2115 * [vdev_max_ms_count] from which point the metaslab count stays
2118 ms_shift = vdev_default_ms_shift;
2120 if ((asize >> ms_shift) < vdev_min_ms_count) {
2122 * For devices that are less than 8G we want to have
2123 * exactly 16 metaslabs. We don't want less as integer
2124 * division rounds down, so less metaslabs mean more
2125 * wasted space. We don't want more as these vdevs are
2126 * small and in the likely event that we are running
2127 * out of space, the SPA will have a hard time finding
2128 * space due to fragmentation.
2130 ms_shift = highbit64(asize / vdev_min_ms_count);
2131 ms_shift = MAX(ms_shift, SPA_MAXBLOCKSHIFT);
2133 } else if ((asize >> ms_shift) > vdev_max_ms_count) {
2134 ms_shift = highbit64(asize / vdev_max_ms_count);
2137 vd->vdev_ms_shift = ms_shift;
2138 ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT);
2142 * Maximize performance by inflating the configured ashift for top level
2143 * vdevs to be as close to the physical ashift as possible while maintaining
2144 * administrator defined limits and ensuring it doesn't go below the
2148 vdev_ashift_optimize(vdev_t *vd)
2150 if (vd == vd->vdev_top) {
2151 if (vd->vdev_ashift < vd->vdev_physical_ashift) {
2152 vd->vdev_ashift = MIN(
2153 MAX(zfs_max_auto_ashift, vd->vdev_ashift),
2154 MAX(zfs_min_auto_ashift, vd->vdev_physical_ashift));
2157 * Unusual case where logical ashift > physical ashift
2158 * so we can't cap the calculated ashift based on max
2159 * ashift as that would cause failures.
2160 * We still check if we need to increase it to match
2163 vd->vdev_ashift = MAX(zfs_min_auto_ashift,
2170 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2172 ASSERT(vd == vd->vdev_top);
2173 /* indirect vdevs don't have metaslabs or dtls */
2174 ASSERT(vdev_is_concrete(vd) || flags == 0);
2175 ASSERT(ISP2(flags));
2176 ASSERT(spa_writeable(vd->vdev_spa));
2178 if (flags & VDD_METASLAB)
2179 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2181 if (flags & VDD_DTL)
2182 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2184 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2188 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
2190 for (int c = 0; c < vd->vdev_children; c++)
2191 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
2193 if (vd->vdev_ops->vdev_op_leaf)
2194 vdev_dirty(vd->vdev_top, flags, vd, txg);
2200 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2201 * the vdev has less than perfect replication. There are four kinds of DTL:
2203 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2205 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2207 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2208 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2209 * txgs that was scrubbed.
2211 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2212 * persistent errors or just some device being offline.
2213 * Unlike the other three, the DTL_OUTAGE map is not generally
2214 * maintained; it's only computed when needed, typically to
2215 * determine whether a device can be detached.
2217 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2218 * either has the data or it doesn't.
2220 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2221 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2222 * if any child is less than fully replicated, then so is its parent.
2223 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2224 * comprising only those txgs which appear in 'maxfaults' or more children;
2225 * those are the txgs we don't have enough replication to read. For example,
2226 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2227 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2228 * two child DTL_MISSING maps.
2230 * It should be clear from the above that to compute the DTLs and outage maps
2231 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2232 * Therefore, that is all we keep on disk. When loading the pool, or after
2233 * a configuration change, we generate all other DTLs from first principles.
2236 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2238 range_tree_t *rt = vd->vdev_dtl[t];
2240 ASSERT(t < DTL_TYPES);
2241 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2242 ASSERT(spa_writeable(vd->vdev_spa));
2244 mutex_enter(&vd->vdev_dtl_lock);
2245 if (!range_tree_contains(rt, txg, size))
2246 range_tree_add(rt, txg, size);
2247 mutex_exit(&vd->vdev_dtl_lock);
2251 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2253 range_tree_t *rt = vd->vdev_dtl[t];
2254 boolean_t dirty = B_FALSE;
2256 ASSERT(t < DTL_TYPES);
2257 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2260 * While we are loading the pool, the DTLs have not been loaded yet.
2261 * Ignore the DTLs and try all devices. This avoids a recursive
2262 * mutex enter on the vdev_dtl_lock, and also makes us try hard
2263 * when loading the pool (relying on the checksum to ensure that
2264 * we get the right data -- note that we while loading, we are
2265 * only reading the MOS, which is always checksummed).
2267 if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE)
2270 mutex_enter(&vd->vdev_dtl_lock);
2271 if (!range_tree_is_empty(rt))
2272 dirty = range_tree_contains(rt, txg, size);
2273 mutex_exit(&vd->vdev_dtl_lock);
2279 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
2281 range_tree_t *rt = vd->vdev_dtl[t];
2284 mutex_enter(&vd->vdev_dtl_lock);
2285 empty = range_tree_is_empty(rt);
2286 mutex_exit(&vd->vdev_dtl_lock);
2292 * Returns the lowest txg in the DTL range.
2295 vdev_dtl_min(vdev_t *vd)
2299 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2300 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2301 ASSERT0(vd->vdev_children);
2303 rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
2304 return (rs->rs_start - 1);
2308 * Returns the highest txg in the DTL.
2311 vdev_dtl_max(vdev_t *vd)
2315 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2316 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2317 ASSERT0(vd->vdev_children);
2319 rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
2320 return (rs->rs_end);
2324 * Determine if a resilvering vdev should remove any DTL entries from
2325 * its range. If the vdev was resilvering for the entire duration of the
2326 * scan then it should excise that range from its DTLs. Otherwise, this
2327 * vdev is considered partially resilvered and should leave its DTL
2328 * entries intact. The comment in vdev_dtl_reassess() describes how we
2332 vdev_dtl_should_excise(vdev_t *vd)
2334 spa_t *spa = vd->vdev_spa;
2335 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2337 ASSERT0(scn->scn_phys.scn_errors);
2338 ASSERT0(vd->vdev_children);
2340 if (vd->vdev_state < VDEV_STATE_DEGRADED)
2343 if (vd->vdev_resilver_txg == 0 ||
2344 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]))
2348 * When a resilver is initiated the scan will assign the scn_max_txg
2349 * value to the highest txg value that exists in all DTLs. If this
2350 * device's max DTL is not part of this scan (i.e. it is not in
2351 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2354 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
2355 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
2356 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
2357 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
2364 * Reassess DTLs after a config change or scrub completion.
2367 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
2369 spa_t *spa = vd->vdev_spa;
2373 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2375 for (int c = 0; c < vd->vdev_children; c++)
2376 vdev_dtl_reassess(vd->vdev_child[c], txg,
2377 scrub_txg, scrub_done);
2379 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
2382 if (vd->vdev_ops->vdev_op_leaf) {
2383 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2385 mutex_enter(&vd->vdev_dtl_lock);
2388 * If we've completed a scan cleanly then determine
2389 * if this vdev should remove any DTLs. We only want to
2390 * excise regions on vdevs that were available during
2391 * the entire duration of this scan.
2393 if (scrub_txg != 0 &&
2394 (spa->spa_scrub_started ||
2395 (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
2396 vdev_dtl_should_excise(vd)) {
2398 * We completed a scrub up to scrub_txg. If we
2399 * did it without rebooting, then the scrub dtl
2400 * will be valid, so excise the old region and
2401 * fold in the scrub dtl. Otherwise, leave the
2402 * dtl as-is if there was an error.
2404 * There's little trick here: to excise the beginning
2405 * of the DTL_MISSING map, we put it into a reference
2406 * tree and then add a segment with refcnt -1 that
2407 * covers the range [0, scrub_txg). This means
2408 * that each txg in that range has refcnt -1 or 0.
2409 * We then add DTL_SCRUB with a refcnt of 2, so that
2410 * entries in the range [0, scrub_txg) will have a
2411 * positive refcnt -- either 1 or 2. We then convert
2412 * the reference tree into the new DTL_MISSING map.
2414 space_reftree_create(&reftree);
2415 space_reftree_add_map(&reftree,
2416 vd->vdev_dtl[DTL_MISSING], 1);
2417 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
2418 space_reftree_add_map(&reftree,
2419 vd->vdev_dtl[DTL_SCRUB], 2);
2420 space_reftree_generate_map(&reftree,
2421 vd->vdev_dtl[DTL_MISSING], 1);
2422 space_reftree_destroy(&reftree);
2424 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
2425 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
2426 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
2428 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
2429 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
2430 if (!vdev_readable(vd))
2431 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
2433 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
2434 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
2437 * If the vdev was resilvering and no longer has any
2438 * DTLs then reset its resilvering flag and dirty
2439 * the top level so that we persist the change.
2441 if (vd->vdev_resilver_txg != 0 &&
2442 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
2443 range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) {
2444 vd->vdev_resilver_txg = 0;
2445 vdev_config_dirty(vd->vdev_top);
2448 mutex_exit(&vd->vdev_dtl_lock);
2451 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2455 mutex_enter(&vd->vdev_dtl_lock);
2456 for (int t = 0; t < DTL_TYPES; t++) {
2457 /* account for child's outage in parent's missing map */
2458 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
2460 continue; /* leaf vdevs only */
2461 if (t == DTL_PARTIAL)
2462 minref = 1; /* i.e. non-zero */
2463 else if (vd->vdev_nparity != 0)
2464 minref = vd->vdev_nparity + 1; /* RAID-Z */
2466 minref = vd->vdev_children; /* any kind of mirror */
2467 space_reftree_create(&reftree);
2468 for (int c = 0; c < vd->vdev_children; c++) {
2469 vdev_t *cvd = vd->vdev_child[c];
2470 mutex_enter(&cvd->vdev_dtl_lock);
2471 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
2472 mutex_exit(&cvd->vdev_dtl_lock);
2474 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
2475 space_reftree_destroy(&reftree);
2477 mutex_exit(&vd->vdev_dtl_lock);
2481 vdev_dtl_load(vdev_t *vd)
2483 spa_t *spa = vd->vdev_spa;
2484 objset_t *mos = spa->spa_meta_objset;
2487 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
2488 ASSERT(vdev_is_concrete(vd));
2490 error = space_map_open(&vd->vdev_dtl_sm, mos,
2491 vd->vdev_dtl_object, 0, -1ULL, 0);
2494 ASSERT(vd->vdev_dtl_sm != NULL);
2496 mutex_enter(&vd->vdev_dtl_lock);
2499 * Now that we've opened the space_map we need to update
2502 space_map_update(vd->vdev_dtl_sm);
2504 error = space_map_load(vd->vdev_dtl_sm,
2505 vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
2506 mutex_exit(&vd->vdev_dtl_lock);
2511 for (int c = 0; c < vd->vdev_children; c++) {
2512 error = vdev_dtl_load(vd->vdev_child[c]);
2521 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
2523 spa_t *spa = vd->vdev_spa;
2525 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
2526 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2531 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
2533 spa_t *spa = vd->vdev_spa;
2534 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
2535 DMU_OT_NONE, 0, tx);
2538 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2545 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
2547 if (vd->vdev_ops != &vdev_hole_ops &&
2548 vd->vdev_ops != &vdev_missing_ops &&
2549 vd->vdev_ops != &vdev_root_ops &&
2550 !vd->vdev_top->vdev_removing) {
2551 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
2552 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
2554 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
2555 vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
2558 for (uint64_t i = 0; i < vd->vdev_children; i++) {
2559 vdev_construct_zaps(vd->vdev_child[i], tx);
2564 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
2566 spa_t *spa = vd->vdev_spa;
2567 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2568 objset_t *mos = spa->spa_meta_objset;
2569 range_tree_t *rtsync;
2571 uint64_t object = space_map_object(vd->vdev_dtl_sm);
2573 ASSERT(vdev_is_concrete(vd));
2574 ASSERT(vd->vdev_ops->vdev_op_leaf);
2576 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2578 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
2579 mutex_enter(&vd->vdev_dtl_lock);
2580 space_map_free(vd->vdev_dtl_sm, tx);
2581 space_map_close(vd->vdev_dtl_sm);
2582 vd->vdev_dtl_sm = NULL;
2583 mutex_exit(&vd->vdev_dtl_lock);
2586 * We only destroy the leaf ZAP for detached leaves or for
2587 * removed log devices. Removed data devices handle leaf ZAP
2588 * cleanup later, once cancellation is no longer possible.
2590 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2591 vd->vdev_top->vdev_islog)) {
2592 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2593 vd->vdev_leaf_zap = 0;
2600 if (vd->vdev_dtl_sm == NULL) {
2601 uint64_t new_object;
2603 new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx);
2604 VERIFY3U(new_object, !=, 0);
2606 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
2608 ASSERT(vd->vdev_dtl_sm != NULL);
2611 rtsync = range_tree_create(NULL, NULL);
2613 mutex_enter(&vd->vdev_dtl_lock);
2614 range_tree_walk(rt, range_tree_add, rtsync);
2615 mutex_exit(&vd->vdev_dtl_lock);
2617 space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx);
2618 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
2619 range_tree_vacate(rtsync, NULL, NULL);
2621 range_tree_destroy(rtsync);
2624 * If the object for the space map has changed then dirty
2625 * the top level so that we update the config.
2627 if (object != space_map_object(vd->vdev_dtl_sm)) {
2628 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
2629 "new object %llu", (u_longlong_t)txg, spa_name(spa),
2630 (u_longlong_t)object,
2631 (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
2632 vdev_config_dirty(vd->vdev_top);
2637 mutex_enter(&vd->vdev_dtl_lock);
2638 space_map_update(vd->vdev_dtl_sm);
2639 mutex_exit(&vd->vdev_dtl_lock);
2643 * Determine whether the specified vdev can be offlined/detached/removed
2644 * without losing data.
2647 vdev_dtl_required(vdev_t *vd)
2649 spa_t *spa = vd->vdev_spa;
2650 vdev_t *tvd = vd->vdev_top;
2651 uint8_t cant_read = vd->vdev_cant_read;
2654 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2656 if (vd == spa->spa_root_vdev || vd == tvd)
2660 * Temporarily mark the device as unreadable, and then determine
2661 * whether this results in any DTL outages in the top-level vdev.
2662 * If not, we can safely offline/detach/remove the device.
2664 vd->vdev_cant_read = B_TRUE;
2665 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2666 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
2667 vd->vdev_cant_read = cant_read;
2668 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2670 if (!required && zio_injection_enabled)
2671 required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2677 * Determine if resilver is needed, and if so the txg range.
2680 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2682 boolean_t needed = B_FALSE;
2683 uint64_t thismin = UINT64_MAX;
2684 uint64_t thismax = 0;
2686 if (vd->vdev_children == 0) {
2687 mutex_enter(&vd->vdev_dtl_lock);
2688 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
2689 vdev_writeable(vd)) {
2691 thismin = vdev_dtl_min(vd);
2692 thismax = vdev_dtl_max(vd);
2695 mutex_exit(&vd->vdev_dtl_lock);
2697 for (int c = 0; c < vd->vdev_children; c++) {
2698 vdev_t *cvd = vd->vdev_child[c];
2699 uint64_t cmin, cmax;
2701 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2702 thismin = MIN(thismin, cmin);
2703 thismax = MAX(thismax, cmax);
2709 if (needed && minp) {
2717 * Gets the checkpoint space map object from the vdev's ZAP.
2718 * Returns the spacemap object, or 0 if it wasn't in the ZAP
2719 * or the ZAP doesn't exist yet.
2722 vdev_checkpoint_sm_object(vdev_t *vd)
2724 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
2725 if (vd->vdev_top_zap == 0) {
2729 uint64_t sm_obj = 0;
2730 int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap,
2731 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj);
2733 ASSERT(err == 0 || err == ENOENT);
2739 vdev_load(vdev_t *vd)
2743 * Recursively load all children.
2745 for (int c = 0; c < vd->vdev_children; c++) {
2746 error = vdev_load(vd->vdev_child[c]);
2752 vdev_set_deflate_ratio(vd);
2755 * If this is a top-level vdev, initialize its metaslabs.
2757 if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
2758 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
2759 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2760 VDEV_AUX_CORRUPT_DATA);
2761 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
2762 "asize=%llu", (u_longlong_t)vd->vdev_ashift,
2763 (u_longlong_t)vd->vdev_asize);
2764 return (SET_ERROR(ENXIO));
2765 } else if ((error = vdev_metaslab_init(vd, 0)) != 0) {
2766 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
2767 "[error=%d]", error);
2768 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2769 VDEV_AUX_CORRUPT_DATA);
2773 uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd);
2774 if (checkpoint_sm_obj != 0) {
2775 objset_t *mos = spa_meta_objset(vd->vdev_spa);
2776 ASSERT(vd->vdev_asize != 0);
2777 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL);
2779 if ((error = space_map_open(&vd->vdev_checkpoint_sm,
2780 mos, checkpoint_sm_obj, 0, vd->vdev_asize,
2781 vd->vdev_ashift))) {
2782 vdev_dbgmsg(vd, "vdev_load: space_map_open "
2783 "failed for checkpoint spacemap (obj %llu) "
2785 (u_longlong_t)checkpoint_sm_obj, error);
2788 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
2789 space_map_update(vd->vdev_checkpoint_sm);
2792 * Since the checkpoint_sm contains free entries
2793 * exclusively we can use sm_alloc to indicate the
2794 * culmulative checkpointed space that has been freed.
2796 vd->vdev_stat.vs_checkpoint_space =
2797 -vd->vdev_checkpoint_sm->sm_alloc;
2798 vd->vdev_spa->spa_checkpoint_info.sci_dspace +=
2799 vd->vdev_stat.vs_checkpoint_space;
2804 * If this is a leaf vdev, load its DTL.
2806 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
2807 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2808 VDEV_AUX_CORRUPT_DATA);
2809 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
2810 "[error=%d]", error);
2814 uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd);
2815 if (obsolete_sm_object != 0) {
2816 objset_t *mos = vd->vdev_spa->spa_meta_objset;
2817 ASSERT(vd->vdev_asize != 0);
2818 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
2820 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
2821 obsolete_sm_object, 0, vd->vdev_asize, 0))) {
2822 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2823 VDEV_AUX_CORRUPT_DATA);
2824 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
2825 "obsolete spacemap (obj %llu) [error=%d]",
2826 (u_longlong_t)obsolete_sm_object, error);
2829 space_map_update(vd->vdev_obsolete_sm);
2836 * The special vdev case is used for hot spares and l2cache devices. Its
2837 * sole purpose it to set the vdev state for the associated vdev. To do this,
2838 * we make sure that we can open the underlying device, then try to read the
2839 * label, and make sure that the label is sane and that it hasn't been
2840 * repurposed to another pool.
2843 vdev_validate_aux(vdev_t *vd)
2846 uint64_t guid, version;
2849 if (!vdev_readable(vd))
2852 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
2853 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2854 VDEV_AUX_CORRUPT_DATA);
2858 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2859 !SPA_VERSION_IS_SUPPORTED(version) ||
2860 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
2861 guid != vd->vdev_guid ||
2862 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
2863 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2864 VDEV_AUX_CORRUPT_DATA);
2870 * We don't actually check the pool state here. If it's in fact in
2871 * use by another pool, we update this fact on the fly when requested.
2878 * Free the objects used to store this vdev's spacemaps, and the array
2879 * that points to them.
2882 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
2884 if (vd->vdev_ms_array == 0)
2887 objset_t *mos = vd->vdev_spa->spa_meta_objset;
2888 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
2889 size_t array_bytes = array_count * sizeof (uint64_t);
2890 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
2891 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
2892 array_bytes, smobj_array, 0));
2894 for (uint64_t i = 0; i < array_count; i++) {
2895 uint64_t smobj = smobj_array[i];
2899 space_map_free_obj(mos, smobj, tx);
2902 kmem_free(smobj_array, array_bytes);
2903 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
2904 vd->vdev_ms_array = 0;
2908 vdev_remove_empty(vdev_t *vd, uint64_t txg)
2910 spa_t *spa = vd->vdev_spa;
2913 ASSERT(vd == vd->vdev_top);
2914 ASSERT3U(txg, ==, spa_syncing_txg(spa));
2916 if (vd->vdev_ms != NULL) {
2917 metaslab_group_t *mg = vd->vdev_mg;
2919 metaslab_group_histogram_verify(mg);
2920 metaslab_class_histogram_verify(mg->mg_class);
2922 for (int m = 0; m < vd->vdev_ms_count; m++) {
2923 metaslab_t *msp = vd->vdev_ms[m];
2925 if (msp == NULL || msp->ms_sm == NULL)
2928 mutex_enter(&msp->ms_lock);
2930 * If the metaslab was not loaded when the vdev
2931 * was removed then the histogram accounting may
2932 * not be accurate. Update the histogram information
2933 * here so that we ensure that the metaslab group
2934 * and metaslab class are up-to-date.
2936 metaslab_group_histogram_remove(mg, msp);
2938 VERIFY0(space_map_allocated(msp->ms_sm));
2939 space_map_close(msp->ms_sm);
2941 mutex_exit(&msp->ms_lock);
2944 if (vd->vdev_checkpoint_sm != NULL) {
2945 ASSERT(spa_has_checkpoint(spa));
2946 space_map_close(vd->vdev_checkpoint_sm);
2947 vd->vdev_checkpoint_sm = NULL;
2950 metaslab_group_histogram_verify(mg);
2951 metaslab_class_histogram_verify(mg->mg_class);
2952 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
2953 ASSERT0(mg->mg_histogram[i]);
2956 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
2957 vdev_destroy_spacemaps(vd, tx);
2959 if (vd->vdev_islog && vd->vdev_top_zap != 0) {
2960 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
2961 vd->vdev_top_zap = 0;
2967 vdev_sync_done(vdev_t *vd, uint64_t txg)
2970 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2972 ASSERT(vdev_is_concrete(vd));
2974 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2975 metaslab_sync_done(msp, txg);
2978 metaslab_sync_reassess(vd->vdev_mg);
2982 vdev_sync(vdev_t *vd, uint64_t txg)
2984 spa_t *spa = vd->vdev_spa;
2989 if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
2992 ASSERT(vd->vdev_removing ||
2993 vd->vdev_ops == &vdev_indirect_ops);
2995 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2996 vdev_indirect_sync_obsolete(vd, tx);
3000 * If the vdev is indirect, it can't have dirty
3001 * metaslabs or DTLs.
3003 if (vd->vdev_ops == &vdev_indirect_ops) {
3004 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
3005 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
3010 ASSERT(vdev_is_concrete(vd));
3012 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
3013 !vd->vdev_removing) {
3014 ASSERT(vd == vd->vdev_top);
3015 ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
3016 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3017 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
3018 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
3019 ASSERT(vd->vdev_ms_array != 0);
3020 vdev_config_dirty(vd);
3024 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
3025 metaslab_sync(msp, txg);
3026 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
3029 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
3030 vdev_dtl_sync(lvd, txg);
3033 * Remove the metadata associated with this vdev once it's empty.
3034 * Note that this is typically used for log/cache device removal;
3035 * we don't empty toplevel vdevs when removing them. But if
3036 * a toplevel happens to be emptied, this is not harmful.
3038 if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) {
3039 vdev_remove_empty(vd, txg);
3042 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
3046 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
3048 return (vd->vdev_ops->vdev_op_asize(vd, psize));
3052 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
3053 * not be opened, and no I/O is attempted.
3056 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
3060 spa_vdev_state_enter(spa, SCL_NONE);
3062 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3063 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3065 if (!vd->vdev_ops->vdev_op_leaf)
3066 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3071 * We don't directly use the aux state here, but if we do a
3072 * vdev_reopen(), we need this value to be present to remember why we
3075 vd->vdev_label_aux = aux;
3078 * Faulted state takes precedence over degraded.
3080 vd->vdev_delayed_close = B_FALSE;
3081 vd->vdev_faulted = 1ULL;
3082 vd->vdev_degraded = 0ULL;
3083 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
3086 * If this device has the only valid copy of the data, then
3087 * back off and simply mark the vdev as degraded instead.
3089 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
3090 vd->vdev_degraded = 1ULL;
3091 vd->vdev_faulted = 0ULL;
3094 * If we reopen the device and it's not dead, only then do we
3099 if (vdev_readable(vd))
3100 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
3103 return (spa_vdev_state_exit(spa, vd, 0));
3107 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
3108 * user that something is wrong. The vdev continues to operate as normal as far
3109 * as I/O is concerned.
3112 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
3116 spa_vdev_state_enter(spa, SCL_NONE);
3118 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3119 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3121 if (!vd->vdev_ops->vdev_op_leaf)
3122 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3125 * If the vdev is already faulted, then don't do anything.
3127 if (vd->vdev_faulted || vd->vdev_degraded)
3128 return (spa_vdev_state_exit(spa, NULL, 0));
3130 vd->vdev_degraded = 1ULL;
3131 if (!vdev_is_dead(vd))
3132 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
3135 return (spa_vdev_state_exit(spa, vd, 0));
3139 * Online the given vdev.
3141 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
3142 * spare device should be detached when the device finishes resilvering.
3143 * Second, the online should be treated like a 'test' online case, so no FMA
3144 * events are generated if the device fails to open.
3147 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
3149 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
3150 boolean_t wasoffline;
3151 vdev_state_t oldstate;
3153 spa_vdev_state_enter(spa, SCL_NONE);
3155 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3156 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3158 if (!vd->vdev_ops->vdev_op_leaf)
3159 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3161 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
3162 oldstate = vd->vdev_state;
3165 vd->vdev_offline = B_FALSE;
3166 vd->vdev_tmpoffline = B_FALSE;
3167 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
3168 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
3170 /* XXX - L2ARC 1.0 does not support expansion */
3171 if (!vd->vdev_aux) {
3172 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3173 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
3177 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
3179 if (!vd->vdev_aux) {
3180 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3181 pvd->vdev_expanding = B_FALSE;
3185 *newstate = vd->vdev_state;
3186 if ((flags & ZFS_ONLINE_UNSPARE) &&
3187 !vdev_is_dead(vd) && vd->vdev_parent &&
3188 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3189 vd->vdev_parent->vdev_child[0] == vd)
3190 vd->vdev_unspare = B_TRUE;
3192 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
3194 /* XXX - L2ARC 1.0 does not support expansion */
3196 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
3197 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
3201 (oldstate < VDEV_STATE_DEGRADED &&
3202 vd->vdev_state >= VDEV_STATE_DEGRADED))
3203 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
3205 return (spa_vdev_state_exit(spa, vd, 0));
3209 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
3213 uint64_t generation;
3214 metaslab_group_t *mg;
3217 spa_vdev_state_enter(spa, SCL_ALLOC);
3219 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3220 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3222 if (!vd->vdev_ops->vdev_op_leaf)
3223 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3227 generation = spa->spa_config_generation + 1;
3230 * If the device isn't already offline, try to offline it.
3232 if (!vd->vdev_offline) {
3234 * If this device has the only valid copy of some data,
3235 * don't allow it to be offlined. Log devices are always
3238 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3239 vdev_dtl_required(vd))
3240 return (spa_vdev_state_exit(spa, NULL, EBUSY));
3243 * If the top-level is a slog and it has had allocations
3244 * then proceed. We check that the vdev's metaslab group
3245 * is not NULL since it's possible that we may have just
3246 * added this vdev but not yet initialized its metaslabs.
3248 if (tvd->vdev_islog && mg != NULL) {
3250 * Prevent any future allocations.
3252 metaslab_group_passivate(mg);
3253 (void) spa_vdev_state_exit(spa, vd, 0);
3255 error = spa_reset_logs(spa);
3258 * If the log device was successfully reset but has
3259 * checkpointed data, do not offline it.
3262 tvd->vdev_checkpoint_sm != NULL) {
3263 ASSERT3U(tvd->vdev_checkpoint_sm->sm_alloc,
3265 error = ZFS_ERR_CHECKPOINT_EXISTS;
3268 spa_vdev_state_enter(spa, SCL_ALLOC);
3271 * Check to see if the config has changed.
3273 if (error || generation != spa->spa_config_generation) {
3274 metaslab_group_activate(mg);
3276 return (spa_vdev_state_exit(spa,
3278 (void) spa_vdev_state_exit(spa, vd, 0);
3281 ASSERT0(tvd->vdev_stat.vs_alloc);
3285 * Offline this device and reopen its top-level vdev.
3286 * If the top-level vdev is a log device then just offline
3287 * it. Otherwise, if this action results in the top-level
3288 * vdev becoming unusable, undo it and fail the request.
3290 vd->vdev_offline = B_TRUE;
3293 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3294 vdev_is_dead(tvd)) {
3295 vd->vdev_offline = B_FALSE;
3297 return (spa_vdev_state_exit(spa, NULL, EBUSY));
3301 * Add the device back into the metaslab rotor so that
3302 * once we online the device it's open for business.
3304 if (tvd->vdev_islog && mg != NULL)
3305 metaslab_group_activate(mg);
3308 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
3310 return (spa_vdev_state_exit(spa, vd, 0));
3314 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
3318 mutex_enter(&spa->spa_vdev_top_lock);
3319 error = vdev_offline_locked(spa, guid, flags);
3320 mutex_exit(&spa->spa_vdev_top_lock);
3326 * Clear the error counts associated with this vdev. Unlike vdev_online() and
3327 * vdev_offline(), we assume the spa config is locked. We also clear all
3328 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
3331 vdev_clear(spa_t *spa, vdev_t *vd)
3333 vdev_t *rvd = spa->spa_root_vdev;
3335 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3340 vd->vdev_stat.vs_read_errors = 0;
3341 vd->vdev_stat.vs_write_errors = 0;
3342 vd->vdev_stat.vs_checksum_errors = 0;
3344 for (int c = 0; c < vd->vdev_children; c++)
3345 vdev_clear(spa, vd->vdev_child[c]);
3348 for (int c = 0; c < spa->spa_l2cache.sav_count; c++)
3349 vdev_clear(spa, spa->spa_l2cache.sav_vdevs[c]);
3351 for (int c = 0; c < spa->spa_spares.sav_count; c++)
3352 vdev_clear(spa, spa->spa_spares.sav_vdevs[c]);
3356 * It makes no sense to "clear" an indirect vdev.
3358 if (!vdev_is_concrete(vd))
3362 * If we're in the FAULTED state or have experienced failed I/O, then
3363 * clear the persistent state and attempt to reopen the device. We
3364 * also mark the vdev config dirty, so that the new faulted state is
3365 * written out to disk.
3367 if (vd->vdev_faulted || vd->vdev_degraded ||
3368 !vdev_readable(vd) || !vdev_writeable(vd)) {
3371 * When reopening in reponse to a clear event, it may be due to
3372 * a fmadm repair request. In this case, if the device is
3373 * still broken, we want to still post the ereport again.
3375 vd->vdev_forcefault = B_TRUE;
3377 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
3378 vd->vdev_cant_read = B_FALSE;
3379 vd->vdev_cant_write = B_FALSE;
3381 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
3383 vd->vdev_forcefault = B_FALSE;
3385 if (vd != rvd && vdev_writeable(vd->vdev_top))
3386 vdev_state_dirty(vd->vdev_top);
3388 if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
3389 spa_async_request(spa, SPA_ASYNC_RESILVER);
3391 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
3395 * When clearing a FMA-diagnosed fault, we always want to
3396 * unspare the device, as we assume that the original spare was
3397 * done in response to the FMA fault.
3399 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
3400 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3401 vd->vdev_parent->vdev_child[0] == vd)
3402 vd->vdev_unspare = B_TRUE;
3406 vdev_is_dead(vdev_t *vd)
3409 * Holes and missing devices are always considered "dead".
3410 * This simplifies the code since we don't have to check for
3411 * these types of devices in the various code paths.
3412 * Instead we rely on the fact that we skip over dead devices
3413 * before issuing I/O to them.
3415 return (vd->vdev_state < VDEV_STATE_DEGRADED ||
3416 vd->vdev_ops == &vdev_hole_ops ||
3417 vd->vdev_ops == &vdev_missing_ops);
3421 vdev_readable(vdev_t *vd)
3423 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
3427 vdev_writeable(vdev_t *vd)
3429 return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
3430 vdev_is_concrete(vd));
3434 vdev_allocatable(vdev_t *vd)
3436 uint64_t state = vd->vdev_state;
3439 * We currently allow allocations from vdevs which may be in the
3440 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3441 * fails to reopen then we'll catch it later when we're holding
3442 * the proper locks. Note that we have to get the vdev state
3443 * in a local variable because although it changes atomically,
3444 * we're asking two separate questions about it.
3446 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
3447 !vd->vdev_cant_write && vdev_is_concrete(vd) &&
3448 vd->vdev_mg->mg_initialized);
3452 vdev_accessible(vdev_t *vd, zio_t *zio)
3454 ASSERT(zio->io_vd == vd);
3456 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
3459 if (zio->io_type == ZIO_TYPE_READ)
3460 return (!vd->vdev_cant_read);
3462 if (zio->io_type == ZIO_TYPE_WRITE)
3463 return (!vd->vdev_cant_write);
3469 vdev_is_spacemap_addressable(vdev_t *vd)
3472 * Assuming 47 bits of the space map entry dedicated for the entry's
3473 * offset (see description in space_map.h), we calculate the maximum
3474 * address that can be described by a space map entry for the given
3477 uint64_t shift = vd->vdev_ashift + 47;
3479 if (shift >= 63) /* detect potential overflow */
3482 return (vd->vdev_asize < (1ULL << shift));
3486 * Get statistics for the given vdev.
3489 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
3491 spa_t *spa = vd->vdev_spa;
3492 vdev_t *rvd = spa->spa_root_vdev;
3493 vdev_t *tvd = vd->vdev_top;
3495 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
3497 mutex_enter(&vd->vdev_stat_lock);
3498 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
3499 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
3500 vs->vs_state = vd->vdev_state;
3501 vs->vs_rsize = vdev_get_min_asize(vd);
3502 if (vd->vdev_ops->vdev_op_leaf)
3503 vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
3505 * Report expandable space on top-level, non-auxillary devices only.
3506 * The expandable space is reported in terms of metaslab sized units
3507 * since that determines how much space the pool can expand.
3509 if (vd->vdev_aux == NULL && tvd != NULL && vd->vdev_max_asize != 0) {
3510 vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize -
3511 spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift);
3513 vs->vs_configured_ashift = vd->vdev_top != NULL
3514 ? vd->vdev_top->vdev_ashift : vd->vdev_ashift;
3515 vs->vs_logical_ashift = vd->vdev_logical_ashift;
3516 vs->vs_physical_ashift = vd->vdev_physical_ashift;
3517 if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
3518 vdev_is_concrete(vd)) {
3519 vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
3523 * If we're getting stats on the root vdev, aggregate the I/O counts
3524 * over all top-level vdevs (i.e. the direct children of the root).
3527 for (int c = 0; c < rvd->vdev_children; c++) {
3528 vdev_t *cvd = rvd->vdev_child[c];
3529 vdev_stat_t *cvs = &cvd->vdev_stat;
3531 for (int t = 0; t < ZIO_TYPES; t++) {
3532 vs->vs_ops[t] += cvs->vs_ops[t];
3533 vs->vs_bytes[t] += cvs->vs_bytes[t];
3535 cvs->vs_scan_removing = cvd->vdev_removing;
3538 mutex_exit(&vd->vdev_stat_lock);
3542 vdev_clear_stats(vdev_t *vd)
3544 mutex_enter(&vd->vdev_stat_lock);
3545 vd->vdev_stat.vs_space = 0;
3546 vd->vdev_stat.vs_dspace = 0;
3547 vd->vdev_stat.vs_alloc = 0;
3548 mutex_exit(&vd->vdev_stat_lock);
3552 vdev_scan_stat_init(vdev_t *vd)
3554 vdev_stat_t *vs = &vd->vdev_stat;
3556 for (int c = 0; c < vd->vdev_children; c++)
3557 vdev_scan_stat_init(vd->vdev_child[c]);
3559 mutex_enter(&vd->vdev_stat_lock);
3560 vs->vs_scan_processed = 0;
3561 mutex_exit(&vd->vdev_stat_lock);
3565 vdev_stat_update(zio_t *zio, uint64_t psize)
3567 spa_t *spa = zio->io_spa;
3568 vdev_t *rvd = spa->spa_root_vdev;
3569 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
3571 uint64_t txg = zio->io_txg;
3572 vdev_stat_t *vs = &vd->vdev_stat;
3573 zio_type_t type = zio->io_type;
3574 int flags = zio->io_flags;
3577 * If this i/o is a gang leader, it didn't do any actual work.
3579 if (zio->io_gang_tree)
3582 if (zio->io_error == 0) {
3584 * If this is a root i/o, don't count it -- we've already
3585 * counted the top-level vdevs, and vdev_get_stats() will
3586 * aggregate them when asked. This reduces contention on
3587 * the root vdev_stat_lock and implicitly handles blocks
3588 * that compress away to holes, for which there is no i/o.
3589 * (Holes never create vdev children, so all the counters
3590 * remain zero, which is what we want.)
3592 * Note: this only applies to successful i/o (io_error == 0)
3593 * because unlike i/o counts, errors are not additive.
3594 * When reading a ditto block, for example, failure of
3595 * one top-level vdev does not imply a root-level error.
3600 ASSERT(vd == zio->io_vd);
3602 if (flags & ZIO_FLAG_IO_BYPASS)
3605 mutex_enter(&vd->vdev_stat_lock);
3607 if (flags & ZIO_FLAG_IO_REPAIR) {
3608 if (flags & ZIO_FLAG_SCAN_THREAD) {
3609 dsl_scan_phys_t *scn_phys =
3610 &spa->spa_dsl_pool->dp_scan->scn_phys;
3611 uint64_t *processed = &scn_phys->scn_processed;
3614 if (vd->vdev_ops->vdev_op_leaf)
3615 atomic_add_64(processed, psize);
3616 vs->vs_scan_processed += psize;
3619 if (flags & ZIO_FLAG_SELF_HEAL)
3620 vs->vs_self_healed += psize;
3624 vs->vs_bytes[type] += psize;
3626 mutex_exit(&vd->vdev_stat_lock);
3630 if (flags & ZIO_FLAG_SPECULATIVE)
3634 * If this is an I/O error that is going to be retried, then ignore the
3635 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
3636 * hard errors, when in reality they can happen for any number of
3637 * innocuous reasons (bus resets, MPxIO link failure, etc).
3639 if (zio->io_error == EIO &&
3640 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
3644 * Intent logs writes won't propagate their error to the root
3645 * I/O so don't mark these types of failures as pool-level
3648 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
3651 mutex_enter(&vd->vdev_stat_lock);
3652 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
3653 if (zio->io_error == ECKSUM)
3654 vs->vs_checksum_errors++;
3656 vs->vs_read_errors++;
3658 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
3659 vs->vs_write_errors++;
3660 mutex_exit(&vd->vdev_stat_lock);
3662 if (spa->spa_load_state == SPA_LOAD_NONE &&
3663 type == ZIO_TYPE_WRITE && txg != 0 &&
3664 (!(flags & ZIO_FLAG_IO_REPAIR) ||
3665 (flags & ZIO_FLAG_SCAN_THREAD) ||
3666 spa->spa_claiming)) {
3668 * This is either a normal write (not a repair), or it's
3669 * a repair induced by the scrub thread, or it's a repair
3670 * made by zil_claim() during spa_load() in the first txg.
3671 * In the normal case, we commit the DTL change in the same
3672 * txg as the block was born. In the scrub-induced repair
3673 * case, we know that scrubs run in first-pass syncing context,
3674 * so we commit the DTL change in spa_syncing_txg(spa).
3675 * In the zil_claim() case, we commit in spa_first_txg(spa).
3677 * We currently do not make DTL entries for failed spontaneous
3678 * self-healing writes triggered by normal (non-scrubbing)
3679 * reads, because we have no transactional context in which to
3680 * do so -- and it's not clear that it'd be desirable anyway.
3682 if (vd->vdev_ops->vdev_op_leaf) {
3683 uint64_t commit_txg = txg;
3684 if (flags & ZIO_FLAG_SCAN_THREAD) {
3685 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3686 ASSERT(spa_sync_pass(spa) == 1);
3687 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
3688 commit_txg = spa_syncing_txg(spa);
3689 } else if (spa->spa_claiming) {
3690 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3691 commit_txg = spa_first_txg(spa);
3693 ASSERT(commit_txg >= spa_syncing_txg(spa));
3694 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
3696 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3697 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
3698 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
3701 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
3706 * Update the in-core space usage stats for this vdev, its metaslab class,
3707 * and the root vdev.
3710 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
3711 int64_t space_delta)
3713 int64_t dspace_delta = space_delta;
3714 spa_t *spa = vd->vdev_spa;
3715 vdev_t *rvd = spa->spa_root_vdev;
3716 metaslab_group_t *mg = vd->vdev_mg;
3717 metaslab_class_t *mc = mg ? mg->mg_class : NULL;
3719 ASSERT(vd == vd->vdev_top);
3722 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
3723 * factor. We must calculate this here and not at the root vdev
3724 * because the root vdev's psize-to-asize is simply the max of its
3725 * childrens', thus not accurate enough for us.
3727 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
3728 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
3729 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
3730 vd->vdev_deflate_ratio;
3732 mutex_enter(&vd->vdev_stat_lock);
3733 vd->vdev_stat.vs_alloc += alloc_delta;
3734 vd->vdev_stat.vs_space += space_delta;
3735 vd->vdev_stat.vs_dspace += dspace_delta;
3736 mutex_exit(&vd->vdev_stat_lock);
3738 if (mc == spa_normal_class(spa)) {
3739 mutex_enter(&rvd->vdev_stat_lock);
3740 rvd->vdev_stat.vs_alloc += alloc_delta;
3741 rvd->vdev_stat.vs_space += space_delta;
3742 rvd->vdev_stat.vs_dspace += dspace_delta;
3743 mutex_exit(&rvd->vdev_stat_lock);
3747 ASSERT(rvd == vd->vdev_parent);
3748 ASSERT(vd->vdev_ms_count != 0);
3750 metaslab_class_space_update(mc,
3751 alloc_delta, defer_delta, space_delta, dspace_delta);
3756 * Mark a top-level vdev's config as dirty, placing it on the dirty list
3757 * so that it will be written out next time the vdev configuration is synced.
3758 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3761 vdev_config_dirty(vdev_t *vd)
3763 spa_t *spa = vd->vdev_spa;
3764 vdev_t *rvd = spa->spa_root_vdev;
3767 ASSERT(spa_writeable(spa));
3770 * If this is an aux vdev (as with l2cache and spare devices), then we
3771 * update the vdev config manually and set the sync flag.
3773 if (vd->vdev_aux != NULL) {
3774 spa_aux_vdev_t *sav = vd->vdev_aux;
3778 for (c = 0; c < sav->sav_count; c++) {
3779 if (sav->sav_vdevs[c] == vd)
3783 if (c == sav->sav_count) {
3785 * We're being removed. There's nothing more to do.
3787 ASSERT(sav->sav_sync == B_TRUE);
3791 sav->sav_sync = B_TRUE;
3793 if (nvlist_lookup_nvlist_array(sav->sav_config,
3794 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
3795 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
3796 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
3802 * Setting the nvlist in the middle if the array is a little
3803 * sketchy, but it will work.
3805 nvlist_free(aux[c]);
3806 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
3812 * The dirty list is protected by the SCL_CONFIG lock. The caller
3813 * must either hold SCL_CONFIG as writer, or must be the sync thread
3814 * (which holds SCL_CONFIG as reader). There's only one sync thread,
3815 * so this is sufficient to ensure mutual exclusion.
3817 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3818 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3819 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3822 for (c = 0; c < rvd->vdev_children; c++)
3823 vdev_config_dirty(rvd->vdev_child[c]);
3825 ASSERT(vd == vd->vdev_top);
3827 if (!list_link_active(&vd->vdev_config_dirty_node) &&
3828 vdev_is_concrete(vd)) {
3829 list_insert_head(&spa->spa_config_dirty_list, vd);
3835 vdev_config_clean(vdev_t *vd)
3837 spa_t *spa = vd->vdev_spa;
3839 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3840 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3841 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3843 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3844 list_remove(&spa->spa_config_dirty_list, vd);
3848 * Mark a top-level vdev's state as dirty, so that the next pass of
3849 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3850 * the state changes from larger config changes because they require
3851 * much less locking, and are often needed for administrative actions.
3854 vdev_state_dirty(vdev_t *vd)
3856 spa_t *spa = vd->vdev_spa;
3858 ASSERT(spa_writeable(spa));
3859 ASSERT(vd == vd->vdev_top);
3862 * The state list is protected by the SCL_STATE lock. The caller
3863 * must either hold SCL_STATE as writer, or must be the sync thread
3864 * (which holds SCL_STATE as reader). There's only one sync thread,
3865 * so this is sufficient to ensure mutual exclusion.
3867 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3868 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3869 spa_config_held(spa, SCL_STATE, RW_READER)));
3871 if (!list_link_active(&vd->vdev_state_dirty_node) &&
3872 vdev_is_concrete(vd))
3873 list_insert_head(&spa->spa_state_dirty_list, vd);
3877 vdev_state_clean(vdev_t *vd)
3879 spa_t *spa = vd->vdev_spa;
3881 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3882 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3883 spa_config_held(spa, SCL_STATE, RW_READER)));
3885 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3886 list_remove(&spa->spa_state_dirty_list, vd);
3890 * Propagate vdev state up from children to parent.
3893 vdev_propagate_state(vdev_t *vd)
3895 spa_t *spa = vd->vdev_spa;
3896 vdev_t *rvd = spa->spa_root_vdev;
3897 int degraded = 0, faulted = 0;
3901 if (vd->vdev_children > 0) {
3902 for (int c = 0; c < vd->vdev_children; c++) {
3903 child = vd->vdev_child[c];
3906 * Don't factor holes or indirect vdevs into the
3909 if (!vdev_is_concrete(child))
3912 if (!vdev_readable(child) ||
3913 (!vdev_writeable(child) && spa_writeable(spa))) {
3915 * Root special: if there is a top-level log
3916 * device, treat the root vdev as if it were
3919 if (child->vdev_islog && vd == rvd)
3923 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
3927 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
3931 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
3934 * Root special: if there is a top-level vdev that cannot be
3935 * opened due to corrupted metadata, then propagate the root
3936 * vdev's aux state as 'corrupt' rather than 'insufficient
3939 if (corrupted && vd == rvd &&
3940 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
3941 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
3942 VDEV_AUX_CORRUPT_DATA);
3945 if (vd->vdev_parent)
3946 vdev_propagate_state(vd->vdev_parent);
3950 * Set a vdev's state. If this is during an open, we don't update the parent
3951 * state, because we're in the process of opening children depth-first.
3952 * Otherwise, we propagate the change to the parent.
3954 * If this routine places a device in a faulted state, an appropriate ereport is
3958 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3960 uint64_t save_state;
3961 spa_t *spa = vd->vdev_spa;
3963 if (state == vd->vdev_state) {
3964 vd->vdev_stat.vs_aux = aux;
3968 save_state = vd->vdev_state;
3970 vd->vdev_state = state;
3971 vd->vdev_stat.vs_aux = aux;
3974 * If we are setting the vdev state to anything but an open state, then
3975 * always close the underlying device unless the device has requested
3976 * a delayed close (i.e. we're about to remove or fault the device).
3977 * Otherwise, we keep accessible but invalid devices open forever.
3978 * We don't call vdev_close() itself, because that implies some extra
3979 * checks (offline, etc) that we don't want here. This is limited to
3980 * leaf devices, because otherwise closing the device will affect other
3983 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
3984 vd->vdev_ops->vdev_op_leaf)
3985 vd->vdev_ops->vdev_op_close(vd);
3987 if (vd->vdev_removed &&
3988 state == VDEV_STATE_CANT_OPEN &&
3989 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
3991 * If the previous state is set to VDEV_STATE_REMOVED, then this
3992 * device was previously marked removed and someone attempted to
3993 * reopen it. If this failed due to a nonexistent device, then
3994 * keep the device in the REMOVED state. We also let this be if
3995 * it is one of our special test online cases, which is only
3996 * attempting to online the device and shouldn't generate an FMA
3999 vd->vdev_state = VDEV_STATE_REMOVED;
4000 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
4001 } else if (state == VDEV_STATE_REMOVED) {
4002 vd->vdev_removed = B_TRUE;
4003 } else if (state == VDEV_STATE_CANT_OPEN) {
4005 * If we fail to open a vdev during an import or recovery, we
4006 * mark it as "not available", which signifies that it was
4007 * never there to begin with. Failure to open such a device
4008 * is not considered an error.
4010 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
4011 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
4012 vd->vdev_ops->vdev_op_leaf)
4013 vd->vdev_not_present = 1;
4016 * Post the appropriate ereport. If the 'prevstate' field is
4017 * set to something other than VDEV_STATE_UNKNOWN, it indicates
4018 * that this is part of a vdev_reopen(). In this case, we don't
4019 * want to post the ereport if the device was already in the
4020 * CANT_OPEN state beforehand.
4022 * If the 'checkremove' flag is set, then this is an attempt to
4023 * online the device in response to an insertion event. If we
4024 * hit this case, then we have detected an insertion event for a
4025 * faulted or offline device that wasn't in the removed state.
4026 * In this scenario, we don't post an ereport because we are
4027 * about to replace the device, or attempt an online with
4028 * vdev_forcefault, which will generate the fault for us.
4030 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
4031 !vd->vdev_not_present && !vd->vdev_checkremove &&
4032 vd != spa->spa_root_vdev) {
4036 case VDEV_AUX_OPEN_FAILED:
4037 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
4039 case VDEV_AUX_CORRUPT_DATA:
4040 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
4042 case VDEV_AUX_NO_REPLICAS:
4043 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
4045 case VDEV_AUX_BAD_GUID_SUM:
4046 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
4048 case VDEV_AUX_TOO_SMALL:
4049 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
4051 case VDEV_AUX_BAD_LABEL:
4052 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
4055 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
4058 zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
4061 /* Erase any notion of persistent removed state */
4062 vd->vdev_removed = B_FALSE;
4064 vd->vdev_removed = B_FALSE;
4068 * Notify the fmd of the state change. Be verbose and post
4069 * notifications even for stuff that's not important; the fmd agent can
4070 * sort it out. Don't emit state change events for non-leaf vdevs since
4071 * they can't change state on their own. The FMD can check their state
4072 * if it wants to when it sees that a leaf vdev had a state change.
4074 if (vd->vdev_ops->vdev_op_leaf)
4075 zfs_post_state_change(spa, vd);
4077 if (!isopen && vd->vdev_parent)
4078 vdev_propagate_state(vd->vdev_parent);
4082 vdev_children_are_offline(vdev_t *vd)
4084 ASSERT(!vd->vdev_ops->vdev_op_leaf);
4086 for (uint64_t i = 0; i < vd->vdev_children; i++) {
4087 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
4095 * Check the vdev configuration to ensure that it's capable of supporting
4096 * a root pool. We do not support partial configuration.
4097 * In addition, only a single top-level vdev is allowed.
4099 * FreeBSD does not have above limitations.
4102 vdev_is_bootable(vdev_t *vd)
4105 if (!vd->vdev_ops->vdev_op_leaf) {
4106 char *vdev_type = vd->vdev_ops->vdev_op_type;
4108 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
4109 vd->vdev_children > 1) {
4111 } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 ||
4112 strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) {
4117 for (int c = 0; c < vd->vdev_children; c++) {
4118 if (!vdev_is_bootable(vd->vdev_child[c]))
4121 #endif /* illumos */
4126 vdev_is_concrete(vdev_t *vd)
4128 vdev_ops_t *ops = vd->vdev_ops;
4129 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
4130 ops == &vdev_missing_ops || ops == &vdev_root_ops) {
4138 * Determine if a log device has valid content. If the vdev was
4139 * removed or faulted in the MOS config then we know that
4140 * the content on the log device has already been written to the pool.
4143 vdev_log_state_valid(vdev_t *vd)
4145 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
4149 for (int c = 0; c < vd->vdev_children; c++)
4150 if (vdev_log_state_valid(vd->vdev_child[c]))
4157 * Expand a vdev if possible.
4160 vdev_expand(vdev_t *vd, uint64_t txg)
4162 ASSERT(vd->vdev_top == vd);
4163 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4165 vdev_set_deflate_ratio(vd);
4167 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
4168 vdev_is_concrete(vd)) {
4169 VERIFY(vdev_metaslab_init(vd, txg) == 0);
4170 vdev_config_dirty(vd);
4178 vdev_split(vdev_t *vd)
4180 vdev_t *cvd, *pvd = vd->vdev_parent;
4182 vdev_remove_child(pvd, vd);
4183 vdev_compact_children(pvd);
4185 cvd = pvd->vdev_child[0];
4186 if (pvd->vdev_children == 1) {
4187 vdev_remove_parent(cvd);
4188 cvd->vdev_splitting = B_TRUE;
4190 vdev_propagate_state(cvd);
4194 vdev_deadman(vdev_t *vd)
4196 for (int c = 0; c < vd->vdev_children; c++) {
4197 vdev_t *cvd = vd->vdev_child[c];
4202 if (vd->vdev_ops->vdev_op_leaf) {
4203 vdev_queue_t *vq = &vd->vdev_queue;
4205 mutex_enter(&vq->vq_lock);
4206 if (avl_numnodes(&vq->vq_active_tree) > 0) {
4207 spa_t *spa = vd->vdev_spa;
4212 * Look at the head of all the pending queues,
4213 * if any I/O has been outstanding for longer than
4214 * the spa_deadman_synctime we panic the system.
4216 fio = avl_first(&vq->vq_active_tree);
4217 delta = gethrtime() - fio->io_timestamp;
4218 if (delta > spa_deadman_synctime(spa)) {
4219 vdev_dbgmsg(vd, "SLOW IO: zio timestamp "
4220 "%lluns, delta %lluns, last io %lluns",
4221 fio->io_timestamp, (u_longlong_t)delta,
4222 vq->vq_io_complete_ts);
4223 fm_panic("I/O to pool '%s' appears to be "
4224 "hung on vdev guid %llu at '%s'.",
4226 (long long unsigned int) vd->vdev_guid,
4230 mutex_exit(&vq->vq_lock);