2 * Copyright (c) 2004, 2007 Lukas Ertl
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
33 #include <sys/malloc.h>
34 #include <sys/systm.h>
36 #include <geom/geom.h>
37 #include <geom/vinum/geom_vinum_var.h>
38 #include <geom/vinum/geom_vinum_raid5.h>
39 #include <geom/vinum/geom_vinum.h>
41 static int gv_raid5_offset(struct gv_plex *, off_t, off_t,
42 off_t *, off_t *, int *, int *, int);
43 static struct bio * gv_raid5_clone_bio(struct bio *, struct gv_sd *,
44 struct gv_raid5_packet *, caddr_t, int);
45 static int gv_raid5_request(struct gv_plex *, struct gv_raid5_packet *,
46 struct bio *, caddr_t, off_t, off_t, int *);
47 static int gv_raid5_check(struct gv_plex *, struct gv_raid5_packet *,
48 struct bio *, caddr_t, off_t, off_t);
49 static int gv_raid5_rebuild(struct gv_plex *, struct gv_raid5_packet *,
50 struct bio *, caddr_t, off_t, off_t);
52 struct gv_raid5_packet *
53 gv_raid5_start(struct gv_plex *p, struct bio *bp, caddr_t addr, off_t boff,
57 struct gv_raid5_packet *wp, *wp2;
58 struct gv_bioq *bq, *bq2;
62 wp = g_malloc(sizeof(*wp), M_WAITOK | M_ZERO);
66 TAILQ_INIT(&wp->bits);
68 if (bp->bio_pflags & GV_BIO_REBUILD)
69 err = gv_raid5_rebuild(p, wp, bp, addr, boff, bcount);
70 else if (bp->bio_pflags & GV_BIO_CHECK)
71 err = gv_raid5_check(p, wp, bp, addr, boff, bcount);
73 err = gv_raid5_request(p, wp, bp, addr, boff, bcount, &delay);
75 /* Means we have a delayed request. */
82 * Building the sub-request failed, we probably need to clean up a lot.
85 G_VINUM_LOGREQ(0, bp, "raid5 plex request failed.");
86 TAILQ_FOREACH_SAFE(bq, &wp->bits, queue, bq2) {
87 TAILQ_REMOVE(&wp->bits, bq, queue);
90 if (wp->waiting != NULL) {
91 if (wp->waiting->bio_cflags & GV_BIO_MALLOC)
92 g_free(wp->waiting->bio_data);
93 g_destroy_bio(wp->waiting);
95 if (wp->parity != NULL) {
96 if (wp->parity->bio_cflags & GV_BIO_MALLOC)
97 g_free(wp->parity->bio_data);
98 g_destroy_bio(wp->parity);
102 TAILQ_FOREACH_SAFE(wp, &p->packets, list, wp2) {
106 TAILQ_REMOVE(&p->packets, wp, list);
107 TAILQ_FOREACH_SAFE(bq, &wp->bits, queue, bq2) {
108 TAILQ_REMOVE(&wp->bits, bq, queue);
114 cbp = bioq_takefirst(p->bqueue);
115 while (cbp != NULL) {
116 if (cbp->bio_cflags & GV_BIO_MALLOC)
117 g_free(cbp->bio_data);
119 cbp = bioq_takefirst(p->bqueue);
122 /* If internal, stop and reset state. */
123 if (bp->bio_pflags & GV_BIO_INTERNAL) {
124 if (bp->bio_pflags & GV_BIO_MALLOC)
125 g_free(bp->bio_data);
128 p->flags &= ~(GV_PLEX_SYNCING | GV_PLEX_REBUILDING |
132 g_io_deliver(bp, err);
140 * Check if the stripe that the work packet wants is already being used by
141 * some other work packet.
144 gv_stripe_active(struct gv_plex *p, struct bio *bp)
146 struct gv_raid5_packet *wp, *owp;
149 wp = bp->bio_caller2;
150 if (wp->lockbase == -1)
154 TAILQ_FOREACH(owp, &p->packets, list) {
157 if ((wp->lockbase >= owp->lockbase) &&
158 (wp->lockbase <= owp->lockbase + owp->length)) {
162 if ((wp->lockbase <= owp->lockbase) &&
163 (wp->lockbase + wp->length >= owp->lockbase)) {
173 gv_raid5_check(struct gv_plex *p, struct gv_raid5_packet *wp, struct bio *bp,
174 caddr_t addr, off_t boff, off_t bcount)
176 struct gv_sd *parity, *s;
180 off_t real_len, real_off;
182 if (p == NULL || LIST_EMPTY(&p->subdisks))
185 gv_raid5_offset(p, boff, bcount, &real_off, &real_len, NULL, &psdno, 1);
187 /* Find the right subdisk. */
190 LIST_FOREACH(s, &p->subdisks, in_plex) {
198 /* Parity stripe not found. */
202 if (parity->state != GV_SD_UP)
205 wp->length = real_len;
207 wp->lockbase = real_off;
209 /* Read all subdisks. */
210 LIST_FOREACH(s, &p->subdisks, in_plex) {
211 /* Skip the parity subdisk. */
214 /* Skip growing subdisks. */
215 if (s->flags & GV_SD_GROW)
218 cbp = gv_raid5_clone_bio(bp, s, wp, NULL, 1);
221 cbp->bio_cmd = BIO_READ;
223 bioq_insert_tail(p->bqueue, cbp);
225 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
227 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
230 /* Read the parity data. */
231 cbp = gv_raid5_clone_bio(bp, parity, wp, NULL, 1);
234 cbp->bio_cmd = BIO_READ;
238 * In case we want to rebuild the parity, create an extra BIO to write
239 * it out. It also acts as buffer for the XOR operations.
241 cbp = gv_raid5_clone_bio(bp, parity, wp, addr, 1);
249 /* Rebuild a degraded RAID5 plex. */
251 gv_raid5_rebuild(struct gv_plex *p, struct gv_raid5_packet *wp, struct bio *bp,
252 caddr_t addr, off_t boff, off_t bcount)
254 struct gv_sd *broken, *s;
257 off_t real_len, real_off;
259 if (p == NULL || LIST_EMPTY(&p->subdisks))
262 gv_raid5_offset(p, boff, bcount, &real_off, &real_len, NULL, NULL, 1);
264 /* Find the right subdisk. */
266 LIST_FOREACH(s, &p->subdisks, in_plex) {
267 if (s->state != GV_SD_UP)
271 /* Broken stripe not found. */
275 switch (broken->state) {
280 if (!(bp->bio_pflags & GV_BIO_REBUILD))
283 G_VINUM_DEBUG(1, "sd %s is reviving", broken->name);
284 gv_set_sd_state(broken, GV_SD_REVIVING, GV_SETSTATE_FORCE);
285 /* Set this bit now, but should be set at end. */
286 broken->flags |= GV_SD_CANGOUP;
293 /* All other subdisk states mean it's not accessible. */
297 wp->length = real_len;
299 wp->lockbase = real_off;
301 KASSERT(wp->length >= 0, ("gv_rebuild_raid5: wp->length < 0"));
303 /* Read all subdisks. */
304 LIST_FOREACH(s, &p->subdisks, in_plex) {
305 /* Skip the broken subdisk. */
309 /* Skip growing subdisks. */
310 if (s->flags & GV_SD_GROW)
313 cbp = gv_raid5_clone_bio(bp, s, wp, NULL, 1);
316 cbp->bio_cmd = BIO_READ;
318 bioq_insert_tail(p->bqueue, cbp);
320 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
322 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
325 /* Write the parity data. */
326 cbp = gv_raid5_clone_bio(bp, broken, wp, NULL, 1);
333 /* Post notification that we're finished. */
337 /* Build a request group to perform (part of) a RAID5 request. */
339 gv_raid5_request(struct gv_plex *p, struct gv_raid5_packet *wp,
340 struct bio *bp, caddr_t addr, off_t boff, off_t bcount, int *delay)
343 struct gv_sd *broken, *original, *parity, *s;
346 int i, psdno, sdno, type, grow;
347 off_t real_len, real_off;
349 gp = bp->bio_to->geom;
351 if (p == NULL || LIST_EMPTY(&p->subdisks))
354 /* We are optimistic and assume that this request will be OK. */
355 #define REQ_TYPE_NORMAL 0
356 #define REQ_TYPE_DEGRADED 1
357 #define REQ_TYPE_NOPARITY 2
359 type = REQ_TYPE_NORMAL;
360 original = parity = broken = NULL;
362 /* XXX: The resize won't crash with rebuild or sync, but we should still
363 * be aware of it. Also this should perhaps be done on rebuild/check as
366 /* If we're over, we must use the old. */
367 if (boff >= p->synced) {
369 /* Or if over the resized offset, we use all drives. */
370 } else if (boff + bcount <= p->synced) {
372 /* Else, we're in the middle, and must wait a bit. */
374 bioq_disksort(p->rqueue, bp);
378 gv_raid5_offset(p, boff, bcount, &real_off, &real_len,
379 &sdno, &psdno, grow);
381 /* Find the right subdisks. */
383 LIST_FOREACH(s, &p->subdisks, in_plex) {
388 if (s->state != GV_SD_UP)
393 if ((original == NULL) || (parity == NULL))
396 /* Our data stripe is missing. */
397 if (original->state != GV_SD_UP)
398 type = REQ_TYPE_DEGRADED;
400 /* If synchronizing request, just write it if disks are stale. */
401 if (original->state == GV_SD_STALE && parity->state == GV_SD_STALE &&
402 bp->bio_pflags & GV_BIO_SYNCREQ && bp->bio_cmd == BIO_WRITE) {
403 type = REQ_TYPE_NORMAL;
404 /* Our parity stripe is missing. */
405 } else if (parity->state != GV_SD_UP) {
406 /* We cannot take another failure if we're already degraded. */
407 if (type != REQ_TYPE_NORMAL)
410 type = REQ_TYPE_NOPARITY;
413 wp->length = real_len;
415 wp->lockbase = real_off;
417 KASSERT(wp->length >= 0, ("gv_build_raid5_request: wp->length < 0"));
419 if ((p->flags & GV_PLEX_REBUILDING) && (boff + real_len < p->synced))
420 type = REQ_TYPE_NORMAL;
422 if ((p->flags & GV_PLEX_REBUILDING) && (boff + real_len >= p->synced)) {
423 bioq_disksort(p->rqueue, bp);
428 switch (bp->bio_cmd) {
431 * For a degraded read we need to read in all stripes except
432 * the broken one plus the parity stripe and then recalculate
435 if (type == REQ_TYPE_DEGRADED) {
436 bzero(wp->data, wp->length);
437 LIST_FOREACH(s, &p->subdisks, in_plex) {
438 /* Skip the broken subdisk. */
441 /* Skip growing if within offset. */
442 if (grow && s->flags & GV_SD_GROW)
444 cbp = gv_raid5_clone_bio(bp, s, wp, NULL, 1);
448 bioq_insert_tail(p->bqueue, cbp);
450 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
452 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
455 /* A normal read can be fulfilled with the original subdisk. */
457 cbp = gv_raid5_clone_bio(bp, original, wp, addr, 0);
461 bioq_insert_tail(p->bqueue, cbp);
469 * A degraded write means we cannot write to the original data
470 * subdisk. Thus we need to read in all valid stripes,
471 * recalculate the parity from the original data, and then
472 * write the parity stripe back out.
474 if (type == REQ_TYPE_DEGRADED) {
475 /* Read all subdisks. */
476 LIST_FOREACH(s, &p->subdisks, in_plex) {
477 /* Skip the broken and the parity subdisk. */
478 if ((s == broken) || (s == parity))
480 /* Skip growing if within offset. */
481 if (grow && s->flags & GV_SD_GROW)
484 cbp = gv_raid5_clone_bio(bp, s, wp, NULL, 1);
487 cbp->bio_cmd = BIO_READ;
489 bioq_insert_tail(p->bqueue, cbp);
491 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
493 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
496 /* Write the parity data. */
497 cbp = gv_raid5_clone_bio(bp, parity, wp, NULL, 1);
500 bcopy(addr, cbp->bio_data, wp->length);
504 * When the parity stripe is missing we just write out the data.
506 } else if (type == REQ_TYPE_NOPARITY) {
507 cbp = gv_raid5_clone_bio(bp, original, wp, addr, 1);
511 bioq_insert_tail(p->bqueue, cbp);
513 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
515 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
518 * A normal write request goes to the original subdisk, then we
519 * read in all other stripes, recalculate the parity and write
520 * out the parity again.
523 /* Read old parity. */
524 cbp = gv_raid5_clone_bio(bp, parity, wp, NULL, 1);
527 cbp->bio_cmd = BIO_READ;
529 bioq_insert_tail(p->bqueue, cbp);
531 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
533 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
536 cbp = gv_raid5_clone_bio(bp, original, wp, NULL, 1);
539 cbp->bio_cmd = BIO_READ;
541 bioq_insert_tail(p->bqueue, cbp);
543 bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
545 TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
547 /* Write new data. */
548 cbp = gv_raid5_clone_bio(bp, original, wp, addr, 1);
553 * We must not write the new data until the old data
554 * was read, so hold this BIO back until we're ready
559 /* The final bio for the parity. */
560 cbp = gv_raid5_clone_bio(bp, parity, wp, NULL, 1);
564 /* Remember that this is the BIO for the parity data. */
577 * Calculate the offsets in the various subdisks for a RAID5 request. Also take
578 * care of new subdisks in an expanded RAID5 array.
579 * XXX: This assumes that the new subdisks are inserted after the others (which
580 * is okay as long as plex_offset is larger). If subdisks are inserted into the
581 * plexlist before, we get problems.
584 gv_raid5_offset(struct gv_plex *p, off_t boff, off_t bcount, off_t *real_off,
585 off_t *real_len, int *sdno, int *psdno, int growing)
588 int sd, psd, sdcount;
589 off_t len_left, stripeend, stripeoff, stripestart;
591 sdcount = p->sdcount;
593 LIST_FOREACH(s, &p->subdisks, in_plex) {
594 if (s->flags & GV_SD_GROW)
599 /* The number of the subdisk containing the parity stripe. */
600 psd = sdcount - 1 - ( boff / (p->stripesize * (sdcount - 1))) %
602 KASSERT(psdno >= 0, ("gv_raid5_offset: psdno < 0"));
604 /* Offset of the start address from the start of the stripe. */
605 stripeoff = boff % (p->stripesize * (sdcount - 1));
606 KASSERT(stripeoff >= 0, ("gv_raid5_offset: stripeoff < 0"));
608 /* The number of the subdisk where the stripe resides. */
609 sd = stripeoff / p->stripesize;
610 KASSERT(sdno >= 0, ("gv_raid5_offset: sdno < 0"));
612 /* At or past parity subdisk. */
616 /* The offset of the stripe on this subdisk. */
617 stripestart = (boff - stripeoff) / (sdcount - 1);
618 KASSERT(stripestart >= 0, ("gv_raid5_offset: stripestart < 0"));
620 stripeoff %= p->stripesize;
622 /* The offset of the request on this subdisk. */
623 *real_off = stripestart + stripeoff;
625 stripeend = stripestart + p->stripesize;
626 len_left = stripeend - *real_off;
627 KASSERT(len_left >= 0, ("gv_raid5_offset: len_left < 0"));
629 *real_len = (bcount <= len_left) ? bcount : len_left;
640 gv_raid5_clone_bio(struct bio *bp, struct gv_sd *s, struct gv_raid5_packet *wp,
641 caddr_t addr, int use_wp)
645 cbp = g_clone_bio(bp);
649 cbp->bio_data = g_malloc(wp->length, M_WAITOK | M_ZERO);
650 cbp->bio_cflags |= GV_BIO_MALLOC;
652 cbp->bio_data = addr;
653 cbp->bio_offset = wp->lockbase + s->drive_offset;
654 cbp->bio_length = wp->length;
655 cbp->bio_done = gv_done;
656 cbp->bio_caller1 = s;
658 cbp->bio_caller2 = wp;