Home | History | Annotate | Line # | Download | only in raidframe
rf_aselect.c revision 1.16
      1 /*	$NetBSD: rf_aselect.c,v 1.16 2004/02/29 01:50:23 oster Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author: Mark Holland, William V. Courtright II
      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 (at) 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  * aselect.c -- algorithm selection code
     32  *
     33  *****************************************************************************/
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: rf_aselect.c,v 1.16 2004/02/29 01:50:23 oster Exp $");
     37 
     38 #include <dev/raidframe/raidframevar.h>
     39 
     40 #include "rf_archs.h"
     41 #include "rf_raid.h"
     42 #include "rf_dag.h"
     43 #include "rf_dagutils.h"
     44 #include "rf_dagfuncs.h"
     45 #include "rf_general.h"
     46 #include "rf_desc.h"
     47 #include "rf_map.h"
     48 
     49 static void InitHdrNode(RF_DagHeader_t **, RF_Raid_t *);
     50 int     rf_SelectAlgorithm(RF_RaidAccessDesc_t *, RF_RaidAccessFlags_t);
     51 
     52 /******************************************************************************
     53  *
     54  * Create and Initialiaze a dag header and termination node
     55  *
     56  *****************************************************************************/
     57 static void
     58 InitHdrNode(RF_DagHeader_t **hdr, RF_Raid_t *raidPtr)
     59 {
     60 	/* create and initialize dag hdr */
     61 	*hdr = rf_AllocDAGHeader();
     62 	rf_MakeAllocList((*hdr)->allocList);
     63 	(*hdr)->status = rf_enable;
     64 	(*hdr)->numSuccedents = 0;
     65 	(*hdr)->raidPtr = raidPtr;
     66 	(*hdr)->next = NULL;
     67 }
     68 
     69 /******************************************************************************
     70  *
     71  * Create a DAG to do a read or write operation.
     72  *
     73  * create a list of dagLists, one list per parity stripe.
     74  * return the lists in the desc->dagList (which is a list of lists).
     75  *
     76  * Normally, each list contains one dag for the entire stripe.  In some
     77  * tricky cases, we break this into multiple dags, either one per stripe
     78  * unit or one per block (sector).  When this occurs, these dags are returned
     79  * as a linked list (dagList) which is executed sequentially (to preserve
     80  * atomic parity updates in the stripe).
     81  *
     82  * dags which operate on independent parity goups (stripes) are returned in
     83  * independent dagLists (distinct elements in desc->dagArray) and may be
     84  * executed concurrently.
     85  *
     86  * Finally, if the SelectionFunc fails to create a dag for a block, we punt
     87  * and return 1.
     88  *
     89  * The above process is performed in two phases:
     90  *   1) create an array(s) of creation functions (eg stripeFuncs)
     91  *   2) create dags and concatenate/merge to form the final dag.
     92  *
     93  * Because dag's are basic blocks (single entry, single exit, unconditional
     94  * control flow, we can add the following optimizations (future work):
     95  *   first-pass optimizer to allow max concurrency (need all data dependencies)
     96  *   second-pass optimizer to eliminate common subexpressions (need true
     97  *                         data dependencies)
     98  *   third-pass optimizer to eliminate dead code (need true data dependencies)
     99  *****************************************************************************/
    100 
    101 #define MAXNSTRIPES 50
    102 
    103 int
    104 rf_SelectAlgorithm(RF_RaidAccessDesc_t *desc, RF_RaidAccessFlags_t flags)
    105 {
    106 	RF_AccessStripeMapHeader_t *asm_h = desc->asmap;
    107 	RF_IoType_t type = desc->type;
    108 	RF_Raid_t *raidPtr = desc->raidPtr;
    109 	void   *bp = desc->bp;
    110 
    111 	RF_AccessStripeMap_t *asmap = asm_h->stripeMap;
    112 	RF_AccessStripeMap_t *asm_p;
    113 	RF_DagHeader_t *dag_h = NULL, *tempdag_h, *lastdag_h;
    114 	RF_DagList_t *dagList, *dagListend;
    115 	int     i, j, k;
    116 	RF_FuncList_t *stripeFuncsList, *stripeFuncs, *stripeFuncsEnd, *temp;
    117 	RF_AccessStripeMap_t *asm_up, *asm_bp;
    118 	RF_AccessStripeMapHeader_t ***asmh_u, *endASMList;
    119 	RF_AccessStripeMapHeader_t ***asmh_b;
    120 	RF_VoidFuncPtr **stripeUnitFuncs, uFunc;
    121 	RF_VoidFuncPtr **blockFuncs, bFunc;
    122 	int     numStripesBailed = 0, cantCreateDAGs = RF_FALSE;
    123 	int     numStripeUnitsBailed = 0;
    124 	int     stripeNum, numUnitDags = 0, stripeUnitNum, numBlockDags = 0;
    125 	RF_StripeNum_t numStripeUnits;
    126 	RF_SectorNum_t numBlocks;
    127 	RF_RaidAddr_t address;
    128 	int     length;
    129 	RF_PhysDiskAddr_t *physPtr;
    130 	caddr_t buffer;
    131 
    132 	lastdag_h = NULL;
    133 	asmh_u = asmh_b = NULL;
    134 	stripeUnitFuncs = NULL;
    135 	blockFuncs = NULL;
    136 
    137 	stripeFuncsList = NULL;
    138 	stripeFuncsEnd = NULL;
    139 
    140 	/* walk through the asm list once collecting information */
    141 	/* attempt to find a single creation function for each stripe */
    142 	desc->numStripes = 0;
    143 	for (i = 0, asm_p = asmap; asm_p; asm_p = asm_p->next, i++) {
    144 		desc->numStripes++;
    145 		stripeFuncs = rf_AllocFuncList();
    146 
    147 		if (stripeFuncsEnd == NULL) {
    148 			stripeFuncsList = stripeFuncs;
    149 		} else {
    150 			stripeFuncsEnd->next = stripeFuncs;
    151 		}
    152 		stripeFuncsEnd = stripeFuncs;
    153 
    154 		(raidPtr->Layout.map->SelectionFunc) (raidPtr, type, asm_p, &(stripeFuncs->fp));
    155 		/* check to see if we found a creation func for this stripe */
    156 		if (stripeFuncs->fp == NULL) {
    157 			/* could not find creation function for entire stripe
    158 			 * so, let's see if we can find one for each stripe
    159 			 * unit in the stripe */
    160 
    161 			if (numStripesBailed == 0) {
    162 				/* one stripe map header for each stripe we
    163 				 * bail on */
    164 				RF_Malloc(asmh_u, sizeof(RF_AccessStripeMapHeader_t **) * asm_h->numStripes, (RF_AccessStripeMapHeader_t ***));
    165 				/* create an array of ptrs to arrays of
    166 				 * stripeFuncs */
    167 				RF_Malloc(stripeUnitFuncs, asm_h->numStripes * sizeof(RF_VoidFuncPtr), (RF_VoidFuncPtr **));
    168 			}
    169 			/* create an array of creation funcs (called
    170 			 * stripeFuncs) for this stripe */
    171 			numStripeUnits = asm_p->numStripeUnitsAccessed;
    172 			RF_Malloc(stripeUnitFuncs[numStripesBailed], numStripeUnits * sizeof(RF_VoidFuncPtr), (RF_VoidFuncPtr *));
    173 			RF_Malloc(asmh_u[numStripesBailed], numStripeUnits * sizeof(RF_AccessStripeMapHeader_t *), (RF_AccessStripeMapHeader_t **));
    174 
    175 			/* lookup array of stripeUnitFuncs for this stripe */
    176 			for (j = 0, physPtr = asm_p->physInfo; physPtr; physPtr = physPtr->next, j++) {
    177 				/* remap for series of single stripe-unit
    178 				 * accesses */
    179 				address = physPtr->raidAddress;
    180 				length = physPtr->numSector;
    181 				buffer = physPtr->bufPtr;
    182 
    183 				asmh_u[numStripesBailed][j] = rf_MapAccess(raidPtr, address, length, buffer, RF_DONT_REMAP);
    184 				asm_up = asmh_u[numStripesBailed][j]->stripeMap;
    185 
    186 				/* get the creation func for this stripe unit */
    187 				(raidPtr->Layout.map->SelectionFunc) (raidPtr, type, asm_up, &(stripeUnitFuncs[numStripesBailed][j]));
    188 
    189 				/* check to see if we found a creation func
    190 				 * for this stripe unit */
    191 				if (stripeUnitFuncs[numStripesBailed][j] == (RF_VoidFuncPtr) NULL) {
    192 					/* could not find creation function
    193 					 * for stripe unit so, let's see if we
    194 					 * can find one for each block in the
    195 					 * stripe unit */
    196 					if (numStripeUnitsBailed == 0) {
    197 						/* one stripe map header for
    198 						 * each stripe unit we bail on */
    199 						RF_Malloc(asmh_b, sizeof(RF_AccessStripeMapHeader_t **) * asm_h->numStripes * raidPtr->Layout.numDataCol, (RF_AccessStripeMapHeader_t ***));
    200 						/* create an array of ptrs to
    201 						 * arrays of blockFuncs */
    202 						RF_Malloc(blockFuncs, asm_h->numStripes * raidPtr->Layout.numDataCol * sizeof(RF_VoidFuncPtr), (RF_VoidFuncPtr **));
    203 					}
    204 					/* create an array of creation funcs
    205 					 * (called blockFuncs) for this stripe
    206 					 * unit */
    207 					numBlocks = physPtr->numSector;
    208 					numBlockDags += numBlocks;
    209 					RF_Malloc(blockFuncs[numStripeUnitsBailed], numBlocks * sizeof(RF_VoidFuncPtr), (RF_VoidFuncPtr *));
    210 					RF_Malloc(asmh_b[numStripeUnitsBailed], numBlocks * sizeof(RF_AccessStripeMapHeader_t *), (RF_AccessStripeMapHeader_t **));
    211 
    212 					/* lookup array of blockFuncs for this
    213 					 * stripe unit */
    214 					for (k = 0; k < numBlocks; k++) {
    215 						/* remap for series of single
    216 						 * stripe-unit accesses */
    217 						address = physPtr->raidAddress + k;
    218 						length = 1;
    219 						buffer = physPtr->bufPtr + (k * (1 << raidPtr->logBytesPerSector));
    220 
    221 						asmh_b[numStripeUnitsBailed][k] = rf_MapAccess(raidPtr, address, length, buffer, RF_DONT_REMAP);
    222 						asm_bp = asmh_b[numStripeUnitsBailed][k]->stripeMap;
    223 
    224 						/* get the creation func for
    225 						 * this stripe unit */
    226 						(raidPtr->Layout.map->SelectionFunc) (raidPtr, type, asm_bp, &(blockFuncs[numStripeUnitsBailed][k]));
    227 
    228 						/* check to see if we found a
    229 						 * creation func for this
    230 						 * stripe unit */
    231 						if (blockFuncs[numStripeUnitsBailed][k] == NULL)
    232 							cantCreateDAGs = RF_TRUE;
    233 					}
    234 					numStripeUnitsBailed++;
    235 				} else {
    236 					numUnitDags++;
    237 				}
    238 			}
    239 			RF_ASSERT(j == numStripeUnits);
    240 			numStripesBailed++;
    241 		}
    242 	}
    243 
    244 	if (cantCreateDAGs) {
    245 		/* free memory and punt */
    246 		if (numStripesBailed > 0) {
    247 			stripeNum = 0;
    248 			stripeFuncs = stripeFuncsList;
    249 			for (i = 0, asm_p = asmap; asm_p; asm_p = asm_p->next, i++) {
    250 				if (stripeFuncs->fp == NULL) {
    251 					numStripeUnits = asm_p->numStripeUnitsAccessed;
    252 					for (j = 0; j < numStripeUnits; j++)
    253 						rf_FreeAccessStripeMap(asmh_u[stripeNum][j]);
    254 					RF_Free(asmh_u[stripeNum], numStripeUnits * sizeof(RF_AccessStripeMapHeader_t *));
    255 					RF_Free(stripeUnitFuncs[stripeNum], numStripeUnits * sizeof(RF_VoidFuncPtr));
    256 					stripeNum++;
    257 				}
    258 				stripeFuncs = stripeFuncs->next;
    259 			}
    260 			RF_ASSERT(stripeNum == numStripesBailed);
    261 			RF_Free(stripeUnitFuncs, asm_h->numStripes * sizeof(RF_VoidFuncPtr));
    262 			RF_Free(asmh_u, asm_h->numStripes * sizeof(RF_AccessStripeMapHeader_t **));
    263 		}
    264 		while (stripeFuncsList != NULL) {
    265 			temp = stripeFuncsList;
    266 			stripeFuncsList = stripeFuncsList->next;
    267 			rf_FreeFuncList(temp);
    268 		}
    269 		desc->numStripes = 0;
    270 		return (1);
    271 	} else {
    272 		/* begin dag creation */
    273 		stripeNum = 0;
    274 		stripeUnitNum = 0;
    275 
    276 		/* create a list of dagLists and fill them in */
    277 
    278 		dagListend = NULL;
    279 
    280 		stripeFuncs = stripeFuncsList;
    281 		for (i = 0, asm_p = asmap; asm_p; asm_p = asm_p->next, i++) {
    282 			/* grab dag header for this stripe */
    283 			dag_h = NULL;
    284 
    285 			dagList = rf_AllocDAGList();
    286 
    287 			/* always tack the new dagList onto the end of the list... */
    288 			if (dagListend == NULL) {
    289 				desc->dagList = dagList;
    290 			} else {
    291 				dagListend->next = dagList;
    292 			}
    293 			dagListend = dagList;
    294 
    295 			dagList->desc = desc;
    296 
    297 			if (stripeFuncs->fp == NULL) {
    298 				/* use bailout functions for this stripe */
    299 				for (j = 0, physPtr = asm_p->physInfo; physPtr; physPtr = physPtr->next, j++) {
    300 					uFunc = stripeUnitFuncs[stripeNum][j];
    301 					if (uFunc == (RF_VoidFuncPtr) NULL) {
    302 						/* use bailout functions for
    303 						 * this stripe unit */
    304 						for (k = 0; k < physPtr->numSector; k++) {
    305 							/* create a dag for
    306 							 * this block */
    307 							InitHdrNode(&tempdag_h, raidPtr);
    308 							dagList->numDags++;
    309 							if (dag_h == NULL) {
    310 								dag_h = tempdag_h;
    311 							} else {
    312 								lastdag_h->next = tempdag_h;
    313 							}
    314 							lastdag_h = tempdag_h;
    315 
    316 							bFunc = blockFuncs[stripeUnitNum][k];
    317 							RF_ASSERT(bFunc);
    318 							asm_bp = asmh_b[stripeUnitNum][k]->stripeMap;
    319 							(*bFunc) (raidPtr, asm_bp, tempdag_h, bp, flags, tempdag_h->allocList);
    320 						}
    321 						stripeUnitNum++;
    322 					} else {
    323 						/* create a dag for this unit */
    324 						InitHdrNode(&tempdag_h, raidPtr);
    325 						dagList->numDags++;
    326 						if (dag_h == NULL) {
    327 							dag_h = tempdag_h;
    328 						} else {
    329 							lastdag_h->next = tempdag_h;
    330 						}
    331 						lastdag_h = tempdag_h;
    332 
    333 						asm_up = asmh_u[stripeNum][j]->stripeMap;
    334 						(*uFunc) (raidPtr, asm_up, tempdag_h, bp, flags, tempdag_h->allocList);
    335 					}
    336 				}
    337 				RF_ASSERT(j == asm_p->numStripeUnitsAccessed);
    338 				/* merge linked bailout dag to existing dag
    339 				 * collection */
    340 				stripeNum++;
    341 			} else {
    342 				/* Create a dag for this parity stripe */
    343 				InitHdrNode(&tempdag_h, raidPtr);
    344 				dagList->numDags++;
    345 				if (dag_h == NULL) {
    346 					dag_h = tempdag_h;
    347 				} else {
    348 					lastdag_h->next = tempdag_h;
    349 				}
    350 				lastdag_h = tempdag_h;
    351 
    352 				(stripeFuncs->fp) (raidPtr, asm_p, tempdag_h, bp, flags, tempdag_h->allocList);
    353 			}
    354 			dagList->dags = dag_h;
    355 			stripeFuncs = stripeFuncs->next;
    356 		}
    357 		RF_ASSERT(i == desc->numStripes);
    358 
    359 		/* free memory */
    360 		if ((numStripesBailed > 0) || (numStripeUnitsBailed > 0)) {
    361 			stripeNum = 0;
    362 			stripeUnitNum = 0;
    363 			if (dag_h->asmList) {
    364 				endASMList = dag_h->asmList;
    365 				while (endASMList->next)
    366 					endASMList = endASMList->next;
    367 			} else
    368 				endASMList = NULL;
    369 			/* walk through io, stripe by stripe */
    370 			stripeFuncs = stripeFuncsList;
    371 			for (i = 0, asm_p = asmap; asm_p; asm_p = asm_p->next, i++) {
    372 				if (stripeFuncs->fp == NULL) {
    373 					numStripeUnits = asm_p->numStripeUnitsAccessed;
    374 					/* walk through stripe, stripe unit by
    375 					 * stripe unit */
    376 					for (j = 0, physPtr = asm_p->physInfo; physPtr; physPtr = physPtr->next, j++) {
    377 						if (stripeUnitFuncs[stripeNum][j] == NULL) {
    378 							numBlocks = physPtr->numSector;
    379 							/* walk through stripe
    380 							 * unit, block by
    381 							 * block */
    382 							for (k = 0; k < numBlocks; k++)
    383 								if (dag_h->asmList == NULL) {
    384 									dag_h->asmList = asmh_b[stripeUnitNum][k];
    385 									endASMList = dag_h->asmList;
    386 								} else {
    387 									endASMList->next = asmh_b[stripeUnitNum][k];
    388 									endASMList = endASMList->next;
    389 								}
    390 							RF_Free(asmh_b[stripeUnitNum], numBlocks * sizeof(RF_AccessStripeMapHeader_t *));
    391 							RF_Free(blockFuncs[stripeUnitNum], numBlocks * sizeof(RF_VoidFuncPtr));
    392 							stripeUnitNum++;
    393 						}
    394 						if (dag_h->asmList == NULL) {
    395 							dag_h->asmList = asmh_u[stripeNum][j];
    396 							endASMList = dag_h->asmList;
    397 						} else {
    398 							endASMList->next = asmh_u[stripeNum][j];
    399 							endASMList = endASMList->next;
    400 						}
    401 					}
    402 					RF_Free(asmh_u[stripeNum], numStripeUnits * sizeof(RF_AccessStripeMapHeader_t *));
    403 					RF_Free(stripeUnitFuncs[stripeNum], numStripeUnits * sizeof(RF_VoidFuncPtr));
    404 					stripeNum++;
    405 				}
    406 				stripeFuncs = stripeFuncs->next;
    407 			}
    408 			RF_ASSERT(stripeNum == numStripesBailed);
    409 			RF_Free(stripeUnitFuncs, asm_h->numStripes * sizeof(RF_VoidFuncPtr));
    410 			RF_Free(asmh_u, asm_h->numStripes * sizeof(RF_AccessStripeMapHeader_t **));
    411 			if (numStripeUnitsBailed > 0) {
    412 				RF_ASSERT(stripeUnitNum == numStripeUnitsBailed);
    413 				RF_Free(blockFuncs, raidPtr->Layout.numDataCol * asm_h->numStripes * sizeof(RF_VoidFuncPtr));
    414 				RF_Free(asmh_b, raidPtr->Layout.numDataCol * asm_h->numStripes * sizeof(RF_AccessStripeMapHeader_t **));
    415 			}
    416 		}
    417 		while (stripeFuncsList != NULL) {
    418 			temp = stripeFuncsList;
    419 			stripeFuncsList = stripeFuncsList->next;
    420 			rf_FreeFuncList(temp);
    421 		}
    422 		return (0);
    423 	}
    424 }
    425