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