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