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 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 by Delphix. All rights reserved.
26 * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
29 #include <sys/zfs_context.h>
30 #include <sys/fm/fs/zfs.h>
32 #include <sys/spa_impl.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/vdev_impl.h>
36 #include <sys/uberblock_impl.h>
37 #include <sys/metaslab.h>
38 #include <sys/metaslab_impl.h>
39 #include <sys/space_map.h>
40 #include <sys/space_reftree.h>
43 #include <sys/fs/zfs.h>
46 #include <sys/dsl_scan.h>
47 #include <sys/trim_map.h>
49 SYSCTL_DECL(_vfs_zfs);
50 SYSCTL_NODE(_vfs_zfs, OID_AUTO, vdev, CTLFLAG_RW, 0, "ZFS VDEV");
53 * Virtual device management.
57 * The limit for ZFS to automatically increase a top-level vdev's ashift
58 * from logical ashift to physical ashift.
60 * Example: one or more 512B emulation child vdevs
61 * child->vdev_ashift = 9 (512 bytes)
62 * child->vdev_physical_ashift = 12 (4096 bytes)
63 * zfs_max_auto_ashift = 11 (2048 bytes)
65 * On pool creation or the addition of a new top-leve vdev, ZFS will
66 * bump the ashift of the top-level vdev to 2048.
68 * Example: one or more 512B emulation child vdevs
69 * child->vdev_ashift = 9 (512 bytes)
70 * child->vdev_physical_ashift = 12 (4096 bytes)
71 * zfs_max_auto_ashift = 13 (8192 bytes)
73 * On pool creation or the addition of a new top-leve vdev, ZFS will
74 * bump the ashift of the top-level vdev to 4096.
76 static uint64_t zfs_max_auto_ashift = SPA_MAXASHIFT;
79 sysctl_vfs_zfs_max_auto_ashift(SYSCTL_HANDLER_ARGS)
84 val = zfs_max_auto_ashift;
85 err = sysctl_handle_64(oidp, &val, 0, req);
86 if (err != 0 || req->newptr == NULL)
89 if (val > SPA_MAXASHIFT)
92 zfs_max_auto_ashift = val;
96 SYSCTL_PROC(_vfs_zfs, OID_AUTO, max_auto_ashift,
97 CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t),
98 sysctl_vfs_zfs_max_auto_ashift, "QU",
99 "Cap on logical -> physical ashift adjustment on new top-level vdevs.");
101 static vdev_ops_t *vdev_ops_table[] = {
120 * Given a vdev type, return the appropriate ops vector.
123 vdev_getops(const char *type)
125 vdev_ops_t *ops, **opspp;
127 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
128 if (strcmp(ops->vdev_op_type, type) == 0)
135 * Default asize function: return the MAX of psize with the asize of
136 * all children. This is what's used by anything other than RAID-Z.
139 vdev_default_asize(vdev_t *vd, uint64_t psize)
141 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
144 for (int c = 0; c < vd->vdev_children; c++) {
145 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
146 asize = MAX(asize, csize);
153 * Get the minimum allocatable size. We define the allocatable size as
154 * the vdev's asize rounded to the nearest metaslab. This allows us to
155 * replace or attach devices which don't have the same physical size but
156 * can still satisfy the same number of allocations.
159 vdev_get_min_asize(vdev_t *vd)
161 vdev_t *pvd = vd->vdev_parent;
164 * If our parent is NULL (inactive spare or cache) or is the root,
165 * just return our own asize.
168 return (vd->vdev_asize);
171 * The top-level vdev just returns the allocatable size rounded
172 * to the nearest metaslab.
174 if (vd == vd->vdev_top)
175 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
178 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
179 * so each child must provide at least 1/Nth of its asize.
181 if (pvd->vdev_ops == &vdev_raidz_ops)
182 return (pvd->vdev_min_asize / pvd->vdev_children);
184 return (pvd->vdev_min_asize);
188 vdev_set_min_asize(vdev_t *vd)
190 vd->vdev_min_asize = vdev_get_min_asize(vd);
192 for (int c = 0; c < vd->vdev_children; c++)
193 vdev_set_min_asize(vd->vdev_child[c]);
197 vdev_lookup_top(spa_t *spa, uint64_t vdev)
199 vdev_t *rvd = spa->spa_root_vdev;
201 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
203 if (vdev < rvd->vdev_children) {
204 ASSERT(rvd->vdev_child[vdev] != NULL);
205 return (rvd->vdev_child[vdev]);
212 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
216 if (vd->vdev_guid == guid)
219 for (int c = 0; c < vd->vdev_children; c++)
220 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
228 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
230 size_t oldsize, newsize;
231 uint64_t id = cvd->vdev_id;
234 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
235 ASSERT(cvd->vdev_parent == NULL);
237 cvd->vdev_parent = pvd;
242 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
244 oldsize = pvd->vdev_children * sizeof (vdev_t *);
245 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
246 newsize = pvd->vdev_children * sizeof (vdev_t *);
248 newchild = kmem_zalloc(newsize, KM_SLEEP);
249 if (pvd->vdev_child != NULL) {
250 bcopy(pvd->vdev_child, newchild, oldsize);
251 kmem_free(pvd->vdev_child, oldsize);
254 pvd->vdev_child = newchild;
255 pvd->vdev_child[id] = cvd;
257 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
258 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
261 * Walk up all ancestors to update guid sum.
263 for (; pvd != NULL; pvd = pvd->vdev_parent)
264 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
268 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
271 uint_t id = cvd->vdev_id;
273 ASSERT(cvd->vdev_parent == pvd);
278 ASSERT(id < pvd->vdev_children);
279 ASSERT(pvd->vdev_child[id] == cvd);
281 pvd->vdev_child[id] = NULL;
282 cvd->vdev_parent = NULL;
284 for (c = 0; c < pvd->vdev_children; c++)
285 if (pvd->vdev_child[c])
288 if (c == pvd->vdev_children) {
289 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
290 pvd->vdev_child = NULL;
291 pvd->vdev_children = 0;
295 * Walk up all ancestors to update guid sum.
297 for (; pvd != NULL; pvd = pvd->vdev_parent)
298 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
302 * Remove any holes in the child array.
305 vdev_compact_children(vdev_t *pvd)
307 vdev_t **newchild, *cvd;
308 int oldc = pvd->vdev_children;
311 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
313 for (int c = newc = 0; c < oldc; c++)
314 if (pvd->vdev_child[c])
317 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
319 for (int c = newc = 0; c < oldc; c++) {
320 if ((cvd = pvd->vdev_child[c]) != NULL) {
321 newchild[newc] = cvd;
322 cvd->vdev_id = newc++;
326 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
327 pvd->vdev_child = newchild;
328 pvd->vdev_children = newc;
332 * Allocate and minimally initialize a vdev_t.
335 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
339 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
341 if (spa->spa_root_vdev == NULL) {
342 ASSERT(ops == &vdev_root_ops);
343 spa->spa_root_vdev = vd;
344 spa->spa_load_guid = spa_generate_guid(NULL);
347 if (guid == 0 && ops != &vdev_hole_ops) {
348 if (spa->spa_root_vdev == vd) {
350 * The root vdev's guid will also be the pool guid,
351 * which must be unique among all pools.
353 guid = spa_generate_guid(NULL);
356 * Any other vdev's guid must be unique within the pool.
358 guid = spa_generate_guid(spa);
360 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
365 vd->vdev_guid = guid;
366 vd->vdev_guid_sum = guid;
368 vd->vdev_state = VDEV_STATE_CLOSED;
369 vd->vdev_ishole = (ops == &vdev_hole_ops);
371 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
372 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
373 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
374 for (int t = 0; t < DTL_TYPES; t++) {
375 vd->vdev_dtl[t] = range_tree_create(NULL, NULL,
378 txg_list_create(&vd->vdev_ms_list,
379 offsetof(struct metaslab, ms_txg_node));
380 txg_list_create(&vd->vdev_dtl_list,
381 offsetof(struct vdev, vdev_dtl_node));
382 vd->vdev_stat.vs_timestamp = gethrtime();
390 * Allocate a new vdev. The 'alloctype' is used to control whether we are
391 * creating a new vdev or loading an existing one - the behavior is slightly
392 * different for each case.
395 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
400 uint64_t guid = 0, islog, nparity;
403 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
405 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
406 return (SET_ERROR(EINVAL));
408 if ((ops = vdev_getops(type)) == NULL)
409 return (SET_ERROR(EINVAL));
412 * If this is a load, get the vdev guid from the nvlist.
413 * Otherwise, vdev_alloc_common() will generate one for us.
415 if (alloctype == VDEV_ALLOC_LOAD) {
418 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
420 return (SET_ERROR(EINVAL));
422 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
423 return (SET_ERROR(EINVAL));
424 } else if (alloctype == VDEV_ALLOC_SPARE) {
425 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
426 return (SET_ERROR(EINVAL));
427 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
428 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
429 return (SET_ERROR(EINVAL));
430 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
431 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
432 return (SET_ERROR(EINVAL));
436 * The first allocated vdev must be of type 'root'.
438 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
439 return (SET_ERROR(EINVAL));
442 * Determine whether we're a log vdev.
445 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
446 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
447 return (SET_ERROR(ENOTSUP));
449 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
450 return (SET_ERROR(ENOTSUP));
453 * Set the nparity property for RAID-Z vdevs.
456 if (ops == &vdev_raidz_ops) {
457 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
459 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
460 return (SET_ERROR(EINVAL));
462 * Previous versions could only support 1 or 2 parity
466 spa_version(spa) < SPA_VERSION_RAIDZ2)
467 return (SET_ERROR(ENOTSUP));
469 spa_version(spa) < SPA_VERSION_RAIDZ3)
470 return (SET_ERROR(ENOTSUP));
473 * We require the parity to be specified for SPAs that
474 * support multiple parity levels.
476 if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
477 return (SET_ERROR(EINVAL));
479 * Otherwise, we default to 1 parity device for RAID-Z.
486 ASSERT(nparity != -1ULL);
488 vd = vdev_alloc_common(spa, id, guid, ops);
490 vd->vdev_islog = islog;
491 vd->vdev_nparity = nparity;
493 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
494 vd->vdev_path = spa_strdup(vd->vdev_path);
495 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
496 vd->vdev_devid = spa_strdup(vd->vdev_devid);
497 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
498 &vd->vdev_physpath) == 0)
499 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
500 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
501 vd->vdev_fru = spa_strdup(vd->vdev_fru);
504 * Set the whole_disk property. If it's not specified, leave the value
507 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
508 &vd->vdev_wholedisk) != 0)
509 vd->vdev_wholedisk = -1ULL;
512 * Look for the 'not present' flag. This will only be set if the device
513 * was not present at the time of import.
515 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
516 &vd->vdev_not_present);
519 * Get the alignment requirement.
521 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
524 * Retrieve the vdev creation time.
526 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
530 * If we're a top-level vdev, try to load the allocation parameters.
532 if (parent && !parent->vdev_parent &&
533 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
534 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
536 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
538 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
540 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
544 if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
545 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
546 alloctype == VDEV_ALLOC_ADD ||
547 alloctype == VDEV_ALLOC_SPLIT ||
548 alloctype == VDEV_ALLOC_ROOTPOOL);
549 vd->vdev_mg = metaslab_group_create(islog ?
550 spa_log_class(spa) : spa_normal_class(spa), vd);
554 * If we're a leaf vdev, try to load the DTL object and other state.
556 if (vd->vdev_ops->vdev_op_leaf &&
557 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
558 alloctype == VDEV_ALLOC_ROOTPOOL)) {
559 if (alloctype == VDEV_ALLOC_LOAD) {
560 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
561 &vd->vdev_dtl_object);
562 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
566 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
569 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
570 &spare) == 0 && spare)
574 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
577 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
578 &vd->vdev_resilver_txg);
581 * When importing a pool, we want to ignore the persistent fault
582 * state, as the diagnosis made on another system may not be
583 * valid in the current context. Local vdevs will
584 * remain in the faulted state.
586 if (spa_load_state(spa) == SPA_LOAD_OPEN) {
587 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
589 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
591 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
594 if (vd->vdev_faulted || vd->vdev_degraded) {
598 VDEV_AUX_ERR_EXCEEDED;
599 if (nvlist_lookup_string(nv,
600 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
601 strcmp(aux, "external") == 0)
602 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
608 * Add ourselves to the parent's list of children.
610 vdev_add_child(parent, vd);
618 vdev_free(vdev_t *vd)
620 spa_t *spa = vd->vdev_spa;
623 * vdev_free() implies closing the vdev first. This is simpler than
624 * trying to ensure complicated semantics for all callers.
628 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
629 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
634 for (int c = 0; c < vd->vdev_children; c++)
635 vdev_free(vd->vdev_child[c]);
637 ASSERT(vd->vdev_child == NULL);
638 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
641 * Discard allocation state.
643 if (vd->vdev_mg != NULL) {
644 vdev_metaslab_fini(vd);
645 metaslab_group_destroy(vd->vdev_mg);
648 ASSERT0(vd->vdev_stat.vs_space);
649 ASSERT0(vd->vdev_stat.vs_dspace);
650 ASSERT0(vd->vdev_stat.vs_alloc);
653 * Remove this vdev from its parent's child list.
655 vdev_remove_child(vd->vdev_parent, vd);
657 ASSERT(vd->vdev_parent == NULL);
660 * Clean up vdev structure.
666 spa_strfree(vd->vdev_path);
668 spa_strfree(vd->vdev_devid);
669 if (vd->vdev_physpath)
670 spa_strfree(vd->vdev_physpath);
672 spa_strfree(vd->vdev_fru);
674 if (vd->vdev_isspare)
675 spa_spare_remove(vd);
676 if (vd->vdev_isl2cache)
677 spa_l2cache_remove(vd);
679 txg_list_destroy(&vd->vdev_ms_list);
680 txg_list_destroy(&vd->vdev_dtl_list);
682 mutex_enter(&vd->vdev_dtl_lock);
683 space_map_close(vd->vdev_dtl_sm);
684 for (int t = 0; t < DTL_TYPES; t++) {
685 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
686 range_tree_destroy(vd->vdev_dtl[t]);
688 mutex_exit(&vd->vdev_dtl_lock);
690 mutex_destroy(&vd->vdev_dtl_lock);
691 mutex_destroy(&vd->vdev_stat_lock);
692 mutex_destroy(&vd->vdev_probe_lock);
694 if (vd == spa->spa_root_vdev)
695 spa->spa_root_vdev = NULL;
697 kmem_free(vd, sizeof (vdev_t));
701 * Transfer top-level vdev state from svd to tvd.
704 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
706 spa_t *spa = svd->vdev_spa;
711 ASSERT(tvd == tvd->vdev_top);
713 tvd->vdev_ms_array = svd->vdev_ms_array;
714 tvd->vdev_ms_shift = svd->vdev_ms_shift;
715 tvd->vdev_ms_count = svd->vdev_ms_count;
717 svd->vdev_ms_array = 0;
718 svd->vdev_ms_shift = 0;
719 svd->vdev_ms_count = 0;
722 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
723 tvd->vdev_mg = svd->vdev_mg;
724 tvd->vdev_ms = svd->vdev_ms;
729 if (tvd->vdev_mg != NULL)
730 tvd->vdev_mg->mg_vd = tvd;
732 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
733 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
734 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
736 svd->vdev_stat.vs_alloc = 0;
737 svd->vdev_stat.vs_space = 0;
738 svd->vdev_stat.vs_dspace = 0;
740 for (t = 0; t < TXG_SIZE; t++) {
741 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
742 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
743 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
744 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
745 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
746 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
749 if (list_link_active(&svd->vdev_config_dirty_node)) {
750 vdev_config_clean(svd);
751 vdev_config_dirty(tvd);
754 if (list_link_active(&svd->vdev_state_dirty_node)) {
755 vdev_state_clean(svd);
756 vdev_state_dirty(tvd);
759 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
760 svd->vdev_deflate_ratio = 0;
762 tvd->vdev_islog = svd->vdev_islog;
767 vdev_top_update(vdev_t *tvd, vdev_t *vd)
774 for (int c = 0; c < vd->vdev_children; c++)
775 vdev_top_update(tvd, vd->vdev_child[c]);
779 * Add a mirror/replacing vdev above an existing vdev.
782 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
784 spa_t *spa = cvd->vdev_spa;
785 vdev_t *pvd = cvd->vdev_parent;
788 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
790 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
792 mvd->vdev_asize = cvd->vdev_asize;
793 mvd->vdev_min_asize = cvd->vdev_min_asize;
794 mvd->vdev_max_asize = cvd->vdev_max_asize;
795 mvd->vdev_ashift = cvd->vdev_ashift;
796 mvd->vdev_logical_ashift = cvd->vdev_logical_ashift;
797 mvd->vdev_physical_ashift = cvd->vdev_physical_ashift;
798 mvd->vdev_state = cvd->vdev_state;
799 mvd->vdev_crtxg = cvd->vdev_crtxg;
801 vdev_remove_child(pvd, cvd);
802 vdev_add_child(pvd, mvd);
803 cvd->vdev_id = mvd->vdev_children;
804 vdev_add_child(mvd, cvd);
805 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
807 if (mvd == mvd->vdev_top)
808 vdev_top_transfer(cvd, mvd);
814 * Remove a 1-way mirror/replacing vdev from the tree.
817 vdev_remove_parent(vdev_t *cvd)
819 vdev_t *mvd = cvd->vdev_parent;
820 vdev_t *pvd = mvd->vdev_parent;
822 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
824 ASSERT(mvd->vdev_children == 1);
825 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
826 mvd->vdev_ops == &vdev_replacing_ops ||
827 mvd->vdev_ops == &vdev_spare_ops);
828 cvd->vdev_ashift = mvd->vdev_ashift;
829 cvd->vdev_logical_ashift = mvd->vdev_logical_ashift;
830 cvd->vdev_physical_ashift = mvd->vdev_physical_ashift;
832 vdev_remove_child(mvd, cvd);
833 vdev_remove_child(pvd, mvd);
836 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
837 * Otherwise, we could have detached an offline device, and when we
838 * go to import the pool we'll think we have two top-level vdevs,
839 * instead of a different version of the same top-level vdev.
841 if (mvd->vdev_top == mvd) {
842 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
843 cvd->vdev_orig_guid = cvd->vdev_guid;
844 cvd->vdev_guid += guid_delta;
845 cvd->vdev_guid_sum += guid_delta;
847 cvd->vdev_id = mvd->vdev_id;
848 vdev_add_child(pvd, cvd);
849 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
851 if (cvd == cvd->vdev_top)
852 vdev_top_transfer(mvd, cvd);
854 ASSERT(mvd->vdev_children == 0);
859 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
861 spa_t *spa = vd->vdev_spa;
862 objset_t *mos = spa->spa_meta_objset;
864 uint64_t oldc = vd->vdev_ms_count;
865 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
869 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
872 * This vdev is not being allocated from yet or is a hole.
874 if (vd->vdev_ms_shift == 0)
877 ASSERT(!vd->vdev_ishole);
880 * Compute the raidz-deflation ratio. Note, we hard-code
881 * in 128k (1 << 17) because it is the current "typical" blocksize.
882 * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change,
883 * or we will inconsistently account for existing bp's.
885 vd->vdev_deflate_ratio = (1 << 17) /
886 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
888 ASSERT(oldc <= newc);
890 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
893 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
894 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
898 vd->vdev_ms_count = newc;
900 for (m = oldc; m < newc; m++) {
904 error = dmu_read(mos, vd->vdev_ms_array,
905 m * sizeof (uint64_t), sizeof (uint64_t), &object,
910 vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, m, object, txg);
914 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
917 * If the vdev is being removed we don't activate
918 * the metaslabs since we want to ensure that no new
919 * allocations are performed on this device.
921 if (oldc == 0 && !vd->vdev_removing)
922 metaslab_group_activate(vd->vdev_mg);
925 spa_config_exit(spa, SCL_ALLOC, FTAG);
931 vdev_metaslab_fini(vdev_t *vd)
934 uint64_t count = vd->vdev_ms_count;
936 if (vd->vdev_ms != NULL) {
937 metaslab_group_passivate(vd->vdev_mg);
938 for (m = 0; m < count; m++) {
939 metaslab_t *msp = vd->vdev_ms[m];
944 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
949 typedef struct vdev_probe_stats {
950 boolean_t vps_readable;
951 boolean_t vps_writeable;
953 } vdev_probe_stats_t;
956 vdev_probe_done(zio_t *zio)
958 spa_t *spa = zio->io_spa;
959 vdev_t *vd = zio->io_vd;
960 vdev_probe_stats_t *vps = zio->io_private;
962 ASSERT(vd->vdev_probe_zio != NULL);
964 if (zio->io_type == ZIO_TYPE_READ) {
965 if (zio->io_error == 0)
966 vps->vps_readable = 1;
967 if (zio->io_error == 0 && spa_writeable(spa)) {
968 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
969 zio->io_offset, zio->io_size, zio->io_data,
970 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
971 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
973 zio_buf_free(zio->io_data, zio->io_size);
975 } else if (zio->io_type == ZIO_TYPE_WRITE) {
976 if (zio->io_error == 0)
977 vps->vps_writeable = 1;
978 zio_buf_free(zio->io_data, zio->io_size);
979 } else if (zio->io_type == ZIO_TYPE_NULL) {
982 vd->vdev_cant_read |= !vps->vps_readable;
983 vd->vdev_cant_write |= !vps->vps_writeable;
985 if (vdev_readable(vd) &&
986 (vdev_writeable(vd) || !spa_writeable(spa))) {
989 ASSERT(zio->io_error != 0);
990 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
991 spa, vd, NULL, 0, 0);
992 zio->io_error = SET_ERROR(ENXIO);
995 mutex_enter(&vd->vdev_probe_lock);
996 ASSERT(vd->vdev_probe_zio == zio);
997 vd->vdev_probe_zio = NULL;
998 mutex_exit(&vd->vdev_probe_lock);
1000 while ((pio = zio_walk_parents(zio)) != NULL)
1001 if (!vdev_accessible(vd, pio))
1002 pio->io_error = SET_ERROR(ENXIO);
1004 kmem_free(vps, sizeof (*vps));
1009 * Determine whether this device is accessible.
1011 * Read and write to several known locations: the pad regions of each
1012 * vdev label but the first, which we leave alone in case it contains
1016 vdev_probe(vdev_t *vd, zio_t *zio)
1018 spa_t *spa = vd->vdev_spa;
1019 vdev_probe_stats_t *vps = NULL;
1022 ASSERT(vd->vdev_ops->vdev_op_leaf);
1025 * Don't probe the probe.
1027 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1031 * To prevent 'probe storms' when a device fails, we create
1032 * just one probe i/o at a time. All zios that want to probe
1033 * this vdev will become parents of the probe io.
1035 mutex_enter(&vd->vdev_probe_lock);
1037 if ((pio = vd->vdev_probe_zio) == NULL) {
1038 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1040 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1041 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
1044 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1046 * vdev_cant_read and vdev_cant_write can only
1047 * transition from TRUE to FALSE when we have the
1048 * SCL_ZIO lock as writer; otherwise they can only
1049 * transition from FALSE to TRUE. This ensures that
1050 * any zio looking at these values can assume that
1051 * failures persist for the life of the I/O. That's
1052 * important because when a device has intermittent
1053 * connectivity problems, we want to ensure that
1054 * they're ascribed to the device (ENXIO) and not
1057 * Since we hold SCL_ZIO as writer here, clear both
1058 * values so the probe can reevaluate from first
1061 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1062 vd->vdev_cant_read = B_FALSE;
1063 vd->vdev_cant_write = B_FALSE;
1066 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1067 vdev_probe_done, vps,
1068 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1071 * We can't change the vdev state in this context, so we
1072 * kick off an async task to do it on our behalf.
1075 vd->vdev_probe_wanted = B_TRUE;
1076 spa_async_request(spa, SPA_ASYNC_PROBE);
1081 zio_add_child(zio, pio);
1083 mutex_exit(&vd->vdev_probe_lock);
1086 ASSERT(zio != NULL);
1090 for (int l = 1; l < VDEV_LABELS; l++) {
1091 zio_nowait(zio_read_phys(pio, vd,
1092 vdev_label_offset(vd->vdev_psize, l,
1093 offsetof(vdev_label_t, vl_pad2)),
1094 VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
1095 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1096 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1107 vdev_open_child(void *arg)
1111 vd->vdev_open_thread = curthread;
1112 vd->vdev_open_error = vdev_open(vd);
1113 vd->vdev_open_thread = NULL;
1117 vdev_uses_zvols(vdev_t *vd)
1119 if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1120 strlen(ZVOL_DIR)) == 0)
1122 for (int c = 0; c < vd->vdev_children; c++)
1123 if (vdev_uses_zvols(vd->vdev_child[c]))
1129 vdev_open_children(vdev_t *vd)
1132 int children = vd->vdev_children;
1135 * in order to handle pools on top of zvols, do the opens
1136 * in a single thread so that the same thread holds the
1137 * spa_namespace_lock
1139 if (B_TRUE || vdev_uses_zvols(vd)) {
1140 for (int c = 0; c < children; c++)
1141 vd->vdev_child[c]->vdev_open_error =
1142 vdev_open(vd->vdev_child[c]);
1145 tq = taskq_create("vdev_open", children, minclsyspri,
1146 children, children, TASKQ_PREPOPULATE);
1148 for (int c = 0; c < children; c++)
1149 VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1156 * Prepare a virtual device for access.
1159 vdev_open(vdev_t *vd)
1161 spa_t *spa = vd->vdev_spa;
1164 uint64_t max_osize = 0;
1165 uint64_t asize, max_asize, psize;
1166 uint64_t logical_ashift = 0;
1167 uint64_t physical_ashift = 0;
1169 ASSERT(vd->vdev_open_thread == curthread ||
1170 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1171 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1172 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1173 vd->vdev_state == VDEV_STATE_OFFLINE);
1175 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1176 vd->vdev_cant_read = B_FALSE;
1177 vd->vdev_cant_write = B_FALSE;
1178 vd->vdev_min_asize = vdev_get_min_asize(vd);
1181 * If this vdev is not removed, check its fault status. If it's
1182 * faulted, bail out of the open.
1184 if (!vd->vdev_removed && vd->vdev_faulted) {
1185 ASSERT(vd->vdev_children == 0);
1186 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1187 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1188 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1189 vd->vdev_label_aux);
1190 return (SET_ERROR(ENXIO));
1191 } else if (vd->vdev_offline) {
1192 ASSERT(vd->vdev_children == 0);
1193 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1194 return (SET_ERROR(ENXIO));
1197 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize,
1198 &logical_ashift, &physical_ashift);
1201 * Reset the vdev_reopening flag so that we actually close
1202 * the vdev on error.
1204 vd->vdev_reopening = B_FALSE;
1205 if (zio_injection_enabled && error == 0)
1206 error = zio_handle_device_injection(vd, NULL, ENXIO);
1209 if (vd->vdev_removed &&
1210 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
1211 vd->vdev_removed = B_FALSE;
1213 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1214 vd->vdev_stat.vs_aux);
1218 vd->vdev_removed = B_FALSE;
1221 * Recheck the faulted flag now that we have confirmed that
1222 * the vdev is accessible. If we're faulted, bail.
1224 if (vd->vdev_faulted) {
1225 ASSERT(vd->vdev_children == 0);
1226 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1227 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1228 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1229 vd->vdev_label_aux);
1230 return (SET_ERROR(ENXIO));
1233 if (vd->vdev_degraded) {
1234 ASSERT(vd->vdev_children == 0);
1235 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1236 VDEV_AUX_ERR_EXCEEDED);
1238 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1242 * For hole or missing vdevs we just return success.
1244 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
1247 if (vd->vdev_ops->vdev_op_leaf) {
1248 vd->vdev_notrim = B_FALSE;
1249 trim_map_create(vd);
1252 for (int c = 0; c < vd->vdev_children; c++) {
1253 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1254 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1260 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1261 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1263 if (vd->vdev_children == 0) {
1264 if (osize < SPA_MINDEVSIZE) {
1265 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1266 VDEV_AUX_TOO_SMALL);
1267 return (SET_ERROR(EOVERFLOW));
1270 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1271 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
1272 VDEV_LABEL_END_SIZE);
1274 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1275 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1276 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1277 VDEV_AUX_TOO_SMALL);
1278 return (SET_ERROR(EOVERFLOW));
1282 max_asize = max_osize;
1285 vd->vdev_psize = psize;
1288 * Make sure the allocatable size hasn't shrunk.
1290 if (asize < vd->vdev_min_asize) {
1291 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1292 VDEV_AUX_BAD_LABEL);
1293 return (SET_ERROR(EINVAL));
1296 vd->vdev_physical_ashift =
1297 MAX(physical_ashift, vd->vdev_physical_ashift);
1298 vd->vdev_logical_ashift = MAX(logical_ashift, vd->vdev_logical_ashift);
1299 vd->vdev_ashift = MAX(vd->vdev_logical_ashift, vd->vdev_ashift);
1301 if (vd->vdev_logical_ashift > SPA_MAXASHIFT) {
1302 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1303 VDEV_AUX_ASHIFT_TOO_BIG);
1307 if (vd->vdev_asize == 0) {
1309 * This is the first-ever open, so use the computed values.
1310 * For testing purposes, a higher ashift can be requested.
1312 vd->vdev_asize = asize;
1313 vd->vdev_max_asize = max_asize;
1316 * Make sure the alignment requirement hasn't increased.
1318 if (vd->vdev_ashift > vd->vdev_top->vdev_ashift &&
1319 vd->vdev_ops->vdev_op_leaf) {
1320 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1321 VDEV_AUX_BAD_LABEL);
1324 vd->vdev_max_asize = max_asize;
1328 * If all children are healthy and the asize has increased,
1329 * then we've experienced dynamic LUN growth. If automatic
1330 * expansion is enabled then use the additional space.
1332 if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
1333 (vd->vdev_expanding || spa->spa_autoexpand))
1334 vd->vdev_asize = asize;
1336 vdev_set_min_asize(vd);
1339 * Ensure we can issue some IO before declaring the
1340 * vdev open for business.
1342 if (vd->vdev_ops->vdev_op_leaf &&
1343 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1344 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1345 VDEV_AUX_ERR_EXCEEDED);
1350 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1351 * resilver. But don't do this if we are doing a reopen for a scrub,
1352 * since this would just restart the scrub we are already doing.
1354 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
1355 vdev_resilver_needed(vd, NULL, NULL))
1356 spa_async_request(spa, SPA_ASYNC_RESILVER);
1362 * Called once the vdevs are all opened, this routine validates the label
1363 * contents. This needs to be done before vdev_load() so that we don't
1364 * inadvertently do repair I/Os to the wrong device.
1366 * If 'strict' is false ignore the spa guid check. This is necessary because
1367 * if the machine crashed during a re-guid the new guid might have been written
1368 * to all of the vdev labels, but not the cached config. The strict check
1369 * will be performed when the pool is opened again using the mos config.
1371 * This function will only return failure if one of the vdevs indicates that it
1372 * has since been destroyed or exported. This is only possible if
1373 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1374 * will be updated but the function will return 0.
1377 vdev_validate(vdev_t *vd, boolean_t strict)
1379 spa_t *spa = vd->vdev_spa;
1381 uint64_t guid = 0, top_guid;
1384 for (int c = 0; c < vd->vdev_children; c++)
1385 if (vdev_validate(vd->vdev_child[c], strict) != 0)
1386 return (SET_ERROR(EBADF));
1389 * If the device has already failed, or was marked offline, don't do
1390 * any further validation. Otherwise, label I/O will fail and we will
1391 * overwrite the previous state.
1393 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1394 uint64_t aux_guid = 0;
1396 uint64_t txg = spa_last_synced_txg(spa) != 0 ?
1397 spa_last_synced_txg(spa) : -1ULL;
1399 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1400 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1401 VDEV_AUX_BAD_LABEL);
1406 * Determine if this vdev has been split off into another
1407 * pool. If so, then refuse to open it.
1409 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
1410 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
1411 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1412 VDEV_AUX_SPLIT_POOL);
1417 if (strict && (nvlist_lookup_uint64(label,
1418 ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1419 guid != spa_guid(spa))) {
1420 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1421 VDEV_AUX_CORRUPT_DATA);
1426 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
1427 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
1432 * If this vdev just became a top-level vdev because its
1433 * sibling was detached, it will have adopted the parent's
1434 * vdev guid -- but the label may or may not be on disk yet.
1435 * Fortunately, either version of the label will have the
1436 * same top guid, so if we're a top-level vdev, we can
1437 * safely compare to that instead.
1439 * If we split this vdev off instead, then we also check the
1440 * original pool's guid. We don't want to consider the vdev
1441 * corrupt if it is partway through a split operation.
1443 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1445 nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1447 ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
1448 (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1449 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1450 VDEV_AUX_CORRUPT_DATA);
1455 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1457 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1458 VDEV_AUX_CORRUPT_DATA);
1466 * If this is a verbatim import, no need to check the
1467 * state of the pool.
1469 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1470 spa_load_state(spa) == SPA_LOAD_OPEN &&
1471 state != POOL_STATE_ACTIVE)
1472 return (SET_ERROR(EBADF));
1475 * If we were able to open and validate a vdev that was
1476 * previously marked permanently unavailable, clear that state
1479 if (vd->vdev_not_present)
1480 vd->vdev_not_present = 0;
1487 * Close a virtual device.
1490 vdev_close(vdev_t *vd)
1492 spa_t *spa = vd->vdev_spa;
1493 vdev_t *pvd = vd->vdev_parent;
1495 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1498 * If our parent is reopening, then we are as well, unless we are
1501 if (pvd != NULL && pvd->vdev_reopening)
1502 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1504 vd->vdev_ops->vdev_op_close(vd);
1506 vdev_cache_purge(vd);
1508 if (vd->vdev_ops->vdev_op_leaf)
1509 trim_map_destroy(vd);
1512 * We record the previous state before we close it, so that if we are
1513 * doing a reopen(), we don't generate FMA ereports if we notice that
1514 * it's still faulted.
1516 vd->vdev_prevstate = vd->vdev_state;
1518 if (vd->vdev_offline)
1519 vd->vdev_state = VDEV_STATE_OFFLINE;
1521 vd->vdev_state = VDEV_STATE_CLOSED;
1522 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1526 vdev_hold(vdev_t *vd)
1528 spa_t *spa = vd->vdev_spa;
1530 ASSERT(spa_is_root(spa));
1531 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1534 for (int c = 0; c < vd->vdev_children; c++)
1535 vdev_hold(vd->vdev_child[c]);
1537 if (vd->vdev_ops->vdev_op_leaf)
1538 vd->vdev_ops->vdev_op_hold(vd);
1542 vdev_rele(vdev_t *vd)
1544 spa_t *spa = vd->vdev_spa;
1546 ASSERT(spa_is_root(spa));
1547 for (int c = 0; c < vd->vdev_children; c++)
1548 vdev_rele(vd->vdev_child[c]);
1550 if (vd->vdev_ops->vdev_op_leaf)
1551 vd->vdev_ops->vdev_op_rele(vd);
1555 * Reopen all interior vdevs and any unopened leaves. We don't actually
1556 * reopen leaf vdevs which had previously been opened as they might deadlock
1557 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
1558 * If the leaf has never been opened then open it, as usual.
1561 vdev_reopen(vdev_t *vd)
1563 spa_t *spa = vd->vdev_spa;
1565 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1567 /* set the reopening flag unless we're taking the vdev offline */
1568 vd->vdev_reopening = !vd->vdev_offline;
1570 (void) vdev_open(vd);
1573 * Call vdev_validate() here to make sure we have the same device.
1574 * Otherwise, a device with an invalid label could be successfully
1575 * opened in response to vdev_reopen().
1578 (void) vdev_validate_aux(vd);
1579 if (vdev_readable(vd) && vdev_writeable(vd) &&
1580 vd->vdev_aux == &spa->spa_l2cache &&
1581 !l2arc_vdev_present(vd))
1582 l2arc_add_vdev(spa, vd);
1584 (void) vdev_validate(vd, B_TRUE);
1588 * Reassess parent vdev's health.
1590 vdev_propagate_state(vd);
1594 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1599 * Normally, partial opens (e.g. of a mirror) are allowed.
1600 * For a create, however, we want to fail the request if
1601 * there are any components we can't open.
1603 error = vdev_open(vd);
1605 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1607 return (error ? error : ENXIO);
1611 * Recursively load DTLs and initialize all labels.
1613 if ((error = vdev_dtl_load(vd)) != 0 ||
1614 (error = vdev_label_init(vd, txg, isreplacing ?
1615 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1624 vdev_metaslab_set_size(vdev_t *vd)
1627 * Aim for roughly 200 metaslabs per vdev.
1629 vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1630 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1634 * Maximize performance by inflating the configured ashift for
1635 * top level vdevs to be as close to the physical ashift as
1636 * possible without exceeding the administrator specified
1640 vdev_ashift_optimize(vdev_t *vd)
1642 if (vd == vd->vdev_top &&
1643 (vd->vdev_ashift < vd->vdev_physical_ashift) &&
1644 (vd->vdev_ashift < zfs_max_auto_ashift)) {
1645 vd->vdev_ashift = MIN(zfs_max_auto_ashift,
1646 vd->vdev_physical_ashift);
1651 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1653 ASSERT(vd == vd->vdev_top);
1654 ASSERT(!vd->vdev_ishole);
1655 ASSERT(ISP2(flags));
1656 ASSERT(spa_writeable(vd->vdev_spa));
1658 if (flags & VDD_METASLAB)
1659 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1661 if (flags & VDD_DTL)
1662 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1664 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1668 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
1670 for (int c = 0; c < vd->vdev_children; c++)
1671 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
1673 if (vd->vdev_ops->vdev_op_leaf)
1674 vdev_dirty(vd->vdev_top, flags, vd, txg);
1680 * A vdev's DTL (dirty time log) is the set of transaction groups for which
1681 * the vdev has less than perfect replication. There are four kinds of DTL:
1683 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
1685 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
1687 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
1688 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
1689 * txgs that was scrubbed.
1691 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
1692 * persistent errors or just some device being offline.
1693 * Unlike the other three, the DTL_OUTAGE map is not generally
1694 * maintained; it's only computed when needed, typically to
1695 * determine whether a device can be detached.
1697 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
1698 * either has the data or it doesn't.
1700 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
1701 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
1702 * if any child is less than fully replicated, then so is its parent.
1703 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
1704 * comprising only those txgs which appear in 'maxfaults' or more children;
1705 * those are the txgs we don't have enough replication to read. For example,
1706 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
1707 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
1708 * two child DTL_MISSING maps.
1710 * It should be clear from the above that to compute the DTLs and outage maps
1711 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
1712 * Therefore, that is all we keep on disk. When loading the pool, or after
1713 * a configuration change, we generate all other DTLs from first principles.
1716 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1718 range_tree_t *rt = vd->vdev_dtl[t];
1720 ASSERT(t < DTL_TYPES);
1721 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1722 ASSERT(spa_writeable(vd->vdev_spa));
1724 mutex_enter(rt->rt_lock);
1725 if (!range_tree_contains(rt, txg, size))
1726 range_tree_add(rt, txg, size);
1727 mutex_exit(rt->rt_lock);
1731 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1733 range_tree_t *rt = vd->vdev_dtl[t];
1734 boolean_t dirty = B_FALSE;
1736 ASSERT(t < DTL_TYPES);
1737 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1739 mutex_enter(rt->rt_lock);
1740 if (range_tree_space(rt) != 0)
1741 dirty = range_tree_contains(rt, txg, size);
1742 mutex_exit(rt->rt_lock);
1748 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
1750 range_tree_t *rt = vd->vdev_dtl[t];
1753 mutex_enter(rt->rt_lock);
1754 empty = (range_tree_space(rt) == 0);
1755 mutex_exit(rt->rt_lock);
1761 * Returns the lowest txg in the DTL range.
1764 vdev_dtl_min(vdev_t *vd)
1768 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1769 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1770 ASSERT0(vd->vdev_children);
1772 rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
1773 return (rs->rs_start - 1);
1777 * Returns the highest txg in the DTL.
1780 vdev_dtl_max(vdev_t *vd)
1784 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1785 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1786 ASSERT0(vd->vdev_children);
1788 rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
1789 return (rs->rs_end);
1793 * Determine if a resilvering vdev should remove any DTL entries from
1794 * its range. If the vdev was resilvering for the entire duration of the
1795 * scan then it should excise that range from its DTLs. Otherwise, this
1796 * vdev is considered partially resilvered and should leave its DTL
1797 * entries intact. The comment in vdev_dtl_reassess() describes how we
1801 vdev_dtl_should_excise(vdev_t *vd)
1803 spa_t *spa = vd->vdev_spa;
1804 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1806 ASSERT0(scn->scn_phys.scn_errors);
1807 ASSERT0(vd->vdev_children);
1809 if (vd->vdev_resilver_txg == 0 ||
1810 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0)
1814 * When a resilver is initiated the scan will assign the scn_max_txg
1815 * value to the highest txg value that exists in all DTLs. If this
1816 * device's max DTL is not part of this scan (i.e. it is not in
1817 * the range (scn_min_txg, scn_max_txg] then it is not eligible
1820 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
1821 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
1822 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
1823 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
1830 * Reassess DTLs after a config change or scrub completion.
1833 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1835 spa_t *spa = vd->vdev_spa;
1839 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1841 for (int c = 0; c < vd->vdev_children; c++)
1842 vdev_dtl_reassess(vd->vdev_child[c], txg,
1843 scrub_txg, scrub_done);
1845 if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
1848 if (vd->vdev_ops->vdev_op_leaf) {
1849 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1851 mutex_enter(&vd->vdev_dtl_lock);
1854 * If we've completed a scan cleanly then determine
1855 * if this vdev should remove any DTLs. We only want to
1856 * excise regions on vdevs that were available during
1857 * the entire duration of this scan.
1859 if (scrub_txg != 0 &&
1860 (spa->spa_scrub_started ||
1861 (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
1862 vdev_dtl_should_excise(vd)) {
1864 * We completed a scrub up to scrub_txg. If we
1865 * did it without rebooting, then the scrub dtl
1866 * will be valid, so excise the old region and
1867 * fold in the scrub dtl. Otherwise, leave the
1868 * dtl as-is if there was an error.
1870 * There's little trick here: to excise the beginning
1871 * of the DTL_MISSING map, we put it into a reference
1872 * tree and then add a segment with refcnt -1 that
1873 * covers the range [0, scrub_txg). This means
1874 * that each txg in that range has refcnt -1 or 0.
1875 * We then add DTL_SCRUB with a refcnt of 2, so that
1876 * entries in the range [0, scrub_txg) will have a
1877 * positive refcnt -- either 1 or 2. We then convert
1878 * the reference tree into the new DTL_MISSING map.
1880 space_reftree_create(&reftree);
1881 space_reftree_add_map(&reftree,
1882 vd->vdev_dtl[DTL_MISSING], 1);
1883 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
1884 space_reftree_add_map(&reftree,
1885 vd->vdev_dtl[DTL_SCRUB], 2);
1886 space_reftree_generate_map(&reftree,
1887 vd->vdev_dtl[DTL_MISSING], 1);
1888 space_reftree_destroy(&reftree);
1890 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
1891 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
1892 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
1894 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
1895 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
1896 if (!vdev_readable(vd))
1897 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
1899 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
1900 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
1903 * If the vdev was resilvering and no longer has any
1904 * DTLs then reset its resilvering flag.
1906 if (vd->vdev_resilver_txg != 0 &&
1907 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 &&
1908 range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0)
1909 vd->vdev_resilver_txg = 0;
1911 mutex_exit(&vd->vdev_dtl_lock);
1914 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1918 mutex_enter(&vd->vdev_dtl_lock);
1919 for (int t = 0; t < DTL_TYPES; t++) {
1920 /* account for child's outage in parent's missing map */
1921 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
1923 continue; /* leaf vdevs only */
1924 if (t == DTL_PARTIAL)
1925 minref = 1; /* i.e. non-zero */
1926 else if (vd->vdev_nparity != 0)
1927 minref = vd->vdev_nparity + 1; /* RAID-Z */
1929 minref = vd->vdev_children; /* any kind of mirror */
1930 space_reftree_create(&reftree);
1931 for (int c = 0; c < vd->vdev_children; c++) {
1932 vdev_t *cvd = vd->vdev_child[c];
1933 mutex_enter(&cvd->vdev_dtl_lock);
1934 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
1935 mutex_exit(&cvd->vdev_dtl_lock);
1937 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
1938 space_reftree_destroy(&reftree);
1940 mutex_exit(&vd->vdev_dtl_lock);
1944 vdev_dtl_load(vdev_t *vd)
1946 spa_t *spa = vd->vdev_spa;
1947 objset_t *mos = spa->spa_meta_objset;
1950 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
1951 ASSERT(!vd->vdev_ishole);
1953 error = space_map_open(&vd->vdev_dtl_sm, mos,
1954 vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock);
1957 ASSERT(vd->vdev_dtl_sm != NULL);
1959 mutex_enter(&vd->vdev_dtl_lock);
1962 * Now that we've opened the space_map we need to update
1965 space_map_update(vd->vdev_dtl_sm);
1967 error = space_map_load(vd->vdev_dtl_sm,
1968 vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
1969 mutex_exit(&vd->vdev_dtl_lock);
1974 for (int c = 0; c < vd->vdev_children; c++) {
1975 error = vdev_dtl_load(vd->vdev_child[c]);
1984 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1986 spa_t *spa = vd->vdev_spa;
1987 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
1988 objset_t *mos = spa->spa_meta_objset;
1989 range_tree_t *rtsync;
1992 uint64_t object = space_map_object(vd->vdev_dtl_sm);
1994 ASSERT(!vd->vdev_ishole);
1995 ASSERT(vd->vdev_ops->vdev_op_leaf);
1997 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1999 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
2000 mutex_enter(&vd->vdev_dtl_lock);
2001 space_map_free(vd->vdev_dtl_sm, tx);
2002 space_map_close(vd->vdev_dtl_sm);
2003 vd->vdev_dtl_sm = NULL;
2004 mutex_exit(&vd->vdev_dtl_lock);
2009 if (vd->vdev_dtl_sm == NULL) {
2010 uint64_t new_object;
2012 new_object = space_map_alloc(mos, tx);
2013 VERIFY3U(new_object, !=, 0);
2015 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
2016 0, -1ULL, 0, &vd->vdev_dtl_lock));
2017 ASSERT(vd->vdev_dtl_sm != NULL);
2020 bzero(&rtlock, sizeof(rtlock));
2021 mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL);
2023 rtsync = range_tree_create(NULL, NULL, &rtlock);
2025 mutex_enter(&rtlock);
2027 mutex_enter(&vd->vdev_dtl_lock);
2028 range_tree_walk(rt, range_tree_add, rtsync);
2029 mutex_exit(&vd->vdev_dtl_lock);
2031 space_map_truncate(vd->vdev_dtl_sm, tx);
2032 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
2033 range_tree_vacate(rtsync, NULL, NULL);
2035 range_tree_destroy(rtsync);
2037 mutex_exit(&rtlock);
2038 mutex_destroy(&rtlock);
2041 * If the object for the space map has changed then dirty
2042 * the top level so that we update the config.
2044 if (object != space_map_object(vd->vdev_dtl_sm)) {
2045 zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, "
2046 "new object %llu", txg, spa_name(spa), object,
2047 space_map_object(vd->vdev_dtl_sm));
2048 vdev_config_dirty(vd->vdev_top);
2053 mutex_enter(&vd->vdev_dtl_lock);
2054 space_map_update(vd->vdev_dtl_sm);
2055 mutex_exit(&vd->vdev_dtl_lock);
2059 * Determine whether the specified vdev can be offlined/detached/removed
2060 * without losing data.
2063 vdev_dtl_required(vdev_t *vd)
2065 spa_t *spa = vd->vdev_spa;
2066 vdev_t *tvd = vd->vdev_top;
2067 uint8_t cant_read = vd->vdev_cant_read;
2070 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2072 if (vd == spa->spa_root_vdev || vd == tvd)
2076 * Temporarily mark the device as unreadable, and then determine
2077 * whether this results in any DTL outages in the top-level vdev.
2078 * If not, we can safely offline/detach/remove the device.
2080 vd->vdev_cant_read = B_TRUE;
2081 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2082 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
2083 vd->vdev_cant_read = cant_read;
2084 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2086 if (!required && zio_injection_enabled)
2087 required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2093 * Determine if resilver is needed, and if so the txg range.
2096 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2098 boolean_t needed = B_FALSE;
2099 uint64_t thismin = UINT64_MAX;
2100 uint64_t thismax = 0;
2102 if (vd->vdev_children == 0) {
2103 mutex_enter(&vd->vdev_dtl_lock);
2104 if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 &&
2105 vdev_writeable(vd)) {
2107 thismin = vdev_dtl_min(vd);
2108 thismax = vdev_dtl_max(vd);
2111 mutex_exit(&vd->vdev_dtl_lock);
2113 for (int c = 0; c < vd->vdev_children; c++) {
2114 vdev_t *cvd = vd->vdev_child[c];
2115 uint64_t cmin, cmax;
2117 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2118 thismin = MIN(thismin, cmin);
2119 thismax = MAX(thismax, cmax);
2125 if (needed && minp) {
2133 vdev_load(vdev_t *vd)
2136 * Recursively load all children.
2138 for (int c = 0; c < vd->vdev_children; c++)
2139 vdev_load(vd->vdev_child[c]);
2142 * If this is a top-level vdev, initialize its metaslabs.
2144 if (vd == vd->vdev_top && !vd->vdev_ishole &&
2145 (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
2146 vdev_metaslab_init(vd, 0) != 0))
2147 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2148 VDEV_AUX_CORRUPT_DATA);
2151 * If this is a leaf vdev, load its DTL.
2153 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
2154 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2155 VDEV_AUX_CORRUPT_DATA);
2159 * The special vdev case is used for hot spares and l2cache devices. Its
2160 * sole purpose it to set the vdev state for the associated vdev. To do this,
2161 * we make sure that we can open the underlying device, then try to read the
2162 * label, and make sure that the label is sane and that it hasn't been
2163 * repurposed to another pool.
2166 vdev_validate_aux(vdev_t *vd)
2169 uint64_t guid, version;
2172 if (!vdev_readable(vd))
2175 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
2176 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2177 VDEV_AUX_CORRUPT_DATA);
2181 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2182 !SPA_VERSION_IS_SUPPORTED(version) ||
2183 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
2184 guid != vd->vdev_guid ||
2185 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
2186 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2187 VDEV_AUX_CORRUPT_DATA);
2193 * We don't actually check the pool state here. If it's in fact in
2194 * use by another pool, we update this fact on the fly when requested.
2201 vdev_remove(vdev_t *vd, uint64_t txg)
2203 spa_t *spa = vd->vdev_spa;
2204 objset_t *mos = spa->spa_meta_objset;
2207 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
2209 if (vd->vdev_ms != NULL) {
2210 for (int m = 0; m < vd->vdev_ms_count; m++) {
2211 metaslab_t *msp = vd->vdev_ms[m];
2213 if (msp == NULL || msp->ms_sm == NULL)
2216 mutex_enter(&msp->ms_lock);
2217 VERIFY0(space_map_allocated(msp->ms_sm));
2218 space_map_free(msp->ms_sm, tx);
2219 space_map_close(msp->ms_sm);
2221 mutex_exit(&msp->ms_lock);
2225 if (vd->vdev_ms_array) {
2226 (void) dmu_object_free(mos, vd->vdev_ms_array, tx);
2227 vd->vdev_ms_array = 0;
2233 vdev_sync_done(vdev_t *vd, uint64_t txg)
2236 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2238 ASSERT(!vd->vdev_ishole);
2240 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2241 metaslab_sync_done(msp, txg);
2244 metaslab_sync_reassess(vd->vdev_mg);
2248 vdev_sync(vdev_t *vd, uint64_t txg)
2250 spa_t *spa = vd->vdev_spa;
2255 ASSERT(!vd->vdev_ishole);
2257 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
2258 ASSERT(vd == vd->vdev_top);
2259 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2260 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2261 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2262 ASSERT(vd->vdev_ms_array != 0);
2263 vdev_config_dirty(vd);
2268 * Remove the metadata associated with this vdev once it's empty.
2270 if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
2271 vdev_remove(vd, txg);
2273 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2274 metaslab_sync(msp, txg);
2275 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2278 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2279 vdev_dtl_sync(lvd, txg);
2281 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2285 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2287 return (vd->vdev_ops->vdev_op_asize(vd, psize));
2291 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
2292 * not be opened, and no I/O is attempted.
2295 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2299 spa_vdev_state_enter(spa, SCL_NONE);
2301 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2302 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2304 if (!vd->vdev_ops->vdev_op_leaf)
2305 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2310 * We don't directly use the aux state here, but if we do a
2311 * vdev_reopen(), we need this value to be present to remember why we
2314 vd->vdev_label_aux = aux;
2317 * Faulted state takes precedence over degraded.
2319 vd->vdev_delayed_close = B_FALSE;
2320 vd->vdev_faulted = 1ULL;
2321 vd->vdev_degraded = 0ULL;
2322 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
2325 * If this device has the only valid copy of the data, then
2326 * back off and simply mark the vdev as degraded instead.
2328 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
2329 vd->vdev_degraded = 1ULL;
2330 vd->vdev_faulted = 0ULL;
2333 * If we reopen the device and it's not dead, only then do we
2338 if (vdev_readable(vd))
2339 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
2342 return (spa_vdev_state_exit(spa, vd, 0));
2346 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
2347 * user that something is wrong. The vdev continues to operate as normal as far
2348 * as I/O is concerned.
2351 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2355 spa_vdev_state_enter(spa, SCL_NONE);
2357 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2358 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2360 if (!vd->vdev_ops->vdev_op_leaf)
2361 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2364 * If the vdev is already faulted, then don't do anything.
2366 if (vd->vdev_faulted || vd->vdev_degraded)
2367 return (spa_vdev_state_exit(spa, NULL, 0));
2369 vd->vdev_degraded = 1ULL;
2370 if (!vdev_is_dead(vd))
2371 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2374 return (spa_vdev_state_exit(spa, vd, 0));
2378 * Online the given vdev.
2380 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
2381 * spare device should be detached when the device finishes resilvering.
2382 * Second, the online should be treated like a 'test' online case, so no FMA
2383 * events are generated if the device fails to open.
2386 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
2388 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
2390 spa_vdev_state_enter(spa, SCL_NONE);
2392 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2393 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2395 if (!vd->vdev_ops->vdev_op_leaf)
2396 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2399 vd->vdev_offline = B_FALSE;
2400 vd->vdev_tmpoffline = B_FALSE;
2401 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
2402 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
2404 /* XXX - L2ARC 1.0 does not support expansion */
2405 if (!vd->vdev_aux) {
2406 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2407 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
2411 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
2413 if (!vd->vdev_aux) {
2414 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2415 pvd->vdev_expanding = B_FALSE;
2419 *newstate = vd->vdev_state;
2420 if ((flags & ZFS_ONLINE_UNSPARE) &&
2421 !vdev_is_dead(vd) && vd->vdev_parent &&
2422 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2423 vd->vdev_parent->vdev_child[0] == vd)
2424 vd->vdev_unspare = B_TRUE;
2426 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
2428 /* XXX - L2ARC 1.0 does not support expansion */
2430 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
2431 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2433 return (spa_vdev_state_exit(spa, vd, 0));
2437 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2441 uint64_t generation;
2442 metaslab_group_t *mg;
2445 spa_vdev_state_enter(spa, SCL_ALLOC);
2447 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2448 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2450 if (!vd->vdev_ops->vdev_op_leaf)
2451 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2455 generation = spa->spa_config_generation + 1;
2458 * If the device isn't already offline, try to offline it.
2460 if (!vd->vdev_offline) {
2462 * If this device has the only valid copy of some data,
2463 * don't allow it to be offlined. Log devices are always
2466 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2467 vdev_dtl_required(vd))
2468 return (spa_vdev_state_exit(spa, NULL, EBUSY));
2471 * If the top-level is a slog and it has had allocations
2472 * then proceed. We check that the vdev's metaslab group
2473 * is not NULL since it's possible that we may have just
2474 * added this vdev but not yet initialized its metaslabs.
2476 if (tvd->vdev_islog && mg != NULL) {
2478 * Prevent any future allocations.
2480 metaslab_group_passivate(mg);
2481 (void) spa_vdev_state_exit(spa, vd, 0);
2483 error = spa_offline_log(spa);
2485 spa_vdev_state_enter(spa, SCL_ALLOC);
2488 * Check to see if the config has changed.
2490 if (error || generation != spa->spa_config_generation) {
2491 metaslab_group_activate(mg);
2493 return (spa_vdev_state_exit(spa,
2495 (void) spa_vdev_state_exit(spa, vd, 0);
2498 ASSERT0(tvd->vdev_stat.vs_alloc);
2502 * Offline this device and reopen its top-level vdev.
2503 * If the top-level vdev is a log device then just offline
2504 * it. Otherwise, if this action results in the top-level
2505 * vdev becoming unusable, undo it and fail the request.
2507 vd->vdev_offline = B_TRUE;
2510 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2511 vdev_is_dead(tvd)) {
2512 vd->vdev_offline = B_FALSE;
2514 return (spa_vdev_state_exit(spa, NULL, EBUSY));
2518 * Add the device back into the metaslab rotor so that
2519 * once we online the device it's open for business.
2521 if (tvd->vdev_islog && mg != NULL)
2522 metaslab_group_activate(mg);
2525 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
2527 return (spa_vdev_state_exit(spa, vd, 0));
2531 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
2535 mutex_enter(&spa->spa_vdev_top_lock);
2536 error = vdev_offline_locked(spa, guid, flags);
2537 mutex_exit(&spa->spa_vdev_top_lock);
2543 * Clear the error counts associated with this vdev. Unlike vdev_online() and
2544 * vdev_offline(), we assume the spa config is locked. We also clear all
2545 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
2548 vdev_clear(spa_t *spa, vdev_t *vd)
2550 vdev_t *rvd = spa->spa_root_vdev;
2552 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2557 vd->vdev_stat.vs_read_errors = 0;
2558 vd->vdev_stat.vs_write_errors = 0;
2559 vd->vdev_stat.vs_checksum_errors = 0;
2561 for (int c = 0; c < vd->vdev_children; c++)
2562 vdev_clear(spa, vd->vdev_child[c]);
2565 for (int c = 0; c < spa->spa_l2cache.sav_count; c++)
2566 vdev_clear(spa, spa->spa_l2cache.sav_vdevs[c]);
2568 for (int c = 0; c < spa->spa_spares.sav_count; c++)
2569 vdev_clear(spa, spa->spa_spares.sav_vdevs[c]);
2573 * If we're in the FAULTED state or have experienced failed I/O, then
2574 * clear the persistent state and attempt to reopen the device. We
2575 * also mark the vdev config dirty, so that the new faulted state is
2576 * written out to disk.
2578 if (vd->vdev_faulted || vd->vdev_degraded ||
2579 !vdev_readable(vd) || !vdev_writeable(vd)) {
2582 * When reopening in reponse to a clear event, it may be due to
2583 * a fmadm repair request. In this case, if the device is
2584 * still broken, we want to still post the ereport again.
2586 vd->vdev_forcefault = B_TRUE;
2588 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
2589 vd->vdev_cant_read = B_FALSE;
2590 vd->vdev_cant_write = B_FALSE;
2592 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
2594 vd->vdev_forcefault = B_FALSE;
2596 if (vd != rvd && vdev_writeable(vd->vdev_top))
2597 vdev_state_dirty(vd->vdev_top);
2599 if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
2600 spa_async_request(spa, SPA_ASYNC_RESILVER);
2602 spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
2606 * When clearing a FMA-diagnosed fault, we always want to
2607 * unspare the device, as we assume that the original spare was
2608 * done in response to the FMA fault.
2610 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
2611 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2612 vd->vdev_parent->vdev_child[0] == vd)
2613 vd->vdev_unspare = B_TRUE;
2617 vdev_is_dead(vdev_t *vd)
2620 * Holes and missing devices are always considered "dead".
2621 * This simplifies the code since we don't have to check for
2622 * these types of devices in the various code paths.
2623 * Instead we rely on the fact that we skip over dead devices
2624 * before issuing I/O to them.
2626 return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
2627 vd->vdev_ops == &vdev_missing_ops);
2631 vdev_readable(vdev_t *vd)
2633 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
2637 vdev_writeable(vdev_t *vd)
2639 return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
2643 vdev_allocatable(vdev_t *vd)
2645 uint64_t state = vd->vdev_state;
2648 * We currently allow allocations from vdevs which may be in the
2649 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
2650 * fails to reopen then we'll catch it later when we're holding
2651 * the proper locks. Note that we have to get the vdev state
2652 * in a local variable because although it changes atomically,
2653 * we're asking two separate questions about it.
2655 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
2656 !vd->vdev_cant_write && !vd->vdev_ishole);
2660 vdev_accessible(vdev_t *vd, zio_t *zio)
2662 ASSERT(zio->io_vd == vd);
2664 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
2667 if (zio->io_type == ZIO_TYPE_READ)
2668 return (!vd->vdev_cant_read);
2670 if (zio->io_type == ZIO_TYPE_WRITE)
2671 return (!vd->vdev_cant_write);
2677 * Get statistics for the given vdev.
2680 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2682 vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2684 mutex_enter(&vd->vdev_stat_lock);
2685 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
2686 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2687 vs->vs_state = vd->vdev_state;
2688 vs->vs_rsize = vdev_get_min_asize(vd);
2689 if (vd->vdev_ops->vdev_op_leaf)
2690 vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2691 vs->vs_esize = vd->vdev_max_asize - vd->vdev_asize;
2692 vs->vs_configured_ashift = vd->vdev_top != NULL
2693 ? vd->vdev_top->vdev_ashift : vd->vdev_ashift;
2694 vs->vs_logical_ashift = vd->vdev_logical_ashift;
2695 vs->vs_physical_ashift = vd->vdev_physical_ashift;
2696 mutex_exit(&vd->vdev_stat_lock);
2699 * If we're getting stats on the root vdev, aggregate the I/O counts
2700 * over all top-level vdevs (i.e. the direct children of the root).
2703 for (int c = 0; c < rvd->vdev_children; c++) {
2704 vdev_t *cvd = rvd->vdev_child[c];
2705 vdev_stat_t *cvs = &cvd->vdev_stat;
2707 mutex_enter(&vd->vdev_stat_lock);
2708 for (int t = 0; t < ZIO_TYPES; t++) {
2709 vs->vs_ops[t] += cvs->vs_ops[t];
2710 vs->vs_bytes[t] += cvs->vs_bytes[t];
2712 cvs->vs_scan_removing = cvd->vdev_removing;
2713 mutex_exit(&vd->vdev_stat_lock);
2719 vdev_clear_stats(vdev_t *vd)
2721 mutex_enter(&vd->vdev_stat_lock);
2722 vd->vdev_stat.vs_space = 0;
2723 vd->vdev_stat.vs_dspace = 0;
2724 vd->vdev_stat.vs_alloc = 0;
2725 mutex_exit(&vd->vdev_stat_lock);
2729 vdev_scan_stat_init(vdev_t *vd)
2731 vdev_stat_t *vs = &vd->vdev_stat;
2733 for (int c = 0; c < vd->vdev_children; c++)
2734 vdev_scan_stat_init(vd->vdev_child[c]);
2736 mutex_enter(&vd->vdev_stat_lock);
2737 vs->vs_scan_processed = 0;
2738 mutex_exit(&vd->vdev_stat_lock);
2742 vdev_stat_update(zio_t *zio, uint64_t psize)
2744 spa_t *spa = zio->io_spa;
2745 vdev_t *rvd = spa->spa_root_vdev;
2746 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2748 uint64_t txg = zio->io_txg;
2749 vdev_stat_t *vs = &vd->vdev_stat;
2750 zio_type_t type = zio->io_type;
2751 int flags = zio->io_flags;
2754 * If this i/o is a gang leader, it didn't do any actual work.
2756 if (zio->io_gang_tree)
2759 if (zio->io_error == 0) {
2761 * If this is a root i/o, don't count it -- we've already
2762 * counted the top-level vdevs, and vdev_get_stats() will
2763 * aggregate them when asked. This reduces contention on
2764 * the root vdev_stat_lock and implicitly handles blocks
2765 * that compress away to holes, for which there is no i/o.
2766 * (Holes never create vdev children, so all the counters
2767 * remain zero, which is what we want.)
2769 * Note: this only applies to successful i/o (io_error == 0)
2770 * because unlike i/o counts, errors are not additive.
2771 * When reading a ditto block, for example, failure of
2772 * one top-level vdev does not imply a root-level error.
2777 ASSERT(vd == zio->io_vd);
2779 if (flags & ZIO_FLAG_IO_BYPASS)
2782 mutex_enter(&vd->vdev_stat_lock);
2784 if (flags & ZIO_FLAG_IO_REPAIR) {
2785 if (flags & ZIO_FLAG_SCAN_THREAD) {
2786 dsl_scan_phys_t *scn_phys =
2787 &spa->spa_dsl_pool->dp_scan->scn_phys;
2788 uint64_t *processed = &scn_phys->scn_processed;
2791 if (vd->vdev_ops->vdev_op_leaf)
2792 atomic_add_64(processed, psize);
2793 vs->vs_scan_processed += psize;
2796 if (flags & ZIO_FLAG_SELF_HEAL)
2797 vs->vs_self_healed += psize;
2801 vs->vs_bytes[type] += psize;
2803 mutex_exit(&vd->vdev_stat_lock);
2807 if (flags & ZIO_FLAG_SPECULATIVE)
2811 * If this is an I/O error that is going to be retried, then ignore the
2812 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
2813 * hard errors, when in reality they can happen for any number of
2814 * innocuous reasons (bus resets, MPxIO link failure, etc).
2816 if (zio->io_error == EIO &&
2817 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
2821 * Intent logs writes won't propagate their error to the root
2822 * I/O so don't mark these types of failures as pool-level
2825 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
2828 mutex_enter(&vd->vdev_stat_lock);
2829 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
2830 if (zio->io_error == ECKSUM)
2831 vs->vs_checksum_errors++;
2833 vs->vs_read_errors++;
2835 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
2836 vs->vs_write_errors++;
2837 mutex_exit(&vd->vdev_stat_lock);
2839 if (type == ZIO_TYPE_WRITE && txg != 0 &&
2840 (!(flags & ZIO_FLAG_IO_REPAIR) ||
2841 (flags & ZIO_FLAG_SCAN_THREAD) ||
2842 spa->spa_claiming)) {
2844 * This is either a normal write (not a repair), or it's
2845 * a repair induced by the scrub thread, or it's a repair
2846 * made by zil_claim() during spa_load() in the first txg.
2847 * In the normal case, we commit the DTL change in the same
2848 * txg as the block was born. In the scrub-induced repair
2849 * case, we know that scrubs run in first-pass syncing context,
2850 * so we commit the DTL change in spa_syncing_txg(spa).
2851 * In the zil_claim() case, we commit in spa_first_txg(spa).
2853 * We currently do not make DTL entries for failed spontaneous
2854 * self-healing writes triggered by normal (non-scrubbing)
2855 * reads, because we have no transactional context in which to
2856 * do so -- and it's not clear that it'd be desirable anyway.
2858 if (vd->vdev_ops->vdev_op_leaf) {
2859 uint64_t commit_txg = txg;
2860 if (flags & ZIO_FLAG_SCAN_THREAD) {
2861 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2862 ASSERT(spa_sync_pass(spa) == 1);
2863 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
2864 commit_txg = spa_syncing_txg(spa);
2865 } else if (spa->spa_claiming) {
2866 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2867 commit_txg = spa_first_txg(spa);
2869 ASSERT(commit_txg >= spa_syncing_txg(spa));
2870 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
2872 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2873 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
2874 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2877 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2882 * Update the in-core space usage stats for this vdev, its metaslab class,
2883 * and the root vdev.
2886 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
2887 int64_t space_delta)
2889 int64_t dspace_delta = space_delta;
2890 spa_t *spa = vd->vdev_spa;
2891 vdev_t *rvd = spa->spa_root_vdev;
2892 metaslab_group_t *mg = vd->vdev_mg;
2893 metaslab_class_t *mc = mg ? mg->mg_class : NULL;
2895 ASSERT(vd == vd->vdev_top);
2898 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
2899 * factor. We must calculate this here and not at the root vdev
2900 * because the root vdev's psize-to-asize is simply the max of its
2901 * childrens', thus not accurate enough for us.
2903 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
2904 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
2905 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
2906 vd->vdev_deflate_ratio;
2908 mutex_enter(&vd->vdev_stat_lock);
2909 vd->vdev_stat.vs_alloc += alloc_delta;
2910 vd->vdev_stat.vs_space += space_delta;
2911 vd->vdev_stat.vs_dspace += dspace_delta;
2912 mutex_exit(&vd->vdev_stat_lock);
2914 if (mc == spa_normal_class(spa)) {
2915 mutex_enter(&rvd->vdev_stat_lock);
2916 rvd->vdev_stat.vs_alloc += alloc_delta;
2917 rvd->vdev_stat.vs_space += space_delta;
2918 rvd->vdev_stat.vs_dspace += dspace_delta;
2919 mutex_exit(&rvd->vdev_stat_lock);
2923 ASSERT(rvd == vd->vdev_parent);
2924 ASSERT(vd->vdev_ms_count != 0);
2926 metaslab_class_space_update(mc,
2927 alloc_delta, defer_delta, space_delta, dspace_delta);
2932 * Mark a top-level vdev's config as dirty, placing it on the dirty list
2933 * so that it will be written out next time the vdev configuration is synced.
2934 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2937 vdev_config_dirty(vdev_t *vd)
2939 spa_t *spa = vd->vdev_spa;
2940 vdev_t *rvd = spa->spa_root_vdev;
2943 ASSERT(spa_writeable(spa));
2946 * If this is an aux vdev (as with l2cache and spare devices), then we
2947 * update the vdev config manually and set the sync flag.
2949 if (vd->vdev_aux != NULL) {
2950 spa_aux_vdev_t *sav = vd->vdev_aux;
2954 for (c = 0; c < sav->sav_count; c++) {
2955 if (sav->sav_vdevs[c] == vd)
2959 if (c == sav->sav_count) {
2961 * We're being removed. There's nothing more to do.
2963 ASSERT(sav->sav_sync == B_TRUE);
2967 sav->sav_sync = B_TRUE;
2969 if (nvlist_lookup_nvlist_array(sav->sav_config,
2970 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
2971 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
2972 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
2978 * Setting the nvlist in the middle if the array is a little
2979 * sketchy, but it will work.
2981 nvlist_free(aux[c]);
2982 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
2988 * The dirty list is protected by the SCL_CONFIG lock. The caller
2989 * must either hold SCL_CONFIG as writer, or must be the sync thread
2990 * (which holds SCL_CONFIG as reader). There's only one sync thread,
2991 * so this is sufficient to ensure mutual exclusion.
2993 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2994 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2995 spa_config_held(spa, SCL_CONFIG, RW_READER)));
2998 for (c = 0; c < rvd->vdev_children; c++)
2999 vdev_config_dirty(rvd->vdev_child[c]);
3001 ASSERT(vd == vd->vdev_top);
3003 if (!list_link_active(&vd->vdev_config_dirty_node) &&
3005 list_insert_head(&spa->spa_config_dirty_list, vd);
3010 vdev_config_clean(vdev_t *vd)
3012 spa_t *spa = vd->vdev_spa;
3014 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3015 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3016 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3018 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3019 list_remove(&spa->spa_config_dirty_list, vd);
3023 * Mark a top-level vdev's state as dirty, so that the next pass of
3024 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3025 * the state changes from larger config changes because they require
3026 * much less locking, and are often needed for administrative actions.
3029 vdev_state_dirty(vdev_t *vd)
3031 spa_t *spa = vd->vdev_spa;
3033 ASSERT(spa_writeable(spa));
3034 ASSERT(vd == vd->vdev_top);
3037 * The state list is protected by the SCL_STATE lock. The caller
3038 * must either hold SCL_STATE as writer, or must be the sync thread
3039 * (which holds SCL_STATE as reader). There's only one sync thread,
3040 * so this is sufficient to ensure mutual exclusion.
3042 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3043 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3044 spa_config_held(spa, SCL_STATE, RW_READER)));
3046 if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
3047 list_insert_head(&spa->spa_state_dirty_list, vd);
3051 vdev_state_clean(vdev_t *vd)
3053 spa_t *spa = vd->vdev_spa;
3055 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3056 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3057 spa_config_held(spa, SCL_STATE, RW_READER)));
3059 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3060 list_remove(&spa->spa_state_dirty_list, vd);
3064 * Propagate vdev state up from children to parent.
3067 vdev_propagate_state(vdev_t *vd)
3069 spa_t *spa = vd->vdev_spa;
3070 vdev_t *rvd = spa->spa_root_vdev;
3071 int degraded = 0, faulted = 0;
3075 if (vd->vdev_children > 0) {
3076 for (int c = 0; c < vd->vdev_children; c++) {
3077 child = vd->vdev_child[c];
3080 * Don't factor holes into the decision.
3082 if (child->vdev_ishole)
3085 if (!vdev_readable(child) ||
3086 (!vdev_writeable(child) && spa_writeable(spa))) {
3088 * Root special: if there is a top-level log
3089 * device, treat the root vdev as if it were
3092 if (child->vdev_islog && vd == rvd)
3096 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
3100 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
3104 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
3107 * Root special: if there is a top-level vdev that cannot be
3108 * opened due to corrupted metadata, then propagate the root
3109 * vdev's aux state as 'corrupt' rather than 'insufficient
3112 if (corrupted && vd == rvd &&
3113 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
3114 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
3115 VDEV_AUX_CORRUPT_DATA);
3118 if (vd->vdev_parent)
3119 vdev_propagate_state(vd->vdev_parent);
3123 * Set a vdev's state. If this is during an open, we don't update the parent
3124 * state, because we're in the process of opening children depth-first.
3125 * Otherwise, we propagate the change to the parent.
3127 * If this routine places a device in a faulted state, an appropriate ereport is
3131 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3133 uint64_t save_state;
3134 spa_t *spa = vd->vdev_spa;
3136 if (state == vd->vdev_state) {
3137 vd->vdev_stat.vs_aux = aux;
3141 save_state = vd->vdev_state;
3143 vd->vdev_state = state;
3144 vd->vdev_stat.vs_aux = aux;
3147 * If we are setting the vdev state to anything but an open state, then
3148 * always close the underlying device unless the device has requested
3149 * a delayed close (i.e. we're about to remove or fault the device).
3150 * Otherwise, we keep accessible but invalid devices open forever.
3151 * We don't call vdev_close() itself, because that implies some extra
3152 * checks (offline, etc) that we don't want here. This is limited to
3153 * leaf devices, because otherwise closing the device will affect other
3156 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
3157 vd->vdev_ops->vdev_op_leaf)
3158 vd->vdev_ops->vdev_op_close(vd);
3161 * If we have brought this vdev back into service, we need
3162 * to notify fmd so that it can gracefully repair any outstanding
3163 * cases due to a missing device. We do this in all cases, even those
3164 * that probably don't correlate to a repaired fault. This is sure to
3165 * catch all cases, and we let the zfs-retire agent sort it out. If
3166 * this is a transient state it's OK, as the retire agent will
3167 * double-check the state of the vdev before repairing it.
3169 if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
3170 vd->vdev_prevstate != state)
3171 zfs_post_state_change(spa, vd);
3173 if (vd->vdev_removed &&
3174 state == VDEV_STATE_CANT_OPEN &&
3175 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
3177 * If the previous state is set to VDEV_STATE_REMOVED, then this
3178 * device was previously marked removed and someone attempted to
3179 * reopen it. If this failed due to a nonexistent device, then
3180 * keep the device in the REMOVED state. We also let this be if
3181 * it is one of our special test online cases, which is only
3182 * attempting to online the device and shouldn't generate an FMA
3185 vd->vdev_state = VDEV_STATE_REMOVED;
3186 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
3187 } else if (state == VDEV_STATE_REMOVED) {
3188 vd->vdev_removed = B_TRUE;
3189 } else if (state == VDEV_STATE_CANT_OPEN) {
3191 * If we fail to open a vdev during an import or recovery, we
3192 * mark it as "not available", which signifies that it was
3193 * never there to begin with. Failure to open such a device
3194 * is not considered an error.
3196 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3197 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3198 vd->vdev_ops->vdev_op_leaf)
3199 vd->vdev_not_present = 1;
3202 * Post the appropriate ereport. If the 'prevstate' field is
3203 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3204 * that this is part of a vdev_reopen(). In this case, we don't
3205 * want to post the ereport if the device was already in the
3206 * CANT_OPEN state beforehand.
3208 * If the 'checkremove' flag is set, then this is an attempt to
3209 * online the device in response to an insertion event. If we
3210 * hit this case, then we have detected an insertion event for a
3211 * faulted or offline device that wasn't in the removed state.
3212 * In this scenario, we don't post an ereport because we are
3213 * about to replace the device, or attempt an online with
3214 * vdev_forcefault, which will generate the fault for us.
3216 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
3217 !vd->vdev_not_present && !vd->vdev_checkremove &&
3218 vd != spa->spa_root_vdev) {
3222 case VDEV_AUX_OPEN_FAILED:
3223 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
3225 case VDEV_AUX_CORRUPT_DATA:
3226 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
3228 case VDEV_AUX_NO_REPLICAS:
3229 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
3231 case VDEV_AUX_BAD_GUID_SUM:
3232 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
3234 case VDEV_AUX_TOO_SMALL:
3235 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3237 case VDEV_AUX_BAD_LABEL:
3238 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3241 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3244 zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3247 /* Erase any notion of persistent removed state */
3248 vd->vdev_removed = B_FALSE;
3250 vd->vdev_removed = B_FALSE;
3253 if (!isopen && vd->vdev_parent)
3254 vdev_propagate_state(vd->vdev_parent);
3258 * Check the vdev configuration to ensure that it's capable of supporting
3261 * On Solaris, we do not support RAID-Z or partial configuration. In
3262 * addition, only a single top-level vdev is allowed and none of the
3263 * leaves can be wholedisks.
3265 * For FreeBSD, we can boot from any configuration. There is a
3266 * limitation that the boot filesystem must be either uncompressed or
3267 * compresses with lzjb compression but I'm not sure how to enforce
3271 vdev_is_bootable(vdev_t *vd)
3274 if (!vd->vdev_ops->vdev_op_leaf) {
3275 char *vdev_type = vd->vdev_ops->vdev_op_type;
3277 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
3278 vd->vdev_children > 1) {
3280 } else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
3281 strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
3284 } else if (vd->vdev_wholedisk == 1) {
3288 for (int c = 0; c < vd->vdev_children; c++) {
3289 if (!vdev_is_bootable(vd->vdev_child[c]))
3297 * Load the state from the original vdev tree (ovd) which
3298 * we've retrieved from the MOS config object. If the original
3299 * vdev was offline or faulted then we transfer that state to the
3300 * device in the current vdev tree (nvd).
3303 vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
3305 spa_t *spa = nvd->vdev_spa;
3307 ASSERT(nvd->vdev_top->vdev_islog);
3308 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3309 ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
3311 for (int c = 0; c < nvd->vdev_children; c++)
3312 vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
3314 if (nvd->vdev_ops->vdev_op_leaf) {
3316 * Restore the persistent vdev state
3318 nvd->vdev_offline = ovd->vdev_offline;
3319 nvd->vdev_faulted = ovd->vdev_faulted;
3320 nvd->vdev_degraded = ovd->vdev_degraded;
3321 nvd->vdev_removed = ovd->vdev_removed;
3326 * Determine if a log device has valid content. If the vdev was
3327 * removed or faulted in the MOS config then we know that
3328 * the content on the log device has already been written to the pool.
3331 vdev_log_state_valid(vdev_t *vd)
3333 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
3337 for (int c = 0; c < vd->vdev_children; c++)
3338 if (vdev_log_state_valid(vd->vdev_child[c]))
3345 * Expand a vdev if possible.
3348 vdev_expand(vdev_t *vd, uint64_t txg)
3350 ASSERT(vd->vdev_top == vd);
3351 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3353 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
3354 VERIFY(vdev_metaslab_init(vd, txg) == 0);
3355 vdev_config_dirty(vd);
3363 vdev_split(vdev_t *vd)
3365 vdev_t *cvd, *pvd = vd->vdev_parent;
3367 vdev_remove_child(pvd, vd);
3368 vdev_compact_children(pvd);
3370 cvd = pvd->vdev_child[0];
3371 if (pvd->vdev_children == 1) {
3372 vdev_remove_parent(cvd);
3373 cvd->vdev_splitting = B_TRUE;
3375 vdev_propagate_state(cvd);
3379 vdev_deadman(vdev_t *vd)
3381 for (int c = 0; c < vd->vdev_children; c++) {
3382 vdev_t *cvd = vd->vdev_child[c];
3387 if (vd->vdev_ops->vdev_op_leaf) {
3388 vdev_queue_t *vq = &vd->vdev_queue;
3390 mutex_enter(&vq->vq_lock);
3391 if (avl_numnodes(&vq->vq_active_tree) > 0) {
3392 spa_t *spa = vd->vdev_spa;
3397 * Look at the head of all the pending queues,
3398 * if any I/O has been outstanding for longer than
3399 * the spa_deadman_synctime we panic the system.
3401 fio = avl_first(&vq->vq_active_tree);
3402 delta = gethrtime() - fio->io_timestamp;
3403 if (delta > spa_deadman_synctime(spa)) {
3404 zfs_dbgmsg("SLOW IO: zio timestamp %lluns, "
3405 "delta %lluns, last io %lluns",
3406 fio->io_timestamp, delta,
3407 vq->vq_io_complete_ts);
3408 fm_panic("I/O to pool '%s' appears to be "
3409 "hung on vdev guid %llu at '%s'.",
3411 (long long unsigned int) vd->vdev_guid,
3415 mutex_exit(&vq->vq_lock);