1 /* $NetBSD: rf_raid5.c,v 1.4 2000/01/08 22:57:30 oster Exp $ */
6 * Copyright (c) 1995 Carnegie-Mellon University.
11 * Permission to use, copy, modify and distribute this software and
12 * its documentation is hereby granted, provided that both the copyright
13 * notice and this permission notice appear in all copies of the
14 * software, derivative works or modified versions, and any portions
15 * thereof, and that both notices appear in supporting documentation.
17 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
18 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
19 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
21 * Carnegie Mellon requests users of this software to return to
23 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
24 * School of Computer Science
25 * Carnegie Mellon University
26 * Pittsburgh PA 15213-3890
28 * any improvements or extensions that they make and grant Carnegie the
29 * rights to redistribute these changes.
32 /******************************************************************************
34 * rf_raid5.c -- implements RAID Level 5
36 *****************************************************************************/
38 #include <dev/raidframe/rf_types.h>
39 #include <dev/raidframe/rf_raid.h>
40 #include <dev/raidframe/rf_raid5.h>
41 #include <dev/raidframe/rf_dag.h>
42 #include <dev/raidframe/rf_dagffrd.h>
43 #include <dev/raidframe/rf_dagffwr.h>
44 #include <dev/raidframe/rf_dagdegrd.h>
45 #include <dev/raidframe/rf_dagdegwr.h>
46 #include <dev/raidframe/rf_dagutils.h>
47 #include <dev/raidframe/rf_general.h>
48 #include <dev/raidframe/rf_map.h>
49 #include <dev/raidframe/rf_utils.h>
51 typedef struct RF_Raid5ConfigInfo_s {
52 RF_RowCol_t **stripeIdentifier; /* filled in at config time and used
53 * by IdentifyStripe */
54 } RF_Raid5ConfigInfo_t;
58 RF_ShutdownList_t ** listp,
62 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
63 RF_Raid5ConfigInfo_t *info;
64 RF_RowCol_t i, j, startdisk;
66 /* create a RAID level 5 configuration structure */
67 RF_MallocAndAdd(info, sizeof(RF_Raid5ConfigInfo_t), (RF_Raid5ConfigInfo_t *), raidPtr->cleanupList);
70 layoutPtr->layoutSpecificInfo = (void *) info;
72 RF_ASSERT(raidPtr->numRow == 1);
74 /* the stripe identifier must identify the disks in each stripe, IN
75 * THE ORDER THAT THEY APPEAR IN THE STRIPE. */
76 info->stripeIdentifier = rf_make_2d_array(raidPtr->numCol, raidPtr->numCol, raidPtr->cleanupList);
77 if (info->stripeIdentifier == NULL)
80 for (i = 0; i < raidPtr->numCol; i++) {
81 for (j = 0; j < raidPtr->numCol; j++) {
82 info->stripeIdentifier[i][j] = (startdisk + j) % raidPtr->numCol;
84 if ((--startdisk) < 0)
85 startdisk = raidPtr->numCol - 1;
88 /* fill in the remaining layout parameters */
89 layoutPtr->numStripe = layoutPtr->stripeUnitsPerDisk;
90 layoutPtr->bytesPerStripeUnit = layoutPtr->sectorsPerStripeUnit << raidPtr->logBytesPerSector;
91 layoutPtr->numDataCol = raidPtr->numCol - 1;
92 layoutPtr->dataSectorsPerStripe = layoutPtr->numDataCol * layoutPtr->sectorsPerStripeUnit;
93 layoutPtr->numParityCol = 1;
94 layoutPtr->dataStripeUnitsPerDisk = layoutPtr->stripeUnitsPerDisk;
96 raidPtr->totalSectors = layoutPtr->stripeUnitsPerDisk * layoutPtr->numDataCol * layoutPtr->sectorsPerStripeUnit;
102 rf_GetDefaultNumFloatingReconBuffersRAID5(RF_Raid_t * raidPtr)
108 rf_GetDefaultHeadSepLimitRAID5(RF_Raid_t * raidPtr)
112 #if !defined(__NetBSD__) && !defined(__FreeBSD__) && !defined(_KERNEL)
113 /* not currently used */
115 rf_ShutdownRAID5(RF_Raid_t * raidPtr)
124 RF_RaidAddr_t raidSector,
127 RF_SectorNum_t * diskSector,
130 RF_StripeNum_t SUID = raidSector / raidPtr->Layout.sectorsPerStripeUnit;
132 *col = (SUID % raidPtr->numCol);
133 *diskSector = (SUID / (raidPtr->Layout.numDataCol)) * raidPtr->Layout.sectorsPerStripeUnit +
134 (raidSector % raidPtr->Layout.sectorsPerStripeUnit);
140 RF_RaidAddr_t raidSector,
143 RF_SectorNum_t * diskSector,
146 RF_StripeNum_t SUID = raidSector / raidPtr->Layout.sectorsPerStripeUnit;
149 *col = raidPtr->Layout.numDataCol - (SUID / raidPtr->Layout.numDataCol) % raidPtr->numCol;
150 *diskSector = (SUID / (raidPtr->Layout.numDataCol)) * raidPtr->Layout.sectorsPerStripeUnit +
151 (raidSector % raidPtr->Layout.sectorsPerStripeUnit);
155 rf_IdentifyStripeRAID5(
158 RF_RowCol_t ** diskids,
159 RF_RowCol_t * outRow)
161 RF_StripeNum_t stripeID = rf_RaidAddressToStripeID(&raidPtr->Layout, addr);
162 RF_Raid5ConfigInfo_t *info = (RF_Raid5ConfigInfo_t *) raidPtr->Layout.layoutSpecificInfo;
165 *diskids = info->stripeIdentifier[stripeID % raidPtr->numCol];
169 rf_MapSIDToPSIDRAID5(
170 RF_RaidLayout_t * layoutPtr,
171 RF_StripeNum_t stripeID,
172 RF_StripeNum_t * psID,
173 RF_ReconUnitNum_t * which_ru)
178 /* select an algorithm for performing an access. Returns two pointers,
179 * one to a function that will return information about the DAG, and
180 * another to a function that will create the dag.
183 rf_RaidFiveDagSelect(
186 RF_AccessStripeMap_t * asmap,
187 RF_VoidFuncPtr * createFunc)
189 RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout);
190 RF_PhysDiskAddr_t *failedPDA = NULL;
191 RF_RowCol_t frow, fcol;
192 RF_RowStatus_t rstat;
195 RF_ASSERT(RF_IO_IS_R_OR_W(type));
197 if (asmap->numDataFailed + asmap->numParityFailed > 1) {
198 RF_ERRORMSG("Multiple disks failed in a single group! Aborting I/O operation.\n");
199 /* *infoFunc = */ *createFunc = NULL;
202 if (asmap->numDataFailed + asmap->numParityFailed == 1) {
204 /* if under recon & already reconstructed, redirect
205 * the access to the spare drive and eliminate the
206 * failure indication */
207 failedPDA = asmap->failedPDAs[0];
208 frow = failedPDA->row;
209 fcol = failedPDA->col;
210 rstat = raidPtr->status[failedPDA->row];
211 prior_recon = (rstat == rf_rs_reconfigured) || (
212 (rstat == rf_rs_reconstructing) ?
213 rf_CheckRUReconstructed(raidPtr->reconControl[frow]->reconMap, failedPDA->startSector) : 0
216 RF_RowCol_t or = failedPDA->row, oc = failedPDA->col;
217 RF_SectorNum_t oo = failedPDA->startSector;
219 if (layoutPtr->map->flags & RF_DISTRIBUTE_SPARE) { /* redirect to dist
222 if (failedPDA == asmap->parityInfo) {
224 /* parity has failed */
225 (layoutPtr->map->MapParity) (raidPtr, failedPDA->raidAddress, &failedPDA->row,
226 &failedPDA->col, &failedPDA->startSector, RF_REMAP);
228 if (asmap->parityInfo->next) { /* redir 2nd component,
230 RF_PhysDiskAddr_t *p = asmap->parityInfo->next;
231 RF_SectorNum_t SUoffs = p->startSector % layoutPtr->sectorsPerStripeUnit;
232 p->row = failedPDA->row;
233 p->col = failedPDA->col;
234 p->startSector = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, failedPDA->startSector) +
237 * really a RAID address */
240 if (asmap->parityInfo->next && failedPDA == asmap->parityInfo->next) {
241 RF_ASSERT(0); /* should not ever
245 /* data has failed */
246 (layoutPtr->map->MapSector) (raidPtr, failedPDA->raidAddress, &failedPDA->row,
247 &failedPDA->col, &failedPDA->startSector, RF_REMAP);
251 } else { /* redirect to dedicated spare
254 failedPDA->row = raidPtr->Disks[frow][fcol].spareRow;
255 failedPDA->col = raidPtr->Disks[frow][fcol].spareCol;
257 /* the parity may have two distinct
258 * components, both of which may need
259 * to be redirected */
260 if (asmap->parityInfo->next) {
261 if (failedPDA == asmap->parityInfo) {
262 failedPDA->next->row = failedPDA->row;
263 failedPDA->next->col = failedPDA->col;
265 if (failedPDA == asmap->parityInfo->next) { /* paranoid: should
267 asmap->parityInfo->row = failedPDA->row;
268 asmap->parityInfo->col = failedPDA->col;
273 RF_ASSERT(failedPDA->col != -1);
275 if (rf_dagDebug || rf_mapDebug) {
276 printf("raid%d: Redirected type '%c' r %d c %d o %ld -> r %d c %d o %ld\n",
277 raidPtr->raidid, type, or, oc,
278 (long) oo, failedPDA->row,
280 (long) failedPDA->startSector);
282 asmap->numDataFailed = asmap->numParityFailed = 0;
285 /* all dags begin/end with block/unblock node therefore, hdrSucc &
286 * termAnt counts should always be 1 also, these counts should not be
287 * visible outside dag creation routines - manipulating the counts
288 * here should be removed */
289 if (type == RF_IO_TYPE_READ) {
290 if (asmap->numDataFailed == 0)
291 *createFunc = (RF_VoidFuncPtr) rf_CreateFaultFreeReadDAG;
293 *createFunc = (RF_VoidFuncPtr) rf_CreateRaidFiveDegradedReadDAG;
297 /* if mirroring, always use large writes. If the access
298 * requires two distinct parity updates, always do a small
299 * write. If the stripe contains a failure but the access
300 * does not, do a small write. The first conditional
301 * (numStripeUnitsAccessed <= numDataCol/2) uses a
302 * less-than-or-equal rather than just a less-than because
303 * when G is 3 or 4, numDataCol/2 is 1, and I want
304 * single-stripe-unit updates to use just one disk. */
305 if ((asmap->numDataFailed + asmap->numParityFailed) == 0) {
306 if (rf_suppressLocksAndLargeWrites ||
307 (((asmap->numStripeUnitsAccessed <= (layoutPtr->numDataCol / 2)) && (layoutPtr->numDataCol != 1)) ||
308 (asmap->parityInfo->next != NULL) || rf_CheckStripeForFailures(raidPtr, asmap))) {
309 *createFunc = (RF_VoidFuncPtr) rf_CreateSmallWriteDAG;
311 *createFunc = (RF_VoidFuncPtr) rf_CreateLargeWriteDAG;
313 if (asmap->numParityFailed == 1)
314 *createFunc = (RF_VoidFuncPtr) rf_CreateNonRedundantWriteDAG;
316 if (asmap->numStripeUnitsAccessed != 1 && failedPDA->numSector != layoutPtr->sectorsPerStripeUnit)
319 *createFunc = (RF_VoidFuncPtr) rf_CreateDegradedWriteDAG;