]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/vdev.c
Ignore pool ashift property during vdev attachment
[mirror_zfs.git] / module / zfs / vdev.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
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.
33 */
34
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"
64
65 /*
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().
71 *
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)
77 *
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.
83 */
84 static uint_t zfs_embedded_slog_min_ms = 64;
85
86 /* default target for number of metaslabs per top-level vdev */
87 static uint_t zfs_vdev_default_ms_count = 200;
88
89 /* minimum number of metaslabs per top-level vdev */
90 static uint_t zfs_vdev_min_ms_count = 16;
91
92 /* practical upper limit of total metaslabs per top-level vdev */
93 static uint_t zfs_vdev_ms_count_limit = 1ULL << 17;
94
95 /* lower limit for metaslab size (512M) */
96 static uint_t zfs_vdev_default_ms_shift = 29;
97
98 /* upper limit for metaslab size (16G) */
99 static uint_t zfs_vdev_max_ms_shift = 34;
100
101 int vdev_validate_skip = B_FALSE;
102
103 /*
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.
106 */
107 int zfs_vdev_dtl_sm_blksz = (1 << 12);
108
109 /*
110 * Rate limit slow IO (delay) events to this many per second.
111 */
112 static unsigned int zfs_slow_io_events_per_second = 20;
113
114 /*
115 * Rate limit checksum events after this many checksum errors per second.
116 */
117 static unsigned int zfs_checksum_events_per_second = 20;
118
119 /*
120 * Ignore errors during scrub/resilver. Allows to work around resilver
121 * upon import when there are pool errors.
122 */
123 static int zfs_scan_ignore_errors = 0;
124
125 /*
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.
129 */
130 int zfs_vdev_standard_sm_blksz = (1 << 17);
131
132 /*
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.
136 */
137 int zfs_nocacheflush = 0;
138
139 /*
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.
146 */
147 uint_t zfs_vdev_max_auto_ashift = 14;
148 uint_t zfs_vdev_min_auto_ashift = ASHIFT_MIN;
149
150 void
151 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
152 {
153 va_list adx;
154 char buf[256];
155
156 va_start(adx, fmt);
157 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
158 va_end(adx);
159
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);
168 }
169 }
170
171 void
172 vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
173 {
174 char state[20];
175
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;
181 }
182
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);
211 }
212
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);
218
219 for (uint64_t i = 0; i < vd->vdev_children; i++)
220 vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
221 }
222
223 /*
224 * Virtual device management.
225 */
226
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
241 };
242
243 /*
244 * Given a vdev type, return the appropriate ops vector.
245 */
246 static vdev_ops_t *
247 vdev_getops(const char *type)
248 {
249 vdev_ops_t *ops, *const *opspp;
250
251 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
252 if (strcmp(ops->vdev_op_type, type) == 0)
253 break;
254
255 return (ops);
256 }
257
258 /*
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.
263 */
264 metaslab_group_t *
265 vdev_get_mg(vdev_t *vd, metaslab_class_t *mc)
266 {
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);
272 }
273
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)
277 {
278 (void) vd, (void) remain_rs;
279
280 physical_rs->rs_start = logical_rs->rs_start;
281 physical_rs->rs_end = logical_rs->rs_end;
282 }
283
284 /*
285 * Derive the enumerated allocation bias from string input.
286 * String origin is either the per-vdev zap or zpool(8).
287 */
288 static vdev_alloc_bias_t
289 vdev_derive_alloc_bias(const char *bias)
290 {
291 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
292
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;
299
300 return (alloc_bias);
301 }
302
303 /*
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.
306 */
307 uint64_t
308 vdev_default_asize(vdev_t *vd, uint64_t psize)
309 {
310 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
311 uint64_t csize;
312
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);
316 }
317
318 return (asize);
319 }
320
321 uint64_t
322 vdev_default_min_asize(vdev_t *vd)
323 {
324 return (vd->vdev_min_asize);
325 }
326
327 /*
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.
332 */
333 uint64_t
334 vdev_get_min_asize(vdev_t *vd)
335 {
336 vdev_t *pvd = vd->vdev_parent;
337
338 /*
339 * If our parent is NULL (inactive spare or cache) or is the root,
340 * just return our own asize.
341 */
342 if (pvd == NULL)
343 return (vd->vdev_asize);
344
345 /*
346 * The top-level vdev just returns the allocatable size rounded
347 * to the nearest metaslab.
348 */
349 if (vd == vd->vdev_top)
350 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
351
352 return (pvd->vdev_ops->vdev_op_min_asize(pvd));
353 }
354
355 void
356 vdev_set_min_asize(vdev_t *vd)
357 {
358 vd->vdev_min_asize = vdev_get_min_asize(vd);
359
360 for (int c = 0; c < vd->vdev_children; c++)
361 vdev_set_min_asize(vd->vdev_child[c]);
362 }
363
364 /*
365 * Get the minimal allocation size for the top-level vdev.
366 */
367 uint64_t
368 vdev_get_min_alloc(vdev_t *vd)
369 {
370 uint64_t min_alloc = 1ULL << vd->vdev_ashift;
371
372 if (vd->vdev_ops->vdev_op_min_alloc != NULL)
373 min_alloc = vd->vdev_ops->vdev_op_min_alloc(vd);
374
375 return (min_alloc);
376 }
377
378 /*
379 * Get the parity level for a top-level vdev.
380 */
381 uint64_t
382 vdev_get_nparity(vdev_t *vd)
383 {
384 uint64_t nparity = 0;
385
386 if (vd->vdev_ops->vdev_op_nparity != NULL)
387 nparity = vd->vdev_ops->vdev_op_nparity(vd);
388
389 return (nparity);
390 }
391
392 static int
393 vdev_prop_get_int(vdev_t *vd, vdev_prop_t prop, uint64_t *value)
394 {
395 spa_t *spa = vd->vdev_spa;
396 objset_t *mos = spa->spa_meta_objset;
397 uint64_t objid;
398 int err;
399
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);
408 }
409
410 err = zap_lookup(mos, objid, vdev_prop_to_name(prop),
411 sizeof (uint64_t), 1, value);
412
413 if (err == ENOENT)
414 *value = vdev_prop_default_numeric(prop);
415
416 return (err);
417 }
418
419 /*
420 * Get the number of data disks for a top-level vdev.
421 */
422 uint64_t
423 vdev_get_ndisks(vdev_t *vd)
424 {
425 uint64_t ndisks = 1;
426
427 if (vd->vdev_ops->vdev_op_ndisks != NULL)
428 ndisks = vd->vdev_ops->vdev_op_ndisks(vd);
429
430 return (ndisks);
431 }
432
433 vdev_t *
434 vdev_lookup_top(spa_t *spa, uint64_t vdev)
435 {
436 vdev_t *rvd = spa->spa_root_vdev;
437
438 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
439
440 if (vdev < rvd->vdev_children) {
441 ASSERT(rvd->vdev_child[vdev] != NULL);
442 return (rvd->vdev_child[vdev]);
443 }
444
445 return (NULL);
446 }
447
448 vdev_t *
449 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
450 {
451 vdev_t *mvd;
452
453 if (vd->vdev_guid == guid)
454 return (vd);
455
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);
460
461 return (NULL);
462 }
463
464 static int
465 vdev_count_leaves_impl(vdev_t *vd)
466 {
467 int n = 0;
468
469 if (vd->vdev_ops->vdev_op_leaf)
470 return (1);
471
472 for (int c = 0; c < vd->vdev_children; c++)
473 n += vdev_count_leaves_impl(vd->vdev_child[c]);
474
475 return (n);
476 }
477
478 int
479 vdev_count_leaves(spa_t *spa)
480 {
481 int rc;
482
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);
486
487 return (rc);
488 }
489
490 void
491 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
492 {
493 size_t oldsize, newsize;
494 uint64_t id = cvd->vdev_id;
495 vdev_t **newchild;
496
497 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
498 ASSERT(cvd->vdev_parent == NULL);
499
500 cvd->vdev_parent = pvd;
501
502 if (pvd == NULL)
503 return;
504
505 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
506
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 *);
510
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);
515 }
516
517 pvd->vdev_child = newchild;
518 pvd->vdev_child[id] = cvd;
519
520 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
521 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
522
523 /*
524 * Walk up all ancestors to update guid sum.
525 */
526 for (; pvd != NULL; pvd = pvd->vdev_parent)
527 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
528
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++;
532 }
533 }
534
535 void
536 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
537 {
538 int c;
539 uint_t id = cvd->vdev_id;
540
541 ASSERT(cvd->vdev_parent == pvd);
542
543 if (pvd == NULL)
544 return;
545
546 ASSERT(id < pvd->vdev_children);
547 ASSERT(pvd->vdev_child[id] == cvd);
548
549 pvd->vdev_child[id] = NULL;
550 cvd->vdev_parent = NULL;
551
552 for (c = 0; c < pvd->vdev_children; c++)
553 if (pvd->vdev_child[c])
554 break;
555
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;
560 }
561
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++;
566 }
567
568 /*
569 * Walk up all ancestors to update guid sum.
570 */
571 for (; pvd != NULL; pvd = pvd->vdev_parent)
572 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
573 }
574
575 /*
576 * Remove any holes in the child array.
577 */
578 void
579 vdev_compact_children(vdev_t *pvd)
580 {
581 vdev_t **newchild, *cvd;
582 int oldc = pvd->vdev_children;
583 int newc;
584
585 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
586
587 if (oldc == 0)
588 return;
589
590 for (int c = newc = 0; c < oldc; c++)
591 if (pvd->vdev_child[c])
592 newc++;
593
594 if (newc > 0) {
595 newchild = kmem_zalloc(newc * sizeof (vdev_t *), KM_SLEEP);
596
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++;
601 }
602 }
603 } else {
604 newchild = NULL;
605 }
606
607 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
608 pvd->vdev_child = newchild;
609 pvd->vdev_children = newc;
610 }
611
612 /*
613 * Allocate and minimally initialize a vdev_t.
614 */
615 vdev_t *
616 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
617 {
618 vdev_t *vd;
619 vdev_indirect_config_t *vic;
620
621 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
622 vic = &vd->vdev_indirect_config;
623
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);
628 }
629
630 if (guid == 0 && ops != &vdev_hole_ops) {
631 if (spa->spa_root_vdev == vd) {
632 /*
633 * The root vdev's guid will also be the pool guid,
634 * which must be unique among all pools.
635 */
636 guid = spa_generate_guid(NULL);
637 } else {
638 /*
639 * Any other vdev's guid must be unique within the pool.
640 */
641 guid = spa_generate_guid(spa);
642 }
643 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
644 }
645
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;
654
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);
659
660 /*
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.
664 */
665 zfs_ratelimit_init(&vd->vdev_delay_rl, &zfs_slow_io_events_per_second,
666 1);
667 zfs_ratelimit_init(&vd->vdev_deadman_rl, &zfs_slow_io_events_per_second,
668 1);
669 zfs_ratelimit_init(&vd->vdev_checksum_rl,
670 &zfs_checksum_events_per_second, 1);
671
672 /*
673 * Default Thresholds for tuning ZED
674 */
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);
679
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);
685
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);
690
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);
695
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);
703
704 mutex_init(&vd->vdev_rebuild_lock, NULL, MUTEX_DEFAULT, NULL);
705 cv_init(&vd->vdev_rebuild_cv, NULL, CV_DEFAULT, NULL);
706
707 for (int t = 0; t < DTL_TYPES; t++) {
708 vd->vdev_dtl[t] = range_tree_create(NULL, RANGE_SEG64, NULL, 0,
709 0);
710 }
711
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);
718
719 return (vd);
720 }
721
722 /*
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.
726 */
727 int
728 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
729 int alloctype)
730 {
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);
740
741 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
742
743 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
744 return (SET_ERROR(EINVAL));
745
746 if ((ops = vdev_getops(type)) == NULL)
747 return (SET_ERROR(EINVAL));
748
749 /*
750 * If this is a load, get the vdev guid from the nvlist.
751 * Otherwise, vdev_alloc_common() will generate one for us.
752 */
753 if (alloctype == VDEV_ALLOC_LOAD) {
754 uint64_t label_id;
755
756 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
757 label_id != id)
758 return (SET_ERROR(EINVAL));
759
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));
771 }
772
773 /*
774 * The first allocated vdev must be of type 'root'.
775 */
776 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
777 return (SET_ERROR(EINVAL));
778
779 /*
780 * Determine whether we're a log vdev.
781 */
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));
786
787 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
788 return (SET_ERROR(ENOTSUP));
789
790 if (top_level && alloctype == VDEV_ALLOC_ADD) {
791 const char *bias;
792
793 /*
794 * If creating a top-level vdev, check for allocation
795 * classes input.
796 */
797 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
798 &bias) == 0) {
799 alloc_bias = vdev_derive_alloc_bias(bias);
800
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));
806 }
807 }
808
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));
814 }
815 }
816
817 /*
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.
821 */
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);
827 }
828 }
829
830 vd = vdev_alloc_common(spa, id, guid, ops);
831 vd->vdev_tsd = tsd;
832 vd->vdev_islog = islog;
833
834 if (top_level && alloc_bias != VDEV_BIAS_NONE)
835 vd->vdev_alloc_bias = alloc_bias;
836
837 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &tmp) == 0)
838 vd->vdev_path = spa_strdup(tmp);
839
840 /*
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).
844 */
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;
850 }
851
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);
856
857 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
858 &tmp) == 0)
859 vd->vdev_enc_sysfs_path = spa_strdup(tmp);
860
861 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &tmp) == 0)
862 vd->vdev_fru = spa_strdup(tmp);
863
864 /*
865 * Set the whole_disk property. If it's not specified, leave the value
866 * as -1.
867 */
868 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
869 &vd->vdev_wholedisk) != 0)
870 vd->vdev_wholedisk = -1ULL;
871
872 vic = &vd->vdev_indirect_config;
873
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);
883
884 /*
885 * Look for the 'not present' flag. This will only be set if the device
886 * was not present at the time of import.
887 */
888 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
889 &vd->vdev_not_present);
890
891 /*
892 * Get the alignment requirement. Ignore pool ashift for vdev
893 * attach case.
894 */
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;
900 }
901
902 /*
903 * Retrieve the vdev creation time.
904 */
905 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
906 &vd->vdev_crtxg);
907
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);
914 }
915
916 /*
917 * If we're a top-level vdev, try to load the allocation parameters.
918 */
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);
935 }
936
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 */
943 }
944
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);
951 }
952
953 /*
954 * If we're a leaf vdev, try to load the DTL object and other state.
955 */
956
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);
965 }
966
967 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
968 uint64_t spare = 0;
969
970 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
971 &spare) == 0 && spare)
972 spa_spare_add(vd);
973 }
974
975 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
976 &vd->vdev_offline);
977
978 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
979 &vd->vdev_resilver_txg);
980
981 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REBUILD_TXG,
982 &vd->vdev_rebuild_txg);
983
984 if (nvlist_exists(nv, ZPOOL_CONFIG_RESILVER_DEFER))
985 vdev_defer_resilver(vd);
986
987 /*
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.
994 *
995 * Local vdevs will remain in the faulted state.
996 */
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);
1005
1006 if (vd->vdev_faulted || vd->vdev_degraded) {
1007 const char *aux;
1008
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;
1017 }
1018 }
1019 }
1020
1021 /*
1022 * Add ourselves to the parent's list of children.
1023 */
1024 vdev_add_child(parent, vd);
1025
1026 *vdp = vd;
1027
1028 return (0);
1029 }
1030
1031 void
1032 vdev_free(vdev_t *vd)
1033 {
1034 spa_t *spa = vd->vdev_spa;
1035
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);
1040
1041 /*
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.
1045 */
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);
1051 }
1052
1053 /*
1054 * vdev_free() implies closing the vdev first. This is simpler than
1055 * trying to ensure complicated semantics for all callers.
1056 */
1057 vdev_close(vd);
1058
1059 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
1060 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
1061
1062 /*
1063 * Free all children.
1064 */
1065 for (int c = 0; c < vd->vdev_children; c++)
1066 vdev_free(vd->vdev_child[c]);
1067
1068 ASSERT(vd->vdev_child == NULL);
1069 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
1070
1071 if (vd->vdev_ops->vdev_op_fini != NULL)
1072 vd->vdev_ops->vdev_op_fini(vd);
1073
1074 /*
1075 * Discard allocation state.
1076 */
1077 if (vd->vdev_mg != NULL) {
1078 vdev_metaslab_fini(vd);
1079 metaslab_group_destroy(vd->vdev_mg);
1080 vd->vdev_mg = NULL;
1081 }
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;
1086 }
1087
1088 ASSERT0(vd->vdev_stat.vs_space);
1089 ASSERT0(vd->vdev_stat.vs_dspace);
1090 ASSERT0(vd->vdev_stat.vs_alloc);
1091
1092 /*
1093 * Remove this vdev from its parent's child list.
1094 */
1095 vdev_remove_child(vd->vdev_parent, vd);
1096
1097 ASSERT(vd->vdev_parent == NULL);
1098 ASSERT(!list_link_active(&vd->vdev_leaf_node));
1099
1100 /*
1101 * Clean up vdev structure.
1102 */
1103 vdev_queue_fini(vd);
1104
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);
1111
1112 if (vd->vdev_enc_sysfs_path)
1113 spa_strfree(vd->vdev_enc_sysfs_path);
1114
1115 if (vd->vdev_fru)
1116 spa_strfree(vd->vdev_fru);
1117
1118 if (vd->vdev_isspare)
1119 spa_spare_remove(vd);
1120 if (vd->vdev_isl2cache)
1121 spa_l2cache_remove(vd);
1122
1123 txg_list_destroy(&vd->vdev_ms_list);
1124 txg_list_destroy(&vd->vdev_dtl_list);
1125
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]);
1131 }
1132 mutex_exit(&vd->vdev_dtl_lock);
1133
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);
1139 }
1140
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;
1146 }
1147 range_tree_destroy(vd->vdev_obsolete_segments);
1148 rw_destroy(&vd->vdev_indirect_rwlock);
1149 mutex_destroy(&vd->vdev_obsolete_lock);
1150
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);
1155
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);
1160
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);
1168
1169 mutex_destroy(&vd->vdev_rebuild_lock);
1170 cv_destroy(&vd->vdev_rebuild_cv);
1171
1172 zfs_ratelimit_fini(&vd->vdev_delay_rl);
1173 zfs_ratelimit_fini(&vd->vdev_deadman_rl);
1174 zfs_ratelimit_fini(&vd->vdev_checksum_rl);
1175
1176 if (vd == spa->spa_root_vdev)
1177 spa->spa_root_vdev = NULL;
1178
1179 kmem_free(vd, sizeof (vdev_t));
1180 }
1181
1182 /*
1183 * Transfer top-level vdev state from svd to tvd.
1184 */
1185 static void
1186 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
1187 {
1188 spa_t *spa = svd->vdev_spa;
1189 metaslab_t *msp;
1190 vdev_t *vd;
1191 int t;
1192
1193 ASSERT(tvd == tvd->vdev_top);
1194
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;
1200
1201 svd->vdev_ms_array = 0;
1202 svd->vdev_ms_shift = 0;
1203 svd->vdev_ms_count = 0;
1204 svd->vdev_top_zap = 0;
1205
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;
1213
1214 svd->vdev_mg = NULL;
1215 svd->vdev_log_mg = NULL;
1216 svd->vdev_ms = NULL;
1217
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;
1222
1223 tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm;
1224 svd->vdev_checkpoint_sm = NULL;
1225
1226 tvd->vdev_alloc_bias = svd->vdev_alloc_bias;
1227 svd->vdev_alloc_bias = VDEV_BIAS_NONE;
1228
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;
1232
1233 svd->vdev_stat.vs_alloc = 0;
1234 svd->vdev_stat.vs_space = 0;
1235 svd->vdev_stat.vs_dspace = 0;
1236
1237 /*
1238 * State which may be set on a top-level vdev that's in the
1239 * process of being removed.
1240 */
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;
1269
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);
1277 }
1278
1279 if (list_link_active(&svd->vdev_config_dirty_node)) {
1280 vdev_config_clean(svd);
1281 vdev_config_dirty(tvd);
1282 }
1283
1284 if (list_link_active(&svd->vdev_state_dirty_node)) {
1285 vdev_state_clean(svd);
1286 vdev_state_dirty(tvd);
1287 }
1288
1289 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
1290 svd->vdev_deflate_ratio = 0;
1291
1292 tvd->vdev_islog = svd->vdev_islog;
1293 svd->vdev_islog = 0;
1294
1295 dsl_scan_io_queue_vdev_xfer(svd, tvd);
1296 }
1297
1298 static void
1299 vdev_top_update(vdev_t *tvd, vdev_t *vd)
1300 {
1301 if (vd == NULL)
1302 return;
1303
1304 vd->vdev_top = tvd;
1305
1306 for (int c = 0; c < vd->vdev_children; c++)
1307 vdev_top_update(tvd, vd->vdev_child[c]);
1308 }
1309
1310 /*
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.
1313 */
1314 vdev_t *
1315 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
1316 {
1317 spa_t *spa = cvd->vdev_spa;
1318 vdev_t *pvd = cvd->vdev_parent;
1319 vdev_t *mvd;
1320
1321 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1322
1323 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
1324
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;
1334
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);
1340
1341 if (mvd == mvd->vdev_top)
1342 vdev_top_transfer(cvd, mvd);
1343
1344 return (mvd);
1345 }
1346
1347 /*
1348 * Remove a 1-way mirror/replacing vdev from the tree.
1349 */
1350 void
1351 vdev_remove_parent(vdev_t *cvd)
1352 {
1353 vdev_t *mvd = cvd->vdev_parent;
1354 vdev_t *pvd = mvd->vdev_parent;
1355
1356 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1357
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);
1367
1368 /*
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.
1373 */
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;
1379
1380 /*
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.
1387 */
1388 if (!cvd->vdev_spa->spa_autoexpand)
1389 cvd->vdev_asize = mvd->vdev_asize;
1390 }
1391 cvd->vdev_id = mvd->vdev_id;
1392 vdev_add_child(pvd, cvd);
1393 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1394
1395 if (cvd == cvd->vdev_top)
1396 vdev_top_transfer(mvd, cvd);
1397
1398 ASSERT(mvd->vdev_children == 0);
1399 vdev_free(mvd);
1400 }
1401
1402 void
1403 vdev_metaslab_group_create(vdev_t *vd)
1404 {
1405 spa_t *spa = vd->vdev_spa;
1406
1407 /*
1408 * metaslab_group_create was delayed until allocation bias was available
1409 */
1410 if (vd->vdev_mg == NULL) {
1411 metaslab_class_t *mc;
1412
1413 if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE)
1414 vd->vdev_alloc_bias = VDEV_BIAS_LOG;
1415
1416 ASSERT3U(vd->vdev_islog, ==,
1417 (vd->vdev_alloc_bias == VDEV_BIAS_LOG));
1418
1419 switch (vd->vdev_alloc_bias) {
1420 case VDEV_BIAS_LOG:
1421 mc = spa_log_class(spa);
1422 break;
1423 case VDEV_BIAS_SPECIAL:
1424 mc = spa_special_class(spa);
1425 break;
1426 case VDEV_BIAS_DEDUP:
1427 mc = spa_dedup_class(spa);
1428 break;
1429 default:
1430 mc = spa_normal_class(spa);
1431 }
1432
1433 vd->vdev_mg = metaslab_group_create(mc, vd,
1434 spa->spa_alloc_count);
1435
1436 if (!vd->vdev_islog) {
1437 vd->vdev_log_mg = metaslab_group_create(
1438 spa_embedded_log_class(spa), vd, 1);
1439 }
1440
1441 /*
1442 * The spa ashift min/max only apply for the normal metaslab
1443 * class. Class destination is late binding so ashift boundary
1444 * setting had to wait until now.
1445 */
1446 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1447 mc == spa_normal_class(spa) && vd->vdev_aux == NULL) {
1448 if (vd->vdev_ashift > spa->spa_max_ashift)
1449 spa->spa_max_ashift = vd->vdev_ashift;
1450 if (vd->vdev_ashift < spa->spa_min_ashift)
1451 spa->spa_min_ashift = vd->vdev_ashift;
1452
1453 uint64_t min_alloc = vdev_get_min_alloc(vd);
1454 if (min_alloc < spa->spa_min_alloc)
1455 spa->spa_min_alloc = min_alloc;
1456 }
1457 }
1458 }
1459
1460 int
1461 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
1462 {
1463 spa_t *spa = vd->vdev_spa;
1464 uint64_t oldc = vd->vdev_ms_count;
1465 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
1466 metaslab_t **mspp;
1467 int error;
1468 boolean_t expanding = (oldc != 0);
1469
1470 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1471
1472 /*
1473 * This vdev is not being allocated from yet or is a hole.
1474 */
1475 if (vd->vdev_ms_shift == 0)
1476 return (0);
1477
1478 ASSERT(!vd->vdev_ishole);
1479
1480 ASSERT(oldc <= newc);
1481
1482 mspp = vmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1483
1484 if (expanding) {
1485 memcpy(mspp, vd->vdev_ms, oldc * sizeof (*mspp));
1486 vmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1487 }
1488
1489 vd->vdev_ms = mspp;
1490 vd->vdev_ms_count = newc;
1491
1492 for (uint64_t m = oldc; m < newc; m++) {
1493 uint64_t object = 0;
1494 /*
1495 * vdev_ms_array may be 0 if we are creating the "fake"
1496 * metaslabs for an indirect vdev for zdb's leak detection.
1497 * See zdb_leak_init().
1498 */
1499 if (txg == 0 && vd->vdev_ms_array != 0) {
1500 error = dmu_read(spa->spa_meta_objset,
1501 vd->vdev_ms_array,
1502 m * sizeof (uint64_t), sizeof (uint64_t), &object,
1503 DMU_READ_PREFETCH);
1504 if (error != 0) {
1505 vdev_dbgmsg(vd, "unable to read the metaslab "
1506 "array [error=%d]", error);
1507 return (error);
1508 }
1509 }
1510
1511 error = metaslab_init(vd->vdev_mg, m, object, txg,
1512 &(vd->vdev_ms[m]));
1513 if (error != 0) {
1514 vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
1515 error);
1516 return (error);
1517 }
1518 }
1519
1520 /*
1521 * Find the emptiest metaslab on the vdev and mark it for use for
1522 * embedded slog by moving it from the regular to the log metaslab
1523 * group.
1524 */
1525 if (vd->vdev_mg->mg_class == spa_normal_class(spa) &&
1526 vd->vdev_ms_count > zfs_embedded_slog_min_ms &&
1527 avl_is_empty(&vd->vdev_log_mg->mg_metaslab_tree)) {
1528 uint64_t slog_msid = 0;
1529 uint64_t smallest = UINT64_MAX;
1530
1531 /*
1532 * Note, we only search the new metaslabs, because the old
1533 * (pre-existing) ones may be active (e.g. have non-empty
1534 * range_tree's), and we don't move them to the new
1535 * metaslab_t.
1536 */
1537 for (uint64_t m = oldc; m < newc; m++) {
1538 uint64_t alloc =
1539 space_map_allocated(vd->vdev_ms[m]->ms_sm);
1540 if (alloc < smallest) {
1541 slog_msid = m;
1542 smallest = alloc;
1543 }
1544 }
1545 metaslab_t *slog_ms = vd->vdev_ms[slog_msid];
1546 /*
1547 * The metaslab was marked as dirty at the end of
1548 * metaslab_init(). Remove it from the dirty list so that we
1549 * can uninitialize and reinitialize it to the new class.
1550 */
1551 if (txg != 0) {
1552 (void) txg_list_remove_this(&vd->vdev_ms_list,
1553 slog_ms, txg);
1554 }
1555 uint64_t sm_obj = space_map_object(slog_ms->ms_sm);
1556 metaslab_fini(slog_ms);
1557 VERIFY0(metaslab_init(vd->vdev_log_mg, slog_msid, sm_obj, txg,
1558 &vd->vdev_ms[slog_msid]));
1559 }
1560
1561 if (txg == 0)
1562 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1563
1564 /*
1565 * If the vdev is marked as non-allocating then don't
1566 * activate the metaslabs since we want to ensure that
1567 * no allocations are performed on this device.
1568 */
1569 if (vd->vdev_noalloc) {
1570 /* track non-allocating vdev space */
1571 spa->spa_nonallocating_dspace += spa_deflate(spa) ?
1572 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
1573 } else if (!expanding) {
1574 metaslab_group_activate(vd->vdev_mg);
1575 if (vd->vdev_log_mg != NULL)
1576 metaslab_group_activate(vd->vdev_log_mg);
1577 }
1578
1579 if (txg == 0)
1580 spa_config_exit(spa, SCL_ALLOC, FTAG);
1581
1582 return (0);
1583 }
1584
1585 void
1586 vdev_metaslab_fini(vdev_t *vd)
1587 {
1588 if (vd->vdev_checkpoint_sm != NULL) {
1589 ASSERT(spa_feature_is_active(vd->vdev_spa,
1590 SPA_FEATURE_POOL_CHECKPOINT));
1591 space_map_close(vd->vdev_checkpoint_sm);
1592 /*
1593 * Even though we close the space map, we need to set its
1594 * pointer to NULL. The reason is that vdev_metaslab_fini()
1595 * may be called multiple times for certain operations
1596 * (i.e. when destroying a pool) so we need to ensure that
1597 * this clause never executes twice. This logic is similar
1598 * to the one used for the vdev_ms clause below.
1599 */
1600 vd->vdev_checkpoint_sm = NULL;
1601 }
1602
1603 if (vd->vdev_ms != NULL) {
1604 metaslab_group_t *mg = vd->vdev_mg;
1605
1606 metaslab_group_passivate(mg);
1607 if (vd->vdev_log_mg != NULL) {
1608 ASSERT(!vd->vdev_islog);
1609 metaslab_group_passivate(vd->vdev_log_mg);
1610 }
1611
1612 uint64_t count = vd->vdev_ms_count;
1613 for (uint64_t m = 0; m < count; m++) {
1614 metaslab_t *msp = vd->vdev_ms[m];
1615 if (msp != NULL)
1616 metaslab_fini(msp);
1617 }
1618 vmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1619 vd->vdev_ms = NULL;
1620 vd->vdev_ms_count = 0;
1621
1622 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
1623 ASSERT0(mg->mg_histogram[i]);
1624 if (vd->vdev_log_mg != NULL)
1625 ASSERT0(vd->vdev_log_mg->mg_histogram[i]);
1626 }
1627 }
1628 ASSERT0(vd->vdev_ms_count);
1629 ASSERT3U(vd->vdev_pending_fastwrite, ==, 0);
1630 }
1631
1632 typedef struct vdev_probe_stats {
1633 boolean_t vps_readable;
1634 boolean_t vps_writeable;
1635 int vps_flags;
1636 } vdev_probe_stats_t;
1637
1638 static void
1639 vdev_probe_done(zio_t *zio)
1640 {
1641 spa_t *spa = zio->io_spa;
1642 vdev_t *vd = zio->io_vd;
1643 vdev_probe_stats_t *vps = zio->io_private;
1644
1645 ASSERT(vd->vdev_probe_zio != NULL);
1646
1647 if (zio->io_type == ZIO_TYPE_READ) {
1648 if (zio->io_error == 0)
1649 vps->vps_readable = 1;
1650 if (zio->io_error == 0 && spa_writeable(spa)) {
1651 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1652 zio->io_offset, zio->io_size, zio->io_abd,
1653 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1654 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1655 } else {
1656 abd_free(zio->io_abd);
1657 }
1658 } else if (zio->io_type == ZIO_TYPE_WRITE) {
1659 if (zio->io_error == 0)
1660 vps->vps_writeable = 1;
1661 abd_free(zio->io_abd);
1662 } else if (zio->io_type == ZIO_TYPE_NULL) {
1663 zio_t *pio;
1664 zio_link_t *zl;
1665
1666 vd->vdev_cant_read |= !vps->vps_readable;
1667 vd->vdev_cant_write |= !vps->vps_writeable;
1668
1669 if (vdev_readable(vd) &&
1670 (vdev_writeable(vd) || !spa_writeable(spa))) {
1671 zio->io_error = 0;
1672 } else {
1673 ASSERT(zio->io_error != 0);
1674 vdev_dbgmsg(vd, "failed probe");
1675 (void) zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
1676 spa, vd, NULL, NULL, 0);
1677 zio->io_error = SET_ERROR(ENXIO);
1678 }
1679
1680 mutex_enter(&vd->vdev_probe_lock);
1681 ASSERT(vd->vdev_probe_zio == zio);
1682 vd->vdev_probe_zio = NULL;
1683 mutex_exit(&vd->vdev_probe_lock);
1684
1685 zl = NULL;
1686 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1687 if (!vdev_accessible(vd, pio))
1688 pio->io_error = SET_ERROR(ENXIO);
1689
1690 kmem_free(vps, sizeof (*vps));
1691 }
1692 }
1693
1694 /*
1695 * Determine whether this device is accessible.
1696 *
1697 * Read and write to several known locations: the pad regions of each
1698 * vdev label but the first, which we leave alone in case it contains
1699 * a VTOC.
1700 */
1701 zio_t *
1702 vdev_probe(vdev_t *vd, zio_t *zio)
1703 {
1704 spa_t *spa = vd->vdev_spa;
1705 vdev_probe_stats_t *vps = NULL;
1706 zio_t *pio;
1707
1708 ASSERT(vd->vdev_ops->vdev_op_leaf);
1709
1710 /*
1711 * Don't probe the probe.
1712 */
1713 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1714 return (NULL);
1715
1716 /*
1717 * To prevent 'probe storms' when a device fails, we create
1718 * just one probe i/o at a time. All zios that want to probe
1719 * this vdev will become parents of the probe io.
1720 */
1721 mutex_enter(&vd->vdev_probe_lock);
1722
1723 if ((pio = vd->vdev_probe_zio) == NULL) {
1724 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1725
1726 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1727 ZIO_FLAG_DONT_AGGREGATE | ZIO_FLAG_TRYHARD;
1728
1729 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1730 /*
1731 * vdev_cant_read and vdev_cant_write can only
1732 * transition from TRUE to FALSE when we have the
1733 * SCL_ZIO lock as writer; otherwise they can only
1734 * transition from FALSE to TRUE. This ensures that
1735 * any zio looking at these values can assume that
1736 * failures persist for the life of the I/O. That's
1737 * important because when a device has intermittent
1738 * connectivity problems, we want to ensure that
1739 * they're ascribed to the device (ENXIO) and not
1740 * the zio (EIO).
1741 *
1742 * Since we hold SCL_ZIO as writer here, clear both
1743 * values so the probe can reevaluate from first
1744 * principles.
1745 */
1746 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1747 vd->vdev_cant_read = B_FALSE;
1748 vd->vdev_cant_write = B_FALSE;
1749 }
1750
1751 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1752 vdev_probe_done, vps,
1753 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1754
1755 /*
1756 * We can't change the vdev state in this context, so we
1757 * kick off an async task to do it on our behalf.
1758 */
1759 if (zio != NULL) {
1760 vd->vdev_probe_wanted = B_TRUE;
1761 spa_async_request(spa, SPA_ASYNC_PROBE);
1762 }
1763 }
1764
1765 if (zio != NULL)
1766 zio_add_child(zio, pio);
1767
1768 mutex_exit(&vd->vdev_probe_lock);
1769
1770 if (vps == NULL) {
1771 ASSERT(zio != NULL);
1772 return (NULL);
1773 }
1774
1775 for (int l = 1; l < VDEV_LABELS; l++) {
1776 zio_nowait(zio_read_phys(pio, vd,
1777 vdev_label_offset(vd->vdev_psize, l,
1778 offsetof(vdev_label_t, vl_be)), VDEV_PAD_SIZE,
1779 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1780 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1781 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1782 }
1783
1784 if (zio == NULL)
1785 return (pio);
1786
1787 zio_nowait(pio);
1788 return (NULL);
1789 }
1790
1791 static void
1792 vdev_load_child(void *arg)
1793 {
1794 vdev_t *vd = arg;
1795
1796 vd->vdev_load_error = vdev_load(vd);
1797 }
1798
1799 static void
1800 vdev_open_child(void *arg)
1801 {
1802 vdev_t *vd = arg;
1803
1804 vd->vdev_open_thread = curthread;
1805 vd->vdev_open_error = vdev_open(vd);
1806 vd->vdev_open_thread = NULL;
1807 }
1808
1809 static boolean_t
1810 vdev_uses_zvols(vdev_t *vd)
1811 {
1812 #ifdef _KERNEL
1813 if (zvol_is_zvol(vd->vdev_path))
1814 return (B_TRUE);
1815 #endif
1816
1817 for (int c = 0; c < vd->vdev_children; c++)
1818 if (vdev_uses_zvols(vd->vdev_child[c]))
1819 return (B_TRUE);
1820
1821 return (B_FALSE);
1822 }
1823
1824 /*
1825 * Returns B_TRUE if the passed child should be opened.
1826 */
1827 static boolean_t
1828 vdev_default_open_children_func(vdev_t *vd)
1829 {
1830 (void) vd;
1831 return (B_TRUE);
1832 }
1833
1834 /*
1835 * Open the requested child vdevs. If any of the leaf vdevs are using
1836 * a ZFS volume then do the opens in a single thread. This avoids a
1837 * deadlock when the current thread is holding the spa_namespace_lock.
1838 */
1839 static void
1840 vdev_open_children_impl(vdev_t *vd, vdev_open_children_func_t *open_func)
1841 {
1842 int children = vd->vdev_children;
1843
1844 taskq_t *tq = taskq_create("vdev_open", children, minclsyspri,
1845 children, children, TASKQ_PREPOPULATE);
1846 vd->vdev_nonrot = B_TRUE;
1847
1848 for (int c = 0; c < children; c++) {
1849 vdev_t *cvd = vd->vdev_child[c];
1850
1851 if (open_func(cvd) == B_FALSE)
1852 continue;
1853
1854 if (tq == NULL || vdev_uses_zvols(vd)) {
1855 cvd->vdev_open_error = vdev_open(cvd);
1856 } else {
1857 VERIFY(taskq_dispatch(tq, vdev_open_child,
1858 cvd, TQ_SLEEP) != TASKQID_INVALID);
1859 }
1860
1861 vd->vdev_nonrot &= cvd->vdev_nonrot;
1862 }
1863
1864 if (tq != NULL) {
1865 taskq_wait(tq);
1866 taskq_destroy(tq);
1867 }
1868 }
1869
1870 /*
1871 * Open all child vdevs.
1872 */
1873 void
1874 vdev_open_children(vdev_t *vd)
1875 {
1876 vdev_open_children_impl(vd, vdev_default_open_children_func);
1877 }
1878
1879 /*
1880 * Conditionally open a subset of child vdevs.
1881 */
1882 void
1883 vdev_open_children_subset(vdev_t *vd, vdev_open_children_func_t *open_func)
1884 {
1885 vdev_open_children_impl(vd, open_func);
1886 }
1887
1888 /*
1889 * Compute the raidz-deflation ratio. Note, we hard-code
1890 * in 128k (1 << 17) because it is the "typical" blocksize.
1891 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1892 * otherwise it would inconsistently account for existing bp's.
1893 */
1894 static void
1895 vdev_set_deflate_ratio(vdev_t *vd)
1896 {
1897 if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
1898 vd->vdev_deflate_ratio = (1 << 17) /
1899 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
1900 }
1901 }
1902
1903 /*
1904 * Choose the best of two ashifts, preferring one between logical ashift
1905 * (absolute minimum) and administrator defined maximum, otherwise take
1906 * the biggest of the two.
1907 */
1908 uint64_t
1909 vdev_best_ashift(uint64_t logical, uint64_t a, uint64_t b)
1910 {
1911 if (a > logical && a <= zfs_vdev_max_auto_ashift) {
1912 if (b <= logical || b > zfs_vdev_max_auto_ashift)
1913 return (a);
1914 else
1915 return (MAX(a, b));
1916 } else if (b <= logical || b > zfs_vdev_max_auto_ashift)
1917 return (MAX(a, b));
1918 return (b);
1919 }
1920
1921 /*
1922 * Maximize performance by inflating the configured ashift for top level
1923 * vdevs to be as close to the physical ashift as possible while maintaining
1924 * administrator defined limits and ensuring it doesn't go below the
1925 * logical ashift.
1926 */
1927 static void
1928 vdev_ashift_optimize(vdev_t *vd)
1929 {
1930 ASSERT(vd == vd->vdev_top);
1931
1932 if (vd->vdev_ashift < vd->vdev_physical_ashift &&
1933 vd->vdev_physical_ashift <= zfs_vdev_max_auto_ashift) {
1934 vd->vdev_ashift = MIN(
1935 MAX(zfs_vdev_max_auto_ashift, vd->vdev_ashift),
1936 MAX(zfs_vdev_min_auto_ashift,
1937 vd->vdev_physical_ashift));
1938 } else {
1939 /*
1940 * If the logical and physical ashifts are the same, then
1941 * we ensure that the top-level vdev's ashift is not smaller
1942 * than our minimum ashift value. For the unusual case
1943 * where logical ashift > physical ashift, we can't cap
1944 * the calculated ashift based on max ashift as that
1945 * would cause failures.
1946 * We still check if we need to increase it to match
1947 * the min ashift.
1948 */
1949 vd->vdev_ashift = MAX(zfs_vdev_min_auto_ashift,
1950 vd->vdev_ashift);
1951 }
1952 }
1953
1954 /*
1955 * Prepare a virtual device for access.
1956 */
1957 int
1958 vdev_open(vdev_t *vd)
1959 {
1960 spa_t *spa = vd->vdev_spa;
1961 int error;
1962 uint64_t osize = 0;
1963 uint64_t max_osize = 0;
1964 uint64_t asize, max_asize, psize;
1965 uint64_t logical_ashift = 0;
1966 uint64_t physical_ashift = 0;
1967
1968 ASSERT(vd->vdev_open_thread == curthread ||
1969 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1970 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1971 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1972 vd->vdev_state == VDEV_STATE_OFFLINE);
1973
1974 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1975 vd->vdev_cant_read = B_FALSE;
1976 vd->vdev_cant_write = B_FALSE;
1977 vd->vdev_min_asize = vdev_get_min_asize(vd);
1978
1979 /*
1980 * If this vdev is not removed, check its fault status. If it's
1981 * faulted, bail out of the open.
1982 */
1983 if (!vd->vdev_removed && vd->vdev_faulted) {
1984 ASSERT(vd->vdev_children == 0);
1985 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1986 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1987 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1988 vd->vdev_label_aux);
1989 return (SET_ERROR(ENXIO));
1990 } else if (vd->vdev_offline) {
1991 ASSERT(vd->vdev_children == 0);
1992 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1993 return (SET_ERROR(ENXIO));
1994 }
1995
1996 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize,
1997 &logical_ashift, &physical_ashift);
1998
1999 /* Keep the device in removed state if unplugged */
2000 if (error == ENOENT && vd->vdev_removed) {
2001 vdev_set_state(vd, B_TRUE, VDEV_STATE_REMOVED,
2002 VDEV_AUX_NONE);
2003 return (error);
2004 }
2005
2006 /*
2007 * Physical volume size should never be larger than its max size, unless
2008 * the disk has shrunk while we were reading it or the device is buggy
2009 * or damaged: either way it's not safe for use, bail out of the open.
2010 */
2011 if (osize > max_osize) {
2012 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2013 VDEV_AUX_OPEN_FAILED);
2014 return (SET_ERROR(ENXIO));
2015 }
2016
2017 /*
2018 * Reset the vdev_reopening flag so that we actually close
2019 * the vdev on error.
2020 */
2021 vd->vdev_reopening = B_FALSE;
2022 if (zio_injection_enabled && error == 0)
2023 error = zio_handle_device_injection(vd, NULL, SET_ERROR(ENXIO));
2024
2025 if (error) {
2026 if (vd->vdev_removed &&
2027 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
2028 vd->vdev_removed = B_FALSE;
2029
2030 if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
2031 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
2032 vd->vdev_stat.vs_aux);
2033 } else {
2034 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2035 vd->vdev_stat.vs_aux);
2036 }
2037 return (error);
2038 }
2039
2040 vd->vdev_removed = B_FALSE;
2041
2042 /*
2043 * Recheck the faulted flag now that we have confirmed that
2044 * the vdev is accessible. If we're faulted, bail.
2045 */
2046 if (vd->vdev_faulted) {
2047 ASSERT(vd->vdev_children == 0);
2048 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
2049 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
2050 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
2051 vd->vdev_label_aux);
2052 return (SET_ERROR(ENXIO));
2053 }
2054
2055 if (vd->vdev_degraded) {
2056 ASSERT(vd->vdev_children == 0);
2057 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
2058 VDEV_AUX_ERR_EXCEEDED);
2059 } else {
2060 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
2061 }
2062
2063 /*
2064 * For hole or missing vdevs we just return success.
2065 */
2066 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
2067 return (0);
2068
2069 for (int c = 0; c < vd->vdev_children; c++) {
2070 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
2071 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
2072 VDEV_AUX_NONE);
2073 break;
2074 }
2075 }
2076
2077 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
2078 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
2079
2080 if (vd->vdev_children == 0) {
2081 if (osize < SPA_MINDEVSIZE) {
2082 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2083 VDEV_AUX_TOO_SMALL);
2084 return (SET_ERROR(EOVERFLOW));
2085 }
2086 psize = osize;
2087 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
2088 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
2089 VDEV_LABEL_END_SIZE);
2090 } else {
2091 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
2092 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
2093 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2094 VDEV_AUX_TOO_SMALL);
2095 return (SET_ERROR(EOVERFLOW));
2096 }
2097 psize = 0;
2098 asize = osize;
2099 max_asize = max_osize;
2100 }
2101
2102 /*
2103 * If the vdev was expanded, record this so that we can re-create the
2104 * uberblock rings in labels {2,3}, during the next sync.
2105 */
2106 if ((psize > vd->vdev_psize) && (vd->vdev_psize != 0))
2107 vd->vdev_copy_uberblocks = B_TRUE;
2108
2109 vd->vdev_psize = psize;
2110
2111 /*
2112 * Make sure the allocatable size hasn't shrunk too much.
2113 */
2114 if (asize < vd->vdev_min_asize) {
2115 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2116 VDEV_AUX_BAD_LABEL);
2117 return (SET_ERROR(EINVAL));
2118 }
2119
2120 /*
2121 * We can always set the logical/physical ashift members since
2122 * their values are only used to calculate the vdev_ashift when
2123 * the device is first added to the config. These values should
2124 * not be used for anything else since they may change whenever
2125 * the device is reopened and we don't store them in the label.
2126 */
2127 vd->vdev_physical_ashift =
2128 MAX(physical_ashift, vd->vdev_physical_ashift);
2129 vd->vdev_logical_ashift = MAX(logical_ashift,
2130 vd->vdev_logical_ashift);
2131
2132 if (vd->vdev_asize == 0) {
2133 /*
2134 * This is the first-ever open, so use the computed values.
2135 * For compatibility, a different ashift can be requested.
2136 */
2137 vd->vdev_asize = asize;
2138 vd->vdev_max_asize = max_asize;
2139
2140 /*
2141 * If the vdev_ashift was not overridden at creation time,
2142 * then set it the logical ashift and optimize the ashift.
2143 */
2144 if (vd->vdev_ashift == 0) {
2145 vd->vdev_ashift = vd->vdev_logical_ashift;
2146
2147 if (vd->vdev_logical_ashift > ASHIFT_MAX) {
2148 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2149 VDEV_AUX_ASHIFT_TOO_BIG);
2150 return (SET_ERROR(EDOM));
2151 }
2152
2153 if (vd->vdev_top == vd && vd->vdev_attaching == B_FALSE)
2154 vdev_ashift_optimize(vd);
2155 vd->vdev_attaching = B_FALSE;
2156 }
2157 if (vd->vdev_ashift != 0 && (vd->vdev_ashift < ASHIFT_MIN ||
2158 vd->vdev_ashift > ASHIFT_MAX)) {
2159 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2160 VDEV_AUX_BAD_ASHIFT);
2161 return (SET_ERROR(EDOM));
2162 }
2163 } else {
2164 /*
2165 * Make sure the alignment required hasn't increased.
2166 */
2167 if (vd->vdev_ashift > vd->vdev_top->vdev_ashift &&
2168 vd->vdev_ops->vdev_op_leaf) {
2169 (void) zfs_ereport_post(
2170 FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT,
2171 spa, vd, NULL, NULL, 0);
2172 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2173 VDEV_AUX_BAD_LABEL);
2174 return (SET_ERROR(EDOM));
2175 }
2176 vd->vdev_max_asize = max_asize;
2177 }
2178
2179 /*
2180 * If all children are healthy we update asize if either:
2181 * The asize has increased, due to a device expansion caused by dynamic
2182 * LUN growth or vdev replacement, and automatic expansion is enabled;
2183 * making the additional space available.
2184 *
2185 * The asize has decreased, due to a device shrink usually caused by a
2186 * vdev replace with a smaller device. This ensures that calculations
2187 * based of max_asize and asize e.g. esize are always valid. It's safe
2188 * to do this as we've already validated that asize is greater than
2189 * vdev_min_asize.
2190 */
2191 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
2192 ((asize > vd->vdev_asize &&
2193 (vd->vdev_expanding || spa->spa_autoexpand)) ||
2194 (asize < vd->vdev_asize)))
2195 vd->vdev_asize = asize;
2196
2197 vdev_set_min_asize(vd);
2198
2199 /*
2200 * Ensure we can issue some IO before declaring the
2201 * vdev open for business.
2202 */
2203 if (vd->vdev_ops->vdev_op_leaf &&
2204 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
2205 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
2206 VDEV_AUX_ERR_EXCEEDED);
2207 return (error);
2208 }
2209
2210 /*
2211 * Track the minimum allocation size.
2212 */
2213 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
2214 vd->vdev_islog == 0 && vd->vdev_aux == NULL) {
2215 uint64_t min_alloc = vdev_get_min_alloc(vd);
2216 if (min_alloc < spa->spa_min_alloc)
2217 spa->spa_min_alloc = min_alloc;
2218 }
2219
2220 /*
2221 * If this is a leaf vdev, assess whether a resilver is needed.
2222 * But don't do this if we are doing a reopen for a scrub, since
2223 * this would just restart the scrub we are already doing.
2224 */
2225 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen)
2226 dsl_scan_assess_vdev(spa->spa_dsl_pool, vd);
2227
2228 return (0);
2229 }
2230
2231 static void
2232 vdev_validate_child(void *arg)
2233 {
2234 vdev_t *vd = arg;
2235
2236 vd->vdev_validate_thread = curthread;
2237 vd->vdev_validate_error = vdev_validate(vd);
2238 vd->vdev_validate_thread = NULL;
2239 }
2240
2241 /*
2242 * Called once the vdevs are all opened, this routine validates the label
2243 * contents. This needs to be done before vdev_load() so that we don't
2244 * inadvertently do repair I/Os to the wrong device.
2245 *
2246 * This function will only return failure if one of the vdevs indicates that it
2247 * has since been destroyed or exported. This is only possible if
2248 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
2249 * will be updated but the function will return 0.
2250 */
2251 int
2252 vdev_validate(vdev_t *vd)
2253 {
2254 spa_t *spa = vd->vdev_spa;
2255 taskq_t *tq = NULL;
2256 nvlist_t *label;
2257 uint64_t guid = 0, aux_guid = 0, top_guid;
2258 uint64_t state;
2259 nvlist_t *nvl;
2260 uint64_t txg;
2261 int children = vd->vdev_children;
2262
2263 if (vdev_validate_skip)
2264 return (0);
2265
2266 if (children > 0) {
2267 tq = taskq_create("vdev_validate", children, minclsyspri,
2268 children, children, TASKQ_PREPOPULATE);
2269 }
2270
2271 for (uint64_t c = 0; c < children; c++) {
2272 vdev_t *cvd = vd->vdev_child[c];
2273
2274 if (tq == NULL || vdev_uses_zvols(cvd)) {
2275 vdev_validate_child(cvd);
2276 } else {
2277 VERIFY(taskq_dispatch(tq, vdev_validate_child, cvd,
2278 TQ_SLEEP) != TASKQID_INVALID);
2279 }
2280 }
2281 if (tq != NULL) {
2282 taskq_wait(tq);
2283 taskq_destroy(tq);
2284 }
2285 for (int c = 0; c < children; c++) {
2286 int error = vd->vdev_child[c]->vdev_validate_error;
2287
2288 if (error != 0)
2289 return (SET_ERROR(EBADF));
2290 }
2291
2292
2293 /*
2294 * If the device has already failed, or was marked offline, don't do
2295 * any further validation. Otherwise, label I/O will fail and we will
2296 * overwrite the previous state.
2297 */
2298 if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
2299 return (0);
2300
2301 /*
2302 * If we are performing an extreme rewind, we allow for a label that
2303 * was modified at a point after the current txg.
2304 * If config lock is not held do not check for the txg. spa_sync could
2305 * be updating the vdev's label before updating spa_last_synced_txg.
2306 */
2307 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 ||
2308 spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG)
2309 txg = UINT64_MAX;
2310 else
2311 txg = spa_last_synced_txg(spa);
2312
2313 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
2314 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2315 VDEV_AUX_BAD_LABEL);
2316 vdev_dbgmsg(vd, "vdev_validate: failed reading config for "
2317 "txg %llu", (u_longlong_t)txg);
2318 return (0);
2319 }
2320
2321 /*
2322 * Determine if this vdev has been split off into another
2323 * pool. If so, then refuse to open it.
2324 */
2325 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
2326 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
2327 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2328 VDEV_AUX_SPLIT_POOL);
2329 nvlist_free(label);
2330 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
2331 return (0);
2332 }
2333
2334 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
2335 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2336 VDEV_AUX_CORRUPT_DATA);
2337 nvlist_free(label);
2338 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2339 ZPOOL_CONFIG_POOL_GUID);
2340 return (0);
2341 }
2342
2343 /*
2344 * If config is not trusted then ignore the spa guid check. This is
2345 * necessary because if the machine crashed during a re-guid the new
2346 * guid might have been written to all of the vdev labels, but not the
2347 * cached config. The check will be performed again once we have the
2348 * trusted config from the MOS.
2349 */
2350 if (spa->spa_trust_config && guid != spa_guid(spa)) {
2351 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2352 VDEV_AUX_CORRUPT_DATA);
2353 nvlist_free(label);
2354 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
2355 "match config (%llu != %llu)", (u_longlong_t)guid,
2356 (u_longlong_t)spa_guid(spa));
2357 return (0);
2358 }
2359
2360 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
2361 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
2362 &aux_guid) != 0)
2363 aux_guid = 0;
2364
2365 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
2366 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2367 VDEV_AUX_CORRUPT_DATA);
2368 nvlist_free(label);
2369 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2370 ZPOOL_CONFIG_GUID);
2371 return (0);
2372 }
2373
2374 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
2375 != 0) {
2376 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2377 VDEV_AUX_CORRUPT_DATA);
2378 nvlist_free(label);
2379 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2380 ZPOOL_CONFIG_TOP_GUID);
2381 return (0);
2382 }
2383
2384 /*
2385 * If this vdev just became a top-level vdev because its sibling was
2386 * detached, it will have adopted the parent's vdev guid -- but the
2387 * label may or may not be on disk yet. Fortunately, either version
2388 * of the label will have the same top guid, so if we're a top-level
2389 * vdev, we can safely compare to that instead.
2390 * However, if the config comes from a cachefile that failed to update
2391 * after the detach, a top-level vdev will appear as a non top-level
2392 * vdev in the config. Also relax the constraints if we perform an
2393 * extreme rewind.
2394 *
2395 * If we split this vdev off instead, then we also check the
2396 * original pool's guid. We don't want to consider the vdev
2397 * corrupt if it is partway through a split operation.
2398 */
2399 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
2400 boolean_t mismatch = B_FALSE;
2401 if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
2402 if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
2403 mismatch = B_TRUE;
2404 } else {
2405 if (vd->vdev_guid != top_guid &&
2406 vd->vdev_top->vdev_guid != guid)
2407 mismatch = B_TRUE;
2408 }
2409
2410 if (mismatch) {
2411 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2412 VDEV_AUX_CORRUPT_DATA);
2413 nvlist_free(label);
2414 vdev_dbgmsg(vd, "vdev_validate: config guid "
2415 "doesn't match label guid");
2416 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
2417 (u_longlong_t)vd->vdev_guid,
2418 (u_longlong_t)vd->vdev_top->vdev_guid);
2419 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
2420 "aux_guid %llu", (u_longlong_t)guid,
2421 (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
2422 return (0);
2423 }
2424 }
2425
2426 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
2427 &state) != 0) {
2428 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2429 VDEV_AUX_CORRUPT_DATA);
2430 nvlist_free(label);
2431 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2432 ZPOOL_CONFIG_POOL_STATE);
2433 return (0);
2434 }
2435
2436 nvlist_free(label);
2437
2438 /*
2439 * If this is a verbatim import, no need to check the
2440 * state of the pool.
2441 */
2442 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
2443 spa_load_state(spa) == SPA_LOAD_OPEN &&
2444 state != POOL_STATE_ACTIVE) {
2445 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
2446 "for spa %s", (u_longlong_t)state, spa->spa_name);
2447 return (SET_ERROR(EBADF));
2448 }
2449
2450 /*
2451 * If we were able to open and validate a vdev that was
2452 * previously marked permanently unavailable, clear that state
2453 * now.
2454 */
2455 if (vd->vdev_not_present)
2456 vd->vdev_not_present = 0;
2457
2458 return (0);
2459 }
2460
2461 static void
2462 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
2463 {
2464 char *old, *new;
2465 if (svd->vdev_path != NULL && dvd->vdev_path != NULL) {
2466 if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) {
2467 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
2468 "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
2469 dvd->vdev_path, svd->vdev_path);
2470 spa_strfree(dvd->vdev_path);
2471 dvd->vdev_path = spa_strdup(svd->vdev_path);
2472 }
2473 } else if (svd->vdev_path != NULL) {
2474 dvd->vdev_path = spa_strdup(svd->vdev_path);
2475 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
2476 (u_longlong_t)dvd->vdev_guid, dvd->vdev_path);
2477 }
2478
2479 /*
2480 * Our enclosure sysfs path may have changed between imports
2481 */
2482 old = dvd->vdev_enc_sysfs_path;
2483 new = svd->vdev_enc_sysfs_path;
2484 if ((old != NULL && new == NULL) ||
2485 (old == NULL && new != NULL) ||
2486 ((old != NULL && new != NULL) && strcmp(new, old) != 0)) {
2487 zfs_dbgmsg("vdev_copy_path: vdev %llu: vdev_enc_sysfs_path "
2488 "changed from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
2489 old, new);
2490
2491 if (dvd->vdev_enc_sysfs_path)
2492 spa_strfree(dvd->vdev_enc_sysfs_path);
2493
2494 if (svd->vdev_enc_sysfs_path) {
2495 dvd->vdev_enc_sysfs_path = spa_strdup(
2496 svd->vdev_enc_sysfs_path);
2497 } else {
2498 dvd->vdev_enc_sysfs_path = NULL;
2499 }
2500 }
2501 }
2502
2503 /*
2504 * Recursively copy vdev paths from one vdev to another. Source and destination
2505 * vdev trees must have same geometry otherwise return error. Intended to copy
2506 * paths from userland config into MOS config.
2507 */
2508 int
2509 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
2510 {
2511 if ((svd->vdev_ops == &vdev_missing_ops) ||
2512 (svd->vdev_ishole && dvd->vdev_ishole) ||
2513 (dvd->vdev_ops == &vdev_indirect_ops))
2514 return (0);
2515
2516 if (svd->vdev_ops != dvd->vdev_ops) {
2517 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
2518 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
2519 return (SET_ERROR(EINVAL));
2520 }
2521
2522 if (svd->vdev_guid != dvd->vdev_guid) {
2523 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
2524 "%llu)", (u_longlong_t)svd->vdev_guid,
2525 (u_longlong_t)dvd->vdev_guid);
2526 return (SET_ERROR(EINVAL));
2527 }
2528
2529 if (svd->vdev_children != dvd->vdev_children) {
2530 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
2531 "%llu != %llu", (u_longlong_t)svd->vdev_children,
2532 (u_longlong_t)dvd->vdev_children);
2533 return (SET_ERROR(EINVAL));
2534 }
2535
2536 for (uint64_t i = 0; i < svd->vdev_children; i++) {
2537 int error = vdev_copy_path_strict(svd->vdev_child[i],
2538 dvd->vdev_child[i]);
2539 if (error != 0)
2540 return (error);
2541 }
2542
2543 if (svd->vdev_ops->vdev_op_leaf)
2544 vdev_copy_path_impl(svd, dvd);
2545
2546 return (0);
2547 }
2548
2549 static void
2550 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
2551 {
2552 ASSERT(stvd->vdev_top == stvd);
2553 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
2554
2555 for (uint64_t i = 0; i < dvd->vdev_children; i++) {
2556 vdev_copy_path_search(stvd, dvd->vdev_child[i]);
2557 }
2558
2559 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
2560 return;
2561
2562 /*
2563 * The idea here is that while a vdev can shift positions within
2564 * a top vdev (when replacing, attaching mirror, etc.) it cannot
2565 * step outside of it.
2566 */
2567 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
2568
2569 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
2570 return;
2571
2572 ASSERT(vd->vdev_ops->vdev_op_leaf);
2573
2574 vdev_copy_path_impl(vd, dvd);
2575 }
2576
2577 /*
2578 * Recursively copy vdev paths from one root vdev to another. Source and
2579 * destination vdev trees may differ in geometry. For each destination leaf
2580 * vdev, search a vdev with the same guid and top vdev id in the source.
2581 * Intended to copy paths from userland config into MOS config.
2582 */
2583 void
2584 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
2585 {
2586 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
2587 ASSERT(srvd->vdev_ops == &vdev_root_ops);
2588 ASSERT(drvd->vdev_ops == &vdev_root_ops);
2589
2590 for (uint64_t i = 0; i < children; i++) {
2591 vdev_copy_path_search(srvd->vdev_child[i],
2592 drvd->vdev_child[i]);
2593 }
2594 }
2595
2596 /*
2597 * Close a virtual device.
2598 */
2599 void
2600 vdev_close(vdev_t *vd)
2601 {
2602 vdev_t *pvd = vd->vdev_parent;
2603 spa_t *spa __maybe_unused = vd->vdev_spa;
2604
2605 ASSERT(vd != NULL);
2606 ASSERT(vd->vdev_open_thread == curthread ||
2607 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2608
2609 /*
2610 * If our parent is reopening, then we are as well, unless we are
2611 * going offline.
2612 */
2613 if (pvd != NULL && pvd->vdev_reopening)
2614 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
2615
2616 vd->vdev_ops->vdev_op_close(vd);
2617
2618 /*
2619 * We record the previous state before we close it, so that if we are
2620 * doing a reopen(), we don't generate FMA ereports if we notice that
2621 * it's still faulted.
2622 */
2623 vd->vdev_prevstate = vd->vdev_state;
2624
2625 if (vd->vdev_offline)
2626 vd->vdev_state = VDEV_STATE_OFFLINE;
2627 else
2628 vd->vdev_state = VDEV_STATE_CLOSED;
2629 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2630 }
2631
2632 void
2633 vdev_hold(vdev_t *vd)
2634 {
2635 spa_t *spa = vd->vdev_spa;
2636
2637 ASSERT(spa_is_root(spa));
2638 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
2639 return;
2640
2641 for (int c = 0; c < vd->vdev_children; c++)
2642 vdev_hold(vd->vdev_child[c]);
2643
2644 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_hold != NULL)
2645 vd->vdev_ops->vdev_op_hold(vd);
2646 }
2647
2648 void
2649 vdev_rele(vdev_t *vd)
2650 {
2651 ASSERT(spa_is_root(vd->vdev_spa));
2652 for (int c = 0; c < vd->vdev_children; c++)
2653 vdev_rele(vd->vdev_child[c]);
2654
2655 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_rele != NULL)
2656 vd->vdev_ops->vdev_op_rele(vd);
2657 }
2658
2659 /*
2660 * Reopen all interior vdevs and any unopened leaves. We don't actually
2661 * reopen leaf vdevs which had previously been opened as they might deadlock
2662 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2663 * If the leaf has never been opened then open it, as usual.
2664 */
2665 void
2666 vdev_reopen(vdev_t *vd)
2667 {
2668 spa_t *spa = vd->vdev_spa;
2669
2670 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2671
2672 /* set the reopening flag unless we're taking the vdev offline */
2673 vd->vdev_reopening = !vd->vdev_offline;
2674 vdev_close(vd);
2675 (void) vdev_open(vd);
2676
2677 /*
2678 * Call vdev_validate() here to make sure we have the same device.
2679 * Otherwise, a device with an invalid label could be successfully
2680 * opened in response to vdev_reopen().
2681 */
2682 if (vd->vdev_aux) {
2683 (void) vdev_validate_aux(vd);
2684 if (vdev_readable(vd) && vdev_writeable(vd) &&
2685 vd->vdev_aux == &spa->spa_l2cache) {
2686 /*
2687 * In case the vdev is present we should evict all ARC
2688 * buffers and pointers to log blocks and reclaim their
2689 * space before restoring its contents to L2ARC.
2690 */
2691 if (l2arc_vdev_present(vd)) {
2692 l2arc_rebuild_vdev(vd, B_TRUE);
2693 } else {
2694 l2arc_add_vdev(spa, vd);
2695 }
2696 spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD);
2697 spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM);
2698 }
2699 } else {
2700 (void) vdev_validate(vd);
2701 }
2702
2703 /*
2704 * Recheck if resilver is still needed and cancel any
2705 * scheduled resilver if resilver is unneeded.
2706 */
2707 if (!vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL) &&
2708 spa->spa_async_tasks & SPA_ASYNC_RESILVER) {
2709 mutex_enter(&spa->spa_async_lock);
2710 spa->spa_async_tasks &= ~SPA_ASYNC_RESILVER;
2711 mutex_exit(&spa->spa_async_lock);
2712 }
2713
2714 /*
2715 * Reassess parent vdev's health.
2716 */
2717 vdev_propagate_state(vd);
2718 }
2719
2720 int
2721 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
2722 {
2723 int error;
2724
2725 /*
2726 * Normally, partial opens (e.g. of a mirror) are allowed.
2727 * For a create, however, we want to fail the request if
2728 * there are any components we can't open.
2729 */
2730 error = vdev_open(vd);
2731
2732 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
2733 vdev_close(vd);
2734 return (error ? error : SET_ERROR(ENXIO));
2735 }
2736
2737 /*
2738 * Recursively load DTLs and initialize all labels.
2739 */
2740 if ((error = vdev_dtl_load(vd)) != 0 ||
2741 (error = vdev_label_init(vd, txg, isreplacing ?
2742 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
2743 vdev_close(vd);
2744 return (error);
2745 }
2746
2747 return (0);
2748 }
2749
2750 void
2751 vdev_metaslab_set_size(vdev_t *vd)
2752 {
2753 uint64_t asize = vd->vdev_asize;
2754 uint64_t ms_count = asize >> zfs_vdev_default_ms_shift;
2755 uint64_t ms_shift;
2756
2757 /*
2758 * There are two dimensions to the metaslab sizing calculation:
2759 * the size of the metaslab and the count of metaslabs per vdev.
2760 *
2761 * The default values used below are a good balance between memory
2762 * usage (larger metaslab size means more memory needed for loaded
2763 * metaslabs; more metaslabs means more memory needed for the
2764 * metaslab_t structs), metaslab load time (larger metaslabs take
2765 * longer to load), and metaslab sync time (more metaslabs means
2766 * more time spent syncing all of them).
2767 *
2768 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2769 * The range of the dimensions are as follows:
2770 *
2771 * 2^29 <= ms_size <= 2^34
2772 * 16 <= ms_count <= 131,072
2773 *
2774 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2775 * at least 512MB (2^29) to minimize fragmentation effects when
2776 * testing with smaller devices. However, the count constraint
2777 * of at least 16 metaslabs will override this minimum size goal.
2778 *
2779 * On the upper end of vdev sizes, we aim for a maximum metaslab
2780 * size of 16GB. However, we will cap the total count to 2^17
2781 * metaslabs to keep our memory footprint in check and let the
2782 * metaslab size grow from there if that limit is hit.
2783 *
2784 * The net effect of applying above constrains is summarized below.
2785 *
2786 * vdev size metaslab count
2787 * --------------|-----------------
2788 * < 8GB ~16
2789 * 8GB - 100GB one per 512MB
2790 * 100GB - 3TB ~200
2791 * 3TB - 2PB one per 16GB
2792 * > 2PB ~131,072
2793 * --------------------------------
2794 *
2795 * Finally, note that all of the above calculate the initial
2796 * number of metaslabs. Expanding a top-level vdev will result
2797 * in additional metaslabs being allocated making it possible
2798 * to exceed the zfs_vdev_ms_count_limit.
2799 */
2800
2801 if (ms_count < zfs_vdev_min_ms_count)
2802 ms_shift = highbit64(asize / zfs_vdev_min_ms_count);
2803 else if (ms_count > zfs_vdev_default_ms_count)
2804 ms_shift = highbit64(asize / zfs_vdev_default_ms_count);
2805 else
2806 ms_shift = zfs_vdev_default_ms_shift;
2807
2808 if (ms_shift < SPA_MAXBLOCKSHIFT) {
2809 ms_shift = SPA_MAXBLOCKSHIFT;
2810 } else if (ms_shift > zfs_vdev_max_ms_shift) {
2811 ms_shift = zfs_vdev_max_ms_shift;
2812 /* cap the total count to constrain memory footprint */
2813 if ((asize >> ms_shift) > zfs_vdev_ms_count_limit)
2814 ms_shift = highbit64(asize / zfs_vdev_ms_count_limit);
2815 }
2816
2817 vd->vdev_ms_shift = ms_shift;
2818 ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT);
2819 }
2820
2821 void
2822 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2823 {
2824 ASSERT(vd == vd->vdev_top);
2825 /* indirect vdevs don't have metaslabs or dtls */
2826 ASSERT(vdev_is_concrete(vd) || flags == 0);
2827 ASSERT(ISP2(flags));
2828 ASSERT(spa_writeable(vd->vdev_spa));
2829
2830 if (flags & VDD_METASLAB)
2831 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2832
2833 if (flags & VDD_DTL)
2834 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2835
2836 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2837 }
2838
2839 void
2840 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
2841 {
2842 for (int c = 0; c < vd->vdev_children; c++)
2843 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
2844
2845 if (vd->vdev_ops->vdev_op_leaf)
2846 vdev_dirty(vd->vdev_top, flags, vd, txg);
2847 }
2848
2849 /*
2850 * DTLs.
2851 *
2852 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2853 * the vdev has less than perfect replication. There are four kinds of DTL:
2854 *
2855 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2856 *
2857 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2858 *
2859 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2860 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2861 * txgs that was scrubbed.
2862 *
2863 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2864 * persistent errors or just some device being offline.
2865 * Unlike the other three, the DTL_OUTAGE map is not generally
2866 * maintained; it's only computed when needed, typically to
2867 * determine whether a device can be detached.
2868 *
2869 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2870 * either has the data or it doesn't.
2871 *
2872 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2873 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2874 * if any child is less than fully replicated, then so is its parent.
2875 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2876 * comprising only those txgs which appear in 'maxfaults' or more children;
2877 * those are the txgs we don't have enough replication to read. For example,
2878 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2879 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2880 * two child DTL_MISSING maps.
2881 *
2882 * It should be clear from the above that to compute the DTLs and outage maps
2883 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2884 * Therefore, that is all we keep on disk. When loading the pool, or after
2885 * a configuration change, we generate all other DTLs from first principles.
2886 */
2887 void
2888 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2889 {
2890 range_tree_t *rt = vd->vdev_dtl[t];
2891
2892 ASSERT(t < DTL_TYPES);
2893 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2894 ASSERT(spa_writeable(vd->vdev_spa));
2895
2896 mutex_enter(&vd->vdev_dtl_lock);
2897 if (!range_tree_contains(rt, txg, size))
2898 range_tree_add(rt, txg, size);
2899 mutex_exit(&vd->vdev_dtl_lock);
2900 }
2901
2902 boolean_t
2903 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2904 {
2905 range_tree_t *rt = vd->vdev_dtl[t];
2906 boolean_t dirty = B_FALSE;
2907
2908 ASSERT(t < DTL_TYPES);
2909 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2910
2911 /*
2912 * While we are loading the pool, the DTLs have not been loaded yet.
2913 * This isn't a problem but it can result in devices being tried
2914 * which are known to not have the data. In which case, the import
2915 * is relying on the checksum to ensure that we get the right data.
2916 * Note that while importing we are only reading the MOS, which is
2917 * always checksummed.
2918 */
2919 mutex_enter(&vd->vdev_dtl_lock);
2920 if (!range_tree_is_empty(rt))
2921 dirty = range_tree_contains(rt, txg, size);
2922 mutex_exit(&vd->vdev_dtl_lock);
2923
2924 return (dirty);
2925 }
2926
2927 boolean_t
2928 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
2929 {
2930 range_tree_t *rt = vd->vdev_dtl[t];
2931 boolean_t empty;
2932
2933 mutex_enter(&vd->vdev_dtl_lock);
2934 empty = range_tree_is_empty(rt);
2935 mutex_exit(&vd->vdev_dtl_lock);
2936
2937 return (empty);
2938 }
2939
2940 /*
2941 * Check if the txg falls within the range which must be
2942 * resilvered. DVAs outside this range can always be skipped.
2943 */
2944 boolean_t
2945 vdev_default_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
2946 uint64_t phys_birth)
2947 {
2948 (void) dva, (void) psize;
2949
2950 /* Set by sequential resilver. */
2951 if (phys_birth == TXG_UNKNOWN)
2952 return (B_TRUE);
2953
2954 return (vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1));
2955 }
2956
2957 /*
2958 * Returns B_TRUE if the vdev determines the DVA needs to be resilvered.
2959 */
2960 boolean_t
2961 vdev_dtl_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
2962 uint64_t phys_birth)
2963 {
2964 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2965
2966 if (vd->vdev_ops->vdev_op_need_resilver == NULL ||
2967 vd->vdev_ops->vdev_op_leaf)
2968 return (B_TRUE);
2969
2970 return (vd->vdev_ops->vdev_op_need_resilver(vd, dva, psize,
2971 phys_birth));
2972 }
2973
2974 /*
2975 * Returns the lowest txg in the DTL range.
2976 */
2977 static uint64_t
2978 vdev_dtl_min(vdev_t *vd)
2979 {
2980 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2981 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2982 ASSERT0(vd->vdev_children);
2983
2984 return (range_tree_min(vd->vdev_dtl[DTL_MISSING]) - 1);
2985 }
2986
2987 /*
2988 * Returns the highest txg in the DTL.
2989 */
2990 static uint64_t
2991 vdev_dtl_max(vdev_t *vd)
2992 {
2993 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2994 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2995 ASSERT0(vd->vdev_children);
2996
2997 return (range_tree_max(vd->vdev_dtl[DTL_MISSING]));
2998 }
2999
3000 /*
3001 * Determine if a resilvering vdev should remove any DTL entries from
3002 * its range. If the vdev was resilvering for the entire duration of the
3003 * scan then it should excise that range from its DTLs. Otherwise, this
3004 * vdev is considered partially resilvered and should leave its DTL
3005 * entries intact. The comment in vdev_dtl_reassess() describes how we
3006 * excise the DTLs.
3007 */
3008 static boolean_t
3009 vdev_dtl_should_excise(vdev_t *vd, boolean_t rebuild_done)
3010 {
3011 ASSERT0(vd->vdev_children);
3012
3013 if (vd->vdev_state < VDEV_STATE_DEGRADED)
3014 return (B_FALSE);
3015
3016 if (vd->vdev_resilver_deferred)
3017 return (B_FALSE);
3018
3019 if (range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]))
3020 return (B_TRUE);
3021
3022 if (rebuild_done) {
3023 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config;
3024 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
3025
3026 /* Rebuild not initiated by attach */
3027 if (vd->vdev_rebuild_txg == 0)
3028 return (B_TRUE);
3029
3030 /*
3031 * When a rebuild completes without error then all missing data
3032 * up to the rebuild max txg has been reconstructed and the DTL
3033 * is eligible for excision.
3034 */
3035 if (vrp->vrp_rebuild_state == VDEV_REBUILD_COMPLETE &&
3036 vdev_dtl_max(vd) <= vrp->vrp_max_txg) {
3037 ASSERT3U(vrp->vrp_min_txg, <=, vdev_dtl_min(vd));
3038 ASSERT3U(vrp->vrp_min_txg, <, vd->vdev_rebuild_txg);
3039 ASSERT3U(vd->vdev_rebuild_txg, <=, vrp->vrp_max_txg);
3040 return (B_TRUE);
3041 }
3042 } else {
3043 dsl_scan_t *scn = vd->vdev_spa->spa_dsl_pool->dp_scan;
3044 dsl_scan_phys_t *scnp __maybe_unused = &scn->scn_phys;
3045
3046 /* Resilver not initiated by attach */
3047 if (vd->vdev_resilver_txg == 0)
3048 return (B_TRUE);
3049
3050 /*
3051 * When a resilver is initiated the scan will assign the
3052 * scn_max_txg value to the highest txg value that exists
3053 * in all DTLs. If this device's max DTL is not part of this
3054 * scan (i.e. it is not in the range (scn_min_txg, scn_max_txg]
3055 * then it is not eligible for excision.
3056 */
3057 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
3058 ASSERT3U(scnp->scn_min_txg, <=, vdev_dtl_min(vd));
3059 ASSERT3U(scnp->scn_min_txg, <, vd->vdev_resilver_txg);
3060 ASSERT3U(vd->vdev_resilver_txg, <=, scnp->scn_max_txg);
3061 return (B_TRUE);
3062 }
3063 }
3064
3065 return (B_FALSE);
3066 }
3067
3068 /*
3069 * Reassess DTLs after a config change or scrub completion. If txg == 0 no
3070 * write operations will be issued to the pool.
3071 */
3072 void
3073 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg,
3074 boolean_t scrub_done, boolean_t rebuild_done)
3075 {
3076 spa_t *spa = vd->vdev_spa;
3077 avl_tree_t reftree;
3078 int minref;
3079
3080 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
3081
3082 for (int c = 0; c < vd->vdev_children; c++)
3083 vdev_dtl_reassess(vd->vdev_child[c], txg,
3084 scrub_txg, scrub_done, rebuild_done);
3085
3086 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
3087 return;
3088
3089 if (vd->vdev_ops->vdev_op_leaf) {
3090 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
3091 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config;
3092 boolean_t check_excise = B_FALSE;
3093 boolean_t wasempty = B_TRUE;
3094
3095 mutex_enter(&vd->vdev_dtl_lock);
3096
3097 /*
3098 * If requested, pretend the scan or rebuild completed cleanly.
3099 */
3100 if (zfs_scan_ignore_errors) {
3101 if (scn != NULL)
3102 scn->scn_phys.scn_errors = 0;
3103 if (vr != NULL)
3104 vr->vr_rebuild_phys.vrp_errors = 0;
3105 }
3106
3107 if (scrub_txg != 0 &&
3108 !range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
3109 wasempty = B_FALSE;
3110 zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d "
3111 "dtl:%llu/%llu errors:%llu",
3112 (u_longlong_t)vd->vdev_guid, (u_longlong_t)txg,
3113 (u_longlong_t)scrub_txg, spa->spa_scrub_started,
3114 (u_longlong_t)vdev_dtl_min(vd),
3115 (u_longlong_t)vdev_dtl_max(vd),
3116 (u_longlong_t)(scn ? scn->scn_phys.scn_errors : 0));
3117 }
3118
3119 /*
3120 * If we've completed a scrub/resilver or a rebuild cleanly
3121 * then determine if this vdev should remove any DTLs. We
3122 * only want to excise regions on vdevs that were available
3123 * during the entire duration of this scan.
3124 */
3125 if (rebuild_done &&
3126 vr != NULL && vr->vr_rebuild_phys.vrp_errors == 0) {
3127 check_excise = B_TRUE;
3128 } else {
3129 if (spa->spa_scrub_started ||
3130 (scn != NULL && scn->scn_phys.scn_errors == 0)) {
3131 check_excise = B_TRUE;
3132 }
3133 }
3134
3135 if (scrub_txg && check_excise &&
3136 vdev_dtl_should_excise(vd, rebuild_done)) {
3137 /*
3138 * We completed a scrub, resilver or rebuild up to
3139 * scrub_txg. If we did it without rebooting, then
3140 * the scrub dtl will be valid, so excise the old
3141 * region and fold in the scrub dtl. Otherwise,
3142 * leave the dtl as-is if there was an error.
3143 *
3144 * There's little trick here: to excise the beginning
3145 * of the DTL_MISSING map, we put it into a reference
3146 * tree and then add a segment with refcnt -1 that
3147 * covers the range [0, scrub_txg). This means
3148 * that each txg in that range has refcnt -1 or 0.
3149 * We then add DTL_SCRUB with a refcnt of 2, so that
3150 * entries in the range [0, scrub_txg) will have a
3151 * positive refcnt -- either 1 or 2. We then convert
3152 * the reference tree into the new DTL_MISSING map.
3153 */
3154 space_reftree_create(&reftree);
3155 space_reftree_add_map(&reftree,
3156 vd->vdev_dtl[DTL_MISSING], 1);
3157 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
3158 space_reftree_add_map(&reftree,
3159 vd->vdev_dtl[DTL_SCRUB], 2);
3160 space_reftree_generate_map(&reftree,
3161 vd->vdev_dtl[DTL_MISSING], 1);
3162 space_reftree_destroy(&reftree);
3163
3164 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
3165 zfs_dbgmsg("update DTL_MISSING:%llu/%llu",
3166 (u_longlong_t)vdev_dtl_min(vd),
3167 (u_longlong_t)vdev_dtl_max(vd));
3168 } else if (!wasempty) {
3169 zfs_dbgmsg("DTL_MISSING is now empty");
3170 }
3171 }
3172 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
3173 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
3174 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
3175 if (scrub_done)
3176 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
3177 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
3178 if (!vdev_readable(vd))
3179 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
3180 else
3181 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
3182 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
3183
3184 /*
3185 * If the vdev was resilvering or rebuilding and no longer
3186 * has any DTLs then reset the appropriate flag and dirty
3187 * the top level so that we persist the change.
3188 */
3189 if (txg != 0 &&
3190 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
3191 range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) {
3192 if (vd->vdev_rebuild_txg != 0) {
3193 vd->vdev_rebuild_txg = 0;
3194 vdev_config_dirty(vd->vdev_top);
3195 } else if (vd->vdev_resilver_txg != 0) {
3196 vd->vdev_resilver_txg = 0;
3197 vdev_config_dirty(vd->vdev_top);
3198 }
3199 }
3200
3201 mutex_exit(&vd->vdev_dtl_lock);
3202
3203 if (txg != 0)
3204 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
3205 return;
3206 }
3207
3208 mutex_enter(&vd->vdev_dtl_lock);
3209 for (int t = 0; t < DTL_TYPES; t++) {
3210 /* account for child's outage in parent's missing map */
3211 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
3212 if (t == DTL_SCRUB)
3213 continue; /* leaf vdevs only */
3214 if (t == DTL_PARTIAL)
3215 minref = 1; /* i.e. non-zero */
3216 else if (vdev_get_nparity(vd) != 0)
3217 minref = vdev_get_nparity(vd) + 1; /* RAID-Z, dRAID */
3218 else
3219 minref = vd->vdev_children; /* any kind of mirror */
3220 space_reftree_create(&reftree);
3221 for (int c = 0; c < vd->vdev_children; c++) {
3222 vdev_t *cvd = vd->vdev_child[c];
3223 mutex_enter(&cvd->vdev_dtl_lock);
3224 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
3225 mutex_exit(&cvd->vdev_dtl_lock);
3226 }
3227 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
3228 space_reftree_destroy(&reftree);
3229 }
3230 mutex_exit(&vd->vdev_dtl_lock);
3231 }
3232
3233 /*
3234 * Iterate over all the vdevs except spare, and post kobj events
3235 */
3236 void
3237 vdev_post_kobj_evt(vdev_t *vd)
3238 {
3239 if (vd->vdev_ops->vdev_op_kobj_evt_post &&
3240 vd->vdev_kobj_flag == B_FALSE) {
3241 vd->vdev_kobj_flag = B_TRUE;
3242 vd->vdev_ops->vdev_op_kobj_evt_post(vd);
3243 }
3244
3245 for (int c = 0; c < vd->vdev_children; c++)
3246 vdev_post_kobj_evt(vd->vdev_child[c]);
3247 }
3248
3249 /*
3250 * Iterate over all the vdevs except spare, and clear kobj events
3251 */
3252 void
3253 vdev_clear_kobj_evt(vdev_t *vd)
3254 {
3255 vd->vdev_kobj_flag = B_FALSE;
3256
3257 for (int c = 0; c < vd->vdev_children; c++)
3258 vdev_clear_kobj_evt(vd->vdev_child[c]);
3259 }
3260
3261 int
3262 vdev_dtl_load(vdev_t *vd)
3263 {
3264 spa_t *spa = vd->vdev_spa;
3265 objset_t *mos = spa->spa_meta_objset;
3266 range_tree_t *rt;
3267 int error = 0;
3268
3269 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
3270 ASSERT(vdev_is_concrete(vd));
3271
3272 /*
3273 * If the dtl cannot be sync'd there is no need to open it.
3274 */
3275 if (spa->spa_mode == SPA_MODE_READ && !spa->spa_read_spacemaps)
3276 return (0);
3277
3278 error = space_map_open(&vd->vdev_dtl_sm, mos,
3279 vd->vdev_dtl_object, 0, -1ULL, 0);
3280 if (error)
3281 return (error);
3282 ASSERT(vd->vdev_dtl_sm != NULL);
3283
3284 rt = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
3285 error = space_map_load(vd->vdev_dtl_sm, rt, SM_ALLOC);
3286 if (error == 0) {
3287 mutex_enter(&vd->vdev_dtl_lock);
3288 range_tree_walk(rt, range_tree_add,
3289 vd->vdev_dtl[DTL_MISSING]);
3290 mutex_exit(&vd->vdev_dtl_lock);
3291 }
3292
3293 range_tree_vacate(rt, NULL, NULL);
3294 range_tree_destroy(rt);
3295
3296 return (error);
3297 }
3298
3299 for (int c = 0; c < vd->vdev_children; c++) {
3300 error = vdev_dtl_load(vd->vdev_child[c]);
3301 if (error != 0)
3302 break;
3303 }
3304
3305 return (error);
3306 }
3307
3308 static void
3309 vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx)
3310 {
3311 spa_t *spa = vd->vdev_spa;
3312 objset_t *mos = spa->spa_meta_objset;
3313 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
3314 const char *string;
3315
3316 ASSERT(alloc_bias != VDEV_BIAS_NONE);
3317
3318 string =
3319 (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG :
3320 (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL :
3321 (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL;
3322
3323 ASSERT(string != NULL);
3324 VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS,
3325 1, strlen(string) + 1, string, tx));
3326
3327 if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) {
3328 spa_activate_allocation_classes(spa, tx);
3329 }
3330 }
3331
3332 void
3333 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
3334 {
3335 spa_t *spa = vd->vdev_spa;
3336
3337 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
3338 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
3339 zapobj, tx));
3340 }
3341
3342 uint64_t
3343 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
3344 {
3345 spa_t *spa = vd->vdev_spa;
3346 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
3347 DMU_OT_NONE, 0, tx);
3348
3349 ASSERT(zap != 0);
3350 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
3351 zap, tx));
3352
3353 return (zap);
3354 }
3355
3356 void
3357 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
3358 {
3359 if (vd->vdev_ops != &vdev_hole_ops &&
3360 vd->vdev_ops != &vdev_missing_ops &&
3361 vd->vdev_ops != &vdev_root_ops &&
3362 !vd->vdev_top->vdev_removing) {
3363 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
3364 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
3365 }
3366 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
3367 vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
3368 if (vd->vdev_alloc_bias != VDEV_BIAS_NONE)
3369 vdev_zap_allocation_data(vd, tx);
3370 }
3371 }
3372 if (vd->vdev_ops == &vdev_root_ops && vd->vdev_root_zap == 0 &&
3373 spa_feature_is_enabled(vd->vdev_spa, SPA_FEATURE_AVZ_V2)) {
3374 if (!spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_AVZ_V2))
3375 spa_feature_incr(vd->vdev_spa, SPA_FEATURE_AVZ_V2, tx);
3376 vd->vdev_root_zap = vdev_create_link_zap(vd, tx);
3377 }
3378
3379 for (uint64_t i = 0; i < vd->vdev_children; i++) {
3380 vdev_construct_zaps(vd->vdev_child[i], tx);
3381 }
3382 }
3383
3384 static void
3385 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
3386 {
3387 spa_t *spa = vd->vdev_spa;
3388 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
3389 objset_t *mos = spa->spa_meta_objset;
3390 range_tree_t *rtsync;
3391 dmu_tx_t *tx;
3392 uint64_t object = space_map_object(vd->vdev_dtl_sm);
3393
3394 ASSERT(vdev_is_concrete(vd));
3395 ASSERT(vd->vdev_ops->vdev_op_leaf);
3396
3397 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3398
3399 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
3400 mutex_enter(&vd->vdev_dtl_lock);
3401 space_map_free(vd->vdev_dtl_sm, tx);
3402 space_map_close(vd->vdev_dtl_sm);
3403 vd->vdev_dtl_sm = NULL;
3404 mutex_exit(&vd->vdev_dtl_lock);
3405
3406 /*
3407 * We only destroy the leaf ZAP for detached leaves or for
3408 * removed log devices. Removed data devices handle leaf ZAP
3409 * cleanup later, once cancellation is no longer possible.
3410 */
3411 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
3412 vd->vdev_top->vdev_islog)) {
3413 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
3414 vd->vdev_leaf_zap = 0;
3415 }
3416
3417 dmu_tx_commit(tx);
3418 return;
3419 }
3420
3421 if (vd->vdev_dtl_sm == NULL) {
3422 uint64_t new_object;
3423
3424 new_object = space_map_alloc(mos, zfs_vdev_dtl_sm_blksz, tx);
3425 VERIFY3U(new_object, !=, 0);
3426
3427 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
3428 0, -1ULL, 0));
3429 ASSERT(vd->vdev_dtl_sm != NULL);
3430 }
3431
3432 rtsync = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
3433
3434 mutex_enter(&vd->vdev_dtl_lock);
3435 range_tree_walk(rt, range_tree_add, rtsync);
3436 mutex_exit(&vd->vdev_dtl_lock);
3437
3438 space_map_truncate(vd->vdev_dtl_sm, zfs_vdev_dtl_sm_blksz, tx);
3439 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx);
3440 range_tree_vacate(rtsync, NULL, NULL);
3441
3442 range_tree_destroy(rtsync);
3443
3444 /*
3445 * If the object for the space map has changed then dirty
3446 * the top level so that we update the config.
3447 */
3448 if (object != space_map_object(vd->vdev_dtl_sm)) {
3449 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
3450 "new object %llu", (u_longlong_t)txg, spa_name(spa),
3451 (u_longlong_t)object,
3452 (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
3453 vdev_config_dirty(vd->vdev_top);
3454 }
3455
3456 dmu_tx_commit(tx);
3457 }
3458
3459 /*
3460 * Determine whether the specified vdev can be offlined/detached/removed
3461 * without losing data.
3462 */
3463 boolean_t
3464 vdev_dtl_required(vdev_t *vd)
3465 {
3466 spa_t *spa = vd->vdev_spa;
3467 vdev_t *tvd = vd->vdev_top;
3468 uint8_t cant_read = vd->vdev_cant_read;
3469 boolean_t required;
3470
3471 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3472
3473 if (vd == spa->spa_root_vdev || vd == tvd)
3474 return (B_TRUE);
3475
3476 /*
3477 * Temporarily mark the device as unreadable, and then determine
3478 * whether this results in any DTL outages in the top-level vdev.
3479 * If not, we can safely offline/detach/remove the device.
3480 */
3481 vd->vdev_cant_read = B_TRUE;
3482 vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE);
3483 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
3484 vd->vdev_cant_read = cant_read;
3485 vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE);
3486
3487 if (!required && zio_injection_enabled) {
3488 required = !!zio_handle_device_injection(vd, NULL,
3489 SET_ERROR(ECHILD));
3490 }
3491
3492 return (required);
3493 }
3494
3495 /*
3496 * Determine if resilver is needed, and if so the txg range.
3497 */
3498 boolean_t
3499 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
3500 {
3501 boolean_t needed = B_FALSE;
3502 uint64_t thismin = UINT64_MAX;
3503 uint64_t thismax = 0;
3504
3505 if (vd->vdev_children == 0) {
3506 mutex_enter(&vd->vdev_dtl_lock);
3507 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
3508 vdev_writeable(vd)) {
3509
3510 thismin = vdev_dtl_min(vd);
3511 thismax = vdev_dtl_max(vd);
3512 needed = B_TRUE;
3513 }
3514 mutex_exit(&vd->vdev_dtl_lock);
3515 } else {
3516 for (int c = 0; c < vd->vdev_children; c++) {
3517 vdev_t *cvd = vd->vdev_child[c];
3518 uint64_t cmin, cmax;
3519
3520 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
3521 thismin = MIN(thismin, cmin);
3522 thismax = MAX(thismax, cmax);
3523 needed = B_TRUE;
3524 }
3525 }
3526 }
3527
3528 if (needed && minp) {
3529 *minp = thismin;
3530 *maxp = thismax;
3531 }
3532 return (needed);
3533 }
3534
3535 /*
3536 * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj
3537 * will contain either the checkpoint spacemap object or zero if none exists.
3538 * All other errors are returned to the caller.
3539 */
3540 int
3541 vdev_checkpoint_sm_object(vdev_t *vd, uint64_t *sm_obj)
3542 {
3543 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
3544
3545 if (vd->vdev_top_zap == 0) {
3546 *sm_obj = 0;
3547 return (0);
3548 }
3549
3550 int error = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap,
3551 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, sm_obj);
3552 if (error == ENOENT) {
3553 *sm_obj = 0;
3554 error = 0;
3555 }
3556
3557 return (error);
3558 }
3559
3560 int
3561 vdev_load(vdev_t *vd)
3562 {
3563 int children = vd->vdev_children;
3564 int error = 0;
3565 taskq_t *tq = NULL;
3566
3567 /*
3568 * It's only worthwhile to use the taskq for the root vdev, because the
3569 * slow part is metaslab_init, and that only happens for top-level
3570 * vdevs.
3571 */
3572 if (vd->vdev_ops == &vdev_root_ops && vd->vdev_children > 0) {
3573 tq = taskq_create("vdev_load", children, minclsyspri,
3574 children, children, TASKQ_PREPOPULATE);
3575 }
3576
3577 /*
3578 * Recursively load all children.
3579 */
3580 for (int c = 0; c < vd->vdev_children; c++) {
3581 vdev_t *cvd = vd->vdev_child[c];
3582
3583 if (tq == NULL || vdev_uses_zvols(cvd)) {
3584 cvd->vdev_load_error = vdev_load(cvd);
3585 } else {
3586 VERIFY(taskq_dispatch(tq, vdev_load_child,
3587 cvd, TQ_SLEEP) != TASKQID_INVALID);
3588 }
3589 }
3590
3591 if (tq != NULL) {
3592 taskq_wait(tq);
3593 taskq_destroy(tq);
3594 }
3595
3596 for (int c = 0; c < vd->vdev_children; c++) {
3597 int error = vd->vdev_child[c]->vdev_load_error;
3598
3599 if (error != 0)
3600 return (error);
3601 }
3602
3603 vdev_set_deflate_ratio(vd);
3604
3605 /*
3606 * On spa_load path, grab the allocation bias from our zap
3607 */
3608 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3609 spa_t *spa = vd->vdev_spa;
3610 char bias_str[64];
3611
3612 error = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
3613 VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str),
3614 bias_str);
3615 if (error == 0) {
3616 ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE);
3617 vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str);
3618 } else if (error != ENOENT) {
3619 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3620 VDEV_AUX_CORRUPT_DATA);
3621 vdev_dbgmsg(vd, "vdev_load: zap_lookup(top_zap=%llu) "
3622 "failed [error=%d]",
3623 (u_longlong_t)vd->vdev_top_zap, error);
3624 return (error);
3625 }
3626 }
3627
3628 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3629 spa_t *spa = vd->vdev_spa;
3630 uint64_t failfast;
3631
3632 error = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
3633 vdev_prop_to_name(VDEV_PROP_FAILFAST), sizeof (failfast),
3634 1, &failfast);
3635 if (error == 0) {
3636 vd->vdev_failfast = failfast & 1;
3637 } else if (error == ENOENT) {
3638 vd->vdev_failfast = vdev_prop_default_numeric(
3639 VDEV_PROP_FAILFAST);
3640 } else {
3641 vdev_dbgmsg(vd,
3642 "vdev_load: zap_lookup(top_zap=%llu) "
3643 "failed [error=%d]",
3644 (u_longlong_t)vd->vdev_top_zap, error);
3645 }
3646 }
3647
3648 /*
3649 * Load any rebuild state from the top-level vdev zap.
3650 */
3651 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3652 error = vdev_rebuild_load(vd);
3653 if (error && error != ENOTSUP) {
3654 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3655 VDEV_AUX_CORRUPT_DATA);
3656 vdev_dbgmsg(vd, "vdev_load: vdev_rebuild_load "
3657 "failed [error=%d]", error);
3658 return (error);
3659 }
3660 }
3661
3662 if (vd->vdev_top_zap != 0 || vd->vdev_leaf_zap != 0) {
3663 uint64_t zapobj;
3664
3665 if (vd->vdev_top_zap != 0)
3666 zapobj = vd->vdev_top_zap;
3667 else
3668 zapobj = vd->vdev_leaf_zap;
3669
3670 error = vdev_prop_get_int(vd, VDEV_PROP_CHECKSUM_N,
3671 &vd->vdev_checksum_n);
3672 if (error && error != ENOENT)
3673 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3674 "failed [error=%d]", (u_longlong_t)zapobj, error);
3675
3676 error = vdev_prop_get_int(vd, VDEV_PROP_CHECKSUM_T,
3677 &vd->vdev_checksum_t);
3678 if (error && error != ENOENT)
3679 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3680 "failed [error=%d]", (u_longlong_t)zapobj, error);
3681
3682 error = vdev_prop_get_int(vd, VDEV_PROP_IO_N,
3683 &vd->vdev_io_n);
3684 if (error && error != ENOENT)
3685 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3686 "failed [error=%d]", (u_longlong_t)zapobj, error);
3687
3688 error = vdev_prop_get_int(vd, VDEV_PROP_IO_T,
3689 &vd->vdev_io_t);
3690 if (error && error != ENOENT)
3691 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3692 "failed [error=%d]", (u_longlong_t)zapobj, error);
3693 }
3694
3695 /*
3696 * If this is a top-level vdev, initialize its metaslabs.
3697 */
3698 if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
3699 vdev_metaslab_group_create(vd);
3700
3701 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
3702 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3703 VDEV_AUX_CORRUPT_DATA);
3704 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
3705 "asize=%llu", (u_longlong_t)vd->vdev_ashift,
3706 (u_longlong_t)vd->vdev_asize);
3707 return (SET_ERROR(ENXIO));
3708 }
3709
3710 error = vdev_metaslab_init(vd, 0);
3711 if (error != 0) {
3712 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
3713 "[error=%d]", error);
3714 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3715 VDEV_AUX_CORRUPT_DATA);
3716 return (error);
3717 }
3718
3719 uint64_t checkpoint_sm_obj;
3720 error = vdev_checkpoint_sm_object(vd, &checkpoint_sm_obj);
3721 if (error == 0 && checkpoint_sm_obj != 0) {
3722 objset_t *mos = spa_meta_objset(vd->vdev_spa);
3723 ASSERT(vd->vdev_asize != 0);
3724 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL);
3725
3726 error = space_map_open(&vd->vdev_checkpoint_sm,
3727 mos, checkpoint_sm_obj, 0, vd->vdev_asize,
3728 vd->vdev_ashift);
3729 if (error != 0) {
3730 vdev_dbgmsg(vd, "vdev_load: space_map_open "
3731 "failed for checkpoint spacemap (obj %llu) "
3732 "[error=%d]",
3733 (u_longlong_t)checkpoint_sm_obj, error);
3734 return (error);
3735 }
3736 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
3737
3738 /*
3739 * Since the checkpoint_sm contains free entries
3740 * exclusively we can use space_map_allocated() to
3741 * indicate the cumulative checkpointed space that
3742 * has been freed.
3743 */
3744 vd->vdev_stat.vs_checkpoint_space =
3745 -space_map_allocated(vd->vdev_checkpoint_sm);
3746 vd->vdev_spa->spa_checkpoint_info.sci_dspace +=
3747 vd->vdev_stat.vs_checkpoint_space;
3748 } else if (error != 0) {
3749 vdev_dbgmsg(vd, "vdev_load: failed to retrieve "
3750 "checkpoint space map object from vdev ZAP "
3751 "[error=%d]", error);
3752 return (error);
3753 }
3754 }
3755
3756 /*
3757 * If this is a leaf vdev, load its DTL.
3758 */
3759 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
3760 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3761 VDEV_AUX_CORRUPT_DATA);
3762 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
3763 "[error=%d]", error);
3764 return (error);
3765 }
3766
3767 uint64_t obsolete_sm_object;
3768 error = vdev_obsolete_sm_object(vd, &obsolete_sm_object);
3769 if (error == 0 && obsolete_sm_object != 0) {
3770 objset_t *mos = vd->vdev_spa->spa_meta_objset;
3771 ASSERT(vd->vdev_asize != 0);
3772 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
3773
3774 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
3775 obsolete_sm_object, 0, vd->vdev_asize, 0))) {
3776 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3777 VDEV_AUX_CORRUPT_DATA);
3778 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
3779 "obsolete spacemap (obj %llu) [error=%d]",
3780 (u_longlong_t)obsolete_sm_object, error);
3781 return (error);
3782 }
3783 } else if (error != 0) {
3784 vdev_dbgmsg(vd, "vdev_load: failed to retrieve obsolete "
3785 "space map object from vdev ZAP [error=%d]", error);
3786 return (error);
3787 }
3788
3789 return (0);
3790 }
3791
3792 /*
3793 * The special vdev case is used for hot spares and l2cache devices. Its
3794 * sole purpose it to set the vdev state for the associated vdev. To do this,
3795 * we make sure that we can open the underlying device, then try to read the
3796 * label, and make sure that the label is sane and that it hasn't been
3797 * repurposed to another pool.
3798 */
3799 int
3800 vdev_validate_aux(vdev_t *vd)
3801 {
3802 nvlist_t *label;
3803 uint64_t guid, version;
3804 uint64_t state;
3805
3806 if (!vdev_readable(vd))
3807 return (0);
3808
3809 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
3810 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3811 VDEV_AUX_CORRUPT_DATA);
3812 return (-1);
3813 }
3814
3815 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
3816 !SPA_VERSION_IS_SUPPORTED(version) ||
3817 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
3818 guid != vd->vdev_guid ||
3819 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
3820 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3821 VDEV_AUX_CORRUPT_DATA);
3822 nvlist_free(label);
3823 return (-1);
3824 }
3825
3826 /*
3827 * We don't actually check the pool state here. If it's in fact in
3828 * use by another pool, we update this fact on the fly when requested.
3829 */
3830 nvlist_free(label);
3831 return (0);
3832 }
3833
3834 static void
3835 vdev_destroy_ms_flush_data(vdev_t *vd, dmu_tx_t *tx)
3836 {
3837 objset_t *mos = spa_meta_objset(vd->vdev_spa);
3838
3839 if (vd->vdev_top_zap == 0)
3840 return;
3841
3842 uint64_t object = 0;
3843 int err = zap_lookup(mos, vd->vdev_top_zap,
3844 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, sizeof (uint64_t), 1, &object);
3845 if (err == ENOENT)
3846 return;
3847 VERIFY0(err);
3848
3849 VERIFY0(dmu_object_free(mos, object, tx));
3850 VERIFY0(zap_remove(mos, vd->vdev_top_zap,
3851 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, tx));
3852 }
3853
3854 /*
3855 * Free the objects used to store this vdev's spacemaps, and the array
3856 * that points to them.
3857 */
3858 void
3859 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
3860 {
3861 if (vd->vdev_ms_array == 0)
3862 return;
3863
3864 objset_t *mos = vd->vdev_spa->spa_meta_objset;
3865 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
3866 size_t array_bytes = array_count * sizeof (uint64_t);
3867 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
3868 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
3869 array_bytes, smobj_array, 0));
3870
3871 for (uint64_t i = 0; i < array_count; i++) {
3872 uint64_t smobj = smobj_array[i];
3873 if (smobj == 0)
3874 continue;
3875
3876 space_map_free_obj(mos, smobj, tx);
3877 }
3878
3879 kmem_free(smobj_array, array_bytes);
3880 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
3881 vdev_destroy_ms_flush_data(vd, tx);
3882 vd->vdev_ms_array = 0;
3883 }
3884
3885 static void
3886 vdev_remove_empty_log(vdev_t *vd, uint64_t txg)
3887 {
3888 spa_t *spa = vd->vdev_spa;
3889
3890 ASSERT(vd->vdev_islog);
3891 ASSERT(vd == vd->vdev_top);
3892 ASSERT3U(txg, ==, spa_syncing_txg(spa));
3893
3894 dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
3895
3896 vdev_destroy_spacemaps(vd, tx);
3897 if (vd->vdev_top_zap != 0) {
3898 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
3899 vd->vdev_top_zap = 0;
3900 }
3901
3902 dmu_tx_commit(tx);
3903 }
3904
3905 void
3906 vdev_sync_done(vdev_t *vd, uint64_t txg)
3907 {
3908 metaslab_t *msp;
3909 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
3910
3911 ASSERT(vdev_is_concrete(vd));
3912
3913 while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
3914 != NULL)
3915 metaslab_sync_done(msp, txg);
3916
3917 if (reassess) {
3918 metaslab_sync_reassess(vd->vdev_mg);
3919 if (vd->vdev_log_mg != NULL)
3920 metaslab_sync_reassess(vd->vdev_log_mg);
3921 }
3922 }
3923
3924 void
3925 vdev_sync(vdev_t *vd, uint64_t txg)
3926 {
3927 spa_t *spa = vd->vdev_spa;
3928 vdev_t *lvd;
3929 metaslab_t *msp;
3930
3931 ASSERT3U(txg, ==, spa->spa_syncing_txg);
3932 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3933 if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
3934 ASSERT(vd->vdev_removing ||
3935 vd->vdev_ops == &vdev_indirect_ops);
3936
3937 vdev_indirect_sync_obsolete(vd, tx);
3938
3939 /*
3940 * If the vdev is indirect, it can't have dirty
3941 * metaslabs or DTLs.
3942 */
3943 if (vd->vdev_ops == &vdev_indirect_ops) {
3944 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
3945 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
3946 dmu_tx_commit(tx);
3947 return;
3948 }
3949 }
3950
3951 ASSERT(vdev_is_concrete(vd));
3952
3953 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
3954 !vd->vdev_removing) {
3955 ASSERT(vd == vd->vdev_top);
3956 ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
3957 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
3958 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
3959 ASSERT(vd->vdev_ms_array != 0);
3960 vdev_config_dirty(vd);
3961 }
3962
3963 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
3964 metaslab_sync(msp, txg);
3965 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
3966 }
3967
3968 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
3969 vdev_dtl_sync(lvd, txg);
3970
3971 /*
3972 * If this is an empty log device being removed, destroy the
3973 * metadata associated with it.
3974 */
3975 if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
3976 vdev_remove_empty_log(vd, txg);
3977
3978 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
3979 dmu_tx_commit(tx);
3980 }
3981
3982 uint64_t
3983 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
3984 {
3985 return (vd->vdev_ops->vdev_op_asize(vd, psize));
3986 }
3987
3988 /*
3989 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
3990 * not be opened, and no I/O is attempted.
3991 */
3992 int
3993 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
3994 {
3995 vdev_t *vd, *tvd;
3996
3997 spa_vdev_state_enter(spa, SCL_NONE);
3998
3999 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4000 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4001
4002 if (!vd->vdev_ops->vdev_op_leaf)
4003 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4004
4005 tvd = vd->vdev_top;
4006
4007 /*
4008 * If user did a 'zpool offline -f' then make the fault persist across
4009 * reboots.
4010 */
4011 if (aux == VDEV_AUX_EXTERNAL_PERSIST) {
4012 /*
4013 * There are two kinds of forced faults: temporary and
4014 * persistent. Temporary faults go away at pool import, while
4015 * persistent faults stay set. Both types of faults can be
4016 * cleared with a zpool clear.
4017 *
4018 * We tell if a vdev is persistently faulted by looking at the
4019 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
4020 * import then it's a persistent fault. Otherwise, it's
4021 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
4022 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
4023 * tells vdev_config_generate() (which gets run later) to set
4024 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
4025 */
4026 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL;
4027 vd->vdev_tmpoffline = B_FALSE;
4028 aux = VDEV_AUX_EXTERNAL;
4029 } else {
4030 vd->vdev_tmpoffline = B_TRUE;
4031 }
4032
4033 /*
4034 * We don't directly use the aux state here, but if we do a
4035 * vdev_reopen(), we need this value to be present to remember why we
4036 * were faulted.
4037 */
4038 vd->vdev_label_aux = aux;
4039
4040 /*
4041 * Faulted state takes precedence over degraded.
4042 */
4043 vd->vdev_delayed_close = B_FALSE;
4044 vd->vdev_faulted = 1ULL;
4045 vd->vdev_degraded = 0ULL;
4046 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
4047
4048 /*
4049 * If this device has the only valid copy of the data, then
4050 * back off and simply mark the vdev as degraded instead.
4051 */
4052 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
4053 vd->vdev_degraded = 1ULL;
4054 vd->vdev_faulted = 0ULL;
4055
4056 /*
4057 * If we reopen the device and it's not dead, only then do we
4058 * mark it degraded.
4059 */
4060 vdev_reopen(tvd);
4061
4062 if (vdev_readable(vd))
4063 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
4064 }
4065
4066 return (spa_vdev_state_exit(spa, vd, 0));
4067 }
4068
4069 /*
4070 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
4071 * user that something is wrong. The vdev continues to operate as normal as far
4072 * as I/O is concerned.
4073 */
4074 int
4075 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
4076 {
4077 vdev_t *vd;
4078
4079 spa_vdev_state_enter(spa, SCL_NONE);
4080
4081 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4082 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4083
4084 if (!vd->vdev_ops->vdev_op_leaf)
4085 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4086
4087 /*
4088 * If the vdev is already faulted, then don't do anything.
4089 */
4090 if (vd->vdev_faulted || vd->vdev_degraded)
4091 return (spa_vdev_state_exit(spa, NULL, 0));
4092
4093 vd->vdev_degraded = 1ULL;
4094 if (!vdev_is_dead(vd))
4095 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
4096 aux);
4097
4098 return (spa_vdev_state_exit(spa, vd, 0));
4099 }
4100
4101 int
4102 vdev_remove_wanted(spa_t *spa, uint64_t guid)
4103 {
4104 vdev_t *vd;
4105
4106 spa_vdev_state_enter(spa, SCL_NONE);
4107
4108 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4109 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4110
4111 /*
4112 * If the vdev is already removed, or expanding which can trigger
4113 * repartition add/remove events, then don't do anything.
4114 */
4115 if (vd->vdev_removed || vd->vdev_expanding)
4116 return (spa_vdev_state_exit(spa, NULL, 0));
4117
4118 /*
4119 * Confirm the vdev has been removed, otherwise don't do anything.
4120 */
4121 if (vd->vdev_ops->vdev_op_leaf && !zio_wait(vdev_probe(vd, NULL)))
4122 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(EEXIST)));
4123
4124 vd->vdev_remove_wanted = B_TRUE;
4125 spa_async_request(spa, SPA_ASYNC_REMOVE);
4126
4127 return (spa_vdev_state_exit(spa, vd, 0));
4128 }
4129
4130
4131 /*
4132 * Online the given vdev.
4133 *
4134 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
4135 * spare device should be detached when the device finishes resilvering.
4136 * Second, the online should be treated like a 'test' online case, so no FMA
4137 * events are generated if the device fails to open.
4138 */
4139 int
4140 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
4141 {
4142 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
4143 boolean_t wasoffline;
4144 vdev_state_t oldstate;
4145
4146 spa_vdev_state_enter(spa, SCL_NONE);
4147
4148 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4149 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4150
4151 if (!vd->vdev_ops->vdev_op_leaf)
4152 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4153
4154 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
4155 oldstate = vd->vdev_state;
4156
4157 tvd = vd->vdev_top;
4158 vd->vdev_offline = B_FALSE;
4159 vd->vdev_tmpoffline = B_FALSE;
4160 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
4161 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
4162
4163 /* XXX - L2ARC 1.0 does not support expansion */
4164 if (!vd->vdev_aux) {
4165 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
4166 pvd->vdev_expanding = !!((flags & ZFS_ONLINE_EXPAND) ||
4167 spa->spa_autoexpand);
4168 vd->vdev_expansion_time = gethrestime_sec();
4169 }
4170
4171 vdev_reopen(tvd);
4172 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
4173
4174 if (!vd->vdev_aux) {
4175 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
4176 pvd->vdev_expanding = B_FALSE;
4177 }
4178
4179 if (newstate)
4180 *newstate = vd->vdev_state;
4181 if ((flags & ZFS_ONLINE_UNSPARE) &&
4182 !vdev_is_dead(vd) && vd->vdev_parent &&
4183 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4184 vd->vdev_parent->vdev_child[0] == vd)
4185 vd->vdev_unspare = B_TRUE;
4186
4187 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
4188
4189 /* XXX - L2ARC 1.0 does not support expansion */
4190 if (vd->vdev_aux)
4191 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
4192 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
4193 }
4194
4195 /* Restart initializing if necessary */
4196 mutex_enter(&vd->vdev_initialize_lock);
4197 if (vdev_writeable(vd) &&
4198 vd->vdev_initialize_thread == NULL &&
4199 vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) {
4200 (void) vdev_initialize(vd);
4201 }
4202 mutex_exit(&vd->vdev_initialize_lock);
4203
4204 /*
4205 * Restart trimming if necessary. We do not restart trimming for cache
4206 * devices here. This is triggered by l2arc_rebuild_vdev()
4207 * asynchronously for the whole device or in l2arc_evict() as it evicts
4208 * space for upcoming writes.
4209 */
4210 mutex_enter(&vd->vdev_trim_lock);
4211 if (vdev_writeable(vd) && !vd->vdev_isl2cache &&
4212 vd->vdev_trim_thread == NULL &&
4213 vd->vdev_trim_state == VDEV_TRIM_ACTIVE) {
4214 (void) vdev_trim(vd, vd->vdev_trim_rate, vd->vdev_trim_partial,
4215 vd->vdev_trim_secure);
4216 }
4217 mutex_exit(&vd->vdev_trim_lock);
4218
4219 if (wasoffline ||
4220 (oldstate < VDEV_STATE_DEGRADED &&
4221 vd->vdev_state >= VDEV_STATE_DEGRADED)) {
4222 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
4223
4224 /*
4225 * Asynchronously detach spare vdev if resilver or
4226 * rebuild is not required
4227 */
4228 if (vd->vdev_unspare &&
4229 !dsl_scan_resilvering(spa->spa_dsl_pool) &&
4230 !dsl_scan_resilver_scheduled(spa->spa_dsl_pool) &&
4231 !vdev_rebuild_active(tvd))
4232 spa_async_request(spa, SPA_ASYNC_DETACH_SPARE);
4233 }
4234 return (spa_vdev_state_exit(spa, vd, 0));
4235 }
4236
4237 static int
4238 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
4239 {
4240 vdev_t *vd, *tvd;
4241 int error = 0;
4242 uint64_t generation;
4243 metaslab_group_t *mg;
4244
4245 top:
4246 spa_vdev_state_enter(spa, SCL_ALLOC);
4247
4248 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4249 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4250
4251 if (!vd->vdev_ops->vdev_op_leaf)
4252 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4253
4254 if (vd->vdev_ops == &vdev_draid_spare_ops)
4255 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
4256
4257 tvd = vd->vdev_top;
4258 mg = tvd->vdev_mg;
4259 generation = spa->spa_config_generation + 1;
4260
4261 /*
4262 * If the device isn't already offline, try to offline it.
4263 */
4264 if (!vd->vdev_offline) {
4265 /*
4266 * If this device has the only valid copy of some data,
4267 * don't allow it to be offlined. Log devices are always
4268 * expendable.
4269 */
4270 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
4271 vdev_dtl_required(vd))
4272 return (spa_vdev_state_exit(spa, NULL,
4273 SET_ERROR(EBUSY)));
4274
4275 /*
4276 * If the top-level is a slog and it has had allocations
4277 * then proceed. We check that the vdev's metaslab group
4278 * is not NULL since it's possible that we may have just
4279 * added this vdev but not yet initialized its metaslabs.
4280 */
4281 if (tvd->vdev_islog && mg != NULL) {
4282 /*
4283 * Prevent any future allocations.
4284 */
4285 ASSERT3P(tvd->vdev_log_mg, ==, NULL);
4286 metaslab_group_passivate(mg);
4287 (void) spa_vdev_state_exit(spa, vd, 0);
4288
4289 error = spa_reset_logs(spa);
4290
4291 /*
4292 * If the log device was successfully reset but has
4293 * checkpointed data, do not offline it.
4294 */
4295 if (error == 0 &&
4296 tvd->vdev_checkpoint_sm != NULL) {
4297 ASSERT3U(space_map_allocated(
4298 tvd->vdev_checkpoint_sm), !=, 0);
4299 error = ZFS_ERR_CHECKPOINT_EXISTS;
4300 }
4301
4302 spa_vdev_state_enter(spa, SCL_ALLOC);
4303
4304 /*
4305 * Check to see if the config has changed.
4306 */
4307 if (error || generation != spa->spa_config_generation) {
4308 metaslab_group_activate(mg);
4309 if (error)
4310 return (spa_vdev_state_exit(spa,
4311 vd, error));
4312 (void) spa_vdev_state_exit(spa, vd, 0);
4313 goto top;
4314 }
4315 ASSERT0(tvd->vdev_stat.vs_alloc);
4316 }
4317
4318 /*
4319 * Offline this device and reopen its top-level vdev.
4320 * If the top-level vdev is a log device then just offline
4321 * it. Otherwise, if this action results in the top-level
4322 * vdev becoming unusable, undo it and fail the request.
4323 */
4324 vd->vdev_offline = B_TRUE;
4325 vdev_reopen(tvd);
4326
4327 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
4328 vdev_is_dead(tvd)) {
4329 vd->vdev_offline = B_FALSE;
4330 vdev_reopen(tvd);
4331 return (spa_vdev_state_exit(spa, NULL,
4332 SET_ERROR(EBUSY)));
4333 }
4334
4335 /*
4336 * Add the device back into the metaslab rotor so that
4337 * once we online the device it's open for business.
4338 */
4339 if (tvd->vdev_islog && mg != NULL)
4340 metaslab_group_activate(mg);
4341 }
4342
4343 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
4344
4345 return (spa_vdev_state_exit(spa, vd, 0));
4346 }
4347
4348 int
4349 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
4350 {
4351 int error;
4352
4353 mutex_enter(&spa->spa_vdev_top_lock);
4354 error = vdev_offline_locked(spa, guid, flags);
4355 mutex_exit(&spa->spa_vdev_top_lock);
4356
4357 return (error);
4358 }
4359
4360 /*
4361 * Clear the error counts associated with this vdev. Unlike vdev_online() and
4362 * vdev_offline(), we assume the spa config is locked. We also clear all
4363 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
4364 */
4365 void
4366 vdev_clear(spa_t *spa, vdev_t *vd)
4367 {
4368 vdev_t *rvd = spa->spa_root_vdev;
4369
4370 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
4371
4372 if (vd == NULL)
4373 vd = rvd;
4374
4375 vd->vdev_stat.vs_read_errors = 0;
4376 vd->vdev_stat.vs_write_errors = 0;
4377 vd->vdev_stat.vs_checksum_errors = 0;
4378 vd->vdev_stat.vs_slow_ios = 0;
4379
4380 for (int c = 0; c < vd->vdev_children; c++)
4381 vdev_clear(spa, vd->vdev_child[c]);
4382
4383 /*
4384 * It makes no sense to "clear" an indirect or removed vdev.
4385 */
4386 if (!vdev_is_concrete(vd) || vd->vdev_removed)
4387 return;
4388
4389 /*
4390 * If we're in the FAULTED state or have experienced failed I/O, then
4391 * clear the persistent state and attempt to reopen the device. We
4392 * also mark the vdev config dirty, so that the new faulted state is
4393 * written out to disk.
4394 */
4395 if (vd->vdev_faulted || vd->vdev_degraded ||
4396 !vdev_readable(vd) || !vdev_writeable(vd)) {
4397 /*
4398 * When reopening in response to a clear event, it may be due to
4399 * a fmadm repair request. In this case, if the device is
4400 * still broken, we want to still post the ereport again.
4401 */
4402 vd->vdev_forcefault = B_TRUE;
4403
4404 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
4405 vd->vdev_cant_read = B_FALSE;
4406 vd->vdev_cant_write = B_FALSE;
4407 vd->vdev_stat.vs_aux = 0;
4408
4409 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
4410
4411 vd->vdev_forcefault = B_FALSE;
4412
4413 if (vd != rvd && vdev_writeable(vd->vdev_top))
4414 vdev_state_dirty(vd->vdev_top);
4415
4416 /* If a resilver isn't required, check if vdevs can be culled */
4417 if (vd->vdev_aux == NULL && !vdev_is_dead(vd) &&
4418 !dsl_scan_resilvering(spa->spa_dsl_pool) &&
4419 !dsl_scan_resilver_scheduled(spa->spa_dsl_pool))
4420 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
4421
4422 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
4423 }
4424
4425 /*
4426 * When clearing a FMA-diagnosed fault, we always want to
4427 * unspare the device, as we assume that the original spare was
4428 * done in response to the FMA fault.
4429 */
4430 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
4431 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4432 vd->vdev_parent->vdev_child[0] == vd)
4433 vd->vdev_unspare = B_TRUE;
4434
4435 /* Clear recent error events cache (i.e. duplicate events tracking) */
4436 zfs_ereport_clear(spa, vd);
4437 }
4438
4439 boolean_t
4440 vdev_is_dead(vdev_t *vd)
4441 {
4442 /*
4443 * Holes and missing devices are always considered "dead".
4444 * This simplifies the code since we don't have to check for
4445 * these types of devices in the various code paths.
4446 * Instead we rely on the fact that we skip over dead devices
4447 * before issuing I/O to them.
4448 */
4449 return (vd->vdev_state < VDEV_STATE_DEGRADED ||
4450 vd->vdev_ops == &vdev_hole_ops ||
4451 vd->vdev_ops == &vdev_missing_ops);
4452 }
4453
4454 boolean_t
4455 vdev_readable(vdev_t *vd)
4456 {
4457 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
4458 }
4459
4460 boolean_t
4461 vdev_writeable(vdev_t *vd)
4462 {
4463 return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
4464 vdev_is_concrete(vd));
4465 }
4466
4467 boolean_t
4468 vdev_allocatable(vdev_t *vd)
4469 {
4470 uint64_t state = vd->vdev_state;
4471
4472 /*
4473 * We currently allow allocations from vdevs which may be in the
4474 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
4475 * fails to reopen then we'll catch it later when we're holding
4476 * the proper locks. Note that we have to get the vdev state
4477 * in a local variable because although it changes atomically,
4478 * we're asking two separate questions about it.
4479 */
4480 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
4481 !vd->vdev_cant_write && vdev_is_concrete(vd) &&
4482 vd->vdev_mg->mg_initialized);
4483 }
4484
4485 boolean_t
4486 vdev_accessible(vdev_t *vd, zio_t *zio)
4487 {
4488 ASSERT(zio->io_vd == vd);
4489
4490 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
4491 return (B_FALSE);
4492
4493 if (zio->io_type == ZIO_TYPE_READ)
4494 return (!vd->vdev_cant_read);
4495
4496 if (zio->io_type == ZIO_TYPE_WRITE)
4497 return (!vd->vdev_cant_write);
4498
4499 return (B_TRUE);
4500 }
4501
4502 static void
4503 vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs)
4504 {
4505 /*
4506 * Exclude the dRAID spare when aggregating to avoid double counting
4507 * the ops and bytes. These IOs are counted by the physical leaves.
4508 */
4509 if (cvd->vdev_ops == &vdev_draid_spare_ops)
4510 return;
4511
4512 for (int t = 0; t < VS_ZIO_TYPES; t++) {
4513 vs->vs_ops[t] += cvs->vs_ops[t];
4514 vs->vs_bytes[t] += cvs->vs_bytes[t];
4515 }
4516
4517 cvs->vs_scan_removing = cvd->vdev_removing;
4518 }
4519
4520 /*
4521 * Get extended stats
4522 */
4523 static void
4524 vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx)
4525 {
4526 (void) cvd;
4527
4528 int t, b;
4529 for (t = 0; t < ZIO_TYPES; t++) {
4530 for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++)
4531 vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b];
4532
4533 for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) {
4534 vsx->vsx_total_histo[t][b] +=
4535 cvsx->vsx_total_histo[t][b];
4536 }
4537 }
4538
4539 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
4540 for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) {
4541 vsx->vsx_queue_histo[t][b] +=
4542 cvsx->vsx_queue_histo[t][b];
4543 }
4544 vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t];
4545 vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t];
4546
4547 for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++)
4548 vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b];
4549
4550 for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++)
4551 vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b];
4552 }
4553
4554 }
4555
4556 boolean_t
4557 vdev_is_spacemap_addressable(vdev_t *vd)
4558 {
4559 if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2))
4560 return (B_TRUE);
4561
4562 /*
4563 * If double-word space map entries are not enabled we assume
4564 * 47 bits of the space map entry are dedicated to the entry's
4565 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
4566 * to calculate the maximum address that can be described by a
4567 * space map entry for the given device.
4568 */
4569 uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS;
4570
4571 if (shift >= 63) /* detect potential overflow */
4572 return (B_TRUE);
4573
4574 return (vd->vdev_asize < (1ULL << shift));
4575 }
4576
4577 /*
4578 * Get statistics for the given vdev.
4579 */
4580 static void
4581 vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
4582 {
4583 int t;
4584 /*
4585 * If we're getting stats on the root vdev, aggregate the I/O counts
4586 * over all top-level vdevs (i.e. the direct children of the root).
4587 */
4588 if (!vd->vdev_ops->vdev_op_leaf) {
4589 if (vs) {
4590 memset(vs->vs_ops, 0, sizeof (vs->vs_ops));
4591 memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes));
4592 }
4593 if (vsx)
4594 memset(vsx, 0, sizeof (*vsx));
4595
4596 for (int c = 0; c < vd->vdev_children; c++) {
4597 vdev_t *cvd = vd->vdev_child[c];
4598 vdev_stat_t *cvs = &cvd->vdev_stat;
4599 vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex;
4600
4601 vdev_get_stats_ex_impl(cvd, cvs, cvsx);
4602 if (vs)
4603 vdev_get_child_stat(cvd, vs, cvs);
4604 if (vsx)
4605 vdev_get_child_stat_ex(cvd, vsx, cvsx);
4606 }
4607 } else {
4608 /*
4609 * We're a leaf. Just copy our ZIO active queue stats in. The
4610 * other leaf stats are updated in vdev_stat_update().
4611 */
4612 if (!vsx)
4613 return;
4614
4615 memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex));
4616
4617 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
4618 vsx->vsx_active_queue[t] = vd->vdev_queue.vq_cactive[t];
4619 vsx->vsx_pend_queue[t] = vdev_queue_class_length(vd, t);
4620 }
4621 }
4622 }
4623
4624 void
4625 vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
4626 {
4627 vdev_t *tvd = vd->vdev_top;
4628 mutex_enter(&vd->vdev_stat_lock);
4629 if (vs) {
4630 memcpy(vs, &vd->vdev_stat, sizeof (*vs));
4631 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
4632 vs->vs_state = vd->vdev_state;
4633 vs->vs_rsize = vdev_get_min_asize(vd);
4634
4635 if (vd->vdev_ops->vdev_op_leaf) {
4636 vs->vs_pspace = vd->vdev_psize;
4637 vs->vs_rsize += VDEV_LABEL_START_SIZE +
4638 VDEV_LABEL_END_SIZE;
4639 /*
4640 * Report initializing progress. Since we don't
4641 * have the initializing locks held, this is only
4642 * an estimate (although a fairly accurate one).
4643 */
4644 vs->vs_initialize_bytes_done =
4645 vd->vdev_initialize_bytes_done;
4646 vs->vs_initialize_bytes_est =
4647 vd->vdev_initialize_bytes_est;
4648 vs->vs_initialize_state = vd->vdev_initialize_state;
4649 vs->vs_initialize_action_time =
4650 vd->vdev_initialize_action_time;
4651
4652 /*
4653 * Report manual TRIM progress. Since we don't have
4654 * the manual TRIM locks held, this is only an
4655 * estimate (although fairly accurate one).
4656 */
4657 vs->vs_trim_notsup = !vd->vdev_has_trim;
4658 vs->vs_trim_bytes_done = vd->vdev_trim_bytes_done;
4659 vs->vs_trim_bytes_est = vd->vdev_trim_bytes_est;
4660 vs->vs_trim_state = vd->vdev_trim_state;
4661 vs->vs_trim_action_time = vd->vdev_trim_action_time;
4662
4663 /* Set when there is a deferred resilver. */
4664 vs->vs_resilver_deferred = vd->vdev_resilver_deferred;
4665 }
4666
4667 /*
4668 * Report expandable space on top-level, non-auxiliary devices
4669 * only. The expandable space is reported in terms of metaslab
4670 * sized units since that determines how much space the pool
4671 * can expand.
4672 */
4673 if (vd->vdev_aux == NULL && tvd != NULL) {
4674 vs->vs_esize = P2ALIGN(
4675 vd->vdev_max_asize - vd->vdev_asize,
4676 1ULL << tvd->vdev_ms_shift);
4677 }
4678
4679 vs->vs_configured_ashift = vd->vdev_top != NULL
4680 ? vd->vdev_top->vdev_ashift : vd->vdev_ashift;
4681 vs->vs_logical_ashift = vd->vdev_logical_ashift;
4682 if (vd->vdev_physical_ashift <= ASHIFT_MAX)
4683 vs->vs_physical_ashift = vd->vdev_physical_ashift;
4684 else
4685 vs->vs_physical_ashift = 0;
4686
4687 /*
4688 * Report fragmentation and rebuild progress for top-level,
4689 * non-auxiliary, concrete devices.
4690 */
4691 if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
4692 vdev_is_concrete(vd)) {
4693 /*
4694 * The vdev fragmentation rating doesn't take into
4695 * account the embedded slog metaslab (vdev_log_mg).
4696 * Since it's only one metaslab, it would have a tiny
4697 * impact on the overall fragmentation.
4698 */
4699 vs->vs_fragmentation = (vd->vdev_mg != NULL) ?
4700 vd->vdev_mg->mg_fragmentation : 0;
4701 }
4702 vs->vs_noalloc = MAX(vd->vdev_noalloc,
4703 tvd ? tvd->vdev_noalloc : 0);
4704 }
4705
4706 vdev_get_stats_ex_impl(vd, vs, vsx);
4707 mutex_exit(&vd->vdev_stat_lock);
4708 }
4709
4710 void
4711 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
4712 {
4713 return (vdev_get_stats_ex(vd, vs, NULL));
4714 }
4715
4716 void
4717 vdev_clear_stats(vdev_t *vd)
4718 {
4719 mutex_enter(&vd->vdev_stat_lock);
4720 vd->vdev_stat.vs_space = 0;
4721 vd->vdev_stat.vs_dspace = 0;
4722 vd->vdev_stat.vs_alloc = 0;
4723 mutex_exit(&vd->vdev_stat_lock);
4724 }
4725
4726 void
4727 vdev_scan_stat_init(vdev_t *vd)
4728 {
4729 vdev_stat_t *vs = &vd->vdev_stat;
4730
4731 for (int c = 0; c < vd->vdev_children; c++)
4732 vdev_scan_stat_init(vd->vdev_child[c]);
4733
4734 mutex_enter(&vd->vdev_stat_lock);
4735 vs->vs_scan_processed = 0;
4736 mutex_exit(&vd->vdev_stat_lock);
4737 }
4738
4739 void
4740 vdev_stat_update(zio_t *zio, uint64_t psize)
4741 {
4742 spa_t *spa = zio->io_spa;
4743 vdev_t *rvd = spa->spa_root_vdev;
4744 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
4745 vdev_t *pvd;
4746 uint64_t txg = zio->io_txg;
4747 /* Suppress ASAN false positive */
4748 #ifdef __SANITIZE_ADDRESS__
4749 vdev_stat_t *vs = vd ? &vd->vdev_stat : NULL;
4750 vdev_stat_ex_t *vsx = vd ? &vd->vdev_stat_ex : NULL;
4751 #else
4752 vdev_stat_t *vs = &vd->vdev_stat;
4753 vdev_stat_ex_t *vsx = &vd->vdev_stat_ex;
4754 #endif
4755 zio_type_t type = zio->io_type;
4756 int flags = zio->io_flags;
4757
4758 /*
4759 * If this i/o is a gang leader, it didn't do any actual work.
4760 */
4761 if (zio->io_gang_tree)
4762 return;
4763
4764 if (zio->io_error == 0) {
4765 /*
4766 * If this is a root i/o, don't count it -- we've already
4767 * counted the top-level vdevs, and vdev_get_stats() will
4768 * aggregate them when asked. This reduces contention on
4769 * the root vdev_stat_lock and implicitly handles blocks
4770 * that compress away to holes, for which there is no i/o.
4771 * (Holes never create vdev children, so all the counters
4772 * remain zero, which is what we want.)
4773 *
4774 * Note: this only applies to successful i/o (io_error == 0)
4775 * because unlike i/o counts, errors are not additive.
4776 * When reading a ditto block, for example, failure of
4777 * one top-level vdev does not imply a root-level error.
4778 */
4779 if (vd == rvd)
4780 return;
4781
4782 ASSERT(vd == zio->io_vd);
4783
4784 if (flags & ZIO_FLAG_IO_BYPASS)
4785 return;
4786
4787 mutex_enter(&vd->vdev_stat_lock);
4788
4789 if (flags & ZIO_FLAG_IO_REPAIR) {
4790 /*
4791 * Repair is the result of a resilver issued by the
4792 * scan thread (spa_sync).
4793 */
4794 if (flags & ZIO_FLAG_SCAN_THREAD) {
4795 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
4796 dsl_scan_phys_t *scn_phys = &scn->scn_phys;
4797 uint64_t *processed = &scn_phys->scn_processed;
4798
4799 if (vd->vdev_ops->vdev_op_leaf)
4800 atomic_add_64(processed, psize);
4801 vs->vs_scan_processed += psize;
4802 }
4803
4804 /*
4805 * Repair is the result of a rebuild issued by the
4806 * rebuild thread (vdev_rebuild_thread). To avoid
4807 * double counting repaired bytes the virtual dRAID
4808 * spare vdev is excluded from the processed bytes.
4809 */
4810 if (zio->io_priority == ZIO_PRIORITY_REBUILD) {
4811 vdev_t *tvd = vd->vdev_top;
4812 vdev_rebuild_t *vr = &tvd->vdev_rebuild_config;
4813 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
4814 uint64_t *rebuilt = &vrp->vrp_bytes_rebuilt;
4815
4816 if (vd->vdev_ops->vdev_op_leaf &&
4817 vd->vdev_ops != &vdev_draid_spare_ops) {
4818 atomic_add_64(rebuilt, psize);
4819 }
4820 vs->vs_rebuild_processed += psize;
4821 }
4822
4823 if (flags & ZIO_FLAG_SELF_HEAL)
4824 vs->vs_self_healed += psize;
4825 }
4826
4827 /*
4828 * The bytes/ops/histograms are recorded at the leaf level and
4829 * aggregated into the higher level vdevs in vdev_get_stats().
4830 */
4831 if (vd->vdev_ops->vdev_op_leaf &&
4832 (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) {
4833 zio_type_t vs_type = type;
4834 zio_priority_t priority = zio->io_priority;
4835
4836 /*
4837 * TRIM ops and bytes are reported to user space as
4838 * ZIO_TYPE_IOCTL. This is done to preserve the
4839 * vdev_stat_t structure layout for user space.
4840 */
4841 if (type == ZIO_TYPE_TRIM)
4842 vs_type = ZIO_TYPE_IOCTL;
4843
4844 /*
4845 * Solely for the purposes of 'zpool iostat -lqrw'
4846 * reporting use the priority to categorize the IO.
4847 * Only the following are reported to user space:
4848 *
4849 * ZIO_PRIORITY_SYNC_READ,
4850 * ZIO_PRIORITY_SYNC_WRITE,
4851 * ZIO_PRIORITY_ASYNC_READ,
4852 * ZIO_PRIORITY_ASYNC_WRITE,
4853 * ZIO_PRIORITY_SCRUB,
4854 * ZIO_PRIORITY_TRIM,
4855 * ZIO_PRIORITY_REBUILD.
4856 */
4857 if (priority == ZIO_PRIORITY_INITIALIZING) {
4858 ASSERT3U(type, ==, ZIO_TYPE_WRITE);
4859 priority = ZIO_PRIORITY_ASYNC_WRITE;
4860 } else if (priority == ZIO_PRIORITY_REMOVAL) {
4861 priority = ((type == ZIO_TYPE_WRITE) ?
4862 ZIO_PRIORITY_ASYNC_WRITE :
4863 ZIO_PRIORITY_ASYNC_READ);
4864 }
4865
4866 vs->vs_ops[vs_type]++;
4867 vs->vs_bytes[vs_type] += psize;
4868
4869 if (flags & ZIO_FLAG_DELEGATED) {
4870 vsx->vsx_agg_histo[priority]
4871 [RQ_HISTO(zio->io_size)]++;
4872 } else {
4873 vsx->vsx_ind_histo[priority]
4874 [RQ_HISTO(zio->io_size)]++;
4875 }
4876
4877 if (zio->io_delta && zio->io_delay) {
4878 vsx->vsx_queue_histo[priority]
4879 [L_HISTO(zio->io_delta - zio->io_delay)]++;
4880 vsx->vsx_disk_histo[type]
4881 [L_HISTO(zio->io_delay)]++;
4882 vsx->vsx_total_histo[type]
4883 [L_HISTO(zio->io_delta)]++;
4884 }
4885 }
4886
4887 mutex_exit(&vd->vdev_stat_lock);
4888 return;
4889 }
4890
4891 if (flags & ZIO_FLAG_SPECULATIVE)
4892 return;
4893
4894 /*
4895 * If this is an I/O error that is going to be retried, then ignore the
4896 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
4897 * hard errors, when in reality they can happen for any number of
4898 * innocuous reasons (bus resets, MPxIO link failure, etc).
4899 */
4900 if (zio->io_error == EIO &&
4901 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
4902 return;
4903
4904 /*
4905 * Intent logs writes won't propagate their error to the root
4906 * I/O so don't mark these types of failures as pool-level
4907 * errors.
4908 */
4909 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
4910 return;
4911
4912 if (type == ZIO_TYPE_WRITE && txg != 0 &&
4913 (!(flags & ZIO_FLAG_IO_REPAIR) ||
4914 (flags & ZIO_FLAG_SCAN_THREAD) ||
4915 spa->spa_claiming)) {
4916 /*
4917 * This is either a normal write (not a repair), or it's
4918 * a repair induced by the scrub thread, or it's a repair
4919 * made by zil_claim() during spa_load() in the first txg.
4920 * In the normal case, we commit the DTL change in the same
4921 * txg as the block was born. In the scrub-induced repair
4922 * case, we know that scrubs run in first-pass syncing context,
4923 * so we commit the DTL change in spa_syncing_txg(spa).
4924 * In the zil_claim() case, we commit in spa_first_txg(spa).
4925 *
4926 * We currently do not make DTL entries for failed spontaneous
4927 * self-healing writes triggered by normal (non-scrubbing)
4928 * reads, because we have no transactional context in which to
4929 * do so -- and it's not clear that it'd be desirable anyway.
4930 */
4931 if (vd->vdev_ops->vdev_op_leaf) {
4932 uint64_t commit_txg = txg;
4933 if (flags & ZIO_FLAG_SCAN_THREAD) {
4934 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
4935 ASSERT(spa_sync_pass(spa) == 1);
4936 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
4937 commit_txg = spa_syncing_txg(spa);
4938 } else if (spa->spa_claiming) {
4939 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
4940 commit_txg = spa_first_txg(spa);
4941 }
4942 ASSERT(commit_txg >= spa_syncing_txg(spa));
4943 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
4944 return;
4945 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
4946 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
4947 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
4948 }
4949 if (vd != rvd)
4950 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
4951 }
4952 }
4953
4954 int64_t
4955 vdev_deflated_space(vdev_t *vd, int64_t space)
4956 {
4957 ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0);
4958 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
4959
4960 return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio);
4961 }
4962
4963 /*
4964 * Update the in-core space usage stats for this vdev, its metaslab class,
4965 * and the root vdev.
4966 */
4967 void
4968 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
4969 int64_t space_delta)
4970 {
4971 (void) defer_delta;
4972 int64_t dspace_delta;
4973 spa_t *spa = vd->vdev_spa;
4974 vdev_t *rvd = spa->spa_root_vdev;
4975
4976 ASSERT(vd == vd->vdev_top);
4977
4978 /*
4979 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
4980 * factor. We must calculate this here and not at the root vdev
4981 * because the root vdev's psize-to-asize is simply the max of its
4982 * children's, thus not accurate enough for us.
4983 */
4984 dspace_delta = vdev_deflated_space(vd, space_delta);
4985
4986 mutex_enter(&vd->vdev_stat_lock);
4987 /* ensure we won't underflow */
4988 if (alloc_delta < 0) {
4989 ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta);
4990 }
4991
4992 vd->vdev_stat.vs_alloc += alloc_delta;
4993 vd->vdev_stat.vs_space += space_delta;
4994 vd->vdev_stat.vs_dspace += dspace_delta;
4995 mutex_exit(&vd->vdev_stat_lock);
4996
4997 /* every class but log contributes to root space stats */
4998 if (vd->vdev_mg != NULL && !vd->vdev_islog) {
4999 ASSERT(!vd->vdev_isl2cache);
5000 mutex_enter(&rvd->vdev_stat_lock);
5001 rvd->vdev_stat.vs_alloc += alloc_delta;
5002 rvd->vdev_stat.vs_space += space_delta;
5003 rvd->vdev_stat.vs_dspace += dspace_delta;
5004 mutex_exit(&rvd->vdev_stat_lock);
5005 }
5006 /* Note: metaslab_class_space_update moved to metaslab_space_update */
5007 }
5008
5009 /*
5010 * Mark a top-level vdev's config as dirty, placing it on the dirty list
5011 * so that it will be written out next time the vdev configuration is synced.
5012 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
5013 */
5014 void
5015 vdev_config_dirty(vdev_t *vd)
5016 {
5017 spa_t *spa = vd->vdev_spa;
5018 vdev_t *rvd = spa->spa_root_vdev;
5019 int c;
5020
5021 ASSERT(spa_writeable(spa));
5022
5023 /*
5024 * If this is an aux vdev (as with l2cache and spare devices), then we
5025 * update the vdev config manually and set the sync flag.
5026 */
5027 if (vd->vdev_aux != NULL) {
5028 spa_aux_vdev_t *sav = vd->vdev_aux;
5029 nvlist_t **aux;
5030 uint_t naux;
5031
5032 for (c = 0; c < sav->sav_count; c++) {
5033 if (sav->sav_vdevs[c] == vd)
5034 break;
5035 }
5036
5037 if (c == sav->sav_count) {
5038 /*
5039 * We're being removed. There's nothing more to do.
5040 */
5041 ASSERT(sav->sav_sync == B_TRUE);
5042 return;
5043 }
5044
5045 sav->sav_sync = B_TRUE;
5046
5047 if (nvlist_lookup_nvlist_array(sav->sav_config,
5048 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
5049 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
5050 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
5051 }
5052
5053 ASSERT(c < naux);
5054
5055 /*
5056 * Setting the nvlist in the middle if the array is a little
5057 * sketchy, but it will work.
5058 */
5059 nvlist_free(aux[c]);
5060 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
5061
5062 return;
5063 }
5064
5065 /*
5066 * The dirty list is protected by the SCL_CONFIG lock. The caller
5067 * must either hold SCL_CONFIG as writer, or must be the sync thread
5068 * (which holds SCL_CONFIG as reader). There's only one sync thread,
5069 * so this is sufficient to ensure mutual exclusion.
5070 */
5071 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
5072 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5073 spa_config_held(spa, SCL_CONFIG, RW_READER)));
5074
5075 if (vd == rvd) {
5076 for (c = 0; c < rvd->vdev_children; c++)
5077 vdev_config_dirty(rvd->vdev_child[c]);
5078 } else {
5079 ASSERT(vd == vd->vdev_top);
5080
5081 if (!list_link_active(&vd->vdev_config_dirty_node) &&
5082 vdev_is_concrete(vd)) {
5083 list_insert_head(&spa->spa_config_dirty_list, vd);
5084 }
5085 }
5086 }
5087
5088 void
5089 vdev_config_clean(vdev_t *vd)
5090 {
5091 spa_t *spa = vd->vdev_spa;
5092
5093 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
5094 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5095 spa_config_held(spa, SCL_CONFIG, RW_READER)));
5096
5097 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
5098 list_remove(&spa->spa_config_dirty_list, vd);
5099 }
5100
5101 /*
5102 * Mark a top-level vdev's state as dirty, so that the next pass of
5103 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
5104 * the state changes from larger config changes because they require
5105 * much less locking, and are often needed for administrative actions.
5106 */
5107 void
5108 vdev_state_dirty(vdev_t *vd)
5109 {
5110 spa_t *spa = vd->vdev_spa;
5111
5112 ASSERT(spa_writeable(spa));
5113 ASSERT(vd == vd->vdev_top);
5114
5115 /*
5116 * The state list is protected by the SCL_STATE lock. The caller
5117 * must either hold SCL_STATE as writer, or must be the sync thread
5118 * (which holds SCL_STATE as reader). There's only one sync thread,
5119 * so this is sufficient to ensure mutual exclusion.
5120 */
5121 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
5122 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5123 spa_config_held(spa, SCL_STATE, RW_READER)));
5124
5125 if (!list_link_active(&vd->vdev_state_dirty_node) &&
5126 vdev_is_concrete(vd))
5127 list_insert_head(&spa->spa_state_dirty_list, vd);
5128 }
5129
5130 void
5131 vdev_state_clean(vdev_t *vd)
5132 {
5133 spa_t *spa = vd->vdev_spa;
5134
5135 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
5136 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5137 spa_config_held(spa, SCL_STATE, RW_READER)));
5138
5139 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
5140 list_remove(&spa->spa_state_dirty_list, vd);
5141 }
5142
5143 /*
5144 * Propagate vdev state up from children to parent.
5145 */
5146 void
5147 vdev_propagate_state(vdev_t *vd)
5148 {
5149 spa_t *spa = vd->vdev_spa;
5150 vdev_t *rvd = spa->spa_root_vdev;
5151 int degraded = 0, faulted = 0;
5152 int corrupted = 0;
5153 vdev_t *child;
5154
5155 if (vd->vdev_children > 0) {
5156 for (int c = 0; c < vd->vdev_children; c++) {
5157 child = vd->vdev_child[c];
5158
5159 /*
5160 * Don't factor holes or indirect vdevs into the
5161 * decision.
5162 */
5163 if (!vdev_is_concrete(child))
5164 continue;
5165
5166 if (!vdev_readable(child) ||
5167 (!vdev_writeable(child) && spa_writeable(spa))) {
5168 /*
5169 * Root special: if there is a top-level log
5170 * device, treat the root vdev as if it were
5171 * degraded.
5172 */
5173 if (child->vdev_islog && vd == rvd)
5174 degraded++;
5175 else
5176 faulted++;
5177 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
5178 degraded++;
5179 }
5180
5181 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
5182 corrupted++;
5183 }
5184
5185 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
5186
5187 /*
5188 * Root special: if there is a top-level vdev that cannot be
5189 * opened due to corrupted metadata, then propagate the root
5190 * vdev's aux state as 'corrupt' rather than 'insufficient
5191 * replicas'.
5192 */
5193 if (corrupted && vd == rvd &&
5194 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
5195 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
5196 VDEV_AUX_CORRUPT_DATA);
5197 }
5198
5199 if (vd->vdev_parent)
5200 vdev_propagate_state(vd->vdev_parent);
5201 }
5202
5203 /*
5204 * Set a vdev's state. If this is during an open, we don't update the parent
5205 * state, because we're in the process of opening children depth-first.
5206 * Otherwise, we propagate the change to the parent.
5207 *
5208 * If this routine places a device in a faulted state, an appropriate ereport is
5209 * generated.
5210 */
5211 void
5212 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
5213 {
5214 uint64_t save_state;
5215 spa_t *spa = vd->vdev_spa;
5216
5217 if (state == vd->vdev_state) {
5218 /*
5219 * Since vdev_offline() code path is already in an offline
5220 * state we can miss a statechange event to OFFLINE. Check
5221 * the previous state to catch this condition.
5222 */
5223 if (vd->vdev_ops->vdev_op_leaf &&
5224 (state == VDEV_STATE_OFFLINE) &&
5225 (vd->vdev_prevstate >= VDEV_STATE_FAULTED)) {
5226 /* post an offline state change */
5227 zfs_post_state_change(spa, vd, vd->vdev_prevstate);
5228 }
5229 vd->vdev_stat.vs_aux = aux;
5230 return;
5231 }
5232
5233 save_state = vd->vdev_state;
5234
5235 vd->vdev_state = state;
5236 vd->vdev_stat.vs_aux = aux;
5237
5238 /*
5239 * If we are setting the vdev state to anything but an open state, then
5240 * always close the underlying device unless the device has requested
5241 * a delayed close (i.e. we're about to remove or fault the device).
5242 * Otherwise, we keep accessible but invalid devices open forever.
5243 * We don't call vdev_close() itself, because that implies some extra
5244 * checks (offline, etc) that we don't want here. This is limited to
5245 * leaf devices, because otherwise closing the device will affect other
5246 * children.
5247 */
5248 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
5249 vd->vdev_ops->vdev_op_leaf)
5250 vd->vdev_ops->vdev_op_close(vd);
5251
5252 if (vd->vdev_removed &&
5253 state == VDEV_STATE_CANT_OPEN &&
5254 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
5255 /*
5256 * If the previous state is set to VDEV_STATE_REMOVED, then this
5257 * device was previously marked removed and someone attempted to
5258 * reopen it. If this failed due to a nonexistent device, then
5259 * keep the device in the REMOVED state. We also let this be if
5260 * it is one of our special test online cases, which is only
5261 * attempting to online the device and shouldn't generate an FMA
5262 * fault.
5263 */
5264 vd->vdev_state = VDEV_STATE_REMOVED;
5265 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
5266 } else if (state == VDEV_STATE_REMOVED) {
5267 vd->vdev_removed = B_TRUE;
5268 } else if (state == VDEV_STATE_CANT_OPEN) {
5269 /*
5270 * If we fail to open a vdev during an import or recovery, we
5271 * mark it as "not available", which signifies that it was
5272 * never there to begin with. Failure to open such a device
5273 * is not considered an error.
5274 */
5275 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
5276 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
5277 vd->vdev_ops->vdev_op_leaf)
5278 vd->vdev_not_present = 1;
5279
5280 /*
5281 * Post the appropriate ereport. If the 'prevstate' field is
5282 * set to something other than VDEV_STATE_UNKNOWN, it indicates
5283 * that this is part of a vdev_reopen(). In this case, we don't
5284 * want to post the ereport if the device was already in the
5285 * CANT_OPEN state beforehand.
5286 *
5287 * If the 'checkremove' flag is set, then this is an attempt to
5288 * online the device in response to an insertion event. If we
5289 * hit this case, then we have detected an insertion event for a
5290 * faulted or offline device that wasn't in the removed state.
5291 * In this scenario, we don't post an ereport because we are
5292 * about to replace the device, or attempt an online with
5293 * vdev_forcefault, which will generate the fault for us.
5294 */
5295 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
5296 !vd->vdev_not_present && !vd->vdev_checkremove &&
5297 vd != spa->spa_root_vdev) {
5298 const char *class;
5299
5300 switch (aux) {
5301 case VDEV_AUX_OPEN_FAILED:
5302 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
5303 break;
5304 case VDEV_AUX_CORRUPT_DATA:
5305 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
5306 break;
5307 case VDEV_AUX_NO_REPLICAS:
5308 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
5309 break;
5310 case VDEV_AUX_BAD_GUID_SUM:
5311 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
5312 break;
5313 case VDEV_AUX_TOO_SMALL:
5314 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
5315 break;
5316 case VDEV_AUX_BAD_LABEL:
5317 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
5318 break;
5319 case VDEV_AUX_BAD_ASHIFT:
5320 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT;
5321 break;
5322 default:
5323 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
5324 }
5325
5326 (void) zfs_ereport_post(class, spa, vd, NULL, NULL,
5327 save_state);
5328 }
5329
5330 /* Erase any notion of persistent removed state */
5331 vd->vdev_removed = B_FALSE;
5332 } else {
5333 vd->vdev_removed = B_FALSE;
5334 }
5335
5336 /*
5337 * Notify ZED of any significant state-change on a leaf vdev.
5338 *
5339 */
5340 if (vd->vdev_ops->vdev_op_leaf) {
5341 /* preserve original state from a vdev_reopen() */
5342 if ((vd->vdev_prevstate != VDEV_STATE_UNKNOWN) &&
5343 (vd->vdev_prevstate != vd->vdev_state) &&
5344 (save_state <= VDEV_STATE_CLOSED))
5345 save_state = vd->vdev_prevstate;
5346
5347 /* filter out state change due to initial vdev_open */
5348 if (save_state > VDEV_STATE_CLOSED)
5349 zfs_post_state_change(spa, vd, save_state);
5350 }
5351
5352 if (!isopen && vd->vdev_parent)
5353 vdev_propagate_state(vd->vdev_parent);
5354 }
5355
5356 boolean_t
5357 vdev_children_are_offline(vdev_t *vd)
5358 {
5359 ASSERT(!vd->vdev_ops->vdev_op_leaf);
5360
5361 for (uint64_t i = 0; i < vd->vdev_children; i++) {
5362 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
5363 return (B_FALSE);
5364 }
5365
5366 return (B_TRUE);
5367 }
5368
5369 /*
5370 * Check the vdev configuration to ensure that it's capable of supporting
5371 * a root pool. We do not support partial configuration.
5372 */
5373 boolean_t
5374 vdev_is_bootable(vdev_t *vd)
5375 {
5376 if (!vd->vdev_ops->vdev_op_leaf) {
5377 const char *vdev_type = vd->vdev_ops->vdev_op_type;
5378
5379 if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0)
5380 return (B_FALSE);
5381 }
5382
5383 for (int c = 0; c < vd->vdev_children; c++) {
5384 if (!vdev_is_bootable(vd->vdev_child[c]))
5385 return (B_FALSE);
5386 }
5387 return (B_TRUE);
5388 }
5389
5390 boolean_t
5391 vdev_is_concrete(vdev_t *vd)
5392 {
5393 vdev_ops_t *ops = vd->vdev_ops;
5394 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
5395 ops == &vdev_missing_ops || ops == &vdev_root_ops) {
5396 return (B_FALSE);
5397 } else {
5398 return (B_TRUE);
5399 }
5400 }
5401
5402 /*
5403 * Determine if a log device has valid content. If the vdev was
5404 * removed or faulted in the MOS config then we know that
5405 * the content on the log device has already been written to the pool.
5406 */
5407 boolean_t
5408 vdev_log_state_valid(vdev_t *vd)
5409 {
5410 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
5411 !vd->vdev_removed)
5412 return (B_TRUE);
5413
5414 for (int c = 0; c < vd->vdev_children; c++)
5415 if (vdev_log_state_valid(vd->vdev_child[c]))
5416 return (B_TRUE);
5417
5418 return (B_FALSE);
5419 }
5420
5421 /*
5422 * Expand a vdev if possible.
5423 */
5424 void
5425 vdev_expand(vdev_t *vd, uint64_t txg)
5426 {
5427 ASSERT(vd->vdev_top == vd);
5428 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5429 ASSERT(vdev_is_concrete(vd));
5430
5431 vdev_set_deflate_ratio(vd);
5432
5433 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
5434 vdev_is_concrete(vd)) {
5435 vdev_metaslab_group_create(vd);
5436 VERIFY(vdev_metaslab_init(vd, txg) == 0);
5437 vdev_config_dirty(vd);
5438 }
5439 }
5440
5441 /*
5442 * Split a vdev.
5443 */
5444 void
5445 vdev_split(vdev_t *vd)
5446 {
5447 vdev_t *cvd, *pvd = vd->vdev_parent;
5448
5449 VERIFY3U(pvd->vdev_children, >, 1);
5450
5451 vdev_remove_child(pvd, vd);
5452 vdev_compact_children(pvd);
5453
5454 ASSERT3P(pvd->vdev_child, !=, NULL);
5455
5456 cvd = pvd->vdev_child[0];
5457 if (pvd->vdev_children == 1) {
5458 vdev_remove_parent(cvd);
5459 cvd->vdev_splitting = B_TRUE;
5460 }
5461 vdev_propagate_state(cvd);
5462 }
5463
5464 void
5465 vdev_deadman(vdev_t *vd, const char *tag)
5466 {
5467 for (int c = 0; c < vd->vdev_children; c++) {
5468 vdev_t *cvd = vd->vdev_child[c];
5469
5470 vdev_deadman(cvd, tag);
5471 }
5472
5473 if (vd->vdev_ops->vdev_op_leaf) {
5474 vdev_queue_t *vq = &vd->vdev_queue;
5475
5476 mutex_enter(&vq->vq_lock);
5477 if (vq->vq_active > 0) {
5478 spa_t *spa = vd->vdev_spa;
5479 zio_t *fio;
5480 uint64_t delta;
5481
5482 zfs_dbgmsg("slow vdev: %s has %u active IOs",
5483 vd->vdev_path, vq->vq_active);
5484
5485 /*
5486 * Look at the head of all the pending queues,
5487 * if any I/O has been outstanding for longer than
5488 * the spa_deadman_synctime invoke the deadman logic.
5489 */
5490 fio = list_head(&vq->vq_active_list);
5491 delta = gethrtime() - fio->io_timestamp;
5492 if (delta > spa_deadman_synctime(spa))
5493 zio_deadman(fio, tag);
5494 }
5495 mutex_exit(&vq->vq_lock);
5496 }
5497 }
5498
5499 void
5500 vdev_defer_resilver(vdev_t *vd)
5501 {
5502 ASSERT(vd->vdev_ops->vdev_op_leaf);
5503
5504 vd->vdev_resilver_deferred = B_TRUE;
5505 vd->vdev_spa->spa_resilver_deferred = B_TRUE;
5506 }
5507
5508 /*
5509 * Clears the resilver deferred flag on all leaf devs under vd. Returns
5510 * B_TRUE if we have devices that need to be resilvered and are available to
5511 * accept resilver I/Os.
5512 */
5513 boolean_t
5514 vdev_clear_resilver_deferred(vdev_t *vd, dmu_tx_t *tx)
5515 {
5516 boolean_t resilver_needed = B_FALSE;
5517 spa_t *spa = vd->vdev_spa;
5518
5519 for (int c = 0; c < vd->vdev_children; c++) {
5520 vdev_t *cvd = vd->vdev_child[c];
5521 resilver_needed |= vdev_clear_resilver_deferred(cvd, tx);
5522 }
5523
5524 if (vd == spa->spa_root_vdev &&
5525 spa_feature_is_active(spa, SPA_FEATURE_RESILVER_DEFER)) {
5526 spa_feature_decr(spa, SPA_FEATURE_RESILVER_DEFER, tx);
5527 vdev_config_dirty(vd);
5528 spa->spa_resilver_deferred = B_FALSE;
5529 return (resilver_needed);
5530 }
5531
5532 if (!vdev_is_concrete(vd) || vd->vdev_aux ||
5533 !vd->vdev_ops->vdev_op_leaf)
5534 return (resilver_needed);
5535
5536 vd->vdev_resilver_deferred = B_FALSE;
5537
5538 return (!vdev_is_dead(vd) && !vd->vdev_offline &&
5539 vdev_resilver_needed(vd, NULL, NULL));
5540 }
5541
5542 boolean_t
5543 vdev_xlate_is_empty(range_seg64_t *rs)
5544 {
5545 return (rs->rs_start == rs->rs_end);
5546 }
5547
5548 /*
5549 * Translate a logical range to the first contiguous physical range for the
5550 * specified vdev_t. This function is initially called with a leaf vdev and
5551 * will walk each parent vdev until it reaches a top-level vdev. Once the
5552 * top-level is reached the physical range is initialized and the recursive
5553 * function begins to unwind. As it unwinds it calls the parent's vdev
5554 * specific translation function to do the real conversion.
5555 */
5556 void
5557 vdev_xlate(vdev_t *vd, const range_seg64_t *logical_rs,
5558 range_seg64_t *physical_rs, range_seg64_t *remain_rs)
5559 {
5560 /*
5561 * Walk up the vdev tree
5562 */
5563 if (vd != vd->vdev_top) {
5564 vdev_xlate(vd->vdev_parent, logical_rs, physical_rs,
5565 remain_rs);
5566 } else {
5567 /*
5568 * We've reached the top-level vdev, initialize the physical
5569 * range to the logical range and set an empty remaining
5570 * range then start to unwind.
5571 */
5572 physical_rs->rs_start = logical_rs->rs_start;
5573 physical_rs->rs_end = logical_rs->rs_end;
5574
5575 remain_rs->rs_start = logical_rs->rs_start;
5576 remain_rs->rs_end = logical_rs->rs_start;
5577
5578 return;
5579 }
5580
5581 vdev_t *pvd = vd->vdev_parent;
5582 ASSERT3P(pvd, !=, NULL);
5583 ASSERT3P(pvd->vdev_ops->vdev_op_xlate, !=, NULL);
5584
5585 /*
5586 * As this recursive function unwinds, translate the logical
5587 * range into its physical and any remaining components by calling
5588 * the vdev specific translate function.
5589 */
5590 range_seg64_t intermediate = { 0 };
5591 pvd->vdev_ops->vdev_op_xlate(vd, physical_rs, &intermediate, remain_rs);
5592
5593 physical_rs->rs_start = intermediate.rs_start;
5594 physical_rs->rs_end = intermediate.rs_end;
5595 }
5596
5597 void
5598 vdev_xlate_walk(vdev_t *vd, const range_seg64_t *logical_rs,
5599 vdev_xlate_func_t *func, void *arg)
5600 {
5601 range_seg64_t iter_rs = *logical_rs;
5602 range_seg64_t physical_rs;
5603 range_seg64_t remain_rs;
5604
5605 while (!vdev_xlate_is_empty(&iter_rs)) {
5606
5607 vdev_xlate(vd, &iter_rs, &physical_rs, &remain_rs);
5608
5609 /*
5610 * With raidz and dRAID, it's possible that the logical range
5611 * does not live on this leaf vdev. Only when there is a non-
5612 * zero physical size call the provided function.
5613 */
5614 if (!vdev_xlate_is_empty(&physical_rs))
5615 func(arg, &physical_rs);
5616
5617 iter_rs = remain_rs;
5618 }
5619 }
5620
5621 static char *
5622 vdev_name(vdev_t *vd, char *buf, int buflen)
5623 {
5624 if (vd->vdev_path == NULL) {
5625 if (strcmp(vd->vdev_ops->vdev_op_type, "root") == 0) {
5626 strlcpy(buf, vd->vdev_spa->spa_name, buflen);
5627 } else if (!vd->vdev_ops->vdev_op_leaf) {
5628 snprintf(buf, buflen, "%s-%llu",
5629 vd->vdev_ops->vdev_op_type,
5630 (u_longlong_t)vd->vdev_id);
5631 }
5632 } else {
5633 strlcpy(buf, vd->vdev_path, buflen);
5634 }
5635 return (buf);
5636 }
5637
5638 /*
5639 * Look at the vdev tree and determine whether any devices are currently being
5640 * replaced.
5641 */
5642 boolean_t
5643 vdev_replace_in_progress(vdev_t *vdev)
5644 {
5645 ASSERT(spa_config_held(vdev->vdev_spa, SCL_ALL, RW_READER) != 0);
5646
5647 if (vdev->vdev_ops == &vdev_replacing_ops)
5648 return (B_TRUE);
5649
5650 /*
5651 * A 'spare' vdev indicates that we have a replace in progress, unless
5652 * it has exactly two children, and the second, the hot spare, has
5653 * finished being resilvered.
5654 */
5655 if (vdev->vdev_ops == &vdev_spare_ops && (vdev->vdev_children > 2 ||
5656 !vdev_dtl_empty(vdev->vdev_child[1], DTL_MISSING)))
5657 return (B_TRUE);
5658
5659 for (int i = 0; i < vdev->vdev_children; i++) {
5660 if (vdev_replace_in_progress(vdev->vdev_child[i]))
5661 return (B_TRUE);
5662 }
5663
5664 return (B_FALSE);
5665 }
5666
5667 /*
5668 * Add a (source=src, propname=propval) list to an nvlist.
5669 */
5670 static void
5671 vdev_prop_add_list(nvlist_t *nvl, const char *propname, const char *strval,
5672 uint64_t intval, zprop_source_t src)
5673 {
5674 nvlist_t *propval;
5675
5676 propval = fnvlist_alloc();
5677 fnvlist_add_uint64(propval, ZPROP_SOURCE, src);
5678
5679 if (strval != NULL)
5680 fnvlist_add_string(propval, ZPROP_VALUE, strval);
5681 else
5682 fnvlist_add_uint64(propval, ZPROP_VALUE, intval);
5683
5684 fnvlist_add_nvlist(nvl, propname, propval);
5685 nvlist_free(propval);
5686 }
5687
5688 static void
5689 vdev_props_set_sync(void *arg, dmu_tx_t *tx)
5690 {
5691 vdev_t *vd;
5692 nvlist_t *nvp = arg;
5693 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
5694 objset_t *mos = spa->spa_meta_objset;
5695 nvpair_t *elem = NULL;
5696 uint64_t vdev_guid;
5697 nvlist_t *nvprops;
5698
5699 vdev_guid = fnvlist_lookup_uint64(nvp, ZPOOL_VDEV_PROPS_SET_VDEV);
5700 nvprops = fnvlist_lookup_nvlist(nvp, ZPOOL_VDEV_PROPS_SET_PROPS);
5701 vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE);
5702
5703 /* this vdev could get removed while waiting for this sync task */
5704 if (vd == NULL)
5705 return;
5706
5707 mutex_enter(&spa->spa_props_lock);
5708
5709 while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
5710 uint64_t intval, objid = 0;
5711 const char *strval;
5712 vdev_prop_t prop;
5713 const char *propname = nvpair_name(elem);
5714 zprop_type_t proptype;
5715
5716 /*
5717 * Set vdev property values in the vdev props mos object.
5718 */
5719 if (vd->vdev_root_zap != 0) {
5720 objid = vd->vdev_root_zap;
5721 } else if (vd->vdev_top_zap != 0) {
5722 objid = vd->vdev_top_zap;
5723 } else if (vd->vdev_leaf_zap != 0) {
5724 objid = vd->vdev_leaf_zap;
5725 } else {
5726 /*
5727 * XXX: implement vdev_props_set_check()
5728 */
5729 panic("vdev not root/top/leaf");
5730 }
5731
5732 switch (prop = vdev_name_to_prop(propname)) {
5733 case VDEV_PROP_USERPROP:
5734 if (vdev_prop_user(propname)) {
5735 strval = fnvpair_value_string(elem);
5736 if (strlen(strval) == 0) {
5737 /* remove the property if value == "" */
5738 (void) zap_remove(mos, objid, propname,
5739 tx);
5740 } else {
5741 VERIFY0(zap_update(mos, objid, propname,
5742 1, strlen(strval) + 1, strval, tx));
5743 }
5744 spa_history_log_internal(spa, "vdev set", tx,
5745 "vdev_guid=%llu: %s=%s",
5746 (u_longlong_t)vdev_guid, nvpair_name(elem),
5747 strval);
5748 }
5749 break;
5750 default:
5751 /* normalize the property name */
5752 propname = vdev_prop_to_name(prop);
5753 proptype = vdev_prop_get_type(prop);
5754
5755 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5756 ASSERT(proptype == PROP_TYPE_STRING);
5757 strval = fnvpair_value_string(elem);
5758 VERIFY0(zap_update(mos, objid, propname,
5759 1, strlen(strval) + 1, strval, tx));
5760 spa_history_log_internal(spa, "vdev set", tx,
5761 "vdev_guid=%llu: %s=%s",
5762 (u_longlong_t)vdev_guid, nvpair_name(elem),
5763 strval);
5764 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5765 intval = fnvpair_value_uint64(elem);
5766
5767 if (proptype == PROP_TYPE_INDEX) {
5768 const char *unused;
5769 VERIFY0(vdev_prop_index_to_string(
5770 prop, intval, &unused));
5771 }
5772 VERIFY0(zap_update(mos, objid, propname,
5773 sizeof (uint64_t), 1, &intval, tx));
5774 spa_history_log_internal(spa, "vdev set", tx,
5775 "vdev_guid=%llu: %s=%lld",
5776 (u_longlong_t)vdev_guid,
5777 nvpair_name(elem), (longlong_t)intval);
5778 } else {
5779 panic("invalid vdev property type %u",
5780 nvpair_type(elem));
5781 }
5782 }
5783
5784 }
5785
5786 mutex_exit(&spa->spa_props_lock);
5787 }
5788
5789 int
5790 vdev_prop_set(vdev_t *vd, nvlist_t *innvl, nvlist_t *outnvl)
5791 {
5792 spa_t *spa = vd->vdev_spa;
5793 nvpair_t *elem = NULL;
5794 uint64_t vdev_guid;
5795 nvlist_t *nvprops;
5796 int error = 0;
5797
5798 ASSERT(vd != NULL);
5799
5800 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_SET_VDEV,
5801 &vdev_guid) != 0)
5802 return (SET_ERROR(EINVAL));
5803
5804 if (nvlist_lookup_nvlist(innvl, ZPOOL_VDEV_PROPS_SET_PROPS,
5805 &nvprops) != 0)
5806 return (SET_ERROR(EINVAL));
5807
5808 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL)
5809 return (SET_ERROR(EINVAL));
5810
5811 while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
5812 const char *propname = nvpair_name(elem);
5813 vdev_prop_t prop = vdev_name_to_prop(propname);
5814 uint64_t intval = 0;
5815 const char *strval = NULL;
5816
5817 if (prop == VDEV_PROP_USERPROP && !vdev_prop_user(propname)) {
5818 error = EINVAL;
5819 goto end;
5820 }
5821
5822 if (vdev_prop_readonly(prop)) {
5823 error = EROFS;
5824 goto end;
5825 }
5826
5827 /* Special Processing */
5828 switch (prop) {
5829 case VDEV_PROP_PATH:
5830 if (vd->vdev_path == NULL) {
5831 error = EROFS;
5832 break;
5833 }
5834 if (nvpair_value_string(elem, &strval) != 0) {
5835 error = EINVAL;
5836 break;
5837 }
5838 /* New path must start with /dev/ */
5839 if (strncmp(strval, "/dev/", 5)) {
5840 error = EINVAL;
5841 break;
5842 }
5843 error = spa_vdev_setpath(spa, vdev_guid, strval);
5844 break;
5845 case VDEV_PROP_ALLOCATING:
5846 if (nvpair_value_uint64(elem, &intval) != 0) {
5847 error = EINVAL;
5848 break;
5849 }
5850 if (intval != vd->vdev_noalloc)
5851 break;
5852 if (intval == 0)
5853 error = spa_vdev_noalloc(spa, vdev_guid);
5854 else
5855 error = spa_vdev_alloc(spa, vdev_guid);
5856 break;
5857 case VDEV_PROP_FAILFAST:
5858 if (nvpair_value_uint64(elem, &intval) != 0) {
5859 error = EINVAL;
5860 break;
5861 }
5862 vd->vdev_failfast = intval & 1;
5863 break;
5864 case VDEV_PROP_CHECKSUM_N:
5865 if (nvpair_value_uint64(elem, &intval) != 0) {
5866 error = EINVAL;
5867 break;
5868 }
5869 vd->vdev_checksum_n = intval;
5870 break;
5871 case VDEV_PROP_CHECKSUM_T:
5872 if (nvpair_value_uint64(elem, &intval) != 0) {
5873 error = EINVAL;
5874 break;
5875 }
5876 vd->vdev_checksum_t = intval;
5877 break;
5878 case VDEV_PROP_IO_N:
5879 if (nvpair_value_uint64(elem, &intval) != 0) {
5880 error = EINVAL;
5881 break;
5882 }
5883 vd->vdev_io_n = intval;
5884 break;
5885 case VDEV_PROP_IO_T:
5886 if (nvpair_value_uint64(elem, &intval) != 0) {
5887 error = EINVAL;
5888 break;
5889 }
5890 vd->vdev_io_t = intval;
5891 break;
5892 default:
5893 /* Most processing is done in vdev_props_set_sync */
5894 break;
5895 }
5896 end:
5897 if (error != 0) {
5898 intval = error;
5899 vdev_prop_add_list(outnvl, propname, strval, intval, 0);
5900 return (error);
5901 }
5902 }
5903
5904 return (dsl_sync_task(spa->spa_name, NULL, vdev_props_set_sync,
5905 innvl, 6, ZFS_SPACE_CHECK_EXTRA_RESERVED));
5906 }
5907
5908 int
5909 vdev_prop_get(vdev_t *vd, nvlist_t *innvl, nvlist_t *outnvl)
5910 {
5911 spa_t *spa = vd->vdev_spa;
5912 objset_t *mos = spa->spa_meta_objset;
5913 int err = 0;
5914 uint64_t objid;
5915 uint64_t vdev_guid;
5916 nvpair_t *elem = NULL;
5917 nvlist_t *nvprops = NULL;
5918 uint64_t intval = 0;
5919 char *strval = NULL;
5920 const char *propname = NULL;
5921 vdev_prop_t prop;
5922
5923 ASSERT(vd != NULL);
5924 ASSERT(mos != NULL);
5925
5926 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_GET_VDEV,
5927 &vdev_guid) != 0)
5928 return (SET_ERROR(EINVAL));
5929
5930 nvlist_lookup_nvlist(innvl, ZPOOL_VDEV_PROPS_GET_PROPS, &nvprops);
5931
5932 if (vd->vdev_root_zap != 0) {
5933 objid = vd->vdev_root_zap;
5934 } else if (vd->vdev_top_zap != 0) {
5935 objid = vd->vdev_top_zap;
5936 } else if (vd->vdev_leaf_zap != 0) {
5937 objid = vd->vdev_leaf_zap;
5938 } else {
5939 return (SET_ERROR(EINVAL));
5940 }
5941 ASSERT(objid != 0);
5942
5943 mutex_enter(&spa->spa_props_lock);
5944
5945 if (nvprops != NULL) {
5946 char namebuf[64] = { 0 };
5947
5948 while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
5949 intval = 0;
5950 strval = NULL;
5951 propname = nvpair_name(elem);
5952 prop = vdev_name_to_prop(propname);
5953 zprop_source_t src = ZPROP_SRC_DEFAULT;
5954 uint64_t integer_size, num_integers;
5955
5956 switch (prop) {
5957 /* Special Read-only Properties */
5958 case VDEV_PROP_NAME:
5959 strval = vdev_name(vd, namebuf,
5960 sizeof (namebuf));
5961 if (strval == NULL)
5962 continue;
5963 vdev_prop_add_list(outnvl, propname, strval, 0,
5964 ZPROP_SRC_NONE);
5965 continue;
5966 case VDEV_PROP_CAPACITY:
5967 /* percent used */
5968 intval = (vd->vdev_stat.vs_dspace == 0) ? 0 :
5969 (vd->vdev_stat.vs_alloc * 100 /
5970 vd->vdev_stat.vs_dspace);
5971 vdev_prop_add_list(outnvl, propname, NULL,
5972 intval, ZPROP_SRC_NONE);
5973 continue;
5974 case VDEV_PROP_STATE:
5975 vdev_prop_add_list(outnvl, propname, NULL,
5976 vd->vdev_state, ZPROP_SRC_NONE);
5977 continue;
5978 case VDEV_PROP_GUID:
5979 vdev_prop_add_list(outnvl, propname, NULL,
5980 vd->vdev_guid, ZPROP_SRC_NONE);
5981 continue;
5982 case VDEV_PROP_ASIZE:
5983 vdev_prop_add_list(outnvl, propname, NULL,
5984 vd->vdev_asize, ZPROP_SRC_NONE);
5985 continue;
5986 case VDEV_PROP_PSIZE:
5987 vdev_prop_add_list(outnvl, propname, NULL,
5988 vd->vdev_psize, ZPROP_SRC_NONE);
5989 continue;
5990 case VDEV_PROP_ASHIFT:
5991 vdev_prop_add_list(outnvl, propname, NULL,
5992 vd->vdev_ashift, ZPROP_SRC_NONE);
5993 continue;
5994 case VDEV_PROP_SIZE:
5995 vdev_prop_add_list(outnvl, propname, NULL,
5996 vd->vdev_stat.vs_dspace, ZPROP_SRC_NONE);
5997 continue;
5998 case VDEV_PROP_FREE:
5999 vdev_prop_add_list(outnvl, propname, NULL,
6000 vd->vdev_stat.vs_dspace -
6001 vd->vdev_stat.vs_alloc, ZPROP_SRC_NONE);
6002 continue;
6003 case VDEV_PROP_ALLOCATED:
6004 vdev_prop_add_list(outnvl, propname, NULL,
6005 vd->vdev_stat.vs_alloc, ZPROP_SRC_NONE);
6006 continue;
6007 case VDEV_PROP_EXPANDSZ:
6008 vdev_prop_add_list(outnvl, propname, NULL,
6009 vd->vdev_stat.vs_esize, ZPROP_SRC_NONE);
6010 continue;
6011 case VDEV_PROP_FRAGMENTATION:
6012 vdev_prop_add_list(outnvl, propname, NULL,
6013 vd->vdev_stat.vs_fragmentation,
6014 ZPROP_SRC_NONE);
6015 continue;
6016 case VDEV_PROP_PARITY:
6017 vdev_prop_add_list(outnvl, propname, NULL,
6018 vdev_get_nparity(vd), ZPROP_SRC_NONE);
6019 continue;
6020 case VDEV_PROP_PATH:
6021 if (vd->vdev_path == NULL)
6022 continue;
6023 vdev_prop_add_list(outnvl, propname,
6024 vd->vdev_path, 0, ZPROP_SRC_NONE);
6025 continue;
6026 case VDEV_PROP_DEVID:
6027 if (vd->vdev_devid == NULL)
6028 continue;
6029 vdev_prop_add_list(outnvl, propname,
6030 vd->vdev_devid, 0, ZPROP_SRC_NONE);
6031 continue;
6032 case VDEV_PROP_PHYS_PATH:
6033 if (vd->vdev_physpath == NULL)
6034 continue;
6035 vdev_prop_add_list(outnvl, propname,
6036 vd->vdev_physpath, 0, ZPROP_SRC_NONE);
6037 continue;
6038 case VDEV_PROP_ENC_PATH:
6039 if (vd->vdev_enc_sysfs_path == NULL)
6040 continue;
6041 vdev_prop_add_list(outnvl, propname,
6042 vd->vdev_enc_sysfs_path, 0, ZPROP_SRC_NONE);
6043 continue;
6044 case VDEV_PROP_FRU:
6045 if (vd->vdev_fru == NULL)
6046 continue;
6047 vdev_prop_add_list(outnvl, propname,
6048 vd->vdev_fru, 0, ZPROP_SRC_NONE);
6049 continue;
6050 case VDEV_PROP_PARENT:
6051 if (vd->vdev_parent != NULL) {
6052 strval = vdev_name(vd->vdev_parent,
6053 namebuf, sizeof (namebuf));
6054 vdev_prop_add_list(outnvl, propname,
6055 strval, 0, ZPROP_SRC_NONE);
6056 }
6057 continue;
6058 case VDEV_PROP_CHILDREN:
6059 if (vd->vdev_children > 0)
6060 strval = kmem_zalloc(ZAP_MAXVALUELEN,
6061 KM_SLEEP);
6062 for (uint64_t i = 0; i < vd->vdev_children;
6063 i++) {
6064 const char *vname;
6065
6066 vname = vdev_name(vd->vdev_child[i],
6067 namebuf, sizeof (namebuf));
6068 if (vname == NULL)
6069 vname = "(unknown)";
6070 if (strlen(strval) > 0)
6071 strlcat(strval, ",",
6072 ZAP_MAXVALUELEN);
6073 strlcat(strval, vname, ZAP_MAXVALUELEN);
6074 }
6075 if (strval != NULL) {
6076 vdev_prop_add_list(outnvl, propname,
6077 strval, 0, ZPROP_SRC_NONE);
6078 kmem_free(strval, ZAP_MAXVALUELEN);
6079 }
6080 continue;
6081 case VDEV_PROP_NUMCHILDREN:
6082 vdev_prop_add_list(outnvl, propname, NULL,
6083 vd->vdev_children, ZPROP_SRC_NONE);
6084 continue;
6085 case VDEV_PROP_READ_ERRORS:
6086 vdev_prop_add_list(outnvl, propname, NULL,
6087 vd->vdev_stat.vs_read_errors,
6088 ZPROP_SRC_NONE);
6089 continue;
6090 case VDEV_PROP_WRITE_ERRORS:
6091 vdev_prop_add_list(outnvl, propname, NULL,
6092 vd->vdev_stat.vs_write_errors,
6093 ZPROP_SRC_NONE);
6094 continue;
6095 case VDEV_PROP_CHECKSUM_ERRORS:
6096 vdev_prop_add_list(outnvl, propname, NULL,
6097 vd->vdev_stat.vs_checksum_errors,
6098 ZPROP_SRC_NONE);
6099 continue;
6100 case VDEV_PROP_INITIALIZE_ERRORS:
6101 vdev_prop_add_list(outnvl, propname, NULL,
6102 vd->vdev_stat.vs_initialize_errors,
6103 ZPROP_SRC_NONE);
6104 continue;
6105 case VDEV_PROP_OPS_NULL:
6106 vdev_prop_add_list(outnvl, propname, NULL,
6107 vd->vdev_stat.vs_ops[ZIO_TYPE_NULL],
6108 ZPROP_SRC_NONE);
6109 continue;
6110 case VDEV_PROP_OPS_READ:
6111 vdev_prop_add_list(outnvl, propname, NULL,
6112 vd->vdev_stat.vs_ops[ZIO_TYPE_READ],
6113 ZPROP_SRC_NONE);
6114 continue;
6115 case VDEV_PROP_OPS_WRITE:
6116 vdev_prop_add_list(outnvl, propname, NULL,
6117 vd->vdev_stat.vs_ops[ZIO_TYPE_WRITE],
6118 ZPROP_SRC_NONE);
6119 continue;
6120 case VDEV_PROP_OPS_FREE:
6121 vdev_prop_add_list(outnvl, propname, NULL,
6122 vd->vdev_stat.vs_ops[ZIO_TYPE_FREE],
6123 ZPROP_SRC_NONE);
6124 continue;
6125 case VDEV_PROP_OPS_CLAIM:
6126 vdev_prop_add_list(outnvl, propname, NULL,
6127 vd->vdev_stat.vs_ops[ZIO_TYPE_CLAIM],
6128 ZPROP_SRC_NONE);
6129 continue;
6130 case VDEV_PROP_OPS_TRIM:
6131 /*
6132 * TRIM ops and bytes are reported to user
6133 * space as ZIO_TYPE_IOCTL. This is done to
6134 * preserve the vdev_stat_t structure layout
6135 * for user space.
6136 */
6137 vdev_prop_add_list(outnvl, propname, NULL,
6138 vd->vdev_stat.vs_ops[ZIO_TYPE_IOCTL],
6139 ZPROP_SRC_NONE);
6140 continue;
6141 case VDEV_PROP_BYTES_NULL:
6142 vdev_prop_add_list(outnvl, propname, NULL,
6143 vd->vdev_stat.vs_bytes[ZIO_TYPE_NULL],
6144 ZPROP_SRC_NONE);
6145 continue;
6146 case VDEV_PROP_BYTES_READ:
6147 vdev_prop_add_list(outnvl, propname, NULL,
6148 vd->vdev_stat.vs_bytes[ZIO_TYPE_READ],
6149 ZPROP_SRC_NONE);
6150 continue;
6151 case VDEV_PROP_BYTES_WRITE:
6152 vdev_prop_add_list(outnvl, propname, NULL,
6153 vd->vdev_stat.vs_bytes[ZIO_TYPE_WRITE],
6154 ZPROP_SRC_NONE);
6155 continue;
6156 case VDEV_PROP_BYTES_FREE:
6157 vdev_prop_add_list(outnvl, propname, NULL,
6158 vd->vdev_stat.vs_bytes[ZIO_TYPE_FREE],
6159 ZPROP_SRC_NONE);
6160 continue;
6161 case VDEV_PROP_BYTES_CLAIM:
6162 vdev_prop_add_list(outnvl, propname, NULL,
6163 vd->vdev_stat.vs_bytes[ZIO_TYPE_CLAIM],
6164 ZPROP_SRC_NONE);
6165 continue;
6166 case VDEV_PROP_BYTES_TRIM:
6167 /*
6168 * TRIM ops and bytes are reported to user
6169 * space as ZIO_TYPE_IOCTL. This is done to
6170 * preserve the vdev_stat_t structure layout
6171 * for user space.
6172 */
6173 vdev_prop_add_list(outnvl, propname, NULL,
6174 vd->vdev_stat.vs_bytes[ZIO_TYPE_IOCTL],
6175 ZPROP_SRC_NONE);
6176 continue;
6177 case VDEV_PROP_REMOVING:
6178 vdev_prop_add_list(outnvl, propname, NULL,
6179 vd->vdev_removing, ZPROP_SRC_NONE);
6180 continue;
6181 /* Numeric Properites */
6182 case VDEV_PROP_ALLOCATING:
6183 /* Leaf vdevs cannot have this property */
6184 if (vd->vdev_mg == NULL &&
6185 vd->vdev_top != NULL) {
6186 src = ZPROP_SRC_NONE;
6187 intval = ZPROP_BOOLEAN_NA;
6188 } else {
6189 err = vdev_prop_get_int(vd, prop,
6190 &intval);
6191 if (err && err != ENOENT)
6192 break;
6193
6194 if (intval ==
6195 vdev_prop_default_numeric(prop))
6196 src = ZPROP_SRC_DEFAULT;
6197 else
6198 src = ZPROP_SRC_LOCAL;
6199 }
6200
6201 vdev_prop_add_list(outnvl, propname, NULL,
6202 intval, src);
6203 break;
6204 case VDEV_PROP_FAILFAST:
6205 src = ZPROP_SRC_LOCAL;
6206 strval = NULL;
6207
6208 err = zap_lookup(mos, objid, nvpair_name(elem),
6209 sizeof (uint64_t), 1, &intval);
6210 if (err == ENOENT) {
6211 intval = vdev_prop_default_numeric(
6212 prop);
6213 err = 0;
6214 } else if (err) {
6215 break;
6216 }
6217 if (intval == vdev_prop_default_numeric(prop))
6218 src = ZPROP_SRC_DEFAULT;
6219
6220 vdev_prop_add_list(outnvl, propname, strval,
6221 intval, src);
6222 break;
6223 case VDEV_PROP_CHECKSUM_N:
6224 case VDEV_PROP_CHECKSUM_T:
6225 case VDEV_PROP_IO_N:
6226 case VDEV_PROP_IO_T:
6227 err = vdev_prop_get_int(vd, prop, &intval);
6228 if (err && err != ENOENT)
6229 break;
6230
6231 if (intval == vdev_prop_default_numeric(prop))
6232 src = ZPROP_SRC_DEFAULT;
6233 else
6234 src = ZPROP_SRC_LOCAL;
6235
6236 vdev_prop_add_list(outnvl, propname, NULL,
6237 intval, src);
6238 break;
6239 /* Text Properties */
6240 case VDEV_PROP_COMMENT:
6241 /* Exists in the ZAP below */
6242 /* FALLTHRU */
6243 case VDEV_PROP_USERPROP:
6244 /* User Properites */
6245 src = ZPROP_SRC_LOCAL;
6246
6247 err = zap_length(mos, objid, nvpair_name(elem),
6248 &integer_size, &num_integers);
6249 if (err)
6250 break;
6251
6252 switch (integer_size) {
6253 case 8:
6254 /* User properties cannot be integers */
6255 err = EINVAL;
6256 break;
6257 case 1:
6258 /* string property */
6259 strval = kmem_alloc(num_integers,
6260 KM_SLEEP);
6261 err = zap_lookup(mos, objid,
6262 nvpair_name(elem), 1,
6263 num_integers, strval);
6264 if (err) {
6265 kmem_free(strval,
6266 num_integers);
6267 break;
6268 }
6269 vdev_prop_add_list(outnvl, propname,
6270 strval, 0, src);
6271 kmem_free(strval, num_integers);
6272 break;
6273 }
6274 break;
6275 default:
6276 err = ENOENT;
6277 break;
6278 }
6279 if (err)
6280 break;
6281 }
6282 } else {
6283 /*
6284 * Get all properties from the MOS vdev property object.
6285 */
6286 zap_cursor_t zc;
6287 zap_attribute_t za;
6288 for (zap_cursor_init(&zc, mos, objid);
6289 (err = zap_cursor_retrieve(&zc, &za)) == 0;
6290 zap_cursor_advance(&zc)) {
6291 intval = 0;
6292 strval = NULL;
6293 zprop_source_t src = ZPROP_SRC_DEFAULT;
6294 propname = za.za_name;
6295
6296 switch (za.za_integer_length) {
6297 case 8:
6298 /* We do not allow integer user properties */
6299 /* This is likely an internal value */
6300 break;
6301 case 1:
6302 /* string property */
6303 strval = kmem_alloc(za.za_num_integers,
6304 KM_SLEEP);
6305 err = zap_lookup(mos, objid, za.za_name, 1,
6306 za.za_num_integers, strval);
6307 if (err) {
6308 kmem_free(strval, za.za_num_integers);
6309 break;
6310 }
6311 vdev_prop_add_list(outnvl, propname, strval, 0,
6312 src);
6313 kmem_free(strval, za.za_num_integers);
6314 break;
6315
6316 default:
6317 break;
6318 }
6319 }
6320 zap_cursor_fini(&zc);
6321 }
6322
6323 mutex_exit(&spa->spa_props_lock);
6324 if (err && err != ENOENT) {
6325 return (err);
6326 }
6327
6328 return (0);
6329 }
6330
6331 EXPORT_SYMBOL(vdev_fault);
6332 EXPORT_SYMBOL(vdev_degrade);
6333 EXPORT_SYMBOL(vdev_online);
6334 EXPORT_SYMBOL(vdev_offline);
6335 EXPORT_SYMBOL(vdev_clear);
6336
6337 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_count, UINT, ZMOD_RW,
6338 "Target number of metaslabs per top-level vdev");
6339
6340 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_shift, UINT, ZMOD_RW,
6341 "Default lower limit for metaslab size");
6342
6343 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, max_ms_shift, UINT, ZMOD_RW,
6344 "Default upper limit for metaslab size");
6345
6346 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, min_ms_count, UINT, ZMOD_RW,
6347 "Minimum number of metaslabs per top-level vdev");
6348
6349 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, ms_count_limit, UINT, ZMOD_RW,
6350 "Practical upper limit of total metaslabs per top-level vdev");
6351
6352 ZFS_MODULE_PARAM(zfs, zfs_, slow_io_events_per_second, UINT, ZMOD_RW,
6353 "Rate limit slow IO (delay) events to this many per second");
6354
6355 /* BEGIN CSTYLED */
6356 ZFS_MODULE_PARAM(zfs, zfs_, checksum_events_per_second, UINT, ZMOD_RW,
6357 "Rate limit checksum events to this many checksum errors per second "
6358 "(do not set below ZED threshold).");
6359 /* END CSTYLED */
6360
6361 ZFS_MODULE_PARAM(zfs, zfs_, scan_ignore_errors, INT, ZMOD_RW,
6362 "Ignore errors during resilver/scrub");
6363
6364 ZFS_MODULE_PARAM(zfs_vdev, vdev_, validate_skip, INT, ZMOD_RW,
6365 "Bypass vdev_validate()");
6366
6367 ZFS_MODULE_PARAM(zfs, zfs_, nocacheflush, INT, ZMOD_RW,
6368 "Disable cache flushes");
6369
6370 ZFS_MODULE_PARAM(zfs, zfs_, embedded_slog_min_ms, UINT, ZMOD_RW,
6371 "Minimum number of metaslabs required to dedicate one for log blocks");
6372
6373 /* BEGIN CSTYLED */
6374 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, min_auto_ashift,
6375 param_set_min_auto_ashift, param_get_uint, ZMOD_RW,
6376 "Minimum ashift used when creating new top-level vdevs");
6377
6378 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, max_auto_ashift,
6379 param_set_max_auto_ashift, param_get_uint, ZMOD_RW,
6380 "Maximum ashift used when optimizing for logical -> physical sector "
6381 "size on new top-level vdevs");
6382 /* END CSTYLED */