Home | History | Annotate | Line # | Download | only in raidframe
rf_reconutil.c revision 1.30
      1 /*	$NetBSD: rf_reconutil.c,v 1.30 2011/05/01 10:01:01 mrg 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  * rf_reconutil.c -- reconstruction utilities
     31  ********************************************/
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: rf_reconutil.c,v 1.30 2011/05/01 10:01:01 mrg Exp $");
     35 
     36 #include <dev/raidframe/raidframevar.h>
     37 
     38 #include "rf_raid.h"
     39 #include "rf_desc.h"
     40 #include "rf_reconutil.h"
     41 #include "rf_reconbuffer.h"
     42 #include "rf_general.h"
     43 #include "rf_decluster.h"
     44 #include "rf_raid5_rotatedspare.h"
     45 #include "rf_interdecluster.h"
     46 #include "rf_chaindecluster.h"
     47 
     48 /*******************************************************************
     49  * allocates/frees the reconstruction control information structures
     50  *******************************************************************/
     51 
     52 /* fcol - failed column
     53  * scol - identifies which spare we are using
     54  */
     55 
     56 RF_ReconCtrl_t *
     57 rf_MakeReconControl(RF_RaidReconDesc_t *reconDesc,
     58 		    RF_RowCol_t fcol, RF_RowCol_t scol)
     59 {
     60 	RF_Raid_t *raidPtr = reconDesc->raidPtr;
     61 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
     62 	RF_ReconUnitCount_t RUsPerPU = layoutPtr->SUsPerPU / layoutPtr->SUsPerRU;
     63 	RF_ReconUnitCount_t numSpareRUs;
     64 	RF_ReconCtrl_t *reconCtrlPtr;
     65 	RF_ReconBuffer_t *rbuf;
     66 	const RF_LayoutSW_t *lp;
     67 #if (RF_INCLUDE_PARITY_DECLUSTERING_DS > 0)
     68 	int     retcode;
     69 #endif
     70 	RF_RowCol_t i;
     71 
     72 	lp = raidPtr->Layout.map;
     73 
     74 	/* make and zero the global reconstruction structure and the per-disk
     75 	 * structure */
     76 	RF_Malloc(reconCtrlPtr, sizeof(RF_ReconCtrl_t), (RF_ReconCtrl_t *));
     77 
     78 	/* note: this zeros the perDiskInfo */
     79 	RF_Malloc(reconCtrlPtr->perDiskInfo, raidPtr->numCol *
     80 		  sizeof(RF_PerDiskReconCtrl_t), (RF_PerDiskReconCtrl_t *));
     81 	reconCtrlPtr->reconDesc = reconDesc;
     82 	reconCtrlPtr->fcol = fcol;
     83 	reconCtrlPtr->spareCol = scol;
     84 	reconCtrlPtr->lastPSID = layoutPtr->numStripe / layoutPtr->SUsPerPU;
     85 	reconCtrlPtr->percentComplete = 0;
     86 	reconCtrlPtr->error = 0;
     87 	reconCtrlPtr->pending_writes = 0;
     88 
     89 	/* initialize each per-disk recon information structure */
     90 	for (i = 0; i < raidPtr->numCol; i++) {
     91 		reconCtrlPtr->perDiskInfo[i].reconCtrl = reconCtrlPtr;
     92 		reconCtrlPtr->perDiskInfo[i].col = i;
     93 		/* make it appear as if we just finished an RU */
     94 		reconCtrlPtr->perDiskInfo[i].curPSID = -1;
     95 		reconCtrlPtr->perDiskInfo[i].ru_count = RUsPerPU - 1;
     96 	}
     97 
     98 	/* Get the number of spare units per disk and the sparemap in case
     99 	 * spare is distributed  */
    100 
    101 	if (lp->GetNumSpareRUs) {
    102 		numSpareRUs = lp->GetNumSpareRUs(raidPtr);
    103 	} else {
    104 		numSpareRUs = 0;
    105 	}
    106 
    107 #if (RF_INCLUDE_PARITY_DECLUSTERING_DS > 0)
    108 	/*
    109          * Not all distributed sparing archs need dynamic mappings
    110          */
    111 	if (lp->InstallSpareTable) {
    112 		retcode = rf_InstallSpareTable(raidPtr, 0, fcol);
    113 		if (retcode) {
    114 			RF_PANIC();	/* XXX fix this */
    115 		}
    116 	}
    117 #endif
    118 	/* make the reconstruction map */
    119 	reconCtrlPtr->reconMap = rf_MakeReconMap(raidPtr, (int) (layoutPtr->SUsPerRU * layoutPtr->sectorsPerStripeUnit),
    120 	    raidPtr->sectorsPerDisk, numSpareRUs);
    121 
    122 	/* make the per-disk reconstruction buffers */
    123 	for (i = 0; i < raidPtr->numCol; i++) {
    124 		reconCtrlPtr->perDiskInfo[i].rbuf = (i == fcol) ? NULL : rf_MakeReconBuffer(raidPtr, i, RF_RBUF_TYPE_EXCLUSIVE);
    125 	}
    126 
    127 	/* initialize the event queue */
    128 	rf_init_mutex2(reconCtrlPtr->eq_mutex, IPL_VM);
    129 
    130 	reconCtrlPtr->eventQueue = NULL;
    131 	reconCtrlPtr->eq_count = 0;
    132 
    133 	/* make the floating recon buffers and append them to the free list */
    134 	rf_mutex_init(&reconCtrlPtr->rb_mutex);
    135 
    136 	reconCtrlPtr->fullBufferList = NULL;
    137 	reconCtrlPtr->floatingRbufs = NULL;
    138 	reconCtrlPtr->committedRbufs = NULL;
    139 	for (i = 0; i < raidPtr->numFloatingReconBufs; i++) {
    140 		rbuf = rf_MakeReconBuffer(raidPtr, fcol,
    141 					  RF_RBUF_TYPE_FLOATING);
    142 		rbuf->next = reconCtrlPtr->floatingRbufs;
    143 		reconCtrlPtr->floatingRbufs = rbuf;
    144 	}
    145 
    146 	/* create the parity stripe status table */
    147 	reconCtrlPtr->pssTable = rf_MakeParityStripeStatusTable(raidPtr);
    148 
    149 	/* set the initial min head sep counter val */
    150 	reconCtrlPtr->minHeadSepCounter = 0;
    151 
    152 	return (reconCtrlPtr);
    153 }
    154 
    155 void
    156 rf_FreeReconControl(RF_Raid_t *raidPtr)
    157 {
    158 	RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl;
    159 	RF_ReconBuffer_t *t;
    160 	RF_ReconUnitNum_t i;
    161 
    162 	RF_ASSERT(reconCtrlPtr);
    163 	for (i = 0; i < raidPtr->numCol; i++)
    164 		if (reconCtrlPtr->perDiskInfo[i].rbuf)
    165 			rf_FreeReconBuffer(reconCtrlPtr->perDiskInfo[i].rbuf);
    166 
    167 	t = reconCtrlPtr->floatingRbufs;
    168 	while (t) {
    169 		reconCtrlPtr->floatingRbufs = t->next;
    170 		rf_FreeReconBuffer(t);
    171 		t = reconCtrlPtr->floatingRbufs;
    172 	}
    173 
    174 	rf_destroy_mutex2(reconCtrlPtr->eq_mutex);
    175 
    176 	rf_FreeReconMap(reconCtrlPtr->reconMap);
    177 	rf_FreeParityStripeStatusTable(raidPtr, reconCtrlPtr->pssTable);
    178 	RF_Free(reconCtrlPtr->perDiskInfo,
    179 		raidPtr->numCol * sizeof(RF_PerDiskReconCtrl_t));
    180 	RF_Free(reconCtrlPtr, sizeof(*reconCtrlPtr));
    181 }
    182 
    183 
    184 /******************************************************************************
    185  * computes the default head separation limit
    186  *****************************************************************************/
    187 RF_HeadSepLimit_t
    188 rf_GetDefaultHeadSepLimit(RF_Raid_t *raidPtr)
    189 {
    190 	RF_HeadSepLimit_t hsl;
    191 	const RF_LayoutSW_t *lp;
    192 
    193 	lp = raidPtr->Layout.map;
    194 	if (lp->GetDefaultHeadSepLimit == NULL)
    195 		return (-1);
    196 	hsl = lp->GetDefaultHeadSepLimit(raidPtr);
    197 	return (hsl);
    198 }
    199 
    200 
    201 /******************************************************************************
    202  * computes the default number of floating recon buffers
    203  *****************************************************************************/
    204 int
    205 rf_GetDefaultNumFloatingReconBuffers(RF_Raid_t *raidPtr)
    206 {
    207 	const RF_LayoutSW_t *lp;
    208 	int     nrb;
    209 
    210 	lp = raidPtr->Layout.map;
    211 	if (lp->GetDefaultNumFloatingReconBuffers == NULL)
    212 		return (3 * raidPtr->numCol);
    213 	nrb = lp->GetDefaultNumFloatingReconBuffers(raidPtr);
    214 	return (nrb);
    215 }
    216 
    217 
    218 /******************************************************************************
    219  * creates and initializes a reconstruction buffer
    220  *****************************************************************************/
    221 RF_ReconBuffer_t *
    222 rf_MakeReconBuffer(RF_Raid_t *raidPtr, RF_RowCol_t col, RF_RbufType_t type)
    223 {
    224 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    225 	RF_ReconBuffer_t *t;
    226 	u_int   recon_buffer_size = rf_RaidAddressToByte(raidPtr, layoutPtr->SUsPerRU * layoutPtr->sectorsPerStripeUnit);
    227 
    228 	t = pool_get(&rf_pools.reconbuffer, PR_WAITOK);
    229 	RF_Malloc(t->buffer, recon_buffer_size, (void *));
    230 	t->raidPtr = raidPtr;
    231 	t->col = col;
    232 	t->priority = RF_IO_RECON_PRIORITY;
    233 	t->type = type;
    234 	t->pssPtr = NULL;
    235 	t->next = NULL;
    236 	return (t);
    237 }
    238 /******************************************************************************
    239  * frees a reconstruction buffer
    240  *****************************************************************************/
    241 void
    242 rf_FreeReconBuffer(RF_ReconBuffer_t *rbuf)
    243 {
    244 	RF_Raid_t *raidPtr = rbuf->raidPtr;
    245 	u_int   recon_buffer_size;
    246 
    247 	recon_buffer_size = rf_RaidAddressToByte(raidPtr, raidPtr->Layout.SUsPerRU * raidPtr->Layout.sectorsPerStripeUnit);
    248 
    249 	RF_Free(rbuf->buffer, recon_buffer_size);
    250 	pool_put(&rf_pools.reconbuffer, rbuf);
    251 }
    252 
    253 #if RF_DEBUG_RECON
    254 XXXX IF you use this, you really want to fix the locking in here.
    255 /******************************************************************************
    256  * debug only:  sanity check the number of floating recon bufs in use
    257  *****************************************************************************/
    258 void
    259 rf_CheckFloatingRbufCount(RF_Raid_t *raidPtr, int dolock)
    260 {
    261 	RF_ReconParityStripeStatus_t *p;
    262 	RF_PSStatusHeader_t *pssTable;
    263 	RF_ReconBuffer_t *rbuf;
    264 	int     i, j, sum = 0;
    265 
    266 	if (dolock)
    267 		RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
    268 	pssTable = raidPtr->reconControl->pssTable;
    269 
    270 	for (i = 0; i < raidPtr->pssTableSize; i++) {
    271 		RF_LOCK_MUTEX(pssTable[i].mutex);
    272 		for (p = pssTable[i].chain; p; p = p->next) {
    273 			rbuf = (RF_ReconBuffer_t *) p->rbuf;
    274 			if (rbuf && rbuf->type == RF_RBUF_TYPE_FLOATING)
    275 				sum++;
    276 
    277 			rbuf = (RF_ReconBuffer_t *) p->writeRbuf;
    278 			if (rbuf && rbuf->type == RF_RBUF_TYPE_FLOATING)
    279 				sum++;
    280 
    281 			for (j = 0; j < p->xorBufCount; j++) {
    282 				rbuf = (RF_ReconBuffer_t *) p->rbufsForXor[j];
    283 				RF_ASSERT(rbuf);
    284 				if (rbuf->type == RF_RBUF_TYPE_FLOATING)
    285 					sum++;
    286 			}
    287 		}
    288 		RF_UNLOCK_MUTEX(pssTable[i].mutex);
    289 	}
    290 
    291 	for (rbuf = raidPtr->reconControl->floatingRbufs; rbuf;
    292 	     rbuf = rbuf->next) {
    293 		if (rbuf->type == RF_RBUF_TYPE_FLOATING)
    294 			sum++;
    295 	}
    296 	for (rbuf = raidPtr->reconControl->committedRbufs; rbuf;
    297 	     rbuf = rbuf->next) {
    298 		if (rbuf->type == RF_RBUF_TYPE_FLOATING)
    299 			sum++;
    300 	}
    301 	for (rbuf = raidPtr->reconControl->fullBufferList; rbuf;
    302 	     rbuf = rbuf->next) {
    303 		if (rbuf->type == RF_RBUF_TYPE_FLOATING)
    304 			sum++;
    305 	}
    306 	RF_ASSERT(sum == raidPtr->numFloatingReconBufs);
    307 
    308 	if (dolock)
    309 		RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
    310 }
    311 #endif
    312 
    313