FreeBSD: Reduce copy_file_range() source lock to shared
[zfs.git] / module / zfs / vdev.c
blob983f444d79b0387730ccdfa3112d57d6d8aec632
1 /*
2 * CDDL HEADER START
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 https://opensource.org/licenses/CDDL-1.0.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2021 by Delphix. All rights reserved.
25 * Copyright 2017 Nexenta Systems, Inc.
26 * Copyright (c) 2014 Integros [integros.com]
27 * Copyright 2016 Toomas Soome <tsoome@me.com>
28 * Copyright 2017 Joyent, Inc.
29 * Copyright (c) 2017, Intel Corporation.
30 * Copyright (c) 2019, Datto Inc. All rights reserved.
31 * Copyright (c) 2021, Klara Inc.
32 * Copyright (c) 2021, 2023 Hewlett Packard Enterprise Development LP.
35 #include <sys/zfs_context.h>
36 #include <sys/fm/fs/zfs.h>
37 #include <sys/spa.h>
38 #include <sys/spa_impl.h>
39 #include <sys/bpobj.h>
40 #include <sys/dmu.h>
41 #include <sys/dmu_tx.h>
42 #include <sys/dsl_dir.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/vdev_rebuild.h>
45 #include <sys/vdev_draid.h>
46 #include <sys/uberblock_impl.h>
47 #include <sys/metaslab.h>
48 #include <sys/metaslab_impl.h>
49 #include <sys/space_map.h>
50 #include <sys/space_reftree.h>
51 #include <sys/zio.h>
52 #include <sys/zap.h>
53 #include <sys/fs/zfs.h>
54 #include <sys/arc.h>
55 #include <sys/zil.h>
56 #include <sys/dsl_scan.h>
57 #include <sys/vdev_raidz.h>
58 #include <sys/abd.h>
59 #include <sys/vdev_initialize.h>
60 #include <sys/vdev_trim.h>
61 #include <sys/vdev_raidz.h>
62 #include <sys/zvol.h>
63 #include <sys/zfs_ratelimit.h>
64 #include "zfs_prop.h"
67 * One metaslab from each (normal-class) vdev is used by the ZIL. These are
68 * called "embedded slog metaslabs", are referenced by vdev_log_mg, and are
69 * part of the spa_embedded_log_class. The metaslab with the most free space
70 * in each vdev is selected for this purpose when the pool is opened (or a
71 * vdev is added). See vdev_metaslab_init().
73 * Log blocks can be allocated from the following locations. Each one is tried
74 * in order until the allocation succeeds:
75 * 1. dedicated log vdevs, aka "slog" (spa_log_class)
76 * 2. embedded slog metaslabs (spa_embedded_log_class)
77 * 3. other metaslabs in normal vdevs (spa_normal_class)
79 * zfs_embedded_slog_min_ms disables the embedded slog if there are fewer
80 * than this number of metaslabs in the vdev. This ensures that we don't set
81 * aside an unreasonable amount of space for the ZIL. If set to less than
82 * 1 << (spa_slop_shift + 1), on small pools the usable space may be reduced
83 * (by more than 1<<spa_slop_shift) due to the embedded slog metaslab.
85 static uint_t zfs_embedded_slog_min_ms = 64;
87 /* default target for number of metaslabs per top-level vdev */
88 static uint_t zfs_vdev_default_ms_count = 200;
90 /* minimum number of metaslabs per top-level vdev */
91 static uint_t zfs_vdev_min_ms_count = 16;
93 /* practical upper limit of total metaslabs per top-level vdev */
94 static uint_t zfs_vdev_ms_count_limit = 1ULL << 17;
96 /* lower limit for metaslab size (512M) */
97 static uint_t zfs_vdev_default_ms_shift = 29;
99 /* upper limit for metaslab size (16G) */
100 static uint_t zfs_vdev_max_ms_shift = 34;
102 int vdev_validate_skip = B_FALSE;
105 * Since the DTL space map of a vdev is not expected to have a lot of
106 * entries, we default its block size to 4K.
108 int zfs_vdev_dtl_sm_blksz = (1 << 12);
111 * Rate limit slow IO (delay) events to this many per second.
113 static unsigned int zfs_slow_io_events_per_second = 20;
116 * Rate limit deadman "hung IO" events to this many per second.
118 static unsigned int zfs_deadman_events_per_second = 1;
121 * Rate limit direct write IO verify failures to this many per scond.
123 static unsigned int zfs_dio_write_verify_events_per_second = 20;
126 * Rate limit checksum events after this many checksum errors per second.
128 static unsigned int zfs_checksum_events_per_second = 20;
131 * Ignore errors during scrub/resilver. Allows to work around resilver
132 * upon import when there are pool errors.
134 static int zfs_scan_ignore_errors = 0;
137 * vdev-wide space maps that have lots of entries written to them at
138 * the end of each transaction can benefit from a higher I/O bandwidth
139 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
141 int zfs_vdev_standard_sm_blksz = (1 << 17);
144 * Tunable parameter for debugging or performance analysis. Setting this
145 * will cause pool corruption on power loss if a volatile out-of-order
146 * write cache is enabled.
148 int zfs_nocacheflush = 0;
151 * Maximum and minimum ashift values that can be automatically set based on
152 * vdev's physical ashift (disk's physical sector size). While ASHIFT_MAX
153 * is higher than the maximum value, it is intentionally limited here to not
154 * excessively impact pool space efficiency. Higher ashift values may still
155 * be forced by vdev logical ashift or by user via ashift property, but won't
156 * be set automatically as a performance optimization.
158 uint_t zfs_vdev_max_auto_ashift = 14;
159 uint_t zfs_vdev_min_auto_ashift = ASHIFT_MIN;
162 * VDEV checksum verification for Direct I/O writes. This is neccessary for
163 * Linux, because anonymous pages can not be placed under write protection
164 * during Direct I/O writes.
166 #if !defined(__FreeBSD__)
167 uint_t zfs_vdev_direct_write_verify = 1;
168 #else
169 uint_t zfs_vdev_direct_write_verify = 0;
170 #endif
172 void
173 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
175 va_list adx;
176 char buf[256];
178 va_start(adx, fmt);
179 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
180 va_end(adx);
182 if (vd->vdev_path != NULL) {
183 zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
184 vd->vdev_path, buf);
185 } else {
186 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
187 vd->vdev_ops->vdev_op_type,
188 (u_longlong_t)vd->vdev_id,
189 (u_longlong_t)vd->vdev_guid, buf);
193 void
194 vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
196 char state[20];
198 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
199 zfs_dbgmsg("%*svdev %llu: %s", indent, "",
200 (u_longlong_t)vd->vdev_id,
201 vd->vdev_ops->vdev_op_type);
202 return;
205 switch (vd->vdev_state) {
206 case VDEV_STATE_UNKNOWN:
207 (void) snprintf(state, sizeof (state), "unknown");
208 break;
209 case VDEV_STATE_CLOSED:
210 (void) snprintf(state, sizeof (state), "closed");
211 break;
212 case VDEV_STATE_OFFLINE:
213 (void) snprintf(state, sizeof (state), "offline");
214 break;
215 case VDEV_STATE_REMOVED:
216 (void) snprintf(state, sizeof (state), "removed");
217 break;
218 case VDEV_STATE_CANT_OPEN:
219 (void) snprintf(state, sizeof (state), "can't open");
220 break;
221 case VDEV_STATE_FAULTED:
222 (void) snprintf(state, sizeof (state), "faulted");
223 break;
224 case VDEV_STATE_DEGRADED:
225 (void) snprintf(state, sizeof (state), "degraded");
226 break;
227 case VDEV_STATE_HEALTHY:
228 (void) snprintf(state, sizeof (state), "healthy");
229 break;
230 default:
231 (void) snprintf(state, sizeof (state), "<state %u>",
232 (uint_t)vd->vdev_state);
235 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
236 "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type,
237 vd->vdev_islog ? " (log)" : "",
238 (u_longlong_t)vd->vdev_guid,
239 vd->vdev_path ? vd->vdev_path : "N/A", state);
241 for (uint64_t i = 0; i < vd->vdev_children; i++)
242 vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
246 * Virtual device management.
249 static vdev_ops_t *const vdev_ops_table[] = {
250 &vdev_root_ops,
251 &vdev_raidz_ops,
252 &vdev_draid_ops,
253 &vdev_draid_spare_ops,
254 &vdev_mirror_ops,
255 &vdev_replacing_ops,
256 &vdev_spare_ops,
257 &vdev_disk_ops,
258 &vdev_file_ops,
259 &vdev_missing_ops,
260 &vdev_hole_ops,
261 &vdev_indirect_ops,
262 NULL
266 * Given a vdev type, return the appropriate ops vector.
268 static vdev_ops_t *
269 vdev_getops(const char *type)
271 vdev_ops_t *ops, *const *opspp;
273 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
274 if (strcmp(ops->vdev_op_type, type) == 0)
275 break;
277 return (ops);
281 * Given a vdev and a metaslab class, find which metaslab group we're
282 * interested in. All vdevs may belong to two different metaslab classes.
283 * Dedicated slog devices use only the primary metaslab group, rather than a
284 * separate log group. For embedded slogs, the vdev_log_mg will be non-NULL.
286 metaslab_group_t *
287 vdev_get_mg(vdev_t *vd, metaslab_class_t *mc)
289 if (mc == spa_embedded_log_class(vd->vdev_spa) &&
290 vd->vdev_log_mg != NULL)
291 return (vd->vdev_log_mg);
292 else
293 return (vd->vdev_mg);
296 void
297 vdev_default_xlate(vdev_t *vd, const range_seg64_t *logical_rs,
298 range_seg64_t *physical_rs, range_seg64_t *remain_rs)
300 (void) vd, (void) remain_rs;
302 physical_rs->rs_start = logical_rs->rs_start;
303 physical_rs->rs_end = logical_rs->rs_end;
307 * Derive the enumerated allocation bias from string input.
308 * String origin is either the per-vdev zap or zpool(8).
310 static vdev_alloc_bias_t
311 vdev_derive_alloc_bias(const char *bias)
313 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
315 if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0)
316 alloc_bias = VDEV_BIAS_LOG;
317 else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
318 alloc_bias = VDEV_BIAS_SPECIAL;
319 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
320 alloc_bias = VDEV_BIAS_DEDUP;
322 return (alloc_bias);
326 * Default asize function: return the MAX of psize with the asize of
327 * all children. This is what's used by anything other than RAID-Z.
329 uint64_t
330 vdev_default_asize(vdev_t *vd, uint64_t psize, uint64_t txg)
332 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
333 uint64_t csize;
335 for (int c = 0; c < vd->vdev_children; c++) {
336 csize = vdev_psize_to_asize_txg(vd->vdev_child[c], psize, txg);
337 asize = MAX(asize, csize);
340 return (asize);
343 uint64_t
344 vdev_default_min_asize(vdev_t *vd)
346 return (vd->vdev_min_asize);
350 * Get the minimum allocatable size. We define the allocatable size as
351 * the vdev's asize rounded to the nearest metaslab. This allows us to
352 * replace or attach devices which don't have the same physical size but
353 * can still satisfy the same number of allocations.
355 uint64_t
356 vdev_get_min_asize(vdev_t *vd)
358 vdev_t *pvd = vd->vdev_parent;
361 * If our parent is NULL (inactive spare or cache) or is the root,
362 * just return our own asize.
364 if (pvd == NULL)
365 return (vd->vdev_asize);
368 * The top-level vdev just returns the allocatable size rounded
369 * to the nearest metaslab.
371 if (vd == vd->vdev_top)
372 return (P2ALIGN_TYPED(vd->vdev_asize, 1ULL << vd->vdev_ms_shift,
373 uint64_t));
375 return (pvd->vdev_ops->vdev_op_min_asize(pvd));
378 void
379 vdev_set_min_asize(vdev_t *vd)
381 vd->vdev_min_asize = vdev_get_min_asize(vd);
383 for (int c = 0; c < vd->vdev_children; c++)
384 vdev_set_min_asize(vd->vdev_child[c]);
388 * Get the minimal allocation size for the top-level vdev.
390 uint64_t
391 vdev_get_min_alloc(vdev_t *vd)
393 uint64_t min_alloc = 1ULL << vd->vdev_ashift;
395 if (vd->vdev_ops->vdev_op_min_alloc != NULL)
396 min_alloc = vd->vdev_ops->vdev_op_min_alloc(vd);
398 return (min_alloc);
402 * Get the parity level for a top-level vdev.
404 uint64_t
405 vdev_get_nparity(vdev_t *vd)
407 uint64_t nparity = 0;
409 if (vd->vdev_ops->vdev_op_nparity != NULL)
410 nparity = vd->vdev_ops->vdev_op_nparity(vd);
412 return (nparity);
415 static int
416 vdev_prop_get_int(vdev_t *vd, vdev_prop_t prop, uint64_t *value)
418 spa_t *spa = vd->vdev_spa;
419 objset_t *mos = spa->spa_meta_objset;
420 uint64_t objid;
421 int err;
423 if (vd->vdev_root_zap != 0) {
424 objid = vd->vdev_root_zap;
425 } else if (vd->vdev_top_zap != 0) {
426 objid = vd->vdev_top_zap;
427 } else if (vd->vdev_leaf_zap != 0) {
428 objid = vd->vdev_leaf_zap;
429 } else {
430 return (EINVAL);
433 err = zap_lookup(mos, objid, vdev_prop_to_name(prop),
434 sizeof (uint64_t), 1, value);
436 if (err == ENOENT)
437 *value = vdev_prop_default_numeric(prop);
439 return (err);
443 * Get the number of data disks for a top-level vdev.
445 uint64_t
446 vdev_get_ndisks(vdev_t *vd)
448 uint64_t ndisks = 1;
450 if (vd->vdev_ops->vdev_op_ndisks != NULL)
451 ndisks = vd->vdev_ops->vdev_op_ndisks(vd);
453 return (ndisks);
456 vdev_t *
457 vdev_lookup_top(spa_t *spa, uint64_t vdev)
459 vdev_t *rvd = spa->spa_root_vdev;
461 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
463 if (vdev < rvd->vdev_children) {
464 ASSERT(rvd->vdev_child[vdev] != NULL);
465 return (rvd->vdev_child[vdev]);
468 return (NULL);
471 vdev_t *
472 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
474 vdev_t *mvd;
476 if (vd->vdev_guid == guid)
477 return (vd);
479 for (int c = 0; c < vd->vdev_children; c++)
480 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
481 NULL)
482 return (mvd);
484 return (NULL);
487 static int
488 vdev_count_leaves_impl(vdev_t *vd)
490 int n = 0;
492 if (vd->vdev_ops->vdev_op_leaf)
493 return (1);
495 for (int c = 0; c < vd->vdev_children; c++)
496 n += vdev_count_leaves_impl(vd->vdev_child[c]);
498 return (n);
502 vdev_count_leaves(spa_t *spa)
504 int rc;
506 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
507 rc = vdev_count_leaves_impl(spa->spa_root_vdev);
508 spa_config_exit(spa, SCL_VDEV, FTAG);
510 return (rc);
513 void
514 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
516 size_t oldsize, newsize;
517 uint64_t id = cvd->vdev_id;
518 vdev_t **newchild;
520 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
521 ASSERT(cvd->vdev_parent == NULL);
523 cvd->vdev_parent = pvd;
525 if (pvd == NULL)
526 return;
528 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
530 oldsize = pvd->vdev_children * sizeof (vdev_t *);
531 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
532 newsize = pvd->vdev_children * sizeof (vdev_t *);
534 newchild = kmem_alloc(newsize, KM_SLEEP);
535 if (pvd->vdev_child != NULL) {
536 memcpy(newchild, pvd->vdev_child, oldsize);
537 kmem_free(pvd->vdev_child, oldsize);
540 pvd->vdev_child = newchild;
541 pvd->vdev_child[id] = cvd;
543 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
544 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
547 * Walk up all ancestors to update guid sum.
549 for (; pvd != NULL; pvd = pvd->vdev_parent)
550 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
552 if (cvd->vdev_ops->vdev_op_leaf) {
553 list_insert_head(&cvd->vdev_spa->spa_leaf_list, cvd);
554 cvd->vdev_spa->spa_leaf_list_gen++;
558 void
559 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
561 int c;
562 uint_t id = cvd->vdev_id;
564 ASSERT(cvd->vdev_parent == pvd);
566 if (pvd == NULL)
567 return;
569 ASSERT(id < pvd->vdev_children);
570 ASSERT(pvd->vdev_child[id] == cvd);
572 pvd->vdev_child[id] = NULL;
573 cvd->vdev_parent = NULL;
575 for (c = 0; c < pvd->vdev_children; c++)
576 if (pvd->vdev_child[c])
577 break;
579 if (c == pvd->vdev_children) {
580 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
581 pvd->vdev_child = NULL;
582 pvd->vdev_children = 0;
585 if (cvd->vdev_ops->vdev_op_leaf) {
586 spa_t *spa = cvd->vdev_spa;
587 list_remove(&spa->spa_leaf_list, cvd);
588 spa->spa_leaf_list_gen++;
592 * Walk up all ancestors to update guid sum.
594 for (; pvd != NULL; pvd = pvd->vdev_parent)
595 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
599 * Remove any holes in the child array.
601 void
602 vdev_compact_children(vdev_t *pvd)
604 vdev_t **newchild, *cvd;
605 int oldc = pvd->vdev_children;
606 int newc;
608 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
610 if (oldc == 0)
611 return;
613 for (int c = newc = 0; c < oldc; c++)
614 if (pvd->vdev_child[c])
615 newc++;
617 if (newc > 0) {
618 newchild = kmem_zalloc(newc * sizeof (vdev_t *), KM_SLEEP);
620 for (int c = newc = 0; c < oldc; c++) {
621 if ((cvd = pvd->vdev_child[c]) != NULL) {
622 newchild[newc] = cvd;
623 cvd->vdev_id = newc++;
626 } else {
627 newchild = NULL;
630 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
631 pvd->vdev_child = newchild;
632 pvd->vdev_children = newc;
636 * Allocate and minimally initialize a vdev_t.
638 vdev_t *
639 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
641 vdev_t *vd;
642 vdev_indirect_config_t *vic;
644 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
645 vic = &vd->vdev_indirect_config;
647 if (spa->spa_root_vdev == NULL) {
648 ASSERT(ops == &vdev_root_ops);
649 spa->spa_root_vdev = vd;
650 spa->spa_load_guid = spa_generate_guid(NULL);
653 if (guid == 0 && ops != &vdev_hole_ops) {
654 if (spa->spa_root_vdev == vd) {
656 * The root vdev's guid will also be the pool guid,
657 * which must be unique among all pools.
659 guid = spa_generate_guid(NULL);
660 } else {
662 * Any other vdev's guid must be unique within the pool.
664 guid = spa_generate_guid(spa);
666 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
669 vd->vdev_spa = spa;
670 vd->vdev_id = id;
671 vd->vdev_guid = guid;
672 vd->vdev_guid_sum = guid;
673 vd->vdev_ops = ops;
674 vd->vdev_state = VDEV_STATE_CLOSED;
675 vd->vdev_ishole = (ops == &vdev_hole_ops);
676 vic->vic_prev_indirect_vdev = UINT64_MAX;
678 rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
679 mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
680 vd->vdev_obsolete_segments = range_tree_create(NULL, RANGE_SEG64, NULL,
681 0, 0);
684 * Initialize rate limit structs for events. We rate limit ZIO delay
685 * and checksum events so that we don't overwhelm ZED with thousands
686 * of events when a disk is acting up.
688 zfs_ratelimit_init(&vd->vdev_delay_rl, &zfs_slow_io_events_per_second,
690 zfs_ratelimit_init(&vd->vdev_deadman_rl, &zfs_deadman_events_per_second,
692 zfs_ratelimit_init(&vd->vdev_dio_verify_rl,
693 &zfs_dio_write_verify_events_per_second, 1);
694 zfs_ratelimit_init(&vd->vdev_checksum_rl,
695 &zfs_checksum_events_per_second, 1);
698 * Default Thresholds for tuning ZED
700 vd->vdev_checksum_n = vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_N);
701 vd->vdev_checksum_t = vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_T);
702 vd->vdev_io_n = vdev_prop_default_numeric(VDEV_PROP_IO_N);
703 vd->vdev_io_t = vdev_prop_default_numeric(VDEV_PROP_IO_T);
704 vd->vdev_slow_io_n = vdev_prop_default_numeric(VDEV_PROP_SLOW_IO_N);
705 vd->vdev_slow_io_t = vdev_prop_default_numeric(VDEV_PROP_SLOW_IO_T);
707 list_link_init(&vd->vdev_config_dirty_node);
708 list_link_init(&vd->vdev_state_dirty_node);
709 list_link_init(&vd->vdev_initialize_node);
710 list_link_init(&vd->vdev_leaf_node);
711 list_link_init(&vd->vdev_trim_node);
713 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_NOLOCKDEP, NULL);
714 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
715 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
716 mutex_init(&vd->vdev_scan_io_queue_lock, NULL, MUTEX_DEFAULT, NULL);
718 mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL);
719 mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL);
720 cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL);
721 cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL);
723 mutex_init(&vd->vdev_trim_lock, NULL, MUTEX_DEFAULT, NULL);
724 mutex_init(&vd->vdev_autotrim_lock, NULL, MUTEX_DEFAULT, NULL);
725 mutex_init(&vd->vdev_trim_io_lock, NULL, MUTEX_DEFAULT, NULL);
726 cv_init(&vd->vdev_trim_cv, NULL, CV_DEFAULT, NULL);
727 cv_init(&vd->vdev_autotrim_cv, NULL, CV_DEFAULT, NULL);
728 cv_init(&vd->vdev_autotrim_kick_cv, NULL, CV_DEFAULT, NULL);
729 cv_init(&vd->vdev_trim_io_cv, NULL, CV_DEFAULT, NULL);
731 mutex_init(&vd->vdev_rebuild_lock, NULL, MUTEX_DEFAULT, NULL);
732 cv_init(&vd->vdev_rebuild_cv, NULL, CV_DEFAULT, NULL);
734 for (int t = 0; t < DTL_TYPES; t++) {
735 vd->vdev_dtl[t] = range_tree_create(NULL, RANGE_SEG64, NULL, 0,
739 txg_list_create(&vd->vdev_ms_list, spa,
740 offsetof(struct metaslab, ms_txg_node));
741 txg_list_create(&vd->vdev_dtl_list, spa,
742 offsetof(struct vdev, vdev_dtl_node));
743 vd->vdev_stat.vs_timestamp = gethrtime();
744 vdev_queue_init(vd);
746 return (vd);
750 * Allocate a new vdev. The 'alloctype' is used to control whether we are
751 * creating a new vdev or loading an existing one - the behavior is slightly
752 * different for each case.
755 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
756 int alloctype)
758 vdev_ops_t *ops;
759 const char *type;
760 uint64_t guid = 0, islog;
761 vdev_t *vd;
762 vdev_indirect_config_t *vic;
763 const char *tmp = NULL;
764 int rc;
765 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
766 boolean_t top_level = (parent && !parent->vdev_parent);
768 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
770 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
771 return (SET_ERROR(EINVAL));
773 if ((ops = vdev_getops(type)) == NULL)
774 return (SET_ERROR(EINVAL));
777 * If this is a load, get the vdev guid from the nvlist.
778 * Otherwise, vdev_alloc_common() will generate one for us.
780 if (alloctype == VDEV_ALLOC_LOAD) {
781 uint64_t label_id;
783 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
784 label_id != id)
785 return (SET_ERROR(EINVAL));
787 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
788 return (SET_ERROR(EINVAL));
789 } else if (alloctype == VDEV_ALLOC_SPARE) {
790 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
791 return (SET_ERROR(EINVAL));
792 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
793 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
794 return (SET_ERROR(EINVAL));
795 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
796 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
797 return (SET_ERROR(EINVAL));
801 * The first allocated vdev must be of type 'root'.
803 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
804 return (SET_ERROR(EINVAL));
807 * Determine whether we're a log vdev.
809 islog = 0;
810 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
811 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
812 return (SET_ERROR(ENOTSUP));
814 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
815 return (SET_ERROR(ENOTSUP));
817 if (top_level && alloctype == VDEV_ALLOC_ADD) {
818 const char *bias;
821 * If creating a top-level vdev, check for allocation
822 * classes input.
824 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
825 &bias) == 0) {
826 alloc_bias = vdev_derive_alloc_bias(bias);
828 /* spa_vdev_add() expects feature to be enabled */
829 if (spa->spa_load_state != SPA_LOAD_CREATE &&
830 !spa_feature_is_enabled(spa,
831 SPA_FEATURE_ALLOCATION_CLASSES)) {
832 return (SET_ERROR(ENOTSUP));
836 /* spa_vdev_add() expects feature to be enabled */
837 if (ops == &vdev_draid_ops &&
838 spa->spa_load_state != SPA_LOAD_CREATE &&
839 !spa_feature_is_enabled(spa, SPA_FEATURE_DRAID)) {
840 return (SET_ERROR(ENOTSUP));
845 * Initialize the vdev specific data. This is done before calling
846 * vdev_alloc_common() since it may fail and this simplifies the
847 * error reporting and cleanup code paths.
849 void *tsd = NULL;
850 if (ops->vdev_op_init != NULL) {
851 rc = ops->vdev_op_init(spa, nv, &tsd);
852 if (rc != 0) {
853 return (rc);
857 vd = vdev_alloc_common(spa, id, guid, ops);
858 vd->vdev_tsd = tsd;
859 vd->vdev_islog = islog;
861 if (top_level && alloc_bias != VDEV_BIAS_NONE)
862 vd->vdev_alloc_bias = alloc_bias;
864 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &tmp) == 0)
865 vd->vdev_path = spa_strdup(tmp);
868 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
869 * fault on a vdev and want it to persist across imports (like with
870 * zpool offline -f).
872 rc = nvlist_lookup_string(nv, ZPOOL_CONFIG_AUX_STATE, &tmp);
873 if (rc == 0 && tmp != NULL && strcmp(tmp, "external") == 0) {
874 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL;
875 vd->vdev_faulted = 1;
876 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
879 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &tmp) == 0)
880 vd->vdev_devid = spa_strdup(tmp);
881 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, &tmp) == 0)
882 vd->vdev_physpath = spa_strdup(tmp);
884 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
885 &tmp) == 0)
886 vd->vdev_enc_sysfs_path = spa_strdup(tmp);
888 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &tmp) == 0)
889 vd->vdev_fru = spa_strdup(tmp);
892 * Set the whole_disk property. If it's not specified, leave the value
893 * as -1.
895 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
896 &vd->vdev_wholedisk) != 0)
897 vd->vdev_wholedisk = -1ULL;
899 vic = &vd->vdev_indirect_config;
901 ASSERT0(vic->vic_mapping_object);
902 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
903 &vic->vic_mapping_object);
904 ASSERT0(vic->vic_births_object);
905 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
906 &vic->vic_births_object);
907 ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
908 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
909 &vic->vic_prev_indirect_vdev);
912 * Look for the 'not present' flag. This will only be set if the device
913 * was not present at the time of import.
915 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
916 &vd->vdev_not_present);
919 * Get the alignment requirement. Ignore pool ashift for vdev
920 * attach case.
922 if (alloctype != VDEV_ALLOC_ATTACH) {
923 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT,
924 &vd->vdev_ashift);
925 } else {
926 vd->vdev_attaching = B_TRUE;
930 * Retrieve the vdev creation time.
932 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
933 &vd->vdev_crtxg);
935 if (vd->vdev_ops == &vdev_root_ops &&
936 (alloctype == VDEV_ALLOC_LOAD ||
937 alloctype == VDEV_ALLOC_SPLIT ||
938 alloctype == VDEV_ALLOC_ROOTPOOL)) {
939 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_ROOT_ZAP,
940 &vd->vdev_root_zap);
944 * If we're a top-level vdev, try to load the allocation parameters.
946 if (top_level &&
947 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
948 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
949 &vd->vdev_ms_array);
950 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
951 &vd->vdev_ms_shift);
952 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
953 &vd->vdev_asize);
954 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NONALLOCATING,
955 &vd->vdev_noalloc);
956 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
957 &vd->vdev_removing);
958 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
959 &vd->vdev_top_zap);
960 vd->vdev_rz_expanding = nvlist_exists(nv,
961 ZPOOL_CONFIG_RAIDZ_EXPANDING);
962 } else {
963 ASSERT0(vd->vdev_top_zap);
966 if (top_level && alloctype != VDEV_ALLOC_ATTACH) {
967 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
968 alloctype == VDEV_ALLOC_ADD ||
969 alloctype == VDEV_ALLOC_SPLIT ||
970 alloctype == VDEV_ALLOC_ROOTPOOL);
971 /* Note: metaslab_group_create() is now deferred */
974 if (vd->vdev_ops->vdev_op_leaf &&
975 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
976 (void) nvlist_lookup_uint64(nv,
977 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
978 } else {
979 ASSERT0(vd->vdev_leaf_zap);
983 * If we're a leaf vdev, try to load the DTL object and other state.
986 if (vd->vdev_ops->vdev_op_leaf &&
987 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
988 alloctype == VDEV_ALLOC_ROOTPOOL)) {
989 if (alloctype == VDEV_ALLOC_LOAD) {
990 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
991 &vd->vdev_dtl_object);
992 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
993 &vd->vdev_unspare);
996 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
997 uint64_t spare = 0;
999 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
1000 &spare) == 0 && spare)
1001 spa_spare_add(vd);
1004 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
1005 &vd->vdev_offline);
1007 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
1008 &vd->vdev_resilver_txg);
1010 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REBUILD_TXG,
1011 &vd->vdev_rebuild_txg);
1013 if (nvlist_exists(nv, ZPOOL_CONFIG_RESILVER_DEFER))
1014 vdev_defer_resilver(vd);
1017 * In general, when importing a pool we want to ignore the
1018 * persistent fault state, as the diagnosis made on another
1019 * system may not be valid in the current context. The only
1020 * exception is if we forced a vdev to a persistently faulted
1021 * state with 'zpool offline -f'. The persistent fault will
1022 * remain across imports until cleared.
1024 * Local vdevs will remain in the faulted state.
1026 if (spa_load_state(spa) == SPA_LOAD_OPEN ||
1027 spa_load_state(spa) == SPA_LOAD_IMPORT) {
1028 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
1029 &vd->vdev_faulted);
1030 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
1031 &vd->vdev_degraded);
1032 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
1033 &vd->vdev_removed);
1035 if (vd->vdev_faulted || vd->vdev_degraded) {
1036 const char *aux;
1038 vd->vdev_label_aux =
1039 VDEV_AUX_ERR_EXCEEDED;
1040 if (nvlist_lookup_string(nv,
1041 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
1042 strcmp(aux, "external") == 0)
1043 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
1044 else
1045 vd->vdev_faulted = 0ULL;
1051 * Add ourselves to the parent's list of children.
1053 vdev_add_child(parent, vd);
1055 *vdp = vd;
1057 return (0);
1060 void
1061 vdev_free(vdev_t *vd)
1063 spa_t *spa = vd->vdev_spa;
1065 ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
1066 ASSERT3P(vd->vdev_trim_thread, ==, NULL);
1067 ASSERT3P(vd->vdev_autotrim_thread, ==, NULL);
1068 ASSERT3P(vd->vdev_rebuild_thread, ==, NULL);
1071 * Scan queues are normally destroyed at the end of a scan. If the
1072 * queue exists here, that implies the vdev is being removed while
1073 * the scan is still running.
1075 if (vd->vdev_scan_io_queue != NULL) {
1076 mutex_enter(&vd->vdev_scan_io_queue_lock);
1077 dsl_scan_io_queue_destroy(vd->vdev_scan_io_queue);
1078 vd->vdev_scan_io_queue = NULL;
1079 mutex_exit(&vd->vdev_scan_io_queue_lock);
1083 * vdev_free() implies closing the vdev first. This is simpler than
1084 * trying to ensure complicated semantics for all callers.
1086 vdev_close(vd);
1088 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
1089 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
1092 * Free all children.
1094 for (int c = 0; c < vd->vdev_children; c++)
1095 vdev_free(vd->vdev_child[c]);
1097 ASSERT(vd->vdev_child == NULL);
1098 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
1100 if (vd->vdev_ops->vdev_op_fini != NULL)
1101 vd->vdev_ops->vdev_op_fini(vd);
1104 * Discard allocation state.
1106 if (vd->vdev_mg != NULL) {
1107 vdev_metaslab_fini(vd);
1108 metaslab_group_destroy(vd->vdev_mg);
1109 vd->vdev_mg = NULL;
1111 if (vd->vdev_log_mg != NULL) {
1112 ASSERT0(vd->vdev_ms_count);
1113 metaslab_group_destroy(vd->vdev_log_mg);
1114 vd->vdev_log_mg = NULL;
1117 ASSERT0(vd->vdev_stat.vs_space);
1118 ASSERT0(vd->vdev_stat.vs_dspace);
1119 ASSERT0(vd->vdev_stat.vs_alloc);
1122 * Remove this vdev from its parent's child list.
1124 vdev_remove_child(vd->vdev_parent, vd);
1126 ASSERT(vd->vdev_parent == NULL);
1127 ASSERT(!list_link_active(&vd->vdev_leaf_node));
1130 * Clean up vdev structure.
1132 vdev_queue_fini(vd);
1134 if (vd->vdev_path)
1135 spa_strfree(vd->vdev_path);
1136 if (vd->vdev_devid)
1137 spa_strfree(vd->vdev_devid);
1138 if (vd->vdev_physpath)
1139 spa_strfree(vd->vdev_physpath);
1141 if (vd->vdev_enc_sysfs_path)
1142 spa_strfree(vd->vdev_enc_sysfs_path);
1144 if (vd->vdev_fru)
1145 spa_strfree(vd->vdev_fru);
1147 if (vd->vdev_isspare)
1148 spa_spare_remove(vd);
1149 if (vd->vdev_isl2cache)
1150 spa_l2cache_remove(vd);
1152 txg_list_destroy(&vd->vdev_ms_list);
1153 txg_list_destroy(&vd->vdev_dtl_list);
1155 mutex_enter(&vd->vdev_dtl_lock);
1156 space_map_close(vd->vdev_dtl_sm);
1157 for (int t = 0; t < DTL_TYPES; t++) {
1158 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
1159 range_tree_destroy(vd->vdev_dtl[t]);
1161 mutex_exit(&vd->vdev_dtl_lock);
1163 EQUIV(vd->vdev_indirect_births != NULL,
1164 vd->vdev_indirect_mapping != NULL);
1165 if (vd->vdev_indirect_births != NULL) {
1166 vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
1167 vdev_indirect_births_close(vd->vdev_indirect_births);
1170 if (vd->vdev_obsolete_sm != NULL) {
1171 ASSERT(vd->vdev_removing ||
1172 vd->vdev_ops == &vdev_indirect_ops);
1173 space_map_close(vd->vdev_obsolete_sm);
1174 vd->vdev_obsolete_sm = NULL;
1176 range_tree_destroy(vd->vdev_obsolete_segments);
1177 rw_destroy(&vd->vdev_indirect_rwlock);
1178 mutex_destroy(&vd->vdev_obsolete_lock);
1180 mutex_destroy(&vd->vdev_dtl_lock);
1181 mutex_destroy(&vd->vdev_stat_lock);
1182 mutex_destroy(&vd->vdev_probe_lock);
1183 mutex_destroy(&vd->vdev_scan_io_queue_lock);
1185 mutex_destroy(&vd->vdev_initialize_lock);
1186 mutex_destroy(&vd->vdev_initialize_io_lock);
1187 cv_destroy(&vd->vdev_initialize_io_cv);
1188 cv_destroy(&vd->vdev_initialize_cv);
1190 mutex_destroy(&vd->vdev_trim_lock);
1191 mutex_destroy(&vd->vdev_autotrim_lock);
1192 mutex_destroy(&vd->vdev_trim_io_lock);
1193 cv_destroy(&vd->vdev_trim_cv);
1194 cv_destroy(&vd->vdev_autotrim_cv);
1195 cv_destroy(&vd->vdev_autotrim_kick_cv);
1196 cv_destroy(&vd->vdev_trim_io_cv);
1198 mutex_destroy(&vd->vdev_rebuild_lock);
1199 cv_destroy(&vd->vdev_rebuild_cv);
1201 zfs_ratelimit_fini(&vd->vdev_delay_rl);
1202 zfs_ratelimit_fini(&vd->vdev_deadman_rl);
1203 zfs_ratelimit_fini(&vd->vdev_dio_verify_rl);
1204 zfs_ratelimit_fini(&vd->vdev_checksum_rl);
1206 if (vd == spa->spa_root_vdev)
1207 spa->spa_root_vdev = NULL;
1209 kmem_free(vd, sizeof (vdev_t));
1213 * Transfer top-level vdev state from svd to tvd.
1215 static void
1216 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
1218 spa_t *spa = svd->vdev_spa;
1219 metaslab_t *msp;
1220 vdev_t *vd;
1221 int t;
1223 ASSERT(tvd == tvd->vdev_top);
1225 tvd->vdev_ms_array = svd->vdev_ms_array;
1226 tvd->vdev_ms_shift = svd->vdev_ms_shift;
1227 tvd->vdev_ms_count = svd->vdev_ms_count;
1228 tvd->vdev_top_zap = svd->vdev_top_zap;
1230 svd->vdev_ms_array = 0;
1231 svd->vdev_ms_shift = 0;
1232 svd->vdev_ms_count = 0;
1233 svd->vdev_top_zap = 0;
1235 if (tvd->vdev_mg)
1236 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
1237 if (tvd->vdev_log_mg)
1238 ASSERT3P(tvd->vdev_log_mg, ==, svd->vdev_log_mg);
1239 tvd->vdev_mg = svd->vdev_mg;
1240 tvd->vdev_log_mg = svd->vdev_log_mg;
1241 tvd->vdev_ms = svd->vdev_ms;
1243 svd->vdev_mg = NULL;
1244 svd->vdev_log_mg = NULL;
1245 svd->vdev_ms = NULL;
1247 if (tvd->vdev_mg != NULL)
1248 tvd->vdev_mg->mg_vd = tvd;
1249 if (tvd->vdev_log_mg != NULL)
1250 tvd->vdev_log_mg->mg_vd = tvd;
1252 tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm;
1253 svd->vdev_checkpoint_sm = NULL;
1255 tvd->vdev_alloc_bias = svd->vdev_alloc_bias;
1256 svd->vdev_alloc_bias = VDEV_BIAS_NONE;
1258 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
1259 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
1260 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
1262 svd->vdev_stat.vs_alloc = 0;
1263 svd->vdev_stat.vs_space = 0;
1264 svd->vdev_stat.vs_dspace = 0;
1267 * State which may be set on a top-level vdev that's in the
1268 * process of being removed.
1270 ASSERT0(tvd->vdev_indirect_config.vic_births_object);
1271 ASSERT0(tvd->vdev_indirect_config.vic_mapping_object);
1272 ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL);
1273 ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL);
1274 ASSERT3P(tvd->vdev_indirect_births, ==, NULL);
1275 ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL);
1276 ASSERT0(tvd->vdev_noalloc);
1277 ASSERT0(tvd->vdev_removing);
1278 ASSERT0(tvd->vdev_rebuilding);
1279 tvd->vdev_noalloc = svd->vdev_noalloc;
1280 tvd->vdev_removing = svd->vdev_removing;
1281 tvd->vdev_rebuilding = svd->vdev_rebuilding;
1282 tvd->vdev_rebuild_config = svd->vdev_rebuild_config;
1283 tvd->vdev_indirect_config = svd->vdev_indirect_config;
1284 tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping;
1285 tvd->vdev_indirect_births = svd->vdev_indirect_births;
1286 range_tree_swap(&svd->vdev_obsolete_segments,
1287 &tvd->vdev_obsolete_segments);
1288 tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm;
1289 svd->vdev_indirect_config.vic_mapping_object = 0;
1290 svd->vdev_indirect_config.vic_births_object = 0;
1291 svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL;
1292 svd->vdev_indirect_mapping = NULL;
1293 svd->vdev_indirect_births = NULL;
1294 svd->vdev_obsolete_sm = NULL;
1295 svd->vdev_noalloc = 0;
1296 svd->vdev_removing = 0;
1297 svd->vdev_rebuilding = 0;
1299 for (t = 0; t < TXG_SIZE; t++) {
1300 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
1301 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
1302 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
1303 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
1304 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
1305 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
1308 if (list_link_active(&svd->vdev_config_dirty_node)) {
1309 vdev_config_clean(svd);
1310 vdev_config_dirty(tvd);
1313 if (list_link_active(&svd->vdev_state_dirty_node)) {
1314 vdev_state_clean(svd);
1315 vdev_state_dirty(tvd);
1318 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
1319 svd->vdev_deflate_ratio = 0;
1321 tvd->vdev_islog = svd->vdev_islog;
1322 svd->vdev_islog = 0;
1324 dsl_scan_io_queue_vdev_xfer(svd, tvd);
1327 static void
1328 vdev_top_update(vdev_t *tvd, vdev_t *vd)
1330 if (vd == NULL)
1331 return;
1333 vd->vdev_top = tvd;
1335 for (int c = 0; c < vd->vdev_children; c++)
1336 vdev_top_update(tvd, vd->vdev_child[c]);
1340 * Add a mirror/replacing vdev above an existing vdev. There is no need to
1341 * call .vdev_op_init() since mirror/replacing vdevs do not have private state.
1343 vdev_t *
1344 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
1346 spa_t *spa = cvd->vdev_spa;
1347 vdev_t *pvd = cvd->vdev_parent;
1348 vdev_t *mvd;
1350 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1352 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
1354 mvd->vdev_asize = cvd->vdev_asize;
1355 mvd->vdev_min_asize = cvd->vdev_min_asize;
1356 mvd->vdev_max_asize = cvd->vdev_max_asize;
1357 mvd->vdev_psize = cvd->vdev_psize;
1358 mvd->vdev_ashift = cvd->vdev_ashift;
1359 mvd->vdev_logical_ashift = cvd->vdev_logical_ashift;
1360 mvd->vdev_physical_ashift = cvd->vdev_physical_ashift;
1361 mvd->vdev_state = cvd->vdev_state;
1362 mvd->vdev_crtxg = cvd->vdev_crtxg;
1364 vdev_remove_child(pvd, cvd);
1365 vdev_add_child(pvd, mvd);
1366 cvd->vdev_id = mvd->vdev_children;
1367 vdev_add_child(mvd, cvd);
1368 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1370 if (mvd == mvd->vdev_top)
1371 vdev_top_transfer(cvd, mvd);
1373 return (mvd);
1377 * Remove a 1-way mirror/replacing vdev from the tree.
1379 void
1380 vdev_remove_parent(vdev_t *cvd)
1382 vdev_t *mvd = cvd->vdev_parent;
1383 vdev_t *pvd = mvd->vdev_parent;
1385 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1387 ASSERT(mvd->vdev_children == 1);
1388 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
1389 mvd->vdev_ops == &vdev_replacing_ops ||
1390 mvd->vdev_ops == &vdev_spare_ops);
1391 cvd->vdev_ashift = mvd->vdev_ashift;
1392 cvd->vdev_logical_ashift = mvd->vdev_logical_ashift;
1393 cvd->vdev_physical_ashift = mvd->vdev_physical_ashift;
1394 vdev_remove_child(mvd, cvd);
1395 vdev_remove_child(pvd, mvd);
1398 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1399 * Otherwise, we could have detached an offline device, and when we
1400 * go to import the pool we'll think we have two top-level vdevs,
1401 * instead of a different version of the same top-level vdev.
1403 if (mvd->vdev_top == mvd) {
1404 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
1405 cvd->vdev_orig_guid = cvd->vdev_guid;
1406 cvd->vdev_guid += guid_delta;
1407 cvd->vdev_guid_sum += guid_delta;
1410 * If pool not set for autoexpand, we need to also preserve
1411 * mvd's asize to prevent automatic expansion of cvd.
1412 * Otherwise if we are adjusting the mirror by attaching and
1413 * detaching children of non-uniform sizes, the mirror could
1414 * autoexpand, unexpectedly requiring larger devices to
1415 * re-establish the mirror.
1417 if (!cvd->vdev_spa->spa_autoexpand)
1418 cvd->vdev_asize = mvd->vdev_asize;
1420 cvd->vdev_id = mvd->vdev_id;
1421 vdev_add_child(pvd, cvd);
1422 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1424 if (cvd == cvd->vdev_top)
1425 vdev_top_transfer(mvd, cvd);
1427 ASSERT(mvd->vdev_children == 0);
1428 vdev_free(mvd);
1432 * Choose GCD for spa_gcd_alloc.
1434 static uint64_t
1435 vdev_gcd(uint64_t a, uint64_t b)
1437 while (b != 0) {
1438 uint64_t t = b;
1439 b = a % b;
1440 a = t;
1442 return (a);
1446 * Set spa_min_alloc and spa_gcd_alloc.
1448 static void
1449 vdev_spa_set_alloc(spa_t *spa, uint64_t min_alloc)
1451 if (min_alloc < spa->spa_min_alloc)
1452 spa->spa_min_alloc = min_alloc;
1453 if (spa->spa_gcd_alloc == INT_MAX) {
1454 spa->spa_gcd_alloc = min_alloc;
1455 } else {
1456 spa->spa_gcd_alloc = vdev_gcd(min_alloc,
1457 spa->spa_gcd_alloc);
1461 void
1462 vdev_metaslab_group_create(vdev_t *vd)
1464 spa_t *spa = vd->vdev_spa;
1467 * metaslab_group_create was delayed until allocation bias was available
1469 if (vd->vdev_mg == NULL) {
1470 metaslab_class_t *mc;
1472 if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE)
1473 vd->vdev_alloc_bias = VDEV_BIAS_LOG;
1475 ASSERT3U(vd->vdev_islog, ==,
1476 (vd->vdev_alloc_bias == VDEV_BIAS_LOG));
1478 switch (vd->vdev_alloc_bias) {
1479 case VDEV_BIAS_LOG:
1480 mc = spa_log_class(spa);
1481 break;
1482 case VDEV_BIAS_SPECIAL:
1483 mc = spa_special_class(spa);
1484 break;
1485 case VDEV_BIAS_DEDUP:
1486 mc = spa_dedup_class(spa);
1487 break;
1488 default:
1489 mc = spa_normal_class(spa);
1492 vd->vdev_mg = metaslab_group_create(mc, vd,
1493 spa->spa_alloc_count);
1495 if (!vd->vdev_islog) {
1496 vd->vdev_log_mg = metaslab_group_create(
1497 spa_embedded_log_class(spa), vd, 1);
1501 * The spa ashift min/max only apply for the normal metaslab
1502 * class. Class destination is late binding so ashift boundary
1503 * setting had to wait until now.
1505 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1506 mc == spa_normal_class(spa) && vd->vdev_aux == NULL) {
1507 if (vd->vdev_ashift > spa->spa_max_ashift)
1508 spa->spa_max_ashift = vd->vdev_ashift;
1509 if (vd->vdev_ashift < spa->spa_min_ashift)
1510 spa->spa_min_ashift = vd->vdev_ashift;
1512 uint64_t min_alloc = vdev_get_min_alloc(vd);
1513 vdev_spa_set_alloc(spa, min_alloc);
1519 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
1521 spa_t *spa = vd->vdev_spa;
1522 uint64_t oldc = vd->vdev_ms_count;
1523 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
1524 metaslab_t **mspp;
1525 int error;
1526 boolean_t expanding = (oldc != 0);
1528 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1531 * This vdev is not being allocated from yet or is a hole.
1533 if (vd->vdev_ms_shift == 0)
1534 return (0);
1536 ASSERT(!vd->vdev_ishole);
1538 ASSERT(oldc <= newc);
1540 mspp = vmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1542 if (expanding) {
1543 memcpy(mspp, vd->vdev_ms, oldc * sizeof (*mspp));
1544 vmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1547 vd->vdev_ms = mspp;
1548 vd->vdev_ms_count = newc;
1550 for (uint64_t m = oldc; m < newc; m++) {
1551 uint64_t object = 0;
1553 * vdev_ms_array may be 0 if we are creating the "fake"
1554 * metaslabs for an indirect vdev for zdb's leak detection.
1555 * See zdb_leak_init().
1557 if (txg == 0 && vd->vdev_ms_array != 0) {
1558 error = dmu_read(spa->spa_meta_objset,
1559 vd->vdev_ms_array,
1560 m * sizeof (uint64_t), sizeof (uint64_t), &object,
1561 DMU_READ_PREFETCH);
1562 if (error != 0) {
1563 vdev_dbgmsg(vd, "unable to read the metaslab "
1564 "array [error=%d]", error);
1565 return (error);
1569 error = metaslab_init(vd->vdev_mg, m, object, txg,
1570 &(vd->vdev_ms[m]));
1571 if (error != 0) {
1572 vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
1573 error);
1574 return (error);
1579 * Find the emptiest metaslab on the vdev and mark it for use for
1580 * embedded slog by moving it from the regular to the log metaslab
1581 * group.
1583 if (vd->vdev_mg->mg_class == spa_normal_class(spa) &&
1584 vd->vdev_ms_count > zfs_embedded_slog_min_ms &&
1585 avl_is_empty(&vd->vdev_log_mg->mg_metaslab_tree)) {
1586 uint64_t slog_msid = 0;
1587 uint64_t smallest = UINT64_MAX;
1590 * Note, we only search the new metaslabs, because the old
1591 * (pre-existing) ones may be active (e.g. have non-empty
1592 * range_tree's), and we don't move them to the new
1593 * metaslab_t.
1595 for (uint64_t m = oldc; m < newc; m++) {
1596 uint64_t alloc =
1597 space_map_allocated(vd->vdev_ms[m]->ms_sm);
1598 if (alloc < smallest) {
1599 slog_msid = m;
1600 smallest = alloc;
1603 metaslab_t *slog_ms = vd->vdev_ms[slog_msid];
1605 * The metaslab was marked as dirty at the end of
1606 * metaslab_init(). Remove it from the dirty list so that we
1607 * can uninitialize and reinitialize it to the new class.
1609 if (txg != 0) {
1610 (void) txg_list_remove_this(&vd->vdev_ms_list,
1611 slog_ms, txg);
1613 uint64_t sm_obj = space_map_object(slog_ms->ms_sm);
1614 metaslab_fini(slog_ms);
1615 VERIFY0(metaslab_init(vd->vdev_log_mg, slog_msid, sm_obj, txg,
1616 &vd->vdev_ms[slog_msid]));
1619 if (txg == 0)
1620 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1623 * If the vdev is marked as non-allocating then don't
1624 * activate the metaslabs since we want to ensure that
1625 * no allocations are performed on this device.
1627 if (vd->vdev_noalloc) {
1628 /* track non-allocating vdev space */
1629 spa->spa_nonallocating_dspace += spa_deflate(spa) ?
1630 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
1631 } else if (!expanding) {
1632 metaslab_group_activate(vd->vdev_mg);
1633 if (vd->vdev_log_mg != NULL)
1634 metaslab_group_activate(vd->vdev_log_mg);
1637 if (txg == 0)
1638 spa_config_exit(spa, SCL_ALLOC, FTAG);
1640 return (0);
1643 void
1644 vdev_metaslab_fini(vdev_t *vd)
1646 if (vd->vdev_checkpoint_sm != NULL) {
1647 ASSERT(spa_feature_is_active(vd->vdev_spa,
1648 SPA_FEATURE_POOL_CHECKPOINT));
1649 space_map_close(vd->vdev_checkpoint_sm);
1651 * Even though we close the space map, we need to set its
1652 * pointer to NULL. The reason is that vdev_metaslab_fini()
1653 * may be called multiple times for certain operations
1654 * (i.e. when destroying a pool) so we need to ensure that
1655 * this clause never executes twice. This logic is similar
1656 * to the one used for the vdev_ms clause below.
1658 vd->vdev_checkpoint_sm = NULL;
1661 if (vd->vdev_ms != NULL) {
1662 metaslab_group_t *mg = vd->vdev_mg;
1664 metaslab_group_passivate(mg);
1665 if (vd->vdev_log_mg != NULL) {
1666 ASSERT(!vd->vdev_islog);
1667 metaslab_group_passivate(vd->vdev_log_mg);
1670 uint64_t count = vd->vdev_ms_count;
1671 for (uint64_t m = 0; m < count; m++) {
1672 metaslab_t *msp = vd->vdev_ms[m];
1673 if (msp != NULL)
1674 metaslab_fini(msp);
1676 vmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1677 vd->vdev_ms = NULL;
1678 vd->vdev_ms_count = 0;
1680 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
1681 ASSERT0(mg->mg_histogram[i]);
1682 if (vd->vdev_log_mg != NULL)
1683 ASSERT0(vd->vdev_log_mg->mg_histogram[i]);
1686 ASSERT0(vd->vdev_ms_count);
1689 typedef struct vdev_probe_stats {
1690 boolean_t vps_readable;
1691 boolean_t vps_writeable;
1692 boolean_t vps_zio_done_probe;
1693 int vps_flags;
1694 } vdev_probe_stats_t;
1696 static void
1697 vdev_probe_done(zio_t *zio)
1699 spa_t *spa = zio->io_spa;
1700 vdev_t *vd = zio->io_vd;
1701 vdev_probe_stats_t *vps = zio->io_private;
1703 ASSERT(vd->vdev_probe_zio != NULL);
1705 if (zio->io_type == ZIO_TYPE_READ) {
1706 if (zio->io_error == 0)
1707 vps->vps_readable = 1;
1708 if (zio->io_error == 0 && spa_writeable(spa)) {
1709 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1710 zio->io_offset, zio->io_size, zio->io_abd,
1711 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1712 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1713 } else {
1714 abd_free(zio->io_abd);
1716 } else if (zio->io_type == ZIO_TYPE_WRITE) {
1717 if (zio->io_error == 0)
1718 vps->vps_writeable = 1;
1719 abd_free(zio->io_abd);
1720 } else if (zio->io_type == ZIO_TYPE_NULL) {
1721 zio_t *pio;
1722 zio_link_t *zl;
1724 vd->vdev_cant_read |= !vps->vps_readable;
1725 vd->vdev_cant_write |= !vps->vps_writeable;
1726 vdev_dbgmsg(vd, "probe done, cant_read=%u cant_write=%u",
1727 vd->vdev_cant_read, vd->vdev_cant_write);
1729 if (vdev_readable(vd) &&
1730 (vdev_writeable(vd) || !spa_writeable(spa))) {
1731 zio->io_error = 0;
1732 } else {
1733 ASSERT(zio->io_error != 0);
1734 vdev_dbgmsg(vd, "failed probe");
1735 (void) zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
1736 spa, vd, NULL, NULL, 0);
1737 zio->io_error = SET_ERROR(ENXIO);
1740 * If this probe was initiated from zio pipeline, then
1741 * change the state in a spa_async_request. Probes that
1742 * were initiated from a vdev_open can change the state
1743 * as part of the open call.
1745 if (vps->vps_zio_done_probe) {
1746 vd->vdev_fault_wanted = B_TRUE;
1747 spa_async_request(spa, SPA_ASYNC_FAULT_VDEV);
1751 mutex_enter(&vd->vdev_probe_lock);
1752 ASSERT(vd->vdev_probe_zio == zio);
1753 vd->vdev_probe_zio = NULL;
1754 mutex_exit(&vd->vdev_probe_lock);
1756 zl = NULL;
1757 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1758 if (!vdev_accessible(vd, pio))
1759 pio->io_error = SET_ERROR(ENXIO);
1761 kmem_free(vps, sizeof (*vps));
1766 * Determine whether this device is accessible.
1768 * Read and write to several known locations: the pad regions of each
1769 * vdev label but the first, which we leave alone in case it contains
1770 * a VTOC.
1772 zio_t *
1773 vdev_probe(vdev_t *vd, zio_t *zio)
1775 spa_t *spa = vd->vdev_spa;
1776 vdev_probe_stats_t *vps = NULL;
1777 zio_t *pio;
1779 ASSERT(vd->vdev_ops->vdev_op_leaf);
1782 * Don't probe the probe.
1784 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1785 return (NULL);
1788 * To prevent 'probe storms' when a device fails, we create
1789 * just one probe i/o at a time. All zios that want to probe
1790 * this vdev will become parents of the probe io.
1792 mutex_enter(&vd->vdev_probe_lock);
1794 if ((pio = vd->vdev_probe_zio) == NULL) {
1795 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1797 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1798 ZIO_FLAG_DONT_AGGREGATE | ZIO_FLAG_TRYHARD;
1799 vps->vps_zio_done_probe = (zio != NULL);
1801 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1803 * vdev_cant_read and vdev_cant_write can only
1804 * transition from TRUE to FALSE when we have the
1805 * SCL_ZIO lock as writer; otherwise they can only
1806 * transition from FALSE to TRUE. This ensures that
1807 * any zio looking at these values can assume that
1808 * failures persist for the life of the I/O. That's
1809 * important because when a device has intermittent
1810 * connectivity problems, we want to ensure that
1811 * they're ascribed to the device (ENXIO) and not
1812 * the zio (EIO).
1814 * Since we hold SCL_ZIO as writer here, clear both
1815 * values so the probe can reevaluate from first
1816 * principles.
1818 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1819 vd->vdev_cant_read = B_FALSE;
1820 vd->vdev_cant_write = B_FALSE;
1823 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1824 vdev_probe_done, vps,
1825 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1828 if (zio != NULL)
1829 zio_add_child(zio, pio);
1831 mutex_exit(&vd->vdev_probe_lock);
1833 if (vps == NULL) {
1834 ASSERT(zio != NULL);
1835 return (NULL);
1838 for (int l = 1; l < VDEV_LABELS; l++) {
1839 zio_nowait(zio_read_phys(pio, vd,
1840 vdev_label_offset(vd->vdev_psize, l,
1841 offsetof(vdev_label_t, vl_be)), VDEV_PAD_SIZE,
1842 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1843 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1844 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1847 if (zio == NULL)
1848 return (pio);
1850 zio_nowait(pio);
1851 return (NULL);
1854 static void
1855 vdev_load_child(void *arg)
1857 vdev_t *vd = arg;
1859 vd->vdev_load_error = vdev_load(vd);
1862 static void
1863 vdev_open_child(void *arg)
1865 vdev_t *vd = arg;
1867 vd->vdev_open_thread = curthread;
1868 vd->vdev_open_error = vdev_open(vd);
1869 vd->vdev_open_thread = NULL;
1872 static boolean_t
1873 vdev_uses_zvols(vdev_t *vd)
1875 #ifdef _KERNEL
1876 if (zvol_is_zvol(vd->vdev_path))
1877 return (B_TRUE);
1878 #endif
1880 for (int c = 0; c < vd->vdev_children; c++)
1881 if (vdev_uses_zvols(vd->vdev_child[c]))
1882 return (B_TRUE);
1884 return (B_FALSE);
1888 * Returns B_TRUE if the passed child should be opened.
1890 static boolean_t
1891 vdev_default_open_children_func(vdev_t *vd)
1893 (void) vd;
1894 return (B_TRUE);
1898 * Open the requested child vdevs. If any of the leaf vdevs are using
1899 * a ZFS volume then do the opens in a single thread. This avoids a
1900 * deadlock when the current thread is holding the spa_namespace_lock.
1902 static void
1903 vdev_open_children_impl(vdev_t *vd, vdev_open_children_func_t *open_func)
1905 int children = vd->vdev_children;
1907 taskq_t *tq = taskq_create("vdev_open", children, minclsyspri,
1908 children, children, TASKQ_PREPOPULATE);
1909 vd->vdev_nonrot = B_TRUE;
1911 for (int c = 0; c < children; c++) {
1912 vdev_t *cvd = vd->vdev_child[c];
1914 if (open_func(cvd) == B_FALSE)
1915 continue;
1917 if (tq == NULL || vdev_uses_zvols(vd)) {
1918 cvd->vdev_open_error = vdev_open(cvd);
1919 } else {
1920 VERIFY(taskq_dispatch(tq, vdev_open_child,
1921 cvd, TQ_SLEEP) != TASKQID_INVALID);
1924 vd->vdev_nonrot &= cvd->vdev_nonrot;
1927 if (tq != NULL) {
1928 taskq_wait(tq);
1929 taskq_destroy(tq);
1934 * Open all child vdevs.
1936 void
1937 vdev_open_children(vdev_t *vd)
1939 vdev_open_children_impl(vd, vdev_default_open_children_func);
1943 * Conditionally open a subset of child vdevs.
1945 void
1946 vdev_open_children_subset(vdev_t *vd, vdev_open_children_func_t *open_func)
1948 vdev_open_children_impl(vd, open_func);
1952 * Compute the raidz-deflation ratio. Note, we hard-code 128k (1 << 17)
1953 * because it is the "typical" blocksize. Even though SPA_MAXBLOCKSIZE
1954 * changed, this algorithm can not change, otherwise it would inconsistently
1955 * account for existing bp's. We also hard-code txg 0 for the same reason
1956 * since expanded RAIDZ vdevs can use a different asize for different birth
1957 * txg's.
1959 static void
1960 vdev_set_deflate_ratio(vdev_t *vd)
1962 if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
1963 vd->vdev_deflate_ratio = (1 << 17) /
1964 (vdev_psize_to_asize_txg(vd, 1 << 17, 0) >>
1965 SPA_MINBLOCKSHIFT);
1970 * Choose the best of two ashifts, preferring one between logical ashift
1971 * (absolute minimum) and administrator defined maximum, otherwise take
1972 * the biggest of the two.
1974 uint64_t
1975 vdev_best_ashift(uint64_t logical, uint64_t a, uint64_t b)
1977 if (a > logical && a <= zfs_vdev_max_auto_ashift) {
1978 if (b <= logical || b > zfs_vdev_max_auto_ashift)
1979 return (a);
1980 else
1981 return (MAX(a, b));
1982 } else if (b <= logical || b > zfs_vdev_max_auto_ashift)
1983 return (MAX(a, b));
1984 return (b);
1988 * Maximize performance by inflating the configured ashift for top level
1989 * vdevs to be as close to the physical ashift as possible while maintaining
1990 * administrator defined limits and ensuring it doesn't go below the
1991 * logical ashift.
1993 static void
1994 vdev_ashift_optimize(vdev_t *vd)
1996 ASSERT(vd == vd->vdev_top);
1998 if (vd->vdev_ashift < vd->vdev_physical_ashift &&
1999 vd->vdev_physical_ashift <= zfs_vdev_max_auto_ashift) {
2000 vd->vdev_ashift = MIN(
2001 MAX(zfs_vdev_max_auto_ashift, vd->vdev_ashift),
2002 MAX(zfs_vdev_min_auto_ashift,
2003 vd->vdev_physical_ashift));
2004 } else {
2006 * If the logical and physical ashifts are the same, then
2007 * we ensure that the top-level vdev's ashift is not smaller
2008 * than our minimum ashift value. For the unusual case
2009 * where logical ashift > physical ashift, we can't cap
2010 * the calculated ashift based on max ashift as that
2011 * would cause failures.
2012 * We still check if we need to increase it to match
2013 * the min ashift.
2015 vd->vdev_ashift = MAX(zfs_vdev_min_auto_ashift,
2016 vd->vdev_ashift);
2021 * Prepare a virtual device for access.
2024 vdev_open(vdev_t *vd)
2026 spa_t *spa = vd->vdev_spa;
2027 int error;
2028 uint64_t osize = 0;
2029 uint64_t max_osize = 0;
2030 uint64_t asize, max_asize, psize;
2031 uint64_t logical_ashift = 0;
2032 uint64_t physical_ashift = 0;
2034 ASSERT(vd->vdev_open_thread == curthread ||
2035 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2036 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
2037 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
2038 vd->vdev_state == VDEV_STATE_OFFLINE);
2040 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2041 vd->vdev_cant_read = B_FALSE;
2042 vd->vdev_cant_write = B_FALSE;
2043 vd->vdev_fault_wanted = B_FALSE;
2044 vd->vdev_min_asize = vdev_get_min_asize(vd);
2047 * If this vdev is not removed, check its fault status. If it's
2048 * faulted, bail out of the open.
2050 if (!vd->vdev_removed && vd->vdev_faulted) {
2051 ASSERT(vd->vdev_children == 0);
2052 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
2053 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
2054 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
2055 vd->vdev_label_aux);
2056 return (SET_ERROR(ENXIO));
2057 } else if (vd->vdev_offline) {
2058 ASSERT(vd->vdev_children == 0);
2059 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
2060 return (SET_ERROR(ENXIO));
2063 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize,
2064 &logical_ashift, &physical_ashift);
2066 /* Keep the device in removed state if unplugged */
2067 if (error == ENOENT && vd->vdev_removed) {
2068 vdev_set_state(vd, B_TRUE, VDEV_STATE_REMOVED,
2069 VDEV_AUX_NONE);
2070 return (error);
2074 * Physical volume size should never be larger than its max size, unless
2075 * the disk has shrunk while we were reading it or the device is buggy
2076 * or damaged: either way it's not safe for use, bail out of the open.
2078 if (osize > max_osize) {
2079 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2080 VDEV_AUX_OPEN_FAILED);
2081 return (SET_ERROR(ENXIO));
2085 * Reset the vdev_reopening flag so that we actually close
2086 * the vdev on error.
2088 vd->vdev_reopening = B_FALSE;
2089 if (zio_injection_enabled && error == 0)
2090 error = zio_handle_device_injection(vd, NULL, SET_ERROR(ENXIO));
2092 if (error) {
2093 if (vd->vdev_removed &&
2094 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
2095 vd->vdev_removed = B_FALSE;
2097 if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
2098 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
2099 vd->vdev_stat.vs_aux);
2100 } else {
2101 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2102 vd->vdev_stat.vs_aux);
2104 return (error);
2107 vd->vdev_removed = B_FALSE;
2110 * Recheck the faulted flag now that we have confirmed that
2111 * the vdev is accessible. If we're faulted, bail.
2113 if (vd->vdev_faulted) {
2114 ASSERT(vd->vdev_children == 0);
2115 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
2116 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
2117 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
2118 vd->vdev_label_aux);
2119 return (SET_ERROR(ENXIO));
2122 if (vd->vdev_degraded) {
2123 ASSERT(vd->vdev_children == 0);
2124 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
2125 VDEV_AUX_ERR_EXCEEDED);
2126 } else {
2127 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
2131 * For hole or missing vdevs we just return success.
2133 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
2134 return (0);
2136 for (int c = 0; c < vd->vdev_children; c++) {
2137 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
2138 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
2139 VDEV_AUX_NONE);
2140 break;
2144 osize = P2ALIGN_TYPED(osize, sizeof (vdev_label_t), uint64_t);
2145 max_osize = P2ALIGN_TYPED(max_osize, sizeof (vdev_label_t), uint64_t);
2147 if (vd->vdev_children == 0) {
2148 if (osize < SPA_MINDEVSIZE) {
2149 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2150 VDEV_AUX_TOO_SMALL);
2151 return (SET_ERROR(EOVERFLOW));
2153 psize = osize;
2154 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
2155 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
2156 VDEV_LABEL_END_SIZE);
2157 } else {
2158 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
2159 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
2160 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2161 VDEV_AUX_TOO_SMALL);
2162 return (SET_ERROR(EOVERFLOW));
2164 psize = 0;
2165 asize = osize;
2166 max_asize = max_osize;
2170 * If the vdev was expanded, record this so that we can re-create the
2171 * uberblock rings in labels {2,3}, during the next sync.
2173 if ((psize > vd->vdev_psize) && (vd->vdev_psize != 0))
2174 vd->vdev_copy_uberblocks = B_TRUE;
2176 vd->vdev_psize = psize;
2179 * Make sure the allocatable size hasn't shrunk too much.
2181 if (asize < vd->vdev_min_asize) {
2182 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2183 VDEV_AUX_BAD_LABEL);
2184 return (SET_ERROR(EINVAL));
2188 * We can always set the logical/physical ashift members since
2189 * their values are only used to calculate the vdev_ashift when
2190 * the device is first added to the config. These values should
2191 * not be used for anything else since they may change whenever
2192 * the device is reopened and we don't store them in the label.
2194 vd->vdev_physical_ashift =
2195 MAX(physical_ashift, vd->vdev_physical_ashift);
2196 vd->vdev_logical_ashift = MAX(logical_ashift,
2197 vd->vdev_logical_ashift);
2199 if (vd->vdev_asize == 0) {
2201 * This is the first-ever open, so use the computed values.
2202 * For compatibility, a different ashift can be requested.
2204 vd->vdev_asize = asize;
2205 vd->vdev_max_asize = max_asize;
2208 * If the vdev_ashift was not overridden at creation time
2209 * (0) or the override value is impossible for the device,
2210 * then set it the logical ashift and optimize the ashift.
2212 if (vd->vdev_ashift < vd->vdev_logical_ashift) {
2213 vd->vdev_ashift = vd->vdev_logical_ashift;
2215 if (vd->vdev_logical_ashift > ASHIFT_MAX) {
2216 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2217 VDEV_AUX_ASHIFT_TOO_BIG);
2218 return (SET_ERROR(EDOM));
2221 if (vd->vdev_top == vd && vd->vdev_attaching == B_FALSE)
2222 vdev_ashift_optimize(vd);
2223 vd->vdev_attaching = B_FALSE;
2225 if (vd->vdev_ashift != 0 && (vd->vdev_ashift < ASHIFT_MIN ||
2226 vd->vdev_ashift > ASHIFT_MAX)) {
2227 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2228 VDEV_AUX_BAD_ASHIFT);
2229 return (SET_ERROR(EDOM));
2231 } else {
2233 * Make sure the alignment required hasn't increased.
2235 if (vd->vdev_ashift > vd->vdev_top->vdev_ashift &&
2236 vd->vdev_ops->vdev_op_leaf) {
2237 (void) zfs_ereport_post(
2238 FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT,
2239 spa, vd, NULL, NULL, 0);
2240 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2241 VDEV_AUX_BAD_LABEL);
2242 return (SET_ERROR(EDOM));
2244 vd->vdev_max_asize = max_asize;
2248 * If all children are healthy we update asize if either:
2249 * The asize has increased, due to a device expansion caused by dynamic
2250 * LUN growth or vdev replacement, and automatic expansion is enabled;
2251 * making the additional space available.
2253 * The asize has decreased, due to a device shrink usually caused by a
2254 * vdev replace with a smaller device. This ensures that calculations
2255 * based of max_asize and asize e.g. esize are always valid. It's safe
2256 * to do this as we've already validated that asize is greater than
2257 * vdev_min_asize.
2259 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
2260 ((asize > vd->vdev_asize &&
2261 (vd->vdev_expanding || spa->spa_autoexpand)) ||
2262 (asize < vd->vdev_asize)))
2263 vd->vdev_asize = asize;
2265 vdev_set_min_asize(vd);
2268 * Ensure we can issue some IO before declaring the
2269 * vdev open for business.
2271 if (vd->vdev_ops->vdev_op_leaf &&
2272 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
2273 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
2274 VDEV_AUX_ERR_EXCEEDED);
2275 return (error);
2279 * Track the minimum allocation size.
2281 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
2282 vd->vdev_islog == 0 && vd->vdev_aux == NULL) {
2283 uint64_t min_alloc = vdev_get_min_alloc(vd);
2284 vdev_spa_set_alloc(spa, min_alloc);
2288 * If this is a leaf vdev, assess whether a resilver is needed.
2289 * But don't do this if we are doing a reopen for a scrub, since
2290 * this would just restart the scrub we are already doing.
2292 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen)
2293 dsl_scan_assess_vdev(spa->spa_dsl_pool, vd);
2295 return (0);
2298 static void
2299 vdev_validate_child(void *arg)
2301 vdev_t *vd = arg;
2303 vd->vdev_validate_thread = curthread;
2304 vd->vdev_validate_error = vdev_validate(vd);
2305 vd->vdev_validate_thread = NULL;
2309 * Called once the vdevs are all opened, this routine validates the label
2310 * contents. This needs to be done before vdev_load() so that we don't
2311 * inadvertently do repair I/Os to the wrong device.
2313 * This function will only return failure if one of the vdevs indicates that it
2314 * has since been destroyed or exported. This is only possible if
2315 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
2316 * will be updated but the function will return 0.
2319 vdev_validate(vdev_t *vd)
2321 spa_t *spa = vd->vdev_spa;
2322 taskq_t *tq = NULL;
2323 nvlist_t *label;
2324 uint64_t guid = 0, aux_guid = 0, top_guid;
2325 uint64_t state;
2326 nvlist_t *nvl;
2327 uint64_t txg;
2328 int children = vd->vdev_children;
2330 if (vdev_validate_skip)
2331 return (0);
2333 if (children > 0) {
2334 tq = taskq_create("vdev_validate", children, minclsyspri,
2335 children, children, TASKQ_PREPOPULATE);
2338 for (uint64_t c = 0; c < children; c++) {
2339 vdev_t *cvd = vd->vdev_child[c];
2341 if (tq == NULL || vdev_uses_zvols(cvd)) {
2342 vdev_validate_child(cvd);
2343 } else {
2344 VERIFY(taskq_dispatch(tq, vdev_validate_child, cvd,
2345 TQ_SLEEP) != TASKQID_INVALID);
2348 if (tq != NULL) {
2349 taskq_wait(tq);
2350 taskq_destroy(tq);
2352 for (int c = 0; c < children; c++) {
2353 int error = vd->vdev_child[c]->vdev_validate_error;
2355 if (error != 0)
2356 return (SET_ERROR(EBADF));
2361 * If the device has already failed, or was marked offline, don't do
2362 * any further validation. Otherwise, label I/O will fail and we will
2363 * overwrite the previous state.
2365 if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
2366 return (0);
2369 * If we are performing an extreme rewind, we allow for a label that
2370 * was modified at a point after the current txg.
2371 * If config lock is not held do not check for the txg. spa_sync could
2372 * be updating the vdev's label before updating spa_last_synced_txg.
2374 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 ||
2375 spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG)
2376 txg = UINT64_MAX;
2377 else
2378 txg = spa_last_synced_txg(spa);
2380 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
2381 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2382 VDEV_AUX_BAD_LABEL);
2383 vdev_dbgmsg(vd, "vdev_validate: failed reading config for "
2384 "txg %llu", (u_longlong_t)txg);
2385 return (0);
2389 * Determine if this vdev has been split off into another
2390 * pool. If so, then refuse to open it.
2392 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
2393 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
2394 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2395 VDEV_AUX_SPLIT_POOL);
2396 nvlist_free(label);
2397 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
2398 return (0);
2401 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
2402 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2403 VDEV_AUX_CORRUPT_DATA);
2404 nvlist_free(label);
2405 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2406 ZPOOL_CONFIG_POOL_GUID);
2407 return (0);
2411 * If config is not trusted then ignore the spa guid check. This is
2412 * necessary because if the machine crashed during a re-guid the new
2413 * guid might have been written to all of the vdev labels, but not the
2414 * cached config. The check will be performed again once we have the
2415 * trusted config from the MOS.
2417 if (spa->spa_trust_config && guid != spa_guid(spa)) {
2418 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2419 VDEV_AUX_CORRUPT_DATA);
2420 nvlist_free(label);
2421 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
2422 "match config (%llu != %llu)", (u_longlong_t)guid,
2423 (u_longlong_t)spa_guid(spa));
2424 return (0);
2427 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
2428 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
2429 &aux_guid) != 0)
2430 aux_guid = 0;
2432 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
2433 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2434 VDEV_AUX_CORRUPT_DATA);
2435 nvlist_free(label);
2436 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2437 ZPOOL_CONFIG_GUID);
2438 return (0);
2441 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
2442 != 0) {
2443 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2444 VDEV_AUX_CORRUPT_DATA);
2445 nvlist_free(label);
2446 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2447 ZPOOL_CONFIG_TOP_GUID);
2448 return (0);
2452 * If this vdev just became a top-level vdev because its sibling was
2453 * detached, it will have adopted the parent's vdev guid -- but the
2454 * label may or may not be on disk yet. Fortunately, either version
2455 * of the label will have the same top guid, so if we're a top-level
2456 * vdev, we can safely compare to that instead.
2457 * However, if the config comes from a cachefile that failed to update
2458 * after the detach, a top-level vdev will appear as a non top-level
2459 * vdev in the config. Also relax the constraints if we perform an
2460 * extreme rewind.
2462 * If we split this vdev off instead, then we also check the
2463 * original pool's guid. We don't want to consider the vdev
2464 * corrupt if it is partway through a split operation.
2466 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
2467 boolean_t mismatch = B_FALSE;
2468 if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
2469 if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
2470 mismatch = B_TRUE;
2471 } else {
2472 if (vd->vdev_guid != top_guid &&
2473 vd->vdev_top->vdev_guid != guid)
2474 mismatch = B_TRUE;
2477 if (mismatch) {
2478 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2479 VDEV_AUX_CORRUPT_DATA);
2480 nvlist_free(label);
2481 vdev_dbgmsg(vd, "vdev_validate: config guid "
2482 "doesn't match label guid");
2483 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
2484 (u_longlong_t)vd->vdev_guid,
2485 (u_longlong_t)vd->vdev_top->vdev_guid);
2486 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
2487 "aux_guid %llu", (u_longlong_t)guid,
2488 (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
2489 return (0);
2493 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
2494 &state) != 0) {
2495 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2496 VDEV_AUX_CORRUPT_DATA);
2497 nvlist_free(label);
2498 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2499 ZPOOL_CONFIG_POOL_STATE);
2500 return (0);
2503 nvlist_free(label);
2506 * If this is a verbatim import, no need to check the
2507 * state of the pool.
2509 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
2510 spa_load_state(spa) == SPA_LOAD_OPEN &&
2511 state != POOL_STATE_ACTIVE) {
2512 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
2513 "for spa %s", (u_longlong_t)state, spa->spa_name);
2514 return (SET_ERROR(EBADF));
2518 * If we were able to open and validate a vdev that was
2519 * previously marked permanently unavailable, clear that state
2520 * now.
2522 if (vd->vdev_not_present)
2523 vd->vdev_not_present = 0;
2525 return (0);
2528 static void
2529 vdev_update_path(const char *prefix, char *svd, char **dvd, uint64_t guid)
2531 if (svd != NULL && *dvd != NULL) {
2532 if (strcmp(svd, *dvd) != 0) {
2533 zfs_dbgmsg("vdev_copy_path: vdev %llu: %s changed "
2534 "from '%s' to '%s'", (u_longlong_t)guid, prefix,
2535 *dvd, svd);
2536 spa_strfree(*dvd);
2537 *dvd = spa_strdup(svd);
2539 } else if (svd != NULL) {
2540 *dvd = spa_strdup(svd);
2541 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
2542 (u_longlong_t)guid, *dvd);
2546 static void
2547 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
2549 char *old, *new;
2551 vdev_update_path("vdev_path", svd->vdev_path, &dvd->vdev_path,
2552 dvd->vdev_guid);
2554 vdev_update_path("vdev_devid", svd->vdev_devid, &dvd->vdev_devid,
2555 dvd->vdev_guid);
2557 vdev_update_path("vdev_physpath", svd->vdev_physpath,
2558 &dvd->vdev_physpath, dvd->vdev_guid);
2561 * Our enclosure sysfs path may have changed between imports
2563 old = dvd->vdev_enc_sysfs_path;
2564 new = svd->vdev_enc_sysfs_path;
2565 if ((old != NULL && new == NULL) ||
2566 (old == NULL && new != NULL) ||
2567 ((old != NULL && new != NULL) && strcmp(new, old) != 0)) {
2568 zfs_dbgmsg("vdev_copy_path: vdev %llu: vdev_enc_sysfs_path "
2569 "changed from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
2570 old, new);
2572 if (dvd->vdev_enc_sysfs_path)
2573 spa_strfree(dvd->vdev_enc_sysfs_path);
2575 if (svd->vdev_enc_sysfs_path) {
2576 dvd->vdev_enc_sysfs_path = spa_strdup(
2577 svd->vdev_enc_sysfs_path);
2578 } else {
2579 dvd->vdev_enc_sysfs_path = NULL;
2585 * Recursively copy vdev paths from one vdev to another. Source and destination
2586 * vdev trees must have same geometry otherwise return error. Intended to copy
2587 * paths from userland config into MOS config.
2590 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
2592 if ((svd->vdev_ops == &vdev_missing_ops) ||
2593 (svd->vdev_ishole && dvd->vdev_ishole) ||
2594 (dvd->vdev_ops == &vdev_indirect_ops))
2595 return (0);
2597 if (svd->vdev_ops != dvd->vdev_ops) {
2598 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
2599 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
2600 return (SET_ERROR(EINVAL));
2603 if (svd->vdev_guid != dvd->vdev_guid) {
2604 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
2605 "%llu)", (u_longlong_t)svd->vdev_guid,
2606 (u_longlong_t)dvd->vdev_guid);
2607 return (SET_ERROR(EINVAL));
2610 if (svd->vdev_children != dvd->vdev_children) {
2611 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
2612 "%llu != %llu", (u_longlong_t)svd->vdev_children,
2613 (u_longlong_t)dvd->vdev_children);
2614 return (SET_ERROR(EINVAL));
2617 for (uint64_t i = 0; i < svd->vdev_children; i++) {
2618 int error = vdev_copy_path_strict(svd->vdev_child[i],
2619 dvd->vdev_child[i]);
2620 if (error != 0)
2621 return (error);
2624 if (svd->vdev_ops->vdev_op_leaf)
2625 vdev_copy_path_impl(svd, dvd);
2627 return (0);
2630 static void
2631 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
2633 ASSERT(stvd->vdev_top == stvd);
2634 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
2636 for (uint64_t i = 0; i < dvd->vdev_children; i++) {
2637 vdev_copy_path_search(stvd, dvd->vdev_child[i]);
2640 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
2641 return;
2644 * The idea here is that while a vdev can shift positions within
2645 * a top vdev (when replacing, attaching mirror, etc.) it cannot
2646 * step outside of it.
2648 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
2650 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
2651 return;
2653 ASSERT(vd->vdev_ops->vdev_op_leaf);
2655 vdev_copy_path_impl(vd, dvd);
2659 * Recursively copy vdev paths from one root vdev to another. Source and
2660 * destination vdev trees may differ in geometry. For each destination leaf
2661 * vdev, search a vdev with the same guid and top vdev id in the source.
2662 * Intended to copy paths from userland config into MOS config.
2664 void
2665 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
2667 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
2668 ASSERT(srvd->vdev_ops == &vdev_root_ops);
2669 ASSERT(drvd->vdev_ops == &vdev_root_ops);
2671 for (uint64_t i = 0; i < children; i++) {
2672 vdev_copy_path_search(srvd->vdev_child[i],
2673 drvd->vdev_child[i]);
2678 * Close a virtual device.
2680 void
2681 vdev_close(vdev_t *vd)
2683 vdev_t *pvd = vd->vdev_parent;
2684 spa_t *spa __maybe_unused = vd->vdev_spa;
2686 ASSERT(vd != NULL);
2687 ASSERT(vd->vdev_open_thread == curthread ||
2688 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2691 * If our parent is reopening, then we are as well, unless we are
2692 * going offline.
2694 if (pvd != NULL && pvd->vdev_reopening)
2695 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
2697 vd->vdev_ops->vdev_op_close(vd);
2700 * We record the previous state before we close it, so that if we are
2701 * doing a reopen(), we don't generate FMA ereports if we notice that
2702 * it's still faulted.
2704 vd->vdev_prevstate = vd->vdev_state;
2706 if (vd->vdev_offline)
2707 vd->vdev_state = VDEV_STATE_OFFLINE;
2708 else
2709 vd->vdev_state = VDEV_STATE_CLOSED;
2710 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2713 void
2714 vdev_hold(vdev_t *vd)
2716 spa_t *spa = vd->vdev_spa;
2718 ASSERT(spa_is_root(spa));
2719 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
2720 return;
2722 for (int c = 0; c < vd->vdev_children; c++)
2723 vdev_hold(vd->vdev_child[c]);
2725 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_hold != NULL)
2726 vd->vdev_ops->vdev_op_hold(vd);
2729 void
2730 vdev_rele(vdev_t *vd)
2732 ASSERT(spa_is_root(vd->vdev_spa));
2733 for (int c = 0; c < vd->vdev_children; c++)
2734 vdev_rele(vd->vdev_child[c]);
2736 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_rele != NULL)
2737 vd->vdev_ops->vdev_op_rele(vd);
2741 * Reopen all interior vdevs and any unopened leaves. We don't actually
2742 * reopen leaf vdevs which had previously been opened as they might deadlock
2743 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2744 * If the leaf has never been opened then open it, as usual.
2746 void
2747 vdev_reopen(vdev_t *vd)
2749 spa_t *spa = vd->vdev_spa;
2751 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2753 /* set the reopening flag unless we're taking the vdev offline */
2754 vd->vdev_reopening = !vd->vdev_offline;
2755 vdev_close(vd);
2756 (void) vdev_open(vd);
2759 * Call vdev_validate() here to make sure we have the same device.
2760 * Otherwise, a device with an invalid label could be successfully
2761 * opened in response to vdev_reopen().
2763 if (vd->vdev_aux) {
2764 (void) vdev_validate_aux(vd);
2765 if (vdev_readable(vd) && vdev_writeable(vd) &&
2766 vd->vdev_aux == &spa->spa_l2cache) {
2768 * In case the vdev is present we should evict all ARC
2769 * buffers and pointers to log blocks and reclaim their
2770 * space before restoring its contents to L2ARC.
2772 if (l2arc_vdev_present(vd)) {
2773 l2arc_rebuild_vdev(vd, B_TRUE);
2774 } else {
2775 l2arc_add_vdev(spa, vd);
2777 spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD);
2778 spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM);
2780 } else {
2781 (void) vdev_validate(vd);
2785 * Recheck if resilver is still needed and cancel any
2786 * scheduled resilver if resilver is unneeded.
2788 if (!vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL) &&
2789 spa->spa_async_tasks & SPA_ASYNC_RESILVER) {
2790 mutex_enter(&spa->spa_async_lock);
2791 spa->spa_async_tasks &= ~SPA_ASYNC_RESILVER;
2792 mutex_exit(&spa->spa_async_lock);
2796 * Reassess parent vdev's health.
2798 vdev_propagate_state(vd);
2802 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
2804 int error;
2807 * Normally, partial opens (e.g. of a mirror) are allowed.
2808 * For a create, however, we want to fail the request if
2809 * there are any components we can't open.
2811 error = vdev_open(vd);
2813 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
2814 vdev_close(vd);
2815 return (error ? error : SET_ERROR(ENXIO));
2819 * Recursively load DTLs and initialize all labels.
2821 if ((error = vdev_dtl_load(vd)) != 0 ||
2822 (error = vdev_label_init(vd, txg, isreplacing ?
2823 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
2824 vdev_close(vd);
2825 return (error);
2828 return (0);
2831 void
2832 vdev_metaslab_set_size(vdev_t *vd)
2834 uint64_t asize = vd->vdev_asize;
2835 uint64_t ms_count = asize >> zfs_vdev_default_ms_shift;
2836 uint64_t ms_shift;
2839 * There are two dimensions to the metaslab sizing calculation:
2840 * the size of the metaslab and the count of metaslabs per vdev.
2842 * The default values used below are a good balance between memory
2843 * usage (larger metaslab size means more memory needed for loaded
2844 * metaslabs; more metaslabs means more memory needed for the
2845 * metaslab_t structs), metaslab load time (larger metaslabs take
2846 * longer to load), and metaslab sync time (more metaslabs means
2847 * more time spent syncing all of them).
2849 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2850 * The range of the dimensions are as follows:
2852 * 2^29 <= ms_size <= 2^34
2853 * 16 <= ms_count <= 131,072
2855 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2856 * at least 512MB (2^29) to minimize fragmentation effects when
2857 * testing with smaller devices. However, the count constraint
2858 * of at least 16 metaslabs will override this minimum size goal.
2860 * On the upper end of vdev sizes, we aim for a maximum metaslab
2861 * size of 16GB. However, we will cap the total count to 2^17
2862 * metaslabs to keep our memory footprint in check and let the
2863 * metaslab size grow from there if that limit is hit.
2865 * The net effect of applying above constrains is summarized below.
2867 * vdev size metaslab count
2868 * --------------|-----------------
2869 * < 8GB ~16
2870 * 8GB - 100GB one per 512MB
2871 * 100GB - 3TB ~200
2872 * 3TB - 2PB one per 16GB
2873 * > 2PB ~131,072
2874 * --------------------------------
2876 * Finally, note that all of the above calculate the initial
2877 * number of metaslabs. Expanding a top-level vdev will result
2878 * in additional metaslabs being allocated making it possible
2879 * to exceed the zfs_vdev_ms_count_limit.
2882 if (ms_count < zfs_vdev_min_ms_count)
2883 ms_shift = highbit64(asize / zfs_vdev_min_ms_count);
2884 else if (ms_count > zfs_vdev_default_ms_count)
2885 ms_shift = highbit64(asize / zfs_vdev_default_ms_count);
2886 else
2887 ms_shift = zfs_vdev_default_ms_shift;
2889 if (ms_shift < SPA_MAXBLOCKSHIFT) {
2890 ms_shift = SPA_MAXBLOCKSHIFT;
2891 } else if (ms_shift > zfs_vdev_max_ms_shift) {
2892 ms_shift = zfs_vdev_max_ms_shift;
2893 /* cap the total count to constrain memory footprint */
2894 if ((asize >> ms_shift) > zfs_vdev_ms_count_limit)
2895 ms_shift = highbit64(asize / zfs_vdev_ms_count_limit);
2898 vd->vdev_ms_shift = ms_shift;
2899 ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT);
2902 void
2903 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2905 ASSERT(vd == vd->vdev_top);
2906 /* indirect vdevs don't have metaslabs or dtls */
2907 ASSERT(vdev_is_concrete(vd) || flags == 0);
2908 ASSERT(ISP2(flags));
2909 ASSERT(spa_writeable(vd->vdev_spa));
2911 if (flags & VDD_METASLAB)
2912 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2914 if (flags & VDD_DTL)
2915 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2917 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2920 void
2921 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
2923 for (int c = 0; c < vd->vdev_children; c++)
2924 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
2926 if (vd->vdev_ops->vdev_op_leaf)
2927 vdev_dirty(vd->vdev_top, flags, vd, txg);
2931 * DTLs.
2933 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2934 * the vdev has less than perfect replication. There are four kinds of DTL:
2936 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2938 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2940 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2941 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2942 * txgs that was scrubbed.
2944 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2945 * persistent errors or just some device being offline.
2946 * Unlike the other three, the DTL_OUTAGE map is not generally
2947 * maintained; it's only computed when needed, typically to
2948 * determine whether a device can be detached.
2950 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2951 * either has the data or it doesn't.
2953 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2954 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2955 * if any child is less than fully replicated, then so is its parent.
2956 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2957 * comprising only those txgs which appear in 'maxfaults' or more children;
2958 * those are the txgs we don't have enough replication to read. For example,
2959 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2960 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2961 * two child DTL_MISSING maps.
2963 * It should be clear from the above that to compute the DTLs and outage maps
2964 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2965 * Therefore, that is all we keep on disk. When loading the pool, or after
2966 * a configuration change, we generate all other DTLs from first principles.
2968 void
2969 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2971 range_tree_t *rt = vd->vdev_dtl[t];
2973 ASSERT(t < DTL_TYPES);
2974 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2975 ASSERT(spa_writeable(vd->vdev_spa));
2977 mutex_enter(&vd->vdev_dtl_lock);
2978 if (!range_tree_contains(rt, txg, size))
2979 range_tree_add(rt, txg, size);
2980 mutex_exit(&vd->vdev_dtl_lock);
2983 boolean_t
2984 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2986 range_tree_t *rt = vd->vdev_dtl[t];
2987 boolean_t dirty = B_FALSE;
2989 ASSERT(t < DTL_TYPES);
2990 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2993 * While we are loading the pool, the DTLs have not been loaded yet.
2994 * This isn't a problem but it can result in devices being tried
2995 * which are known to not have the data. In which case, the import
2996 * is relying on the checksum to ensure that we get the right data.
2997 * Note that while importing we are only reading the MOS, which is
2998 * always checksummed.
3000 mutex_enter(&vd->vdev_dtl_lock);
3001 if (!range_tree_is_empty(rt))
3002 dirty = range_tree_contains(rt, txg, size);
3003 mutex_exit(&vd->vdev_dtl_lock);
3005 return (dirty);
3008 boolean_t
3009 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
3011 range_tree_t *rt = vd->vdev_dtl[t];
3012 boolean_t empty;
3014 mutex_enter(&vd->vdev_dtl_lock);
3015 empty = range_tree_is_empty(rt);
3016 mutex_exit(&vd->vdev_dtl_lock);
3018 return (empty);
3022 * Check if the txg falls within the range which must be
3023 * resilvered. DVAs outside this range can always be skipped.
3025 boolean_t
3026 vdev_default_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
3027 uint64_t phys_birth)
3029 (void) dva, (void) psize;
3031 /* Set by sequential resilver. */
3032 if (phys_birth == TXG_UNKNOWN)
3033 return (B_TRUE);
3035 return (vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1));
3039 * Returns B_TRUE if the vdev determines the DVA needs to be resilvered.
3041 boolean_t
3042 vdev_dtl_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
3043 uint64_t phys_birth)
3045 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
3047 if (vd->vdev_ops->vdev_op_need_resilver == NULL ||
3048 vd->vdev_ops->vdev_op_leaf)
3049 return (B_TRUE);
3051 return (vd->vdev_ops->vdev_op_need_resilver(vd, dva, psize,
3052 phys_birth));
3056 * Returns the lowest txg in the DTL range.
3058 static uint64_t
3059 vdev_dtl_min(vdev_t *vd)
3061 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
3062 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
3063 ASSERT0(vd->vdev_children);
3065 return (range_tree_min(vd->vdev_dtl[DTL_MISSING]) - 1);
3069 * Returns the highest txg in the DTL.
3071 static uint64_t
3072 vdev_dtl_max(vdev_t *vd)
3074 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
3075 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
3076 ASSERT0(vd->vdev_children);
3078 return (range_tree_max(vd->vdev_dtl[DTL_MISSING]));
3082 * Determine if a resilvering vdev should remove any DTL entries from
3083 * its range. If the vdev was resilvering for the entire duration of the
3084 * scan then it should excise that range from its DTLs. Otherwise, this
3085 * vdev is considered partially resilvered and should leave its DTL
3086 * entries intact. The comment in vdev_dtl_reassess() describes how we
3087 * excise the DTLs.
3089 static boolean_t
3090 vdev_dtl_should_excise(vdev_t *vd, boolean_t rebuild_done)
3092 ASSERT0(vd->vdev_children);
3094 if (vd->vdev_state < VDEV_STATE_DEGRADED)
3095 return (B_FALSE);
3097 if (vd->vdev_resilver_deferred)
3098 return (B_FALSE);
3100 if (range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]))
3101 return (B_TRUE);
3103 if (rebuild_done) {
3104 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config;
3105 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
3107 /* Rebuild not initiated by attach */
3108 if (vd->vdev_rebuild_txg == 0)
3109 return (B_TRUE);
3112 * When a rebuild completes without error then all missing data
3113 * up to the rebuild max txg has been reconstructed and the DTL
3114 * is eligible for excision.
3116 if (vrp->vrp_rebuild_state == VDEV_REBUILD_COMPLETE &&
3117 vdev_dtl_max(vd) <= vrp->vrp_max_txg) {
3118 ASSERT3U(vrp->vrp_min_txg, <=, vdev_dtl_min(vd));
3119 ASSERT3U(vrp->vrp_min_txg, <, vd->vdev_rebuild_txg);
3120 ASSERT3U(vd->vdev_rebuild_txg, <=, vrp->vrp_max_txg);
3121 return (B_TRUE);
3123 } else {
3124 dsl_scan_t *scn = vd->vdev_spa->spa_dsl_pool->dp_scan;
3125 dsl_scan_phys_t *scnp __maybe_unused = &scn->scn_phys;
3127 /* Resilver not initiated by attach */
3128 if (vd->vdev_resilver_txg == 0)
3129 return (B_TRUE);
3132 * When a resilver is initiated the scan will assign the
3133 * scn_max_txg value to the highest txg value that exists
3134 * in all DTLs. If this device's max DTL is not part of this
3135 * scan (i.e. it is not in the range (scn_min_txg, scn_max_txg]
3136 * then it is not eligible for excision.
3138 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
3139 ASSERT3U(scnp->scn_min_txg, <=, vdev_dtl_min(vd));
3140 ASSERT3U(scnp->scn_min_txg, <, vd->vdev_resilver_txg);
3141 ASSERT3U(vd->vdev_resilver_txg, <=, scnp->scn_max_txg);
3142 return (B_TRUE);
3146 return (B_FALSE);
3150 * Reassess DTLs after a config change or scrub completion. If txg == 0 no
3151 * write operations will be issued to the pool.
3153 static void
3154 vdev_dtl_reassess_impl(vdev_t *vd, uint64_t txg, uint64_t scrub_txg,
3155 boolean_t scrub_done, boolean_t rebuild_done, boolean_t faulting)
3157 spa_t *spa = vd->vdev_spa;
3158 avl_tree_t reftree;
3159 int minref;
3161 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
3163 for (int c = 0; c < vd->vdev_children; c++)
3164 vdev_dtl_reassess_impl(vd->vdev_child[c], txg,
3165 scrub_txg, scrub_done, rebuild_done, faulting);
3167 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
3168 return;
3170 if (vd->vdev_ops->vdev_op_leaf) {
3171 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
3172 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config;
3173 boolean_t check_excise = B_FALSE;
3174 boolean_t wasempty = B_TRUE;
3176 mutex_enter(&vd->vdev_dtl_lock);
3179 * If requested, pretend the scan or rebuild completed cleanly.
3181 if (zfs_scan_ignore_errors) {
3182 if (scn != NULL)
3183 scn->scn_phys.scn_errors = 0;
3184 if (vr != NULL)
3185 vr->vr_rebuild_phys.vrp_errors = 0;
3188 if (scrub_txg != 0 &&
3189 !range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
3190 wasempty = B_FALSE;
3191 zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d "
3192 "dtl:%llu/%llu errors:%llu",
3193 (u_longlong_t)vd->vdev_guid, (u_longlong_t)txg,
3194 (u_longlong_t)scrub_txg, spa->spa_scrub_started,
3195 (u_longlong_t)vdev_dtl_min(vd),
3196 (u_longlong_t)vdev_dtl_max(vd),
3197 (u_longlong_t)(scn ? scn->scn_phys.scn_errors : 0));
3201 * If we've completed a scrub/resilver or a rebuild cleanly
3202 * then determine if this vdev should remove any DTLs. We
3203 * only want to excise regions on vdevs that were available
3204 * during the entire duration of this scan.
3206 if (rebuild_done &&
3207 vr != NULL && vr->vr_rebuild_phys.vrp_errors == 0) {
3208 check_excise = B_TRUE;
3209 } else {
3210 if (spa->spa_scrub_started ||
3211 (scn != NULL && scn->scn_phys.scn_errors == 0)) {
3212 check_excise = B_TRUE;
3216 if (scrub_txg && check_excise &&
3217 vdev_dtl_should_excise(vd, rebuild_done)) {
3219 * We completed a scrub, resilver or rebuild up to
3220 * scrub_txg. If we did it without rebooting, then
3221 * the scrub dtl will be valid, so excise the old
3222 * region and fold in the scrub dtl. Otherwise,
3223 * leave the dtl as-is if there was an error.
3225 * There's little trick here: to excise the beginning
3226 * of the DTL_MISSING map, we put it into a reference
3227 * tree and then add a segment with refcnt -1 that
3228 * covers the range [0, scrub_txg). This means
3229 * that each txg in that range has refcnt -1 or 0.
3230 * We then add DTL_SCRUB with a refcnt of 2, so that
3231 * entries in the range [0, scrub_txg) will have a
3232 * positive refcnt -- either 1 or 2. We then convert
3233 * the reference tree into the new DTL_MISSING map.
3235 space_reftree_create(&reftree);
3236 space_reftree_add_map(&reftree,
3237 vd->vdev_dtl[DTL_MISSING], 1);
3238 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
3239 space_reftree_add_map(&reftree,
3240 vd->vdev_dtl[DTL_SCRUB], 2);
3241 space_reftree_generate_map(&reftree,
3242 vd->vdev_dtl[DTL_MISSING], 1);
3243 space_reftree_destroy(&reftree);
3245 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
3246 zfs_dbgmsg("update DTL_MISSING:%llu/%llu",
3247 (u_longlong_t)vdev_dtl_min(vd),
3248 (u_longlong_t)vdev_dtl_max(vd));
3249 } else if (!wasempty) {
3250 zfs_dbgmsg("DTL_MISSING is now empty");
3253 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
3254 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
3255 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
3256 if (scrub_done)
3257 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
3258 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
3261 * For the faulting case, treat members of a replacing vdev
3262 * as if they are not available. It's more likely than not that
3263 * a vdev in a replacing vdev could encounter read errors so
3264 * treat it as not being able to contribute.
3266 if (!vdev_readable(vd) ||
3267 (faulting && vd->vdev_parent != NULL &&
3268 vd->vdev_parent->vdev_ops == &vdev_replacing_ops)) {
3269 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
3270 } else {
3271 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
3272 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
3276 * If the vdev was resilvering or rebuilding and no longer
3277 * has any DTLs then reset the appropriate flag and dirty
3278 * the top level so that we persist the change.
3280 if (txg != 0 &&
3281 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
3282 range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) {
3283 if (vd->vdev_rebuild_txg != 0) {
3284 vd->vdev_rebuild_txg = 0;
3285 vdev_config_dirty(vd->vdev_top);
3286 } else if (vd->vdev_resilver_txg != 0) {
3287 vd->vdev_resilver_txg = 0;
3288 vdev_config_dirty(vd->vdev_top);
3292 mutex_exit(&vd->vdev_dtl_lock);
3294 if (txg != 0)
3295 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
3296 } else {
3297 mutex_enter(&vd->vdev_dtl_lock);
3298 for (int t = 0; t < DTL_TYPES; t++) {
3299 /* account for child's outage in parent's missing map */
3300 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
3301 if (t == DTL_SCRUB) {
3302 /* leaf vdevs only */
3303 continue;
3305 if (t == DTL_PARTIAL) {
3306 /* i.e. non-zero */
3307 minref = 1;
3308 } else if (vdev_get_nparity(vd) != 0) {
3309 /* RAIDZ, DRAID */
3310 minref = vdev_get_nparity(vd) + 1;
3311 } else {
3312 /* any kind of mirror */
3313 minref = vd->vdev_children;
3315 space_reftree_create(&reftree);
3316 for (int c = 0; c < vd->vdev_children; c++) {
3317 vdev_t *cvd = vd->vdev_child[c];
3318 mutex_enter(&cvd->vdev_dtl_lock);
3319 space_reftree_add_map(&reftree,
3320 cvd->vdev_dtl[s], 1);
3321 mutex_exit(&cvd->vdev_dtl_lock);
3323 space_reftree_generate_map(&reftree,
3324 vd->vdev_dtl[t], minref);
3325 space_reftree_destroy(&reftree);
3327 mutex_exit(&vd->vdev_dtl_lock);
3330 if (vd->vdev_top->vdev_ops == &vdev_raidz_ops) {
3331 raidz_dtl_reassessed(vd);
3335 void
3336 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg,
3337 boolean_t scrub_done, boolean_t rebuild_done)
3339 return (vdev_dtl_reassess_impl(vd, txg, scrub_txg, scrub_done,
3340 rebuild_done, B_FALSE));
3344 * Iterate over all the vdevs except spare, and post kobj events
3346 void
3347 vdev_post_kobj_evt(vdev_t *vd)
3349 if (vd->vdev_ops->vdev_op_kobj_evt_post &&
3350 vd->vdev_kobj_flag == B_FALSE) {
3351 vd->vdev_kobj_flag = B_TRUE;
3352 vd->vdev_ops->vdev_op_kobj_evt_post(vd);
3355 for (int c = 0; c < vd->vdev_children; c++)
3356 vdev_post_kobj_evt(vd->vdev_child[c]);
3360 * Iterate over all the vdevs except spare, and clear kobj events
3362 void
3363 vdev_clear_kobj_evt(vdev_t *vd)
3365 vd->vdev_kobj_flag = B_FALSE;
3367 for (int c = 0; c < vd->vdev_children; c++)
3368 vdev_clear_kobj_evt(vd->vdev_child[c]);
3372 vdev_dtl_load(vdev_t *vd)
3374 spa_t *spa = vd->vdev_spa;
3375 objset_t *mos = spa->spa_meta_objset;
3376 range_tree_t *rt;
3377 int error = 0;
3379 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
3380 ASSERT(vdev_is_concrete(vd));
3383 * If the dtl cannot be sync'd there is no need to open it.
3385 if (spa->spa_mode == SPA_MODE_READ && !spa->spa_read_spacemaps)
3386 return (0);
3388 error = space_map_open(&vd->vdev_dtl_sm, mos,
3389 vd->vdev_dtl_object, 0, -1ULL, 0);
3390 if (error)
3391 return (error);
3392 ASSERT(vd->vdev_dtl_sm != NULL);
3394 rt = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
3395 error = space_map_load(vd->vdev_dtl_sm, rt, SM_ALLOC);
3396 if (error == 0) {
3397 mutex_enter(&vd->vdev_dtl_lock);
3398 range_tree_walk(rt, range_tree_add,
3399 vd->vdev_dtl[DTL_MISSING]);
3400 mutex_exit(&vd->vdev_dtl_lock);
3403 range_tree_vacate(rt, NULL, NULL);
3404 range_tree_destroy(rt);
3406 return (error);
3409 for (int c = 0; c < vd->vdev_children; c++) {
3410 error = vdev_dtl_load(vd->vdev_child[c]);
3411 if (error != 0)
3412 break;
3415 return (error);
3418 static void
3419 vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx)
3421 spa_t *spa = vd->vdev_spa;
3422 objset_t *mos = spa->spa_meta_objset;
3423 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
3424 const char *string;
3426 ASSERT(alloc_bias != VDEV_BIAS_NONE);
3428 string =
3429 (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG :
3430 (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL :
3431 (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL;
3433 ASSERT(string != NULL);
3434 VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS,
3435 1, strlen(string) + 1, string, tx));
3437 if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) {
3438 spa_activate_allocation_classes(spa, tx);
3442 void
3443 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
3445 spa_t *spa = vd->vdev_spa;
3447 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
3448 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
3449 zapobj, tx));
3452 uint64_t
3453 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
3455 spa_t *spa = vd->vdev_spa;
3456 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
3457 DMU_OT_NONE, 0, tx);
3459 ASSERT(zap != 0);
3460 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
3461 zap, tx));
3463 return (zap);
3466 void
3467 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
3469 if (vd->vdev_ops != &vdev_hole_ops &&
3470 vd->vdev_ops != &vdev_missing_ops &&
3471 vd->vdev_ops != &vdev_root_ops &&
3472 !vd->vdev_top->vdev_removing) {
3473 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
3474 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
3476 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
3477 vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
3478 if (vd->vdev_alloc_bias != VDEV_BIAS_NONE)
3479 vdev_zap_allocation_data(vd, tx);
3482 if (vd->vdev_ops == &vdev_root_ops && vd->vdev_root_zap == 0 &&
3483 spa_feature_is_enabled(vd->vdev_spa, SPA_FEATURE_AVZ_V2)) {
3484 if (!spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_AVZ_V2))
3485 spa_feature_incr(vd->vdev_spa, SPA_FEATURE_AVZ_V2, tx);
3486 vd->vdev_root_zap = vdev_create_link_zap(vd, tx);
3489 for (uint64_t i = 0; i < vd->vdev_children; i++) {
3490 vdev_construct_zaps(vd->vdev_child[i], tx);
3494 static void
3495 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
3497 spa_t *spa = vd->vdev_spa;
3498 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
3499 objset_t *mos = spa->spa_meta_objset;
3500 range_tree_t *rtsync;
3501 dmu_tx_t *tx;
3502 uint64_t object = space_map_object(vd->vdev_dtl_sm);
3504 ASSERT(vdev_is_concrete(vd));
3505 ASSERT(vd->vdev_ops->vdev_op_leaf);
3507 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3509 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
3510 mutex_enter(&vd->vdev_dtl_lock);
3511 space_map_free(vd->vdev_dtl_sm, tx);
3512 space_map_close(vd->vdev_dtl_sm);
3513 vd->vdev_dtl_sm = NULL;
3514 mutex_exit(&vd->vdev_dtl_lock);
3517 * We only destroy the leaf ZAP for detached leaves or for
3518 * removed log devices. Removed data devices handle leaf ZAP
3519 * cleanup later, once cancellation is no longer possible.
3521 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
3522 vd->vdev_top->vdev_islog)) {
3523 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
3524 vd->vdev_leaf_zap = 0;
3527 dmu_tx_commit(tx);
3528 return;
3531 if (vd->vdev_dtl_sm == NULL) {
3532 uint64_t new_object;
3534 new_object = space_map_alloc(mos, zfs_vdev_dtl_sm_blksz, tx);
3535 VERIFY3U(new_object, !=, 0);
3537 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
3538 0, -1ULL, 0));
3539 ASSERT(vd->vdev_dtl_sm != NULL);
3542 rtsync = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
3544 mutex_enter(&vd->vdev_dtl_lock);
3545 range_tree_walk(rt, range_tree_add, rtsync);
3546 mutex_exit(&vd->vdev_dtl_lock);
3548 space_map_truncate(vd->vdev_dtl_sm, zfs_vdev_dtl_sm_blksz, tx);
3549 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx);
3550 range_tree_vacate(rtsync, NULL, NULL);
3552 range_tree_destroy(rtsync);
3555 * If the object for the space map has changed then dirty
3556 * the top level so that we update the config.
3558 if (object != space_map_object(vd->vdev_dtl_sm)) {
3559 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
3560 "new object %llu", (u_longlong_t)txg, spa_name(spa),
3561 (u_longlong_t)object,
3562 (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
3563 vdev_config_dirty(vd->vdev_top);
3566 dmu_tx_commit(tx);
3570 * Determine whether the specified vdev can be
3571 * - offlined
3572 * - detached
3573 * - removed
3574 * - faulted
3575 * without losing data.
3577 boolean_t
3578 vdev_dtl_required(vdev_t *vd)
3580 spa_t *spa = vd->vdev_spa;
3581 vdev_t *tvd = vd->vdev_top;
3582 uint8_t cant_read = vd->vdev_cant_read;
3583 boolean_t required;
3584 boolean_t faulting = vd->vdev_state == VDEV_STATE_FAULTED;
3586 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3588 if (vd == spa->spa_root_vdev || vd == tvd)
3589 return (B_TRUE);
3592 * Temporarily mark the device as unreadable, and then determine
3593 * whether this results in any DTL outages in the top-level vdev.
3594 * If not, we can safely offline/detach/remove the device.
3596 vd->vdev_cant_read = B_TRUE;
3597 vdev_dtl_reassess_impl(tvd, 0, 0, B_FALSE, B_FALSE, faulting);
3598 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
3599 vd->vdev_cant_read = cant_read;
3600 vdev_dtl_reassess_impl(tvd, 0, 0, B_FALSE, B_FALSE, faulting);
3602 if (!required && zio_injection_enabled) {
3603 required = !!zio_handle_device_injection(vd, NULL,
3604 SET_ERROR(ECHILD));
3607 return (required);
3611 * Determine if resilver is needed, and if so the txg range.
3613 boolean_t
3614 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
3616 boolean_t needed = B_FALSE;
3617 uint64_t thismin = UINT64_MAX;
3618 uint64_t thismax = 0;
3620 if (vd->vdev_children == 0) {
3621 mutex_enter(&vd->vdev_dtl_lock);
3622 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
3623 vdev_writeable(vd)) {
3625 thismin = vdev_dtl_min(vd);
3626 thismax = vdev_dtl_max(vd);
3627 needed = B_TRUE;
3629 mutex_exit(&vd->vdev_dtl_lock);
3630 } else {
3631 for (int c = 0; c < vd->vdev_children; c++) {
3632 vdev_t *cvd = vd->vdev_child[c];
3633 uint64_t cmin, cmax;
3635 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
3636 thismin = MIN(thismin, cmin);
3637 thismax = MAX(thismax, cmax);
3638 needed = B_TRUE;
3643 if (needed && minp) {
3644 *minp = thismin;
3645 *maxp = thismax;
3647 return (needed);
3651 * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj
3652 * will contain either the checkpoint spacemap object or zero if none exists.
3653 * All other errors are returned to the caller.
3656 vdev_checkpoint_sm_object(vdev_t *vd, uint64_t *sm_obj)
3658 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
3660 if (vd->vdev_top_zap == 0) {
3661 *sm_obj = 0;
3662 return (0);
3665 int error = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap,
3666 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, sm_obj);
3667 if (error == ENOENT) {
3668 *sm_obj = 0;
3669 error = 0;
3672 return (error);
3676 vdev_load(vdev_t *vd)
3678 int children = vd->vdev_children;
3679 int error = 0;
3680 taskq_t *tq = NULL;
3683 * It's only worthwhile to use the taskq for the root vdev, because the
3684 * slow part is metaslab_init, and that only happens for top-level
3685 * vdevs.
3687 if (vd->vdev_ops == &vdev_root_ops && vd->vdev_children > 0) {
3688 tq = taskq_create("vdev_load", children, minclsyspri,
3689 children, children, TASKQ_PREPOPULATE);
3693 * Recursively load all children.
3695 for (int c = 0; c < vd->vdev_children; c++) {
3696 vdev_t *cvd = vd->vdev_child[c];
3698 if (tq == NULL || vdev_uses_zvols(cvd)) {
3699 cvd->vdev_load_error = vdev_load(cvd);
3700 } else {
3701 VERIFY(taskq_dispatch(tq, vdev_load_child,
3702 cvd, TQ_SLEEP) != TASKQID_INVALID);
3706 if (tq != NULL) {
3707 taskq_wait(tq);
3708 taskq_destroy(tq);
3711 for (int c = 0; c < vd->vdev_children; c++) {
3712 int error = vd->vdev_child[c]->vdev_load_error;
3714 if (error != 0)
3715 return (error);
3718 vdev_set_deflate_ratio(vd);
3720 if (vd->vdev_ops == &vdev_raidz_ops) {
3721 error = vdev_raidz_load(vd);
3722 if (error != 0)
3723 return (error);
3727 * On spa_load path, grab the allocation bias from our zap
3729 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3730 spa_t *spa = vd->vdev_spa;
3731 char bias_str[64];
3733 error = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
3734 VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str),
3735 bias_str);
3736 if (error == 0) {
3737 ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE);
3738 vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str);
3739 } else if (error != ENOENT) {
3740 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3741 VDEV_AUX_CORRUPT_DATA);
3742 vdev_dbgmsg(vd, "vdev_load: zap_lookup(top_zap=%llu) "
3743 "failed [error=%d]",
3744 (u_longlong_t)vd->vdev_top_zap, error);
3745 return (error);
3749 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3750 spa_t *spa = vd->vdev_spa;
3751 uint64_t failfast;
3753 error = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
3754 vdev_prop_to_name(VDEV_PROP_FAILFAST), sizeof (failfast),
3755 1, &failfast);
3756 if (error == 0) {
3757 vd->vdev_failfast = failfast & 1;
3758 } else if (error == ENOENT) {
3759 vd->vdev_failfast = vdev_prop_default_numeric(
3760 VDEV_PROP_FAILFAST);
3761 } else {
3762 vdev_dbgmsg(vd,
3763 "vdev_load: zap_lookup(top_zap=%llu) "
3764 "failed [error=%d]",
3765 (u_longlong_t)vd->vdev_top_zap, error);
3770 * Load any rebuild state from the top-level vdev zap.
3772 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3773 error = vdev_rebuild_load(vd);
3774 if (error && error != ENOTSUP) {
3775 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3776 VDEV_AUX_CORRUPT_DATA);
3777 vdev_dbgmsg(vd, "vdev_load: vdev_rebuild_load "
3778 "failed [error=%d]", error);
3779 return (error);
3783 if (vd->vdev_top_zap != 0 || vd->vdev_leaf_zap != 0) {
3784 uint64_t zapobj;
3786 if (vd->vdev_top_zap != 0)
3787 zapobj = vd->vdev_top_zap;
3788 else
3789 zapobj = vd->vdev_leaf_zap;
3791 error = vdev_prop_get_int(vd, VDEV_PROP_CHECKSUM_N,
3792 &vd->vdev_checksum_n);
3793 if (error && error != ENOENT)
3794 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3795 "failed [error=%d]", (u_longlong_t)zapobj, error);
3797 error = vdev_prop_get_int(vd, VDEV_PROP_CHECKSUM_T,
3798 &vd->vdev_checksum_t);
3799 if (error && error != ENOENT)
3800 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3801 "failed [error=%d]", (u_longlong_t)zapobj, error);
3803 error = vdev_prop_get_int(vd, VDEV_PROP_IO_N,
3804 &vd->vdev_io_n);
3805 if (error && error != ENOENT)
3806 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3807 "failed [error=%d]", (u_longlong_t)zapobj, error);
3809 error = vdev_prop_get_int(vd, VDEV_PROP_IO_T,
3810 &vd->vdev_io_t);
3811 if (error && error != ENOENT)
3812 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3813 "failed [error=%d]", (u_longlong_t)zapobj, error);
3815 error = vdev_prop_get_int(vd, VDEV_PROP_SLOW_IO_N,
3816 &vd->vdev_slow_io_n);
3817 if (error && error != ENOENT)
3818 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3819 "failed [error=%d]", (u_longlong_t)zapobj, error);
3821 error = vdev_prop_get_int(vd, VDEV_PROP_SLOW_IO_T,
3822 &vd->vdev_slow_io_t);
3823 if (error && error != ENOENT)
3824 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3825 "failed [error=%d]", (u_longlong_t)zapobj, error);
3829 * If this is a top-level vdev, initialize its metaslabs.
3831 if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
3832 vdev_metaslab_group_create(vd);
3834 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
3835 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3836 VDEV_AUX_CORRUPT_DATA);
3837 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
3838 "asize=%llu", (u_longlong_t)vd->vdev_ashift,
3839 (u_longlong_t)vd->vdev_asize);
3840 return (SET_ERROR(ENXIO));
3843 error = vdev_metaslab_init(vd, 0);
3844 if (error != 0) {
3845 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
3846 "[error=%d]", error);
3847 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3848 VDEV_AUX_CORRUPT_DATA);
3849 return (error);
3852 uint64_t checkpoint_sm_obj;
3853 error = vdev_checkpoint_sm_object(vd, &checkpoint_sm_obj);
3854 if (error == 0 && checkpoint_sm_obj != 0) {
3855 objset_t *mos = spa_meta_objset(vd->vdev_spa);
3856 ASSERT(vd->vdev_asize != 0);
3857 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL);
3859 error = space_map_open(&vd->vdev_checkpoint_sm,
3860 mos, checkpoint_sm_obj, 0, vd->vdev_asize,
3861 vd->vdev_ashift);
3862 if (error != 0) {
3863 vdev_dbgmsg(vd, "vdev_load: space_map_open "
3864 "failed for checkpoint spacemap (obj %llu) "
3865 "[error=%d]",
3866 (u_longlong_t)checkpoint_sm_obj, error);
3867 return (error);
3869 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
3872 * Since the checkpoint_sm contains free entries
3873 * exclusively we can use space_map_allocated() to
3874 * indicate the cumulative checkpointed space that
3875 * has been freed.
3877 vd->vdev_stat.vs_checkpoint_space =
3878 -space_map_allocated(vd->vdev_checkpoint_sm);
3879 vd->vdev_spa->spa_checkpoint_info.sci_dspace +=
3880 vd->vdev_stat.vs_checkpoint_space;
3881 } else if (error != 0) {
3882 vdev_dbgmsg(vd, "vdev_load: failed to retrieve "
3883 "checkpoint space map object from vdev ZAP "
3884 "[error=%d]", error);
3885 return (error);
3890 * If this is a leaf vdev, load its DTL.
3892 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
3893 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3894 VDEV_AUX_CORRUPT_DATA);
3895 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
3896 "[error=%d]", error);
3897 return (error);
3900 uint64_t obsolete_sm_object;
3901 error = vdev_obsolete_sm_object(vd, &obsolete_sm_object);
3902 if (error == 0 && obsolete_sm_object != 0) {
3903 objset_t *mos = vd->vdev_spa->spa_meta_objset;
3904 ASSERT(vd->vdev_asize != 0);
3905 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
3907 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
3908 obsolete_sm_object, 0, vd->vdev_asize, 0))) {
3909 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3910 VDEV_AUX_CORRUPT_DATA);
3911 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
3912 "obsolete spacemap (obj %llu) [error=%d]",
3913 (u_longlong_t)obsolete_sm_object, error);
3914 return (error);
3916 } else if (error != 0) {
3917 vdev_dbgmsg(vd, "vdev_load: failed to retrieve obsolete "
3918 "space map object from vdev ZAP [error=%d]", error);
3919 return (error);
3922 return (0);
3926 * The special vdev case is used for hot spares and l2cache devices. Its
3927 * sole purpose it to set the vdev state for the associated vdev. To do this,
3928 * we make sure that we can open the underlying device, then try to read the
3929 * label, and make sure that the label is sane and that it hasn't been
3930 * repurposed to another pool.
3933 vdev_validate_aux(vdev_t *vd)
3935 nvlist_t *label;
3936 uint64_t guid, version;
3937 uint64_t state;
3939 if (!vdev_readable(vd))
3940 return (0);
3942 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
3943 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3944 VDEV_AUX_CORRUPT_DATA);
3945 return (-1);
3948 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
3949 !SPA_VERSION_IS_SUPPORTED(version) ||
3950 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
3951 guid != vd->vdev_guid ||
3952 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
3953 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3954 VDEV_AUX_CORRUPT_DATA);
3955 nvlist_free(label);
3956 return (-1);
3960 * We don't actually check the pool state here. If it's in fact in
3961 * use by another pool, we update this fact on the fly when requested.
3963 nvlist_free(label);
3964 return (0);
3967 static void
3968 vdev_destroy_ms_flush_data(vdev_t *vd, dmu_tx_t *tx)
3970 objset_t *mos = spa_meta_objset(vd->vdev_spa);
3972 if (vd->vdev_top_zap == 0)
3973 return;
3975 uint64_t object = 0;
3976 int err = zap_lookup(mos, vd->vdev_top_zap,
3977 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, sizeof (uint64_t), 1, &object);
3978 if (err == ENOENT)
3979 return;
3980 VERIFY0(err);
3982 VERIFY0(dmu_object_free(mos, object, tx));
3983 VERIFY0(zap_remove(mos, vd->vdev_top_zap,
3984 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, tx));
3988 * Free the objects used to store this vdev's spacemaps, and the array
3989 * that points to them.
3991 void
3992 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
3994 if (vd->vdev_ms_array == 0)
3995 return;
3997 objset_t *mos = vd->vdev_spa->spa_meta_objset;
3998 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
3999 size_t array_bytes = array_count * sizeof (uint64_t);
4000 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
4001 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
4002 array_bytes, smobj_array, 0));
4004 for (uint64_t i = 0; i < array_count; i++) {
4005 uint64_t smobj = smobj_array[i];
4006 if (smobj == 0)
4007 continue;
4009 space_map_free_obj(mos, smobj, tx);
4012 kmem_free(smobj_array, array_bytes);
4013 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
4014 vdev_destroy_ms_flush_data(vd, tx);
4015 vd->vdev_ms_array = 0;
4018 static void
4019 vdev_remove_empty_log(vdev_t *vd, uint64_t txg)
4021 spa_t *spa = vd->vdev_spa;
4023 ASSERT(vd->vdev_islog);
4024 ASSERT(vd == vd->vdev_top);
4025 ASSERT3U(txg, ==, spa_syncing_txg(spa));
4027 dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
4029 vdev_destroy_spacemaps(vd, tx);
4030 if (vd->vdev_top_zap != 0) {
4031 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
4032 vd->vdev_top_zap = 0;
4035 dmu_tx_commit(tx);
4038 void
4039 vdev_sync_done(vdev_t *vd, uint64_t txg)
4041 metaslab_t *msp;
4042 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
4044 ASSERT(vdev_is_concrete(vd));
4046 while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
4047 != NULL)
4048 metaslab_sync_done(msp, txg);
4050 if (reassess) {
4051 metaslab_sync_reassess(vd->vdev_mg);
4052 if (vd->vdev_log_mg != NULL)
4053 metaslab_sync_reassess(vd->vdev_log_mg);
4057 void
4058 vdev_sync(vdev_t *vd, uint64_t txg)
4060 spa_t *spa = vd->vdev_spa;
4061 vdev_t *lvd;
4062 metaslab_t *msp;
4064 ASSERT3U(txg, ==, spa->spa_syncing_txg);
4065 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
4066 if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
4067 ASSERT(vd->vdev_removing ||
4068 vd->vdev_ops == &vdev_indirect_ops);
4070 vdev_indirect_sync_obsolete(vd, tx);
4073 * If the vdev is indirect, it can't have dirty
4074 * metaslabs or DTLs.
4076 if (vd->vdev_ops == &vdev_indirect_ops) {
4077 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
4078 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
4079 dmu_tx_commit(tx);
4080 return;
4084 ASSERT(vdev_is_concrete(vd));
4086 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
4087 !vd->vdev_removing) {
4088 ASSERT(vd == vd->vdev_top);
4089 ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
4090 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
4091 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
4092 ASSERT(vd->vdev_ms_array != 0);
4093 vdev_config_dirty(vd);
4096 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
4097 metaslab_sync(msp, txg);
4098 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
4101 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
4102 vdev_dtl_sync(lvd, txg);
4105 * If this is an empty log device being removed, destroy the
4106 * metadata associated with it.
4108 if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
4109 vdev_remove_empty_log(vd, txg);
4111 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
4112 dmu_tx_commit(tx);
4116 * Return the amount of space that should be (or was) allocated for the given
4117 * psize (compressed block size) in the given TXG. Note that for expanded
4118 * RAIDZ vdevs, the size allocated for older BP's may be larger. See
4119 * vdev_raidz_asize().
4121 uint64_t
4122 vdev_psize_to_asize_txg(vdev_t *vd, uint64_t psize, uint64_t txg)
4124 return (vd->vdev_ops->vdev_op_asize(vd, psize, txg));
4127 uint64_t
4128 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
4130 return (vdev_psize_to_asize_txg(vd, psize, 0));
4134 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
4135 * not be opened, and no I/O is attempted.
4138 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
4140 vdev_t *vd, *tvd;
4142 spa_vdev_state_enter(spa, SCL_NONE);
4144 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4145 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4147 if (!vd->vdev_ops->vdev_op_leaf)
4148 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4150 tvd = vd->vdev_top;
4153 * If user did a 'zpool offline -f' then make the fault persist across
4154 * reboots.
4156 if (aux == VDEV_AUX_EXTERNAL_PERSIST) {
4158 * There are two kinds of forced faults: temporary and
4159 * persistent. Temporary faults go away at pool import, while
4160 * persistent faults stay set. Both types of faults can be
4161 * cleared with a zpool clear.
4163 * We tell if a vdev is persistently faulted by looking at the
4164 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
4165 * import then it's a persistent fault. Otherwise, it's
4166 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
4167 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
4168 * tells vdev_config_generate() (which gets run later) to set
4169 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
4171 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL;
4172 vd->vdev_tmpoffline = B_FALSE;
4173 aux = VDEV_AUX_EXTERNAL;
4174 } else {
4175 vd->vdev_tmpoffline = B_TRUE;
4179 * We don't directly use the aux state here, but if we do a
4180 * vdev_reopen(), we need this value to be present to remember why we
4181 * were faulted.
4183 vd->vdev_label_aux = aux;
4186 * Faulted state takes precedence over degraded.
4188 vd->vdev_delayed_close = B_FALSE;
4189 vd->vdev_faulted = 1ULL;
4190 vd->vdev_degraded = 0ULL;
4191 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
4194 * If this device has the only valid copy of the data, then
4195 * back off and simply mark the vdev as degraded instead.
4197 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
4198 vd->vdev_degraded = 1ULL;
4199 vd->vdev_faulted = 0ULL;
4202 * If we reopen the device and it's not dead, only then do we
4203 * mark it degraded.
4205 vdev_reopen(tvd);
4207 if (vdev_readable(vd))
4208 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
4211 return (spa_vdev_state_exit(spa, vd, 0));
4215 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
4216 * user that something is wrong. The vdev continues to operate as normal as far
4217 * as I/O is concerned.
4220 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
4222 vdev_t *vd;
4224 spa_vdev_state_enter(spa, SCL_NONE);
4226 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4227 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4229 if (!vd->vdev_ops->vdev_op_leaf)
4230 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4233 * If the vdev is already faulted, then don't do anything.
4235 if (vd->vdev_faulted || vd->vdev_degraded)
4236 return (spa_vdev_state_exit(spa, NULL, 0));
4238 vd->vdev_degraded = 1ULL;
4239 if (!vdev_is_dead(vd))
4240 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
4241 aux);
4243 return (spa_vdev_state_exit(spa, vd, 0));
4247 vdev_remove_wanted(spa_t *spa, uint64_t guid)
4249 vdev_t *vd;
4251 spa_vdev_state_enter(spa, SCL_NONE);
4253 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4254 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4257 * If the vdev is already removed, or expanding which can trigger
4258 * repartition add/remove events, then don't do anything.
4260 if (vd->vdev_removed || vd->vdev_expanding)
4261 return (spa_vdev_state_exit(spa, NULL, 0));
4264 * Confirm the vdev has been removed, otherwise don't do anything.
4266 if (vd->vdev_ops->vdev_op_leaf && !zio_wait(vdev_probe(vd, NULL)))
4267 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(EEXIST)));
4269 vd->vdev_remove_wanted = B_TRUE;
4270 spa_async_request(spa, SPA_ASYNC_REMOVE);
4272 return (spa_vdev_state_exit(spa, vd, 0));
4277 * Online the given vdev.
4279 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
4280 * spare device should be detached when the device finishes resilvering.
4281 * Second, the online should be treated like a 'test' online case, so no FMA
4282 * events are generated if the device fails to open.
4285 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
4287 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
4288 boolean_t wasoffline;
4289 vdev_state_t oldstate;
4291 spa_vdev_state_enter(spa, SCL_NONE);
4293 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4294 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4296 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
4297 oldstate = vd->vdev_state;
4299 tvd = vd->vdev_top;
4300 vd->vdev_offline = B_FALSE;
4301 vd->vdev_tmpoffline = B_FALSE;
4302 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
4303 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
4305 /* XXX - L2ARC 1.0 does not support expansion */
4306 if (!vd->vdev_aux) {
4307 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
4308 pvd->vdev_expanding = !!((flags & ZFS_ONLINE_EXPAND) ||
4309 spa->spa_autoexpand);
4310 vd->vdev_expansion_time = gethrestime_sec();
4313 vdev_reopen(tvd);
4314 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
4316 if (!vd->vdev_aux) {
4317 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
4318 pvd->vdev_expanding = B_FALSE;
4321 if (newstate)
4322 *newstate = vd->vdev_state;
4323 if ((flags & ZFS_ONLINE_UNSPARE) &&
4324 !vdev_is_dead(vd) && vd->vdev_parent &&
4325 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4326 vd->vdev_parent->vdev_child[0] == vd)
4327 vd->vdev_unspare = B_TRUE;
4329 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
4331 /* XXX - L2ARC 1.0 does not support expansion */
4332 if (vd->vdev_aux)
4333 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
4334 spa->spa_ccw_fail_time = 0;
4335 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
4338 /* Restart initializing if necessary */
4339 mutex_enter(&vd->vdev_initialize_lock);
4340 if (vdev_writeable(vd) &&
4341 vd->vdev_initialize_thread == NULL &&
4342 vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) {
4343 (void) vdev_initialize(vd);
4345 mutex_exit(&vd->vdev_initialize_lock);
4348 * Restart trimming if necessary. We do not restart trimming for cache
4349 * devices here. This is triggered by l2arc_rebuild_vdev()
4350 * asynchronously for the whole device or in l2arc_evict() as it evicts
4351 * space for upcoming writes.
4353 mutex_enter(&vd->vdev_trim_lock);
4354 if (vdev_writeable(vd) && !vd->vdev_isl2cache &&
4355 vd->vdev_trim_thread == NULL &&
4356 vd->vdev_trim_state == VDEV_TRIM_ACTIVE) {
4357 (void) vdev_trim(vd, vd->vdev_trim_rate, vd->vdev_trim_partial,
4358 vd->vdev_trim_secure);
4360 mutex_exit(&vd->vdev_trim_lock);
4362 if (wasoffline ||
4363 (oldstate < VDEV_STATE_DEGRADED &&
4364 vd->vdev_state >= VDEV_STATE_DEGRADED)) {
4365 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
4368 * Asynchronously detach spare vdev if resilver or
4369 * rebuild is not required
4371 if (vd->vdev_unspare &&
4372 !dsl_scan_resilvering(spa->spa_dsl_pool) &&
4373 !dsl_scan_resilver_scheduled(spa->spa_dsl_pool) &&
4374 !vdev_rebuild_active(tvd))
4375 spa_async_request(spa, SPA_ASYNC_DETACH_SPARE);
4377 return (spa_vdev_state_exit(spa, vd, 0));
4380 static int
4381 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
4383 vdev_t *vd, *tvd;
4384 int error = 0;
4385 uint64_t generation;
4386 metaslab_group_t *mg;
4388 top:
4389 spa_vdev_state_enter(spa, SCL_ALLOC);
4391 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4392 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4394 if (!vd->vdev_ops->vdev_op_leaf)
4395 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4397 if (vd->vdev_ops == &vdev_draid_spare_ops)
4398 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
4400 tvd = vd->vdev_top;
4401 mg = tvd->vdev_mg;
4402 generation = spa->spa_config_generation + 1;
4405 * If the device isn't already offline, try to offline it.
4407 if (!vd->vdev_offline) {
4409 * If this device has the only valid copy of some data,
4410 * don't allow it to be offlined. Log devices are always
4411 * expendable.
4413 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
4414 vdev_dtl_required(vd))
4415 return (spa_vdev_state_exit(spa, NULL,
4416 SET_ERROR(EBUSY)));
4419 * If the top-level is a slog and it has had allocations
4420 * then proceed. We check that the vdev's metaslab group
4421 * is not NULL since it's possible that we may have just
4422 * added this vdev but not yet initialized its metaslabs.
4424 if (tvd->vdev_islog && mg != NULL) {
4426 * Prevent any future allocations.
4428 ASSERT3P(tvd->vdev_log_mg, ==, NULL);
4429 metaslab_group_passivate(mg);
4430 (void) spa_vdev_state_exit(spa, vd, 0);
4432 error = spa_reset_logs(spa);
4435 * If the log device was successfully reset but has
4436 * checkpointed data, do not offline it.
4438 if (error == 0 &&
4439 tvd->vdev_checkpoint_sm != NULL) {
4440 ASSERT3U(space_map_allocated(
4441 tvd->vdev_checkpoint_sm), !=, 0);
4442 error = ZFS_ERR_CHECKPOINT_EXISTS;
4445 spa_vdev_state_enter(spa, SCL_ALLOC);
4448 * Check to see if the config has changed.
4450 if (error || generation != spa->spa_config_generation) {
4451 metaslab_group_activate(mg);
4452 if (error)
4453 return (spa_vdev_state_exit(spa,
4454 vd, error));
4455 (void) spa_vdev_state_exit(spa, vd, 0);
4456 goto top;
4458 ASSERT0(tvd->vdev_stat.vs_alloc);
4462 * Offline this device and reopen its top-level vdev.
4463 * If the top-level vdev is a log device then just offline
4464 * it. Otherwise, if this action results in the top-level
4465 * vdev becoming unusable, undo it and fail the request.
4467 vd->vdev_offline = B_TRUE;
4468 vdev_reopen(tvd);
4470 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
4471 vdev_is_dead(tvd)) {
4472 vd->vdev_offline = B_FALSE;
4473 vdev_reopen(tvd);
4474 return (spa_vdev_state_exit(spa, NULL,
4475 SET_ERROR(EBUSY)));
4479 * Add the device back into the metaslab rotor so that
4480 * once we online the device it's open for business.
4482 if (tvd->vdev_islog && mg != NULL)
4483 metaslab_group_activate(mg);
4486 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
4488 return (spa_vdev_state_exit(spa, vd, 0));
4492 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
4494 int error;
4496 mutex_enter(&spa->spa_vdev_top_lock);
4497 error = vdev_offline_locked(spa, guid, flags);
4498 mutex_exit(&spa->spa_vdev_top_lock);
4500 return (error);
4504 * Clear the error counts associated with this vdev. Unlike vdev_online() and
4505 * vdev_offline(), we assume the spa config is locked. We also clear all
4506 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
4508 void
4509 vdev_clear(spa_t *spa, vdev_t *vd)
4511 vdev_t *rvd = spa->spa_root_vdev;
4513 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
4515 if (vd == NULL)
4516 vd = rvd;
4518 vd->vdev_stat.vs_read_errors = 0;
4519 vd->vdev_stat.vs_write_errors = 0;
4520 vd->vdev_stat.vs_checksum_errors = 0;
4521 vd->vdev_stat.vs_dio_verify_errors = 0;
4522 vd->vdev_stat.vs_slow_ios = 0;
4524 for (int c = 0; c < vd->vdev_children; c++)
4525 vdev_clear(spa, vd->vdev_child[c]);
4528 * It makes no sense to "clear" an indirect or removed vdev.
4530 if (!vdev_is_concrete(vd) || vd->vdev_removed)
4531 return;
4534 * If we're in the FAULTED state or have experienced failed I/O, then
4535 * clear the persistent state and attempt to reopen the device. We
4536 * also mark the vdev config dirty, so that the new faulted state is
4537 * written out to disk.
4539 if (vd->vdev_faulted || vd->vdev_degraded ||
4540 !vdev_readable(vd) || !vdev_writeable(vd)) {
4542 * When reopening in response to a clear event, it may be due to
4543 * a fmadm repair request. In this case, if the device is
4544 * still broken, we want to still post the ereport again.
4546 vd->vdev_forcefault = B_TRUE;
4548 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
4549 vd->vdev_cant_read = B_FALSE;
4550 vd->vdev_cant_write = B_FALSE;
4551 vd->vdev_stat.vs_aux = 0;
4553 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
4555 vd->vdev_forcefault = B_FALSE;
4557 if (vd != rvd && vdev_writeable(vd->vdev_top))
4558 vdev_state_dirty(vd->vdev_top);
4560 /* If a resilver isn't required, check if vdevs can be culled */
4561 if (vd->vdev_aux == NULL && !vdev_is_dead(vd) &&
4562 !dsl_scan_resilvering(spa->spa_dsl_pool) &&
4563 !dsl_scan_resilver_scheduled(spa->spa_dsl_pool))
4564 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
4566 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
4570 * When clearing a FMA-diagnosed fault, we always want to
4571 * unspare the device, as we assume that the original spare was
4572 * done in response to the FMA fault.
4574 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
4575 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4576 vd->vdev_parent->vdev_child[0] == vd)
4577 vd->vdev_unspare = B_TRUE;
4579 /* Clear recent error events cache (i.e. duplicate events tracking) */
4580 zfs_ereport_clear(spa, vd);
4583 boolean_t
4584 vdev_is_dead(vdev_t *vd)
4587 * Holes and missing devices are always considered "dead".
4588 * This simplifies the code since we don't have to check for
4589 * these types of devices in the various code paths.
4590 * Instead we rely on the fact that we skip over dead devices
4591 * before issuing I/O to them.
4593 return (vd->vdev_state < VDEV_STATE_DEGRADED ||
4594 vd->vdev_ops == &vdev_hole_ops ||
4595 vd->vdev_ops == &vdev_missing_ops);
4598 boolean_t
4599 vdev_readable(vdev_t *vd)
4601 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
4604 boolean_t
4605 vdev_writeable(vdev_t *vd)
4607 return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
4608 vdev_is_concrete(vd));
4611 boolean_t
4612 vdev_allocatable(vdev_t *vd)
4614 uint64_t state = vd->vdev_state;
4617 * We currently allow allocations from vdevs which may be in the
4618 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
4619 * fails to reopen then we'll catch it later when we're holding
4620 * the proper locks. Note that we have to get the vdev state
4621 * in a local variable because although it changes atomically,
4622 * we're asking two separate questions about it.
4624 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
4625 !vd->vdev_cant_write && vdev_is_concrete(vd) &&
4626 vd->vdev_mg->mg_initialized);
4629 boolean_t
4630 vdev_accessible(vdev_t *vd, zio_t *zio)
4632 ASSERT(zio->io_vd == vd);
4634 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
4635 return (B_FALSE);
4637 if (zio->io_type == ZIO_TYPE_READ)
4638 return (!vd->vdev_cant_read);
4640 if (zio->io_type == ZIO_TYPE_WRITE)
4641 return (!vd->vdev_cant_write);
4643 return (B_TRUE);
4646 static void
4647 vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs)
4650 * Exclude the dRAID spare when aggregating to avoid double counting
4651 * the ops and bytes. These IOs are counted by the physical leaves.
4653 if (cvd->vdev_ops == &vdev_draid_spare_ops)
4654 return;
4656 for (int t = 0; t < VS_ZIO_TYPES; t++) {
4657 vs->vs_ops[t] += cvs->vs_ops[t];
4658 vs->vs_bytes[t] += cvs->vs_bytes[t];
4661 cvs->vs_scan_removing = cvd->vdev_removing;
4665 * Get extended stats
4667 static void
4668 vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx)
4670 (void) cvd;
4672 int t, b;
4673 for (t = 0; t < ZIO_TYPES; t++) {
4674 for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++)
4675 vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b];
4677 for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) {
4678 vsx->vsx_total_histo[t][b] +=
4679 cvsx->vsx_total_histo[t][b];
4683 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
4684 for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) {
4685 vsx->vsx_queue_histo[t][b] +=
4686 cvsx->vsx_queue_histo[t][b];
4688 vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t];
4689 vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t];
4691 for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++)
4692 vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b];
4694 for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++)
4695 vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b];
4700 boolean_t
4701 vdev_is_spacemap_addressable(vdev_t *vd)
4703 if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2))
4704 return (B_TRUE);
4707 * If double-word space map entries are not enabled we assume
4708 * 47 bits of the space map entry are dedicated to the entry's
4709 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
4710 * to calculate the maximum address that can be described by a
4711 * space map entry for the given device.
4713 uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS;
4715 if (shift >= 63) /* detect potential overflow */
4716 return (B_TRUE);
4718 return (vd->vdev_asize < (1ULL << shift));
4722 * Get statistics for the given vdev.
4724 static void
4725 vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
4727 int t;
4729 * If we're getting stats on the root vdev, aggregate the I/O counts
4730 * over all top-level vdevs (i.e. the direct children of the root).
4732 if (!vd->vdev_ops->vdev_op_leaf) {
4733 if (vs) {
4734 memset(vs->vs_ops, 0, sizeof (vs->vs_ops));
4735 memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes));
4737 if (vsx)
4738 memset(vsx, 0, sizeof (*vsx));
4740 for (int c = 0; c < vd->vdev_children; c++) {
4741 vdev_t *cvd = vd->vdev_child[c];
4742 vdev_stat_t *cvs = &cvd->vdev_stat;
4743 vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex;
4745 vdev_get_stats_ex_impl(cvd, cvs, cvsx);
4746 if (vs)
4747 vdev_get_child_stat(cvd, vs, cvs);
4748 if (vsx)
4749 vdev_get_child_stat_ex(cvd, vsx, cvsx);
4751 } else {
4753 * We're a leaf. Just copy our ZIO active queue stats in. The
4754 * other leaf stats are updated in vdev_stat_update().
4756 if (!vsx)
4757 return;
4759 memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex));
4761 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
4762 vsx->vsx_active_queue[t] = vd->vdev_queue.vq_cactive[t];
4763 vsx->vsx_pend_queue[t] = vdev_queue_class_length(vd, t);
4768 void
4769 vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
4771 vdev_t *tvd = vd->vdev_top;
4772 mutex_enter(&vd->vdev_stat_lock);
4773 if (vs) {
4774 memcpy(vs, &vd->vdev_stat, sizeof (*vs));
4775 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
4776 vs->vs_state = vd->vdev_state;
4777 vs->vs_rsize = vdev_get_min_asize(vd);
4779 if (vd->vdev_ops->vdev_op_leaf) {
4780 vs->vs_pspace = vd->vdev_psize;
4781 vs->vs_rsize += VDEV_LABEL_START_SIZE +
4782 VDEV_LABEL_END_SIZE;
4784 * Report initializing progress. Since we don't
4785 * have the initializing locks held, this is only
4786 * an estimate (although a fairly accurate one).
4788 vs->vs_initialize_bytes_done =
4789 vd->vdev_initialize_bytes_done;
4790 vs->vs_initialize_bytes_est =
4791 vd->vdev_initialize_bytes_est;
4792 vs->vs_initialize_state = vd->vdev_initialize_state;
4793 vs->vs_initialize_action_time =
4794 vd->vdev_initialize_action_time;
4797 * Report manual TRIM progress. Since we don't have
4798 * the manual TRIM locks held, this is only an
4799 * estimate (although fairly accurate one).
4801 vs->vs_trim_notsup = !vd->vdev_has_trim;
4802 vs->vs_trim_bytes_done = vd->vdev_trim_bytes_done;
4803 vs->vs_trim_bytes_est = vd->vdev_trim_bytes_est;
4804 vs->vs_trim_state = vd->vdev_trim_state;
4805 vs->vs_trim_action_time = vd->vdev_trim_action_time;
4807 /* Set when there is a deferred resilver. */
4808 vs->vs_resilver_deferred = vd->vdev_resilver_deferred;
4812 * Report expandable space on top-level, non-auxiliary devices
4813 * only. The expandable space is reported in terms of metaslab
4814 * sized units since that determines how much space the pool
4815 * can expand.
4817 if (vd->vdev_aux == NULL && tvd != NULL) {
4818 vs->vs_esize = P2ALIGN_TYPED(
4819 vd->vdev_max_asize - vd->vdev_asize,
4820 1ULL << tvd->vdev_ms_shift, uint64_t);
4823 vs->vs_configured_ashift = vd->vdev_top != NULL
4824 ? vd->vdev_top->vdev_ashift : vd->vdev_ashift;
4825 vs->vs_logical_ashift = vd->vdev_logical_ashift;
4826 if (vd->vdev_physical_ashift <= ASHIFT_MAX)
4827 vs->vs_physical_ashift = vd->vdev_physical_ashift;
4828 else
4829 vs->vs_physical_ashift = 0;
4832 * Report fragmentation and rebuild progress for top-level,
4833 * non-auxiliary, concrete devices.
4835 if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
4836 vdev_is_concrete(vd)) {
4838 * The vdev fragmentation rating doesn't take into
4839 * account the embedded slog metaslab (vdev_log_mg).
4840 * Since it's only one metaslab, it would have a tiny
4841 * impact on the overall fragmentation.
4843 vs->vs_fragmentation = (vd->vdev_mg != NULL) ?
4844 vd->vdev_mg->mg_fragmentation : 0;
4846 vs->vs_noalloc = MAX(vd->vdev_noalloc,
4847 tvd ? tvd->vdev_noalloc : 0);
4850 vdev_get_stats_ex_impl(vd, vs, vsx);
4851 mutex_exit(&vd->vdev_stat_lock);
4854 void
4855 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
4857 return (vdev_get_stats_ex(vd, vs, NULL));
4860 void
4861 vdev_clear_stats(vdev_t *vd)
4863 mutex_enter(&vd->vdev_stat_lock);
4864 vd->vdev_stat.vs_space = 0;
4865 vd->vdev_stat.vs_dspace = 0;
4866 vd->vdev_stat.vs_alloc = 0;
4867 mutex_exit(&vd->vdev_stat_lock);
4870 void
4871 vdev_scan_stat_init(vdev_t *vd)
4873 vdev_stat_t *vs = &vd->vdev_stat;
4875 for (int c = 0; c < vd->vdev_children; c++)
4876 vdev_scan_stat_init(vd->vdev_child[c]);
4878 mutex_enter(&vd->vdev_stat_lock);
4879 vs->vs_scan_processed = 0;
4880 mutex_exit(&vd->vdev_stat_lock);
4883 void
4884 vdev_stat_update(zio_t *zio, uint64_t psize)
4886 spa_t *spa = zio->io_spa;
4887 vdev_t *rvd = spa->spa_root_vdev;
4888 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
4889 vdev_t *pvd;
4890 uint64_t txg = zio->io_txg;
4891 /* Suppress ASAN false positive */
4892 #ifdef __SANITIZE_ADDRESS__
4893 vdev_stat_t *vs = vd ? &vd->vdev_stat : NULL;
4894 vdev_stat_ex_t *vsx = vd ? &vd->vdev_stat_ex : NULL;
4895 #else
4896 vdev_stat_t *vs = &vd->vdev_stat;
4897 vdev_stat_ex_t *vsx = &vd->vdev_stat_ex;
4898 #endif
4899 zio_type_t type = zio->io_type;
4900 int flags = zio->io_flags;
4903 * If this i/o is a gang leader, it didn't do any actual work.
4905 if (zio->io_gang_tree)
4906 return;
4908 if (zio->io_error == 0) {
4910 * If this is a root i/o, don't count it -- we've already
4911 * counted the top-level vdevs, and vdev_get_stats() will
4912 * aggregate them when asked. This reduces contention on
4913 * the root vdev_stat_lock and implicitly handles blocks
4914 * that compress away to holes, for which there is no i/o.
4915 * (Holes never create vdev children, so all the counters
4916 * remain zero, which is what we want.)
4918 * Note: this only applies to successful i/o (io_error == 0)
4919 * because unlike i/o counts, errors are not additive.
4920 * When reading a ditto block, for example, failure of
4921 * one top-level vdev does not imply a root-level error.
4923 if (vd == rvd)
4924 return;
4926 ASSERT(vd == zio->io_vd);
4928 if (flags & ZIO_FLAG_IO_BYPASS)
4929 return;
4931 mutex_enter(&vd->vdev_stat_lock);
4933 if (flags & ZIO_FLAG_IO_REPAIR) {
4935 * Repair is the result of a resilver issued by the
4936 * scan thread (spa_sync).
4938 if (flags & ZIO_FLAG_SCAN_THREAD) {
4939 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
4940 dsl_scan_phys_t *scn_phys = &scn->scn_phys;
4941 uint64_t *processed = &scn_phys->scn_processed;
4943 if (vd->vdev_ops->vdev_op_leaf)
4944 atomic_add_64(processed, psize);
4945 vs->vs_scan_processed += psize;
4949 * Repair is the result of a rebuild issued by the
4950 * rebuild thread (vdev_rebuild_thread). To avoid
4951 * double counting repaired bytes the virtual dRAID
4952 * spare vdev is excluded from the processed bytes.
4954 if (zio->io_priority == ZIO_PRIORITY_REBUILD) {
4955 vdev_t *tvd = vd->vdev_top;
4956 vdev_rebuild_t *vr = &tvd->vdev_rebuild_config;
4957 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
4958 uint64_t *rebuilt = &vrp->vrp_bytes_rebuilt;
4960 if (vd->vdev_ops->vdev_op_leaf &&
4961 vd->vdev_ops != &vdev_draid_spare_ops) {
4962 atomic_add_64(rebuilt, psize);
4964 vs->vs_rebuild_processed += psize;
4967 if (flags & ZIO_FLAG_SELF_HEAL)
4968 vs->vs_self_healed += psize;
4972 * The bytes/ops/histograms are recorded at the leaf level and
4973 * aggregated into the higher level vdevs in vdev_get_stats().
4975 if (vd->vdev_ops->vdev_op_leaf &&
4976 (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) {
4977 zio_type_t vs_type = type;
4978 zio_priority_t priority = zio->io_priority;
4981 * TRIM ops and bytes are reported to user space as
4982 * ZIO_TYPE_FLUSH. This is done to preserve the
4983 * vdev_stat_t structure layout for user space.
4985 if (type == ZIO_TYPE_TRIM)
4986 vs_type = ZIO_TYPE_FLUSH;
4989 * Solely for the purposes of 'zpool iostat -lqrw'
4990 * reporting use the priority to categorize the IO.
4991 * Only the following are reported to user space:
4993 * ZIO_PRIORITY_SYNC_READ,
4994 * ZIO_PRIORITY_SYNC_WRITE,
4995 * ZIO_PRIORITY_ASYNC_READ,
4996 * ZIO_PRIORITY_ASYNC_WRITE,
4997 * ZIO_PRIORITY_SCRUB,
4998 * ZIO_PRIORITY_TRIM,
4999 * ZIO_PRIORITY_REBUILD.
5001 if (priority == ZIO_PRIORITY_INITIALIZING) {
5002 ASSERT3U(type, ==, ZIO_TYPE_WRITE);
5003 priority = ZIO_PRIORITY_ASYNC_WRITE;
5004 } else if (priority == ZIO_PRIORITY_REMOVAL) {
5005 priority = ((type == ZIO_TYPE_WRITE) ?
5006 ZIO_PRIORITY_ASYNC_WRITE :
5007 ZIO_PRIORITY_ASYNC_READ);
5010 vs->vs_ops[vs_type]++;
5011 vs->vs_bytes[vs_type] += psize;
5013 if (flags & ZIO_FLAG_DELEGATED) {
5014 vsx->vsx_agg_histo[priority]
5015 [RQ_HISTO(zio->io_size)]++;
5016 } else {
5017 vsx->vsx_ind_histo[priority]
5018 [RQ_HISTO(zio->io_size)]++;
5021 if (zio->io_delta && zio->io_delay) {
5022 vsx->vsx_queue_histo[priority]
5023 [L_HISTO(zio->io_delta - zio->io_delay)]++;
5024 vsx->vsx_disk_histo[type]
5025 [L_HISTO(zio->io_delay)]++;
5026 vsx->vsx_total_histo[type]
5027 [L_HISTO(zio->io_delta)]++;
5031 mutex_exit(&vd->vdev_stat_lock);
5032 return;
5035 if (flags & ZIO_FLAG_SPECULATIVE)
5036 return;
5039 * If this is an I/O error that is going to be retried, then ignore the
5040 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
5041 * hard errors, when in reality they can happen for any number of
5042 * innocuous reasons (bus resets, MPxIO link failure, etc).
5044 if (zio->io_error == EIO &&
5045 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
5046 return;
5049 * Intent logs writes won't propagate their error to the root
5050 * I/O so don't mark these types of failures as pool-level
5051 * errors.
5053 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
5054 return;
5056 if (type == ZIO_TYPE_WRITE && txg != 0 &&
5057 (!(flags & ZIO_FLAG_IO_REPAIR) ||
5058 (flags & ZIO_FLAG_SCAN_THREAD) ||
5059 spa->spa_claiming)) {
5061 * This is either a normal write (not a repair), or it's
5062 * a repair induced by the scrub thread, or it's a repair
5063 * made by zil_claim() during spa_load() in the first txg.
5064 * In the normal case, we commit the DTL change in the same
5065 * txg as the block was born. In the scrub-induced repair
5066 * case, we know that scrubs run in first-pass syncing context,
5067 * so we commit the DTL change in spa_syncing_txg(spa).
5068 * In the zil_claim() case, we commit in spa_first_txg(spa).
5070 * We currently do not make DTL entries for failed spontaneous
5071 * self-healing writes triggered by normal (non-scrubbing)
5072 * reads, because we have no transactional context in which to
5073 * do so -- and it's not clear that it'd be desirable anyway.
5075 if (vd->vdev_ops->vdev_op_leaf) {
5076 uint64_t commit_txg = txg;
5077 if (flags & ZIO_FLAG_SCAN_THREAD) {
5078 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
5079 ASSERT(spa_sync_pass(spa) == 1);
5080 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
5081 commit_txg = spa_syncing_txg(spa);
5082 } else if (spa->spa_claiming) {
5083 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
5084 commit_txg = spa_first_txg(spa);
5086 ASSERT(commit_txg >= spa_syncing_txg(spa));
5087 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
5088 return;
5089 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
5090 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
5091 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
5093 if (vd != rvd)
5094 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
5098 int64_t
5099 vdev_deflated_space(vdev_t *vd, int64_t space)
5101 ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0);
5102 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
5104 return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio);
5108 * Update the in-core space usage stats for this vdev, its metaslab class,
5109 * and the root vdev.
5111 void
5112 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
5113 int64_t space_delta)
5115 (void) defer_delta;
5116 int64_t dspace_delta;
5117 spa_t *spa = vd->vdev_spa;
5118 vdev_t *rvd = spa->spa_root_vdev;
5120 ASSERT(vd == vd->vdev_top);
5123 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
5124 * factor. We must calculate this here and not at the root vdev
5125 * because the root vdev's psize-to-asize is simply the max of its
5126 * children's, thus not accurate enough for us.
5128 dspace_delta = vdev_deflated_space(vd, space_delta);
5130 mutex_enter(&vd->vdev_stat_lock);
5131 /* ensure we won't underflow */
5132 if (alloc_delta < 0) {
5133 ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta);
5136 vd->vdev_stat.vs_alloc += alloc_delta;
5137 vd->vdev_stat.vs_space += space_delta;
5138 vd->vdev_stat.vs_dspace += dspace_delta;
5139 mutex_exit(&vd->vdev_stat_lock);
5141 /* every class but log contributes to root space stats */
5142 if (vd->vdev_mg != NULL && !vd->vdev_islog) {
5143 ASSERT(!vd->vdev_isl2cache);
5144 mutex_enter(&rvd->vdev_stat_lock);
5145 rvd->vdev_stat.vs_alloc += alloc_delta;
5146 rvd->vdev_stat.vs_space += space_delta;
5147 rvd->vdev_stat.vs_dspace += dspace_delta;
5148 mutex_exit(&rvd->vdev_stat_lock);
5150 /* Note: metaslab_class_space_update moved to metaslab_space_update */
5154 * Mark a top-level vdev's config as dirty, placing it on the dirty list
5155 * so that it will be written out next time the vdev configuration is synced.
5156 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
5158 void
5159 vdev_config_dirty(vdev_t *vd)
5161 spa_t *spa = vd->vdev_spa;
5162 vdev_t *rvd = spa->spa_root_vdev;
5163 int c;
5165 ASSERT(spa_writeable(spa));
5168 * If this is an aux vdev (as with l2cache and spare devices), then we
5169 * update the vdev config manually and set the sync flag.
5171 if (vd->vdev_aux != NULL) {
5172 spa_aux_vdev_t *sav = vd->vdev_aux;
5173 nvlist_t **aux;
5174 uint_t naux;
5176 for (c = 0; c < sav->sav_count; c++) {
5177 if (sav->sav_vdevs[c] == vd)
5178 break;
5181 if (c == sav->sav_count) {
5183 * We're being removed. There's nothing more to do.
5185 ASSERT(sav->sav_sync == B_TRUE);
5186 return;
5189 sav->sav_sync = B_TRUE;
5191 if (nvlist_lookup_nvlist_array(sav->sav_config,
5192 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
5193 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
5194 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
5197 ASSERT(c < naux);
5200 * Setting the nvlist in the middle if the array is a little
5201 * sketchy, but it will work.
5203 nvlist_free(aux[c]);
5204 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
5206 return;
5210 * The dirty list is protected by the SCL_CONFIG lock. The caller
5211 * must either hold SCL_CONFIG as writer, or must be the sync thread
5212 * (which holds SCL_CONFIG as reader). There's only one sync thread,
5213 * so this is sufficient to ensure mutual exclusion.
5215 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
5216 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5217 spa_config_held(spa, SCL_CONFIG, RW_READER)));
5219 if (vd == rvd) {
5220 for (c = 0; c < rvd->vdev_children; c++)
5221 vdev_config_dirty(rvd->vdev_child[c]);
5222 } else {
5223 ASSERT(vd == vd->vdev_top);
5225 if (!list_link_active(&vd->vdev_config_dirty_node) &&
5226 vdev_is_concrete(vd)) {
5227 list_insert_head(&spa->spa_config_dirty_list, vd);
5232 void
5233 vdev_config_clean(vdev_t *vd)
5235 spa_t *spa = vd->vdev_spa;
5237 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
5238 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5239 spa_config_held(spa, SCL_CONFIG, RW_READER)));
5241 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
5242 list_remove(&spa->spa_config_dirty_list, vd);
5246 * Mark a top-level vdev's state as dirty, so that the next pass of
5247 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
5248 * the state changes from larger config changes because they require
5249 * much less locking, and are often needed for administrative actions.
5251 void
5252 vdev_state_dirty(vdev_t *vd)
5254 spa_t *spa = vd->vdev_spa;
5256 ASSERT(spa_writeable(spa));
5257 ASSERT(vd == vd->vdev_top);
5260 * The state list is protected by the SCL_STATE lock. The caller
5261 * must either hold SCL_STATE as writer, or must be the sync thread
5262 * (which holds SCL_STATE as reader). There's only one sync thread,
5263 * so this is sufficient to ensure mutual exclusion.
5265 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
5266 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5267 spa_config_held(spa, SCL_STATE, RW_READER)));
5269 if (!list_link_active(&vd->vdev_state_dirty_node) &&
5270 vdev_is_concrete(vd))
5271 list_insert_head(&spa->spa_state_dirty_list, vd);
5274 void
5275 vdev_state_clean(vdev_t *vd)
5277 spa_t *spa = vd->vdev_spa;
5279 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
5280 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5281 spa_config_held(spa, SCL_STATE, RW_READER)));
5283 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
5284 list_remove(&spa->spa_state_dirty_list, vd);
5288 * Propagate vdev state up from children to parent.
5290 void
5291 vdev_propagate_state(vdev_t *vd)
5293 spa_t *spa = vd->vdev_spa;
5294 vdev_t *rvd = spa->spa_root_vdev;
5295 int degraded = 0, faulted = 0;
5296 int corrupted = 0;
5297 vdev_t *child;
5299 if (vd->vdev_children > 0) {
5300 for (int c = 0; c < vd->vdev_children; c++) {
5301 child = vd->vdev_child[c];
5304 * Don't factor holes or indirect vdevs into the
5305 * decision.
5307 if (!vdev_is_concrete(child))
5308 continue;
5310 if (!vdev_readable(child) ||
5311 (!vdev_writeable(child) && spa_writeable(spa))) {
5313 * Root special: if there is a top-level log
5314 * device, treat the root vdev as if it were
5315 * degraded.
5317 if (child->vdev_islog && vd == rvd)
5318 degraded++;
5319 else
5320 faulted++;
5321 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
5322 degraded++;
5325 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
5326 corrupted++;
5329 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
5332 * Root special: if there is a top-level vdev that cannot be
5333 * opened due to corrupted metadata, then propagate the root
5334 * vdev's aux state as 'corrupt' rather than 'insufficient
5335 * replicas'.
5337 if (corrupted && vd == rvd &&
5338 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
5339 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
5340 VDEV_AUX_CORRUPT_DATA);
5343 if (vd->vdev_parent)
5344 vdev_propagate_state(vd->vdev_parent);
5348 * Set a vdev's state. If this is during an open, we don't update the parent
5349 * state, because we're in the process of opening children depth-first.
5350 * Otherwise, we propagate the change to the parent.
5352 * If this routine places a device in a faulted state, an appropriate ereport is
5353 * generated.
5355 void
5356 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
5358 uint64_t save_state;
5359 spa_t *spa = vd->vdev_spa;
5361 if (state == vd->vdev_state) {
5363 * Since vdev_offline() code path is already in an offline
5364 * state we can miss a statechange event to OFFLINE. Check
5365 * the previous state to catch this condition.
5367 if (vd->vdev_ops->vdev_op_leaf &&
5368 (state == VDEV_STATE_OFFLINE) &&
5369 (vd->vdev_prevstate >= VDEV_STATE_FAULTED)) {
5370 /* post an offline state change */
5371 zfs_post_state_change(spa, vd, vd->vdev_prevstate);
5373 vd->vdev_stat.vs_aux = aux;
5374 return;
5377 save_state = vd->vdev_state;
5379 vd->vdev_state = state;
5380 vd->vdev_stat.vs_aux = aux;
5383 * If we are setting the vdev state to anything but an open state, then
5384 * always close the underlying device unless the device has requested
5385 * a delayed close (i.e. we're about to remove or fault the device).
5386 * Otherwise, we keep accessible but invalid devices open forever.
5387 * We don't call vdev_close() itself, because that implies some extra
5388 * checks (offline, etc) that we don't want here. This is limited to
5389 * leaf devices, because otherwise closing the device will affect other
5390 * children.
5392 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
5393 vd->vdev_ops->vdev_op_leaf)
5394 vd->vdev_ops->vdev_op_close(vd);
5396 if (vd->vdev_removed &&
5397 state == VDEV_STATE_CANT_OPEN &&
5398 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
5400 * If the previous state is set to VDEV_STATE_REMOVED, then this
5401 * device was previously marked removed and someone attempted to
5402 * reopen it. If this failed due to a nonexistent device, then
5403 * keep the device in the REMOVED state. We also let this be if
5404 * it is one of our special test online cases, which is only
5405 * attempting to online the device and shouldn't generate an FMA
5406 * fault.
5408 vd->vdev_state = VDEV_STATE_REMOVED;
5409 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
5410 } else if (state == VDEV_STATE_REMOVED) {
5411 vd->vdev_removed = B_TRUE;
5412 } else if (state == VDEV_STATE_CANT_OPEN) {
5414 * If we fail to open a vdev during an import or recovery, we
5415 * mark it as "not available", which signifies that it was
5416 * never there to begin with. Failure to open such a device
5417 * is not considered an error.
5419 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
5420 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
5421 vd->vdev_ops->vdev_op_leaf)
5422 vd->vdev_not_present = 1;
5425 * Post the appropriate ereport. If the 'prevstate' field is
5426 * set to something other than VDEV_STATE_UNKNOWN, it indicates
5427 * that this is part of a vdev_reopen(). In this case, we don't
5428 * want to post the ereport if the device was already in the
5429 * CANT_OPEN state beforehand.
5431 * If the 'checkremove' flag is set, then this is an attempt to
5432 * online the device in response to an insertion event. If we
5433 * hit this case, then we have detected an insertion event for a
5434 * faulted or offline device that wasn't in the removed state.
5435 * In this scenario, we don't post an ereport because we are
5436 * about to replace the device, or attempt an online with
5437 * vdev_forcefault, which will generate the fault for us.
5439 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
5440 !vd->vdev_not_present && !vd->vdev_checkremove &&
5441 vd != spa->spa_root_vdev) {
5442 const char *class;
5444 switch (aux) {
5445 case VDEV_AUX_OPEN_FAILED:
5446 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
5447 break;
5448 case VDEV_AUX_CORRUPT_DATA:
5449 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
5450 break;
5451 case VDEV_AUX_NO_REPLICAS:
5452 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
5453 break;
5454 case VDEV_AUX_BAD_GUID_SUM:
5455 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
5456 break;
5457 case VDEV_AUX_TOO_SMALL:
5458 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
5459 break;
5460 case VDEV_AUX_BAD_LABEL:
5461 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
5462 break;
5463 case VDEV_AUX_BAD_ASHIFT:
5464 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT;
5465 break;
5466 default:
5467 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
5470 (void) zfs_ereport_post(class, spa, vd, NULL, NULL,
5471 save_state);
5474 /* Erase any notion of persistent removed state */
5475 vd->vdev_removed = B_FALSE;
5476 } else {
5477 vd->vdev_removed = B_FALSE;
5481 * Notify ZED of any significant state-change on a leaf vdev.
5484 if (vd->vdev_ops->vdev_op_leaf) {
5485 /* preserve original state from a vdev_reopen() */
5486 if ((vd->vdev_prevstate != VDEV_STATE_UNKNOWN) &&
5487 (vd->vdev_prevstate != vd->vdev_state) &&
5488 (save_state <= VDEV_STATE_CLOSED))
5489 save_state = vd->vdev_prevstate;
5491 /* filter out state change due to initial vdev_open */
5492 if (save_state > VDEV_STATE_CLOSED)
5493 zfs_post_state_change(spa, vd, save_state);
5496 if (!isopen && vd->vdev_parent)
5497 vdev_propagate_state(vd->vdev_parent);
5500 boolean_t
5501 vdev_children_are_offline(vdev_t *vd)
5503 ASSERT(!vd->vdev_ops->vdev_op_leaf);
5505 for (uint64_t i = 0; i < vd->vdev_children; i++) {
5506 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
5507 return (B_FALSE);
5510 return (B_TRUE);
5514 * Check the vdev configuration to ensure that it's capable of supporting
5515 * a root pool. We do not support partial configuration.
5517 boolean_t
5518 vdev_is_bootable(vdev_t *vd)
5520 if (!vd->vdev_ops->vdev_op_leaf) {
5521 const char *vdev_type = vd->vdev_ops->vdev_op_type;
5523 if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0)
5524 return (B_FALSE);
5527 for (int c = 0; c < vd->vdev_children; c++) {
5528 if (!vdev_is_bootable(vd->vdev_child[c]))
5529 return (B_FALSE);
5531 return (B_TRUE);
5534 boolean_t
5535 vdev_is_concrete(vdev_t *vd)
5537 vdev_ops_t *ops = vd->vdev_ops;
5538 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
5539 ops == &vdev_missing_ops || ops == &vdev_root_ops) {
5540 return (B_FALSE);
5541 } else {
5542 return (B_TRUE);
5547 * Determine if a log device has valid content. If the vdev was
5548 * removed or faulted in the MOS config then we know that
5549 * the content on the log device has already been written to the pool.
5551 boolean_t
5552 vdev_log_state_valid(vdev_t *vd)
5554 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
5555 !vd->vdev_removed)
5556 return (B_TRUE);
5558 for (int c = 0; c < vd->vdev_children; c++)
5559 if (vdev_log_state_valid(vd->vdev_child[c]))
5560 return (B_TRUE);
5562 return (B_FALSE);
5566 * Expand a vdev if possible.
5568 void
5569 vdev_expand(vdev_t *vd, uint64_t txg)
5571 ASSERT(vd->vdev_top == vd);
5572 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5573 ASSERT(vdev_is_concrete(vd));
5575 vdev_set_deflate_ratio(vd);
5577 if ((vd->vdev_spa->spa_raidz_expand == NULL ||
5578 vd->vdev_spa->spa_raidz_expand->vre_vdev_id != vd->vdev_id) &&
5579 (vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
5580 vdev_is_concrete(vd)) {
5581 vdev_metaslab_group_create(vd);
5582 VERIFY(vdev_metaslab_init(vd, txg) == 0);
5583 vdev_config_dirty(vd);
5588 * Split a vdev.
5590 void
5591 vdev_split(vdev_t *vd)
5593 vdev_t *cvd, *pvd = vd->vdev_parent;
5595 VERIFY3U(pvd->vdev_children, >, 1);
5597 vdev_remove_child(pvd, vd);
5598 vdev_compact_children(pvd);
5600 ASSERT3P(pvd->vdev_child, !=, NULL);
5602 cvd = pvd->vdev_child[0];
5603 if (pvd->vdev_children == 1) {
5604 vdev_remove_parent(cvd);
5605 cvd->vdev_splitting = B_TRUE;
5607 vdev_propagate_state(cvd);
5610 void
5611 vdev_deadman(vdev_t *vd, const char *tag)
5613 for (int c = 0; c < vd->vdev_children; c++) {
5614 vdev_t *cvd = vd->vdev_child[c];
5616 vdev_deadman(cvd, tag);
5619 if (vd->vdev_ops->vdev_op_leaf) {
5620 vdev_queue_t *vq = &vd->vdev_queue;
5622 mutex_enter(&vq->vq_lock);
5623 if (vq->vq_active > 0) {
5624 spa_t *spa = vd->vdev_spa;
5625 zio_t *fio;
5626 uint64_t delta;
5628 zfs_dbgmsg("slow vdev: %s has %u active IOs",
5629 vd->vdev_path, vq->vq_active);
5632 * Look at the head of all the pending queues,
5633 * if any I/O has been outstanding for longer than
5634 * the spa_deadman_synctime invoke the deadman logic.
5636 fio = list_head(&vq->vq_active_list);
5637 delta = gethrtime() - fio->io_timestamp;
5638 if (delta > spa_deadman_synctime(spa))
5639 zio_deadman(fio, tag);
5641 mutex_exit(&vq->vq_lock);
5645 void
5646 vdev_defer_resilver(vdev_t *vd)
5648 ASSERT(vd->vdev_ops->vdev_op_leaf);
5650 vd->vdev_resilver_deferred = B_TRUE;
5651 vd->vdev_spa->spa_resilver_deferred = B_TRUE;
5655 * Clears the resilver deferred flag on all leaf devs under vd. Returns
5656 * B_TRUE if we have devices that need to be resilvered and are available to
5657 * accept resilver I/Os.
5659 boolean_t
5660 vdev_clear_resilver_deferred(vdev_t *vd, dmu_tx_t *tx)
5662 boolean_t resilver_needed = B_FALSE;
5663 spa_t *spa = vd->vdev_spa;
5665 for (int c = 0; c < vd->vdev_children; c++) {
5666 vdev_t *cvd = vd->vdev_child[c];
5667 resilver_needed |= vdev_clear_resilver_deferred(cvd, tx);
5670 if (vd == spa->spa_root_vdev &&
5671 spa_feature_is_active(spa, SPA_FEATURE_RESILVER_DEFER)) {
5672 spa_feature_decr(spa, SPA_FEATURE_RESILVER_DEFER, tx);
5673 vdev_config_dirty(vd);
5674 spa->spa_resilver_deferred = B_FALSE;
5675 return (resilver_needed);
5678 if (!vdev_is_concrete(vd) || vd->vdev_aux ||
5679 !vd->vdev_ops->vdev_op_leaf)
5680 return (resilver_needed);
5682 vd->vdev_resilver_deferred = B_FALSE;
5684 return (!vdev_is_dead(vd) && !vd->vdev_offline &&
5685 vdev_resilver_needed(vd, NULL, NULL));
5688 boolean_t
5689 vdev_xlate_is_empty(range_seg64_t *rs)
5691 return (rs->rs_start == rs->rs_end);
5695 * Translate a logical range to the first contiguous physical range for the
5696 * specified vdev_t. This function is initially called with a leaf vdev and
5697 * will walk each parent vdev until it reaches a top-level vdev. Once the
5698 * top-level is reached the physical range is initialized and the recursive
5699 * function begins to unwind. As it unwinds it calls the parent's vdev
5700 * specific translation function to do the real conversion.
5702 void
5703 vdev_xlate(vdev_t *vd, const range_seg64_t *logical_rs,
5704 range_seg64_t *physical_rs, range_seg64_t *remain_rs)
5707 * Walk up the vdev tree
5709 if (vd != vd->vdev_top) {
5710 vdev_xlate(vd->vdev_parent, logical_rs, physical_rs,
5711 remain_rs);
5712 } else {
5714 * We've reached the top-level vdev, initialize the physical
5715 * range to the logical range and set an empty remaining
5716 * range then start to unwind.
5718 physical_rs->rs_start = logical_rs->rs_start;
5719 physical_rs->rs_end = logical_rs->rs_end;
5721 remain_rs->rs_start = logical_rs->rs_start;
5722 remain_rs->rs_end = logical_rs->rs_start;
5724 return;
5727 vdev_t *pvd = vd->vdev_parent;
5728 ASSERT3P(pvd, !=, NULL);
5729 ASSERT3P(pvd->vdev_ops->vdev_op_xlate, !=, NULL);
5732 * As this recursive function unwinds, translate the logical
5733 * range into its physical and any remaining components by calling
5734 * the vdev specific translate function.
5736 range_seg64_t intermediate = { 0 };
5737 pvd->vdev_ops->vdev_op_xlate(vd, physical_rs, &intermediate, remain_rs);
5739 physical_rs->rs_start = intermediate.rs_start;
5740 physical_rs->rs_end = intermediate.rs_end;
5743 void
5744 vdev_xlate_walk(vdev_t *vd, const range_seg64_t *logical_rs,
5745 vdev_xlate_func_t *func, void *arg)
5747 range_seg64_t iter_rs = *logical_rs;
5748 range_seg64_t physical_rs;
5749 range_seg64_t remain_rs;
5751 while (!vdev_xlate_is_empty(&iter_rs)) {
5753 vdev_xlate(vd, &iter_rs, &physical_rs, &remain_rs);
5756 * With raidz and dRAID, it's possible that the logical range
5757 * does not live on this leaf vdev. Only when there is a non-
5758 * zero physical size call the provided function.
5760 if (!vdev_xlate_is_empty(&physical_rs))
5761 func(arg, &physical_rs);
5763 iter_rs = remain_rs;
5767 static char *
5768 vdev_name(vdev_t *vd, char *buf, int buflen)
5770 if (vd->vdev_path == NULL) {
5771 if (strcmp(vd->vdev_ops->vdev_op_type, "root") == 0) {
5772 strlcpy(buf, vd->vdev_spa->spa_name, buflen);
5773 } else if (!vd->vdev_ops->vdev_op_leaf) {
5774 snprintf(buf, buflen, "%s-%llu",
5775 vd->vdev_ops->vdev_op_type,
5776 (u_longlong_t)vd->vdev_id);
5778 } else {
5779 strlcpy(buf, vd->vdev_path, buflen);
5781 return (buf);
5785 * Look at the vdev tree and determine whether any devices are currently being
5786 * replaced.
5788 boolean_t
5789 vdev_replace_in_progress(vdev_t *vdev)
5791 ASSERT(spa_config_held(vdev->vdev_spa, SCL_ALL, RW_READER) != 0);
5793 if (vdev->vdev_ops == &vdev_replacing_ops)
5794 return (B_TRUE);
5797 * A 'spare' vdev indicates that we have a replace in progress, unless
5798 * it has exactly two children, and the second, the hot spare, has
5799 * finished being resilvered.
5801 if (vdev->vdev_ops == &vdev_spare_ops && (vdev->vdev_children > 2 ||
5802 !vdev_dtl_empty(vdev->vdev_child[1], DTL_MISSING)))
5803 return (B_TRUE);
5805 for (int i = 0; i < vdev->vdev_children; i++) {
5806 if (vdev_replace_in_progress(vdev->vdev_child[i]))
5807 return (B_TRUE);
5810 return (B_FALSE);
5814 * Add a (source=src, propname=propval) list to an nvlist.
5816 static void
5817 vdev_prop_add_list(nvlist_t *nvl, const char *propname, const char *strval,
5818 uint64_t intval, zprop_source_t src)
5820 nvlist_t *propval;
5822 propval = fnvlist_alloc();
5823 fnvlist_add_uint64(propval, ZPROP_SOURCE, src);
5825 if (strval != NULL)
5826 fnvlist_add_string(propval, ZPROP_VALUE, strval);
5827 else
5828 fnvlist_add_uint64(propval, ZPROP_VALUE, intval);
5830 fnvlist_add_nvlist(nvl, propname, propval);
5831 nvlist_free(propval);
5834 static void
5835 vdev_props_set_sync(void *arg, dmu_tx_t *tx)
5837 vdev_t *vd;
5838 nvlist_t *nvp = arg;
5839 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
5840 objset_t *mos = spa->spa_meta_objset;
5841 nvpair_t *elem = NULL;
5842 uint64_t vdev_guid;
5843 uint64_t objid;
5844 nvlist_t *nvprops;
5846 vdev_guid = fnvlist_lookup_uint64(nvp, ZPOOL_VDEV_PROPS_SET_VDEV);
5847 nvprops = fnvlist_lookup_nvlist(nvp, ZPOOL_VDEV_PROPS_SET_PROPS);
5848 vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE);
5850 /* this vdev could get removed while waiting for this sync task */
5851 if (vd == NULL)
5852 return;
5855 * Set vdev property values in the vdev props mos object.
5857 if (vd->vdev_root_zap != 0) {
5858 objid = vd->vdev_root_zap;
5859 } else if (vd->vdev_top_zap != 0) {
5860 objid = vd->vdev_top_zap;
5861 } else if (vd->vdev_leaf_zap != 0) {
5862 objid = vd->vdev_leaf_zap;
5863 } else {
5864 panic("unexpected vdev type");
5867 mutex_enter(&spa->spa_props_lock);
5869 while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
5870 uint64_t intval;
5871 const char *strval;
5872 vdev_prop_t prop;
5873 const char *propname = nvpair_name(elem);
5874 zprop_type_t proptype;
5876 switch (prop = vdev_name_to_prop(propname)) {
5877 case VDEV_PROP_USERPROP:
5878 if (vdev_prop_user(propname)) {
5879 strval = fnvpair_value_string(elem);
5880 if (strlen(strval) == 0) {
5881 /* remove the property if value == "" */
5882 (void) zap_remove(mos, objid, propname,
5883 tx);
5884 } else {
5885 VERIFY0(zap_update(mos, objid, propname,
5886 1, strlen(strval) + 1, strval, tx));
5888 spa_history_log_internal(spa, "vdev set", tx,
5889 "vdev_guid=%llu: %s=%s",
5890 (u_longlong_t)vdev_guid, nvpair_name(elem),
5891 strval);
5893 break;
5894 default:
5895 /* normalize the property name */
5896 propname = vdev_prop_to_name(prop);
5897 proptype = vdev_prop_get_type(prop);
5899 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5900 ASSERT(proptype == PROP_TYPE_STRING);
5901 strval = fnvpair_value_string(elem);
5902 VERIFY0(zap_update(mos, objid, propname,
5903 1, strlen(strval) + 1, strval, tx));
5904 spa_history_log_internal(spa, "vdev set", tx,
5905 "vdev_guid=%llu: %s=%s",
5906 (u_longlong_t)vdev_guid, nvpair_name(elem),
5907 strval);
5908 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5909 intval = fnvpair_value_uint64(elem);
5911 if (proptype == PROP_TYPE_INDEX) {
5912 const char *unused;
5913 VERIFY0(vdev_prop_index_to_string(
5914 prop, intval, &unused));
5916 VERIFY0(zap_update(mos, objid, propname,
5917 sizeof (uint64_t), 1, &intval, tx));
5918 spa_history_log_internal(spa, "vdev set", tx,
5919 "vdev_guid=%llu: %s=%lld",
5920 (u_longlong_t)vdev_guid,
5921 nvpair_name(elem), (longlong_t)intval);
5922 } else {
5923 panic("invalid vdev property type %u",
5924 nvpair_type(elem));
5930 mutex_exit(&spa->spa_props_lock);
5934 vdev_prop_set(vdev_t *vd, nvlist_t *innvl, nvlist_t *outnvl)
5936 spa_t *spa = vd->vdev_spa;
5937 nvpair_t *elem = NULL;
5938 uint64_t vdev_guid;
5939 nvlist_t *nvprops;
5940 int error = 0;
5942 ASSERT(vd != NULL);
5944 /* Check that vdev has a zap we can use */
5945 if (vd->vdev_root_zap == 0 &&
5946 vd->vdev_top_zap == 0 &&
5947 vd->vdev_leaf_zap == 0)
5948 return (SET_ERROR(EINVAL));
5950 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_SET_VDEV,
5951 &vdev_guid) != 0)
5952 return (SET_ERROR(EINVAL));
5954 if (nvlist_lookup_nvlist(innvl, ZPOOL_VDEV_PROPS_SET_PROPS,
5955 &nvprops) != 0)
5956 return (SET_ERROR(EINVAL));
5958 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL)
5959 return (SET_ERROR(EINVAL));
5961 while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
5962 const char *propname = nvpair_name(elem);
5963 vdev_prop_t prop = vdev_name_to_prop(propname);
5964 uint64_t intval = 0;
5965 const char *strval = NULL;
5967 if (prop == VDEV_PROP_USERPROP && !vdev_prop_user(propname)) {
5968 error = EINVAL;
5969 goto end;
5972 if (vdev_prop_readonly(prop)) {
5973 error = EROFS;
5974 goto end;
5977 /* Special Processing */
5978 switch (prop) {
5979 case VDEV_PROP_PATH:
5980 if (vd->vdev_path == NULL) {
5981 error = EROFS;
5982 break;
5984 if (nvpair_value_string(elem, &strval) != 0) {
5985 error = EINVAL;
5986 break;
5988 /* New path must start with /dev/ */
5989 if (strncmp(strval, "/dev/", 5)) {
5990 error = EINVAL;
5991 break;
5993 error = spa_vdev_setpath(spa, vdev_guid, strval);
5994 break;
5995 case VDEV_PROP_ALLOCATING:
5996 if (nvpair_value_uint64(elem, &intval) != 0) {
5997 error = EINVAL;
5998 break;
6000 if (intval != vd->vdev_noalloc)
6001 break;
6002 if (intval == 0)
6003 error = spa_vdev_noalloc(spa, vdev_guid);
6004 else
6005 error = spa_vdev_alloc(spa, vdev_guid);
6006 break;
6007 case VDEV_PROP_FAILFAST:
6008 if (nvpair_value_uint64(elem, &intval) != 0) {
6009 error = EINVAL;
6010 break;
6012 vd->vdev_failfast = intval & 1;
6013 break;
6014 case VDEV_PROP_CHECKSUM_N:
6015 if (nvpair_value_uint64(elem, &intval) != 0) {
6016 error = EINVAL;
6017 break;
6019 vd->vdev_checksum_n = intval;
6020 break;
6021 case VDEV_PROP_CHECKSUM_T:
6022 if (nvpair_value_uint64(elem, &intval) != 0) {
6023 error = EINVAL;
6024 break;
6026 vd->vdev_checksum_t = intval;
6027 break;
6028 case VDEV_PROP_IO_N:
6029 if (nvpair_value_uint64(elem, &intval) != 0) {
6030 error = EINVAL;
6031 break;
6033 vd->vdev_io_n = intval;
6034 break;
6035 case VDEV_PROP_IO_T:
6036 if (nvpair_value_uint64(elem, &intval) != 0) {
6037 error = EINVAL;
6038 break;
6040 vd->vdev_io_t = intval;
6041 break;
6042 case VDEV_PROP_SLOW_IO_N:
6043 if (nvpair_value_uint64(elem, &intval) != 0) {
6044 error = EINVAL;
6045 break;
6047 vd->vdev_slow_io_n = intval;
6048 break;
6049 case VDEV_PROP_SLOW_IO_T:
6050 if (nvpair_value_uint64(elem, &intval) != 0) {
6051 error = EINVAL;
6052 break;
6054 vd->vdev_slow_io_t = intval;
6055 break;
6056 default:
6057 /* Most processing is done in vdev_props_set_sync */
6058 break;
6060 end:
6061 if (error != 0) {
6062 intval = error;
6063 vdev_prop_add_list(outnvl, propname, strval, intval, 0);
6064 return (error);
6068 return (dsl_sync_task(spa->spa_name, NULL, vdev_props_set_sync,
6069 innvl, 6, ZFS_SPACE_CHECK_EXTRA_RESERVED));
6073 vdev_prop_get(vdev_t *vd, nvlist_t *innvl, nvlist_t *outnvl)
6075 spa_t *spa = vd->vdev_spa;
6076 objset_t *mos = spa->spa_meta_objset;
6077 int err = 0;
6078 uint64_t objid;
6079 uint64_t vdev_guid;
6080 nvpair_t *elem = NULL;
6081 nvlist_t *nvprops = NULL;
6082 uint64_t intval = 0;
6083 char *strval = NULL;
6084 const char *propname = NULL;
6085 vdev_prop_t prop;
6087 ASSERT(vd != NULL);
6088 ASSERT(mos != NULL);
6090 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_GET_VDEV,
6091 &vdev_guid) != 0)
6092 return (SET_ERROR(EINVAL));
6094 nvlist_lookup_nvlist(innvl, ZPOOL_VDEV_PROPS_GET_PROPS, &nvprops);
6096 if (vd->vdev_root_zap != 0) {
6097 objid = vd->vdev_root_zap;
6098 } else if (vd->vdev_top_zap != 0) {
6099 objid = vd->vdev_top_zap;
6100 } else if (vd->vdev_leaf_zap != 0) {
6101 objid = vd->vdev_leaf_zap;
6102 } else {
6103 return (SET_ERROR(EINVAL));
6105 ASSERT(objid != 0);
6107 mutex_enter(&spa->spa_props_lock);
6109 if (nvprops != NULL) {
6110 char namebuf[64] = { 0 };
6112 while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
6113 intval = 0;
6114 strval = NULL;
6115 propname = nvpair_name(elem);
6116 prop = vdev_name_to_prop(propname);
6117 zprop_source_t src = ZPROP_SRC_DEFAULT;
6118 uint64_t integer_size, num_integers;
6120 switch (prop) {
6121 /* Special Read-only Properties */
6122 case VDEV_PROP_NAME:
6123 strval = vdev_name(vd, namebuf,
6124 sizeof (namebuf));
6125 if (strval == NULL)
6126 continue;
6127 vdev_prop_add_list(outnvl, propname, strval, 0,
6128 ZPROP_SRC_NONE);
6129 continue;
6130 case VDEV_PROP_CAPACITY:
6131 /* percent used */
6132 intval = (vd->vdev_stat.vs_dspace == 0) ? 0 :
6133 (vd->vdev_stat.vs_alloc * 100 /
6134 vd->vdev_stat.vs_dspace);
6135 vdev_prop_add_list(outnvl, propname, NULL,
6136 intval, ZPROP_SRC_NONE);
6137 continue;
6138 case VDEV_PROP_STATE:
6139 vdev_prop_add_list(outnvl, propname, NULL,
6140 vd->vdev_state, ZPROP_SRC_NONE);
6141 continue;
6142 case VDEV_PROP_GUID:
6143 vdev_prop_add_list(outnvl, propname, NULL,
6144 vd->vdev_guid, ZPROP_SRC_NONE);
6145 continue;
6146 case VDEV_PROP_ASIZE:
6147 vdev_prop_add_list(outnvl, propname, NULL,
6148 vd->vdev_asize, ZPROP_SRC_NONE);
6149 continue;
6150 case VDEV_PROP_PSIZE:
6151 vdev_prop_add_list(outnvl, propname, NULL,
6152 vd->vdev_psize, ZPROP_SRC_NONE);
6153 continue;
6154 case VDEV_PROP_ASHIFT:
6155 vdev_prop_add_list(outnvl, propname, NULL,
6156 vd->vdev_ashift, ZPROP_SRC_NONE);
6157 continue;
6158 case VDEV_PROP_SIZE:
6159 vdev_prop_add_list(outnvl, propname, NULL,
6160 vd->vdev_stat.vs_dspace, ZPROP_SRC_NONE);
6161 continue;
6162 case VDEV_PROP_FREE:
6163 vdev_prop_add_list(outnvl, propname, NULL,
6164 vd->vdev_stat.vs_dspace -
6165 vd->vdev_stat.vs_alloc, ZPROP_SRC_NONE);
6166 continue;
6167 case VDEV_PROP_ALLOCATED:
6168 vdev_prop_add_list(outnvl, propname, NULL,
6169 vd->vdev_stat.vs_alloc, ZPROP_SRC_NONE);
6170 continue;
6171 case VDEV_PROP_EXPANDSZ:
6172 vdev_prop_add_list(outnvl, propname, NULL,
6173 vd->vdev_stat.vs_esize, ZPROP_SRC_NONE);
6174 continue;
6175 case VDEV_PROP_FRAGMENTATION:
6176 vdev_prop_add_list(outnvl, propname, NULL,
6177 vd->vdev_stat.vs_fragmentation,
6178 ZPROP_SRC_NONE);
6179 continue;
6180 case VDEV_PROP_PARITY:
6181 vdev_prop_add_list(outnvl, propname, NULL,
6182 vdev_get_nparity(vd), ZPROP_SRC_NONE);
6183 continue;
6184 case VDEV_PROP_PATH:
6185 if (vd->vdev_path == NULL)
6186 continue;
6187 vdev_prop_add_list(outnvl, propname,
6188 vd->vdev_path, 0, ZPROP_SRC_NONE);
6189 continue;
6190 case VDEV_PROP_DEVID:
6191 if (vd->vdev_devid == NULL)
6192 continue;
6193 vdev_prop_add_list(outnvl, propname,
6194 vd->vdev_devid, 0, ZPROP_SRC_NONE);
6195 continue;
6196 case VDEV_PROP_PHYS_PATH:
6197 if (vd->vdev_physpath == NULL)
6198 continue;
6199 vdev_prop_add_list(outnvl, propname,
6200 vd->vdev_physpath, 0, ZPROP_SRC_NONE);
6201 continue;
6202 case VDEV_PROP_ENC_PATH:
6203 if (vd->vdev_enc_sysfs_path == NULL)
6204 continue;
6205 vdev_prop_add_list(outnvl, propname,
6206 vd->vdev_enc_sysfs_path, 0, ZPROP_SRC_NONE);
6207 continue;
6208 case VDEV_PROP_FRU:
6209 if (vd->vdev_fru == NULL)
6210 continue;
6211 vdev_prop_add_list(outnvl, propname,
6212 vd->vdev_fru, 0, ZPROP_SRC_NONE);
6213 continue;
6214 case VDEV_PROP_PARENT:
6215 if (vd->vdev_parent != NULL) {
6216 strval = vdev_name(vd->vdev_parent,
6217 namebuf, sizeof (namebuf));
6218 vdev_prop_add_list(outnvl, propname,
6219 strval, 0, ZPROP_SRC_NONE);
6221 continue;
6222 case VDEV_PROP_CHILDREN:
6223 if (vd->vdev_children > 0)
6224 strval = kmem_zalloc(ZAP_MAXVALUELEN,
6225 KM_SLEEP);
6226 for (uint64_t i = 0; i < vd->vdev_children;
6227 i++) {
6228 const char *vname;
6230 vname = vdev_name(vd->vdev_child[i],
6231 namebuf, sizeof (namebuf));
6232 if (vname == NULL)
6233 vname = "(unknown)";
6234 if (strlen(strval) > 0)
6235 strlcat(strval, ",",
6236 ZAP_MAXVALUELEN);
6237 strlcat(strval, vname, ZAP_MAXVALUELEN);
6239 if (strval != NULL) {
6240 vdev_prop_add_list(outnvl, propname,
6241 strval, 0, ZPROP_SRC_NONE);
6242 kmem_free(strval, ZAP_MAXVALUELEN);
6244 continue;
6245 case VDEV_PROP_NUMCHILDREN:
6246 vdev_prop_add_list(outnvl, propname, NULL,
6247 vd->vdev_children, ZPROP_SRC_NONE);
6248 continue;
6249 case VDEV_PROP_READ_ERRORS:
6250 vdev_prop_add_list(outnvl, propname, NULL,
6251 vd->vdev_stat.vs_read_errors,
6252 ZPROP_SRC_NONE);
6253 continue;
6254 case VDEV_PROP_WRITE_ERRORS:
6255 vdev_prop_add_list(outnvl, propname, NULL,
6256 vd->vdev_stat.vs_write_errors,
6257 ZPROP_SRC_NONE);
6258 continue;
6259 case VDEV_PROP_CHECKSUM_ERRORS:
6260 vdev_prop_add_list(outnvl, propname, NULL,
6261 vd->vdev_stat.vs_checksum_errors,
6262 ZPROP_SRC_NONE);
6263 continue;
6264 case VDEV_PROP_INITIALIZE_ERRORS:
6265 vdev_prop_add_list(outnvl, propname, NULL,
6266 vd->vdev_stat.vs_initialize_errors,
6267 ZPROP_SRC_NONE);
6268 continue;
6269 case VDEV_PROP_TRIM_ERRORS:
6270 vdev_prop_add_list(outnvl, propname, NULL,
6271 vd->vdev_stat.vs_trim_errors,
6272 ZPROP_SRC_NONE);
6273 continue;
6274 case VDEV_PROP_SLOW_IOS:
6275 vdev_prop_add_list(outnvl, propname, NULL,
6276 vd->vdev_stat.vs_slow_ios,
6277 ZPROP_SRC_NONE);
6278 continue;
6279 case VDEV_PROP_OPS_NULL:
6280 vdev_prop_add_list(outnvl, propname, NULL,
6281 vd->vdev_stat.vs_ops[ZIO_TYPE_NULL],
6282 ZPROP_SRC_NONE);
6283 continue;
6284 case VDEV_PROP_OPS_READ:
6285 vdev_prop_add_list(outnvl, propname, NULL,
6286 vd->vdev_stat.vs_ops[ZIO_TYPE_READ],
6287 ZPROP_SRC_NONE);
6288 continue;
6289 case VDEV_PROP_OPS_WRITE:
6290 vdev_prop_add_list(outnvl, propname, NULL,
6291 vd->vdev_stat.vs_ops[ZIO_TYPE_WRITE],
6292 ZPROP_SRC_NONE);
6293 continue;
6294 case VDEV_PROP_OPS_FREE:
6295 vdev_prop_add_list(outnvl, propname, NULL,
6296 vd->vdev_stat.vs_ops[ZIO_TYPE_FREE],
6297 ZPROP_SRC_NONE);
6298 continue;
6299 case VDEV_PROP_OPS_CLAIM:
6300 vdev_prop_add_list(outnvl, propname, NULL,
6301 vd->vdev_stat.vs_ops[ZIO_TYPE_CLAIM],
6302 ZPROP_SRC_NONE);
6303 continue;
6304 case VDEV_PROP_OPS_TRIM:
6306 * TRIM ops and bytes are reported to user
6307 * space as ZIO_TYPE_FLUSH. This is done to
6308 * preserve the vdev_stat_t structure layout
6309 * for user space.
6311 vdev_prop_add_list(outnvl, propname, NULL,
6312 vd->vdev_stat.vs_ops[ZIO_TYPE_FLUSH],
6313 ZPROP_SRC_NONE);
6314 continue;
6315 case VDEV_PROP_BYTES_NULL:
6316 vdev_prop_add_list(outnvl, propname, NULL,
6317 vd->vdev_stat.vs_bytes[ZIO_TYPE_NULL],
6318 ZPROP_SRC_NONE);
6319 continue;
6320 case VDEV_PROP_BYTES_READ:
6321 vdev_prop_add_list(outnvl, propname, NULL,
6322 vd->vdev_stat.vs_bytes[ZIO_TYPE_READ],
6323 ZPROP_SRC_NONE);
6324 continue;
6325 case VDEV_PROP_BYTES_WRITE:
6326 vdev_prop_add_list(outnvl, propname, NULL,
6327 vd->vdev_stat.vs_bytes[ZIO_TYPE_WRITE],
6328 ZPROP_SRC_NONE);
6329 continue;
6330 case VDEV_PROP_BYTES_FREE:
6331 vdev_prop_add_list(outnvl, propname, NULL,
6332 vd->vdev_stat.vs_bytes[ZIO_TYPE_FREE],
6333 ZPROP_SRC_NONE);
6334 continue;
6335 case VDEV_PROP_BYTES_CLAIM:
6336 vdev_prop_add_list(outnvl, propname, NULL,
6337 vd->vdev_stat.vs_bytes[ZIO_TYPE_CLAIM],
6338 ZPROP_SRC_NONE);
6339 continue;
6340 case VDEV_PROP_BYTES_TRIM:
6342 * TRIM ops and bytes are reported to user
6343 * space as ZIO_TYPE_FLUSH. This is done to
6344 * preserve the vdev_stat_t structure layout
6345 * for user space.
6347 vdev_prop_add_list(outnvl, propname, NULL,
6348 vd->vdev_stat.vs_bytes[ZIO_TYPE_FLUSH],
6349 ZPROP_SRC_NONE);
6350 continue;
6351 case VDEV_PROP_REMOVING:
6352 vdev_prop_add_list(outnvl, propname, NULL,
6353 vd->vdev_removing, ZPROP_SRC_NONE);
6354 continue;
6355 case VDEV_PROP_RAIDZ_EXPANDING:
6356 /* Only expose this for raidz */
6357 if (vd->vdev_ops == &vdev_raidz_ops) {
6358 vdev_prop_add_list(outnvl, propname,
6359 NULL, vd->vdev_rz_expanding,
6360 ZPROP_SRC_NONE);
6362 continue;
6363 case VDEV_PROP_TRIM_SUPPORT:
6364 /* only valid for leaf vdevs */
6365 if (vd->vdev_ops->vdev_op_leaf) {
6366 vdev_prop_add_list(outnvl, propname,
6367 NULL, vd->vdev_has_trim,
6368 ZPROP_SRC_NONE);
6370 continue;
6371 /* Numeric Properites */
6372 case VDEV_PROP_ALLOCATING:
6373 /* Leaf vdevs cannot have this property */
6374 if (vd->vdev_mg == NULL &&
6375 vd->vdev_top != NULL) {
6376 src = ZPROP_SRC_NONE;
6377 intval = ZPROP_BOOLEAN_NA;
6378 } else {
6379 err = vdev_prop_get_int(vd, prop,
6380 &intval);
6381 if (err && err != ENOENT)
6382 break;
6384 if (intval ==
6385 vdev_prop_default_numeric(prop))
6386 src = ZPROP_SRC_DEFAULT;
6387 else
6388 src = ZPROP_SRC_LOCAL;
6391 vdev_prop_add_list(outnvl, propname, NULL,
6392 intval, src);
6393 break;
6394 case VDEV_PROP_FAILFAST:
6395 src = ZPROP_SRC_LOCAL;
6396 strval = NULL;
6398 err = zap_lookup(mos, objid, nvpair_name(elem),
6399 sizeof (uint64_t), 1, &intval);
6400 if (err == ENOENT) {
6401 intval = vdev_prop_default_numeric(
6402 prop);
6403 err = 0;
6404 } else if (err) {
6405 break;
6407 if (intval == vdev_prop_default_numeric(prop))
6408 src = ZPROP_SRC_DEFAULT;
6410 vdev_prop_add_list(outnvl, propname, strval,
6411 intval, src);
6412 break;
6413 case VDEV_PROP_CHECKSUM_N:
6414 case VDEV_PROP_CHECKSUM_T:
6415 case VDEV_PROP_IO_N:
6416 case VDEV_PROP_IO_T:
6417 case VDEV_PROP_SLOW_IO_N:
6418 case VDEV_PROP_SLOW_IO_T:
6419 err = vdev_prop_get_int(vd, prop, &intval);
6420 if (err && err != ENOENT)
6421 break;
6423 if (intval == vdev_prop_default_numeric(prop))
6424 src = ZPROP_SRC_DEFAULT;
6425 else
6426 src = ZPROP_SRC_LOCAL;
6428 vdev_prop_add_list(outnvl, propname, NULL,
6429 intval, src);
6430 break;
6431 /* Text Properties */
6432 case VDEV_PROP_COMMENT:
6433 /* Exists in the ZAP below */
6434 /* FALLTHRU */
6435 case VDEV_PROP_USERPROP:
6436 /* User Properites */
6437 src = ZPROP_SRC_LOCAL;
6439 err = zap_length(mos, objid, nvpair_name(elem),
6440 &integer_size, &num_integers);
6441 if (err)
6442 break;
6444 switch (integer_size) {
6445 case 8:
6446 /* User properties cannot be integers */
6447 err = EINVAL;
6448 break;
6449 case 1:
6450 /* string property */
6451 strval = kmem_alloc(num_integers,
6452 KM_SLEEP);
6453 err = zap_lookup(mos, objid,
6454 nvpair_name(elem), 1,
6455 num_integers, strval);
6456 if (err) {
6457 kmem_free(strval,
6458 num_integers);
6459 break;
6461 vdev_prop_add_list(outnvl, propname,
6462 strval, 0, src);
6463 kmem_free(strval, num_integers);
6464 break;
6466 break;
6467 default:
6468 err = ENOENT;
6469 break;
6471 if (err)
6472 break;
6474 } else {
6476 * Get all properties from the MOS vdev property object.
6478 zap_cursor_t zc;
6479 zap_attribute_t *za = zap_attribute_alloc();
6480 for (zap_cursor_init(&zc, mos, objid);
6481 (err = zap_cursor_retrieve(&zc, za)) == 0;
6482 zap_cursor_advance(&zc)) {
6483 intval = 0;
6484 strval = NULL;
6485 zprop_source_t src = ZPROP_SRC_DEFAULT;
6486 propname = za->za_name;
6488 switch (za->za_integer_length) {
6489 case 8:
6490 /* We do not allow integer user properties */
6491 /* This is likely an internal value */
6492 break;
6493 case 1:
6494 /* string property */
6495 strval = kmem_alloc(za->za_num_integers,
6496 KM_SLEEP);
6497 err = zap_lookup(mos, objid, za->za_name, 1,
6498 za->za_num_integers, strval);
6499 if (err) {
6500 kmem_free(strval, za->za_num_integers);
6501 break;
6503 vdev_prop_add_list(outnvl, propname, strval, 0,
6504 src);
6505 kmem_free(strval, za->za_num_integers);
6506 break;
6508 default:
6509 break;
6512 zap_cursor_fini(&zc);
6513 zap_attribute_free(za);
6516 mutex_exit(&spa->spa_props_lock);
6517 if (err && err != ENOENT) {
6518 return (err);
6521 return (0);
6524 EXPORT_SYMBOL(vdev_fault);
6525 EXPORT_SYMBOL(vdev_degrade);
6526 EXPORT_SYMBOL(vdev_online);
6527 EXPORT_SYMBOL(vdev_offline);
6528 EXPORT_SYMBOL(vdev_clear);
6530 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_count, UINT, ZMOD_RW,
6531 "Target number of metaslabs per top-level vdev");
6533 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_shift, UINT, ZMOD_RW,
6534 "Default lower limit for metaslab size");
6536 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, max_ms_shift, UINT, ZMOD_RW,
6537 "Default upper limit for metaslab size");
6539 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, min_ms_count, UINT, ZMOD_RW,
6540 "Minimum number of metaslabs per top-level vdev");
6542 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, ms_count_limit, UINT, ZMOD_RW,
6543 "Practical upper limit of total metaslabs per top-level vdev");
6545 ZFS_MODULE_PARAM(zfs, zfs_, slow_io_events_per_second, UINT, ZMOD_RW,
6546 "Rate limit slow IO (delay) events to this many per second");
6548 ZFS_MODULE_PARAM(zfs, zfs_, deadman_events_per_second, UINT, ZMOD_RW,
6549 "Rate limit hung IO (deadman) events to this many per second");
6551 ZFS_MODULE_PARAM(zfs, zfs_, dio_write_verify_events_per_second, UINT, ZMOD_RW,
6552 "Rate Direct I/O write verify events to this many per second");
6554 /* BEGIN CSTYLED */
6555 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, direct_write_verify, UINT, ZMOD_RW,
6556 "Direct I/O writes will perform for checksum verification before "
6557 "commiting write");
6559 ZFS_MODULE_PARAM(zfs, zfs_, checksum_events_per_second, UINT, ZMOD_RW,
6560 "Rate limit checksum events to this many checksum errors per second "
6561 "(do not set below ZED threshold).");
6562 /* END CSTYLED */
6564 ZFS_MODULE_PARAM(zfs, zfs_, scan_ignore_errors, INT, ZMOD_RW,
6565 "Ignore errors during resilver/scrub");
6567 ZFS_MODULE_PARAM(zfs_vdev, vdev_, validate_skip, INT, ZMOD_RW,
6568 "Bypass vdev_validate()");
6570 ZFS_MODULE_PARAM(zfs, zfs_, nocacheflush, INT, ZMOD_RW,
6571 "Disable cache flushes");
6573 ZFS_MODULE_PARAM(zfs, zfs_, embedded_slog_min_ms, UINT, ZMOD_RW,
6574 "Minimum number of metaslabs required to dedicate one for log blocks");
6576 /* BEGIN CSTYLED */
6577 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, min_auto_ashift,
6578 param_set_min_auto_ashift, param_get_uint, ZMOD_RW,
6579 "Minimum ashift used when creating new top-level vdevs");
6581 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, max_auto_ashift,
6582 param_set_max_auto_ashift, param_get_uint, ZMOD_RW,
6583 "Maximum ashift used when optimizing for logical -> physical sector "
6584 "size on new top-level vdevs");
6585 /* END CSTYLED */