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