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