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 2016 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
29 */
30
31 #include <sys/zfs_context.h>
32 #include <sys/spa.h>
33 #include <sys/spa_impl.h>
34 #include <sys/dsl_pool.h>
35 #include <sys/dsl_scan.h>
36 #include <sys/vdev_impl.h>
37 #include <sys/zio.h>
38 #include <sys/abd.h>
39 #include <sys/wbc.h>
40 #include <sys/fs/zfs.h>
41
42 /*
43 * Virtual device vector for mirroring.
44 */
45
46 typedef struct mirror_child {
47 vdev_t *mc_vd;
48 uint64_t mc_offset;
49 int mc_error;
50 uint8_t mc_tried;
51 uint8_t mc_skipped;
52 uint8_t mc_speculative;
53 int mc_index; /* index in mirror_map_t */
54 avl_node_t mc_node; /* used for sorting based on weight */
55 int64_t mc_weight; /* thread-local copy of vdev_weight */
56 } mirror_child_t;
57
58 typedef struct mirror_map {
59 int mm_children;
60 int mm_resilvering;
61 int mm_preferred;
62 int mm_root;
63 mirror_child_t mm_child[1];
64 } mirror_map_t;
65
66 int vdev_mirror_shift = 21;
67
68 static void
69 vdev_mirror_map_free(zio_t *zio)
70 {
71 mirror_map_t *mm = zio->io_vsd;
72
73 kmem_free(mm, offsetof(mirror_map_t, mm_child[mm->mm_children]));
74 }
75
76 static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
77 vdev_mirror_map_free,
78 zio_vsd_default_cksum_report
79 };
80
81 static mirror_map_t *
82 vdev_mirror_map_alloc(zio_t *zio)
83 {
84 mirror_map_t *mm = NULL;
85 mirror_child_t *mc;
86 vdev_t *vd = zio->io_vd;
87 int c, d;
88
89 if (vd == NULL) {
90 dva_t *dva = zio->io_bp->blk_dva;
91 spa_t *spa = zio->io_spa;
92
93 c = BP_GET_NDVAS(zio->io_bp);
94
95 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
96 mm->mm_children = c;
97 mm->mm_resilvering = B_FALSE;
98 mm->mm_preferred = spa_get_random(c);
99 mm->mm_root = B_TRUE;
100
101 /*
102 * Check the other, lower-index DVAs to see if they're on
103 * the same vdev as the child we picked. If they are, use
104 * them since they are likely to have been allocated from
105 * the primary metaslab in use at the time, and hence are
106 * more likely to have locality with single-copy data.
107 */
108 for (c = mm->mm_preferred, d = c - 1; d >= 0; d--) {
109 if (DVA_GET_VDEV(&dva[d]) == DVA_GET_VDEV(&dva[c]))
110 mm->mm_preferred = d;
111 }
112
113 for (c = 0; c < mm->mm_children; c++) {
114 mc = &mm->mm_child[c];
115
116 mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
117 mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
118 mc->mc_index = c;
119 mc->mc_weight = (mc->mc_vd != NULL ?
120 mc->mc_vd->vdev_weight : 0);
121 }
122 } else {
123 int replacing;
124
125 c = vd->vdev_children;
126
127 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
128 mm->mm_children = c;
129 /*
130 * If we are resilvering, then we should handle scrub reads
131 * differently; we shouldn't issue them to the resilvering
132 * device because it might not have those blocks.
133 *
134 * We are resilvering iff:
135 * 1) We are a replacing vdev (ie our name is "replacing-1" or
136 * "spare-1" or something like that), and
137 * 2) The pool is currently being resilvered.
138 *
139 * We cannot simply check vd->vdev_resilver_txg, because it's
140 * not set in this path.
141 *
142 * Nor can we just check our vdev_ops; there are cases (such as
143 * when a user types "zpool replace pool odev spare_dev" and
144 * spare_dev is in the spare list, or when a spare device is
145 * automatically used to replace a DEGRADED device) when
146 * resilvering is complete but both the original vdev and the
147 * spare vdev remain in the pool. That behavior is intentional.
148 * It helps implement the policy that a spare should be
149 * automatically removed from the pool after the user replaces
150 * the device that originally failed.
151 */
152 replacing = (vd->vdev_ops == &vdev_replacing_ops ||
153 vd->vdev_ops == &vdev_spare_ops);
154 /*
155 * If a spa load is in progress, then spa_dsl_pool may be
156 * uninitialized. But we shouldn't be resilvering during a spa
157 * load anyway.
158 */
159 if (replacing &&
160 (spa_load_state(vd->vdev_spa) == SPA_LOAD_NONE) &&
161 dsl_scan_resilvering(vd->vdev_spa->spa_dsl_pool)) {
162 mm->mm_resilvering = B_TRUE;
163 } else {
164 mm->mm_resilvering = B_FALSE;
165 }
166
167 mm->mm_preferred = mm->mm_resilvering ? 0 :
168 (zio->io_offset >> vdev_mirror_shift) % c;
169 mm->mm_root = B_FALSE;
170
171 for (c = 0; c < mm->mm_children; c++) {
172 mc = &mm->mm_child[c];
173 mc->mc_vd = vd->vdev_child[c];
174 mc->mc_offset = zio->io_offset;
175 mc->mc_index = c;
176 mc->mc_weight = (mc->mc_vd != NULL ?
177 mc->mc_vd->vdev_weight : 0);
178 }
179 }
180
181 zio->io_vsd = mm;
182 zio->io_vsd_ops = &vdev_mirror_vsd_ops;
183 return (mm);
184 }
185
186 static int
187 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
188 uint64_t *ashift)
189 {
190 int numerrors = 0;
191 int lasterror = 0;
192
193 if (vd->vdev_children == 0) {
194 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
195 return (SET_ERROR(EINVAL));
196 }
197
198 vdev_open_children(vd);
199
200 for (int c = 0; c < vd->vdev_children; c++) {
201 vdev_t *cvd = vd->vdev_child[c];
202
203 if (cvd->vdev_open_error) {
204 lasterror = cvd->vdev_open_error;
205 numerrors++;
206 continue;
207 }
208
209 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
210 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
211 *ashift = MAX(*ashift, cvd->vdev_ashift);
212 }
213
214 if (numerrors == vd->vdev_children) {
215 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
216 return (lasterror);
217 }
218
219 return (0);
220 }
221
222 static void
223 vdev_mirror_close(vdev_t *vd)
224 {
225 for (int c = 0; c < vd->vdev_children; c++)
226 vdev_close(vd->vdev_child[c]);
227 }
228
229 static void
230 vdev_mirror_child_done(zio_t *zio)
231 {
232 mirror_child_t *mc = zio->io_private;
233
234 mc->mc_error = zio->io_error;
235 mc->mc_tried = 1;
236 mc->mc_skipped = 0;
237 }
238
239 static void
240 vdev_mirror_scrub_done(zio_t *zio)
241 {
242 mirror_child_t *mc = zio->io_private;
243
244 if (zio->io_error == 0) {
245 zio_t *pio;
246 zio_link_t *zl = NULL;
247
248 mutex_enter(&zio->io_lock);
249 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
250 mutex_enter(&pio->io_lock);
251 ASSERT3U(zio->io_size, >=, pio->io_size);
252 abd_copy(pio->io_abd, zio->io_abd, pio->io_size);
253 mutex_exit(&pio->io_lock);
254 }
255 mutex_exit(&zio->io_lock);
256 }
257 abd_free(zio->io_abd);
258
259 mc->mc_error = zio->io_error;
260 mc->mc_tried = 1;
261 mc->mc_skipped = 0;
262 }
263
264 static int
265 vdev_weight_compar(const void *mc_a, const void *mc_b)
266 {
267 const mirror_child_t *a = mc_a, *b = mc_b;
268
269 /*
270 * 1) if a's weight is less than b's, a goes right in the tree
271 * 2) if a's weight is greater than b's, a goes left
272 * 3) if a's and b's weights are equal, lower map index goes left
273 * 4) if weight and map index are equal, it's the same object
274 */
275 if (a->mc_weight < b->mc_weight)
276 return (1);
277 if (a->mc_weight > b->mc_weight)
278 return (-1);
279 if (a->mc_index > b->mc_index)
280 return (1);
281 if (a->mc_index < b->mc_index)
282 return (-1);
283 ASSERT3P(a->mc_vd, ==, b->mc_vd);
284 return (0);
285 }
286
287 static boolean_t
288 child_select_mc(mirror_child_t *mc, uint64_t txg)
289 {
290 if (mc->mc_tried || mc->mc_skipped)
291 return (B_FALSE);
292 if (!vdev_readable(mc->mc_vd)) {
293 mc->mc_error = SET_ERROR(ENXIO);
294 mc->mc_tried = 1; /* don't even try */
295 mc->mc_skipped = 1;
296 return (B_FALSE);
297 }
298 if (!vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) {
299 mc->mc_weight--;
300 mc->mc_vd->vdev_weight = mc->mc_weight;
301 return (B_TRUE);
302 }
303 mc->mc_error = SET_ERROR(ESTALE);
304 mc->mc_skipped = 1;
305 mc->mc_speculative = 1;
306 return (B_FALSE);
307 }
308
309 static void
310 child_select_cleanup(mirror_map_t *mm, avl_tree_t *vdevs_by_weight)
311 {
312 for (int i = 0; i < mm->mm_children; i++)
313 avl_remove(vdevs_by_weight, &mm->mm_child[i]);
314 avl_destroy(vdevs_by_weight);
315 }
316
317 /*
318 * Try to find a child whose DTL doesn't contain the block we want to read.
319 * If we can't, try the read on any vdev we haven't already tried.
320 */
321 static int
322 vdev_mirror_child_select(zio_t *zio)
323 {
324 mirror_map_t *mm = zio->io_vsd;
325 uint64_t txg = zio->io_txg;
326 /*
327 * Look at the weights of the vdevs in the mirror; the weights help
328 * decide which vdev to read from; the highest-weight suitable child
329 * index is returned, and its weight is decremented in order to avoid
330 * creating "hot" devices; once all the vdevs' weights are zero, the
331 * weights are set back to the ones configured in vdev props
332 */
333 int64_t max_weight = 0;
334
335 ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
336
337 for (int c = 0; c < mm->mm_children; c++) {
338 mirror_child_t *mc = &mm->mm_child[c];
339 if (mc->mc_vd == NULL)
340 continue;
341 max_weight = MAX(max_weight, mc->mc_weight);
342 }
343
344 /*
345 * Recalculate weights
346 */
347 if (max_weight == 0) {
348 for (int c = 0; c < mm->mm_children; c++) {
349 mirror_child_t *mc = &mm->mm_child[c];
350 if (mc->mc_vd == NULL)
351 continue;
352 mc->mc_weight =
353 vdev_queue_get_prop_uint64(&mc->mc_vd->vdev_queue,
354 VDEV_PROP_PREFERRED_READ) + 1;
355 mc->mc_vd->vdev_weight = mc->mc_weight;
356 }
357 }
358
359 if (mm->mm_children > 1) {
360 avl_tree_t vdevs_by_weight;
361
362 avl_create(&vdevs_by_weight, vdev_weight_compar,
363 sizeof (mirror_child_t), offsetof(mirror_child_t, mc_node));
364
365 /*
366 * Sort the weighted list
367 */
368 for (int i = 0; i < mm->mm_children; i++)
369 avl_add(&vdevs_by_weight, &mm->mm_child[i]);
370
371 /*
372 * Try to find a child whose DTL doesn't contain the block to
373 * read. If a child is known to be completely inaccessible
374 * (vdev_readable() returning B_FALSE), don't even try.
375 */
376 for (mirror_child_t *mc = avl_first(&vdevs_by_weight);
377 mc != NULL; mc = AVL_NEXT(&vdevs_by_weight, mc)) {
378 if (child_select_mc(mc, txg)) {
379 child_select_cleanup(mm, &vdevs_by_weight);
380 return (mc->mc_index);
381 }
382 }
383 child_select_cleanup(mm, &vdevs_by_weight);
384 } else {
385 if (child_select_mc(&mm->mm_child[0], txg))
386 return (0);
387 }
388
389 /*
390 * Every device is either missing or has this txg in its DTL.
391 * Look for any child we haven't already tried before giving up.
392 */
393 for (int c = 0; c < mm->mm_children; c++)
394 if (!mm->mm_child[c].mc_tried && mm->mm_child[c].mc_vd != NULL)
395 return (c);
396
397 /*
398 * Every child failed. There's no place left to look.
399 */
400 return (-1);
401 }
402
403 static void
404 vdev_mirror_io_start(zio_t *zio)
405 {
406 mirror_map_t *mm;
407 mirror_child_t *mc;
408 int c, children;
409 boolean_t spec_case = B_FALSE;
410 spa_t *spa = zio->io_spa;
411
412 mm = vdev_mirror_map_alloc(zio);
413
414 if (zio->io_child_type != ZIO_CHILD_VDEV &&
415 BP_IS_SPECIAL(zio->io_bp))
416 spec_case = B_TRUE;
417
418 if (zio->io_type == ZIO_TYPE_READ) {
419 if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_resilvering) {
420 int target = 0;
421
422 /*
423 * Scrub of special BPs should take into
424 * account the state of WBC-Window
425 */
426 if (spec_case) {
427 target = wbc_select_dva(
428 spa_get_wbc_data(spa), zio);
429 }
430
431 /*
432 * For scrubbing reads we need to allocate a read
433 * buffer for each child and issue reads to all
434 * children. If any child succeeds, it will copy its
435 * data into zio->io_data in vdev_mirror_scrub_done.
436 */
437 for (c = 0; c < mm->mm_children; c++) {
438 mc = &mm->mm_child[c];
439 if (mc->mc_vd == NULL) {
440 /*
441 * Invalid vdev id in blkptr caused
442 * mc_vd to be NULL here.
443 * Just skip this vdev.
444 */
445 continue;
446 }
447
448 if (spec_case && c != target)
449 continue;
450
451 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
452 mc->mc_vd, mc->mc_offset,
453 abd_alloc_sametype(zio->io_abd,
454 zio->io_size), zio->io_size,
455 zio->io_type, zio->io_priority, 0,
456 vdev_mirror_scrub_done, mc));
457 }
458 zio_execute(zio);
459 return;
460 }
461 /*
462 * For normal reads just pick one child.
463 */
464
465 if (spec_case)
466 c = wbc_select_dva(spa_get_wbc_data(spa), zio);
467 else
468 c = vdev_mirror_child_select(zio);
469
470 children = (c >= 0);
471 } else {
472 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
473
474 /*
475 * Writes go to all children.
476 */
477 c = 0;
478 children = mm->mm_children;
479 }
480
481 for (; children--; c++) {
482 mc = &mm->mm_child[c];
483 if (mc->mc_vd == NULL) {
484 /*
485 * Invalid vdev in blkptr caused mc_vd to be NULL here.
486 * Just skip this vdev.
487 */
488 continue;
489 }
490
491 if (spec_case) {
492 if (zio->io_type == ZIO_TYPE_WRITE &&
493 !vdev_is_special(mc->mc_vd))
494 continue;
495 }
496
497 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
498 mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
499 zio->io_type, zio->io_priority, 0,
500 vdev_mirror_child_done, mc));
501
502 if (spec_case)
503 break;
504 }
505
506 zio_execute(zio);
507 }
508
509 static int
510 vdev_mirror_worst_error(mirror_map_t *mm)
511 {
512 int error[2] = { 0, 0 };
513
514 for (int c = 0; c < mm->mm_children; c++) {
515 mirror_child_t *mc = &mm->mm_child[c];
516 int s = mc->mc_speculative;
517 error[s] = zio_worst_error(error[s], mc->mc_error);
518 }
519
520 return (error[0] ? error[0] : error[1]);
521 }
522
523 static void
524 vdev_mirror_io_done(zio_t *zio)
525 {
526 mirror_map_t *mm = zio->io_vsd;
527 mirror_child_t *mc;
528 int c;
529 int good_copies = 0;
530 int unexpected_errors = 0;
531
532 for (c = 0; c < mm->mm_children; c++) {
533 mc = &mm->mm_child[c];
534
535 if (mc->mc_error) {
536 if (!mc->mc_skipped)
537 unexpected_errors++;
538 } else if (mc->mc_tried) {
539 good_copies++;
540 }
541 }
542
543 if (zio->io_type == ZIO_TYPE_WRITE) {
544 /*
545 * XXX -- for now, treat partial writes as success.
546 *
547 * Now that we support write reallocation, it would be better
548 * to treat partial failure as real failure unless there are
549 * no non-degraded top-level vdevs left, and not update DTLs
550 * if we intend to reallocate.
551 */
552 /* XXPOLICY */
553 if (good_copies != mm->mm_children) {
554 /*
555 * Always require at least one good copy.
556 *
557 * For ditto blocks (io_vd == NULL), require
558 * all copies to be good.
559 *
560 * XXX -- for replacing vdevs, there's no great answer.
561 * If the old device is really dead, we may not even
562 * be able to access it -- so we only want to
563 * require good writes to the new device. But if
564 * the new device turns out to be flaky, we want
565 * to be able to detach it -- which requires all
566 * writes to the old device to have succeeded.
567 */
568 if (good_copies == 0 || zio->io_vd == NULL)
569 zio->io_error = vdev_mirror_worst_error(mm);
570 }
571 return;
572 }
573
574 ASSERT(zio->io_type == ZIO_TYPE_READ);
575
576 /*
577 * If we don't have a good copy yet, keep trying other children.
578 */
579 /* XXPOLICY */
580 if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
581 ASSERT(c >= 0 && c < mm->mm_children);
582 mc = &mm->mm_child[c];
583 zio_vdev_io_redone(zio);
584 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
585 mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
586 ZIO_TYPE_READ, zio->io_priority, 0,
587 vdev_mirror_child_done, mc));
588 return;
589 }
590
591 /* XXPOLICY */
592 if (good_copies == 0) {
593 zio->io_error = vdev_mirror_worst_error(mm);
594 ASSERT(zio->io_error != 0);
595 }
596
597 if (good_copies && spa_writeable(zio->io_spa) &&
598 (unexpected_errors ||
599 (zio->io_flags & ZIO_FLAG_RESILVER) ||
600 ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_resilvering))) {
601 /*
602 * Use the good data we have in hand to repair damaged children.
603 */
604 for (c = 0; c < mm->mm_children; c++) {
605 /*
606 * Don't rewrite known good children.
607 * Not only is it unnecessary, it could
608 * actually be harmful: if the system lost
609 * power while rewriting the only good copy,
610 * there would be no good copies left!
611 */
612 mc = &mm->mm_child[c];
613
614 if (mc->mc_error == 0) {
615 if (mc->mc_tried)
616 continue;
617 if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
618 !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
619 zio->io_txg, 1))
620 continue;
621 mc->mc_error = SET_ERROR(ESTALE);
622 }
623
624 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
625 mc->mc_vd, mc->mc_offset,
626 zio->io_abd, zio->io_size,
627 ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
628 ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
629 ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
630 }
631 }
632 }
633
634 static void
635 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
636 {
637 if (faulted == vd->vdev_children)
638 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
639 VDEV_AUX_NO_REPLICAS);
640 else if (degraded + faulted != 0)
641 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
642 else
643 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
644 }
645
646 vdev_ops_t vdev_mirror_ops = {
647 vdev_mirror_open,
648 vdev_mirror_close,
649 vdev_default_asize,
650 vdev_mirror_io_start,
651 vdev_mirror_io_done,
652 vdev_mirror_state_change,
653 NULL,
654 NULL,
655 NULL,
656 VDEV_TYPE_MIRROR, /* name of this vdev type */
657 B_FALSE /* not a leaf vdev */
658 };
659
660 vdev_ops_t vdev_replacing_ops = {
661 vdev_mirror_open,
662 vdev_mirror_close,
663 vdev_default_asize,
664 vdev_mirror_io_start,
665 vdev_mirror_io_done,
666 vdev_mirror_state_change,
667 NULL,
668 NULL,
669 NULL,
670 VDEV_TYPE_REPLACING, /* name of this vdev type */
671 B_FALSE /* not a leaf vdev */
672 };
673
674 vdev_ops_t vdev_spare_ops = {
675 vdev_mirror_open,
676 vdev_mirror_close,
677 vdev_default_asize,
678 vdev_mirror_io_start,
679 vdev_mirror_io_done,
680 vdev_mirror_state_change,
681 NULL,
682 NULL,
683 NULL,
684 VDEV_TYPE_SPARE, /* name of this vdev type */
685 B_FALSE /* not a leaf vdev */
686 };