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