2004-06-12 21:16:10 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2004 Lukas Ertl
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/bio.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/kthread.h>
|
|
|
|
#include <sys/libkern.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
|
|
|
|
#include <geom/geom.h>
|
|
|
|
#include <geom/vinum/geom_vinum_var.h>
|
|
|
|
#include <geom/vinum/geom_vinum_raid5.h>
|
|
|
|
#include <geom/vinum/geom_vinum.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the stripe that the work packet wants is already being used by
|
|
|
|
* some other work packet.
|
|
|
|
*/
|
|
|
|
int
|
2004-09-18 13:44:43 +00:00
|
|
|
gv_stripe_active(struct gv_plex *p, struct bio *bp)
|
2004-06-12 21:16:10 +00:00
|
|
|
{
|
2004-09-18 13:44:43 +00:00
|
|
|
struct gv_raid5_packet *wp, *owp;
|
|
|
|
int overlap;
|
|
|
|
|
|
|
|
wp = bp->bio_driver1;
|
|
|
|
if (wp->lockbase == -1)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
overlap = 0;
|
|
|
|
TAILQ_FOREACH(owp, &p->packets, list) {
|
|
|
|
if (owp == wp)
|
|
|
|
break;
|
|
|
|
if ((wp->lockbase >= owp->lockbase) &&
|
|
|
|
(wp->lockbase <= owp->lockbase + owp->length)) {
|
|
|
|
overlap++;
|
|
|
|
break;
|
2004-06-12 21:16:10 +00:00
|
|
|
}
|
2004-09-18 13:44:43 +00:00
|
|
|
if ((wp->lockbase <= owp->lockbase) &&
|
|
|
|
(wp->lockbase + wp->length >= owp->lockbase)) {
|
|
|
|
overlap++;
|
|
|
|
break;
|
2004-06-12 21:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-18 13:44:43 +00:00
|
|
|
return (overlap);
|
2004-06-12 21:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Build a request group to perform (part of) a RAID5 request. */
|
|
|
|
int
|
2004-09-18 13:44:43 +00:00
|
|
|
gv_build_raid5_req(struct gv_plex *p, struct gv_raid5_packet *wp,
|
|
|
|
struct bio *bp, caddr_t addr, off_t boff, off_t bcount)
|
2004-06-12 21:16:10 +00:00
|
|
|
{
|
|
|
|
struct g_geom *gp;
|
|
|
|
struct gv_sd *broken, *original, *parity, *s;
|
2004-09-18 13:44:43 +00:00
|
|
|
struct gv_bioq *bq;
|
|
|
|
struct bio *cbp, *pbp;
|
|
|
|
int i, psdno, sdno, type;
|
|
|
|
off_t len_left, real_len, real_off, stripeend, stripeoff, stripestart;
|
2004-06-12 21:16:10 +00:00
|
|
|
|
|
|
|
gp = bp->bio_to->geom;
|
|
|
|
|
|
|
|
if (p == NULL || LIST_EMPTY(&p->subdisks))
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
/* We are optimistic and assume that this request will be OK. */
|
2004-09-18 13:44:43 +00:00
|
|
|
#define REQ_TYPE_NORMAL 0
|
|
|
|
#define REQ_TYPE_DEGRADED 1
|
|
|
|
#define REQ_TYPE_NOPARITY 2
|
|
|
|
|
|
|
|
type = REQ_TYPE_NORMAL;
|
2004-06-12 21:16:10 +00:00
|
|
|
original = parity = broken = NULL;
|
|
|
|
|
|
|
|
/* The number of the subdisk containing the parity stripe. */
|
|
|
|
psdno = p->sdcount - 1 - ( boff / (p->stripesize * (p->sdcount - 1))) %
|
|
|
|
p->sdcount;
|
|
|
|
KASSERT(psdno >= 0, ("gv_build_raid5_request: psdno < 0"));
|
|
|
|
|
|
|
|
/* Offset of the start address from the start of the stripe. */
|
|
|
|
stripeoff = boff % (p->stripesize * (p->sdcount - 1));
|
|
|
|
KASSERT(stripeoff >= 0, ("gv_build_raid5_request: stripeoff < 0"));
|
|
|
|
|
|
|
|
/* The number of the subdisk where the stripe resides. */
|
|
|
|
sdno = stripeoff / p->stripesize;
|
|
|
|
KASSERT(sdno >= 0, ("gv_build_raid5_request: sdno < 0"));
|
|
|
|
|
|
|
|
/* At or past parity subdisk. */
|
|
|
|
if (sdno >= psdno)
|
|
|
|
sdno++;
|
|
|
|
|
|
|
|
/* The offset of the stripe on this subdisk. */
|
|
|
|
stripestart = (boff - stripeoff) / (p->sdcount - 1);
|
|
|
|
KASSERT(stripestart >= 0, ("gv_build_raid5_request: stripestart < 0"));
|
|
|
|
|
2004-06-14 17:06:55 +00:00
|
|
|
stripeoff %= p->stripesize;
|
2004-06-12 21:16:10 +00:00
|
|
|
|
|
|
|
/* The offset of the request on this subdisk. */
|
|
|
|
real_off = stripestart + stripeoff;
|
|
|
|
|
|
|
|
stripeend = stripestart + p->stripesize;
|
|
|
|
len_left = stripeend - real_off;
|
|
|
|
KASSERT(len_left >= 0, ("gv_build_raid5_request: len_left < 0"));
|
|
|
|
|
|
|
|
/* Find the right subdisks. */
|
|
|
|
i = 0;
|
|
|
|
LIST_FOREACH(s, &p->subdisks, in_plex) {
|
|
|
|
if (i == sdno)
|
|
|
|
original = s;
|
|
|
|
if (i == psdno)
|
|
|
|
parity = s;
|
|
|
|
if (s->state != GV_SD_UP)
|
|
|
|
broken = s;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((original == NULL) || (parity == NULL))
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
/* Our data stripe is missing. */
|
|
|
|
if (original->state != GV_SD_UP)
|
2004-09-18 13:44:43 +00:00
|
|
|
type = REQ_TYPE_DEGRADED;
|
2004-06-12 21:16:10 +00:00
|
|
|
/* Our parity stripe is missing. */
|
|
|
|
if (parity->state != GV_SD_UP) {
|
|
|
|
/* We cannot take another failure if we're already degraded. */
|
2004-09-18 13:44:43 +00:00
|
|
|
if (type != REQ_TYPE_NORMAL)
|
2004-06-12 21:16:10 +00:00
|
|
|
return (ENXIO);
|
|
|
|
else
|
2004-09-18 13:44:43 +00:00
|
|
|
type = REQ_TYPE_NOPARITY;
|
2004-06-12 21:16:10 +00:00
|
|
|
}
|
|
|
|
|
2004-09-18 13:44:43 +00:00
|
|
|
real_len = (bcount <= len_left) ? bcount : len_left;
|
|
|
|
wp->length = real_len;
|
2004-06-12 21:16:10 +00:00
|
|
|
wp->data = addr;
|
2004-09-18 13:44:43 +00:00
|
|
|
wp->lockbase = real_off;
|
2004-06-12 21:16:10 +00:00
|
|
|
|
|
|
|
KASSERT(wp->length >= 0, ("gv_build_raid5_request: wp->length < 0"));
|
|
|
|
|
|
|
|
switch (bp->bio_cmd) {
|
|
|
|
case BIO_READ:
|
|
|
|
/*
|
|
|
|
* For a degraded read we need to read in all stripes except
|
|
|
|
* the broken one plus the parity stripe and then recalculate
|
|
|
|
* the desired data.
|
|
|
|
*/
|
2004-09-18 13:44:43 +00:00
|
|
|
if (type == REQ_TYPE_DEGRADED) {
|
|
|
|
bzero(wp->data, wp->length);
|
2004-06-12 21:16:10 +00:00
|
|
|
LIST_FOREACH(s, &p->subdisks, in_plex) {
|
|
|
|
/* Skip the broken subdisk. */
|
|
|
|
if (s == broken)
|
|
|
|
continue;
|
2004-09-18 13:44:43 +00:00
|
|
|
cbp = g_clone_bio(bp);
|
|
|
|
if (cbp == NULL)
|
2004-06-22 14:54:31 +00:00
|
|
|
return (ENOMEM);
|
2004-09-18 13:44:43 +00:00
|
|
|
cbp->bio_data = g_malloc(real_len, M_WAITOK);
|
|
|
|
cbp->bio_cflags |= GV_BIO_MALLOC;
|
|
|
|
cbp->bio_offset = real_off;
|
|
|
|
cbp->bio_length = real_len;
|
|
|
|
cbp->bio_done = gv_plex_done;
|
|
|
|
cbp->bio_caller2 = s->consumer;
|
|
|
|
cbp->bio_driver1 = wp;
|
|
|
|
|
|
|
|
GV_ENQUEUE(bp, cbp, pbp);
|
|
|
|
|
|
|
|
bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
|
|
|
|
bq->bp = cbp;
|
|
|
|
TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
|
2004-06-12 21:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* A normal read can be fulfilled with the original subdisk. */
|
|
|
|
} else {
|
2004-09-18 13:44:43 +00:00
|
|
|
cbp = g_clone_bio(bp);
|
|
|
|
if (cbp == NULL)
|
2004-06-12 21:16:10 +00:00
|
|
|
return (ENOMEM);
|
2004-09-18 13:44:43 +00:00
|
|
|
cbp->bio_offset = real_off;
|
|
|
|
cbp->bio_length = real_len;
|
|
|
|
cbp->bio_data = addr;
|
|
|
|
cbp->bio_done = g_std_done;
|
|
|
|
cbp->bio_caller2 = original->consumer;
|
|
|
|
|
|
|
|
GV_ENQUEUE(bp, cbp, pbp);
|
2004-06-12 21:16:10 +00:00
|
|
|
}
|
2004-09-18 13:44:43 +00:00
|
|
|
wp->lockbase = -1;
|
|
|
|
|
2004-06-12 21:16:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_WRITE:
|
|
|
|
/*
|
|
|
|
* A degraded write means we cannot write to the original data
|
|
|
|
* subdisk. Thus we need to read in all valid stripes,
|
|
|
|
* recalculate the parity from the original data, and then
|
|
|
|
* write the parity stripe back out.
|
|
|
|
*/
|
2004-09-18 13:44:43 +00:00
|
|
|
if (type == REQ_TYPE_DEGRADED) {
|
|
|
|
/* Read all subdisks. */
|
2004-06-12 21:16:10 +00:00
|
|
|
LIST_FOREACH(s, &p->subdisks, in_plex) {
|
|
|
|
/* Skip the broken and the parity subdisk. */
|
2004-09-18 13:44:43 +00:00
|
|
|
if ((s == broken) || (s == parity))
|
2004-06-12 21:16:10 +00:00
|
|
|
continue;
|
|
|
|
|
2004-09-18 13:44:43 +00:00
|
|
|
cbp = g_clone_bio(bp);
|
|
|
|
if (cbp == NULL)
|
2004-06-22 14:54:31 +00:00
|
|
|
return (ENOMEM);
|
2004-09-18 13:44:43 +00:00
|
|
|
cbp->bio_cmd = BIO_READ;
|
|
|
|
cbp->bio_data = g_malloc(real_len, M_WAITOK);
|
|
|
|
cbp->bio_cflags |= GV_BIO_MALLOC;
|
|
|
|
cbp->bio_offset = real_off;
|
|
|
|
cbp->bio_length = real_len;
|
|
|
|
cbp->bio_done = gv_plex_done;
|
|
|
|
cbp->bio_caller2 = s->consumer;
|
|
|
|
cbp->bio_driver1 = wp;
|
|
|
|
|
|
|
|
GV_ENQUEUE(bp, cbp, pbp);
|
|
|
|
|
|
|
|
bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
|
|
|
|
bq->bp = cbp;
|
|
|
|
TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
|
2004-06-12 21:16:10 +00:00
|
|
|
}
|
|
|
|
|
2004-09-18 13:44:43 +00:00
|
|
|
/* Write the parity data. */
|
|
|
|
cbp = g_clone_bio(bp);
|
|
|
|
if (cbp == NULL)
|
2004-06-12 21:16:10 +00:00
|
|
|
return (ENOMEM);
|
2004-09-18 13:44:43 +00:00
|
|
|
cbp->bio_data = g_malloc(real_len, M_WAITOK);
|
|
|
|
cbp->bio_cflags |= GV_BIO_MALLOC;
|
|
|
|
bcopy(addr, cbp->bio_data, real_len);
|
|
|
|
cbp->bio_offset = real_off;
|
|
|
|
cbp->bio_length = real_len;
|
|
|
|
cbp->bio_done = gv_plex_done;
|
|
|
|
cbp->bio_caller2 = parity->consumer;
|
|
|
|
cbp->bio_driver1 = wp;
|
|
|
|
wp->parity = cbp;
|
2004-06-12 21:16:10 +00:00
|
|
|
|
|
|
|
/*
|
2004-09-18 13:44:43 +00:00
|
|
|
* When the parity stripe is missing we just write out the data.
|
2004-06-12 21:16:10 +00:00
|
|
|
*/
|
2004-09-18 13:44:43 +00:00
|
|
|
} else if (type == REQ_TYPE_NOPARITY) {
|
|
|
|
cbp = g_clone_bio(bp);
|
|
|
|
if (cbp == NULL)
|
2004-06-22 14:54:31 +00:00
|
|
|
return (ENOMEM);
|
2004-09-18 13:44:43 +00:00
|
|
|
cbp->bio_offset = real_off;
|
|
|
|
cbp->bio_length = real_len;
|
|
|
|
cbp->bio_data = addr;
|
|
|
|
cbp->bio_done = gv_plex_done;
|
|
|
|
cbp->bio_caller2 = original->consumer;
|
|
|
|
cbp->bio_driver1 = wp;
|
2004-06-12 21:16:10 +00:00
|
|
|
|
2004-09-18 13:44:43 +00:00
|
|
|
GV_ENQUEUE(bp, cbp, pbp);
|
2004-06-12 21:16:10 +00:00
|
|
|
|
2004-09-18 13:44:43 +00:00
|
|
|
bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
|
|
|
|
bq->bp = cbp;
|
|
|
|
TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
|
2004-06-12 21:16:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A normal write request goes to the original subdisk, then we
|
|
|
|
* read in all other stripes, recalculate the parity and write
|
|
|
|
* out the parity again.
|
|
|
|
*/
|
|
|
|
} else {
|
2004-09-18 13:44:43 +00:00
|
|
|
/* Read old parity. */
|
|
|
|
cbp = g_clone_bio(bp);
|
|
|
|
if (cbp == NULL)
|
2004-06-22 14:54:31 +00:00
|
|
|
return (ENOMEM);
|
2004-09-18 13:44:43 +00:00
|
|
|
cbp->bio_cmd = BIO_READ;
|
|
|
|
cbp->bio_data = g_malloc(real_len, M_WAITOK);
|
|
|
|
cbp->bio_cflags |= GV_BIO_MALLOC;
|
|
|
|
cbp->bio_offset = real_off;
|
|
|
|
cbp->bio_length = real_len;
|
|
|
|
cbp->bio_done = gv_plex_done;
|
|
|
|
cbp->bio_caller2 = parity->consumer;
|
|
|
|
cbp->bio_driver1 = wp;
|
|
|
|
|
|
|
|
GV_ENQUEUE(bp, cbp, pbp);
|
|
|
|
|
|
|
|
bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
|
|
|
|
bq->bp = cbp;
|
|
|
|
TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
|
|
|
|
|
|
|
|
/* Read old data. */
|
|
|
|
cbp = g_clone_bio(bp);
|
|
|
|
if (cbp == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
cbp->bio_cmd = BIO_READ;
|
|
|
|
cbp->bio_data = g_malloc(real_len, M_WAITOK);
|
|
|
|
cbp->bio_cflags |= GV_BIO_MALLOC;
|
|
|
|
cbp->bio_offset = real_off;
|
|
|
|
cbp->bio_length = real_len;
|
|
|
|
cbp->bio_done = gv_plex_done;
|
|
|
|
cbp->bio_caller2 = original->consumer;
|
|
|
|
cbp->bio_driver1 = wp;
|
|
|
|
|
|
|
|
GV_ENQUEUE(bp, cbp, pbp);
|
|
|
|
|
|
|
|
bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
|
|
|
|
bq->bp = cbp;
|
|
|
|
TAILQ_INSERT_TAIL(&wp->bits, bq, queue);
|
|
|
|
|
|
|
|
/* Write new data. */
|
|
|
|
cbp = g_clone_bio(bp);
|
|
|
|
if (cbp == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
cbp->bio_data = addr;
|
|
|
|
cbp->bio_offset = real_off;
|
|
|
|
cbp->bio_length = real_len;
|
|
|
|
cbp->bio_done = gv_plex_done;
|
|
|
|
cbp->bio_caller2 = original->consumer;
|
2004-06-12 21:16:10 +00:00
|
|
|
|
2004-09-18 13:44:43 +00:00
|
|
|
cbp->bio_driver1 = wp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We must not write the new data until the old data
|
|
|
|
* was read, so hold this BIO back until we're ready
|
|
|
|
* for it.
|
|
|
|
*/
|
|
|
|
wp->waiting = cbp;
|
|
|
|
|
|
|
|
/* The final bio for the parity. */
|
|
|
|
cbp = g_clone_bio(bp);
|
|
|
|
if (cbp == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
cbp->bio_data = g_malloc(real_len, M_WAITOK | M_ZERO);
|
|
|
|
cbp->bio_cflags |= GV_BIO_MALLOC;
|
|
|
|
cbp->bio_offset = real_off;
|
|
|
|
cbp->bio_length = real_len;
|
|
|
|
cbp->bio_done = gv_plex_done;
|
|
|
|
cbp->bio_caller2 = parity->consumer;
|
|
|
|
cbp->bio_driver1 = wp;
|
|
|
|
|
|
|
|
/* Remember that this is the BIO for the parity data. */
|
|
|
|
wp->parity = cbp;
|
2004-06-12 21:16:10 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-09-18 13:44:43 +00:00
|
|
|
|
2004-06-12 21:16:10 +00:00
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|