xref: /dragonfly/sys/dev/raid/vinum/vinumrequest.c (revision 59a92d18)
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  * $DragonFly: src/sys/dev/raid/vinum/vinumrequest.c,v 1.21 2007/09/15 20:06:39 swildner Exp $
43  */
44 
45 #include "vinumhdr.h"
46 #include "request.h"
47 #include <sys/resourcevar.h>
48 
49 enum requeststatus bre(struct request *rq,
50     int plexno,
51     vinum_off_t * diskstart,
52     vinum_off_t diskend);
53 enum requeststatus bre5(struct request *rq,
54     int plexno,
55     vinum_off_t * diskstart,
56     vinum_off_t diskend);
57 enum requeststatus build_read_request(struct request *rq, int volplexno);
58 enum requeststatus build_write_request(struct request *rq);
59 enum requeststatus build_rq_buffer(struct rqelement *rqe, struct plex *plex);
60 int find_alternate_sd(struct request *rq);
61 int check_range_covered(struct request *);
62 void complete_rqe(struct bio *bio);
63 void complete_raid5_write(struct rqelement *);
64 int abortrequest(struct request *rq, int error);
65 void sdio_done(struct bio *bio);
66 struct bio *vinum_bounds_check(struct bio *bio, struct volume *vol);
67 caddr_t allocdatabuf(struct rqelement *rqe);
68 void freedatabuf(struct rqelement *rqe);
69 
70 #ifdef VINUMDEBUG
71 struct rqinfo rqinfo[RQINFO_SIZE];
72 struct rqinfo *rqip = rqinfo;
73 
74 void
75 logrq(enum rqinfo_type type, union rqinfou info, struct bio *ubio)
76 {
77     cdev_t dev;
78 
79     crit_enter();
80 
81     microtime(&rqip->timestamp);			    /* when did this happen? */
82     rqip->type = type;
83     rqip->bio = ubio;					    /* user buffer */
84 
85     switch (type) {
86     case loginfo_user_bp:
87     case loginfo_user_bpl:
88     case loginfo_sdio:					    /* subdisk I/O */
89     case loginfo_sdiol:					    /* subdisk I/O launch */
90     case loginfo_sdiodone:				    /* subdisk I/O complete */
91 	bcopy(info.bio, &rqip->info.bio, sizeof(struct bio));
92 	dev = info.bio->bio_driver_info;
93 	rqip->devmajor = major(dev);
94 	rqip->devminor = minor(dev);
95 	break;
96 
97     case loginfo_iodone:
98     case loginfo_rqe:
99     case loginfo_raid5_data:
100     case loginfo_raid5_parity:
101 	bcopy(info.rqe, &rqip->info.rqe, sizeof(struct rqelement));
102 	dev = info.rqe->b.b_bio1.bio_driver_info;
103 	rqip->devmajor = major(dev);
104 	rqip->devminor = minor(dev);
105 	break;
106 
107     case loginfo_lockwait:
108     case loginfo_lock:
109     case loginfo_unlock:
110 	bcopy(info.lockinfo, &rqip->info.lockinfo, sizeof(struct rangelock));
111 
112 	break;
113 
114     case loginfo_unused:
115 	break;
116     }
117     rqip++;
118     if (rqip >= &rqinfo[RQINFO_SIZE])			    /* wrap around */
119 	rqip = rqinfo;
120     crit_exit();
121 }
122 
123 #endif
124 
125 int
126 vinumstrategy(struct dev_strategy_args *ap)
127 {
128     cdev_t dev = ap->a_head.a_dev;
129     struct bio *bio = ap->a_bio;
130     struct buf *bp = bio->bio_buf;
131     struct bio *nbio = bio;
132     struct volume *vol = NULL;
133     int volno;
134 
135     switch (DEVTYPE(dev)) {
136     case VINUM_SD_TYPE:
137     case VINUM_RAWSD_TYPE:
138 	bio->bio_driver_info = dev;
139 	sdio(bio);
140 	break;
141     case VINUM_DRIVE_TYPE:
142     default:
143 	/*
144 	 * In fact, vinum doesn't handle drives: they're
145 	 * handled directly by the disk drivers
146 	 */
147 	bp->b_error = EIO;				    /* I/O error */
148 	bp->b_flags |= B_ERROR;
149 	biodone(bio);
150 	break;
151 
152     case VINUM_VOLUME_TYPE:				    /* volume I/O */
153 	volno = Volno(dev);
154 	vol = &VOL[volno];
155 	if (vol->state != volume_up) {			    /* can't access this volume */
156 	    bp->b_error = EIO;				    /* I/O error */
157 	    bp->b_flags |= B_ERROR;
158 	    biodone(bio);
159 	    break;
160 	}
161 	nbio = vinum_bounds_check(bio, vol);
162 	if (nbio == NULL) {
163 	    biodone(bio);
164 	    break;
165 	}
166 	/* FALLTHROUGH */
167     case VINUM_PLEX_TYPE:
168     case VINUM_RAWPLEX_TYPE:
169 	/*
170 	 * Plex I/O is pretty much the same as volume I/O
171 	 * for a single plex.  Indicate this by passing a NULL
172 	 * pointer (set above) for the volume
173 	 */
174 	bp->b_resid = bp->b_bcount;			    /* transfer everything */
175 	vinumstart(dev, nbio, 0);
176 	break;
177     }
178     return(0);
179 }
180 
181 /*
182  * Start a transfer.  Return -1 on error,
183  * 0 if OK, 1 if we need to retry.
184  * Parameter reviveok is set when doing
185  * transfers for revives: it allows transfers to
186  * be started immediately when a revive is in
187  * progress.  During revive, normal transfers
188  * are queued if they share address space with
189  * a currently active revive operation.
190  */
191 int
192 vinumstart(cdev_t dev, struct bio *bio, int reviveok)
193 {
194     struct buf *bp = bio->bio_buf;
195     int plexno;
196     int maxplex;					    /* maximum number of plexes to handle */
197     struct volume *vol;
198     struct request *rq;					    /* build up our request here */
199     enum requeststatus status;
200 
201     bio->bio_driver_info = dev;
202 
203 #if VINUMDEBUG
204     if (debug & DEBUG_LASTREQS)
205 	logrq(loginfo_user_bp, (union rqinfou) bio, bio);
206 #endif
207 
208     if ((bp->b_bcount % DEV_BSIZE) != 0) {		    /* bad length */
209 	bp->b_error = EINVAL;				    /* invalid size */
210 	bp->b_flags |= B_ERROR;
211 	biodone(bio);
212 	return -1;
213     }
214     rq = (struct request *) Malloc(sizeof(struct request)); /* allocate a request struct */
215     if (rq == NULL) {					    /* can't do it */
216 	bp->b_error = ENOMEM;				    /* can't get memory */
217 	bp->b_flags |= B_ERROR;
218 	biodone(bio);
219 	return -1;
220     }
221     bzero(rq, sizeof(struct request));
222 
223     /*
224      * Note the volume ID.  This can be NULL, which
225      * the request building functions use as an
226      * indication for single plex I/O
227      */
228     rq->bio = bio;					    /* and the user buffer struct */
229 
230     if (DEVTYPE(dev) == VINUM_VOLUME_TYPE) {	    /* it's a volume, */
231 	rq->volplex.volno = Volno(dev);		    /* get the volume number */
232 	vol = &VOL[rq->volplex.volno];			    /* and point to it */
233 	vol->active++;					    /* one more active request */
234 	maxplex = vol->plexes;				    /* consider all its plexes */
235     } else {
236 	vol = NULL;					    /* no volume */
237 	rq->volplex.plexno = Plexno(dev);		    /* point to the plex */
238 	rq->isplex = 1;					    /* note that it's a plex */
239 	maxplex = 1;					    /* just the one plex */
240     }
241 
242     if (bp->b_cmd == BUF_CMD_READ) {
243 	/*
244 	 * This is a read request.  Decide
245 	 * which plex to read from.
246 	 *
247 	 * There's a potential race condition here,
248 	 * since we're not locked, and we could end
249 	 * up multiply incrementing the round-robin
250 	 * counter.  This doesn't have any serious
251 	 * effects, however.
252 	 */
253 	if (vol != NULL) {
254 	    plexno = vol->preferred_plex;		    /* get the plex to use */
255 	    if (plexno < 0) {				    /* round robin */
256 		plexno = vol->last_plex_read;
257 		vol->last_plex_read++;
258 		if (vol->last_plex_read >= vol->plexes)	    /* got the the end? */
259 		    vol->last_plex_read = 0;		    /* wrap around */
260 	    }
261 	    status = build_read_request(rq, plexno);	    /* build a request */
262 	} else {
263 	    vinum_off_t diskaddr = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT);
264 							    /* start offset of transfer */
265 	    status = bre(rq,				    /* build a request list */
266 		rq->volplex.plexno,
267 		&diskaddr,
268 		diskaddr + (bp->b_bcount / DEV_BSIZE));
269 	}
270 
271 	if (status > REQUEST_RECOVERED) {		    /* can't satisfy it */
272 	    if (status == REQUEST_DOWN) {		    /* not enough subdisks */
273 		bp->b_error = EIO;			    /* I/O error */
274 		bp->b_flags |= B_ERROR;
275 	    }
276 	    biodone(bio);
277 	    freerq(rq);
278 	    return -1;
279 	}
280 	return launch_requests(rq, reviveok);		    /* now start the requests if we can */
281     } else
282 	/*
283 	 * This is a write operation.  We write to all plexes.  If this is
284 	 * a RAID-4 or RAID-5 plex, we must also update the parity stripe.
285 	 */
286     {
287 	if (vol != NULL)
288 	    status = build_write_request(rq);		    /* Not all the subdisks are up */
289 	else {						    /* plex I/O */
290 	    vinum_off_t diskstart;
291 	    vinum_off_t diskend;
292 
293 	    diskstart = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT); /* start offset of transfer */
294 	    diskend = diskstart + bp->b_bcount / DEV_BSIZE;
295 	    status = bre(rq, Plexno(dev),
296 		&diskstart, diskend);  /* build requests for the plex */
297 	}
298 	if (status > REQUEST_RECOVERED) {		    /* can't satisfy it */
299 	    if (status == REQUEST_DOWN) {		    /* not enough subdisks */
300 		bp->b_error = EIO;			    /* I/O error */
301 		bp->b_flags |= B_ERROR;
302 	    }
303 	    biodone(bio);
304 	    freerq(rq);
305 	    return -1;
306 	}
307 	return launch_requests(rq, reviveok);		    /* now start the requests if we can */
308     }
309 }
310 
311 /*
312  * Call the low-level strategy routines to
313  * perform the requests in a struct request
314  */
315 int
316 launch_requests(struct request *rq, int reviveok)
317 {
318     struct rqgroup *rqg;
319     int rqno;						    /* loop index */
320     struct rqelement *rqe;				    /* current element */
321     struct drive *drive;
322     int rcount;						    /* request count */
323 
324     /*
325      * First find out whether we're reviving, and the
326      * request contains a conflict.  If so, we hang
327      * the request off plex->waitlist of the first
328      * plex we find which is reviving
329      */
330 
331     if ((rq->flags & XFR_REVIVECONFLICT)		    /* possible revive conflict */
332     &&(!reviveok)) {					    /* and we don't want to do it now, */
333 	struct sd *sd;
334 	struct request *waitlist;			    /* point to the waitlist */
335 
336 	sd = &SD[rq->sdno];
337 	if (sd->waitlist != NULL) {			    /* something there already, */
338 	    waitlist = sd->waitlist;
339 	    while (waitlist->next != NULL)		    /* find the end */
340 		waitlist = waitlist->next;
341 	    waitlist->next = rq;			    /* hook our request there */
342 	} else
343 	    sd->waitlist = rq;				    /* hook our request at the front */
344 
345 #if VINUMDEBUG
346 	if (debug & DEBUG_REVIVECONFLICT) {
347 	    log(LOG_DEBUG,
348 		"Revive conflict sd %d: %p\n%s dev %d.%d, offset 0x%llx, length %d\n",
349 		rq->sdno,
350 		rq,
351 		(rq->bio->bio_buf->b_cmd & BUF_CMD_READ) ? "Read" : "Write",
352 		major(((cdev_t)rq->bio->bio_driver_info)),
353 		minor(((cdev_t)rq->bio->bio_driver_info)),
354 		rq->bio->bio_offset,
355 		rq->bio->bio_buf->b_bcount);
356 	}
357 #endif
358 	return 0;					    /* and get out of here */
359     }
360     rq->active = 0;					    /* nothing yet */
361 #if VINUMDEBUG
362     if (debug & DEBUG_ADDRESSES)
363 	log(LOG_DEBUG,
364 	    "Request: %p\n%s dev %d.%d, offset 0x%llx, length %d\n",
365 	    rq,
366 	    (rq->bio->bio_buf->b_cmd == BUF_CMD_READ) ? "Read" : "Write",
367 	    major(((cdev_t)rq->bio->bio_driver_info)),
368 	    minor(((cdev_t)rq->bio->bio_driver_info)),
369 	    rq->bio->bio_offset,
370 	    rq->bio->bio_buf->b_bcount);
371     vinum_conf.lastrq = rq;
372     vinum_conf.lastbio = rq->bio;
373     if (debug & DEBUG_LASTREQS)
374 	logrq(loginfo_user_bpl, (union rqinfou) rq->bio, rq->bio);
375 #endif
376 
377     /*
378      * This loop happens without any participation
379      * of the bottom half, so it requires no
380      * protection.
381      */
382     for (rqg = rq->rqg; rqg != NULL; rqg = rqg->next) {	    /* through the whole request chain */
383 	rqg->active = rqg->count;			    /* they're all active */
384 	for (rqno = 0; rqno < rqg->count; rqno++) {
385 	    rqe = &rqg->rqe[rqno];
386 	    if (rqe->flags & XFR_BAD_SUBDISK)		    /* this subdisk is bad, */
387 		rqg->active--;				    /* one less active request */
388 	}
389 	if (rqg->active)				    /* we have at least one active request, */
390 	    rq->active++;				    /* one more active request group */
391     }
392 
393     /*
394      * Now fire off the requests.  In this loop the
395      * bottom half could be completing requests
396      * before we finish, so we need critical section protection.
397      */
398     crit_enter();
399     for (rqg = rq->rqg; rqg != NULL;) {			    /* through the whole request chain */
400 	if (rqg->lockbase >= 0)				    /* this rqg needs a lock first */
401 	    rqg->lock = lockrange(rqg->lockbase, rqg->rq->bio->bio_buf, &PLEX[rqg->plexno]);
402 	rcount = rqg->count;
403 	for (rqno = 0; rqno < rcount;) {
404 	    cdev_t dev;
405 
406 	    rqe = &rqg->rqe[rqno];
407 
408 	    /*
409 	     * Point to next rqg before the bottom end
410 	     * changes the structures.
411 	     */
412 	    if (++rqno >= rcount)
413 		rqg = rqg->next;
414 	    if ((rqe->flags & XFR_BAD_SUBDISK) == 0) {	    /* this subdisk is good, */
415 		drive = &DRIVE[rqe->driveno];		    /* look at drive */
416 		drive->active++;
417 		if (drive->active >= drive->maxactive)
418 		    drive->maxactive = drive->active;
419 		vinum_conf.active++;
420 		if (vinum_conf.active >= vinum_conf.maxactive)
421 		    vinum_conf.maxactive = vinum_conf.active;
422 
423 		dev = rqe->b.b_bio1.bio_driver_info;
424 #ifdef VINUMDEBUG
425 		if (debug & DEBUG_ADDRESSES)
426 		    log(LOG_DEBUG,
427 			"  %s dev %d.%d, sd %d, offset 0x%llx, devoffset 0x%llx, length %d\n",
428 			(rqe->b.b_cmd == BUF_CMD_READ) ? "Read" : "Write",
429 			major(dev),
430 			minor(dev),
431 			rqe->sdno,
432 			rqe->b.b_bio1.bio_offset - ((off_t)SD[rqe->sdno].driveoffset << DEV_BSHIFT),
433 			rqe->b.b_bio1.bio_offset,
434 			rqe->b.b_bcount);
435 		if (debug & DEBUG_LASTREQS)
436 		    logrq(loginfo_rqe, (union rqinfou) rqe, rq->bio);
437 #endif
438 		/* fire off the request */
439 		/* XXX this had better not be a low level drive */
440 		dev_dstrategy(dev, &rqe->b.b_bio1);
441 	    }
442 	}
443     }
444     crit_exit();
445     return 0;
446 }
447 
448 /*
449  * define the low-level requests needed to perform a
450  * high-level I/O operation for a specific plex 'plexno'.
451  *
452  * Return REQUEST_OK if all subdisks involved in the request are up,
453  * REQUEST_DOWN if some subdisks are not up, and REQUEST_EOF if the
454  * request is at least partially outside the bounds of the subdisks.
455  *
456  * Modify the pointer *diskstart to point to the end address.  On
457  * read, return on the first bad subdisk, so that the caller
458  * (build_read_request) can try alternatives.
459  *
460  * On entry to this routine, the rqg structures are not assigned.  The
461  * assignment is performed by expandrq().  Strictly speaking, the
462  * elements rqe->sdno of all entries should be set to -1, since 0
463  * (from bzero) is a valid subdisk number.  We avoid this problem by
464  * initializing the ones we use, and not looking at the others (index
465  * >= rqg->requests).
466  */
467 enum requeststatus
468 bre(struct request *rq,
469     int plexno,
470     vinum_off_t * diskaddr,
471     vinum_off_t diskend)
472 {
473     int sdno;
474     struct sd *sd;
475     struct rqgroup *rqg;
476     struct bio *bio;
477     struct buf *bp;					    /* user's bp */
478     struct plex *plex;
479     enum requeststatus status;				    /* return value */
480     vinum_off_t plexoffset;					    /* offset of transfer in plex */
481     vinum_off_t stripebase;					    /* base address of stripe (1st subdisk) */
482     vinum_off_t stripeoffset;				    /* offset in stripe */
483     vinum_off_t blockoffset;				    /* offset in stripe on subdisk */
484     struct rqelement *rqe;				    /* point to this request information */
485     vinum_off_t diskstart = *diskaddr;			    /* remember where this transfer starts */
486     enum requeststatus s;				    /* temp return value */
487 
488     bio = rq->bio;					    /* buffer pointer */
489     bp = bio->bio_buf;
490     status = REQUEST_OK;				    /* return value: OK until proven otherwise */
491     plex = &PLEX[plexno];				    /* point to the plex */
492 
493     switch (plex->organization) {
494     case plex_concat:
495 	sd = NULL;					    /* (keep compiler quiet) */
496 	for (sdno = 0; sdno < plex->subdisks; sdno++) {
497 	    sd = &SD[plex->sdnos[sdno]];
498 	    if (*diskaddr < sd->plexoffset)		    /* we must have a hole, */
499 		status = REQUEST_DEGRADED;		    /* note the fact */
500 	    if (*diskaddr < (sd->plexoffset + sd->sectors)) { /* the request starts in this subdisk */
501 		rqg = allocrqg(rq, 1);			    /* space for the request */
502 		if (rqg == NULL) {			    /* malloc failed */
503 		    bp->b_error = ENOMEM;
504 		    bp->b_flags |= B_ERROR;
505 		    return REQUEST_ENOMEM;
506 		}
507 		rqg->plexno = plexno;
508 
509 		rqe = &rqg->rqe[0];			    /* point to the element */
510 		rqe->rqg = rqg;				    /* group */
511 		rqe->sdno = sd->sdno;			    /* put in the subdisk number */
512 		plexoffset = *diskaddr;			    /* start offset in plex */
513 		rqe->sdoffset = plexoffset - sd->plexoffset; /* start offset in subdisk */
514 		rqe->useroffset = plexoffset - diskstart;   /* start offset in user buffer */
515 		rqe->dataoffset = 0;
516 		rqe->datalen = u64min(diskend - *diskaddr,
517 				      sd->sectors - rqe->sdoffset);
518 		rqe->groupoffset = 0;			    /* no groups for concatenated plexes */
519 		rqe->grouplen = 0;
520 		rqe->buflen = rqe->datalen;		    /* buffer length is data buffer length */
521 		rqe->flags = 0;
522 		rqe->driveno = sd->driveno;
523 		if (sd->state != sd_up) {		    /* *now* we find the sd is down */
524 		    s = checksdstate(sd, rq, *diskaddr, diskend); /* do we need to change state? */
525 		    if (s == REQUEST_DOWN) {		    /* down? */
526 			rqe->flags = XFR_BAD_SUBDISK;	    /* yup */
527 			if (rq->bio->bio_buf->b_cmd == BUF_CMD_READ)    /* read request, */
528 			    return REQUEST_DEGRADED;	    /* give up here */
529 			/*
530 			 * If we're writing, don't give up
531 			 * because of a bad subdisk.  Go
532 			 * through to the bitter end, but note
533 			 * which ones we can't access.
534 			 */
535 			status = REQUEST_DEGRADED;	    /* can't do it all */
536 		    }
537 		}
538 		*diskaddr += rqe->datalen;		    /* bump the address */
539 		if (build_rq_buffer(rqe, plex)) {	    /* build the buffer */
540 		    deallocrqg(rqg);
541 		    bp->b_error = ENOMEM;
542 		    bp->b_flags |= B_ERROR;
543 		    return REQUEST_ENOMEM;		    /* can't do it */
544 		}
545 	    }
546 	    if (*diskaddr == diskend)			    /* we're finished, */
547 		break;					    /* get out of here */
548 	}
549 	/*
550 	 * We've got to the end of the plex.  Have we got to the end of
551 	 * the transfer?  It would seem that having an offset beyond the
552 	 * end of the subdisk is an error, but in fact it can happen if
553 	 * the volume has another plex of different size.  There's a valid
554 	 * question as to why you would want to do this, but currently
555 	 * it's allowed.
556 	 *
557 	 * In a previous version, I returned REQUEST_DOWN here.  I think
558 	 * REQUEST_EOF is more appropriate now.
559 	 */
560 	if (diskend > sd->sectors + sd->plexoffset)	    /* pointing beyond EOF? */
561 	    status = REQUEST_EOF;
562 	break;
563 
564     case plex_striped:
565 	{
566 	    while (*diskaddr < diskend) {		    /* until we get it all sorted out */
567 		if (*diskaddr >= plex->length)		    /* beyond the end of the plex */
568 		    return REQUEST_EOF;			    /* can't continue */
569 
570 		/* The offset of the start address from the start of the stripe. */
571 		stripeoffset = *diskaddr % (plex->stripesize * plex->subdisks);
572 
573 		/* The plex-relative address of the start of the stripe. */
574 		stripebase = *diskaddr - stripeoffset;
575 
576 		/* The number of the subdisk in which the start is located. */
577 		sdno = stripeoffset / plex->stripesize;
578 
579 		/* The offset from the beginning of the stripe on this subdisk. */
580 		blockoffset = stripeoffset % plex->stripesize;
581 
582 		sd = &SD[plex->sdnos[sdno]];		    /* the subdisk in question */
583 		rqg = allocrqg(rq, 1);			    /* space for the request */
584 		if (rqg == NULL) {			    /* malloc failed */
585 		    bp->b_error = ENOMEM;
586 		    bp->b_flags |= B_ERROR;
587 		    return REQUEST_ENOMEM;
588 		}
589 		rqg->plexno = plexno;
590 
591 		rqe = &rqg->rqe[0];			    /* point to the element */
592 		rqe->rqg = rqg;
593 		rqe->sdoffset = stripebase / plex->subdisks + blockoffset; /* start offset in this subdisk */
594 		rqe->useroffset = *diskaddr - diskstart;    /* The offset of the start in the user buffer */
595 		rqe->dataoffset = 0;
596 		rqe->datalen = u64min(diskend - *diskaddr,
597 				      plex->stripesize - blockoffset);
598 		rqe->groupoffset = 0;			    /* no groups for striped plexes */
599 		rqe->grouplen = 0;
600 		rqe->buflen = rqe->datalen;		    /* buffer length is data buffer length */
601 		rqe->flags = 0;
602 		rqe->sdno = sd->sdno;			    /* put in the subdisk number */
603 		rqe->driveno = sd->driveno;
604 
605 		if (sd->state != sd_up) {		    /* *now* we find the sd is down */
606 		    s = checksdstate(sd, rq, *diskaddr, diskend); /* do we need to change state? */
607 		    if (s == REQUEST_DOWN) {		    /* down? */
608 			rqe->flags = XFR_BAD_SUBDISK;	    /* yup */
609 			if (rq->bio->bio_buf->b_cmd == BUF_CMD_READ)	    /* read request, */
610 			    return REQUEST_DEGRADED;	    /* give up here */
611 			/*
612 			 * If we're writing, don't give up
613 			 * because of a bad subdisk.  Go through
614 			 * to the bitter end, but note which
615 			 * ones we can't access.
616 			 */
617 			status = REQUEST_DEGRADED;	    /* can't do it all */
618 		    }
619 		}
620 		/*
621 		 * It would seem that having an offset
622 		 * beyond the end of the subdisk is an
623 		 * error, but in fact it can happen if the
624 		 * volume has another plex of different
625 		 * size.  There's a valid question as to why
626 		 * you would want to do this, but currently
627 		 * it's allowed.
628 		 */
629 		if (rqe->sdoffset + rqe->datalen > sd->sectors) { /* ends beyond the end of the subdisk? */
630 		    rqe->datalen = sd->sectors - rqe->sdoffset;	/* truncate */
631 #if VINUMDEBUG
632 		    if (debug & DEBUG_EOFINFO) {	    /* tell on the request */
633 			log(LOG_DEBUG,
634 			    "vinum: EOF on plex %s, sd %s offset %jx (user offset %jx)\n",
635 			    plex->name,
636 			    sd->name,
637 			    (uintmax_t)sd->sectors,
638 			    (uintmax_t)bp->b_bio1.bio_offset);
639 			log(LOG_DEBUG,
640 			    "vinum: stripebase 0x%llx, stripeoffset 0x%llx, "
641 			    "blockoffset 0x%llx\n",
642 			    (long long)stripebase,
643 			    (long long)stripeoffset,
644 			    (long long)blockoffset);
645 		    }
646 #endif
647 		}
648 		if (build_rq_buffer(rqe, plex)) {	    /* build the buffer */
649 		    deallocrqg(rqg);
650 		    bp->b_error = ENOMEM;
651 		    bp->b_flags |= B_ERROR;
652 		    return REQUEST_ENOMEM;		    /* can't do it */
653 		}
654 		*diskaddr += rqe->datalen;		    /* look at the remainder */
655 		if ((*diskaddr < diskend)		    /* didn't finish the request on this stripe */
656 		&&(*diskaddr < plex->length)) {		    /* and there's more to come */
657 		    plex->multiblock++;			    /* count another one */
658 		    if (sdno == plex->subdisks - 1)	    /* last subdisk, */
659 			plex->multistripe++;		    /* another stripe as well */
660 		}
661 	    }
662 	}
663 	break;
664 
665 	/*
666 	 * RAID-4 and RAID-5 are complicated enough to have their own
667 	 * function.
668 	 */
669     case plex_raid4:
670     case plex_raid5:
671 	status = bre5(rq, plexno, diskaddr, diskend);
672 	break;
673 
674     default:
675 	log(LOG_ERR, "vinum: invalid plex type %d in bre\n", plex->organization);
676 	status = REQUEST_DOWN;				    /* can't access it */
677     }
678 
679     return status;
680 }
681 
682 /*
683  * Build up a request structure for reading volumes.
684  * This function is not needed for plex reads, since there's
685  * no recovery if a plex read can't be satisified.
686  */
687 enum requeststatus
688 build_read_request(struct request *rq,			    /* request */
689     int plexindex)
690 {							    /* index in the volume's plex table */
691     struct bio *bio;
692     struct buf *bp;
693     vinum_off_t startaddr;					    /* offset of previous part of transfer */
694     vinum_off_t diskaddr;					    /* offset of current part of transfer */
695     vinum_off_t diskend;					    /* and end offset of transfer */
696     int plexno;						    /* plex index in vinum_conf */
697     struct rqgroup *rqg;				    /* point to the request we're working on */
698     struct volume *vol;					    /* volume in question */
699     int recovered = 0;					    /* set if we recover a read */
700     enum requeststatus status = REQUEST_OK;
701     int plexmask;					    /* bit mask of plexes, for recovery */
702 
703     bio = rq->bio;					    /* buffer pointer */
704     bp = bio->bio_buf;
705     diskaddr = bio->bio_offset >> DEV_BSHIFT;		    /* start offset of transfer */
706     diskend = diskaddr + (bp->b_bcount / DEV_BSIZE);	    /* and end offset of transfer */
707     rqg = &rq->rqg[plexindex];				    /* plex request */
708     vol = &VOL[rq->volplex.volno];			    /* point to volume */
709 
710     while (diskaddr < diskend) {			    /* build up request components */
711 	startaddr = diskaddr;
712 	status = bre(rq, vol->plex[plexindex], &diskaddr, diskend); /* build up a request */
713 	switch (status) {
714 	case REQUEST_OK:
715 	    continue;
716 
717 	case REQUEST_RECOVERED:
718 	    /*
719 	     * XXX FIXME if we have more than one plex, and we can
720 	     * satisfy the request from another, don't use the
721 	     * recovered request, since it's more expensive.
722 	     */
723 	    recovered = 1;
724 	    break;
725 
726 	case REQUEST_ENOMEM:
727 	    return status;
728 	    /*
729 	     * If we get here, our request is not complete.  Try
730 	     * to fill in the missing parts from another plex.
731 	     * This can happen multiple times in this function,
732 	     * and we reinitialize the plex mask each time, since
733 	     * we could have a hole in our plexes.
734 	     */
735 	case REQUEST_EOF:
736 	case REQUEST_DOWN:				    /* can't access the plex */
737 	case REQUEST_DEGRADED:				    /* can't access the plex */
738 	    plexmask = ((1 << vol->plexes) - 1)		    /* all plexes in the volume */
739 	    &~(1 << plexindex);				    /* except for the one we were looking at */
740 	    for (plexno = 0; plexno < vol->plexes; plexno++) {
741 		if (plexmask == 0)			    /* no plexes left to try */
742 		    return REQUEST_DOWN;		    /* failed */
743 		diskaddr = startaddr;			    /* start at the beginning again */
744 		if (plexmask & (1 << plexno)) {		    /* we haven't tried this plex yet */
745 		    bre(rq, vol->plex[plexno], &diskaddr, diskend); /* try a request */
746 		    if (diskaddr > startaddr) {		    /* we satisfied another part */
747 			recovered = 1;			    /* we recovered from the problem */
748 			status = REQUEST_OK;		    /* don't complain about it */
749 			break;
750 		    }
751 		}
752 	    }
753 	    if (diskaddr == startaddr)			    /* didn't get any further, */
754 		return status;
755 	}
756 	if (recovered)
757 	    vol->recovered_reads += recovered;		    /* adjust our recovery count */
758     }
759     return status;
760 }
761 
762 /*
763  * Build up a request structure for writes.
764  * Return 0 if all subdisks involved in the request are up, 1 if some
765  * subdisks are not up, and -1 if the request is at least partially
766  * outside the bounds of the subdisks.
767  */
768 enum requeststatus
769 build_write_request(struct request *rq)
770 {							    /* request */
771     struct bio *bio;
772     struct buf *bp;
773     vinum_off_t diskstart;					    /* offset of current part of transfer */
774     vinum_off_t diskend;					    /* and end offset of transfer */
775     int plexno;						    /* plex index in vinum_conf */
776     struct volume *vol;					    /* volume in question */
777     enum requeststatus status;
778 
779     bio = rq->bio;					    /* buffer pointer */
780     bp = bio->bio_buf;
781     vol = &VOL[rq->volplex.volno];			    /* point to volume */
782     diskend = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT) + (bp->b_bcount / DEV_BSIZE);	    /* end offset of transfer */
783     status = REQUEST_DOWN;				    /* assume the worst */
784     for (plexno = 0; plexno < vol->plexes; plexno++) {
785 	diskstart = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT);			    /* start offset of transfer */
786 	/*
787 	 * Build requests for the plex.
788 	 * We take the best possible result here (min,
789 	 * not max): we're happy if we can write at all
790 	 */
791 	status = u64min(status,
792 		     bre(rq, vol->plex[plexno], &diskstart, diskend));
793     }
794     return status;
795 }
796 
797 /* Fill in the struct buf part of a request element. */
798 enum requeststatus
799 build_rq_buffer(struct rqelement *rqe, struct plex *plex)
800 {
801     struct sd *sd;					    /* point to subdisk */
802     struct volume *vol;
803     struct buf *bp;
804     struct buf *ubp;					    /* user (high level) buffer header */
805     struct bio *ubio;
806 
807     vol = &VOL[rqe->rqg->rq->volplex.volno];
808     sd = &SD[rqe->sdno];				    /* point to subdisk */
809     bp = &rqe->b;
810     ubio = rqe->rqg->rq->bio;				    /* pointer to user buffer header */
811     ubp = ubio->bio_buf;
812 
813     /* Initialize the buf struct */
814     /* copy these flags from user bp */
815     bp->b_flags = ubp->b_flags & (B_ORDERED | B_NOCACHE);
816     bp->b_cmd = ubp->b_cmd;
817 #ifdef VINUMDEBUG
818     if (rqe->flags & XFR_BUFLOCKED)			    /* paranoia */
819 	panic("build_rq_buffer: rqe already locked");	    /* XXX remove this when we're sure */
820 #endif
821     initbufbio(bp);
822     BUF_LOCK(bp, LK_EXCLUSIVE);				    /* and lock it */
823     BUF_KERNPROC(bp);
824     rqe->flags |= XFR_BUFLOCKED;
825     bp->b_bio1.bio_done = complete_rqe;
826     /*
827      * You'd think that we wouldn't need to even
828      * build the request buffer for a dead subdisk,
829      * but in some cases we need information like
830      * the user buffer address.  Err on the side of
831      * generosity and supply what we can.  That
832      * obviously doesn't include drive information
833      * when the drive is dead.
834      */
835     if ((rqe->flags & XFR_BAD_SUBDISK) == 0)		    /* subdisk is accessible, */
836 	bp->b_bio1.bio_driver_info = DRIVE[rqe->driveno].dev; /* drive device */
837     bp->b_bio1.bio_offset = (off_t)(rqe->sdoffset + sd->driveoffset) << DEV_BSHIFT;	/* start address */
838     bp->b_bcount = rqe->buflen << DEV_BSHIFT;		    /* number of bytes to transfer */
839     bp->b_resid = bp->b_bcount;				    /* and it's still all waiting */
840 
841     if (rqe->flags & XFR_MALLOCED) {			    /* this operation requires a malloced buffer */
842 	bp->b_data = Malloc(bp->b_bcount);		    /* get a buffer to put it in */
843 	if (bp->b_data == NULL) {			    /* failed */
844 	    abortrequest(rqe->rqg->rq, ENOMEM);
845 	    return REQUEST_ENOMEM;			    /* no memory */
846 	}
847     } else
848 	/*
849 	 * Point directly to user buffer data.  This means
850 	 * that we don't need to do anything when we have
851 	 * finished the transfer
852 	 */
853 	bp->b_data = ubp->b_data + rqe->useroffset * DEV_BSIZE;
854     /*
855      * On a recovery read, we perform an XOR of
856      * all blocks to the user buffer.  To make
857      * this work, we first clean out the buffer
858      */
859     if ((rqe->flags & (XFR_RECOVERY_READ | XFR_BAD_SUBDISK))
860 	== (XFR_RECOVERY_READ | XFR_BAD_SUBDISK)) {	    /* bad subdisk of a recovery read */
861 	int length = rqe->grouplen << DEV_BSHIFT;	    /* and count involved */
862 	char *data = (char *) &rqe->b.b_data[rqe->groupoffset << DEV_BSHIFT]; /* destination */
863 
864 	bzero(data, length);				    /* clean it out */
865     }
866     return 0;
867 }
868 
869 /*
870  * Abort a request: free resources and complete the
871  * user request with the specified error
872  */
873 int
874 abortrequest(struct request *rq, int error)
875 {
876     struct buf *bp = rq->bio->bio_buf;			    /* user buffer */
877 
878     bp->b_error = error;
879     freerq(rq);						    /* free everything we're doing */
880     bp->b_flags |= B_ERROR;
881     return error;					    /* and give up */
882 }
883 
884 /*
885  * Check that our transfer will cover the
886  * complete address space of the user request.
887  *
888  * Return 1 if it can, otherwise 0
889  */
890 int
891 check_range_covered(struct request *rq)
892 {
893     return 1;
894 }
895 
896 /* Perform I/O on a subdisk */
897 void
898 sdio(struct bio *bio)
899 {
900     cdev_t dev;
901     struct sd *sd;
902     struct sdbuf *sbp;
903     vinum_off_t endoffset;
904     struct drive *drive;
905     struct buf *bp = bio->bio_buf;
906 
907     dev = bio->bio_driver_info;
908 
909 #if VINUMDEBUG
910     if (debug & DEBUG_LASTREQS)
911 	logrq(loginfo_sdio, (union rqinfou) bio, bio);
912 #endif
913     sd = &SD[Sdno(dev)];				    /* point to the subdisk */
914     drive = &DRIVE[sd->driveno];
915 
916     if (drive->state != drive_up) {
917 	if (sd->state >= sd_crashed) {
918 	    if (bp->b_cmd != BUF_CMD_READ)		    /* writing, */
919 		set_sd_state(sd->sdno, sd_stale, setstate_force);
920 	    else
921 		set_sd_state(sd->sdno, sd_crashed, setstate_force);
922 	}
923 	bp->b_error = EIO;
924 	bp->b_flags |= B_ERROR;
925 	biodone(bio);
926 	return;
927     }
928     /*
929      * We allow access to any kind of subdisk as long as we can expect
930      * to get the I/O performed.
931      */
932     if (sd->state < sd_empty) {				    /* nothing to talk to, */
933 	bp->b_error = EIO;
934 	bp->b_flags |= B_ERROR;
935 	biodone(bio);
936 	return;
937     }
938     /* Get a buffer */
939     sbp = (struct sdbuf *) Malloc(sizeof(struct sdbuf));
940     if (sbp == NULL) {
941 	bp->b_error = ENOMEM;
942 	bp->b_flags |= B_ERROR;
943 	biodone(bio);
944 	return;
945     }
946     bzero(sbp, sizeof(struct sdbuf));			    /* start with nothing */
947     sbp->b.b_cmd = bp->b_cmd;
948     sbp->b.b_bcount = bp->b_bcount;			    /* number of bytes to transfer */
949     sbp->b.b_resid = bp->b_resid;			    /* and amount waiting */
950     sbp->b.b_data = bp->b_data;				    /* data buffer */
951     initbufbio(&sbp->b);
952     BUF_LOCK(&sbp->b, LK_EXCLUSIVE);			    /* and lock it */
953     BUF_KERNPROC(&sbp->b);
954     sbp->b.b_bio1.bio_offset = bio->bio_offset + ((off_t)sd->driveoffset << DEV_BSHIFT);
955     sbp->b.b_bio1.bio_done = sdio_done;			    /* come here on completion */
956     sbp->b.b_bio1.bio_flags |= BIO_SYNC;
957     sbp->bio = bio;					    /* note the address of the original header */
958     sbp->sdno = sd->sdno;				    /* note for statistics */
959     sbp->driveno = sd->driveno;
960     endoffset = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT) + sbp->b.b_bcount / DEV_BSIZE;  /* final sector offset */
961     if (endoffset > sd->sectors) {			    /* beyond the end */
962 	sbp->b.b_bcount -= (endoffset - sd->sectors) * DEV_BSIZE; /* trim */
963 	if (sbp->b.b_bcount <= 0) {			    /* nothing to transfer */
964 	    bp->b_resid = bp->b_bcount;			    /* nothing transferred */
965 	    biodone(bio);
966 	    BUF_UNLOCK(&sbp->b);
967 	    uninitbufbio(&sbp->b);
968 	    Free(sbp);
969 	    return;
970 	}
971     }
972 #if VINUMDEBUG
973     if (debug & DEBUG_ADDRESSES)
974 	log(LOG_DEBUG,
975 	    "  %s dev %s, sd %d, offset 0x%llx, devoffset 0x%llx, length %d\n",
976 	    (sbp->b.b_cmd == BUF_CMD_READ) ? "Read" : "Write",
977 	    drive->devicename,
978 	    sbp->sdno,
979 	    sbp->b.b_bio1.bio_offset - ((off_t)SD[sbp->sdno].driveoffset << DEV_BSHIFT),
980 	    sbp->b.b_bio1.bio_offset,
981 	    sbp->b.b_bcount);
982 #endif
983     crit_enter();
984 #if VINUMDEBUG
985     if (debug & DEBUG_LASTREQS)
986 	logrq(loginfo_sdiol, (union rqinfou) &sbp->b.b_bio1, &sbp->b.b_bio1);
987 #endif
988     vn_strategy(drive->vp, &sbp->b.b_bio1);
989     crit_exit();
990 }
991 
992 /*
993  * Determine the size of the transfer, and make sure it is
994  * within the boundaries of the partition. Adjust transfer
995  * if needed, and signal errors or early completion.
996  *
997  * Volumes are simpler than disk slices: they only contain
998  * one component (though we call them a, b and c to make
999  * system utilities happy), and they always take up the
1000  * complete space of the "partition".
1001  *
1002  * I'm still not happy with this: why should the label be
1003  * protected?  If it weren't so damned difficult to write
1004  * one in the first pleace (because it's protected), it wouldn't
1005  * be a problem.
1006  */
1007 struct bio *
1008 vinum_bounds_check(struct bio *bio, struct volume *vol)
1009 {
1010     struct buf *bp = bio->bio_buf;
1011     struct bio *nbio;
1012     vinum_off_t maxsize = vol->size;				    /* size of the partition (sectors) */
1013     int size = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT; /* size of this request (sectors) */
1014     vinum_off_t blkno = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT);
1015 
1016     if (size == 0)					    /* no transfer specified, */
1017 	return 0;					    /* treat as EOF */
1018     /* beyond partition? */
1019     if (bio->bio_offset < 0				    /* negative start */
1020 	|| blkno + size > maxsize) {		    /* or goes beyond the end of the partition */
1021 	/* if exactly at end of disk, return an EOF */
1022 	if (blkno == maxsize) {
1023 	    bp->b_resid = bp->b_bcount;
1024 	    return (NULL);
1025 	}
1026 	/* or truncate if part of it fits */
1027 	size = maxsize - blkno;
1028 	if (size <= 0) {				    /* nothing to transfer */
1029 	    bp->b_error = EINVAL;
1030 	    bp->b_flags |= B_ERROR;
1031 	    return (NULL);
1032 	}
1033 	bp->b_bcount = size << DEV_BSHIFT;
1034     }
1035     nbio = push_bio(bio);
1036     nbio->bio_offset = bio->bio_offset;
1037     return (nbio);
1038 }
1039 
1040 /*
1041  * Allocate a request group and hook
1042  * it in in the list for rq
1043  */
1044 struct rqgroup *
1045 allocrqg(struct request *rq, int elements)
1046 {
1047     struct rqgroup *rqg;				    /* the one we're going to allocate */
1048     int size = sizeof(struct rqgroup) + elements * sizeof(struct rqelement);
1049 
1050     rqg = (struct rqgroup *) Malloc(size);
1051     if (rqg != NULL) {					    /* malloc OK, */
1052 	if (rq->rqg)					    /* we already have requests */
1053 	    rq->lrqg->next = rqg;			    /* hang it off the end */
1054 	else						    /* first request */
1055 	    rq->rqg = rqg;				    /* at the start */
1056 	rq->lrqg = rqg;					    /* this one is the last in the list */
1057 
1058 	bzero(rqg, size);				    /* no old junk */
1059 	rqg->rq = rq;					    /* point back to the parent request */
1060 	rqg->count = elements;				    /* number of requests in the group */
1061 	rqg->lockbase = -1;				    /* no lock required yet */
1062     }
1063     return rqg;
1064 }
1065 
1066 /*
1067  * Deallocate a request group out of a chain.  We do
1068  * this by linear search: the chain is short, this
1069  * almost never happens, and currently it can only
1070  * happen to the first member of the chain.
1071  */
1072 void
1073 deallocrqg(struct rqgroup *rqg)
1074 {
1075     struct rqgroup *rqgc = rqg->rq->rqg;		    /* point to the request chain */
1076 
1077     if (rqg->lock)					    /* got a lock? */
1078 	unlockrange(rqg->plexno, rqg->lock);		    /* yes, free it */
1079     if (rqgc == rqg)					    /* we're first in line */
1080 	rqg->rq->rqg = rqg->next;			    /* unhook ourselves */
1081     else {
1082 	while ((rqgc->next != NULL)			    /* find the group */
1083 	&&(rqgc->next != rqg))
1084 	    rqgc = rqgc->next;
1085 	if (rqgc->next == NULL)
1086 	    log(LOG_ERR,
1087 		"vinum deallocrqg: rqg %p not found in request %p\n",
1088 		rqg->rq,
1089 		rqg);
1090 	else
1091 	    rqgc->next = rqg->next;			    /* make the chain jump over us */
1092     }
1093     Free(rqg);
1094 }
1095 
1096 /* Local Variables: */
1097 /* fill-column: 50 */
1098 /* End: */
1099