2 * Copyright (c) 1997, 1998, 1999
3 * Nan Yang Computer Services Limited. All rights reserved.
5 * Parts copyright (c) 1997, 1998 Cybernet Corporation, NetMAX project.
7 * Written by Greg Lehey
9 * This software is distributed under the so-called ``Berkeley
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Nan Yang Computer
24 * 4. Neither the name of the Company nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * This software is provided ``as is'', and any express or implied
29 * warranties, including, but not limited to, the implied warranties of
30 * merchantability and fitness for a particular purpose are disclaimed.
31 * In no event shall the company or contributors be liable for any
32 * direct, indirect, incidental, special, exemplary, or consequential
33 * damages (including, but not limited to, procurement of substitute
34 * goods or services; loss of use, data, or profits; or business
35 * interruption) however caused and on any theory of liability, whether
36 * in contract, strict liability, or tort (including negligence or
37 * otherwise) arising in any way out of the use of this software, even if
38 * advised of the possibility of such damage.
40 * $Id: vinumrequest.c,v 1.30 2001/01/09 04:20:55 grog Exp grog $
41 * $FreeBSD: src/sys/dev/vinum/vinumrequest.c,v 1.44.2.5 2002/08/28 04:30:56 grog Exp $
46 #include <sys/resourcevar.h>
48 enum requeststatus bre(struct request *rq,
50 vinum_off_t * diskstart,
52 enum requeststatus bre5(struct request *rq,
54 vinum_off_t * diskstart,
56 enum requeststatus build_read_request(struct request *rq, int volplexno);
57 enum requeststatus build_write_request(struct request *rq);
58 enum requeststatus build_rq_buffer(struct rqelement *rqe, struct plex *plex);
59 int find_alternate_sd(struct request *rq);
60 int check_range_covered(struct request *);
61 void complete_rqe(struct bio *bio);
62 void complete_raid5_write(struct rqelement *);
63 int abortrequest(struct request *rq, int error);
64 void sdio_done(struct bio *bio);
65 struct bio *vinum_bounds_check(struct bio *bio, struct volume *vol);
66 caddr_t allocdatabuf(struct rqelement *rqe);
67 void freedatabuf(struct rqelement *rqe);
70 struct rqinfo rqinfo[RQINFO_SIZE];
71 struct rqinfo *rqip = rqinfo;
74 logrq(enum rqinfo_type type, union rqinfou info, struct bio *ubio)
80 microtime(&rqip->timestamp); /* when did this happen? */
82 rqip->bio = ubio; /* user buffer */
86 case loginfo_user_bpl:
87 case loginfo_sdio: /* subdisk I/O */
88 case loginfo_sdiol: /* subdisk I/O launch */
89 case loginfo_sdiodone: /* subdisk I/O complete */
90 bcopy(info.bio, &rqip->info.bio, sizeof(struct bio));
91 dev = info.bio->bio_driver_info;
92 rqip->devmajor = major(dev);
93 rqip->devminor = minor(dev);
98 case loginfo_raid5_data:
99 case loginfo_raid5_parity:
100 bcopy(info.rqe, &rqip->info.rqe, sizeof(struct rqelement));
101 dev = info.rqe->b.b_bio1.bio_driver_info;
102 rqip->devmajor = major(dev);
103 rqip->devminor = minor(dev);
106 case loginfo_lockwait:
109 bcopy(info.lockinfo, &rqip->info.lockinfo, sizeof(struct rangelock));
117 if (rqip >= &rqinfo[RQINFO_SIZE]) /* wrap around */
125 vinumstrategy(struct dev_strategy_args *ap)
127 cdev_t dev = ap->a_head.a_dev;
128 struct bio *bio = ap->a_bio;
129 struct buf *bp = bio->bio_buf;
130 struct bio *nbio = bio;
131 struct volume *vol = NULL;
134 switch (DEVTYPE(dev)) {
136 case VINUM_RAWSD_TYPE:
137 bio->bio_driver_info = dev;
140 case VINUM_DRIVE_TYPE:
143 * In fact, vinum doesn't handle drives: they're
144 * handled directly by the disk drivers
146 bp->b_error = EIO; /* I/O error */
147 bp->b_flags |= B_ERROR;
151 case VINUM_VOLUME_TYPE: /* volume I/O */
154 if (vol->state != volume_up) { /* can't access this volume */
155 bp->b_error = EIO; /* I/O error */
156 bp->b_flags |= B_ERROR;
160 nbio = vinum_bounds_check(bio, vol);
166 case VINUM_PLEX_TYPE:
167 case VINUM_RAWPLEX_TYPE:
169 * Plex I/O is pretty much the same as volume I/O
170 * for a single plex. Indicate this by passing a NULL
171 * pointer (set above) for the volume
173 bp->b_resid = bp->b_bcount; /* transfer everything */
174 vinumstart(dev, nbio, 0);
181 * Start a transfer. Return -1 on error,
182 * 0 if OK, 1 if we need to retry.
183 * Parameter reviveok is set when doing
184 * transfers for revives: it allows transfers to
185 * be started immediately when a revive is in
186 * progress. During revive, normal transfers
187 * are queued if they share address space with
188 * a currently active revive operation.
191 vinumstart(cdev_t dev, struct bio *bio, int reviveok)
193 struct buf *bp = bio->bio_buf;
196 struct request *rq; /* build up our request here */
197 enum requeststatus status;
199 bio->bio_driver_info = dev;
202 if (debug & DEBUG_LASTREQS)
203 logrq(loginfo_user_bp, (union rqinfou) bio, bio);
206 if ((bp->b_bcount % DEV_BSIZE) != 0) { /* bad length */
207 bp->b_error = EINVAL; /* invalid size */
208 bp->b_flags |= B_ERROR;
212 rq = (struct request *) Malloc(sizeof(struct request)); /* allocate a request struct */
213 if (rq == NULL) { /* can't do it */
214 bp->b_error = ENOMEM; /* can't get memory */
215 bp->b_flags |= B_ERROR;
219 bzero(rq, sizeof(struct request));
222 * Note the volume ID. This can be NULL, which
223 * the request building functions use as an
224 * indication for single plex I/O
226 rq->bio = bio; /* and the user buffer struct */
228 if (DEVTYPE(dev) == VINUM_VOLUME_TYPE) { /* it's a volume, */
229 rq->volplex.volno = Volno(dev); /* get the volume number */
230 vol = &VOL[rq->volplex.volno]; /* and point to it */
231 vol->active++; /* one more active request */
233 vol = NULL; /* no volume */
234 rq->volplex.plexno = Plexno(dev); /* point to the plex */
235 rq->isplex = 1; /* note that it's a plex */
238 if (bp->b_cmd == BUF_CMD_READ) {
240 * This is a read request. Decide
241 * which plex to read from.
243 * There's a potential race condition here,
244 * since we're not locked, and we could end
245 * up multiply incrementing the round-robin
246 * counter. This doesn't have any serious
250 plexno = vol->preferred_plex; /* get the plex to use */
251 if (plexno < 0) { /* round robin */
252 plexno = vol->last_plex_read;
253 vol->last_plex_read++;
254 if (vol->last_plex_read >= vol->plexes) /* got the the end? */
255 vol->last_plex_read = 0; /* wrap around */
257 status = build_read_request(rq, plexno); /* build a request */
259 vinum_off_t diskaddr = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT);
260 /* start offset of transfer */
261 status = bre(rq, /* build a request list */
264 diskaddr + (bp->b_bcount / DEV_BSIZE));
267 if (status > REQUEST_RECOVERED) { /* can't satisfy it */
268 if (status == REQUEST_DOWN) { /* not enough subdisks */
269 bp->b_error = EIO; /* I/O error */
270 bp->b_flags |= B_ERROR;
276 return launch_requests(rq, reviveok); /* now start the requests if we can */
279 * This is a write operation. We write to all plexes. If this is
280 * a RAID-4 or RAID-5 plex, we must also update the parity stripe.
284 status = build_write_request(rq); /* Not all the subdisks are up */
285 else { /* plex I/O */
286 vinum_off_t diskstart;
289 diskstart = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT); /* start offset of transfer */
290 diskend = diskstart + bp->b_bcount / DEV_BSIZE;
291 status = bre(rq, Plexno(dev),
292 &diskstart, diskend); /* build requests for the plex */
294 if (status > REQUEST_RECOVERED) { /* can't satisfy it */
295 if (status == REQUEST_DOWN) { /* not enough subdisks */
296 bp->b_error = EIO; /* I/O error */
297 bp->b_flags |= B_ERROR;
303 return launch_requests(rq, reviveok); /* now start the requests if we can */
308 * Call the low-level strategy routines to
309 * perform the requests in a struct request
312 launch_requests(struct request *rq, int reviveok)
315 int rqno; /* loop index */
316 struct rqelement *rqe; /* current element */
318 int rcount; /* request count */
321 * First find out whether we're reviving, and the
322 * request contains a conflict. If so, we hang
323 * the request off plex->waitlist of the first
324 * plex we find which is reviving
327 if ((rq->flags & XFR_REVIVECONFLICT) /* possible revive conflict */
328 &&(!reviveok)) { /* and we don't want to do it now, */
330 struct request *waitlist; /* point to the waitlist */
333 if (sd->waitlist != NULL) { /* something there already, */
334 waitlist = sd->waitlist;
335 while (waitlist->next != NULL) /* find the end */
336 waitlist = waitlist->next;
337 waitlist->next = rq; /* hook our request there */
339 sd->waitlist = rq; /* hook our request at the front */
342 if (debug & DEBUG_REVIVECONFLICT) {
344 "Revive conflict sd %d: %p\n%s dev %d.%d, offset 0x%jx, length %d\n",
347 (rq->bio->bio_buf->b_cmd & BUF_CMD_READ) ? "Read" : "Write",
348 major(((cdev_t)rq->bio->bio_driver_info)),
349 minor(((cdev_t)rq->bio->bio_driver_info)),
350 (uintmax_t)rq->bio->bio_offset,
351 rq->bio->bio_buf->b_bcount);
354 return 0; /* and get out of here */
356 rq->active = 0; /* nothing yet */
358 if (debug & DEBUG_ADDRESSES)
360 "Request: %p\n%s dev %d.%d, offset 0x%jx, length %d\n",
362 (rq->bio->bio_buf->b_cmd == BUF_CMD_READ) ? "Read" : "Write",
363 major(((cdev_t)rq->bio->bio_driver_info)),
364 minor(((cdev_t)rq->bio->bio_driver_info)),
365 (uintmax_t)rq->bio->bio_offset,
366 rq->bio->bio_buf->b_bcount);
367 vinum_conf.lastrq = rq;
368 vinum_conf.lastbio = rq->bio;
369 if (debug & DEBUG_LASTREQS)
370 logrq(loginfo_user_bpl, (union rqinfou) rq->bio, rq->bio);
374 * This loop happens without any participation
375 * of the bottom half, so it requires no
378 for (rqg = rq->rqg; rqg != NULL; rqg = rqg->next) { /* through the whole request chain */
379 rqg->active = rqg->count; /* they're all active */
380 for (rqno = 0; rqno < rqg->count; rqno++) {
381 rqe = &rqg->rqe[rqno];
382 if (rqe->flags & XFR_BAD_SUBDISK) /* this subdisk is bad, */
383 rqg->active--; /* one less active request */
385 if (rqg->active) /* we have at least one active request, */
386 rq->active++; /* one more active request group */
390 * Now fire off the requests. In this loop the
391 * bottom half could be completing requests
392 * before we finish, so we need critical section protection.
395 for (rqg = rq->rqg; rqg != NULL;) { /* through the whole request chain */
396 if (rqg->lockbase >= 0) /* this rqg needs a lock first */
397 rqg->lock = lockrange(rqg->lockbase, rqg->rq->bio->bio_buf, &PLEX[rqg->plexno]);
399 for (rqno = 0; rqno < rcount;) {
402 rqe = &rqg->rqe[rqno];
405 * Point to next rqg before the bottom end
406 * changes the structures.
408 if (++rqno >= rcount)
410 if ((rqe->flags & XFR_BAD_SUBDISK) == 0) { /* this subdisk is good, */
411 drive = &DRIVE[rqe->driveno]; /* look at drive */
413 if (drive->active >= drive->maxactive)
414 drive->maxactive = drive->active;
416 if (vinum_conf.active >= vinum_conf.maxactive)
417 vinum_conf.maxactive = vinum_conf.active;
419 dev = rqe->b.b_bio1.bio_driver_info;
421 if (debug & DEBUG_ADDRESSES)
423 " %s dev %d.%d, sd %d, offset 0x%jx, devoffset 0x%jx, length %d\n",
424 (rqe->b.b_cmd == BUF_CMD_READ) ? "Read" : "Write",
428 (uintmax_t)(rqe->b.b_bio1.bio_offset - ((off_t)SD[rqe->sdno].driveoffset << DEV_BSHIFT)),
429 (uintmax_t)rqe->b.b_bio1.bio_offset,
431 if (debug & DEBUG_LASTREQS)
432 logrq(loginfo_rqe, (union rqinfou) rqe, rq->bio);
434 /* fire off the request */
435 /* XXX this had better not be a low level drive */
436 dev_dstrategy(dev, &rqe->b.b_bio1);
445 * define the low-level requests needed to perform a
446 * high-level I/O operation for a specific plex 'plexno'.
448 * Return REQUEST_OK if all subdisks involved in the request are up,
449 * REQUEST_DOWN if some subdisks are not up, and REQUEST_EOF if the
450 * request is at least partially outside the bounds of the subdisks.
452 * Modify the pointer *diskstart to point to the end address. On
453 * read, return on the first bad subdisk, so that the caller
454 * (build_read_request) can try alternatives.
456 * On entry to this routine, the rqg structures are not assigned. The
457 * assignment is performed by expandrq(). Strictly speaking, the
458 * elements rqe->sdno of all entries should be set to -1, since 0
459 * (from bzero) is a valid subdisk number. We avoid this problem by
460 * initializing the ones we use, and not looking at the others (index
464 bre(struct request *rq,
466 vinum_off_t * diskaddr,
473 struct buf *bp; /* user's bp */
475 enum requeststatus status; /* return value */
476 vinum_off_t plexoffset; /* offset of transfer in plex */
477 vinum_off_t stripebase; /* base address of stripe (1st subdisk) */
478 vinum_off_t stripeoffset; /* offset in stripe */
479 vinum_off_t blockoffset; /* offset in stripe on subdisk */
480 struct rqelement *rqe; /* point to this request information */
481 vinum_off_t diskstart = *diskaddr; /* remember where this transfer starts */
482 enum requeststatus s; /* temp return value */
484 bio = rq->bio; /* buffer pointer */
486 status = REQUEST_OK; /* return value: OK until proven otherwise */
487 plex = &PLEX[plexno]; /* point to the plex */
489 switch (plex->organization) {
491 sd = NULL; /* (keep compiler quiet) */
492 for (sdno = 0; sdno < plex->subdisks; sdno++) {
493 sd = &SD[plex->sdnos[sdno]];
494 if (*diskaddr < sd->plexoffset) /* we must have a hole, */
495 status = REQUEST_DEGRADED; /* note the fact */
496 if (*diskaddr < (sd->plexoffset + sd->sectors)) { /* the request starts in this subdisk */
497 rqg = allocrqg(rq, 1); /* space for the request */
498 if (rqg == NULL) { /* malloc failed */
499 bp->b_error = ENOMEM;
500 bp->b_flags |= B_ERROR;
501 return REQUEST_ENOMEM;
503 rqg->plexno = plexno;
505 rqe = &rqg->rqe[0]; /* point to the element */
506 rqe->rqg = rqg; /* group */
507 rqe->sdno = sd->sdno; /* put in the subdisk number */
508 plexoffset = *diskaddr; /* start offset in plex */
509 rqe->sdoffset = plexoffset - sd->plexoffset; /* start offset in subdisk */
510 rqe->useroffset = plexoffset - diskstart; /* start offset in user buffer */
512 rqe->datalen = u64min(diskend - *diskaddr,
513 sd->sectors - rqe->sdoffset);
514 rqe->groupoffset = 0; /* no groups for concatenated plexes */
516 rqe->buflen = rqe->datalen; /* buffer length is data buffer length */
518 rqe->driveno = sd->driveno;
519 if (sd->state != sd_up) { /* *now* we find the sd is down */
520 s = checksdstate(sd, rq, *diskaddr, diskend); /* do we need to change state? */
521 if (s == REQUEST_DOWN) { /* down? */
522 rqe->flags = XFR_BAD_SUBDISK; /* yup */
523 if (rq->bio->bio_buf->b_cmd == BUF_CMD_READ) /* read request, */
524 return REQUEST_DEGRADED; /* give up here */
526 * If we're writing, don't give up
527 * because of a bad subdisk. Go
528 * through to the bitter end, but note
529 * which ones we can't access.
531 status = REQUEST_DEGRADED; /* can't do it all */
534 *diskaddr += rqe->datalen; /* bump the address */
535 if (build_rq_buffer(rqe, plex)) { /* build the buffer */
537 bp->b_error = ENOMEM;
538 bp->b_flags |= B_ERROR;
539 return REQUEST_ENOMEM; /* can't do it */
542 if (*diskaddr == diskend) /* we're finished, */
543 break; /* get out of here */
546 * We've got to the end of the plex. Have we got to the end of
547 * the transfer? It would seem that having an offset beyond the
548 * end of the subdisk is an error, but in fact it can happen if
549 * the volume has another plex of different size. There's a valid
550 * question as to why you would want to do this, but currently
553 * In a previous version, I returned REQUEST_DOWN here. I think
554 * REQUEST_EOF is more appropriate now.
556 if (diskend > sd->sectors + sd->plexoffset) /* pointing beyond EOF? */
557 status = REQUEST_EOF;
562 while (*diskaddr < diskend) { /* until we get it all sorted out */
563 if (*diskaddr >= plex->length) /* beyond the end of the plex */
564 return REQUEST_EOF; /* can't continue */
566 /* The offset of the start address from the start of the stripe. */
567 stripeoffset = *diskaddr % (plex->stripesize * plex->subdisks);
569 /* The plex-relative address of the start of the stripe. */
570 stripebase = *diskaddr - stripeoffset;
572 /* The number of the subdisk in which the start is located. */
573 sdno = stripeoffset / plex->stripesize;
575 /* The offset from the beginning of the stripe on this subdisk. */
576 blockoffset = stripeoffset % plex->stripesize;
578 sd = &SD[plex->sdnos[sdno]]; /* the subdisk in question */
579 rqg = allocrqg(rq, 1); /* space for the request */
580 if (rqg == NULL) { /* malloc failed */
581 bp->b_error = ENOMEM;
582 bp->b_flags |= B_ERROR;
583 return REQUEST_ENOMEM;
585 rqg->plexno = plexno;
587 rqe = &rqg->rqe[0]; /* point to the element */
589 rqe->sdoffset = stripebase / plex->subdisks + blockoffset; /* start offset in this subdisk */
590 rqe->useroffset = *diskaddr - diskstart; /* The offset of the start in the user buffer */
592 rqe->datalen = u64min(diskend - *diskaddr,
593 plex->stripesize - blockoffset);
594 rqe->groupoffset = 0; /* no groups for striped plexes */
596 rqe->buflen = rqe->datalen; /* buffer length is data buffer length */
598 rqe->sdno = sd->sdno; /* put in the subdisk number */
599 rqe->driveno = sd->driveno;
601 if (sd->state != sd_up) { /* *now* we find the sd is down */
602 s = checksdstate(sd, rq, *diskaddr, diskend); /* do we need to change state? */
603 if (s == REQUEST_DOWN) { /* down? */
604 rqe->flags = XFR_BAD_SUBDISK; /* yup */
605 if (rq->bio->bio_buf->b_cmd == BUF_CMD_READ) /* read request, */
606 return REQUEST_DEGRADED; /* give up here */
608 * If we're writing, don't give up
609 * because of a bad subdisk. Go through
610 * to the bitter end, but note which
611 * ones we can't access.
613 status = REQUEST_DEGRADED; /* can't do it all */
617 * It would seem that having an offset
618 * beyond the end of the subdisk is an
619 * error, but in fact it can happen if the
620 * volume has another plex of different
621 * size. There's a valid question as to why
622 * you would want to do this, but currently
625 if (rqe->sdoffset + rqe->datalen > sd->sectors) { /* ends beyond the end of the subdisk? */
626 rqe->datalen = sd->sectors - rqe->sdoffset; /* truncate */
628 if (debug & DEBUG_EOFINFO) { /* tell on the request */
630 "vinum: EOF on plex %s, sd %s offset %jx (user offset %jx)\n",
633 (uintmax_t)sd->sectors,
634 (uintmax_t)bp->b_bio1.bio_offset);
636 "vinum: stripebase 0x%llx, stripeoffset 0x%llx, "
637 "blockoffset 0x%llx\n",
638 (long long)stripebase,
639 (long long)stripeoffset,
640 (long long)blockoffset);
644 if (build_rq_buffer(rqe, plex)) { /* build the buffer */
646 bp->b_error = ENOMEM;
647 bp->b_flags |= B_ERROR;
648 return REQUEST_ENOMEM; /* can't do it */
650 *diskaddr += rqe->datalen; /* look at the remainder */
651 if ((*diskaddr < diskend) /* didn't finish the request on this stripe */
652 &&(*diskaddr < plex->length)) { /* and there's more to come */
653 plex->multiblock++; /* count another one */
654 if (sdno == plex->subdisks - 1) /* last subdisk, */
655 plex->multistripe++; /* another stripe as well */
662 * RAID-4 and RAID-5 are complicated enough to have their own
667 status = bre5(rq, plexno, diskaddr, diskend);
671 log(LOG_ERR, "vinum: invalid plex type %d in bre\n", plex->organization);
672 status = REQUEST_DOWN; /* can't access it */
679 * Build up a request structure for reading volumes.
680 * This function is not needed for plex reads, since there's
681 * no recovery if a plex read can't be satisified.
684 build_read_request(struct request *rq, /* request */
686 { /* index in the volume's plex table */
689 vinum_off_t startaddr; /* offset of previous part of transfer */
690 vinum_off_t diskaddr; /* offset of current part of transfer */
691 vinum_off_t diskend; /* and end offset of transfer */
692 int plexno; /* plex index in vinum_conf */
693 struct volume *vol; /* volume in question */
694 int recovered = 0; /* set if we recover a read */
695 enum requeststatus status = REQUEST_OK;
696 int plexmask; /* bit mask of plexes, for recovery */
698 bio = rq->bio; /* buffer pointer */
700 diskaddr = bio->bio_offset >> DEV_BSHIFT; /* start offset of transfer */
701 diskend = diskaddr + (bp->b_bcount / DEV_BSIZE); /* and end offset of transfer */
702 vol = &VOL[rq->volplex.volno]; /* point to volume */
704 while (diskaddr < diskend) { /* build up request components */
705 startaddr = diskaddr;
706 status = bre(rq, vol->plex[plexindex], &diskaddr, diskend); /* build up a request */
711 case REQUEST_RECOVERED:
713 * XXX FIXME if we have more than one plex, and we can
714 * satisfy the request from another, don't use the
715 * recovered request, since it's more expensive.
723 * If we get here, our request is not complete. Try
724 * to fill in the missing parts from another plex.
725 * This can happen multiple times in this function,
726 * and we reinitialize the plex mask each time, since
727 * we could have a hole in our plexes.
730 case REQUEST_DOWN: /* can't access the plex */
731 case REQUEST_DEGRADED: /* can't access the plex */
732 plexmask = ((1 << vol->plexes) - 1) /* all plexes in the volume */
733 &~(1 << plexindex); /* except for the one we were looking at */
734 for (plexno = 0; plexno < vol->plexes; plexno++) {
735 if (plexmask == 0) /* no plexes left to try */
736 return REQUEST_DOWN; /* failed */
737 diskaddr = startaddr; /* start at the beginning again */
738 if (plexmask & (1 << plexno)) { /* we haven't tried this plex yet */
739 bre(rq, vol->plex[plexno], &diskaddr, diskend); /* try a request */
740 if (diskaddr > startaddr) { /* we satisfied another part */
741 recovered = 1; /* we recovered from the problem */
742 status = REQUEST_OK; /* don't complain about it */
747 if (diskaddr == startaddr) /* didn't get any further, */
751 vol->recovered_reads += recovered; /* adjust our recovery count */
757 * Build up a request structure for writes.
758 * Return 0 if all subdisks involved in the request are up, 1 if some
759 * subdisks are not up, and -1 if the request is at least partially
760 * outside the bounds of the subdisks.
763 build_write_request(struct request *rq)
767 vinum_off_t diskstart; /* offset of current part of transfer */
768 vinum_off_t diskend; /* and end offset of transfer */
769 int plexno; /* plex index in vinum_conf */
770 struct volume *vol; /* volume in question */
771 enum requeststatus status;
773 bio = rq->bio; /* buffer pointer */
775 vol = &VOL[rq->volplex.volno]; /* point to volume */
776 diskend = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT) + (bp->b_bcount / DEV_BSIZE); /* end offset of transfer */
777 status = REQUEST_DOWN; /* assume the worst */
778 for (plexno = 0; plexno < vol->plexes; plexno++) {
779 diskstart = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT); /* start offset of transfer */
781 * Build requests for the plex.
782 * We take the best possible result here (min,
783 * not max): we're happy if we can write at all
785 status = u64min(status,
786 bre(rq, vol->plex[plexno], &diskstart, diskend));
791 /* Fill in the struct buf part of a request element. */
793 build_rq_buffer(struct rqelement *rqe, struct plex *plex)
795 struct sd *sd; /* point to subdisk */
797 struct buf *ubp; /* user (high level) buffer header */
800 sd = &SD[rqe->sdno]; /* point to subdisk */
802 ubio = rqe->rqg->rq->bio; /* pointer to user buffer header */
805 /* Initialize the buf struct */
806 /* copy these flags from user bp */
807 bp->b_flags = ubp->b_flags & (B_ORDERED | B_NOCACHE);
808 bp->b_cmd = ubp->b_cmd;
810 if (rqe->flags & XFR_BUFLOCKED) /* paranoia */
811 panic("build_rq_buffer: rqe already locked"); /* XXX remove this when we're sure */
814 BUF_LOCK(bp, LK_EXCLUSIVE); /* and lock it */
816 rqe->flags |= XFR_BUFLOCKED;
817 bp->b_bio1.bio_done = complete_rqe;
819 * You'd think that we wouldn't need to even
820 * build the request buffer for a dead subdisk,
821 * but in some cases we need information like
822 * the user buffer address. Err on the side of
823 * generosity and supply what we can. That
824 * obviously doesn't include drive information
825 * when the drive is dead.
827 if ((rqe->flags & XFR_BAD_SUBDISK) == 0) /* subdisk is accessible, */
828 bp->b_bio1.bio_driver_info = DRIVE[rqe->driveno].dev; /* drive device */
829 bp->b_bio1.bio_offset = (off_t)(rqe->sdoffset + sd->driveoffset) << DEV_BSHIFT; /* start address */
830 bp->b_bcount = rqe->buflen << DEV_BSHIFT; /* number of bytes to transfer */
831 bp->b_resid = bp->b_bcount; /* and it's still all waiting */
833 if (rqe->flags & XFR_MALLOCED) { /* this operation requires a malloced buffer */
834 bp->b_data = Malloc(bp->b_bcount); /* get a buffer to put it in */
835 if (bp->b_data == NULL) { /* failed */
836 abortrequest(rqe->rqg->rq, ENOMEM);
837 return REQUEST_ENOMEM; /* no memory */
841 * Point directly to user buffer data. This means
842 * that we don't need to do anything when we have
843 * finished the transfer
845 bp->b_data = ubp->b_data + rqe->useroffset * DEV_BSIZE;
847 * On a recovery read, we perform an XOR of
848 * all blocks to the user buffer. To make
849 * this work, we first clean out the buffer
851 if ((rqe->flags & (XFR_RECOVERY_READ | XFR_BAD_SUBDISK))
852 == (XFR_RECOVERY_READ | XFR_BAD_SUBDISK)) { /* bad subdisk of a recovery read */
853 int length = rqe->grouplen << DEV_BSHIFT; /* and count involved */
854 char *data = (char *) &rqe->b.b_data[rqe->groupoffset << DEV_BSHIFT]; /* destination */
856 bzero(data, length); /* clean it out */
862 * Abort a request: free resources and complete the
863 * user request with the specified error
866 abortrequest(struct request *rq, int error)
868 struct buf *bp = rq->bio->bio_buf; /* user buffer */
871 freerq(rq); /* free everything we're doing */
872 bp->b_flags |= B_ERROR;
873 return error; /* and give up */
877 * Check that our transfer will cover the
878 * complete address space of the user request.
880 * Return 1 if it can, otherwise 0
883 check_range_covered(struct request *rq)
888 /* Perform I/O on a subdisk */
890 sdio(struct bio *bio)
895 vinum_off_t endoffset;
897 struct buf *bp = bio->bio_buf;
899 dev = bio->bio_driver_info;
902 if (debug & DEBUG_LASTREQS)
903 logrq(loginfo_sdio, (union rqinfou) bio, bio);
905 sd = &SD[Sdno(dev)]; /* point to the subdisk */
906 drive = &DRIVE[sd->driveno];
908 if (drive->state != drive_up) {
909 if (sd->state >= sd_crashed) {
910 if (bp->b_cmd != BUF_CMD_READ) /* writing, */
911 set_sd_state(sd->sdno, sd_stale, setstate_force);
913 set_sd_state(sd->sdno, sd_crashed, setstate_force);
916 bp->b_flags |= B_ERROR;
921 * We allow access to any kind of subdisk as long as we can expect
922 * to get the I/O performed.
924 if (sd->state < sd_empty) { /* nothing to talk to, */
926 bp->b_flags |= B_ERROR;
931 sbp = (struct sdbuf *) Malloc(sizeof(struct sdbuf));
933 bp->b_error = ENOMEM;
934 bp->b_flags |= B_ERROR;
938 bzero(sbp, sizeof(struct sdbuf)); /* start with nothing */
939 sbp->b.b_cmd = bp->b_cmd;
940 sbp->b.b_bcount = bp->b_bcount; /* number of bytes to transfer */
941 sbp->b.b_resid = bp->b_resid; /* and amount waiting */
942 sbp->b.b_data = bp->b_data; /* data buffer */
944 BUF_LOCK(&sbp->b, LK_EXCLUSIVE); /* and lock it */
945 BUF_KERNPROC(&sbp->b);
946 sbp->b.b_bio1.bio_offset = bio->bio_offset + ((off_t)sd->driveoffset << DEV_BSHIFT);
947 sbp->b.b_bio1.bio_done = sdio_done; /* come here on completion */
948 sbp->b.b_bio1.bio_flags |= BIO_SYNC;
949 sbp->bio = bio; /* note the address of the original header */
950 sbp->sdno = sd->sdno; /* note for statistics */
951 sbp->driveno = sd->driveno;
952 endoffset = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT) + sbp->b.b_bcount / DEV_BSIZE; /* final sector offset */
953 if (endoffset > sd->sectors) { /* beyond the end */
954 sbp->b.b_bcount -= (endoffset - sd->sectors) * DEV_BSIZE; /* trim */
955 if (sbp->b.b_bcount <= 0) { /* nothing to transfer */
956 bp->b_resid = bp->b_bcount; /* nothing transferred */
959 uninitbufbio(&sbp->b);
965 if (debug & DEBUG_ADDRESSES)
967 " %s dev %s, sd %d, offset 0x%jx, devoffset 0x%jx, length %d\n",
968 (sbp->b.b_cmd == BUF_CMD_READ) ? "Read" : "Write",
971 (uintmax_t)(sbp->b.b_bio1.bio_offset - ((off_t)SD[sbp->sdno].driveoffset << DEV_BSHIFT)),
972 (uintmax_t)sbp->b.b_bio1.bio_offset,
977 if (debug & DEBUG_LASTREQS)
978 logrq(loginfo_sdiol, (union rqinfou) &sbp->b.b_bio1, &sbp->b.b_bio1);
980 vn_strategy(drive->vp, &sbp->b.b_bio1);
985 * Determine the size of the transfer, and make sure it is
986 * within the boundaries of the partition. Adjust transfer
987 * if needed, and signal errors or early completion.
989 * Volumes are simpler than disk slices: they only contain
990 * one component (though we call them a, b and c to make
991 * system utilities happy), and they always take up the
992 * complete space of the "partition".
994 * I'm still not happy with this: why should the label be
995 * protected? If it weren't so damned difficult to write
996 * one in the first pleace (because it's protected), it wouldn't
1000 vinum_bounds_check(struct bio *bio, struct volume *vol)
1002 struct buf *bp = bio->bio_buf;
1004 vinum_off_t maxsize = vol->size; /* size of the partition (sectors) */
1005 int size = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT; /* size of this request (sectors) */
1006 vinum_off_t blkno = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT);
1008 if (size == 0) /* no transfer specified, */
1009 return 0; /* treat as EOF */
1010 /* beyond partition? */
1011 if (bio->bio_offset < 0 /* negative start */
1012 || blkno + size > maxsize) { /* or goes beyond the end of the partition */
1013 /* if exactly at end of disk, return an EOF */
1014 if (blkno == maxsize) {
1015 bp->b_resid = bp->b_bcount;
1018 /* or truncate if part of it fits */
1019 size = maxsize - blkno;
1020 if (size <= 0) { /* nothing to transfer */
1021 bp->b_error = EINVAL;
1022 bp->b_flags |= B_ERROR;
1025 bp->b_bcount = size << DEV_BSHIFT;
1027 nbio = push_bio(bio);
1028 nbio->bio_offset = bio->bio_offset;
1033 * Allocate a request group and hook
1034 * it in in the list for rq
1037 allocrqg(struct request *rq, int elements)
1039 struct rqgroup *rqg; /* the one we're going to allocate */
1040 int size = sizeof(struct rqgroup) + elements * sizeof(struct rqelement);
1042 rqg = (struct rqgroup *) Malloc(size);
1043 if (rqg != NULL) { /* malloc OK, */
1044 if (rq->rqg) /* we already have requests */
1045 rq->lrqg->next = rqg; /* hang it off the end */
1046 else /* first request */
1047 rq->rqg = rqg; /* at the start */
1048 rq->lrqg = rqg; /* this one is the last in the list */
1050 bzero(rqg, size); /* no old junk */
1051 rqg->rq = rq; /* point back to the parent request */
1052 rqg->count = elements; /* number of requests in the group */
1053 rqg->lockbase = -1; /* no lock required yet */
1059 * Deallocate a request group out of a chain. We do
1060 * this by linear search: the chain is short, this
1061 * almost never happens, and currently it can only
1062 * happen to the first member of the chain.
1065 deallocrqg(struct rqgroup *rqg)
1067 struct rqgroup *rqgc = rqg->rq->rqg; /* point to the request chain */
1069 if (rqg->lock) /* got a lock? */
1070 unlockrange(rqg->plexno, rqg->lock); /* yes, free it */
1071 if (rqgc == rqg) /* we're first in line */
1072 rqg->rq->rqg = rqg->next; /* unhook ourselves */
1074 while ((rqgc->next != NULL) /* find the group */
1075 &&(rqgc->next != rqg))
1077 if (rqgc->next == NULL)
1079 "vinum deallocrqg: rqg %p not found in request %p\n",
1083 rqgc->next = rqg->next; /* make the chain jump over us */