]> git.proxmox.com Git - mirror_zfs-debian.git/blob - module/zfs/vdev_mirror.c
New upstream version 0.7.2
[mirror_zfs-debian.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/vdev_impl.h>
33 #include <sys/zio.h>
34 #include <sys/abd.h>
35 #include <sys/fs/zfs.h>
36
37 /*
38 * Virtual device vector for mirroring.
39 */
40
41 typedef struct mirror_child {
42 vdev_t *mc_vd;
43 uint64_t mc_offset;
44 int mc_error;
45 int mc_load;
46 uint8_t mc_tried;
47 uint8_t mc_skipped;
48 uint8_t mc_speculative;
49 } mirror_child_t;
50
51 typedef struct mirror_map {
52 int *mm_preferred;
53 int mm_preferred_cnt;
54 int mm_children;
55 boolean_t mm_replacing;
56 boolean_t mm_root;
57 mirror_child_t mm_child[];
58 } mirror_map_t;
59
60 static int vdev_mirror_shift = 21;
61
62 /*
63 * The load configuration settings below are tuned by default for
64 * the case where all devices are of the same rotational type.
65 *
66 * If there is a mixture of rotating and non-rotating media, setting
67 * zfs_vdev_mirror_non_rotating_seek_inc to 0 may well provide better results
68 * as it will direct more reads to the non-rotating vdevs which are more likely
69 * to have a higher performance.
70 */
71
72 /* Rotating media load calculation configuration. */
73 static int zfs_vdev_mirror_rotating_inc = 0;
74 static int zfs_vdev_mirror_rotating_seek_inc = 5;
75 static int zfs_vdev_mirror_rotating_seek_offset = 1 * 1024 * 1024;
76
77 /* Non-rotating media load calculation configuration. */
78 static int zfs_vdev_mirror_non_rotating_inc = 0;
79 static int zfs_vdev_mirror_non_rotating_seek_inc = 1;
80
81 static inline size_t
82 vdev_mirror_map_size(int children)
83 {
84 return (offsetof(mirror_map_t, mm_child[children]) +
85 sizeof (int) * children);
86 }
87
88 static inline mirror_map_t *
89 vdev_mirror_map_alloc(int children, boolean_t replacing, boolean_t root)
90 {
91 mirror_map_t *mm;
92
93 mm = kmem_zalloc(vdev_mirror_map_size(children), KM_SLEEP);
94 mm->mm_children = children;
95 mm->mm_replacing = replacing;
96 mm->mm_root = root;
97 mm->mm_preferred = (int *)((uintptr_t)mm +
98 offsetof(mirror_map_t, mm_child[children]));
99
100 return (mm);
101 }
102
103 static void
104 vdev_mirror_map_free(zio_t *zio)
105 {
106 mirror_map_t *mm = zio->io_vsd;
107
108 kmem_free(mm, vdev_mirror_map_size(mm->mm_children));
109 }
110
111 static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
112 vdev_mirror_map_free,
113 zio_vsd_default_cksum_report
114 };
115
116 static int
117 vdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset)
118 {
119 uint64_t lastoffset;
120 int load;
121
122 /* All DVAs have equal weight at the root. */
123 if (mm->mm_root)
124 return (INT_MAX);
125
126 /*
127 * We don't return INT_MAX if the device is resilvering i.e.
128 * vdev_resilver_txg != 0 as when tested performance was slightly
129 * worse overall when resilvering with compared to without.
130 */
131
132 /* Standard load based on pending queue length. */
133 load = vdev_queue_length(vd);
134 lastoffset = vdev_queue_lastoffset(vd);
135
136 if (vd->vdev_nonrot) {
137 /* Non-rotating media. */
138 if (lastoffset == zio_offset)
139 return (load + zfs_vdev_mirror_non_rotating_inc);
140
141 /*
142 * Apply a seek penalty even for non-rotating devices as
143 * sequential I/O's can be aggregated into fewer operations on
144 * the device, thus avoiding unnecessary per-command overhead
145 * and boosting performance.
146 */
147 return (load + zfs_vdev_mirror_non_rotating_seek_inc);
148 }
149
150 /* Rotating media I/O's which directly follow the last I/O. */
151 if (lastoffset == zio_offset)
152 return (load + zfs_vdev_mirror_rotating_inc);
153
154 /*
155 * Apply half the seek increment to I/O's within seek offset
156 * of the last I/O queued to this vdev as they should incur less
157 * of a seek increment.
158 */
159 if (ABS(lastoffset - zio_offset) <
160 zfs_vdev_mirror_rotating_seek_offset)
161 return (load + (zfs_vdev_mirror_rotating_seek_inc / 2));
162
163 /* Apply the full seek increment to all other I/O's. */
164 return (load + zfs_vdev_mirror_rotating_seek_inc);
165 }
166
167 /*
168 * Avoid inlining the function to keep vdev_mirror_io_start(), which
169 * is this functions only caller, as small as possible on the stack.
170 */
171 noinline static mirror_map_t *
172 vdev_mirror_map_init(zio_t *zio)
173 {
174 mirror_map_t *mm = NULL;
175 mirror_child_t *mc;
176 vdev_t *vd = zio->io_vd;
177 int c;
178
179 if (vd == NULL) {
180 dva_t *dva = zio->io_bp->blk_dva;
181 spa_t *spa = zio->io_spa;
182
183 mm = vdev_mirror_map_alloc(BP_GET_NDVAS(zio->io_bp), B_FALSE,
184 B_TRUE);
185 for (c = 0; c < mm->mm_children; c++) {
186 mc = &mm->mm_child[c];
187
188 mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
189 mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
190 }
191 } else {
192 mm = vdev_mirror_map_alloc(vd->vdev_children,
193 (vd->vdev_ops == &vdev_replacing_ops ||
194 vd->vdev_ops == &vdev_spare_ops), B_FALSE);
195 for (c = 0; c < mm->mm_children; c++) {
196 mc = &mm->mm_child[c];
197 mc->mc_vd = vd->vdev_child[c];
198 mc->mc_offset = zio->io_offset;
199 }
200 }
201
202 zio->io_vsd = mm;
203 zio->io_vsd_ops = &vdev_mirror_vsd_ops;
204 return (mm);
205 }
206
207 static int
208 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
209 uint64_t *ashift)
210 {
211 int numerrors = 0;
212 int lasterror = 0;
213 int c;
214
215 if (vd->vdev_children == 0) {
216 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
217 return (SET_ERROR(EINVAL));
218 }
219
220 vdev_open_children(vd);
221
222 for (c = 0; c < vd->vdev_children; c++) {
223 vdev_t *cvd = vd->vdev_child[c];
224
225 if (cvd->vdev_open_error) {
226 lasterror = cvd->vdev_open_error;
227 numerrors++;
228 continue;
229 }
230
231 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
232 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
233 *ashift = MAX(*ashift, cvd->vdev_ashift);
234 }
235
236 if (numerrors == vd->vdev_children) {
237 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
238 return (lasterror);
239 }
240
241 return (0);
242 }
243
244 static void
245 vdev_mirror_close(vdev_t *vd)
246 {
247 int c;
248
249 for (c = 0; c < vd->vdev_children; c++)
250 vdev_close(vd->vdev_child[c]);
251 }
252
253 static void
254 vdev_mirror_child_done(zio_t *zio)
255 {
256 mirror_child_t *mc = zio->io_private;
257
258 mc->mc_error = zio->io_error;
259 mc->mc_tried = 1;
260 mc->mc_skipped = 0;
261 }
262
263 static void
264 vdev_mirror_scrub_done(zio_t *zio)
265 {
266 mirror_child_t *mc = zio->io_private;
267
268 if (zio->io_error == 0) {
269 zio_t *pio;
270 zio_link_t *zl = NULL;
271
272 mutex_enter(&zio->io_lock);
273 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
274 mutex_enter(&pio->io_lock);
275 ASSERT3U(zio->io_size, >=, pio->io_size);
276 abd_copy(pio->io_abd, zio->io_abd, pio->io_size);
277 mutex_exit(&pio->io_lock);
278 }
279 mutex_exit(&zio->io_lock);
280 }
281
282 abd_free(zio->io_abd);
283
284 mc->mc_error = zio->io_error;
285 mc->mc_tried = 1;
286 mc->mc_skipped = 0;
287 }
288
289 /*
290 * Check the other, lower-index DVAs to see if they're on the same
291 * vdev as the child we picked. If they are, use them since they
292 * are likely to have been allocated from the primary metaslab in
293 * use at the time, and hence are more likely to have locality with
294 * single-copy data.
295 */
296 static int
297 vdev_mirror_dva_select(zio_t *zio, int p)
298 {
299 dva_t *dva = zio->io_bp->blk_dva;
300 mirror_map_t *mm = zio->io_vsd;
301 int preferred;
302 int c;
303
304 preferred = mm->mm_preferred[p];
305 for (p--; p >= 0; p--) {
306 c = mm->mm_preferred[p];
307 if (DVA_GET_VDEV(&dva[c]) == DVA_GET_VDEV(&dva[preferred]))
308 preferred = c;
309 }
310 return (preferred);
311 }
312
313 static int
314 vdev_mirror_preferred_child_randomize(zio_t *zio)
315 {
316 mirror_map_t *mm = zio->io_vsd;
317 int p;
318
319 if (mm->mm_root) {
320 p = spa_get_random(mm->mm_preferred_cnt);
321 return (vdev_mirror_dva_select(zio, p));
322 }
323
324 /*
325 * To ensure we don't always favour the first matching vdev,
326 * which could lead to wear leveling issues on SSD's, we
327 * use the I/O offset as a pseudo random seed into the vdevs
328 * which have the lowest load.
329 */
330 p = (zio->io_offset >> vdev_mirror_shift) % mm->mm_preferred_cnt;
331 return (mm->mm_preferred[p]);
332 }
333
334 /*
335 * Try to find a vdev whose DTL doesn't contain the block we want to read
336 * prefering vdevs based on determined load.
337 *
338 * Try to find a child whose DTL doesn't contain the block we want to read.
339 * If we can't, try the read on any vdev we haven't already tried.
340 */
341 static int
342 vdev_mirror_child_select(zio_t *zio)
343 {
344 mirror_map_t *mm = zio->io_vsd;
345 uint64_t txg = zio->io_txg;
346 int c, lowest_load;
347
348 ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
349
350 lowest_load = INT_MAX;
351 mm->mm_preferred_cnt = 0;
352 for (c = 0; c < mm->mm_children; c++) {
353 mirror_child_t *mc;
354
355 mc = &mm->mm_child[c];
356 if (mc->mc_tried || mc->mc_skipped)
357 continue;
358
359 if (mc->mc_vd == NULL || !vdev_readable(mc->mc_vd)) {
360 mc->mc_error = SET_ERROR(ENXIO);
361 mc->mc_tried = 1; /* don't even try */
362 mc->mc_skipped = 1;
363 continue;
364 }
365
366 if (vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) {
367 mc->mc_error = SET_ERROR(ESTALE);
368 mc->mc_skipped = 1;
369 mc->mc_speculative = 1;
370 continue;
371 }
372
373 mc->mc_load = vdev_mirror_load(mm, mc->mc_vd, mc->mc_offset);
374 if (mc->mc_load > lowest_load)
375 continue;
376
377 if (mc->mc_load < lowest_load) {
378 lowest_load = mc->mc_load;
379 mm->mm_preferred_cnt = 0;
380 }
381 mm->mm_preferred[mm->mm_preferred_cnt] = c;
382 mm->mm_preferred_cnt++;
383 }
384
385 if (mm->mm_preferred_cnt == 1) {
386 vdev_queue_register_lastoffset(
387 mm->mm_child[mm->mm_preferred[0]].mc_vd, zio);
388 return (mm->mm_preferred[0]);
389 }
390
391 if (mm->mm_preferred_cnt > 1) {
392 int c = vdev_mirror_preferred_child_randomize(zio);
393
394 vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd, zio);
395 return (c);
396 }
397
398 /*
399 * Every device is either missing or has this txg in its DTL.
400 * Look for any child we haven't already tried before giving up.
401 */
402 for (c = 0; c < mm->mm_children; c++) {
403 if (!mm->mm_child[c].mc_tried) {
404 vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd,
405 zio);
406 return (c);
407 }
408 }
409
410 /*
411 * Every child failed. There's no place left to look.
412 */
413 return (-1);
414 }
415
416 static void
417 vdev_mirror_io_start(zio_t *zio)
418 {
419 mirror_map_t *mm;
420 mirror_child_t *mc;
421 int c, children;
422
423 mm = vdev_mirror_map_init(zio);
424
425 if (zio->io_type == ZIO_TYPE_READ) {
426 if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) {
427 /*
428 * For scrubbing reads we need to allocate a read
429 * buffer for each child and issue reads to all
430 * children. If any child succeeds, it will copy its
431 * data into zio->io_data in vdev_mirror_scrub_done.
432 */
433 for (c = 0; c < mm->mm_children; c++) {
434 mc = &mm->mm_child[c];
435 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
436 mc->mc_vd, mc->mc_offset,
437 abd_alloc_sametype(zio->io_abd,
438 zio->io_size), zio->io_size,
439 zio->io_type, zio->io_priority, 0,
440 vdev_mirror_scrub_done, mc));
441 }
442 zio_execute(zio);
443 return;
444 }
445 /*
446 * For normal reads just pick one child.
447 */
448 c = vdev_mirror_child_select(zio);
449 children = (c >= 0);
450 } else {
451 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
452
453 /*
454 * Writes go to all children.
455 */
456 c = 0;
457 children = mm->mm_children;
458 }
459
460 while (children--) {
461 mc = &mm->mm_child[c];
462 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
463 mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
464 zio->io_type, zio->io_priority, 0,
465 vdev_mirror_child_done, mc));
466 c++;
467 }
468
469 zio_execute(zio);
470 }
471
472 static int
473 vdev_mirror_worst_error(mirror_map_t *mm)
474 {
475 int c, error[2] = { 0, 0 };
476
477 for (c = 0; c < mm->mm_children; c++) {
478 mirror_child_t *mc = &mm->mm_child[c];
479 int s = mc->mc_speculative;
480 error[s] = zio_worst_error(error[s], mc->mc_error);
481 }
482
483 return (error[0] ? error[0] : error[1]);
484 }
485
486 static void
487 vdev_mirror_io_done(zio_t *zio)
488 {
489 mirror_map_t *mm = zio->io_vsd;
490 mirror_child_t *mc;
491 int c;
492 int good_copies = 0;
493 int unexpected_errors = 0;
494
495 for (c = 0; c < mm->mm_children; c++) {
496 mc = &mm->mm_child[c];
497
498 if (mc->mc_error) {
499 if (!mc->mc_skipped)
500 unexpected_errors++;
501 } else if (mc->mc_tried) {
502 good_copies++;
503 }
504 }
505
506 if (zio->io_type == ZIO_TYPE_WRITE) {
507 /*
508 * XXX -- for now, treat partial writes as success.
509 *
510 * Now that we support write reallocation, it would be better
511 * to treat partial failure as real failure unless there are
512 * no non-degraded top-level vdevs left, and not update DTLs
513 * if we intend to reallocate.
514 */
515 /* XXPOLICY */
516 if (good_copies != mm->mm_children) {
517 /*
518 * Always require at least one good copy.
519 *
520 * For ditto blocks (io_vd == NULL), require
521 * all copies to be good.
522 *
523 * XXX -- for replacing vdevs, there's no great answer.
524 * If the old device is really dead, we may not even
525 * be able to access it -- so we only want to
526 * require good writes to the new device. But if
527 * the new device turns out to be flaky, we want
528 * to be able to detach it -- which requires all
529 * writes to the old device to have succeeded.
530 */
531 if (good_copies == 0 || zio->io_vd == NULL)
532 zio->io_error = vdev_mirror_worst_error(mm);
533 }
534 return;
535 }
536
537 ASSERT(zio->io_type == ZIO_TYPE_READ);
538
539 /*
540 * If we don't have a good copy yet, keep trying other children.
541 */
542 /* XXPOLICY */
543 if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
544 ASSERT(c >= 0 && c < mm->mm_children);
545 mc = &mm->mm_child[c];
546 zio_vdev_io_redone(zio);
547 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
548 mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
549 ZIO_TYPE_READ, zio->io_priority, 0,
550 vdev_mirror_child_done, mc));
551 return;
552 }
553
554 /* XXPOLICY */
555 if (good_copies == 0) {
556 zio->io_error = vdev_mirror_worst_error(mm);
557 ASSERT(zio->io_error != 0);
558 }
559
560 if (good_copies && spa_writeable(zio->io_spa) &&
561 (unexpected_errors ||
562 (zio->io_flags & ZIO_FLAG_RESILVER) ||
563 ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
564 /*
565 * Use the good data we have in hand to repair damaged children.
566 */
567 for (c = 0; c < mm->mm_children; c++) {
568 /*
569 * Don't rewrite known good children.
570 * Not only is it unnecessary, it could
571 * actually be harmful: if the system lost
572 * power while rewriting the only good copy,
573 * there would be no good copies left!
574 */
575 mc = &mm->mm_child[c];
576
577 if (mc->mc_error == 0) {
578 if (mc->mc_tried)
579 continue;
580 if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
581 !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
582 zio->io_txg, 1))
583 continue;
584 mc->mc_error = SET_ERROR(ESTALE);
585 }
586
587 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
588 mc->mc_vd, mc->mc_offset,
589 zio->io_abd, zio->io_size,
590 ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
591 ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
592 ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
593 }
594 }
595 }
596
597 static void
598 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
599 {
600 if (faulted == vd->vdev_children)
601 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
602 VDEV_AUX_NO_REPLICAS);
603 else if (degraded + faulted != 0)
604 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
605 else
606 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
607 }
608
609 vdev_ops_t vdev_mirror_ops = {
610 vdev_mirror_open,
611 vdev_mirror_close,
612 vdev_default_asize,
613 vdev_mirror_io_start,
614 vdev_mirror_io_done,
615 vdev_mirror_state_change,
616 NULL,
617 NULL,
618 NULL,
619 VDEV_TYPE_MIRROR, /* name of this vdev type */
620 B_FALSE /* not a leaf vdev */
621 };
622
623 vdev_ops_t vdev_replacing_ops = {
624 vdev_mirror_open,
625 vdev_mirror_close,
626 vdev_default_asize,
627 vdev_mirror_io_start,
628 vdev_mirror_io_done,
629 vdev_mirror_state_change,
630 NULL,
631 NULL,
632 NULL,
633 VDEV_TYPE_REPLACING, /* name of this vdev type */
634 B_FALSE /* not a leaf vdev */
635 };
636
637 vdev_ops_t vdev_spare_ops = {
638 vdev_mirror_open,
639 vdev_mirror_close,
640 vdev_default_asize,
641 vdev_mirror_io_start,
642 vdev_mirror_io_done,
643 vdev_mirror_state_change,
644 NULL,
645 NULL,
646 NULL,
647 VDEV_TYPE_SPARE, /* name of this vdev type */
648 B_FALSE /* not a leaf vdev */
649 };
650
651 #if defined(_KERNEL) && defined(HAVE_SPL)
652 /* BEGIN CSTYLED */
653 module_param(zfs_vdev_mirror_rotating_inc, int, 0644);
654 MODULE_PARM_DESC(zfs_vdev_mirror_rotating_inc,
655 "Rotating media load increment for non-seeking I/O's");
656
657 module_param(zfs_vdev_mirror_rotating_seek_inc, int, 0644);
658 MODULE_PARM_DESC(zfs_vdev_mirror_rotating_seek_inc,
659 "Rotating media load increment for seeking I/O's");
660
661 module_param(zfs_vdev_mirror_rotating_seek_offset, int, 0644);
662
663 MODULE_PARM_DESC(zfs_vdev_mirror_rotating_seek_offset,
664 "Offset in bytes from the last I/O which "
665 "triggers a reduced rotating media seek increment");
666
667 module_param(zfs_vdev_mirror_non_rotating_inc, int, 0644);
668 MODULE_PARM_DESC(zfs_vdev_mirror_non_rotating_inc,
669 "Non-rotating media load increment for non-seeking I/O's");
670
671 module_param(zfs_vdev_mirror_non_rotating_seek_inc, int, 0644);
672 MODULE_PARM_DESC(zfs_vdev_mirror_non_rotating_seek_inc,
673 "Non-rotating media load increment for seeking I/O's");
674 /* END CSTYLED */
675 #endif