Home | History | Annotate | Line # | Download | only in raidframe
rf_dagffrd.c revision 1.4.10.1
      1 /*	$NetBSD: rf_dagffrd.c,v 1.4.10.1 2001/10/11 00:02:16 fvdl Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author: Mark Holland, Daniel Stodolsky, 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  * rf_dagffrd.c
     31  *
     32  * code for creating fault-free read DAGs
     33  *
     34  */
     35 
     36 #include <dev/raidframe/raidframevar.h>
     37 
     38 #include "rf_raid.h"
     39 #include "rf_dag.h"
     40 #include "rf_dagutils.h"
     41 #include "rf_dagfuncs.h"
     42 #include "rf_debugMem.h"
     43 #include "rf_memchunk.h"
     44 #include "rf_general.h"
     45 #include "rf_dagffrd.h"
     46 
     47 /******************************************************************************
     48  *
     49  * General comments on DAG creation:
     50  *
     51  * All DAGs in this file use roll-away error recovery.  Each DAG has a single
     52  * commit node, usually called "Cmt."  If an error occurs before the Cmt node
     53  * is reached, the execution engine will halt forward execution and work
     54  * backward through the graph, executing the undo functions.  Assuming that
     55  * each node in the graph prior to the Cmt node are undoable and atomic - or -
     56  * does not make changes to permanent state, the graph will fail atomically.
     57  * If an error occurs after the Cmt node executes, the engine will roll-forward
     58  * through the graph, blindly executing nodes until it reaches the end.
     59  * If a graph reaches the end, it is assumed to have completed successfully.
     60  *
     61  * A graph has only 1 Cmt node.
     62  *
     63  */
     64 
     65 
     66 /******************************************************************************
     67  *
     68  * The following wrappers map the standard DAG creation interface to the
     69  * DAG creation routines.  Additionally, these wrappers enable experimentation
     70  * with new DAG structures by providing an extra level of indirection, allowing
     71  * the DAG creation routines to be replaced at this single point.
     72  */
     73 
     74 void
     75 rf_CreateFaultFreeReadDAG(
     76     RF_Raid_t * raidPtr,
     77     RF_AccessStripeMap_t * asmap,
     78     RF_DagHeader_t * dag_h,
     79     void *bp,
     80     RF_RaidAccessFlags_t flags,
     81     RF_AllocListElem_t * allocList)
     82 {
     83 	rf_CreateNonredundantDAG(raidPtr, asmap, dag_h, bp, flags, allocList,
     84 	    RF_IO_TYPE_READ);
     85 }
     86 
     87 
     88 /******************************************************************************
     89  *
     90  * DAG creation code begins here
     91  */
     92 
     93 /******************************************************************************
     94  *
     95  * creates a DAG to perform a nonredundant read or write of data within one
     96  * stripe.
     97  * For reads, this DAG is as follows:
     98  *
     99  *                   /---- read ----\
    100  *    Header -- Block ---- read ---- Commit -- Terminate
    101  *                   \---- read ----/
    102  *
    103  * For writes, this DAG is as follows:
    104  *
    105  *                    /---- write ----\
    106  *    Header -- Commit ---- write ---- Block -- Terminate
    107  *                    \---- write ----/
    108  *
    109  * There is one disk node per stripe unit accessed, and all disk nodes are in
    110  * parallel.
    111  *
    112  * Tricky point here:  The first disk node (read or write) is created
    113  * normally.  Subsequent disk nodes are created by copying the first one,
    114  * and modifying a few params.  The "succedents" and "antecedents" fields are
    115  * _not_ re-created in each node, but rather left pointing to the same array
    116  * that was malloc'd when the first node was created.  Thus, it's essential
    117  * that when this DAG is freed, the succedents and antecedents fields be freed
    118  * in ONLY ONE of the read nodes.  This does not apply to the "params" field
    119  * because it is recreated for each READ node.
    120  *
    121  * Note that normal-priority accesses do not need to be tagged with their
    122  * parity stripe ID, because they will never be promoted.  Hence, I've
    123  * commented-out the code to do this, and marked it with UNNEEDED.
    124  *
    125  *****************************************************************************/
    126 
    127 void
    128 rf_CreateNonredundantDAG(
    129     RF_Raid_t * raidPtr,
    130     RF_AccessStripeMap_t * asmap,
    131     RF_DagHeader_t * dag_h,
    132     void *bp,
    133     RF_RaidAccessFlags_t flags,
    134     RF_AllocListElem_t * allocList,
    135     RF_IoType_t type)
    136 {
    137 	RF_DagNode_t *nodes, *diskNodes, *blockNode, *commitNode, *termNode;
    138 	RF_PhysDiskAddr_t *pda = asmap->physInfo;
    139 	int     (*doFunc) (RF_DagNode_t *), (*undoFunc) (RF_DagNode_t *);
    140 	int     i, n, totalNumNodes;
    141 	char   *name;
    142 
    143 	n = asmap->numStripeUnitsAccessed;
    144 	dag_h->creator = "NonredundantDAG";
    145 
    146 	RF_ASSERT(RF_IO_IS_R_OR_W(type));
    147 	switch (type) {
    148 	case RF_IO_TYPE_READ:
    149 		doFunc = rf_DiskReadFunc;
    150 		undoFunc = rf_DiskReadUndoFunc;
    151 		name = "R  ";
    152 		if (rf_dagDebug)
    153 			printf("[Creating non-redundant read DAG]\n");
    154 		break;
    155 	case RF_IO_TYPE_WRITE:
    156 		doFunc = rf_DiskWriteFunc;
    157 		undoFunc = rf_DiskWriteUndoFunc;
    158 		name = "W  ";
    159 		if (rf_dagDebug)
    160 			printf("[Creating non-redundant write DAG]\n");
    161 		break;
    162 	default:
    163 		RF_PANIC();
    164 	}
    165 
    166 	/*
    167          * For reads, the dag can not commit until the block node is reached.
    168          * for writes, the dag commits immediately.
    169          */
    170 	dag_h->numCommitNodes = 1;
    171 	dag_h->numCommits = 0;
    172 	dag_h->numSuccedents = 1;
    173 
    174 	/*
    175          * Node count:
    176          * 1 block node
    177          * n data reads (or writes)
    178          * 1 commit node
    179          * 1 terminator node
    180          */
    181 	RF_ASSERT(n > 0);
    182 	totalNumNodes = n + 3;
    183 	RF_CallocAndAdd(nodes, totalNumNodes, sizeof(RF_DagNode_t),
    184 	    (RF_DagNode_t *), allocList);
    185 	i = 0;
    186 	diskNodes = &nodes[i];
    187 	i += n;
    188 	blockNode = &nodes[i];
    189 	i += 1;
    190 	commitNode = &nodes[i];
    191 	i += 1;
    192 	termNode = &nodes[i];
    193 	i += 1;
    194 	RF_ASSERT(i == totalNumNodes);
    195 
    196 	/* initialize nodes */
    197 	switch (type) {
    198 	case RF_IO_TYPE_READ:
    199 		rf_InitNode(blockNode, rf_wait, RF_FALSE, rf_NullNodeFunc, rf_NullNodeUndoFunc,
    200 		    NULL, n, 0, 0, 0, dag_h, "Nil", allocList);
    201 		rf_InitNode(commitNode, rf_wait, RF_TRUE, rf_NullNodeFunc, rf_NullNodeUndoFunc,
    202 		    NULL, 1, n, 0, 0, dag_h, "Cmt", allocList);
    203 		rf_InitNode(termNode, rf_wait, RF_FALSE, rf_TerminateFunc, rf_TerminateUndoFunc,
    204 		    NULL, 0, 1, 0, 0, dag_h, "Trm", allocList);
    205 		break;
    206 	case RF_IO_TYPE_WRITE:
    207 		rf_InitNode(blockNode, rf_wait, RF_FALSE, rf_NullNodeFunc, rf_NullNodeUndoFunc,
    208 		    NULL, 1, 0, 0, 0, dag_h, "Nil", allocList);
    209 		rf_InitNode(commitNode, rf_wait, RF_TRUE, rf_NullNodeFunc, rf_NullNodeUndoFunc,
    210 		    NULL, n, 1, 0, 0, dag_h, "Cmt", allocList);
    211 		rf_InitNode(termNode, rf_wait, RF_FALSE, rf_TerminateFunc, rf_TerminateUndoFunc,
    212 		    NULL, 0, n, 0, 0, dag_h, "Trm", allocList);
    213 		break;
    214 	default:
    215 		RF_PANIC();
    216 	}
    217 
    218 	for (i = 0; i < n; i++) {
    219 		RF_ASSERT(pda != NULL);
    220 		rf_InitNode(&diskNodes[i], rf_wait, RF_FALSE, doFunc, undoFunc, rf_GenericWakeupFunc,
    221 		    1, 1, 4, 0, dag_h, name, allocList);
    222 		diskNodes[i].params[0].p = pda;
    223 		diskNodes[i].params[1].p = pda->bufPtr;
    224 		/* parity stripe id is not necessary */
    225 		diskNodes[i].params[2].v = 0;
    226 		diskNodes[i].params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, 0, 0, 0);
    227 		pda = pda->next;
    228 	}
    229 
    230 	/*
    231          * Connect nodes.
    232          */
    233 
    234 	/* connect hdr to block node */
    235 	RF_ASSERT(blockNode->numAntecedents == 0);
    236 	dag_h->succedents[0] = blockNode;
    237 
    238 	if (type == RF_IO_TYPE_READ) {
    239 		/* connecting a nonredundant read DAG */
    240 		RF_ASSERT(blockNode->numSuccedents == n);
    241 		RF_ASSERT(commitNode->numAntecedents == n);
    242 		for (i = 0; i < n; i++) {
    243 			/* connect block node to each read node */
    244 			RF_ASSERT(diskNodes[i].numAntecedents == 1);
    245 			blockNode->succedents[i] = &diskNodes[i];
    246 			diskNodes[i].antecedents[0] = blockNode;
    247 			diskNodes[i].antType[0] = rf_control;
    248 
    249 			/* connect each read node to the commit node */
    250 			RF_ASSERT(diskNodes[i].numSuccedents == 1);
    251 			diskNodes[i].succedents[0] = commitNode;
    252 			commitNode->antecedents[i] = &diskNodes[i];
    253 			commitNode->antType[i] = rf_control;
    254 		}
    255 		/* connect the commit node to the term node */
    256 		RF_ASSERT(commitNode->numSuccedents == 1);
    257 		RF_ASSERT(termNode->numAntecedents == 1);
    258 		RF_ASSERT(termNode->numSuccedents == 0);
    259 		commitNode->succedents[0] = termNode;
    260 		termNode->antecedents[0] = commitNode;
    261 		termNode->antType[0] = rf_control;
    262 	} else {
    263 		/* connecting a nonredundant write DAG */
    264 		/* connect the block node to the commit node */
    265 		RF_ASSERT(blockNode->numSuccedents == 1);
    266 		RF_ASSERT(commitNode->numAntecedents == 1);
    267 		blockNode->succedents[0] = commitNode;
    268 		commitNode->antecedents[0] = blockNode;
    269 		commitNode->antType[0] = rf_control;
    270 
    271 		RF_ASSERT(commitNode->numSuccedents == n);
    272 		RF_ASSERT(termNode->numAntecedents == n);
    273 		RF_ASSERT(termNode->numSuccedents == 0);
    274 		for (i = 0; i < n; i++) {
    275 			/* connect the commit node to each write node */
    276 			RF_ASSERT(diskNodes[i].numAntecedents == 1);
    277 			commitNode->succedents[i] = &diskNodes[i];
    278 			diskNodes[i].antecedents[0] = commitNode;
    279 			diskNodes[i].antType[0] = rf_control;
    280 
    281 			/* connect each write node to the term node */
    282 			RF_ASSERT(diskNodes[i].numSuccedents == 1);
    283 			diskNodes[i].succedents[0] = termNode;
    284 			termNode->antecedents[i] = &diskNodes[i];
    285 			termNode->antType[i] = rf_control;
    286 		}
    287 	}
    288 }
    289 /******************************************************************************
    290  * Create a fault-free read DAG for RAID level 1
    291  *
    292  * Hdr -> Nil -> Rmir -> Cmt -> Trm
    293  *
    294  * The "Rmir" node schedules a read from the disk in the mirror pair with the
    295  * shortest disk queue.  the proper queue is selected at Rmir execution.  this
    296  * deferred mapping is unlike other archs in RAIDframe which generally fix
    297  * mapping at DAG creation time.
    298  *
    299  * Parameters:  raidPtr   - description of the physical array
    300  *              asmap     - logical & physical addresses for this access
    301  *              bp        - buffer ptr (for holding read data)
    302  *              flags     - general flags (e.g. disk locking)
    303  *              allocList - list of memory allocated in DAG creation
    304  *****************************************************************************/
    305 
    306 static void
    307 CreateMirrorReadDAG(
    308     RF_Raid_t * raidPtr,
    309     RF_AccessStripeMap_t * asmap,
    310     RF_DagHeader_t * dag_h,
    311     void *bp,
    312     RF_RaidAccessFlags_t flags,
    313     RF_AllocListElem_t * allocList,
    314     int (*readfunc) (RF_DagNode_t * node))
    315 {
    316 	RF_DagNode_t *readNodes, *nodes, *blockNode, *commitNode, *termNode;
    317 	RF_PhysDiskAddr_t *data_pda = asmap->physInfo;
    318 	RF_PhysDiskAddr_t *parity_pda = asmap->parityInfo;
    319 	int     i, n, totalNumNodes;
    320 
    321 	n = asmap->numStripeUnitsAccessed;
    322 	dag_h->creator = "RaidOneReadDAG";
    323 	if (rf_dagDebug) {
    324 		printf("[Creating RAID level 1 read DAG]\n");
    325 	}
    326 	/*
    327          * This dag can not commit until the commit node is reached
    328          * errors prior to the commit point imply the dag has failed.
    329          */
    330 	dag_h->numCommitNodes = 1;
    331 	dag_h->numCommits = 0;
    332 	dag_h->numSuccedents = 1;
    333 
    334 	/*
    335          * Node count:
    336          * n data reads
    337          * 1 block node
    338          * 1 commit node
    339          * 1 terminator node
    340          */
    341 	RF_ASSERT(n > 0);
    342 	totalNumNodes = n + 3;
    343 	RF_CallocAndAdd(nodes, totalNumNodes, sizeof(RF_DagNode_t),
    344 	    (RF_DagNode_t *), allocList);
    345 	i = 0;
    346 	readNodes = &nodes[i];
    347 	i += n;
    348 	blockNode = &nodes[i];
    349 	i += 1;
    350 	commitNode = &nodes[i];
    351 	i += 1;
    352 	termNode = &nodes[i];
    353 	i += 1;
    354 	RF_ASSERT(i == totalNumNodes);
    355 
    356 	/* initialize nodes */
    357 	rf_InitNode(blockNode, rf_wait, RF_FALSE, rf_NullNodeFunc,
    358 	    rf_NullNodeUndoFunc, NULL, n, 0, 0, 0, dag_h, "Nil", allocList);
    359 	rf_InitNode(commitNode, rf_wait, RF_TRUE, rf_NullNodeFunc,
    360 	    rf_NullNodeUndoFunc, NULL, 1, n, 0, 0, dag_h, "Cmt", allocList);
    361 	rf_InitNode(termNode, rf_wait, RF_FALSE, rf_TerminateFunc,
    362 	    rf_TerminateUndoFunc, NULL, 0, 1, 0, 0, dag_h, "Trm", allocList);
    363 
    364 	for (i = 0; i < n; i++) {
    365 		RF_ASSERT(data_pda != NULL);
    366 		RF_ASSERT(parity_pda != NULL);
    367 		rf_InitNode(&readNodes[i], rf_wait, RF_FALSE, readfunc,
    368 		    rf_DiskReadMirrorUndoFunc, rf_GenericWakeupFunc, 1, 1, 5, 0, dag_h,
    369 		    "Rmir", allocList);
    370 		readNodes[i].params[0].p = data_pda;
    371 		readNodes[i].params[1].p = data_pda->bufPtr;
    372 		/* parity stripe id is not necessary */
    373 		readNodes[i].params[2].p = 0;
    374 		readNodes[i].params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, 0, 0, 0);
    375 		readNodes[i].params[4].p = parity_pda;
    376 		data_pda = data_pda->next;
    377 		parity_pda = parity_pda->next;
    378 	}
    379 
    380 	/*
    381          * Connect nodes
    382          */
    383 
    384 	/* connect hdr to block node */
    385 	RF_ASSERT(blockNode->numAntecedents == 0);
    386 	dag_h->succedents[0] = blockNode;
    387 
    388 	/* connect block node to read nodes */
    389 	RF_ASSERT(blockNode->numSuccedents == n);
    390 	for (i = 0; i < n; i++) {
    391 		RF_ASSERT(readNodes[i].numAntecedents == 1);
    392 		blockNode->succedents[i] = &readNodes[i];
    393 		readNodes[i].antecedents[0] = blockNode;
    394 		readNodes[i].antType[0] = rf_control;
    395 	}
    396 
    397 	/* connect read nodes to commit node */
    398 	RF_ASSERT(commitNode->numAntecedents == n);
    399 	for (i = 0; i < n; i++) {
    400 		RF_ASSERT(readNodes[i].numSuccedents == 1);
    401 		readNodes[i].succedents[0] = commitNode;
    402 		commitNode->antecedents[i] = &readNodes[i];
    403 		commitNode->antType[i] = rf_control;
    404 	}
    405 
    406 	/* connect commit node to term node */
    407 	RF_ASSERT(commitNode->numSuccedents == 1);
    408 	RF_ASSERT(termNode->numAntecedents == 1);
    409 	RF_ASSERT(termNode->numSuccedents == 0);
    410 	commitNode->succedents[0] = termNode;
    411 	termNode->antecedents[0] = commitNode;
    412 	termNode->antType[0] = rf_control;
    413 }
    414 
    415 void
    416 rf_CreateMirrorIdleReadDAG(
    417     RF_Raid_t * raidPtr,
    418     RF_AccessStripeMap_t * asmap,
    419     RF_DagHeader_t * dag_h,
    420     void *bp,
    421     RF_RaidAccessFlags_t flags,
    422     RF_AllocListElem_t * allocList)
    423 {
    424 	CreateMirrorReadDAG(raidPtr, asmap, dag_h, bp, flags, allocList,
    425 	    rf_DiskReadMirrorIdleFunc);
    426 }
    427 
    428 void
    429 rf_CreateMirrorPartitionReadDAG(
    430     RF_Raid_t * raidPtr,
    431     RF_AccessStripeMap_t * asmap,
    432     RF_DagHeader_t * dag_h,
    433     void *bp,
    434     RF_RaidAccessFlags_t flags,
    435     RF_AllocListElem_t * allocList)
    436 {
    437 	CreateMirrorReadDAG(raidPtr, asmap, dag_h, bp, flags, allocList,
    438 	    rf_DiskReadMirrorPartitionFunc);
    439 }
    440