4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright 2017 Nexenta Systems, Inc.
26 * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
27 * Copyright (c) 2014 Integros [integros.com]
28 * Copyright 2016 Toomas Soome <tsoome@me.com>
31 #include <sys/zfs_context.h>
32 #include <sys/fm/fs/zfs.h>
34 #include <sys/spa_impl.h>
36 #include <sys/dmu_tx.h>
37 #include <sys/vdev_impl.h>
38 #include <sys/uberblock_impl.h>
39 #include <sys/metaslab.h>
40 #include <sys/metaslab_impl.h>
41 #include <sys/space_map.h>
42 #include <sys/space_reftree.h>
45 #include <sys/fs/zfs.h>
48 #include <sys/dsl_scan.h>
50 #include <sys/trim_map.h>
52 SYSCTL_DECL(_vfs_zfs);
53 SYSCTL_NODE(_vfs_zfs, OID_AUTO, vdev, CTLFLAG_RW, 0, "ZFS VDEV");
56 * Virtual device management.
60 * The limit for ZFS to automatically increase a top-level vdev's ashift
61 * from logical ashift to physical ashift.
63 * Example: one or more 512B emulation child vdevs
64 * child->vdev_ashift = 9 (512 bytes)
65 * child->vdev_physical_ashift = 12 (4096 bytes)
66 * zfs_max_auto_ashift = 11 (2048 bytes)
67 * zfs_min_auto_ashift = 9 (512 bytes)
69 * On pool creation or the addition of a new top-level vdev, ZFS will
70 * increase the ashift of the top-level vdev to 2048 as limited by
71 * zfs_max_auto_ashift.
73 * Example: one or more 512B emulation child vdevs
74 * child->vdev_ashift = 9 (512 bytes)
75 * child->vdev_physical_ashift = 12 (4096 bytes)
76 * zfs_max_auto_ashift = 13 (8192 bytes)
77 * zfs_min_auto_ashift = 9 (512 bytes)
79 * On pool creation or the addition of a new top-level vdev, ZFS will
80 * increase the ashift of the top-level vdev to 4096 to match the
81 * max vdev_physical_ashift.
83 * Example: one or more 512B emulation child vdevs
84 * child->vdev_ashift = 9 (512 bytes)
85 * child->vdev_physical_ashift = 9 (512 bytes)
86 * zfs_max_auto_ashift = 13 (8192 bytes)
87 * zfs_min_auto_ashift = 12 (4096 bytes)
89 * On pool creation or the addition of a new top-level vdev, ZFS will
90 * increase the ashift of the top-level vdev to 4096 to match the
91 * zfs_min_auto_ashift.
93 static uint64_t zfs_max_auto_ashift = SPA_MAXASHIFT;
94 static uint64_t zfs_min_auto_ashift = SPA_MINASHIFT;
97 sysctl_vfs_zfs_max_auto_ashift(SYSCTL_HANDLER_ARGS)
102 val = zfs_max_auto_ashift;
103 err = sysctl_handle_64(oidp, &val, 0, req);
104 if (err != 0 || req->newptr == NULL)
107 if (val > SPA_MAXASHIFT || val < zfs_min_auto_ashift)
110 zfs_max_auto_ashift = val;
114 SYSCTL_PROC(_vfs_zfs, OID_AUTO, max_auto_ashift,
115 CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t),
116 sysctl_vfs_zfs_max_auto_ashift, "QU",
117 "Max ashift used when optimising for logical -> physical sectors size on "
118 "new top-level vdevs.");
121 sysctl_vfs_zfs_min_auto_ashift(SYSCTL_HANDLER_ARGS)
126 val = zfs_min_auto_ashift;
127 err = sysctl_handle_64(oidp, &val, 0, req);
128 if (err != 0 || req->newptr == NULL)
131 if (val < SPA_MINASHIFT || val > zfs_max_auto_ashift)
134 zfs_min_auto_ashift = val;
138 SYSCTL_PROC(_vfs_zfs, OID_AUTO, min_auto_ashift,
139 CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t),
140 sysctl_vfs_zfs_min_auto_ashift, "QU",
141 "Min ashift used when creating new top-level vdevs.");
143 static vdev_ops_t *vdev_ops_table[] = {
162 * When a vdev is added, it will be divided into approximately (but no
163 * more than) this number of metaslabs.
165 int metaslabs_per_vdev = 200;
166 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, metaslabs_per_vdev, CTLFLAG_RDTUN,
167 &metaslabs_per_vdev, 0,
168 "When a vdev is added, how many metaslabs the vdev should be divided into");
171 * Given a vdev type, return the appropriate ops vector.
174 vdev_getops(const char *type)
176 vdev_ops_t *ops, **opspp;
178 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
179 if (strcmp(ops->vdev_op_type, type) == 0)
186 * Default asize function: return the MAX of psize with the asize of
187 * all children. This is what's used by anything other than RAID-Z.
190 vdev_default_asize(vdev_t *vd, uint64_t psize)
192 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
195 for (int c = 0; c < vd->vdev_children; c++) {
196 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
197 asize = MAX(asize, csize);
204 * Get the minimum allocatable size. We define the allocatable size as
205 * the vdev's asize rounded to the nearest metaslab. This allows us to
206 * replace or attach devices which don't have the same physical size but
207 * can still satisfy the same number of allocations.
210 vdev_get_min_asize(vdev_t *vd)
212 vdev_t *pvd = vd->vdev_parent;
215 * If our parent is NULL (inactive spare or cache) or is the root,
216 * just return our own asize.
219 return (vd->vdev_asize);
222 * The top-level vdev just returns the allocatable size rounded
223 * to the nearest metaslab.
225 if (vd == vd->vdev_top)
226 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
229 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
230 * so each child must provide at least 1/Nth of its asize.
232 if (pvd->vdev_ops == &vdev_raidz_ops)
233 return ((pvd->vdev_min_asize + pvd->vdev_children - 1) /
236 return (pvd->vdev_min_asize);
240 vdev_set_min_asize(vdev_t *vd)
242 vd->vdev_min_asize = vdev_get_min_asize(vd);
244 for (int c = 0; c < vd->vdev_children; c++)
245 vdev_set_min_asize(vd->vdev_child[c]);
249 vdev_lookup_top(spa_t *spa, uint64_t vdev)
251 vdev_t *rvd = spa->spa_root_vdev;
253 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
255 if (vdev < rvd->vdev_children) {
256 ASSERT(rvd->vdev_child[vdev] != NULL);
257 return (rvd->vdev_child[vdev]);
264 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
268 if (vd->vdev_guid == guid)
271 for (int c = 0; c < vd->vdev_children; c++)
272 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
280 vdev_count_leaves_impl(vdev_t *vd)
284 if (vd->vdev_ops->vdev_op_leaf)
287 for (int c = 0; c < vd->vdev_children; c++)
288 n += vdev_count_leaves_impl(vd->vdev_child[c]);
294 vdev_count_leaves(spa_t *spa)
296 return (vdev_count_leaves_impl(spa->spa_root_vdev));
300 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
302 size_t oldsize, newsize;
303 uint64_t id = cvd->vdev_id;
305 spa_t *spa = cvd->vdev_spa;
307 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
308 ASSERT(cvd->vdev_parent == NULL);
310 cvd->vdev_parent = pvd;
315 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
317 oldsize = pvd->vdev_children * sizeof (vdev_t *);
318 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
319 newsize = pvd->vdev_children * sizeof (vdev_t *);
321 newchild = kmem_zalloc(newsize, KM_SLEEP);
322 if (pvd->vdev_child != NULL) {
323 bcopy(pvd->vdev_child, newchild, oldsize);
324 kmem_free(pvd->vdev_child, oldsize);
327 pvd->vdev_child = newchild;
328 pvd->vdev_child[id] = cvd;
330 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
331 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
334 * Walk up all ancestors to update guid sum.
336 for (; pvd != NULL; pvd = pvd->vdev_parent)
337 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
341 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
344 uint_t id = cvd->vdev_id;
346 ASSERT(cvd->vdev_parent == pvd);
351 ASSERT(id < pvd->vdev_children);
352 ASSERT(pvd->vdev_child[id] == cvd);
354 pvd->vdev_child[id] = NULL;
355 cvd->vdev_parent = NULL;
357 for (c = 0; c < pvd->vdev_children; c++)
358 if (pvd->vdev_child[c])
361 if (c == pvd->vdev_children) {
362 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
363 pvd->vdev_child = NULL;
364 pvd->vdev_children = 0;
368 * Walk up all ancestors to update guid sum.
370 for (; pvd != NULL; pvd = pvd->vdev_parent)
371 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
375 * Remove any holes in the child array.
378 vdev_compact_children(vdev_t *pvd)
380 vdev_t **newchild, *cvd;
381 int oldc = pvd->vdev_children;
384 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
386 for (int c = newc = 0; c < oldc; c++)
387 if (pvd->vdev_child[c])
390 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
392 for (int c = newc = 0; c < oldc; c++) {
393 if ((cvd = pvd->vdev_child[c]) != NULL) {
394 newchild[newc] = cvd;
395 cvd->vdev_id = newc++;
399 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
400 pvd->vdev_child = newchild;
401 pvd->vdev_children = newc;
405 * Allocate and minimally initialize a vdev_t.
408 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
412 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
414 if (spa->spa_root_vdev == NULL) {
415 ASSERT(ops == &vdev_root_ops);
416 spa->spa_root_vdev = vd;
417 spa->spa_load_guid = spa_generate_guid(NULL);
420 if (guid == 0 && ops != &vdev_hole_ops) {
421 if (spa->spa_root_vdev == vd) {
423 * The root vdev's guid will also be the pool guid,
424 * which must be unique among all pools.
426 guid = spa_generate_guid(NULL);
429 * Any other vdev's guid must be unique within the pool.
431 guid = spa_generate_guid(spa);
433 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
438 vd->vdev_guid = guid;
439 vd->vdev_guid_sum = guid;
441 vd->vdev_state = VDEV_STATE_CLOSED;
442 vd->vdev_ishole = (ops == &vdev_hole_ops);
444 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
445 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
446 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
447 mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL);
448 for (int t = 0; t < DTL_TYPES; t++) {
449 vd->vdev_dtl[t] = range_tree_create(NULL, NULL,
452 txg_list_create(&vd->vdev_ms_list, spa,
453 offsetof(struct metaslab, ms_txg_node));
454 txg_list_create(&vd->vdev_dtl_list, spa,
455 offsetof(struct vdev, vdev_dtl_node));
456 vd->vdev_stat.vs_timestamp = gethrtime();
464 * Allocate a new vdev. The 'alloctype' is used to control whether we are
465 * creating a new vdev or loading an existing one - the behavior is slightly
466 * different for each case.
469 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
474 uint64_t guid = 0, islog, nparity;
477 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
479 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
480 return (SET_ERROR(EINVAL));
482 if ((ops = vdev_getops(type)) == NULL)
483 return (SET_ERROR(EINVAL));
486 * If this is a load, get the vdev guid from the nvlist.
487 * Otherwise, vdev_alloc_common() will generate one for us.
489 if (alloctype == VDEV_ALLOC_LOAD) {
492 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
494 return (SET_ERROR(EINVAL));
496 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
497 return (SET_ERROR(EINVAL));
498 } else if (alloctype == VDEV_ALLOC_SPARE) {
499 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
500 return (SET_ERROR(EINVAL));
501 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
502 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
503 return (SET_ERROR(EINVAL));
504 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
505 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
506 return (SET_ERROR(EINVAL));
510 * The first allocated vdev must be of type 'root'.
512 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
513 return (SET_ERROR(EINVAL));
516 * Determine whether we're a log vdev.
519 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
520 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
521 return (SET_ERROR(ENOTSUP));
523 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
524 return (SET_ERROR(ENOTSUP));
527 * Set the nparity property for RAID-Z vdevs.
530 if (ops == &vdev_raidz_ops) {
531 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
533 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
534 return (SET_ERROR(EINVAL));
536 * Previous versions could only support 1 or 2 parity
540 spa_version(spa) < SPA_VERSION_RAIDZ2)
541 return (SET_ERROR(ENOTSUP));
543 spa_version(spa) < SPA_VERSION_RAIDZ3)
544 return (SET_ERROR(ENOTSUP));
547 * We require the parity to be specified for SPAs that
548 * support multiple parity levels.
550 if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
551 return (SET_ERROR(EINVAL));
553 * Otherwise, we default to 1 parity device for RAID-Z.
560 ASSERT(nparity != -1ULL);
562 vd = vdev_alloc_common(spa, id, guid, ops);
564 vd->vdev_islog = islog;
565 vd->vdev_nparity = nparity;
567 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
568 vd->vdev_path = spa_strdup(vd->vdev_path);
569 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
570 vd->vdev_devid = spa_strdup(vd->vdev_devid);
571 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
572 &vd->vdev_physpath) == 0)
573 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
574 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
575 vd->vdev_fru = spa_strdup(vd->vdev_fru);
578 * Set the whole_disk property. If it's not specified, leave the value
581 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
582 &vd->vdev_wholedisk) != 0)
583 vd->vdev_wholedisk = -1ULL;
586 * Look for the 'not present' flag. This will only be set if the device
587 * was not present at the time of import.
589 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
590 &vd->vdev_not_present);
593 * Get the alignment requirement.
595 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
598 * Retrieve the vdev creation time.
600 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
604 * If we're a top-level vdev, try to load the allocation parameters.
606 if (parent && !parent->vdev_parent &&
607 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
608 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
610 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
612 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
614 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
616 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
619 ASSERT0(vd->vdev_top_zap);
622 if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
623 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
624 alloctype == VDEV_ALLOC_ADD ||
625 alloctype == VDEV_ALLOC_SPLIT ||
626 alloctype == VDEV_ALLOC_ROOTPOOL);
627 vd->vdev_mg = metaslab_group_create(islog ?
628 spa_log_class(spa) : spa_normal_class(spa), vd);
631 if (vd->vdev_ops->vdev_op_leaf &&
632 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
633 (void) nvlist_lookup_uint64(nv,
634 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
636 ASSERT0(vd->vdev_leaf_zap);
640 * If we're a leaf vdev, try to load the DTL object and other state.
643 if (vd->vdev_ops->vdev_op_leaf &&
644 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
645 alloctype == VDEV_ALLOC_ROOTPOOL)) {
646 if (alloctype == VDEV_ALLOC_LOAD) {
647 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
648 &vd->vdev_dtl_object);
649 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
653 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
656 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
657 &spare) == 0 && spare)
661 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
664 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
665 &vd->vdev_resilver_txg);
668 * When importing a pool, we want to ignore the persistent fault
669 * state, as the diagnosis made on another system may not be
670 * valid in the current context. Local vdevs will
671 * remain in the faulted state.
673 if (spa_load_state(spa) == SPA_LOAD_OPEN) {
674 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
676 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
678 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
681 if (vd->vdev_faulted || vd->vdev_degraded) {
685 VDEV_AUX_ERR_EXCEEDED;
686 if (nvlist_lookup_string(nv,
687 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
688 strcmp(aux, "external") == 0)
689 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
695 * Add ourselves to the parent's list of children.
697 vdev_add_child(parent, vd);
705 vdev_free(vdev_t *vd)
707 spa_t *spa = vd->vdev_spa;
710 * vdev_free() implies closing the vdev first. This is simpler than
711 * trying to ensure complicated semantics for all callers.
715 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
716 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
721 for (int c = 0; c < vd->vdev_children; c++)
722 vdev_free(vd->vdev_child[c]);
724 ASSERT(vd->vdev_child == NULL);
725 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
728 * Discard allocation state.
730 if (vd->vdev_mg != NULL) {
731 vdev_metaslab_fini(vd);
732 metaslab_group_destroy(vd->vdev_mg);
735 ASSERT0(vd->vdev_stat.vs_space);
736 ASSERT0(vd->vdev_stat.vs_dspace);
737 ASSERT0(vd->vdev_stat.vs_alloc);
740 * Remove this vdev from its parent's child list.
742 vdev_remove_child(vd->vdev_parent, vd);
744 ASSERT(vd->vdev_parent == NULL);
747 * Clean up vdev structure.
753 spa_strfree(vd->vdev_path);
755 spa_strfree(vd->vdev_devid);
756 if (vd->vdev_physpath)
757 spa_strfree(vd->vdev_physpath);
759 spa_strfree(vd->vdev_fru);
761 if (vd->vdev_isspare)
762 spa_spare_remove(vd);
763 if (vd->vdev_isl2cache)
764 spa_l2cache_remove(vd);
766 txg_list_destroy(&vd->vdev_ms_list);
767 txg_list_destroy(&vd->vdev_dtl_list);
769 mutex_enter(&vd->vdev_dtl_lock);
770 space_map_close(vd->vdev_dtl_sm);
771 for (int t = 0; t < DTL_TYPES; t++) {
772 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
773 range_tree_destroy(vd->vdev_dtl[t]);
775 mutex_exit(&vd->vdev_dtl_lock);
777 mutex_destroy(&vd->vdev_queue_lock);
778 mutex_destroy(&vd->vdev_dtl_lock);
779 mutex_destroy(&vd->vdev_stat_lock);
780 mutex_destroy(&vd->vdev_probe_lock);
782 if (vd == spa->spa_root_vdev)
783 spa->spa_root_vdev = NULL;
785 kmem_free(vd, sizeof (vdev_t));
789 * Transfer top-level vdev state from svd to tvd.
792 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
794 spa_t *spa = svd->vdev_spa;
799 ASSERT(tvd == tvd->vdev_top);
801 tvd->vdev_ms_array = svd->vdev_ms_array;
802 tvd->vdev_ms_shift = svd->vdev_ms_shift;
803 tvd->vdev_ms_count = svd->vdev_ms_count;
804 tvd->vdev_top_zap = svd->vdev_top_zap;
806 svd->vdev_ms_array = 0;
807 svd->vdev_ms_shift = 0;
808 svd->vdev_ms_count = 0;
809 svd->vdev_top_zap = 0;
812 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
813 tvd->vdev_mg = svd->vdev_mg;
814 tvd->vdev_ms = svd->vdev_ms;
819 if (tvd->vdev_mg != NULL)
820 tvd->vdev_mg->mg_vd = tvd;
822 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
823 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
824 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
826 svd->vdev_stat.vs_alloc = 0;
827 svd->vdev_stat.vs_space = 0;
828 svd->vdev_stat.vs_dspace = 0;
830 for (t = 0; t < TXG_SIZE; t++) {
831 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
832 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
833 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
834 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
835 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
836 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
839 if (list_link_active(&svd->vdev_config_dirty_node)) {
840 vdev_config_clean(svd);
841 vdev_config_dirty(tvd);
844 if (list_link_active(&svd->vdev_state_dirty_node)) {
845 vdev_state_clean(svd);
846 vdev_state_dirty(tvd);
849 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
850 svd->vdev_deflate_ratio = 0;
852 tvd->vdev_islog = svd->vdev_islog;
857 vdev_top_update(vdev_t *tvd, vdev_t *vd)
864 for (int c = 0; c < vd->vdev_children; c++)
865 vdev_top_update(tvd, vd->vdev_child[c]);
869 * Add a mirror/replacing vdev above an existing vdev.
872 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
874 spa_t *spa = cvd->vdev_spa;
875 vdev_t *pvd = cvd->vdev_parent;
878 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
880 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
882 mvd->vdev_asize = cvd->vdev_asize;
883 mvd->vdev_min_asize = cvd->vdev_min_asize;
884 mvd->vdev_max_asize = cvd->vdev_max_asize;
885 mvd->vdev_ashift = cvd->vdev_ashift;
886 mvd->vdev_logical_ashift = cvd->vdev_logical_ashift;
887 mvd->vdev_physical_ashift = cvd->vdev_physical_ashift;
888 mvd->vdev_state = cvd->vdev_state;
889 mvd->vdev_crtxg = cvd->vdev_crtxg;
891 vdev_remove_child(pvd, cvd);
892 vdev_add_child(pvd, mvd);
893 cvd->vdev_id = mvd->vdev_children;
894 vdev_add_child(mvd, cvd);
895 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
897 if (mvd == mvd->vdev_top)
898 vdev_top_transfer(cvd, mvd);
904 * Remove a 1-way mirror/replacing vdev from the tree.
907 vdev_remove_parent(vdev_t *cvd)
909 vdev_t *mvd = cvd->vdev_parent;
910 vdev_t *pvd = mvd->vdev_parent;
912 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
914 ASSERT(mvd->vdev_children == 1);
915 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
916 mvd->vdev_ops == &vdev_replacing_ops ||
917 mvd->vdev_ops == &vdev_spare_ops);
918 cvd->vdev_ashift = mvd->vdev_ashift;
919 cvd->vdev_logical_ashift = mvd->vdev_logical_ashift;
920 cvd->vdev_physical_ashift = mvd->vdev_physical_ashift;
922 vdev_remove_child(mvd, cvd);
923 vdev_remove_child(pvd, mvd);
926 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
927 * Otherwise, we could have detached an offline device, and when we
928 * go to import the pool we'll think we have two top-level vdevs,
929 * instead of a different version of the same top-level vdev.
931 if (mvd->vdev_top == mvd) {
932 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
933 cvd->vdev_orig_guid = cvd->vdev_guid;
934 cvd->vdev_guid += guid_delta;
935 cvd->vdev_guid_sum += guid_delta;
937 cvd->vdev_id = mvd->vdev_id;
938 vdev_add_child(pvd, cvd);
939 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
941 if (cvd == cvd->vdev_top)
942 vdev_top_transfer(mvd, cvd);
944 ASSERT(mvd->vdev_children == 0);
949 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
951 spa_t *spa = vd->vdev_spa;
952 objset_t *mos = spa->spa_meta_objset;
954 uint64_t oldc = vd->vdev_ms_count;
955 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
959 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
962 * This vdev is not being allocated from yet or is a hole.
964 if (vd->vdev_ms_shift == 0)
967 ASSERT(!vd->vdev_ishole);
970 * Compute the raidz-deflation ratio. Note, we hard-code
971 * in 128k (1 << 17) because it is the "typical" blocksize.
972 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
973 * otherwise it would inconsistently account for existing bp's.
975 vd->vdev_deflate_ratio = (1 << 17) /
976 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
978 ASSERT(oldc <= newc);
980 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
983 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
984 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
988 vd->vdev_ms_count = newc;
990 for (m = oldc; m < newc; m++) {
994 error = dmu_read(mos, vd->vdev_ms_array,
995 m * sizeof (uint64_t), sizeof (uint64_t), &object,
1001 error = metaslab_init(vd->vdev_mg, m, object, txg,
1008 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1011 * If the vdev is being removed we don't activate
1012 * the metaslabs since we want to ensure that no new
1013 * allocations are performed on this device.
1015 if (oldc == 0 && !vd->vdev_removing)
1016 metaslab_group_activate(vd->vdev_mg);
1019 spa_config_exit(spa, SCL_ALLOC, FTAG);
1025 vdev_metaslab_fini(vdev_t *vd)
1028 uint64_t count = vd->vdev_ms_count;
1030 if (vd->vdev_ms != NULL) {
1031 metaslab_group_passivate(vd->vdev_mg);
1032 for (m = 0; m < count; m++) {
1033 metaslab_t *msp = vd->vdev_ms[m];
1038 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1043 typedef struct vdev_probe_stats {
1044 boolean_t vps_readable;
1045 boolean_t vps_writeable;
1047 } vdev_probe_stats_t;
1050 vdev_probe_done(zio_t *zio)
1052 spa_t *spa = zio->io_spa;
1053 vdev_t *vd = zio->io_vd;
1054 vdev_probe_stats_t *vps = zio->io_private;
1056 ASSERT(vd->vdev_probe_zio != NULL);
1058 if (zio->io_type == ZIO_TYPE_READ) {
1059 if (zio->io_error == 0)
1060 vps->vps_readable = 1;
1061 if (zio->io_error == 0 && spa_writeable(spa)) {
1062 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1063 zio->io_offset, zio->io_size, zio->io_abd,
1064 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1065 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1067 abd_free(zio->io_abd);
1069 } else if (zio->io_type == ZIO_TYPE_WRITE) {
1070 if (zio->io_error == 0)
1071 vps->vps_writeable = 1;
1072 abd_free(zio->io_abd);
1073 } else if (zio->io_type == ZIO_TYPE_NULL) {
1076 vd->vdev_cant_read |= !vps->vps_readable;
1077 vd->vdev_cant_write |= !vps->vps_writeable;
1079 if (vdev_readable(vd) &&
1080 (vdev_writeable(vd) || !spa_writeable(spa))) {
1083 ASSERT(zio->io_error != 0);
1084 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
1085 spa, vd, NULL, 0, 0);
1086 zio->io_error = SET_ERROR(ENXIO);
1089 mutex_enter(&vd->vdev_probe_lock);
1090 ASSERT(vd->vdev_probe_zio == zio);
1091 vd->vdev_probe_zio = NULL;
1092 mutex_exit(&vd->vdev_probe_lock);
1094 zio_link_t *zl = NULL;
1095 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1096 if (!vdev_accessible(vd, pio))
1097 pio->io_error = SET_ERROR(ENXIO);
1099 kmem_free(vps, sizeof (*vps));
1104 * Determine whether this device is accessible.
1106 * Read and write to several known locations: the pad regions of each
1107 * vdev label but the first, which we leave alone in case it contains
1111 vdev_probe(vdev_t *vd, zio_t *zio)
1113 spa_t *spa = vd->vdev_spa;
1114 vdev_probe_stats_t *vps = NULL;
1117 ASSERT(vd->vdev_ops->vdev_op_leaf);
1120 * Don't probe the probe.
1122 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1126 * To prevent 'probe storms' when a device fails, we create
1127 * just one probe i/o at a time. All zios that want to probe
1128 * this vdev will become parents of the probe io.
1130 mutex_enter(&vd->vdev_probe_lock);
1132 if ((pio = vd->vdev_probe_zio) == NULL) {
1133 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1135 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1136 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
1139 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1141 * vdev_cant_read and vdev_cant_write can only
1142 * transition from TRUE to FALSE when we have the
1143 * SCL_ZIO lock as writer; otherwise they can only
1144 * transition from FALSE to TRUE. This ensures that
1145 * any zio looking at these values can assume that
1146 * failures persist for the life of the I/O. That's
1147 * important because when a device has intermittent
1148 * connectivity problems, we want to ensure that
1149 * they're ascribed to the device (ENXIO) and not
1152 * Since we hold SCL_ZIO as writer here, clear both
1153 * values so the probe can reevaluate from first
1156 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1157 vd->vdev_cant_read = B_FALSE;
1158 vd->vdev_cant_write = B_FALSE;
1161 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1162 vdev_probe_done, vps,
1163 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1166 * We can't change the vdev state in this context, so we
1167 * kick off an async task to do it on our behalf.
1170 vd->vdev_probe_wanted = B_TRUE;
1171 spa_async_request(spa, SPA_ASYNC_PROBE);
1176 zio_add_child(zio, pio);
1178 mutex_exit(&vd->vdev_probe_lock);
1181 ASSERT(zio != NULL);
1185 for (int l = 1; l < VDEV_LABELS; l++) {
1186 zio_nowait(zio_read_phys(pio, vd,
1187 vdev_label_offset(vd->vdev_psize, l,
1188 offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE,
1189 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1190 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1191 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1202 vdev_open_child(void *arg)
1206 vd->vdev_open_thread = curthread;
1207 vd->vdev_open_error = vdev_open(vd);
1208 vd->vdev_open_thread = NULL;
1212 vdev_uses_zvols(vdev_t *vd)
1214 if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1215 strlen(ZVOL_DIR)) == 0)
1217 for (int c = 0; c < vd->vdev_children; c++)
1218 if (vdev_uses_zvols(vd->vdev_child[c]))
1224 vdev_open_children(vdev_t *vd)
1227 int children = vd->vdev_children;
1230 * in order to handle pools on top of zvols, do the opens
1231 * in a single thread so that the same thread holds the
1232 * spa_namespace_lock
1234 if (B_TRUE || vdev_uses_zvols(vd)) {
1235 for (int c = 0; c < children; c++)
1236 vd->vdev_child[c]->vdev_open_error =
1237 vdev_open(vd->vdev_child[c]);
1240 tq = taskq_create("vdev_open", children, minclsyspri,
1241 children, children, TASKQ_PREPOPULATE);
1243 for (int c = 0; c < children; c++)
1244 VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1251 * Prepare a virtual device for access.
1254 vdev_open(vdev_t *vd)
1256 spa_t *spa = vd->vdev_spa;
1259 uint64_t max_osize = 0;
1260 uint64_t asize, max_asize, psize;
1261 uint64_t logical_ashift = 0;
1262 uint64_t physical_ashift = 0;
1264 ASSERT(vd->vdev_open_thread == curthread ||
1265 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1266 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1267 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1268 vd->vdev_state == VDEV_STATE_OFFLINE);
1270 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1271 vd->vdev_cant_read = B_FALSE;
1272 vd->vdev_cant_write = B_FALSE;
1273 vd->vdev_notrim = B_FALSE;
1274 vd->vdev_min_asize = vdev_get_min_asize(vd);
1277 * If this vdev is not removed, check its fault status. If it's
1278 * faulted, bail out of the open.
1280 if (!vd->vdev_removed && vd->vdev_faulted) {
1281 ASSERT(vd->vdev_children == 0);
1282 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1283 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1284 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1285 vd->vdev_label_aux);
1286 return (SET_ERROR(ENXIO));
1287 } else if (vd->vdev_offline) {
1288 ASSERT(vd->vdev_children == 0);
1289 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1290 return (SET_ERROR(ENXIO));
1293 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize,
1294 &logical_ashift, &physical_ashift);
1297 * Reset the vdev_reopening flag so that we actually close
1298 * the vdev on error.
1300 vd->vdev_reopening = B_FALSE;
1301 if (zio_injection_enabled && error == 0)
1302 error = zio_handle_device_injection(vd, NULL, ENXIO);
1305 if (vd->vdev_removed &&
1306 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
1307 vd->vdev_removed = B_FALSE;
1309 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1310 vd->vdev_stat.vs_aux);
1314 vd->vdev_removed = B_FALSE;
1317 * Recheck the faulted flag now that we have confirmed that
1318 * the vdev is accessible. If we're faulted, bail.
1320 if (vd->vdev_faulted) {
1321 ASSERT(vd->vdev_children == 0);
1322 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1323 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1324 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1325 vd->vdev_label_aux);
1326 return (SET_ERROR(ENXIO));
1329 if (vd->vdev_degraded) {
1330 ASSERT(vd->vdev_children == 0);
1331 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1332 VDEV_AUX_ERR_EXCEEDED);
1334 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1338 * For hole or missing vdevs we just return success.
1340 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
1343 if (zfs_trim_enabled && !vd->vdev_notrim && vd->vdev_ops->vdev_op_leaf)
1344 trim_map_create(vd);
1346 for (int c = 0; c < vd->vdev_children; c++) {
1347 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1348 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1354 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1355 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1357 if (vd->vdev_children == 0) {
1358 if (osize < SPA_MINDEVSIZE) {
1359 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1360 VDEV_AUX_TOO_SMALL);
1361 return (SET_ERROR(EOVERFLOW));
1364 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1365 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
1366 VDEV_LABEL_END_SIZE);
1368 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1369 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1370 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1371 VDEV_AUX_TOO_SMALL);
1372 return (SET_ERROR(EOVERFLOW));
1376 max_asize = max_osize;
1379 vd->vdev_psize = psize;
1382 * Make sure the allocatable size hasn't shrunk too much.
1384 if (asize < vd->vdev_min_asize) {
1385 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1386 VDEV_AUX_BAD_LABEL);
1387 return (SET_ERROR(EINVAL));
1390 vd->vdev_physical_ashift =
1391 MAX(physical_ashift, vd->vdev_physical_ashift);
1392 vd->vdev_logical_ashift = MAX(logical_ashift, vd->vdev_logical_ashift);
1393 vd->vdev_ashift = MAX(vd->vdev_logical_ashift, vd->vdev_ashift);
1395 if (vd->vdev_logical_ashift > SPA_MAXASHIFT) {
1396 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1397 VDEV_AUX_ASHIFT_TOO_BIG);
1401 if (vd->vdev_asize == 0) {
1403 * This is the first-ever open, so use the computed values.
1404 * For testing purposes, a higher ashift can be requested.
1406 vd->vdev_asize = asize;
1407 vd->vdev_max_asize = max_asize;
1410 * Make sure the alignment requirement hasn't increased.
1412 if (vd->vdev_ashift > vd->vdev_top->vdev_ashift &&
1413 vd->vdev_ops->vdev_op_leaf) {
1414 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1415 VDEV_AUX_BAD_LABEL);
1418 vd->vdev_max_asize = max_asize;
1422 * If all children are healthy we update asize if either:
1423 * The asize has increased, due to a device expansion caused by dynamic
1424 * LUN growth or vdev replacement, and automatic expansion is enabled;
1425 * making the additional space available.
1427 * The asize has decreased, due to a device shrink usually caused by a
1428 * vdev replace with a smaller device. This ensures that calculations
1429 * based of max_asize and asize e.g. esize are always valid. It's safe
1430 * to do this as we've already validated that asize is greater than
1433 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1434 ((asize > vd->vdev_asize &&
1435 (vd->vdev_expanding || spa->spa_autoexpand)) ||
1436 (asize < vd->vdev_asize)))
1437 vd->vdev_asize = asize;
1439 vdev_set_min_asize(vd);
1442 * Ensure we can issue some IO before declaring the
1443 * vdev open for business.
1445 if (vd->vdev_ops->vdev_op_leaf &&
1446 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1447 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1448 VDEV_AUX_ERR_EXCEEDED);
1453 * Track the min and max ashift values for normal data devices.
1455 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1456 !vd->vdev_islog && vd->vdev_aux == NULL) {
1457 if (vd->vdev_ashift > spa->spa_max_ashift)
1458 spa->spa_max_ashift = vd->vdev_ashift;
1459 if (vd->vdev_ashift < spa->spa_min_ashift)
1460 spa->spa_min_ashift = vd->vdev_ashift;
1464 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1465 * resilver. But don't do this if we are doing a reopen for a scrub,
1466 * since this would just restart the scrub we are already doing.
1468 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
1469 vdev_resilver_needed(vd, NULL, NULL))
1470 spa_async_request(spa, SPA_ASYNC_RESILVER);
1476 * Called once the vdevs are all opened, this routine validates the label
1477 * contents. This needs to be done before vdev_load() so that we don't
1478 * inadvertently do repair I/Os to the wrong device.
1480 * If 'strict' is false ignore the spa guid check. This is necessary because
1481 * if the machine crashed during a re-guid the new guid might have been written
1482 * to all of the vdev labels, but not the cached config. The strict check
1483 * will be performed when the pool is opened again using the mos config.
1485 * This function will only return failure if one of the vdevs indicates that it
1486 * has since been destroyed or exported. This is only possible if
1487 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1488 * will be updated but the function will return 0.
1491 vdev_validate(vdev_t *vd, boolean_t strict)
1493 spa_t *spa = vd->vdev_spa;
1495 uint64_t guid = 0, top_guid;
1498 for (int c = 0; c < vd->vdev_children; c++)
1499 if (vdev_validate(vd->vdev_child[c], strict) != 0)
1500 return (SET_ERROR(EBADF));
1503 * If the device has already failed, or was marked offline, don't do
1504 * any further validation. Otherwise, label I/O will fail and we will
1505 * overwrite the previous state.
1507 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1508 uint64_t aux_guid = 0;
1510 uint64_t txg = spa_last_synced_txg(spa) != 0 ?
1511 spa_last_synced_txg(spa) : -1ULL;
1513 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1514 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1515 VDEV_AUX_BAD_LABEL);
1520 * Determine if this vdev has been split off into another
1521 * pool. If so, then refuse to open it.
1523 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
1524 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
1525 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1526 VDEV_AUX_SPLIT_POOL);
1531 if (strict && (nvlist_lookup_uint64(label,
1532 ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1533 guid != spa_guid(spa))) {
1534 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1535 VDEV_AUX_CORRUPT_DATA);
1540 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
1541 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
1546 * If this vdev just became a top-level vdev because its
1547 * sibling was detached, it will have adopted the parent's
1548 * vdev guid -- but the label may or may not be on disk yet.
1549 * Fortunately, either version of the label will have the
1550 * same top guid, so if we're a top-level vdev, we can
1551 * safely compare to that instead.
1553 * If we split this vdev off instead, then we also check the
1554 * original pool's guid. We don't want to consider the vdev
1555 * corrupt if it is partway through a split operation.
1557 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1559 nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1561 ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
1562 (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1563 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1564 VDEV_AUX_CORRUPT_DATA);
1569 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1571 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1572 VDEV_AUX_CORRUPT_DATA);
1580 * If this is a verbatim import, no need to check the
1581 * state of the pool.
1583 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1584 spa_load_state(spa) == SPA_LOAD_OPEN &&
1585 state != POOL_STATE_ACTIVE)
1586 return (SET_ERROR(EBADF));
1589 * If we were able to open and validate a vdev that was
1590 * previously marked permanently unavailable, clear that state
1593 if (vd->vdev_not_present)
1594 vd->vdev_not_present = 0;
1601 * Close a virtual device.
1604 vdev_close(vdev_t *vd)
1606 spa_t *spa = vd->vdev_spa;
1607 vdev_t *pvd = vd->vdev_parent;
1609 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1612 * If our parent is reopening, then we are as well, unless we are
1615 if (pvd != NULL && pvd->vdev_reopening)
1616 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1618 vd->vdev_ops->vdev_op_close(vd);
1620 vdev_cache_purge(vd);
1622 if (vd->vdev_ops->vdev_op_leaf)
1623 trim_map_destroy(vd);
1626 * We record the previous state before we close it, so that if we are
1627 * doing a reopen(), we don't generate FMA ereports if we notice that
1628 * it's still faulted.
1630 vd->vdev_prevstate = vd->vdev_state;
1632 if (vd->vdev_offline)
1633 vd->vdev_state = VDEV_STATE_OFFLINE;
1635 vd->vdev_state = VDEV_STATE_CLOSED;
1636 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1640 vdev_hold(vdev_t *vd)
1642 spa_t *spa = vd->vdev_spa;
1644 ASSERT(spa_is_root(spa));
1645 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1648 for (int c = 0; c < vd->vdev_children; c++)
1649 vdev_hold(vd->vdev_child[c]);
1651 if (vd->vdev_ops->vdev_op_leaf)
1652 vd->vdev_ops->vdev_op_hold(vd);
1656 vdev_rele(vdev_t *vd)
1658 spa_t *spa = vd->vdev_spa;
1660 ASSERT(spa_is_root(spa));
1661 for (int c = 0; c < vd->vdev_children; c++)
1662 vdev_rele(vd->vdev_child[c]);
1664 if (vd->vdev_ops->vdev_op_leaf)
1665 vd->vdev_ops->vdev_op_rele(vd);
1669 * Reopen all interior vdevs and any unopened leaves. We don't actually
1670 * reopen leaf vdevs which had previously been opened as they might deadlock
1671 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
1672 * If the leaf has never been opened then open it, as usual.
1675 vdev_reopen(vdev_t *vd)
1677 spa_t *spa = vd->vdev_spa;
1679 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1681 /* set the reopening flag unless we're taking the vdev offline */
1682 vd->vdev_reopening = !vd->vdev_offline;
1684 (void) vdev_open(vd);
1687 * Call vdev_validate() here to make sure we have the same device.
1688 * Otherwise, a device with an invalid label could be successfully
1689 * opened in response to vdev_reopen().
1692 (void) vdev_validate_aux(vd);
1693 if (vdev_readable(vd) && vdev_writeable(vd) &&
1694 vd->vdev_aux == &spa->spa_l2cache &&
1695 !l2arc_vdev_present(vd))
1696 l2arc_add_vdev(spa, vd);
1698 (void) vdev_validate(vd, B_TRUE);
1702 * Reassess parent vdev's health.
1704 vdev_propagate_state(vd);
1708 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1713 * Normally, partial opens (e.g. of a mirror) are allowed.
1714 * For a create, however, we want to fail the request if
1715 * there are any components we can't open.
1717 error = vdev_open(vd);
1719 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1721 return (error ? error : ENXIO);
1725 * Recursively load DTLs and initialize all labels.
1727 if ((error = vdev_dtl_load(vd)) != 0 ||
1728 (error = vdev_label_init(vd, txg, isreplacing ?
1729 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1738 vdev_metaslab_set_size(vdev_t *vd)
1741 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
1743 vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev);
1744 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1748 * Maximize performance by inflating the configured ashift for top level
1749 * vdevs to be as close to the physical ashift as possible while maintaining
1750 * administrator defined limits and ensuring it doesn't go below the
1754 vdev_ashift_optimize(vdev_t *vd)
1756 if (vd == vd->vdev_top) {
1757 if (vd->vdev_ashift < vd->vdev_physical_ashift) {
1758 vd->vdev_ashift = MIN(
1759 MAX(zfs_max_auto_ashift, vd->vdev_ashift),
1760 MAX(zfs_min_auto_ashift, vd->vdev_physical_ashift));
1763 * Unusual case where logical ashift > physical ashift
1764 * so we can't cap the calculated ashift based on max
1765 * ashift as that would cause failures.
1766 * We still check if we need to increase it to match
1769 vd->vdev_ashift = MAX(zfs_min_auto_ashift,
1776 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1778 ASSERT(vd == vd->vdev_top);
1779 ASSERT(!vd->vdev_ishole);
1780 ASSERT(ISP2(flags));
1781 ASSERT(spa_writeable(vd->vdev_spa));
1783 if (flags & VDD_METASLAB)
1784 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1786 if (flags & VDD_DTL)
1787 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1789 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1793 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
1795 for (int c = 0; c < vd->vdev_children; c++)
1796 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
1798 if (vd->vdev_ops->vdev_op_leaf)
1799 vdev_dirty(vd->vdev_top, flags, vd, txg);
1805 * A vdev's DTL (dirty time log) is the set of transaction groups for which
1806 * the vdev has less than perfect replication. There are four kinds of DTL:
1808 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
1810 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
1812 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
1813 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
1814 * txgs that was scrubbed.
1816 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
1817 * persistent errors or just some device being offline.
1818 * Unlike the other three, the DTL_OUTAGE map is not generally
1819 * maintained; it's only computed when needed, typically to
1820 * determine whether a device can be detached.
1822 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
1823 * either has the data or it doesn't.
1825 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
1826 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
1827 * if any child is less than fully replicated, then so is its parent.
1828 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
1829 * comprising only those txgs which appear in 'maxfaults' or more children;
1830 * those are the txgs we don't have enough replication to read. For example,
1831 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
1832 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
1833 * two child DTL_MISSING maps.
1835 * It should be clear from the above that to compute the DTLs and outage maps
1836 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
1837 * Therefore, that is all we keep on disk. When loading the pool, or after
1838 * a configuration change, we generate all other DTLs from first principles.
1841 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1843 range_tree_t *rt = vd->vdev_dtl[t];
1845 ASSERT(t < DTL_TYPES);
1846 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1847 ASSERT(spa_writeable(vd->vdev_spa));
1849 mutex_enter(rt->rt_lock);
1850 if (!range_tree_contains(rt, txg, size))
1851 range_tree_add(rt, txg, size);
1852 mutex_exit(rt->rt_lock);
1856 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1858 range_tree_t *rt = vd->vdev_dtl[t];
1859 boolean_t dirty = B_FALSE;
1861 ASSERT(t < DTL_TYPES);
1862 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1864 mutex_enter(rt->rt_lock);
1865 if (range_tree_space(rt) != 0)
1866 dirty = range_tree_contains(rt, txg, size);
1867 mutex_exit(rt->rt_lock);
1873 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
1875 range_tree_t *rt = vd->vdev_dtl[t];
1878 mutex_enter(rt->rt_lock);
1879 empty = (range_tree_space(rt) == 0);
1880 mutex_exit(rt->rt_lock);
1886 * Returns the lowest txg in the DTL range.
1889 vdev_dtl_min(vdev_t *vd)
1893 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1894 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1895 ASSERT0(vd->vdev_children);
1897 rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
1898 return (rs->rs_start - 1);
1902 * Returns the highest txg in the DTL.
1905 vdev_dtl_max(vdev_t *vd)
1909 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1910 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1911 ASSERT0(vd->vdev_children);
1913 rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
1914 return (rs->rs_end);
1918 * Determine if a resilvering vdev should remove any DTL entries from
1919 * its range. If the vdev was resilvering for the entire duration of the
1920 * scan then it should excise that range from its DTLs. Otherwise, this
1921 * vdev is considered partially resilvered and should leave its DTL
1922 * entries intact. The comment in vdev_dtl_reassess() describes how we
1926 vdev_dtl_should_excise(vdev_t *vd)
1928 spa_t *spa = vd->vdev_spa;
1929 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1931 ASSERT0(scn->scn_phys.scn_errors);
1932 ASSERT0(vd->vdev_children);
1934 if (vd->vdev_state < VDEV_STATE_DEGRADED)
1937 if (vd->vdev_resilver_txg == 0 ||
1938 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0)
1942 * When a resilver is initiated the scan will assign the scn_max_txg
1943 * value to the highest txg value that exists in all DTLs. If this
1944 * device's max DTL is not part of this scan (i.e. it is not in
1945 * the range (scn_min_txg, scn_max_txg] then it is not eligible
1948 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
1949 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
1950 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
1951 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
1958 * Reassess DTLs after a config change or scrub completion.
1961 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1963 spa_t *spa = vd->vdev_spa;
1967 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1969 for (int c = 0; c < vd->vdev_children; c++)
1970 vdev_dtl_reassess(vd->vdev_child[c], txg,
1971 scrub_txg, scrub_done);
1973 if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
1976 if (vd->vdev_ops->vdev_op_leaf) {
1977 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1979 mutex_enter(&vd->vdev_dtl_lock);
1982 * If we've completed a scan cleanly then determine
1983 * if this vdev should remove any DTLs. We only want to
1984 * excise regions on vdevs that were available during
1985 * the entire duration of this scan.
1987 if (scrub_txg != 0 &&
1988 (spa->spa_scrub_started ||
1989 (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
1990 vdev_dtl_should_excise(vd)) {
1992 * We completed a scrub up to scrub_txg. If we
1993 * did it without rebooting, then the scrub dtl
1994 * will be valid, so excise the old region and
1995 * fold in the scrub dtl. Otherwise, leave the
1996 * dtl as-is if there was an error.
1998 * There's little trick here: to excise the beginning
1999 * of the DTL_MISSING map, we put it into a reference
2000 * tree and then add a segment with refcnt -1 that
2001 * covers the range [0, scrub_txg). This means
2002 * that each txg in that range has refcnt -1 or 0.
2003 * We then add DTL_SCRUB with a refcnt of 2, so that
2004 * entries in the range [0, scrub_txg) will have a
2005 * positive refcnt -- either 1 or 2. We then convert
2006 * the reference tree into the new DTL_MISSING map.
2008 space_reftree_create(&reftree);
2009 space_reftree_add_map(&reftree,
2010 vd->vdev_dtl[DTL_MISSING], 1);
2011 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
2012 space_reftree_add_map(&reftree,
2013 vd->vdev_dtl[DTL_SCRUB], 2);
2014 space_reftree_generate_map(&reftree,
2015 vd->vdev_dtl[DTL_MISSING], 1);
2016 space_reftree_destroy(&reftree);
2018 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
2019 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
2020 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
2022 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
2023 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
2024 if (!vdev_readable(vd))
2025 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
2027 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
2028 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
2031 * If the vdev was resilvering and no longer has any
2032 * DTLs then reset its resilvering flag and dirty
2033 * the top level so that we persist the change.
2035 if (vd->vdev_resilver_txg != 0 &&
2036 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 &&
2037 range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0) {
2038 vd->vdev_resilver_txg = 0;
2039 vdev_config_dirty(vd->vdev_top);
2042 mutex_exit(&vd->vdev_dtl_lock);
2045 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2049 mutex_enter(&vd->vdev_dtl_lock);
2050 for (int t = 0; t < DTL_TYPES; t++) {
2051 /* account for child's outage in parent's missing map */
2052 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
2054 continue; /* leaf vdevs only */
2055 if (t == DTL_PARTIAL)
2056 minref = 1; /* i.e. non-zero */
2057 else if (vd->vdev_nparity != 0)
2058 minref = vd->vdev_nparity + 1; /* RAID-Z */
2060 minref = vd->vdev_children; /* any kind of mirror */
2061 space_reftree_create(&reftree);
2062 for (int c = 0; c < vd->vdev_children; c++) {
2063 vdev_t *cvd = vd->vdev_child[c];
2064 mutex_enter(&cvd->vdev_dtl_lock);
2065 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
2066 mutex_exit(&cvd->vdev_dtl_lock);
2068 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
2069 space_reftree_destroy(&reftree);
2071 mutex_exit(&vd->vdev_dtl_lock);
2075 vdev_dtl_load(vdev_t *vd)
2077 spa_t *spa = vd->vdev_spa;
2078 objset_t *mos = spa->spa_meta_objset;
2081 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
2082 ASSERT(!vd->vdev_ishole);
2084 error = space_map_open(&vd->vdev_dtl_sm, mos,
2085 vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock);
2088 ASSERT(vd->vdev_dtl_sm != NULL);
2090 mutex_enter(&vd->vdev_dtl_lock);
2093 * Now that we've opened the space_map we need to update
2096 space_map_update(vd->vdev_dtl_sm);
2098 error = space_map_load(vd->vdev_dtl_sm,
2099 vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
2100 mutex_exit(&vd->vdev_dtl_lock);
2105 for (int c = 0; c < vd->vdev_children; c++) {
2106 error = vdev_dtl_load(vd->vdev_child[c]);
2115 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
2117 spa_t *spa = vd->vdev_spa;
2119 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
2120 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2125 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
2127 spa_t *spa = vd->vdev_spa;
2128 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
2129 DMU_OT_NONE, 0, tx);
2132 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2139 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
2141 if (vd->vdev_ops != &vdev_hole_ops &&
2142 vd->vdev_ops != &vdev_missing_ops &&
2143 vd->vdev_ops != &vdev_root_ops &&
2144 !vd->vdev_top->vdev_removing) {
2145 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
2146 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
2148 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
2149 vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
2152 for (uint64_t i = 0; i < vd->vdev_children; i++) {
2153 vdev_construct_zaps(vd->vdev_child[i], tx);
2158 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
2160 spa_t *spa = vd->vdev_spa;
2161 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2162 objset_t *mos = spa->spa_meta_objset;
2163 range_tree_t *rtsync;
2166 uint64_t object = space_map_object(vd->vdev_dtl_sm);
2168 ASSERT(!vd->vdev_ishole);
2169 ASSERT(vd->vdev_ops->vdev_op_leaf);
2171 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2173 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
2174 mutex_enter(&vd->vdev_dtl_lock);
2175 space_map_free(vd->vdev_dtl_sm, tx);
2176 space_map_close(vd->vdev_dtl_sm);
2177 vd->vdev_dtl_sm = NULL;
2178 mutex_exit(&vd->vdev_dtl_lock);
2181 * We only destroy the leaf ZAP for detached leaves or for
2182 * removed log devices. Removed data devices handle leaf ZAP
2183 * cleanup later, once cancellation is no longer possible.
2185 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2186 vd->vdev_top->vdev_islog)) {
2187 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2188 vd->vdev_leaf_zap = 0;
2195 if (vd->vdev_dtl_sm == NULL) {
2196 uint64_t new_object;
2198 new_object = space_map_alloc(mos, tx);
2199 VERIFY3U(new_object, !=, 0);
2201 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
2202 0, -1ULL, 0, &vd->vdev_dtl_lock));
2203 ASSERT(vd->vdev_dtl_sm != NULL);
2206 bzero(&rtlock, sizeof(rtlock));
2207 mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL);
2209 rtsync = range_tree_create(NULL, NULL, &rtlock);
2211 mutex_enter(&rtlock);
2213 mutex_enter(&vd->vdev_dtl_lock);
2214 range_tree_walk(rt, range_tree_add, rtsync);
2215 mutex_exit(&vd->vdev_dtl_lock);
2217 space_map_truncate(vd->vdev_dtl_sm, tx);
2218 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
2219 range_tree_vacate(rtsync, NULL, NULL);
2221 range_tree_destroy(rtsync);
2223 mutex_exit(&rtlock);
2224 mutex_destroy(&rtlock);
2227 * If the object for the space map has changed then dirty
2228 * the top level so that we update the config.
2230 if (object != space_map_object(vd->vdev_dtl_sm)) {
2231 zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, "
2232 "new object %llu", txg, spa_name(spa), object,
2233 space_map_object(vd->vdev_dtl_sm));
2234 vdev_config_dirty(vd->vdev_top);
2239 mutex_enter(&vd->vdev_dtl_lock);
2240 space_map_update(vd->vdev_dtl_sm);
2241 mutex_exit(&vd->vdev_dtl_lock);
2245 * Determine whether the specified vdev can be offlined/detached/removed
2246 * without losing data.
2249 vdev_dtl_required(vdev_t *vd)
2251 spa_t *spa = vd->vdev_spa;
2252 vdev_t *tvd = vd->vdev_top;
2253 uint8_t cant_read = vd->vdev_cant_read;
2256 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2258 if (vd == spa->spa_root_vdev || vd == tvd)
2262 * Temporarily mark the device as unreadable, and then determine
2263 * whether this results in any DTL outages in the top-level vdev.
2264 * If not, we can safely offline/detach/remove the device.
2266 vd->vdev_cant_read = B_TRUE;
2267 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2268 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
2269 vd->vdev_cant_read = cant_read;
2270 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2272 if (!required && zio_injection_enabled)
2273 required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2279 * Determine if resilver is needed, and if so the txg range.
2282 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2284 boolean_t needed = B_FALSE;
2285 uint64_t thismin = UINT64_MAX;
2286 uint64_t thismax = 0;
2288 if (vd->vdev_children == 0) {
2289 mutex_enter(&vd->vdev_dtl_lock);
2290 if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 &&
2291 vdev_writeable(vd)) {
2293 thismin = vdev_dtl_min(vd);
2294 thismax = vdev_dtl_max(vd);
2297 mutex_exit(&vd->vdev_dtl_lock);
2299 for (int c = 0; c < vd->vdev_children; c++) {
2300 vdev_t *cvd = vd->vdev_child[c];
2301 uint64_t cmin, cmax;
2303 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2304 thismin = MIN(thismin, cmin);
2305 thismax = MAX(thismax, cmax);
2311 if (needed && minp) {
2319 vdev_load(vdev_t *vd)
2322 * Recursively load all children.
2324 for (int c = 0; c < vd->vdev_children; c++)
2325 vdev_load(vd->vdev_child[c]);
2328 * If this is a top-level vdev, initialize its metaslabs.
2330 if (vd == vd->vdev_top && !vd->vdev_ishole &&
2331 (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
2332 vdev_metaslab_init(vd, 0) != 0))
2333 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2334 VDEV_AUX_CORRUPT_DATA);
2337 * If this is a leaf vdev, load its DTL.
2339 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
2340 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2341 VDEV_AUX_CORRUPT_DATA);
2345 * The special vdev case is used for hot spares and l2cache devices. Its
2346 * sole purpose it to set the vdev state for the associated vdev. To do this,
2347 * we make sure that we can open the underlying device, then try to read the
2348 * label, and make sure that the label is sane and that it hasn't been
2349 * repurposed to another pool.
2352 vdev_validate_aux(vdev_t *vd)
2355 uint64_t guid, version;
2358 if (!vdev_readable(vd))
2361 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
2362 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2363 VDEV_AUX_CORRUPT_DATA);
2367 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2368 !SPA_VERSION_IS_SUPPORTED(version) ||
2369 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
2370 guid != vd->vdev_guid ||
2371 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
2372 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2373 VDEV_AUX_CORRUPT_DATA);
2379 * We don't actually check the pool state here. If it's in fact in
2380 * use by another pool, we update this fact on the fly when requested.
2387 vdev_remove(vdev_t *vd, uint64_t txg)
2389 spa_t *spa = vd->vdev_spa;
2390 objset_t *mos = spa->spa_meta_objset;
2393 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
2394 ASSERT(vd == vd->vdev_top);
2395 ASSERT3U(txg, ==, spa_syncing_txg(spa));
2397 if (vd->vdev_ms != NULL) {
2398 metaslab_group_t *mg = vd->vdev_mg;
2400 metaslab_group_histogram_verify(mg);
2401 metaslab_class_histogram_verify(mg->mg_class);
2403 for (int m = 0; m < vd->vdev_ms_count; m++) {
2404 metaslab_t *msp = vd->vdev_ms[m];
2406 if (msp == NULL || msp->ms_sm == NULL)
2409 mutex_enter(&msp->ms_lock);
2411 * If the metaslab was not loaded when the vdev
2412 * was removed then the histogram accounting may
2413 * not be accurate. Update the histogram information
2414 * here so that we ensure that the metaslab group
2415 * and metaslab class are up-to-date.
2417 metaslab_group_histogram_remove(mg, msp);
2419 VERIFY0(space_map_allocated(msp->ms_sm));
2420 space_map_free(msp->ms_sm, tx);
2421 space_map_close(msp->ms_sm);
2423 mutex_exit(&msp->ms_lock);
2426 metaslab_group_histogram_verify(mg);
2427 metaslab_class_histogram_verify(mg->mg_class);
2428 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
2429 ASSERT0(mg->mg_histogram[i]);
2433 if (vd->vdev_ms_array) {
2434 (void) dmu_object_free(mos, vd->vdev_ms_array, tx);
2435 vd->vdev_ms_array = 0;
2438 if (vd->vdev_islog && vd->vdev_top_zap != 0) {
2439 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
2440 vd->vdev_top_zap = 0;
2446 vdev_sync_done(vdev_t *vd, uint64_t txg)
2449 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2451 ASSERT(!vd->vdev_ishole);
2453 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2454 metaslab_sync_done(msp, txg);
2457 metaslab_sync_reassess(vd->vdev_mg);
2461 vdev_sync(vdev_t *vd, uint64_t txg)
2463 spa_t *spa = vd->vdev_spa;
2468 ASSERT(!vd->vdev_ishole);
2470 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
2471 ASSERT(vd == vd->vdev_top);
2472 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2473 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2474 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2475 ASSERT(vd->vdev_ms_array != 0);
2476 vdev_config_dirty(vd);
2481 * Remove the metadata associated with this vdev once it's empty.
2483 if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
2484 vdev_remove(vd, txg);
2486 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2487 metaslab_sync(msp, txg);
2488 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2491 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2492 vdev_dtl_sync(lvd, txg);
2494 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2498 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2500 return (vd->vdev_ops->vdev_op_asize(vd, psize));
2504 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
2505 * not be opened, and no I/O is attempted.
2508 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2512 spa_vdev_state_enter(spa, SCL_NONE);
2514 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2515 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2517 if (!vd->vdev_ops->vdev_op_leaf)
2518 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2523 * We don't directly use the aux state here, but if we do a
2524 * vdev_reopen(), we need this value to be present to remember why we
2527 vd->vdev_label_aux = aux;
2530 * Faulted state takes precedence over degraded.
2532 vd->vdev_delayed_close = B_FALSE;
2533 vd->vdev_faulted = 1ULL;
2534 vd->vdev_degraded = 0ULL;
2535 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
2538 * If this device has the only valid copy of the data, then
2539 * back off and simply mark the vdev as degraded instead.
2541 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
2542 vd->vdev_degraded = 1ULL;
2543 vd->vdev_faulted = 0ULL;
2546 * If we reopen the device and it's not dead, only then do we
2551 if (vdev_readable(vd))
2552 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
2555 return (spa_vdev_state_exit(spa, vd, 0));
2559 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
2560 * user that something is wrong. The vdev continues to operate as normal as far
2561 * as I/O is concerned.
2564 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2568 spa_vdev_state_enter(spa, SCL_NONE);
2570 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2571 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2573 if (!vd->vdev_ops->vdev_op_leaf)
2574 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2577 * If the vdev is already faulted, then don't do anything.
2579 if (vd->vdev_faulted || vd->vdev_degraded)
2580 return (spa_vdev_state_exit(spa, NULL, 0));
2582 vd->vdev_degraded = 1ULL;
2583 if (!vdev_is_dead(vd))
2584 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2587 return (spa_vdev_state_exit(spa, vd, 0));
2591 * Online the given vdev.
2593 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
2594 * spare device should be detached when the device finishes resilvering.
2595 * Second, the online should be treated like a 'test' online case, so no FMA
2596 * events are generated if the device fails to open.
2599 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
2601 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
2602 boolean_t wasoffline;
2603 vdev_state_t oldstate;
2605 spa_vdev_state_enter(spa, SCL_NONE);
2607 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2608 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2610 if (!vd->vdev_ops->vdev_op_leaf)
2611 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2613 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
2614 oldstate = vd->vdev_state;
2617 vd->vdev_offline = B_FALSE;
2618 vd->vdev_tmpoffline = B_FALSE;
2619 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
2620 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
2622 /* XXX - L2ARC 1.0 does not support expansion */
2623 if (!vd->vdev_aux) {
2624 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2625 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
2629 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
2631 if (!vd->vdev_aux) {
2632 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2633 pvd->vdev_expanding = B_FALSE;
2637 *newstate = vd->vdev_state;
2638 if ((flags & ZFS_ONLINE_UNSPARE) &&
2639 !vdev_is_dead(vd) && vd->vdev_parent &&
2640 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2641 vd->vdev_parent->vdev_child[0] == vd)
2642 vd->vdev_unspare = B_TRUE;
2644 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
2646 /* XXX - L2ARC 1.0 does not support expansion */
2648 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
2649 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2653 (oldstate < VDEV_STATE_DEGRADED &&
2654 vd->vdev_state >= VDEV_STATE_DEGRADED))
2655 spa_event_notify(spa, vd, ESC_ZFS_VDEV_ONLINE);
2657 return (spa_vdev_state_exit(spa, vd, 0));
2661 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2665 uint64_t generation;
2666 metaslab_group_t *mg;
2669 spa_vdev_state_enter(spa, SCL_ALLOC);
2671 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2672 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2674 if (!vd->vdev_ops->vdev_op_leaf)
2675 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2679 generation = spa->spa_config_generation + 1;
2682 * If the device isn't already offline, try to offline it.
2684 if (!vd->vdev_offline) {
2686 * If this device has the only valid copy of some data,
2687 * don't allow it to be offlined. Log devices are always
2690 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2691 vdev_dtl_required(vd))
2692 return (spa_vdev_state_exit(spa, NULL, EBUSY));
2695 * If the top-level is a slog and it has had allocations
2696 * then proceed. We check that the vdev's metaslab group
2697 * is not NULL since it's possible that we may have just
2698 * added this vdev but not yet initialized its metaslabs.
2700 if (tvd->vdev_islog && mg != NULL) {
2702 * Prevent any future allocations.
2704 metaslab_group_passivate(mg);
2705 (void) spa_vdev_state_exit(spa, vd, 0);
2707 error = spa_offline_log(spa);
2709 spa_vdev_state_enter(spa, SCL_ALLOC);
2712 * Check to see if the config has changed.
2714 if (error || generation != spa->spa_config_generation) {
2715 metaslab_group_activate(mg);
2717 return (spa_vdev_state_exit(spa,
2719 (void) spa_vdev_state_exit(spa, vd, 0);
2722 ASSERT0(tvd->vdev_stat.vs_alloc);
2726 * Offline this device and reopen its top-level vdev.
2727 * If the top-level vdev is a log device then just offline
2728 * it. Otherwise, if this action results in the top-level
2729 * vdev becoming unusable, undo it and fail the request.
2731 vd->vdev_offline = B_TRUE;
2734 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2735 vdev_is_dead(tvd)) {
2736 vd->vdev_offline = B_FALSE;
2738 return (spa_vdev_state_exit(spa, NULL, EBUSY));
2742 * Add the device back into the metaslab rotor so that
2743 * once we online the device it's open for business.
2745 if (tvd->vdev_islog && mg != NULL)
2746 metaslab_group_activate(mg);
2749 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
2751 return (spa_vdev_state_exit(spa, vd, 0));
2755 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
2759 mutex_enter(&spa->spa_vdev_top_lock);
2760 error = vdev_offline_locked(spa, guid, flags);
2761 mutex_exit(&spa->spa_vdev_top_lock);
2767 * Clear the error counts associated with this vdev. Unlike vdev_online() and
2768 * vdev_offline(), we assume the spa config is locked. We also clear all
2769 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
2772 vdev_clear(spa_t *spa, vdev_t *vd)
2774 vdev_t *rvd = spa->spa_root_vdev;
2776 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2781 vd->vdev_stat.vs_read_errors = 0;
2782 vd->vdev_stat.vs_write_errors = 0;
2783 vd->vdev_stat.vs_checksum_errors = 0;
2785 for (int c = 0; c < vd->vdev_children; c++)
2786 vdev_clear(spa, vd->vdev_child[c]);
2789 for (int c = 0; c < spa->spa_l2cache.sav_count; c++)
2790 vdev_clear(spa, spa->spa_l2cache.sav_vdevs[c]);
2792 for (int c = 0; c < spa->spa_spares.sav_count; c++)
2793 vdev_clear(spa, spa->spa_spares.sav_vdevs[c]);
2797 * If we're in the FAULTED state or have experienced failed I/O, then
2798 * clear the persistent state and attempt to reopen the device. We
2799 * also mark the vdev config dirty, so that the new faulted state is
2800 * written out to disk.
2802 if (vd->vdev_faulted || vd->vdev_degraded ||
2803 !vdev_readable(vd) || !vdev_writeable(vd)) {
2806 * When reopening in reponse to a clear event, it may be due to
2807 * a fmadm repair request. In this case, if the device is
2808 * still broken, we want to still post the ereport again.
2810 vd->vdev_forcefault = B_TRUE;
2812 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
2813 vd->vdev_cant_read = B_FALSE;
2814 vd->vdev_cant_write = B_FALSE;
2816 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
2818 vd->vdev_forcefault = B_FALSE;
2820 if (vd != rvd && vdev_writeable(vd->vdev_top))
2821 vdev_state_dirty(vd->vdev_top);
2823 if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
2824 spa_async_request(spa, SPA_ASYNC_RESILVER);
2826 spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
2830 * When clearing a FMA-diagnosed fault, we always want to
2831 * unspare the device, as we assume that the original spare was
2832 * done in response to the FMA fault.
2834 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
2835 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2836 vd->vdev_parent->vdev_child[0] == vd)
2837 vd->vdev_unspare = B_TRUE;
2841 vdev_is_dead(vdev_t *vd)
2844 * Holes and missing devices are always considered "dead".
2845 * This simplifies the code since we don't have to check for
2846 * these types of devices in the various code paths.
2847 * Instead we rely on the fact that we skip over dead devices
2848 * before issuing I/O to them.
2850 return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
2851 vd->vdev_ops == &vdev_missing_ops);
2855 vdev_readable(vdev_t *vd)
2857 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
2861 vdev_writeable(vdev_t *vd)
2863 return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
2867 vdev_allocatable(vdev_t *vd)
2869 uint64_t state = vd->vdev_state;
2872 * We currently allow allocations from vdevs which may be in the
2873 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
2874 * fails to reopen then we'll catch it later when we're holding
2875 * the proper locks. Note that we have to get the vdev state
2876 * in a local variable because although it changes atomically,
2877 * we're asking two separate questions about it.
2879 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
2880 !vd->vdev_cant_write && !vd->vdev_ishole &&
2881 vd->vdev_mg->mg_initialized);
2885 vdev_accessible(vdev_t *vd, zio_t *zio)
2887 ASSERT(zio->io_vd == vd);
2889 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
2892 if (zio->io_type == ZIO_TYPE_READ)
2893 return (!vd->vdev_cant_read);
2895 if (zio->io_type == ZIO_TYPE_WRITE)
2896 return (!vd->vdev_cant_write);
2902 * Get statistics for the given vdev.
2905 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2907 spa_t *spa = vd->vdev_spa;
2908 vdev_t *rvd = spa->spa_root_vdev;
2909 vdev_t *tvd = vd->vdev_top;
2911 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2913 mutex_enter(&vd->vdev_stat_lock);
2914 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
2915 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2916 vs->vs_state = vd->vdev_state;
2917 vs->vs_rsize = vdev_get_min_asize(vd);
2918 if (vd->vdev_ops->vdev_op_leaf)
2919 vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2921 * Report expandable space on top-level, non-auxillary devices only.
2922 * The expandable space is reported in terms of metaslab sized units
2923 * since that determines how much space the pool can expand.
2925 if (vd->vdev_aux == NULL && tvd != NULL && vd->vdev_max_asize != 0) {
2926 vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize,
2927 1ULL << tvd->vdev_ms_shift);
2929 vs->vs_configured_ashift = vd->vdev_top != NULL
2930 ? vd->vdev_top->vdev_ashift : vd->vdev_ashift;
2931 vs->vs_logical_ashift = vd->vdev_logical_ashift;
2932 vs->vs_physical_ashift = vd->vdev_physical_ashift;
2933 if (vd->vdev_aux == NULL && vd == vd->vdev_top && !vd->vdev_ishole) {
2934 vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
2938 * If we're getting stats on the root vdev, aggregate the I/O counts
2939 * over all top-level vdevs (i.e. the direct children of the root).
2942 for (int c = 0; c < rvd->vdev_children; c++) {
2943 vdev_t *cvd = rvd->vdev_child[c];
2944 vdev_stat_t *cvs = &cvd->vdev_stat;
2946 for (int t = 0; t < ZIO_TYPES; t++) {
2947 vs->vs_ops[t] += cvs->vs_ops[t];
2948 vs->vs_bytes[t] += cvs->vs_bytes[t];
2950 cvs->vs_scan_removing = cvd->vdev_removing;
2953 mutex_exit(&vd->vdev_stat_lock);
2957 vdev_clear_stats(vdev_t *vd)
2959 mutex_enter(&vd->vdev_stat_lock);
2960 vd->vdev_stat.vs_space = 0;
2961 vd->vdev_stat.vs_dspace = 0;
2962 vd->vdev_stat.vs_alloc = 0;
2963 mutex_exit(&vd->vdev_stat_lock);
2967 vdev_scan_stat_init(vdev_t *vd)
2969 vdev_stat_t *vs = &vd->vdev_stat;
2971 for (int c = 0; c < vd->vdev_children; c++)
2972 vdev_scan_stat_init(vd->vdev_child[c]);
2974 mutex_enter(&vd->vdev_stat_lock);
2975 vs->vs_scan_processed = 0;
2976 mutex_exit(&vd->vdev_stat_lock);
2980 vdev_stat_update(zio_t *zio, uint64_t psize)
2982 spa_t *spa = zio->io_spa;
2983 vdev_t *rvd = spa->spa_root_vdev;
2984 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2986 uint64_t txg = zio->io_txg;
2987 vdev_stat_t *vs = &vd->vdev_stat;
2988 zio_type_t type = zio->io_type;
2989 int flags = zio->io_flags;
2992 * If this i/o is a gang leader, it didn't do any actual work.
2994 if (zio->io_gang_tree)
2997 if (zio->io_error == 0) {
2999 * If this is a root i/o, don't count it -- we've already
3000 * counted the top-level vdevs, and vdev_get_stats() will
3001 * aggregate them when asked. This reduces contention on
3002 * the root vdev_stat_lock and implicitly handles blocks
3003 * that compress away to holes, for which there is no i/o.
3004 * (Holes never create vdev children, so all the counters
3005 * remain zero, which is what we want.)
3007 * Note: this only applies to successful i/o (io_error == 0)
3008 * because unlike i/o counts, errors are not additive.
3009 * When reading a ditto block, for example, failure of
3010 * one top-level vdev does not imply a root-level error.
3015 ASSERT(vd == zio->io_vd);
3017 if (flags & ZIO_FLAG_IO_BYPASS)
3020 mutex_enter(&vd->vdev_stat_lock);
3022 if (flags & ZIO_FLAG_IO_REPAIR) {
3023 if (flags & ZIO_FLAG_SCAN_THREAD) {
3024 dsl_scan_phys_t *scn_phys =
3025 &spa->spa_dsl_pool->dp_scan->scn_phys;
3026 uint64_t *processed = &scn_phys->scn_processed;
3029 if (vd->vdev_ops->vdev_op_leaf)
3030 atomic_add_64(processed, psize);
3031 vs->vs_scan_processed += psize;
3034 if (flags & ZIO_FLAG_SELF_HEAL)
3035 vs->vs_self_healed += psize;
3039 vs->vs_bytes[type] += psize;
3041 mutex_exit(&vd->vdev_stat_lock);
3045 if (flags & ZIO_FLAG_SPECULATIVE)
3049 * If this is an I/O error that is going to be retried, then ignore the
3050 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
3051 * hard errors, when in reality they can happen for any number of
3052 * innocuous reasons (bus resets, MPxIO link failure, etc).
3054 if (zio->io_error == EIO &&
3055 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
3059 * Intent logs writes won't propagate their error to the root
3060 * I/O so don't mark these types of failures as pool-level
3063 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
3066 mutex_enter(&vd->vdev_stat_lock);
3067 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
3068 if (zio->io_error == ECKSUM)
3069 vs->vs_checksum_errors++;
3071 vs->vs_read_errors++;
3073 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
3074 vs->vs_write_errors++;
3075 mutex_exit(&vd->vdev_stat_lock);
3077 if (type == ZIO_TYPE_WRITE && txg != 0 &&
3078 (!(flags & ZIO_FLAG_IO_REPAIR) ||
3079 (flags & ZIO_FLAG_SCAN_THREAD) ||
3080 spa->spa_claiming)) {
3082 * This is either a normal write (not a repair), or it's
3083 * a repair induced by the scrub thread, or it's a repair
3084 * made by zil_claim() during spa_load() in the first txg.
3085 * In the normal case, we commit the DTL change in the same
3086 * txg as the block was born. In the scrub-induced repair
3087 * case, we know that scrubs run in first-pass syncing context,
3088 * so we commit the DTL change in spa_syncing_txg(spa).
3089 * In the zil_claim() case, we commit in spa_first_txg(spa).
3091 * We currently do not make DTL entries for failed spontaneous
3092 * self-healing writes triggered by normal (non-scrubbing)
3093 * reads, because we have no transactional context in which to
3094 * do so -- and it's not clear that it'd be desirable anyway.
3096 if (vd->vdev_ops->vdev_op_leaf) {
3097 uint64_t commit_txg = txg;
3098 if (flags & ZIO_FLAG_SCAN_THREAD) {
3099 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3100 ASSERT(spa_sync_pass(spa) == 1);
3101 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
3102 commit_txg = spa_syncing_txg(spa);
3103 } else if (spa->spa_claiming) {
3104 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3105 commit_txg = spa_first_txg(spa);
3107 ASSERT(commit_txg >= spa_syncing_txg(spa));
3108 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
3110 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3111 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
3112 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
3115 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
3120 * Update the in-core space usage stats for this vdev, its metaslab class,
3121 * and the root vdev.
3124 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
3125 int64_t space_delta)
3127 int64_t dspace_delta = space_delta;
3128 spa_t *spa = vd->vdev_spa;
3129 vdev_t *rvd = spa->spa_root_vdev;
3130 metaslab_group_t *mg = vd->vdev_mg;
3131 metaslab_class_t *mc = mg ? mg->mg_class : NULL;
3133 ASSERT(vd == vd->vdev_top);
3136 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
3137 * factor. We must calculate this here and not at the root vdev
3138 * because the root vdev's psize-to-asize is simply the max of its
3139 * childrens', thus not accurate enough for us.
3141 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
3142 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
3143 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
3144 vd->vdev_deflate_ratio;
3146 mutex_enter(&vd->vdev_stat_lock);
3147 vd->vdev_stat.vs_alloc += alloc_delta;
3148 vd->vdev_stat.vs_space += space_delta;
3149 vd->vdev_stat.vs_dspace += dspace_delta;
3150 mutex_exit(&vd->vdev_stat_lock);
3152 if (mc == spa_normal_class(spa)) {
3153 mutex_enter(&rvd->vdev_stat_lock);
3154 rvd->vdev_stat.vs_alloc += alloc_delta;
3155 rvd->vdev_stat.vs_space += space_delta;
3156 rvd->vdev_stat.vs_dspace += dspace_delta;
3157 mutex_exit(&rvd->vdev_stat_lock);
3161 ASSERT(rvd == vd->vdev_parent);
3162 ASSERT(vd->vdev_ms_count != 0);
3164 metaslab_class_space_update(mc,
3165 alloc_delta, defer_delta, space_delta, dspace_delta);
3170 * Mark a top-level vdev's config as dirty, placing it on the dirty list
3171 * so that it will be written out next time the vdev configuration is synced.
3172 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3175 vdev_config_dirty(vdev_t *vd)
3177 spa_t *spa = vd->vdev_spa;
3178 vdev_t *rvd = spa->spa_root_vdev;
3181 ASSERT(spa_writeable(spa));
3184 * If this is an aux vdev (as with l2cache and spare devices), then we
3185 * update the vdev config manually and set the sync flag.
3187 if (vd->vdev_aux != NULL) {
3188 spa_aux_vdev_t *sav = vd->vdev_aux;
3192 for (c = 0; c < sav->sav_count; c++) {
3193 if (sav->sav_vdevs[c] == vd)
3197 if (c == sav->sav_count) {
3199 * We're being removed. There's nothing more to do.
3201 ASSERT(sav->sav_sync == B_TRUE);
3205 sav->sav_sync = B_TRUE;
3207 if (nvlist_lookup_nvlist_array(sav->sav_config,
3208 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
3209 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
3210 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
3216 * Setting the nvlist in the middle if the array is a little
3217 * sketchy, but it will work.
3219 nvlist_free(aux[c]);
3220 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
3226 * The dirty list is protected by the SCL_CONFIG lock. The caller
3227 * must either hold SCL_CONFIG as writer, or must be the sync thread
3228 * (which holds SCL_CONFIG as reader). There's only one sync thread,
3229 * so this is sufficient to ensure mutual exclusion.
3231 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3232 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3233 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3236 for (c = 0; c < rvd->vdev_children; c++)
3237 vdev_config_dirty(rvd->vdev_child[c]);
3239 ASSERT(vd == vd->vdev_top);
3241 if (!list_link_active(&vd->vdev_config_dirty_node) &&
3243 list_insert_head(&spa->spa_config_dirty_list, vd);
3248 vdev_config_clean(vdev_t *vd)
3250 spa_t *spa = vd->vdev_spa;
3252 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3253 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3254 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3256 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3257 list_remove(&spa->spa_config_dirty_list, vd);
3261 * Mark a top-level vdev's state as dirty, so that the next pass of
3262 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3263 * the state changes from larger config changes because they require
3264 * much less locking, and are often needed for administrative actions.
3267 vdev_state_dirty(vdev_t *vd)
3269 spa_t *spa = vd->vdev_spa;
3271 ASSERT(spa_writeable(spa));
3272 ASSERT(vd == vd->vdev_top);
3275 * The state list is protected by the SCL_STATE lock. The caller
3276 * must either hold SCL_STATE as writer, or must be the sync thread
3277 * (which holds SCL_STATE as reader). There's only one sync thread,
3278 * so this is sufficient to ensure mutual exclusion.
3280 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3281 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3282 spa_config_held(spa, SCL_STATE, RW_READER)));
3284 if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
3285 list_insert_head(&spa->spa_state_dirty_list, vd);
3289 vdev_state_clean(vdev_t *vd)
3291 spa_t *spa = vd->vdev_spa;
3293 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3294 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3295 spa_config_held(spa, SCL_STATE, RW_READER)));
3297 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3298 list_remove(&spa->spa_state_dirty_list, vd);
3302 * Propagate vdev state up from children to parent.
3305 vdev_propagate_state(vdev_t *vd)
3307 spa_t *spa = vd->vdev_spa;
3308 vdev_t *rvd = spa->spa_root_vdev;
3309 int degraded = 0, faulted = 0;
3313 if (vd->vdev_children > 0) {
3314 for (int c = 0; c < vd->vdev_children; c++) {
3315 child = vd->vdev_child[c];
3318 * Don't factor holes into the decision.
3320 if (child->vdev_ishole)
3323 if (!vdev_readable(child) ||
3324 (!vdev_writeable(child) && spa_writeable(spa))) {
3326 * Root special: if there is a top-level log
3327 * device, treat the root vdev as if it were
3330 if (child->vdev_islog && vd == rvd)
3334 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
3338 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
3342 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
3345 * Root special: if there is a top-level vdev that cannot be
3346 * opened due to corrupted metadata, then propagate the root
3347 * vdev's aux state as 'corrupt' rather than 'insufficient
3350 if (corrupted && vd == rvd &&
3351 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
3352 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
3353 VDEV_AUX_CORRUPT_DATA);
3356 if (vd->vdev_parent)
3357 vdev_propagate_state(vd->vdev_parent);
3361 * Set a vdev's state. If this is during an open, we don't update the parent
3362 * state, because we're in the process of opening children depth-first.
3363 * Otherwise, we propagate the change to the parent.
3365 * If this routine places a device in a faulted state, an appropriate ereport is
3369 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3371 uint64_t save_state;
3372 spa_t *spa = vd->vdev_spa;
3374 if (state == vd->vdev_state) {
3375 vd->vdev_stat.vs_aux = aux;
3379 save_state = vd->vdev_state;
3381 vd->vdev_state = state;
3382 vd->vdev_stat.vs_aux = aux;
3385 * If we are setting the vdev state to anything but an open state, then
3386 * always close the underlying device unless the device has requested
3387 * a delayed close (i.e. we're about to remove or fault the device).
3388 * Otherwise, we keep accessible but invalid devices open forever.
3389 * We don't call vdev_close() itself, because that implies some extra
3390 * checks (offline, etc) that we don't want here. This is limited to
3391 * leaf devices, because otherwise closing the device will affect other
3394 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
3395 vd->vdev_ops->vdev_op_leaf)
3396 vd->vdev_ops->vdev_op_close(vd);
3398 if (vd->vdev_removed &&
3399 state == VDEV_STATE_CANT_OPEN &&
3400 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
3402 * If the previous state is set to VDEV_STATE_REMOVED, then this
3403 * device was previously marked removed and someone attempted to
3404 * reopen it. If this failed due to a nonexistent device, then
3405 * keep the device in the REMOVED state. We also let this be if
3406 * it is one of our special test online cases, which is only
3407 * attempting to online the device and shouldn't generate an FMA
3410 vd->vdev_state = VDEV_STATE_REMOVED;
3411 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
3412 } else if (state == VDEV_STATE_REMOVED) {
3413 vd->vdev_removed = B_TRUE;
3414 } else if (state == VDEV_STATE_CANT_OPEN) {
3416 * If we fail to open a vdev during an import or recovery, we
3417 * mark it as "not available", which signifies that it was
3418 * never there to begin with. Failure to open such a device
3419 * is not considered an error.
3421 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3422 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3423 vd->vdev_ops->vdev_op_leaf)
3424 vd->vdev_not_present = 1;
3427 * Post the appropriate ereport. If the 'prevstate' field is
3428 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3429 * that this is part of a vdev_reopen(). In this case, we don't
3430 * want to post the ereport if the device was already in the
3431 * CANT_OPEN state beforehand.
3433 * If the 'checkremove' flag is set, then this is an attempt to
3434 * online the device in response to an insertion event. If we
3435 * hit this case, then we have detected an insertion event for a
3436 * faulted or offline device that wasn't in the removed state.
3437 * In this scenario, we don't post an ereport because we are
3438 * about to replace the device, or attempt an online with
3439 * vdev_forcefault, which will generate the fault for us.
3441 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
3442 !vd->vdev_not_present && !vd->vdev_checkremove &&
3443 vd != spa->spa_root_vdev) {
3447 case VDEV_AUX_OPEN_FAILED:
3448 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
3450 case VDEV_AUX_CORRUPT_DATA:
3451 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
3453 case VDEV_AUX_NO_REPLICAS:
3454 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
3456 case VDEV_AUX_BAD_GUID_SUM:
3457 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
3459 case VDEV_AUX_TOO_SMALL:
3460 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3462 case VDEV_AUX_BAD_LABEL:
3463 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3466 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3469 zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3472 /* Erase any notion of persistent removed state */
3473 vd->vdev_removed = B_FALSE;
3475 vd->vdev_removed = B_FALSE;
3479 * Notify the fmd of the state change. Be verbose and post
3480 * notifications even for stuff that's not important; the fmd agent can
3481 * sort it out. Don't emit state change events for non-leaf vdevs since
3482 * they can't change state on their own. The FMD can check their state
3483 * if it wants to when it sees that a leaf vdev had a state change.
3485 if (vd->vdev_ops->vdev_op_leaf)
3486 zfs_post_state_change(spa, vd);
3488 if (!isopen && vd->vdev_parent)
3489 vdev_propagate_state(vd->vdev_parent);
3493 * Check the vdev configuration to ensure that it's capable of supporting
3494 * a root pool. We do not support partial configuration.
3495 * In addition, only a single top-level vdev is allowed.
3497 * FreeBSD does not have above limitations.
3500 vdev_is_bootable(vdev_t *vd)
3503 if (!vd->vdev_ops->vdev_op_leaf) {
3504 char *vdev_type = vd->vdev_ops->vdev_op_type;
3506 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
3507 vd->vdev_children > 1) {
3509 } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
3514 for (int c = 0; c < vd->vdev_children; c++) {
3515 if (!vdev_is_bootable(vd->vdev_child[c]))
3518 #endif /* illumos */
3523 * Load the state from the original vdev tree (ovd) which
3524 * we've retrieved from the MOS config object. If the original
3525 * vdev was offline or faulted then we transfer that state to the
3526 * device in the current vdev tree (nvd).
3529 vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
3531 spa_t *spa = nvd->vdev_spa;
3533 ASSERT(nvd->vdev_top->vdev_islog);
3534 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3535 ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
3537 for (int c = 0; c < nvd->vdev_children; c++)
3538 vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
3540 if (nvd->vdev_ops->vdev_op_leaf) {
3542 * Restore the persistent vdev state
3544 nvd->vdev_offline = ovd->vdev_offline;
3545 nvd->vdev_faulted = ovd->vdev_faulted;
3546 nvd->vdev_degraded = ovd->vdev_degraded;
3547 nvd->vdev_removed = ovd->vdev_removed;
3552 * Determine if a log device has valid content. If the vdev was
3553 * removed or faulted in the MOS config then we know that
3554 * the content on the log device has already been written to the pool.
3557 vdev_log_state_valid(vdev_t *vd)
3559 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
3563 for (int c = 0; c < vd->vdev_children; c++)
3564 if (vdev_log_state_valid(vd->vdev_child[c]))
3571 * Expand a vdev if possible.
3574 vdev_expand(vdev_t *vd, uint64_t txg)
3576 ASSERT(vd->vdev_top == vd);
3577 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3579 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
3580 VERIFY(vdev_metaslab_init(vd, txg) == 0);
3581 vdev_config_dirty(vd);
3589 vdev_split(vdev_t *vd)
3591 vdev_t *cvd, *pvd = vd->vdev_parent;
3593 vdev_remove_child(pvd, vd);
3594 vdev_compact_children(pvd);
3596 cvd = pvd->vdev_child[0];
3597 if (pvd->vdev_children == 1) {
3598 vdev_remove_parent(cvd);
3599 cvd->vdev_splitting = B_TRUE;
3601 vdev_propagate_state(cvd);
3605 vdev_deadman(vdev_t *vd)
3607 for (int c = 0; c < vd->vdev_children; c++) {
3608 vdev_t *cvd = vd->vdev_child[c];
3613 if (vd->vdev_ops->vdev_op_leaf) {
3614 vdev_queue_t *vq = &vd->vdev_queue;
3616 mutex_enter(&vq->vq_lock);
3617 if (avl_numnodes(&vq->vq_active_tree) > 0) {
3618 spa_t *spa = vd->vdev_spa;
3623 * Look at the head of all the pending queues,
3624 * if any I/O has been outstanding for longer than
3625 * the spa_deadman_synctime we panic the system.
3627 fio = avl_first(&vq->vq_active_tree);
3628 delta = gethrtime() - fio->io_timestamp;
3629 if (delta > spa_deadman_synctime(spa)) {
3630 zfs_dbgmsg("SLOW IO: zio timestamp %lluns, "
3631 "delta %lluns, last io %lluns",
3632 fio->io_timestamp, delta,
3633 vq->vq_io_complete_ts);
3634 fm_panic("I/O to pool '%s' appears to be "
3635 "hung on vdev guid %llu at '%s'.",
3637 (long long unsigned int) vd->vdev_guid,
3641 mutex_exit(&vq->vq_lock);