4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
25 * Copyright 2017 Nexenta Systems, Inc.
26 * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
27 * Copyright (c) 2014 Integros [integros.com]
28 * Copyright 2016 Toomas Soome <tsoome@me.com>
29 * Copyright 2017 Joyent, Inc.
32 #include <sys/zfs_context.h>
33 #include <sys/fm/fs/zfs.h>
35 #include <sys/spa_impl.h>
36 #include <sys/bpobj.h>
38 #include <sys/dmu_tx.h>
39 #include <sys/dsl_dir.h>
40 #include <sys/vdev_impl.h>
41 #include <sys/uberblock_impl.h>
42 #include <sys/metaslab.h>
43 #include <sys/metaslab_impl.h>
44 #include <sys/space_map.h>
45 #include <sys/space_reftree.h>
48 #include <sys/fs/zfs.h>
51 #include <sys/dsl_scan.h>
53 #include <sys/trim_map.h>
55 SYSCTL_DECL(_vfs_zfs);
56 SYSCTL_NODE(_vfs_zfs, OID_AUTO, vdev, CTLFLAG_RW, 0, "ZFS VDEV");
59 * Virtual device management.
63 * The limit for ZFS to automatically increase a top-level vdev's ashift
64 * from logical ashift to physical ashift.
66 * Example: one or more 512B emulation child vdevs
67 * child->vdev_ashift = 9 (512 bytes)
68 * child->vdev_physical_ashift = 12 (4096 bytes)
69 * zfs_max_auto_ashift = 11 (2048 bytes)
70 * zfs_min_auto_ashift = 9 (512 bytes)
72 * On pool creation or the addition of a new top-level vdev, ZFS will
73 * increase the ashift of the top-level vdev to 2048 as limited by
74 * zfs_max_auto_ashift.
76 * Example: one or more 512B emulation child vdevs
77 * child->vdev_ashift = 9 (512 bytes)
78 * child->vdev_physical_ashift = 12 (4096 bytes)
79 * zfs_max_auto_ashift = 13 (8192 bytes)
80 * zfs_min_auto_ashift = 9 (512 bytes)
82 * On pool creation or the addition of a new top-level vdev, ZFS will
83 * increase the ashift of the top-level vdev to 4096 to match the
84 * max vdev_physical_ashift.
86 * Example: one or more 512B emulation child vdevs
87 * child->vdev_ashift = 9 (512 bytes)
88 * child->vdev_physical_ashift = 9 (512 bytes)
89 * zfs_max_auto_ashift = 13 (8192 bytes)
90 * zfs_min_auto_ashift = 12 (4096 bytes)
92 * On pool creation or the addition of a new top-level vdev, ZFS will
93 * increase the ashift of the top-level vdev to 4096 to match the
94 * zfs_min_auto_ashift.
96 static uint64_t zfs_max_auto_ashift = SPA_MAXASHIFT;
97 static uint64_t zfs_min_auto_ashift = SPA_MINASHIFT;
100 sysctl_vfs_zfs_max_auto_ashift(SYSCTL_HANDLER_ARGS)
105 val = zfs_max_auto_ashift;
106 err = sysctl_handle_64(oidp, &val, 0, req);
107 if (err != 0 || req->newptr == NULL)
110 if (val > SPA_MAXASHIFT || val < zfs_min_auto_ashift)
113 zfs_max_auto_ashift = val;
117 SYSCTL_PROC(_vfs_zfs, OID_AUTO, max_auto_ashift,
118 CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t),
119 sysctl_vfs_zfs_max_auto_ashift, "QU",
120 "Max ashift used when optimising for logical -> physical sectors size on "
121 "new top-level vdevs.");
124 sysctl_vfs_zfs_min_auto_ashift(SYSCTL_HANDLER_ARGS)
129 val = zfs_min_auto_ashift;
130 err = sysctl_handle_64(oidp, &val, 0, req);
131 if (err != 0 || req->newptr == NULL)
134 if (val < SPA_MINASHIFT || val > zfs_max_auto_ashift)
137 zfs_min_auto_ashift = val;
141 SYSCTL_PROC(_vfs_zfs, OID_AUTO, min_auto_ashift,
142 CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t),
143 sysctl_vfs_zfs_min_auto_ashift, "QU",
144 "Min ashift used when creating new top-level vdevs.");
146 static vdev_ops_t *vdev_ops_table[] = {
166 * When a vdev is added, it will be divided into approximately (but no
167 * more than) this number of metaslabs.
169 int metaslabs_per_vdev = 200;
170 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, metaslabs_per_vdev, CTLFLAG_RDTUN,
171 &metaslabs_per_vdev, 0,
172 "When a vdev is added, how many metaslabs the vdev should be divided into");
174 boolean_t vdev_validate_skip = B_FALSE;
178 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
184 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
187 if (vd->vdev_path != NULL) {
188 zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
191 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
192 vd->vdev_ops->vdev_op_type,
193 (u_longlong_t)vd->vdev_id,
194 (u_longlong_t)vd->vdev_guid, buf);
199 vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
203 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
204 zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id,
205 vd->vdev_ops->vdev_op_type);
209 switch (vd->vdev_state) {
210 case VDEV_STATE_UNKNOWN:
211 (void) snprintf(state, sizeof (state), "unknown");
213 case VDEV_STATE_CLOSED:
214 (void) snprintf(state, sizeof (state), "closed");
216 case VDEV_STATE_OFFLINE:
217 (void) snprintf(state, sizeof (state), "offline");
219 case VDEV_STATE_REMOVED:
220 (void) snprintf(state, sizeof (state), "removed");
222 case VDEV_STATE_CANT_OPEN:
223 (void) snprintf(state, sizeof (state), "can't open");
225 case VDEV_STATE_FAULTED:
226 (void) snprintf(state, sizeof (state), "faulted");
228 case VDEV_STATE_DEGRADED:
229 (void) snprintf(state, sizeof (state), "degraded");
231 case VDEV_STATE_HEALTHY:
232 (void) snprintf(state, sizeof (state), "healthy");
235 (void) snprintf(state, sizeof (state), "<state %u>",
236 (uint_t)vd->vdev_state);
239 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
240 "", vd->vdev_id, vd->vdev_ops->vdev_op_type,
241 vd->vdev_islog ? " (log)" : "",
242 (u_longlong_t)vd->vdev_guid,
243 vd->vdev_path ? vd->vdev_path : "N/A", state);
245 for (uint64_t i = 0; i < vd->vdev_children; i++)
246 vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
250 * Given a vdev type, return the appropriate ops vector.
253 vdev_getops(const char *type)
255 vdev_ops_t *ops, **opspp;
257 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
258 if (strcmp(ops->vdev_op_type, type) == 0)
265 * Default asize function: return the MAX of psize with the asize of
266 * all children. This is what's used by anything other than RAID-Z.
269 vdev_default_asize(vdev_t *vd, uint64_t psize)
271 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
274 for (int c = 0; c < vd->vdev_children; c++) {
275 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
276 asize = MAX(asize, csize);
283 * Get the minimum allocatable size. We define the allocatable size as
284 * the vdev's asize rounded to the nearest metaslab. This allows us to
285 * replace or attach devices which don't have the same physical size but
286 * can still satisfy the same number of allocations.
289 vdev_get_min_asize(vdev_t *vd)
291 vdev_t *pvd = vd->vdev_parent;
294 * If our parent is NULL (inactive spare or cache) or is the root,
295 * just return our own asize.
298 return (vd->vdev_asize);
301 * The top-level vdev just returns the allocatable size rounded
302 * to the nearest metaslab.
304 if (vd == vd->vdev_top)
305 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
308 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
309 * so each child must provide at least 1/Nth of its asize.
311 if (pvd->vdev_ops == &vdev_raidz_ops)
312 return ((pvd->vdev_min_asize + pvd->vdev_children - 1) /
315 return (pvd->vdev_min_asize);
319 vdev_set_min_asize(vdev_t *vd)
321 vd->vdev_min_asize = vdev_get_min_asize(vd);
323 for (int c = 0; c < vd->vdev_children; c++)
324 vdev_set_min_asize(vd->vdev_child[c]);
328 vdev_lookup_top(spa_t *spa, uint64_t vdev)
330 vdev_t *rvd = spa->spa_root_vdev;
332 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
334 if (vdev < rvd->vdev_children) {
335 ASSERT(rvd->vdev_child[vdev] != NULL);
336 return (rvd->vdev_child[vdev]);
343 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
347 if (vd->vdev_guid == guid)
350 for (int c = 0; c < vd->vdev_children; c++)
351 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
359 vdev_count_leaves_impl(vdev_t *vd)
363 if (vd->vdev_ops->vdev_op_leaf)
366 for (int c = 0; c < vd->vdev_children; c++)
367 n += vdev_count_leaves_impl(vd->vdev_child[c]);
373 vdev_count_leaves(spa_t *spa)
375 return (vdev_count_leaves_impl(spa->spa_root_vdev));
379 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
381 size_t oldsize, newsize;
382 uint64_t id = cvd->vdev_id;
384 spa_t *spa = cvd->vdev_spa;
386 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
387 ASSERT(cvd->vdev_parent == NULL);
389 cvd->vdev_parent = pvd;
394 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
396 oldsize = pvd->vdev_children * sizeof (vdev_t *);
397 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
398 newsize = pvd->vdev_children * sizeof (vdev_t *);
400 newchild = kmem_zalloc(newsize, KM_SLEEP);
401 if (pvd->vdev_child != NULL) {
402 bcopy(pvd->vdev_child, newchild, oldsize);
403 kmem_free(pvd->vdev_child, oldsize);
406 pvd->vdev_child = newchild;
407 pvd->vdev_child[id] = cvd;
409 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
410 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
413 * Walk up all ancestors to update guid sum.
415 for (; pvd != NULL; pvd = pvd->vdev_parent)
416 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
420 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
423 uint_t id = cvd->vdev_id;
425 ASSERT(cvd->vdev_parent == pvd);
430 ASSERT(id < pvd->vdev_children);
431 ASSERT(pvd->vdev_child[id] == cvd);
433 pvd->vdev_child[id] = NULL;
434 cvd->vdev_parent = NULL;
436 for (c = 0; c < pvd->vdev_children; c++)
437 if (pvd->vdev_child[c])
440 if (c == pvd->vdev_children) {
441 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
442 pvd->vdev_child = NULL;
443 pvd->vdev_children = 0;
447 * Walk up all ancestors to update guid sum.
449 for (; pvd != NULL; pvd = pvd->vdev_parent)
450 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
454 * Remove any holes in the child array.
457 vdev_compact_children(vdev_t *pvd)
459 vdev_t **newchild, *cvd;
460 int oldc = pvd->vdev_children;
463 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
465 for (int c = newc = 0; c < oldc; c++)
466 if (pvd->vdev_child[c])
469 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
471 for (int c = newc = 0; c < oldc; c++) {
472 if ((cvd = pvd->vdev_child[c]) != NULL) {
473 newchild[newc] = cvd;
474 cvd->vdev_id = newc++;
478 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
479 pvd->vdev_child = newchild;
480 pvd->vdev_children = newc;
484 * Allocate and minimally initialize a vdev_t.
487 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
490 vdev_indirect_config_t *vic;
492 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
493 vic = &vd->vdev_indirect_config;
495 if (spa->spa_root_vdev == NULL) {
496 ASSERT(ops == &vdev_root_ops);
497 spa->spa_root_vdev = vd;
498 spa->spa_load_guid = spa_generate_guid(NULL);
501 if (guid == 0 && ops != &vdev_hole_ops) {
502 if (spa->spa_root_vdev == vd) {
504 * The root vdev's guid will also be the pool guid,
505 * which must be unique among all pools.
507 guid = spa_generate_guid(NULL);
510 * Any other vdev's guid must be unique within the pool.
512 guid = spa_generate_guid(spa);
514 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
519 vd->vdev_guid = guid;
520 vd->vdev_guid_sum = guid;
522 vd->vdev_state = VDEV_STATE_CLOSED;
523 vd->vdev_ishole = (ops == &vdev_hole_ops);
524 vic->vic_prev_indirect_vdev = UINT64_MAX;
526 rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
527 mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
528 vd->vdev_obsolete_segments = range_tree_create(NULL, NULL);
530 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
531 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
532 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
533 mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL);
534 for (int t = 0; t < DTL_TYPES; t++) {
535 vd->vdev_dtl[t] = range_tree_create(NULL, NULL);
537 txg_list_create(&vd->vdev_ms_list, spa,
538 offsetof(struct metaslab, ms_txg_node));
539 txg_list_create(&vd->vdev_dtl_list, spa,
540 offsetof(struct vdev, vdev_dtl_node));
541 vd->vdev_stat.vs_timestamp = gethrtime();
549 * Allocate a new vdev. The 'alloctype' is used to control whether we are
550 * creating a new vdev or loading an existing one - the behavior is slightly
551 * different for each case.
554 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
559 uint64_t guid = 0, islog, nparity;
561 vdev_indirect_config_t *vic;
563 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
565 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
566 return (SET_ERROR(EINVAL));
568 if ((ops = vdev_getops(type)) == NULL)
569 return (SET_ERROR(EINVAL));
572 * If this is a load, get the vdev guid from the nvlist.
573 * Otherwise, vdev_alloc_common() will generate one for us.
575 if (alloctype == VDEV_ALLOC_LOAD) {
578 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
580 return (SET_ERROR(EINVAL));
582 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
583 return (SET_ERROR(EINVAL));
584 } else if (alloctype == VDEV_ALLOC_SPARE) {
585 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
586 return (SET_ERROR(EINVAL));
587 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
588 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
589 return (SET_ERROR(EINVAL));
590 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
591 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
592 return (SET_ERROR(EINVAL));
596 * The first allocated vdev must be of type 'root'.
598 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
599 return (SET_ERROR(EINVAL));
602 * Determine whether we're a log vdev.
605 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
606 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
607 return (SET_ERROR(ENOTSUP));
609 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
610 return (SET_ERROR(ENOTSUP));
613 * Set the nparity property for RAID-Z vdevs.
616 if (ops == &vdev_raidz_ops) {
617 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
619 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
620 return (SET_ERROR(EINVAL));
622 * Previous versions could only support 1 or 2 parity
626 spa_version(spa) < SPA_VERSION_RAIDZ2)
627 return (SET_ERROR(ENOTSUP));
629 spa_version(spa) < SPA_VERSION_RAIDZ3)
630 return (SET_ERROR(ENOTSUP));
633 * We require the parity to be specified for SPAs that
634 * support multiple parity levels.
636 if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
637 return (SET_ERROR(EINVAL));
639 * Otherwise, we default to 1 parity device for RAID-Z.
646 ASSERT(nparity != -1ULL);
648 vd = vdev_alloc_common(spa, id, guid, ops);
649 vic = &vd->vdev_indirect_config;
651 vd->vdev_islog = islog;
652 vd->vdev_nparity = nparity;
654 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
655 vd->vdev_path = spa_strdup(vd->vdev_path);
656 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
657 vd->vdev_devid = spa_strdup(vd->vdev_devid);
658 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
659 &vd->vdev_physpath) == 0)
660 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
661 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
662 vd->vdev_fru = spa_strdup(vd->vdev_fru);
665 * Set the whole_disk property. If it's not specified, leave the value
668 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
669 &vd->vdev_wholedisk) != 0)
670 vd->vdev_wholedisk = -1ULL;
672 ASSERT0(vic->vic_mapping_object);
673 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
674 &vic->vic_mapping_object);
675 ASSERT0(vic->vic_births_object);
676 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
677 &vic->vic_births_object);
678 ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
679 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
680 &vic->vic_prev_indirect_vdev);
683 * Look for the 'not present' flag. This will only be set if the device
684 * was not present at the time of import.
686 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
687 &vd->vdev_not_present);
690 * Get the alignment requirement.
692 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
695 * Retrieve the vdev creation time.
697 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
701 * If we're a top-level vdev, try to load the allocation parameters.
703 if (parent && !parent->vdev_parent &&
704 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
705 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
707 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
709 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
711 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
713 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
716 ASSERT0(vd->vdev_top_zap);
719 if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
720 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
721 alloctype == VDEV_ALLOC_ADD ||
722 alloctype == VDEV_ALLOC_SPLIT ||
723 alloctype == VDEV_ALLOC_ROOTPOOL);
724 vd->vdev_mg = metaslab_group_create(islog ?
725 spa_log_class(spa) : spa_normal_class(spa), vd);
728 if (vd->vdev_ops->vdev_op_leaf &&
729 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
730 (void) nvlist_lookup_uint64(nv,
731 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
733 ASSERT0(vd->vdev_leaf_zap);
737 * If we're a leaf vdev, try to load the DTL object and other state.
740 if (vd->vdev_ops->vdev_op_leaf &&
741 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
742 alloctype == VDEV_ALLOC_ROOTPOOL)) {
743 if (alloctype == VDEV_ALLOC_LOAD) {
744 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
745 &vd->vdev_dtl_object);
746 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
750 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
753 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
754 &spare) == 0 && spare)
758 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
761 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
762 &vd->vdev_resilver_txg);
765 * When importing a pool, we want to ignore the persistent fault
766 * state, as the diagnosis made on another system may not be
767 * valid in the current context. Local vdevs will
768 * remain in the faulted state.
770 if (spa_load_state(spa) == SPA_LOAD_OPEN) {
771 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
773 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
775 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
778 if (vd->vdev_faulted || vd->vdev_degraded) {
782 VDEV_AUX_ERR_EXCEEDED;
783 if (nvlist_lookup_string(nv,
784 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
785 strcmp(aux, "external") == 0)
786 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
792 * Add ourselves to the parent's list of children.
794 vdev_add_child(parent, vd);
802 vdev_free(vdev_t *vd)
804 spa_t *spa = vd->vdev_spa;
807 * vdev_free() implies closing the vdev first. This is simpler than
808 * trying to ensure complicated semantics for all callers.
812 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
813 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
818 for (int c = 0; c < vd->vdev_children; c++)
819 vdev_free(vd->vdev_child[c]);
821 ASSERT(vd->vdev_child == NULL);
822 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
825 * Discard allocation state.
827 if (vd->vdev_mg != NULL) {
828 vdev_metaslab_fini(vd);
829 metaslab_group_destroy(vd->vdev_mg);
832 ASSERT0(vd->vdev_stat.vs_space);
833 ASSERT0(vd->vdev_stat.vs_dspace);
834 ASSERT0(vd->vdev_stat.vs_alloc);
837 * Remove this vdev from its parent's child list.
839 vdev_remove_child(vd->vdev_parent, vd);
841 ASSERT(vd->vdev_parent == NULL);
844 * Clean up vdev structure.
850 spa_strfree(vd->vdev_path);
852 spa_strfree(vd->vdev_devid);
853 if (vd->vdev_physpath)
854 spa_strfree(vd->vdev_physpath);
856 spa_strfree(vd->vdev_fru);
858 if (vd->vdev_isspare)
859 spa_spare_remove(vd);
860 if (vd->vdev_isl2cache)
861 spa_l2cache_remove(vd);
863 txg_list_destroy(&vd->vdev_ms_list);
864 txg_list_destroy(&vd->vdev_dtl_list);
866 mutex_enter(&vd->vdev_dtl_lock);
867 space_map_close(vd->vdev_dtl_sm);
868 for (int t = 0; t < DTL_TYPES; t++) {
869 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
870 range_tree_destroy(vd->vdev_dtl[t]);
872 mutex_exit(&vd->vdev_dtl_lock);
874 EQUIV(vd->vdev_indirect_births != NULL,
875 vd->vdev_indirect_mapping != NULL);
876 if (vd->vdev_indirect_births != NULL) {
877 vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
878 vdev_indirect_births_close(vd->vdev_indirect_births);
881 if (vd->vdev_obsolete_sm != NULL) {
882 ASSERT(vd->vdev_removing ||
883 vd->vdev_ops == &vdev_indirect_ops);
884 space_map_close(vd->vdev_obsolete_sm);
885 vd->vdev_obsolete_sm = NULL;
887 range_tree_destroy(vd->vdev_obsolete_segments);
888 rw_destroy(&vd->vdev_indirect_rwlock);
889 mutex_destroy(&vd->vdev_obsolete_lock);
891 mutex_destroy(&vd->vdev_queue_lock);
892 mutex_destroy(&vd->vdev_dtl_lock);
893 mutex_destroy(&vd->vdev_stat_lock);
894 mutex_destroy(&vd->vdev_probe_lock);
896 if (vd == spa->spa_root_vdev)
897 spa->spa_root_vdev = NULL;
899 kmem_free(vd, sizeof (vdev_t));
903 * Transfer top-level vdev state from svd to tvd.
906 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
908 spa_t *spa = svd->vdev_spa;
913 ASSERT(tvd == tvd->vdev_top);
915 tvd->vdev_ms_array = svd->vdev_ms_array;
916 tvd->vdev_ms_shift = svd->vdev_ms_shift;
917 tvd->vdev_ms_count = svd->vdev_ms_count;
918 tvd->vdev_top_zap = svd->vdev_top_zap;
920 svd->vdev_ms_array = 0;
921 svd->vdev_ms_shift = 0;
922 svd->vdev_ms_count = 0;
923 svd->vdev_top_zap = 0;
926 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
927 tvd->vdev_mg = svd->vdev_mg;
928 tvd->vdev_ms = svd->vdev_ms;
933 if (tvd->vdev_mg != NULL)
934 tvd->vdev_mg->mg_vd = tvd;
936 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
937 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
938 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
940 svd->vdev_stat.vs_alloc = 0;
941 svd->vdev_stat.vs_space = 0;
942 svd->vdev_stat.vs_dspace = 0;
944 for (t = 0; t < TXG_SIZE; t++) {
945 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
946 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
947 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
948 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
949 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
950 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
953 if (list_link_active(&svd->vdev_config_dirty_node)) {
954 vdev_config_clean(svd);
955 vdev_config_dirty(tvd);
958 if (list_link_active(&svd->vdev_state_dirty_node)) {
959 vdev_state_clean(svd);
960 vdev_state_dirty(tvd);
963 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
964 svd->vdev_deflate_ratio = 0;
966 tvd->vdev_islog = svd->vdev_islog;
971 vdev_top_update(vdev_t *tvd, vdev_t *vd)
978 for (int c = 0; c < vd->vdev_children; c++)
979 vdev_top_update(tvd, vd->vdev_child[c]);
983 * Add a mirror/replacing vdev above an existing vdev.
986 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
988 spa_t *spa = cvd->vdev_spa;
989 vdev_t *pvd = cvd->vdev_parent;
992 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
994 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
996 mvd->vdev_asize = cvd->vdev_asize;
997 mvd->vdev_min_asize = cvd->vdev_min_asize;
998 mvd->vdev_max_asize = cvd->vdev_max_asize;
999 mvd->vdev_psize = cvd->vdev_psize;
1000 mvd->vdev_ashift = cvd->vdev_ashift;
1001 mvd->vdev_logical_ashift = cvd->vdev_logical_ashift;
1002 mvd->vdev_physical_ashift = cvd->vdev_physical_ashift;
1003 mvd->vdev_state = cvd->vdev_state;
1004 mvd->vdev_crtxg = cvd->vdev_crtxg;
1006 vdev_remove_child(pvd, cvd);
1007 vdev_add_child(pvd, mvd);
1008 cvd->vdev_id = mvd->vdev_children;
1009 vdev_add_child(mvd, cvd);
1010 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1012 if (mvd == mvd->vdev_top)
1013 vdev_top_transfer(cvd, mvd);
1019 * Remove a 1-way mirror/replacing vdev from the tree.
1022 vdev_remove_parent(vdev_t *cvd)
1024 vdev_t *mvd = cvd->vdev_parent;
1025 vdev_t *pvd = mvd->vdev_parent;
1027 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1029 ASSERT(mvd->vdev_children == 1);
1030 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
1031 mvd->vdev_ops == &vdev_replacing_ops ||
1032 mvd->vdev_ops == &vdev_spare_ops);
1033 cvd->vdev_ashift = mvd->vdev_ashift;
1034 cvd->vdev_logical_ashift = mvd->vdev_logical_ashift;
1035 cvd->vdev_physical_ashift = mvd->vdev_physical_ashift;
1037 vdev_remove_child(mvd, cvd);
1038 vdev_remove_child(pvd, mvd);
1041 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1042 * Otherwise, we could have detached an offline device, and when we
1043 * go to import the pool we'll think we have two top-level vdevs,
1044 * instead of a different version of the same top-level vdev.
1046 if (mvd->vdev_top == mvd) {
1047 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
1048 cvd->vdev_orig_guid = cvd->vdev_guid;
1049 cvd->vdev_guid += guid_delta;
1050 cvd->vdev_guid_sum += guid_delta;
1052 cvd->vdev_id = mvd->vdev_id;
1053 vdev_add_child(pvd, cvd);
1054 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1056 if (cvd == cvd->vdev_top)
1057 vdev_top_transfer(mvd, cvd);
1059 ASSERT(mvd->vdev_children == 0);
1064 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
1066 spa_t *spa = vd->vdev_spa;
1067 objset_t *mos = spa->spa_meta_objset;
1069 uint64_t oldc = vd->vdev_ms_count;
1070 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
1074 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1077 * This vdev is not being allocated from yet or is a hole.
1079 if (vd->vdev_ms_shift == 0)
1082 ASSERT(!vd->vdev_ishole);
1084 ASSERT(oldc <= newc);
1086 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1089 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
1090 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1094 vd->vdev_ms_count = newc;
1096 for (m = oldc; m < newc; m++) {
1097 uint64_t object = 0;
1100 * vdev_ms_array may be 0 if we are creating the "fake"
1101 * metaslabs for an indirect vdev for zdb's leak detection.
1102 * See zdb_leak_init().
1104 if (txg == 0 && vd->vdev_ms_array != 0) {
1105 error = dmu_read(mos, vd->vdev_ms_array,
1106 m * sizeof (uint64_t), sizeof (uint64_t), &object,
1109 vdev_dbgmsg(vd, "unable to read the metaslab "
1110 "array [error=%d]", error);
1115 error = metaslab_init(vd->vdev_mg, m, object, txg,
1118 vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
1125 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1128 * If the vdev is being removed we don't activate
1129 * the metaslabs since we want to ensure that no new
1130 * allocations are performed on this device.
1132 if (oldc == 0 && !vd->vdev_removing)
1133 metaslab_group_activate(vd->vdev_mg);
1136 spa_config_exit(spa, SCL_ALLOC, FTAG);
1142 vdev_metaslab_fini(vdev_t *vd)
1144 if (vd->vdev_ms != NULL) {
1145 uint64_t count = vd->vdev_ms_count;
1147 metaslab_group_passivate(vd->vdev_mg);
1148 for (uint64_t m = 0; m < count; m++) {
1149 metaslab_t *msp = vd->vdev_ms[m];
1154 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1157 vd->vdev_ms_count = 0;
1159 ASSERT0(vd->vdev_ms_count);
1162 typedef struct vdev_probe_stats {
1163 boolean_t vps_readable;
1164 boolean_t vps_writeable;
1166 } vdev_probe_stats_t;
1169 vdev_probe_done(zio_t *zio)
1171 spa_t *spa = zio->io_spa;
1172 vdev_t *vd = zio->io_vd;
1173 vdev_probe_stats_t *vps = zio->io_private;
1175 ASSERT(vd->vdev_probe_zio != NULL);
1177 if (zio->io_type == ZIO_TYPE_READ) {
1178 if (zio->io_error == 0)
1179 vps->vps_readable = 1;
1180 if (zio->io_error == 0 && spa_writeable(spa)) {
1181 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1182 zio->io_offset, zio->io_size, zio->io_abd,
1183 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1184 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1186 abd_free(zio->io_abd);
1188 } else if (zio->io_type == ZIO_TYPE_WRITE) {
1189 if (zio->io_error == 0)
1190 vps->vps_writeable = 1;
1191 abd_free(zio->io_abd);
1192 } else if (zio->io_type == ZIO_TYPE_NULL) {
1195 vd->vdev_cant_read |= !vps->vps_readable;
1196 vd->vdev_cant_write |= !vps->vps_writeable;
1198 if (vdev_readable(vd) &&
1199 (vdev_writeable(vd) || !spa_writeable(spa))) {
1202 ASSERT(zio->io_error != 0);
1203 vdev_dbgmsg(vd, "failed probe");
1204 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
1205 spa, vd, NULL, 0, 0);
1206 zio->io_error = SET_ERROR(ENXIO);
1209 mutex_enter(&vd->vdev_probe_lock);
1210 ASSERT(vd->vdev_probe_zio == zio);
1211 vd->vdev_probe_zio = NULL;
1212 mutex_exit(&vd->vdev_probe_lock);
1214 zio_link_t *zl = NULL;
1215 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1216 if (!vdev_accessible(vd, pio))
1217 pio->io_error = SET_ERROR(ENXIO);
1219 kmem_free(vps, sizeof (*vps));
1224 * Determine whether this device is accessible.
1226 * Read and write to several known locations: the pad regions of each
1227 * vdev label but the first, which we leave alone in case it contains
1231 vdev_probe(vdev_t *vd, zio_t *zio)
1233 spa_t *spa = vd->vdev_spa;
1234 vdev_probe_stats_t *vps = NULL;
1237 ASSERT(vd->vdev_ops->vdev_op_leaf);
1240 * Don't probe the probe.
1242 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1246 * To prevent 'probe storms' when a device fails, we create
1247 * just one probe i/o at a time. All zios that want to probe
1248 * this vdev will become parents of the probe io.
1250 mutex_enter(&vd->vdev_probe_lock);
1252 if ((pio = vd->vdev_probe_zio) == NULL) {
1253 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1255 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1256 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
1259 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1261 * vdev_cant_read and vdev_cant_write can only
1262 * transition from TRUE to FALSE when we have the
1263 * SCL_ZIO lock as writer; otherwise they can only
1264 * transition from FALSE to TRUE. This ensures that
1265 * any zio looking at these values can assume that
1266 * failures persist for the life of the I/O. That's
1267 * important because when a device has intermittent
1268 * connectivity problems, we want to ensure that
1269 * they're ascribed to the device (ENXIO) and not
1272 * Since we hold SCL_ZIO as writer here, clear both
1273 * values so the probe can reevaluate from first
1276 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1277 vd->vdev_cant_read = B_FALSE;
1278 vd->vdev_cant_write = B_FALSE;
1281 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1282 vdev_probe_done, vps,
1283 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1286 * We can't change the vdev state in this context, so we
1287 * kick off an async task to do it on our behalf.
1290 vd->vdev_probe_wanted = B_TRUE;
1291 spa_async_request(spa, SPA_ASYNC_PROBE);
1296 zio_add_child(zio, pio);
1298 mutex_exit(&vd->vdev_probe_lock);
1301 ASSERT(zio != NULL);
1305 for (int l = 1; l < VDEV_LABELS; l++) {
1306 zio_nowait(zio_read_phys(pio, vd,
1307 vdev_label_offset(vd->vdev_psize, l,
1308 offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE,
1309 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1310 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1311 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1322 vdev_open_child(void *arg)
1326 vd->vdev_open_thread = curthread;
1327 vd->vdev_open_error = vdev_open(vd);
1328 vd->vdev_open_thread = NULL;
1332 vdev_uses_zvols(vdev_t *vd)
1334 if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1335 strlen(ZVOL_DIR)) == 0)
1337 for (int c = 0; c < vd->vdev_children; c++)
1338 if (vdev_uses_zvols(vd->vdev_child[c]))
1344 vdev_open_children(vdev_t *vd)
1347 int children = vd->vdev_children;
1350 * in order to handle pools on top of zvols, do the opens
1351 * in a single thread so that the same thread holds the
1352 * spa_namespace_lock
1354 if (B_TRUE || vdev_uses_zvols(vd)) {
1355 for (int c = 0; c < children; c++)
1356 vd->vdev_child[c]->vdev_open_error =
1357 vdev_open(vd->vdev_child[c]);
1360 tq = taskq_create("vdev_open", children, minclsyspri,
1361 children, children, TASKQ_PREPOPULATE);
1363 for (int c = 0; c < children; c++)
1364 VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1371 * Compute the raidz-deflation ratio. Note, we hard-code
1372 * in 128k (1 << 17) because it is the "typical" blocksize.
1373 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1374 * otherwise it would inconsistently account for existing bp's.
1377 vdev_set_deflate_ratio(vdev_t *vd)
1379 if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
1380 vd->vdev_deflate_ratio = (1 << 17) /
1381 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
1386 * Prepare a virtual device for access.
1389 vdev_open(vdev_t *vd)
1391 spa_t *spa = vd->vdev_spa;
1394 uint64_t max_osize = 0;
1395 uint64_t asize, max_asize, psize;
1396 uint64_t logical_ashift = 0;
1397 uint64_t physical_ashift = 0;
1399 ASSERT(vd->vdev_open_thread == curthread ||
1400 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1401 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1402 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1403 vd->vdev_state == VDEV_STATE_OFFLINE);
1405 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1406 vd->vdev_cant_read = B_FALSE;
1407 vd->vdev_cant_write = B_FALSE;
1408 vd->vdev_notrim = B_FALSE;
1409 vd->vdev_min_asize = vdev_get_min_asize(vd);
1412 * If this vdev is not removed, check its fault status. If it's
1413 * faulted, bail out of the open.
1415 if (!vd->vdev_removed && vd->vdev_faulted) {
1416 ASSERT(vd->vdev_children == 0);
1417 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1418 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1419 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1420 vd->vdev_label_aux);
1421 return (SET_ERROR(ENXIO));
1422 } else if (vd->vdev_offline) {
1423 ASSERT(vd->vdev_children == 0);
1424 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1425 return (SET_ERROR(ENXIO));
1428 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize,
1429 &logical_ashift, &physical_ashift);
1432 * Reset the vdev_reopening flag so that we actually close
1433 * the vdev on error.
1435 vd->vdev_reopening = B_FALSE;
1436 if (zio_injection_enabled && error == 0)
1437 error = zio_handle_device_injection(vd, NULL, ENXIO);
1440 if (vd->vdev_removed &&
1441 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
1442 vd->vdev_removed = B_FALSE;
1444 if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
1445 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
1446 vd->vdev_stat.vs_aux);
1448 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1449 vd->vdev_stat.vs_aux);
1454 vd->vdev_removed = B_FALSE;
1457 * Recheck the faulted flag now that we have confirmed that
1458 * the vdev is accessible. If we're faulted, bail.
1460 if (vd->vdev_faulted) {
1461 ASSERT(vd->vdev_children == 0);
1462 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1463 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1464 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1465 vd->vdev_label_aux);
1466 return (SET_ERROR(ENXIO));
1469 if (vd->vdev_degraded) {
1470 ASSERT(vd->vdev_children == 0);
1471 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1472 VDEV_AUX_ERR_EXCEEDED);
1474 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1478 * For hole or missing vdevs we just return success.
1480 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
1483 if (zfs_trim_enabled && !vd->vdev_notrim && vd->vdev_ops->vdev_op_leaf)
1484 trim_map_create(vd);
1486 for (int c = 0; c < vd->vdev_children; c++) {
1487 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1488 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1494 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1495 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1497 if (vd->vdev_children == 0) {
1498 if (osize < SPA_MINDEVSIZE) {
1499 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1500 VDEV_AUX_TOO_SMALL);
1501 return (SET_ERROR(EOVERFLOW));
1504 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1505 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
1506 VDEV_LABEL_END_SIZE);
1508 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1509 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1510 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1511 VDEV_AUX_TOO_SMALL);
1512 return (SET_ERROR(EOVERFLOW));
1516 max_asize = max_osize;
1519 vd->vdev_psize = psize;
1522 * Make sure the allocatable size hasn't shrunk too much.
1524 if (asize < vd->vdev_min_asize) {
1525 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1526 VDEV_AUX_BAD_LABEL);
1527 return (SET_ERROR(EINVAL));
1530 vd->vdev_physical_ashift =
1531 MAX(physical_ashift, vd->vdev_physical_ashift);
1532 vd->vdev_logical_ashift = MAX(logical_ashift, vd->vdev_logical_ashift);
1533 vd->vdev_ashift = MAX(vd->vdev_logical_ashift, vd->vdev_ashift);
1535 if (vd->vdev_logical_ashift > SPA_MAXASHIFT) {
1536 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1537 VDEV_AUX_ASHIFT_TOO_BIG);
1541 if (vd->vdev_asize == 0) {
1543 * This is the first-ever open, so use the computed values.
1544 * For testing purposes, a higher ashift can be requested.
1546 vd->vdev_asize = asize;
1547 vd->vdev_max_asize = max_asize;
1550 * Make sure the alignment requirement hasn't increased.
1552 if (vd->vdev_ashift > vd->vdev_top->vdev_ashift &&
1553 vd->vdev_ops->vdev_op_leaf) {
1554 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1555 VDEV_AUX_BAD_LABEL);
1558 vd->vdev_max_asize = max_asize;
1562 * If all children are healthy we update asize if either:
1563 * The asize has increased, due to a device expansion caused by dynamic
1564 * LUN growth or vdev replacement, and automatic expansion is enabled;
1565 * making the additional space available.
1567 * The asize has decreased, due to a device shrink usually caused by a
1568 * vdev replace with a smaller device. This ensures that calculations
1569 * based of max_asize and asize e.g. esize are always valid. It's safe
1570 * to do this as we've already validated that asize is greater than
1573 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1574 ((asize > vd->vdev_asize &&
1575 (vd->vdev_expanding || spa->spa_autoexpand)) ||
1576 (asize < vd->vdev_asize)))
1577 vd->vdev_asize = asize;
1579 vdev_set_min_asize(vd);
1582 * Ensure we can issue some IO before declaring the
1583 * vdev open for business.
1585 if (vd->vdev_ops->vdev_op_leaf &&
1586 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1587 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1588 VDEV_AUX_ERR_EXCEEDED);
1593 * Track the min and max ashift values for normal data devices.
1595 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1596 !vd->vdev_islog && vd->vdev_aux == NULL) {
1597 if (vd->vdev_ashift > spa->spa_max_ashift)
1598 spa->spa_max_ashift = vd->vdev_ashift;
1599 if (vd->vdev_ashift < spa->spa_min_ashift)
1600 spa->spa_min_ashift = vd->vdev_ashift;
1604 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1605 * resilver. But don't do this if we are doing a reopen for a scrub,
1606 * since this would just restart the scrub we are already doing.
1608 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
1609 vdev_resilver_needed(vd, NULL, NULL))
1610 spa_async_request(spa, SPA_ASYNC_RESILVER);
1616 * Called once the vdevs are all opened, this routine validates the label
1617 * contents. This needs to be done before vdev_load() so that we don't
1618 * inadvertently do repair I/Os to the wrong device.
1620 * This function will only return failure if one of the vdevs indicates that it
1621 * has since been destroyed or exported. This is only possible if
1622 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1623 * will be updated but the function will return 0.
1626 vdev_validate(vdev_t *vd)
1628 spa_t *spa = vd->vdev_spa;
1630 uint64_t guid = 0, aux_guid = 0, top_guid;
1635 if (vdev_validate_skip)
1638 for (uint64_t c = 0; c < vd->vdev_children; c++)
1639 if (vdev_validate(vd->vdev_child[c]) != 0)
1640 return (SET_ERROR(EBADF));
1643 * If the device has already failed, or was marked offline, don't do
1644 * any further validation. Otherwise, label I/O will fail and we will
1645 * overwrite the previous state.
1647 if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
1651 * If we are performing an extreme rewind, we allow for a label that
1652 * was modified at a point after the current txg.
1654 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0)
1657 txg = spa_last_synced_txg(spa);
1659 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1660 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1661 VDEV_AUX_BAD_LABEL);
1662 vdev_dbgmsg(vd, "vdev_validate: failed reading config");
1667 * Determine if this vdev has been split off into another
1668 * pool. If so, then refuse to open it.
1670 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
1671 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
1672 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1673 VDEV_AUX_SPLIT_POOL);
1675 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
1679 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
1680 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1681 VDEV_AUX_CORRUPT_DATA);
1683 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1684 ZPOOL_CONFIG_POOL_GUID);
1689 * If config is not trusted then ignore the spa guid check. This is
1690 * necessary because if the machine crashed during a re-guid the new
1691 * guid might have been written to all of the vdev labels, but not the
1692 * cached config. The check will be performed again once we have the
1693 * trusted config from the MOS.
1695 if (spa->spa_trust_config && guid != spa_guid(spa)) {
1696 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1697 VDEV_AUX_CORRUPT_DATA);
1699 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
1700 "match config (%llu != %llu)", (u_longlong_t)guid,
1701 (u_longlong_t)spa_guid(spa));
1705 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
1706 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
1710 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
1711 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1712 VDEV_AUX_CORRUPT_DATA);
1714 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1719 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
1721 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1722 VDEV_AUX_CORRUPT_DATA);
1724 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1725 ZPOOL_CONFIG_TOP_GUID);
1730 * If this vdev just became a top-level vdev because its sibling was
1731 * detached, it will have adopted the parent's vdev guid -- but the
1732 * label may or may not be on disk yet. Fortunately, either version
1733 * of the label will have the same top guid, so if we're a top-level
1734 * vdev, we can safely compare to that instead.
1735 * However, if the config comes from a cachefile that failed to update
1736 * after the detach, a top-level vdev will appear as a non top-level
1737 * vdev in the config. Also relax the constraints if we perform an
1740 * If we split this vdev off instead, then we also check the
1741 * original pool's guid. We don't want to consider the vdev
1742 * corrupt if it is partway through a split operation.
1744 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
1745 boolean_t mismatch = B_FALSE;
1746 if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
1747 if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
1750 if (vd->vdev_guid != top_guid &&
1751 vd->vdev_top->vdev_guid != guid)
1756 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1757 VDEV_AUX_CORRUPT_DATA);
1759 vdev_dbgmsg(vd, "vdev_validate: config guid "
1760 "doesn't match label guid");
1761 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
1762 (u_longlong_t)vd->vdev_guid,
1763 (u_longlong_t)vd->vdev_top->vdev_guid);
1764 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
1765 "aux_guid %llu", (u_longlong_t)guid,
1766 (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
1771 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1773 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1774 VDEV_AUX_CORRUPT_DATA);
1776 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1777 ZPOOL_CONFIG_POOL_STATE);
1784 * If this is a verbatim import, no need to check the
1785 * state of the pool.
1787 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1788 spa_load_state(spa) == SPA_LOAD_OPEN &&
1789 state != POOL_STATE_ACTIVE) {
1790 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
1791 "for spa %s", (u_longlong_t)state, spa->spa_name);
1792 return (SET_ERROR(EBADF));
1796 * If we were able to open and validate a vdev that was
1797 * previously marked permanently unavailable, clear that state
1800 if (vd->vdev_not_present)
1801 vd->vdev_not_present = 0;
1807 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
1809 if (svd->vdev_path != NULL && dvd->vdev_path != NULL) {
1810 if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) {
1811 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
1812 "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
1813 dvd->vdev_path, svd->vdev_path);
1814 spa_strfree(dvd->vdev_path);
1815 dvd->vdev_path = spa_strdup(svd->vdev_path);
1817 } else if (svd->vdev_path != NULL) {
1818 dvd->vdev_path = spa_strdup(svd->vdev_path);
1819 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
1820 (u_longlong_t)dvd->vdev_guid, dvd->vdev_path);
1825 * Recursively copy vdev paths from one vdev to another. Source and destination
1826 * vdev trees must have same geometry otherwise return error. Intended to copy
1827 * paths from userland config into MOS config.
1830 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
1832 if ((svd->vdev_ops == &vdev_missing_ops) ||
1833 (svd->vdev_ishole && dvd->vdev_ishole) ||
1834 (dvd->vdev_ops == &vdev_indirect_ops))
1837 if (svd->vdev_ops != dvd->vdev_ops) {
1838 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
1839 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
1840 return (SET_ERROR(EINVAL));
1843 if (svd->vdev_guid != dvd->vdev_guid) {
1844 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
1845 "%llu)", (u_longlong_t)svd->vdev_guid,
1846 (u_longlong_t)dvd->vdev_guid);
1847 return (SET_ERROR(EINVAL));
1850 if (svd->vdev_children != dvd->vdev_children) {
1851 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
1852 "%llu != %llu", (u_longlong_t)svd->vdev_children,
1853 (u_longlong_t)dvd->vdev_children);
1854 return (SET_ERROR(EINVAL));
1857 for (uint64_t i = 0; i < svd->vdev_children; i++) {
1858 int error = vdev_copy_path_strict(svd->vdev_child[i],
1859 dvd->vdev_child[i]);
1864 if (svd->vdev_ops->vdev_op_leaf)
1865 vdev_copy_path_impl(svd, dvd);
1871 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
1873 ASSERT(stvd->vdev_top == stvd);
1874 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
1876 for (uint64_t i = 0; i < dvd->vdev_children; i++) {
1877 vdev_copy_path_search(stvd, dvd->vdev_child[i]);
1880 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
1884 * The idea here is that while a vdev can shift positions within
1885 * a top vdev (when replacing, attaching mirror, etc.) it cannot
1886 * step outside of it.
1888 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
1890 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
1893 ASSERT(vd->vdev_ops->vdev_op_leaf);
1895 vdev_copy_path_impl(vd, dvd);
1899 * Recursively copy vdev paths from one root vdev to another. Source and
1900 * destination vdev trees may differ in geometry. For each destination leaf
1901 * vdev, search a vdev with the same guid and top vdev id in the source.
1902 * Intended to copy paths from userland config into MOS config.
1905 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
1907 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
1908 ASSERT(srvd->vdev_ops == &vdev_root_ops);
1909 ASSERT(drvd->vdev_ops == &vdev_root_ops);
1911 for (uint64_t i = 0; i < children; i++) {
1912 vdev_copy_path_search(srvd->vdev_child[i],
1913 drvd->vdev_child[i]);
1918 * Close a virtual device.
1921 vdev_close(vdev_t *vd)
1923 spa_t *spa = vd->vdev_spa;
1924 vdev_t *pvd = vd->vdev_parent;
1926 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1929 * If our parent is reopening, then we are as well, unless we are
1932 if (pvd != NULL && pvd->vdev_reopening)
1933 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1935 vd->vdev_ops->vdev_op_close(vd);
1937 vdev_cache_purge(vd);
1939 if (vd->vdev_ops->vdev_op_leaf)
1940 trim_map_destroy(vd);
1943 * We record the previous state before we close it, so that if we are
1944 * doing a reopen(), we don't generate FMA ereports if we notice that
1945 * it's still faulted.
1947 vd->vdev_prevstate = vd->vdev_state;
1949 if (vd->vdev_offline)
1950 vd->vdev_state = VDEV_STATE_OFFLINE;
1952 vd->vdev_state = VDEV_STATE_CLOSED;
1953 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1957 vdev_hold(vdev_t *vd)
1959 spa_t *spa = vd->vdev_spa;
1961 ASSERT(spa_is_root(spa));
1962 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1965 for (int c = 0; c < vd->vdev_children; c++)
1966 vdev_hold(vd->vdev_child[c]);
1968 if (vd->vdev_ops->vdev_op_leaf)
1969 vd->vdev_ops->vdev_op_hold(vd);
1973 vdev_rele(vdev_t *vd)
1975 spa_t *spa = vd->vdev_spa;
1977 ASSERT(spa_is_root(spa));
1978 for (int c = 0; c < vd->vdev_children; c++)
1979 vdev_rele(vd->vdev_child[c]);
1981 if (vd->vdev_ops->vdev_op_leaf)
1982 vd->vdev_ops->vdev_op_rele(vd);
1986 * Reopen all interior vdevs and any unopened leaves. We don't actually
1987 * reopen leaf vdevs which had previously been opened as they might deadlock
1988 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
1989 * If the leaf has never been opened then open it, as usual.
1992 vdev_reopen(vdev_t *vd)
1994 spa_t *spa = vd->vdev_spa;
1996 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1998 /* set the reopening flag unless we're taking the vdev offline */
1999 vd->vdev_reopening = !vd->vdev_offline;
2001 (void) vdev_open(vd);
2004 * Call vdev_validate() here to make sure we have the same device.
2005 * Otherwise, a device with an invalid label could be successfully
2006 * opened in response to vdev_reopen().
2009 (void) vdev_validate_aux(vd);
2010 if (vdev_readable(vd) && vdev_writeable(vd) &&
2011 vd->vdev_aux == &spa->spa_l2cache &&
2012 !l2arc_vdev_present(vd))
2013 l2arc_add_vdev(spa, vd);
2015 (void) vdev_validate(vd);
2019 * Reassess parent vdev's health.
2021 vdev_propagate_state(vd);
2025 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
2030 * Normally, partial opens (e.g. of a mirror) are allowed.
2031 * For a create, however, we want to fail the request if
2032 * there are any components we can't open.
2034 error = vdev_open(vd);
2036 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
2038 return (error ? error : ENXIO);
2042 * Recursively load DTLs and initialize all labels.
2044 if ((error = vdev_dtl_load(vd)) != 0 ||
2045 (error = vdev_label_init(vd, txg, isreplacing ?
2046 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
2055 vdev_metaslab_set_size(vdev_t *vd)
2058 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
2060 vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev);
2061 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
2065 * Maximize performance by inflating the configured ashift for top level
2066 * vdevs to be as close to the physical ashift as possible while maintaining
2067 * administrator defined limits and ensuring it doesn't go below the
2071 vdev_ashift_optimize(vdev_t *vd)
2073 if (vd == vd->vdev_top) {
2074 if (vd->vdev_ashift < vd->vdev_physical_ashift) {
2075 vd->vdev_ashift = MIN(
2076 MAX(zfs_max_auto_ashift, vd->vdev_ashift),
2077 MAX(zfs_min_auto_ashift, vd->vdev_physical_ashift));
2080 * Unusual case where logical ashift > physical ashift
2081 * so we can't cap the calculated ashift based on max
2082 * ashift as that would cause failures.
2083 * We still check if we need to increase it to match
2086 vd->vdev_ashift = MAX(zfs_min_auto_ashift,
2093 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2095 ASSERT(vd == vd->vdev_top);
2096 /* indirect vdevs don't have metaslabs or dtls */
2097 ASSERT(vdev_is_concrete(vd) || flags == 0);
2098 ASSERT(ISP2(flags));
2099 ASSERT(spa_writeable(vd->vdev_spa));
2101 if (flags & VDD_METASLAB)
2102 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2104 if (flags & VDD_DTL)
2105 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2107 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2111 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
2113 for (int c = 0; c < vd->vdev_children; c++)
2114 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
2116 if (vd->vdev_ops->vdev_op_leaf)
2117 vdev_dirty(vd->vdev_top, flags, vd, txg);
2123 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2124 * the vdev has less than perfect replication. There are four kinds of DTL:
2126 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2128 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2130 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2131 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2132 * txgs that was scrubbed.
2134 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2135 * persistent errors or just some device being offline.
2136 * Unlike the other three, the DTL_OUTAGE map is not generally
2137 * maintained; it's only computed when needed, typically to
2138 * determine whether a device can be detached.
2140 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2141 * either has the data or it doesn't.
2143 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2144 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2145 * if any child is less than fully replicated, then so is its parent.
2146 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2147 * comprising only those txgs which appear in 'maxfaults' or more children;
2148 * those are the txgs we don't have enough replication to read. For example,
2149 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2150 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2151 * two child DTL_MISSING maps.
2153 * It should be clear from the above that to compute the DTLs and outage maps
2154 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2155 * Therefore, that is all we keep on disk. When loading the pool, or after
2156 * a configuration change, we generate all other DTLs from first principles.
2159 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2161 range_tree_t *rt = vd->vdev_dtl[t];
2163 ASSERT(t < DTL_TYPES);
2164 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2165 ASSERT(spa_writeable(vd->vdev_spa));
2167 mutex_enter(&vd->vdev_dtl_lock);
2168 if (!range_tree_contains(rt, txg, size))
2169 range_tree_add(rt, txg, size);
2170 mutex_exit(&vd->vdev_dtl_lock);
2174 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2176 range_tree_t *rt = vd->vdev_dtl[t];
2177 boolean_t dirty = B_FALSE;
2179 ASSERT(t < DTL_TYPES);
2180 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2183 * While we are loading the pool, the DTLs have not been loaded yet.
2184 * Ignore the DTLs and try all devices. This avoids a recursive
2185 * mutex enter on the vdev_dtl_lock, and also makes us try hard
2186 * when loading the pool (relying on the checksum to ensure that
2187 * we get the right data -- note that we while loading, we are
2188 * only reading the MOS, which is always checksummed).
2190 if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE)
2193 mutex_enter(&vd->vdev_dtl_lock);
2194 if (range_tree_space(rt) != 0)
2195 dirty = range_tree_contains(rt, txg, size);
2196 mutex_exit(&vd->vdev_dtl_lock);
2202 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
2204 range_tree_t *rt = vd->vdev_dtl[t];
2207 mutex_enter(&vd->vdev_dtl_lock);
2208 empty = (range_tree_space(rt) == 0);
2209 mutex_exit(&vd->vdev_dtl_lock);
2215 * Returns the lowest txg in the DTL range.
2218 vdev_dtl_min(vdev_t *vd)
2222 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2223 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2224 ASSERT0(vd->vdev_children);
2226 rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
2227 return (rs->rs_start - 1);
2231 * Returns the highest txg in the DTL.
2234 vdev_dtl_max(vdev_t *vd)
2238 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2239 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2240 ASSERT0(vd->vdev_children);
2242 rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
2243 return (rs->rs_end);
2247 * Determine if a resilvering vdev should remove any DTL entries from
2248 * its range. If the vdev was resilvering for the entire duration of the
2249 * scan then it should excise that range from its DTLs. Otherwise, this
2250 * vdev is considered partially resilvered and should leave its DTL
2251 * entries intact. The comment in vdev_dtl_reassess() describes how we
2255 vdev_dtl_should_excise(vdev_t *vd)
2257 spa_t *spa = vd->vdev_spa;
2258 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2260 ASSERT0(scn->scn_phys.scn_errors);
2261 ASSERT0(vd->vdev_children);
2263 if (vd->vdev_state < VDEV_STATE_DEGRADED)
2266 if (vd->vdev_resilver_txg == 0 ||
2267 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0)
2271 * When a resilver is initiated the scan will assign the scn_max_txg
2272 * value to the highest txg value that exists in all DTLs. If this
2273 * device's max DTL is not part of this scan (i.e. it is not in
2274 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2277 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
2278 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
2279 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
2280 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
2287 * Reassess DTLs after a config change or scrub completion.
2290 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
2292 spa_t *spa = vd->vdev_spa;
2296 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2298 for (int c = 0; c < vd->vdev_children; c++)
2299 vdev_dtl_reassess(vd->vdev_child[c], txg,
2300 scrub_txg, scrub_done);
2302 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
2305 if (vd->vdev_ops->vdev_op_leaf) {
2306 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2308 mutex_enter(&vd->vdev_dtl_lock);
2311 * If we've completed a scan cleanly then determine
2312 * if this vdev should remove any DTLs. We only want to
2313 * excise regions on vdevs that were available during
2314 * the entire duration of this scan.
2316 if (scrub_txg != 0 &&
2317 (spa->spa_scrub_started ||
2318 (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
2319 vdev_dtl_should_excise(vd)) {
2321 * We completed a scrub up to scrub_txg. If we
2322 * did it without rebooting, then the scrub dtl
2323 * will be valid, so excise the old region and
2324 * fold in the scrub dtl. Otherwise, leave the
2325 * dtl as-is if there was an error.
2327 * There's little trick here: to excise the beginning
2328 * of the DTL_MISSING map, we put it into a reference
2329 * tree and then add a segment with refcnt -1 that
2330 * covers the range [0, scrub_txg). This means
2331 * that each txg in that range has refcnt -1 or 0.
2332 * We then add DTL_SCRUB with a refcnt of 2, so that
2333 * entries in the range [0, scrub_txg) will have a
2334 * positive refcnt -- either 1 or 2. We then convert
2335 * the reference tree into the new DTL_MISSING map.
2337 space_reftree_create(&reftree);
2338 space_reftree_add_map(&reftree,
2339 vd->vdev_dtl[DTL_MISSING], 1);
2340 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
2341 space_reftree_add_map(&reftree,
2342 vd->vdev_dtl[DTL_SCRUB], 2);
2343 space_reftree_generate_map(&reftree,
2344 vd->vdev_dtl[DTL_MISSING], 1);
2345 space_reftree_destroy(&reftree);
2347 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
2348 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
2349 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
2351 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
2352 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
2353 if (!vdev_readable(vd))
2354 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
2356 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
2357 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
2360 * If the vdev was resilvering and no longer has any
2361 * DTLs then reset its resilvering flag and dirty
2362 * the top level so that we persist the change.
2364 if (vd->vdev_resilver_txg != 0 &&
2365 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 &&
2366 range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0) {
2367 vd->vdev_resilver_txg = 0;
2368 vdev_config_dirty(vd->vdev_top);
2371 mutex_exit(&vd->vdev_dtl_lock);
2374 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2378 mutex_enter(&vd->vdev_dtl_lock);
2379 for (int t = 0; t < DTL_TYPES; t++) {
2380 /* account for child's outage in parent's missing map */
2381 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
2383 continue; /* leaf vdevs only */
2384 if (t == DTL_PARTIAL)
2385 minref = 1; /* i.e. non-zero */
2386 else if (vd->vdev_nparity != 0)
2387 minref = vd->vdev_nparity + 1; /* RAID-Z */
2389 minref = vd->vdev_children; /* any kind of mirror */
2390 space_reftree_create(&reftree);
2391 for (int c = 0; c < vd->vdev_children; c++) {
2392 vdev_t *cvd = vd->vdev_child[c];
2393 mutex_enter(&cvd->vdev_dtl_lock);
2394 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
2395 mutex_exit(&cvd->vdev_dtl_lock);
2397 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
2398 space_reftree_destroy(&reftree);
2400 mutex_exit(&vd->vdev_dtl_lock);
2404 vdev_dtl_load(vdev_t *vd)
2406 spa_t *spa = vd->vdev_spa;
2407 objset_t *mos = spa->spa_meta_objset;
2410 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
2411 ASSERT(vdev_is_concrete(vd));
2413 error = space_map_open(&vd->vdev_dtl_sm, mos,
2414 vd->vdev_dtl_object, 0, -1ULL, 0);
2417 ASSERT(vd->vdev_dtl_sm != NULL);
2419 mutex_enter(&vd->vdev_dtl_lock);
2422 * Now that we've opened the space_map we need to update
2425 space_map_update(vd->vdev_dtl_sm);
2427 error = space_map_load(vd->vdev_dtl_sm,
2428 vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
2429 mutex_exit(&vd->vdev_dtl_lock);
2434 for (int c = 0; c < vd->vdev_children; c++) {
2435 error = vdev_dtl_load(vd->vdev_child[c]);
2444 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
2446 spa_t *spa = vd->vdev_spa;
2448 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
2449 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2454 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
2456 spa_t *spa = vd->vdev_spa;
2457 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
2458 DMU_OT_NONE, 0, tx);
2461 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2468 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
2470 if (vd->vdev_ops != &vdev_hole_ops &&
2471 vd->vdev_ops != &vdev_missing_ops &&
2472 vd->vdev_ops != &vdev_root_ops &&
2473 !vd->vdev_top->vdev_removing) {
2474 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
2475 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
2477 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
2478 vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
2481 for (uint64_t i = 0; i < vd->vdev_children; i++) {
2482 vdev_construct_zaps(vd->vdev_child[i], tx);
2487 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
2489 spa_t *spa = vd->vdev_spa;
2490 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2491 objset_t *mos = spa->spa_meta_objset;
2492 range_tree_t *rtsync;
2494 uint64_t object = space_map_object(vd->vdev_dtl_sm);
2496 ASSERT(vdev_is_concrete(vd));
2497 ASSERT(vd->vdev_ops->vdev_op_leaf);
2499 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2501 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
2502 mutex_enter(&vd->vdev_dtl_lock);
2503 space_map_free(vd->vdev_dtl_sm, tx);
2504 space_map_close(vd->vdev_dtl_sm);
2505 vd->vdev_dtl_sm = NULL;
2506 mutex_exit(&vd->vdev_dtl_lock);
2509 * We only destroy the leaf ZAP for detached leaves or for
2510 * removed log devices. Removed data devices handle leaf ZAP
2511 * cleanup later, once cancellation is no longer possible.
2513 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2514 vd->vdev_top->vdev_islog)) {
2515 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2516 vd->vdev_leaf_zap = 0;
2523 if (vd->vdev_dtl_sm == NULL) {
2524 uint64_t new_object;
2526 new_object = space_map_alloc(mos, tx);
2527 VERIFY3U(new_object, !=, 0);
2529 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
2531 ASSERT(vd->vdev_dtl_sm != NULL);
2534 rtsync = range_tree_create(NULL, NULL);
2536 mutex_enter(&vd->vdev_dtl_lock);
2537 range_tree_walk(rt, range_tree_add, rtsync);
2538 mutex_exit(&vd->vdev_dtl_lock);
2540 space_map_truncate(vd->vdev_dtl_sm, tx);
2541 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
2542 range_tree_vacate(rtsync, NULL, NULL);
2544 range_tree_destroy(rtsync);
2547 * If the object for the space map has changed then dirty
2548 * the top level so that we update the config.
2550 if (object != space_map_object(vd->vdev_dtl_sm)) {
2551 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
2552 "new object %llu", (u_longlong_t)txg, spa_name(spa),
2553 (u_longlong_t)object,
2554 (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
2555 vdev_config_dirty(vd->vdev_top);
2560 mutex_enter(&vd->vdev_dtl_lock);
2561 space_map_update(vd->vdev_dtl_sm);
2562 mutex_exit(&vd->vdev_dtl_lock);
2566 * Determine whether the specified vdev can be offlined/detached/removed
2567 * without losing data.
2570 vdev_dtl_required(vdev_t *vd)
2572 spa_t *spa = vd->vdev_spa;
2573 vdev_t *tvd = vd->vdev_top;
2574 uint8_t cant_read = vd->vdev_cant_read;
2577 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2579 if (vd == spa->spa_root_vdev || vd == tvd)
2583 * Temporarily mark the device as unreadable, and then determine
2584 * whether this results in any DTL outages in the top-level vdev.
2585 * If not, we can safely offline/detach/remove the device.
2587 vd->vdev_cant_read = B_TRUE;
2588 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2589 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
2590 vd->vdev_cant_read = cant_read;
2591 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2593 if (!required && zio_injection_enabled)
2594 required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2600 * Determine if resilver is needed, and if so the txg range.
2603 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2605 boolean_t needed = B_FALSE;
2606 uint64_t thismin = UINT64_MAX;
2607 uint64_t thismax = 0;
2609 if (vd->vdev_children == 0) {
2610 mutex_enter(&vd->vdev_dtl_lock);
2611 if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 &&
2612 vdev_writeable(vd)) {
2614 thismin = vdev_dtl_min(vd);
2615 thismax = vdev_dtl_max(vd);
2618 mutex_exit(&vd->vdev_dtl_lock);
2620 for (int c = 0; c < vd->vdev_children; c++) {
2621 vdev_t *cvd = vd->vdev_child[c];
2622 uint64_t cmin, cmax;
2624 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2625 thismin = MIN(thismin, cmin);
2626 thismax = MAX(thismax, cmax);
2632 if (needed && minp) {
2640 vdev_load(vdev_t *vd)
2644 * Recursively load all children.
2646 for (int c = 0; c < vd->vdev_children; c++) {
2647 error = vdev_load(vd->vdev_child[c]);
2653 vdev_set_deflate_ratio(vd);
2656 * If this is a top-level vdev, initialize its metaslabs.
2658 if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
2659 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
2660 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2661 VDEV_AUX_CORRUPT_DATA);
2662 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
2663 "asize=%llu", (u_longlong_t)vd->vdev_ashift,
2664 (u_longlong_t)vd->vdev_asize);
2665 return (SET_ERROR(ENXIO));
2666 } else if ((error = vdev_metaslab_init(vd, 0)) != 0) {
2667 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
2668 "[error=%d]", error);
2669 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2670 VDEV_AUX_CORRUPT_DATA);
2676 * If this is a leaf vdev, load its DTL.
2678 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
2679 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2680 VDEV_AUX_CORRUPT_DATA);
2681 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
2682 "[error=%d]", error);
2686 uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd);
2687 if (obsolete_sm_object != 0) {
2688 objset_t *mos = vd->vdev_spa->spa_meta_objset;
2689 ASSERT(vd->vdev_asize != 0);
2690 ASSERT(vd->vdev_obsolete_sm == NULL);
2692 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
2693 obsolete_sm_object, 0, vd->vdev_asize, 0))) {
2694 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2695 VDEV_AUX_CORRUPT_DATA);
2696 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
2697 "obsolete spacemap (obj %llu) [error=%d]",
2698 (u_longlong_t)obsolete_sm_object, error);
2701 space_map_update(vd->vdev_obsolete_sm);
2708 * The special vdev case is used for hot spares and l2cache devices. Its
2709 * sole purpose it to set the vdev state for the associated vdev. To do this,
2710 * we make sure that we can open the underlying device, then try to read the
2711 * label, and make sure that the label is sane and that it hasn't been
2712 * repurposed to another pool.
2715 vdev_validate_aux(vdev_t *vd)
2718 uint64_t guid, version;
2721 if (!vdev_readable(vd))
2724 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
2725 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2726 VDEV_AUX_CORRUPT_DATA);
2730 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2731 !SPA_VERSION_IS_SUPPORTED(version) ||
2732 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
2733 guid != vd->vdev_guid ||
2734 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
2735 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2736 VDEV_AUX_CORRUPT_DATA);
2742 * We don't actually check the pool state here. If it's in fact in
2743 * use by another pool, we update this fact on the fly when requested.
2750 * Free the objects used to store this vdev's spacemaps, and the array
2751 * that points to them.
2754 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
2756 if (vd->vdev_ms_array == 0)
2759 objset_t *mos = vd->vdev_spa->spa_meta_objset;
2760 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
2761 size_t array_bytes = array_count * sizeof (uint64_t);
2762 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
2763 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
2764 array_bytes, smobj_array, 0));
2766 for (uint64_t i = 0; i < array_count; i++) {
2767 uint64_t smobj = smobj_array[i];
2771 space_map_free_obj(mos, smobj, tx);
2774 kmem_free(smobj_array, array_bytes);
2775 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
2776 vd->vdev_ms_array = 0;
2780 vdev_remove_empty(vdev_t *vd, uint64_t txg)
2782 spa_t *spa = vd->vdev_spa;
2785 ASSERT(vd == vd->vdev_top);
2786 ASSERT3U(txg, ==, spa_syncing_txg(spa));
2788 if (vd->vdev_ms != NULL) {
2789 metaslab_group_t *mg = vd->vdev_mg;
2791 metaslab_group_histogram_verify(mg);
2792 metaslab_class_histogram_verify(mg->mg_class);
2794 for (int m = 0; m < vd->vdev_ms_count; m++) {
2795 metaslab_t *msp = vd->vdev_ms[m];
2797 if (msp == NULL || msp->ms_sm == NULL)
2800 mutex_enter(&msp->ms_lock);
2802 * If the metaslab was not loaded when the vdev
2803 * was removed then the histogram accounting may
2804 * not be accurate. Update the histogram information
2805 * here so that we ensure that the metaslab group
2806 * and metaslab class are up-to-date.
2808 metaslab_group_histogram_remove(mg, msp);
2810 VERIFY0(space_map_allocated(msp->ms_sm));
2811 space_map_close(msp->ms_sm);
2813 mutex_exit(&msp->ms_lock);
2816 metaslab_group_histogram_verify(mg);
2817 metaslab_class_histogram_verify(mg->mg_class);
2818 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
2819 ASSERT0(mg->mg_histogram[i]);
2822 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
2823 vdev_destroy_spacemaps(vd, tx);
2825 if (vd->vdev_islog && vd->vdev_top_zap != 0) {
2826 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
2827 vd->vdev_top_zap = 0;
2833 vdev_sync_done(vdev_t *vd, uint64_t txg)
2836 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2838 ASSERT(vdev_is_concrete(vd));
2840 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2841 metaslab_sync_done(msp, txg);
2844 metaslab_sync_reassess(vd->vdev_mg);
2848 vdev_sync(vdev_t *vd, uint64_t txg)
2850 spa_t *spa = vd->vdev_spa;
2855 if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
2858 ASSERT(vd->vdev_removing ||
2859 vd->vdev_ops == &vdev_indirect_ops);
2861 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2862 vdev_indirect_sync_obsolete(vd, tx);
2866 * If the vdev is indirect, it can't have dirty
2867 * metaslabs or DTLs.
2869 if (vd->vdev_ops == &vdev_indirect_ops) {
2870 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
2871 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
2876 ASSERT(vdev_is_concrete(vd));
2878 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
2879 !vd->vdev_removing) {
2880 ASSERT(vd == vd->vdev_top);
2881 ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
2882 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2883 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2884 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2885 ASSERT(vd->vdev_ms_array != 0);
2886 vdev_config_dirty(vd);
2890 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2891 metaslab_sync(msp, txg);
2892 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2895 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2896 vdev_dtl_sync(lvd, txg);
2899 * Remove the metadata associated with this vdev once it's empty.
2900 * Note that this is typically used for log/cache device removal;
2901 * we don't empty toplevel vdevs when removing them. But if
2902 * a toplevel happens to be emptied, this is not harmful.
2904 if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) {
2905 vdev_remove_empty(vd, txg);
2908 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2912 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2914 return (vd->vdev_ops->vdev_op_asize(vd, psize));
2918 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
2919 * not be opened, and no I/O is attempted.
2922 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2926 spa_vdev_state_enter(spa, SCL_NONE);
2928 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2929 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2931 if (!vd->vdev_ops->vdev_op_leaf)
2932 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2937 * We don't directly use the aux state here, but if we do a
2938 * vdev_reopen(), we need this value to be present to remember why we
2941 vd->vdev_label_aux = aux;
2944 * Faulted state takes precedence over degraded.
2946 vd->vdev_delayed_close = B_FALSE;
2947 vd->vdev_faulted = 1ULL;
2948 vd->vdev_degraded = 0ULL;
2949 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
2952 * If this device has the only valid copy of the data, then
2953 * back off and simply mark the vdev as degraded instead.
2955 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
2956 vd->vdev_degraded = 1ULL;
2957 vd->vdev_faulted = 0ULL;
2960 * If we reopen the device and it's not dead, only then do we
2965 if (vdev_readable(vd))
2966 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
2969 return (spa_vdev_state_exit(spa, vd, 0));
2973 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
2974 * user that something is wrong. The vdev continues to operate as normal as far
2975 * as I/O is concerned.
2978 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2982 spa_vdev_state_enter(spa, SCL_NONE);
2984 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2985 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2987 if (!vd->vdev_ops->vdev_op_leaf)
2988 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2991 * If the vdev is already faulted, then don't do anything.
2993 if (vd->vdev_faulted || vd->vdev_degraded)
2994 return (spa_vdev_state_exit(spa, NULL, 0));
2996 vd->vdev_degraded = 1ULL;
2997 if (!vdev_is_dead(vd))
2998 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
3001 return (spa_vdev_state_exit(spa, vd, 0));
3005 * Online the given vdev.
3007 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
3008 * spare device should be detached when the device finishes resilvering.
3009 * Second, the online should be treated like a 'test' online case, so no FMA
3010 * events are generated if the device fails to open.
3013 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
3015 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
3016 boolean_t wasoffline;
3017 vdev_state_t oldstate;
3019 spa_vdev_state_enter(spa, SCL_NONE);
3021 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3022 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3024 if (!vd->vdev_ops->vdev_op_leaf)
3025 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3027 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
3028 oldstate = vd->vdev_state;
3031 vd->vdev_offline = B_FALSE;
3032 vd->vdev_tmpoffline = B_FALSE;
3033 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
3034 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
3036 /* XXX - L2ARC 1.0 does not support expansion */
3037 if (!vd->vdev_aux) {
3038 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3039 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
3043 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
3045 if (!vd->vdev_aux) {
3046 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3047 pvd->vdev_expanding = B_FALSE;
3051 *newstate = vd->vdev_state;
3052 if ((flags & ZFS_ONLINE_UNSPARE) &&
3053 !vdev_is_dead(vd) && vd->vdev_parent &&
3054 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3055 vd->vdev_parent->vdev_child[0] == vd)
3056 vd->vdev_unspare = B_TRUE;
3058 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
3060 /* XXX - L2ARC 1.0 does not support expansion */
3062 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
3063 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
3067 (oldstate < VDEV_STATE_DEGRADED &&
3068 vd->vdev_state >= VDEV_STATE_DEGRADED))
3069 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
3071 return (spa_vdev_state_exit(spa, vd, 0));
3075 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
3079 uint64_t generation;
3080 metaslab_group_t *mg;
3083 spa_vdev_state_enter(spa, SCL_ALLOC);
3085 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3086 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3088 if (!vd->vdev_ops->vdev_op_leaf)
3089 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3093 generation = spa->spa_config_generation + 1;
3096 * If the device isn't already offline, try to offline it.
3098 if (!vd->vdev_offline) {
3100 * If this device has the only valid copy of some data,
3101 * don't allow it to be offlined. Log devices are always
3104 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3105 vdev_dtl_required(vd))
3106 return (spa_vdev_state_exit(spa, NULL, EBUSY));
3109 * If the top-level is a slog and it has had allocations
3110 * then proceed. We check that the vdev's metaslab group
3111 * is not NULL since it's possible that we may have just
3112 * added this vdev but not yet initialized its metaslabs.
3114 if (tvd->vdev_islog && mg != NULL) {
3116 * Prevent any future allocations.
3118 metaslab_group_passivate(mg);
3119 (void) spa_vdev_state_exit(spa, vd, 0);
3121 error = spa_reset_logs(spa);
3123 spa_vdev_state_enter(spa, SCL_ALLOC);
3126 * Check to see if the config has changed.
3128 if (error || generation != spa->spa_config_generation) {
3129 metaslab_group_activate(mg);
3131 return (spa_vdev_state_exit(spa,
3133 (void) spa_vdev_state_exit(spa, vd, 0);
3136 ASSERT0(tvd->vdev_stat.vs_alloc);
3140 * Offline this device and reopen its top-level vdev.
3141 * If the top-level vdev is a log device then just offline
3142 * it. Otherwise, if this action results in the top-level
3143 * vdev becoming unusable, undo it and fail the request.
3145 vd->vdev_offline = B_TRUE;
3148 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3149 vdev_is_dead(tvd)) {
3150 vd->vdev_offline = B_FALSE;
3152 return (spa_vdev_state_exit(spa, NULL, EBUSY));
3156 * Add the device back into the metaslab rotor so that
3157 * once we online the device it's open for business.
3159 if (tvd->vdev_islog && mg != NULL)
3160 metaslab_group_activate(mg);
3163 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
3165 return (spa_vdev_state_exit(spa, vd, 0));
3169 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
3173 mutex_enter(&spa->spa_vdev_top_lock);
3174 error = vdev_offline_locked(spa, guid, flags);
3175 mutex_exit(&spa->spa_vdev_top_lock);
3181 * Clear the error counts associated with this vdev. Unlike vdev_online() and
3182 * vdev_offline(), we assume the spa config is locked. We also clear all
3183 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
3186 vdev_clear(spa_t *spa, vdev_t *vd)
3188 vdev_t *rvd = spa->spa_root_vdev;
3190 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3195 vd->vdev_stat.vs_read_errors = 0;
3196 vd->vdev_stat.vs_write_errors = 0;
3197 vd->vdev_stat.vs_checksum_errors = 0;
3199 for (int c = 0; c < vd->vdev_children; c++)
3200 vdev_clear(spa, vd->vdev_child[c]);
3203 for (int c = 0; c < spa->spa_l2cache.sav_count; c++)
3204 vdev_clear(spa, spa->spa_l2cache.sav_vdevs[c]);
3206 for (int c = 0; c < spa->spa_spares.sav_count; c++)
3207 vdev_clear(spa, spa->spa_spares.sav_vdevs[c]);
3211 * It makes no sense to "clear" an indirect vdev.
3213 if (!vdev_is_concrete(vd))
3217 * If we're in the FAULTED state or have experienced failed I/O, then
3218 * clear the persistent state and attempt to reopen the device. We
3219 * also mark the vdev config dirty, so that the new faulted state is
3220 * written out to disk.
3222 if (vd->vdev_faulted || vd->vdev_degraded ||
3223 !vdev_readable(vd) || !vdev_writeable(vd)) {
3226 * When reopening in reponse to a clear event, it may be due to
3227 * a fmadm repair request. In this case, if the device is
3228 * still broken, we want to still post the ereport again.
3230 vd->vdev_forcefault = B_TRUE;
3232 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
3233 vd->vdev_cant_read = B_FALSE;
3234 vd->vdev_cant_write = B_FALSE;
3236 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
3238 vd->vdev_forcefault = B_FALSE;
3240 if (vd != rvd && vdev_writeable(vd->vdev_top))
3241 vdev_state_dirty(vd->vdev_top);
3243 if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
3244 spa_async_request(spa, SPA_ASYNC_RESILVER);
3246 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
3250 * When clearing a FMA-diagnosed fault, we always want to
3251 * unspare the device, as we assume that the original spare was
3252 * done in response to the FMA fault.
3254 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
3255 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3256 vd->vdev_parent->vdev_child[0] == vd)
3257 vd->vdev_unspare = B_TRUE;
3261 vdev_is_dead(vdev_t *vd)
3264 * Holes and missing devices are always considered "dead".
3265 * This simplifies the code since we don't have to check for
3266 * these types of devices in the various code paths.
3267 * Instead we rely on the fact that we skip over dead devices
3268 * before issuing I/O to them.
3270 return (vd->vdev_state < VDEV_STATE_DEGRADED ||
3271 vd->vdev_ops == &vdev_hole_ops ||
3272 vd->vdev_ops == &vdev_missing_ops);
3276 vdev_readable(vdev_t *vd)
3278 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
3282 vdev_writeable(vdev_t *vd)
3284 return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
3285 vdev_is_concrete(vd));
3289 vdev_allocatable(vdev_t *vd)
3291 uint64_t state = vd->vdev_state;
3294 * We currently allow allocations from vdevs which may be in the
3295 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3296 * fails to reopen then we'll catch it later when we're holding
3297 * the proper locks. Note that we have to get the vdev state
3298 * in a local variable because although it changes atomically,
3299 * we're asking two separate questions about it.
3301 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
3302 !vd->vdev_cant_write && vdev_is_concrete(vd) &&
3303 vd->vdev_mg->mg_initialized);
3307 vdev_accessible(vdev_t *vd, zio_t *zio)
3309 ASSERT(zio->io_vd == vd);
3311 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
3314 if (zio->io_type == ZIO_TYPE_READ)
3315 return (!vd->vdev_cant_read);
3317 if (zio->io_type == ZIO_TYPE_WRITE)
3318 return (!vd->vdev_cant_write);
3324 * Get statistics for the given vdev.
3327 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
3329 spa_t *spa = vd->vdev_spa;
3330 vdev_t *rvd = spa->spa_root_vdev;
3331 vdev_t *tvd = vd->vdev_top;
3333 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
3335 mutex_enter(&vd->vdev_stat_lock);
3336 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
3337 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
3338 vs->vs_state = vd->vdev_state;
3339 vs->vs_rsize = vdev_get_min_asize(vd);
3340 if (vd->vdev_ops->vdev_op_leaf)
3341 vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
3343 * Report expandable space on top-level, non-auxillary devices only.
3344 * The expandable space is reported in terms of metaslab sized units
3345 * since that determines how much space the pool can expand.
3347 if (vd->vdev_aux == NULL && tvd != NULL && vd->vdev_max_asize != 0) {
3348 vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize -
3349 spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift);
3351 vs->vs_configured_ashift = vd->vdev_top != NULL
3352 ? vd->vdev_top->vdev_ashift : vd->vdev_ashift;
3353 vs->vs_logical_ashift = vd->vdev_logical_ashift;
3354 vs->vs_physical_ashift = vd->vdev_physical_ashift;
3355 if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
3356 vdev_is_concrete(vd)) {
3357 vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
3361 * If we're getting stats on the root vdev, aggregate the I/O counts
3362 * over all top-level vdevs (i.e. the direct children of the root).
3365 for (int c = 0; c < rvd->vdev_children; c++) {
3366 vdev_t *cvd = rvd->vdev_child[c];
3367 vdev_stat_t *cvs = &cvd->vdev_stat;
3369 for (int t = 0; t < ZIO_TYPES; t++) {
3370 vs->vs_ops[t] += cvs->vs_ops[t];
3371 vs->vs_bytes[t] += cvs->vs_bytes[t];
3373 cvs->vs_scan_removing = cvd->vdev_removing;
3376 mutex_exit(&vd->vdev_stat_lock);
3380 vdev_clear_stats(vdev_t *vd)
3382 mutex_enter(&vd->vdev_stat_lock);
3383 vd->vdev_stat.vs_space = 0;
3384 vd->vdev_stat.vs_dspace = 0;
3385 vd->vdev_stat.vs_alloc = 0;
3386 mutex_exit(&vd->vdev_stat_lock);
3390 vdev_scan_stat_init(vdev_t *vd)
3392 vdev_stat_t *vs = &vd->vdev_stat;
3394 for (int c = 0; c < vd->vdev_children; c++)
3395 vdev_scan_stat_init(vd->vdev_child[c]);
3397 mutex_enter(&vd->vdev_stat_lock);
3398 vs->vs_scan_processed = 0;
3399 mutex_exit(&vd->vdev_stat_lock);
3403 vdev_stat_update(zio_t *zio, uint64_t psize)
3405 spa_t *spa = zio->io_spa;
3406 vdev_t *rvd = spa->spa_root_vdev;
3407 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
3409 uint64_t txg = zio->io_txg;
3410 vdev_stat_t *vs = &vd->vdev_stat;
3411 zio_type_t type = zio->io_type;
3412 int flags = zio->io_flags;
3415 * If this i/o is a gang leader, it didn't do any actual work.
3417 if (zio->io_gang_tree)
3420 if (zio->io_error == 0) {
3422 * If this is a root i/o, don't count it -- we've already
3423 * counted the top-level vdevs, and vdev_get_stats() will
3424 * aggregate them when asked. This reduces contention on
3425 * the root vdev_stat_lock and implicitly handles blocks
3426 * that compress away to holes, for which there is no i/o.
3427 * (Holes never create vdev children, so all the counters
3428 * remain zero, which is what we want.)
3430 * Note: this only applies to successful i/o (io_error == 0)
3431 * because unlike i/o counts, errors are not additive.
3432 * When reading a ditto block, for example, failure of
3433 * one top-level vdev does not imply a root-level error.
3438 ASSERT(vd == zio->io_vd);
3440 if (flags & ZIO_FLAG_IO_BYPASS)
3443 mutex_enter(&vd->vdev_stat_lock);
3445 if (flags & ZIO_FLAG_IO_REPAIR) {
3446 if (flags & ZIO_FLAG_SCAN_THREAD) {
3447 dsl_scan_phys_t *scn_phys =
3448 &spa->spa_dsl_pool->dp_scan->scn_phys;
3449 uint64_t *processed = &scn_phys->scn_processed;
3452 if (vd->vdev_ops->vdev_op_leaf)
3453 atomic_add_64(processed, psize);
3454 vs->vs_scan_processed += psize;
3457 if (flags & ZIO_FLAG_SELF_HEAL)
3458 vs->vs_self_healed += psize;
3462 vs->vs_bytes[type] += psize;
3464 mutex_exit(&vd->vdev_stat_lock);
3468 if (flags & ZIO_FLAG_SPECULATIVE)
3472 * If this is an I/O error that is going to be retried, then ignore the
3473 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
3474 * hard errors, when in reality they can happen for any number of
3475 * innocuous reasons (bus resets, MPxIO link failure, etc).
3477 if (zio->io_error == EIO &&
3478 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
3482 * Intent logs writes won't propagate their error to the root
3483 * I/O so don't mark these types of failures as pool-level
3486 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
3489 mutex_enter(&vd->vdev_stat_lock);
3490 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
3491 if (zio->io_error == ECKSUM)
3492 vs->vs_checksum_errors++;
3494 vs->vs_read_errors++;
3496 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
3497 vs->vs_write_errors++;
3498 mutex_exit(&vd->vdev_stat_lock);
3500 if (spa->spa_load_state == SPA_LOAD_NONE &&
3501 type == ZIO_TYPE_WRITE && txg != 0 &&
3502 (!(flags & ZIO_FLAG_IO_REPAIR) ||
3503 (flags & ZIO_FLAG_SCAN_THREAD) ||
3504 spa->spa_claiming)) {
3506 * This is either a normal write (not a repair), or it's
3507 * a repair induced by the scrub thread, or it's a repair
3508 * made by zil_claim() during spa_load() in the first txg.
3509 * In the normal case, we commit the DTL change in the same
3510 * txg as the block was born. In the scrub-induced repair
3511 * case, we know that scrubs run in first-pass syncing context,
3512 * so we commit the DTL change in spa_syncing_txg(spa).
3513 * In the zil_claim() case, we commit in spa_first_txg(spa).
3515 * We currently do not make DTL entries for failed spontaneous
3516 * self-healing writes triggered by normal (non-scrubbing)
3517 * reads, because we have no transactional context in which to
3518 * do so -- and it's not clear that it'd be desirable anyway.
3520 if (vd->vdev_ops->vdev_op_leaf) {
3521 uint64_t commit_txg = txg;
3522 if (flags & ZIO_FLAG_SCAN_THREAD) {
3523 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3524 ASSERT(spa_sync_pass(spa) == 1);
3525 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
3526 commit_txg = spa_syncing_txg(spa);
3527 } else if (spa->spa_claiming) {
3528 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3529 commit_txg = spa_first_txg(spa);
3531 ASSERT(commit_txg >= spa_syncing_txg(spa));
3532 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
3534 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3535 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
3536 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
3539 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
3544 * Update the in-core space usage stats for this vdev, its metaslab class,
3545 * and the root vdev.
3548 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
3549 int64_t space_delta)
3551 int64_t dspace_delta = space_delta;
3552 spa_t *spa = vd->vdev_spa;
3553 vdev_t *rvd = spa->spa_root_vdev;
3554 metaslab_group_t *mg = vd->vdev_mg;
3555 metaslab_class_t *mc = mg ? mg->mg_class : NULL;
3557 ASSERT(vd == vd->vdev_top);
3560 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
3561 * factor. We must calculate this here and not at the root vdev
3562 * because the root vdev's psize-to-asize is simply the max of its
3563 * childrens', thus not accurate enough for us.
3565 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
3566 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
3567 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
3568 vd->vdev_deflate_ratio;
3570 mutex_enter(&vd->vdev_stat_lock);
3571 vd->vdev_stat.vs_alloc += alloc_delta;
3572 vd->vdev_stat.vs_space += space_delta;
3573 vd->vdev_stat.vs_dspace += dspace_delta;
3574 mutex_exit(&vd->vdev_stat_lock);
3576 if (mc == spa_normal_class(spa)) {
3577 mutex_enter(&rvd->vdev_stat_lock);
3578 rvd->vdev_stat.vs_alloc += alloc_delta;
3579 rvd->vdev_stat.vs_space += space_delta;
3580 rvd->vdev_stat.vs_dspace += dspace_delta;
3581 mutex_exit(&rvd->vdev_stat_lock);
3585 ASSERT(rvd == vd->vdev_parent);
3586 ASSERT(vd->vdev_ms_count != 0);
3588 metaslab_class_space_update(mc,
3589 alloc_delta, defer_delta, space_delta, dspace_delta);
3594 * Mark a top-level vdev's config as dirty, placing it on the dirty list
3595 * so that it will be written out next time the vdev configuration is synced.
3596 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3599 vdev_config_dirty(vdev_t *vd)
3601 spa_t *spa = vd->vdev_spa;
3602 vdev_t *rvd = spa->spa_root_vdev;
3605 ASSERT(spa_writeable(spa));
3608 * If this is an aux vdev (as with l2cache and spare devices), then we
3609 * update the vdev config manually and set the sync flag.
3611 if (vd->vdev_aux != NULL) {
3612 spa_aux_vdev_t *sav = vd->vdev_aux;
3616 for (c = 0; c < sav->sav_count; c++) {
3617 if (sav->sav_vdevs[c] == vd)
3621 if (c == sav->sav_count) {
3623 * We're being removed. There's nothing more to do.
3625 ASSERT(sav->sav_sync == B_TRUE);
3629 sav->sav_sync = B_TRUE;
3631 if (nvlist_lookup_nvlist_array(sav->sav_config,
3632 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
3633 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
3634 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
3640 * Setting the nvlist in the middle if the array is a little
3641 * sketchy, but it will work.
3643 nvlist_free(aux[c]);
3644 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
3650 * The dirty list is protected by the SCL_CONFIG lock. The caller
3651 * must either hold SCL_CONFIG as writer, or must be the sync thread
3652 * (which holds SCL_CONFIG as reader). There's only one sync thread,
3653 * so this is sufficient to ensure mutual exclusion.
3655 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3656 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3657 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3660 for (c = 0; c < rvd->vdev_children; c++)
3661 vdev_config_dirty(rvd->vdev_child[c]);
3663 ASSERT(vd == vd->vdev_top);
3665 if (!list_link_active(&vd->vdev_config_dirty_node) &&
3666 vdev_is_concrete(vd)) {
3667 list_insert_head(&spa->spa_config_dirty_list, vd);
3673 vdev_config_clean(vdev_t *vd)
3675 spa_t *spa = vd->vdev_spa;
3677 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3678 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3679 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3681 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3682 list_remove(&spa->spa_config_dirty_list, vd);
3686 * Mark a top-level vdev's state as dirty, so that the next pass of
3687 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3688 * the state changes from larger config changes because they require
3689 * much less locking, and are often needed for administrative actions.
3692 vdev_state_dirty(vdev_t *vd)
3694 spa_t *spa = vd->vdev_spa;
3696 ASSERT(spa_writeable(spa));
3697 ASSERT(vd == vd->vdev_top);
3700 * The state list is protected by the SCL_STATE lock. The caller
3701 * must either hold SCL_STATE as writer, or must be the sync thread
3702 * (which holds SCL_STATE as reader). There's only one sync thread,
3703 * so this is sufficient to ensure mutual exclusion.
3705 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3706 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3707 spa_config_held(spa, SCL_STATE, RW_READER)));
3709 if (!list_link_active(&vd->vdev_state_dirty_node) &&
3710 vdev_is_concrete(vd))
3711 list_insert_head(&spa->spa_state_dirty_list, vd);
3715 vdev_state_clean(vdev_t *vd)
3717 spa_t *spa = vd->vdev_spa;
3719 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3720 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3721 spa_config_held(spa, SCL_STATE, RW_READER)));
3723 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3724 list_remove(&spa->spa_state_dirty_list, vd);
3728 * Propagate vdev state up from children to parent.
3731 vdev_propagate_state(vdev_t *vd)
3733 spa_t *spa = vd->vdev_spa;
3734 vdev_t *rvd = spa->spa_root_vdev;
3735 int degraded = 0, faulted = 0;
3739 if (vd->vdev_children > 0) {
3740 for (int c = 0; c < vd->vdev_children; c++) {
3741 child = vd->vdev_child[c];
3744 * Don't factor holes or indirect vdevs into the
3747 if (!vdev_is_concrete(child))
3750 if (!vdev_readable(child) ||
3751 (!vdev_writeable(child) && spa_writeable(spa))) {
3753 * Root special: if there is a top-level log
3754 * device, treat the root vdev as if it were
3757 if (child->vdev_islog && vd == rvd)
3761 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
3765 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
3769 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
3772 * Root special: if there is a top-level vdev that cannot be
3773 * opened due to corrupted metadata, then propagate the root
3774 * vdev's aux state as 'corrupt' rather than 'insufficient
3777 if (corrupted && vd == rvd &&
3778 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
3779 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
3780 VDEV_AUX_CORRUPT_DATA);
3783 if (vd->vdev_parent)
3784 vdev_propagate_state(vd->vdev_parent);
3788 * Set a vdev's state. If this is during an open, we don't update the parent
3789 * state, because we're in the process of opening children depth-first.
3790 * Otherwise, we propagate the change to the parent.
3792 * If this routine places a device in a faulted state, an appropriate ereport is
3796 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3798 uint64_t save_state;
3799 spa_t *spa = vd->vdev_spa;
3801 if (state == vd->vdev_state) {
3802 vd->vdev_stat.vs_aux = aux;
3806 save_state = vd->vdev_state;
3808 vd->vdev_state = state;
3809 vd->vdev_stat.vs_aux = aux;
3812 * If we are setting the vdev state to anything but an open state, then
3813 * always close the underlying device unless the device has requested
3814 * a delayed close (i.e. we're about to remove or fault the device).
3815 * Otherwise, we keep accessible but invalid devices open forever.
3816 * We don't call vdev_close() itself, because that implies some extra
3817 * checks (offline, etc) that we don't want here. This is limited to
3818 * leaf devices, because otherwise closing the device will affect other
3821 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
3822 vd->vdev_ops->vdev_op_leaf)
3823 vd->vdev_ops->vdev_op_close(vd);
3825 if (vd->vdev_removed &&
3826 state == VDEV_STATE_CANT_OPEN &&
3827 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
3829 * If the previous state is set to VDEV_STATE_REMOVED, then this
3830 * device was previously marked removed and someone attempted to
3831 * reopen it. If this failed due to a nonexistent device, then
3832 * keep the device in the REMOVED state. We also let this be if
3833 * it is one of our special test online cases, which is only
3834 * attempting to online the device and shouldn't generate an FMA
3837 vd->vdev_state = VDEV_STATE_REMOVED;
3838 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
3839 } else if (state == VDEV_STATE_REMOVED) {
3840 vd->vdev_removed = B_TRUE;
3841 } else if (state == VDEV_STATE_CANT_OPEN) {
3843 * If we fail to open a vdev during an import or recovery, we
3844 * mark it as "not available", which signifies that it was
3845 * never there to begin with. Failure to open such a device
3846 * is not considered an error.
3848 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3849 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3850 vd->vdev_ops->vdev_op_leaf)
3851 vd->vdev_not_present = 1;
3854 * Post the appropriate ereport. If the 'prevstate' field is
3855 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3856 * that this is part of a vdev_reopen(). In this case, we don't
3857 * want to post the ereport if the device was already in the
3858 * CANT_OPEN state beforehand.
3860 * If the 'checkremove' flag is set, then this is an attempt to
3861 * online the device in response to an insertion event. If we
3862 * hit this case, then we have detected an insertion event for a
3863 * faulted or offline device that wasn't in the removed state.
3864 * In this scenario, we don't post an ereport because we are
3865 * about to replace the device, or attempt an online with
3866 * vdev_forcefault, which will generate the fault for us.
3868 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
3869 !vd->vdev_not_present && !vd->vdev_checkremove &&
3870 vd != spa->spa_root_vdev) {
3874 case VDEV_AUX_OPEN_FAILED:
3875 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
3877 case VDEV_AUX_CORRUPT_DATA:
3878 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
3880 case VDEV_AUX_NO_REPLICAS:
3881 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
3883 case VDEV_AUX_BAD_GUID_SUM:
3884 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
3886 case VDEV_AUX_TOO_SMALL:
3887 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3889 case VDEV_AUX_BAD_LABEL:
3890 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3893 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3896 zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3899 /* Erase any notion of persistent removed state */
3900 vd->vdev_removed = B_FALSE;
3902 vd->vdev_removed = B_FALSE;
3906 * Notify the fmd of the state change. Be verbose and post
3907 * notifications even for stuff that's not important; the fmd agent can
3908 * sort it out. Don't emit state change events for non-leaf vdevs since
3909 * they can't change state on their own. The FMD can check their state
3910 * if it wants to when it sees that a leaf vdev had a state change.
3912 if (vd->vdev_ops->vdev_op_leaf)
3913 zfs_post_state_change(spa, vd);
3915 if (!isopen && vd->vdev_parent)
3916 vdev_propagate_state(vd->vdev_parent);
3920 vdev_children_are_offline(vdev_t *vd)
3922 ASSERT(!vd->vdev_ops->vdev_op_leaf);
3924 for (uint64_t i = 0; i < vd->vdev_children; i++) {
3925 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
3933 * Check the vdev configuration to ensure that it's capable of supporting
3934 * a root pool. We do not support partial configuration.
3935 * In addition, only a single top-level vdev is allowed.
3937 * FreeBSD does not have above limitations.
3940 vdev_is_bootable(vdev_t *vd)
3943 if (!vd->vdev_ops->vdev_op_leaf) {
3944 char *vdev_type = vd->vdev_ops->vdev_op_type;
3946 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
3947 vd->vdev_children > 1) {
3949 } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 ||
3950 strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) {
3955 for (int c = 0; c < vd->vdev_children; c++) {
3956 if (!vdev_is_bootable(vd->vdev_child[c]))
3959 #endif /* illumos */
3964 vdev_is_concrete(vdev_t *vd)
3966 vdev_ops_t *ops = vd->vdev_ops;
3967 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
3968 ops == &vdev_missing_ops || ops == &vdev_root_ops) {
3976 * Determine if a log device has valid content. If the vdev was
3977 * removed or faulted in the MOS config then we know that
3978 * the content on the log device has already been written to the pool.
3981 vdev_log_state_valid(vdev_t *vd)
3983 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
3987 for (int c = 0; c < vd->vdev_children; c++)
3988 if (vdev_log_state_valid(vd->vdev_child[c]))
3995 * Expand a vdev if possible.
3998 vdev_expand(vdev_t *vd, uint64_t txg)
4000 ASSERT(vd->vdev_top == vd);
4001 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4003 vdev_set_deflate_ratio(vd);
4005 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
4006 vdev_is_concrete(vd)) {
4007 VERIFY(vdev_metaslab_init(vd, txg) == 0);
4008 vdev_config_dirty(vd);
4016 vdev_split(vdev_t *vd)
4018 vdev_t *cvd, *pvd = vd->vdev_parent;
4020 vdev_remove_child(pvd, vd);
4021 vdev_compact_children(pvd);
4023 cvd = pvd->vdev_child[0];
4024 if (pvd->vdev_children == 1) {
4025 vdev_remove_parent(cvd);
4026 cvd->vdev_splitting = B_TRUE;
4028 vdev_propagate_state(cvd);
4032 vdev_deadman(vdev_t *vd)
4034 for (int c = 0; c < vd->vdev_children; c++) {
4035 vdev_t *cvd = vd->vdev_child[c];
4040 if (vd->vdev_ops->vdev_op_leaf) {
4041 vdev_queue_t *vq = &vd->vdev_queue;
4043 mutex_enter(&vq->vq_lock);
4044 if (avl_numnodes(&vq->vq_active_tree) > 0) {
4045 spa_t *spa = vd->vdev_spa;
4050 * Look at the head of all the pending queues,
4051 * if any I/O has been outstanding for longer than
4052 * the spa_deadman_synctime we panic the system.
4054 fio = avl_first(&vq->vq_active_tree);
4055 delta = gethrtime() - fio->io_timestamp;
4056 if (delta > spa_deadman_synctime(spa)) {
4057 vdev_dbgmsg(vd, "SLOW IO: zio timestamp "
4058 "%lluns, delta %lluns, last io %lluns",
4059 fio->io_timestamp, (u_longlong_t)delta,
4060 vq->vq_io_complete_ts);
4061 fm_panic("I/O to pool '%s' appears to be "
4062 "hung on vdev guid %llu at '%s'.",
4064 (long long unsigned int) vd->vdev_guid,
4068 mutex_exit(&vq->vq_lock);