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