BRT should return EOPNOTSUPP
[zfs.git] / module / zfs / vdev.c
blobf3812b843e95609eda35f6a202826345eec68024
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/zvol.h>
62 #include <sys/zfs_ratelimit.h>
63 #include "zfs_prop.h"
66 * One metaslab from each (normal-class) vdev is used by the ZIL. These are
67 * called "embedded slog metaslabs", are referenced by vdev_log_mg, and are
68 * part of the spa_embedded_log_class. The metaslab with the most free space
69 * in each vdev is selected for this purpose when the pool is opened (or a
70 * vdev is added). See vdev_metaslab_init().
72 * Log blocks can be allocated from the following locations. Each one is tried
73 * in order until the allocation succeeds:
74 * 1. dedicated log vdevs, aka "slog" (spa_log_class)
75 * 2. embedded slog metaslabs (spa_embedded_log_class)
76 * 3. other metaslabs in normal vdevs (spa_normal_class)
78 * zfs_embedded_slog_min_ms disables the embedded slog if there are fewer
79 * than this number of metaslabs in the vdev. This ensures that we don't set
80 * aside an unreasonable amount of space for the ZIL. If set to less than
81 * 1 << (spa_slop_shift + 1), on small pools the usable space may be reduced
82 * (by more than 1<<spa_slop_shift) due to the embedded slog metaslab.
84 static uint_t zfs_embedded_slog_min_ms = 64;
86 /* default target for number of metaslabs per top-level vdev */
87 static uint_t zfs_vdev_default_ms_count = 200;
89 /* minimum number of metaslabs per top-level vdev */
90 static uint_t zfs_vdev_min_ms_count = 16;
92 /* practical upper limit of total metaslabs per top-level vdev */
93 static uint_t zfs_vdev_ms_count_limit = 1ULL << 17;
95 /* lower limit for metaslab size (512M) */
96 static uint_t zfs_vdev_default_ms_shift = 29;
98 /* upper limit for metaslab size (16G) */
99 static uint_t zfs_vdev_max_ms_shift = 34;
101 int vdev_validate_skip = B_FALSE;
104 * Since the DTL space map of a vdev is not expected to have a lot of
105 * entries, we default its block size to 4K.
107 int zfs_vdev_dtl_sm_blksz = (1 << 12);
110 * Rate limit slow IO (delay) events to this many per second.
112 static unsigned int zfs_slow_io_events_per_second = 20;
115 * Rate limit checksum events after this many checksum errors per second.
117 static unsigned int zfs_checksum_events_per_second = 20;
120 * Ignore errors during scrub/resilver. Allows to work around resilver
121 * upon import when there are pool errors.
123 static int zfs_scan_ignore_errors = 0;
126 * vdev-wide space maps that have lots of entries written to them at
127 * the end of each transaction can benefit from a higher I/O bandwidth
128 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
130 int zfs_vdev_standard_sm_blksz = (1 << 17);
133 * Tunable parameter for debugging or performance analysis. Setting this
134 * will cause pool corruption on power loss if a volatile out-of-order
135 * write cache is enabled.
137 int zfs_nocacheflush = 0;
140 * Maximum and minimum ashift values that can be automatically set based on
141 * vdev's physical ashift (disk's physical sector size). While ASHIFT_MAX
142 * is higher than the maximum value, it is intentionally limited here to not
143 * excessively impact pool space efficiency. Higher ashift values may still
144 * be forced by vdev logical ashift or by user via ashift property, but won't
145 * be set automatically as a performance optimization.
147 uint_t zfs_vdev_max_auto_ashift = 14;
148 uint_t zfs_vdev_min_auto_ashift = ASHIFT_MIN;
150 void
151 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
153 va_list adx;
154 char buf[256];
156 va_start(adx, fmt);
157 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
158 va_end(adx);
160 if (vd->vdev_path != NULL) {
161 zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
162 vd->vdev_path, buf);
163 } else {
164 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
165 vd->vdev_ops->vdev_op_type,
166 (u_longlong_t)vd->vdev_id,
167 (u_longlong_t)vd->vdev_guid, buf);
171 void
172 vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
174 char state[20];
176 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
177 zfs_dbgmsg("%*svdev %llu: %s", indent, "",
178 (u_longlong_t)vd->vdev_id,
179 vd->vdev_ops->vdev_op_type);
180 return;
183 switch (vd->vdev_state) {
184 case VDEV_STATE_UNKNOWN:
185 (void) snprintf(state, sizeof (state), "unknown");
186 break;
187 case VDEV_STATE_CLOSED:
188 (void) snprintf(state, sizeof (state), "closed");
189 break;
190 case VDEV_STATE_OFFLINE:
191 (void) snprintf(state, sizeof (state), "offline");
192 break;
193 case VDEV_STATE_REMOVED:
194 (void) snprintf(state, sizeof (state), "removed");
195 break;
196 case VDEV_STATE_CANT_OPEN:
197 (void) snprintf(state, sizeof (state), "can't open");
198 break;
199 case VDEV_STATE_FAULTED:
200 (void) snprintf(state, sizeof (state), "faulted");
201 break;
202 case VDEV_STATE_DEGRADED:
203 (void) snprintf(state, sizeof (state), "degraded");
204 break;
205 case VDEV_STATE_HEALTHY:
206 (void) snprintf(state, sizeof (state), "healthy");
207 break;
208 default:
209 (void) snprintf(state, sizeof (state), "<state %u>",
210 (uint_t)vd->vdev_state);
213 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
214 "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type,
215 vd->vdev_islog ? " (log)" : "",
216 (u_longlong_t)vd->vdev_guid,
217 vd->vdev_path ? vd->vdev_path : "N/A", state);
219 for (uint64_t i = 0; i < vd->vdev_children; i++)
220 vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
224 * Virtual device management.
227 static vdev_ops_t *const vdev_ops_table[] = {
228 &vdev_root_ops,
229 &vdev_raidz_ops,
230 &vdev_draid_ops,
231 &vdev_draid_spare_ops,
232 &vdev_mirror_ops,
233 &vdev_replacing_ops,
234 &vdev_spare_ops,
235 &vdev_disk_ops,
236 &vdev_file_ops,
237 &vdev_missing_ops,
238 &vdev_hole_ops,
239 &vdev_indirect_ops,
240 NULL
244 * Given a vdev type, return the appropriate ops vector.
246 static vdev_ops_t *
247 vdev_getops(const char *type)
249 vdev_ops_t *ops, *const *opspp;
251 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
252 if (strcmp(ops->vdev_op_type, type) == 0)
253 break;
255 return (ops);
259 * Given a vdev and a metaslab class, find which metaslab group we're
260 * interested in. All vdevs may belong to two different metaslab classes.
261 * Dedicated slog devices use only the primary metaslab group, rather than a
262 * separate log group. For embedded slogs, the vdev_log_mg will be non-NULL.
264 metaslab_group_t *
265 vdev_get_mg(vdev_t *vd, metaslab_class_t *mc)
267 if (mc == spa_embedded_log_class(vd->vdev_spa) &&
268 vd->vdev_log_mg != NULL)
269 return (vd->vdev_log_mg);
270 else
271 return (vd->vdev_mg);
274 void
275 vdev_default_xlate(vdev_t *vd, const range_seg64_t *logical_rs,
276 range_seg64_t *physical_rs, range_seg64_t *remain_rs)
278 (void) vd, (void) remain_rs;
280 physical_rs->rs_start = logical_rs->rs_start;
281 physical_rs->rs_end = logical_rs->rs_end;
285 * Derive the enumerated allocation bias from string input.
286 * String origin is either the per-vdev zap or zpool(8).
288 static vdev_alloc_bias_t
289 vdev_derive_alloc_bias(const char *bias)
291 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
293 if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0)
294 alloc_bias = VDEV_BIAS_LOG;
295 else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
296 alloc_bias = VDEV_BIAS_SPECIAL;
297 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
298 alloc_bias = VDEV_BIAS_DEDUP;
300 return (alloc_bias);
304 * Default asize function: return the MAX of psize with the asize of
305 * all children. This is what's used by anything other than RAID-Z.
307 uint64_t
308 vdev_default_asize(vdev_t *vd, uint64_t psize)
310 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
311 uint64_t csize;
313 for (int c = 0; c < vd->vdev_children; c++) {
314 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
315 asize = MAX(asize, csize);
318 return (asize);
321 uint64_t
322 vdev_default_min_asize(vdev_t *vd)
324 return (vd->vdev_min_asize);
328 * Get the minimum allocatable size. We define the allocatable size as
329 * the vdev's asize rounded to the nearest metaslab. This allows us to
330 * replace or attach devices which don't have the same physical size but
331 * can still satisfy the same number of allocations.
333 uint64_t
334 vdev_get_min_asize(vdev_t *vd)
336 vdev_t *pvd = vd->vdev_parent;
339 * If our parent is NULL (inactive spare or cache) or is the root,
340 * just return our own asize.
342 if (pvd == NULL)
343 return (vd->vdev_asize);
346 * The top-level vdev just returns the allocatable size rounded
347 * to the nearest metaslab.
349 if (vd == vd->vdev_top)
350 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
352 return (pvd->vdev_ops->vdev_op_min_asize(pvd));
355 void
356 vdev_set_min_asize(vdev_t *vd)
358 vd->vdev_min_asize = vdev_get_min_asize(vd);
360 for (int c = 0; c < vd->vdev_children; c++)
361 vdev_set_min_asize(vd->vdev_child[c]);
365 * Get the minimal allocation size for the top-level vdev.
367 uint64_t
368 vdev_get_min_alloc(vdev_t *vd)
370 uint64_t min_alloc = 1ULL << vd->vdev_ashift;
372 if (vd->vdev_ops->vdev_op_min_alloc != NULL)
373 min_alloc = vd->vdev_ops->vdev_op_min_alloc(vd);
375 return (min_alloc);
379 * Get the parity level for a top-level vdev.
381 uint64_t
382 vdev_get_nparity(vdev_t *vd)
384 uint64_t nparity = 0;
386 if (vd->vdev_ops->vdev_op_nparity != NULL)
387 nparity = vd->vdev_ops->vdev_op_nparity(vd);
389 return (nparity);
392 static int
393 vdev_prop_get_int(vdev_t *vd, vdev_prop_t prop, uint64_t *value)
395 spa_t *spa = vd->vdev_spa;
396 objset_t *mos = spa->spa_meta_objset;
397 uint64_t objid;
398 int err;
400 if (vd->vdev_root_zap != 0) {
401 objid = vd->vdev_root_zap;
402 } else if (vd->vdev_top_zap != 0) {
403 objid = vd->vdev_top_zap;
404 } else if (vd->vdev_leaf_zap != 0) {
405 objid = vd->vdev_leaf_zap;
406 } else {
407 return (EINVAL);
410 err = zap_lookup(mos, objid, vdev_prop_to_name(prop),
411 sizeof (uint64_t), 1, value);
413 if (err == ENOENT)
414 *value = vdev_prop_default_numeric(prop);
416 return (err);
420 * Get the number of data disks for a top-level vdev.
422 uint64_t
423 vdev_get_ndisks(vdev_t *vd)
425 uint64_t ndisks = 1;
427 if (vd->vdev_ops->vdev_op_ndisks != NULL)
428 ndisks = vd->vdev_ops->vdev_op_ndisks(vd);
430 return (ndisks);
433 vdev_t *
434 vdev_lookup_top(spa_t *spa, uint64_t vdev)
436 vdev_t *rvd = spa->spa_root_vdev;
438 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
440 if (vdev < rvd->vdev_children) {
441 ASSERT(rvd->vdev_child[vdev] != NULL);
442 return (rvd->vdev_child[vdev]);
445 return (NULL);
448 vdev_t *
449 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
451 vdev_t *mvd;
453 if (vd->vdev_guid == guid)
454 return (vd);
456 for (int c = 0; c < vd->vdev_children; c++)
457 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
458 NULL)
459 return (mvd);
461 return (NULL);
464 static int
465 vdev_count_leaves_impl(vdev_t *vd)
467 int n = 0;
469 if (vd->vdev_ops->vdev_op_leaf)
470 return (1);
472 for (int c = 0; c < vd->vdev_children; c++)
473 n += vdev_count_leaves_impl(vd->vdev_child[c]);
475 return (n);
479 vdev_count_leaves(spa_t *spa)
481 int rc;
483 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
484 rc = vdev_count_leaves_impl(spa->spa_root_vdev);
485 spa_config_exit(spa, SCL_VDEV, FTAG);
487 return (rc);
490 void
491 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
493 size_t oldsize, newsize;
494 uint64_t id = cvd->vdev_id;
495 vdev_t **newchild;
497 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
498 ASSERT(cvd->vdev_parent == NULL);
500 cvd->vdev_parent = pvd;
502 if (pvd == NULL)
503 return;
505 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
507 oldsize = pvd->vdev_children * sizeof (vdev_t *);
508 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
509 newsize = pvd->vdev_children * sizeof (vdev_t *);
511 newchild = kmem_alloc(newsize, KM_SLEEP);
512 if (pvd->vdev_child != NULL) {
513 memcpy(newchild, pvd->vdev_child, oldsize);
514 kmem_free(pvd->vdev_child, oldsize);
517 pvd->vdev_child = newchild;
518 pvd->vdev_child[id] = cvd;
520 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
521 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
524 * Walk up all ancestors to update guid sum.
526 for (; pvd != NULL; pvd = pvd->vdev_parent)
527 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
529 if (cvd->vdev_ops->vdev_op_leaf) {
530 list_insert_head(&cvd->vdev_spa->spa_leaf_list, cvd);
531 cvd->vdev_spa->spa_leaf_list_gen++;
535 void
536 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
538 int c;
539 uint_t id = cvd->vdev_id;
541 ASSERT(cvd->vdev_parent == pvd);
543 if (pvd == NULL)
544 return;
546 ASSERT(id < pvd->vdev_children);
547 ASSERT(pvd->vdev_child[id] == cvd);
549 pvd->vdev_child[id] = NULL;
550 cvd->vdev_parent = NULL;
552 for (c = 0; c < pvd->vdev_children; c++)
553 if (pvd->vdev_child[c])
554 break;
556 if (c == pvd->vdev_children) {
557 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
558 pvd->vdev_child = NULL;
559 pvd->vdev_children = 0;
562 if (cvd->vdev_ops->vdev_op_leaf) {
563 spa_t *spa = cvd->vdev_spa;
564 list_remove(&spa->spa_leaf_list, cvd);
565 spa->spa_leaf_list_gen++;
569 * Walk up all ancestors to update guid sum.
571 for (; pvd != NULL; pvd = pvd->vdev_parent)
572 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
576 * Remove any holes in the child array.
578 void
579 vdev_compact_children(vdev_t *pvd)
581 vdev_t **newchild, *cvd;
582 int oldc = pvd->vdev_children;
583 int newc;
585 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
587 if (oldc == 0)
588 return;
590 for (int c = newc = 0; c < oldc; c++)
591 if (pvd->vdev_child[c])
592 newc++;
594 if (newc > 0) {
595 newchild = kmem_zalloc(newc * sizeof (vdev_t *), KM_SLEEP);
597 for (int c = newc = 0; c < oldc; c++) {
598 if ((cvd = pvd->vdev_child[c]) != NULL) {
599 newchild[newc] = cvd;
600 cvd->vdev_id = newc++;
603 } else {
604 newchild = NULL;
607 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
608 pvd->vdev_child = newchild;
609 pvd->vdev_children = newc;
613 * Allocate and minimally initialize a vdev_t.
615 vdev_t *
616 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
618 vdev_t *vd;
619 vdev_indirect_config_t *vic;
621 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
622 vic = &vd->vdev_indirect_config;
624 if (spa->spa_root_vdev == NULL) {
625 ASSERT(ops == &vdev_root_ops);
626 spa->spa_root_vdev = vd;
627 spa->spa_load_guid = spa_generate_guid(NULL);
630 if (guid == 0 && ops != &vdev_hole_ops) {
631 if (spa->spa_root_vdev == vd) {
633 * The root vdev's guid will also be the pool guid,
634 * which must be unique among all pools.
636 guid = spa_generate_guid(NULL);
637 } else {
639 * Any other vdev's guid must be unique within the pool.
641 guid = spa_generate_guid(spa);
643 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
646 vd->vdev_spa = spa;
647 vd->vdev_id = id;
648 vd->vdev_guid = guid;
649 vd->vdev_guid_sum = guid;
650 vd->vdev_ops = ops;
651 vd->vdev_state = VDEV_STATE_CLOSED;
652 vd->vdev_ishole = (ops == &vdev_hole_ops);
653 vic->vic_prev_indirect_vdev = UINT64_MAX;
655 rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
656 mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
657 vd->vdev_obsolete_segments = range_tree_create(NULL, RANGE_SEG64, NULL,
658 0, 0);
661 * Initialize rate limit structs for events. We rate limit ZIO delay
662 * and checksum events so that we don't overwhelm ZED with thousands
663 * of events when a disk is acting up.
665 zfs_ratelimit_init(&vd->vdev_delay_rl, &zfs_slow_io_events_per_second,
667 zfs_ratelimit_init(&vd->vdev_deadman_rl, &zfs_slow_io_events_per_second,
669 zfs_ratelimit_init(&vd->vdev_checksum_rl,
670 &zfs_checksum_events_per_second, 1);
673 * Default Thresholds for tuning ZED
675 vd->vdev_checksum_n = vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_N);
676 vd->vdev_checksum_t = vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_T);
677 vd->vdev_io_n = vdev_prop_default_numeric(VDEV_PROP_IO_N);
678 vd->vdev_io_t = vdev_prop_default_numeric(VDEV_PROP_IO_T);
680 list_link_init(&vd->vdev_config_dirty_node);
681 list_link_init(&vd->vdev_state_dirty_node);
682 list_link_init(&vd->vdev_initialize_node);
683 list_link_init(&vd->vdev_leaf_node);
684 list_link_init(&vd->vdev_trim_node);
686 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_NOLOCKDEP, NULL);
687 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
688 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
689 mutex_init(&vd->vdev_scan_io_queue_lock, NULL, MUTEX_DEFAULT, NULL);
691 mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL);
692 mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL);
693 cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL);
694 cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL);
696 mutex_init(&vd->vdev_trim_lock, NULL, MUTEX_DEFAULT, NULL);
697 mutex_init(&vd->vdev_autotrim_lock, NULL, MUTEX_DEFAULT, NULL);
698 mutex_init(&vd->vdev_trim_io_lock, NULL, MUTEX_DEFAULT, NULL);
699 cv_init(&vd->vdev_trim_cv, NULL, CV_DEFAULT, NULL);
700 cv_init(&vd->vdev_autotrim_cv, NULL, CV_DEFAULT, NULL);
701 cv_init(&vd->vdev_autotrim_kick_cv, NULL, CV_DEFAULT, NULL);
702 cv_init(&vd->vdev_trim_io_cv, NULL, CV_DEFAULT, NULL);
704 mutex_init(&vd->vdev_rebuild_lock, NULL, MUTEX_DEFAULT, NULL);
705 cv_init(&vd->vdev_rebuild_cv, NULL, CV_DEFAULT, NULL);
707 for (int t = 0; t < DTL_TYPES; t++) {
708 vd->vdev_dtl[t] = range_tree_create(NULL, RANGE_SEG64, NULL, 0,
712 txg_list_create(&vd->vdev_ms_list, spa,
713 offsetof(struct metaslab, ms_txg_node));
714 txg_list_create(&vd->vdev_dtl_list, spa,
715 offsetof(struct vdev, vdev_dtl_node));
716 vd->vdev_stat.vs_timestamp = gethrtime();
717 vdev_queue_init(vd);
719 return (vd);
723 * Allocate a new vdev. The 'alloctype' is used to control whether we are
724 * creating a new vdev or loading an existing one - the behavior is slightly
725 * different for each case.
728 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
729 int alloctype)
731 vdev_ops_t *ops;
732 const char *type;
733 uint64_t guid = 0, islog;
734 vdev_t *vd;
735 vdev_indirect_config_t *vic;
736 const char *tmp = NULL;
737 int rc;
738 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
739 boolean_t top_level = (parent && !parent->vdev_parent);
741 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
743 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
744 return (SET_ERROR(EINVAL));
746 if ((ops = vdev_getops(type)) == NULL)
747 return (SET_ERROR(EINVAL));
750 * If this is a load, get the vdev guid from the nvlist.
751 * Otherwise, vdev_alloc_common() will generate one for us.
753 if (alloctype == VDEV_ALLOC_LOAD) {
754 uint64_t label_id;
756 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
757 label_id != id)
758 return (SET_ERROR(EINVAL));
760 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
761 return (SET_ERROR(EINVAL));
762 } else if (alloctype == VDEV_ALLOC_SPARE) {
763 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
764 return (SET_ERROR(EINVAL));
765 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
766 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
767 return (SET_ERROR(EINVAL));
768 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
769 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
770 return (SET_ERROR(EINVAL));
774 * The first allocated vdev must be of type 'root'.
776 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
777 return (SET_ERROR(EINVAL));
780 * Determine whether we're a log vdev.
782 islog = 0;
783 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
784 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
785 return (SET_ERROR(ENOTSUP));
787 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
788 return (SET_ERROR(ENOTSUP));
790 if (top_level && alloctype == VDEV_ALLOC_ADD) {
791 const char *bias;
794 * If creating a top-level vdev, check for allocation
795 * classes input.
797 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
798 &bias) == 0) {
799 alloc_bias = vdev_derive_alloc_bias(bias);
801 /* spa_vdev_add() expects feature to be enabled */
802 if (spa->spa_load_state != SPA_LOAD_CREATE &&
803 !spa_feature_is_enabled(spa,
804 SPA_FEATURE_ALLOCATION_CLASSES)) {
805 return (SET_ERROR(ENOTSUP));
809 /* spa_vdev_add() expects feature to be enabled */
810 if (ops == &vdev_draid_ops &&
811 spa->spa_load_state != SPA_LOAD_CREATE &&
812 !spa_feature_is_enabled(spa, SPA_FEATURE_DRAID)) {
813 return (SET_ERROR(ENOTSUP));
818 * Initialize the vdev specific data. This is done before calling
819 * vdev_alloc_common() since it may fail and this simplifies the
820 * error reporting and cleanup code paths.
822 void *tsd = NULL;
823 if (ops->vdev_op_init != NULL) {
824 rc = ops->vdev_op_init(spa, nv, &tsd);
825 if (rc != 0) {
826 return (rc);
830 vd = vdev_alloc_common(spa, id, guid, ops);
831 vd->vdev_tsd = tsd;
832 vd->vdev_islog = islog;
834 if (top_level && alloc_bias != VDEV_BIAS_NONE)
835 vd->vdev_alloc_bias = alloc_bias;
837 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &tmp) == 0)
838 vd->vdev_path = spa_strdup(tmp);
841 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
842 * fault on a vdev and want it to persist across imports (like with
843 * zpool offline -f).
845 rc = nvlist_lookup_string(nv, ZPOOL_CONFIG_AUX_STATE, &tmp);
846 if (rc == 0 && tmp != NULL && strcmp(tmp, "external") == 0) {
847 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL;
848 vd->vdev_faulted = 1;
849 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
852 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &tmp) == 0)
853 vd->vdev_devid = spa_strdup(tmp);
854 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, &tmp) == 0)
855 vd->vdev_physpath = spa_strdup(tmp);
857 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
858 &tmp) == 0)
859 vd->vdev_enc_sysfs_path = spa_strdup(tmp);
861 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &tmp) == 0)
862 vd->vdev_fru = spa_strdup(tmp);
865 * Set the whole_disk property. If it's not specified, leave the value
866 * as -1.
868 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
869 &vd->vdev_wholedisk) != 0)
870 vd->vdev_wholedisk = -1ULL;
872 vic = &vd->vdev_indirect_config;
874 ASSERT0(vic->vic_mapping_object);
875 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
876 &vic->vic_mapping_object);
877 ASSERT0(vic->vic_births_object);
878 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
879 &vic->vic_births_object);
880 ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
881 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
882 &vic->vic_prev_indirect_vdev);
885 * Look for the 'not present' flag. This will only be set if the device
886 * was not present at the time of import.
888 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
889 &vd->vdev_not_present);
892 * Get the alignment requirement. Ignore pool ashift for vdev
893 * attach case.
895 if (alloctype != VDEV_ALLOC_ATTACH) {
896 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT,
897 &vd->vdev_ashift);
898 } else {
899 vd->vdev_attaching = B_TRUE;
903 * Retrieve the vdev creation time.
905 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
906 &vd->vdev_crtxg);
908 if (vd->vdev_ops == &vdev_root_ops &&
909 (alloctype == VDEV_ALLOC_LOAD ||
910 alloctype == VDEV_ALLOC_SPLIT ||
911 alloctype == VDEV_ALLOC_ROOTPOOL)) {
912 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_ROOT_ZAP,
913 &vd->vdev_root_zap);
917 * If we're a top-level vdev, try to load the allocation parameters.
919 if (top_level &&
920 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
921 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
922 &vd->vdev_ms_array);
923 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
924 &vd->vdev_ms_shift);
925 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
926 &vd->vdev_asize);
927 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NONALLOCATING,
928 &vd->vdev_noalloc);
929 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
930 &vd->vdev_removing);
931 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
932 &vd->vdev_top_zap);
933 } else {
934 ASSERT0(vd->vdev_top_zap);
937 if (top_level && alloctype != VDEV_ALLOC_ATTACH) {
938 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
939 alloctype == VDEV_ALLOC_ADD ||
940 alloctype == VDEV_ALLOC_SPLIT ||
941 alloctype == VDEV_ALLOC_ROOTPOOL);
942 /* Note: metaslab_group_create() is now deferred */
945 if (vd->vdev_ops->vdev_op_leaf &&
946 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
947 (void) nvlist_lookup_uint64(nv,
948 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
949 } else {
950 ASSERT0(vd->vdev_leaf_zap);
954 * If we're a leaf vdev, try to load the DTL object and other state.
957 if (vd->vdev_ops->vdev_op_leaf &&
958 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
959 alloctype == VDEV_ALLOC_ROOTPOOL)) {
960 if (alloctype == VDEV_ALLOC_LOAD) {
961 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
962 &vd->vdev_dtl_object);
963 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
964 &vd->vdev_unspare);
967 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
968 uint64_t spare = 0;
970 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
971 &spare) == 0 && spare)
972 spa_spare_add(vd);
975 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
976 &vd->vdev_offline);
978 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
979 &vd->vdev_resilver_txg);
981 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REBUILD_TXG,
982 &vd->vdev_rebuild_txg);
984 if (nvlist_exists(nv, ZPOOL_CONFIG_RESILVER_DEFER))
985 vdev_defer_resilver(vd);
988 * In general, when importing a pool we want to ignore the
989 * persistent fault state, as the diagnosis made on another
990 * system may not be valid in the current context. The only
991 * exception is if we forced a vdev to a persistently faulted
992 * state with 'zpool offline -f'. The persistent fault will
993 * remain across imports until cleared.
995 * Local vdevs will remain in the faulted state.
997 if (spa_load_state(spa) == SPA_LOAD_OPEN ||
998 spa_load_state(spa) == SPA_LOAD_IMPORT) {
999 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
1000 &vd->vdev_faulted);
1001 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
1002 &vd->vdev_degraded);
1003 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
1004 &vd->vdev_removed);
1006 if (vd->vdev_faulted || vd->vdev_degraded) {
1007 const char *aux;
1009 vd->vdev_label_aux =
1010 VDEV_AUX_ERR_EXCEEDED;
1011 if (nvlist_lookup_string(nv,
1012 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
1013 strcmp(aux, "external") == 0)
1014 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
1015 else
1016 vd->vdev_faulted = 0ULL;
1022 * Add ourselves to the parent's list of children.
1024 vdev_add_child(parent, vd);
1026 *vdp = vd;
1028 return (0);
1031 void
1032 vdev_free(vdev_t *vd)
1034 spa_t *spa = vd->vdev_spa;
1036 ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
1037 ASSERT3P(vd->vdev_trim_thread, ==, NULL);
1038 ASSERT3P(vd->vdev_autotrim_thread, ==, NULL);
1039 ASSERT3P(vd->vdev_rebuild_thread, ==, NULL);
1042 * Scan queues are normally destroyed at the end of a scan. If the
1043 * queue exists here, that implies the vdev is being removed while
1044 * the scan is still running.
1046 if (vd->vdev_scan_io_queue != NULL) {
1047 mutex_enter(&vd->vdev_scan_io_queue_lock);
1048 dsl_scan_io_queue_destroy(vd->vdev_scan_io_queue);
1049 vd->vdev_scan_io_queue = NULL;
1050 mutex_exit(&vd->vdev_scan_io_queue_lock);
1054 * vdev_free() implies closing the vdev first. This is simpler than
1055 * trying to ensure complicated semantics for all callers.
1057 vdev_close(vd);
1059 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
1060 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
1063 * Free all children.
1065 for (int c = 0; c < vd->vdev_children; c++)
1066 vdev_free(vd->vdev_child[c]);
1068 ASSERT(vd->vdev_child == NULL);
1069 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
1071 if (vd->vdev_ops->vdev_op_fini != NULL)
1072 vd->vdev_ops->vdev_op_fini(vd);
1075 * Discard allocation state.
1077 if (vd->vdev_mg != NULL) {
1078 vdev_metaslab_fini(vd);
1079 metaslab_group_destroy(vd->vdev_mg);
1080 vd->vdev_mg = NULL;
1082 if (vd->vdev_log_mg != NULL) {
1083 ASSERT0(vd->vdev_ms_count);
1084 metaslab_group_destroy(vd->vdev_log_mg);
1085 vd->vdev_log_mg = NULL;
1088 ASSERT0(vd->vdev_stat.vs_space);
1089 ASSERT0(vd->vdev_stat.vs_dspace);
1090 ASSERT0(vd->vdev_stat.vs_alloc);
1093 * Remove this vdev from its parent's child list.
1095 vdev_remove_child(vd->vdev_parent, vd);
1097 ASSERT(vd->vdev_parent == NULL);
1098 ASSERT(!list_link_active(&vd->vdev_leaf_node));
1101 * Clean up vdev structure.
1103 vdev_queue_fini(vd);
1105 if (vd->vdev_path)
1106 spa_strfree(vd->vdev_path);
1107 if (vd->vdev_devid)
1108 spa_strfree(vd->vdev_devid);
1109 if (vd->vdev_physpath)
1110 spa_strfree(vd->vdev_physpath);
1112 if (vd->vdev_enc_sysfs_path)
1113 spa_strfree(vd->vdev_enc_sysfs_path);
1115 if (vd->vdev_fru)
1116 spa_strfree(vd->vdev_fru);
1118 if (vd->vdev_isspare)
1119 spa_spare_remove(vd);
1120 if (vd->vdev_isl2cache)
1121 spa_l2cache_remove(vd);
1123 txg_list_destroy(&vd->vdev_ms_list);
1124 txg_list_destroy(&vd->vdev_dtl_list);
1126 mutex_enter(&vd->vdev_dtl_lock);
1127 space_map_close(vd->vdev_dtl_sm);
1128 for (int t = 0; t < DTL_TYPES; t++) {
1129 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
1130 range_tree_destroy(vd->vdev_dtl[t]);
1132 mutex_exit(&vd->vdev_dtl_lock);
1134 EQUIV(vd->vdev_indirect_births != NULL,
1135 vd->vdev_indirect_mapping != NULL);
1136 if (vd->vdev_indirect_births != NULL) {
1137 vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
1138 vdev_indirect_births_close(vd->vdev_indirect_births);
1141 if (vd->vdev_obsolete_sm != NULL) {
1142 ASSERT(vd->vdev_removing ||
1143 vd->vdev_ops == &vdev_indirect_ops);
1144 space_map_close(vd->vdev_obsolete_sm);
1145 vd->vdev_obsolete_sm = NULL;
1147 range_tree_destroy(vd->vdev_obsolete_segments);
1148 rw_destroy(&vd->vdev_indirect_rwlock);
1149 mutex_destroy(&vd->vdev_obsolete_lock);
1151 mutex_destroy(&vd->vdev_dtl_lock);
1152 mutex_destroy(&vd->vdev_stat_lock);
1153 mutex_destroy(&vd->vdev_probe_lock);
1154 mutex_destroy(&vd->vdev_scan_io_queue_lock);
1156 mutex_destroy(&vd->vdev_initialize_lock);
1157 mutex_destroy(&vd->vdev_initialize_io_lock);
1158 cv_destroy(&vd->vdev_initialize_io_cv);
1159 cv_destroy(&vd->vdev_initialize_cv);
1161 mutex_destroy(&vd->vdev_trim_lock);
1162 mutex_destroy(&vd->vdev_autotrim_lock);
1163 mutex_destroy(&vd->vdev_trim_io_lock);
1164 cv_destroy(&vd->vdev_trim_cv);
1165 cv_destroy(&vd->vdev_autotrim_cv);
1166 cv_destroy(&vd->vdev_autotrim_kick_cv);
1167 cv_destroy(&vd->vdev_trim_io_cv);
1169 mutex_destroy(&vd->vdev_rebuild_lock);
1170 cv_destroy(&vd->vdev_rebuild_cv);
1172 zfs_ratelimit_fini(&vd->vdev_delay_rl);
1173 zfs_ratelimit_fini(&vd->vdev_deadman_rl);
1174 zfs_ratelimit_fini(&vd->vdev_checksum_rl);
1176 if (vd == spa->spa_root_vdev)
1177 spa->spa_root_vdev = NULL;
1179 kmem_free(vd, sizeof (vdev_t));
1183 * Transfer top-level vdev state from svd to tvd.
1185 static void
1186 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
1188 spa_t *spa = svd->vdev_spa;
1189 metaslab_t *msp;
1190 vdev_t *vd;
1191 int t;
1193 ASSERT(tvd == tvd->vdev_top);
1195 tvd->vdev_pending_fastwrite = svd->vdev_pending_fastwrite;
1196 tvd->vdev_ms_array = svd->vdev_ms_array;
1197 tvd->vdev_ms_shift = svd->vdev_ms_shift;
1198 tvd->vdev_ms_count = svd->vdev_ms_count;
1199 tvd->vdev_top_zap = svd->vdev_top_zap;
1201 svd->vdev_ms_array = 0;
1202 svd->vdev_ms_shift = 0;
1203 svd->vdev_ms_count = 0;
1204 svd->vdev_top_zap = 0;
1206 if (tvd->vdev_mg)
1207 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
1208 if (tvd->vdev_log_mg)
1209 ASSERT3P(tvd->vdev_log_mg, ==, svd->vdev_log_mg);
1210 tvd->vdev_mg = svd->vdev_mg;
1211 tvd->vdev_log_mg = svd->vdev_log_mg;
1212 tvd->vdev_ms = svd->vdev_ms;
1214 svd->vdev_mg = NULL;
1215 svd->vdev_log_mg = NULL;
1216 svd->vdev_ms = NULL;
1218 if (tvd->vdev_mg != NULL)
1219 tvd->vdev_mg->mg_vd = tvd;
1220 if (tvd->vdev_log_mg != NULL)
1221 tvd->vdev_log_mg->mg_vd = tvd;
1223 tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm;
1224 svd->vdev_checkpoint_sm = NULL;
1226 tvd->vdev_alloc_bias = svd->vdev_alloc_bias;
1227 svd->vdev_alloc_bias = VDEV_BIAS_NONE;
1229 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
1230 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
1231 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
1233 svd->vdev_stat.vs_alloc = 0;
1234 svd->vdev_stat.vs_space = 0;
1235 svd->vdev_stat.vs_dspace = 0;
1238 * State which may be set on a top-level vdev that's in the
1239 * process of being removed.
1241 ASSERT0(tvd->vdev_indirect_config.vic_births_object);
1242 ASSERT0(tvd->vdev_indirect_config.vic_mapping_object);
1243 ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL);
1244 ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL);
1245 ASSERT3P(tvd->vdev_indirect_births, ==, NULL);
1246 ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL);
1247 ASSERT0(tvd->vdev_noalloc);
1248 ASSERT0(tvd->vdev_removing);
1249 ASSERT0(tvd->vdev_rebuilding);
1250 tvd->vdev_noalloc = svd->vdev_noalloc;
1251 tvd->vdev_removing = svd->vdev_removing;
1252 tvd->vdev_rebuilding = svd->vdev_rebuilding;
1253 tvd->vdev_rebuild_config = svd->vdev_rebuild_config;
1254 tvd->vdev_indirect_config = svd->vdev_indirect_config;
1255 tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping;
1256 tvd->vdev_indirect_births = svd->vdev_indirect_births;
1257 range_tree_swap(&svd->vdev_obsolete_segments,
1258 &tvd->vdev_obsolete_segments);
1259 tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm;
1260 svd->vdev_indirect_config.vic_mapping_object = 0;
1261 svd->vdev_indirect_config.vic_births_object = 0;
1262 svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL;
1263 svd->vdev_indirect_mapping = NULL;
1264 svd->vdev_indirect_births = NULL;
1265 svd->vdev_obsolete_sm = NULL;
1266 svd->vdev_noalloc = 0;
1267 svd->vdev_removing = 0;
1268 svd->vdev_rebuilding = 0;
1270 for (t = 0; t < TXG_SIZE; t++) {
1271 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
1272 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
1273 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
1274 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
1275 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
1276 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
1279 if (list_link_active(&svd->vdev_config_dirty_node)) {
1280 vdev_config_clean(svd);
1281 vdev_config_dirty(tvd);
1284 if (list_link_active(&svd->vdev_state_dirty_node)) {
1285 vdev_state_clean(svd);
1286 vdev_state_dirty(tvd);
1289 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
1290 svd->vdev_deflate_ratio = 0;
1292 tvd->vdev_islog = svd->vdev_islog;
1293 svd->vdev_islog = 0;
1295 dsl_scan_io_queue_vdev_xfer(svd, tvd);
1298 static void
1299 vdev_top_update(vdev_t *tvd, vdev_t *vd)
1301 if (vd == NULL)
1302 return;
1304 vd->vdev_top = tvd;
1306 for (int c = 0; c < vd->vdev_children; c++)
1307 vdev_top_update(tvd, vd->vdev_child[c]);
1311 * Add a mirror/replacing vdev above an existing vdev. There is no need to
1312 * call .vdev_op_init() since mirror/replacing vdevs do not have private state.
1314 vdev_t *
1315 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
1317 spa_t *spa = cvd->vdev_spa;
1318 vdev_t *pvd = cvd->vdev_parent;
1319 vdev_t *mvd;
1321 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1323 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
1325 mvd->vdev_asize = cvd->vdev_asize;
1326 mvd->vdev_min_asize = cvd->vdev_min_asize;
1327 mvd->vdev_max_asize = cvd->vdev_max_asize;
1328 mvd->vdev_psize = cvd->vdev_psize;
1329 mvd->vdev_ashift = cvd->vdev_ashift;
1330 mvd->vdev_logical_ashift = cvd->vdev_logical_ashift;
1331 mvd->vdev_physical_ashift = cvd->vdev_physical_ashift;
1332 mvd->vdev_state = cvd->vdev_state;
1333 mvd->vdev_crtxg = cvd->vdev_crtxg;
1335 vdev_remove_child(pvd, cvd);
1336 vdev_add_child(pvd, mvd);
1337 cvd->vdev_id = mvd->vdev_children;
1338 vdev_add_child(mvd, cvd);
1339 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1341 if (mvd == mvd->vdev_top)
1342 vdev_top_transfer(cvd, mvd);
1344 return (mvd);
1348 * Remove a 1-way mirror/replacing vdev from the tree.
1350 void
1351 vdev_remove_parent(vdev_t *cvd)
1353 vdev_t *mvd = cvd->vdev_parent;
1354 vdev_t *pvd = mvd->vdev_parent;
1356 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1358 ASSERT(mvd->vdev_children == 1);
1359 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
1360 mvd->vdev_ops == &vdev_replacing_ops ||
1361 mvd->vdev_ops == &vdev_spare_ops);
1362 cvd->vdev_ashift = mvd->vdev_ashift;
1363 cvd->vdev_logical_ashift = mvd->vdev_logical_ashift;
1364 cvd->vdev_physical_ashift = mvd->vdev_physical_ashift;
1365 vdev_remove_child(mvd, cvd);
1366 vdev_remove_child(pvd, mvd);
1369 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1370 * Otherwise, we could have detached an offline device, and when we
1371 * go to import the pool we'll think we have two top-level vdevs,
1372 * instead of a different version of the same top-level vdev.
1374 if (mvd->vdev_top == mvd) {
1375 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
1376 cvd->vdev_orig_guid = cvd->vdev_guid;
1377 cvd->vdev_guid += guid_delta;
1378 cvd->vdev_guid_sum += guid_delta;
1381 * If pool not set for autoexpand, we need to also preserve
1382 * mvd's asize to prevent automatic expansion of cvd.
1383 * Otherwise if we are adjusting the mirror by attaching and
1384 * detaching children of non-uniform sizes, the mirror could
1385 * autoexpand, unexpectedly requiring larger devices to
1386 * re-establish the mirror.
1388 if (!cvd->vdev_spa->spa_autoexpand)
1389 cvd->vdev_asize = mvd->vdev_asize;
1391 cvd->vdev_id = mvd->vdev_id;
1392 vdev_add_child(pvd, cvd);
1393 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1395 if (cvd == cvd->vdev_top)
1396 vdev_top_transfer(mvd, cvd);
1398 ASSERT(mvd->vdev_children == 0);
1399 vdev_free(mvd);
1403 * Choose GCD for spa_gcd_alloc.
1405 static uint64_t
1406 vdev_gcd(uint64_t a, uint64_t b)
1408 while (b != 0) {
1409 uint64_t t = b;
1410 b = a % b;
1411 a = t;
1413 return (a);
1417 * Set spa_min_alloc and spa_gcd_alloc.
1419 static void
1420 vdev_spa_set_alloc(spa_t *spa, uint64_t min_alloc)
1422 if (min_alloc < spa->spa_min_alloc)
1423 spa->spa_min_alloc = min_alloc;
1424 if (spa->spa_gcd_alloc == INT_MAX) {
1425 spa->spa_gcd_alloc = min_alloc;
1426 } else {
1427 spa->spa_gcd_alloc = vdev_gcd(min_alloc,
1428 spa->spa_gcd_alloc);
1432 void
1433 vdev_metaslab_group_create(vdev_t *vd)
1435 spa_t *spa = vd->vdev_spa;
1438 * metaslab_group_create was delayed until allocation bias was available
1440 if (vd->vdev_mg == NULL) {
1441 metaslab_class_t *mc;
1443 if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE)
1444 vd->vdev_alloc_bias = VDEV_BIAS_LOG;
1446 ASSERT3U(vd->vdev_islog, ==,
1447 (vd->vdev_alloc_bias == VDEV_BIAS_LOG));
1449 switch (vd->vdev_alloc_bias) {
1450 case VDEV_BIAS_LOG:
1451 mc = spa_log_class(spa);
1452 break;
1453 case VDEV_BIAS_SPECIAL:
1454 mc = spa_special_class(spa);
1455 break;
1456 case VDEV_BIAS_DEDUP:
1457 mc = spa_dedup_class(spa);
1458 break;
1459 default:
1460 mc = spa_normal_class(spa);
1463 vd->vdev_mg = metaslab_group_create(mc, vd,
1464 spa->spa_alloc_count);
1466 if (!vd->vdev_islog) {
1467 vd->vdev_log_mg = metaslab_group_create(
1468 spa_embedded_log_class(spa), vd, 1);
1472 * The spa ashift min/max only apply for the normal metaslab
1473 * class. Class destination is late binding so ashift boundary
1474 * setting had to wait until now.
1476 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1477 mc == spa_normal_class(spa) && vd->vdev_aux == NULL) {
1478 if (vd->vdev_ashift > spa->spa_max_ashift)
1479 spa->spa_max_ashift = vd->vdev_ashift;
1480 if (vd->vdev_ashift < spa->spa_min_ashift)
1481 spa->spa_min_ashift = vd->vdev_ashift;
1483 uint64_t min_alloc = vdev_get_min_alloc(vd);
1484 vdev_spa_set_alloc(spa, min_alloc);
1490 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
1492 spa_t *spa = vd->vdev_spa;
1493 uint64_t oldc = vd->vdev_ms_count;
1494 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
1495 metaslab_t **mspp;
1496 int error;
1497 boolean_t expanding = (oldc != 0);
1499 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1502 * This vdev is not being allocated from yet or is a hole.
1504 if (vd->vdev_ms_shift == 0)
1505 return (0);
1507 ASSERT(!vd->vdev_ishole);
1509 ASSERT(oldc <= newc);
1511 mspp = vmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1513 if (expanding) {
1514 memcpy(mspp, vd->vdev_ms, oldc * sizeof (*mspp));
1515 vmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1518 vd->vdev_ms = mspp;
1519 vd->vdev_ms_count = newc;
1521 for (uint64_t m = oldc; m < newc; m++) {
1522 uint64_t object = 0;
1524 * vdev_ms_array may be 0 if we are creating the "fake"
1525 * metaslabs for an indirect vdev for zdb's leak detection.
1526 * See zdb_leak_init().
1528 if (txg == 0 && vd->vdev_ms_array != 0) {
1529 error = dmu_read(spa->spa_meta_objset,
1530 vd->vdev_ms_array,
1531 m * sizeof (uint64_t), sizeof (uint64_t), &object,
1532 DMU_READ_PREFETCH);
1533 if (error != 0) {
1534 vdev_dbgmsg(vd, "unable to read the metaslab "
1535 "array [error=%d]", error);
1536 return (error);
1540 error = metaslab_init(vd->vdev_mg, m, object, txg,
1541 &(vd->vdev_ms[m]));
1542 if (error != 0) {
1543 vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
1544 error);
1545 return (error);
1550 * Find the emptiest metaslab on the vdev and mark it for use for
1551 * embedded slog by moving it from the regular to the log metaslab
1552 * group.
1554 if (vd->vdev_mg->mg_class == spa_normal_class(spa) &&
1555 vd->vdev_ms_count > zfs_embedded_slog_min_ms &&
1556 avl_is_empty(&vd->vdev_log_mg->mg_metaslab_tree)) {
1557 uint64_t slog_msid = 0;
1558 uint64_t smallest = UINT64_MAX;
1561 * Note, we only search the new metaslabs, because the old
1562 * (pre-existing) ones may be active (e.g. have non-empty
1563 * range_tree's), and we don't move them to the new
1564 * metaslab_t.
1566 for (uint64_t m = oldc; m < newc; m++) {
1567 uint64_t alloc =
1568 space_map_allocated(vd->vdev_ms[m]->ms_sm);
1569 if (alloc < smallest) {
1570 slog_msid = m;
1571 smallest = alloc;
1574 metaslab_t *slog_ms = vd->vdev_ms[slog_msid];
1576 * The metaslab was marked as dirty at the end of
1577 * metaslab_init(). Remove it from the dirty list so that we
1578 * can uninitialize and reinitialize it to the new class.
1580 if (txg != 0) {
1581 (void) txg_list_remove_this(&vd->vdev_ms_list,
1582 slog_ms, txg);
1584 uint64_t sm_obj = space_map_object(slog_ms->ms_sm);
1585 metaslab_fini(slog_ms);
1586 VERIFY0(metaslab_init(vd->vdev_log_mg, slog_msid, sm_obj, txg,
1587 &vd->vdev_ms[slog_msid]));
1590 if (txg == 0)
1591 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1594 * If the vdev is marked as non-allocating then don't
1595 * activate the metaslabs since we want to ensure that
1596 * no allocations are performed on this device.
1598 if (vd->vdev_noalloc) {
1599 /* track non-allocating vdev space */
1600 spa->spa_nonallocating_dspace += spa_deflate(spa) ?
1601 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
1602 } else if (!expanding) {
1603 metaslab_group_activate(vd->vdev_mg);
1604 if (vd->vdev_log_mg != NULL)
1605 metaslab_group_activate(vd->vdev_log_mg);
1608 if (txg == 0)
1609 spa_config_exit(spa, SCL_ALLOC, FTAG);
1611 return (0);
1614 void
1615 vdev_metaslab_fini(vdev_t *vd)
1617 if (vd->vdev_checkpoint_sm != NULL) {
1618 ASSERT(spa_feature_is_active(vd->vdev_spa,
1619 SPA_FEATURE_POOL_CHECKPOINT));
1620 space_map_close(vd->vdev_checkpoint_sm);
1622 * Even though we close the space map, we need to set its
1623 * pointer to NULL. The reason is that vdev_metaslab_fini()
1624 * may be called multiple times for certain operations
1625 * (i.e. when destroying a pool) so we need to ensure that
1626 * this clause never executes twice. This logic is similar
1627 * to the one used for the vdev_ms clause below.
1629 vd->vdev_checkpoint_sm = NULL;
1632 if (vd->vdev_ms != NULL) {
1633 metaslab_group_t *mg = vd->vdev_mg;
1635 metaslab_group_passivate(mg);
1636 if (vd->vdev_log_mg != NULL) {
1637 ASSERT(!vd->vdev_islog);
1638 metaslab_group_passivate(vd->vdev_log_mg);
1641 uint64_t count = vd->vdev_ms_count;
1642 for (uint64_t m = 0; m < count; m++) {
1643 metaslab_t *msp = vd->vdev_ms[m];
1644 if (msp != NULL)
1645 metaslab_fini(msp);
1647 vmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1648 vd->vdev_ms = NULL;
1649 vd->vdev_ms_count = 0;
1651 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
1652 ASSERT0(mg->mg_histogram[i]);
1653 if (vd->vdev_log_mg != NULL)
1654 ASSERT0(vd->vdev_log_mg->mg_histogram[i]);
1657 ASSERT0(vd->vdev_ms_count);
1658 ASSERT3U(vd->vdev_pending_fastwrite, ==, 0);
1661 typedef struct vdev_probe_stats {
1662 boolean_t vps_readable;
1663 boolean_t vps_writeable;
1664 int vps_flags;
1665 } vdev_probe_stats_t;
1667 static void
1668 vdev_probe_done(zio_t *zio)
1670 spa_t *spa = zio->io_spa;
1671 vdev_t *vd = zio->io_vd;
1672 vdev_probe_stats_t *vps = zio->io_private;
1674 ASSERT(vd->vdev_probe_zio != NULL);
1676 if (zio->io_type == ZIO_TYPE_READ) {
1677 if (zio->io_error == 0)
1678 vps->vps_readable = 1;
1679 if (zio->io_error == 0 && spa_writeable(spa)) {
1680 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1681 zio->io_offset, zio->io_size, zio->io_abd,
1682 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1683 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1684 } else {
1685 abd_free(zio->io_abd);
1687 } else if (zio->io_type == ZIO_TYPE_WRITE) {
1688 if (zio->io_error == 0)
1689 vps->vps_writeable = 1;
1690 abd_free(zio->io_abd);
1691 } else if (zio->io_type == ZIO_TYPE_NULL) {
1692 zio_t *pio;
1693 zio_link_t *zl;
1695 vd->vdev_cant_read |= !vps->vps_readable;
1696 vd->vdev_cant_write |= !vps->vps_writeable;
1698 if (vdev_readable(vd) &&
1699 (vdev_writeable(vd) || !spa_writeable(spa))) {
1700 zio->io_error = 0;
1701 } else {
1702 ASSERT(zio->io_error != 0);
1703 vdev_dbgmsg(vd, "failed probe");
1704 (void) zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
1705 spa, vd, NULL, NULL, 0);
1706 zio->io_error = SET_ERROR(ENXIO);
1709 mutex_enter(&vd->vdev_probe_lock);
1710 ASSERT(vd->vdev_probe_zio == zio);
1711 vd->vdev_probe_zio = NULL;
1712 mutex_exit(&vd->vdev_probe_lock);
1714 zl = NULL;
1715 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1716 if (!vdev_accessible(vd, pio))
1717 pio->io_error = SET_ERROR(ENXIO);
1719 kmem_free(vps, sizeof (*vps));
1724 * Determine whether this device is accessible.
1726 * Read and write to several known locations: the pad regions of each
1727 * vdev label but the first, which we leave alone in case it contains
1728 * a VTOC.
1730 zio_t *
1731 vdev_probe(vdev_t *vd, zio_t *zio)
1733 spa_t *spa = vd->vdev_spa;
1734 vdev_probe_stats_t *vps = NULL;
1735 zio_t *pio;
1737 ASSERT(vd->vdev_ops->vdev_op_leaf);
1740 * Don't probe the probe.
1742 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1743 return (NULL);
1746 * To prevent 'probe storms' when a device fails, we create
1747 * just one probe i/o at a time. All zios that want to probe
1748 * this vdev will become parents of the probe io.
1750 mutex_enter(&vd->vdev_probe_lock);
1752 if ((pio = vd->vdev_probe_zio) == NULL) {
1753 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1755 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1756 ZIO_FLAG_DONT_AGGREGATE | ZIO_FLAG_TRYHARD;
1758 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1760 * vdev_cant_read and vdev_cant_write can only
1761 * transition from TRUE to FALSE when we have the
1762 * SCL_ZIO lock as writer; otherwise they can only
1763 * transition from FALSE to TRUE. This ensures that
1764 * any zio looking at these values can assume that
1765 * failures persist for the life of the I/O. That's
1766 * important because when a device has intermittent
1767 * connectivity problems, we want to ensure that
1768 * they're ascribed to the device (ENXIO) and not
1769 * the zio (EIO).
1771 * Since we hold SCL_ZIO as writer here, clear both
1772 * values so the probe can reevaluate from first
1773 * principles.
1775 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1776 vd->vdev_cant_read = B_FALSE;
1777 vd->vdev_cant_write = B_FALSE;
1780 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1781 vdev_probe_done, vps,
1782 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1785 * We can't change the vdev state in this context, so we
1786 * kick off an async task to do it on our behalf.
1788 if (zio != NULL) {
1789 vd->vdev_probe_wanted = B_TRUE;
1790 spa_async_request(spa, SPA_ASYNC_PROBE);
1794 if (zio != NULL)
1795 zio_add_child(zio, pio);
1797 mutex_exit(&vd->vdev_probe_lock);
1799 if (vps == NULL) {
1800 ASSERT(zio != NULL);
1801 return (NULL);
1804 for (int l = 1; l < VDEV_LABELS; l++) {
1805 zio_nowait(zio_read_phys(pio, vd,
1806 vdev_label_offset(vd->vdev_psize, l,
1807 offsetof(vdev_label_t, vl_be)), VDEV_PAD_SIZE,
1808 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1809 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1810 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1813 if (zio == NULL)
1814 return (pio);
1816 zio_nowait(pio);
1817 return (NULL);
1820 static void
1821 vdev_load_child(void *arg)
1823 vdev_t *vd = arg;
1825 vd->vdev_load_error = vdev_load(vd);
1828 static void
1829 vdev_open_child(void *arg)
1831 vdev_t *vd = arg;
1833 vd->vdev_open_thread = curthread;
1834 vd->vdev_open_error = vdev_open(vd);
1835 vd->vdev_open_thread = NULL;
1838 static boolean_t
1839 vdev_uses_zvols(vdev_t *vd)
1841 #ifdef _KERNEL
1842 if (zvol_is_zvol(vd->vdev_path))
1843 return (B_TRUE);
1844 #endif
1846 for (int c = 0; c < vd->vdev_children; c++)
1847 if (vdev_uses_zvols(vd->vdev_child[c]))
1848 return (B_TRUE);
1850 return (B_FALSE);
1854 * Returns B_TRUE if the passed child should be opened.
1856 static boolean_t
1857 vdev_default_open_children_func(vdev_t *vd)
1859 (void) vd;
1860 return (B_TRUE);
1864 * Open the requested child vdevs. If any of the leaf vdevs are using
1865 * a ZFS volume then do the opens in a single thread. This avoids a
1866 * deadlock when the current thread is holding the spa_namespace_lock.
1868 static void
1869 vdev_open_children_impl(vdev_t *vd, vdev_open_children_func_t *open_func)
1871 int children = vd->vdev_children;
1873 taskq_t *tq = taskq_create("vdev_open", children, minclsyspri,
1874 children, children, TASKQ_PREPOPULATE);
1875 vd->vdev_nonrot = B_TRUE;
1877 for (int c = 0; c < children; c++) {
1878 vdev_t *cvd = vd->vdev_child[c];
1880 if (open_func(cvd) == B_FALSE)
1881 continue;
1883 if (tq == NULL || vdev_uses_zvols(vd)) {
1884 cvd->vdev_open_error = vdev_open(cvd);
1885 } else {
1886 VERIFY(taskq_dispatch(tq, vdev_open_child,
1887 cvd, TQ_SLEEP) != TASKQID_INVALID);
1890 vd->vdev_nonrot &= cvd->vdev_nonrot;
1893 if (tq != NULL) {
1894 taskq_wait(tq);
1895 taskq_destroy(tq);
1900 * Open all child vdevs.
1902 void
1903 vdev_open_children(vdev_t *vd)
1905 vdev_open_children_impl(vd, vdev_default_open_children_func);
1909 * Conditionally open a subset of child vdevs.
1911 void
1912 vdev_open_children_subset(vdev_t *vd, vdev_open_children_func_t *open_func)
1914 vdev_open_children_impl(vd, open_func);
1918 * Compute the raidz-deflation ratio. Note, we hard-code
1919 * in 128k (1 << 17) because it is the "typical" blocksize.
1920 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1921 * otherwise it would inconsistently account for existing bp's.
1923 static void
1924 vdev_set_deflate_ratio(vdev_t *vd)
1926 if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
1927 vd->vdev_deflate_ratio = (1 << 17) /
1928 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
1933 * Choose the best of two ashifts, preferring one between logical ashift
1934 * (absolute minimum) and administrator defined maximum, otherwise take
1935 * the biggest of the two.
1937 uint64_t
1938 vdev_best_ashift(uint64_t logical, uint64_t a, uint64_t b)
1940 if (a > logical && a <= zfs_vdev_max_auto_ashift) {
1941 if (b <= logical || b > zfs_vdev_max_auto_ashift)
1942 return (a);
1943 else
1944 return (MAX(a, b));
1945 } else if (b <= logical || b > zfs_vdev_max_auto_ashift)
1946 return (MAX(a, b));
1947 return (b);
1951 * Maximize performance by inflating the configured ashift for top level
1952 * vdevs to be as close to the physical ashift as possible while maintaining
1953 * administrator defined limits and ensuring it doesn't go below the
1954 * logical ashift.
1956 static void
1957 vdev_ashift_optimize(vdev_t *vd)
1959 ASSERT(vd == vd->vdev_top);
1961 if (vd->vdev_ashift < vd->vdev_physical_ashift &&
1962 vd->vdev_physical_ashift <= zfs_vdev_max_auto_ashift) {
1963 vd->vdev_ashift = MIN(
1964 MAX(zfs_vdev_max_auto_ashift, vd->vdev_ashift),
1965 MAX(zfs_vdev_min_auto_ashift,
1966 vd->vdev_physical_ashift));
1967 } else {
1969 * If the logical and physical ashifts are the same, then
1970 * we ensure that the top-level vdev's ashift is not smaller
1971 * than our minimum ashift value. For the unusual case
1972 * where logical ashift > physical ashift, we can't cap
1973 * the calculated ashift based on max ashift as that
1974 * would cause failures.
1975 * We still check if we need to increase it to match
1976 * the min ashift.
1978 vd->vdev_ashift = MAX(zfs_vdev_min_auto_ashift,
1979 vd->vdev_ashift);
1984 * Prepare a virtual device for access.
1987 vdev_open(vdev_t *vd)
1989 spa_t *spa = vd->vdev_spa;
1990 int error;
1991 uint64_t osize = 0;
1992 uint64_t max_osize = 0;
1993 uint64_t asize, max_asize, psize;
1994 uint64_t logical_ashift = 0;
1995 uint64_t physical_ashift = 0;
1997 ASSERT(vd->vdev_open_thread == curthread ||
1998 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1999 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
2000 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
2001 vd->vdev_state == VDEV_STATE_OFFLINE);
2003 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2004 vd->vdev_cant_read = B_FALSE;
2005 vd->vdev_cant_write = B_FALSE;
2006 vd->vdev_min_asize = vdev_get_min_asize(vd);
2009 * If this vdev is not removed, check its fault status. If it's
2010 * faulted, bail out of the open.
2012 if (!vd->vdev_removed && vd->vdev_faulted) {
2013 ASSERT(vd->vdev_children == 0);
2014 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
2015 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
2016 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
2017 vd->vdev_label_aux);
2018 return (SET_ERROR(ENXIO));
2019 } else if (vd->vdev_offline) {
2020 ASSERT(vd->vdev_children == 0);
2021 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
2022 return (SET_ERROR(ENXIO));
2025 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize,
2026 &logical_ashift, &physical_ashift);
2028 /* Keep the device in removed state if unplugged */
2029 if (error == ENOENT && vd->vdev_removed) {
2030 vdev_set_state(vd, B_TRUE, VDEV_STATE_REMOVED,
2031 VDEV_AUX_NONE);
2032 return (error);
2036 * Physical volume size should never be larger than its max size, unless
2037 * the disk has shrunk while we were reading it or the device is buggy
2038 * or damaged: either way it's not safe for use, bail out of the open.
2040 if (osize > max_osize) {
2041 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2042 VDEV_AUX_OPEN_FAILED);
2043 return (SET_ERROR(ENXIO));
2047 * Reset the vdev_reopening flag so that we actually close
2048 * the vdev on error.
2050 vd->vdev_reopening = B_FALSE;
2051 if (zio_injection_enabled && error == 0)
2052 error = zio_handle_device_injection(vd, NULL, SET_ERROR(ENXIO));
2054 if (error) {
2055 if (vd->vdev_removed &&
2056 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
2057 vd->vdev_removed = B_FALSE;
2059 if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
2060 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
2061 vd->vdev_stat.vs_aux);
2062 } else {
2063 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2064 vd->vdev_stat.vs_aux);
2066 return (error);
2069 vd->vdev_removed = B_FALSE;
2072 * Recheck the faulted flag now that we have confirmed that
2073 * the vdev is accessible. If we're faulted, bail.
2075 if (vd->vdev_faulted) {
2076 ASSERT(vd->vdev_children == 0);
2077 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
2078 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
2079 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
2080 vd->vdev_label_aux);
2081 return (SET_ERROR(ENXIO));
2084 if (vd->vdev_degraded) {
2085 ASSERT(vd->vdev_children == 0);
2086 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
2087 VDEV_AUX_ERR_EXCEEDED);
2088 } else {
2089 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
2093 * For hole or missing vdevs we just return success.
2095 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
2096 return (0);
2098 for (int c = 0; c < vd->vdev_children; c++) {
2099 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
2100 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
2101 VDEV_AUX_NONE);
2102 break;
2106 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
2107 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
2109 if (vd->vdev_children == 0) {
2110 if (osize < SPA_MINDEVSIZE) {
2111 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2112 VDEV_AUX_TOO_SMALL);
2113 return (SET_ERROR(EOVERFLOW));
2115 psize = osize;
2116 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
2117 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
2118 VDEV_LABEL_END_SIZE);
2119 } else {
2120 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
2121 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
2122 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2123 VDEV_AUX_TOO_SMALL);
2124 return (SET_ERROR(EOVERFLOW));
2126 psize = 0;
2127 asize = osize;
2128 max_asize = max_osize;
2132 * If the vdev was expanded, record this so that we can re-create the
2133 * uberblock rings in labels {2,3}, during the next sync.
2135 if ((psize > vd->vdev_psize) && (vd->vdev_psize != 0))
2136 vd->vdev_copy_uberblocks = B_TRUE;
2138 vd->vdev_psize = psize;
2141 * Make sure the allocatable size hasn't shrunk too much.
2143 if (asize < vd->vdev_min_asize) {
2144 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2145 VDEV_AUX_BAD_LABEL);
2146 return (SET_ERROR(EINVAL));
2150 * We can always set the logical/physical ashift members since
2151 * their values are only used to calculate the vdev_ashift when
2152 * the device is first added to the config. These values should
2153 * not be used for anything else since they may change whenever
2154 * the device is reopened and we don't store them in the label.
2156 vd->vdev_physical_ashift =
2157 MAX(physical_ashift, vd->vdev_physical_ashift);
2158 vd->vdev_logical_ashift = MAX(logical_ashift,
2159 vd->vdev_logical_ashift);
2161 if (vd->vdev_asize == 0) {
2163 * This is the first-ever open, so use the computed values.
2164 * For compatibility, a different ashift can be requested.
2166 vd->vdev_asize = asize;
2167 vd->vdev_max_asize = max_asize;
2170 * If the vdev_ashift was not overridden at creation time,
2171 * then set it the logical ashift and optimize the ashift.
2173 if (vd->vdev_ashift == 0) {
2174 vd->vdev_ashift = vd->vdev_logical_ashift;
2176 if (vd->vdev_logical_ashift > ASHIFT_MAX) {
2177 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2178 VDEV_AUX_ASHIFT_TOO_BIG);
2179 return (SET_ERROR(EDOM));
2182 if (vd->vdev_top == vd && vd->vdev_attaching == B_FALSE)
2183 vdev_ashift_optimize(vd);
2184 vd->vdev_attaching = B_FALSE;
2186 if (vd->vdev_ashift != 0 && (vd->vdev_ashift < ASHIFT_MIN ||
2187 vd->vdev_ashift > ASHIFT_MAX)) {
2188 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2189 VDEV_AUX_BAD_ASHIFT);
2190 return (SET_ERROR(EDOM));
2192 } else {
2194 * Make sure the alignment required hasn't increased.
2196 if (vd->vdev_ashift > vd->vdev_top->vdev_ashift &&
2197 vd->vdev_ops->vdev_op_leaf) {
2198 (void) zfs_ereport_post(
2199 FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT,
2200 spa, vd, NULL, NULL, 0);
2201 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2202 VDEV_AUX_BAD_LABEL);
2203 return (SET_ERROR(EDOM));
2205 vd->vdev_max_asize = max_asize;
2209 * If all children are healthy we update asize if either:
2210 * The asize has increased, due to a device expansion caused by dynamic
2211 * LUN growth or vdev replacement, and automatic expansion is enabled;
2212 * making the additional space available.
2214 * The asize has decreased, due to a device shrink usually caused by a
2215 * vdev replace with a smaller device. This ensures that calculations
2216 * based of max_asize and asize e.g. esize are always valid. It's safe
2217 * to do this as we've already validated that asize is greater than
2218 * vdev_min_asize.
2220 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
2221 ((asize > vd->vdev_asize &&
2222 (vd->vdev_expanding || spa->spa_autoexpand)) ||
2223 (asize < vd->vdev_asize)))
2224 vd->vdev_asize = asize;
2226 vdev_set_min_asize(vd);
2229 * Ensure we can issue some IO before declaring the
2230 * vdev open for business.
2232 if (vd->vdev_ops->vdev_op_leaf &&
2233 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
2234 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
2235 VDEV_AUX_ERR_EXCEEDED);
2236 return (error);
2240 * Track the minimum allocation size.
2242 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
2243 vd->vdev_islog == 0 && vd->vdev_aux == NULL) {
2244 uint64_t min_alloc = vdev_get_min_alloc(vd);
2245 vdev_spa_set_alloc(spa, min_alloc);
2249 * If this is a leaf vdev, assess whether a resilver is needed.
2250 * But don't do this if we are doing a reopen for a scrub, since
2251 * this would just restart the scrub we are already doing.
2253 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen)
2254 dsl_scan_assess_vdev(spa->spa_dsl_pool, vd);
2256 return (0);
2259 static void
2260 vdev_validate_child(void *arg)
2262 vdev_t *vd = arg;
2264 vd->vdev_validate_thread = curthread;
2265 vd->vdev_validate_error = vdev_validate(vd);
2266 vd->vdev_validate_thread = NULL;
2270 * Called once the vdevs are all opened, this routine validates the label
2271 * contents. This needs to be done before vdev_load() so that we don't
2272 * inadvertently do repair I/Os to the wrong device.
2274 * This function will only return failure if one of the vdevs indicates that it
2275 * has since been destroyed or exported. This is only possible if
2276 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
2277 * will be updated but the function will return 0.
2280 vdev_validate(vdev_t *vd)
2282 spa_t *spa = vd->vdev_spa;
2283 taskq_t *tq = NULL;
2284 nvlist_t *label;
2285 uint64_t guid = 0, aux_guid = 0, top_guid;
2286 uint64_t state;
2287 nvlist_t *nvl;
2288 uint64_t txg;
2289 int children = vd->vdev_children;
2291 if (vdev_validate_skip)
2292 return (0);
2294 if (children > 0) {
2295 tq = taskq_create("vdev_validate", children, minclsyspri,
2296 children, children, TASKQ_PREPOPULATE);
2299 for (uint64_t c = 0; c < children; c++) {
2300 vdev_t *cvd = vd->vdev_child[c];
2302 if (tq == NULL || vdev_uses_zvols(cvd)) {
2303 vdev_validate_child(cvd);
2304 } else {
2305 VERIFY(taskq_dispatch(tq, vdev_validate_child, cvd,
2306 TQ_SLEEP) != TASKQID_INVALID);
2309 if (tq != NULL) {
2310 taskq_wait(tq);
2311 taskq_destroy(tq);
2313 for (int c = 0; c < children; c++) {
2314 int error = vd->vdev_child[c]->vdev_validate_error;
2316 if (error != 0)
2317 return (SET_ERROR(EBADF));
2322 * If the device has already failed, or was marked offline, don't do
2323 * any further validation. Otherwise, label I/O will fail and we will
2324 * overwrite the previous state.
2326 if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
2327 return (0);
2330 * If we are performing an extreme rewind, we allow for a label that
2331 * was modified at a point after the current txg.
2332 * If config lock is not held do not check for the txg. spa_sync could
2333 * be updating the vdev's label before updating spa_last_synced_txg.
2335 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 ||
2336 spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG)
2337 txg = UINT64_MAX;
2338 else
2339 txg = spa_last_synced_txg(spa);
2341 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
2342 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2343 VDEV_AUX_BAD_LABEL);
2344 vdev_dbgmsg(vd, "vdev_validate: failed reading config for "
2345 "txg %llu", (u_longlong_t)txg);
2346 return (0);
2350 * Determine if this vdev has been split off into another
2351 * pool. If so, then refuse to open it.
2353 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
2354 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
2355 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2356 VDEV_AUX_SPLIT_POOL);
2357 nvlist_free(label);
2358 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
2359 return (0);
2362 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
2363 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2364 VDEV_AUX_CORRUPT_DATA);
2365 nvlist_free(label);
2366 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2367 ZPOOL_CONFIG_POOL_GUID);
2368 return (0);
2372 * If config is not trusted then ignore the spa guid check. This is
2373 * necessary because if the machine crashed during a re-guid the new
2374 * guid might have been written to all of the vdev labels, but not the
2375 * cached config. The check will be performed again once we have the
2376 * trusted config from the MOS.
2378 if (spa->spa_trust_config && guid != spa_guid(spa)) {
2379 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2380 VDEV_AUX_CORRUPT_DATA);
2381 nvlist_free(label);
2382 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
2383 "match config (%llu != %llu)", (u_longlong_t)guid,
2384 (u_longlong_t)spa_guid(spa));
2385 return (0);
2388 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
2389 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
2390 &aux_guid) != 0)
2391 aux_guid = 0;
2393 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
2394 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2395 VDEV_AUX_CORRUPT_DATA);
2396 nvlist_free(label);
2397 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2398 ZPOOL_CONFIG_GUID);
2399 return (0);
2402 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
2403 != 0) {
2404 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2405 VDEV_AUX_CORRUPT_DATA);
2406 nvlist_free(label);
2407 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2408 ZPOOL_CONFIG_TOP_GUID);
2409 return (0);
2413 * If this vdev just became a top-level vdev because its sibling was
2414 * detached, it will have adopted the parent's vdev guid -- but the
2415 * label may or may not be on disk yet. Fortunately, either version
2416 * of the label will have the same top guid, so if we're a top-level
2417 * vdev, we can safely compare to that instead.
2418 * However, if the config comes from a cachefile that failed to update
2419 * after the detach, a top-level vdev will appear as a non top-level
2420 * vdev in the config. Also relax the constraints if we perform an
2421 * extreme rewind.
2423 * If we split this vdev off instead, then we also check the
2424 * original pool's guid. We don't want to consider the vdev
2425 * corrupt if it is partway through a split operation.
2427 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
2428 boolean_t mismatch = B_FALSE;
2429 if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
2430 if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
2431 mismatch = B_TRUE;
2432 } else {
2433 if (vd->vdev_guid != top_guid &&
2434 vd->vdev_top->vdev_guid != guid)
2435 mismatch = B_TRUE;
2438 if (mismatch) {
2439 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2440 VDEV_AUX_CORRUPT_DATA);
2441 nvlist_free(label);
2442 vdev_dbgmsg(vd, "vdev_validate: config guid "
2443 "doesn't match label guid");
2444 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
2445 (u_longlong_t)vd->vdev_guid,
2446 (u_longlong_t)vd->vdev_top->vdev_guid);
2447 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
2448 "aux_guid %llu", (u_longlong_t)guid,
2449 (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
2450 return (0);
2454 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
2455 &state) != 0) {
2456 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2457 VDEV_AUX_CORRUPT_DATA);
2458 nvlist_free(label);
2459 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2460 ZPOOL_CONFIG_POOL_STATE);
2461 return (0);
2464 nvlist_free(label);
2467 * If this is a verbatim import, no need to check the
2468 * state of the pool.
2470 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
2471 spa_load_state(spa) == SPA_LOAD_OPEN &&
2472 state != POOL_STATE_ACTIVE) {
2473 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
2474 "for spa %s", (u_longlong_t)state, spa->spa_name);
2475 return (SET_ERROR(EBADF));
2479 * If we were able to open and validate a vdev that was
2480 * previously marked permanently unavailable, clear that state
2481 * now.
2483 if (vd->vdev_not_present)
2484 vd->vdev_not_present = 0;
2486 return (0);
2489 static void
2490 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
2492 char *old, *new;
2493 if (svd->vdev_path != NULL && dvd->vdev_path != NULL) {
2494 if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) {
2495 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
2496 "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
2497 dvd->vdev_path, svd->vdev_path);
2498 spa_strfree(dvd->vdev_path);
2499 dvd->vdev_path = spa_strdup(svd->vdev_path);
2501 } else if (svd->vdev_path != NULL) {
2502 dvd->vdev_path = spa_strdup(svd->vdev_path);
2503 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
2504 (u_longlong_t)dvd->vdev_guid, dvd->vdev_path);
2508 * Our enclosure sysfs path may have changed between imports
2510 old = dvd->vdev_enc_sysfs_path;
2511 new = svd->vdev_enc_sysfs_path;
2512 if ((old != NULL && new == NULL) ||
2513 (old == NULL && new != NULL) ||
2514 ((old != NULL && new != NULL) && strcmp(new, old) != 0)) {
2515 zfs_dbgmsg("vdev_copy_path: vdev %llu: vdev_enc_sysfs_path "
2516 "changed from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
2517 old, new);
2519 if (dvd->vdev_enc_sysfs_path)
2520 spa_strfree(dvd->vdev_enc_sysfs_path);
2522 if (svd->vdev_enc_sysfs_path) {
2523 dvd->vdev_enc_sysfs_path = spa_strdup(
2524 svd->vdev_enc_sysfs_path);
2525 } else {
2526 dvd->vdev_enc_sysfs_path = NULL;
2532 * Recursively copy vdev paths from one vdev to another. Source and destination
2533 * vdev trees must have same geometry otherwise return error. Intended to copy
2534 * paths from userland config into MOS config.
2537 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
2539 if ((svd->vdev_ops == &vdev_missing_ops) ||
2540 (svd->vdev_ishole && dvd->vdev_ishole) ||
2541 (dvd->vdev_ops == &vdev_indirect_ops))
2542 return (0);
2544 if (svd->vdev_ops != dvd->vdev_ops) {
2545 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
2546 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
2547 return (SET_ERROR(EINVAL));
2550 if (svd->vdev_guid != dvd->vdev_guid) {
2551 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
2552 "%llu)", (u_longlong_t)svd->vdev_guid,
2553 (u_longlong_t)dvd->vdev_guid);
2554 return (SET_ERROR(EINVAL));
2557 if (svd->vdev_children != dvd->vdev_children) {
2558 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
2559 "%llu != %llu", (u_longlong_t)svd->vdev_children,
2560 (u_longlong_t)dvd->vdev_children);
2561 return (SET_ERROR(EINVAL));
2564 for (uint64_t i = 0; i < svd->vdev_children; i++) {
2565 int error = vdev_copy_path_strict(svd->vdev_child[i],
2566 dvd->vdev_child[i]);
2567 if (error != 0)
2568 return (error);
2571 if (svd->vdev_ops->vdev_op_leaf)
2572 vdev_copy_path_impl(svd, dvd);
2574 return (0);
2577 static void
2578 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
2580 ASSERT(stvd->vdev_top == stvd);
2581 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
2583 for (uint64_t i = 0; i < dvd->vdev_children; i++) {
2584 vdev_copy_path_search(stvd, dvd->vdev_child[i]);
2587 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
2588 return;
2591 * The idea here is that while a vdev can shift positions within
2592 * a top vdev (when replacing, attaching mirror, etc.) it cannot
2593 * step outside of it.
2595 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
2597 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
2598 return;
2600 ASSERT(vd->vdev_ops->vdev_op_leaf);
2602 vdev_copy_path_impl(vd, dvd);
2606 * Recursively copy vdev paths from one root vdev to another. Source and
2607 * destination vdev trees may differ in geometry. For each destination leaf
2608 * vdev, search a vdev with the same guid and top vdev id in the source.
2609 * Intended to copy paths from userland config into MOS config.
2611 void
2612 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
2614 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
2615 ASSERT(srvd->vdev_ops == &vdev_root_ops);
2616 ASSERT(drvd->vdev_ops == &vdev_root_ops);
2618 for (uint64_t i = 0; i < children; i++) {
2619 vdev_copy_path_search(srvd->vdev_child[i],
2620 drvd->vdev_child[i]);
2625 * Close a virtual device.
2627 void
2628 vdev_close(vdev_t *vd)
2630 vdev_t *pvd = vd->vdev_parent;
2631 spa_t *spa __maybe_unused = vd->vdev_spa;
2633 ASSERT(vd != NULL);
2634 ASSERT(vd->vdev_open_thread == curthread ||
2635 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2638 * If our parent is reopening, then we are as well, unless we are
2639 * going offline.
2641 if (pvd != NULL && pvd->vdev_reopening)
2642 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
2644 vd->vdev_ops->vdev_op_close(vd);
2647 * We record the previous state before we close it, so that if we are
2648 * doing a reopen(), we don't generate FMA ereports if we notice that
2649 * it's still faulted.
2651 vd->vdev_prevstate = vd->vdev_state;
2653 if (vd->vdev_offline)
2654 vd->vdev_state = VDEV_STATE_OFFLINE;
2655 else
2656 vd->vdev_state = VDEV_STATE_CLOSED;
2657 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2660 void
2661 vdev_hold(vdev_t *vd)
2663 spa_t *spa = vd->vdev_spa;
2665 ASSERT(spa_is_root(spa));
2666 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
2667 return;
2669 for (int c = 0; c < vd->vdev_children; c++)
2670 vdev_hold(vd->vdev_child[c]);
2672 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_hold != NULL)
2673 vd->vdev_ops->vdev_op_hold(vd);
2676 void
2677 vdev_rele(vdev_t *vd)
2679 ASSERT(spa_is_root(vd->vdev_spa));
2680 for (int c = 0; c < vd->vdev_children; c++)
2681 vdev_rele(vd->vdev_child[c]);
2683 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_rele != NULL)
2684 vd->vdev_ops->vdev_op_rele(vd);
2688 * Reopen all interior vdevs and any unopened leaves. We don't actually
2689 * reopen leaf vdevs which had previously been opened as they might deadlock
2690 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2691 * If the leaf has never been opened then open it, as usual.
2693 void
2694 vdev_reopen(vdev_t *vd)
2696 spa_t *spa = vd->vdev_spa;
2698 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2700 /* set the reopening flag unless we're taking the vdev offline */
2701 vd->vdev_reopening = !vd->vdev_offline;
2702 vdev_close(vd);
2703 (void) vdev_open(vd);
2706 * Call vdev_validate() here to make sure we have the same device.
2707 * Otherwise, a device with an invalid label could be successfully
2708 * opened in response to vdev_reopen().
2710 if (vd->vdev_aux) {
2711 (void) vdev_validate_aux(vd);
2712 if (vdev_readable(vd) && vdev_writeable(vd) &&
2713 vd->vdev_aux == &spa->spa_l2cache) {
2715 * In case the vdev is present we should evict all ARC
2716 * buffers and pointers to log blocks and reclaim their
2717 * space before restoring its contents to L2ARC.
2719 if (l2arc_vdev_present(vd)) {
2720 l2arc_rebuild_vdev(vd, B_TRUE);
2721 } else {
2722 l2arc_add_vdev(spa, vd);
2724 spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD);
2725 spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM);
2727 } else {
2728 (void) vdev_validate(vd);
2732 * Recheck if resilver is still needed and cancel any
2733 * scheduled resilver if resilver is unneeded.
2735 if (!vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL) &&
2736 spa->spa_async_tasks & SPA_ASYNC_RESILVER) {
2737 mutex_enter(&spa->spa_async_lock);
2738 spa->spa_async_tasks &= ~SPA_ASYNC_RESILVER;
2739 mutex_exit(&spa->spa_async_lock);
2743 * Reassess parent vdev's health.
2745 vdev_propagate_state(vd);
2749 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
2751 int error;
2754 * Normally, partial opens (e.g. of a mirror) are allowed.
2755 * For a create, however, we want to fail the request if
2756 * there are any components we can't open.
2758 error = vdev_open(vd);
2760 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
2761 vdev_close(vd);
2762 return (error ? error : SET_ERROR(ENXIO));
2766 * Recursively load DTLs and initialize all labels.
2768 if ((error = vdev_dtl_load(vd)) != 0 ||
2769 (error = vdev_label_init(vd, txg, isreplacing ?
2770 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
2771 vdev_close(vd);
2772 return (error);
2775 return (0);
2778 void
2779 vdev_metaslab_set_size(vdev_t *vd)
2781 uint64_t asize = vd->vdev_asize;
2782 uint64_t ms_count = asize >> zfs_vdev_default_ms_shift;
2783 uint64_t ms_shift;
2786 * There are two dimensions to the metaslab sizing calculation:
2787 * the size of the metaslab and the count of metaslabs per vdev.
2789 * The default values used below are a good balance between memory
2790 * usage (larger metaslab size means more memory needed for loaded
2791 * metaslabs; more metaslabs means more memory needed for the
2792 * metaslab_t structs), metaslab load time (larger metaslabs take
2793 * longer to load), and metaslab sync time (more metaslabs means
2794 * more time spent syncing all of them).
2796 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2797 * The range of the dimensions are as follows:
2799 * 2^29 <= ms_size <= 2^34
2800 * 16 <= ms_count <= 131,072
2802 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2803 * at least 512MB (2^29) to minimize fragmentation effects when
2804 * testing with smaller devices. However, the count constraint
2805 * of at least 16 metaslabs will override this minimum size goal.
2807 * On the upper end of vdev sizes, we aim for a maximum metaslab
2808 * size of 16GB. However, we will cap the total count to 2^17
2809 * metaslabs to keep our memory footprint in check and let the
2810 * metaslab size grow from there if that limit is hit.
2812 * The net effect of applying above constrains is summarized below.
2814 * vdev size metaslab count
2815 * --------------|-----------------
2816 * < 8GB ~16
2817 * 8GB - 100GB one per 512MB
2818 * 100GB - 3TB ~200
2819 * 3TB - 2PB one per 16GB
2820 * > 2PB ~131,072
2821 * --------------------------------
2823 * Finally, note that all of the above calculate the initial
2824 * number of metaslabs. Expanding a top-level vdev will result
2825 * in additional metaslabs being allocated making it possible
2826 * to exceed the zfs_vdev_ms_count_limit.
2829 if (ms_count < zfs_vdev_min_ms_count)
2830 ms_shift = highbit64(asize / zfs_vdev_min_ms_count);
2831 else if (ms_count > zfs_vdev_default_ms_count)
2832 ms_shift = highbit64(asize / zfs_vdev_default_ms_count);
2833 else
2834 ms_shift = zfs_vdev_default_ms_shift;
2836 if (ms_shift < SPA_MAXBLOCKSHIFT) {
2837 ms_shift = SPA_MAXBLOCKSHIFT;
2838 } else if (ms_shift > zfs_vdev_max_ms_shift) {
2839 ms_shift = zfs_vdev_max_ms_shift;
2840 /* cap the total count to constrain memory footprint */
2841 if ((asize >> ms_shift) > zfs_vdev_ms_count_limit)
2842 ms_shift = highbit64(asize / zfs_vdev_ms_count_limit);
2845 vd->vdev_ms_shift = ms_shift;
2846 ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT);
2849 void
2850 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2852 ASSERT(vd == vd->vdev_top);
2853 /* indirect vdevs don't have metaslabs or dtls */
2854 ASSERT(vdev_is_concrete(vd) || flags == 0);
2855 ASSERT(ISP2(flags));
2856 ASSERT(spa_writeable(vd->vdev_spa));
2858 if (flags & VDD_METASLAB)
2859 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2861 if (flags & VDD_DTL)
2862 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2864 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2867 void
2868 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
2870 for (int c = 0; c < vd->vdev_children; c++)
2871 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
2873 if (vd->vdev_ops->vdev_op_leaf)
2874 vdev_dirty(vd->vdev_top, flags, vd, txg);
2878 * DTLs.
2880 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2881 * the vdev has less than perfect replication. There are four kinds of DTL:
2883 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2885 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2887 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2888 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2889 * txgs that was scrubbed.
2891 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2892 * persistent errors or just some device being offline.
2893 * Unlike the other three, the DTL_OUTAGE map is not generally
2894 * maintained; it's only computed when needed, typically to
2895 * determine whether a device can be detached.
2897 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2898 * either has the data or it doesn't.
2900 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2901 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2902 * if any child is less than fully replicated, then so is its parent.
2903 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2904 * comprising only those txgs which appear in 'maxfaults' or more children;
2905 * those are the txgs we don't have enough replication to read. For example,
2906 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2907 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2908 * two child DTL_MISSING maps.
2910 * It should be clear from the above that to compute the DTLs and outage maps
2911 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2912 * Therefore, that is all we keep on disk. When loading the pool, or after
2913 * a configuration change, we generate all other DTLs from first principles.
2915 void
2916 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2918 range_tree_t *rt = vd->vdev_dtl[t];
2920 ASSERT(t < DTL_TYPES);
2921 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2922 ASSERT(spa_writeable(vd->vdev_spa));
2924 mutex_enter(&vd->vdev_dtl_lock);
2925 if (!range_tree_contains(rt, txg, size))
2926 range_tree_add(rt, txg, size);
2927 mutex_exit(&vd->vdev_dtl_lock);
2930 boolean_t
2931 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2933 range_tree_t *rt = vd->vdev_dtl[t];
2934 boolean_t dirty = B_FALSE;
2936 ASSERT(t < DTL_TYPES);
2937 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2940 * While we are loading the pool, the DTLs have not been loaded yet.
2941 * This isn't a problem but it can result in devices being tried
2942 * which are known to not have the data. In which case, the import
2943 * is relying on the checksum to ensure that we get the right data.
2944 * Note that while importing we are only reading the MOS, which is
2945 * always checksummed.
2947 mutex_enter(&vd->vdev_dtl_lock);
2948 if (!range_tree_is_empty(rt))
2949 dirty = range_tree_contains(rt, txg, size);
2950 mutex_exit(&vd->vdev_dtl_lock);
2952 return (dirty);
2955 boolean_t
2956 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
2958 range_tree_t *rt = vd->vdev_dtl[t];
2959 boolean_t empty;
2961 mutex_enter(&vd->vdev_dtl_lock);
2962 empty = range_tree_is_empty(rt);
2963 mutex_exit(&vd->vdev_dtl_lock);
2965 return (empty);
2969 * Check if the txg falls within the range which must be
2970 * resilvered. DVAs outside this range can always be skipped.
2972 boolean_t
2973 vdev_default_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
2974 uint64_t phys_birth)
2976 (void) dva, (void) psize;
2978 /* Set by sequential resilver. */
2979 if (phys_birth == TXG_UNKNOWN)
2980 return (B_TRUE);
2982 return (vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1));
2986 * Returns B_TRUE if the vdev determines the DVA needs to be resilvered.
2988 boolean_t
2989 vdev_dtl_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
2990 uint64_t phys_birth)
2992 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2994 if (vd->vdev_ops->vdev_op_need_resilver == NULL ||
2995 vd->vdev_ops->vdev_op_leaf)
2996 return (B_TRUE);
2998 return (vd->vdev_ops->vdev_op_need_resilver(vd, dva, psize,
2999 phys_birth));
3003 * Returns the lowest txg in the DTL range.
3005 static uint64_t
3006 vdev_dtl_min(vdev_t *vd)
3008 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
3009 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
3010 ASSERT0(vd->vdev_children);
3012 return (range_tree_min(vd->vdev_dtl[DTL_MISSING]) - 1);
3016 * Returns the highest txg in the DTL.
3018 static uint64_t
3019 vdev_dtl_max(vdev_t *vd)
3021 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
3022 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
3023 ASSERT0(vd->vdev_children);
3025 return (range_tree_max(vd->vdev_dtl[DTL_MISSING]));
3029 * Determine if a resilvering vdev should remove any DTL entries from
3030 * its range. If the vdev was resilvering for the entire duration of the
3031 * scan then it should excise that range from its DTLs. Otherwise, this
3032 * vdev is considered partially resilvered and should leave its DTL
3033 * entries intact. The comment in vdev_dtl_reassess() describes how we
3034 * excise the DTLs.
3036 static boolean_t
3037 vdev_dtl_should_excise(vdev_t *vd, boolean_t rebuild_done)
3039 ASSERT0(vd->vdev_children);
3041 if (vd->vdev_state < VDEV_STATE_DEGRADED)
3042 return (B_FALSE);
3044 if (vd->vdev_resilver_deferred)
3045 return (B_FALSE);
3047 if (range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]))
3048 return (B_TRUE);
3050 if (rebuild_done) {
3051 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config;
3052 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
3054 /* Rebuild not initiated by attach */
3055 if (vd->vdev_rebuild_txg == 0)
3056 return (B_TRUE);
3059 * When a rebuild completes without error then all missing data
3060 * up to the rebuild max txg has been reconstructed and the DTL
3061 * is eligible for excision.
3063 if (vrp->vrp_rebuild_state == VDEV_REBUILD_COMPLETE &&
3064 vdev_dtl_max(vd) <= vrp->vrp_max_txg) {
3065 ASSERT3U(vrp->vrp_min_txg, <=, vdev_dtl_min(vd));
3066 ASSERT3U(vrp->vrp_min_txg, <, vd->vdev_rebuild_txg);
3067 ASSERT3U(vd->vdev_rebuild_txg, <=, vrp->vrp_max_txg);
3068 return (B_TRUE);
3070 } else {
3071 dsl_scan_t *scn = vd->vdev_spa->spa_dsl_pool->dp_scan;
3072 dsl_scan_phys_t *scnp __maybe_unused = &scn->scn_phys;
3074 /* Resilver not initiated by attach */
3075 if (vd->vdev_resilver_txg == 0)
3076 return (B_TRUE);
3079 * When a resilver is initiated the scan will assign the
3080 * scn_max_txg value to the highest txg value that exists
3081 * in all DTLs. If this device's max DTL is not part of this
3082 * scan (i.e. it is not in the range (scn_min_txg, scn_max_txg]
3083 * then it is not eligible for excision.
3085 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
3086 ASSERT3U(scnp->scn_min_txg, <=, vdev_dtl_min(vd));
3087 ASSERT3U(scnp->scn_min_txg, <, vd->vdev_resilver_txg);
3088 ASSERT3U(vd->vdev_resilver_txg, <=, scnp->scn_max_txg);
3089 return (B_TRUE);
3093 return (B_FALSE);
3097 * Reassess DTLs after a config change or scrub completion. If txg == 0 no
3098 * write operations will be issued to the pool.
3100 void
3101 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg,
3102 boolean_t scrub_done, boolean_t rebuild_done)
3104 spa_t *spa = vd->vdev_spa;
3105 avl_tree_t reftree;
3106 int minref;
3108 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
3110 for (int c = 0; c < vd->vdev_children; c++)
3111 vdev_dtl_reassess(vd->vdev_child[c], txg,
3112 scrub_txg, scrub_done, rebuild_done);
3114 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
3115 return;
3117 if (vd->vdev_ops->vdev_op_leaf) {
3118 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
3119 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config;
3120 boolean_t check_excise = B_FALSE;
3121 boolean_t wasempty = B_TRUE;
3123 mutex_enter(&vd->vdev_dtl_lock);
3126 * If requested, pretend the scan or rebuild completed cleanly.
3128 if (zfs_scan_ignore_errors) {
3129 if (scn != NULL)
3130 scn->scn_phys.scn_errors = 0;
3131 if (vr != NULL)
3132 vr->vr_rebuild_phys.vrp_errors = 0;
3135 if (scrub_txg != 0 &&
3136 !range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
3137 wasempty = B_FALSE;
3138 zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d "
3139 "dtl:%llu/%llu errors:%llu",
3140 (u_longlong_t)vd->vdev_guid, (u_longlong_t)txg,
3141 (u_longlong_t)scrub_txg, spa->spa_scrub_started,
3142 (u_longlong_t)vdev_dtl_min(vd),
3143 (u_longlong_t)vdev_dtl_max(vd),
3144 (u_longlong_t)(scn ? scn->scn_phys.scn_errors : 0));
3148 * If we've completed a scrub/resilver or a rebuild cleanly
3149 * then determine if this vdev should remove any DTLs. We
3150 * only want to excise regions on vdevs that were available
3151 * during the entire duration of this scan.
3153 if (rebuild_done &&
3154 vr != NULL && vr->vr_rebuild_phys.vrp_errors == 0) {
3155 check_excise = B_TRUE;
3156 } else {
3157 if (spa->spa_scrub_started ||
3158 (scn != NULL && scn->scn_phys.scn_errors == 0)) {
3159 check_excise = B_TRUE;
3163 if (scrub_txg && check_excise &&
3164 vdev_dtl_should_excise(vd, rebuild_done)) {
3166 * We completed a scrub, resilver or rebuild up to
3167 * scrub_txg. If we did it without rebooting, then
3168 * the scrub dtl will be valid, so excise the old
3169 * region and fold in the scrub dtl. Otherwise,
3170 * leave the dtl as-is if there was an error.
3172 * There's little trick here: to excise the beginning
3173 * of the DTL_MISSING map, we put it into a reference
3174 * tree and then add a segment with refcnt -1 that
3175 * covers the range [0, scrub_txg). This means
3176 * that each txg in that range has refcnt -1 or 0.
3177 * We then add DTL_SCRUB with a refcnt of 2, so that
3178 * entries in the range [0, scrub_txg) will have a
3179 * positive refcnt -- either 1 or 2. We then convert
3180 * the reference tree into the new DTL_MISSING map.
3182 space_reftree_create(&reftree);
3183 space_reftree_add_map(&reftree,
3184 vd->vdev_dtl[DTL_MISSING], 1);
3185 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
3186 space_reftree_add_map(&reftree,
3187 vd->vdev_dtl[DTL_SCRUB], 2);
3188 space_reftree_generate_map(&reftree,
3189 vd->vdev_dtl[DTL_MISSING], 1);
3190 space_reftree_destroy(&reftree);
3192 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
3193 zfs_dbgmsg("update DTL_MISSING:%llu/%llu",
3194 (u_longlong_t)vdev_dtl_min(vd),
3195 (u_longlong_t)vdev_dtl_max(vd));
3196 } else if (!wasempty) {
3197 zfs_dbgmsg("DTL_MISSING is now empty");
3200 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
3201 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
3202 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
3203 if (scrub_done)
3204 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
3205 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
3206 if (!vdev_readable(vd))
3207 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
3208 else
3209 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
3210 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
3213 * If the vdev was resilvering or rebuilding and no longer
3214 * has any DTLs then reset the appropriate flag and dirty
3215 * the top level so that we persist the change.
3217 if (txg != 0 &&
3218 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
3219 range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) {
3220 if (vd->vdev_rebuild_txg != 0) {
3221 vd->vdev_rebuild_txg = 0;
3222 vdev_config_dirty(vd->vdev_top);
3223 } else if (vd->vdev_resilver_txg != 0) {
3224 vd->vdev_resilver_txg = 0;
3225 vdev_config_dirty(vd->vdev_top);
3229 mutex_exit(&vd->vdev_dtl_lock);
3231 if (txg != 0)
3232 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
3233 return;
3236 mutex_enter(&vd->vdev_dtl_lock);
3237 for (int t = 0; t < DTL_TYPES; t++) {
3238 /* account for child's outage in parent's missing map */
3239 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
3240 if (t == DTL_SCRUB)
3241 continue; /* leaf vdevs only */
3242 if (t == DTL_PARTIAL)
3243 minref = 1; /* i.e. non-zero */
3244 else if (vdev_get_nparity(vd) != 0)
3245 minref = vdev_get_nparity(vd) + 1; /* RAID-Z, dRAID */
3246 else
3247 minref = vd->vdev_children; /* any kind of mirror */
3248 space_reftree_create(&reftree);
3249 for (int c = 0; c < vd->vdev_children; c++) {
3250 vdev_t *cvd = vd->vdev_child[c];
3251 mutex_enter(&cvd->vdev_dtl_lock);
3252 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
3253 mutex_exit(&cvd->vdev_dtl_lock);
3255 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
3256 space_reftree_destroy(&reftree);
3258 mutex_exit(&vd->vdev_dtl_lock);
3262 * Iterate over all the vdevs except spare, and post kobj events
3264 void
3265 vdev_post_kobj_evt(vdev_t *vd)
3267 if (vd->vdev_ops->vdev_op_kobj_evt_post &&
3268 vd->vdev_kobj_flag == B_FALSE) {
3269 vd->vdev_kobj_flag = B_TRUE;
3270 vd->vdev_ops->vdev_op_kobj_evt_post(vd);
3273 for (int c = 0; c < vd->vdev_children; c++)
3274 vdev_post_kobj_evt(vd->vdev_child[c]);
3278 * Iterate over all the vdevs except spare, and clear kobj events
3280 void
3281 vdev_clear_kobj_evt(vdev_t *vd)
3283 vd->vdev_kobj_flag = B_FALSE;
3285 for (int c = 0; c < vd->vdev_children; c++)
3286 vdev_clear_kobj_evt(vd->vdev_child[c]);
3290 vdev_dtl_load(vdev_t *vd)
3292 spa_t *spa = vd->vdev_spa;
3293 objset_t *mos = spa->spa_meta_objset;
3294 range_tree_t *rt;
3295 int error = 0;
3297 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
3298 ASSERT(vdev_is_concrete(vd));
3301 * If the dtl cannot be sync'd there is no need to open it.
3303 if (spa->spa_mode == SPA_MODE_READ && !spa->spa_read_spacemaps)
3304 return (0);
3306 error = space_map_open(&vd->vdev_dtl_sm, mos,
3307 vd->vdev_dtl_object, 0, -1ULL, 0);
3308 if (error)
3309 return (error);
3310 ASSERT(vd->vdev_dtl_sm != NULL);
3312 rt = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
3313 error = space_map_load(vd->vdev_dtl_sm, rt, SM_ALLOC);
3314 if (error == 0) {
3315 mutex_enter(&vd->vdev_dtl_lock);
3316 range_tree_walk(rt, range_tree_add,
3317 vd->vdev_dtl[DTL_MISSING]);
3318 mutex_exit(&vd->vdev_dtl_lock);
3321 range_tree_vacate(rt, NULL, NULL);
3322 range_tree_destroy(rt);
3324 return (error);
3327 for (int c = 0; c < vd->vdev_children; c++) {
3328 error = vdev_dtl_load(vd->vdev_child[c]);
3329 if (error != 0)
3330 break;
3333 return (error);
3336 static void
3337 vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx)
3339 spa_t *spa = vd->vdev_spa;
3340 objset_t *mos = spa->spa_meta_objset;
3341 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
3342 const char *string;
3344 ASSERT(alloc_bias != VDEV_BIAS_NONE);
3346 string =
3347 (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG :
3348 (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL :
3349 (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL;
3351 ASSERT(string != NULL);
3352 VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS,
3353 1, strlen(string) + 1, string, tx));
3355 if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) {
3356 spa_activate_allocation_classes(spa, tx);
3360 void
3361 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
3363 spa_t *spa = vd->vdev_spa;
3365 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
3366 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
3367 zapobj, tx));
3370 uint64_t
3371 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
3373 spa_t *spa = vd->vdev_spa;
3374 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
3375 DMU_OT_NONE, 0, tx);
3377 ASSERT(zap != 0);
3378 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
3379 zap, tx));
3381 return (zap);
3384 void
3385 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
3387 if (vd->vdev_ops != &vdev_hole_ops &&
3388 vd->vdev_ops != &vdev_missing_ops &&
3389 vd->vdev_ops != &vdev_root_ops &&
3390 !vd->vdev_top->vdev_removing) {
3391 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
3392 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
3394 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
3395 vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
3396 if (vd->vdev_alloc_bias != VDEV_BIAS_NONE)
3397 vdev_zap_allocation_data(vd, tx);
3400 if (vd->vdev_ops == &vdev_root_ops && vd->vdev_root_zap == 0 &&
3401 spa_feature_is_enabled(vd->vdev_spa, SPA_FEATURE_AVZ_V2)) {
3402 if (!spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_AVZ_V2))
3403 spa_feature_incr(vd->vdev_spa, SPA_FEATURE_AVZ_V2, tx);
3404 vd->vdev_root_zap = vdev_create_link_zap(vd, tx);
3407 for (uint64_t i = 0; i < vd->vdev_children; i++) {
3408 vdev_construct_zaps(vd->vdev_child[i], tx);
3412 static void
3413 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
3415 spa_t *spa = vd->vdev_spa;
3416 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
3417 objset_t *mos = spa->spa_meta_objset;
3418 range_tree_t *rtsync;
3419 dmu_tx_t *tx;
3420 uint64_t object = space_map_object(vd->vdev_dtl_sm);
3422 ASSERT(vdev_is_concrete(vd));
3423 ASSERT(vd->vdev_ops->vdev_op_leaf);
3425 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3427 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
3428 mutex_enter(&vd->vdev_dtl_lock);
3429 space_map_free(vd->vdev_dtl_sm, tx);
3430 space_map_close(vd->vdev_dtl_sm);
3431 vd->vdev_dtl_sm = NULL;
3432 mutex_exit(&vd->vdev_dtl_lock);
3435 * We only destroy the leaf ZAP for detached leaves or for
3436 * removed log devices. Removed data devices handle leaf ZAP
3437 * cleanup later, once cancellation is no longer possible.
3439 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
3440 vd->vdev_top->vdev_islog)) {
3441 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
3442 vd->vdev_leaf_zap = 0;
3445 dmu_tx_commit(tx);
3446 return;
3449 if (vd->vdev_dtl_sm == NULL) {
3450 uint64_t new_object;
3452 new_object = space_map_alloc(mos, zfs_vdev_dtl_sm_blksz, tx);
3453 VERIFY3U(new_object, !=, 0);
3455 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
3456 0, -1ULL, 0));
3457 ASSERT(vd->vdev_dtl_sm != NULL);
3460 rtsync = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
3462 mutex_enter(&vd->vdev_dtl_lock);
3463 range_tree_walk(rt, range_tree_add, rtsync);
3464 mutex_exit(&vd->vdev_dtl_lock);
3466 space_map_truncate(vd->vdev_dtl_sm, zfs_vdev_dtl_sm_blksz, tx);
3467 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx);
3468 range_tree_vacate(rtsync, NULL, NULL);
3470 range_tree_destroy(rtsync);
3473 * If the object for the space map has changed then dirty
3474 * the top level so that we update the config.
3476 if (object != space_map_object(vd->vdev_dtl_sm)) {
3477 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
3478 "new object %llu", (u_longlong_t)txg, spa_name(spa),
3479 (u_longlong_t)object,
3480 (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
3481 vdev_config_dirty(vd->vdev_top);
3484 dmu_tx_commit(tx);
3488 * Determine whether the specified vdev can be offlined/detached/removed
3489 * without losing data.
3491 boolean_t
3492 vdev_dtl_required(vdev_t *vd)
3494 spa_t *spa = vd->vdev_spa;
3495 vdev_t *tvd = vd->vdev_top;
3496 uint8_t cant_read = vd->vdev_cant_read;
3497 boolean_t required;
3499 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3501 if (vd == spa->spa_root_vdev || vd == tvd)
3502 return (B_TRUE);
3505 * Temporarily mark the device as unreadable, and then determine
3506 * whether this results in any DTL outages in the top-level vdev.
3507 * If not, we can safely offline/detach/remove the device.
3509 vd->vdev_cant_read = B_TRUE;
3510 vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE);
3511 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
3512 vd->vdev_cant_read = cant_read;
3513 vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE);
3515 if (!required && zio_injection_enabled) {
3516 required = !!zio_handle_device_injection(vd, NULL,
3517 SET_ERROR(ECHILD));
3520 return (required);
3524 * Determine if resilver is needed, and if so the txg range.
3526 boolean_t
3527 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
3529 boolean_t needed = B_FALSE;
3530 uint64_t thismin = UINT64_MAX;
3531 uint64_t thismax = 0;
3533 if (vd->vdev_children == 0) {
3534 mutex_enter(&vd->vdev_dtl_lock);
3535 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
3536 vdev_writeable(vd)) {
3538 thismin = vdev_dtl_min(vd);
3539 thismax = vdev_dtl_max(vd);
3540 needed = B_TRUE;
3542 mutex_exit(&vd->vdev_dtl_lock);
3543 } else {
3544 for (int c = 0; c < vd->vdev_children; c++) {
3545 vdev_t *cvd = vd->vdev_child[c];
3546 uint64_t cmin, cmax;
3548 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
3549 thismin = MIN(thismin, cmin);
3550 thismax = MAX(thismax, cmax);
3551 needed = B_TRUE;
3556 if (needed && minp) {
3557 *minp = thismin;
3558 *maxp = thismax;
3560 return (needed);
3564 * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj
3565 * will contain either the checkpoint spacemap object or zero if none exists.
3566 * All other errors are returned to the caller.
3569 vdev_checkpoint_sm_object(vdev_t *vd, uint64_t *sm_obj)
3571 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
3573 if (vd->vdev_top_zap == 0) {
3574 *sm_obj = 0;
3575 return (0);
3578 int error = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap,
3579 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, sm_obj);
3580 if (error == ENOENT) {
3581 *sm_obj = 0;
3582 error = 0;
3585 return (error);
3589 vdev_load(vdev_t *vd)
3591 int children = vd->vdev_children;
3592 int error = 0;
3593 taskq_t *tq = NULL;
3596 * It's only worthwhile to use the taskq for the root vdev, because the
3597 * slow part is metaslab_init, and that only happens for top-level
3598 * vdevs.
3600 if (vd->vdev_ops == &vdev_root_ops && vd->vdev_children > 0) {
3601 tq = taskq_create("vdev_load", children, minclsyspri,
3602 children, children, TASKQ_PREPOPULATE);
3606 * Recursively load all children.
3608 for (int c = 0; c < vd->vdev_children; c++) {
3609 vdev_t *cvd = vd->vdev_child[c];
3611 if (tq == NULL || vdev_uses_zvols(cvd)) {
3612 cvd->vdev_load_error = vdev_load(cvd);
3613 } else {
3614 VERIFY(taskq_dispatch(tq, vdev_load_child,
3615 cvd, TQ_SLEEP) != TASKQID_INVALID);
3619 if (tq != NULL) {
3620 taskq_wait(tq);
3621 taskq_destroy(tq);
3624 for (int c = 0; c < vd->vdev_children; c++) {
3625 int error = vd->vdev_child[c]->vdev_load_error;
3627 if (error != 0)
3628 return (error);
3631 vdev_set_deflate_ratio(vd);
3634 * On spa_load path, grab the allocation bias from our zap
3636 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3637 spa_t *spa = vd->vdev_spa;
3638 char bias_str[64];
3640 error = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
3641 VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str),
3642 bias_str);
3643 if (error == 0) {
3644 ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE);
3645 vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str);
3646 } else if (error != ENOENT) {
3647 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3648 VDEV_AUX_CORRUPT_DATA);
3649 vdev_dbgmsg(vd, "vdev_load: zap_lookup(top_zap=%llu) "
3650 "failed [error=%d]",
3651 (u_longlong_t)vd->vdev_top_zap, error);
3652 return (error);
3656 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3657 spa_t *spa = vd->vdev_spa;
3658 uint64_t failfast;
3660 error = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
3661 vdev_prop_to_name(VDEV_PROP_FAILFAST), sizeof (failfast),
3662 1, &failfast);
3663 if (error == 0) {
3664 vd->vdev_failfast = failfast & 1;
3665 } else if (error == ENOENT) {
3666 vd->vdev_failfast = vdev_prop_default_numeric(
3667 VDEV_PROP_FAILFAST);
3668 } else {
3669 vdev_dbgmsg(vd,
3670 "vdev_load: zap_lookup(top_zap=%llu) "
3671 "failed [error=%d]",
3672 (u_longlong_t)vd->vdev_top_zap, error);
3677 * Load any rebuild state from the top-level vdev zap.
3679 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3680 error = vdev_rebuild_load(vd);
3681 if (error && error != ENOTSUP) {
3682 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3683 VDEV_AUX_CORRUPT_DATA);
3684 vdev_dbgmsg(vd, "vdev_load: vdev_rebuild_load "
3685 "failed [error=%d]", error);
3686 return (error);
3690 if (vd->vdev_top_zap != 0 || vd->vdev_leaf_zap != 0) {
3691 uint64_t zapobj;
3693 if (vd->vdev_top_zap != 0)
3694 zapobj = vd->vdev_top_zap;
3695 else
3696 zapobj = vd->vdev_leaf_zap;
3698 error = vdev_prop_get_int(vd, VDEV_PROP_CHECKSUM_N,
3699 &vd->vdev_checksum_n);
3700 if (error && error != ENOENT)
3701 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3702 "failed [error=%d]", (u_longlong_t)zapobj, error);
3704 error = vdev_prop_get_int(vd, VDEV_PROP_CHECKSUM_T,
3705 &vd->vdev_checksum_t);
3706 if (error && error != ENOENT)
3707 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3708 "failed [error=%d]", (u_longlong_t)zapobj, error);
3710 error = vdev_prop_get_int(vd, VDEV_PROP_IO_N,
3711 &vd->vdev_io_n);
3712 if (error && error != ENOENT)
3713 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3714 "failed [error=%d]", (u_longlong_t)zapobj, error);
3716 error = vdev_prop_get_int(vd, VDEV_PROP_IO_T,
3717 &vd->vdev_io_t);
3718 if (error && error != ENOENT)
3719 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3720 "failed [error=%d]", (u_longlong_t)zapobj, error);
3724 * If this is a top-level vdev, initialize its metaslabs.
3726 if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
3727 vdev_metaslab_group_create(vd);
3729 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
3730 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3731 VDEV_AUX_CORRUPT_DATA);
3732 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
3733 "asize=%llu", (u_longlong_t)vd->vdev_ashift,
3734 (u_longlong_t)vd->vdev_asize);
3735 return (SET_ERROR(ENXIO));
3738 error = vdev_metaslab_init(vd, 0);
3739 if (error != 0) {
3740 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
3741 "[error=%d]", error);
3742 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3743 VDEV_AUX_CORRUPT_DATA);
3744 return (error);
3747 uint64_t checkpoint_sm_obj;
3748 error = vdev_checkpoint_sm_object(vd, &checkpoint_sm_obj);
3749 if (error == 0 && checkpoint_sm_obj != 0) {
3750 objset_t *mos = spa_meta_objset(vd->vdev_spa);
3751 ASSERT(vd->vdev_asize != 0);
3752 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL);
3754 error = space_map_open(&vd->vdev_checkpoint_sm,
3755 mos, checkpoint_sm_obj, 0, vd->vdev_asize,
3756 vd->vdev_ashift);
3757 if (error != 0) {
3758 vdev_dbgmsg(vd, "vdev_load: space_map_open "
3759 "failed for checkpoint spacemap (obj %llu) "
3760 "[error=%d]",
3761 (u_longlong_t)checkpoint_sm_obj, error);
3762 return (error);
3764 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
3767 * Since the checkpoint_sm contains free entries
3768 * exclusively we can use space_map_allocated() to
3769 * indicate the cumulative checkpointed space that
3770 * has been freed.
3772 vd->vdev_stat.vs_checkpoint_space =
3773 -space_map_allocated(vd->vdev_checkpoint_sm);
3774 vd->vdev_spa->spa_checkpoint_info.sci_dspace +=
3775 vd->vdev_stat.vs_checkpoint_space;
3776 } else if (error != 0) {
3777 vdev_dbgmsg(vd, "vdev_load: failed to retrieve "
3778 "checkpoint space map object from vdev ZAP "
3779 "[error=%d]", error);
3780 return (error);
3785 * If this is a leaf vdev, load its DTL.
3787 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
3788 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3789 VDEV_AUX_CORRUPT_DATA);
3790 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
3791 "[error=%d]", error);
3792 return (error);
3795 uint64_t obsolete_sm_object;
3796 error = vdev_obsolete_sm_object(vd, &obsolete_sm_object);
3797 if (error == 0 && obsolete_sm_object != 0) {
3798 objset_t *mos = vd->vdev_spa->spa_meta_objset;
3799 ASSERT(vd->vdev_asize != 0);
3800 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
3802 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
3803 obsolete_sm_object, 0, vd->vdev_asize, 0))) {
3804 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3805 VDEV_AUX_CORRUPT_DATA);
3806 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
3807 "obsolete spacemap (obj %llu) [error=%d]",
3808 (u_longlong_t)obsolete_sm_object, error);
3809 return (error);
3811 } else if (error != 0) {
3812 vdev_dbgmsg(vd, "vdev_load: failed to retrieve obsolete "
3813 "space map object from vdev ZAP [error=%d]", error);
3814 return (error);
3817 return (0);
3821 * The special vdev case is used for hot spares and l2cache devices. Its
3822 * sole purpose it to set the vdev state for the associated vdev. To do this,
3823 * we make sure that we can open the underlying device, then try to read the
3824 * label, and make sure that the label is sane and that it hasn't been
3825 * repurposed to another pool.
3828 vdev_validate_aux(vdev_t *vd)
3830 nvlist_t *label;
3831 uint64_t guid, version;
3832 uint64_t state;
3834 if (!vdev_readable(vd))
3835 return (0);
3837 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
3838 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3839 VDEV_AUX_CORRUPT_DATA);
3840 return (-1);
3843 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
3844 !SPA_VERSION_IS_SUPPORTED(version) ||
3845 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
3846 guid != vd->vdev_guid ||
3847 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
3848 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3849 VDEV_AUX_CORRUPT_DATA);
3850 nvlist_free(label);
3851 return (-1);
3855 * We don't actually check the pool state here. If it's in fact in
3856 * use by another pool, we update this fact on the fly when requested.
3858 nvlist_free(label);
3859 return (0);
3862 static void
3863 vdev_destroy_ms_flush_data(vdev_t *vd, dmu_tx_t *tx)
3865 objset_t *mos = spa_meta_objset(vd->vdev_spa);
3867 if (vd->vdev_top_zap == 0)
3868 return;
3870 uint64_t object = 0;
3871 int err = zap_lookup(mos, vd->vdev_top_zap,
3872 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, sizeof (uint64_t), 1, &object);
3873 if (err == ENOENT)
3874 return;
3875 VERIFY0(err);
3877 VERIFY0(dmu_object_free(mos, object, tx));
3878 VERIFY0(zap_remove(mos, vd->vdev_top_zap,
3879 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, tx));
3883 * Free the objects used to store this vdev's spacemaps, and the array
3884 * that points to them.
3886 void
3887 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
3889 if (vd->vdev_ms_array == 0)
3890 return;
3892 objset_t *mos = vd->vdev_spa->spa_meta_objset;
3893 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
3894 size_t array_bytes = array_count * sizeof (uint64_t);
3895 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
3896 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
3897 array_bytes, smobj_array, 0));
3899 for (uint64_t i = 0; i < array_count; i++) {
3900 uint64_t smobj = smobj_array[i];
3901 if (smobj == 0)
3902 continue;
3904 space_map_free_obj(mos, smobj, tx);
3907 kmem_free(smobj_array, array_bytes);
3908 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
3909 vdev_destroy_ms_flush_data(vd, tx);
3910 vd->vdev_ms_array = 0;
3913 static void
3914 vdev_remove_empty_log(vdev_t *vd, uint64_t txg)
3916 spa_t *spa = vd->vdev_spa;
3918 ASSERT(vd->vdev_islog);
3919 ASSERT(vd == vd->vdev_top);
3920 ASSERT3U(txg, ==, spa_syncing_txg(spa));
3922 dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
3924 vdev_destroy_spacemaps(vd, tx);
3925 if (vd->vdev_top_zap != 0) {
3926 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
3927 vd->vdev_top_zap = 0;
3930 dmu_tx_commit(tx);
3933 void
3934 vdev_sync_done(vdev_t *vd, uint64_t txg)
3936 metaslab_t *msp;
3937 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
3939 ASSERT(vdev_is_concrete(vd));
3941 while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
3942 != NULL)
3943 metaslab_sync_done(msp, txg);
3945 if (reassess) {
3946 metaslab_sync_reassess(vd->vdev_mg);
3947 if (vd->vdev_log_mg != NULL)
3948 metaslab_sync_reassess(vd->vdev_log_mg);
3952 void
3953 vdev_sync(vdev_t *vd, uint64_t txg)
3955 spa_t *spa = vd->vdev_spa;
3956 vdev_t *lvd;
3957 metaslab_t *msp;
3959 ASSERT3U(txg, ==, spa->spa_syncing_txg);
3960 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3961 if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
3962 ASSERT(vd->vdev_removing ||
3963 vd->vdev_ops == &vdev_indirect_ops);
3965 vdev_indirect_sync_obsolete(vd, tx);
3968 * If the vdev is indirect, it can't have dirty
3969 * metaslabs or DTLs.
3971 if (vd->vdev_ops == &vdev_indirect_ops) {
3972 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
3973 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
3974 dmu_tx_commit(tx);
3975 return;
3979 ASSERT(vdev_is_concrete(vd));
3981 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
3982 !vd->vdev_removing) {
3983 ASSERT(vd == vd->vdev_top);
3984 ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
3985 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
3986 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
3987 ASSERT(vd->vdev_ms_array != 0);
3988 vdev_config_dirty(vd);
3991 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
3992 metaslab_sync(msp, txg);
3993 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
3996 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
3997 vdev_dtl_sync(lvd, txg);
4000 * If this is an empty log device being removed, destroy the
4001 * metadata associated with it.
4003 if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
4004 vdev_remove_empty_log(vd, txg);
4006 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
4007 dmu_tx_commit(tx);
4010 uint64_t
4011 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
4013 return (vd->vdev_ops->vdev_op_asize(vd, psize));
4017 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
4018 * not be opened, and no I/O is attempted.
4021 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
4023 vdev_t *vd, *tvd;
4025 spa_vdev_state_enter(spa, SCL_NONE);
4027 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4028 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4030 if (!vd->vdev_ops->vdev_op_leaf)
4031 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4033 tvd = vd->vdev_top;
4036 * If user did a 'zpool offline -f' then make the fault persist across
4037 * reboots.
4039 if (aux == VDEV_AUX_EXTERNAL_PERSIST) {
4041 * There are two kinds of forced faults: temporary and
4042 * persistent. Temporary faults go away at pool import, while
4043 * persistent faults stay set. Both types of faults can be
4044 * cleared with a zpool clear.
4046 * We tell if a vdev is persistently faulted by looking at the
4047 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
4048 * import then it's a persistent fault. Otherwise, it's
4049 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
4050 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
4051 * tells vdev_config_generate() (which gets run later) to set
4052 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
4054 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL;
4055 vd->vdev_tmpoffline = B_FALSE;
4056 aux = VDEV_AUX_EXTERNAL;
4057 } else {
4058 vd->vdev_tmpoffline = B_TRUE;
4062 * We don't directly use the aux state here, but if we do a
4063 * vdev_reopen(), we need this value to be present to remember why we
4064 * were faulted.
4066 vd->vdev_label_aux = aux;
4069 * Faulted state takes precedence over degraded.
4071 vd->vdev_delayed_close = B_FALSE;
4072 vd->vdev_faulted = 1ULL;
4073 vd->vdev_degraded = 0ULL;
4074 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
4077 * If this device has the only valid copy of the data, then
4078 * back off and simply mark the vdev as degraded instead.
4080 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
4081 vd->vdev_degraded = 1ULL;
4082 vd->vdev_faulted = 0ULL;
4085 * If we reopen the device and it's not dead, only then do we
4086 * mark it degraded.
4088 vdev_reopen(tvd);
4090 if (vdev_readable(vd))
4091 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
4094 return (spa_vdev_state_exit(spa, vd, 0));
4098 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
4099 * user that something is wrong. The vdev continues to operate as normal as far
4100 * as I/O is concerned.
4103 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
4105 vdev_t *vd;
4107 spa_vdev_state_enter(spa, SCL_NONE);
4109 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4110 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4112 if (!vd->vdev_ops->vdev_op_leaf)
4113 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4116 * If the vdev is already faulted, then don't do anything.
4118 if (vd->vdev_faulted || vd->vdev_degraded)
4119 return (spa_vdev_state_exit(spa, NULL, 0));
4121 vd->vdev_degraded = 1ULL;
4122 if (!vdev_is_dead(vd))
4123 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
4124 aux);
4126 return (spa_vdev_state_exit(spa, vd, 0));
4130 vdev_remove_wanted(spa_t *spa, uint64_t guid)
4132 vdev_t *vd;
4134 spa_vdev_state_enter(spa, SCL_NONE);
4136 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4137 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4140 * If the vdev is already removed, or expanding which can trigger
4141 * repartition add/remove events, then don't do anything.
4143 if (vd->vdev_removed || vd->vdev_expanding)
4144 return (spa_vdev_state_exit(spa, NULL, 0));
4147 * Confirm the vdev has been removed, otherwise don't do anything.
4149 if (vd->vdev_ops->vdev_op_leaf && !zio_wait(vdev_probe(vd, NULL)))
4150 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(EEXIST)));
4152 vd->vdev_remove_wanted = B_TRUE;
4153 spa_async_request(spa, SPA_ASYNC_REMOVE);
4155 return (spa_vdev_state_exit(spa, vd, 0));
4160 * Online the given vdev.
4162 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
4163 * spare device should be detached when the device finishes resilvering.
4164 * Second, the online should be treated like a 'test' online case, so no FMA
4165 * events are generated if the device fails to open.
4168 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
4170 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
4171 boolean_t wasoffline;
4172 vdev_state_t oldstate;
4174 spa_vdev_state_enter(spa, SCL_NONE);
4176 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4177 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4179 if (!vd->vdev_ops->vdev_op_leaf)
4180 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4182 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
4183 oldstate = vd->vdev_state;
4185 tvd = vd->vdev_top;
4186 vd->vdev_offline = B_FALSE;
4187 vd->vdev_tmpoffline = B_FALSE;
4188 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
4189 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
4191 /* XXX - L2ARC 1.0 does not support expansion */
4192 if (!vd->vdev_aux) {
4193 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
4194 pvd->vdev_expanding = !!((flags & ZFS_ONLINE_EXPAND) ||
4195 spa->spa_autoexpand);
4196 vd->vdev_expansion_time = gethrestime_sec();
4199 vdev_reopen(tvd);
4200 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
4202 if (!vd->vdev_aux) {
4203 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
4204 pvd->vdev_expanding = B_FALSE;
4207 if (newstate)
4208 *newstate = vd->vdev_state;
4209 if ((flags & ZFS_ONLINE_UNSPARE) &&
4210 !vdev_is_dead(vd) && vd->vdev_parent &&
4211 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4212 vd->vdev_parent->vdev_child[0] == vd)
4213 vd->vdev_unspare = B_TRUE;
4215 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
4217 /* XXX - L2ARC 1.0 does not support expansion */
4218 if (vd->vdev_aux)
4219 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
4220 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
4223 /* Restart initializing if necessary */
4224 mutex_enter(&vd->vdev_initialize_lock);
4225 if (vdev_writeable(vd) &&
4226 vd->vdev_initialize_thread == NULL &&
4227 vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) {
4228 (void) vdev_initialize(vd);
4230 mutex_exit(&vd->vdev_initialize_lock);
4233 * Restart trimming if necessary. We do not restart trimming for cache
4234 * devices here. This is triggered by l2arc_rebuild_vdev()
4235 * asynchronously for the whole device or in l2arc_evict() as it evicts
4236 * space for upcoming writes.
4238 mutex_enter(&vd->vdev_trim_lock);
4239 if (vdev_writeable(vd) && !vd->vdev_isl2cache &&
4240 vd->vdev_trim_thread == NULL &&
4241 vd->vdev_trim_state == VDEV_TRIM_ACTIVE) {
4242 (void) vdev_trim(vd, vd->vdev_trim_rate, vd->vdev_trim_partial,
4243 vd->vdev_trim_secure);
4245 mutex_exit(&vd->vdev_trim_lock);
4247 if (wasoffline ||
4248 (oldstate < VDEV_STATE_DEGRADED &&
4249 vd->vdev_state >= VDEV_STATE_DEGRADED)) {
4250 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
4253 * Asynchronously detach spare vdev if resilver or
4254 * rebuild is not required
4256 if (vd->vdev_unspare &&
4257 !dsl_scan_resilvering(spa->spa_dsl_pool) &&
4258 !dsl_scan_resilver_scheduled(spa->spa_dsl_pool) &&
4259 !vdev_rebuild_active(tvd))
4260 spa_async_request(spa, SPA_ASYNC_DETACH_SPARE);
4262 return (spa_vdev_state_exit(spa, vd, 0));
4265 static int
4266 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
4268 vdev_t *vd, *tvd;
4269 int error = 0;
4270 uint64_t generation;
4271 metaslab_group_t *mg;
4273 top:
4274 spa_vdev_state_enter(spa, SCL_ALLOC);
4276 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4277 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4279 if (!vd->vdev_ops->vdev_op_leaf)
4280 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4282 if (vd->vdev_ops == &vdev_draid_spare_ops)
4283 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
4285 tvd = vd->vdev_top;
4286 mg = tvd->vdev_mg;
4287 generation = spa->spa_config_generation + 1;
4290 * If the device isn't already offline, try to offline it.
4292 if (!vd->vdev_offline) {
4294 * If this device has the only valid copy of some data,
4295 * don't allow it to be offlined. Log devices are always
4296 * expendable.
4298 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
4299 vdev_dtl_required(vd))
4300 return (spa_vdev_state_exit(spa, NULL,
4301 SET_ERROR(EBUSY)));
4304 * If the top-level is a slog and it has had allocations
4305 * then proceed. We check that the vdev's metaslab group
4306 * is not NULL since it's possible that we may have just
4307 * added this vdev but not yet initialized its metaslabs.
4309 if (tvd->vdev_islog && mg != NULL) {
4311 * Prevent any future allocations.
4313 ASSERT3P(tvd->vdev_log_mg, ==, NULL);
4314 metaslab_group_passivate(mg);
4315 (void) spa_vdev_state_exit(spa, vd, 0);
4317 error = spa_reset_logs(spa);
4320 * If the log device was successfully reset but has
4321 * checkpointed data, do not offline it.
4323 if (error == 0 &&
4324 tvd->vdev_checkpoint_sm != NULL) {
4325 ASSERT3U(space_map_allocated(
4326 tvd->vdev_checkpoint_sm), !=, 0);
4327 error = ZFS_ERR_CHECKPOINT_EXISTS;
4330 spa_vdev_state_enter(spa, SCL_ALLOC);
4333 * Check to see if the config has changed.
4335 if (error || generation != spa->spa_config_generation) {
4336 metaslab_group_activate(mg);
4337 if (error)
4338 return (spa_vdev_state_exit(spa,
4339 vd, error));
4340 (void) spa_vdev_state_exit(spa, vd, 0);
4341 goto top;
4343 ASSERT0(tvd->vdev_stat.vs_alloc);
4347 * Offline this device and reopen its top-level vdev.
4348 * If the top-level vdev is a log device then just offline
4349 * it. Otherwise, if this action results in the top-level
4350 * vdev becoming unusable, undo it and fail the request.
4352 vd->vdev_offline = B_TRUE;
4353 vdev_reopen(tvd);
4355 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
4356 vdev_is_dead(tvd)) {
4357 vd->vdev_offline = B_FALSE;
4358 vdev_reopen(tvd);
4359 return (spa_vdev_state_exit(spa, NULL,
4360 SET_ERROR(EBUSY)));
4364 * Add the device back into the metaslab rotor so that
4365 * once we online the device it's open for business.
4367 if (tvd->vdev_islog && mg != NULL)
4368 metaslab_group_activate(mg);
4371 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
4373 return (spa_vdev_state_exit(spa, vd, 0));
4377 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
4379 int error;
4381 mutex_enter(&spa->spa_vdev_top_lock);
4382 error = vdev_offline_locked(spa, guid, flags);
4383 mutex_exit(&spa->spa_vdev_top_lock);
4385 return (error);
4389 * Clear the error counts associated with this vdev. Unlike vdev_online() and
4390 * vdev_offline(), we assume the spa config is locked. We also clear all
4391 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
4393 void
4394 vdev_clear(spa_t *spa, vdev_t *vd)
4396 vdev_t *rvd = spa->spa_root_vdev;
4398 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
4400 if (vd == NULL)
4401 vd = rvd;
4403 vd->vdev_stat.vs_read_errors = 0;
4404 vd->vdev_stat.vs_write_errors = 0;
4405 vd->vdev_stat.vs_checksum_errors = 0;
4406 vd->vdev_stat.vs_slow_ios = 0;
4408 for (int c = 0; c < vd->vdev_children; c++)
4409 vdev_clear(spa, vd->vdev_child[c]);
4412 * It makes no sense to "clear" an indirect or removed vdev.
4414 if (!vdev_is_concrete(vd) || vd->vdev_removed)
4415 return;
4418 * If we're in the FAULTED state or have experienced failed I/O, then
4419 * clear the persistent state and attempt to reopen the device. We
4420 * also mark the vdev config dirty, so that the new faulted state is
4421 * written out to disk.
4423 if (vd->vdev_faulted || vd->vdev_degraded ||
4424 !vdev_readable(vd) || !vdev_writeable(vd)) {
4426 * When reopening in response to a clear event, it may be due to
4427 * a fmadm repair request. In this case, if the device is
4428 * still broken, we want to still post the ereport again.
4430 vd->vdev_forcefault = B_TRUE;
4432 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
4433 vd->vdev_cant_read = B_FALSE;
4434 vd->vdev_cant_write = B_FALSE;
4435 vd->vdev_stat.vs_aux = 0;
4437 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
4439 vd->vdev_forcefault = B_FALSE;
4441 if (vd != rvd && vdev_writeable(vd->vdev_top))
4442 vdev_state_dirty(vd->vdev_top);
4444 /* If a resilver isn't required, check if vdevs can be culled */
4445 if (vd->vdev_aux == NULL && !vdev_is_dead(vd) &&
4446 !dsl_scan_resilvering(spa->spa_dsl_pool) &&
4447 !dsl_scan_resilver_scheduled(spa->spa_dsl_pool))
4448 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
4450 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
4454 * When clearing a FMA-diagnosed fault, we always want to
4455 * unspare the device, as we assume that the original spare was
4456 * done in response to the FMA fault.
4458 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
4459 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4460 vd->vdev_parent->vdev_child[0] == vd)
4461 vd->vdev_unspare = B_TRUE;
4463 /* Clear recent error events cache (i.e. duplicate events tracking) */
4464 zfs_ereport_clear(spa, vd);
4467 boolean_t
4468 vdev_is_dead(vdev_t *vd)
4471 * Holes and missing devices are always considered "dead".
4472 * This simplifies the code since we don't have to check for
4473 * these types of devices in the various code paths.
4474 * Instead we rely on the fact that we skip over dead devices
4475 * before issuing I/O to them.
4477 return (vd->vdev_state < VDEV_STATE_DEGRADED ||
4478 vd->vdev_ops == &vdev_hole_ops ||
4479 vd->vdev_ops == &vdev_missing_ops);
4482 boolean_t
4483 vdev_readable(vdev_t *vd)
4485 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
4488 boolean_t
4489 vdev_writeable(vdev_t *vd)
4491 return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
4492 vdev_is_concrete(vd));
4495 boolean_t
4496 vdev_allocatable(vdev_t *vd)
4498 uint64_t state = vd->vdev_state;
4501 * We currently allow allocations from vdevs which may be in the
4502 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
4503 * fails to reopen then we'll catch it later when we're holding
4504 * the proper locks. Note that we have to get the vdev state
4505 * in a local variable because although it changes atomically,
4506 * we're asking two separate questions about it.
4508 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
4509 !vd->vdev_cant_write && vdev_is_concrete(vd) &&
4510 vd->vdev_mg->mg_initialized);
4513 boolean_t
4514 vdev_accessible(vdev_t *vd, zio_t *zio)
4516 ASSERT(zio->io_vd == vd);
4518 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
4519 return (B_FALSE);
4521 if (zio->io_type == ZIO_TYPE_READ)
4522 return (!vd->vdev_cant_read);
4524 if (zio->io_type == ZIO_TYPE_WRITE)
4525 return (!vd->vdev_cant_write);
4527 return (B_TRUE);
4530 static void
4531 vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs)
4534 * Exclude the dRAID spare when aggregating to avoid double counting
4535 * the ops and bytes. These IOs are counted by the physical leaves.
4537 if (cvd->vdev_ops == &vdev_draid_spare_ops)
4538 return;
4540 for (int t = 0; t < VS_ZIO_TYPES; t++) {
4541 vs->vs_ops[t] += cvs->vs_ops[t];
4542 vs->vs_bytes[t] += cvs->vs_bytes[t];
4545 cvs->vs_scan_removing = cvd->vdev_removing;
4549 * Get extended stats
4551 static void
4552 vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx)
4554 (void) cvd;
4556 int t, b;
4557 for (t = 0; t < ZIO_TYPES; t++) {
4558 for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++)
4559 vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b];
4561 for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) {
4562 vsx->vsx_total_histo[t][b] +=
4563 cvsx->vsx_total_histo[t][b];
4567 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
4568 for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) {
4569 vsx->vsx_queue_histo[t][b] +=
4570 cvsx->vsx_queue_histo[t][b];
4572 vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t];
4573 vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t];
4575 for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++)
4576 vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b];
4578 for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++)
4579 vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b];
4584 boolean_t
4585 vdev_is_spacemap_addressable(vdev_t *vd)
4587 if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2))
4588 return (B_TRUE);
4591 * If double-word space map entries are not enabled we assume
4592 * 47 bits of the space map entry are dedicated to the entry's
4593 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
4594 * to calculate the maximum address that can be described by a
4595 * space map entry for the given device.
4597 uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS;
4599 if (shift >= 63) /* detect potential overflow */
4600 return (B_TRUE);
4602 return (vd->vdev_asize < (1ULL << shift));
4606 * Get statistics for the given vdev.
4608 static void
4609 vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
4611 int t;
4613 * If we're getting stats on the root vdev, aggregate the I/O counts
4614 * over all top-level vdevs (i.e. the direct children of the root).
4616 if (!vd->vdev_ops->vdev_op_leaf) {
4617 if (vs) {
4618 memset(vs->vs_ops, 0, sizeof (vs->vs_ops));
4619 memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes));
4621 if (vsx)
4622 memset(vsx, 0, sizeof (*vsx));
4624 for (int c = 0; c < vd->vdev_children; c++) {
4625 vdev_t *cvd = vd->vdev_child[c];
4626 vdev_stat_t *cvs = &cvd->vdev_stat;
4627 vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex;
4629 vdev_get_stats_ex_impl(cvd, cvs, cvsx);
4630 if (vs)
4631 vdev_get_child_stat(cvd, vs, cvs);
4632 if (vsx)
4633 vdev_get_child_stat_ex(cvd, vsx, cvsx);
4635 } else {
4637 * We're a leaf. Just copy our ZIO active queue stats in. The
4638 * other leaf stats are updated in vdev_stat_update().
4640 if (!vsx)
4641 return;
4643 memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex));
4645 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
4646 vsx->vsx_active_queue[t] = vd->vdev_queue.vq_cactive[t];
4647 vsx->vsx_pend_queue[t] = vdev_queue_class_length(vd, t);
4652 void
4653 vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
4655 vdev_t *tvd = vd->vdev_top;
4656 mutex_enter(&vd->vdev_stat_lock);
4657 if (vs) {
4658 memcpy(vs, &vd->vdev_stat, sizeof (*vs));
4659 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
4660 vs->vs_state = vd->vdev_state;
4661 vs->vs_rsize = vdev_get_min_asize(vd);
4663 if (vd->vdev_ops->vdev_op_leaf) {
4664 vs->vs_pspace = vd->vdev_psize;
4665 vs->vs_rsize += VDEV_LABEL_START_SIZE +
4666 VDEV_LABEL_END_SIZE;
4668 * Report initializing progress. Since we don't
4669 * have the initializing locks held, this is only
4670 * an estimate (although a fairly accurate one).
4672 vs->vs_initialize_bytes_done =
4673 vd->vdev_initialize_bytes_done;
4674 vs->vs_initialize_bytes_est =
4675 vd->vdev_initialize_bytes_est;
4676 vs->vs_initialize_state = vd->vdev_initialize_state;
4677 vs->vs_initialize_action_time =
4678 vd->vdev_initialize_action_time;
4681 * Report manual TRIM progress. Since we don't have
4682 * the manual TRIM locks held, this is only an
4683 * estimate (although fairly accurate one).
4685 vs->vs_trim_notsup = !vd->vdev_has_trim;
4686 vs->vs_trim_bytes_done = vd->vdev_trim_bytes_done;
4687 vs->vs_trim_bytes_est = vd->vdev_trim_bytes_est;
4688 vs->vs_trim_state = vd->vdev_trim_state;
4689 vs->vs_trim_action_time = vd->vdev_trim_action_time;
4691 /* Set when there is a deferred resilver. */
4692 vs->vs_resilver_deferred = vd->vdev_resilver_deferred;
4696 * Report expandable space on top-level, non-auxiliary devices
4697 * only. The expandable space is reported in terms of metaslab
4698 * sized units since that determines how much space the pool
4699 * can expand.
4701 if (vd->vdev_aux == NULL && tvd != NULL) {
4702 vs->vs_esize = P2ALIGN(
4703 vd->vdev_max_asize - vd->vdev_asize,
4704 1ULL << tvd->vdev_ms_shift);
4707 vs->vs_configured_ashift = vd->vdev_top != NULL
4708 ? vd->vdev_top->vdev_ashift : vd->vdev_ashift;
4709 vs->vs_logical_ashift = vd->vdev_logical_ashift;
4710 if (vd->vdev_physical_ashift <= ASHIFT_MAX)
4711 vs->vs_physical_ashift = vd->vdev_physical_ashift;
4712 else
4713 vs->vs_physical_ashift = 0;
4716 * Report fragmentation and rebuild progress for top-level,
4717 * non-auxiliary, concrete devices.
4719 if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
4720 vdev_is_concrete(vd)) {
4722 * The vdev fragmentation rating doesn't take into
4723 * account the embedded slog metaslab (vdev_log_mg).
4724 * Since it's only one metaslab, it would have a tiny
4725 * impact on the overall fragmentation.
4727 vs->vs_fragmentation = (vd->vdev_mg != NULL) ?
4728 vd->vdev_mg->mg_fragmentation : 0;
4730 vs->vs_noalloc = MAX(vd->vdev_noalloc,
4731 tvd ? tvd->vdev_noalloc : 0);
4734 vdev_get_stats_ex_impl(vd, vs, vsx);
4735 mutex_exit(&vd->vdev_stat_lock);
4738 void
4739 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
4741 return (vdev_get_stats_ex(vd, vs, NULL));
4744 void
4745 vdev_clear_stats(vdev_t *vd)
4747 mutex_enter(&vd->vdev_stat_lock);
4748 vd->vdev_stat.vs_space = 0;
4749 vd->vdev_stat.vs_dspace = 0;
4750 vd->vdev_stat.vs_alloc = 0;
4751 mutex_exit(&vd->vdev_stat_lock);
4754 void
4755 vdev_scan_stat_init(vdev_t *vd)
4757 vdev_stat_t *vs = &vd->vdev_stat;
4759 for (int c = 0; c < vd->vdev_children; c++)
4760 vdev_scan_stat_init(vd->vdev_child[c]);
4762 mutex_enter(&vd->vdev_stat_lock);
4763 vs->vs_scan_processed = 0;
4764 mutex_exit(&vd->vdev_stat_lock);
4767 void
4768 vdev_stat_update(zio_t *zio, uint64_t psize)
4770 spa_t *spa = zio->io_spa;
4771 vdev_t *rvd = spa->spa_root_vdev;
4772 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
4773 vdev_t *pvd;
4774 uint64_t txg = zio->io_txg;
4775 /* Suppress ASAN false positive */
4776 #ifdef __SANITIZE_ADDRESS__
4777 vdev_stat_t *vs = vd ? &vd->vdev_stat : NULL;
4778 vdev_stat_ex_t *vsx = vd ? &vd->vdev_stat_ex : NULL;
4779 #else
4780 vdev_stat_t *vs = &vd->vdev_stat;
4781 vdev_stat_ex_t *vsx = &vd->vdev_stat_ex;
4782 #endif
4783 zio_type_t type = zio->io_type;
4784 int flags = zio->io_flags;
4787 * If this i/o is a gang leader, it didn't do any actual work.
4789 if (zio->io_gang_tree)
4790 return;
4792 if (zio->io_error == 0) {
4794 * If this is a root i/o, don't count it -- we've already
4795 * counted the top-level vdevs, and vdev_get_stats() will
4796 * aggregate them when asked. This reduces contention on
4797 * the root vdev_stat_lock and implicitly handles blocks
4798 * that compress away to holes, for which there is no i/o.
4799 * (Holes never create vdev children, so all the counters
4800 * remain zero, which is what we want.)
4802 * Note: this only applies to successful i/o (io_error == 0)
4803 * because unlike i/o counts, errors are not additive.
4804 * When reading a ditto block, for example, failure of
4805 * one top-level vdev does not imply a root-level error.
4807 if (vd == rvd)
4808 return;
4810 ASSERT(vd == zio->io_vd);
4812 if (flags & ZIO_FLAG_IO_BYPASS)
4813 return;
4815 mutex_enter(&vd->vdev_stat_lock);
4817 if (flags & ZIO_FLAG_IO_REPAIR) {
4819 * Repair is the result of a resilver issued by the
4820 * scan thread (spa_sync).
4822 if (flags & ZIO_FLAG_SCAN_THREAD) {
4823 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
4824 dsl_scan_phys_t *scn_phys = &scn->scn_phys;
4825 uint64_t *processed = &scn_phys->scn_processed;
4827 if (vd->vdev_ops->vdev_op_leaf)
4828 atomic_add_64(processed, psize);
4829 vs->vs_scan_processed += psize;
4833 * Repair is the result of a rebuild issued by the
4834 * rebuild thread (vdev_rebuild_thread). To avoid
4835 * double counting repaired bytes the virtual dRAID
4836 * spare vdev is excluded from the processed bytes.
4838 if (zio->io_priority == ZIO_PRIORITY_REBUILD) {
4839 vdev_t *tvd = vd->vdev_top;
4840 vdev_rebuild_t *vr = &tvd->vdev_rebuild_config;
4841 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
4842 uint64_t *rebuilt = &vrp->vrp_bytes_rebuilt;
4844 if (vd->vdev_ops->vdev_op_leaf &&
4845 vd->vdev_ops != &vdev_draid_spare_ops) {
4846 atomic_add_64(rebuilt, psize);
4848 vs->vs_rebuild_processed += psize;
4851 if (flags & ZIO_FLAG_SELF_HEAL)
4852 vs->vs_self_healed += psize;
4856 * The bytes/ops/histograms are recorded at the leaf level and
4857 * aggregated into the higher level vdevs in vdev_get_stats().
4859 if (vd->vdev_ops->vdev_op_leaf &&
4860 (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) {
4861 zio_type_t vs_type = type;
4862 zio_priority_t priority = zio->io_priority;
4865 * TRIM ops and bytes are reported to user space as
4866 * ZIO_TYPE_IOCTL. This is done to preserve the
4867 * vdev_stat_t structure layout for user space.
4869 if (type == ZIO_TYPE_TRIM)
4870 vs_type = ZIO_TYPE_IOCTL;
4873 * Solely for the purposes of 'zpool iostat -lqrw'
4874 * reporting use the priority to categorize the IO.
4875 * Only the following are reported to user space:
4877 * ZIO_PRIORITY_SYNC_READ,
4878 * ZIO_PRIORITY_SYNC_WRITE,
4879 * ZIO_PRIORITY_ASYNC_READ,
4880 * ZIO_PRIORITY_ASYNC_WRITE,
4881 * ZIO_PRIORITY_SCRUB,
4882 * ZIO_PRIORITY_TRIM,
4883 * ZIO_PRIORITY_REBUILD.
4885 if (priority == ZIO_PRIORITY_INITIALIZING) {
4886 ASSERT3U(type, ==, ZIO_TYPE_WRITE);
4887 priority = ZIO_PRIORITY_ASYNC_WRITE;
4888 } else if (priority == ZIO_PRIORITY_REMOVAL) {
4889 priority = ((type == ZIO_TYPE_WRITE) ?
4890 ZIO_PRIORITY_ASYNC_WRITE :
4891 ZIO_PRIORITY_ASYNC_READ);
4894 vs->vs_ops[vs_type]++;
4895 vs->vs_bytes[vs_type] += psize;
4897 if (flags & ZIO_FLAG_DELEGATED) {
4898 vsx->vsx_agg_histo[priority]
4899 [RQ_HISTO(zio->io_size)]++;
4900 } else {
4901 vsx->vsx_ind_histo[priority]
4902 [RQ_HISTO(zio->io_size)]++;
4905 if (zio->io_delta && zio->io_delay) {
4906 vsx->vsx_queue_histo[priority]
4907 [L_HISTO(zio->io_delta - zio->io_delay)]++;
4908 vsx->vsx_disk_histo[type]
4909 [L_HISTO(zio->io_delay)]++;
4910 vsx->vsx_total_histo[type]
4911 [L_HISTO(zio->io_delta)]++;
4915 mutex_exit(&vd->vdev_stat_lock);
4916 return;
4919 if (flags & ZIO_FLAG_SPECULATIVE)
4920 return;
4923 * If this is an I/O error that is going to be retried, then ignore the
4924 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
4925 * hard errors, when in reality they can happen for any number of
4926 * innocuous reasons (bus resets, MPxIO link failure, etc).
4928 if (zio->io_error == EIO &&
4929 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
4930 return;
4933 * Intent logs writes won't propagate their error to the root
4934 * I/O so don't mark these types of failures as pool-level
4935 * errors.
4937 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
4938 return;
4940 if (type == ZIO_TYPE_WRITE && txg != 0 &&
4941 (!(flags & ZIO_FLAG_IO_REPAIR) ||
4942 (flags & ZIO_FLAG_SCAN_THREAD) ||
4943 spa->spa_claiming)) {
4945 * This is either a normal write (not a repair), or it's
4946 * a repair induced by the scrub thread, or it's a repair
4947 * made by zil_claim() during spa_load() in the first txg.
4948 * In the normal case, we commit the DTL change in the same
4949 * txg as the block was born. In the scrub-induced repair
4950 * case, we know that scrubs run in first-pass syncing context,
4951 * so we commit the DTL change in spa_syncing_txg(spa).
4952 * In the zil_claim() case, we commit in spa_first_txg(spa).
4954 * We currently do not make DTL entries for failed spontaneous
4955 * self-healing writes triggered by normal (non-scrubbing)
4956 * reads, because we have no transactional context in which to
4957 * do so -- and it's not clear that it'd be desirable anyway.
4959 if (vd->vdev_ops->vdev_op_leaf) {
4960 uint64_t commit_txg = txg;
4961 if (flags & ZIO_FLAG_SCAN_THREAD) {
4962 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
4963 ASSERT(spa_sync_pass(spa) == 1);
4964 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
4965 commit_txg = spa_syncing_txg(spa);
4966 } else if (spa->spa_claiming) {
4967 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
4968 commit_txg = spa_first_txg(spa);
4970 ASSERT(commit_txg >= spa_syncing_txg(spa));
4971 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
4972 return;
4973 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
4974 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
4975 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
4977 if (vd != rvd)
4978 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
4982 int64_t
4983 vdev_deflated_space(vdev_t *vd, int64_t space)
4985 ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0);
4986 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
4988 return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio);
4992 * Update the in-core space usage stats for this vdev, its metaslab class,
4993 * and the root vdev.
4995 void
4996 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
4997 int64_t space_delta)
4999 (void) defer_delta;
5000 int64_t dspace_delta;
5001 spa_t *spa = vd->vdev_spa;
5002 vdev_t *rvd = spa->spa_root_vdev;
5004 ASSERT(vd == vd->vdev_top);
5007 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
5008 * factor. We must calculate this here and not at the root vdev
5009 * because the root vdev's psize-to-asize is simply the max of its
5010 * children's, thus not accurate enough for us.
5012 dspace_delta = vdev_deflated_space(vd, space_delta);
5014 mutex_enter(&vd->vdev_stat_lock);
5015 /* ensure we won't underflow */
5016 if (alloc_delta < 0) {
5017 ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta);
5020 vd->vdev_stat.vs_alloc += alloc_delta;
5021 vd->vdev_stat.vs_space += space_delta;
5022 vd->vdev_stat.vs_dspace += dspace_delta;
5023 mutex_exit(&vd->vdev_stat_lock);
5025 /* every class but log contributes to root space stats */
5026 if (vd->vdev_mg != NULL && !vd->vdev_islog) {
5027 ASSERT(!vd->vdev_isl2cache);
5028 mutex_enter(&rvd->vdev_stat_lock);
5029 rvd->vdev_stat.vs_alloc += alloc_delta;
5030 rvd->vdev_stat.vs_space += space_delta;
5031 rvd->vdev_stat.vs_dspace += dspace_delta;
5032 mutex_exit(&rvd->vdev_stat_lock);
5034 /* Note: metaslab_class_space_update moved to metaslab_space_update */
5038 * Mark a top-level vdev's config as dirty, placing it on the dirty list
5039 * so that it will be written out next time the vdev configuration is synced.
5040 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
5042 void
5043 vdev_config_dirty(vdev_t *vd)
5045 spa_t *spa = vd->vdev_spa;
5046 vdev_t *rvd = spa->spa_root_vdev;
5047 int c;
5049 ASSERT(spa_writeable(spa));
5052 * If this is an aux vdev (as with l2cache and spare devices), then we
5053 * update the vdev config manually and set the sync flag.
5055 if (vd->vdev_aux != NULL) {
5056 spa_aux_vdev_t *sav = vd->vdev_aux;
5057 nvlist_t **aux;
5058 uint_t naux;
5060 for (c = 0; c < sav->sav_count; c++) {
5061 if (sav->sav_vdevs[c] == vd)
5062 break;
5065 if (c == sav->sav_count) {
5067 * We're being removed. There's nothing more to do.
5069 ASSERT(sav->sav_sync == B_TRUE);
5070 return;
5073 sav->sav_sync = B_TRUE;
5075 if (nvlist_lookup_nvlist_array(sav->sav_config,
5076 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
5077 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
5078 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
5081 ASSERT(c < naux);
5084 * Setting the nvlist in the middle if the array is a little
5085 * sketchy, but it will work.
5087 nvlist_free(aux[c]);
5088 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
5090 return;
5094 * The dirty list is protected by the SCL_CONFIG lock. The caller
5095 * must either hold SCL_CONFIG as writer, or must be the sync thread
5096 * (which holds SCL_CONFIG as reader). There's only one sync thread,
5097 * so this is sufficient to ensure mutual exclusion.
5099 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
5100 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5101 spa_config_held(spa, SCL_CONFIG, RW_READER)));
5103 if (vd == rvd) {
5104 for (c = 0; c < rvd->vdev_children; c++)
5105 vdev_config_dirty(rvd->vdev_child[c]);
5106 } else {
5107 ASSERT(vd == vd->vdev_top);
5109 if (!list_link_active(&vd->vdev_config_dirty_node) &&
5110 vdev_is_concrete(vd)) {
5111 list_insert_head(&spa->spa_config_dirty_list, vd);
5116 void
5117 vdev_config_clean(vdev_t *vd)
5119 spa_t *spa = vd->vdev_spa;
5121 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
5122 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5123 spa_config_held(spa, SCL_CONFIG, RW_READER)));
5125 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
5126 list_remove(&spa->spa_config_dirty_list, vd);
5130 * Mark a top-level vdev's state as dirty, so that the next pass of
5131 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
5132 * the state changes from larger config changes because they require
5133 * much less locking, and are often needed for administrative actions.
5135 void
5136 vdev_state_dirty(vdev_t *vd)
5138 spa_t *spa = vd->vdev_spa;
5140 ASSERT(spa_writeable(spa));
5141 ASSERT(vd == vd->vdev_top);
5144 * The state list is protected by the SCL_STATE lock. The caller
5145 * must either hold SCL_STATE as writer, or must be the sync thread
5146 * (which holds SCL_STATE as reader). There's only one sync thread,
5147 * so this is sufficient to ensure mutual exclusion.
5149 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
5150 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5151 spa_config_held(spa, SCL_STATE, RW_READER)));
5153 if (!list_link_active(&vd->vdev_state_dirty_node) &&
5154 vdev_is_concrete(vd))
5155 list_insert_head(&spa->spa_state_dirty_list, vd);
5158 void
5159 vdev_state_clean(vdev_t *vd)
5161 spa_t *spa = vd->vdev_spa;
5163 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
5164 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5165 spa_config_held(spa, SCL_STATE, RW_READER)));
5167 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
5168 list_remove(&spa->spa_state_dirty_list, vd);
5172 * Propagate vdev state up from children to parent.
5174 void
5175 vdev_propagate_state(vdev_t *vd)
5177 spa_t *spa = vd->vdev_spa;
5178 vdev_t *rvd = spa->spa_root_vdev;
5179 int degraded = 0, faulted = 0;
5180 int corrupted = 0;
5181 vdev_t *child;
5183 if (vd->vdev_children > 0) {
5184 for (int c = 0; c < vd->vdev_children; c++) {
5185 child = vd->vdev_child[c];
5188 * Don't factor holes or indirect vdevs into the
5189 * decision.
5191 if (!vdev_is_concrete(child))
5192 continue;
5194 if (!vdev_readable(child) ||
5195 (!vdev_writeable(child) && spa_writeable(spa))) {
5197 * Root special: if there is a top-level log
5198 * device, treat the root vdev as if it were
5199 * degraded.
5201 if (child->vdev_islog && vd == rvd)
5202 degraded++;
5203 else
5204 faulted++;
5205 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
5206 degraded++;
5209 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
5210 corrupted++;
5213 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
5216 * Root special: if there is a top-level vdev that cannot be
5217 * opened due to corrupted metadata, then propagate the root
5218 * vdev's aux state as 'corrupt' rather than 'insufficient
5219 * replicas'.
5221 if (corrupted && vd == rvd &&
5222 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
5223 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
5224 VDEV_AUX_CORRUPT_DATA);
5227 if (vd->vdev_parent)
5228 vdev_propagate_state(vd->vdev_parent);
5232 * Set a vdev's state. If this is during an open, we don't update the parent
5233 * state, because we're in the process of opening children depth-first.
5234 * Otherwise, we propagate the change to the parent.
5236 * If this routine places a device in a faulted state, an appropriate ereport is
5237 * generated.
5239 void
5240 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
5242 uint64_t save_state;
5243 spa_t *spa = vd->vdev_spa;
5245 if (state == vd->vdev_state) {
5247 * Since vdev_offline() code path is already in an offline
5248 * state we can miss a statechange event to OFFLINE. Check
5249 * the previous state to catch this condition.
5251 if (vd->vdev_ops->vdev_op_leaf &&
5252 (state == VDEV_STATE_OFFLINE) &&
5253 (vd->vdev_prevstate >= VDEV_STATE_FAULTED)) {
5254 /* post an offline state change */
5255 zfs_post_state_change(spa, vd, vd->vdev_prevstate);
5257 vd->vdev_stat.vs_aux = aux;
5258 return;
5261 save_state = vd->vdev_state;
5263 vd->vdev_state = state;
5264 vd->vdev_stat.vs_aux = aux;
5267 * If we are setting the vdev state to anything but an open state, then
5268 * always close the underlying device unless the device has requested
5269 * a delayed close (i.e. we're about to remove or fault the device).
5270 * Otherwise, we keep accessible but invalid devices open forever.
5271 * We don't call vdev_close() itself, because that implies some extra
5272 * checks (offline, etc) that we don't want here. This is limited to
5273 * leaf devices, because otherwise closing the device will affect other
5274 * children.
5276 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
5277 vd->vdev_ops->vdev_op_leaf)
5278 vd->vdev_ops->vdev_op_close(vd);
5280 if (vd->vdev_removed &&
5281 state == VDEV_STATE_CANT_OPEN &&
5282 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
5284 * If the previous state is set to VDEV_STATE_REMOVED, then this
5285 * device was previously marked removed and someone attempted to
5286 * reopen it. If this failed due to a nonexistent device, then
5287 * keep the device in the REMOVED state. We also let this be if
5288 * it is one of our special test online cases, which is only
5289 * attempting to online the device and shouldn't generate an FMA
5290 * fault.
5292 vd->vdev_state = VDEV_STATE_REMOVED;
5293 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
5294 } else if (state == VDEV_STATE_REMOVED) {
5295 vd->vdev_removed = B_TRUE;
5296 } else if (state == VDEV_STATE_CANT_OPEN) {
5298 * If we fail to open a vdev during an import or recovery, we
5299 * mark it as "not available", which signifies that it was
5300 * never there to begin with. Failure to open such a device
5301 * is not considered an error.
5303 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
5304 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
5305 vd->vdev_ops->vdev_op_leaf)
5306 vd->vdev_not_present = 1;
5309 * Post the appropriate ereport. If the 'prevstate' field is
5310 * set to something other than VDEV_STATE_UNKNOWN, it indicates
5311 * that this is part of a vdev_reopen(). In this case, we don't
5312 * want to post the ereport if the device was already in the
5313 * CANT_OPEN state beforehand.
5315 * If the 'checkremove' flag is set, then this is an attempt to
5316 * online the device in response to an insertion event. If we
5317 * hit this case, then we have detected an insertion event for a
5318 * faulted or offline device that wasn't in the removed state.
5319 * In this scenario, we don't post an ereport because we are
5320 * about to replace the device, or attempt an online with
5321 * vdev_forcefault, which will generate the fault for us.
5323 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
5324 !vd->vdev_not_present && !vd->vdev_checkremove &&
5325 vd != spa->spa_root_vdev) {
5326 const char *class;
5328 switch (aux) {
5329 case VDEV_AUX_OPEN_FAILED:
5330 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
5331 break;
5332 case VDEV_AUX_CORRUPT_DATA:
5333 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
5334 break;
5335 case VDEV_AUX_NO_REPLICAS:
5336 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
5337 break;
5338 case VDEV_AUX_BAD_GUID_SUM:
5339 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
5340 break;
5341 case VDEV_AUX_TOO_SMALL:
5342 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
5343 break;
5344 case VDEV_AUX_BAD_LABEL:
5345 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
5346 break;
5347 case VDEV_AUX_BAD_ASHIFT:
5348 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT;
5349 break;
5350 default:
5351 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
5354 (void) zfs_ereport_post(class, spa, vd, NULL, NULL,
5355 save_state);
5358 /* Erase any notion of persistent removed state */
5359 vd->vdev_removed = B_FALSE;
5360 } else {
5361 vd->vdev_removed = B_FALSE;
5365 * Notify ZED of any significant state-change on a leaf vdev.
5368 if (vd->vdev_ops->vdev_op_leaf) {
5369 /* preserve original state from a vdev_reopen() */
5370 if ((vd->vdev_prevstate != VDEV_STATE_UNKNOWN) &&
5371 (vd->vdev_prevstate != vd->vdev_state) &&
5372 (save_state <= VDEV_STATE_CLOSED))
5373 save_state = vd->vdev_prevstate;
5375 /* filter out state change due to initial vdev_open */
5376 if (save_state > VDEV_STATE_CLOSED)
5377 zfs_post_state_change(spa, vd, save_state);
5380 if (!isopen && vd->vdev_parent)
5381 vdev_propagate_state(vd->vdev_parent);
5384 boolean_t
5385 vdev_children_are_offline(vdev_t *vd)
5387 ASSERT(!vd->vdev_ops->vdev_op_leaf);
5389 for (uint64_t i = 0; i < vd->vdev_children; i++) {
5390 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
5391 return (B_FALSE);
5394 return (B_TRUE);
5398 * Check the vdev configuration to ensure that it's capable of supporting
5399 * a root pool. We do not support partial configuration.
5401 boolean_t
5402 vdev_is_bootable(vdev_t *vd)
5404 if (!vd->vdev_ops->vdev_op_leaf) {
5405 const char *vdev_type = vd->vdev_ops->vdev_op_type;
5407 if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0)
5408 return (B_FALSE);
5411 for (int c = 0; c < vd->vdev_children; c++) {
5412 if (!vdev_is_bootable(vd->vdev_child[c]))
5413 return (B_FALSE);
5415 return (B_TRUE);
5418 boolean_t
5419 vdev_is_concrete(vdev_t *vd)
5421 vdev_ops_t *ops = vd->vdev_ops;
5422 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
5423 ops == &vdev_missing_ops || ops == &vdev_root_ops) {
5424 return (B_FALSE);
5425 } else {
5426 return (B_TRUE);
5431 * Determine if a log device has valid content. If the vdev was
5432 * removed or faulted in the MOS config then we know that
5433 * the content on the log device has already been written to the pool.
5435 boolean_t
5436 vdev_log_state_valid(vdev_t *vd)
5438 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
5439 !vd->vdev_removed)
5440 return (B_TRUE);
5442 for (int c = 0; c < vd->vdev_children; c++)
5443 if (vdev_log_state_valid(vd->vdev_child[c]))
5444 return (B_TRUE);
5446 return (B_FALSE);
5450 * Expand a vdev if possible.
5452 void
5453 vdev_expand(vdev_t *vd, uint64_t txg)
5455 ASSERT(vd->vdev_top == vd);
5456 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5457 ASSERT(vdev_is_concrete(vd));
5459 vdev_set_deflate_ratio(vd);
5461 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
5462 vdev_is_concrete(vd)) {
5463 vdev_metaslab_group_create(vd);
5464 VERIFY(vdev_metaslab_init(vd, txg) == 0);
5465 vdev_config_dirty(vd);
5470 * Split a vdev.
5472 void
5473 vdev_split(vdev_t *vd)
5475 vdev_t *cvd, *pvd = vd->vdev_parent;
5477 VERIFY3U(pvd->vdev_children, >, 1);
5479 vdev_remove_child(pvd, vd);
5480 vdev_compact_children(pvd);
5482 ASSERT3P(pvd->vdev_child, !=, NULL);
5484 cvd = pvd->vdev_child[0];
5485 if (pvd->vdev_children == 1) {
5486 vdev_remove_parent(cvd);
5487 cvd->vdev_splitting = B_TRUE;
5489 vdev_propagate_state(cvd);
5492 void
5493 vdev_deadman(vdev_t *vd, const char *tag)
5495 for (int c = 0; c < vd->vdev_children; c++) {
5496 vdev_t *cvd = vd->vdev_child[c];
5498 vdev_deadman(cvd, tag);
5501 if (vd->vdev_ops->vdev_op_leaf) {
5502 vdev_queue_t *vq = &vd->vdev_queue;
5504 mutex_enter(&vq->vq_lock);
5505 if (vq->vq_active > 0) {
5506 spa_t *spa = vd->vdev_spa;
5507 zio_t *fio;
5508 uint64_t delta;
5510 zfs_dbgmsg("slow vdev: %s has %u active IOs",
5511 vd->vdev_path, vq->vq_active);
5514 * Look at the head of all the pending queues,
5515 * if any I/O has been outstanding for longer than
5516 * the spa_deadman_synctime invoke the deadman logic.
5518 fio = list_head(&vq->vq_active_list);
5519 delta = gethrtime() - fio->io_timestamp;
5520 if (delta > spa_deadman_synctime(spa))
5521 zio_deadman(fio, tag);
5523 mutex_exit(&vq->vq_lock);
5527 void
5528 vdev_defer_resilver(vdev_t *vd)
5530 ASSERT(vd->vdev_ops->vdev_op_leaf);
5532 vd->vdev_resilver_deferred = B_TRUE;
5533 vd->vdev_spa->spa_resilver_deferred = B_TRUE;
5537 * Clears the resilver deferred flag on all leaf devs under vd. Returns
5538 * B_TRUE if we have devices that need to be resilvered and are available to
5539 * accept resilver I/Os.
5541 boolean_t
5542 vdev_clear_resilver_deferred(vdev_t *vd, dmu_tx_t *tx)
5544 boolean_t resilver_needed = B_FALSE;
5545 spa_t *spa = vd->vdev_spa;
5547 for (int c = 0; c < vd->vdev_children; c++) {
5548 vdev_t *cvd = vd->vdev_child[c];
5549 resilver_needed |= vdev_clear_resilver_deferred(cvd, tx);
5552 if (vd == spa->spa_root_vdev &&
5553 spa_feature_is_active(spa, SPA_FEATURE_RESILVER_DEFER)) {
5554 spa_feature_decr(spa, SPA_FEATURE_RESILVER_DEFER, tx);
5555 vdev_config_dirty(vd);
5556 spa->spa_resilver_deferred = B_FALSE;
5557 return (resilver_needed);
5560 if (!vdev_is_concrete(vd) || vd->vdev_aux ||
5561 !vd->vdev_ops->vdev_op_leaf)
5562 return (resilver_needed);
5564 vd->vdev_resilver_deferred = B_FALSE;
5566 return (!vdev_is_dead(vd) && !vd->vdev_offline &&
5567 vdev_resilver_needed(vd, NULL, NULL));
5570 boolean_t
5571 vdev_xlate_is_empty(range_seg64_t *rs)
5573 return (rs->rs_start == rs->rs_end);
5577 * Translate a logical range to the first contiguous physical range for the
5578 * specified vdev_t. This function is initially called with a leaf vdev and
5579 * will walk each parent vdev until it reaches a top-level vdev. Once the
5580 * top-level is reached the physical range is initialized and the recursive
5581 * function begins to unwind. As it unwinds it calls the parent's vdev
5582 * specific translation function to do the real conversion.
5584 void
5585 vdev_xlate(vdev_t *vd, const range_seg64_t *logical_rs,
5586 range_seg64_t *physical_rs, range_seg64_t *remain_rs)
5589 * Walk up the vdev tree
5591 if (vd != vd->vdev_top) {
5592 vdev_xlate(vd->vdev_parent, logical_rs, physical_rs,
5593 remain_rs);
5594 } else {
5596 * We've reached the top-level vdev, initialize the physical
5597 * range to the logical range and set an empty remaining
5598 * range then start to unwind.
5600 physical_rs->rs_start = logical_rs->rs_start;
5601 physical_rs->rs_end = logical_rs->rs_end;
5603 remain_rs->rs_start = logical_rs->rs_start;
5604 remain_rs->rs_end = logical_rs->rs_start;
5606 return;
5609 vdev_t *pvd = vd->vdev_parent;
5610 ASSERT3P(pvd, !=, NULL);
5611 ASSERT3P(pvd->vdev_ops->vdev_op_xlate, !=, NULL);
5614 * As this recursive function unwinds, translate the logical
5615 * range into its physical and any remaining components by calling
5616 * the vdev specific translate function.
5618 range_seg64_t intermediate = { 0 };
5619 pvd->vdev_ops->vdev_op_xlate(vd, physical_rs, &intermediate, remain_rs);
5621 physical_rs->rs_start = intermediate.rs_start;
5622 physical_rs->rs_end = intermediate.rs_end;
5625 void
5626 vdev_xlate_walk(vdev_t *vd, const range_seg64_t *logical_rs,
5627 vdev_xlate_func_t *func, void *arg)
5629 range_seg64_t iter_rs = *logical_rs;
5630 range_seg64_t physical_rs;
5631 range_seg64_t remain_rs;
5633 while (!vdev_xlate_is_empty(&iter_rs)) {
5635 vdev_xlate(vd, &iter_rs, &physical_rs, &remain_rs);
5638 * With raidz and dRAID, it's possible that the logical range
5639 * does not live on this leaf vdev. Only when there is a non-
5640 * zero physical size call the provided function.
5642 if (!vdev_xlate_is_empty(&physical_rs))
5643 func(arg, &physical_rs);
5645 iter_rs = remain_rs;
5649 static char *
5650 vdev_name(vdev_t *vd, char *buf, int buflen)
5652 if (vd->vdev_path == NULL) {
5653 if (strcmp(vd->vdev_ops->vdev_op_type, "root") == 0) {
5654 strlcpy(buf, vd->vdev_spa->spa_name, buflen);
5655 } else if (!vd->vdev_ops->vdev_op_leaf) {
5656 snprintf(buf, buflen, "%s-%llu",
5657 vd->vdev_ops->vdev_op_type,
5658 (u_longlong_t)vd->vdev_id);
5660 } else {
5661 strlcpy(buf, vd->vdev_path, buflen);
5663 return (buf);
5667 * Look at the vdev tree and determine whether any devices are currently being
5668 * replaced.
5670 boolean_t
5671 vdev_replace_in_progress(vdev_t *vdev)
5673 ASSERT(spa_config_held(vdev->vdev_spa, SCL_ALL, RW_READER) != 0);
5675 if (vdev->vdev_ops == &vdev_replacing_ops)
5676 return (B_TRUE);
5679 * A 'spare' vdev indicates that we have a replace in progress, unless
5680 * it has exactly two children, and the second, the hot spare, has
5681 * finished being resilvered.
5683 if (vdev->vdev_ops == &vdev_spare_ops && (vdev->vdev_children > 2 ||
5684 !vdev_dtl_empty(vdev->vdev_child[1], DTL_MISSING)))
5685 return (B_TRUE);
5687 for (int i = 0; i < vdev->vdev_children; i++) {
5688 if (vdev_replace_in_progress(vdev->vdev_child[i]))
5689 return (B_TRUE);
5692 return (B_FALSE);
5696 * Add a (source=src, propname=propval) list to an nvlist.
5698 static void
5699 vdev_prop_add_list(nvlist_t *nvl, const char *propname, const char *strval,
5700 uint64_t intval, zprop_source_t src)
5702 nvlist_t *propval;
5704 propval = fnvlist_alloc();
5705 fnvlist_add_uint64(propval, ZPROP_SOURCE, src);
5707 if (strval != NULL)
5708 fnvlist_add_string(propval, ZPROP_VALUE, strval);
5709 else
5710 fnvlist_add_uint64(propval, ZPROP_VALUE, intval);
5712 fnvlist_add_nvlist(nvl, propname, propval);
5713 nvlist_free(propval);
5716 static void
5717 vdev_props_set_sync(void *arg, dmu_tx_t *tx)
5719 vdev_t *vd;
5720 nvlist_t *nvp = arg;
5721 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
5722 objset_t *mos = spa->spa_meta_objset;
5723 nvpair_t *elem = NULL;
5724 uint64_t vdev_guid;
5725 uint64_t objid;
5726 nvlist_t *nvprops;
5728 vdev_guid = fnvlist_lookup_uint64(nvp, ZPOOL_VDEV_PROPS_SET_VDEV);
5729 nvprops = fnvlist_lookup_nvlist(nvp, ZPOOL_VDEV_PROPS_SET_PROPS);
5730 vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE);
5732 /* this vdev could get removed while waiting for this sync task */
5733 if (vd == NULL)
5734 return;
5737 * Set vdev property values in the vdev props mos object.
5739 if (vd->vdev_root_zap != 0) {
5740 objid = vd->vdev_root_zap;
5741 } else if (vd->vdev_top_zap != 0) {
5742 objid = vd->vdev_top_zap;
5743 } else if (vd->vdev_leaf_zap != 0) {
5744 objid = vd->vdev_leaf_zap;
5745 } else {
5746 panic("unexpected vdev type");
5749 mutex_enter(&spa->spa_props_lock);
5751 while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
5752 uint64_t intval;
5753 const char *strval;
5754 vdev_prop_t prop;
5755 const char *propname = nvpair_name(elem);
5756 zprop_type_t proptype;
5758 switch (prop = vdev_name_to_prop(propname)) {
5759 case VDEV_PROP_USERPROP:
5760 if (vdev_prop_user(propname)) {
5761 strval = fnvpair_value_string(elem);
5762 if (strlen(strval) == 0) {
5763 /* remove the property if value == "" */
5764 (void) zap_remove(mos, objid, propname,
5765 tx);
5766 } else {
5767 VERIFY0(zap_update(mos, objid, propname,
5768 1, strlen(strval) + 1, strval, tx));
5770 spa_history_log_internal(spa, "vdev set", tx,
5771 "vdev_guid=%llu: %s=%s",
5772 (u_longlong_t)vdev_guid, nvpair_name(elem),
5773 strval);
5775 break;
5776 default:
5777 /* normalize the property name */
5778 propname = vdev_prop_to_name(prop);
5779 proptype = vdev_prop_get_type(prop);
5781 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5782 ASSERT(proptype == PROP_TYPE_STRING);
5783 strval = fnvpair_value_string(elem);
5784 VERIFY0(zap_update(mos, objid, propname,
5785 1, strlen(strval) + 1, strval, tx));
5786 spa_history_log_internal(spa, "vdev set", tx,
5787 "vdev_guid=%llu: %s=%s",
5788 (u_longlong_t)vdev_guid, nvpair_name(elem),
5789 strval);
5790 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5791 intval = fnvpair_value_uint64(elem);
5793 if (proptype == PROP_TYPE_INDEX) {
5794 const char *unused;
5795 VERIFY0(vdev_prop_index_to_string(
5796 prop, intval, &unused));
5798 VERIFY0(zap_update(mos, objid, propname,
5799 sizeof (uint64_t), 1, &intval, tx));
5800 spa_history_log_internal(spa, "vdev set", tx,
5801 "vdev_guid=%llu: %s=%lld",
5802 (u_longlong_t)vdev_guid,
5803 nvpair_name(elem), (longlong_t)intval);
5804 } else {
5805 panic("invalid vdev property type %u",
5806 nvpair_type(elem));
5812 mutex_exit(&spa->spa_props_lock);
5816 vdev_prop_set(vdev_t *vd, nvlist_t *innvl, nvlist_t *outnvl)
5818 spa_t *spa = vd->vdev_spa;
5819 nvpair_t *elem = NULL;
5820 uint64_t vdev_guid;
5821 nvlist_t *nvprops;
5822 int error = 0;
5824 ASSERT(vd != NULL);
5826 /* Check that vdev has a zap we can use */
5827 if (vd->vdev_root_zap == 0 &&
5828 vd->vdev_top_zap == 0 &&
5829 vd->vdev_leaf_zap == 0)
5830 return (SET_ERROR(EINVAL));
5832 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_SET_VDEV,
5833 &vdev_guid) != 0)
5834 return (SET_ERROR(EINVAL));
5836 if (nvlist_lookup_nvlist(innvl, ZPOOL_VDEV_PROPS_SET_PROPS,
5837 &nvprops) != 0)
5838 return (SET_ERROR(EINVAL));
5840 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL)
5841 return (SET_ERROR(EINVAL));
5843 while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
5844 const char *propname = nvpair_name(elem);
5845 vdev_prop_t prop = vdev_name_to_prop(propname);
5846 uint64_t intval = 0;
5847 const char *strval = NULL;
5849 if (prop == VDEV_PROP_USERPROP && !vdev_prop_user(propname)) {
5850 error = EINVAL;
5851 goto end;
5854 if (vdev_prop_readonly(prop)) {
5855 error = EROFS;
5856 goto end;
5859 /* Special Processing */
5860 switch (prop) {
5861 case VDEV_PROP_PATH:
5862 if (vd->vdev_path == NULL) {
5863 error = EROFS;
5864 break;
5866 if (nvpair_value_string(elem, &strval) != 0) {
5867 error = EINVAL;
5868 break;
5870 /* New path must start with /dev/ */
5871 if (strncmp(strval, "/dev/", 5)) {
5872 error = EINVAL;
5873 break;
5875 error = spa_vdev_setpath(spa, vdev_guid, strval);
5876 break;
5877 case VDEV_PROP_ALLOCATING:
5878 if (nvpair_value_uint64(elem, &intval) != 0) {
5879 error = EINVAL;
5880 break;
5882 if (intval != vd->vdev_noalloc)
5883 break;
5884 if (intval == 0)
5885 error = spa_vdev_noalloc(spa, vdev_guid);
5886 else
5887 error = spa_vdev_alloc(spa, vdev_guid);
5888 break;
5889 case VDEV_PROP_FAILFAST:
5890 if (nvpair_value_uint64(elem, &intval) != 0) {
5891 error = EINVAL;
5892 break;
5894 vd->vdev_failfast = intval & 1;
5895 break;
5896 case VDEV_PROP_CHECKSUM_N:
5897 if (nvpair_value_uint64(elem, &intval) != 0) {
5898 error = EINVAL;
5899 break;
5901 vd->vdev_checksum_n = intval;
5902 break;
5903 case VDEV_PROP_CHECKSUM_T:
5904 if (nvpair_value_uint64(elem, &intval) != 0) {
5905 error = EINVAL;
5906 break;
5908 vd->vdev_checksum_t = intval;
5909 break;
5910 case VDEV_PROP_IO_N:
5911 if (nvpair_value_uint64(elem, &intval) != 0) {
5912 error = EINVAL;
5913 break;
5915 vd->vdev_io_n = intval;
5916 break;
5917 case VDEV_PROP_IO_T:
5918 if (nvpair_value_uint64(elem, &intval) != 0) {
5919 error = EINVAL;
5920 break;
5922 vd->vdev_io_t = intval;
5923 break;
5924 default:
5925 /* Most processing is done in vdev_props_set_sync */
5926 break;
5928 end:
5929 if (error != 0) {
5930 intval = error;
5931 vdev_prop_add_list(outnvl, propname, strval, intval, 0);
5932 return (error);
5936 return (dsl_sync_task(spa->spa_name, NULL, vdev_props_set_sync,
5937 innvl, 6, ZFS_SPACE_CHECK_EXTRA_RESERVED));
5941 vdev_prop_get(vdev_t *vd, nvlist_t *innvl, nvlist_t *outnvl)
5943 spa_t *spa = vd->vdev_spa;
5944 objset_t *mos = spa->spa_meta_objset;
5945 int err = 0;
5946 uint64_t objid;
5947 uint64_t vdev_guid;
5948 nvpair_t *elem = NULL;
5949 nvlist_t *nvprops = NULL;
5950 uint64_t intval = 0;
5951 char *strval = NULL;
5952 const char *propname = NULL;
5953 vdev_prop_t prop;
5955 ASSERT(vd != NULL);
5956 ASSERT(mos != NULL);
5958 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_GET_VDEV,
5959 &vdev_guid) != 0)
5960 return (SET_ERROR(EINVAL));
5962 nvlist_lookup_nvlist(innvl, ZPOOL_VDEV_PROPS_GET_PROPS, &nvprops);
5964 if (vd->vdev_root_zap != 0) {
5965 objid = vd->vdev_root_zap;
5966 } else if (vd->vdev_top_zap != 0) {
5967 objid = vd->vdev_top_zap;
5968 } else if (vd->vdev_leaf_zap != 0) {
5969 objid = vd->vdev_leaf_zap;
5970 } else {
5971 return (SET_ERROR(EINVAL));
5973 ASSERT(objid != 0);
5975 mutex_enter(&spa->spa_props_lock);
5977 if (nvprops != NULL) {
5978 char namebuf[64] = { 0 };
5980 while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
5981 intval = 0;
5982 strval = NULL;
5983 propname = nvpair_name(elem);
5984 prop = vdev_name_to_prop(propname);
5985 zprop_source_t src = ZPROP_SRC_DEFAULT;
5986 uint64_t integer_size, num_integers;
5988 switch (prop) {
5989 /* Special Read-only Properties */
5990 case VDEV_PROP_NAME:
5991 strval = vdev_name(vd, namebuf,
5992 sizeof (namebuf));
5993 if (strval == NULL)
5994 continue;
5995 vdev_prop_add_list(outnvl, propname, strval, 0,
5996 ZPROP_SRC_NONE);
5997 continue;
5998 case VDEV_PROP_CAPACITY:
5999 /* percent used */
6000 intval = (vd->vdev_stat.vs_dspace == 0) ? 0 :
6001 (vd->vdev_stat.vs_alloc * 100 /
6002 vd->vdev_stat.vs_dspace);
6003 vdev_prop_add_list(outnvl, propname, NULL,
6004 intval, ZPROP_SRC_NONE);
6005 continue;
6006 case VDEV_PROP_STATE:
6007 vdev_prop_add_list(outnvl, propname, NULL,
6008 vd->vdev_state, ZPROP_SRC_NONE);
6009 continue;
6010 case VDEV_PROP_GUID:
6011 vdev_prop_add_list(outnvl, propname, NULL,
6012 vd->vdev_guid, ZPROP_SRC_NONE);
6013 continue;
6014 case VDEV_PROP_ASIZE:
6015 vdev_prop_add_list(outnvl, propname, NULL,
6016 vd->vdev_asize, ZPROP_SRC_NONE);
6017 continue;
6018 case VDEV_PROP_PSIZE:
6019 vdev_prop_add_list(outnvl, propname, NULL,
6020 vd->vdev_psize, ZPROP_SRC_NONE);
6021 continue;
6022 case VDEV_PROP_ASHIFT:
6023 vdev_prop_add_list(outnvl, propname, NULL,
6024 vd->vdev_ashift, ZPROP_SRC_NONE);
6025 continue;
6026 case VDEV_PROP_SIZE:
6027 vdev_prop_add_list(outnvl, propname, NULL,
6028 vd->vdev_stat.vs_dspace, ZPROP_SRC_NONE);
6029 continue;
6030 case VDEV_PROP_FREE:
6031 vdev_prop_add_list(outnvl, propname, NULL,
6032 vd->vdev_stat.vs_dspace -
6033 vd->vdev_stat.vs_alloc, ZPROP_SRC_NONE);
6034 continue;
6035 case VDEV_PROP_ALLOCATED:
6036 vdev_prop_add_list(outnvl, propname, NULL,
6037 vd->vdev_stat.vs_alloc, ZPROP_SRC_NONE);
6038 continue;
6039 case VDEV_PROP_EXPANDSZ:
6040 vdev_prop_add_list(outnvl, propname, NULL,
6041 vd->vdev_stat.vs_esize, ZPROP_SRC_NONE);
6042 continue;
6043 case VDEV_PROP_FRAGMENTATION:
6044 vdev_prop_add_list(outnvl, propname, NULL,
6045 vd->vdev_stat.vs_fragmentation,
6046 ZPROP_SRC_NONE);
6047 continue;
6048 case VDEV_PROP_PARITY:
6049 vdev_prop_add_list(outnvl, propname, NULL,
6050 vdev_get_nparity(vd), ZPROP_SRC_NONE);
6051 continue;
6052 case VDEV_PROP_PATH:
6053 if (vd->vdev_path == NULL)
6054 continue;
6055 vdev_prop_add_list(outnvl, propname,
6056 vd->vdev_path, 0, ZPROP_SRC_NONE);
6057 continue;
6058 case VDEV_PROP_DEVID:
6059 if (vd->vdev_devid == NULL)
6060 continue;
6061 vdev_prop_add_list(outnvl, propname,
6062 vd->vdev_devid, 0, ZPROP_SRC_NONE);
6063 continue;
6064 case VDEV_PROP_PHYS_PATH:
6065 if (vd->vdev_physpath == NULL)
6066 continue;
6067 vdev_prop_add_list(outnvl, propname,
6068 vd->vdev_physpath, 0, ZPROP_SRC_NONE);
6069 continue;
6070 case VDEV_PROP_ENC_PATH:
6071 if (vd->vdev_enc_sysfs_path == NULL)
6072 continue;
6073 vdev_prop_add_list(outnvl, propname,
6074 vd->vdev_enc_sysfs_path, 0, ZPROP_SRC_NONE);
6075 continue;
6076 case VDEV_PROP_FRU:
6077 if (vd->vdev_fru == NULL)
6078 continue;
6079 vdev_prop_add_list(outnvl, propname,
6080 vd->vdev_fru, 0, ZPROP_SRC_NONE);
6081 continue;
6082 case VDEV_PROP_PARENT:
6083 if (vd->vdev_parent != NULL) {
6084 strval = vdev_name(vd->vdev_parent,
6085 namebuf, sizeof (namebuf));
6086 vdev_prop_add_list(outnvl, propname,
6087 strval, 0, ZPROP_SRC_NONE);
6089 continue;
6090 case VDEV_PROP_CHILDREN:
6091 if (vd->vdev_children > 0)
6092 strval = kmem_zalloc(ZAP_MAXVALUELEN,
6093 KM_SLEEP);
6094 for (uint64_t i = 0; i < vd->vdev_children;
6095 i++) {
6096 const char *vname;
6098 vname = vdev_name(vd->vdev_child[i],
6099 namebuf, sizeof (namebuf));
6100 if (vname == NULL)
6101 vname = "(unknown)";
6102 if (strlen(strval) > 0)
6103 strlcat(strval, ",",
6104 ZAP_MAXVALUELEN);
6105 strlcat(strval, vname, ZAP_MAXVALUELEN);
6107 if (strval != NULL) {
6108 vdev_prop_add_list(outnvl, propname,
6109 strval, 0, ZPROP_SRC_NONE);
6110 kmem_free(strval, ZAP_MAXVALUELEN);
6112 continue;
6113 case VDEV_PROP_NUMCHILDREN:
6114 vdev_prop_add_list(outnvl, propname, NULL,
6115 vd->vdev_children, ZPROP_SRC_NONE);
6116 continue;
6117 case VDEV_PROP_READ_ERRORS:
6118 vdev_prop_add_list(outnvl, propname, NULL,
6119 vd->vdev_stat.vs_read_errors,
6120 ZPROP_SRC_NONE);
6121 continue;
6122 case VDEV_PROP_WRITE_ERRORS:
6123 vdev_prop_add_list(outnvl, propname, NULL,
6124 vd->vdev_stat.vs_write_errors,
6125 ZPROP_SRC_NONE);
6126 continue;
6127 case VDEV_PROP_CHECKSUM_ERRORS:
6128 vdev_prop_add_list(outnvl, propname, NULL,
6129 vd->vdev_stat.vs_checksum_errors,
6130 ZPROP_SRC_NONE);
6131 continue;
6132 case VDEV_PROP_INITIALIZE_ERRORS:
6133 vdev_prop_add_list(outnvl, propname, NULL,
6134 vd->vdev_stat.vs_initialize_errors,
6135 ZPROP_SRC_NONE);
6136 continue;
6137 case VDEV_PROP_OPS_NULL:
6138 vdev_prop_add_list(outnvl, propname, NULL,
6139 vd->vdev_stat.vs_ops[ZIO_TYPE_NULL],
6140 ZPROP_SRC_NONE);
6141 continue;
6142 case VDEV_PROP_OPS_READ:
6143 vdev_prop_add_list(outnvl, propname, NULL,
6144 vd->vdev_stat.vs_ops[ZIO_TYPE_READ],
6145 ZPROP_SRC_NONE);
6146 continue;
6147 case VDEV_PROP_OPS_WRITE:
6148 vdev_prop_add_list(outnvl, propname, NULL,
6149 vd->vdev_stat.vs_ops[ZIO_TYPE_WRITE],
6150 ZPROP_SRC_NONE);
6151 continue;
6152 case VDEV_PROP_OPS_FREE:
6153 vdev_prop_add_list(outnvl, propname, NULL,
6154 vd->vdev_stat.vs_ops[ZIO_TYPE_FREE],
6155 ZPROP_SRC_NONE);
6156 continue;
6157 case VDEV_PROP_OPS_CLAIM:
6158 vdev_prop_add_list(outnvl, propname, NULL,
6159 vd->vdev_stat.vs_ops[ZIO_TYPE_CLAIM],
6160 ZPROP_SRC_NONE);
6161 continue;
6162 case VDEV_PROP_OPS_TRIM:
6164 * TRIM ops and bytes are reported to user
6165 * space as ZIO_TYPE_IOCTL. This is done to
6166 * preserve the vdev_stat_t structure layout
6167 * for user space.
6169 vdev_prop_add_list(outnvl, propname, NULL,
6170 vd->vdev_stat.vs_ops[ZIO_TYPE_IOCTL],
6171 ZPROP_SRC_NONE);
6172 continue;
6173 case VDEV_PROP_BYTES_NULL:
6174 vdev_prop_add_list(outnvl, propname, NULL,
6175 vd->vdev_stat.vs_bytes[ZIO_TYPE_NULL],
6176 ZPROP_SRC_NONE);
6177 continue;
6178 case VDEV_PROP_BYTES_READ:
6179 vdev_prop_add_list(outnvl, propname, NULL,
6180 vd->vdev_stat.vs_bytes[ZIO_TYPE_READ],
6181 ZPROP_SRC_NONE);
6182 continue;
6183 case VDEV_PROP_BYTES_WRITE:
6184 vdev_prop_add_list(outnvl, propname, NULL,
6185 vd->vdev_stat.vs_bytes[ZIO_TYPE_WRITE],
6186 ZPROP_SRC_NONE);
6187 continue;
6188 case VDEV_PROP_BYTES_FREE:
6189 vdev_prop_add_list(outnvl, propname, NULL,
6190 vd->vdev_stat.vs_bytes[ZIO_TYPE_FREE],
6191 ZPROP_SRC_NONE);
6192 continue;
6193 case VDEV_PROP_BYTES_CLAIM:
6194 vdev_prop_add_list(outnvl, propname, NULL,
6195 vd->vdev_stat.vs_bytes[ZIO_TYPE_CLAIM],
6196 ZPROP_SRC_NONE);
6197 continue;
6198 case VDEV_PROP_BYTES_TRIM:
6200 * TRIM ops and bytes are reported to user
6201 * space as ZIO_TYPE_IOCTL. This is done to
6202 * preserve the vdev_stat_t structure layout
6203 * for user space.
6205 vdev_prop_add_list(outnvl, propname, NULL,
6206 vd->vdev_stat.vs_bytes[ZIO_TYPE_IOCTL],
6207 ZPROP_SRC_NONE);
6208 continue;
6209 case VDEV_PROP_REMOVING:
6210 vdev_prop_add_list(outnvl, propname, NULL,
6211 vd->vdev_removing, ZPROP_SRC_NONE);
6212 continue;
6213 /* Numeric Properites */
6214 case VDEV_PROP_ALLOCATING:
6215 /* Leaf vdevs cannot have this property */
6216 if (vd->vdev_mg == NULL &&
6217 vd->vdev_top != NULL) {
6218 src = ZPROP_SRC_NONE;
6219 intval = ZPROP_BOOLEAN_NA;
6220 } else {
6221 err = vdev_prop_get_int(vd, prop,
6222 &intval);
6223 if (err && err != ENOENT)
6224 break;
6226 if (intval ==
6227 vdev_prop_default_numeric(prop))
6228 src = ZPROP_SRC_DEFAULT;
6229 else
6230 src = ZPROP_SRC_LOCAL;
6233 vdev_prop_add_list(outnvl, propname, NULL,
6234 intval, src);
6235 break;
6236 case VDEV_PROP_FAILFAST:
6237 src = ZPROP_SRC_LOCAL;
6238 strval = NULL;
6240 err = zap_lookup(mos, objid, nvpair_name(elem),
6241 sizeof (uint64_t), 1, &intval);
6242 if (err == ENOENT) {
6243 intval = vdev_prop_default_numeric(
6244 prop);
6245 err = 0;
6246 } else if (err) {
6247 break;
6249 if (intval == vdev_prop_default_numeric(prop))
6250 src = ZPROP_SRC_DEFAULT;
6252 vdev_prop_add_list(outnvl, propname, strval,
6253 intval, src);
6254 break;
6255 case VDEV_PROP_CHECKSUM_N:
6256 case VDEV_PROP_CHECKSUM_T:
6257 case VDEV_PROP_IO_N:
6258 case VDEV_PROP_IO_T:
6259 err = vdev_prop_get_int(vd, prop, &intval);
6260 if (err && err != ENOENT)
6261 break;
6263 if (intval == vdev_prop_default_numeric(prop))
6264 src = ZPROP_SRC_DEFAULT;
6265 else
6266 src = ZPROP_SRC_LOCAL;
6268 vdev_prop_add_list(outnvl, propname, NULL,
6269 intval, src);
6270 break;
6271 /* Text Properties */
6272 case VDEV_PROP_COMMENT:
6273 /* Exists in the ZAP below */
6274 /* FALLTHRU */
6275 case VDEV_PROP_USERPROP:
6276 /* User Properites */
6277 src = ZPROP_SRC_LOCAL;
6279 err = zap_length(mos, objid, nvpair_name(elem),
6280 &integer_size, &num_integers);
6281 if (err)
6282 break;
6284 switch (integer_size) {
6285 case 8:
6286 /* User properties cannot be integers */
6287 err = EINVAL;
6288 break;
6289 case 1:
6290 /* string property */
6291 strval = kmem_alloc(num_integers,
6292 KM_SLEEP);
6293 err = zap_lookup(mos, objid,
6294 nvpair_name(elem), 1,
6295 num_integers, strval);
6296 if (err) {
6297 kmem_free(strval,
6298 num_integers);
6299 break;
6301 vdev_prop_add_list(outnvl, propname,
6302 strval, 0, src);
6303 kmem_free(strval, num_integers);
6304 break;
6306 break;
6307 default:
6308 err = ENOENT;
6309 break;
6311 if (err)
6312 break;
6314 } else {
6316 * Get all properties from the MOS vdev property object.
6318 zap_cursor_t zc;
6319 zap_attribute_t za;
6320 for (zap_cursor_init(&zc, mos, objid);
6321 (err = zap_cursor_retrieve(&zc, &za)) == 0;
6322 zap_cursor_advance(&zc)) {
6323 intval = 0;
6324 strval = NULL;
6325 zprop_source_t src = ZPROP_SRC_DEFAULT;
6326 propname = za.za_name;
6328 switch (za.za_integer_length) {
6329 case 8:
6330 /* We do not allow integer user properties */
6331 /* This is likely an internal value */
6332 break;
6333 case 1:
6334 /* string property */
6335 strval = kmem_alloc(za.za_num_integers,
6336 KM_SLEEP);
6337 err = zap_lookup(mos, objid, za.za_name, 1,
6338 za.za_num_integers, strval);
6339 if (err) {
6340 kmem_free(strval, za.za_num_integers);
6341 break;
6343 vdev_prop_add_list(outnvl, propname, strval, 0,
6344 src);
6345 kmem_free(strval, za.za_num_integers);
6346 break;
6348 default:
6349 break;
6352 zap_cursor_fini(&zc);
6355 mutex_exit(&spa->spa_props_lock);
6356 if (err && err != ENOENT) {
6357 return (err);
6360 return (0);
6363 EXPORT_SYMBOL(vdev_fault);
6364 EXPORT_SYMBOL(vdev_degrade);
6365 EXPORT_SYMBOL(vdev_online);
6366 EXPORT_SYMBOL(vdev_offline);
6367 EXPORT_SYMBOL(vdev_clear);
6369 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_count, UINT, ZMOD_RW,
6370 "Target number of metaslabs per top-level vdev");
6372 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_shift, UINT, ZMOD_RW,
6373 "Default lower limit for metaslab size");
6375 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, max_ms_shift, UINT, ZMOD_RW,
6376 "Default upper limit for metaslab size");
6378 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, min_ms_count, UINT, ZMOD_RW,
6379 "Minimum number of metaslabs per top-level vdev");
6381 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, ms_count_limit, UINT, ZMOD_RW,
6382 "Practical upper limit of total metaslabs per top-level vdev");
6384 ZFS_MODULE_PARAM(zfs, zfs_, slow_io_events_per_second, UINT, ZMOD_RW,
6385 "Rate limit slow IO (delay) events to this many per second");
6387 /* BEGIN CSTYLED */
6388 ZFS_MODULE_PARAM(zfs, zfs_, checksum_events_per_second, UINT, ZMOD_RW,
6389 "Rate limit checksum events to this many checksum errors per second "
6390 "(do not set below ZED threshold).");
6391 /* END CSTYLED */
6393 ZFS_MODULE_PARAM(zfs, zfs_, scan_ignore_errors, INT, ZMOD_RW,
6394 "Ignore errors during resilver/scrub");
6396 ZFS_MODULE_PARAM(zfs_vdev, vdev_, validate_skip, INT, ZMOD_RW,
6397 "Bypass vdev_validate()");
6399 ZFS_MODULE_PARAM(zfs, zfs_, nocacheflush, INT, ZMOD_RW,
6400 "Disable cache flushes");
6402 ZFS_MODULE_PARAM(zfs, zfs_, embedded_slog_min_ms, UINT, ZMOD_RW,
6403 "Minimum number of metaslabs required to dedicate one for log blocks");
6405 /* BEGIN CSTYLED */
6406 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, min_auto_ashift,
6407 param_set_min_auto_ashift, param_get_uint, ZMOD_RW,
6408 "Minimum ashift used when creating new top-level vdevs");
6410 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, max_auto_ashift,
6411 param_set_max_auto_ashift, param_get_uint, ZMOD_RW,
6412 "Maximum ashift used when optimizing for logical -> physical sector "
6413 "size on new top-level vdevs");
6414 /* END CSTYLED */