1 /* $NetBSD: rf_raid5.c,v 1.3 1999/02/05 00:06:16 oster 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@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 * rf_raid5.c -- implements RAID Level 5 32 * 33 *****************************************************************************/ 34 35 #include "rf_types.h" 36 #include "rf_raid.h" 37 #include "rf_raid5.h" 38 #include "rf_dag.h" 39 #include "rf_dagffrd.h" 40 #include "rf_dagffwr.h" 41 #include "rf_dagdegrd.h" 42 #include "rf_dagdegwr.h" 43 #include "rf_dagutils.h" 44 #include "rf_threadid.h" 45 #include "rf_general.h" 46 #include "rf_map.h" 47 #include "rf_utils.h" 48 49 typedef struct RF_Raid5ConfigInfo_s { 50 RF_RowCol_t **stripeIdentifier; /* filled in at config time and used 51 * by IdentifyStripe */ 52 } RF_Raid5ConfigInfo_t; 53 54 int 55 rf_ConfigureRAID5( 56 RF_ShutdownList_t ** listp, 57 RF_Raid_t * raidPtr, 58 RF_Config_t * cfgPtr) 59 { 60 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout; 61 RF_Raid5ConfigInfo_t *info; 62 RF_RowCol_t i, j, startdisk; 63 64 /* create a RAID level 5 configuration structure */ 65 RF_MallocAndAdd(info, sizeof(RF_Raid5ConfigInfo_t), (RF_Raid5ConfigInfo_t *), raidPtr->cleanupList); 66 if (info == NULL) 67 return (ENOMEM); 68 layoutPtr->layoutSpecificInfo = (void *) info; 69 70 RF_ASSERT(raidPtr->numRow == 1); 71 72 /* the stripe identifier must identify the disks in each stripe, IN 73 * THE ORDER THAT THEY APPEAR IN THE STRIPE. */ 74 info->stripeIdentifier = rf_make_2d_array(raidPtr->numCol, raidPtr->numCol, raidPtr->cleanupList); 75 if (info->stripeIdentifier == NULL) 76 return (ENOMEM); 77 startdisk = 0; 78 for (i = 0; i < raidPtr->numCol; i++) { 79 for (j = 0; j < raidPtr->numCol; j++) { 80 info->stripeIdentifier[i][j] = (startdisk + j) % raidPtr->numCol; 81 } 82 if ((--startdisk) < 0) 83 startdisk = raidPtr->numCol - 1; 84 } 85 86 /* fill in the remaining layout parameters */ 87 layoutPtr->numStripe = layoutPtr->stripeUnitsPerDisk; 88 layoutPtr->bytesPerStripeUnit = layoutPtr->sectorsPerStripeUnit << raidPtr->logBytesPerSector; 89 layoutPtr->numDataCol = raidPtr->numCol - 1; 90 layoutPtr->dataSectorsPerStripe = layoutPtr->numDataCol * layoutPtr->sectorsPerStripeUnit; 91 layoutPtr->numParityCol = 1; 92 layoutPtr->dataStripeUnitsPerDisk = layoutPtr->stripeUnitsPerDisk; 93 94 raidPtr->totalSectors = layoutPtr->stripeUnitsPerDisk * layoutPtr->numDataCol * layoutPtr->sectorsPerStripeUnit; 95 96 return (0); 97 } 98 99 int 100 rf_GetDefaultNumFloatingReconBuffersRAID5(RF_Raid_t * raidPtr) 101 { 102 return (20); 103 } 104 105 RF_HeadSepLimit_t 106 rf_GetDefaultHeadSepLimitRAID5(RF_Raid_t * raidPtr) 107 { 108 return (10); 109 } 110 #if !defined(__NetBSD__) && !defined(_KERNEL) 111 /* not currently used */ 112 int 113 rf_ShutdownRAID5(RF_Raid_t * raidPtr) 114 { 115 return (0); 116 } 117 #endif 118 119 void 120 rf_MapSectorRAID5( 121 RF_Raid_t * raidPtr, 122 RF_RaidAddr_t raidSector, 123 RF_RowCol_t * row, 124 RF_RowCol_t * col, 125 RF_SectorNum_t * diskSector, 126 int remap) 127 { 128 RF_StripeNum_t SUID = raidSector / raidPtr->Layout.sectorsPerStripeUnit; 129 *row = 0; 130 *col = (SUID % raidPtr->numCol); 131 *diskSector = (SUID / (raidPtr->Layout.numDataCol)) * raidPtr->Layout.sectorsPerStripeUnit + 132 (raidSector % raidPtr->Layout.sectorsPerStripeUnit); 133 } 134 135 void 136 rf_MapParityRAID5( 137 RF_Raid_t * raidPtr, 138 RF_RaidAddr_t raidSector, 139 RF_RowCol_t * row, 140 RF_RowCol_t * col, 141 RF_SectorNum_t * diskSector, 142 int remap) 143 { 144 RF_StripeNum_t SUID = raidSector / raidPtr->Layout.sectorsPerStripeUnit; 145 146 *row = 0; 147 *col = raidPtr->Layout.numDataCol - (SUID / raidPtr->Layout.numDataCol) % raidPtr->numCol; 148 *diskSector = (SUID / (raidPtr->Layout.numDataCol)) * raidPtr->Layout.sectorsPerStripeUnit + 149 (raidSector % raidPtr->Layout.sectorsPerStripeUnit); 150 } 151 152 void 153 rf_IdentifyStripeRAID5( 154 RF_Raid_t * raidPtr, 155 RF_RaidAddr_t addr, 156 RF_RowCol_t ** diskids, 157 RF_RowCol_t * outRow) 158 { 159 RF_StripeNum_t stripeID = rf_RaidAddressToStripeID(&raidPtr->Layout, addr); 160 RF_Raid5ConfigInfo_t *info = (RF_Raid5ConfigInfo_t *) raidPtr->Layout.layoutSpecificInfo; 161 162 *outRow = 0; 163 *diskids = info->stripeIdentifier[stripeID % raidPtr->numCol]; 164 } 165 166 void 167 rf_MapSIDToPSIDRAID5( 168 RF_RaidLayout_t * layoutPtr, 169 RF_StripeNum_t stripeID, 170 RF_StripeNum_t * psID, 171 RF_ReconUnitNum_t * which_ru) 172 { 173 *which_ru = 0; 174 *psID = stripeID; 175 } 176 /* select an algorithm for performing an access. Returns two pointers, 177 * one to a function that will return information about the DAG, and 178 * another to a function that will create the dag. 179 */ 180 void 181 rf_RaidFiveDagSelect( 182 RF_Raid_t * raidPtr, 183 RF_IoType_t type, 184 RF_AccessStripeMap_t * asmap, 185 RF_VoidFuncPtr * createFunc) 186 { 187 RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout); 188 RF_PhysDiskAddr_t *failedPDA = NULL; 189 RF_RowCol_t frow, fcol; 190 RF_RowStatus_t rstat; 191 int prior_recon; 192 int tid; 193 194 RF_ASSERT(RF_IO_IS_R_OR_W(type)); 195 196 if (asmap->numDataFailed + asmap->numParityFailed > 1) { 197 RF_ERRORMSG("Multiple disks failed in a single group! Aborting I/O operation.\n"); 198 /* *infoFunc = */ *createFunc = NULL; 199 return; 200 } else 201 if (asmap->numDataFailed + asmap->numParityFailed == 1) { 202 203 /* if under recon & already reconstructed, redirect 204 * the access to the spare drive and eliminate the 205 * failure indication */ 206 failedPDA = asmap->failedPDAs[0]; 207 frow = failedPDA->row; 208 fcol = failedPDA->col; 209 rstat = raidPtr->status[failedPDA->row]; 210 prior_recon = (rstat == rf_rs_reconfigured) || ( 211 (rstat == rf_rs_reconstructing) ? 212 rf_CheckRUReconstructed(raidPtr->reconControl[frow]->reconMap, failedPDA->startSector) : 0 213 ); 214 if (prior_recon) { 215 RF_RowCol_t or = failedPDA->row, oc = failedPDA->col; 216 RF_SectorNum_t oo = failedPDA->startSector; 217 218 if (layoutPtr->map->flags & RF_DISTRIBUTE_SPARE) { /* redirect to dist 219 * spare space */ 220 221 if (failedPDA == asmap->parityInfo) { 222 223 /* parity has failed */ 224 (layoutPtr->map->MapParity) (raidPtr, failedPDA->raidAddress, &failedPDA->row, 225 &failedPDA->col, &failedPDA->startSector, RF_REMAP); 226 227 if (asmap->parityInfo->next) { /* redir 2nd component, 228 * if any */ 229 RF_PhysDiskAddr_t *p = asmap->parityInfo->next; 230 RF_SectorNum_t SUoffs = p->startSector % layoutPtr->sectorsPerStripeUnit; 231 p->row = failedPDA->row; 232 p->col = failedPDA->col; 233 p->startSector = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, failedPDA->startSector) + 234 SUoffs; /* cheating: 235 * startSector is not 236 * really a RAID address */ 237 } 238 } else 239 if (asmap->parityInfo->next && failedPDA == asmap->parityInfo->next) { 240 RF_ASSERT(0); /* should not ever 241 * happen */ 242 } else { 243 244 /* data has failed */ 245 (layoutPtr->map->MapSector) (raidPtr, failedPDA->raidAddress, &failedPDA->row, 246 &failedPDA->col, &failedPDA->startSector, RF_REMAP); 247 248 } 249 250 } else { /* redirect to dedicated spare 251 * space */ 252 253 failedPDA->row = raidPtr->Disks[frow][fcol].spareRow; 254 failedPDA->col = raidPtr->Disks[frow][fcol].spareCol; 255 256 /* the parity may have two distinct 257 * components, both of which may need 258 * to be redirected */ 259 if (asmap->parityInfo->next) { 260 if (failedPDA == asmap->parityInfo) { 261 failedPDA->next->row = failedPDA->row; 262 failedPDA->next->col = failedPDA->col; 263 } else 264 if (failedPDA == asmap->parityInfo->next) { /* paranoid: should 265 * never occur */ 266 asmap->parityInfo->row = failedPDA->row; 267 asmap->parityInfo->col = failedPDA->col; 268 } 269 } 270 } 271 272 RF_ASSERT(failedPDA->col != -1); 273 274 if (rf_dagDebug || rf_mapDebug) { 275 rf_get_threadid(tid); 276 printf("[%d] Redirected type '%c' r %d c %d o %ld -> r %d c %d o %ld\n", 277 tid, type, or, oc, (long) oo, failedPDA->row, failedPDA->col, 278 (long) failedPDA->startSector); 279 } 280 asmap->numDataFailed = asmap->numParityFailed = 0; 281 } 282 } 283 /* all dags begin/end with block/unblock node therefore, hdrSucc & 284 * termAnt counts should always be 1 also, these counts should not be 285 * visible outside dag creation routines - manipulating the counts 286 * here should be removed */ 287 if (type == RF_IO_TYPE_READ) { 288 if (asmap->numDataFailed == 0) 289 *createFunc = (RF_VoidFuncPtr) rf_CreateFaultFreeReadDAG; 290 else 291 *createFunc = (RF_VoidFuncPtr) rf_CreateRaidFiveDegradedReadDAG; 292 } else { 293 294 295 /* if mirroring, always use large writes. If the access 296 * requires two distinct parity updates, always do a small 297 * write. If the stripe contains a failure but the access 298 * does not, do a small write. The first conditional 299 * (numStripeUnitsAccessed <= numDataCol/2) uses a 300 * less-than-or-equal rather than just a less-than because 301 * when G is 3 or 4, numDataCol/2 is 1, and I want 302 * single-stripe-unit updates to use just one disk. */ 303 if ((asmap->numDataFailed + asmap->numParityFailed) == 0) { 304 if (rf_suppressLocksAndLargeWrites || 305 (((asmap->numStripeUnitsAccessed <= (layoutPtr->numDataCol / 2)) && (layoutPtr->numDataCol != 1)) || 306 (asmap->parityInfo->next != NULL) || rf_CheckStripeForFailures(raidPtr, asmap))) { 307 *createFunc = (RF_VoidFuncPtr) rf_CreateSmallWriteDAG; 308 } else 309 *createFunc = (RF_VoidFuncPtr) rf_CreateLargeWriteDAG; 310 } else { 311 if (asmap->numParityFailed == 1) 312 *createFunc = (RF_VoidFuncPtr) rf_CreateNonRedundantWriteDAG; 313 else 314 if (asmap->numStripeUnitsAccessed != 1 && failedPDA->numSector != layoutPtr->sectorsPerStripeUnit) 315 *createFunc = NULL; 316 else 317 *createFunc = (RF_VoidFuncPtr) rf_CreateDegradedWriteDAG; 318 } 319 } 320 } 321