]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/vdev.c
Parallelize vdev_load
[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 http://www.opensolaris.org/os/licensing.
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, 2020 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 */
32
33 #include <sys/zfs_context.h>
34 #include <sys/fm/fs/zfs.h>
35 #include <sys/spa.h>
36 #include <sys/spa_impl.h>
37 #include <sys/bpobj.h>
38 #include <sys/dmu.h>
39 #include <sys/dmu_tx.h>
40 #include <sys/dsl_dir.h>
41 #include <sys/vdev_impl.h>
42 #include <sys/vdev_rebuild.h>
43 #include <sys/vdev_draid.h>
44 #include <sys/uberblock_impl.h>
45 #include <sys/metaslab.h>
46 #include <sys/metaslab_impl.h>
47 #include <sys/space_map.h>
48 #include <sys/space_reftree.h>
49 #include <sys/zio.h>
50 #include <sys/zap.h>
51 #include <sys/fs/zfs.h>
52 #include <sys/arc.h>
53 #include <sys/zil.h>
54 #include <sys/dsl_scan.h>
55 #include <sys/vdev_raidz.h>
56 #include <sys/abd.h>
57 #include <sys/vdev_initialize.h>
58 #include <sys/vdev_trim.h>
59 #include <sys/zvol.h>
60 #include <sys/zfs_ratelimit.h>
61
62 /*
63 * One metaslab from each (normal-class) vdev is used by the ZIL. These are
64 * called "embedded slog metaslabs", are referenced by vdev_log_mg, and are
65 * part of the spa_embedded_log_class. The metaslab with the most free space
66 * in each vdev is selected for this purpose when the pool is opened (or a
67 * vdev is added). See vdev_metaslab_init().
68 *
69 * Log blocks can be allocated from the following locations. Each one is tried
70 * in order until the allocation succeeds:
71 * 1. dedicated log vdevs, aka "slog" (spa_log_class)
72 * 2. embedded slog metaslabs (spa_embedded_log_class)
73 * 3. other metaslabs in normal vdevs (spa_normal_class)
74 *
75 * zfs_embedded_slog_min_ms disables the embedded slog if there are fewer
76 * than this number of metaslabs in the vdev. This ensures that we don't set
77 * aside an unreasonable amount of space for the ZIL. If set to less than
78 * 1 << (spa_slop_shift + 1), on small pools the usable space may be reduced
79 * (by more than 1<<spa_slop_shift) due to the embedded slog metaslab.
80 */
81 int zfs_embedded_slog_min_ms = 64;
82
83 /* default target for number of metaslabs per top-level vdev */
84 int zfs_vdev_default_ms_count = 200;
85
86 /* minimum number of metaslabs per top-level vdev */
87 int zfs_vdev_min_ms_count = 16;
88
89 /* practical upper limit of total metaslabs per top-level vdev */
90 int zfs_vdev_ms_count_limit = 1ULL << 17;
91
92 /* lower limit for metaslab size (512M) */
93 int zfs_vdev_default_ms_shift = 29;
94
95 /* upper limit for metaslab size (16G) */
96 int zfs_vdev_max_ms_shift = 34;
97
98 int vdev_validate_skip = B_FALSE;
99
100 /*
101 * Since the DTL space map of a vdev is not expected to have a lot of
102 * entries, we default its block size to 4K.
103 */
104 int zfs_vdev_dtl_sm_blksz = (1 << 12);
105
106 /*
107 * Rate limit slow IO (delay) events to this many per second.
108 */
109 unsigned int zfs_slow_io_events_per_second = 20;
110
111 /*
112 * Rate limit checksum events after this many checksum errors per second.
113 */
114 unsigned int zfs_checksum_events_per_second = 20;
115
116 /*
117 * Ignore errors during scrub/resilver. Allows to work around resilver
118 * upon import when there are pool errors.
119 */
120 int zfs_scan_ignore_errors = 0;
121
122 /*
123 * vdev-wide space maps that have lots of entries written to them at
124 * the end of each transaction can benefit from a higher I/O bandwidth
125 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
126 */
127 int zfs_vdev_standard_sm_blksz = (1 << 17);
128
129 /*
130 * Tunable parameter for debugging or performance analysis. Setting this
131 * will cause pool corruption on power loss if a volatile out-of-order
132 * write cache is enabled.
133 */
134 int zfs_nocacheflush = 0;
135
136 uint64_t zfs_vdev_max_auto_ashift = ASHIFT_MAX;
137 uint64_t zfs_vdev_min_auto_ashift = ASHIFT_MIN;
138
139 /*PRINTFLIKE2*/
140 void
141 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
142 {
143 va_list adx;
144 char buf[256];
145
146 va_start(adx, fmt);
147 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
148 va_end(adx);
149
150 if (vd->vdev_path != NULL) {
151 zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
152 vd->vdev_path, buf);
153 } else {
154 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
155 vd->vdev_ops->vdev_op_type,
156 (u_longlong_t)vd->vdev_id,
157 (u_longlong_t)vd->vdev_guid, buf);
158 }
159 }
160
161 void
162 vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
163 {
164 char state[20];
165
166 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
167 zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id,
168 vd->vdev_ops->vdev_op_type);
169 return;
170 }
171
172 switch (vd->vdev_state) {
173 case VDEV_STATE_UNKNOWN:
174 (void) snprintf(state, sizeof (state), "unknown");
175 break;
176 case VDEV_STATE_CLOSED:
177 (void) snprintf(state, sizeof (state), "closed");
178 break;
179 case VDEV_STATE_OFFLINE:
180 (void) snprintf(state, sizeof (state), "offline");
181 break;
182 case VDEV_STATE_REMOVED:
183 (void) snprintf(state, sizeof (state), "removed");
184 break;
185 case VDEV_STATE_CANT_OPEN:
186 (void) snprintf(state, sizeof (state), "can't open");
187 break;
188 case VDEV_STATE_FAULTED:
189 (void) snprintf(state, sizeof (state), "faulted");
190 break;
191 case VDEV_STATE_DEGRADED:
192 (void) snprintf(state, sizeof (state), "degraded");
193 break;
194 case VDEV_STATE_HEALTHY:
195 (void) snprintf(state, sizeof (state), "healthy");
196 break;
197 default:
198 (void) snprintf(state, sizeof (state), "<state %u>",
199 (uint_t)vd->vdev_state);
200 }
201
202 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
203 "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type,
204 vd->vdev_islog ? " (log)" : "",
205 (u_longlong_t)vd->vdev_guid,
206 vd->vdev_path ? vd->vdev_path : "N/A", state);
207
208 for (uint64_t i = 0; i < vd->vdev_children; i++)
209 vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
210 }
211
212 /*
213 * Virtual device management.
214 */
215
216 static vdev_ops_t *vdev_ops_table[] = {
217 &vdev_root_ops,
218 &vdev_raidz_ops,
219 &vdev_draid_ops,
220 &vdev_draid_spare_ops,
221 &vdev_mirror_ops,
222 &vdev_replacing_ops,
223 &vdev_spare_ops,
224 &vdev_disk_ops,
225 &vdev_file_ops,
226 &vdev_missing_ops,
227 &vdev_hole_ops,
228 &vdev_indirect_ops,
229 NULL
230 };
231
232 /*
233 * Given a vdev type, return the appropriate ops vector.
234 */
235 static vdev_ops_t *
236 vdev_getops(const char *type)
237 {
238 vdev_ops_t *ops, **opspp;
239
240 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
241 if (strcmp(ops->vdev_op_type, type) == 0)
242 break;
243
244 return (ops);
245 }
246
247 /*
248 * Given a vdev and a metaslab class, find which metaslab group we're
249 * interested in. All vdevs may belong to two different metaslab classes.
250 * Dedicated slog devices use only the primary metaslab group, rather than a
251 * separate log group. For embedded slogs, the vdev_log_mg will be non-NULL.
252 */
253 metaslab_group_t *
254 vdev_get_mg(vdev_t *vd, metaslab_class_t *mc)
255 {
256 if (mc == spa_embedded_log_class(vd->vdev_spa) &&
257 vd->vdev_log_mg != NULL)
258 return (vd->vdev_log_mg);
259 else
260 return (vd->vdev_mg);
261 }
262
263 /* ARGSUSED */
264 void
265 vdev_default_xlate(vdev_t *vd, const range_seg64_t *logical_rs,
266 range_seg64_t *physical_rs, range_seg64_t *remain_rs)
267 {
268 physical_rs->rs_start = logical_rs->rs_start;
269 physical_rs->rs_end = logical_rs->rs_end;
270 }
271
272 /*
273 * Derive the enumerated allocation bias from string input.
274 * String origin is either the per-vdev zap or zpool(8).
275 */
276 static vdev_alloc_bias_t
277 vdev_derive_alloc_bias(const char *bias)
278 {
279 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
280
281 if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0)
282 alloc_bias = VDEV_BIAS_LOG;
283 else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
284 alloc_bias = VDEV_BIAS_SPECIAL;
285 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
286 alloc_bias = VDEV_BIAS_DEDUP;
287
288 return (alloc_bias);
289 }
290
291 /*
292 * Default asize function: return the MAX of psize with the asize of
293 * all children. This is what's used by anything other than RAID-Z.
294 */
295 uint64_t
296 vdev_default_asize(vdev_t *vd, uint64_t psize)
297 {
298 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
299 uint64_t csize;
300
301 for (int c = 0; c < vd->vdev_children; c++) {
302 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
303 asize = MAX(asize, csize);
304 }
305
306 return (asize);
307 }
308
309 uint64_t
310 vdev_default_min_asize(vdev_t *vd)
311 {
312 return (vd->vdev_min_asize);
313 }
314
315 /*
316 * Get the minimum allocatable size. We define the allocatable size as
317 * the vdev's asize rounded to the nearest metaslab. This allows us to
318 * replace or attach devices which don't have the same physical size but
319 * can still satisfy the same number of allocations.
320 */
321 uint64_t
322 vdev_get_min_asize(vdev_t *vd)
323 {
324 vdev_t *pvd = vd->vdev_parent;
325
326 /*
327 * If our parent is NULL (inactive spare or cache) or is the root,
328 * just return our own asize.
329 */
330 if (pvd == NULL)
331 return (vd->vdev_asize);
332
333 /*
334 * The top-level vdev just returns the allocatable size rounded
335 * to the nearest metaslab.
336 */
337 if (vd == vd->vdev_top)
338 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
339
340 return (pvd->vdev_ops->vdev_op_min_asize(pvd));
341 }
342
343 void
344 vdev_set_min_asize(vdev_t *vd)
345 {
346 vd->vdev_min_asize = vdev_get_min_asize(vd);
347
348 for (int c = 0; c < vd->vdev_children; c++)
349 vdev_set_min_asize(vd->vdev_child[c]);
350 }
351
352 /*
353 * Get the minimal allocation size for the top-level vdev.
354 */
355 uint64_t
356 vdev_get_min_alloc(vdev_t *vd)
357 {
358 uint64_t min_alloc = 1ULL << vd->vdev_ashift;
359
360 if (vd->vdev_ops->vdev_op_min_alloc != NULL)
361 min_alloc = vd->vdev_ops->vdev_op_min_alloc(vd);
362
363 return (min_alloc);
364 }
365
366 /*
367 * Get the parity level for a top-level vdev.
368 */
369 uint64_t
370 vdev_get_nparity(vdev_t *vd)
371 {
372 uint64_t nparity = 0;
373
374 if (vd->vdev_ops->vdev_op_nparity != NULL)
375 nparity = vd->vdev_ops->vdev_op_nparity(vd);
376
377 return (nparity);
378 }
379
380 /*
381 * Get the number of data disks for a top-level vdev.
382 */
383 uint64_t
384 vdev_get_ndisks(vdev_t *vd)
385 {
386 uint64_t ndisks = 1;
387
388 if (vd->vdev_ops->vdev_op_ndisks != NULL)
389 ndisks = vd->vdev_ops->vdev_op_ndisks(vd);
390
391 return (ndisks);
392 }
393
394 vdev_t *
395 vdev_lookup_top(spa_t *spa, uint64_t vdev)
396 {
397 vdev_t *rvd = spa->spa_root_vdev;
398
399 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
400
401 if (vdev < rvd->vdev_children) {
402 ASSERT(rvd->vdev_child[vdev] != NULL);
403 return (rvd->vdev_child[vdev]);
404 }
405
406 return (NULL);
407 }
408
409 vdev_t *
410 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
411 {
412 vdev_t *mvd;
413
414 if (vd->vdev_guid == guid)
415 return (vd);
416
417 for (int c = 0; c < vd->vdev_children; c++)
418 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
419 NULL)
420 return (mvd);
421
422 return (NULL);
423 }
424
425 static int
426 vdev_count_leaves_impl(vdev_t *vd)
427 {
428 int n = 0;
429
430 if (vd->vdev_ops->vdev_op_leaf)
431 return (1);
432
433 for (int c = 0; c < vd->vdev_children; c++)
434 n += vdev_count_leaves_impl(vd->vdev_child[c]);
435
436 return (n);
437 }
438
439 int
440 vdev_count_leaves(spa_t *spa)
441 {
442 int rc;
443
444 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
445 rc = vdev_count_leaves_impl(spa->spa_root_vdev);
446 spa_config_exit(spa, SCL_VDEV, FTAG);
447
448 return (rc);
449 }
450
451 void
452 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
453 {
454 size_t oldsize, newsize;
455 uint64_t id = cvd->vdev_id;
456 vdev_t **newchild;
457
458 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
459 ASSERT(cvd->vdev_parent == NULL);
460
461 cvd->vdev_parent = pvd;
462
463 if (pvd == NULL)
464 return;
465
466 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
467
468 oldsize = pvd->vdev_children * sizeof (vdev_t *);
469 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
470 newsize = pvd->vdev_children * sizeof (vdev_t *);
471
472 newchild = kmem_alloc(newsize, KM_SLEEP);
473 if (pvd->vdev_child != NULL) {
474 bcopy(pvd->vdev_child, newchild, oldsize);
475 kmem_free(pvd->vdev_child, oldsize);
476 }
477
478 pvd->vdev_child = newchild;
479 pvd->vdev_child[id] = cvd;
480
481 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
482 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
483
484 /*
485 * Walk up all ancestors to update guid sum.
486 */
487 for (; pvd != NULL; pvd = pvd->vdev_parent)
488 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
489
490 if (cvd->vdev_ops->vdev_op_leaf) {
491 list_insert_head(&cvd->vdev_spa->spa_leaf_list, cvd);
492 cvd->vdev_spa->spa_leaf_list_gen++;
493 }
494 }
495
496 void
497 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
498 {
499 int c;
500 uint_t id = cvd->vdev_id;
501
502 ASSERT(cvd->vdev_parent == pvd);
503
504 if (pvd == NULL)
505 return;
506
507 ASSERT(id < pvd->vdev_children);
508 ASSERT(pvd->vdev_child[id] == cvd);
509
510 pvd->vdev_child[id] = NULL;
511 cvd->vdev_parent = NULL;
512
513 for (c = 0; c < pvd->vdev_children; c++)
514 if (pvd->vdev_child[c])
515 break;
516
517 if (c == pvd->vdev_children) {
518 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
519 pvd->vdev_child = NULL;
520 pvd->vdev_children = 0;
521 }
522
523 if (cvd->vdev_ops->vdev_op_leaf) {
524 spa_t *spa = cvd->vdev_spa;
525 list_remove(&spa->spa_leaf_list, cvd);
526 spa->spa_leaf_list_gen++;
527 }
528
529 /*
530 * Walk up all ancestors to update guid sum.
531 */
532 for (; pvd != NULL; pvd = pvd->vdev_parent)
533 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
534 }
535
536 /*
537 * Remove any holes in the child array.
538 */
539 void
540 vdev_compact_children(vdev_t *pvd)
541 {
542 vdev_t **newchild, *cvd;
543 int oldc = pvd->vdev_children;
544 int newc;
545
546 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
547
548 if (oldc == 0)
549 return;
550
551 for (int c = newc = 0; c < oldc; c++)
552 if (pvd->vdev_child[c])
553 newc++;
554
555 if (newc > 0) {
556 newchild = kmem_zalloc(newc * sizeof (vdev_t *), KM_SLEEP);
557
558 for (int c = newc = 0; c < oldc; c++) {
559 if ((cvd = pvd->vdev_child[c]) != NULL) {
560 newchild[newc] = cvd;
561 cvd->vdev_id = newc++;
562 }
563 }
564 } else {
565 newchild = NULL;
566 }
567
568 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
569 pvd->vdev_child = newchild;
570 pvd->vdev_children = newc;
571 }
572
573 /*
574 * Allocate and minimally initialize a vdev_t.
575 */
576 vdev_t *
577 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
578 {
579 vdev_t *vd;
580 vdev_indirect_config_t *vic;
581
582 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
583 vic = &vd->vdev_indirect_config;
584
585 if (spa->spa_root_vdev == NULL) {
586 ASSERT(ops == &vdev_root_ops);
587 spa->spa_root_vdev = vd;
588 spa->spa_load_guid = spa_generate_guid(NULL);
589 }
590
591 if (guid == 0 && ops != &vdev_hole_ops) {
592 if (spa->spa_root_vdev == vd) {
593 /*
594 * The root vdev's guid will also be the pool guid,
595 * which must be unique among all pools.
596 */
597 guid = spa_generate_guid(NULL);
598 } else {
599 /*
600 * Any other vdev's guid must be unique within the pool.
601 */
602 guid = spa_generate_guid(spa);
603 }
604 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
605 }
606
607 vd->vdev_spa = spa;
608 vd->vdev_id = id;
609 vd->vdev_guid = guid;
610 vd->vdev_guid_sum = guid;
611 vd->vdev_ops = ops;
612 vd->vdev_state = VDEV_STATE_CLOSED;
613 vd->vdev_ishole = (ops == &vdev_hole_ops);
614 vic->vic_prev_indirect_vdev = UINT64_MAX;
615
616 rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
617 mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
618 vd->vdev_obsolete_segments = range_tree_create(NULL, RANGE_SEG64, NULL,
619 0, 0);
620
621 /*
622 * Initialize rate limit structs for events. We rate limit ZIO delay
623 * and checksum events so that we don't overwhelm ZED with thousands
624 * of events when a disk is acting up.
625 */
626 zfs_ratelimit_init(&vd->vdev_delay_rl, &zfs_slow_io_events_per_second,
627 1);
628 zfs_ratelimit_init(&vd->vdev_checksum_rl,
629 &zfs_checksum_events_per_second, 1);
630
631 list_link_init(&vd->vdev_config_dirty_node);
632 list_link_init(&vd->vdev_state_dirty_node);
633 list_link_init(&vd->vdev_initialize_node);
634 list_link_init(&vd->vdev_leaf_node);
635 list_link_init(&vd->vdev_trim_node);
636
637 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_NOLOCKDEP, NULL);
638 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
639 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
640 mutex_init(&vd->vdev_scan_io_queue_lock, NULL, MUTEX_DEFAULT, NULL);
641
642 mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL);
643 mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL);
644 cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL);
645 cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL);
646
647 mutex_init(&vd->vdev_trim_lock, NULL, MUTEX_DEFAULT, NULL);
648 mutex_init(&vd->vdev_autotrim_lock, NULL, MUTEX_DEFAULT, NULL);
649 mutex_init(&vd->vdev_trim_io_lock, NULL, MUTEX_DEFAULT, NULL);
650 cv_init(&vd->vdev_trim_cv, NULL, CV_DEFAULT, NULL);
651 cv_init(&vd->vdev_autotrim_cv, NULL, CV_DEFAULT, NULL);
652 cv_init(&vd->vdev_trim_io_cv, NULL, CV_DEFAULT, NULL);
653
654 mutex_init(&vd->vdev_rebuild_lock, NULL, MUTEX_DEFAULT, NULL);
655 cv_init(&vd->vdev_rebuild_cv, NULL, CV_DEFAULT, NULL);
656
657 for (int t = 0; t < DTL_TYPES; t++) {
658 vd->vdev_dtl[t] = range_tree_create(NULL, RANGE_SEG64, NULL, 0,
659 0);
660 }
661
662 txg_list_create(&vd->vdev_ms_list, spa,
663 offsetof(struct metaslab, ms_txg_node));
664 txg_list_create(&vd->vdev_dtl_list, spa,
665 offsetof(struct vdev, vdev_dtl_node));
666 vd->vdev_stat.vs_timestamp = gethrtime();
667 vdev_queue_init(vd);
668 vdev_cache_init(vd);
669
670 return (vd);
671 }
672
673 /*
674 * Allocate a new vdev. The 'alloctype' is used to control whether we are
675 * creating a new vdev or loading an existing one - the behavior is slightly
676 * different for each case.
677 */
678 int
679 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
680 int alloctype)
681 {
682 vdev_ops_t *ops;
683 char *type;
684 uint64_t guid = 0, islog;
685 vdev_t *vd;
686 vdev_indirect_config_t *vic;
687 char *tmp = NULL;
688 int rc;
689 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
690 boolean_t top_level = (parent && !parent->vdev_parent);
691
692 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
693
694 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
695 return (SET_ERROR(EINVAL));
696
697 if ((ops = vdev_getops(type)) == NULL)
698 return (SET_ERROR(EINVAL));
699
700 /*
701 * If this is a load, get the vdev guid from the nvlist.
702 * Otherwise, vdev_alloc_common() will generate one for us.
703 */
704 if (alloctype == VDEV_ALLOC_LOAD) {
705 uint64_t label_id;
706
707 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
708 label_id != id)
709 return (SET_ERROR(EINVAL));
710
711 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
712 return (SET_ERROR(EINVAL));
713 } else if (alloctype == VDEV_ALLOC_SPARE) {
714 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
715 return (SET_ERROR(EINVAL));
716 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
717 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
718 return (SET_ERROR(EINVAL));
719 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
720 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
721 return (SET_ERROR(EINVAL));
722 }
723
724 /*
725 * The first allocated vdev must be of type 'root'.
726 */
727 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
728 return (SET_ERROR(EINVAL));
729
730 /*
731 * Determine whether we're a log vdev.
732 */
733 islog = 0;
734 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
735 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
736 return (SET_ERROR(ENOTSUP));
737
738 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
739 return (SET_ERROR(ENOTSUP));
740
741 if (top_level && alloctype == VDEV_ALLOC_ADD) {
742 char *bias;
743
744 /*
745 * If creating a top-level vdev, check for allocation
746 * classes input.
747 */
748 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
749 &bias) == 0) {
750 alloc_bias = vdev_derive_alloc_bias(bias);
751
752 /* spa_vdev_add() expects feature to be enabled */
753 if (spa->spa_load_state != SPA_LOAD_CREATE &&
754 !spa_feature_is_enabled(spa,
755 SPA_FEATURE_ALLOCATION_CLASSES)) {
756 return (SET_ERROR(ENOTSUP));
757 }
758 }
759
760 /* spa_vdev_add() expects feature to be enabled */
761 if (ops == &vdev_draid_ops &&
762 spa->spa_load_state != SPA_LOAD_CREATE &&
763 !spa_feature_is_enabled(spa, SPA_FEATURE_DRAID)) {
764 return (SET_ERROR(ENOTSUP));
765 }
766 }
767
768 /*
769 * Initialize the vdev specific data. This is done before calling
770 * vdev_alloc_common() since it may fail and this simplifies the
771 * error reporting and cleanup code paths.
772 */
773 void *tsd = NULL;
774 if (ops->vdev_op_init != NULL) {
775 rc = ops->vdev_op_init(spa, nv, &tsd);
776 if (rc != 0) {
777 return (rc);
778 }
779 }
780
781 vd = vdev_alloc_common(spa, id, guid, ops);
782 vd->vdev_tsd = tsd;
783 vd->vdev_islog = islog;
784
785 if (top_level && alloc_bias != VDEV_BIAS_NONE)
786 vd->vdev_alloc_bias = alloc_bias;
787
788 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
789 vd->vdev_path = spa_strdup(vd->vdev_path);
790
791 /*
792 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
793 * fault on a vdev and want it to persist across imports (like with
794 * zpool offline -f).
795 */
796 rc = nvlist_lookup_string(nv, ZPOOL_CONFIG_AUX_STATE, &tmp);
797 if (rc == 0 && tmp != NULL && strcmp(tmp, "external") == 0) {
798 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL;
799 vd->vdev_faulted = 1;
800 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
801 }
802
803 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
804 vd->vdev_devid = spa_strdup(vd->vdev_devid);
805 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
806 &vd->vdev_physpath) == 0)
807 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
808
809 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
810 &vd->vdev_enc_sysfs_path) == 0)
811 vd->vdev_enc_sysfs_path = spa_strdup(vd->vdev_enc_sysfs_path);
812
813 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
814 vd->vdev_fru = spa_strdup(vd->vdev_fru);
815
816 /*
817 * Set the whole_disk property. If it's not specified, leave the value
818 * as -1.
819 */
820 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
821 &vd->vdev_wholedisk) != 0)
822 vd->vdev_wholedisk = -1ULL;
823
824 vic = &vd->vdev_indirect_config;
825
826 ASSERT0(vic->vic_mapping_object);
827 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
828 &vic->vic_mapping_object);
829 ASSERT0(vic->vic_births_object);
830 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
831 &vic->vic_births_object);
832 ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
833 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
834 &vic->vic_prev_indirect_vdev);
835
836 /*
837 * Look for the 'not present' flag. This will only be set if the device
838 * was not present at the time of import.
839 */
840 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
841 &vd->vdev_not_present);
842
843 /*
844 * Get the alignment requirement.
845 */
846 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
847
848 /*
849 * Retrieve the vdev creation time.
850 */
851 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
852 &vd->vdev_crtxg);
853
854 /*
855 * If we're a top-level vdev, try to load the allocation parameters.
856 */
857 if (top_level &&
858 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
859 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
860 &vd->vdev_ms_array);
861 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
862 &vd->vdev_ms_shift);
863 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
864 &vd->vdev_asize);
865 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
866 &vd->vdev_removing);
867 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
868 &vd->vdev_top_zap);
869 } else {
870 ASSERT0(vd->vdev_top_zap);
871 }
872
873 if (top_level && alloctype != VDEV_ALLOC_ATTACH) {
874 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
875 alloctype == VDEV_ALLOC_ADD ||
876 alloctype == VDEV_ALLOC_SPLIT ||
877 alloctype == VDEV_ALLOC_ROOTPOOL);
878 /* Note: metaslab_group_create() is now deferred */
879 }
880
881 if (vd->vdev_ops->vdev_op_leaf &&
882 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
883 (void) nvlist_lookup_uint64(nv,
884 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
885 } else {
886 ASSERT0(vd->vdev_leaf_zap);
887 }
888
889 /*
890 * If we're a leaf vdev, try to load the DTL object and other state.
891 */
892
893 if (vd->vdev_ops->vdev_op_leaf &&
894 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
895 alloctype == VDEV_ALLOC_ROOTPOOL)) {
896 if (alloctype == VDEV_ALLOC_LOAD) {
897 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
898 &vd->vdev_dtl_object);
899 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
900 &vd->vdev_unspare);
901 }
902
903 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
904 uint64_t spare = 0;
905
906 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
907 &spare) == 0 && spare)
908 spa_spare_add(vd);
909 }
910
911 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
912 &vd->vdev_offline);
913
914 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
915 &vd->vdev_resilver_txg);
916
917 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REBUILD_TXG,
918 &vd->vdev_rebuild_txg);
919
920 if (nvlist_exists(nv, ZPOOL_CONFIG_RESILVER_DEFER))
921 vdev_defer_resilver(vd);
922
923 /*
924 * In general, when importing a pool we want to ignore the
925 * persistent fault state, as the diagnosis made on another
926 * system may not be valid in the current context. The only
927 * exception is if we forced a vdev to a persistently faulted
928 * state with 'zpool offline -f'. The persistent fault will
929 * remain across imports until cleared.
930 *
931 * Local vdevs will remain in the faulted state.
932 */
933 if (spa_load_state(spa) == SPA_LOAD_OPEN ||
934 spa_load_state(spa) == SPA_LOAD_IMPORT) {
935 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
936 &vd->vdev_faulted);
937 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
938 &vd->vdev_degraded);
939 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
940 &vd->vdev_removed);
941
942 if (vd->vdev_faulted || vd->vdev_degraded) {
943 char *aux;
944
945 vd->vdev_label_aux =
946 VDEV_AUX_ERR_EXCEEDED;
947 if (nvlist_lookup_string(nv,
948 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
949 strcmp(aux, "external") == 0)
950 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
951 else
952 vd->vdev_faulted = 0ULL;
953 }
954 }
955 }
956
957 /*
958 * Add ourselves to the parent's list of children.
959 */
960 vdev_add_child(parent, vd);
961
962 *vdp = vd;
963
964 return (0);
965 }
966
967 void
968 vdev_free(vdev_t *vd)
969 {
970 spa_t *spa = vd->vdev_spa;
971
972 ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
973 ASSERT3P(vd->vdev_trim_thread, ==, NULL);
974 ASSERT3P(vd->vdev_autotrim_thread, ==, NULL);
975 ASSERT3P(vd->vdev_rebuild_thread, ==, NULL);
976
977 /*
978 * Scan queues are normally destroyed at the end of a scan. If the
979 * queue exists here, that implies the vdev is being removed while
980 * the scan is still running.
981 */
982 if (vd->vdev_scan_io_queue != NULL) {
983 mutex_enter(&vd->vdev_scan_io_queue_lock);
984 dsl_scan_io_queue_destroy(vd->vdev_scan_io_queue);
985 vd->vdev_scan_io_queue = NULL;
986 mutex_exit(&vd->vdev_scan_io_queue_lock);
987 }
988
989 /*
990 * vdev_free() implies closing the vdev first. This is simpler than
991 * trying to ensure complicated semantics for all callers.
992 */
993 vdev_close(vd);
994
995 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
996 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
997
998 /*
999 * Free all children.
1000 */
1001 for (int c = 0; c < vd->vdev_children; c++)
1002 vdev_free(vd->vdev_child[c]);
1003
1004 ASSERT(vd->vdev_child == NULL);
1005 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
1006
1007 if (vd->vdev_ops->vdev_op_fini != NULL)
1008 vd->vdev_ops->vdev_op_fini(vd);
1009
1010 /*
1011 * Discard allocation state.
1012 */
1013 if (vd->vdev_mg != NULL) {
1014 vdev_metaslab_fini(vd);
1015 metaslab_group_destroy(vd->vdev_mg);
1016 vd->vdev_mg = NULL;
1017 }
1018 if (vd->vdev_log_mg != NULL) {
1019 ASSERT0(vd->vdev_ms_count);
1020 metaslab_group_destroy(vd->vdev_log_mg);
1021 vd->vdev_log_mg = NULL;
1022 }
1023
1024 ASSERT0(vd->vdev_stat.vs_space);
1025 ASSERT0(vd->vdev_stat.vs_dspace);
1026 ASSERT0(vd->vdev_stat.vs_alloc);
1027
1028 /*
1029 * Remove this vdev from its parent's child list.
1030 */
1031 vdev_remove_child(vd->vdev_parent, vd);
1032
1033 ASSERT(vd->vdev_parent == NULL);
1034 ASSERT(!list_link_active(&vd->vdev_leaf_node));
1035
1036 /*
1037 * Clean up vdev structure.
1038 */
1039 vdev_queue_fini(vd);
1040 vdev_cache_fini(vd);
1041
1042 if (vd->vdev_path)
1043 spa_strfree(vd->vdev_path);
1044 if (vd->vdev_devid)
1045 spa_strfree(vd->vdev_devid);
1046 if (vd->vdev_physpath)
1047 spa_strfree(vd->vdev_physpath);
1048
1049 if (vd->vdev_enc_sysfs_path)
1050 spa_strfree(vd->vdev_enc_sysfs_path);
1051
1052 if (vd->vdev_fru)
1053 spa_strfree(vd->vdev_fru);
1054
1055 if (vd->vdev_isspare)
1056 spa_spare_remove(vd);
1057 if (vd->vdev_isl2cache)
1058 spa_l2cache_remove(vd);
1059
1060 txg_list_destroy(&vd->vdev_ms_list);
1061 txg_list_destroy(&vd->vdev_dtl_list);
1062
1063 mutex_enter(&vd->vdev_dtl_lock);
1064 space_map_close(vd->vdev_dtl_sm);
1065 for (int t = 0; t < DTL_TYPES; t++) {
1066 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
1067 range_tree_destroy(vd->vdev_dtl[t]);
1068 }
1069 mutex_exit(&vd->vdev_dtl_lock);
1070
1071 EQUIV(vd->vdev_indirect_births != NULL,
1072 vd->vdev_indirect_mapping != NULL);
1073 if (vd->vdev_indirect_births != NULL) {
1074 vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
1075 vdev_indirect_births_close(vd->vdev_indirect_births);
1076 }
1077
1078 if (vd->vdev_obsolete_sm != NULL) {
1079 ASSERT(vd->vdev_removing ||
1080 vd->vdev_ops == &vdev_indirect_ops);
1081 space_map_close(vd->vdev_obsolete_sm);
1082 vd->vdev_obsolete_sm = NULL;
1083 }
1084 range_tree_destroy(vd->vdev_obsolete_segments);
1085 rw_destroy(&vd->vdev_indirect_rwlock);
1086 mutex_destroy(&vd->vdev_obsolete_lock);
1087
1088 mutex_destroy(&vd->vdev_dtl_lock);
1089 mutex_destroy(&vd->vdev_stat_lock);
1090 mutex_destroy(&vd->vdev_probe_lock);
1091 mutex_destroy(&vd->vdev_scan_io_queue_lock);
1092
1093 mutex_destroy(&vd->vdev_initialize_lock);
1094 mutex_destroy(&vd->vdev_initialize_io_lock);
1095 cv_destroy(&vd->vdev_initialize_io_cv);
1096 cv_destroy(&vd->vdev_initialize_cv);
1097
1098 mutex_destroy(&vd->vdev_trim_lock);
1099 mutex_destroy(&vd->vdev_autotrim_lock);
1100 mutex_destroy(&vd->vdev_trim_io_lock);
1101 cv_destroy(&vd->vdev_trim_cv);
1102 cv_destroy(&vd->vdev_autotrim_cv);
1103 cv_destroy(&vd->vdev_trim_io_cv);
1104
1105 mutex_destroy(&vd->vdev_rebuild_lock);
1106 cv_destroy(&vd->vdev_rebuild_cv);
1107
1108 zfs_ratelimit_fini(&vd->vdev_delay_rl);
1109 zfs_ratelimit_fini(&vd->vdev_checksum_rl);
1110
1111 if (vd == spa->spa_root_vdev)
1112 spa->spa_root_vdev = NULL;
1113
1114 kmem_free(vd, sizeof (vdev_t));
1115 }
1116
1117 /*
1118 * Transfer top-level vdev state from svd to tvd.
1119 */
1120 static void
1121 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
1122 {
1123 spa_t *spa = svd->vdev_spa;
1124 metaslab_t *msp;
1125 vdev_t *vd;
1126 int t;
1127
1128 ASSERT(tvd == tvd->vdev_top);
1129
1130 tvd->vdev_pending_fastwrite = svd->vdev_pending_fastwrite;
1131 tvd->vdev_ms_array = svd->vdev_ms_array;
1132 tvd->vdev_ms_shift = svd->vdev_ms_shift;
1133 tvd->vdev_ms_count = svd->vdev_ms_count;
1134 tvd->vdev_top_zap = svd->vdev_top_zap;
1135
1136 svd->vdev_ms_array = 0;
1137 svd->vdev_ms_shift = 0;
1138 svd->vdev_ms_count = 0;
1139 svd->vdev_top_zap = 0;
1140
1141 if (tvd->vdev_mg)
1142 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
1143 if (tvd->vdev_log_mg)
1144 ASSERT3P(tvd->vdev_log_mg, ==, svd->vdev_log_mg);
1145 tvd->vdev_mg = svd->vdev_mg;
1146 tvd->vdev_log_mg = svd->vdev_log_mg;
1147 tvd->vdev_ms = svd->vdev_ms;
1148
1149 svd->vdev_mg = NULL;
1150 svd->vdev_log_mg = NULL;
1151 svd->vdev_ms = NULL;
1152
1153 if (tvd->vdev_mg != NULL)
1154 tvd->vdev_mg->mg_vd = tvd;
1155 if (tvd->vdev_log_mg != NULL)
1156 tvd->vdev_log_mg->mg_vd = tvd;
1157
1158 tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm;
1159 svd->vdev_checkpoint_sm = NULL;
1160
1161 tvd->vdev_alloc_bias = svd->vdev_alloc_bias;
1162 svd->vdev_alloc_bias = VDEV_BIAS_NONE;
1163
1164 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
1165 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
1166 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
1167
1168 svd->vdev_stat.vs_alloc = 0;
1169 svd->vdev_stat.vs_space = 0;
1170 svd->vdev_stat.vs_dspace = 0;
1171
1172 /*
1173 * State which may be set on a top-level vdev that's in the
1174 * process of being removed.
1175 */
1176 ASSERT0(tvd->vdev_indirect_config.vic_births_object);
1177 ASSERT0(tvd->vdev_indirect_config.vic_mapping_object);
1178 ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL);
1179 ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL);
1180 ASSERT3P(tvd->vdev_indirect_births, ==, NULL);
1181 ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL);
1182 ASSERT0(tvd->vdev_removing);
1183 ASSERT0(tvd->vdev_rebuilding);
1184 tvd->vdev_removing = svd->vdev_removing;
1185 tvd->vdev_rebuilding = svd->vdev_rebuilding;
1186 tvd->vdev_rebuild_config = svd->vdev_rebuild_config;
1187 tvd->vdev_indirect_config = svd->vdev_indirect_config;
1188 tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping;
1189 tvd->vdev_indirect_births = svd->vdev_indirect_births;
1190 range_tree_swap(&svd->vdev_obsolete_segments,
1191 &tvd->vdev_obsolete_segments);
1192 tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm;
1193 svd->vdev_indirect_config.vic_mapping_object = 0;
1194 svd->vdev_indirect_config.vic_births_object = 0;
1195 svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL;
1196 svd->vdev_indirect_mapping = NULL;
1197 svd->vdev_indirect_births = NULL;
1198 svd->vdev_obsolete_sm = NULL;
1199 svd->vdev_removing = 0;
1200 svd->vdev_rebuilding = 0;
1201
1202 for (t = 0; t < TXG_SIZE; t++) {
1203 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
1204 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
1205 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
1206 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
1207 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
1208 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
1209 }
1210
1211 if (list_link_active(&svd->vdev_config_dirty_node)) {
1212 vdev_config_clean(svd);
1213 vdev_config_dirty(tvd);
1214 }
1215
1216 if (list_link_active(&svd->vdev_state_dirty_node)) {
1217 vdev_state_clean(svd);
1218 vdev_state_dirty(tvd);
1219 }
1220
1221 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
1222 svd->vdev_deflate_ratio = 0;
1223
1224 tvd->vdev_islog = svd->vdev_islog;
1225 svd->vdev_islog = 0;
1226
1227 dsl_scan_io_queue_vdev_xfer(svd, tvd);
1228 }
1229
1230 static void
1231 vdev_top_update(vdev_t *tvd, vdev_t *vd)
1232 {
1233 if (vd == NULL)
1234 return;
1235
1236 vd->vdev_top = tvd;
1237
1238 for (int c = 0; c < vd->vdev_children; c++)
1239 vdev_top_update(tvd, vd->vdev_child[c]);
1240 }
1241
1242 /*
1243 * Add a mirror/replacing vdev above an existing vdev. There is no need to
1244 * call .vdev_op_init() since mirror/replacing vdevs do not have private state.
1245 */
1246 vdev_t *
1247 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
1248 {
1249 spa_t *spa = cvd->vdev_spa;
1250 vdev_t *pvd = cvd->vdev_parent;
1251 vdev_t *mvd;
1252
1253 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1254
1255 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
1256
1257 mvd->vdev_asize = cvd->vdev_asize;
1258 mvd->vdev_min_asize = cvd->vdev_min_asize;
1259 mvd->vdev_max_asize = cvd->vdev_max_asize;
1260 mvd->vdev_psize = cvd->vdev_psize;
1261 mvd->vdev_ashift = cvd->vdev_ashift;
1262 mvd->vdev_logical_ashift = cvd->vdev_logical_ashift;
1263 mvd->vdev_physical_ashift = cvd->vdev_physical_ashift;
1264 mvd->vdev_state = cvd->vdev_state;
1265 mvd->vdev_crtxg = cvd->vdev_crtxg;
1266
1267 vdev_remove_child(pvd, cvd);
1268 vdev_add_child(pvd, mvd);
1269 cvd->vdev_id = mvd->vdev_children;
1270 vdev_add_child(mvd, cvd);
1271 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1272
1273 if (mvd == mvd->vdev_top)
1274 vdev_top_transfer(cvd, mvd);
1275
1276 return (mvd);
1277 }
1278
1279 /*
1280 * Remove a 1-way mirror/replacing vdev from the tree.
1281 */
1282 void
1283 vdev_remove_parent(vdev_t *cvd)
1284 {
1285 vdev_t *mvd = cvd->vdev_parent;
1286 vdev_t *pvd = mvd->vdev_parent;
1287
1288 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1289
1290 ASSERT(mvd->vdev_children == 1);
1291 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
1292 mvd->vdev_ops == &vdev_replacing_ops ||
1293 mvd->vdev_ops == &vdev_spare_ops);
1294 cvd->vdev_ashift = mvd->vdev_ashift;
1295 cvd->vdev_logical_ashift = mvd->vdev_logical_ashift;
1296 cvd->vdev_physical_ashift = mvd->vdev_physical_ashift;
1297 vdev_remove_child(mvd, cvd);
1298 vdev_remove_child(pvd, mvd);
1299
1300 /*
1301 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1302 * Otherwise, we could have detached an offline device, and when we
1303 * go to import the pool we'll think we have two top-level vdevs,
1304 * instead of a different version of the same top-level vdev.
1305 */
1306 if (mvd->vdev_top == mvd) {
1307 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
1308 cvd->vdev_orig_guid = cvd->vdev_guid;
1309 cvd->vdev_guid += guid_delta;
1310 cvd->vdev_guid_sum += guid_delta;
1311
1312 /*
1313 * If pool not set for autoexpand, we need to also preserve
1314 * mvd's asize to prevent automatic expansion of cvd.
1315 * Otherwise if we are adjusting the mirror by attaching and
1316 * detaching children of non-uniform sizes, the mirror could
1317 * autoexpand, unexpectedly requiring larger devices to
1318 * re-establish the mirror.
1319 */
1320 if (!cvd->vdev_spa->spa_autoexpand)
1321 cvd->vdev_asize = mvd->vdev_asize;
1322 }
1323 cvd->vdev_id = mvd->vdev_id;
1324 vdev_add_child(pvd, cvd);
1325 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1326
1327 if (cvd == cvd->vdev_top)
1328 vdev_top_transfer(mvd, cvd);
1329
1330 ASSERT(mvd->vdev_children == 0);
1331 vdev_free(mvd);
1332 }
1333
1334 void
1335 vdev_metaslab_group_create(vdev_t *vd)
1336 {
1337 spa_t *spa = vd->vdev_spa;
1338
1339 /*
1340 * metaslab_group_create was delayed until allocation bias was available
1341 */
1342 if (vd->vdev_mg == NULL) {
1343 metaslab_class_t *mc;
1344
1345 if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE)
1346 vd->vdev_alloc_bias = VDEV_BIAS_LOG;
1347
1348 ASSERT3U(vd->vdev_islog, ==,
1349 (vd->vdev_alloc_bias == VDEV_BIAS_LOG));
1350
1351 switch (vd->vdev_alloc_bias) {
1352 case VDEV_BIAS_LOG:
1353 mc = spa_log_class(spa);
1354 break;
1355 case VDEV_BIAS_SPECIAL:
1356 mc = spa_special_class(spa);
1357 break;
1358 case VDEV_BIAS_DEDUP:
1359 mc = spa_dedup_class(spa);
1360 break;
1361 default:
1362 mc = spa_normal_class(spa);
1363 }
1364
1365 vd->vdev_mg = metaslab_group_create(mc, vd,
1366 spa->spa_alloc_count);
1367
1368 if (!vd->vdev_islog) {
1369 vd->vdev_log_mg = metaslab_group_create(
1370 spa_embedded_log_class(spa), vd, 1);
1371 }
1372
1373 /*
1374 * The spa ashift min/max only apply for the normal metaslab
1375 * class. Class destination is late binding so ashift boundry
1376 * setting had to wait until now.
1377 */
1378 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1379 mc == spa_normal_class(spa) && vd->vdev_aux == NULL) {
1380 if (vd->vdev_ashift > spa->spa_max_ashift)
1381 spa->spa_max_ashift = vd->vdev_ashift;
1382 if (vd->vdev_ashift < spa->spa_min_ashift)
1383 spa->spa_min_ashift = vd->vdev_ashift;
1384
1385 uint64_t min_alloc = vdev_get_min_alloc(vd);
1386 if (min_alloc < spa->spa_min_alloc)
1387 spa->spa_min_alloc = min_alloc;
1388 }
1389 }
1390 }
1391
1392 int
1393 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
1394 {
1395 spa_t *spa = vd->vdev_spa;
1396 uint64_t oldc = vd->vdev_ms_count;
1397 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
1398 metaslab_t **mspp;
1399 int error;
1400 boolean_t expanding = (oldc != 0);
1401
1402 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1403
1404 /*
1405 * This vdev is not being allocated from yet or is a hole.
1406 */
1407 if (vd->vdev_ms_shift == 0)
1408 return (0);
1409
1410 ASSERT(!vd->vdev_ishole);
1411
1412 ASSERT(oldc <= newc);
1413
1414 mspp = vmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1415
1416 if (expanding) {
1417 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
1418 vmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1419 }
1420
1421 vd->vdev_ms = mspp;
1422 vd->vdev_ms_count = newc;
1423
1424 for (uint64_t m = oldc; m < newc; m++) {
1425 uint64_t object = 0;
1426 /*
1427 * vdev_ms_array may be 0 if we are creating the "fake"
1428 * metaslabs for an indirect vdev for zdb's leak detection.
1429 * See zdb_leak_init().
1430 */
1431 if (txg == 0 && vd->vdev_ms_array != 0) {
1432 error = dmu_read(spa->spa_meta_objset,
1433 vd->vdev_ms_array,
1434 m * sizeof (uint64_t), sizeof (uint64_t), &object,
1435 DMU_READ_PREFETCH);
1436 if (error != 0) {
1437 vdev_dbgmsg(vd, "unable to read the metaslab "
1438 "array [error=%d]", error);
1439 return (error);
1440 }
1441 }
1442
1443 error = metaslab_init(vd->vdev_mg, m, object, txg,
1444 &(vd->vdev_ms[m]));
1445 if (error != 0) {
1446 vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
1447 error);
1448 return (error);
1449 }
1450 }
1451
1452 /*
1453 * Find the emptiest metaslab on the vdev and mark it for use for
1454 * embedded slog by moving it from the regular to the log metaslab
1455 * group.
1456 */
1457 if (vd->vdev_mg->mg_class == spa_normal_class(spa) &&
1458 vd->vdev_ms_count > zfs_embedded_slog_min_ms &&
1459 avl_is_empty(&vd->vdev_log_mg->mg_metaslab_tree)) {
1460 uint64_t slog_msid = 0;
1461 uint64_t smallest = UINT64_MAX;
1462
1463 /*
1464 * Note, we only search the new metaslabs, because the old
1465 * (pre-existing) ones may be active (e.g. have non-empty
1466 * range_tree's), and we don't move them to the new
1467 * metaslab_t.
1468 */
1469 for (uint64_t m = oldc; m < newc; m++) {
1470 uint64_t alloc =
1471 space_map_allocated(vd->vdev_ms[m]->ms_sm);
1472 if (alloc < smallest) {
1473 slog_msid = m;
1474 smallest = alloc;
1475 }
1476 }
1477 metaslab_t *slog_ms = vd->vdev_ms[slog_msid];
1478 /*
1479 * The metaslab was marked as dirty at the end of
1480 * metaslab_init(). Remove it from the dirty list so that we
1481 * can uninitialize and reinitialize it to the new class.
1482 */
1483 if (txg != 0) {
1484 (void) txg_list_remove_this(&vd->vdev_ms_list,
1485 slog_ms, txg);
1486 }
1487 uint64_t sm_obj = space_map_object(slog_ms->ms_sm);
1488 metaslab_fini(slog_ms);
1489 VERIFY0(metaslab_init(vd->vdev_log_mg, slog_msid, sm_obj, txg,
1490 &vd->vdev_ms[slog_msid]));
1491 }
1492
1493 if (txg == 0)
1494 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1495
1496 /*
1497 * If the vdev is being removed we don't activate
1498 * the metaslabs since we want to ensure that no new
1499 * allocations are performed on this device.
1500 */
1501 if (!expanding && !vd->vdev_removing) {
1502 metaslab_group_activate(vd->vdev_mg);
1503 if (vd->vdev_log_mg != NULL)
1504 metaslab_group_activate(vd->vdev_log_mg);
1505 }
1506
1507 if (txg == 0)
1508 spa_config_exit(spa, SCL_ALLOC, FTAG);
1509
1510 /*
1511 * Regardless whether this vdev was just added or it is being
1512 * expanded, the metaslab count has changed. Recalculate the
1513 * block limit.
1514 */
1515 spa_log_sm_set_blocklimit(spa);
1516
1517 return (0);
1518 }
1519
1520 void
1521 vdev_metaslab_fini(vdev_t *vd)
1522 {
1523 if (vd->vdev_checkpoint_sm != NULL) {
1524 ASSERT(spa_feature_is_active(vd->vdev_spa,
1525 SPA_FEATURE_POOL_CHECKPOINT));
1526 space_map_close(vd->vdev_checkpoint_sm);
1527 /*
1528 * Even though we close the space map, we need to set its
1529 * pointer to NULL. The reason is that vdev_metaslab_fini()
1530 * may be called multiple times for certain operations
1531 * (i.e. when destroying a pool) so we need to ensure that
1532 * this clause never executes twice. This logic is similar
1533 * to the one used for the vdev_ms clause below.
1534 */
1535 vd->vdev_checkpoint_sm = NULL;
1536 }
1537
1538 if (vd->vdev_ms != NULL) {
1539 metaslab_group_t *mg = vd->vdev_mg;
1540
1541 metaslab_group_passivate(mg);
1542 if (vd->vdev_log_mg != NULL) {
1543 ASSERT(!vd->vdev_islog);
1544 metaslab_group_passivate(vd->vdev_log_mg);
1545 }
1546
1547 uint64_t count = vd->vdev_ms_count;
1548 for (uint64_t m = 0; m < count; m++) {
1549 metaslab_t *msp = vd->vdev_ms[m];
1550 if (msp != NULL)
1551 metaslab_fini(msp);
1552 }
1553 vmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1554 vd->vdev_ms = NULL;
1555 vd->vdev_ms_count = 0;
1556
1557 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
1558 ASSERT0(mg->mg_histogram[i]);
1559 if (vd->vdev_log_mg != NULL)
1560 ASSERT0(vd->vdev_log_mg->mg_histogram[i]);
1561 }
1562 }
1563 ASSERT0(vd->vdev_ms_count);
1564 ASSERT3U(vd->vdev_pending_fastwrite, ==, 0);
1565 }
1566
1567 typedef struct vdev_probe_stats {
1568 boolean_t vps_readable;
1569 boolean_t vps_writeable;
1570 int vps_flags;
1571 } vdev_probe_stats_t;
1572
1573 static void
1574 vdev_probe_done(zio_t *zio)
1575 {
1576 spa_t *spa = zio->io_spa;
1577 vdev_t *vd = zio->io_vd;
1578 vdev_probe_stats_t *vps = zio->io_private;
1579
1580 ASSERT(vd->vdev_probe_zio != NULL);
1581
1582 if (zio->io_type == ZIO_TYPE_READ) {
1583 if (zio->io_error == 0)
1584 vps->vps_readable = 1;
1585 if (zio->io_error == 0 && spa_writeable(spa)) {
1586 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1587 zio->io_offset, zio->io_size, zio->io_abd,
1588 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1589 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1590 } else {
1591 abd_free(zio->io_abd);
1592 }
1593 } else if (zio->io_type == ZIO_TYPE_WRITE) {
1594 if (zio->io_error == 0)
1595 vps->vps_writeable = 1;
1596 abd_free(zio->io_abd);
1597 } else if (zio->io_type == ZIO_TYPE_NULL) {
1598 zio_t *pio;
1599 zio_link_t *zl;
1600
1601 vd->vdev_cant_read |= !vps->vps_readable;
1602 vd->vdev_cant_write |= !vps->vps_writeable;
1603
1604 if (vdev_readable(vd) &&
1605 (vdev_writeable(vd) || !spa_writeable(spa))) {
1606 zio->io_error = 0;
1607 } else {
1608 ASSERT(zio->io_error != 0);
1609 vdev_dbgmsg(vd, "failed probe");
1610 (void) zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
1611 spa, vd, NULL, NULL, 0);
1612 zio->io_error = SET_ERROR(ENXIO);
1613 }
1614
1615 mutex_enter(&vd->vdev_probe_lock);
1616 ASSERT(vd->vdev_probe_zio == zio);
1617 vd->vdev_probe_zio = NULL;
1618 mutex_exit(&vd->vdev_probe_lock);
1619
1620 zl = NULL;
1621 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1622 if (!vdev_accessible(vd, pio))
1623 pio->io_error = SET_ERROR(ENXIO);
1624
1625 kmem_free(vps, sizeof (*vps));
1626 }
1627 }
1628
1629 /*
1630 * Determine whether this device is accessible.
1631 *
1632 * Read and write to several known locations: the pad regions of each
1633 * vdev label but the first, which we leave alone in case it contains
1634 * a VTOC.
1635 */
1636 zio_t *
1637 vdev_probe(vdev_t *vd, zio_t *zio)
1638 {
1639 spa_t *spa = vd->vdev_spa;
1640 vdev_probe_stats_t *vps = NULL;
1641 zio_t *pio;
1642
1643 ASSERT(vd->vdev_ops->vdev_op_leaf);
1644
1645 /*
1646 * Don't probe the probe.
1647 */
1648 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1649 return (NULL);
1650
1651 /*
1652 * To prevent 'probe storms' when a device fails, we create
1653 * just one probe i/o at a time. All zios that want to probe
1654 * this vdev will become parents of the probe io.
1655 */
1656 mutex_enter(&vd->vdev_probe_lock);
1657
1658 if ((pio = vd->vdev_probe_zio) == NULL) {
1659 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1660
1661 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1662 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
1663 ZIO_FLAG_TRYHARD;
1664
1665 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1666 /*
1667 * vdev_cant_read and vdev_cant_write can only
1668 * transition from TRUE to FALSE when we have the
1669 * SCL_ZIO lock as writer; otherwise they can only
1670 * transition from FALSE to TRUE. This ensures that
1671 * any zio looking at these values can assume that
1672 * failures persist for the life of the I/O. That's
1673 * important because when a device has intermittent
1674 * connectivity problems, we want to ensure that
1675 * they're ascribed to the device (ENXIO) and not
1676 * the zio (EIO).
1677 *
1678 * Since we hold SCL_ZIO as writer here, clear both
1679 * values so the probe can reevaluate from first
1680 * principles.
1681 */
1682 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1683 vd->vdev_cant_read = B_FALSE;
1684 vd->vdev_cant_write = B_FALSE;
1685 }
1686
1687 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1688 vdev_probe_done, vps,
1689 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1690
1691 /*
1692 * We can't change the vdev state in this context, so we
1693 * kick off an async task to do it on our behalf.
1694 */
1695 if (zio != NULL) {
1696 vd->vdev_probe_wanted = B_TRUE;
1697 spa_async_request(spa, SPA_ASYNC_PROBE);
1698 }
1699 }
1700
1701 if (zio != NULL)
1702 zio_add_child(zio, pio);
1703
1704 mutex_exit(&vd->vdev_probe_lock);
1705
1706 if (vps == NULL) {
1707 ASSERT(zio != NULL);
1708 return (NULL);
1709 }
1710
1711 for (int l = 1; l < VDEV_LABELS; l++) {
1712 zio_nowait(zio_read_phys(pio, vd,
1713 vdev_label_offset(vd->vdev_psize, l,
1714 offsetof(vdev_label_t, vl_be)), VDEV_PAD_SIZE,
1715 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1716 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1717 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1718 }
1719
1720 if (zio == NULL)
1721 return (pio);
1722
1723 zio_nowait(pio);
1724 return (NULL);
1725 }
1726
1727 static void
1728 vdev_load_child(void *arg)
1729 {
1730 vdev_t *vd = arg;
1731
1732 vd->vdev_load_error = vdev_load(vd);
1733 }
1734
1735 static void
1736 vdev_open_child(void *arg)
1737 {
1738 vdev_t *vd = arg;
1739
1740 vd->vdev_open_thread = curthread;
1741 vd->vdev_open_error = vdev_open(vd);
1742 vd->vdev_open_thread = NULL;
1743 }
1744
1745 static boolean_t
1746 vdev_uses_zvols(vdev_t *vd)
1747 {
1748 #ifdef _KERNEL
1749 if (zvol_is_zvol(vd->vdev_path))
1750 return (B_TRUE);
1751 #endif
1752
1753 for (int c = 0; c < vd->vdev_children; c++)
1754 if (vdev_uses_zvols(vd->vdev_child[c]))
1755 return (B_TRUE);
1756
1757 return (B_FALSE);
1758 }
1759
1760 /*
1761 * Returns B_TRUE if the passed child should be opened.
1762 */
1763 static boolean_t
1764 vdev_default_open_children_func(vdev_t *vd)
1765 {
1766 return (B_TRUE);
1767 }
1768
1769 /*
1770 * Open the requested child vdevs. If any of the leaf vdevs are using
1771 * a ZFS volume then do the opens in a single thread. This avoids a
1772 * deadlock when the current thread is holding the spa_namespace_lock.
1773 */
1774 static void
1775 vdev_open_children_impl(vdev_t *vd, vdev_open_children_func_t *open_func)
1776 {
1777 int children = vd->vdev_children;
1778
1779 taskq_t *tq = taskq_create("vdev_open", children, minclsyspri,
1780 children, children, TASKQ_PREPOPULATE);
1781 vd->vdev_nonrot = B_TRUE;
1782
1783 for (int c = 0; c < children; c++) {
1784 vdev_t *cvd = vd->vdev_child[c];
1785
1786 if (open_func(cvd) == B_FALSE)
1787 continue;
1788
1789 if (tq == NULL || vdev_uses_zvols(vd)) {
1790 cvd->vdev_open_error = vdev_open(cvd);
1791 } else {
1792 VERIFY(taskq_dispatch(tq, vdev_open_child,
1793 cvd, TQ_SLEEP) != TASKQID_INVALID);
1794 }
1795
1796 vd->vdev_nonrot &= cvd->vdev_nonrot;
1797 }
1798
1799 if (tq != NULL) {
1800 taskq_wait(tq);
1801 taskq_destroy(tq);
1802 }
1803 }
1804
1805 /*
1806 * Open all child vdevs.
1807 */
1808 void
1809 vdev_open_children(vdev_t *vd)
1810 {
1811 vdev_open_children_impl(vd, vdev_default_open_children_func);
1812 }
1813
1814 /*
1815 * Conditionally open a subset of child vdevs.
1816 */
1817 void
1818 vdev_open_children_subset(vdev_t *vd, vdev_open_children_func_t *open_func)
1819 {
1820 vdev_open_children_impl(vd, open_func);
1821 }
1822
1823 /*
1824 * Compute the raidz-deflation ratio. Note, we hard-code
1825 * in 128k (1 << 17) because it is the "typical" blocksize.
1826 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1827 * otherwise it would inconsistently account for existing bp's.
1828 */
1829 static void
1830 vdev_set_deflate_ratio(vdev_t *vd)
1831 {
1832 if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
1833 vd->vdev_deflate_ratio = (1 << 17) /
1834 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
1835 }
1836 }
1837
1838 /*
1839 * Maximize performance by inflating the configured ashift for top level
1840 * vdevs to be as close to the physical ashift as possible while maintaining
1841 * administrator defined limits and ensuring it doesn't go below the
1842 * logical ashift.
1843 */
1844 static void
1845 vdev_ashift_optimize(vdev_t *vd)
1846 {
1847 ASSERT(vd == vd->vdev_top);
1848
1849 if (vd->vdev_ashift < vd->vdev_physical_ashift) {
1850 vd->vdev_ashift = MIN(
1851 MAX(zfs_vdev_max_auto_ashift, vd->vdev_ashift),
1852 MAX(zfs_vdev_min_auto_ashift,
1853 vd->vdev_physical_ashift));
1854 } else {
1855 /*
1856 * If the logical and physical ashifts are the same, then
1857 * we ensure that the top-level vdev's ashift is not smaller
1858 * than our minimum ashift value. For the unusual case
1859 * where logical ashift > physical ashift, we can't cap
1860 * the calculated ashift based on max ashift as that
1861 * would cause failures.
1862 * We still check if we need to increase it to match
1863 * the min ashift.
1864 */
1865 vd->vdev_ashift = MAX(zfs_vdev_min_auto_ashift,
1866 vd->vdev_ashift);
1867 }
1868 }
1869
1870 /*
1871 * Prepare a virtual device for access.
1872 */
1873 int
1874 vdev_open(vdev_t *vd)
1875 {
1876 spa_t *spa = vd->vdev_spa;
1877 int error;
1878 uint64_t osize = 0;
1879 uint64_t max_osize = 0;
1880 uint64_t asize, max_asize, psize;
1881 uint64_t logical_ashift = 0;
1882 uint64_t physical_ashift = 0;
1883
1884 ASSERT(vd->vdev_open_thread == curthread ||
1885 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1886 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1887 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1888 vd->vdev_state == VDEV_STATE_OFFLINE);
1889
1890 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1891 vd->vdev_cant_read = B_FALSE;
1892 vd->vdev_cant_write = B_FALSE;
1893 vd->vdev_min_asize = vdev_get_min_asize(vd);
1894
1895 /*
1896 * If this vdev is not removed, check its fault status. If it's
1897 * faulted, bail out of the open.
1898 */
1899 if (!vd->vdev_removed && vd->vdev_faulted) {
1900 ASSERT(vd->vdev_children == 0);
1901 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1902 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1903 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1904 vd->vdev_label_aux);
1905 return (SET_ERROR(ENXIO));
1906 } else if (vd->vdev_offline) {
1907 ASSERT(vd->vdev_children == 0);
1908 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1909 return (SET_ERROR(ENXIO));
1910 }
1911
1912 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize,
1913 &logical_ashift, &physical_ashift);
1914 /*
1915 * Physical volume size should never be larger than its max size, unless
1916 * the disk has shrunk while we were reading it or the device is buggy
1917 * or damaged: either way it's not safe for use, bail out of the open.
1918 */
1919 if (osize > max_osize) {
1920 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1921 VDEV_AUX_OPEN_FAILED);
1922 return (SET_ERROR(ENXIO));
1923 }
1924
1925 /*
1926 * Reset the vdev_reopening flag so that we actually close
1927 * the vdev on error.
1928 */
1929 vd->vdev_reopening = B_FALSE;
1930 if (zio_injection_enabled && error == 0)
1931 error = zio_handle_device_injection(vd, NULL, SET_ERROR(ENXIO));
1932
1933 if (error) {
1934 if (vd->vdev_removed &&
1935 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
1936 vd->vdev_removed = B_FALSE;
1937
1938 if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
1939 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
1940 vd->vdev_stat.vs_aux);
1941 } else {
1942 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1943 vd->vdev_stat.vs_aux);
1944 }
1945 return (error);
1946 }
1947
1948 vd->vdev_removed = B_FALSE;
1949
1950 /*
1951 * Recheck the faulted flag now that we have confirmed that
1952 * the vdev is accessible. If we're faulted, bail.
1953 */
1954 if (vd->vdev_faulted) {
1955 ASSERT(vd->vdev_children == 0);
1956 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1957 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1958 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1959 vd->vdev_label_aux);
1960 return (SET_ERROR(ENXIO));
1961 }
1962
1963 if (vd->vdev_degraded) {
1964 ASSERT(vd->vdev_children == 0);
1965 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1966 VDEV_AUX_ERR_EXCEEDED);
1967 } else {
1968 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1969 }
1970
1971 /*
1972 * For hole or missing vdevs we just return success.
1973 */
1974 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
1975 return (0);
1976
1977 for (int c = 0; c < vd->vdev_children; c++) {
1978 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1979 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1980 VDEV_AUX_NONE);
1981 break;
1982 }
1983 }
1984
1985 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1986 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1987
1988 if (vd->vdev_children == 0) {
1989 if (osize < SPA_MINDEVSIZE) {
1990 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1991 VDEV_AUX_TOO_SMALL);
1992 return (SET_ERROR(EOVERFLOW));
1993 }
1994 psize = osize;
1995 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1996 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
1997 VDEV_LABEL_END_SIZE);
1998 } else {
1999 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
2000 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
2001 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2002 VDEV_AUX_TOO_SMALL);
2003 return (SET_ERROR(EOVERFLOW));
2004 }
2005 psize = 0;
2006 asize = osize;
2007 max_asize = max_osize;
2008 }
2009
2010 /*
2011 * If the vdev was expanded, record this so that we can re-create the
2012 * uberblock rings in labels {2,3}, during the next sync.
2013 */
2014 if ((psize > vd->vdev_psize) && (vd->vdev_psize != 0))
2015 vd->vdev_copy_uberblocks = B_TRUE;
2016
2017 vd->vdev_psize = psize;
2018
2019 /*
2020 * Make sure the allocatable size hasn't shrunk too much.
2021 */
2022 if (asize < vd->vdev_min_asize) {
2023 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2024 VDEV_AUX_BAD_LABEL);
2025 return (SET_ERROR(EINVAL));
2026 }
2027
2028 /*
2029 * We can always set the logical/physical ashift members since
2030 * their values are only used to calculate the vdev_ashift when
2031 * the device is first added to the config. These values should
2032 * not be used for anything else since they may change whenever
2033 * the device is reopened and we don't store them in the label.
2034 */
2035 vd->vdev_physical_ashift =
2036 MAX(physical_ashift, vd->vdev_physical_ashift);
2037 vd->vdev_logical_ashift = MAX(logical_ashift,
2038 vd->vdev_logical_ashift);
2039
2040 if (vd->vdev_asize == 0) {
2041 /*
2042 * This is the first-ever open, so use the computed values.
2043 * For compatibility, a different ashift can be requested.
2044 */
2045 vd->vdev_asize = asize;
2046 vd->vdev_max_asize = max_asize;
2047
2048 /*
2049 * If the vdev_ashift was not overriden at creation time,
2050 * then set it the logical ashift and optimize the ashift.
2051 */
2052 if (vd->vdev_ashift == 0) {
2053 vd->vdev_ashift = vd->vdev_logical_ashift;
2054
2055 if (vd->vdev_logical_ashift > ASHIFT_MAX) {
2056 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2057 VDEV_AUX_ASHIFT_TOO_BIG);
2058 return (SET_ERROR(EDOM));
2059 }
2060
2061 if (vd->vdev_top == vd) {
2062 vdev_ashift_optimize(vd);
2063 }
2064 }
2065 if (vd->vdev_ashift != 0 && (vd->vdev_ashift < ASHIFT_MIN ||
2066 vd->vdev_ashift > ASHIFT_MAX)) {
2067 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2068 VDEV_AUX_BAD_ASHIFT);
2069 return (SET_ERROR(EDOM));
2070 }
2071 } else {
2072 /*
2073 * Make sure the alignment required hasn't increased.
2074 */
2075 if (vd->vdev_ashift > vd->vdev_top->vdev_ashift &&
2076 vd->vdev_ops->vdev_op_leaf) {
2077 (void) zfs_ereport_post(
2078 FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT,
2079 spa, vd, NULL, NULL, 0);
2080 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2081 VDEV_AUX_BAD_LABEL);
2082 return (SET_ERROR(EDOM));
2083 }
2084 vd->vdev_max_asize = max_asize;
2085 }
2086
2087 /*
2088 * If all children are healthy we update asize if either:
2089 * The asize has increased, due to a device expansion caused by dynamic
2090 * LUN growth or vdev replacement, and automatic expansion is enabled;
2091 * making the additional space available.
2092 *
2093 * The asize has decreased, due to a device shrink usually caused by a
2094 * vdev replace with a smaller device. This ensures that calculations
2095 * based of max_asize and asize e.g. esize are always valid. It's safe
2096 * to do this as we've already validated that asize is greater than
2097 * vdev_min_asize.
2098 */
2099 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
2100 ((asize > vd->vdev_asize &&
2101 (vd->vdev_expanding || spa->spa_autoexpand)) ||
2102 (asize < vd->vdev_asize)))
2103 vd->vdev_asize = asize;
2104
2105 vdev_set_min_asize(vd);
2106
2107 /*
2108 * Ensure we can issue some IO before declaring the
2109 * vdev open for business.
2110 */
2111 if (vd->vdev_ops->vdev_op_leaf &&
2112 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
2113 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
2114 VDEV_AUX_ERR_EXCEEDED);
2115 return (error);
2116 }
2117
2118 /*
2119 * Track the the minimum allocation size.
2120 */
2121 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
2122 vd->vdev_islog == 0 && vd->vdev_aux == NULL) {
2123 uint64_t min_alloc = vdev_get_min_alloc(vd);
2124 if (min_alloc < spa->spa_min_alloc)
2125 spa->spa_min_alloc = min_alloc;
2126 }
2127
2128 /*
2129 * If this is a leaf vdev, assess whether a resilver is needed.
2130 * But don't do this if we are doing a reopen for a scrub, since
2131 * this would just restart the scrub we are already doing.
2132 */
2133 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen)
2134 dsl_scan_assess_vdev(spa->spa_dsl_pool, vd);
2135
2136 return (0);
2137 }
2138
2139 /*
2140 * Called once the vdevs are all opened, this routine validates the label
2141 * contents. This needs to be done before vdev_load() so that we don't
2142 * inadvertently do repair I/Os to the wrong device.
2143 *
2144 * This function will only return failure if one of the vdevs indicates that it
2145 * has since been destroyed or exported. This is only possible if
2146 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
2147 * will be updated but the function will return 0.
2148 */
2149 int
2150 vdev_validate(vdev_t *vd)
2151 {
2152 spa_t *spa = vd->vdev_spa;
2153 nvlist_t *label;
2154 uint64_t guid = 0, aux_guid = 0, top_guid;
2155 uint64_t state;
2156 nvlist_t *nvl;
2157 uint64_t txg;
2158
2159 if (vdev_validate_skip)
2160 return (0);
2161
2162 for (uint64_t c = 0; c < vd->vdev_children; c++)
2163 if (vdev_validate(vd->vdev_child[c]) != 0)
2164 return (SET_ERROR(EBADF));
2165
2166 /*
2167 * If the device has already failed, or was marked offline, don't do
2168 * any further validation. Otherwise, label I/O will fail and we will
2169 * overwrite the previous state.
2170 */
2171 if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
2172 return (0);
2173
2174 /*
2175 * If we are performing an extreme rewind, we allow for a label that
2176 * was modified at a point after the current txg.
2177 * If config lock is not held do not check for the txg. spa_sync could
2178 * be updating the vdev's label before updating spa_last_synced_txg.
2179 */
2180 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 ||
2181 spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG)
2182 txg = UINT64_MAX;
2183 else
2184 txg = spa_last_synced_txg(spa);
2185
2186 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
2187 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2188 VDEV_AUX_BAD_LABEL);
2189 vdev_dbgmsg(vd, "vdev_validate: failed reading config for "
2190 "txg %llu", (u_longlong_t)txg);
2191 return (0);
2192 }
2193
2194 /*
2195 * Determine if this vdev has been split off into another
2196 * pool. If so, then refuse to open it.
2197 */
2198 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
2199 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
2200 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2201 VDEV_AUX_SPLIT_POOL);
2202 nvlist_free(label);
2203 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
2204 return (0);
2205 }
2206
2207 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
2208 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2209 VDEV_AUX_CORRUPT_DATA);
2210 nvlist_free(label);
2211 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2212 ZPOOL_CONFIG_POOL_GUID);
2213 return (0);
2214 }
2215
2216 /*
2217 * If config is not trusted then ignore the spa guid check. This is
2218 * necessary because if the machine crashed during a re-guid the new
2219 * guid might have been written to all of the vdev labels, but not the
2220 * cached config. The check will be performed again once we have the
2221 * trusted config from the MOS.
2222 */
2223 if (spa->spa_trust_config && guid != spa_guid(spa)) {
2224 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2225 VDEV_AUX_CORRUPT_DATA);
2226 nvlist_free(label);
2227 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
2228 "match config (%llu != %llu)", (u_longlong_t)guid,
2229 (u_longlong_t)spa_guid(spa));
2230 return (0);
2231 }
2232
2233 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
2234 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
2235 &aux_guid) != 0)
2236 aux_guid = 0;
2237
2238 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
2239 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2240 VDEV_AUX_CORRUPT_DATA);
2241 nvlist_free(label);
2242 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2243 ZPOOL_CONFIG_GUID);
2244 return (0);
2245 }
2246
2247 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
2248 != 0) {
2249 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2250 VDEV_AUX_CORRUPT_DATA);
2251 nvlist_free(label);
2252 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2253 ZPOOL_CONFIG_TOP_GUID);
2254 return (0);
2255 }
2256
2257 /*
2258 * If this vdev just became a top-level vdev because its sibling was
2259 * detached, it will have adopted the parent's vdev guid -- but the
2260 * label may or may not be on disk yet. Fortunately, either version
2261 * of the label will have the same top guid, so if we're a top-level
2262 * vdev, we can safely compare to that instead.
2263 * However, if the config comes from a cachefile that failed to update
2264 * after the detach, a top-level vdev will appear as a non top-level
2265 * vdev in the config. Also relax the constraints if we perform an
2266 * extreme rewind.
2267 *
2268 * If we split this vdev off instead, then we also check the
2269 * original pool's guid. We don't want to consider the vdev
2270 * corrupt if it is partway through a split operation.
2271 */
2272 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
2273 boolean_t mismatch = B_FALSE;
2274 if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
2275 if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
2276 mismatch = B_TRUE;
2277 } else {
2278 if (vd->vdev_guid != top_guid &&
2279 vd->vdev_top->vdev_guid != guid)
2280 mismatch = B_TRUE;
2281 }
2282
2283 if (mismatch) {
2284 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2285 VDEV_AUX_CORRUPT_DATA);
2286 nvlist_free(label);
2287 vdev_dbgmsg(vd, "vdev_validate: config guid "
2288 "doesn't match label guid");
2289 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
2290 (u_longlong_t)vd->vdev_guid,
2291 (u_longlong_t)vd->vdev_top->vdev_guid);
2292 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
2293 "aux_guid %llu", (u_longlong_t)guid,
2294 (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
2295 return (0);
2296 }
2297 }
2298
2299 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
2300 &state) != 0) {
2301 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2302 VDEV_AUX_CORRUPT_DATA);
2303 nvlist_free(label);
2304 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2305 ZPOOL_CONFIG_POOL_STATE);
2306 return (0);
2307 }
2308
2309 nvlist_free(label);
2310
2311 /*
2312 * If this is a verbatim import, no need to check the
2313 * state of the pool.
2314 */
2315 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
2316 spa_load_state(spa) == SPA_LOAD_OPEN &&
2317 state != POOL_STATE_ACTIVE) {
2318 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
2319 "for spa %s", (u_longlong_t)state, spa->spa_name);
2320 return (SET_ERROR(EBADF));
2321 }
2322
2323 /*
2324 * If we were able to open and validate a vdev that was
2325 * previously marked permanently unavailable, clear that state
2326 * now.
2327 */
2328 if (vd->vdev_not_present)
2329 vd->vdev_not_present = 0;
2330
2331 return (0);
2332 }
2333
2334 static void
2335 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
2336 {
2337 if (svd->vdev_path != NULL && dvd->vdev_path != NULL) {
2338 if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) {
2339 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
2340 "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
2341 dvd->vdev_path, svd->vdev_path);
2342 spa_strfree(dvd->vdev_path);
2343 dvd->vdev_path = spa_strdup(svd->vdev_path);
2344 }
2345 } else if (svd->vdev_path != NULL) {
2346 dvd->vdev_path = spa_strdup(svd->vdev_path);
2347 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
2348 (u_longlong_t)dvd->vdev_guid, dvd->vdev_path);
2349 }
2350 }
2351
2352 /*
2353 * Recursively copy vdev paths from one vdev to another. Source and destination
2354 * vdev trees must have same geometry otherwise return error. Intended to copy
2355 * paths from userland config into MOS config.
2356 */
2357 int
2358 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
2359 {
2360 if ((svd->vdev_ops == &vdev_missing_ops) ||
2361 (svd->vdev_ishole && dvd->vdev_ishole) ||
2362 (dvd->vdev_ops == &vdev_indirect_ops))
2363 return (0);
2364
2365 if (svd->vdev_ops != dvd->vdev_ops) {
2366 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
2367 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
2368 return (SET_ERROR(EINVAL));
2369 }
2370
2371 if (svd->vdev_guid != dvd->vdev_guid) {
2372 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
2373 "%llu)", (u_longlong_t)svd->vdev_guid,
2374 (u_longlong_t)dvd->vdev_guid);
2375 return (SET_ERROR(EINVAL));
2376 }
2377
2378 if (svd->vdev_children != dvd->vdev_children) {
2379 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
2380 "%llu != %llu", (u_longlong_t)svd->vdev_children,
2381 (u_longlong_t)dvd->vdev_children);
2382 return (SET_ERROR(EINVAL));
2383 }
2384
2385 for (uint64_t i = 0; i < svd->vdev_children; i++) {
2386 int error = vdev_copy_path_strict(svd->vdev_child[i],
2387 dvd->vdev_child[i]);
2388 if (error != 0)
2389 return (error);
2390 }
2391
2392 if (svd->vdev_ops->vdev_op_leaf)
2393 vdev_copy_path_impl(svd, dvd);
2394
2395 return (0);
2396 }
2397
2398 static void
2399 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
2400 {
2401 ASSERT(stvd->vdev_top == stvd);
2402 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
2403
2404 for (uint64_t i = 0; i < dvd->vdev_children; i++) {
2405 vdev_copy_path_search(stvd, dvd->vdev_child[i]);
2406 }
2407
2408 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
2409 return;
2410
2411 /*
2412 * The idea here is that while a vdev can shift positions within
2413 * a top vdev (when replacing, attaching mirror, etc.) it cannot
2414 * step outside of it.
2415 */
2416 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
2417
2418 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
2419 return;
2420
2421 ASSERT(vd->vdev_ops->vdev_op_leaf);
2422
2423 vdev_copy_path_impl(vd, dvd);
2424 }
2425
2426 /*
2427 * Recursively copy vdev paths from one root vdev to another. Source and
2428 * destination vdev trees may differ in geometry. For each destination leaf
2429 * vdev, search a vdev with the same guid and top vdev id in the source.
2430 * Intended to copy paths from userland config into MOS config.
2431 */
2432 void
2433 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
2434 {
2435 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
2436 ASSERT(srvd->vdev_ops == &vdev_root_ops);
2437 ASSERT(drvd->vdev_ops == &vdev_root_ops);
2438
2439 for (uint64_t i = 0; i < children; i++) {
2440 vdev_copy_path_search(srvd->vdev_child[i],
2441 drvd->vdev_child[i]);
2442 }
2443 }
2444
2445 /*
2446 * Close a virtual device.
2447 */
2448 void
2449 vdev_close(vdev_t *vd)
2450 {
2451 vdev_t *pvd = vd->vdev_parent;
2452 spa_t *spa __maybe_unused = vd->vdev_spa;
2453
2454 ASSERT(vd != NULL);
2455 ASSERT(vd->vdev_open_thread == curthread ||
2456 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2457
2458 /*
2459 * If our parent is reopening, then we are as well, unless we are
2460 * going offline.
2461 */
2462 if (pvd != NULL && pvd->vdev_reopening)
2463 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
2464
2465 vd->vdev_ops->vdev_op_close(vd);
2466
2467 vdev_cache_purge(vd);
2468
2469 /*
2470 * We record the previous state before we close it, so that if we are
2471 * doing a reopen(), we don't generate FMA ereports if we notice that
2472 * it's still faulted.
2473 */
2474 vd->vdev_prevstate = vd->vdev_state;
2475
2476 if (vd->vdev_offline)
2477 vd->vdev_state = VDEV_STATE_OFFLINE;
2478 else
2479 vd->vdev_state = VDEV_STATE_CLOSED;
2480 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2481 }
2482
2483 void
2484 vdev_hold(vdev_t *vd)
2485 {
2486 spa_t *spa = vd->vdev_spa;
2487
2488 ASSERT(spa_is_root(spa));
2489 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
2490 return;
2491
2492 for (int c = 0; c < vd->vdev_children; c++)
2493 vdev_hold(vd->vdev_child[c]);
2494
2495 if (vd->vdev_ops->vdev_op_leaf)
2496 vd->vdev_ops->vdev_op_hold(vd);
2497 }
2498
2499 void
2500 vdev_rele(vdev_t *vd)
2501 {
2502 ASSERT(spa_is_root(vd->vdev_spa));
2503 for (int c = 0; c < vd->vdev_children; c++)
2504 vdev_rele(vd->vdev_child[c]);
2505
2506 if (vd->vdev_ops->vdev_op_leaf)
2507 vd->vdev_ops->vdev_op_rele(vd);
2508 }
2509
2510 /*
2511 * Reopen all interior vdevs and any unopened leaves. We don't actually
2512 * reopen leaf vdevs which had previously been opened as they might deadlock
2513 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2514 * If the leaf has never been opened then open it, as usual.
2515 */
2516 void
2517 vdev_reopen(vdev_t *vd)
2518 {
2519 spa_t *spa = vd->vdev_spa;
2520
2521 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2522
2523 /* set the reopening flag unless we're taking the vdev offline */
2524 vd->vdev_reopening = !vd->vdev_offline;
2525 vdev_close(vd);
2526 (void) vdev_open(vd);
2527
2528 /*
2529 * Call vdev_validate() here to make sure we have the same device.
2530 * Otherwise, a device with an invalid label could be successfully
2531 * opened in response to vdev_reopen().
2532 */
2533 if (vd->vdev_aux) {
2534 (void) vdev_validate_aux(vd);
2535 if (vdev_readable(vd) && vdev_writeable(vd) &&
2536 vd->vdev_aux == &spa->spa_l2cache) {
2537 /*
2538 * In case the vdev is present we should evict all ARC
2539 * buffers and pointers to log blocks and reclaim their
2540 * space before restoring its contents to L2ARC.
2541 */
2542 if (l2arc_vdev_present(vd)) {
2543 l2arc_rebuild_vdev(vd, B_TRUE);
2544 } else {
2545 l2arc_add_vdev(spa, vd);
2546 }
2547 spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD);
2548 spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM);
2549 }
2550 } else {
2551 (void) vdev_validate(vd);
2552 }
2553
2554 /*
2555 * Reassess parent vdev's health.
2556 */
2557 vdev_propagate_state(vd);
2558 }
2559
2560 int
2561 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
2562 {
2563 int error;
2564
2565 /*
2566 * Normally, partial opens (e.g. of a mirror) are allowed.
2567 * For a create, however, we want to fail the request if
2568 * there are any components we can't open.
2569 */
2570 error = vdev_open(vd);
2571
2572 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
2573 vdev_close(vd);
2574 return (error ? error : SET_ERROR(ENXIO));
2575 }
2576
2577 /*
2578 * Recursively load DTLs and initialize all labels.
2579 */
2580 if ((error = vdev_dtl_load(vd)) != 0 ||
2581 (error = vdev_label_init(vd, txg, isreplacing ?
2582 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
2583 vdev_close(vd);
2584 return (error);
2585 }
2586
2587 return (0);
2588 }
2589
2590 void
2591 vdev_metaslab_set_size(vdev_t *vd)
2592 {
2593 uint64_t asize = vd->vdev_asize;
2594 uint64_t ms_count = asize >> zfs_vdev_default_ms_shift;
2595 uint64_t ms_shift;
2596
2597 /*
2598 * There are two dimensions to the metaslab sizing calculation:
2599 * the size of the metaslab and the count of metaslabs per vdev.
2600 *
2601 * The default values used below are a good balance between memory
2602 * usage (larger metaslab size means more memory needed for loaded
2603 * metaslabs; more metaslabs means more memory needed for the
2604 * metaslab_t structs), metaslab load time (larger metaslabs take
2605 * longer to load), and metaslab sync time (more metaslabs means
2606 * more time spent syncing all of them).
2607 *
2608 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2609 * The range of the dimensions are as follows:
2610 *
2611 * 2^29 <= ms_size <= 2^34
2612 * 16 <= ms_count <= 131,072
2613 *
2614 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2615 * at least 512MB (2^29) to minimize fragmentation effects when
2616 * testing with smaller devices. However, the count constraint
2617 * of at least 16 metaslabs will override this minimum size goal.
2618 *
2619 * On the upper end of vdev sizes, we aim for a maximum metaslab
2620 * size of 16GB. However, we will cap the total count to 2^17
2621 * metaslabs to keep our memory footprint in check and let the
2622 * metaslab size grow from there if that limit is hit.
2623 *
2624 * The net effect of applying above constrains is summarized below.
2625 *
2626 * vdev size metaslab count
2627 * --------------|-----------------
2628 * < 8GB ~16
2629 * 8GB - 100GB one per 512MB
2630 * 100GB - 3TB ~200
2631 * 3TB - 2PB one per 16GB
2632 * > 2PB ~131,072
2633 * --------------------------------
2634 *
2635 * Finally, note that all of the above calculate the initial
2636 * number of metaslabs. Expanding a top-level vdev will result
2637 * in additional metaslabs being allocated making it possible
2638 * to exceed the zfs_vdev_ms_count_limit.
2639 */
2640
2641 if (ms_count < zfs_vdev_min_ms_count)
2642 ms_shift = highbit64(asize / zfs_vdev_min_ms_count);
2643 else if (ms_count > zfs_vdev_default_ms_count)
2644 ms_shift = highbit64(asize / zfs_vdev_default_ms_count);
2645 else
2646 ms_shift = zfs_vdev_default_ms_shift;
2647
2648 if (ms_shift < SPA_MAXBLOCKSHIFT) {
2649 ms_shift = SPA_MAXBLOCKSHIFT;
2650 } else if (ms_shift > zfs_vdev_max_ms_shift) {
2651 ms_shift = zfs_vdev_max_ms_shift;
2652 /* cap the total count to constrain memory footprint */
2653 if ((asize >> ms_shift) > zfs_vdev_ms_count_limit)
2654 ms_shift = highbit64(asize / zfs_vdev_ms_count_limit);
2655 }
2656
2657 vd->vdev_ms_shift = ms_shift;
2658 ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT);
2659 }
2660
2661 void
2662 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2663 {
2664 ASSERT(vd == vd->vdev_top);
2665 /* indirect vdevs don't have metaslabs or dtls */
2666 ASSERT(vdev_is_concrete(vd) || flags == 0);
2667 ASSERT(ISP2(flags));
2668 ASSERT(spa_writeable(vd->vdev_spa));
2669
2670 if (flags & VDD_METASLAB)
2671 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2672
2673 if (flags & VDD_DTL)
2674 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2675
2676 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2677 }
2678
2679 void
2680 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
2681 {
2682 for (int c = 0; c < vd->vdev_children; c++)
2683 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
2684
2685 if (vd->vdev_ops->vdev_op_leaf)
2686 vdev_dirty(vd->vdev_top, flags, vd, txg);
2687 }
2688
2689 /*
2690 * DTLs.
2691 *
2692 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2693 * the vdev has less than perfect replication. There are four kinds of DTL:
2694 *
2695 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2696 *
2697 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2698 *
2699 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2700 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2701 * txgs that was scrubbed.
2702 *
2703 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2704 * persistent errors or just some device being offline.
2705 * Unlike the other three, the DTL_OUTAGE map is not generally
2706 * maintained; it's only computed when needed, typically to
2707 * determine whether a device can be detached.
2708 *
2709 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2710 * either has the data or it doesn't.
2711 *
2712 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2713 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2714 * if any child is less than fully replicated, then so is its parent.
2715 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2716 * comprising only those txgs which appear in 'maxfaults' or more children;
2717 * those are the txgs we don't have enough replication to read. For example,
2718 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2719 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2720 * two child DTL_MISSING maps.
2721 *
2722 * It should be clear from the above that to compute the DTLs and outage maps
2723 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2724 * Therefore, that is all we keep on disk. When loading the pool, or after
2725 * a configuration change, we generate all other DTLs from first principles.
2726 */
2727 void
2728 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2729 {
2730 range_tree_t *rt = vd->vdev_dtl[t];
2731
2732 ASSERT(t < DTL_TYPES);
2733 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2734 ASSERT(spa_writeable(vd->vdev_spa));
2735
2736 mutex_enter(&vd->vdev_dtl_lock);
2737 if (!range_tree_contains(rt, txg, size))
2738 range_tree_add(rt, txg, size);
2739 mutex_exit(&vd->vdev_dtl_lock);
2740 }
2741
2742 boolean_t
2743 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2744 {
2745 range_tree_t *rt = vd->vdev_dtl[t];
2746 boolean_t dirty = B_FALSE;
2747
2748 ASSERT(t < DTL_TYPES);
2749 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2750
2751 /*
2752 * While we are loading the pool, the DTLs have not been loaded yet.
2753 * This isn't a problem but it can result in devices being tried
2754 * which are known to not have the data. In which case, the import
2755 * is relying on the checksum to ensure that we get the right data.
2756 * Note that while importing we are only reading the MOS, which is
2757 * always checksummed.
2758 */
2759 mutex_enter(&vd->vdev_dtl_lock);
2760 if (!range_tree_is_empty(rt))
2761 dirty = range_tree_contains(rt, txg, size);
2762 mutex_exit(&vd->vdev_dtl_lock);
2763
2764 return (dirty);
2765 }
2766
2767 boolean_t
2768 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
2769 {
2770 range_tree_t *rt = vd->vdev_dtl[t];
2771 boolean_t empty;
2772
2773 mutex_enter(&vd->vdev_dtl_lock);
2774 empty = range_tree_is_empty(rt);
2775 mutex_exit(&vd->vdev_dtl_lock);
2776
2777 return (empty);
2778 }
2779
2780 /*
2781 * Check if the txg falls within the range which must be
2782 * resilvered. DVAs outside this range can always be skipped.
2783 */
2784 boolean_t
2785 vdev_default_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
2786 uint64_t phys_birth)
2787 {
2788 /* Set by sequential resilver. */
2789 if (phys_birth == TXG_UNKNOWN)
2790 return (B_TRUE);
2791
2792 return (vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1));
2793 }
2794
2795 /*
2796 * Returns B_TRUE if the vdev determines the DVA needs to be resilvered.
2797 */
2798 boolean_t
2799 vdev_dtl_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
2800 uint64_t phys_birth)
2801 {
2802 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2803
2804 if (vd->vdev_ops->vdev_op_need_resilver == NULL ||
2805 vd->vdev_ops->vdev_op_leaf)
2806 return (B_TRUE);
2807
2808 return (vd->vdev_ops->vdev_op_need_resilver(vd, dva, psize,
2809 phys_birth));
2810 }
2811
2812 /*
2813 * Returns the lowest txg in the DTL range.
2814 */
2815 static uint64_t
2816 vdev_dtl_min(vdev_t *vd)
2817 {
2818 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2819 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2820 ASSERT0(vd->vdev_children);
2821
2822 return (range_tree_min(vd->vdev_dtl[DTL_MISSING]) - 1);
2823 }
2824
2825 /*
2826 * Returns the highest txg in the DTL.
2827 */
2828 static uint64_t
2829 vdev_dtl_max(vdev_t *vd)
2830 {
2831 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2832 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2833 ASSERT0(vd->vdev_children);
2834
2835 return (range_tree_max(vd->vdev_dtl[DTL_MISSING]));
2836 }
2837
2838 /*
2839 * Determine if a resilvering vdev should remove any DTL entries from
2840 * its range. If the vdev was resilvering for the entire duration of the
2841 * scan then it should excise that range from its DTLs. Otherwise, this
2842 * vdev is considered partially resilvered and should leave its DTL
2843 * entries intact. The comment in vdev_dtl_reassess() describes how we
2844 * excise the DTLs.
2845 */
2846 static boolean_t
2847 vdev_dtl_should_excise(vdev_t *vd, boolean_t rebuild_done)
2848 {
2849 ASSERT0(vd->vdev_children);
2850
2851 if (vd->vdev_state < VDEV_STATE_DEGRADED)
2852 return (B_FALSE);
2853
2854 if (vd->vdev_resilver_deferred)
2855 return (B_FALSE);
2856
2857 if (range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]))
2858 return (B_TRUE);
2859
2860 if (rebuild_done) {
2861 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config;
2862 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
2863
2864 /* Rebuild not initiated by attach */
2865 if (vd->vdev_rebuild_txg == 0)
2866 return (B_TRUE);
2867
2868 /*
2869 * When a rebuild completes without error then all missing data
2870 * up to the rebuild max txg has been reconstructed and the DTL
2871 * is eligible for excision.
2872 */
2873 if (vrp->vrp_rebuild_state == VDEV_REBUILD_COMPLETE &&
2874 vdev_dtl_max(vd) <= vrp->vrp_max_txg) {
2875 ASSERT3U(vrp->vrp_min_txg, <=, vdev_dtl_min(vd));
2876 ASSERT3U(vrp->vrp_min_txg, <, vd->vdev_rebuild_txg);
2877 ASSERT3U(vd->vdev_rebuild_txg, <=, vrp->vrp_max_txg);
2878 return (B_TRUE);
2879 }
2880 } else {
2881 dsl_scan_t *scn = vd->vdev_spa->spa_dsl_pool->dp_scan;
2882 dsl_scan_phys_t *scnp __maybe_unused = &scn->scn_phys;
2883
2884 /* Resilver not initiated by attach */
2885 if (vd->vdev_resilver_txg == 0)
2886 return (B_TRUE);
2887
2888 /*
2889 * When a resilver is initiated the scan will assign the
2890 * scn_max_txg value to the highest txg value that exists
2891 * in all DTLs. If this device's max DTL is not part of this
2892 * scan (i.e. it is not in the range (scn_min_txg, scn_max_txg]
2893 * then it is not eligible for excision.
2894 */
2895 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
2896 ASSERT3U(scnp->scn_min_txg, <=, vdev_dtl_min(vd));
2897 ASSERT3U(scnp->scn_min_txg, <, vd->vdev_resilver_txg);
2898 ASSERT3U(vd->vdev_resilver_txg, <=, scnp->scn_max_txg);
2899 return (B_TRUE);
2900 }
2901 }
2902
2903 return (B_FALSE);
2904 }
2905
2906 /*
2907 * Reassess DTLs after a config change or scrub completion. If txg == 0 no
2908 * write operations will be issued to the pool.
2909 */
2910 void
2911 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg,
2912 boolean_t scrub_done, boolean_t rebuild_done)
2913 {
2914 spa_t *spa = vd->vdev_spa;
2915 avl_tree_t reftree;
2916 int minref;
2917
2918 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2919
2920 for (int c = 0; c < vd->vdev_children; c++)
2921 vdev_dtl_reassess(vd->vdev_child[c], txg,
2922 scrub_txg, scrub_done, rebuild_done);
2923
2924 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
2925 return;
2926
2927 if (vd->vdev_ops->vdev_op_leaf) {
2928 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2929 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config;
2930 boolean_t check_excise = B_FALSE;
2931 boolean_t wasempty = B_TRUE;
2932
2933 mutex_enter(&vd->vdev_dtl_lock);
2934
2935 /*
2936 * If requested, pretend the scan or rebuild completed cleanly.
2937 */
2938 if (zfs_scan_ignore_errors) {
2939 if (scn != NULL)
2940 scn->scn_phys.scn_errors = 0;
2941 if (vr != NULL)
2942 vr->vr_rebuild_phys.vrp_errors = 0;
2943 }
2944
2945 if (scrub_txg != 0 &&
2946 !range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
2947 wasempty = B_FALSE;
2948 zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d "
2949 "dtl:%llu/%llu errors:%llu",
2950 (u_longlong_t)vd->vdev_guid, (u_longlong_t)txg,
2951 (u_longlong_t)scrub_txg, spa->spa_scrub_started,
2952 (u_longlong_t)vdev_dtl_min(vd),
2953 (u_longlong_t)vdev_dtl_max(vd),
2954 (u_longlong_t)(scn ? scn->scn_phys.scn_errors : 0));
2955 }
2956
2957 /*
2958 * If we've completed a scrub/resilver or a rebuild cleanly
2959 * then determine if this vdev should remove any DTLs. We
2960 * only want to excise regions on vdevs that were available
2961 * during the entire duration of this scan.
2962 */
2963 if (rebuild_done &&
2964 vr != NULL && vr->vr_rebuild_phys.vrp_errors == 0) {
2965 check_excise = B_TRUE;
2966 } else {
2967 if (spa->spa_scrub_started ||
2968 (scn != NULL && scn->scn_phys.scn_errors == 0)) {
2969 check_excise = B_TRUE;
2970 }
2971 }
2972
2973 if (scrub_txg && check_excise &&
2974 vdev_dtl_should_excise(vd, rebuild_done)) {
2975 /*
2976 * We completed a scrub, resilver or rebuild up to
2977 * scrub_txg. If we did it without rebooting, then
2978 * the scrub dtl will be valid, so excise the old
2979 * region and fold in the scrub dtl. Otherwise,
2980 * leave the dtl as-is if there was an error.
2981 *
2982 * There's little trick here: to excise the beginning
2983 * of the DTL_MISSING map, we put it into a reference
2984 * tree and then add a segment with refcnt -1 that
2985 * covers the range [0, scrub_txg). This means
2986 * that each txg in that range has refcnt -1 or 0.
2987 * We then add DTL_SCRUB with a refcnt of 2, so that
2988 * entries in the range [0, scrub_txg) will have a
2989 * positive refcnt -- either 1 or 2. We then convert
2990 * the reference tree into the new DTL_MISSING map.
2991 */
2992 space_reftree_create(&reftree);
2993 space_reftree_add_map(&reftree,
2994 vd->vdev_dtl[DTL_MISSING], 1);
2995 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
2996 space_reftree_add_map(&reftree,
2997 vd->vdev_dtl[DTL_SCRUB], 2);
2998 space_reftree_generate_map(&reftree,
2999 vd->vdev_dtl[DTL_MISSING], 1);
3000 space_reftree_destroy(&reftree);
3001
3002 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
3003 zfs_dbgmsg("update DTL_MISSING:%llu/%llu",
3004 (u_longlong_t)vdev_dtl_min(vd),
3005 (u_longlong_t)vdev_dtl_max(vd));
3006 } else if (!wasempty) {
3007 zfs_dbgmsg("DTL_MISSING is now empty");
3008 }
3009 }
3010 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
3011 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
3012 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
3013 if (scrub_done)
3014 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
3015 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
3016 if (!vdev_readable(vd))
3017 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
3018 else
3019 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
3020 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
3021
3022 /*
3023 * If the vdev was resilvering or rebuilding and no longer
3024 * has any DTLs then reset the appropriate flag and dirty
3025 * the top level so that we persist the change.
3026 */
3027 if (txg != 0 &&
3028 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
3029 range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) {
3030 if (vd->vdev_rebuild_txg != 0) {
3031 vd->vdev_rebuild_txg = 0;
3032 vdev_config_dirty(vd->vdev_top);
3033 } else if (vd->vdev_resilver_txg != 0) {
3034 vd->vdev_resilver_txg = 0;
3035 vdev_config_dirty(vd->vdev_top);
3036 }
3037 }
3038
3039 mutex_exit(&vd->vdev_dtl_lock);
3040
3041 if (txg != 0)
3042 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
3043 return;
3044 }
3045
3046 mutex_enter(&vd->vdev_dtl_lock);
3047 for (int t = 0; t < DTL_TYPES; t++) {
3048 /* account for child's outage in parent's missing map */
3049 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
3050 if (t == DTL_SCRUB)
3051 continue; /* leaf vdevs only */
3052 if (t == DTL_PARTIAL)
3053 minref = 1; /* i.e. non-zero */
3054 else if (vdev_get_nparity(vd) != 0)
3055 minref = vdev_get_nparity(vd) + 1; /* RAID-Z, dRAID */
3056 else
3057 minref = vd->vdev_children; /* any kind of mirror */
3058 space_reftree_create(&reftree);
3059 for (int c = 0; c < vd->vdev_children; c++) {
3060 vdev_t *cvd = vd->vdev_child[c];
3061 mutex_enter(&cvd->vdev_dtl_lock);
3062 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
3063 mutex_exit(&cvd->vdev_dtl_lock);
3064 }
3065 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
3066 space_reftree_destroy(&reftree);
3067 }
3068 mutex_exit(&vd->vdev_dtl_lock);
3069 }
3070
3071 int
3072 vdev_dtl_load(vdev_t *vd)
3073 {
3074 spa_t *spa = vd->vdev_spa;
3075 objset_t *mos = spa->spa_meta_objset;
3076 range_tree_t *rt;
3077 int error = 0;
3078
3079 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
3080 ASSERT(vdev_is_concrete(vd));
3081
3082 error = space_map_open(&vd->vdev_dtl_sm, mos,
3083 vd->vdev_dtl_object, 0, -1ULL, 0);
3084 if (error)
3085 return (error);
3086 ASSERT(vd->vdev_dtl_sm != NULL);
3087
3088 rt = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
3089 error = space_map_load(vd->vdev_dtl_sm, rt, SM_ALLOC);
3090 if (error == 0) {
3091 mutex_enter(&vd->vdev_dtl_lock);
3092 range_tree_walk(rt, range_tree_add,
3093 vd->vdev_dtl[DTL_MISSING]);
3094 mutex_exit(&vd->vdev_dtl_lock);
3095 }
3096
3097 range_tree_vacate(rt, NULL, NULL);
3098 range_tree_destroy(rt);
3099
3100 return (error);
3101 }
3102
3103 for (int c = 0; c < vd->vdev_children; c++) {
3104 error = vdev_dtl_load(vd->vdev_child[c]);
3105 if (error != 0)
3106 break;
3107 }
3108
3109 return (error);
3110 }
3111
3112 static void
3113 vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx)
3114 {
3115 spa_t *spa = vd->vdev_spa;
3116 objset_t *mos = spa->spa_meta_objset;
3117 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
3118 const char *string;
3119
3120 ASSERT(alloc_bias != VDEV_BIAS_NONE);
3121
3122 string =
3123 (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG :
3124 (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL :
3125 (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL;
3126
3127 ASSERT(string != NULL);
3128 VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS,
3129 1, strlen(string) + 1, string, tx));
3130
3131 if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) {
3132 spa_activate_allocation_classes(spa, tx);
3133 }
3134 }
3135
3136 void
3137 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
3138 {
3139 spa_t *spa = vd->vdev_spa;
3140
3141 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
3142 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
3143 zapobj, tx));
3144 }
3145
3146 uint64_t
3147 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
3148 {
3149 spa_t *spa = vd->vdev_spa;
3150 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
3151 DMU_OT_NONE, 0, tx);
3152
3153 ASSERT(zap != 0);
3154 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
3155 zap, tx));
3156
3157 return (zap);
3158 }
3159
3160 void
3161 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
3162 {
3163 if (vd->vdev_ops != &vdev_hole_ops &&
3164 vd->vdev_ops != &vdev_missing_ops &&
3165 vd->vdev_ops != &vdev_root_ops &&
3166 !vd->vdev_top->vdev_removing) {
3167 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
3168 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
3169 }
3170 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
3171 vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
3172 if (vd->vdev_alloc_bias != VDEV_BIAS_NONE)
3173 vdev_zap_allocation_data(vd, tx);
3174 }
3175 }
3176
3177 for (uint64_t i = 0; i < vd->vdev_children; i++) {
3178 vdev_construct_zaps(vd->vdev_child[i], tx);
3179 }
3180 }
3181
3182 static void
3183 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
3184 {
3185 spa_t *spa = vd->vdev_spa;
3186 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
3187 objset_t *mos = spa->spa_meta_objset;
3188 range_tree_t *rtsync;
3189 dmu_tx_t *tx;
3190 uint64_t object = space_map_object(vd->vdev_dtl_sm);
3191
3192 ASSERT(vdev_is_concrete(vd));
3193 ASSERT(vd->vdev_ops->vdev_op_leaf);
3194
3195 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3196
3197 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
3198 mutex_enter(&vd->vdev_dtl_lock);
3199 space_map_free(vd->vdev_dtl_sm, tx);
3200 space_map_close(vd->vdev_dtl_sm);
3201 vd->vdev_dtl_sm = NULL;
3202 mutex_exit(&vd->vdev_dtl_lock);
3203
3204 /*
3205 * We only destroy the leaf ZAP for detached leaves or for
3206 * removed log devices. Removed data devices handle leaf ZAP
3207 * cleanup later, once cancellation is no longer possible.
3208 */
3209 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
3210 vd->vdev_top->vdev_islog)) {
3211 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
3212 vd->vdev_leaf_zap = 0;
3213 }
3214
3215 dmu_tx_commit(tx);
3216 return;
3217 }
3218
3219 if (vd->vdev_dtl_sm == NULL) {
3220 uint64_t new_object;
3221
3222 new_object = space_map_alloc(mos, zfs_vdev_dtl_sm_blksz, tx);
3223 VERIFY3U(new_object, !=, 0);
3224
3225 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
3226 0, -1ULL, 0));
3227 ASSERT(vd->vdev_dtl_sm != NULL);
3228 }
3229
3230 rtsync = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
3231
3232 mutex_enter(&vd->vdev_dtl_lock);
3233 range_tree_walk(rt, range_tree_add, rtsync);
3234 mutex_exit(&vd->vdev_dtl_lock);
3235
3236 space_map_truncate(vd->vdev_dtl_sm, zfs_vdev_dtl_sm_blksz, tx);
3237 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx);
3238 range_tree_vacate(rtsync, NULL, NULL);
3239
3240 range_tree_destroy(rtsync);
3241
3242 /*
3243 * If the object for the space map has changed then dirty
3244 * the top level so that we update the config.
3245 */
3246 if (object != space_map_object(vd->vdev_dtl_sm)) {
3247 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
3248 "new object %llu", (u_longlong_t)txg, spa_name(spa),
3249 (u_longlong_t)object,
3250 (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
3251 vdev_config_dirty(vd->vdev_top);
3252 }
3253
3254 dmu_tx_commit(tx);
3255 }
3256
3257 /*
3258 * Determine whether the specified vdev can be offlined/detached/removed
3259 * without losing data.
3260 */
3261 boolean_t
3262 vdev_dtl_required(vdev_t *vd)
3263 {
3264 spa_t *spa = vd->vdev_spa;
3265 vdev_t *tvd = vd->vdev_top;
3266 uint8_t cant_read = vd->vdev_cant_read;
3267 boolean_t required;
3268
3269 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3270
3271 if (vd == spa->spa_root_vdev || vd == tvd)
3272 return (B_TRUE);
3273
3274 /*
3275 * Temporarily mark the device as unreadable, and then determine
3276 * whether this results in any DTL outages in the top-level vdev.
3277 * If not, we can safely offline/detach/remove the device.
3278 */
3279 vd->vdev_cant_read = B_TRUE;
3280 vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE);
3281 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
3282 vd->vdev_cant_read = cant_read;
3283 vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE);
3284
3285 if (!required && zio_injection_enabled) {
3286 required = !!zio_handle_device_injection(vd, NULL,
3287 SET_ERROR(ECHILD));
3288 }
3289
3290 return (required);
3291 }
3292
3293 /*
3294 * Determine if resilver is needed, and if so the txg range.
3295 */
3296 boolean_t
3297 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
3298 {
3299 boolean_t needed = B_FALSE;
3300 uint64_t thismin = UINT64_MAX;
3301 uint64_t thismax = 0;
3302
3303 if (vd->vdev_children == 0) {
3304 mutex_enter(&vd->vdev_dtl_lock);
3305 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
3306 vdev_writeable(vd)) {
3307
3308 thismin = vdev_dtl_min(vd);
3309 thismax = vdev_dtl_max(vd);
3310 needed = B_TRUE;
3311 }
3312 mutex_exit(&vd->vdev_dtl_lock);
3313 } else {
3314 for (int c = 0; c < vd->vdev_children; c++) {
3315 vdev_t *cvd = vd->vdev_child[c];
3316 uint64_t cmin, cmax;
3317
3318 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
3319 thismin = MIN(thismin, cmin);
3320 thismax = MAX(thismax, cmax);
3321 needed = B_TRUE;
3322 }
3323 }
3324 }
3325
3326 if (needed && minp) {
3327 *minp = thismin;
3328 *maxp = thismax;
3329 }
3330 return (needed);
3331 }
3332
3333 /*
3334 * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj
3335 * will contain either the checkpoint spacemap object or zero if none exists.
3336 * All other errors are returned to the caller.
3337 */
3338 int
3339 vdev_checkpoint_sm_object(vdev_t *vd, uint64_t *sm_obj)
3340 {
3341 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
3342
3343 if (vd->vdev_top_zap == 0) {
3344 *sm_obj = 0;
3345 return (0);
3346 }
3347
3348 int error = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap,
3349 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, sm_obj);
3350 if (error == ENOENT) {
3351 *sm_obj = 0;
3352 error = 0;
3353 }
3354
3355 return (error);
3356 }
3357
3358 int
3359 vdev_load(vdev_t *vd)
3360 {
3361 int children = vd->vdev_children;
3362 int error = 0;
3363 taskq_t *tq = NULL;
3364
3365 /*
3366 * It's only worthwhile to use the taskq for the root vdev, because the
3367 * slow part is metaslab_init, and that only happens for top-level
3368 * vdevs.
3369 */
3370 if (vd->vdev_ops == &vdev_root_ops && vd->vdev_children > 0) {
3371 tq = taskq_create("vdev_load", children, minclsyspri,
3372 children, children, TASKQ_PREPOPULATE);
3373 }
3374
3375 /*
3376 * Recursively load all children.
3377 */
3378 for (int c = 0; c < vd->vdev_children; c++) {
3379 vdev_t *cvd = vd->vdev_child[c];
3380
3381 if (tq == NULL || vdev_uses_zvols(cvd)) {
3382 cvd->vdev_load_error = vdev_load(cvd);
3383 } else {
3384 VERIFY(taskq_dispatch(tq, vdev_load_child,
3385 cvd, TQ_SLEEP) != TASKQID_INVALID);
3386 }
3387 }
3388
3389 if (tq != NULL) {
3390 taskq_wait(tq);
3391 taskq_destroy(tq);
3392 }
3393
3394 for (int c = 0; c < vd->vdev_children; c++) {
3395 int error = vd->vdev_child[c]->vdev_load_error;
3396
3397 if (error != 0)
3398 return (error);
3399 }
3400
3401 vdev_set_deflate_ratio(vd);
3402
3403 /*
3404 * On spa_load path, grab the allocation bias from our zap
3405 */
3406 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3407 spa_t *spa = vd->vdev_spa;
3408 char bias_str[64];
3409
3410 error = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
3411 VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str),
3412 bias_str);
3413 if (error == 0) {
3414 ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE);
3415 vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str);
3416 } else if (error != ENOENT) {
3417 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3418 VDEV_AUX_CORRUPT_DATA);
3419 vdev_dbgmsg(vd, "vdev_load: zap_lookup(top_zap=%llu) "
3420 "failed [error=%d]", vd->vdev_top_zap, error);
3421 return (error);
3422 }
3423 }
3424
3425 /*
3426 * Load any rebuild state from the top-level vdev zap.
3427 */
3428 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3429 error = vdev_rebuild_load(vd);
3430 if (error && error != ENOTSUP) {
3431 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3432 VDEV_AUX_CORRUPT_DATA);
3433 vdev_dbgmsg(vd, "vdev_load: vdev_rebuild_load "
3434 "failed [error=%d]", error);
3435 return (error);
3436 }
3437 }
3438
3439 /*
3440 * If this is a top-level vdev, initialize its metaslabs.
3441 */
3442 if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
3443 vdev_metaslab_group_create(vd);
3444
3445 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
3446 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3447 VDEV_AUX_CORRUPT_DATA);
3448 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
3449 "asize=%llu", (u_longlong_t)vd->vdev_ashift,
3450 (u_longlong_t)vd->vdev_asize);
3451 return (SET_ERROR(ENXIO));
3452 }
3453
3454 error = vdev_metaslab_init(vd, 0);
3455 if (error != 0) {
3456 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
3457 "[error=%d]", error);
3458 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3459 VDEV_AUX_CORRUPT_DATA);
3460 return (error);
3461 }
3462
3463 uint64_t checkpoint_sm_obj;
3464 error = vdev_checkpoint_sm_object(vd, &checkpoint_sm_obj);
3465 if (error == 0 && checkpoint_sm_obj != 0) {
3466 objset_t *mos = spa_meta_objset(vd->vdev_spa);
3467 ASSERT(vd->vdev_asize != 0);
3468 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL);
3469
3470 error = space_map_open(&vd->vdev_checkpoint_sm,
3471 mos, checkpoint_sm_obj, 0, vd->vdev_asize,
3472 vd->vdev_ashift);
3473 if (error != 0) {
3474 vdev_dbgmsg(vd, "vdev_load: space_map_open "
3475 "failed for checkpoint spacemap (obj %llu) "
3476 "[error=%d]",
3477 (u_longlong_t)checkpoint_sm_obj, error);
3478 return (error);
3479 }
3480 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
3481
3482 /*
3483 * Since the checkpoint_sm contains free entries
3484 * exclusively we can use space_map_allocated() to
3485 * indicate the cumulative checkpointed space that
3486 * has been freed.
3487 */
3488 vd->vdev_stat.vs_checkpoint_space =
3489 -space_map_allocated(vd->vdev_checkpoint_sm);
3490 vd->vdev_spa->spa_checkpoint_info.sci_dspace +=
3491 vd->vdev_stat.vs_checkpoint_space;
3492 } else if (error != 0) {
3493 vdev_dbgmsg(vd, "vdev_load: failed to retrieve "
3494 "checkpoint space map object from vdev ZAP "
3495 "[error=%d]", error);
3496 return (error);
3497 }
3498 }
3499
3500 /*
3501 * If this is a leaf vdev, load its DTL.
3502 */
3503 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
3504 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3505 VDEV_AUX_CORRUPT_DATA);
3506 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
3507 "[error=%d]", error);
3508 return (error);
3509 }
3510
3511 uint64_t obsolete_sm_object;
3512 error = vdev_obsolete_sm_object(vd, &obsolete_sm_object);
3513 if (error == 0 && obsolete_sm_object != 0) {
3514 objset_t *mos = vd->vdev_spa->spa_meta_objset;
3515 ASSERT(vd->vdev_asize != 0);
3516 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
3517
3518 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
3519 obsolete_sm_object, 0, vd->vdev_asize, 0))) {
3520 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3521 VDEV_AUX_CORRUPT_DATA);
3522 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
3523 "obsolete spacemap (obj %llu) [error=%d]",
3524 (u_longlong_t)obsolete_sm_object, error);
3525 return (error);
3526 }
3527 } else if (error != 0) {
3528 vdev_dbgmsg(vd, "vdev_load: failed to retrieve obsolete "
3529 "space map object from vdev ZAP [error=%d]", error);
3530 return (error);
3531 }
3532
3533 return (0);
3534 }
3535
3536 /*
3537 * The special vdev case is used for hot spares and l2cache devices. Its
3538 * sole purpose it to set the vdev state for the associated vdev. To do this,
3539 * we make sure that we can open the underlying device, then try to read the
3540 * label, and make sure that the label is sane and that it hasn't been
3541 * repurposed to another pool.
3542 */
3543 int
3544 vdev_validate_aux(vdev_t *vd)
3545 {
3546 nvlist_t *label;
3547 uint64_t guid, version;
3548 uint64_t state;
3549
3550 if (!vdev_readable(vd))
3551 return (0);
3552
3553 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
3554 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3555 VDEV_AUX_CORRUPT_DATA);
3556 return (-1);
3557 }
3558
3559 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
3560 !SPA_VERSION_IS_SUPPORTED(version) ||
3561 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
3562 guid != vd->vdev_guid ||
3563 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
3564 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3565 VDEV_AUX_CORRUPT_DATA);
3566 nvlist_free(label);
3567 return (-1);
3568 }
3569
3570 /*
3571 * We don't actually check the pool state here. If it's in fact in
3572 * use by another pool, we update this fact on the fly when requested.
3573 */
3574 nvlist_free(label);
3575 return (0);
3576 }
3577
3578 static void
3579 vdev_destroy_ms_flush_data(vdev_t *vd, dmu_tx_t *tx)
3580 {
3581 objset_t *mos = spa_meta_objset(vd->vdev_spa);
3582
3583 if (vd->vdev_top_zap == 0)
3584 return;
3585
3586 uint64_t object = 0;
3587 int err = zap_lookup(mos, vd->vdev_top_zap,
3588 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, sizeof (uint64_t), 1, &object);
3589 if (err == ENOENT)
3590 return;
3591 VERIFY0(err);
3592
3593 VERIFY0(dmu_object_free(mos, object, tx));
3594 VERIFY0(zap_remove(mos, vd->vdev_top_zap,
3595 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, tx));
3596 }
3597
3598 /*
3599 * Free the objects used to store this vdev's spacemaps, and the array
3600 * that points to them.
3601 */
3602 void
3603 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
3604 {
3605 if (vd->vdev_ms_array == 0)
3606 return;
3607
3608 objset_t *mos = vd->vdev_spa->spa_meta_objset;
3609 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
3610 size_t array_bytes = array_count * sizeof (uint64_t);
3611 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
3612 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
3613 array_bytes, smobj_array, 0));
3614
3615 for (uint64_t i = 0; i < array_count; i++) {
3616 uint64_t smobj = smobj_array[i];
3617 if (smobj == 0)
3618 continue;
3619
3620 space_map_free_obj(mos, smobj, tx);
3621 }
3622
3623 kmem_free(smobj_array, array_bytes);
3624 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
3625 vdev_destroy_ms_flush_data(vd, tx);
3626 vd->vdev_ms_array = 0;
3627 }
3628
3629 static void
3630 vdev_remove_empty_log(vdev_t *vd, uint64_t txg)
3631 {
3632 spa_t *spa = vd->vdev_spa;
3633
3634 ASSERT(vd->vdev_islog);
3635 ASSERT(vd == vd->vdev_top);
3636 ASSERT3U(txg, ==, spa_syncing_txg(spa));
3637
3638 dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
3639
3640 vdev_destroy_spacemaps(vd, tx);
3641 if (vd->vdev_top_zap != 0) {
3642 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
3643 vd->vdev_top_zap = 0;
3644 }
3645
3646 dmu_tx_commit(tx);
3647 }
3648
3649 void
3650 vdev_sync_done(vdev_t *vd, uint64_t txg)
3651 {
3652 metaslab_t *msp;
3653 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
3654
3655 ASSERT(vdev_is_concrete(vd));
3656
3657 while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
3658 != NULL)
3659 metaslab_sync_done(msp, txg);
3660
3661 if (reassess) {
3662 metaslab_sync_reassess(vd->vdev_mg);
3663 if (vd->vdev_log_mg != NULL)
3664 metaslab_sync_reassess(vd->vdev_log_mg);
3665 }
3666 }
3667
3668 void
3669 vdev_sync(vdev_t *vd, uint64_t txg)
3670 {
3671 spa_t *spa = vd->vdev_spa;
3672 vdev_t *lvd;
3673 metaslab_t *msp;
3674
3675 ASSERT3U(txg, ==, spa->spa_syncing_txg);
3676 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3677 if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
3678 ASSERT(vd->vdev_removing ||
3679 vd->vdev_ops == &vdev_indirect_ops);
3680
3681 vdev_indirect_sync_obsolete(vd, tx);
3682
3683 /*
3684 * If the vdev is indirect, it can't have dirty
3685 * metaslabs or DTLs.
3686 */
3687 if (vd->vdev_ops == &vdev_indirect_ops) {
3688 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
3689 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
3690 dmu_tx_commit(tx);
3691 return;
3692 }
3693 }
3694
3695 ASSERT(vdev_is_concrete(vd));
3696
3697 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
3698 !vd->vdev_removing) {
3699 ASSERT(vd == vd->vdev_top);
3700 ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
3701 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
3702 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
3703 ASSERT(vd->vdev_ms_array != 0);
3704 vdev_config_dirty(vd);
3705 }
3706
3707 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
3708 metaslab_sync(msp, txg);
3709 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
3710 }
3711
3712 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
3713 vdev_dtl_sync(lvd, txg);
3714
3715 /*
3716 * If this is an empty log device being removed, destroy the
3717 * metadata associated with it.
3718 */
3719 if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
3720 vdev_remove_empty_log(vd, txg);
3721
3722 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
3723 dmu_tx_commit(tx);
3724 }
3725
3726 uint64_t
3727 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
3728 {
3729 return (vd->vdev_ops->vdev_op_asize(vd, psize));
3730 }
3731
3732 /*
3733 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
3734 * not be opened, and no I/O is attempted.
3735 */
3736 int
3737 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
3738 {
3739 vdev_t *vd, *tvd;
3740
3741 spa_vdev_state_enter(spa, SCL_NONE);
3742
3743 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3744 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
3745
3746 if (!vd->vdev_ops->vdev_op_leaf)
3747 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
3748
3749 tvd = vd->vdev_top;
3750
3751 /*
3752 * If user did a 'zpool offline -f' then make the fault persist across
3753 * reboots.
3754 */
3755 if (aux == VDEV_AUX_EXTERNAL_PERSIST) {
3756 /*
3757 * There are two kinds of forced faults: temporary and
3758 * persistent. Temporary faults go away at pool import, while
3759 * persistent faults stay set. Both types of faults can be
3760 * cleared with a zpool clear.
3761 *
3762 * We tell if a vdev is persistently faulted by looking at the
3763 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
3764 * import then it's a persistent fault. Otherwise, it's
3765 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
3766 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
3767 * tells vdev_config_generate() (which gets run later) to set
3768 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
3769 */
3770 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL;
3771 vd->vdev_tmpoffline = B_FALSE;
3772 aux = VDEV_AUX_EXTERNAL;
3773 } else {
3774 vd->vdev_tmpoffline = B_TRUE;
3775 }
3776
3777 /*
3778 * We don't directly use the aux state here, but if we do a
3779 * vdev_reopen(), we need this value to be present to remember why we
3780 * were faulted.
3781 */
3782 vd->vdev_label_aux = aux;
3783
3784 /*
3785 * Faulted state takes precedence over degraded.
3786 */
3787 vd->vdev_delayed_close = B_FALSE;
3788 vd->vdev_faulted = 1ULL;
3789 vd->vdev_degraded = 0ULL;
3790 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
3791
3792 /*
3793 * If this device has the only valid copy of the data, then
3794 * back off and simply mark the vdev as degraded instead.
3795 */
3796 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
3797 vd->vdev_degraded = 1ULL;
3798 vd->vdev_faulted = 0ULL;
3799
3800 /*
3801 * If we reopen the device and it's not dead, only then do we
3802 * mark it degraded.
3803 */
3804 vdev_reopen(tvd);
3805
3806 if (vdev_readable(vd))
3807 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
3808 }
3809
3810 return (spa_vdev_state_exit(spa, vd, 0));
3811 }
3812
3813 /*
3814 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
3815 * user that something is wrong. The vdev continues to operate as normal as far
3816 * as I/O is concerned.
3817 */
3818 int
3819 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
3820 {
3821 vdev_t *vd;
3822
3823 spa_vdev_state_enter(spa, SCL_NONE);
3824
3825 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3826 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
3827
3828 if (!vd->vdev_ops->vdev_op_leaf)
3829 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
3830
3831 /*
3832 * If the vdev is already faulted, then don't do anything.
3833 */
3834 if (vd->vdev_faulted || vd->vdev_degraded)
3835 return (spa_vdev_state_exit(spa, NULL, 0));
3836
3837 vd->vdev_degraded = 1ULL;
3838 if (!vdev_is_dead(vd))
3839 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
3840 aux);
3841
3842 return (spa_vdev_state_exit(spa, vd, 0));
3843 }
3844
3845 /*
3846 * Online the given vdev.
3847 *
3848 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
3849 * spare device should be detached when the device finishes resilvering.
3850 * Second, the online should be treated like a 'test' online case, so no FMA
3851 * events are generated if the device fails to open.
3852 */
3853 int
3854 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
3855 {
3856 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
3857 boolean_t wasoffline;
3858 vdev_state_t oldstate;
3859
3860 spa_vdev_state_enter(spa, SCL_NONE);
3861
3862 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3863 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
3864
3865 if (!vd->vdev_ops->vdev_op_leaf)
3866 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
3867
3868 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
3869 oldstate = vd->vdev_state;
3870
3871 tvd = vd->vdev_top;
3872 vd->vdev_offline = B_FALSE;
3873 vd->vdev_tmpoffline = B_FALSE;
3874 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
3875 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
3876
3877 /* XXX - L2ARC 1.0 does not support expansion */
3878 if (!vd->vdev_aux) {
3879 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3880 pvd->vdev_expanding = !!((flags & ZFS_ONLINE_EXPAND) ||
3881 spa->spa_autoexpand);
3882 vd->vdev_expansion_time = gethrestime_sec();
3883 }
3884
3885 vdev_reopen(tvd);
3886 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
3887
3888 if (!vd->vdev_aux) {
3889 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3890 pvd->vdev_expanding = B_FALSE;
3891 }
3892
3893 if (newstate)
3894 *newstate = vd->vdev_state;
3895 if ((flags & ZFS_ONLINE_UNSPARE) &&
3896 !vdev_is_dead(vd) && vd->vdev_parent &&
3897 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3898 vd->vdev_parent->vdev_child[0] == vd)
3899 vd->vdev_unspare = B_TRUE;
3900
3901 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
3902
3903 /* XXX - L2ARC 1.0 does not support expansion */
3904 if (vd->vdev_aux)
3905 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
3906 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
3907 }
3908
3909 /* Restart initializing if necessary */
3910 mutex_enter(&vd->vdev_initialize_lock);
3911 if (vdev_writeable(vd) &&
3912 vd->vdev_initialize_thread == NULL &&
3913 vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) {
3914 (void) vdev_initialize(vd);
3915 }
3916 mutex_exit(&vd->vdev_initialize_lock);
3917
3918 /*
3919 * Restart trimming if necessary. We do not restart trimming for cache
3920 * devices here. This is triggered by l2arc_rebuild_vdev()
3921 * asynchronously for the whole device or in l2arc_evict() as it evicts
3922 * space for upcoming writes.
3923 */
3924 mutex_enter(&vd->vdev_trim_lock);
3925 if (vdev_writeable(vd) && !vd->vdev_isl2cache &&
3926 vd->vdev_trim_thread == NULL &&
3927 vd->vdev_trim_state == VDEV_TRIM_ACTIVE) {
3928 (void) vdev_trim(vd, vd->vdev_trim_rate, vd->vdev_trim_partial,
3929 vd->vdev_trim_secure);
3930 }
3931 mutex_exit(&vd->vdev_trim_lock);
3932
3933 if (wasoffline ||
3934 (oldstate < VDEV_STATE_DEGRADED &&
3935 vd->vdev_state >= VDEV_STATE_DEGRADED))
3936 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
3937
3938 return (spa_vdev_state_exit(spa, vd, 0));
3939 }
3940
3941 static int
3942 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
3943 {
3944 vdev_t *vd, *tvd;
3945 int error = 0;
3946 uint64_t generation;
3947 metaslab_group_t *mg;
3948
3949 top:
3950 spa_vdev_state_enter(spa, SCL_ALLOC);
3951
3952 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3953 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
3954
3955 if (!vd->vdev_ops->vdev_op_leaf)
3956 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
3957
3958 if (vd->vdev_ops == &vdev_draid_spare_ops)
3959 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3960
3961 tvd = vd->vdev_top;
3962 mg = tvd->vdev_mg;
3963 generation = spa->spa_config_generation + 1;
3964
3965 /*
3966 * If the device isn't already offline, try to offline it.
3967 */
3968 if (!vd->vdev_offline) {
3969 /*
3970 * If this device has the only valid copy of some data,
3971 * don't allow it to be offlined. Log devices are always
3972 * expendable.
3973 */
3974 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3975 vdev_dtl_required(vd))
3976 return (spa_vdev_state_exit(spa, NULL,
3977 SET_ERROR(EBUSY)));
3978
3979 /*
3980 * If the top-level is a slog and it has had allocations
3981 * then proceed. We check that the vdev's metaslab group
3982 * is not NULL since it's possible that we may have just
3983 * added this vdev but not yet initialized its metaslabs.
3984 */
3985 if (tvd->vdev_islog && mg != NULL) {
3986 /*
3987 * Prevent any future allocations.
3988 */
3989 ASSERT3P(tvd->vdev_log_mg, ==, NULL);
3990 metaslab_group_passivate(mg);
3991 (void) spa_vdev_state_exit(spa, vd, 0);
3992
3993 error = spa_reset_logs(spa);
3994
3995 /*
3996 * If the log device was successfully reset but has
3997 * checkpointed data, do not offline it.
3998 */
3999 if (error == 0 &&
4000 tvd->vdev_checkpoint_sm != NULL) {
4001 ASSERT3U(space_map_allocated(
4002 tvd->vdev_checkpoint_sm), !=, 0);
4003 error = ZFS_ERR_CHECKPOINT_EXISTS;
4004 }
4005
4006 spa_vdev_state_enter(spa, SCL_ALLOC);
4007
4008 /*
4009 * Check to see if the config has changed.
4010 */
4011 if (error || generation != spa->spa_config_generation) {
4012 metaslab_group_activate(mg);
4013 if (error)
4014 return (spa_vdev_state_exit(spa,
4015 vd, error));
4016 (void) spa_vdev_state_exit(spa, vd, 0);
4017 goto top;
4018 }
4019 ASSERT0(tvd->vdev_stat.vs_alloc);
4020 }
4021
4022 /*
4023 * Offline this device and reopen its top-level vdev.
4024 * If the top-level vdev is a log device then just offline
4025 * it. Otherwise, if this action results in the top-level
4026 * vdev becoming unusable, undo it and fail the request.
4027 */
4028 vd->vdev_offline = B_TRUE;
4029 vdev_reopen(tvd);
4030
4031 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
4032 vdev_is_dead(tvd)) {
4033 vd->vdev_offline = B_FALSE;
4034 vdev_reopen(tvd);
4035 return (spa_vdev_state_exit(spa, NULL,
4036 SET_ERROR(EBUSY)));
4037 }
4038
4039 /*
4040 * Add the device back into the metaslab rotor so that
4041 * once we online the device it's open for business.
4042 */
4043 if (tvd->vdev_islog && mg != NULL)
4044 metaslab_group_activate(mg);
4045 }
4046
4047 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
4048
4049 return (spa_vdev_state_exit(spa, vd, 0));
4050 }
4051
4052 int
4053 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
4054 {
4055 int error;
4056
4057 mutex_enter(&spa->spa_vdev_top_lock);
4058 error = vdev_offline_locked(spa, guid, flags);
4059 mutex_exit(&spa->spa_vdev_top_lock);
4060
4061 return (error);
4062 }
4063
4064 /*
4065 * Clear the error counts associated with this vdev. Unlike vdev_online() and
4066 * vdev_offline(), we assume the spa config is locked. We also clear all
4067 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
4068 */
4069 void
4070 vdev_clear(spa_t *spa, vdev_t *vd)
4071 {
4072 vdev_t *rvd = spa->spa_root_vdev;
4073
4074 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
4075
4076 if (vd == NULL)
4077 vd = rvd;
4078
4079 vd->vdev_stat.vs_read_errors = 0;
4080 vd->vdev_stat.vs_write_errors = 0;
4081 vd->vdev_stat.vs_checksum_errors = 0;
4082 vd->vdev_stat.vs_slow_ios = 0;
4083
4084 for (int c = 0; c < vd->vdev_children; c++)
4085 vdev_clear(spa, vd->vdev_child[c]);
4086
4087 /*
4088 * It makes no sense to "clear" an indirect vdev.
4089 */
4090 if (!vdev_is_concrete(vd))
4091 return;
4092
4093 /*
4094 * If we're in the FAULTED state or have experienced failed I/O, then
4095 * clear the persistent state and attempt to reopen the device. We
4096 * also mark the vdev config dirty, so that the new faulted state is
4097 * written out to disk.
4098 */
4099 if (vd->vdev_faulted || vd->vdev_degraded ||
4100 !vdev_readable(vd) || !vdev_writeable(vd)) {
4101 /*
4102 * When reopening in response to a clear event, it may be due to
4103 * a fmadm repair request. In this case, if the device is
4104 * still broken, we want to still post the ereport again.
4105 */
4106 vd->vdev_forcefault = B_TRUE;
4107
4108 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
4109 vd->vdev_cant_read = B_FALSE;
4110 vd->vdev_cant_write = B_FALSE;
4111 vd->vdev_stat.vs_aux = 0;
4112
4113 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
4114
4115 vd->vdev_forcefault = B_FALSE;
4116
4117 if (vd != rvd && vdev_writeable(vd->vdev_top))
4118 vdev_state_dirty(vd->vdev_top);
4119
4120 /* If a resilver isn't required, check if vdevs can be culled */
4121 if (vd->vdev_aux == NULL && !vdev_is_dead(vd) &&
4122 !dsl_scan_resilvering(spa->spa_dsl_pool) &&
4123 !dsl_scan_resilver_scheduled(spa->spa_dsl_pool))
4124 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
4125
4126 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
4127 }
4128
4129 /*
4130 * When clearing a FMA-diagnosed fault, we always want to
4131 * unspare the device, as we assume that the original spare was
4132 * done in response to the FMA fault.
4133 */
4134 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
4135 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4136 vd->vdev_parent->vdev_child[0] == vd)
4137 vd->vdev_unspare = B_TRUE;
4138 }
4139
4140 boolean_t
4141 vdev_is_dead(vdev_t *vd)
4142 {
4143 /*
4144 * Holes and missing devices are always considered "dead".
4145 * This simplifies the code since we don't have to check for
4146 * these types of devices in the various code paths.
4147 * Instead we rely on the fact that we skip over dead devices
4148 * before issuing I/O to them.
4149 */
4150 return (vd->vdev_state < VDEV_STATE_DEGRADED ||
4151 vd->vdev_ops == &vdev_hole_ops ||
4152 vd->vdev_ops == &vdev_missing_ops);
4153 }
4154
4155 boolean_t
4156 vdev_readable(vdev_t *vd)
4157 {
4158 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
4159 }
4160
4161 boolean_t
4162 vdev_writeable(vdev_t *vd)
4163 {
4164 return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
4165 vdev_is_concrete(vd));
4166 }
4167
4168 boolean_t
4169 vdev_allocatable(vdev_t *vd)
4170 {
4171 uint64_t state = vd->vdev_state;
4172
4173 /*
4174 * We currently allow allocations from vdevs which may be in the
4175 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
4176 * fails to reopen then we'll catch it later when we're holding
4177 * the proper locks. Note that we have to get the vdev state
4178 * in a local variable because although it changes atomically,
4179 * we're asking two separate questions about it.
4180 */
4181 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
4182 !vd->vdev_cant_write && vdev_is_concrete(vd) &&
4183 vd->vdev_mg->mg_initialized);
4184 }
4185
4186 boolean_t
4187 vdev_accessible(vdev_t *vd, zio_t *zio)
4188 {
4189 ASSERT(zio->io_vd == vd);
4190
4191 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
4192 return (B_FALSE);
4193
4194 if (zio->io_type == ZIO_TYPE_READ)
4195 return (!vd->vdev_cant_read);
4196
4197 if (zio->io_type == ZIO_TYPE_WRITE)
4198 return (!vd->vdev_cant_write);
4199
4200 return (B_TRUE);
4201 }
4202
4203 static void
4204 vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs)
4205 {
4206 /*
4207 * Exclude the dRAID spare when aggregating to avoid double counting
4208 * the ops and bytes. These IOs are counted by the physical leaves.
4209 */
4210 if (cvd->vdev_ops == &vdev_draid_spare_ops)
4211 return;
4212
4213 for (int t = 0; t < VS_ZIO_TYPES; t++) {
4214 vs->vs_ops[t] += cvs->vs_ops[t];
4215 vs->vs_bytes[t] += cvs->vs_bytes[t];
4216 }
4217
4218 cvs->vs_scan_removing = cvd->vdev_removing;
4219 }
4220
4221 /*
4222 * Get extended stats
4223 */
4224 static void
4225 vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx)
4226 {
4227 int t, b;
4228 for (t = 0; t < ZIO_TYPES; t++) {
4229 for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++)
4230 vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b];
4231
4232 for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) {
4233 vsx->vsx_total_histo[t][b] +=
4234 cvsx->vsx_total_histo[t][b];
4235 }
4236 }
4237
4238 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
4239 for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) {
4240 vsx->vsx_queue_histo[t][b] +=
4241 cvsx->vsx_queue_histo[t][b];
4242 }
4243 vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t];
4244 vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t];
4245
4246 for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++)
4247 vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b];
4248
4249 for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++)
4250 vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b];
4251 }
4252
4253 }
4254
4255 boolean_t
4256 vdev_is_spacemap_addressable(vdev_t *vd)
4257 {
4258 if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2))
4259 return (B_TRUE);
4260
4261 /*
4262 * If double-word space map entries are not enabled we assume
4263 * 47 bits of the space map entry are dedicated to the entry's
4264 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
4265 * to calculate the maximum address that can be described by a
4266 * space map entry for the given device.
4267 */
4268 uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS;
4269
4270 if (shift >= 63) /* detect potential overflow */
4271 return (B_TRUE);
4272
4273 return (vd->vdev_asize < (1ULL << shift));
4274 }
4275
4276 /*
4277 * Get statistics for the given vdev.
4278 */
4279 static void
4280 vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
4281 {
4282 int t;
4283 /*
4284 * If we're getting stats on the root vdev, aggregate the I/O counts
4285 * over all top-level vdevs (i.e. the direct children of the root).
4286 */
4287 if (!vd->vdev_ops->vdev_op_leaf) {
4288 if (vs) {
4289 memset(vs->vs_ops, 0, sizeof (vs->vs_ops));
4290 memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes));
4291 }
4292 if (vsx)
4293 memset(vsx, 0, sizeof (*vsx));
4294
4295 for (int c = 0; c < vd->vdev_children; c++) {
4296 vdev_t *cvd = vd->vdev_child[c];
4297 vdev_stat_t *cvs = &cvd->vdev_stat;
4298 vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex;
4299
4300 vdev_get_stats_ex_impl(cvd, cvs, cvsx);
4301 if (vs)
4302 vdev_get_child_stat(cvd, vs, cvs);
4303 if (vsx)
4304 vdev_get_child_stat_ex(cvd, vsx, cvsx);
4305 }
4306 } else {
4307 /*
4308 * We're a leaf. Just copy our ZIO active queue stats in. The
4309 * other leaf stats are updated in vdev_stat_update().
4310 */
4311 if (!vsx)
4312 return;
4313
4314 memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex));
4315
4316 for (t = 0; t < ARRAY_SIZE(vd->vdev_queue.vq_class); t++) {
4317 vsx->vsx_active_queue[t] =
4318 vd->vdev_queue.vq_class[t].vqc_active;
4319 vsx->vsx_pend_queue[t] = avl_numnodes(
4320 &vd->vdev_queue.vq_class[t].vqc_queued_tree);
4321 }
4322 }
4323 }
4324
4325 void
4326 vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
4327 {
4328 vdev_t *tvd = vd->vdev_top;
4329 mutex_enter(&vd->vdev_stat_lock);
4330 if (vs) {
4331 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
4332 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
4333 vs->vs_state = vd->vdev_state;
4334 vs->vs_rsize = vdev_get_min_asize(vd);
4335
4336 if (vd->vdev_ops->vdev_op_leaf) {
4337 vs->vs_rsize += VDEV_LABEL_START_SIZE +
4338 VDEV_LABEL_END_SIZE;
4339 /*
4340 * Report initializing progress. Since we don't
4341 * have the initializing locks held, this is only
4342 * an estimate (although a fairly accurate one).
4343 */
4344 vs->vs_initialize_bytes_done =
4345 vd->vdev_initialize_bytes_done;
4346 vs->vs_initialize_bytes_est =
4347 vd->vdev_initialize_bytes_est;
4348 vs->vs_initialize_state = vd->vdev_initialize_state;
4349 vs->vs_initialize_action_time =
4350 vd->vdev_initialize_action_time;
4351
4352 /*
4353 * Report manual TRIM progress. Since we don't have
4354 * the manual TRIM locks held, this is only an
4355 * estimate (although fairly accurate one).
4356 */
4357 vs->vs_trim_notsup = !vd->vdev_has_trim;
4358 vs->vs_trim_bytes_done = vd->vdev_trim_bytes_done;
4359 vs->vs_trim_bytes_est = vd->vdev_trim_bytes_est;
4360 vs->vs_trim_state = vd->vdev_trim_state;
4361 vs->vs_trim_action_time = vd->vdev_trim_action_time;
4362
4363 /* Set when there is a deferred resilver. */
4364 vs->vs_resilver_deferred = vd->vdev_resilver_deferred;
4365 }
4366
4367 /*
4368 * Report expandable space on top-level, non-auxiliary devices
4369 * only. The expandable space is reported in terms of metaslab
4370 * sized units since that determines how much space the pool
4371 * can expand.
4372 */
4373 if (vd->vdev_aux == NULL && tvd != NULL) {
4374 vs->vs_esize = P2ALIGN(
4375 vd->vdev_max_asize - vd->vdev_asize,
4376 1ULL << tvd->vdev_ms_shift);
4377 }
4378
4379 vs->vs_configured_ashift = vd->vdev_top != NULL
4380 ? vd->vdev_top->vdev_ashift : vd->vdev_ashift;
4381 vs->vs_logical_ashift = vd->vdev_logical_ashift;
4382 vs->vs_physical_ashift = vd->vdev_physical_ashift;
4383
4384 /*
4385 * Report fragmentation and rebuild progress for top-level,
4386 * non-auxiliary, concrete devices.
4387 */
4388 if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
4389 vdev_is_concrete(vd)) {
4390 /*
4391 * The vdev fragmentation rating doesn't take into
4392 * account the embedded slog metaslab (vdev_log_mg).
4393 * Since it's only one metaslab, it would have a tiny
4394 * impact on the overall fragmentation.
4395 */
4396 vs->vs_fragmentation = (vd->vdev_mg != NULL) ?
4397 vd->vdev_mg->mg_fragmentation : 0;
4398 }
4399 }
4400
4401 vdev_get_stats_ex_impl(vd, vs, vsx);
4402 mutex_exit(&vd->vdev_stat_lock);
4403 }
4404
4405 void
4406 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
4407 {
4408 return (vdev_get_stats_ex(vd, vs, NULL));
4409 }
4410
4411 void
4412 vdev_clear_stats(vdev_t *vd)
4413 {
4414 mutex_enter(&vd->vdev_stat_lock);
4415 vd->vdev_stat.vs_space = 0;
4416 vd->vdev_stat.vs_dspace = 0;
4417 vd->vdev_stat.vs_alloc = 0;
4418 mutex_exit(&vd->vdev_stat_lock);
4419 }
4420
4421 void
4422 vdev_scan_stat_init(vdev_t *vd)
4423 {
4424 vdev_stat_t *vs = &vd->vdev_stat;
4425
4426 for (int c = 0; c < vd->vdev_children; c++)
4427 vdev_scan_stat_init(vd->vdev_child[c]);
4428
4429 mutex_enter(&vd->vdev_stat_lock);
4430 vs->vs_scan_processed = 0;
4431 mutex_exit(&vd->vdev_stat_lock);
4432 }
4433
4434 void
4435 vdev_stat_update(zio_t *zio, uint64_t psize)
4436 {
4437 spa_t *spa = zio->io_spa;
4438 vdev_t *rvd = spa->spa_root_vdev;
4439 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
4440 vdev_t *pvd;
4441 uint64_t txg = zio->io_txg;
4442 vdev_stat_t *vs = &vd->vdev_stat;
4443 vdev_stat_ex_t *vsx = &vd->vdev_stat_ex;
4444 zio_type_t type = zio->io_type;
4445 int flags = zio->io_flags;
4446
4447 /*
4448 * If this i/o is a gang leader, it didn't do any actual work.
4449 */
4450 if (zio->io_gang_tree)
4451 return;
4452
4453 if (zio->io_error == 0) {
4454 /*
4455 * If this is a root i/o, don't count it -- we've already
4456 * counted the top-level vdevs, and vdev_get_stats() will
4457 * aggregate them when asked. This reduces contention on
4458 * the root vdev_stat_lock and implicitly handles blocks
4459 * that compress away to holes, for which there is no i/o.
4460 * (Holes never create vdev children, so all the counters
4461 * remain zero, which is what we want.)
4462 *
4463 * Note: this only applies to successful i/o (io_error == 0)
4464 * because unlike i/o counts, errors are not additive.
4465 * When reading a ditto block, for example, failure of
4466 * one top-level vdev does not imply a root-level error.
4467 */
4468 if (vd == rvd)
4469 return;
4470
4471 ASSERT(vd == zio->io_vd);
4472
4473 if (flags & ZIO_FLAG_IO_BYPASS)
4474 return;
4475
4476 mutex_enter(&vd->vdev_stat_lock);
4477
4478 if (flags & ZIO_FLAG_IO_REPAIR) {
4479 /*
4480 * Repair is the result of a resilver issued by the
4481 * scan thread (spa_sync).
4482 */
4483 if (flags & ZIO_FLAG_SCAN_THREAD) {
4484 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
4485 dsl_scan_phys_t *scn_phys = &scn->scn_phys;
4486 uint64_t *processed = &scn_phys->scn_processed;
4487
4488 if (vd->vdev_ops->vdev_op_leaf)
4489 atomic_add_64(processed, psize);
4490 vs->vs_scan_processed += psize;
4491 }
4492
4493 /*
4494 * Repair is the result of a rebuild issued by the
4495 * rebuild thread (vdev_rebuild_thread). To avoid
4496 * double counting repaired bytes the virtual dRAID
4497 * spare vdev is excluded from the processed bytes.
4498 */
4499 if (zio->io_priority == ZIO_PRIORITY_REBUILD) {
4500 vdev_t *tvd = vd->vdev_top;
4501 vdev_rebuild_t *vr = &tvd->vdev_rebuild_config;
4502 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
4503 uint64_t *rebuilt = &vrp->vrp_bytes_rebuilt;
4504
4505 if (vd->vdev_ops->vdev_op_leaf &&
4506 vd->vdev_ops != &vdev_draid_spare_ops) {
4507 atomic_add_64(rebuilt, psize);
4508 }
4509 vs->vs_rebuild_processed += psize;
4510 }
4511
4512 if (flags & ZIO_FLAG_SELF_HEAL)
4513 vs->vs_self_healed += psize;
4514 }
4515
4516 /*
4517 * The bytes/ops/histograms are recorded at the leaf level and
4518 * aggregated into the higher level vdevs in vdev_get_stats().
4519 */
4520 if (vd->vdev_ops->vdev_op_leaf &&
4521 (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) {
4522 zio_type_t vs_type = type;
4523 zio_priority_t priority = zio->io_priority;
4524
4525 /*
4526 * TRIM ops and bytes are reported to user space as
4527 * ZIO_TYPE_IOCTL. This is done to preserve the
4528 * vdev_stat_t structure layout for user space.
4529 */
4530 if (type == ZIO_TYPE_TRIM)
4531 vs_type = ZIO_TYPE_IOCTL;
4532
4533 /*
4534 * Solely for the purposes of 'zpool iostat -lqrw'
4535 * reporting use the priority to catagorize the IO.
4536 * Only the following are reported to user space:
4537 *
4538 * ZIO_PRIORITY_SYNC_READ,
4539 * ZIO_PRIORITY_SYNC_WRITE,
4540 * ZIO_PRIORITY_ASYNC_READ,
4541 * ZIO_PRIORITY_ASYNC_WRITE,
4542 * ZIO_PRIORITY_SCRUB,
4543 * ZIO_PRIORITY_TRIM.
4544 */
4545 if (priority == ZIO_PRIORITY_REBUILD) {
4546 priority = ((type == ZIO_TYPE_WRITE) ?
4547 ZIO_PRIORITY_ASYNC_WRITE :
4548 ZIO_PRIORITY_SCRUB);
4549 } else if (priority == ZIO_PRIORITY_INITIALIZING) {
4550 ASSERT3U(type, ==, ZIO_TYPE_WRITE);
4551 priority = ZIO_PRIORITY_ASYNC_WRITE;
4552 } else if (priority == ZIO_PRIORITY_REMOVAL) {
4553 priority = ((type == ZIO_TYPE_WRITE) ?
4554 ZIO_PRIORITY_ASYNC_WRITE :
4555 ZIO_PRIORITY_ASYNC_READ);
4556 }
4557
4558 vs->vs_ops[vs_type]++;
4559 vs->vs_bytes[vs_type] += psize;
4560
4561 if (flags & ZIO_FLAG_DELEGATED) {
4562 vsx->vsx_agg_histo[priority]
4563 [RQ_HISTO(zio->io_size)]++;
4564 } else {
4565 vsx->vsx_ind_histo[priority]
4566 [RQ_HISTO(zio->io_size)]++;
4567 }
4568
4569 if (zio->io_delta && zio->io_delay) {
4570 vsx->vsx_queue_histo[priority]
4571 [L_HISTO(zio->io_delta - zio->io_delay)]++;
4572 vsx->vsx_disk_histo[type]
4573 [L_HISTO(zio->io_delay)]++;
4574 vsx->vsx_total_histo[type]
4575 [L_HISTO(zio->io_delta)]++;
4576 }
4577 }
4578
4579 mutex_exit(&vd->vdev_stat_lock);
4580 return;
4581 }
4582
4583 if (flags & ZIO_FLAG_SPECULATIVE)
4584 return;
4585
4586 /*
4587 * If this is an I/O error that is going to be retried, then ignore the
4588 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
4589 * hard errors, when in reality they can happen for any number of
4590 * innocuous reasons (bus resets, MPxIO link failure, etc).
4591 */
4592 if (zio->io_error == EIO &&
4593 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
4594 return;
4595
4596 /*
4597 * Intent logs writes won't propagate their error to the root
4598 * I/O so don't mark these types of failures as pool-level
4599 * errors.
4600 */
4601 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
4602 return;
4603
4604 if (type == ZIO_TYPE_WRITE && txg != 0 &&
4605 (!(flags & ZIO_FLAG_IO_REPAIR) ||
4606 (flags & ZIO_FLAG_SCAN_THREAD) ||
4607 spa->spa_claiming)) {
4608 /*
4609 * This is either a normal write (not a repair), or it's
4610 * a repair induced by the scrub thread, or it's a repair
4611 * made by zil_claim() during spa_load() in the first txg.
4612 * In the normal case, we commit the DTL change in the same
4613 * txg as the block was born. In the scrub-induced repair
4614 * case, we know that scrubs run in first-pass syncing context,
4615 * so we commit the DTL change in spa_syncing_txg(spa).
4616 * In the zil_claim() case, we commit in spa_first_txg(spa).
4617 *
4618 * We currently do not make DTL entries for failed spontaneous
4619 * self-healing writes triggered by normal (non-scrubbing)
4620 * reads, because we have no transactional context in which to
4621 * do so -- and it's not clear that it'd be desirable anyway.
4622 */
4623 if (vd->vdev_ops->vdev_op_leaf) {
4624 uint64_t commit_txg = txg;
4625 if (flags & ZIO_FLAG_SCAN_THREAD) {
4626 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
4627 ASSERT(spa_sync_pass(spa) == 1);
4628 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
4629 commit_txg = spa_syncing_txg(spa);
4630 } else if (spa->spa_claiming) {
4631 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
4632 commit_txg = spa_first_txg(spa);
4633 }
4634 ASSERT(commit_txg >= spa_syncing_txg(spa));
4635 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
4636 return;
4637 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
4638 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
4639 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
4640 }
4641 if (vd != rvd)
4642 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
4643 }
4644 }
4645
4646 int64_t
4647 vdev_deflated_space(vdev_t *vd, int64_t space)
4648 {
4649 ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0);
4650 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
4651
4652 return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio);
4653 }
4654
4655 /*
4656 * Update the in-core space usage stats for this vdev, its metaslab class,
4657 * and the root vdev.
4658 */
4659 void
4660 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
4661 int64_t space_delta)
4662 {
4663 int64_t dspace_delta;
4664 spa_t *spa = vd->vdev_spa;
4665 vdev_t *rvd = spa->spa_root_vdev;
4666
4667 ASSERT(vd == vd->vdev_top);
4668
4669 /*
4670 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
4671 * factor. We must calculate this here and not at the root vdev
4672 * because the root vdev's psize-to-asize is simply the max of its
4673 * children's, thus not accurate enough for us.
4674 */
4675 dspace_delta = vdev_deflated_space(vd, space_delta);
4676
4677 mutex_enter(&vd->vdev_stat_lock);
4678 /* ensure we won't underflow */
4679 if (alloc_delta < 0) {
4680 ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta);
4681 }
4682
4683 vd->vdev_stat.vs_alloc += alloc_delta;
4684 vd->vdev_stat.vs_space += space_delta;
4685 vd->vdev_stat.vs_dspace += dspace_delta;
4686 mutex_exit(&vd->vdev_stat_lock);
4687
4688 /* every class but log contributes to root space stats */
4689 if (vd->vdev_mg != NULL && !vd->vdev_islog) {
4690 ASSERT(!vd->vdev_isl2cache);
4691 mutex_enter(&rvd->vdev_stat_lock);
4692 rvd->vdev_stat.vs_alloc += alloc_delta;
4693 rvd->vdev_stat.vs_space += space_delta;
4694 rvd->vdev_stat.vs_dspace += dspace_delta;
4695 mutex_exit(&rvd->vdev_stat_lock);
4696 }
4697 /* Note: metaslab_class_space_update moved to metaslab_space_update */
4698 }
4699
4700 /*
4701 * Mark a top-level vdev's config as dirty, placing it on the dirty list
4702 * so that it will be written out next time the vdev configuration is synced.
4703 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
4704 */
4705 void
4706 vdev_config_dirty(vdev_t *vd)
4707 {
4708 spa_t *spa = vd->vdev_spa;
4709 vdev_t *rvd = spa->spa_root_vdev;
4710 int c;
4711
4712 ASSERT(spa_writeable(spa));
4713
4714 /*
4715 * If this is an aux vdev (as with l2cache and spare devices), then we
4716 * update the vdev config manually and set the sync flag.
4717 */
4718 if (vd->vdev_aux != NULL) {
4719 spa_aux_vdev_t *sav = vd->vdev_aux;
4720 nvlist_t **aux;
4721 uint_t naux;
4722
4723 for (c = 0; c < sav->sav_count; c++) {
4724 if (sav->sav_vdevs[c] == vd)
4725 break;
4726 }
4727
4728 if (c == sav->sav_count) {
4729 /*
4730 * We're being removed. There's nothing more to do.
4731 */
4732 ASSERT(sav->sav_sync == B_TRUE);
4733 return;
4734 }
4735
4736 sav->sav_sync = B_TRUE;
4737
4738 if (nvlist_lookup_nvlist_array(sav->sav_config,
4739 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
4740 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
4741 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
4742 }
4743
4744 ASSERT(c < naux);
4745
4746 /*
4747 * Setting the nvlist in the middle if the array is a little
4748 * sketchy, but it will work.
4749 */
4750 nvlist_free(aux[c]);
4751 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
4752
4753 return;
4754 }
4755
4756 /*
4757 * The dirty list is protected by the SCL_CONFIG lock. The caller
4758 * must either hold SCL_CONFIG as writer, or must be the sync thread
4759 * (which holds SCL_CONFIG as reader). There's only one sync thread,
4760 * so this is sufficient to ensure mutual exclusion.
4761 */
4762 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
4763 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4764 spa_config_held(spa, SCL_CONFIG, RW_READER)));
4765
4766 if (vd == rvd) {
4767 for (c = 0; c < rvd->vdev_children; c++)
4768 vdev_config_dirty(rvd->vdev_child[c]);
4769 } else {
4770 ASSERT(vd == vd->vdev_top);
4771
4772 if (!list_link_active(&vd->vdev_config_dirty_node) &&
4773 vdev_is_concrete(vd)) {
4774 list_insert_head(&spa->spa_config_dirty_list, vd);
4775 }
4776 }
4777 }
4778
4779 void
4780 vdev_config_clean(vdev_t *vd)
4781 {
4782 spa_t *spa = vd->vdev_spa;
4783
4784 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
4785 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4786 spa_config_held(spa, SCL_CONFIG, RW_READER)));
4787
4788 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
4789 list_remove(&spa->spa_config_dirty_list, vd);
4790 }
4791
4792 /*
4793 * Mark a top-level vdev's state as dirty, so that the next pass of
4794 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
4795 * the state changes from larger config changes because they require
4796 * much less locking, and are often needed for administrative actions.
4797 */
4798 void
4799 vdev_state_dirty(vdev_t *vd)
4800 {
4801 spa_t *spa = vd->vdev_spa;
4802
4803 ASSERT(spa_writeable(spa));
4804 ASSERT(vd == vd->vdev_top);
4805
4806 /*
4807 * The state list is protected by the SCL_STATE lock. The caller
4808 * must either hold SCL_STATE as writer, or must be the sync thread
4809 * (which holds SCL_STATE as reader). There's only one sync thread,
4810 * so this is sufficient to ensure mutual exclusion.
4811 */
4812 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
4813 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4814 spa_config_held(spa, SCL_STATE, RW_READER)));
4815
4816 if (!list_link_active(&vd->vdev_state_dirty_node) &&
4817 vdev_is_concrete(vd))
4818 list_insert_head(&spa->spa_state_dirty_list, vd);
4819 }
4820
4821 void
4822 vdev_state_clean(vdev_t *vd)
4823 {
4824 spa_t *spa = vd->vdev_spa;
4825
4826 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
4827 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
4828 spa_config_held(spa, SCL_STATE, RW_READER)));
4829
4830 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
4831 list_remove(&spa->spa_state_dirty_list, vd);
4832 }
4833
4834 /*
4835 * Propagate vdev state up from children to parent.
4836 */
4837 void
4838 vdev_propagate_state(vdev_t *vd)
4839 {
4840 spa_t *spa = vd->vdev_spa;
4841 vdev_t *rvd = spa->spa_root_vdev;
4842 int degraded = 0, faulted = 0;
4843 int corrupted = 0;
4844 vdev_t *child;
4845
4846 if (vd->vdev_children > 0) {
4847 for (int c = 0; c < vd->vdev_children; c++) {
4848 child = vd->vdev_child[c];
4849
4850 /*
4851 * Don't factor holes or indirect vdevs into the
4852 * decision.
4853 */
4854 if (!vdev_is_concrete(child))
4855 continue;
4856
4857 if (!vdev_readable(child) ||
4858 (!vdev_writeable(child) && spa_writeable(spa))) {
4859 /*
4860 * Root special: if there is a top-level log
4861 * device, treat the root vdev as if it were
4862 * degraded.
4863 */
4864 if (child->vdev_islog && vd == rvd)
4865 degraded++;
4866 else
4867 faulted++;
4868 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
4869 degraded++;
4870 }
4871
4872 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
4873 corrupted++;
4874 }
4875
4876 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
4877
4878 /*
4879 * Root special: if there is a top-level vdev that cannot be
4880 * opened due to corrupted metadata, then propagate the root
4881 * vdev's aux state as 'corrupt' rather than 'insufficient
4882 * replicas'.
4883 */
4884 if (corrupted && vd == rvd &&
4885 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
4886 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
4887 VDEV_AUX_CORRUPT_DATA);
4888 }
4889
4890 if (vd->vdev_parent)
4891 vdev_propagate_state(vd->vdev_parent);
4892 }
4893
4894 /*
4895 * Set a vdev's state. If this is during an open, we don't update the parent
4896 * state, because we're in the process of opening children depth-first.
4897 * Otherwise, we propagate the change to the parent.
4898 *
4899 * If this routine places a device in a faulted state, an appropriate ereport is
4900 * generated.
4901 */
4902 void
4903 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
4904 {
4905 uint64_t save_state;
4906 spa_t *spa = vd->vdev_spa;
4907
4908 if (state == vd->vdev_state) {
4909 /*
4910 * Since vdev_offline() code path is already in an offline
4911 * state we can miss a statechange event to OFFLINE. Check
4912 * the previous state to catch this condition.
4913 */
4914 if (vd->vdev_ops->vdev_op_leaf &&
4915 (state == VDEV_STATE_OFFLINE) &&
4916 (vd->vdev_prevstate >= VDEV_STATE_FAULTED)) {
4917 /* post an offline state change */
4918 zfs_post_state_change(spa, vd, vd->vdev_prevstate);
4919 }
4920 vd->vdev_stat.vs_aux = aux;
4921 return;
4922 }
4923
4924 save_state = vd->vdev_state;
4925
4926 vd->vdev_state = state;
4927 vd->vdev_stat.vs_aux = aux;
4928
4929 /*
4930 * If we are setting the vdev state to anything but an open state, then
4931 * always close the underlying device unless the device has requested
4932 * a delayed close (i.e. we're about to remove or fault the device).
4933 * Otherwise, we keep accessible but invalid devices open forever.
4934 * We don't call vdev_close() itself, because that implies some extra
4935 * checks (offline, etc) that we don't want here. This is limited to
4936 * leaf devices, because otherwise closing the device will affect other
4937 * children.
4938 */
4939 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
4940 vd->vdev_ops->vdev_op_leaf)
4941 vd->vdev_ops->vdev_op_close(vd);
4942
4943 if (vd->vdev_removed &&
4944 state == VDEV_STATE_CANT_OPEN &&
4945 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
4946 /*
4947 * If the previous state is set to VDEV_STATE_REMOVED, then this
4948 * device was previously marked removed and someone attempted to
4949 * reopen it. If this failed due to a nonexistent device, then
4950 * keep the device in the REMOVED state. We also let this be if
4951 * it is one of our special test online cases, which is only
4952 * attempting to online the device and shouldn't generate an FMA
4953 * fault.
4954 */
4955 vd->vdev_state = VDEV_STATE_REMOVED;
4956 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
4957 } else if (state == VDEV_STATE_REMOVED) {
4958 vd->vdev_removed = B_TRUE;
4959 } else if (state == VDEV_STATE_CANT_OPEN) {
4960 /*
4961 * If we fail to open a vdev during an import or recovery, we
4962 * mark it as "not available", which signifies that it was
4963 * never there to begin with. Failure to open such a device
4964 * is not considered an error.
4965 */
4966 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
4967 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
4968 vd->vdev_ops->vdev_op_leaf)
4969 vd->vdev_not_present = 1;
4970
4971 /*
4972 * Post the appropriate ereport. If the 'prevstate' field is
4973 * set to something other than VDEV_STATE_UNKNOWN, it indicates
4974 * that this is part of a vdev_reopen(). In this case, we don't
4975 * want to post the ereport if the device was already in the
4976 * CANT_OPEN state beforehand.
4977 *
4978 * If the 'checkremove' flag is set, then this is an attempt to
4979 * online the device in response to an insertion event. If we
4980 * hit this case, then we have detected an insertion event for a
4981 * faulted or offline device that wasn't in the removed state.
4982 * In this scenario, we don't post an ereport because we are
4983 * about to replace the device, or attempt an online with
4984 * vdev_forcefault, which will generate the fault for us.
4985 */
4986 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
4987 !vd->vdev_not_present && !vd->vdev_checkremove &&
4988 vd != spa->spa_root_vdev) {
4989 const char *class;
4990
4991 switch (aux) {
4992 case VDEV_AUX_OPEN_FAILED:
4993 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
4994 break;
4995 case VDEV_AUX_CORRUPT_DATA:
4996 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
4997 break;
4998 case VDEV_AUX_NO_REPLICAS:
4999 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
5000 break;
5001 case VDEV_AUX_BAD_GUID_SUM:
5002 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
5003 break;
5004 case VDEV_AUX_TOO_SMALL:
5005 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
5006 break;
5007 case VDEV_AUX_BAD_LABEL:
5008 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
5009 break;
5010 case VDEV_AUX_BAD_ASHIFT:
5011 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT;
5012 break;
5013 default:
5014 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
5015 }
5016
5017 (void) zfs_ereport_post(class, spa, vd, NULL, NULL,
5018 save_state);
5019 }
5020
5021 /* Erase any notion of persistent removed state */
5022 vd->vdev_removed = B_FALSE;
5023 } else {
5024 vd->vdev_removed = B_FALSE;
5025 }
5026
5027 /*
5028 * Notify ZED of any significant state-change on a leaf vdev.
5029 *
5030 */
5031 if (vd->vdev_ops->vdev_op_leaf) {
5032 /* preserve original state from a vdev_reopen() */
5033 if ((vd->vdev_prevstate != VDEV_STATE_UNKNOWN) &&
5034 (vd->vdev_prevstate != vd->vdev_state) &&
5035 (save_state <= VDEV_STATE_CLOSED))
5036 save_state = vd->vdev_prevstate;
5037
5038 /* filter out state change due to initial vdev_open */
5039 if (save_state > VDEV_STATE_CLOSED)
5040 zfs_post_state_change(spa, vd, save_state);
5041 }
5042
5043 if (!isopen && vd->vdev_parent)
5044 vdev_propagate_state(vd->vdev_parent);
5045 }
5046
5047 boolean_t
5048 vdev_children_are_offline(vdev_t *vd)
5049 {
5050 ASSERT(!vd->vdev_ops->vdev_op_leaf);
5051
5052 for (uint64_t i = 0; i < vd->vdev_children; i++) {
5053 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
5054 return (B_FALSE);
5055 }
5056
5057 return (B_TRUE);
5058 }
5059
5060 /*
5061 * Check the vdev configuration to ensure that it's capable of supporting
5062 * a root pool. We do not support partial configuration.
5063 */
5064 boolean_t
5065 vdev_is_bootable(vdev_t *vd)
5066 {
5067 if (!vd->vdev_ops->vdev_op_leaf) {
5068 const char *vdev_type = vd->vdev_ops->vdev_op_type;
5069
5070 if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 ||
5071 strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) {
5072 return (B_FALSE);
5073 }
5074 }
5075
5076 for (int c = 0; c < vd->vdev_children; c++) {
5077 if (!vdev_is_bootable(vd->vdev_child[c]))
5078 return (B_FALSE);
5079 }
5080 return (B_TRUE);
5081 }
5082
5083 boolean_t
5084 vdev_is_concrete(vdev_t *vd)
5085 {
5086 vdev_ops_t *ops = vd->vdev_ops;
5087 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
5088 ops == &vdev_missing_ops || ops == &vdev_root_ops) {
5089 return (B_FALSE);
5090 } else {
5091 return (B_TRUE);
5092 }
5093 }
5094
5095 /*
5096 * Determine if a log device has valid content. If the vdev was
5097 * removed or faulted in the MOS config then we know that
5098 * the content on the log device has already been written to the pool.
5099 */
5100 boolean_t
5101 vdev_log_state_valid(vdev_t *vd)
5102 {
5103 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
5104 !vd->vdev_removed)
5105 return (B_TRUE);
5106
5107 for (int c = 0; c < vd->vdev_children; c++)
5108 if (vdev_log_state_valid(vd->vdev_child[c]))
5109 return (B_TRUE);
5110
5111 return (B_FALSE);
5112 }
5113
5114 /*
5115 * Expand a vdev if possible.
5116 */
5117 void
5118 vdev_expand(vdev_t *vd, uint64_t txg)
5119 {
5120 ASSERT(vd->vdev_top == vd);
5121 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5122 ASSERT(vdev_is_concrete(vd));
5123
5124 vdev_set_deflate_ratio(vd);
5125
5126 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
5127 vdev_is_concrete(vd)) {
5128 vdev_metaslab_group_create(vd);
5129 VERIFY(vdev_metaslab_init(vd, txg) == 0);
5130 vdev_config_dirty(vd);
5131 }
5132 }
5133
5134 /*
5135 * Split a vdev.
5136 */
5137 void
5138 vdev_split(vdev_t *vd)
5139 {
5140 vdev_t *cvd, *pvd = vd->vdev_parent;
5141
5142 vdev_remove_child(pvd, vd);
5143 vdev_compact_children(pvd);
5144
5145 cvd = pvd->vdev_child[0];
5146 if (pvd->vdev_children == 1) {
5147 vdev_remove_parent(cvd);
5148 cvd->vdev_splitting = B_TRUE;
5149 }
5150 vdev_propagate_state(cvd);
5151 }
5152
5153 void
5154 vdev_deadman(vdev_t *vd, char *tag)
5155 {
5156 for (int c = 0; c < vd->vdev_children; c++) {
5157 vdev_t *cvd = vd->vdev_child[c];
5158
5159 vdev_deadman(cvd, tag);
5160 }
5161
5162 if (vd->vdev_ops->vdev_op_leaf) {
5163 vdev_queue_t *vq = &vd->vdev_queue;
5164
5165 mutex_enter(&vq->vq_lock);
5166 if (avl_numnodes(&vq->vq_active_tree) > 0) {
5167 spa_t *spa = vd->vdev_spa;
5168 zio_t *fio;
5169 uint64_t delta;
5170
5171 zfs_dbgmsg("slow vdev: %s has %d active IOs",
5172 vd->vdev_path, avl_numnodes(&vq->vq_active_tree));
5173
5174 /*
5175 * Look at the head of all the pending queues,
5176 * if any I/O has been outstanding for longer than
5177 * the spa_deadman_synctime invoke the deadman logic.
5178 */
5179 fio = avl_first(&vq->vq_active_tree);
5180 delta = gethrtime() - fio->io_timestamp;
5181 if (delta > spa_deadman_synctime(spa))
5182 zio_deadman(fio, tag);
5183 }
5184 mutex_exit(&vq->vq_lock);
5185 }
5186 }
5187
5188 void
5189 vdev_defer_resilver(vdev_t *vd)
5190 {
5191 ASSERT(vd->vdev_ops->vdev_op_leaf);
5192
5193 vd->vdev_resilver_deferred = B_TRUE;
5194 vd->vdev_spa->spa_resilver_deferred = B_TRUE;
5195 }
5196
5197 /*
5198 * Clears the resilver deferred flag on all leaf devs under vd. Returns
5199 * B_TRUE if we have devices that need to be resilvered and are available to
5200 * accept resilver I/Os.
5201 */
5202 boolean_t
5203 vdev_clear_resilver_deferred(vdev_t *vd, dmu_tx_t *tx)
5204 {
5205 boolean_t resilver_needed = B_FALSE;
5206 spa_t *spa = vd->vdev_spa;
5207
5208 for (int c = 0; c < vd->vdev_children; c++) {
5209 vdev_t *cvd = vd->vdev_child[c];
5210 resilver_needed |= vdev_clear_resilver_deferred(cvd, tx);
5211 }
5212
5213 if (vd == spa->spa_root_vdev &&
5214 spa_feature_is_active(spa, SPA_FEATURE_RESILVER_DEFER)) {
5215 spa_feature_decr(spa, SPA_FEATURE_RESILVER_DEFER, tx);
5216 vdev_config_dirty(vd);
5217 spa->spa_resilver_deferred = B_FALSE;
5218 return (resilver_needed);
5219 }
5220
5221 if (!vdev_is_concrete(vd) || vd->vdev_aux ||
5222 !vd->vdev_ops->vdev_op_leaf)
5223 return (resilver_needed);
5224
5225 vd->vdev_resilver_deferred = B_FALSE;
5226
5227 return (!vdev_is_dead(vd) && !vd->vdev_offline &&
5228 vdev_resilver_needed(vd, NULL, NULL));
5229 }
5230
5231 boolean_t
5232 vdev_xlate_is_empty(range_seg64_t *rs)
5233 {
5234 return (rs->rs_start == rs->rs_end);
5235 }
5236
5237 /*
5238 * Translate a logical range to the first contiguous physical range for the
5239 * specified vdev_t. This function is initially called with a leaf vdev and
5240 * will walk each parent vdev until it reaches a top-level vdev. Once the
5241 * top-level is reached the physical range is initialized and the recursive
5242 * function begins to unwind. As it unwinds it calls the parent's vdev
5243 * specific translation function to do the real conversion.
5244 */
5245 void
5246 vdev_xlate(vdev_t *vd, const range_seg64_t *logical_rs,
5247 range_seg64_t *physical_rs, range_seg64_t *remain_rs)
5248 {
5249 /*
5250 * Walk up the vdev tree
5251 */
5252 if (vd != vd->vdev_top) {
5253 vdev_xlate(vd->vdev_parent, logical_rs, physical_rs,
5254 remain_rs);
5255 } else {
5256 /*
5257 * We've reached the top-level vdev, initialize the physical
5258 * range to the logical range and set an empty remaining
5259 * range then start to unwind.
5260 */
5261 physical_rs->rs_start = logical_rs->rs_start;
5262 physical_rs->rs_end = logical_rs->rs_end;
5263
5264 remain_rs->rs_start = logical_rs->rs_start;
5265 remain_rs->rs_end = logical_rs->rs_start;
5266
5267 return;
5268 }
5269
5270 vdev_t *pvd = vd->vdev_parent;
5271 ASSERT3P(pvd, !=, NULL);
5272 ASSERT3P(pvd->vdev_ops->vdev_op_xlate, !=, NULL);
5273
5274 /*
5275 * As this recursive function unwinds, translate the logical
5276 * range into its physical and any remaining components by calling
5277 * the vdev specific translate function.
5278 */
5279 range_seg64_t intermediate = { 0 };
5280 pvd->vdev_ops->vdev_op_xlate(vd, physical_rs, &intermediate, remain_rs);
5281
5282 physical_rs->rs_start = intermediate.rs_start;
5283 physical_rs->rs_end = intermediate.rs_end;
5284 }
5285
5286 void
5287 vdev_xlate_walk(vdev_t *vd, const range_seg64_t *logical_rs,
5288 vdev_xlate_func_t *func, void *arg)
5289 {
5290 range_seg64_t iter_rs = *logical_rs;
5291 range_seg64_t physical_rs;
5292 range_seg64_t remain_rs;
5293
5294 while (!vdev_xlate_is_empty(&iter_rs)) {
5295
5296 vdev_xlate(vd, &iter_rs, &physical_rs, &remain_rs);
5297
5298 /*
5299 * With raidz and dRAID, it's possible that the logical range
5300 * does not live on this leaf vdev. Only when there is a non-
5301 * zero physical size call the provided function.
5302 */
5303 if (!vdev_xlate_is_empty(&physical_rs))
5304 func(arg, &physical_rs);
5305
5306 iter_rs = remain_rs;
5307 }
5308 }
5309
5310 /*
5311 * Look at the vdev tree and determine whether any devices are currently being
5312 * replaced.
5313 */
5314 boolean_t
5315 vdev_replace_in_progress(vdev_t *vdev)
5316 {
5317 ASSERT(spa_config_held(vdev->vdev_spa, SCL_ALL, RW_READER) != 0);
5318
5319 if (vdev->vdev_ops == &vdev_replacing_ops)
5320 return (B_TRUE);
5321
5322 /*
5323 * A 'spare' vdev indicates that we have a replace in progress, unless
5324 * it has exactly two children, and the second, the hot spare, has
5325 * finished being resilvered.
5326 */
5327 if (vdev->vdev_ops == &vdev_spare_ops && (vdev->vdev_children > 2 ||
5328 !vdev_dtl_empty(vdev->vdev_child[1], DTL_MISSING)))
5329 return (B_TRUE);
5330
5331 for (int i = 0; i < vdev->vdev_children; i++) {
5332 if (vdev_replace_in_progress(vdev->vdev_child[i]))
5333 return (B_TRUE);
5334 }
5335
5336 return (B_FALSE);
5337 }
5338
5339 EXPORT_SYMBOL(vdev_fault);
5340 EXPORT_SYMBOL(vdev_degrade);
5341 EXPORT_SYMBOL(vdev_online);
5342 EXPORT_SYMBOL(vdev_offline);
5343 EXPORT_SYMBOL(vdev_clear);
5344
5345 /* BEGIN CSTYLED */
5346 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_count, INT, ZMOD_RW,
5347 "Target number of metaslabs per top-level vdev");
5348
5349 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_shift, INT, ZMOD_RW,
5350 "Default limit for metaslab size");
5351
5352 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, min_ms_count, INT, ZMOD_RW,
5353 "Minimum number of metaslabs per top-level vdev");
5354
5355 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, ms_count_limit, INT, ZMOD_RW,
5356 "Practical upper limit of total metaslabs per top-level vdev");
5357
5358 ZFS_MODULE_PARAM(zfs, zfs_, slow_io_events_per_second, UINT, ZMOD_RW,
5359 "Rate limit slow IO (delay) events to this many per second");
5360
5361 ZFS_MODULE_PARAM(zfs, zfs_, checksum_events_per_second, UINT, ZMOD_RW,
5362 "Rate limit checksum events to this many checksum errors per second "
5363 "(do not set below zed threshold).");
5364
5365 ZFS_MODULE_PARAM(zfs, zfs_, scan_ignore_errors, INT, ZMOD_RW,
5366 "Ignore errors during resilver/scrub");
5367
5368 ZFS_MODULE_PARAM(zfs_vdev, vdev_, validate_skip, INT, ZMOD_RW,
5369 "Bypass vdev_validate()");
5370
5371 ZFS_MODULE_PARAM(zfs, zfs_, nocacheflush, INT, ZMOD_RW,
5372 "Disable cache flushes");
5373
5374 ZFS_MODULE_PARAM(zfs, zfs_, embedded_slog_min_ms, INT, ZMOD_RW,
5375 "Minimum number of metaslabs required to dedicate one for log blocks");
5376
5377 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, min_auto_ashift,
5378 param_set_min_auto_ashift, param_get_ulong, ZMOD_RW,
5379 "Minimum ashift used when creating new top-level vdevs");
5380
5381 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, max_auto_ashift,
5382 param_set_max_auto_ashift, param_get_ulong, ZMOD_RW,
5383 "Maximum ashift used when optimizing for logical -> physical sector "
5384 "size on new top-level vdevs");
5385 /* END CSTYLED */