2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004, 2007 Lukas Ertl
5 * Copyright (c) 2007, 2009 Ulf Lilleengen
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
36 #include <sys/malloc.h>
37 #include <sys/systm.h>
39 #include <geom/geom.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_check_parity(struct gv_plex *, struct bio *,
45 struct gv_raid5_packet *);
46 static int gv_normal_parity(struct gv_plex *, struct bio *,
47 struct gv_raid5_packet *);
48 static void gv_plex_flush(struct gv_plex *);
49 static int gv_plex_offset(struct gv_plex *, off_t, off_t, off_t *, off_t *,
51 static int gv_plex_normal_request(struct gv_plex *, struct bio *, off_t,
53 static void gv_post_bio(struct gv_softc *, struct bio *);
56 gv_plex_start(struct gv_plex *p, struct bio *bp)
60 struct gv_raid5_packet *wp;
62 off_t bcount, boff, len;
64 bcount = bp->bio_length;
66 boff = bp->bio_offset;
68 /* Walk over the whole length of the request, we might split it up. */
73 * RAID5 plexes need special treatment, as a single request
74 * might involve several read/write sub-requests.
76 if (p->org == GV_PLEX_RAID5) {
77 wp = gv_raid5_start(p, bp, addr, boff, bcount);
83 if (TAILQ_EMPTY(&wp->bits))
85 else if (wp->lockbase != -1)
86 TAILQ_INSERT_TAIL(&p->packets, wp, list);
89 * Requests to concatenated and striped plexes go straight
93 len = gv_plex_normal_request(p, bp, boff, bcount, addr);
104 * Fire off all sub-requests. We get the correct consumer (== drive)
105 * to send each request to via the subdisk that was stored in
108 cbp = bioq_takefirst(p->bqueue);
109 while (cbp != NULL) {
111 * RAID5 sub-requests need to come in correct order, otherwise
112 * we trip over the parity, as it might be overwritten by
113 * another sub-request. We abuse cbp->bio_caller2 to mark
114 * potential overlap situations.
116 if (cbp->bio_caller2 != NULL && gv_stripe_active(p, cbp)) {
117 /* Park the bio on the waiting queue. */
118 cbp->bio_pflags |= GV_BIO_ONHOLD;
119 bioq_disksort(p->wqueue, cbp);
121 s = cbp->bio_caller1;
122 g_io_request(cbp, s->drive_sc->consumer);
124 cbp = bioq_takefirst(p->bqueue);
129 gv_plex_offset(struct gv_plex *p, off_t boff, off_t bcount, off_t *real_off,
130 off_t *real_len, int *sdno, int growing)
134 off_t len_left, stripeend, stripeno, stripestart;
139 * Find the subdisk where this request starts. The subdisks in
140 * this list must be ordered by plex_offset.
143 LIST_FOREACH(s, &p->subdisks, in_plex) {
144 if (s->plex_offset <= boff &&
145 s->plex_offset + s->size > boff) {
151 if (s == NULL || s->drive_sc == NULL)
152 return (GV_ERR_NOTFOUND);
154 /* Calculate corresponding offsets on disk. */
155 *real_off = boff - s->plex_offset;
156 len_left = s->size - (*real_off);
157 KASSERT(len_left >= 0, ("gv_plex_offset: len_left < 0"));
158 *real_len = (bcount > len_left) ? len_left : bcount;
161 case GV_PLEX_STRIPED:
162 /* The number of the stripe where the request starts. */
163 stripeno = boff / p->stripesize;
164 KASSERT(stripeno >= 0, ("gv_plex_offset: stripeno < 0"));
166 /* Take growing subdisks into account when calculating. */
167 sdcount = gv_sdcount(p, (boff >= p->synced));
169 if (!(boff + bcount <= p->synced) &&
170 (p->flags & GV_PLEX_GROWING) &&
172 return (GV_ERR_ISBUSY);
173 *sdno = stripeno % sdcount;
175 KASSERT(sdno >= 0, ("gv_plex_offset: sdno < 0"));
176 stripestart = (stripeno / sdcount) *
178 KASSERT(stripestart >= 0, ("gv_plex_offset: stripestart < 0"));
179 stripeend = stripestart + p->stripesize;
180 *real_off = boff - (stripeno * p->stripesize) +
182 len_left = stripeend - *real_off;
183 KASSERT(len_left >= 0, ("gv_plex_offset: len_left < 0"));
185 *real_len = (bcount <= len_left) ? bcount : len_left;
189 return (GV_ERR_PLEXORG);
195 * Prepare a normal plex request.
198 gv_plex_normal_request(struct gv_plex *p, struct bio *bp, off_t boff,
199 off_t bcount, caddr_t addr)
203 off_t real_len, real_off;
208 real_len = real_off = 0;
212 if (p == NULL || LIST_EMPTY(&p->subdisks))
215 err = gv_plex_offset(p, boff, bcount, &real_off,
216 &real_len, &sdno, (bp->bio_pflags & GV_BIO_GROW));
217 /* If the request was blocked, put it into wait. */
218 if (err == GV_ERR_ISBUSY) {
219 bioq_disksort(p->rqueue, bp);
220 return (-1); /* "Fail", and delay request. */
228 /* Find the right subdisk. */
230 LIST_FOREACH(s, &p->subdisks, in_plex) {
236 /* Subdisk not found. */
237 if (s == NULL || s->drive_sc == NULL)
240 /* Now check if we can handle the request on this subdisk. */
243 /* If the subdisk is up, just continue. */
246 if (bp->bio_pflags & GV_BIO_INTERNAL)
247 G_VINUM_DEBUG(0, "subdisk must be in the stale state in"
248 " order to perform administrative requests");
251 if (!(bp->bio_pflags & GV_BIO_SYNCREQ)) {
252 G_VINUM_DEBUG(0, "subdisk stale, unable to perform "
257 G_VINUM_DEBUG(1, "sd %s is initializing", s->name);
258 gv_set_sd_state(s, GV_SD_INITIALIZING, GV_SETSTATE_FORCE);
260 case GV_SD_INITIALIZING:
261 if (bp->bio_cmd == BIO_READ)
265 /* All other subdisk states mean it's not accessible. */
269 /* Clone the bio and adjust the offsets and sizes. */
270 cbp = g_clone_bio(bp);
275 cbp->bio_offset = real_off + s->drive_offset;
276 cbp->bio_length = real_len;
277 cbp->bio_data = addr;
278 cbp->bio_done = gv_done;
279 cbp->bio_caller1 = s;
281 /* Store the sub-requests now and let others issue them. */
282 bioq_insert_tail(p->bqueue, cbp);
285 G_VINUM_LOGREQ(0, bp, "plex request failed.");
286 /* Building the sub-request failed. If internal BIO, do not deliver. */
287 if (bp->bio_pflags & GV_BIO_INTERNAL) {
288 if (bp->bio_pflags & GV_BIO_MALLOC)
289 g_free(bp->bio_data);
291 p->flags &= ~(GV_PLEX_SYNCING | GV_PLEX_REBUILDING |
295 g_io_deliver(bp, err);
300 * Handle a completed request to a striped or concatenated plex.
303 gv_plex_normal_done(struct gv_plex *p, struct bio *bp)
307 pbp = bp->bio_parent;
308 if (pbp->bio_error == 0)
309 pbp->bio_error = bp->bio_error;
312 if (pbp->bio_children == pbp->bio_inbed) {
313 /* Just set it to length since multiple plexes will
314 * screw things up. */
315 pbp->bio_completed = pbp->bio_length;
316 if (pbp->bio_pflags & GV_BIO_SYNCREQ)
317 gv_sync_complete(p, pbp);
318 else if (pbp->bio_pflags & GV_BIO_GROW)
319 gv_grow_complete(p, pbp);
321 g_io_deliver(pbp, pbp->bio_error);
326 * Handle a completed request to a RAID-5 plex.
329 gv_plex_raid5_done(struct gv_plex *p, struct bio *bp)
332 struct bio *cbp, *pbp;
333 struct gv_bioq *bq, *bq2;
334 struct gv_raid5_packet *wp;
340 wp = bp->bio_caller2;
342 switch (bp->bio_parent->bio_cmd) {
345 completed = bp->bio_completed;
349 TAILQ_FOREACH_SAFE(bq, &wp->bits, queue, bq2) {
352 TAILQ_REMOVE(&wp->bits, bq, queue);
354 for (i = 0; i < wp->length; i++)
355 wp->data[i] ^= bp->bio_data[i];
358 if (TAILQ_EMPTY(&wp->bits)) {
359 completed = wp->length;
360 if (wp->lockbase != -1) {
361 TAILQ_REMOVE(&p->packets, wp, list);
362 /* Bring the waiting bios back into the game. */
363 pbp = bioq_takefirst(p->wqueue);
364 while (pbp != NULL) {
365 gv_post_bio(sc, pbp);
366 pbp = bioq_takefirst(p->wqueue);
375 /* XXX can this ever happen? */
377 completed = bp->bio_completed;
381 /* Check if we need to handle parity data. */
382 TAILQ_FOREACH_SAFE(bq, &wp->bits, queue, bq2) {
385 TAILQ_REMOVE(&wp->bits, bq, queue);
389 for (i = 0; i < wp->length; i++)
390 cbp->bio_data[i] ^= bp->bio_data[i];
395 /* Handle parity data. */
396 if (TAILQ_EMPTY(&wp->bits)) {
397 if (bp->bio_parent->bio_pflags & GV_BIO_CHECK)
398 i = gv_check_parity(p, bp, wp);
400 i = gv_normal_parity(p, bp, wp);
402 /* All of our sub-requests have finished. */
404 completed = wp->length;
405 TAILQ_REMOVE(&p->packets, wp, list);
406 /* Bring the waiting bios back into the game. */
407 pbp = bioq_takefirst(p->wqueue);
408 while (pbp != NULL) {
409 gv_post_bio(sc, pbp);
410 pbp = bioq_takefirst(p->wqueue);
419 pbp = bp->bio_parent;
420 if (pbp->bio_error == 0)
421 pbp->bio_error = bp->bio_error;
422 pbp->bio_completed += completed;
424 /* When the original request is finished, we deliver it. */
426 if (pbp->bio_inbed == pbp->bio_children) {
427 /* Hand it over for checking or delivery. */
428 if (pbp->bio_cmd == BIO_WRITE &&
429 (pbp->bio_pflags & GV_BIO_CHECK)) {
430 gv_parity_complete(p, pbp);
431 } else if (pbp->bio_cmd == BIO_WRITE &&
432 (pbp->bio_pflags & GV_BIO_REBUILD)) {
433 gv_rebuild_complete(p, pbp);
434 } else if (pbp->bio_pflags & GV_BIO_INIT) {
435 gv_init_complete(p, pbp);
436 } else if (pbp->bio_pflags & GV_BIO_SYNCREQ) {
437 gv_sync_complete(p, pbp);
438 } else if (pbp->bio_pflags & GV_BIO_GROW) {
439 gv_grow_complete(p, pbp);
441 g_io_deliver(pbp, pbp->bio_error);
445 /* Clean up what we allocated. */
446 if (bp->bio_cflags & GV_BIO_MALLOC)
447 g_free(bp->bio_data);
452 gv_check_parity(struct gv_plex *p, struct bio *bp, struct gv_raid5_packet *wp)
456 int err, finished, i;
461 if (wp->waiting != NULL) {
464 s = pbp->bio_caller1;
465 g_io_request(pbp, s->drive_sc->consumer);
468 } else if (wp->parity != NULL) {
472 /* Check if the parity is correct. */
473 for (i = 0; i < wp->length; i++) {
474 if (bp->bio_data[i] != pbp->bio_data[i]) {
480 /* The parity is not correct... */
482 bp->bio_parent->bio_error = EAGAIN;
484 /* ... but we rebuild it. */
485 if (bp->bio_parent->bio_pflags & GV_BIO_PARITY) {
486 s = pbp->bio_caller1;
487 g_io_request(pbp, s->drive_sc->consumer);
493 * Clean up the BIO we would have used for rebuilding the
497 bp->bio_parent->bio_inbed++;
507 gv_normal_parity(struct gv_plex *p, struct bio *bp, struct gv_raid5_packet *wp)
509 struct bio *cbp, *pbp;
515 if (wp->waiting != NULL) {
519 for (i = 0; i < wp->length; i++)
520 cbp->bio_data[i] ^= pbp->bio_data[i];
521 s = pbp->bio_caller1;
522 g_io_request(pbp, s->drive_sc->consumer);
525 } else if (wp->parity != NULL) {
528 s = cbp->bio_caller1;
529 g_io_request(cbp, s->drive_sc->consumer);
536 /* Flush the queue with delayed requests. */
538 gv_plex_flush(struct gv_plex *p)
544 bp = bioq_takefirst(p->rqueue);
546 gv_plex_start(p, bp);
547 bp = bioq_takefirst(p->rqueue);
552 gv_post_bio(struct gv_softc *sc, struct bio *bp)
555 KASSERT(sc != NULL, ("NULL sc"));
556 KASSERT(bp != NULL, ("NULL bp"));
557 mtx_lock(&sc->bqueue_mtx);
558 bioq_disksort(sc->bqueue_down, bp);
560 mtx_unlock(&sc->bqueue_mtx);
564 gv_sync_request(struct gv_plex *from, struct gv_plex *to, off_t offset,
565 off_t length, int type, caddr_t data)
570 KASSERT(from != NULL, ("NULL from"));
571 KASSERT(to != NULL, ("NULL to"));
572 sc = from->vinumconf;
573 KASSERT(sc != NULL, ("NULL sc"));
577 G_VINUM_DEBUG(0, "sync from '%s' failed at offset "
578 " %jd; out of memory", from->name, offset);
581 bp->bio_length = length;
582 bp->bio_done = gv_done;
583 bp->bio_pflags |= GV_BIO_SYNCREQ;
584 bp->bio_offset = offset;
585 bp->bio_caller1 = from;
586 bp->bio_caller2 = to;
589 data = g_malloc(length, M_WAITOK);
590 bp->bio_pflags |= GV_BIO_MALLOC; /* Free on the next run. */
593 /* Send down next. */
595 //gv_plex_start(from, bp);
600 * Handle a finished plex sync bio.
603 gv_sync_complete(struct gv_plex *to, struct bio *bp)
605 struct gv_plex *from, *p;
612 g_topology_assert_not();
615 KASSERT(to != NULL, ("NULL to"));
616 KASSERT(bp != NULL, ("NULL bp"));
617 from = bp->bio_caller2;
618 KASSERT(from != NULL, ("NULL from"));
620 KASSERT(v != NULL, ("NULL v"));
622 KASSERT(sc != NULL, ("NULL sc"));
624 /* If it was a read, write it. */
625 if (bp->bio_cmd == BIO_READ) {
626 err = gv_sync_request(from, to, bp->bio_offset, bp->bio_length,
627 BIO_WRITE, bp->bio_data);
628 /* If it was a write, read the next one. */
629 } else if (bp->bio_cmd == BIO_WRITE) {
630 if (bp->bio_pflags & GV_BIO_MALLOC)
631 g_free(bp->bio_data);
632 to->synced += bp->bio_length;
633 /* If we're finished, clean up. */
634 if (bp->bio_offset + bp->bio_length >= from->size) {
635 G_VINUM_DEBUG(1, "syncing of %s from %s completed",
636 to->name, from->name);
637 /* Update our state. */
638 LIST_FOREACH(s, &to->subdisks, in_plex)
639 gv_set_sd_state(s, GV_SD_UP, 0);
640 gv_update_plex_state(to);
641 to->flags &= ~GV_PLEX_SYNCING;
643 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
645 offset = bp->bio_offset + bp->bio_length;
646 err = gv_sync_request(from, to, offset,
647 MIN(bp->bio_length, from->size - offset),
652 /* Clean up if there was an error. */
654 to->flags &= ~GV_PLEX_SYNCING;
655 G_VINUM_DEBUG(0, "error syncing plexes: error code %d", err);
658 /* Check if all plexes are synced, and lower refcounts. */
660 LIST_FOREACH(p, &v->plexes, in_volume) {
661 if (p->flags & GV_PLEX_SYNCING) {
666 /* If we came here, all plexes are synced, and we're free. */
667 gv_access(v->provider, -1, -1, 0);
669 G_VINUM_DEBUG(1, "plex sync completed");
675 * Create a new bio struct for the next grow request.
678 gv_grow_request(struct gv_plex *p, off_t offset, off_t length, int type,
684 KASSERT(p != NULL, ("gv_grow_request: NULL p"));
686 KASSERT(sc != NULL, ("gv_grow_request: NULL sc"));
690 G_VINUM_DEBUG(0, "grow of %s failed creating bio: "
691 "out of memory", p->name);
696 bp->bio_done = gv_done;
699 bp->bio_offset = offset;
700 bp->bio_length = length;
701 bp->bio_pflags |= GV_BIO_GROW;
703 data = g_malloc(length, M_WAITOK);
704 bp->bio_pflags |= GV_BIO_MALLOC;
708 //gv_plex_start(p, bp);
713 * Finish handling of a bio to a growing plex.
716 gv_grow_complete(struct gv_plex *p, struct bio *bp)
721 off_t origsize, offset;
725 KASSERT(v != NULL, ("gv_grow_complete: NULL v"));
727 KASSERT(sc != NULL, ("gv_grow_complete: NULL sc"));
730 /* If it was a read, write it. */
731 if (bp->bio_cmd == BIO_READ) {
732 p->synced += bp->bio_length;
733 err = gv_grow_request(p, bp->bio_offset, bp->bio_length,
734 BIO_WRITE, bp->bio_data);
735 /* If it was a write, read next. */
736 } else if (bp->bio_cmd == BIO_WRITE) {
737 if (bp->bio_pflags & GV_BIO_MALLOC)
738 g_free(bp->bio_data);
740 /* Find the real size of the plex. */
741 sdcount = gv_sdcount(p, 1);
742 s = LIST_FIRST(&p->subdisks);
743 KASSERT(s != NULL, ("NULL s"));
744 origsize = (s->size * (sdcount - 1));
745 if (bp->bio_offset + bp->bio_length >= origsize) {
746 G_VINUM_DEBUG(1, "growing of %s completed", p->name);
747 p->flags &= ~GV_PLEX_GROWING;
748 LIST_FOREACH(s, &p->subdisks, in_plex) {
749 s->flags &= ~GV_SD_GROW;
750 gv_set_sd_state(s, GV_SD_UP, 0);
752 p->size = gv_plex_size(p);
753 gv_update_vol_size(v, gv_vol_size(v));
754 gv_set_plex_state(p, GV_PLEX_UP, 0);
756 gv_access(v->provider, -1, -1, 0);
759 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
760 /* Issue delayed requests. */
763 offset = bp->bio_offset + bp->bio_length;
764 err = gv_grow_request(p, offset,
765 MIN(bp->bio_length, origsize - offset),
772 p->flags &= ~GV_PLEX_GROWING;
773 G_VINUM_DEBUG(0, "error growing plex: error code %d", err);
779 * Create an initialization BIO and send it off to the consumer. Assume that
780 * we're given initialization data as parameter.
783 gv_init_request(struct gv_sd *s, off_t start, caddr_t data, off_t length)
786 struct g_consumer *cp;
787 struct bio *bp, *cbp;
789 KASSERT(s != NULL, ("gv_init_request: NULL s"));
791 KASSERT(d != NULL, ("gv_init_request: NULL d"));
793 KASSERT(cp != NULL, ("gv_init_request: NULL cp"));
797 G_VINUM_DEBUG(0, "subdisk '%s' init: write failed at offset %jd"
798 " (drive offset %jd); out of memory", s->name,
799 (intmax_t)s->initialized, (intmax_t)start);
800 return; /* XXX: Error codes. */
802 bp->bio_cmd = BIO_WRITE;
804 bp->bio_done = gv_done;
806 bp->bio_length = length;
807 bp->bio_pflags |= GV_BIO_INIT;
808 bp->bio_offset = start;
811 /* Then ofcourse, we have to clone it. */
812 cbp = g_clone_bio(bp);
814 G_VINUM_DEBUG(0, "subdisk '%s' init: write failed at offset %jd"
815 " (drive offset %jd); out of memory", s->name,
816 (intmax_t)s->initialized, (intmax_t)start);
817 return; /* XXX: Error codes. */
819 cbp->bio_done = gv_done;
820 cbp->bio_caller1 = s;
821 /* Send it off to the consumer. */
822 g_io_request(cbp, cp);
826 * Handle a finished initialization BIO.
829 gv_init_complete(struct gv_plex *p, struct bio *bp)
833 struct g_consumer *cp;
840 start = bp->bio_offset;
841 length = bp->bio_length;
842 error = bp->bio_error;
845 KASSERT(s != NULL, ("gv_init_complete: NULL s"));
847 KASSERT(d != NULL, ("gv_init_complete: NULL d"));
849 KASSERT(cp != NULL, ("gv_init_complete: NULL cp"));
851 KASSERT(sc != NULL, ("gv_init_complete: NULL sc"));
856 * First we need to find out if it was okay, and abort if it's not.
857 * Then we need to free previous buffers, find out the correct subdisk,
858 * as well as getting the correct starting point and length of the BIO.
860 if (start >= s->drive_offset + s->size) {
861 /* Free the data we initialized. */
864 g_topology_assert_not();
866 g_access(cp, 0, -1, 0);
869 gv_set_sd_state(s, GV_SD_STALE, GV_SETSTATE_FORCE |
872 gv_set_sd_state(s, GV_SD_UP, GV_SETSTATE_CONFIG);
874 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
875 G_VINUM_DEBUG(1, "subdisk '%s' init: finished "
876 "successfully", s->name);
880 s->initialized += length;
882 gv_init_request(s, start, data, length);
886 * Create a new bio struct for the next parity rebuild. Used both by internal
887 * rebuild of degraded plexes as well as user initiated rebuilds/checks.
890 gv_parity_request(struct gv_plex *p, int flags, off_t offset)
895 KASSERT(p != NULL, ("gv_parity_request: NULL p"));
897 KASSERT(sc != NULL, ("gv_parity_request: NULL sc"));
901 G_VINUM_DEBUG(0, "rebuild of %s failed creating bio: "
902 "out of memory", p->name);
906 bp->bio_cmd = BIO_WRITE;
907 bp->bio_done = gv_done;
909 bp->bio_length = p->stripesize;
913 * Check if it's a rebuild of a degraded plex or a user request of
916 if (flags & GV_BIO_REBUILD)
917 bp->bio_data = g_malloc(GV_DFLT_SYNCSIZE, M_WAITOK);
918 else if (flags & GV_BIO_CHECK)
919 bp->bio_data = g_malloc(p->stripesize, M_WAITOK | M_ZERO);
921 G_VINUM_DEBUG(0, "invalid flags given in rebuild");
925 bp->bio_pflags = flags;
926 bp->bio_pflags |= GV_BIO_MALLOC;
928 /* We still have more parity to build. */
929 bp->bio_offset = offset;
931 //gv_plex_start(p, bp); /* Send it down to the plex. */
935 * Handle a finished parity write.
938 gv_parity_complete(struct gv_plex *p, struct bio *bp)
943 error = bp->bio_error;
944 flags = bp->bio_pflags;
945 flags &= ~GV_BIO_MALLOC;
948 KASSERT(sc != NULL, ("gv_parity_complete: NULL sc"));
950 /* Clean up what we allocated. */
951 if (bp->bio_pflags & GV_BIO_MALLOC)
952 g_free(bp->bio_data);
955 if (error == EAGAIN) {
956 G_VINUM_DEBUG(0, "parity incorrect at offset 0x%jx",
957 (intmax_t)p->synced);
960 /* Any error is fatal, except EAGAIN when we're rebuilding. */
961 if (error && !(error == EAGAIN && (flags & GV_BIO_PARITY))) {
962 /* Make sure we don't have the lock. */
963 g_topology_assert_not();
965 gv_access(p->vol_sc->provider, -1, -1, 0);
967 G_VINUM_DEBUG(0, "parity check on %s failed at 0x%jx "
968 "errno %d", p->name, (intmax_t)p->synced, error);
971 p->synced += p->stripesize;
974 if (p->synced >= p->size) {
975 /* Make sure we don't have the lock. */
976 g_topology_assert_not();
978 gv_access(p->vol_sc->provider, -1, -1, 0);
980 /* We're finished. */
981 G_VINUM_DEBUG(1, "parity operation on %s finished", p->name);
983 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
987 /* Send down next. It will determine if we need to itself. */
988 gv_parity_request(p, flags, p->synced);
992 * Handle a finished plex rebuild bio.
995 gv_rebuild_complete(struct gv_plex *p, struct bio *bp)
1002 error = bp->bio_error;
1003 flags = bp->bio_pflags;
1004 offset = bp->bio_offset;
1005 flags &= ~GV_BIO_MALLOC;
1007 KASSERT(sc != NULL, ("gv_rebuild_complete: NULL sc"));
1009 /* Clean up what we allocated. */
1010 if (bp->bio_pflags & GV_BIO_MALLOC)
1011 g_free(bp->bio_data);
1015 g_topology_assert_not();
1017 gv_access(p->vol_sc->provider, -1, -1, 0);
1018 g_topology_unlock();
1020 G_VINUM_DEBUG(0, "rebuild of %s failed at offset %jd errno: %d",
1021 p->name, (intmax_t)offset, error);
1022 p->flags &= ~GV_PLEX_REBUILDING;
1024 gv_plex_flush(p); /* Flush out remaining rebuild BIOs. */
1028 offset += (p->stripesize * (gv_sdcount(p, 1) - 1));
1029 if (offset >= p->size) {
1030 /* We're finished. */
1031 g_topology_assert_not();
1033 gv_access(p->vol_sc->provider, -1, -1, 0);
1034 g_topology_unlock();
1036 G_VINUM_DEBUG(1, "rebuild of %s finished", p->name);
1037 gv_save_config(p->vinumconf);
1038 p->flags &= ~GV_PLEX_REBUILDING;
1040 /* Try to up all subdisks. */
1041 LIST_FOREACH(s, &p->subdisks, in_plex)
1042 gv_update_sd_state(s);
1043 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
1044 gv_plex_flush(p); /* Flush out remaining rebuild BIOs. */
1048 /* Send down next. It will determine if we need to itself. */
1049 gv_parity_request(p, flags, offset);