Improve N-way mirror performance
[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 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/fs/zfs.h>
35
36 /*
37  * Virtual device vector for mirroring.
38  */
39
40 typedef struct mirror_child {
41         vdev_t          *mc_vd;
42         uint64_t        mc_offset;
43         int             mc_error;
44         int             mc_pending;
45         uint8_t         mc_tried;
46         uint8_t         mc_skipped;
47         uint8_t         mc_speculative;
48 } mirror_child_t;
49
50 typedef struct mirror_map {
51         int             mm_children;
52         int             mm_replacing;
53         int             mm_preferred;
54         int             mm_root;
55         mirror_child_t  mm_child[1];
56 } mirror_map_t;
57
58 /*
59  * When the children are equally busy queue incoming requests to a single
60  * child for N microseconds.  This is done to maximize the likelihood that
61  * the Linux elevator will be able to merge requests while it is plugged.
62  * Otherwise, requests are queued to the least busy device.
63  *
64  * For rotational disks the Linux elevator will plug for 10ms which is
65  * why zfs_vdev_mirror_switch_us is set to 10ms by default.  For non-
66  * rotational disks the elevator will not plug, but 10ms is still a small
67  * enough value that the requests will get spread over all the children.
68  *
69  * For fast SSDs it may make sense to decrease zfs_vdev_mirror_switch_us
70  * significantly to bound the worst case latencies.  It would probably be
71  * ideal to calculate a decaying average of the last observed latencies and
72  * use that to dynamically adjust the zfs_vdev_mirror_switch_us time.
73  */
74 int zfs_vdev_mirror_switch_us = 10000;
75
76 static void
77 vdev_mirror_map_free(zio_t *zio)
78 {
79         mirror_map_t *mm = zio->io_vsd;
80
81         kmem_free(mm, offsetof(mirror_map_t, mm_child[mm->mm_children]));
82 }
83
84 static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
85         vdev_mirror_map_free,
86         zio_vsd_default_cksum_report
87 };
88
89 static int
90 vdev_mirror_pending(vdev_t *vd)
91 {
92         vdev_queue_t *vq = &vd->vdev_queue;
93         int pending;
94
95         mutex_enter(&vq->vq_lock);
96         pending = avl_numnodes(&vq->vq_pending_tree);
97         mutex_exit(&vq->vq_lock);
98
99         return (pending);
100 }
101
102 static mirror_map_t *
103 vdev_mirror_map_alloc(zio_t *zio)
104 {
105         mirror_map_t *mm = NULL;
106         mirror_child_t *mc;
107         vdev_t *vd = zio->io_vd;
108         int c, d;
109
110         if (vd == NULL) {
111                 dva_t *dva = zio->io_bp->blk_dva;
112                 spa_t *spa = zio->io_spa;
113
114                 c = BP_GET_NDVAS(zio->io_bp);
115
116                 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_PUSHPAGE);
117                 mm->mm_children = c;
118                 mm->mm_replacing = B_FALSE;
119                 mm->mm_preferred = spa_get_random(c);
120                 mm->mm_root = B_TRUE;
121
122                 /*
123                  * Check the other, lower-index DVAs to see if they're on
124                  * the same vdev as the child we picked.  If they are, use
125                  * them since they are likely to have been allocated from
126                  * the primary metaslab in use at the time, and hence are
127                  * more likely to have locality with single-copy data.
128                  */
129                 for (c = mm->mm_preferred, d = c - 1; d >= 0; d--) {
130                         if (DVA_GET_VDEV(&dva[d]) == DVA_GET_VDEV(&dva[c]))
131                                 mm->mm_preferred = d;
132                 }
133
134                 for (c = 0; c < mm->mm_children; c++) {
135                         mc = &mm->mm_child[c];
136
137                         mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
138                         mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
139                 }
140         } else {
141                 int lowest_pending = INT_MAX;
142                 int lowest_nr = 1;
143
144                 c = vd->vdev_children;
145
146                 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_PUSHPAGE);
147                 mm->mm_children = c;
148                 mm->mm_replacing = (vd->vdev_ops == &vdev_replacing_ops ||
149                     vd->vdev_ops == &vdev_spare_ops);
150                 mm->mm_preferred = 0;
151                 mm->mm_root = B_FALSE;
152
153                 for (c = 0; c < mm->mm_children; c++) {
154                         mc = &mm->mm_child[c];
155                         mc->mc_vd = vd->vdev_child[c];
156                         mc->mc_offset = zio->io_offset;
157
158                         if (mm->mm_replacing)
159                                 continue;
160
161                         if (!vdev_readable(mc->mc_vd)) {
162                                 mc->mc_error = ENXIO;
163                                 mc->mc_tried = 1;
164                                 mc->mc_skipped = 1;
165                                 mc->mc_pending = INT_MAX;
166                                 continue;
167                         }
168
169                         mc->mc_pending = vdev_mirror_pending(mc->mc_vd);
170                         if (mc->mc_pending < lowest_pending) {
171                                 lowest_pending = mc->mc_pending;
172                                 lowest_nr = 1;
173                         } else if (mc->mc_pending == lowest_pending) {
174                                 lowest_nr++;
175                         }
176                 }
177
178                 d = gethrtime() / (NSEC_PER_USEC * zfs_vdev_mirror_switch_us);
179                 d = (d % lowest_nr) + 1;
180
181                 for (c = 0; c < mm->mm_children; c++) {
182                         mc = &mm->mm_child[c];
183
184                         if (mm->mm_child[c].mc_pending == lowest_pending) {
185                                 if (--d == 0) {
186                                         mm->mm_preferred = c;
187                                         break;
188                                 }
189                         }
190                 }
191         }
192
193         zio->io_vsd = mm;
194         zio->io_vsd_ops = &vdev_mirror_vsd_ops;
195         return (mm);
196 }
197
198 static int
199 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
200     uint64_t *ashift)
201 {
202         int numerrors = 0;
203         int lasterror = 0;
204         int c;
205
206         if (vd->vdev_children == 0) {
207                 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
208                 return (EINVAL);
209         }
210
211         vdev_open_children(vd);
212
213         for (c = 0; c < vd->vdev_children; c++) {
214                 vdev_t *cvd = vd->vdev_child[c];
215
216                 if (cvd->vdev_open_error) {
217                         lasterror = cvd->vdev_open_error;
218                         numerrors++;
219                         continue;
220                 }
221
222                 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
223                 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
224                 *ashift = MAX(*ashift, cvd->vdev_ashift);
225         }
226
227         if (numerrors == vd->vdev_children) {
228                 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
229                 return (lasterror);
230         }
231
232         return (0);
233 }
234
235 static void
236 vdev_mirror_close(vdev_t *vd)
237 {
238         int c;
239
240         for (c = 0; c < vd->vdev_children; c++)
241                 vdev_close(vd->vdev_child[c]);
242 }
243
244 static void
245 vdev_mirror_child_done(zio_t *zio)
246 {
247         mirror_child_t *mc = zio->io_private;
248
249         mc->mc_error = zio->io_error;
250         mc->mc_tried = 1;
251         mc->mc_skipped = 0;
252 }
253
254 static void
255 vdev_mirror_scrub_done(zio_t *zio)
256 {
257         mirror_child_t *mc = zio->io_private;
258
259         if (zio->io_error == 0) {
260                 zio_t *pio;
261
262                 mutex_enter(&zio->io_lock);
263                 while ((pio = zio_walk_parents(zio)) != NULL) {
264                         mutex_enter(&pio->io_lock);
265                         ASSERT3U(zio->io_size, >=, pio->io_size);
266                         bcopy(zio->io_data, pio->io_data, pio->io_size);
267                         mutex_exit(&pio->io_lock);
268                 }
269                 mutex_exit(&zio->io_lock);
270         }
271
272         zio_buf_free(zio->io_data, zio->io_size);
273
274         mc->mc_error = zio->io_error;
275         mc->mc_tried = 1;
276         mc->mc_skipped = 0;
277 }
278
279 /*
280  * Try to find a child whose DTL doesn't contain the block we want to read.
281  * If we can't, try the read on any vdev we haven't already tried.
282  */
283 static int
284 vdev_mirror_child_select(zio_t *zio)
285 {
286         mirror_map_t *mm = zio->io_vsd;
287         mirror_child_t *mc;
288         uint64_t txg = zio->io_txg;
289         int i, c;
290
291         ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
292
293         /*
294          * Try to find a child whose DTL doesn't contain the block to read.
295          * If a child is known to be completely inaccessible (indicated by
296          * vdev_readable() returning B_FALSE), don't even try.
297          */
298         for (i = 0, c = mm->mm_preferred; i < mm->mm_children; i++, c++) {
299                 if (c >= mm->mm_children)
300                         c = 0;
301                 mc = &mm->mm_child[c];
302                 if (mc->mc_tried || mc->mc_skipped)
303                         continue;
304                 if (!vdev_readable(mc->mc_vd)) {
305                         mc->mc_error = ENXIO;
306                         mc->mc_tried = 1;       /* don't even try */
307                         mc->mc_skipped = 1;
308                         continue;
309                 }
310                 if (!vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1))
311                         return (c);
312                 mc->mc_error = ESTALE;
313                 mc->mc_skipped = 1;
314                 mc->mc_speculative = 1;
315         }
316
317         /*
318          * Every device is either missing or has this txg in its DTL.
319          * Look for any child we haven't already tried before giving up.
320          */
321         for (c = 0; c < mm->mm_children; c++)
322                 if (!mm->mm_child[c].mc_tried)
323                         return (c);
324
325         /*
326          * Every child failed.  There's no place left to look.
327          */
328         return (-1);
329 }
330
331 static int
332 vdev_mirror_io_start(zio_t *zio)
333 {
334         mirror_map_t *mm;
335         mirror_child_t *mc;
336         int c, children;
337
338         mm = vdev_mirror_map_alloc(zio);
339
340         if (zio->io_type == ZIO_TYPE_READ) {
341                 if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) {
342                         /*
343                          * For scrubbing reads we need to allocate a read
344                          * buffer for each child and issue reads to all
345                          * children.  If any child succeeds, it will copy its
346                          * data into zio->io_data in vdev_mirror_scrub_done.
347                          */
348                         for (c = 0; c < mm->mm_children; c++) {
349                                 mc = &mm->mm_child[c];
350                                 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
351                                     mc->mc_vd, mc->mc_offset,
352                                     zio_buf_alloc(zio->io_size), zio->io_size,
353                                     zio->io_type, zio->io_priority, 0,
354                                     vdev_mirror_scrub_done, mc));
355                         }
356                         return (ZIO_PIPELINE_CONTINUE);
357                 }
358                 /*
359                  * For normal reads just pick one child.
360                  */
361                 c = vdev_mirror_child_select(zio);
362                 children = (c >= 0);
363         } else {
364                 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
365
366                 /*
367                  * Writes go to all children.
368                  */
369                 c = 0;
370                 children = mm->mm_children;
371         }
372
373         while (children--) {
374                 mc = &mm->mm_child[c];
375                 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
376                     mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
377                     zio->io_type, zio->io_priority, 0,
378                     vdev_mirror_child_done, mc));
379                 c++;
380         }
381
382         return (ZIO_PIPELINE_CONTINUE);
383 }
384
385 static int
386 vdev_mirror_worst_error(mirror_map_t *mm)
387 {
388         int c, error[2] = { 0, 0 };
389
390         for (c = 0; c < mm->mm_children; c++) {
391                 mirror_child_t *mc = &mm->mm_child[c];
392                 int s = mc->mc_speculative;
393                 error[s] = zio_worst_error(error[s], mc->mc_error);
394         }
395
396         return (error[0] ? error[0] : error[1]);
397 }
398
399 static void
400 vdev_mirror_io_done(zio_t *zio)
401 {
402         mirror_map_t *mm = zio->io_vsd;
403         mirror_child_t *mc;
404         int c;
405         int good_copies = 0;
406         int unexpected_errors = 0;
407
408         for (c = 0; c < mm->mm_children; c++) {
409                 mc = &mm->mm_child[c];
410
411                 if (mc->mc_error) {
412                         if (!mc->mc_skipped)
413                                 unexpected_errors++;
414                 } else if (mc->mc_tried) {
415                         good_copies++;
416                 }
417         }
418
419         if (zio->io_type == ZIO_TYPE_WRITE) {
420                 /*
421                  * XXX -- for now, treat partial writes as success.
422                  *
423                  * Now that we support write reallocation, it would be better
424                  * to treat partial failure as real failure unless there are
425                  * no non-degraded top-level vdevs left, and not update DTLs
426                  * if we intend to reallocate.
427                  */
428                 /* XXPOLICY */
429                 if (good_copies != mm->mm_children) {
430                         /*
431                          * Always require at least one good copy.
432                          *
433                          * For ditto blocks (io_vd == NULL), require
434                          * all copies to be good.
435                          *
436                          * XXX -- for replacing vdevs, there's no great answer.
437                          * If the old device is really dead, we may not even
438                          * be able to access it -- so we only want to
439                          * require good writes to the new device.  But if
440                          * the new device turns out to be flaky, we want
441                          * to be able to detach it -- which requires all
442                          * writes to the old device to have succeeded.
443                          */
444                         if (good_copies == 0 || zio->io_vd == NULL)
445                                 zio->io_error = vdev_mirror_worst_error(mm);
446                 }
447                 return;
448         }
449
450         ASSERT(zio->io_type == ZIO_TYPE_READ);
451
452         /*
453          * If we don't have a good copy yet, keep trying other children.
454          */
455         /* XXPOLICY */
456         if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
457                 ASSERT(c >= 0 && c < mm->mm_children);
458                 mc = &mm->mm_child[c];
459                 zio_vdev_io_redone(zio);
460                 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
461                     mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
462                     ZIO_TYPE_READ, zio->io_priority, 0,
463                     vdev_mirror_child_done, mc));
464                 return;
465         }
466
467         /* XXPOLICY */
468         if (good_copies == 0) {
469                 zio->io_error = vdev_mirror_worst_error(mm);
470                 ASSERT(zio->io_error != 0);
471         }
472
473         if (good_copies && spa_writeable(zio->io_spa) &&
474             (unexpected_errors ||
475             (zio->io_flags & ZIO_FLAG_RESILVER) ||
476             ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
477                 /*
478                  * Use the good data we have in hand to repair damaged children.
479                  */
480                 for (c = 0; c < mm->mm_children; c++) {
481                         /*
482                          * Don't rewrite known good children.
483                          * Not only is it unnecessary, it could
484                          * actually be harmful: if the system lost
485                          * power while rewriting the only good copy,
486                          * there would be no good copies left!
487                          */
488                         mc = &mm->mm_child[c];
489
490                         if (mc->mc_error == 0) {
491                                 if (mc->mc_tried)
492                                         continue;
493                                 if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
494                                     !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
495                                     zio->io_txg, 1))
496                                         continue;
497                                 mc->mc_error = ESTALE;
498                         }
499
500                         zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
501                             mc->mc_vd, mc->mc_offset,
502                             zio->io_data, zio->io_size,
503                             ZIO_TYPE_WRITE, zio->io_priority,
504                             ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
505                             ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
506                 }
507         }
508 }
509
510 static void
511 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
512 {
513         if (faulted == vd->vdev_children)
514                 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
515                     VDEV_AUX_NO_REPLICAS);
516         else if (degraded + faulted != 0)
517                 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
518         else
519                 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
520 }
521
522 vdev_ops_t vdev_mirror_ops = {
523         vdev_mirror_open,
524         vdev_mirror_close,
525         vdev_default_asize,
526         vdev_mirror_io_start,
527         vdev_mirror_io_done,
528         vdev_mirror_state_change,
529         NULL,
530         NULL,
531         VDEV_TYPE_MIRROR,       /* name of this vdev type */
532         B_FALSE                 /* not a leaf vdev */
533 };
534
535 vdev_ops_t vdev_replacing_ops = {
536         vdev_mirror_open,
537         vdev_mirror_close,
538         vdev_default_asize,
539         vdev_mirror_io_start,
540         vdev_mirror_io_done,
541         vdev_mirror_state_change,
542         NULL,
543         NULL,
544         VDEV_TYPE_REPLACING,    /* name of this vdev type */
545         B_FALSE                 /* not a leaf vdev */
546 };
547
548 vdev_ops_t vdev_spare_ops = {
549         vdev_mirror_open,
550         vdev_mirror_close,
551         vdev_default_asize,
552         vdev_mirror_io_start,
553         vdev_mirror_io_done,
554         vdev_mirror_state_change,
555         NULL,
556         NULL,
557         VDEV_TYPE_SPARE,        /* name of this vdev type */
558         B_FALSE                 /* not a leaf vdev */
559 };
560
561 #if defined(_KERNEL) && defined(HAVE_SPL)
562 module_param(zfs_vdev_mirror_switch_us, int, 0644);
563 MODULE_PARM_DESC(zfs_vdev_mirror_switch_us, "Switch mirrors every N usecs");
564 #endif