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