1 /* $NetBSD: rf_map.c,v 1.53 2026/10/24 00:20:24 tls Exp $ */ 2 /* 3 * Copyright (c) 1995 Carnegie-Mellon University. 4 * All rights reserved. 5 * 6 * Author: Mark Holland 7 * 8 * Permission to use, copy, modify and distribute this software and 9 * its documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 16 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie the 26 * rights to redistribute these changes. 27 */ 28 29 /************************************************************************** 30 * 31 * map.c -- main code for mapping RAID addresses to physical disk addresses 32 * 33 **************************************************************************/ 34 35 #include <sys/cdefs.h> 36 __KERNEL_RCSID(0, "$NetBSD: rf_map.c,v 1.53 2026/10/24 00:20:24 tls Exp $"); 37 38 #include <dev/raidframe/raidframevar.h> 39 40 #include "rf_threadstuff.h" 41 #include "rf_raid.h" 42 #include "rf_general.h" 43 #include "rf_map.h" 44 #include "rf_shutdown.h" 45 46 static void rf_FreePDAList(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *pda_list); 47 static void rf_FreeASMList(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asm_list); 48 49 /*************************************************************************** 50 * 51 * MapAccess -- main 1st order mapping routine. Maps an access in the 52 * RAID address space to the corresponding set of physical disk 53 * addresses. The result is returned as a list of AccessStripeMap 54 * structures, one per stripe accessed. Each ASM structure contains a 55 * pointer to a list of PhysDiskAddr structures, which describe the 56 * physical locations touched by the user access. Note that this 57 * routine returns only static mapping information, i.e. the list of 58 * physical addresses returned does not necessarily identify the set 59 * of physical locations that will actually be read or written. The 60 * routine also maps the parity. The physical disk location returned 61 * always indicates the entire parity unit, even when only a subset of 62 * it is being accessed. This is because an access that is not stripe 63 * unit aligned but that spans a stripe unit boundary may require 64 * access two distinct portions of the parity unit, and we can't yet 65 * tell which portion(s) we'll actually need. We leave it up to the 66 * algorithm selection code to decide what subset of the parity unit 67 * to access. Note that addresses in the RAID address space must 68 * always be maintained as longs, instead of ints. 69 * 70 * This routine returns NULL if numBlocks is 0 71 * 72 * raidAddress - starting address in RAID address space 73 * numBlocks - number of blocks in RAID address space to access 74 * buffer - buffer to supply/receive data 75 * remap - 1 => remap address to spare space 76 ***************************************************************************/ 77 78 RF_AccessStripeMapHeader_t * 79 rf_MapAccess(RF_Raid_t *raidPtr, RF_RaidAddr_t raidAddress, 80 RF_SectorCount_t numBlocks, void *buffer, int remap) 81 { 82 RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout); 83 RF_AccessStripeMapHeader_t *asm_hdr = NULL; 84 RF_AccessStripeMap_t *asm_list = NULL, *asm_p = NULL; 85 int faultsTolerated = layoutPtr->map->faultsTolerated; 86 /* we'll change raidAddress along the way */ 87 RF_RaidAddr_t startAddress = raidAddress; 88 RF_RaidAddr_t endAddress = raidAddress + numBlocks; 89 RF_RaidDisk_t *disks = raidPtr->Disks; 90 RF_PhysDiskAddr_t *pda_p; 91 #if (RF_INCLUDE_DECL_PQ > 0) || (RF_INCLUDE_RAID6 > 0) 92 RF_PhysDiskAddr_t *pda_q; 93 #endif 94 RF_StripeCount_t numStripes = 0; 95 RF_RaidAddr_t stripeRealEndAddress, stripeEndAddress, 96 nextStripeUnitAddress; 97 RF_RaidAddr_t startAddrWithinStripe, lastRaidAddr; 98 RF_StripeCount_t totStripes; 99 RF_StripeNum_t stripeID, lastSID, SUID, lastSUID; 100 RF_AccessStripeMap_t *asmList, *t_asm; 101 RF_PhysDiskAddr_t *pdaList, *t_pda; 102 103 /* allocate all the ASMs and PDAs up front */ 104 lastRaidAddr = raidAddress + numBlocks - 1; 105 stripeID = rf_RaidAddressToStripeID(layoutPtr, raidAddress); 106 lastSID = rf_RaidAddressToStripeID(layoutPtr, lastRaidAddr); 107 totStripes = lastSID - stripeID + 1; 108 SUID = rf_RaidAddressToStripeUnitID(layoutPtr, raidAddress); 109 lastSUID = rf_RaidAddressToStripeUnitID(layoutPtr, lastRaidAddr); 110 111 asmList = rf_AllocASMList(raidPtr, totStripes); 112 113 /* may also need pda(s) per stripe for parity */ 114 pdaList = rf_AllocPDAList(raidPtr, lastSUID - SUID + 1 + 115 faultsTolerated * totStripes); 116 117 118 if (raidAddress + numBlocks > raidPtr->totalSectors) { 119 RF_ERRORMSG1("Unable to map access because offset (%d) was invalid\n", 120 (int) raidAddress); 121 return (NULL); 122 } 123 #if RF_DEBUG_MAP 124 if (rf_mapDebug) 125 rf_PrintRaidAddressInfo(raidPtr, raidAddress, numBlocks); 126 #endif 127 for (; raidAddress < endAddress;) { 128 /* make the next stripe structure */ 129 RF_ASSERT(asmList); 130 t_asm = asmList; 131 asmList = asmList->next; 132 memset(t_asm, 0, sizeof(*t_asm)); 133 if (!asm_p) 134 asm_list = asm_p = t_asm; 135 else { 136 asm_p->next = t_asm; 137 asm_p = asm_p->next; 138 } 139 numStripes++; 140 141 /* map SUs from current location to the end of the stripe */ 142 asm_p->stripeID = /* rf_RaidAddressToStripeID(layoutPtr, 143 raidAddress) */ stripeID++; 144 stripeRealEndAddress = rf_RaidAddressOfNextStripeBoundary(layoutPtr, raidAddress); 145 stripeEndAddress = RF_MIN(endAddress, stripeRealEndAddress); 146 asm_p->raidAddress = raidAddress; 147 asm_p->endRaidAddress = stripeEndAddress; 148 149 /* map each stripe unit in the stripe */ 150 pda_p = NULL; 151 152 /* Raid addr of start of portion of access that is 153 within this stripe */ 154 startAddrWithinStripe = raidAddress; 155 156 for (; raidAddress < stripeEndAddress;) { 157 RF_ASSERT(pdaList); 158 t_pda = pdaList; 159 pdaList = pdaList->next; 160 memset(t_pda, 0, sizeof(*t_pda)); 161 if (!pda_p) 162 asm_p->physInfo = pda_p = t_pda; 163 else { 164 pda_p->next = t_pda; 165 pda_p = pda_p->next; 166 } 167 168 pda_p->type = RF_PDA_TYPE_DATA; 169 (layoutPtr->map->MapSector) (raidPtr, raidAddress, 170 &(pda_p->col), 171 &(pda_p->startSector), 172 remap); 173 174 /* mark any failures we find. failedPDA is 175 * don't-care if there is more than one 176 * failure */ 177 178 /* the RAID address corresponding to this 179 physical diskaddress */ 180 pda_p->raidAddress = raidAddress; 181 nextStripeUnitAddress = rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr, raidAddress); 182 pda_p->numSector = RF_MIN(endAddress, nextStripeUnitAddress) - raidAddress; 183 RF_ASSERT(pda_p->numSector != 0); 184 rf_ASMCheckStatus(raidPtr, pda_p, asm_p, disks, 0); 185 pda_p->bufPtr = (char *)buffer + rf_RaidAddressToByte(raidPtr, (raidAddress - startAddress)); 186 asm_p->totalSectorsAccessed += pda_p->numSector; 187 asm_p->numStripeUnitsAccessed++; 188 189 raidAddress = RF_MIN(endAddress, nextStripeUnitAddress); 190 } 191 192 /* Map the parity. At this stage, the startSector and 193 * numSector fields for the parity unit are always set 194 * to indicate the entire parity unit. We may modify 195 * this after mapping the data portion. */ 196 switch (faultsTolerated) { 197 case 0: 198 break; 199 case 1: /* single fault tolerant */ 200 RF_ASSERT(pdaList); 201 t_pda = pdaList; 202 pdaList = pdaList->next; 203 memset(t_pda, 0, sizeof(*t_pda)); 204 pda_p = asm_p->parityInfo = t_pda; 205 pda_p->type = RF_PDA_TYPE_PARITY; 206 (layoutPtr->map->MapParity) (raidPtr, rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe), 207 &(pda_p->col), &(pda_p->startSector), remap); 208 pda_p->numSector = layoutPtr->sectorsPerStripeUnit; 209 /* raidAddr may be needed to find unit to redirect to */ 210 pda_p->raidAddress = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe); 211 rf_ASMCheckStatus(raidPtr, pda_p, asm_p, disks, 1); 212 rf_ASMParityAdjust(raidPtr, asm_p->parityInfo, startAddrWithinStripe, endAddress, layoutPtr, asm_p); 213 214 break; 215 #if (RF_INCLUDE_DECL_PQ > 0) || (RF_INCLUDE_RAID6 > 0) 216 case 2: /* two fault tolerant */ 217 RF_ASSERT(pdaList && pdaList->next); 218 t_pda = pdaList; 219 pdaList = pdaList->next; 220 memset(t_pda, 0, sizeof(*t_pda)); 221 pda_p = asm_p->parityInfo = t_pda; 222 pda_p->type = RF_PDA_TYPE_PARITY; 223 t_pda = pdaList; 224 pdaList = pdaList->next; 225 memset(t_pda, 0, sizeof(*t_pda)); 226 pda_q = asm_p->qInfo = t_pda; 227 pda_q->type = RF_PDA_TYPE_Q; 228 (layoutPtr->map->MapParity) (raidPtr, rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe), 229 &(pda_p->col), &(pda_p->startSector), remap); 230 (layoutPtr->map->MapQ) (raidPtr, rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe), 231 &(pda_q->col), &(pda_q->startSector), remap); 232 pda_q->numSector = pda_p->numSector = layoutPtr->sectorsPerStripeUnit; 233 /* raidAddr may be needed to find unit to redirect to */ 234 pda_p->raidAddress = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe); 235 pda_q->raidAddress = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe); 236 /* failure mode stuff */ 237 rf_ASMCheckStatus(raidPtr, pda_p, asm_p, disks, 1); 238 rf_ASMCheckStatus(raidPtr, pda_q, asm_p, disks, 1); 239 rf_ASMParityAdjust(raidPtr, asm_p->parityInfo, startAddrWithinStripe, endAddress, layoutPtr, asm_p); 240 rf_ASMParityAdjust(raidPtr, asm_p->qInfo, startAddrWithinStripe, endAddress, layoutPtr, asm_p); 241 break; 242 #endif 243 } 244 } 245 RF_ASSERT(asmList == NULL && pdaList == NULL); 246 /* make the header structure */ 247 asm_hdr = rf_AllocAccessStripeMapHeader(raidPtr); 248 RF_ASSERT(numStripes == totStripes); 249 asm_hdr->numStripes = numStripes; 250 asm_hdr->stripeMap = asm_list; 251 252 #if RF_DEBUG_MAP 253 if (rf_mapDebug) 254 rf_PrintAccessStripeMap(asm_hdr); 255 #endif 256 return (asm_hdr); 257 } 258 259 /*************************************************************************** 260 * This routine walks through an ASM list and marks the PDAs that have 261 * failed. It's called only when a disk failure causes an in-flight 262 * DAG to fail. The parity may consist of two components, but we want 263 * to use only one failedPDA pointer. Thus we set failedPDA to point 264 * to the first parity component, and rely on the rest of the code to 265 * do the right thing with this. 266 ***************************************************************************/ 267 268 void 269 rf_MarkFailuresInASMList(RF_Raid_t *raidPtr, 270 RF_AccessStripeMapHeader_t *asm_h) 271 { 272 RF_RaidDisk_t *disks = raidPtr->Disks; 273 RF_AccessStripeMap_t *asmap; 274 RF_PhysDiskAddr_t *pda; 275 276 for (asmap = asm_h->stripeMap; asmap; asmap = asmap->next) { 277 asmap->numDataFailed = 0; 278 asmap->numParityFailed = 0; 279 asmap->numQFailed = 0; 280 asmap->numFailedPDAs = 0; 281 memset(asmap->failedPDAs, 0, 282 RF_MAX_FAILED_PDA * sizeof(*asmap->failedPDAs)); 283 for (pda = asmap->physInfo; pda; pda = pda->next) { 284 if (RF_DEAD_DISK(disks[pda->col].status)) { 285 asmap->numDataFailed++; 286 asmap->failedPDAs[asmap->numFailedPDAs] = pda; 287 asmap->numFailedPDAs++; 288 } 289 } 290 pda = asmap->parityInfo; 291 if (pda && RF_DEAD_DISK(disks[pda->col].status)) { 292 asmap->numParityFailed++; 293 asmap->failedPDAs[asmap->numFailedPDAs] = pda; 294 asmap->numFailedPDAs++; 295 } 296 pda = asmap->qInfo; 297 if (pda && RF_DEAD_DISK(disks[pda->col].status)) { 298 asmap->numQFailed++; 299 asmap->failedPDAs[asmap->numFailedPDAs] = pda; 300 asmap->numFailedPDAs++; 301 } 302 } 303 } 304 305 /*************************************************************************** 306 * 307 * routines to allocate and free list elements. All allocation 308 * routines zero the structure before returning it. 309 * 310 * FreePhysDiskAddr is static. It should never be called directly, 311 * because FreeAccessStripeMap takes care of freeing the PhysDiskAddr 312 * list. 313 * 314 ***************************************************************************/ 315 316 /* called at shutdown time. So far, all that is necessary is to 317 release all the free lists */ 318 static void rf_ShutdownMapModule(void *); 319 static void 320 rf_ShutdownMapModule(void *arg) 321 { 322 RF_Raid_t *raidPtr; 323 324 raidPtr = (RF_Raid_t *) arg; 325 326 pool_destroy(&raidPtr->pools.asm_hdr); 327 pool_destroy(&raidPtr->pools.asmap); 328 pool_destroy(&raidPtr->pools.asmhle); 329 pool_destroy(&raidPtr->pools.pda); 330 pool_destroy(&raidPtr->pools.fss); 331 pool_destroy(&raidPtr->pools.vfple); 332 pool_destroy(&raidPtr->pools.vple); 333 } 334 335 int 336 rf_ConfigureMapModule(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr, 337 RF_Config_t *cfgPtr) 338 { 339 size_t stripe_data_bytes; 340 u_int max_stripes_per_bp, c, d, o, stripes, reserved_vple; 341 size_t min_asmhdr, max_asmhdr; 342 size_t min_asmap, max_asmap; 343 size_t min_asmhle, max_asmhle; 344 size_t min_pda, max_pda; 345 size_t min_fss, max_fss; 346 size_t min_vfple, max_vfple; 347 size_t min_vple, max_vple; 348 349 c = raidPtr->numCol; 350 d = raidPtr->Layout.numDataCol; 351 o = raidPtr->openings; 352 stripe_data_bytes = (size_t)raidPtr->Layout.dataSectorsPerStripe << 353 raidPtr->logBytesPerSector; 354 /* Total active stripes for 'openings' MAXPHYS I/Os + 1 parity op. */ 355 max_stripes_per_bp = howmany(MAXPHYS, stripe_data_bytes) + 1; 356 stripes = o * max_stripes_per_bp + 1; 357 /* VPLEs permanently held by pre-allocated buffer free lists. */ 358 reserved_vple = raidPtr->numReservedBuffers + 359 raidPtr->numReservedStripeBuffers; 360 361 /* 362 * Prime (xmin) for all 'openings' admitted MAXPHYS I/Os + 1 parity op 363 * so rf_RaidIOThread / rf_EngineThread never block in pool_get(PR_WAITOK) 364 * while in-flight I/O completions are queued behind them. 365 */ 366 /* Up to 3 ASMs/stripe (main + 2 recovery) + 2*d end-stripe SU splits/IO. */ 367 min_asmhdr = 3 * stripes + o * 2 * d; 368 max_asmhdr = 2 * min_asmhdr; 369 370 /* 1 ASM per ASM header in worst case, plus 1/stripe in initial map. */ 371 min_asmap = 3 * stripes + o * 2 * d; 372 max_asmap = 2 * min_asmap; 373 374 /* 1 list elem per data col on up to 2 split end stripes per opening. */ 375 min_asmhle = o * 2 * d; 376 max_asmhle = 2 * min_asmhle; 377 378 /* Up to 2*c PDAs per active stripe (data + parity + recovery). */ 379 min_pda = 2 * c * stripes; 380 max_pda = 2 * min_pda; 381 382 /* Up to 1 failed-stripe descriptor per active stripe. */ 383 min_fss = stripes; 384 max_fss = 2 * min_fss; 385 386 /* 1 callback elem per data col on up to 2 split end stripes per opening. */ 387 min_vfple = o * 2 * d; 388 max_vfple = 2 * min_vfple; 389 390 /* Reserved buffer list elems + up to c+1 active buffer elems per stripe. */ 391 min_vple = reserved_vple + stripes * (c + 1); 392 max_vple = reserved_vple + 2 * stripes * (c + 1); 393 394 rf_pool_init(raidPtr, raidPtr->poolNames.asm_hdr, &raidPtr->pools.asm_hdr, sizeof(RF_AccessStripeMapHeader_t), 395 "asmhdr", min_asmhdr, max_asmhdr); 396 rf_pool_init(raidPtr, raidPtr->poolNames.asmap, &raidPtr->pools.asmap, sizeof(RF_AccessStripeMap_t), 397 "asmap", min_asmap, max_asmap); 398 rf_pool_init(raidPtr, raidPtr->poolNames.asmhle, &raidPtr->pools.asmhle, sizeof(RF_ASMHeaderListElem_t), 399 "asmhle", min_asmhle, max_asmhle); 400 rf_pool_init(raidPtr, raidPtr->poolNames.pda, &raidPtr->pools.pda, sizeof(RF_PhysDiskAddr_t), 401 "pda", min_pda, max_pda); 402 rf_pool_init(raidPtr, raidPtr->poolNames.fss, &raidPtr->pools.fss, sizeof(RF_FailedStripe_t), 403 "fss", min_fss, max_fss); 404 rf_pool_init(raidPtr, raidPtr->poolNames.vfple, &raidPtr->pools.vfple, sizeof(RF_VoidFunctionPointerListElem_t), 405 "vfple", min_vfple, max_vfple); 406 rf_pool_init(raidPtr, raidPtr->poolNames.vple, &raidPtr->pools.vple, sizeof(RF_VoidPointerListElem_t), 407 "vple", min_vple, max_vple); 408 rf_ShutdownCreate(listp, rf_ShutdownMapModule, raidPtr); 409 410 return (0); 411 } 412 413 RF_AccessStripeMapHeader_t * 414 rf_AllocAccessStripeMapHeader(RF_Raid_t *raidPtr) 415 { 416 return pool_get(&raidPtr->pools.asm_hdr, PR_WAITOK | PR_ZERO); 417 } 418 419 void 420 rf_FreeAccessStripeMapHeader(RF_Raid_t *raidPtr, RF_AccessStripeMapHeader_t *p) 421 { 422 pool_put(&raidPtr->pools.asm_hdr, p); 423 } 424 425 426 RF_VoidFunctionPointerListElem_t * 427 rf_AllocVFPListElem(RF_Raid_t *raidPtr) 428 { 429 return pool_get(&raidPtr->pools.vfple, PR_WAITOK | PR_ZERO); 430 } 431 432 void 433 rf_FreeVFPListElem(RF_Raid_t *raidPtr, RF_VoidFunctionPointerListElem_t *p) 434 { 435 436 pool_put(&raidPtr->pools.vfple, p); 437 } 438 439 440 RF_VoidPointerListElem_t * 441 rf_AllocVPListElem(RF_Raid_t *raidPtr) 442 { 443 return pool_get(&raidPtr->pools.vple, PR_WAITOK | PR_ZERO); 444 } 445 446 void 447 rf_FreeVPListElem(RF_Raid_t *raidPtr, RF_VoidPointerListElem_t *p) 448 { 449 450 pool_put(&raidPtr->pools.vple, p); 451 } 452 453 RF_ASMHeaderListElem_t * 454 rf_AllocASMHeaderListElem(RF_Raid_t *raidPtr) 455 { 456 return pool_get(&raidPtr->pools.asmhle, PR_WAITOK | PR_ZERO); 457 } 458 459 void 460 rf_FreeASMHeaderListElem(RF_Raid_t *raidPtr, RF_ASMHeaderListElem_t *p) 461 { 462 463 pool_put(&raidPtr->pools.asmhle, p); 464 } 465 466 RF_FailedStripe_t * 467 rf_AllocFailedStripeStruct(RF_Raid_t *raidPtr) 468 { 469 return pool_get(&raidPtr->pools.fss, PR_WAITOK | PR_ZERO); 470 } 471 472 void 473 rf_FreeFailedStripeStruct(RF_Raid_t *raidPtr, RF_FailedStripe_t *p) 474 { 475 pool_put(&raidPtr->pools.fss, p); 476 } 477 478 479 480 481 482 RF_PhysDiskAddr_t * 483 rf_AllocPhysDiskAddr(RF_Raid_t *raidPtr) 484 { 485 return pool_get(&raidPtr->pools.pda, PR_WAITOK | PR_ZERO); 486 } 487 /* allocates a list of PDAs, locking the free list only once when we 488 * have to call calloc, we do it one component at a time to simplify 489 * the process of freeing the list at program shutdown. This should 490 * not be much of a performance hit, because it should be very 491 * infrequently executed. */ 492 RF_PhysDiskAddr_t * 493 rf_AllocPDAList(RF_Raid_t *raidPtr, int count) 494 { 495 RF_PhysDiskAddr_t *p, *prev; 496 int i; 497 498 p = NULL; 499 prev = NULL; 500 for (i = 0; i < count; i++) { 501 p = pool_get(&raidPtr->pools.pda, PR_WAITOK); 502 p->next = prev; 503 prev = p; 504 } 505 506 return (p); 507 } 508 509 void 510 rf_FreePhysDiskAddr(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *p) 511 { 512 pool_put(&raidPtr->pools.pda, p); 513 } 514 515 static void 516 rf_FreePDAList(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *pda_list) 517 { 518 RF_PhysDiskAddr_t *p, *tmp; 519 520 p=pda_list; 521 while (p) { 522 tmp = p->next; 523 pool_put(&raidPtr->pools.pda, p); 524 p = tmp; 525 } 526 } 527 528 /* this is essentially identical to AllocPDAList. I should combine 529 * the two. when we have to call calloc, we do it one component at a 530 * time to simplify the process of freeing the list at program 531 * shutdown. This should not be much of a performance hit, because it 532 * should be very infrequently executed. */ 533 RF_AccessStripeMap_t * 534 rf_AllocASMList(RF_Raid_t *raidPtr, int count) 535 { 536 RF_AccessStripeMap_t *p, *prev; 537 int i; 538 539 p = NULL; 540 prev = NULL; 541 for (i = 0; i < count; i++) { 542 p = pool_get(&raidPtr->pools.asmap, PR_WAITOK); 543 p->next = prev; 544 prev = p; 545 } 546 return (p); 547 } 548 549 static void 550 rf_FreeASMList(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asm_list) 551 { 552 RF_AccessStripeMap_t *p, *tmp; 553 554 p=asm_list; 555 while (p) { 556 tmp = p->next; 557 pool_put(&raidPtr->pools.asmap, p); 558 p = tmp; 559 } 560 } 561 562 void 563 rf_FreeAccessStripeMap(RF_Raid_t *raidPtr, RF_AccessStripeMapHeader_t *hdr) 564 { 565 RF_AccessStripeMap_t *p; 566 RF_PhysDiskAddr_t *pdp, *trailer, *pdaList = NULL, *pdaEnd = NULL; 567 int count = 0, t; 568 569 for (p = hdr->stripeMap; p; p = p->next) { 570 571 /* link the 3 pda lists into the accumulating pda list */ 572 573 if (!pdaList) 574 pdaList = p->qInfo; 575 else 576 pdaEnd->next = p->qInfo; 577 for (trailer = NULL, pdp = p->qInfo; pdp;) { 578 trailer = pdp; 579 pdp = pdp->next; 580 count++; 581 } 582 if (trailer) 583 pdaEnd = trailer; 584 585 if (!pdaList) 586 pdaList = p->parityInfo; 587 else 588 pdaEnd->next = p->parityInfo; 589 for (trailer = NULL, pdp = p->parityInfo; pdp;) { 590 trailer = pdp; 591 pdp = pdp->next; 592 count++; 593 } 594 if (trailer) 595 pdaEnd = trailer; 596 597 if (!pdaList) 598 pdaList = p->physInfo; 599 else 600 pdaEnd->next = p->physInfo; 601 for (trailer = NULL, pdp = p->physInfo; pdp;) { 602 trailer = pdp; 603 pdp = pdp->next; 604 count++; 605 } 606 if (trailer) 607 pdaEnd = trailer; 608 } 609 610 /* debug only */ 611 for (t = 0, pdp = pdaList; pdp; pdp = pdp->next) 612 t++; 613 RF_ASSERT(t == count); 614 615 if (pdaList) 616 rf_FreePDAList(raidPtr, pdaList); 617 rf_FreeASMList(raidPtr, hdr->stripeMap); 618 rf_FreeAccessStripeMapHeader(raidPtr, hdr); 619 } 620 /* We can't use the large write optimization if there are any failures 621 * in the stripe. In the declustered layout, there is no way to 622 * immediately determine what disks constitute a stripe, so we 623 * actually have to hunt through the stripe looking for failures. The 624 * reason we map the parity instead of just using asm->parityInfo->col 625 * is because the latter may have been already redirected to a spare 626 * drive, which would mess up the computation of the stripe offset. 627 * 628 * ASSUMES AT MOST ONE FAILURE IN THE STRIPE. */ 629 int 630 rf_CheckStripeForFailures(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap) 631 { 632 RF_RowCol_t tcol, pcol, *diskids, i; 633 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout; 634 RF_StripeCount_t stripeOffset; 635 int numFailures; 636 RF_RaidAddr_t sosAddr; 637 RF_SectorNum_t diskOffset, poffset; 638 639 /* quick out in the fault-free case. */ 640 rf_lock_mutex2(raidPtr->mutex); 641 numFailures = raidPtr->numFailures; 642 rf_unlock_mutex2(raidPtr->mutex); 643 if (numFailures == 0) 644 return (0); 645 646 sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, 647 asmap->raidAddress); 648 (layoutPtr->map->IdentifyStripe) (raidPtr, asmap->raidAddress, 649 &diskids); 650 (layoutPtr->map->MapParity) (raidPtr, asmap->raidAddress, 651 &pcol, &poffset, 0); /* get pcol */ 652 653 /* this need not be true if we've redirected the access to a 654 * spare in another row RF_ASSERT(row == testrow); */ 655 stripeOffset = 0; 656 for (i = 0; i < layoutPtr->numDataCol + layoutPtr->numParityCol; i++) { 657 if (diskids[i] != pcol) { 658 if (RF_DEAD_DISK(raidPtr->Disks[diskids[i]].status)) { 659 if (raidPtr->status != rf_rs_reconstructing) 660 return (1); 661 RF_ASSERT(raidPtr->reconControl->fcol == diskids[i]); 662 layoutPtr->map->MapSector(raidPtr, 663 sosAddr + stripeOffset * layoutPtr->sectorsPerStripeUnit, 664 &tcol, &diskOffset, 0); 665 RF_ASSERT(tcol == diskids[i]); 666 if (!rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, diskOffset)) 667 return (1); 668 asmap->flags |= RF_ASM_REDIR_LARGE_WRITE; 669 return (0); 670 } 671 stripeOffset++; 672 } 673 } 674 return (0); 675 } 676 #if (RF_INCLUDE_DECL_PQ > 0) || (RF_INCLUDE_RAID6 > 0) || (RF_INCLUDE_EVENODD >0) 677 /* 678 return the number of failed data units in the stripe. 679 */ 680 681 int 682 rf_NumFailedDataUnitsInStripe(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap) 683 { 684 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout; 685 RF_RowCol_t tcol, i; 686 RF_SectorNum_t diskOffset; 687 RF_RaidAddr_t sosAddr; 688 int numFailures; 689 690 /* quick out in the fault-free case. */ 691 rf_lock_mutex2(raidPtr->mutex); 692 numFailures = raidPtr->numFailures; 693 rf_unlock_mutex2(raidPtr->mutex); 694 if (numFailures == 0) 695 return (0); 696 numFailures = 0; 697 698 sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, 699 asmap->raidAddress); 700 for (i = 0; i < layoutPtr->numDataCol; i++) { 701 (layoutPtr->map->MapSector) (raidPtr, sosAddr + i * layoutPtr->sectorsPerStripeUnit, 702 &tcol, &diskOffset, 0); 703 if (RF_DEAD_DISK(raidPtr->Disks[tcol].status)) 704 numFailures++; 705 } 706 707 return numFailures; 708 } 709 #endif 710 711 /**************************************************************************** 712 * 713 * debug routines 714 * 715 ***************************************************************************/ 716 #if RF_DEBUG_MAP 717 void 718 rf_PrintAccessStripeMap(RF_AccessStripeMapHeader_t *asm_h) 719 { 720 rf_PrintFullAccessStripeMap(asm_h, 0); 721 } 722 #endif 723 724 /* prbuf - flag to print buffer pointers */ 725 void 726 rf_PrintFullAccessStripeMap(RF_AccessStripeMapHeader_t *asm_h, int prbuf) 727 { 728 int i; 729 RF_AccessStripeMap_t *asmap = asm_h->stripeMap; 730 RF_PhysDiskAddr_t *p; 731 printf("%d stripes total\n", (int) asm_h->numStripes); 732 for (; asmap; asmap = asmap->next) { 733 /* printf("Num failures: %d\n",asmap->numDataFailed); */ 734 /* printf("Num sectors: 735 * %d\n",(int)asmap->totalSectorsAccessed); */ 736 printf("Stripe %d (%d sectors), failures: %d data, %d parity: ", 737 (int) asmap->stripeID, 738 (int) asmap->totalSectorsAccessed, 739 (int) asmap->numDataFailed, 740 (int) asmap->numParityFailed); 741 if (asmap->parityInfo) { 742 printf("Parity [c%d s%d-%d", asmap->parityInfo->col, 743 (int) asmap->parityInfo->startSector, 744 (int) (asmap->parityInfo->startSector + 745 asmap->parityInfo->numSector - 1)); 746 if (prbuf) 747 printf(" b0x%lx", (unsigned long) asmap->parityInfo->bufPtr); 748 if (asmap->parityInfo->next) { 749 printf(", c%d s%d-%d", asmap->parityInfo->next->col, 750 (int) asmap->parityInfo->next->startSector, 751 (int) (asmap->parityInfo->next->startSector + 752 asmap->parityInfo->next->numSector - 1)); 753 if (prbuf) 754 printf(" b0x%lx", (unsigned long) asmap->parityInfo->next->bufPtr); 755 RF_ASSERT(asmap->parityInfo->next->next == NULL); 756 } 757 printf("]\n\t"); 758 } 759 for (i = 0, p = asmap->physInfo; p; p = p->next, i++) { 760 printf("SU c%d s%d-%d ", p->col, (int) p->startSector, 761 (int) (p->startSector + p->numSector - 1)); 762 if (prbuf) 763 printf("b0x%lx ", (unsigned long) p->bufPtr); 764 if (i && !(i & 1)) 765 printf("\n\t"); 766 } 767 printf("\n"); 768 p = asm_h->stripeMap->failedPDAs[0]; 769 if (asm_h->stripeMap->numDataFailed + asm_h->stripeMap->numParityFailed > 1) 770 printf("[multiple failures]\n"); 771 else 772 if (asm_h->stripeMap->numDataFailed + asm_h->stripeMap->numParityFailed > 0) 773 printf("\t[Failed PDA: c%d s%d-%d]\n", p->col, 774 (int) p->startSector, (int) (p->startSector + p->numSector - 1)); 775 } 776 } 777 778 #if RF_MAP_DEBUG 779 void 780 rf_PrintRaidAddressInfo(RF_Raid_t *raidPtr, RF_RaidAddr_t raidAddr, 781 RF_SectorCount_t numBlocks) 782 { 783 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout; 784 RF_RaidAddr_t ra, sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, raidAddr); 785 786 printf("Raid addrs of SU boundaries from start of stripe to end of access:\n\t"); 787 for (ra = sosAddr; ra <= raidAddr + numBlocks; ra += layoutPtr->sectorsPerStripeUnit) { 788 printf("%d (0x%x), ", (int) ra, (int) ra); 789 } 790 printf("\n"); 791 printf("Offset into stripe unit: %d (0x%x)\n", 792 (int) (raidAddr % layoutPtr->sectorsPerStripeUnit), 793 (int) (raidAddr % layoutPtr->sectorsPerStripeUnit)); 794 } 795 #endif 796 /* given a parity descriptor and the starting address within a stripe, 797 * range restrict the parity descriptor to touch only the correct 798 * stuff. */ 799 void 800 rf_ASMParityAdjust(RF_Raid_t *raidPtr, 801 RF_PhysDiskAddr_t *toAdjust, 802 RF_StripeNum_t startAddrWithinStripe, 803 RF_SectorNum_t endAddress, 804 RF_RaidLayout_t *layoutPtr, 805 RF_AccessStripeMap_t *asm_p) 806 { 807 RF_PhysDiskAddr_t *new_pda; 808 809 /* when we're accessing only a portion of one stripe unit, we 810 * want the parity descriptor to identify only the chunk of 811 * parity associated with the data. When the access spans 812 * exactly one stripe unit boundary and is less than a stripe 813 * unit in size, it uses two disjoint regions of the parity 814 * unit. When an access spans more than one stripe unit 815 * boundary, it uses all of the parity unit. 816 * 817 * To better handle the case where stripe units are small, we 818 * may eventually want to change the 2nd case so that if the 819 * SU size is below some threshold, we just read/write the 820 * whole thing instead of breaking it up into two accesses. */ 821 if (asm_p->numStripeUnitsAccessed == 1) { 822 int x = (startAddrWithinStripe % layoutPtr->sectorsPerStripeUnit); 823 toAdjust->startSector += x; 824 toAdjust->raidAddress += x; 825 toAdjust->numSector = asm_p->physInfo->numSector; 826 RF_ASSERT(toAdjust->numSector != 0); 827 } else 828 if (asm_p->numStripeUnitsAccessed == 2 && asm_p->totalSectorsAccessed < layoutPtr->sectorsPerStripeUnit) { 829 int x = (startAddrWithinStripe % layoutPtr->sectorsPerStripeUnit); 830 831 /* create a second pda and copy the parity map info 832 * into it */ 833 RF_ASSERT(toAdjust->next == NULL); 834 /* the following will get freed in rf_FreeAccessStripeMap() via 835 rf_FreePDAList() */ 836 new_pda = toAdjust->next = rf_AllocPhysDiskAddr(raidPtr); 837 *new_pda = *toAdjust; /* structure assignment */ 838 new_pda->next = NULL; 839 840 /* adjust the start sector & number of blocks for the 841 * first parity pda */ 842 toAdjust->startSector += x; 843 toAdjust->raidAddress += x; 844 toAdjust->numSector = rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr, startAddrWithinStripe) - startAddrWithinStripe; 845 RF_ASSERT(toAdjust->numSector != 0); 846 847 /* adjust the second pda */ 848 new_pda->numSector = endAddress - rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, endAddress); 849 /* new_pda->raidAddress = 850 * rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr, 851 * toAdjust->raidAddress); */ 852 RF_ASSERT(new_pda->numSector != 0); 853 } 854 } 855 856 /* Check if a disk has been spared or failed. If spared, redirect the 857 * I/O. If it has been failed, record it in the asm pointer. Fifth 858 * arg is whether data or parity. */ 859 void 860 rf_ASMCheckStatus(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *pda_p, 861 RF_AccessStripeMap_t *asm_p, RF_RaidDisk_t *disks, 862 int parity) 863 { 864 RF_DiskStatus_t dstatus; 865 RF_RowCol_t fcol; 866 867 dstatus = disks[pda_p->col].status; 868 869 if (dstatus == rf_ds_spared) { 870 /* if the disk has been spared, redirect access to the spare */ 871 fcol = pda_p->col; 872 pda_p->col = disks[fcol].spareCol; 873 } else 874 if (dstatus == rf_ds_dist_spared) { 875 /* ditto if disk has been spared to dist spare space */ 876 #if RF_DEBUG_MAP 877 RF_RowCol_t oc = pda_p->col; 878 RF_SectorNum_t oo = pda_p->startSector; 879 #endif 880 if (pda_p->type == RF_PDA_TYPE_DATA) 881 raidPtr->Layout.map->MapSector(raidPtr, pda_p->raidAddress, &pda_p->col, &pda_p->startSector, RF_REMAP); 882 else 883 raidPtr->Layout.map->MapParity(raidPtr, pda_p->raidAddress, &pda_p->col, &pda_p->startSector, RF_REMAP); 884 885 #if RF_DEBUG_MAP 886 if (rf_mapDebug) { 887 printf("Redirected c %d o %d -> c %d o %d\n", oc, (int) oo, 888 pda_p->col, (int) pda_p->startSector); 889 } 890 #endif 891 } else 892 if (RF_DEAD_DISK(dstatus)) { 893 /* if the disk is inaccessible, mark the 894 * failure */ 895 if (parity) 896 asm_p->numParityFailed++; 897 else { 898 asm_p->numDataFailed++; 899 } 900 asm_p->failedPDAs[asm_p->numFailedPDAs] = pda_p; 901 asm_p->numFailedPDAs++; 902 #if 0 903 switch (asm_p->numParityFailed + asm_p->numDataFailed) { 904 case 1: 905 asm_p->failedPDAs[0] = pda_p; 906 break; 907 case 2: 908 asm_p->failedPDAs[1] = pda_p; 909 default: 910 break; 911 } 912 #endif 913 } 914 /* the redirected access should never span a stripe unit boundary */ 915 RF_ASSERT(rf_RaidAddressToStripeUnitID(&raidPtr->Layout, pda_p->raidAddress) == 916 rf_RaidAddressToStripeUnitID(&raidPtr->Layout, pda_p->raidAddress + pda_p->numSector - 1)); 917 RF_ASSERT(pda_p->col != -1); 918 } 919