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