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