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 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
28 */
29
30 #include <sys/zfs_context.h>
31 #include <sys/spa.h>
32 #include <sys/spa_impl.h>
33 #include <sys/dsl_pool.h>
34 #include <sys/dsl_scan.h>
35 #include <sys/vdev_impl.h>
36 #include <sys/vdev_draid.h>
37 #include <sys/zio.h>
38 #include <sys/abd.h>
39 #include <sys/fs/zfs.h>
40
41 /*
42 * Vdev mirror kstats
43 */
44 static kstat_t *mirror_ksp = NULL;
45
46 typedef struct mirror_stats {
47 kstat_named_t vdev_mirror_stat_rotating_linear;
48 kstat_named_t vdev_mirror_stat_rotating_offset;
49 kstat_named_t vdev_mirror_stat_rotating_seek;
50 kstat_named_t vdev_mirror_stat_non_rotating_linear;
51 kstat_named_t vdev_mirror_stat_non_rotating_seek;
52
53 kstat_named_t vdev_mirror_stat_preferred_found;
54 kstat_named_t vdev_mirror_stat_preferred_not_found;
55 } mirror_stats_t;
56
57 static mirror_stats_t mirror_stats = {
58 /* New I/O follows directly the last I/O */
59 { "rotating_linear", KSTAT_DATA_UINT64 },
60 /* New I/O is within zfs_vdev_mirror_rotating_seek_offset of the last */
61 { "rotating_offset", KSTAT_DATA_UINT64 },
62 /* New I/O requires random seek */
63 { "rotating_seek", KSTAT_DATA_UINT64 },
64 /* New I/O follows directly the last I/O (nonrot) */
65 { "non_rotating_linear", KSTAT_DATA_UINT64 },
66 /* New I/O requires random seek (nonrot) */
67 { "non_rotating_seek", KSTAT_DATA_UINT64 },
68 /* Preferred child vdev found */
69 { "preferred_found", KSTAT_DATA_UINT64 },
70 /* Preferred child vdev not found or equal load */
71 { "preferred_not_found", KSTAT_DATA_UINT64 },
72
73 };
74
75 #define MIRROR_STAT(stat) (mirror_stats.stat.value.ui64)
76 #define MIRROR_INCR(stat, val) atomic_add_64(&MIRROR_STAT(stat), val)
77 #define MIRROR_BUMP(stat) MIRROR_INCR(stat, 1)
78
79 void
vdev_mirror_stat_init(void)80 vdev_mirror_stat_init(void)
81 {
82 mirror_ksp = kstat_create("zfs", 0, "vdev_mirror_stats",
83 "misc", KSTAT_TYPE_NAMED,
84 sizeof (mirror_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
85 if (mirror_ksp != NULL) {
86 mirror_ksp->ks_data = &mirror_stats;
87 kstat_install(mirror_ksp);
88 }
89 }
90
91 void
vdev_mirror_stat_fini(void)92 vdev_mirror_stat_fini(void)
93 {
94 if (mirror_ksp != NULL) {
95 kstat_delete(mirror_ksp);
96 mirror_ksp = NULL;
97 }
98 }
99
100 /*
101 * Virtual device vector for mirroring.
102 */
103 typedef struct mirror_child {
104 vdev_t *mc_vd;
105 uint64_t mc_offset;
106 int mc_error;
107 int mc_load;
108 uint8_t mc_tried;
109 uint8_t mc_skipped;
110 uint8_t mc_speculative;
111 uint8_t mc_rebuilding;
112 } mirror_child_t;
113
114 typedef struct mirror_map {
115 int *mm_preferred;
116 int mm_preferred_cnt;
117 int mm_children;
118 boolean_t mm_resilvering;
119 boolean_t mm_rebuilding;
120 boolean_t mm_root;
121 mirror_child_t mm_child[];
122 } mirror_map_t;
123
124 static int vdev_mirror_shift = 21;
125
126 /*
127 * The load configuration settings below are tuned by default for
128 * the case where all devices are of the same rotational type.
129 *
130 * If there is a mixture of rotating and non-rotating media, setting
131 * zfs_vdev_mirror_non_rotating_seek_inc to 0 may well provide better results
132 * as it will direct more reads to the non-rotating vdevs which are more likely
133 * to have a higher performance.
134 */
135
136 /* Rotating media load calculation configuration. */
137 static int zfs_vdev_mirror_rotating_inc = 0;
138 static int zfs_vdev_mirror_rotating_seek_inc = 5;
139 static int zfs_vdev_mirror_rotating_seek_offset = 1 * 1024 * 1024;
140
141 /* Non-rotating media load calculation configuration. */
142 static int zfs_vdev_mirror_non_rotating_inc = 0;
143 static int zfs_vdev_mirror_non_rotating_seek_inc = 1;
144
145 static inline size_t
vdev_mirror_map_size(int children)146 vdev_mirror_map_size(int children)
147 {
148 return (offsetof(mirror_map_t, mm_child[children]) +
149 sizeof (int) * children);
150 }
151
152 static inline mirror_map_t *
vdev_mirror_map_alloc(int children,boolean_t resilvering,boolean_t root)153 vdev_mirror_map_alloc(int children, boolean_t resilvering, boolean_t root)
154 {
155 mirror_map_t *mm;
156
157 mm = kmem_zalloc(vdev_mirror_map_size(children), KM_SLEEP);
158 mm->mm_children = children;
159 mm->mm_resilvering = resilvering;
160 mm->mm_root = root;
161 mm->mm_preferred = (int *)((uintptr_t)mm +
162 offsetof(mirror_map_t, mm_child[children]));
163
164 return (mm);
165 }
166
167 static void
vdev_mirror_map_free(zio_t * zio)168 vdev_mirror_map_free(zio_t *zio)
169 {
170 mirror_map_t *mm = zio->io_vsd;
171
172 kmem_free(mm, vdev_mirror_map_size(mm->mm_children));
173 }
174
175 static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
176 .vsd_free = vdev_mirror_map_free,
177 .vsd_cksum_report = zio_vsd_default_cksum_report
178 };
179
180 static int
vdev_mirror_load(mirror_map_t * mm,vdev_t * vd,uint64_t zio_offset)181 vdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset)
182 {
183 uint64_t last_offset;
184 int64_t offset_diff;
185 int load;
186
187 /* All DVAs have equal weight at the root. */
188 if (mm->mm_root)
189 return (INT_MAX);
190
191 /*
192 * We don't return INT_MAX if the device is resilvering i.e.
193 * vdev_resilver_txg != 0 as when tested performance was slightly
194 * worse overall when resilvering with compared to without.
195 */
196
197 /* Fix zio_offset for leaf vdevs */
198 if (vd->vdev_ops->vdev_op_leaf)
199 zio_offset += VDEV_LABEL_START_SIZE;
200
201 /* Standard load based on pending queue length. */
202 load = vdev_queue_length(vd);
203 last_offset = vdev_queue_last_offset(vd);
204
205 if (vd->vdev_nonrot) {
206 /* Non-rotating media. */
207 if (last_offset == zio_offset) {
208 MIRROR_BUMP(vdev_mirror_stat_non_rotating_linear);
209 return (load + zfs_vdev_mirror_non_rotating_inc);
210 }
211
212 /*
213 * Apply a seek penalty even for non-rotating devices as
214 * sequential I/O's can be aggregated into fewer operations on
215 * the device, thus avoiding unnecessary per-command overhead
216 * and boosting performance.
217 */
218 MIRROR_BUMP(vdev_mirror_stat_non_rotating_seek);
219 return (load + zfs_vdev_mirror_non_rotating_seek_inc);
220 }
221
222 /* Rotating media I/O's which directly follow the last I/O. */
223 if (last_offset == zio_offset) {
224 MIRROR_BUMP(vdev_mirror_stat_rotating_linear);
225 return (load + zfs_vdev_mirror_rotating_inc);
226 }
227
228 /*
229 * Apply half the seek increment to I/O's within seek offset
230 * of the last I/O issued to this vdev as they should incur less
231 * of a seek increment.
232 */
233 offset_diff = (int64_t)(last_offset - zio_offset);
234 if (ABS(offset_diff) < zfs_vdev_mirror_rotating_seek_offset) {
235 MIRROR_BUMP(vdev_mirror_stat_rotating_offset);
236 return (load + (zfs_vdev_mirror_rotating_seek_inc / 2));
237 }
238
239 /* Apply the full seek increment to all other I/O's. */
240 MIRROR_BUMP(vdev_mirror_stat_rotating_seek);
241 return (load + zfs_vdev_mirror_rotating_seek_inc);
242 }
243
244 static boolean_t
vdev_mirror_rebuilding(vdev_t * vd)245 vdev_mirror_rebuilding(vdev_t *vd)
246 {
247 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_rebuild_txg)
248 return (B_TRUE);
249
250 for (int i = 0; i < vd->vdev_children; i++) {
251 if (vdev_mirror_rebuilding(vd->vdev_child[i])) {
252 return (B_TRUE);
253 }
254 }
255
256 return (B_FALSE);
257 }
258
259 /*
260 * Avoid inlining the function to keep vdev_mirror_io_start(), which
261 * is this functions only caller, as small as possible on the stack.
262 */
263 noinline static mirror_map_t *
vdev_mirror_map_init(zio_t * zio)264 vdev_mirror_map_init(zio_t *zio)
265 {
266 mirror_map_t *mm = NULL;
267 mirror_child_t *mc;
268 vdev_t *vd = zio->io_vd;
269 int c;
270
271 if (vd == NULL) {
272 dva_t *dva = zio->io_bp->blk_dva;
273 spa_t *spa = zio->io_spa;
274 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
275 dva_t dva_copy[SPA_DVAS_PER_BP];
276
277 /*
278 * The sequential scrub code sorts and issues all DVAs
279 * of a bp separately. Each of these IOs includes all
280 * original DVA copies so that repairs can be performed
281 * in the event of an error, but we only actually want
282 * to check the first DVA since the others will be
283 * checked by their respective sorted IOs. Only if we
284 * hit an error will we try all DVAs upon retrying.
285 *
286 * Note: This check is safe even if the user switches
287 * from a legacy scrub to a sequential one in the middle
288 * of processing, since scn_is_sorted isn't updated until
289 * all outstanding IOs from the previous scrub pass
290 * complete.
291 */
292 if ((zio->io_flags & ZIO_FLAG_SCRUB) &&
293 !(zio->io_flags & ZIO_FLAG_IO_RETRY) &&
294 dsl_scan_scrubbing(spa->spa_dsl_pool) &&
295 scn->scn_is_sorted) {
296 c = 1;
297 } else {
298 c = BP_GET_NDVAS(zio->io_bp);
299 }
300
301 /*
302 * If the pool cannot be written to, then infer that some
303 * DVAs might be invalid or point to vdevs that do not exist.
304 * We skip them.
305 */
306 if (!spa_writeable(spa)) {
307 ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
308 int j = 0;
309 for (int i = 0; i < c; i++) {
310 if (zfs_dva_valid(spa, &dva[i], zio->io_bp))
311 dva_copy[j++] = dva[i];
312 }
313 if (j == 0) {
314 zio->io_vsd = NULL;
315 zio->io_error = ENXIO;
316 return (NULL);
317 }
318 if (j < c) {
319 dva = dva_copy;
320 c = j;
321 }
322 }
323
324 mm = vdev_mirror_map_alloc(c, B_FALSE, B_TRUE);
325 for (c = 0; c < mm->mm_children; c++) {
326 mc = &mm->mm_child[c];
327
328 mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
329 mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
330 if (mc->mc_vd == NULL) {
331 kmem_free(mm, vdev_mirror_map_size(
332 mm->mm_children));
333 zio->io_vsd = NULL;
334 zio->io_error = ENXIO;
335 return (NULL);
336 }
337 }
338 } else {
339 /*
340 * If we are resilvering, then we should handle scrub reads
341 * differently; we shouldn't issue them to the resilvering
342 * device because it might not have those blocks.
343 *
344 * We are resilvering iff:
345 * 1) We are a replacing vdev (ie our name is "replacing-1" or
346 * "spare-1" or something like that), and
347 * 2) The pool is currently being resilvered.
348 *
349 * We cannot simply check vd->vdev_resilver_txg, because it's
350 * not set in this path.
351 *
352 * Nor can we just check our vdev_ops; there are cases (such as
353 * when a user types "zpool replace pool odev spare_dev" and
354 * spare_dev is in the spare list, or when a spare device is
355 * automatically used to replace a DEGRADED device) when
356 * resilvering is complete but both the original vdev and the
357 * spare vdev remain in the pool. That behavior is intentional.
358 * It helps implement the policy that a spare should be
359 * automatically removed from the pool after the user replaces
360 * the device that originally failed.
361 *
362 * If a spa load is in progress, then spa_dsl_pool may be
363 * uninitialized. But we shouldn't be resilvering during a spa
364 * load anyway.
365 */
366 boolean_t replacing = (vd->vdev_ops == &vdev_replacing_ops ||
367 vd->vdev_ops == &vdev_spare_ops) &&
368 spa_load_state(vd->vdev_spa) == SPA_LOAD_NONE &&
369 dsl_scan_resilvering(vd->vdev_spa->spa_dsl_pool);
370 mm = vdev_mirror_map_alloc(vd->vdev_children, replacing,
371 B_FALSE);
372 for (c = 0; c < mm->mm_children; c++) {
373 mc = &mm->mm_child[c];
374 mc->mc_vd = vd->vdev_child[c];
375 mc->mc_offset = zio->io_offset;
376
377 if (vdev_mirror_rebuilding(mc->mc_vd))
378 mm->mm_rebuilding = mc->mc_rebuilding = B_TRUE;
379 }
380 }
381
382 zio->io_vsd = mm;
383 zio->io_vsd_ops = &vdev_mirror_vsd_ops;
384 return (mm);
385 }
386
387 static int
vdev_mirror_open(vdev_t * vd,uint64_t * asize,uint64_t * max_asize,uint64_t * logical_ashift,uint64_t * physical_ashift)388 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
389 uint64_t *logical_ashift, uint64_t *physical_ashift)
390 {
391 int numerrors = 0;
392 int lasterror = 0;
393
394 if (vd->vdev_children == 0) {
395 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
396 return (SET_ERROR(EINVAL));
397 }
398
399 vdev_open_children(vd);
400
401 for (int c = 0; c < vd->vdev_children; c++) {
402 vdev_t *cvd = vd->vdev_child[c];
403
404 if (cvd->vdev_open_error) {
405 lasterror = cvd->vdev_open_error;
406 numerrors++;
407 continue;
408 }
409
410 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
411 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
412 *logical_ashift = MAX(*logical_ashift, cvd->vdev_ashift);
413 *physical_ashift = MAX(*physical_ashift,
414 cvd->vdev_physical_ashift);
415 }
416
417 if (numerrors == vd->vdev_children) {
418 if (vdev_children_are_offline(vd))
419 vd->vdev_stat.vs_aux = VDEV_AUX_CHILDREN_OFFLINE;
420 else
421 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
422 return (lasterror);
423 }
424
425 return (0);
426 }
427
428 static void
vdev_mirror_close(vdev_t * vd)429 vdev_mirror_close(vdev_t *vd)
430 {
431 for (int c = 0; c < vd->vdev_children; c++)
432 vdev_close(vd->vdev_child[c]);
433 }
434
435 static void
vdev_mirror_child_done(zio_t * zio)436 vdev_mirror_child_done(zio_t *zio)
437 {
438 mirror_child_t *mc = zio->io_private;
439
440 mc->mc_error = zio->io_error;
441 mc->mc_tried = 1;
442 mc->mc_skipped = 0;
443 }
444
445 static void
vdev_mirror_scrub_done(zio_t * zio)446 vdev_mirror_scrub_done(zio_t *zio)
447 {
448 mirror_child_t *mc = zio->io_private;
449
450 if (zio->io_error == 0) {
451 zio_t *pio;
452 zio_link_t *zl = NULL;
453
454 mutex_enter(&zio->io_lock);
455 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
456 mutex_enter(&pio->io_lock);
457 ASSERT3U(zio->io_size, >=, pio->io_size);
458 abd_copy(pio->io_abd, zio->io_abd, pio->io_size);
459 mutex_exit(&pio->io_lock);
460 }
461 mutex_exit(&zio->io_lock);
462 }
463
464 abd_free(zio->io_abd);
465
466 mc->mc_error = zio->io_error;
467 mc->mc_tried = 1;
468 mc->mc_skipped = 0;
469 }
470
471 /*
472 * Check the other, lower-index DVAs to see if they're on the same
473 * vdev as the child we picked. If they are, use them since they
474 * are likely to have been allocated from the primary metaslab in
475 * use at the time, and hence are more likely to have locality with
476 * single-copy data.
477 */
478 static int
vdev_mirror_dva_select(zio_t * zio,int p)479 vdev_mirror_dva_select(zio_t *zio, int p)
480 {
481 dva_t *dva = zio->io_bp->blk_dva;
482 mirror_map_t *mm = zio->io_vsd;
483 int preferred;
484 int c;
485
486 preferred = mm->mm_preferred[p];
487 for (p--; p >= 0; p--) {
488 c = mm->mm_preferred[p];
489 if (DVA_GET_VDEV(&dva[c]) == DVA_GET_VDEV(&dva[preferred]))
490 preferred = c;
491 }
492 return (preferred);
493 }
494
495 static int
vdev_mirror_preferred_child_randomize(zio_t * zio)496 vdev_mirror_preferred_child_randomize(zio_t *zio)
497 {
498 mirror_map_t *mm = zio->io_vsd;
499 int p;
500
501 if (mm->mm_root) {
502 p = spa_get_random(mm->mm_preferred_cnt);
503 return (vdev_mirror_dva_select(zio, p));
504 }
505
506 /*
507 * To ensure we don't always favour the first matching vdev,
508 * which could lead to wear leveling issues on SSD's, we
509 * use the I/O offset as a pseudo random seed into the vdevs
510 * which have the lowest load.
511 */
512 p = (zio->io_offset >> vdev_mirror_shift) % mm->mm_preferred_cnt;
513 return (mm->mm_preferred[p]);
514 }
515
516 static boolean_t
vdev_mirror_child_readable(mirror_child_t * mc)517 vdev_mirror_child_readable(mirror_child_t *mc)
518 {
519 vdev_t *vd = mc->mc_vd;
520
521 if (vd->vdev_top != NULL && vd->vdev_top->vdev_ops == &vdev_draid_ops)
522 return (vdev_draid_readable(vd, mc->mc_offset));
523 else
524 return (vdev_readable(vd));
525 }
526
527 static boolean_t
vdev_mirror_child_missing(mirror_child_t * mc,uint64_t txg,uint64_t size)528 vdev_mirror_child_missing(mirror_child_t *mc, uint64_t txg, uint64_t size)
529 {
530 vdev_t *vd = mc->mc_vd;
531
532 if (vd->vdev_top != NULL && vd->vdev_top->vdev_ops == &vdev_draid_ops)
533 return (vdev_draid_missing(vd, mc->mc_offset, txg, size));
534 else
535 return (vdev_dtl_contains(vd, DTL_MISSING, txg, size));
536 }
537
538 /*
539 * Try to find a vdev whose DTL doesn't contain the block we want to read
540 * preferring vdevs based on determined load. If we can't, try the read on
541 * any vdev we haven't already tried.
542 *
543 * Distributed spares are an exception to the above load rule. They are
544 * always preferred in order to detect gaps in the distributed spare which
545 * are created when another disk in the dRAID fails. In order to restore
546 * redundancy those gaps must be read to trigger the required repair IO.
547 */
548 static int
vdev_mirror_child_select(zio_t * zio)549 vdev_mirror_child_select(zio_t *zio)
550 {
551 mirror_map_t *mm = zio->io_vsd;
552 uint64_t txg = zio->io_txg;
553 int c, lowest_load;
554
555 ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
556
557 lowest_load = INT_MAX;
558 mm->mm_preferred_cnt = 0;
559 for (c = 0; c < mm->mm_children; c++) {
560 mirror_child_t *mc;
561
562 mc = &mm->mm_child[c];
563 if (mc->mc_tried || mc->mc_skipped)
564 continue;
565
566 if (mc->mc_vd == NULL ||
567 !vdev_mirror_child_readable(mc)) {
568 mc->mc_error = SET_ERROR(ENXIO);
569 mc->mc_tried = 1; /* don't even try */
570 mc->mc_skipped = 1;
571 continue;
572 }
573
574 if (vdev_mirror_child_missing(mc, txg, 1)) {
575 mc->mc_error = SET_ERROR(ESTALE);
576 mc->mc_skipped = 1;
577 mc->mc_speculative = 1;
578 continue;
579 }
580
581 if (mc->mc_vd->vdev_ops == &vdev_draid_spare_ops) {
582 mm->mm_preferred[0] = c;
583 mm->mm_preferred_cnt = 1;
584 break;
585 }
586
587 mc->mc_load = vdev_mirror_load(mm, mc->mc_vd, mc->mc_offset);
588 if (mc->mc_load > lowest_load)
589 continue;
590
591 if (mc->mc_load < lowest_load) {
592 lowest_load = mc->mc_load;
593 mm->mm_preferred_cnt = 0;
594 }
595 mm->mm_preferred[mm->mm_preferred_cnt] = c;
596 mm->mm_preferred_cnt++;
597 }
598
599 if (mm->mm_preferred_cnt == 1) {
600 MIRROR_BUMP(vdev_mirror_stat_preferred_found);
601 return (mm->mm_preferred[0]);
602 }
603
604 if (mm->mm_preferred_cnt > 1) {
605 MIRROR_BUMP(vdev_mirror_stat_preferred_not_found);
606 return (vdev_mirror_preferred_child_randomize(zio));
607 }
608
609 /*
610 * Every device is either missing or has this txg in its DTL.
611 * Look for any child we haven't already tried before giving up.
612 */
613 for (c = 0; c < mm->mm_children; c++) {
614 if (!mm->mm_child[c].mc_tried)
615 return (c);
616 }
617
618 /*
619 * Every child failed. There's no place left to look.
620 */
621 return (-1);
622 }
623
624 static void
vdev_mirror_io_start(zio_t * zio)625 vdev_mirror_io_start(zio_t *zio)
626 {
627 mirror_map_t *mm;
628 mirror_child_t *mc;
629 int c, children;
630
631 mm = vdev_mirror_map_init(zio);
632
633 if (mm == NULL) {
634 ASSERT(!spa_trust_config(zio->io_spa));
635 ASSERT(zio->io_type == ZIO_TYPE_READ);
636 zio_execute(zio);
637 return;
638 }
639
640 if (zio->io_type == ZIO_TYPE_READ) {
641 if (zio->io_bp != NULL &&
642 (zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_resilvering) {
643 /*
644 * For scrubbing reads (if we can verify the
645 * checksum here, as indicated by io_bp being
646 * non-NULL) we need to allocate a read buffer for
647 * each child and issue reads to all children. If
648 * any child succeeds, it will copy its data into
649 * zio->io_data in vdev_mirror_scrub_done.
650 */
651 for (c = 0; c < mm->mm_children; c++) {
652 mc = &mm->mm_child[c];
653 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
654 mc->mc_vd, mc->mc_offset,
655 abd_alloc_sametype(zio->io_abd,
656 zio->io_size), zio->io_size,
657 zio->io_type, zio->io_priority, 0,
658 vdev_mirror_scrub_done, mc));
659 }
660 zio_execute(zio);
661 return;
662 }
663 /*
664 * For normal reads just pick one child.
665 */
666 c = vdev_mirror_child_select(zio);
667 children = (c >= 0);
668 } else {
669 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
670
671 /*
672 * Writes go to all children.
673 */
674 c = 0;
675 children = mm->mm_children;
676 }
677
678 while (children--) {
679 mc = &mm->mm_child[c];
680 c++;
681
682 /*
683 * When sequentially resilvering only issue write repair
684 * IOs to the vdev which is being rebuilt since performance
685 * is limited by the slowest child. This is an issue for
686 * faster replacement devices such as distributed spares.
687 */
688 if ((zio->io_priority == ZIO_PRIORITY_REBUILD) &&
689 (zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
690 !(zio->io_flags & ZIO_FLAG_SCRUB) &&
691 mm->mm_rebuilding && !mc->mc_rebuilding) {
692 continue;
693 }
694
695 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
696 mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
697 zio->io_type, zio->io_priority, 0,
698 vdev_mirror_child_done, mc));
699 }
700
701 zio_execute(zio);
702 }
703
704 static int
vdev_mirror_worst_error(mirror_map_t * mm)705 vdev_mirror_worst_error(mirror_map_t *mm)
706 {
707 int error[2] = { 0, 0 };
708
709 for (int c = 0; c < mm->mm_children; c++) {
710 mirror_child_t *mc = &mm->mm_child[c];
711 int s = mc->mc_speculative;
712 error[s] = zio_worst_error(error[s], mc->mc_error);
713 }
714
715 return (error[0] ? error[0] : error[1]);
716 }
717
718 static void
vdev_mirror_io_done(zio_t * zio)719 vdev_mirror_io_done(zio_t *zio)
720 {
721 mirror_map_t *mm = zio->io_vsd;
722 mirror_child_t *mc;
723 int c;
724 int good_copies = 0;
725 int unexpected_errors = 0;
726
727 if (mm == NULL)
728 return;
729
730 for (c = 0; c < mm->mm_children; c++) {
731 mc = &mm->mm_child[c];
732
733 if (mc->mc_error) {
734 if (!mc->mc_skipped)
735 unexpected_errors++;
736 } else if (mc->mc_tried) {
737 good_copies++;
738 }
739 }
740
741 if (zio->io_type == ZIO_TYPE_WRITE) {
742 /*
743 * XXX -- for now, treat partial writes as success.
744 *
745 * Now that we support write reallocation, it would be better
746 * to treat partial failure as real failure unless there are
747 * no non-degraded top-level vdevs left, and not update DTLs
748 * if we intend to reallocate.
749 */
750 /* XXPOLICY */
751 if (good_copies != mm->mm_children) {
752 /*
753 * Always require at least one good copy.
754 *
755 * For ditto blocks (io_vd == NULL), require
756 * all copies to be good.
757 *
758 * XXX -- for replacing vdevs, there's no great answer.
759 * If the old device is really dead, we may not even
760 * be able to access it -- so we only want to
761 * require good writes to the new device. But if
762 * the new device turns out to be flaky, we want
763 * to be able to detach it -- which requires all
764 * writes to the old device to have succeeded.
765 */
766 if (good_copies == 0 || zio->io_vd == NULL)
767 zio->io_error = vdev_mirror_worst_error(mm);
768 }
769 return;
770 }
771
772 ASSERT(zio->io_type == ZIO_TYPE_READ);
773
774 /*
775 * If we don't have a good copy yet, keep trying other children.
776 */
777 /* XXPOLICY */
778 if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
779 ASSERT(c >= 0 && c < mm->mm_children);
780 mc = &mm->mm_child[c];
781 zio_vdev_io_redone(zio);
782 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
783 mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
784 ZIO_TYPE_READ, zio->io_priority, 0,
785 vdev_mirror_child_done, mc));
786 return;
787 }
788
789 /* XXPOLICY */
790 if (good_copies == 0) {
791 zio->io_error = vdev_mirror_worst_error(mm);
792 ASSERT(zio->io_error != 0);
793 }
794
795 if (good_copies && spa_writeable(zio->io_spa) &&
796 (unexpected_errors ||
797 (zio->io_flags & ZIO_FLAG_RESILVER) ||
798 ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_resilvering))) {
799 /*
800 * Use the good data we have in hand to repair damaged children.
801 */
802 for (c = 0; c < mm->mm_children; c++) {
803 /*
804 * Don't rewrite known good children.
805 * Not only is it unnecessary, it could
806 * actually be harmful: if the system lost
807 * power while rewriting the only good copy,
808 * there would be no good copies left!
809 */
810 mc = &mm->mm_child[c];
811
812 if (mc->mc_error == 0) {
813 vdev_ops_t *ops = mc->mc_vd->vdev_ops;
814
815 if (mc->mc_tried)
816 continue;
817 /*
818 * We didn't try this child. We need to
819 * repair it if:
820 * 1. it's a scrub (in which case we have
821 * tried everything that was healthy)
822 * - or -
823 * 2. it's an indirect or distributed spare
824 * vdev (in which case it could point to any
825 * other vdev, which might have a bad DTL)
826 * - or -
827 * 3. the DTL indicates that this data is
828 * missing from this vdev
829 */
830 if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
831 ops != &vdev_indirect_ops &&
832 ops != &vdev_draid_spare_ops &&
833 !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
834 zio->io_txg, 1))
835 continue;
836 mc->mc_error = SET_ERROR(ESTALE);
837 }
838
839 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
840 mc->mc_vd, mc->mc_offset,
841 zio->io_abd, zio->io_size, ZIO_TYPE_WRITE,
842 zio->io_priority == ZIO_PRIORITY_REBUILD ?
843 ZIO_PRIORITY_REBUILD : ZIO_PRIORITY_ASYNC_WRITE,
844 ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
845 ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
846 }
847 }
848 }
849
850 static void
vdev_mirror_state_change(vdev_t * vd,int faulted,int degraded)851 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
852 {
853 if (faulted == vd->vdev_children) {
854 if (vdev_children_are_offline(vd)) {
855 vdev_set_state(vd, B_FALSE, VDEV_STATE_OFFLINE,
856 VDEV_AUX_CHILDREN_OFFLINE);
857 } else {
858 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
859 VDEV_AUX_NO_REPLICAS);
860 }
861 } else if (degraded + faulted != 0) {
862 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
863 } else {
864 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
865 }
866 }
867
868 /*
869 * Return the maximum asize for a rebuild zio in the provided range.
870 */
871 static uint64_t
vdev_mirror_rebuild_asize(vdev_t * vd,uint64_t start,uint64_t asize,uint64_t max_segment)872 vdev_mirror_rebuild_asize(vdev_t *vd, uint64_t start, uint64_t asize,
873 uint64_t max_segment)
874 {
875 uint64_t psize = MIN(P2ROUNDUP(max_segment, 1 << vd->vdev_ashift),
876 SPA_MAXBLOCKSIZE);
877
878 return (MIN(asize, vdev_psize_to_asize(vd, psize)));
879 }
880
881 vdev_ops_t vdev_mirror_ops = {
882 .vdev_op_init = NULL,
883 .vdev_op_fini = NULL,
884 .vdev_op_open = vdev_mirror_open,
885 .vdev_op_close = vdev_mirror_close,
886 .vdev_op_asize = vdev_default_asize,
887 .vdev_op_min_asize = vdev_default_min_asize,
888 .vdev_op_min_alloc = NULL,
889 .vdev_op_io_start = vdev_mirror_io_start,
890 .vdev_op_io_done = vdev_mirror_io_done,
891 .vdev_op_state_change = vdev_mirror_state_change,
892 .vdev_op_need_resilver = vdev_default_need_resilver,
893 .vdev_op_hold = NULL,
894 .vdev_op_rele = NULL,
895 .vdev_op_remap = NULL,
896 .vdev_op_xlate = vdev_default_xlate,
897 .vdev_op_rebuild_asize = vdev_mirror_rebuild_asize,
898 .vdev_op_metaslab_init = NULL,
899 .vdev_op_config_generate = NULL,
900 .vdev_op_nparity = NULL,
901 .vdev_op_ndisks = NULL,
902 .vdev_op_type = VDEV_TYPE_MIRROR, /* name of this vdev type */
903 .vdev_op_leaf = B_FALSE /* not a leaf vdev */
904 };
905
906 vdev_ops_t vdev_replacing_ops = {
907 .vdev_op_init = NULL,
908 .vdev_op_fini = NULL,
909 .vdev_op_open = vdev_mirror_open,
910 .vdev_op_close = vdev_mirror_close,
911 .vdev_op_asize = vdev_default_asize,
912 .vdev_op_min_asize = vdev_default_min_asize,
913 .vdev_op_min_alloc = NULL,
914 .vdev_op_io_start = vdev_mirror_io_start,
915 .vdev_op_io_done = vdev_mirror_io_done,
916 .vdev_op_state_change = vdev_mirror_state_change,
917 .vdev_op_need_resilver = vdev_default_need_resilver,
918 .vdev_op_hold = NULL,
919 .vdev_op_rele = NULL,
920 .vdev_op_remap = NULL,
921 .vdev_op_xlate = vdev_default_xlate,
922 .vdev_op_rebuild_asize = vdev_mirror_rebuild_asize,
923 .vdev_op_metaslab_init = NULL,
924 .vdev_op_config_generate = NULL,
925 .vdev_op_nparity = NULL,
926 .vdev_op_ndisks = NULL,
927 .vdev_op_type = VDEV_TYPE_REPLACING, /* name of this vdev type */
928 .vdev_op_leaf = B_FALSE /* not a leaf vdev */
929 };
930
931 vdev_ops_t vdev_spare_ops = {
932 .vdev_op_init = NULL,
933 .vdev_op_fini = NULL,
934 .vdev_op_open = vdev_mirror_open,
935 .vdev_op_close = vdev_mirror_close,
936 .vdev_op_asize = vdev_default_asize,
937 .vdev_op_min_asize = vdev_default_min_asize,
938 .vdev_op_min_alloc = NULL,
939 .vdev_op_io_start = vdev_mirror_io_start,
940 .vdev_op_io_done = vdev_mirror_io_done,
941 .vdev_op_state_change = vdev_mirror_state_change,
942 .vdev_op_need_resilver = vdev_default_need_resilver,
943 .vdev_op_hold = NULL,
944 .vdev_op_rele = NULL,
945 .vdev_op_remap = NULL,
946 .vdev_op_xlate = vdev_default_xlate,
947 .vdev_op_rebuild_asize = vdev_mirror_rebuild_asize,
948 .vdev_op_metaslab_init = NULL,
949 .vdev_op_config_generate = NULL,
950 .vdev_op_nparity = NULL,
951 .vdev_op_ndisks = NULL,
952 .vdev_op_type = VDEV_TYPE_SPARE, /* name of this vdev type */
953 .vdev_op_leaf = B_FALSE /* not a leaf vdev */
954 };
955
956 /* BEGIN CSTYLED */
957 ZFS_MODULE_PARAM(zfs_vdev_mirror, zfs_vdev_mirror_, rotating_inc, INT, ZMOD_RW,
958 "Rotating media load increment for non-seeking I/O's");
959
960 ZFS_MODULE_PARAM(zfs_vdev_mirror, zfs_vdev_mirror_, rotating_seek_inc, INT, ZMOD_RW,
961 "Rotating media load increment for seeking I/O's");
962
963 ZFS_MODULE_PARAM(zfs_vdev_mirror, zfs_vdev_mirror_, rotating_seek_offset, INT, ZMOD_RW,
964 "Offset in bytes from the last I/O which triggers "
965 "a reduced rotating media seek increment");
966
967 ZFS_MODULE_PARAM(zfs_vdev_mirror, zfs_vdev_mirror_, non_rotating_inc, INT, ZMOD_RW,
968 "Non-rotating media load increment for non-seeking I/O's");
969
970 ZFS_MODULE_PARAM(zfs_vdev_mirror, zfs_vdev_mirror_, non_rotating_seek_inc, INT, ZMOD_RW,
971 "Non-rotating media load increment for seeking I/O's");
972 /* END CSTYLED */
973