Home | History | Annotate | Line # | Download | only in raidframe
rf_reconbuffer.c revision 1.18
      1 /*	$NetBSD: rf_reconbuffer.c,v 1.18 2004/03/01 23:30:58 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 (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  * rf_reconbuffer.c -- reconstruction buffer manager
     32  *
     33  ***************************************************/
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: rf_reconbuffer.c,v 1.18 2004/03/01 23:30:58 oster Exp $");
     37 
     38 #include "rf_raid.h"
     39 #include "rf_reconbuffer.h"
     40 #include "rf_acctrace.h"
     41 #include "rf_etimer.h"
     42 #include "rf_general.h"
     43 #include "rf_revent.h"
     44 #include "rf_reconutil.h"
     45 #include "rf_nwayxor.h"
     46 
     47 #ifdef DEBUG
     48 
     49 #define Dprintf1(s,a) if (rf_reconbufferDebug) printf(s,a)
     50 #define Dprintf2(s,a,b) if (rf_reconbufferDebug) printf(s,a,b)
     51 #define Dprintf3(s,a,b,c) if (rf_reconbufferDebug) printf(s,a,b,c)
     52 #define Dprintf4(s,a,b,c,d) if (rf_reconbufferDebug) printf(s,a,b,c,d)
     53 #define Dprintf5(s,a,b,c,d,e) if (rf_reconbufferDebug) printf(s,a,b,c,d,e)
     54 
     55 #else /* DEBUG */
     56 
     57 #define Dprintf1(s,a) {}
     58 #define Dprintf2(s,a,b) {}
     59 #define Dprintf3(s,a,b,c) {}
     60 #define Dprintf4(s,a,b,c,d) {}
     61 #define Dprintf5(s,a,b,c,d,e) {}
     62 
     63 #endif
     64 
     65 /*****************************************************************************
     66  *
     67  * Submit a reconstruction buffer to the manager for XOR.  We can only
     68  * submit a buffer if (1) we can xor into an existing buffer, which
     69  * means we don't have to acquire a new one, (2) we can acquire a
     70  * floating recon buffer, or (3) the caller has indicated that we are
     71  * allowed to keep the submitted buffer.
     72  *
     73  * Returns non-zero if and only if we were not able to submit.
     74  * In this case, we append the current disk ID to the wait list on the
     75  * indicated RU, so that it will be re-enabled when we acquire a buffer
     76  * for this RU.
     77  *
     78  ****************************************************************************/
     79 
     80 /*
     81  * nWayXorFuncs[i] is a pointer to a function that will xor "i"
     82  * bufs into the accumulating sum.
     83  */
     84 static const RF_VoidFuncPtr nWayXorFuncs[] = {
     85 	NULL,
     86 	(RF_VoidFuncPtr) rf_nWayXor1,
     87 	(RF_VoidFuncPtr) rf_nWayXor2,
     88 	(RF_VoidFuncPtr) rf_nWayXor3,
     89 	(RF_VoidFuncPtr) rf_nWayXor4,
     90 	(RF_VoidFuncPtr) rf_nWayXor5,
     91 	(RF_VoidFuncPtr) rf_nWayXor6,
     92 	(RF_VoidFuncPtr) rf_nWayXor7,
     93 	(RF_VoidFuncPtr) rf_nWayXor8,
     94 	(RF_VoidFuncPtr) rf_nWayXor9
     95 };
     96 
     97 /*
     98  * rbuf          - the recon buffer to submit
     99  * keep_it       - whether we can keep this buffer or we have to return it
    100  * use_committed - whether to use a committed or an available recon buffer
    101  */
    102 int
    103 rf_SubmitReconBuffer(RF_ReconBuffer_t *rbuf, int keep_it, int use_committed)
    104 {
    105 	const RF_LayoutSW_t *lp;
    106 	int     rc;
    107 
    108 	lp = rbuf->raidPtr->Layout.map;
    109 	rc = lp->SubmitReconBuffer(rbuf, keep_it, use_committed);
    110 	return (rc);
    111 }
    112 
    113 /*
    114  * rbuf          - the recon buffer to submit
    115  * keep_it       - whether we can keep this buffer or we have to return it
    116  * use_committed - whether to use a committed or an available recon buffer
    117  */
    118 int
    119 rf_SubmitReconBufferBasic(RF_ReconBuffer_t *rbuf, int keep_it,
    120 			  int use_committed)
    121 {
    122 	RF_Raid_t *raidPtr = rbuf->raidPtr;
    123 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    124 	RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl;
    125 	RF_ReconParityStripeStatus_t *pssPtr;
    126 	RF_ReconBuffer_t *targetRbuf, *t = NULL;	/* temporary rbuf
    127 							 * pointers */
    128 	caddr_t ta;		/* temporary data buffer pointer */
    129 	RF_CallbackDesc_t *cb, *p;
    130 	int     retcode = 0, created = 0;
    131 
    132 	RF_Etimer_t timer;
    133 
    134 	/* makes no sense to have a submission from the failed disk */
    135 	RF_ASSERT(rbuf);
    136 	RF_ASSERT(rbuf->col != reconCtrlPtr->fcol);
    137 
    138 	Dprintf4("RECON: submission by col %d for psid %ld ru %d (failed offset %ld)\n",
    139 	    rbuf->col, (long) rbuf->parityStripeID, rbuf->which_ru, (long) rbuf->failedDiskSectorOffset);
    140 
    141 	RF_LOCK_PSS_MUTEX(raidPtr, rbuf->parityStripeID);
    142 
    143 	RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
    144 
    145 	pssPtr = rf_LookupRUStatus(raidPtr, reconCtrlPtr->pssTable, rbuf->parityStripeID, rbuf->which_ru, RF_PSS_NONE, &created);
    146 	RF_ASSERT(pssPtr);	/* if it didn't exist, we wouldn't have gotten
    147 				 * an rbuf for it */
    148 
    149 	/* check to see if enough buffers have accumulated to do an XOR.  If
    150 	 * so, there's no need to acquire a floating rbuf.  Before we can do
    151 	 * any XORing, we must have acquired a destination buffer.  If we
    152 	 * have, then we can go ahead and do the XOR if (1) including this
    153 	 * buffer, enough bufs have accumulated, or (2) this is the last
    154 	 * submission for this stripe. Otherwise, we have to go acquire a
    155 	 * floating rbuf. */
    156 
    157 	targetRbuf = (RF_ReconBuffer_t *) pssPtr->rbuf;
    158 	if ((targetRbuf != NULL) &&
    159 	    ((pssPtr->xorBufCount == rf_numBufsToAccumulate - 1) || (targetRbuf->count + pssPtr->xorBufCount + 1 == layoutPtr->numDataCol))) {
    160 		pssPtr->rbufsForXor[pssPtr->xorBufCount++] = rbuf;	/* install this buffer */
    161 		Dprintf2("RECON: col %d invoking a %d-way XOR\n", rbuf->col, pssPtr->xorBufCount);
    162 		RF_ETIMER_START(timer);
    163 		rf_MultiWayReconXor(raidPtr, pssPtr);
    164 		RF_ETIMER_STOP(timer);
    165 		RF_ETIMER_EVAL(timer);
    166 		raidPtr->accumXorTimeUs += RF_ETIMER_VAL_US(timer);
    167 		if (!keep_it) {
    168 #if RF_ACC_TRACE > 0
    169 			raidPtr->recon_tracerecs[rbuf->col].xor_us = RF_ETIMER_VAL_US(timer);
    170 			RF_ETIMER_STOP(raidPtr->recon_tracerecs[rbuf->col].recon_timer);
    171 			RF_ETIMER_EVAL(raidPtr->recon_tracerecs[rbuf->col].recon_timer);
    172 			raidPtr->recon_tracerecs[rbuf->col].specific.recon.recon_return_to_submit_us +=
    173 			    RF_ETIMER_VAL_US(raidPtr->recon_tracerecs[rbuf->col].recon_timer);
    174 			RF_ETIMER_START(raidPtr->recon_tracerecs[rbuf->col].recon_timer);
    175 
    176 			rf_LogTraceRec(raidPtr, &raidPtr->recon_tracerecs[rbuf->col]);
    177 #endif
    178 		}
    179 		rf_CheckForFullRbuf(raidPtr, reconCtrlPtr, pssPtr, layoutPtr->numDataCol);
    180 
    181 		/* if use_committed is on, we _must_ consume a buffer off the
    182 		 * committed list. */
    183 		if (use_committed) {
    184 			t = reconCtrlPtr->committedRbufs;
    185 			RF_ASSERT(t);
    186 			reconCtrlPtr->committedRbufs = t->next;
    187 			rf_ReleaseFloatingReconBuffer(raidPtr, t);
    188 		}
    189 		if (keep_it) {
    190 			RF_UNLOCK_PSS_MUTEX(raidPtr, rbuf->parityStripeID);
    191 			RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
    192 			rf_FreeReconBuffer(rbuf);
    193 			return (retcode);
    194 		}
    195 		goto out;
    196 	}
    197 	/* set the value of "t", which we'll use as the rbuf from here on */
    198 	if (keep_it) {
    199 		t = rbuf;
    200 	} else {
    201 		if (use_committed) {	/* if a buffer has been committed to
    202 					 * us, use it */
    203 			t = reconCtrlPtr->committedRbufs;
    204 			RF_ASSERT(t);
    205 			reconCtrlPtr->committedRbufs = t->next;
    206 			t->next = NULL;
    207 		} else
    208 			if (reconCtrlPtr->floatingRbufs) {
    209 				t = reconCtrlPtr->floatingRbufs;
    210 				reconCtrlPtr->floatingRbufs = t->next;
    211 				t->next = NULL;
    212 			}
    213 	}
    214 
    215 	/* If we weren't able to acquire a buffer, append to the end of the
    216 	 * buf list in the recon ctrl struct. */
    217 	if (!t) {
    218 		RF_ASSERT(!keep_it && !use_committed);
    219 		Dprintf1("RECON: col %d failed to acquire floating rbuf\n", rbuf->col);
    220 
    221 		raidPtr->procsInBufWait++;
    222 		if ((raidPtr->procsInBufWait == raidPtr->numCol - 1) && (raidPtr->numFullReconBuffers == 0)) {
    223 			printf("Buffer wait deadlock detected.  Exiting.\n");
    224 			rf_PrintPSStatusTable(raidPtr);
    225 			RF_PANIC();
    226 		}
    227 		pssPtr->flags |= RF_PSS_BUFFERWAIT;
    228 		cb = rf_AllocCallbackDesc();	/* append to buf wait list in
    229 						 * recon ctrl structure */
    230 		cb->col = rbuf->col;
    231 		cb->callbackArg.v = rbuf->parityStripeID;
    232 		cb->callbackArg2.v = rbuf->which_ru;
    233 		cb->next = NULL;
    234 		if (!reconCtrlPtr->bufferWaitList)
    235 			reconCtrlPtr->bufferWaitList = cb;
    236 		else {		/* might want to maintain head/tail pointers
    237 				 * here rather than search for end of list */
    238 			for (p = reconCtrlPtr->bufferWaitList; p->next; p = p->next);
    239 			p->next = cb;
    240 		}
    241 		retcode = 1;
    242 		goto out;
    243 	}
    244 	Dprintf1("RECON: col %d acquired rbuf\n", rbuf->col);
    245 #if RF_ACC_TRACE > 0
    246 	RF_ETIMER_STOP(raidPtr->recon_tracerecs[rbuf->col].recon_timer);
    247 	RF_ETIMER_EVAL(raidPtr->recon_tracerecs[rbuf->col].recon_timer);
    248 	raidPtr->recon_tracerecs[rbuf->col].specific.recon.recon_return_to_submit_us +=
    249 	    RF_ETIMER_VAL_US(raidPtr->recon_tracerecs[rbuf->col].recon_timer);
    250 	RF_ETIMER_START(raidPtr->recon_tracerecs[rbuf->col].recon_timer);
    251 
    252 	rf_LogTraceRec(raidPtr, &raidPtr->recon_tracerecs[rbuf->col]);
    253 #endif
    254 
    255 	/* initialize the buffer */
    256 	if (t != rbuf) {
    257 		t->col = reconCtrlPtr->fcol;
    258 		t->parityStripeID = rbuf->parityStripeID;
    259 		t->which_ru = rbuf->which_ru;
    260 		t->failedDiskSectorOffset = rbuf->failedDiskSectorOffset;
    261 		t->spCol = rbuf->spCol;
    262 		t->spOffset = rbuf->spOffset;
    263 
    264 		ta = t->buffer;
    265 		t->buffer = rbuf->buffer;
    266 		rbuf->buffer = ta;	/* swap buffers */
    267 	}
    268 	/* the first installation always gets installed as the destination
    269 	 * buffer. subsequent installations get stacked up to allow for
    270 	 * multi-way XOR */
    271 	if (!pssPtr->rbuf) {
    272 		pssPtr->rbuf = t;
    273 		t->count = 1;
    274 	} else
    275 		pssPtr->rbufsForXor[pssPtr->xorBufCount++] = t;	/* install this buffer */
    276 
    277 	rf_CheckForFullRbuf(raidPtr, reconCtrlPtr, pssPtr, layoutPtr->numDataCol);	/* the buffer is full if
    278 											 * G=2 */
    279 
    280 out:
    281 	RF_UNLOCK_PSS_MUTEX(raidPtr, rbuf->parityStripeID);
    282 	RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
    283 	return (retcode);
    284 }
    285 /* pssPtr - the pss descriptor for this parity stripe */
    286 int
    287 rf_MultiWayReconXor(RF_Raid_t *raidPtr, RF_ReconParityStripeStatus_t *pssPtr)
    288 {
    289 	int     i, numBufs = pssPtr->xorBufCount;
    290 	int     numBytes = rf_RaidAddressToByte(raidPtr, raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU);
    291 	RF_ReconBuffer_t **rbufs = (RF_ReconBuffer_t **) pssPtr->rbufsForXor;
    292 	RF_ReconBuffer_t *targetRbuf = (RF_ReconBuffer_t *) pssPtr->rbuf;
    293 
    294 	RF_ASSERT(pssPtr->rbuf != NULL);
    295 	RF_ASSERT(numBufs > 0 && numBufs < RF_PS_MAX_BUFS);
    296 #ifdef _KERNEL
    297 #ifndef __NetBSD__
    298 	thread_block();		/* yield the processor before doing a big XOR */
    299 #endif
    300 #endif				/* _KERNEL */
    301 	/*
    302          * XXX
    303          *
    304          * What if more than 9 bufs?
    305          */
    306 	nWayXorFuncs[numBufs] (pssPtr->rbufsForXor, targetRbuf, numBytes / sizeof(long));
    307 
    308 	/* release all the reconstruction buffers except the last one, which
    309 	 * belongs to the disk whose submission caused this XOR to take place */
    310 	for (i = 0; i < numBufs - 1; i++) {
    311 		if (rbufs[i]->type == RF_RBUF_TYPE_FLOATING)
    312 			rf_ReleaseFloatingReconBuffer(raidPtr, rbufs[i]);
    313 		else
    314 			if (rbufs[i]->type == RF_RBUF_TYPE_FORCED)
    315 				rf_FreeReconBuffer(rbufs[i]);
    316 			else
    317 				RF_ASSERT(0);
    318 	}
    319 	targetRbuf->count += pssPtr->xorBufCount;
    320 	pssPtr->xorBufCount = 0;
    321 	return (0);
    322 }
    323 /* removes one full buffer from one of the full-buffer lists and returns it.
    324  *
    325  * ASSUMES THE RB_MUTEX IS UNLOCKED AT ENTRY.
    326  */
    327 RF_ReconBuffer_t *
    328 rf_GetFullReconBuffer(RF_ReconCtrl_t *reconCtrlPtr)
    329 {
    330 	RF_ReconBuffer_t *p;
    331 
    332 	RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
    333 
    334 	if ((p = reconCtrlPtr->fullBufferList) != NULL) {
    335 		reconCtrlPtr->fullBufferList = p->next;
    336 		p->next = NULL;
    337 	}
    338 	RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
    339 	return (p);
    340 }
    341 
    342 
    343 /* if the reconstruction buffer is full, move it to the full list,
    344  * which is maintained sorted by failed disk sector offset
    345  *
    346  * ASSUMES THE RB_MUTEX IS LOCKED AT ENTRY.  */
    347 int
    348 rf_CheckForFullRbuf(RF_Raid_t *raidPtr, RF_ReconCtrl_t *reconCtrl,
    349 		    RF_ReconParityStripeStatus_t *pssPtr, int numDataCol)
    350 {
    351 	RF_ReconBuffer_t *p, *pt, *rbuf = (RF_ReconBuffer_t *) pssPtr->rbuf;
    352 
    353 	if (rbuf->count == numDataCol) {
    354 		raidPtr->numFullReconBuffers++;
    355 		Dprintf2("RECON: rbuf for psid %ld ru %d has filled\n",
    356 		    (long) rbuf->parityStripeID, rbuf->which_ru);
    357 		if (!reconCtrl->fullBufferList || (rbuf->failedDiskSectorOffset < reconCtrl->fullBufferList->failedDiskSectorOffset)) {
    358 			Dprintf2("RECON: rbuf for psid %ld ru %d is head of list\n",
    359 			    (long) rbuf->parityStripeID, rbuf->which_ru);
    360 			rbuf->next = reconCtrl->fullBufferList;
    361 			reconCtrl->fullBufferList = rbuf;
    362 		} else {
    363 			for (pt = reconCtrl->fullBufferList, p = pt->next; p && p->failedDiskSectorOffset < rbuf->failedDiskSectorOffset; pt = p, p = p->next);
    364 			rbuf->next = p;
    365 			pt->next = rbuf;
    366 			Dprintf2("RECON: rbuf for psid %ld ru %d is in list\n",
    367 			    (long) rbuf->parityStripeID, rbuf->which_ru);
    368 		}
    369 		rbuf->pssPtr = pssPtr;
    370 		pssPtr->rbuf = NULL;
    371 		rf_CauseReconEvent(raidPtr, rbuf->col, NULL, RF_REVENT_BUFREADY);
    372 	}
    373 	return (0);
    374 }
    375 
    376 
    377 /* release a floating recon buffer for someone else to use.
    378  * assumes the rb_mutex is LOCKED at entry
    379  */
    380 void
    381 rf_ReleaseFloatingReconBuffer(RF_Raid_t *raidPtr, RF_ReconBuffer_t *rbuf)
    382 {
    383 	RF_ReconCtrl_t *rcPtr = raidPtr->reconControl;
    384 	RF_CallbackDesc_t *cb;
    385 
    386 	Dprintf2("RECON: releasing rbuf for psid %ld ru %d\n",
    387 	    (long) rbuf->parityStripeID, rbuf->which_ru);
    388 
    389 	/* if anyone is waiting on buffers, wake one of them up.  They will
    390 	 * subsequently wake up anyone else waiting on their RU */
    391 	if (rcPtr->bufferWaitList) {
    392 		rbuf->next = rcPtr->committedRbufs;
    393 		rcPtr->committedRbufs = rbuf;
    394 		cb = rcPtr->bufferWaitList;
    395 		rcPtr->bufferWaitList = cb->next;
    396 		rf_CauseReconEvent(raidPtr, cb->col, (void *) 1, RF_REVENT_BUFCLEAR);	/* arg==1 => we've
    397 												 * committed a buffer */
    398 		rf_FreeCallbackDesc(cb);
    399 		raidPtr->procsInBufWait--;
    400 	} else {
    401 		rbuf->next = rcPtr->floatingRbufs;
    402 		rcPtr->floatingRbufs = rbuf;
    403 	}
    404 }
    405