xref: /netbsd-src/sys/dev/raidframe/rf_raid5.c (revision 8a8f936f250a330d54f8a24ed0e92aadf9743a7b)
1 /*	$NetBSD: rf_raid5.c,v 1.5 2001/10/04 15:58:55 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 <dev/raidframe/raidframevar.h>
36 
37 #include "rf_raid.h"
38 #include "rf_raid5.h"
39 #include "rf_dag.h"
40 #include "rf_dagffrd.h"
41 #include "rf_dagffwr.h"
42 #include "rf_dagdegrd.h"
43 #include "rf_dagdegwr.h"
44 #include "rf_dagutils.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 
193 	RF_ASSERT(RF_IO_IS_R_OR_W(type));
194 
195 	if (asmap->numDataFailed + asmap->numParityFailed > 1) {
196 		RF_ERRORMSG("Multiple disks failed in a single group!  Aborting I/O operation.\n");
197 		 /* *infoFunc = */ *createFunc = NULL;
198 		return;
199 	} else
200 		if (asmap->numDataFailed + asmap->numParityFailed == 1) {
201 
202 			/* if under recon & already reconstructed, redirect
203 			 * the access to the spare drive and eliminate the
204 			 * failure indication */
205 			failedPDA = asmap->failedPDAs[0];
206 			frow = failedPDA->row;
207 			fcol = failedPDA->col;
208 			rstat = raidPtr->status[failedPDA->row];
209 			prior_recon = (rstat == rf_rs_reconfigured) || (
210 			    (rstat == rf_rs_reconstructing) ?
211 			    rf_CheckRUReconstructed(raidPtr->reconControl[frow]->reconMap, failedPDA->startSector) : 0
212 			    );
213 			if (prior_recon) {
214 				RF_RowCol_t or = failedPDA->row, oc = failedPDA->col;
215 				RF_SectorNum_t oo = failedPDA->startSector;
216 
217 				if (layoutPtr->map->flags & RF_DISTRIBUTE_SPARE) {	/* redirect to dist
218 											 * spare space */
219 
220 					if (failedPDA == asmap->parityInfo) {
221 
222 						/* parity has failed */
223 						(layoutPtr->map->MapParity) (raidPtr, failedPDA->raidAddress, &failedPDA->row,
224 						    &failedPDA->col, &failedPDA->startSector, RF_REMAP);
225 
226 						if (asmap->parityInfo->next) {	/* redir 2nd component,
227 										 * if any */
228 							RF_PhysDiskAddr_t *p = asmap->parityInfo->next;
229 							RF_SectorNum_t SUoffs = p->startSector % layoutPtr->sectorsPerStripeUnit;
230 							p->row = failedPDA->row;
231 							p->col = failedPDA->col;
232 							p->startSector = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, failedPDA->startSector) +
233 							    SUoffs;	/* cheating:
234 									 * startSector is not
235 									 * really a RAID address */
236 						}
237 					} else
238 						if (asmap->parityInfo->next && failedPDA == asmap->parityInfo->next) {
239 							RF_ASSERT(0);	/* should not ever
240 									 * happen */
241 						} else {
242 
243 							/* data has failed */
244 							(layoutPtr->map->MapSector) (raidPtr, failedPDA->raidAddress, &failedPDA->row,
245 							    &failedPDA->col, &failedPDA->startSector, RF_REMAP);
246 
247 						}
248 
249 				} else {	/* redirect to dedicated spare
250 						 * space */
251 
252 					failedPDA->row = raidPtr->Disks[frow][fcol].spareRow;
253 					failedPDA->col = raidPtr->Disks[frow][fcol].spareCol;
254 
255 					/* the parity may have two distinct
256 					 * components, both of which may need
257 					 * to be redirected */
258 					if (asmap->parityInfo->next) {
259 						if (failedPDA == asmap->parityInfo) {
260 							failedPDA->next->row = failedPDA->row;
261 							failedPDA->next->col = failedPDA->col;
262 						} else
263 							if (failedPDA == asmap->parityInfo->next) {	/* paranoid:  should
264 													 * never occur */
265 								asmap->parityInfo->row = failedPDA->row;
266 								asmap->parityInfo->col = failedPDA->col;
267 							}
268 					}
269 				}
270 
271 				RF_ASSERT(failedPDA->col != -1);
272 
273 				if (rf_dagDebug || rf_mapDebug) {
274 					printf("raid%d: Redirected type '%c' r %d c %d o %ld -> r %d c %d o %ld\n",
275 					       raidPtr->raidid, type, or, oc,
276 					       (long) oo, failedPDA->row,
277 					       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