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