]> granicus.if.org Git - zfs/blob - module/zfs/vdev_mirror.c
Fix 0 byte memory leak in zfs receive
[zfs] / 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                 dva_t dva_copy[SPA_DVAS_PER_BP];
258
259                 c = BP_GET_NDVAS(zio->io_bp);
260
261                 /*
262                  * If we do not trust the pool config, some DVAs might be
263                  * invalid or point to vdevs that do not exist. We skip them.
264                  */
265                 if (!spa_trust_config(spa)) {
266                         ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
267                         int j = 0;
268                         for (int i = 0; i < c; i++) {
269                                 if (zfs_dva_valid(spa, &dva[i], zio->io_bp))
270                                         dva_copy[j++] = dva[i];
271                         }
272                         if (j == 0) {
273                                 zio->io_vsd = NULL;
274                                 zio->io_error = ENXIO;
275                                 return (NULL);
276                         }
277                         if (j < c) {
278                                 dva = dva_copy;
279                                 c = j;
280                         }
281                 }
282
283                 mm = vdev_mirror_map_alloc(c, B_FALSE, B_TRUE);
284                 for (c = 0; c < mm->mm_children; c++) {
285                         mc = &mm->mm_child[c];
286
287                         mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
288                         mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
289                 }
290         } else {
291                 /*
292                  * If we are resilvering, then we should handle scrub reads
293                  * differently; we shouldn't issue them to the resilvering
294                  * device because it might not have those blocks.
295                  *
296                  * We are resilvering iff:
297                  * 1) We are a replacing vdev (ie our name is "replacing-1" or
298                  *    "spare-1" or something like that), and
299                  * 2) The pool is currently being resilvered.
300                  *
301                  * We cannot simply check vd->vdev_resilver_txg, because it's
302                  * not set in this path.
303                  *
304                  * Nor can we just check our vdev_ops; there are cases (such as
305                  * when a user types "zpool replace pool odev spare_dev" and
306                  * spare_dev is in the spare list, or when a spare device is
307                  * automatically used to replace a DEGRADED device) when
308                  * resilvering is complete but both the original vdev and the
309                  * spare vdev remain in the pool.  That behavior is intentional.
310                  * It helps implement the policy that a spare should be
311                  * automatically removed from the pool after the user replaces
312                  * the device that originally failed.
313                  *
314                  * If a spa load is in progress, then spa_dsl_pool may be
315                  * uninitialized.  But we shouldn't be resilvering during a spa
316                  * load anyway.
317                  */
318                 boolean_t replacing = (vd->vdev_ops == &vdev_replacing_ops ||
319                     vd->vdev_ops == &vdev_spare_ops) &&
320                     spa_load_state(vd->vdev_spa) == SPA_LOAD_NONE &&
321                     dsl_scan_resilvering(vd->vdev_spa->spa_dsl_pool);
322                 mm = vdev_mirror_map_alloc(vd->vdev_children, replacing,
323                     B_FALSE);
324                 for (c = 0; c < mm->mm_children; c++) {
325                         mc = &mm->mm_child[c];
326                         mc->mc_vd = vd->vdev_child[c];
327                         mc->mc_offset = zio->io_offset;
328                 }
329         }
330
331         zio->io_vsd = mm;
332         zio->io_vsd_ops = &vdev_mirror_vsd_ops;
333         return (mm);
334 }
335
336 static int
337 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
338     uint64_t *ashift)
339 {
340         int numerrors = 0;
341         int lasterror = 0;
342
343         if (vd->vdev_children == 0) {
344                 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
345                 return (SET_ERROR(EINVAL));
346         }
347
348         vdev_open_children(vd);
349
350         for (int c = 0; c < vd->vdev_children; c++) {
351                 vdev_t *cvd = vd->vdev_child[c];
352
353                 if (cvd->vdev_open_error) {
354                         lasterror = cvd->vdev_open_error;
355                         numerrors++;
356                         continue;
357                 }
358
359                 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
360                 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
361                 *ashift = MAX(*ashift, cvd->vdev_ashift);
362         }
363
364         if (numerrors == vd->vdev_children) {
365                 if (vdev_children_are_offline(vd))
366                         vd->vdev_stat.vs_aux = VDEV_AUX_CHILDREN_OFFLINE;
367                 else
368                         vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
369                 return (lasterror);
370         }
371
372         return (0);
373 }
374
375 static void
376 vdev_mirror_close(vdev_t *vd)
377 {
378         for (int c = 0; c < vd->vdev_children; c++)
379                 vdev_close(vd->vdev_child[c]);
380 }
381
382 static void
383 vdev_mirror_child_done(zio_t *zio)
384 {
385         mirror_child_t *mc = zio->io_private;
386
387         mc->mc_error = zio->io_error;
388         mc->mc_tried = 1;
389         mc->mc_skipped = 0;
390 }
391
392 static void
393 vdev_mirror_scrub_done(zio_t *zio)
394 {
395         mirror_child_t *mc = zio->io_private;
396
397         if (zio->io_error == 0) {
398                 zio_t *pio;
399                 zio_link_t *zl = NULL;
400
401                 mutex_enter(&zio->io_lock);
402                 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
403                         mutex_enter(&pio->io_lock);
404                         ASSERT3U(zio->io_size, >=, pio->io_size);
405                         abd_copy(pio->io_abd, zio->io_abd, pio->io_size);
406                         mutex_exit(&pio->io_lock);
407                 }
408                 mutex_exit(&zio->io_lock);
409         }
410
411         abd_free(zio->io_abd);
412
413         mc->mc_error = zio->io_error;
414         mc->mc_tried = 1;
415         mc->mc_skipped = 0;
416 }
417
418 /*
419  * Check the other, lower-index DVAs to see if they're on the same
420  * vdev as the child we picked.  If they are, use them since they
421  * are likely to have been allocated from the primary metaslab in
422  * use at the time, and hence are more likely to have locality with
423  * single-copy data.
424  */
425 static int
426 vdev_mirror_dva_select(zio_t *zio, int p)
427 {
428         dva_t *dva = zio->io_bp->blk_dva;
429         mirror_map_t *mm = zio->io_vsd;
430         int preferred;
431         int c;
432
433         preferred = mm->mm_preferred[p];
434         for (p--; p >= 0; p--) {
435                 c = mm->mm_preferred[p];
436                 if (DVA_GET_VDEV(&dva[c]) == DVA_GET_VDEV(&dva[preferred]))
437                         preferred = c;
438         }
439         return (preferred);
440 }
441
442 static int
443 vdev_mirror_preferred_child_randomize(zio_t *zio)
444 {
445         mirror_map_t *mm = zio->io_vsd;
446         int p;
447
448         if (mm->mm_root) {
449                 p = spa_get_random(mm->mm_preferred_cnt);
450                 return (vdev_mirror_dva_select(zio, p));
451         }
452
453         /*
454          * To ensure we don't always favour the first matching vdev,
455          * which could lead to wear leveling issues on SSD's, we
456          * use the I/O offset as a pseudo random seed into the vdevs
457          * which have the lowest load.
458          */
459         p = (zio->io_offset >> vdev_mirror_shift) % mm->mm_preferred_cnt;
460         return (mm->mm_preferred[p]);
461 }
462
463 /*
464  * Try to find a vdev whose DTL doesn't contain the block we want to read
465  * prefering vdevs based on determined load.
466  *
467  * Try to find a child whose DTL doesn't contain the block we want to read.
468  * If we can't, try the read on any vdev we haven't already tried.
469  */
470 static int
471 vdev_mirror_child_select(zio_t *zio)
472 {
473         mirror_map_t *mm = zio->io_vsd;
474         uint64_t txg = zio->io_txg;
475         int c, lowest_load;
476
477         ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
478
479         lowest_load = INT_MAX;
480         mm->mm_preferred_cnt = 0;
481         for (c = 0; c < mm->mm_children; c++) {
482                 mirror_child_t *mc;
483
484                 mc = &mm->mm_child[c];
485                 if (mc->mc_tried || mc->mc_skipped)
486                         continue;
487
488                 if (mc->mc_vd == NULL || !vdev_readable(mc->mc_vd)) {
489                         mc->mc_error = SET_ERROR(ENXIO);
490                         mc->mc_tried = 1;       /* don't even try */
491                         mc->mc_skipped = 1;
492                         continue;
493                 }
494
495                 if (vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) {
496                         mc->mc_error = SET_ERROR(ESTALE);
497                         mc->mc_skipped = 1;
498                         mc->mc_speculative = 1;
499                         continue;
500                 }
501
502                 mc->mc_load = vdev_mirror_load(mm, mc->mc_vd, mc->mc_offset);
503                 if (mc->mc_load > lowest_load)
504                         continue;
505
506                 if (mc->mc_load < lowest_load) {
507                         lowest_load = mc->mc_load;
508                         mm->mm_preferred_cnt = 0;
509                 }
510                 mm->mm_preferred[mm->mm_preferred_cnt] = c;
511                 mm->mm_preferred_cnt++;
512         }
513
514         if (mm->mm_preferred_cnt == 1) {
515                 MIRROR_BUMP(vdev_mirror_stat_preferred_found);
516                 return (mm->mm_preferred[0]);
517         }
518
519         if (mm->mm_preferred_cnt > 1) {
520                 MIRROR_BUMP(vdev_mirror_stat_preferred_not_found);
521                 return (vdev_mirror_preferred_child_randomize(zio));
522         }
523
524         /*
525          * Every device is either missing or has this txg in its DTL.
526          * Look for any child we haven't already tried before giving up.
527          */
528         for (c = 0; c < mm->mm_children; c++) {
529                 if (!mm->mm_child[c].mc_tried)
530                         return (c);
531         }
532
533         /*
534          * Every child failed.  There's no place left to look.
535          */
536         return (-1);
537 }
538
539 static void
540 vdev_mirror_io_start(zio_t *zio)
541 {
542         mirror_map_t *mm;
543         mirror_child_t *mc;
544         int c, children;
545
546         mm = vdev_mirror_map_init(zio);
547
548         if (mm == NULL) {
549                 ASSERT(!spa_trust_config(zio->io_spa));
550                 ASSERT(zio->io_type == ZIO_TYPE_READ);
551                 zio_execute(zio);
552                 return;
553         }
554
555         if (zio->io_type == ZIO_TYPE_READ) {
556                 if (zio->io_bp != NULL &&
557                     (zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_resilvering) {
558                         /*
559                          * For scrubbing reads (if we can verify the
560                          * checksum here, as indicated by io_bp being
561                          * non-NULL) we need to allocate a read buffer for
562                          * each child and issue reads to all children.  If
563                          * any child succeeds, it will copy its data into
564                          * zio->io_data in vdev_mirror_scrub_done.
565                          */
566                         for (c = 0; c < mm->mm_children; c++) {
567                                 mc = &mm->mm_child[c];
568                                 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
569                                     mc->mc_vd, mc->mc_offset,
570                                     abd_alloc_sametype(zio->io_abd,
571                                     zio->io_size), zio->io_size,
572                                     zio->io_type, zio->io_priority, 0,
573                                     vdev_mirror_scrub_done, mc));
574                         }
575                         zio_execute(zio);
576                         return;
577                 }
578                 /*
579                  * For normal reads just pick one child.
580                  */
581                 c = vdev_mirror_child_select(zio);
582                 children = (c >= 0);
583         } else {
584                 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
585
586                 /*
587                  * Writes go to all children.
588                  */
589                 c = 0;
590                 children = mm->mm_children;
591         }
592
593         while (children--) {
594                 mc = &mm->mm_child[c];
595                 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
596                     mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
597                     zio->io_type, zio->io_priority, 0,
598                     vdev_mirror_child_done, mc));
599                 c++;
600         }
601
602         zio_execute(zio);
603 }
604
605 static int
606 vdev_mirror_worst_error(mirror_map_t *mm)
607 {
608         int error[2] = { 0, 0 };
609
610         for (int c = 0; c < mm->mm_children; c++) {
611                 mirror_child_t *mc = &mm->mm_child[c];
612                 int s = mc->mc_speculative;
613                 error[s] = zio_worst_error(error[s], mc->mc_error);
614         }
615
616         return (error[0] ? error[0] : error[1]);
617 }
618
619 static void
620 vdev_mirror_io_done(zio_t *zio)
621 {
622         mirror_map_t *mm = zio->io_vsd;
623         mirror_child_t *mc;
624         int c;
625         int good_copies = 0;
626         int unexpected_errors = 0;
627
628         if (mm == NULL)
629                 return;
630
631         for (c = 0; c < mm->mm_children; c++) {
632                 mc = &mm->mm_child[c];
633
634                 if (mc->mc_error) {
635                         if (!mc->mc_skipped)
636                                 unexpected_errors++;
637                 } else if (mc->mc_tried) {
638                         good_copies++;
639                 }
640         }
641
642         if (zio->io_type == ZIO_TYPE_WRITE) {
643                 /*
644                  * XXX -- for now, treat partial writes as success.
645                  *
646                  * Now that we support write reallocation, it would be better
647                  * to treat partial failure as real failure unless there are
648                  * no non-degraded top-level vdevs left, and not update DTLs
649                  * if we intend to reallocate.
650                  */
651                 /* XXPOLICY */
652                 if (good_copies != mm->mm_children) {
653                         /*
654                          * Always require at least one good copy.
655                          *
656                          * For ditto blocks (io_vd == NULL), require
657                          * all copies to be good.
658                          *
659                          * XXX -- for replacing vdevs, there's no great answer.
660                          * If the old device is really dead, we may not even
661                          * be able to access it -- so we only want to
662                          * require good writes to the new device.  But if
663                          * the new device turns out to be flaky, we want
664                          * to be able to detach it -- which requires all
665                          * writes to the old device to have succeeded.
666                          */
667                         if (good_copies == 0 || zio->io_vd == NULL)
668                                 zio->io_error = vdev_mirror_worst_error(mm);
669                 }
670                 return;
671         }
672
673         ASSERT(zio->io_type == ZIO_TYPE_READ);
674
675         /*
676          * If we don't have a good copy yet, keep trying other children.
677          */
678         /* XXPOLICY */
679         if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
680                 ASSERT(c >= 0 && c < mm->mm_children);
681                 mc = &mm->mm_child[c];
682                 zio_vdev_io_redone(zio);
683                 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
684                     mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
685                     ZIO_TYPE_READ, zio->io_priority, 0,
686                     vdev_mirror_child_done, mc));
687                 return;
688         }
689
690         /* XXPOLICY */
691         if (good_copies == 0) {
692                 zio->io_error = vdev_mirror_worst_error(mm);
693                 ASSERT(zio->io_error != 0);
694         }
695
696         if (good_copies && spa_writeable(zio->io_spa) &&
697             (unexpected_errors ||
698             (zio->io_flags & ZIO_FLAG_RESILVER) ||
699             ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_resilvering))) {
700                 /*
701                  * Use the good data we have in hand to repair damaged children.
702                  */
703                 for (c = 0; c < mm->mm_children; c++) {
704                         /*
705                          * Don't rewrite known good children.
706                          * Not only is it unnecessary, it could
707                          * actually be harmful: if the system lost
708                          * power while rewriting the only good copy,
709                          * there would be no good copies left!
710                          */
711                         mc = &mm->mm_child[c];
712
713                         if (mc->mc_error == 0) {
714                                 if (mc->mc_tried)
715                                         continue;
716                                 /*
717                                  * We didn't try this child.  We need to
718                                  * repair it if:
719                                  * 1. it's a scrub (in which case we have
720                                  * tried everything that was healthy)
721                                  *  - or -
722                                  * 2. it's an indirect vdev (in which case
723                                  * it could point to any other vdev, which
724                                  * might have a bad DTL)
725                                  *  - or -
726                                  * 3. the DTL indicates that this data is
727                                  * missing from this vdev
728                                  */
729                                 if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
730                                     mc->mc_vd->vdev_ops != &vdev_indirect_ops &&
731                                     !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
732                                     zio->io_txg, 1))
733                                         continue;
734                                 mc->mc_error = SET_ERROR(ESTALE);
735                         }
736
737                         zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
738                             mc->mc_vd, mc->mc_offset,
739                             zio->io_abd, zio->io_size,
740                             ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
741                             ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
742                             ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
743                 }
744         }
745 }
746
747 static void
748 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
749 {
750         if (faulted == vd->vdev_children) {
751                 if (vdev_children_are_offline(vd)) {
752                         vdev_set_state(vd, B_FALSE, VDEV_STATE_OFFLINE,
753                             VDEV_AUX_CHILDREN_OFFLINE);
754                 } else {
755                         vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
756                             VDEV_AUX_NO_REPLICAS);
757                 }
758         } else if (degraded + faulted != 0) {
759                 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
760         } else {
761                 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
762         }
763 }
764
765 vdev_ops_t vdev_mirror_ops = {
766         vdev_mirror_open,
767         vdev_mirror_close,
768         vdev_default_asize,
769         vdev_mirror_io_start,
770         vdev_mirror_io_done,
771         vdev_mirror_state_change,
772         NULL,
773         NULL,
774         NULL,
775         NULL,
776         vdev_default_xlate,
777         VDEV_TYPE_MIRROR,       /* name of this vdev type */
778         B_FALSE                 /* not a leaf vdev */
779 };
780
781 vdev_ops_t vdev_replacing_ops = {
782         vdev_mirror_open,
783         vdev_mirror_close,
784         vdev_default_asize,
785         vdev_mirror_io_start,
786         vdev_mirror_io_done,
787         vdev_mirror_state_change,
788         NULL,
789         NULL,
790         NULL,
791         NULL,
792         vdev_default_xlate,
793         VDEV_TYPE_REPLACING,    /* name of this vdev type */
794         B_FALSE                 /* not a leaf vdev */
795 };
796
797 vdev_ops_t vdev_spare_ops = {
798         vdev_mirror_open,
799         vdev_mirror_close,
800         vdev_default_asize,
801         vdev_mirror_io_start,
802         vdev_mirror_io_done,
803         vdev_mirror_state_change,
804         NULL,
805         NULL,
806         NULL,
807         NULL,
808         vdev_default_xlate,
809         VDEV_TYPE_SPARE,        /* name of this vdev type */
810         B_FALSE                 /* not a leaf vdev */
811 };
812
813 #if defined(_KERNEL)
814 /* BEGIN CSTYLED */
815 module_param(zfs_vdev_mirror_rotating_inc, int, 0644);
816 MODULE_PARM_DESC(zfs_vdev_mirror_rotating_inc,
817         "Rotating media load increment for non-seeking I/O's");
818
819 module_param(zfs_vdev_mirror_rotating_seek_inc, int, 0644);
820 MODULE_PARM_DESC(zfs_vdev_mirror_rotating_seek_inc,
821         "Rotating media load increment for seeking I/O's");
822
823 module_param(zfs_vdev_mirror_rotating_seek_offset, int, 0644);
824
825 MODULE_PARM_DESC(zfs_vdev_mirror_rotating_seek_offset,
826         "Offset in bytes from the last I/O which "
827         "triggers a reduced rotating media seek increment");
828
829 module_param(zfs_vdev_mirror_non_rotating_inc, int, 0644);
830 MODULE_PARM_DESC(zfs_vdev_mirror_non_rotating_inc,
831         "Non-rotating media load increment for non-seeking I/O's");
832
833 module_param(zfs_vdev_mirror_non_rotating_seek_inc, int, 0644);
834 MODULE_PARM_DESC(zfs_vdev_mirror_non_rotating_seek_inc,
835         "Non-rotating media load increment for seeking I/O's");
836 /* END CSTYLED */
837 #endif