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