kernel: Remove some unused variables in RAID and disk drivers.
[dragonfly.git] / sys / dev / raid / vinum / vinumrequest.c
CommitLineData
984263bc
MD
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
1f2de5d4
MD
44#include "vinumhdr.h"
45#include "request.h"
984263bc
MD
46#include <sys/resourcevar.h>
47
48enum requeststatus bre(struct request *rq,
49 int plexno,
c2fcd54e
MD
50 vinum_off_t * diskstart,
51 vinum_off_t diskend);
984263bc
MD
52enum requeststatus bre5(struct request *rq,
53 int plexno,
c2fcd54e
MD
54 vinum_off_t * diskstart,
55 vinum_off_t diskend);
984263bc
MD
56enum requeststatus build_read_request(struct request *rq, int volplexno);
57enum requeststatus build_write_request(struct request *rq);
58enum requeststatus build_rq_buffer(struct rqelement *rqe, struct plex *plex);
59int find_alternate_sd(struct request *rq);
60int check_range_covered(struct request *);
81b5c339 61void complete_rqe(struct bio *bio);
984263bc
MD
62void complete_raid5_write(struct rqelement *);
63int abortrequest(struct request *rq, int error);
81b5c339
MD
64void sdio_done(struct bio *bio);
65struct bio *vinum_bounds_check(struct bio *bio, struct volume *vol);
984263bc
MD
66caddr_t allocdatabuf(struct rqelement *rqe);
67void freedatabuf(struct rqelement *rqe);
68
69#ifdef VINUMDEBUG
70struct rqinfo rqinfo[RQINFO_SIZE];
71struct rqinfo *rqip = rqinfo;
72
73void
81b5c339 74logrq(enum rqinfo_type type, union rqinfou info, struct bio *ubio)
984263bc 75{
b13267a5 76 cdev_t dev;
81b5c339 77
407c6ab2 78 crit_enter();
984263bc
MD
79
80 microtime(&rqip->timestamp); /* when did this happen? */
81 rqip->type = type;
81b5c339
MD
82 rqip->bio = ubio; /* user buffer */
83
984263bc
MD
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 */
81b5c339
MD
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);
984263bc
MD
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));
81b5c339
MD
101 dev = info.rqe->b.b_bio1.bio_driver_info;
102 rqip->devmajor = major(dev);
103 rqip->devminor = minor(dev);
984263bc
MD
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;
407c6ab2 119 crit_exit();
984263bc
MD
120}
121
122#endif
123
fef8985e
MD
124int
125vinumstrategy(struct dev_strategy_args *ap)
984263bc 126{
b13267a5 127 cdev_t dev = ap->a_head.a_dev;
fef8985e 128 struct bio *bio = ap->a_bio;
81b5c339
MD
129 struct buf *bp = bio->bio_buf;
130 struct bio *nbio = bio;
984263bc 131 struct volume *vol = NULL;
81b5c339 132 int volno;
984263bc 133
81b5c339 134 switch (DEVTYPE(dev)) {
984263bc
MD
135 case VINUM_SD_TYPE:
136 case VINUM_RAWSD_TYPE:
81b5c339
MD
137 bio->bio_driver_info = dev;
138 sdio(bio);
fef8985e
MD
139 break;
140 case VINUM_DRIVE_TYPE:
141 default:
984263bc
MD
142 /*
143 * In fact, vinum doesn't handle drives: they're
144 * handled directly by the disk drivers
145 */
984263bc
MD
146 bp->b_error = EIO; /* I/O error */
147 bp->b_flags |= B_ERROR;
81b5c339 148 biodone(bio);
fef8985e 149 break;
984263bc
MD
150
151 case VINUM_VOLUME_TYPE: /* volume I/O */
81b5c339 152 volno = Volno(dev);
984263bc
MD
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;
81b5c339 157 biodone(bio);
fef8985e 158 break;
984263bc 159 }
81b5c339
MD
160 nbio = vinum_bounds_check(bio, vol);
161 if (nbio == NULL) {
162 biodone(bio);
fef8985e 163 break;
984263bc
MD
164 }
165 /* FALLTHROUGH */
fef8985e
MD
166 case VINUM_PLEX_TYPE:
167 case VINUM_RAWPLEX_TYPE:
984263bc
MD
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 */
984263bc 173 bp->b_resid = bp->b_bcount; /* transfer everything */
81b5c339 174 vinumstart(dev, nbio, 0);
fef8985e 175 break;
984263bc 176 }
fef8985e 177 return(0);
984263bc
MD
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 */
190int
b13267a5 191vinumstart(cdev_t dev, struct bio *bio, int reviveok)
984263bc 192{
81b5c339 193 struct buf *bp = bio->bio_buf;
984263bc 194 int plexno;
984263bc
MD
195 struct volume *vol;
196 struct request *rq; /* build up our request here */
197 enum requeststatus status;
198
81b5c339
MD
199 bio->bio_driver_info = dev;
200
984263bc
MD
201#if VINUMDEBUG
202 if (debug & DEBUG_LASTREQS)
81b5c339 203 logrq(loginfo_user_bp, (union rqinfou) bio, bio);
984263bc
MD
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;
81b5c339 209 biodone(bio);
984263bc
MD
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;
81b5c339 216 biodone(bio);
984263bc
MD
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 */
81b5c339 226 rq->bio = bio; /* and the user buffer struct */
984263bc 227
81b5c339
MD
228 if (DEVTYPE(dev) == VINUM_VOLUME_TYPE) { /* it's a volume, */
229 rq->volplex.volno = Volno(dev); /* get the volume number */
984263bc
MD
230 vol = &VOL[rq->volplex.volno]; /* and point to it */
231 vol->active++; /* one more active request */
984263bc
MD
232 } else {
233 vol = NULL; /* no volume */
81b5c339 234 rq->volplex.plexno = Plexno(dev); /* point to the plex */
984263bc 235 rq->isplex = 1; /* note that it's a plex */
984263bc
MD
236 }
237
10f3fee5 238 if (bp->b_cmd == BUF_CMD_READ) {
984263bc
MD
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 {
c2fcd54e 259 vinum_off_t diskaddr = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT);
54078292 260 /* start offset of transfer */
984263bc
MD
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 }
81b5c339 272 biodone(bio);
984263bc
MD
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 */
c2fcd54e
MD
286 vinum_off_t diskstart;
287 vinum_off_t diskend;
984263bc 288
c2fcd54e 289 diskstart = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT); /* start offset of transfer */
54078292
MD
290 diskend = diskstart + bp->b_bcount / DEV_BSIZE;
291 status = bre(rq, Plexno(dev),
292 &diskstart, diskend); /* build requests for the plex */
984263bc
MD
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 }
81b5c339 299 biodone(bio);
984263bc
MD
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 */
311int
312launch_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 */
984263bc
MD
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#if VINUMDEBUG
81b5c339 342 if (debug & DEBUG_REVIVECONFLICT) {
984263bc 343 log(LOG_DEBUG,
591bdbe9 344 "Revive conflict sd %d: %p\n%s dev %d.%d, offset 0x%llx, length %d\n",
984263bc
MD
345 rq->sdno,
346 rq,
10f3fee5 347 (rq->bio->bio_buf->b_cmd & BUF_CMD_READ) ? "Read" : "Write",
b13267a5
MD
348 major(((cdev_t)rq->bio->bio_driver_info)),
349 minor(((cdev_t)rq->bio->bio_driver_info)),
54078292 350 rq->bio->bio_offset,
81b5c339
MD
351 rq->bio->bio_buf->b_bcount);
352 }
984263bc
MD
353#endif
354 return 0; /* and get out of here */
355 }
356 rq->active = 0; /* nothing yet */
357#if VINUMDEBUG
358 if (debug & DEBUG_ADDRESSES)
359 log(LOG_DEBUG,
591bdbe9 360 "Request: %p\n%s dev %d.%d, offset 0x%llx, length %d\n",
984263bc 361 rq,
10f3fee5 362 (rq->bio->bio_buf->b_cmd == BUF_CMD_READ) ? "Read" : "Write",
b13267a5
MD
363 major(((cdev_t)rq->bio->bio_driver_info)),
364 minor(((cdev_t)rq->bio->bio_driver_info)),
54078292 365 rq->bio->bio_offset,
81b5c339 366 rq->bio->bio_buf->b_bcount);
984263bc 367 vinum_conf.lastrq = rq;
81b5c339 368 vinum_conf.lastbio = rq->bio;
984263bc 369 if (debug & DEBUG_LASTREQS)
81b5c339 370 logrq(loginfo_user_bpl, (union rqinfou) rq->bio, rq->bio);
984263bc
MD
371#endif
372
984263bc
MD
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
407c6ab2 392 * before we finish, so we need critical section protection.
984263bc 393 */
407c6ab2 394 crit_enter();
984263bc
MD
395 for (rqg = rq->rqg; rqg != NULL;) { /* through the whole request chain */
396 if (rqg->lockbase >= 0) /* this rqg needs a lock first */
81b5c339 397 rqg->lock = lockrange(rqg->lockbase, rqg->rq->bio->bio_buf, &PLEX[rqg->plexno]);
984263bc
MD
398 rcount = rqg->count;
399 for (rqno = 0; rqno < rcount;) {
b13267a5 400 cdev_t dev;
81b5c339 401
984263bc
MD
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
81b5c339 419 dev = rqe->b.b_bio1.bio_driver_info;
984263bc
MD
420#ifdef VINUMDEBUG
421 if (debug & DEBUG_ADDRESSES)
422 log(LOG_DEBUG,
591bdbe9 423 " %s dev %d.%d, sd %d, offset 0x%llx, devoffset 0x%llx, length %d\n",
10f3fee5 424 (rqe->b.b_cmd == BUF_CMD_READ) ? "Read" : "Write",
81b5c339
MD
425 major(dev),
426 minor(dev),
984263bc 427 rqe->sdno,
54078292
MD
428 rqe->b.b_bio1.bio_offset - ((off_t)SD[rqe->sdno].driveoffset << DEV_BSHIFT),
429 rqe->b.b_bio1.bio_offset,
984263bc
MD
430 rqe->b.b_bcount);
431 if (debug & DEBUG_LASTREQS)
81b5c339 432 logrq(loginfo_rqe, (union rqinfou) rqe, rq->bio);
984263bc
MD
433#endif
434 /* fire off the request */
c1a85d27 435 /* XXX this had better not be a low level drive */
81b5c339 436 dev_dstrategy(dev, &rqe->b.b_bio1);
984263bc
MD
437 }
438 }
439 }
407c6ab2 440 crit_exit();
984263bc
MD
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 */
463enum requeststatus
464bre(struct request *rq,
465 int plexno,
c2fcd54e
MD
466 vinum_off_t * diskaddr,
467 vinum_off_t diskend)
984263bc
MD
468{
469 int sdno;
470 struct sd *sd;
471 struct rqgroup *rqg;
81b5c339 472 struct bio *bio;
984263bc
MD
473 struct buf *bp; /* user's bp */
474 struct plex *plex;
475 enum requeststatus status; /* return value */
c2fcd54e
MD
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 */
984263bc 480 struct rqelement *rqe; /* point to this request information */
c2fcd54e 481 vinum_off_t diskstart = *diskaddr; /* remember where this transfer starts */
984263bc
MD
482 enum requeststatus s; /* temp return value */
483
81b5c339
MD
484 bio = rq->bio; /* buffer pointer */
485 bp = bio->bio_buf;
984263bc
MD
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;
0d0607d0
MD
512 rqe->datalen = u64min(diskend - *diskaddr,
513 sd->sectors - rqe->sdoffset);
984263bc
MD
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 */
10f3fee5 523 if (rq->bio->bio_buf->b_cmd == BUF_CMD_READ) /* read request, */
984263bc
MD
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;
0d0607d0
MD
592 rqe->datalen = u64min(diskend - *diskaddr,
593 plex->stripesize - blockoffset);
984263bc
MD
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 */
10f3fee5 605 if (rq->bio->bio_buf->b_cmd == BUF_CMD_READ) /* read request, */
984263bc
MD
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#if VINUMDEBUG
628 if (debug & DEBUG_EOFINFO) { /* tell on the request */
629 log(LOG_DEBUG,
00449ae2 630 "vinum: EOF on plex %s, sd %s offset %jx (user offset %jx)\n",
984263bc
MD
631 plex->name,
632 sd->name,
00449ae2
SW
633 (uintmax_t)sd->sectors,
634 (uintmax_t)bp->b_bio1.bio_offset);
984263bc 635 log(LOG_DEBUG,
d557216f
MD
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);
984263bc
MD
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 */
683enum requeststatus
684build_read_request(struct request *rq, /* request */
685 int plexindex)
686{ /* index in the volume's plex table */
81b5c339 687 struct bio *bio;
984263bc 688 struct buf *bp;
c2fcd54e
MD
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 */
984263bc 692 int plexno; /* plex index in vinum_conf */
984263bc
MD
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
81b5c339
MD
698 bio = rq->bio; /* buffer pointer */
699 bp = bio->bio_buf;
54078292 700 diskaddr = bio->bio_offset >> DEV_BSHIFT; /* start offset of transfer */
984263bc 701 diskend = diskaddr + (bp->b_bcount / DEV_BSIZE); /* and end offset of transfer */
984263bc
MD
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 */
762enum requeststatus
763build_write_request(struct request *rq)
764{ /* request */
81b5c339 765 struct bio *bio;
984263bc 766 struct buf *bp;
c2fcd54e
MD
767 vinum_off_t diskstart; /* offset of current part of transfer */
768 vinum_off_t diskend; /* and end offset of transfer */
984263bc
MD
769 int plexno; /* plex index in vinum_conf */
770 struct volume *vol; /* volume in question */
771 enum requeststatus status;
772
81b5c339
MD
773 bio = rq->bio; /* buffer pointer */
774 bp = bio->bio_buf;
984263bc 775 vol = &VOL[rq->volplex.volno]; /* point to volume */
c2fcd54e 776 diskend = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT) + (bp->b_bcount / DEV_BSIZE); /* end offset of transfer */
984263bc
MD
777 status = REQUEST_DOWN; /* assume the worst */
778 for (plexno = 0; plexno < vol->plexes; plexno++) {
c2fcd54e 779 diskstart = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT); /* start offset of transfer */
984263bc
MD
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 */
0d0607d0
MD
785 status = u64min(status,
786 bre(rq, vol->plex[plexno], &diskstart, diskend));
984263bc
MD
787 }
788 return status;
789}
790
791/* Fill in the struct buf part of a request element. */
792enum requeststatus
793build_rq_buffer(struct rqelement *rqe, struct plex *plex)
794{
795 struct sd *sd; /* point to subdisk */
984263bc
MD
796 struct buf *bp;
797 struct buf *ubp; /* user (high level) buffer header */
81b5c339 798 struct bio *ubio;
984263bc 799
984263bc
MD
800 sd = &SD[rqe->sdno]; /* point to subdisk */
801 bp = &rqe->b;
81b5c339
MD
802 ubio = rqe->rqg->rq->bio; /* pointer to user buffer header */
803 ubp = ubio->bio_buf;
984263bc
MD
804
805 /* Initialize the buf struct */
806 /* copy these flags from user bp */
ae8e83e6 807 bp->b_flags = ubp->b_flags & (B_ORDERED | B_NOCACHE);
10f3fee5 808 bp->b_cmd = ubp->b_cmd;
984263bc
MD
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
b5d7061d 813 initbufbio(bp);
984263bc
MD
814 BUF_LOCK(bp, LK_EXCLUSIVE); /* and lock it */
815 BUF_KERNPROC(bp);
816 rqe->flags |= XFR_BUFLOCKED;
81b5c339 817 bp->b_bio1.bio_done = complete_rqe;
984263bc
MD
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, */
81b5c339 828 bp->b_bio1.bio_driver_info = DRIVE[rqe->driveno].dev; /* drive device */
54078292 829 bp->b_bio1.bio_offset = (off_t)(rqe->sdoffset + sd->driveoffset) << DEV_BSHIFT; /* start address */
984263bc
MD
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 */
984263bc
MD
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 */
865int
866abortrequest(struct request *rq, int error)
867{
81b5c339 868 struct buf *bp = rq->bio->bio_buf; /* user buffer */
984263bc
MD
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 */
882int
883check_range_covered(struct request *rq)
884{
885 return 1;
886}
887
888/* Perform I/O on a subdisk */
889void
81b5c339 890sdio(struct bio *bio)
984263bc 891{
b13267a5 892 cdev_t dev;
984263bc
MD
893 struct sd *sd;
894 struct sdbuf *sbp;
c2fcd54e 895 vinum_off_t endoffset;
984263bc 896 struct drive *drive;
81b5c339
MD
897 struct buf *bp = bio->bio_buf;
898
899 dev = bio->bio_driver_info;
984263bc
MD
900
901#if VINUMDEBUG
902 if (debug & DEBUG_LASTREQS)
81b5c339 903 logrq(loginfo_sdio, (union rqinfou) bio, bio);
984263bc 904#endif
81b5c339 905 sd = &SD[Sdno(dev)]; /* point to the subdisk */
984263bc
MD
906 drive = &DRIVE[sd->driveno];
907
908 if (drive->state != drive_up) {
909 if (sd->state >= sd_crashed) {
10f3fee5 910 if (bp->b_cmd != BUF_CMD_READ) /* writing, */
984263bc
MD
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;
81b5c339 917 biodone(bio);
984263bc
MD
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;
81b5c339 927 biodone(bio);
984263bc
MD
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;
81b5c339 935 biodone(bio);
984263bc
MD
936 return;
937 }
938 bzero(sbp, sizeof(struct sdbuf)); /* start with nothing */
4ef09ff8 939 sbp->b.b_cmd = bp->b_cmd;
984263bc
MD
940 sbp->b.b_bcount = bp->b_bcount; /* number of bytes to transfer */
941 sbp->b.b_resid = bp->b_resid; /* and amount waiting */
984263bc 942 sbp->b.b_data = bp->b_data; /* data buffer */
b5d7061d 943 initbufbio(&sbp->b);
984263bc
MD
944 BUF_LOCK(&sbp->b, LK_EXCLUSIVE); /* and lock it */
945 BUF_KERNPROC(&sbp->b);
54078292 946 sbp->b.b_bio1.bio_offset = bio->bio_offset + ((off_t)sd->driveoffset << DEV_BSHIFT);
81b5c339 947 sbp->b.b_bio1.bio_done = sdio_done; /* come here on completion */
ae8e83e6 948 sbp->b.b_bio1.bio_flags |= BIO_SYNC;
81b5c339 949 sbp->bio = bio; /* note the address of the original header */
984263bc
MD
950 sbp->sdno = sd->sdno; /* note for statistics */
951 sbp->driveno = sd->driveno;
c2fcd54e 952 endoffset = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT) + sbp->b.b_bcount / DEV_BSIZE; /* final sector offset */
984263bc
MD
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 */
81b5c339 957 biodone(bio);
984263bc 958 BUF_UNLOCK(&sbp->b);
b5d7061d 959 uninitbufbio(&sbp->b);
984263bc
MD
960 Free(sbp);
961 return;
962 }
963 }
964#if VINUMDEBUG
965 if (debug & DEBUG_ADDRESSES)
966 log(LOG_DEBUG,
c1a85d27 967 " %s dev %s, sd %d, offset 0x%llx, devoffset 0x%llx, length %d\n",
10f3fee5 968 (sbp->b.b_cmd == BUF_CMD_READ) ? "Read" : "Write",
c1a85d27 969 drive->devicename,
984263bc 970 sbp->sdno,
54078292
MD
971 sbp->b.b_bio1.bio_offset - ((off_t)SD[sbp->sdno].driveoffset << DEV_BSHIFT),
972 sbp->b.b_bio1.bio_offset,
984263bc
MD
973 sbp->b.b_bcount);
974#endif
407c6ab2 975 crit_enter();
984263bc
MD
976#if VINUMDEBUG
977 if (debug & DEBUG_LASTREQS)
81b5c339 978 logrq(loginfo_sdiol, (union rqinfou) &sbp->b.b_bio1, &sbp->b.b_bio1);
984263bc 979#endif
c1a85d27 980 vn_strategy(drive->vp, &sbp->b.b_bio1);
407c6ab2 981 crit_exit();
984263bc
MD
982}
983
984/*
984263bc
MD
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 */
81b5c339
MD
999struct bio *
1000vinum_bounds_check(struct bio *bio, struct volume *vol)
984263bc 1001{
81b5c339
MD
1002 struct buf *bp = bio->bio_buf;
1003 struct bio *nbio;
c2fcd54e 1004 vinum_off_t maxsize = vol->size; /* size of the partition (sectors) */
984263bc 1005 int size = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT; /* size of this request (sectors) */
c2fcd54e 1006 vinum_off_t blkno = (vinum_off_t)(bio->bio_offset >> DEV_BSHIFT);
984263bc 1007
984263bc
MD
1008 if (size == 0) /* no transfer specified, */
1009 return 0; /* treat as EOF */
1010 /* beyond partition? */
54078292
MD
1011 if (bio->bio_offset < 0 /* negative start */
1012 || blkno + size > maxsize) { /* or goes beyond the end of the partition */
984263bc 1013 /* if exactly at end of disk, return an EOF */
54078292 1014 if (blkno == maxsize) {
984263bc 1015 bp->b_resid = bp->b_bcount;
81b5c339 1016 return (NULL);
984263bc
MD
1017 }
1018 /* or truncate if part of it fits */
54078292 1019 size = maxsize - blkno;
984263bc
MD
1020 if (size <= 0) { /* nothing to transfer */
1021 bp->b_error = EINVAL;
1022 bp->b_flags |= B_ERROR;
81b5c339 1023 return (NULL);
984263bc
MD
1024 }
1025 bp->b_bcount = size << DEV_BSHIFT;
1026 }
81b5c339 1027 nbio = push_bio(bio);
54078292 1028 nbio->bio_offset = bio->bio_offset;
81b5c339 1029 return (nbio);
984263bc
MD
1030}
1031
1032/*
1033 * Allocate a request group and hook
1034 * it in in the list for rq
1035 */
1036struct rqgroup *
1037allocrqg(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 */
1064void
1065deallocrqg(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}