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;
195 int maxplex; /* maximum number of plexes to handle */
197 struct request *rq; /* build up our request here */
198 enum requeststatus status;
200 bio->bio_driver_info = dev;
203 if (debug & DEBUG_LASTREQS)
204 logrq(loginfo_user_bp, (union rqinfou) bio, bio);
207 if ((bp->b_bcount % DEV_BSIZE) != 0) { /* bad length */
208 bp->b_error = EINVAL; /* invalid size */
209 bp->b_flags |= B_ERROR;
213 rq = (struct request *) Malloc(sizeof(struct request)); /* allocate a request struct */
214 if (rq == NULL) { /* can't do it */
215 bp->b_error = ENOMEM; /* can't get memory */
216 bp->b_flags |= B_ERROR;
220 bzero(rq, sizeof(struct request));
223 * Note the volume ID. This can be NULL, which
224 * the request building functions use as an
225 * indication for single plex I/O
227 rq->bio = bio; /* and the user buffer struct */
229 if (DEVTYPE(dev) == VINUM_VOLUME_TYPE) { /* it's a volume, */
230 rq->volplex.volno = Volno(dev); /* get the volume number */
231 vol = &VOL[rq->volplex.volno]; /* and point to it */
232 vol->active++; /* one more active request */
233 maxplex = vol->plexes; /* consider all its plexes */
235 vol = NULL; /* no volume */
236 rq->volplex.plexno = Plexno(dev); /* point to the plex */
237 rq->isplex = 1; /* note that it's a plex */
238 maxplex = 1; /* just the one plex */
241 if (bp->b_cmd == BUF_CMD_READ) {
243 * This is a read request. Decide
244 * which plex to read from.
246 * There's a potential race condition here,
247 * since we're not locked, and we could end
248 * up multiply incrementing the round-robin
249 * counter. This doesn't have any serious
253 plexno = vol->preferred_plex; /* get the plex to use */
254 if (plexno < 0) { /* round robin */
255 plexno = vol->last_plex_read;
256 vol->last_plex_read++;
257 if (vol->last_plex_read >= vol->plexes) /* got the the end? */
258 vol->last_plex_read = 0; /* wrap around */
260 status = build_read_request(rq, plexno); /* build a request */
262 vinum_off_t diskaddr = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT);
263 /* start offset of transfer */
264 status = bre(rq, /* build a request list */
267 diskaddr + (bp->b_bcount / DEV_BSIZE));
270 if (status > REQUEST_RECOVERED) { /* can't satisfy it */
271 if (status == REQUEST_DOWN) { /* not enough subdisks */
272 bp->b_error = EIO; /* I/O error */
273 bp->b_flags |= B_ERROR;
279 return launch_requests(rq, reviveok); /* now start the requests if we can */
282 * This is a write operation. We write to all plexes. If this is
283 * a RAID-4 or RAID-5 plex, we must also update the parity stripe.
287 status = build_write_request(rq); /* Not all the subdisks are up */
288 else { /* plex I/O */
289 vinum_off_t diskstart;
292 diskstart = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT); /* start offset of transfer */
293 diskend = diskstart + bp->b_bcount / DEV_BSIZE;
294 status = bre(rq, Plexno(dev),
295 &diskstart, diskend); /* build requests for the plex */
297 if (status > REQUEST_RECOVERED) { /* can't satisfy it */
298 if (status == REQUEST_DOWN) { /* not enough subdisks */
299 bp->b_error = EIO; /* I/O error */
300 bp->b_flags |= B_ERROR;
306 return launch_requests(rq, reviveok); /* now start the requests if we can */
311 * Call the low-level strategy routines to
312 * perform the requests in a struct request
315 launch_requests(struct request *rq, int reviveok)
318 int rqno; /* loop index */
319 struct rqelement *rqe; /* current element */
321 int rcount; /* request count */
324 * First find out whether we're reviving, and the
325 * request contains a conflict. If so, we hang
326 * the request off plex->waitlist of the first
327 * plex we find which is reviving
330 if ((rq->flags & XFR_REVIVECONFLICT) /* possible revive conflict */
331 &&(!reviveok)) { /* and we don't want to do it now, */
333 struct request *waitlist; /* point to the waitlist */
336 if (sd->waitlist != NULL) { /* something there already, */
337 waitlist = sd->waitlist;
338 while (waitlist->next != NULL) /* find the end */
339 waitlist = waitlist->next;
340 waitlist->next = rq; /* hook our request there */
342 sd->waitlist = rq; /* hook our request at the front */
345 if (debug & DEBUG_REVIVECONFLICT) {
347 "Revive conflict sd %d: %p\n%s dev %d.%d, offset 0x%llx, length %d\n",
350 (rq->bio->bio_buf->b_cmd & BUF_CMD_READ) ? "Read" : "Write",
351 major(((cdev_t)rq->bio->bio_driver_info)),
352 minor(((cdev_t)rq->bio->bio_driver_info)),
354 rq->bio->bio_buf->b_bcount);
357 return 0; /* and get out of here */
359 rq->active = 0; /* nothing yet */
361 if (debug & DEBUG_ADDRESSES)
363 "Request: %p\n%s dev %d.%d, offset 0x%llx, length %d\n",
365 (rq->bio->bio_buf->b_cmd == BUF_CMD_READ) ? "Read" : "Write",
366 major(((cdev_t)rq->bio->bio_driver_info)),
367 minor(((cdev_t)rq->bio->bio_driver_info)),
369 rq->bio->bio_buf->b_bcount);
370 vinum_conf.lastrq = rq;
371 vinum_conf.lastbio = rq->bio;
372 if (debug & DEBUG_LASTREQS)
373 logrq(loginfo_user_bpl, (union rqinfou) rq->bio, rq->bio);
377 * This loop happens without any participation
378 * of the bottom half, so it requires no
381 for (rqg = rq->rqg; rqg != NULL; rqg = rqg->next) { /* through the whole request chain */
382 rqg->active = rqg->count; /* they're all active */
383 for (rqno = 0; rqno < rqg->count; rqno++) {
384 rqe = &rqg->rqe[rqno];
385 if (rqe->flags & XFR_BAD_SUBDISK) /* this subdisk is bad, */
386 rqg->active--; /* one less active request */
388 if (rqg->active) /* we have at least one active request, */
389 rq->active++; /* one more active request group */
393 * Now fire off the requests. In this loop the
394 * bottom half could be completing requests
395 * before we finish, so we need critical section protection.
398 for (rqg = rq->rqg; rqg != NULL;) { /* through the whole request chain */
399 if (rqg->lockbase >= 0) /* this rqg needs a lock first */
400 rqg->lock = lockrange(rqg->lockbase, rqg->rq->bio->bio_buf, &PLEX[rqg->plexno]);
402 for (rqno = 0; rqno < rcount;) {
405 rqe = &rqg->rqe[rqno];
408 * Point to next rqg before the bottom end
409 * changes the structures.
411 if (++rqno >= rcount)
413 if ((rqe->flags & XFR_BAD_SUBDISK) == 0) { /* this subdisk is good, */
414 drive = &DRIVE[rqe->driveno]; /* look at drive */
416 if (drive->active >= drive->maxactive)
417 drive->maxactive = drive->active;
419 if (vinum_conf.active >= vinum_conf.maxactive)
420 vinum_conf.maxactive = vinum_conf.active;
422 dev = rqe->b.b_bio1.bio_driver_info;
424 if (debug & DEBUG_ADDRESSES)
426 " %s dev %d.%d, sd %d, offset 0x%llx, devoffset 0x%llx, length %d\n",
427 (rqe->b.b_cmd == BUF_CMD_READ) ? "Read" : "Write",
431 rqe->b.b_bio1.bio_offset - ((off_t)SD[rqe->sdno].driveoffset << DEV_BSHIFT),
432 rqe->b.b_bio1.bio_offset,
434 if (debug & DEBUG_LASTREQS)
435 logrq(loginfo_rqe, (union rqinfou) rqe, rq->bio);
437 /* fire off the request */
438 /* XXX this had better not be a low level drive */
439 dev_dstrategy(dev, &rqe->b.b_bio1);
448 * define the low-level requests needed to perform a
449 * high-level I/O operation for a specific plex 'plexno'.
451 * Return REQUEST_OK if all subdisks involved in the request are up,
452 * REQUEST_DOWN if some subdisks are not up, and REQUEST_EOF if the
453 * request is at least partially outside the bounds of the subdisks.
455 * Modify the pointer *diskstart to point to the end address. On
456 * read, return on the first bad subdisk, so that the caller
457 * (build_read_request) can try alternatives.
459 * On entry to this routine, the rqg structures are not assigned. The
460 * assignment is performed by expandrq(). Strictly speaking, the
461 * elements rqe->sdno of all entries should be set to -1, since 0
462 * (from bzero) is a valid subdisk number. We avoid this problem by
463 * initializing the ones we use, and not looking at the others (index
467 bre(struct request *rq,
469 vinum_off_t * diskaddr,
476 struct buf *bp; /* user's bp */
478 enum requeststatus status; /* return value */
479 vinum_off_t plexoffset; /* offset of transfer in plex */
480 vinum_off_t stripebase; /* base address of stripe (1st subdisk) */
481 vinum_off_t stripeoffset; /* offset in stripe */
482 vinum_off_t blockoffset; /* offset in stripe on subdisk */
483 struct rqelement *rqe; /* point to this request information */
484 vinum_off_t diskstart = *diskaddr; /* remember where this transfer starts */
485 enum requeststatus s; /* temp return value */
487 bio = rq->bio; /* buffer pointer */
489 status = REQUEST_OK; /* return value: OK until proven otherwise */
490 plex = &PLEX[plexno]; /* point to the plex */
492 switch (plex->organization) {
494 sd = NULL; /* (keep compiler quiet) */
495 for (sdno = 0; sdno < plex->subdisks; sdno++) {
496 sd = &SD[plex->sdnos[sdno]];
497 if (*diskaddr < sd->plexoffset) /* we must have a hole, */
498 status = REQUEST_DEGRADED; /* note the fact */
499 if (*diskaddr < (sd->plexoffset + sd->sectors)) { /* the request starts in this subdisk */
500 rqg = allocrqg(rq, 1); /* space for the request */
501 if (rqg == NULL) { /* malloc failed */
502 bp->b_error = ENOMEM;
503 bp->b_flags |= B_ERROR;
504 return REQUEST_ENOMEM;
506 rqg->plexno = plexno;
508 rqe = &rqg->rqe[0]; /* point to the element */
509 rqe->rqg = rqg; /* group */
510 rqe->sdno = sd->sdno; /* put in the subdisk number */
511 plexoffset = *diskaddr; /* start offset in plex */
512 rqe->sdoffset = plexoffset - sd->plexoffset; /* start offset in subdisk */
513 rqe->useroffset = plexoffset - diskstart; /* start offset in user buffer */
515 rqe->datalen = u64min(diskend - *diskaddr,
516 sd->sectors - rqe->sdoffset);
517 rqe->groupoffset = 0; /* no groups for concatenated plexes */
519 rqe->buflen = rqe->datalen; /* buffer length is data buffer length */
521 rqe->driveno = sd->driveno;
522 if (sd->state != sd_up) { /* *now* we find the sd is down */
523 s = checksdstate(sd, rq, *diskaddr, diskend); /* do we need to change state? */
524 if (s == REQUEST_DOWN) { /* down? */
525 rqe->flags = XFR_BAD_SUBDISK; /* yup */
526 if (rq->bio->bio_buf->b_cmd == BUF_CMD_READ) /* read request, */
527 return REQUEST_DEGRADED; /* give up here */
529 * If we're writing, don't give up
530 * because of a bad subdisk. Go
531 * through to the bitter end, but note
532 * which ones we can't access.
534 status = REQUEST_DEGRADED; /* can't do it all */
537 *diskaddr += rqe->datalen; /* bump the address */
538 if (build_rq_buffer(rqe, plex)) { /* build the buffer */
540 bp->b_error = ENOMEM;
541 bp->b_flags |= B_ERROR;
542 return REQUEST_ENOMEM; /* can't do it */
545 if (*diskaddr == diskend) /* we're finished, */
546 break; /* get out of here */
549 * We've got to the end of the plex. Have we got to the end of
550 * the transfer? It would seem that having an offset beyond the
551 * end of the subdisk is an error, but in fact it can happen if
552 * the volume has another plex of different size. There's a valid
553 * question as to why you would want to do this, but currently
556 * In a previous version, I returned REQUEST_DOWN here. I think
557 * REQUEST_EOF is more appropriate now.
559 if (diskend > sd->sectors + sd->plexoffset) /* pointing beyond EOF? */
560 status = REQUEST_EOF;
565 while (*diskaddr < diskend) { /* until we get it all sorted out */
566 if (*diskaddr >= plex->length) /* beyond the end of the plex */
567 return REQUEST_EOF; /* can't continue */
569 /* The offset of the start address from the start of the stripe. */
570 stripeoffset = *diskaddr % (plex->stripesize * plex->subdisks);
572 /* The plex-relative address of the start of the stripe. */
573 stripebase = *diskaddr - stripeoffset;
575 /* The number of the subdisk in which the start is located. */
576 sdno = stripeoffset / plex->stripesize;
578 /* The offset from the beginning of the stripe on this subdisk. */
579 blockoffset = stripeoffset % plex->stripesize;
581 sd = &SD[plex->sdnos[sdno]]; /* the subdisk in question */
582 rqg = allocrqg(rq, 1); /* space for the request */
583 if (rqg == NULL) { /* malloc failed */
584 bp->b_error = ENOMEM;
585 bp->b_flags |= B_ERROR;
586 return REQUEST_ENOMEM;
588 rqg->plexno = plexno;
590 rqe = &rqg->rqe[0]; /* point to the element */
592 rqe->sdoffset = stripebase / plex->subdisks + blockoffset; /* start offset in this subdisk */
593 rqe->useroffset = *diskaddr - diskstart; /* The offset of the start in the user buffer */
595 rqe->datalen = u64min(diskend - *diskaddr,
596 plex->stripesize - blockoffset);
597 rqe->groupoffset = 0; /* no groups for striped plexes */
599 rqe->buflen = rqe->datalen; /* buffer length is data buffer length */
601 rqe->sdno = sd->sdno; /* put in the subdisk number */
602 rqe->driveno = sd->driveno;
604 if (sd->state != sd_up) { /* *now* we find the sd is down */
605 s = checksdstate(sd, rq, *diskaddr, diskend); /* do we need to change state? */
606 if (s == REQUEST_DOWN) { /* down? */
607 rqe->flags = XFR_BAD_SUBDISK; /* yup */
608 if (rq->bio->bio_buf->b_cmd == BUF_CMD_READ) /* read request, */
609 return REQUEST_DEGRADED; /* give up here */
611 * If we're writing, don't give up
612 * because of a bad subdisk. Go through
613 * to the bitter end, but note which
614 * ones we can't access.
616 status = REQUEST_DEGRADED; /* can't do it all */
620 * It would seem that having an offset
621 * beyond the end of the subdisk is an
622 * error, but in fact it can happen if the
623 * volume has another plex of different
624 * size. There's a valid question as to why
625 * you would want to do this, but currently
628 if (rqe->sdoffset + rqe->datalen > sd->sectors) { /* ends beyond the end of the subdisk? */
629 rqe->datalen = sd->sectors - rqe->sdoffset; /* truncate */
631 if (debug & DEBUG_EOFINFO) { /* tell on the request */
633 "vinum: EOF on plex %s, sd %s offset %jx (user offset %jx)\n",
636 (uintmax_t)sd->sectors,
637 (uintmax_t)bp->b_bio1.bio_offset);
639 "vinum: stripebase 0x%llx, stripeoffset 0x%llx, "
640 "blockoffset 0x%llx\n",
641 (long long)stripebase,
642 (long long)stripeoffset,
643 (long long)blockoffset);
647 if (build_rq_buffer(rqe, plex)) { /* build the buffer */
649 bp->b_error = ENOMEM;
650 bp->b_flags |= B_ERROR;
651 return REQUEST_ENOMEM; /* can't do it */
653 *diskaddr += rqe->datalen; /* look at the remainder */
654 if ((*diskaddr < diskend) /* didn't finish the request on this stripe */
655 &&(*diskaddr < plex->length)) { /* and there's more to come */
656 plex->multiblock++; /* count another one */
657 if (sdno == plex->subdisks - 1) /* last subdisk, */
658 plex->multistripe++; /* another stripe as well */
665 * RAID-4 and RAID-5 are complicated enough to have their own
670 status = bre5(rq, plexno, diskaddr, diskend);
674 log(LOG_ERR, "vinum: invalid plex type %d in bre\n", plex->organization);
675 status = REQUEST_DOWN; /* can't access it */
682 * Build up a request structure for reading volumes.
683 * This function is not needed for plex reads, since there's
684 * no recovery if a plex read can't be satisified.
687 build_read_request(struct request *rq, /* request */
689 { /* index in the volume's plex table */
692 vinum_off_t startaddr; /* offset of previous part of transfer */
693 vinum_off_t diskaddr; /* offset of current part of transfer */
694 vinum_off_t diskend; /* and end offset of transfer */
695 int plexno; /* plex index in vinum_conf */
696 struct rqgroup *rqg; /* point to the request we're working on */
697 struct volume *vol; /* volume in question */
698 int recovered = 0; /* set if we recover a read */
699 enum requeststatus status = REQUEST_OK;
700 int plexmask; /* bit mask of plexes, for recovery */
702 bio = rq->bio; /* buffer pointer */
704 diskaddr = bio->bio_offset >> DEV_BSHIFT; /* start offset of transfer */
705 diskend = diskaddr + (bp->b_bcount / DEV_BSIZE); /* and end offset of transfer */
706 rqg = &rq->rqg[plexindex]; /* plex request */
707 vol = &VOL[rq->volplex.volno]; /* point to volume */
709 while (diskaddr < diskend) { /* build up request components */
710 startaddr = diskaddr;
711 status = bre(rq, vol->plex[plexindex], &diskaddr, diskend); /* build up a request */
716 case REQUEST_RECOVERED:
718 * XXX FIXME if we have more than one plex, and we can
719 * satisfy the request from another, don't use the
720 * recovered request, since it's more expensive.
728 * If we get here, our request is not complete. Try
729 * to fill in the missing parts from another plex.
730 * This can happen multiple times in this function,
731 * and we reinitialize the plex mask each time, since
732 * we could have a hole in our plexes.
735 case REQUEST_DOWN: /* can't access the plex */
736 case REQUEST_DEGRADED: /* can't access the plex */
737 plexmask = ((1 << vol->plexes) - 1) /* all plexes in the volume */
738 &~(1 << plexindex); /* except for the one we were looking at */
739 for (plexno = 0; plexno < vol->plexes; plexno++) {
740 if (plexmask == 0) /* no plexes left to try */
741 return REQUEST_DOWN; /* failed */
742 diskaddr = startaddr; /* start at the beginning again */
743 if (plexmask & (1 << plexno)) { /* we haven't tried this plex yet */
744 bre(rq, vol->plex[plexno], &diskaddr, diskend); /* try a request */
745 if (diskaddr > startaddr) { /* we satisfied another part */
746 recovered = 1; /* we recovered from the problem */
747 status = REQUEST_OK; /* don't complain about it */
752 if (diskaddr == startaddr) /* didn't get any further, */
756 vol->recovered_reads += recovered; /* adjust our recovery count */
762 * Build up a request structure for writes.
763 * Return 0 if all subdisks involved in the request are up, 1 if some
764 * subdisks are not up, and -1 if the request is at least partially
765 * outside the bounds of the subdisks.
768 build_write_request(struct request *rq)
772 vinum_off_t diskstart; /* offset of current part of transfer */
773 vinum_off_t diskend; /* and end offset of transfer */
774 int plexno; /* plex index in vinum_conf */
775 struct volume *vol; /* volume in question */
776 enum requeststatus status;
778 bio = rq->bio; /* buffer pointer */
780 vol = &VOL[rq->volplex.volno]; /* point to volume */
781 diskend = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT) + (bp->b_bcount / DEV_BSIZE); /* end offset of transfer */
782 status = REQUEST_DOWN; /* assume the worst */
783 for (plexno = 0; plexno < vol->plexes; plexno++) {
784 diskstart = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT); /* start offset of transfer */
786 * Build requests for the plex.
787 * We take the best possible result here (min,
788 * not max): we're happy if we can write at all
790 status = u64min(status,
791 bre(rq, vol->plex[plexno], &diskstart, diskend));
796 /* Fill in the struct buf part of a request element. */
798 build_rq_buffer(struct rqelement *rqe, struct plex *plex)
800 struct sd *sd; /* point to subdisk */
803 struct buf *ubp; /* user (high level) buffer header */
806 vol = &VOL[rqe->rqg->rq->volplex.volno];
807 sd = &SD[rqe->sdno]; /* point to subdisk */
809 ubio = rqe->rqg->rq->bio; /* pointer to user buffer header */
812 /* Initialize the buf struct */
813 /* copy these flags from user bp */
814 bp->b_flags = ubp->b_flags & (B_ORDERED | B_NOCACHE);
815 bp->b_cmd = ubp->b_cmd;
817 if (rqe->flags & XFR_BUFLOCKED) /* paranoia */
818 panic("build_rq_buffer: rqe already locked"); /* XXX remove this when we're sure */
821 BUF_LOCK(bp, LK_EXCLUSIVE); /* and lock it */
823 rqe->flags |= XFR_BUFLOCKED;
824 bp->b_bio1.bio_done = complete_rqe;
826 * You'd think that we wouldn't need to even
827 * build the request buffer for a dead subdisk,
828 * but in some cases we need information like
829 * the user buffer address. Err on the side of
830 * generosity and supply what we can. That
831 * obviously doesn't include drive information
832 * when the drive is dead.
834 if ((rqe->flags & XFR_BAD_SUBDISK) == 0) /* subdisk is accessible, */
835 bp->b_bio1.bio_driver_info = DRIVE[rqe->driveno].dev; /* drive device */
836 bp->b_bio1.bio_offset = (off_t)(rqe->sdoffset + sd->driveoffset) << DEV_BSHIFT; /* start address */
837 bp->b_bcount = rqe->buflen << DEV_BSHIFT; /* number of bytes to transfer */
838 bp->b_resid = bp->b_bcount; /* and it's still all waiting */
840 if (rqe->flags & XFR_MALLOCED) { /* this operation requires a malloced buffer */
841 bp->b_data = Malloc(bp->b_bcount); /* get a buffer to put it in */
842 if (bp->b_data == NULL) { /* failed */
843 abortrequest(rqe->rqg->rq, ENOMEM);
844 return REQUEST_ENOMEM; /* no memory */
848 * Point directly to user buffer data. This means
849 * that we don't need to do anything when we have
850 * finished the transfer
852 bp->b_data = ubp->b_data + rqe->useroffset * DEV_BSIZE;
854 * On a recovery read, we perform an XOR of
855 * all blocks to the user buffer. To make
856 * this work, we first clean out the buffer
858 if ((rqe->flags & (XFR_RECOVERY_READ | XFR_BAD_SUBDISK))
859 == (XFR_RECOVERY_READ | XFR_BAD_SUBDISK)) { /* bad subdisk of a recovery read */
860 int length = rqe->grouplen << DEV_BSHIFT; /* and count involved */
861 char *data = (char *) &rqe->b.b_data[rqe->groupoffset << DEV_BSHIFT]; /* destination */
863 bzero(data, length); /* clean it out */
869 * Abort a request: free resources and complete the
870 * user request with the specified error
873 abortrequest(struct request *rq, int error)
875 struct buf *bp = rq->bio->bio_buf; /* user buffer */
878 freerq(rq); /* free everything we're doing */
879 bp->b_flags |= B_ERROR;
880 return error; /* and give up */
884 * Check that our transfer will cover the
885 * complete address space of the user request.
887 * Return 1 if it can, otherwise 0
890 check_range_covered(struct request *rq)
895 /* Perform I/O on a subdisk */
897 sdio(struct bio *bio)
902 vinum_off_t endoffset;
904 struct buf *bp = bio->bio_buf;
906 dev = bio->bio_driver_info;
909 if (debug & DEBUG_LASTREQS)
910 logrq(loginfo_sdio, (union rqinfou) bio, bio);
912 sd = &SD[Sdno(dev)]; /* point to the subdisk */
913 drive = &DRIVE[sd->driveno];
915 if (drive->state != drive_up) {
916 if (sd->state >= sd_crashed) {
917 if (bp->b_cmd != BUF_CMD_READ) /* writing, */
918 set_sd_state(sd->sdno, sd_stale, setstate_force);
920 set_sd_state(sd->sdno, sd_crashed, setstate_force);
923 bp->b_flags |= B_ERROR;
928 * We allow access to any kind of subdisk as long as we can expect
929 * to get the I/O performed.
931 if (sd->state < sd_empty) { /* nothing to talk to, */
933 bp->b_flags |= B_ERROR;
938 sbp = (struct sdbuf *) Malloc(sizeof(struct sdbuf));
940 bp->b_error = ENOMEM;
941 bp->b_flags |= B_ERROR;
945 bzero(sbp, sizeof(struct sdbuf)); /* start with nothing */
946 sbp->b.b_cmd = bp->b_cmd;
947 sbp->b.b_bcount = bp->b_bcount; /* number of bytes to transfer */
948 sbp->b.b_resid = bp->b_resid; /* and amount waiting */
949 sbp->b.b_data = bp->b_data; /* data buffer */
951 BUF_LOCK(&sbp->b, LK_EXCLUSIVE); /* and lock it */
952 BUF_KERNPROC(&sbp->b);
953 sbp->b.b_bio1.bio_offset = bio->bio_offset + ((off_t)sd->driveoffset << DEV_BSHIFT);
954 sbp->b.b_bio1.bio_done = sdio_done; /* come here on completion */
955 sbp->b.b_bio1.bio_flags |= BIO_SYNC;
956 sbp->bio = bio; /* note the address of the original header */
957 sbp->sdno = sd->sdno; /* note for statistics */
958 sbp->driveno = sd->driveno;
959 endoffset = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT) + sbp->b.b_bcount / DEV_BSIZE; /* final sector offset */
960 if (endoffset > sd->sectors) { /* beyond the end */
961 sbp->b.b_bcount -= (endoffset - sd->sectors) * DEV_BSIZE; /* trim */
962 if (sbp->b.b_bcount <= 0) { /* nothing to transfer */
963 bp->b_resid = bp->b_bcount; /* nothing transferred */
966 uninitbufbio(&sbp->b);
972 if (debug & DEBUG_ADDRESSES)
974 " %s dev %s, sd %d, offset 0x%llx, devoffset 0x%llx, length %d\n",
975 (sbp->b.b_cmd == BUF_CMD_READ) ? "Read" : "Write",
978 sbp->b.b_bio1.bio_offset - ((off_t)SD[sbp->sdno].driveoffset << DEV_BSHIFT),
979 sbp->b.b_bio1.bio_offset,
984 if (debug & DEBUG_LASTREQS)
985 logrq(loginfo_sdiol, (union rqinfou) &sbp->b.b_bio1, &sbp->b.b_bio1);
987 vn_strategy(drive->vp, &sbp->b.b_bio1);
992 * Determine the size of the transfer, and make sure it is
993 * within the boundaries of the partition. Adjust transfer
994 * if needed, and signal errors or early completion.
996 * Volumes are simpler than disk slices: they only contain
997 * one component (though we call them a, b and c to make
998 * system utilities happy), and they always take up the
999 * complete space of the "partition".
1001 * I'm still not happy with this: why should the label be
1002 * protected? If it weren't so damned difficult to write
1003 * one in the first pleace (because it's protected), it wouldn't
1007 vinum_bounds_check(struct bio *bio, struct volume *vol)
1009 struct buf *bp = bio->bio_buf;
1011 vinum_off_t maxsize = vol->size; /* size of the partition (sectors) */
1012 int size = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT; /* size of this request (sectors) */
1013 vinum_off_t blkno = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT);
1015 if (size == 0) /* no transfer specified, */
1016 return 0; /* treat as EOF */
1017 /* beyond partition? */
1018 if (bio->bio_offset < 0 /* negative start */
1019 || blkno + size > maxsize) { /* or goes beyond the end of the partition */
1020 /* if exactly at end of disk, return an EOF */
1021 if (blkno == maxsize) {
1022 bp->b_resid = bp->b_bcount;
1025 /* or truncate if part of it fits */
1026 size = maxsize - blkno;
1027 if (size <= 0) { /* nothing to transfer */
1028 bp->b_error = EINVAL;
1029 bp->b_flags |= B_ERROR;
1032 bp->b_bcount = size << DEV_BSHIFT;
1034 nbio = push_bio(bio);
1035 nbio->bio_offset = bio->bio_offset;
1040 * Allocate a request group and hook
1041 * it in in the list for rq
1044 allocrqg(struct request *rq, int elements)
1046 struct rqgroup *rqg; /* the one we're going to allocate */
1047 int size = sizeof(struct rqgroup) + elements * sizeof(struct rqelement);
1049 rqg = (struct rqgroup *) Malloc(size);
1050 if (rqg != NULL) { /* malloc OK, */
1051 if (rq->rqg) /* we already have requests */
1052 rq->lrqg->next = rqg; /* hang it off the end */
1053 else /* first request */
1054 rq->rqg = rqg; /* at the start */
1055 rq->lrqg = rqg; /* this one is the last in the list */
1057 bzero(rqg, size); /* no old junk */
1058 rqg->rq = rq; /* point back to the parent request */
1059 rqg->count = elements; /* number of requests in the group */
1060 rqg->lockbase = -1; /* no lock required yet */
1066 * Deallocate a request group out of a chain. We do
1067 * this by linear search: the chain is short, this
1068 * almost never happens, and currently it can only
1069 * happen to the first member of the chain.
1072 deallocrqg(struct rqgroup *rqg)
1074 struct rqgroup *rqgc = rqg->rq->rqg; /* point to the request chain */
1076 if (rqg->lock) /* got a lock? */
1077 unlockrange(rqg->plexno, rqg->lock); /* yes, free it */
1078 if (rqgc == rqg) /* we're first in line */
1079 rqg->rq->rqg = rqg->next; /* unhook ourselves */
1081 while ((rqgc->next != NULL) /* find the group */
1082 &&(rqgc->next != rqg))
1084 if (rqgc->next == NULL)
1086 "vinum deallocrqg: rqg %p not found in request %p\n",
1090 rqgc->next = rqg->next; /* make the chain jump over us */