xref: /dragonfly/sys/dev/raid/vinum/vinumrequest.c (revision 32ac4919)
1 /*-
2  * Copyright (c) 1997, 1998, 1999
3  *  Nan Yang Computer Services Limited.  All rights reserved.
4  *
5  *  Parts copyright (c) 1997, 1998 Cybernet Corporation, NetMAX project.
6  *
7  *  Written by Greg Lehey
8  *
9  *  This software is distributed under the so-called ``Berkeley
10  *  License'':
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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
23  *      Services Limited.
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.
27  *
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.
39  *
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 $
42  */
43 
44 #include "vinumhdr.h"
45 #include "request.h"
46 #include <sys/resourcevar.h>
47 
48 enum requeststatus bre(struct request *rq,
49     int plexno,
50     vinum_off_t * diskstart,
51     vinum_off_t diskend);
52 enum requeststatus bre5(struct request *rq,
53     int plexno,
54     vinum_off_t * diskstart,
55     vinum_off_t diskend);
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);
68 
69 #ifdef VINUMDEBUG
70 struct rqinfo rqinfo[RQINFO_SIZE];
71 struct rqinfo *rqip = rqinfo;
72 
73 void
74 logrq(enum rqinfo_type type, union rqinfou info, struct bio *ubio)
75 {
76     cdev_t dev;
77 
78     crit_enter();
79 
80     microtime(&rqip->timestamp);			    /* when did this happen? */
81     rqip->type = type;
82     rqip->bio = ubio;					    /* user buffer */
83 
84     switch (type) {
85     case loginfo_user_bp:
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);
94 	break;
95 
96     case loginfo_iodone:
97     case loginfo_rqe:
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);
104 	break;
105 
106     case loginfo_lockwait:
107     case loginfo_lock:
108     case loginfo_unlock:
109 	bcopy(info.lockinfo, &rqip->info.lockinfo, sizeof(struct rangelock));
110 
111 	break;
112 
113     case loginfo_unused:
114 	break;
115     }
116     rqip++;
117     if (rqip >= &rqinfo[RQINFO_SIZE])			    /* wrap around */
118 	rqip = rqinfo;
119     crit_exit();
120 }
121 
122 #endif
123 
124 int
125 vinumstrategy(struct dev_strategy_args *ap)
126 {
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;
132     int volno;
133 
134     switch (DEVTYPE(dev)) {
135     case VINUM_SD_TYPE:
136     case VINUM_RAWSD_TYPE:
137 	bio->bio_driver_info = dev;
138 	sdio(bio);
139 	break;
140     case VINUM_DRIVE_TYPE:
141     default:
142 	/*
143 	 * In fact, vinum doesn't handle drives: they're
144 	 * handled directly by the disk drivers
145 	 */
146 	bp->b_error = EIO;				    /* I/O error */
147 	bp->b_flags |= B_ERROR;
148 	biodone(bio);
149 	break;
150 
151     case VINUM_VOLUME_TYPE:				    /* volume I/O */
152 	volno = Volno(dev);
153 	vol = &VOL[volno];
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;
157 	    biodone(bio);
158 	    break;
159 	}
160 	nbio = vinum_bounds_check(bio, vol);
161 	if (nbio == NULL) {
162 	    biodone(bio);
163 	    break;
164 	}
165 	/* FALLTHROUGH */
166     case VINUM_PLEX_TYPE:
167     case VINUM_RAWPLEX_TYPE:
168 	/*
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
172 	 */
173 	bp->b_resid = bp->b_bcount;			    /* transfer everything */
174 	vinumstart(dev, nbio, 0);
175 	break;
176     }
177     return(0);
178 }
179 
180 /*
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.
189  */
190 int
191 vinumstart(cdev_t dev, struct bio *bio, int reviveok)
192 {
193     struct buf *bp = bio->bio_buf;
194     int plexno;
195     struct volume *vol;
196     struct request *rq;					    /* build up our request here */
197     enum requeststatus status;
198 
199     bio->bio_driver_info = dev;
200 
201 #ifdef VINUMDEBUG
202     if (debug & DEBUG_LASTREQS)
203 	logrq(loginfo_user_bp, (union rqinfou) bio, bio);
204 #endif
205 
206     if ((bp->b_bcount % DEV_BSIZE) != 0) {		    /* bad length */
207 	bp->b_error = EINVAL;				    /* invalid size */
208 	bp->b_flags |= B_ERROR;
209 	biodone(bio);
210 	return -1;
211     }
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;
216 	biodone(bio);
217 	return -1;
218     }
219     bzero(rq, sizeof(struct request));
220 
221     /*
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
225      */
226     rq->bio = bio;					    /* and the user buffer struct */
227 
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 */
232     } else {
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 */
236     }
237 
238     if (bp->b_cmd == BUF_CMD_READ) {
239 	/*
240 	 * This is a read request.  Decide
241 	 * which plex to read from.
242 	 *
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
247 	 * effects, however.
248 	 */
249 	if (vol != NULL) {
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 */
256 	    }
257 	    status = build_read_request(rq, plexno);	    /* build a request */
258 	} else {
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 */
262 		rq->volplex.plexno,
263 		&diskaddr,
264 		diskaddr + (bp->b_bcount / DEV_BSIZE));
265 	}
266 
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;
271 	    }
272 	    biodone(bio);
273 	    freerq(rq);
274 	    return -1;
275 	}
276 	return launch_requests(rq, reviveok);		    /* now start the requests if we can */
277     } else
278 	/*
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.
281 	 */
282     {
283 	if (vol != NULL)
284 	    status = build_write_request(rq);		    /* Not all the subdisks are up */
285 	else {						    /* plex I/O */
286 	    vinum_off_t diskstart;
287 	    vinum_off_t diskend;
288 
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 */
293 	}
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;
298 	    }
299 	    biodone(bio);
300 	    freerq(rq);
301 	    return -1;
302 	}
303 	return launch_requests(rq, reviveok);		    /* now start the requests if we can */
304     }
305 }
306 
307 /*
308  * Call the low-level strategy routines to
309  * perform the requests in a struct request
310  */
311 int
312 launch_requests(struct request *rq, int reviveok)
313 {
314     struct rqgroup *rqg;
315     int rqno;						    /* loop index */
316     struct rqelement *rqe;				    /* current element */
317     struct drive *drive;
318     int rcount;						    /* request count */
319 
320     /*
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
325      */
326 
327     if ((rq->flags & XFR_REVIVECONFLICT)		    /* possible revive conflict */
328     &&(!reviveok)) {					    /* and we don't want to do it now, */
329 	struct sd *sd;
330 	struct request *waitlist;			    /* point to the waitlist */
331 
332 	sd = &SD[rq->sdno];
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 */
338 	} else
339 	    sd->waitlist = rq;				    /* hook our request at the front */
340 
341 #ifdef VINUMDEBUG
342 	if (debug & DEBUG_REVIVECONFLICT) {
343 	    log(LOG_DEBUG,
344 		"Revive conflict sd %d: %p\n%s dev %d.%d, offset 0x%jx, length %d\n",
345 		rq->sdno,
346 		rq,
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);
352 	}
353 #endif
354 	return 0;					    /* and get out of here */
355     }
356     rq->active = 0;					    /* nothing yet */
357 #ifdef VINUMDEBUG
358     if (debug & DEBUG_ADDRESSES)
359 	log(LOG_DEBUG,
360 	    "Request: %p\n%s dev %d.%d, offset 0x%jx, length %d\n",
361 	    rq,
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);
371 #endif
372 
373     /*
374      * This loop happens without any participation
375      * of the bottom half, so it requires no
376      * protection.
377      */
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 */
384 	}
385 	if (rqg->active)				    /* we have at least one active request, */
386 	    rq->active++;				    /* one more active request group */
387     }
388 
389     /*
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.
393      */
394     crit_enter();
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]);
398 	rcount = rqg->count;
399 	for (rqno = 0; rqno < rcount;) {
400 	    cdev_t dev;
401 
402 	    rqe = &rqg->rqe[rqno];
403 
404 	    /*
405 	     * Point to next rqg before the bottom end
406 	     * changes the structures.
407 	     */
408 	    if (++rqno >= rcount)
409 		rqg = rqg->next;
410 	    if ((rqe->flags & XFR_BAD_SUBDISK) == 0) {	    /* this subdisk is good, */
411 		drive = &DRIVE[rqe->driveno];		    /* look at drive */
412 		drive->active++;
413 		if (drive->active >= drive->maxactive)
414 		    drive->maxactive = drive->active;
415 		vinum_conf.active++;
416 		if (vinum_conf.active >= vinum_conf.maxactive)
417 		    vinum_conf.maxactive = vinum_conf.active;
418 
419 		dev = rqe->b.b_bio1.bio_driver_info;
420 #ifdef VINUMDEBUG
421 		if (debug & DEBUG_ADDRESSES)
422 		    log(LOG_DEBUG,
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",
425 			major(dev),
426 			minor(dev),
427 			rqe->sdno,
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,
430 			rqe->b.b_bcount);
431 		if (debug & DEBUG_LASTREQS)
432 		    logrq(loginfo_rqe, (union rqinfou) rqe, rq->bio);
433 #endif
434 		/* fire off the request */
435 		/* XXX this had better not be a low level drive */
436 		dev_dstrategy(dev, &rqe->b.b_bio1);
437 	    }
438 	}
439     }
440     crit_exit();
441     return 0;
442 }
443 
444 /*
445  * define the low-level requests needed to perform a
446  * high-level I/O operation for a specific plex 'plexno'.
447  *
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.
451  *
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.
455  *
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
461  * >= rqg->requests).
462  */
463 enum requeststatus
464 bre(struct request *rq,
465     int plexno,
466     vinum_off_t * diskaddr,
467     vinum_off_t diskend)
468 {
469     int sdno;
470     struct sd *sd;
471     struct rqgroup *rqg;
472     struct bio *bio;
473     struct buf *bp;					    /* user's bp */
474     struct plex *plex;
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 */
483 
484     bio = rq->bio;					    /* buffer pointer */
485     bp = bio->bio_buf;
486     status = REQUEST_OK;				    /* return value: OK until proven otherwise */
487     plex = &PLEX[plexno];				    /* point to the plex */
488 
489     switch (plex->organization) {
490     case plex_concat:
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;
502 		}
503 		rqg->plexno = plexno;
504 
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 */
511 		rqe->dataoffset = 0;
512 		rqe->datalen = u64min(diskend - *diskaddr,
513 				      sd->sectors - rqe->sdoffset);
514 		rqe->groupoffset = 0;			    /* no groups for concatenated plexes */
515 		rqe->grouplen = 0;
516 		rqe->buflen = rqe->datalen;		    /* buffer length is data buffer length */
517 		rqe->flags = 0;
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 */
525 			/*
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.
530 			 */
531 			status = REQUEST_DEGRADED;	    /* can't do it all */
532 		    }
533 		}
534 		*diskaddr += rqe->datalen;		    /* bump the address */
535 		if (build_rq_buffer(rqe, plex)) {	    /* build the buffer */
536 		    deallocrqg(rqg);
537 		    bp->b_error = ENOMEM;
538 		    bp->b_flags |= B_ERROR;
539 		    return REQUEST_ENOMEM;		    /* can't do it */
540 		}
541 	    }
542 	    if (*diskaddr == diskend)			    /* we're finished, */
543 		break;					    /* get out of here */
544 	}
545 	/*
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
551 	 * it's allowed.
552 	 *
553 	 * In a previous version, I returned REQUEST_DOWN here.  I think
554 	 * REQUEST_EOF is more appropriate now.
555 	 */
556 	if (diskend > sd->sectors + sd->plexoffset)	    /* pointing beyond EOF? */
557 	    status = REQUEST_EOF;
558 	break;
559 
560     case plex_striped:
561 	{
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 */
565 
566 		/* The offset of the start address from the start of the stripe. */
567 		stripeoffset = *diskaddr % (plex->stripesize * plex->subdisks);
568 
569 		/* The plex-relative address of the start of the stripe. */
570 		stripebase = *diskaddr - stripeoffset;
571 
572 		/* The number of the subdisk in which the start is located. */
573 		sdno = stripeoffset / plex->stripesize;
574 
575 		/* The offset from the beginning of the stripe on this subdisk. */
576 		blockoffset = stripeoffset % plex->stripesize;
577 
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;
584 		}
585 		rqg->plexno = plexno;
586 
587 		rqe = &rqg->rqe[0];			    /* point to the element */
588 		rqe->rqg = rqg;
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 */
591 		rqe->dataoffset = 0;
592 		rqe->datalen = u64min(diskend - *diskaddr,
593 				      plex->stripesize - blockoffset);
594 		rqe->groupoffset = 0;			    /* no groups for striped plexes */
595 		rqe->grouplen = 0;
596 		rqe->buflen = rqe->datalen;		    /* buffer length is data buffer length */
597 		rqe->flags = 0;
598 		rqe->sdno = sd->sdno;			    /* put in the subdisk number */
599 		rqe->driveno = sd->driveno;
600 
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 */
607 			/*
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.
612 			 */
613 			status = REQUEST_DEGRADED;	    /* can't do it all */
614 		    }
615 		}
616 		/*
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
623 		 * it's allowed.
624 		 */
625 		if (rqe->sdoffset + rqe->datalen > sd->sectors) { /* ends beyond the end of the subdisk? */
626 		    rqe->datalen = sd->sectors - rqe->sdoffset;	/* truncate */
627 #ifdef VINUMDEBUG
628 		    if (debug & DEBUG_EOFINFO) {	    /* tell on the request */
629 			log(LOG_DEBUG,
630 			    "vinum: EOF on plex %s, sd %s offset %jx (user offset %jx)\n",
631 			    plex->name,
632 			    sd->name,
633 			    (uintmax_t)sd->sectors,
634 			    (uintmax_t)bp->b_bio1.bio_offset);
635 			log(LOG_DEBUG,
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);
641 		    }
642 #endif
643 		}
644 		if (build_rq_buffer(rqe, plex)) {	    /* build the buffer */
645 		    deallocrqg(rqg);
646 		    bp->b_error = ENOMEM;
647 		    bp->b_flags |= B_ERROR;
648 		    return REQUEST_ENOMEM;		    /* can't do it */
649 		}
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 */
656 		}
657 	    }
658 	}
659 	break;
660 
661 	/*
662 	 * RAID-4 and RAID-5 are complicated enough to have their own
663 	 * function.
664 	 */
665     case plex_raid4:
666     case plex_raid5:
667 	status = bre5(rq, plexno, diskaddr, diskend);
668 	break;
669 
670     default:
671 	log(LOG_ERR, "vinum: invalid plex type %d in bre\n", plex->organization);
672 	status = REQUEST_DOWN;				    /* can't access it */
673     }
674 
675     return status;
676 }
677 
678 /*
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.
682  */
683 enum requeststatus
684 build_read_request(struct request *rq,			    /* request */
685     int plexindex)
686 {							    /* index in the volume's plex table */
687     struct bio *bio;
688     struct buf *bp;
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 */
697 
698     bio = rq->bio;					    /* buffer pointer */
699     bp = bio->bio_buf;
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 */
703 
704     while (diskaddr < diskend) {			    /* build up request components */
705 	startaddr = diskaddr;
706 	status = bre(rq, vol->plex[plexindex], &diskaddr, diskend); /* build up a request */
707 	switch (status) {
708 	case REQUEST_OK:
709 	    continue;
710 
711 	case REQUEST_RECOVERED:
712 	    /*
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.
716 	     */
717 	    recovered = 1;
718 	    break;
719 
720 	case REQUEST_ENOMEM:
721 	    return status;
722 	    /*
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.
728 	     */
729 	case REQUEST_EOF:
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 */
743 			break;
744 		    }
745 		}
746 	    }
747 	    if (diskaddr == startaddr)			    /* didn't get any further, */
748 		return status;
749 	}
750 	if (recovered)
751 	    vol->recovered_reads += recovered;		    /* adjust our recovery count */
752     }
753     return status;
754 }
755 
756 /*
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.
761  */
762 enum requeststatus
763 build_write_request(struct request *rq)
764 {							    /* request */
765     struct bio *bio;
766     struct buf *bp;
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;
772 
773     bio = rq->bio;					    /* buffer pointer */
774     bp = bio->bio_buf;
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 */
780 	/*
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
784 	 */
785 	status = u64min(status,
786 		     bre(rq, vol->plex[plexno], &diskstart, diskend));
787     }
788     return status;
789 }
790 
791 /* Fill in the struct buf part of a request element. */
792 enum requeststatus
793 build_rq_buffer(struct rqelement *rqe, struct plex *plex)
794 {
795     struct sd *sd;					    /* point to subdisk */
796     struct buf *bp;
797     struct buf *ubp;					    /* user (high level) buffer header */
798     struct bio *ubio;
799 
800     sd = &SD[rqe->sdno];				    /* point to subdisk */
801     bp = &rqe->b;
802     ubio = rqe->rqg->rq->bio;				    /* pointer to user buffer header */
803     ubp = ubio->bio_buf;
804 
805     /* Initialize the buf struct */
806     /* copy these flags from user bp */
807     bp->b_flags = ubp->b_flags & B_NOCACHE;
808     bp->b_cmd = ubp->b_cmd;
809 #ifdef VINUMDEBUG
810     if (rqe->flags & XFR_BUFLOCKED)			    /* paranoia */
811 	panic("build_rq_buffer: rqe already locked");	    /* XXX remove this when we're sure */
812 #endif
813     initbufbio(bp);
814     BUF_LOCK(bp, LK_EXCLUSIVE);				    /* and lock it */
815     BUF_KERNPROC(bp);
816     rqe->flags |= XFR_BUFLOCKED;
817     bp->b_bio1.bio_done = complete_rqe;
818     /*
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.
826      */
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 */
832 
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 */
838 	}
839     } else
840 	/*
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
844 	 */
845 	bp->b_data = ubp->b_data + rqe->useroffset * DEV_BSIZE;
846     /*
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
850      */
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 */
855 
856 	bzero(data, length);				    /* clean it out */
857     }
858     return 0;
859 }
860 
861 /*
862  * Abort a request: free resources and complete the
863  * user request with the specified error
864  */
865 int
866 abortrequest(struct request *rq, int error)
867 {
868     struct buf *bp = rq->bio->bio_buf;			    /* user buffer */
869 
870     bp->b_error = error;
871     freerq(rq);						    /* free everything we're doing */
872     bp->b_flags |= B_ERROR;
873     return error;					    /* and give up */
874 }
875 
876 /*
877  * Check that our transfer will cover the
878  * complete address space of the user request.
879  *
880  * Return 1 if it can, otherwise 0
881  */
882 int
883 check_range_covered(struct request *rq)
884 {
885     return 1;
886 }
887 
888 /* Perform I/O on a subdisk */
889 void
890 sdio(struct bio *bio)
891 {
892     cdev_t dev;
893     struct sd *sd;
894     struct sdbuf *sbp;
895     vinum_off_t endoffset;
896     struct drive *drive;
897     struct buf *bp = bio->bio_buf;
898 
899     dev = bio->bio_driver_info;
900 
901 #ifdef VINUMDEBUG
902     if (debug & DEBUG_LASTREQS)
903 	logrq(loginfo_sdio, (union rqinfou) bio, bio);
904 #endif
905     sd = &SD[Sdno(dev)];				    /* point to the subdisk */
906     drive = &DRIVE[sd->driveno];
907 
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);
912 	    else
913 		set_sd_state(sd->sdno, sd_crashed, setstate_force);
914 	}
915 	bp->b_error = EIO;
916 	bp->b_flags |= B_ERROR;
917 	biodone(bio);
918 	return;
919     }
920     /*
921      * We allow access to any kind of subdisk as long as we can expect
922      * to get the I/O performed.
923      */
924     if (sd->state < sd_empty) {				    /* nothing to talk to, */
925 	bp->b_error = EIO;
926 	bp->b_flags |= B_ERROR;
927 	biodone(bio);
928 	return;
929     }
930     /* Get a buffer */
931     sbp = (struct sdbuf *) Malloc(sizeof(struct sdbuf));
932     if (sbp == NULL) {
933 	bp->b_error = ENOMEM;
934 	bp->b_flags |= B_ERROR;
935 	biodone(bio);
936 	return;
937     }
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 */
943     initbufbio(&sbp->b);
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 */
957 	    biodone(bio);
958 	    BUF_UNLOCK(&sbp->b);
959 	    uninitbufbio(&sbp->b);
960 	    Free(sbp);
961 	    return;
962 	}
963     }
964 #ifdef VINUMDEBUG
965     if (debug & DEBUG_ADDRESSES)
966 	log(LOG_DEBUG,
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",
969 	    drive->devicename,
970 	    sbp->sdno,
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,
973 	    sbp->b.b_bcount);
974 #endif
975     crit_enter();
976 #ifdef VINUMDEBUG
977     if (debug & DEBUG_LASTREQS)
978 	logrq(loginfo_sdiol, (union rqinfou) &sbp->b.b_bio1, &sbp->b.b_bio1);
979 #endif
980     vn_strategy(drive->vp, &sbp->b.b_bio1);
981     crit_exit();
982 }
983 
984 /*
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.
988  *
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".
993  *
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
997  * be a problem.
998  */
999 struct bio *
1000 vinum_bounds_check(struct bio *bio, struct volume *vol)
1001 {
1002     struct buf *bp = bio->bio_buf;
1003     struct bio *nbio;
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);
1007 
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;
1016 	    return (NULL);
1017 	}
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;
1023 	    return (NULL);
1024 	}
1025 	bp->b_bcount = size << DEV_BSHIFT;
1026     }
1027     nbio = push_bio(bio);
1028     nbio->bio_offset = bio->bio_offset;
1029     return (nbio);
1030 }
1031 
1032 /*
1033  * Allocate a request group and hook
1034  * it in in the list for rq
1035  */
1036 struct rqgroup *
1037 allocrqg(struct request *rq, int elements)
1038 {
1039     struct rqgroup *rqg;				    /* the one we're going to allocate */
1040     int size = sizeof(struct rqgroup) + elements * sizeof(struct rqelement);
1041 
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 */
1049 
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 */
1054     }
1055     return rqg;
1056 }
1057 
1058 /*
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.
1063  */
1064 void
1065 deallocrqg(struct rqgroup *rqg)
1066 {
1067     struct rqgroup *rqgc = rqg->rq->rqg;		    /* point to the request chain */
1068 
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 */
1073     else {
1074 	while ((rqgc->next != NULL)			    /* find the group */
1075 	&&(rqgc->next != rqg))
1076 	    rqgc = rqgc->next;
1077 	if (rqgc->next == NULL)
1078 	    log(LOG_ERR,
1079 		"vinum deallocrqg: rqg %p not found in request %p\n",
1080 		rqg->rq,
1081 		rqg);
1082 	else
1083 	    rqgc->next = rqg->next;			    /* make the chain jump over us */
1084     }
1085     Free(rqg);
1086 }
1087