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