2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004, 2007 Lukas Ertl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/malloc.h>
36 #include <sys/systm.h>
38 #include <geom/geom.h>
39 #include <geom/geom_dbg.h>
40 #include <geom/vinum/geom_vinum_var.h>
41 #include <geom/vinum/geom_vinum_raid5.h>
42 #include <geom/vinum/geom_vinum.h>
44 static int gv_raid5_offset(struct gv_plex *, off_t, off_t,
45 off_t *, off_t *, int *, int *, int);
46 static struct bio * gv_raid5_clone_bio(struct bio *, struct gv_sd *,
47 struct gv_raid5_packet *, caddr_t, int);
48 static int gv_raid5_request(struct gv_plex *, struct gv_raid5_packet *,
49 struct bio *, caddr_t, off_t, off_t, int *);
50 static int gv_raid5_check(struct gv_plex *, struct gv_raid5_packet *,
51 struct bio *, caddr_t, off_t, off_t);
52 static int gv_raid5_rebuild(struct gv_plex *, struct gv_raid5_packet *,
53 struct bio *, caddr_t, off_t, off_t);
55 struct gv_raid5_packet *
56 gv_raid5_start(struct gv_plex *p, struct bio *bp, caddr_t addr, off_t boff,
60 struct gv_raid5_packet *wp, *wp2;
61 struct gv_bioq *bq, *bq2;
65 wp = g_malloc(sizeof(*wp), M_WAITOK | M_ZERO);
69 TAILQ_INIT(&wp->bits);
71 if (bp->bio_pflags & GV_BIO_REBUILD)
72 err = gv_raid5_rebuild(p, wp, bp, addr, boff, bcount);
73 else if (bp->bio_pflags & GV_BIO_CHECK)
74 err = gv_raid5_check(p, wp, bp, addr, boff, bcount);
76 err = gv_raid5_request(p, wp, bp, addr, boff, bcount, &delay);
78 /* Means we have a delayed request. */
85 * Building the sub-request failed, we probably need to clean up a lot.
88 G_VINUM_LOGREQ(0, bp, "raid5 plex request failed.");
89 TAILQ_FOREACH_SAFE(bq, &wp->bits, queue, bq2) {
90 TAILQ_REMOVE(&wp->bits, bq, queue);
93 if (wp->waiting != NULL) {
94 if (wp->waiting->bio_cflags & GV_BIO_MALLOC)
95 g_free(wp->waiting->bio_data);
96 g_destroy_bio(wp->waiting);
98 if (wp->parity != NULL) {
99 if (wp->parity->bio_cflags & GV_BIO_MALLOC)
100 g_free(wp->parity->bio_data);
101 g_destroy_bio(wp->parity);
105 TAILQ_FOREACH_SAFE(wp, &p->packets, list, wp2) {
109 TAILQ_REMOVE(&p->packets, wp, list);
110 TAILQ_FOREACH_SAFE(bq, &wp->bits, queue, bq2) {
111 TAILQ_REMOVE(&wp->bits, bq, queue);
117 cbp = bioq_takefirst(p->bqueue);
118 while (cbp != NULL) {
119 if (cbp->bio_cflags & GV_BIO_MALLOC)
120 g_free(cbp->bio_data);
122 cbp = bioq_takefirst(p->bqueue);
125 /* If internal, stop and reset state. */
126 if (bp->bio_pflags & GV_BIO_INTERNAL) {
127 if (bp->bio_pflags & GV_BIO_MALLOC)
128 g_free(bp->bio_data);
131 p->flags &= ~(GV_PLEX_SYNCING | GV_PLEX_REBUILDING |
135 g_io_deliver(bp, err);
143 * Check if the stripe that the work packet wants is already being used by
144 * some other work packet.
147 gv_stripe_active(struct gv_plex *p, struct bio *bp)
149 struct gv_raid5_packet *wp, *owp;
152 wp = bp->bio_caller2;
153 if (wp->lockbase == -1)
157 TAILQ_FOREACH(owp, &p->packets, list) {
160 if ((wp->lockbase >= owp->lockbase) &&
161 (wp->lockbase <= owp->lockbase + owp->length)) {
165 if ((wp->lockbase <= owp->lockbase) &&
166 (wp->lockbase + wp->length >= owp->lockbase)) {
176 gv_raid5_check(struct gv_plex *p, struct gv_raid5_packet *wp, struct bio *bp,
177 caddr_t addr, off_t boff, off_t bcount)
179 struct gv_sd *parity, *s;
183 off_t real_len, real_off;
185 if (p == NULL || LIST_EMPTY(&p->subdisks))
188 gv_raid5_offset(p, boff, bcount, &real_off, &real_len, NULL, &psdno, 1);
190 /* Find the right subdisk. */
193 LIST_FOREACH(s, &p->subdisks, in_plex) {
201 /* Parity stripe not found. */
205 if (parity->state != GV_SD_UP)
208 wp->length = real_len;
210 wp->lockbase = real_off;
212 /* Read all subdisks. */
213 LIST_FOREACH(s, &p->subdisks, in_plex) {
214 /* Skip the parity subdisk. */
217 /* Skip growing subdisks. */
218 if (s->flags & GV_SD_GROW)
221 cbp = gv_raid5_clone_bio(bp, s, wp, NULL, 1);
224 cbp->bio_cmd = BIO_READ;
226 bioq_insert_tail(p->bqueue, cbp);
228 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
230 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
233 /* Read the parity data. */
234 cbp = gv_raid5_clone_bio(bp, parity, wp, NULL, 1);
237 cbp->bio_cmd = BIO_READ;
241 * In case we want to rebuild the parity, create an extra BIO to write
242 * it out. It also acts as buffer for the XOR operations.
244 cbp = gv_raid5_clone_bio(bp, parity, wp, addr, 1);
252 /* Rebuild a degraded RAID5 plex. */
254 gv_raid5_rebuild(struct gv_plex *p, struct gv_raid5_packet *wp, struct bio *bp,
255 caddr_t addr, off_t boff, off_t bcount)
257 struct gv_sd *broken, *s;
260 off_t real_len, real_off;
262 if (p == NULL || LIST_EMPTY(&p->subdisks))
265 gv_raid5_offset(p, boff, bcount, &real_off, &real_len, NULL, NULL, 1);
267 /* Find the right subdisk. */
269 LIST_FOREACH(s, &p->subdisks, in_plex) {
270 if (s->state != GV_SD_UP)
274 /* Broken stripe not found. */
278 switch (broken->state) {
283 if (!(bp->bio_pflags & GV_BIO_REBUILD))
286 G_VINUM_DEBUG(1, "sd %s is reviving", broken->name);
287 gv_set_sd_state(broken, GV_SD_REVIVING, GV_SETSTATE_FORCE);
288 /* Set this bit now, but should be set at end. */
289 broken->flags |= GV_SD_CANGOUP;
296 /* All other subdisk states mean it's not accessible. */
300 wp->length = real_len;
302 wp->lockbase = real_off;
304 KASSERT(wp->length >= 0, ("gv_rebuild_raid5: wp->length < 0"));
306 /* Read all subdisks. */
307 LIST_FOREACH(s, &p->subdisks, in_plex) {
308 /* Skip the broken subdisk. */
312 /* Skip growing subdisks. */
313 if (s->flags & GV_SD_GROW)
316 cbp = gv_raid5_clone_bio(bp, s, wp, NULL, 1);
319 cbp->bio_cmd = BIO_READ;
321 bioq_insert_tail(p->bqueue, cbp);
323 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
325 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
328 /* Write the parity data. */
329 cbp = gv_raid5_clone_bio(bp, broken, wp, NULL, 1);
336 /* Post notification that we're finished. */
340 /* Build a request group to perform (part of) a RAID5 request. */
342 gv_raid5_request(struct gv_plex *p, struct gv_raid5_packet *wp,
343 struct bio *bp, caddr_t addr, off_t boff, off_t bcount, int *delay)
346 struct gv_sd *broken, *original, *parity, *s;
349 int i, psdno, sdno, type, grow;
350 off_t real_len, real_off;
352 gp = bp->bio_to->geom;
354 if (p == NULL || LIST_EMPTY(&p->subdisks))
357 /* We are optimistic and assume that this request will be OK. */
358 #define REQ_TYPE_NORMAL 0
359 #define REQ_TYPE_DEGRADED 1
360 #define REQ_TYPE_NOPARITY 2
362 type = REQ_TYPE_NORMAL;
363 original = parity = broken = NULL;
365 /* XXX: The resize won't crash with rebuild or sync, but we should still
366 * be aware of it. Also this should perhaps be done on rebuild/check as
369 /* If we're over, we must use the old. */
370 if (boff >= p->synced) {
372 /* Or if over the resized offset, we use all drives. */
373 } else if (boff + bcount <= p->synced) {
375 /* Else, we're in the middle, and must wait a bit. */
377 bioq_disksort(p->rqueue, bp);
381 gv_raid5_offset(p, boff, bcount, &real_off, &real_len,
382 &sdno, &psdno, grow);
384 /* Find the right subdisks. */
386 LIST_FOREACH(s, &p->subdisks, in_plex) {
391 if (s->state != GV_SD_UP)
396 if ((original == NULL) || (parity == NULL))
399 /* Our data stripe is missing. */
400 if (original->state != GV_SD_UP)
401 type = REQ_TYPE_DEGRADED;
403 /* If synchronizing request, just write it if disks are stale. */
404 if (original->state == GV_SD_STALE && parity->state == GV_SD_STALE &&
405 bp->bio_pflags & GV_BIO_SYNCREQ && bp->bio_cmd == BIO_WRITE) {
406 type = REQ_TYPE_NORMAL;
407 /* Our parity stripe is missing. */
408 } else if (parity->state != GV_SD_UP) {
409 /* We cannot take another failure if we're already degraded. */
410 if (type != REQ_TYPE_NORMAL)
413 type = REQ_TYPE_NOPARITY;
416 wp->length = real_len;
418 wp->lockbase = real_off;
420 KASSERT(wp->length >= 0, ("gv_build_raid5_request: wp->length < 0"));
422 if ((p->flags & GV_PLEX_REBUILDING) && (boff + real_len < p->synced))
423 type = REQ_TYPE_NORMAL;
425 if ((p->flags & GV_PLEX_REBUILDING) && (boff + real_len >= p->synced)) {
426 bioq_disksort(p->rqueue, bp);
431 switch (bp->bio_cmd) {
434 * For a degraded read we need to read in all stripes except
435 * the broken one plus the parity stripe and then recalculate
438 if (type == REQ_TYPE_DEGRADED) {
439 bzero(wp->data, wp->length);
440 LIST_FOREACH(s, &p->subdisks, in_plex) {
441 /* Skip the broken subdisk. */
444 /* Skip growing if within offset. */
445 if (grow && s->flags & GV_SD_GROW)
447 cbp = gv_raid5_clone_bio(bp, s, wp, NULL, 1);
451 bioq_insert_tail(p->bqueue, cbp);
453 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
455 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
458 /* A normal read can be fulfilled with the original subdisk. */
460 cbp = gv_raid5_clone_bio(bp, original, wp, addr, 0);
464 bioq_insert_tail(p->bqueue, cbp);
472 * A degraded write means we cannot write to the original data
473 * subdisk. Thus we need to read in all valid stripes,
474 * recalculate the parity from the original data, and then
475 * write the parity stripe back out.
477 if (type == REQ_TYPE_DEGRADED) {
478 /* Read all subdisks. */
479 LIST_FOREACH(s, &p->subdisks, in_plex) {
480 /* Skip the broken and the parity subdisk. */
481 if ((s == broken) || (s == parity))
483 /* Skip growing if within offset. */
484 if (grow && s->flags & GV_SD_GROW)
487 cbp = gv_raid5_clone_bio(bp, s, wp, NULL, 1);
490 cbp->bio_cmd = BIO_READ;
492 bioq_insert_tail(p->bqueue, cbp);
494 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
496 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
499 /* Write the parity data. */
500 cbp = gv_raid5_clone_bio(bp, parity, wp, NULL, 1);
503 bcopy(addr, cbp->bio_data, wp->length);
507 * When the parity stripe is missing we just write out the data.
509 } else if (type == REQ_TYPE_NOPARITY) {
510 cbp = gv_raid5_clone_bio(bp, original, wp, addr, 1);
514 bioq_insert_tail(p->bqueue, cbp);
516 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
518 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
521 * A normal write request goes to the original subdisk, then we
522 * read in all other stripes, recalculate the parity and write
523 * out the parity again.
526 /* Read old parity. */
527 cbp = gv_raid5_clone_bio(bp, parity, wp, NULL, 1);
530 cbp->bio_cmd = BIO_READ;
532 bioq_insert_tail(p->bqueue, cbp);
534 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
536 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
539 cbp = gv_raid5_clone_bio(bp, original, wp, NULL, 1);
542 cbp->bio_cmd = BIO_READ;
544 bioq_insert_tail(p->bqueue, cbp);
546 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
548 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
550 /* Write new data. */
551 cbp = gv_raid5_clone_bio(bp, original, wp, addr, 1);
556 * We must not write the new data until the old data
557 * was read, so hold this BIO back until we're ready
562 /* The final bio for the parity. */
563 cbp = gv_raid5_clone_bio(bp, parity, wp, NULL, 1);
567 /* Remember that this is the BIO for the parity data. */
580 * Calculate the offsets in the various subdisks for a RAID5 request. Also take
581 * care of new subdisks in an expanded RAID5 array.
582 * XXX: This assumes that the new subdisks are inserted after the others (which
583 * is okay as long as plex_offset is larger). If subdisks are inserted into the
584 * plexlist before, we get problems.
587 gv_raid5_offset(struct gv_plex *p, off_t boff, off_t bcount, off_t *real_off,
588 off_t *real_len, int *sdno, int *psdno, int growing)
591 int sd, psd, sdcount;
592 off_t len_left, stripeend, stripeoff, stripestart;
594 sdcount = p->sdcount;
596 LIST_FOREACH(s, &p->subdisks, in_plex) {
597 if (s->flags & GV_SD_GROW)
602 /* The number of the subdisk containing the parity stripe. */
603 psd = sdcount - 1 - ( boff / (p->stripesize * (sdcount - 1))) %
605 KASSERT(psdno >= 0, ("gv_raid5_offset: psdno < 0"));
607 /* Offset of the start address from the start of the stripe. */
608 stripeoff = boff % (p->stripesize * (sdcount - 1));
609 KASSERT(stripeoff >= 0, ("gv_raid5_offset: stripeoff < 0"));
611 /* The number of the subdisk where the stripe resides. */
612 sd = stripeoff / p->stripesize;
613 KASSERT(sdno >= 0, ("gv_raid5_offset: sdno < 0"));
615 /* At or past parity subdisk. */
619 /* The offset of the stripe on this subdisk. */
620 stripestart = (boff - stripeoff) / (sdcount - 1);
621 KASSERT(stripestart >= 0, ("gv_raid5_offset: stripestart < 0"));
623 stripeoff %= p->stripesize;
625 /* The offset of the request on this subdisk. */
626 *real_off = stripestart + stripeoff;
628 stripeend = stripestart + p->stripesize;
629 len_left = stripeend - *real_off;
630 KASSERT(len_left >= 0, ("gv_raid5_offset: len_left < 0"));
632 *real_len = (bcount <= len_left) ? bcount : len_left;
643 gv_raid5_clone_bio(struct bio *bp, struct gv_sd *s, struct gv_raid5_packet *wp,
644 caddr_t addr, int use_wp)
648 cbp = g_clone_bio(bp);
652 cbp->bio_data = g_malloc(wp->length, M_WAITOK | M_ZERO);
653 cbp->bio_cflags |= GV_BIO_MALLOC;
655 cbp->bio_data = addr;
656 cbp->bio_offset = wp->lockbase + s->drive_offset;
657 cbp->bio_length = wp->length;
658 cbp->bio_done = gv_done;
659 cbp->bio_caller1 = s;
661 cbp->bio_caller2 = wp;