Home | History | Annotate | Line # | Download | only in raidframe
rf_psstatus.c revision 1.10
      1 /*	$NetBSD: rf_psstatus.c,v 1.10 2002/09/19 22:52:52 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  * psstatus.c
     32  *
     33  * The reconstruction code maintains a bunch of status related to the parity
     34  * stripes that are currently under reconstruction.  This header file defines
     35  * the status structures.
     36  *
     37  *****************************************************************************/
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: rf_psstatus.c,v 1.10 2002/09/19 22:52:52 oster Exp $");
     41 
     42 #include <dev/raidframe/raidframevar.h>
     43 
     44 #include "rf_raid.h"
     45 #include "rf_general.h"
     46 #include "rf_debugprint.h"
     47 #include "rf_freelist.h"
     48 #include "rf_psstatus.h"
     49 #include "rf_shutdown.h"
     50 
     51 #if RF_DEBUG_PSS
     52 #define Dprintf1(s,a)         if (rf_pssDebug) rf_debug_printf(s,(void *)((unsigned long)a),NULL,NULL,NULL,NULL,NULL,NULL,NULL)
     53 #define Dprintf2(s,a,b)       if (rf_pssDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),NULL,NULL,NULL,NULL,NULL,NULL)
     54 #define Dprintf3(s,a,b,c)     if (rf_pssDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),NULL,NULL,NULL,NULL,NULL)
     55 #else
     56 #define Dprintf1(s,a)
     57 #define Dprintf2(s,a,b)
     58 #define Dprintf3(s,a,b,c)
     59 #endif
     60 
     61 static void
     62 RealPrintPSStatusTable(RF_Raid_t * raidPtr,
     63     RF_PSStatusHeader_t * pssTable);
     64 
     65 #define RF_MAX_FREE_PSS  32
     66 #define RF_PSS_INC        8
     67 #define RF_PSS_INITIAL    4
     68 
     69 static int init_pss(RF_ReconParityStripeStatus_t *, RF_Raid_t *);
     70 static void clean_pss(RF_ReconParityStripeStatus_t *, RF_Raid_t *);
     71 static void rf_ShutdownPSStatus(void *);
     72 
     73 static int
     74 init_pss(p, raidPtr)
     75 	RF_ReconParityStripeStatus_t *p;
     76 	RF_Raid_t *raidPtr;
     77 {
     78 	RF_Calloc(p->issued, raidPtr->numCol, sizeof(char), (char *));
     79 	if (p->issued == NULL)
     80 		return (ENOMEM);
     81 	return (0);
     82 }
     83 
     84 static void
     85 clean_pss(p, raidPtr)
     86 	RF_ReconParityStripeStatus_t *p;
     87 	RF_Raid_t *raidPtr;
     88 {
     89 	RF_Free(p->issued, raidPtr->numCol * sizeof(char));
     90 }
     91 
     92 static void
     93 rf_ShutdownPSStatus(arg)
     94 	void   *arg;
     95 {
     96 	RF_Raid_t *raidPtr = (RF_Raid_t *) arg;
     97 
     98 	RF_FREELIST_DESTROY_CLEAN_ARG(raidPtr->pss_freelist, next, (RF_ReconParityStripeStatus_t *), clean_pss, raidPtr);
     99 }
    100 
    101 int
    102 rf_ConfigurePSStatus(
    103     RF_ShutdownList_t ** listp,
    104     RF_Raid_t * raidPtr,
    105     RF_Config_t * cfgPtr)
    106 {
    107 	int     rc;
    108 
    109 	raidPtr->pssTableSize = RF_PSS_DEFAULT_TABLESIZE;
    110 	RF_FREELIST_CREATE(raidPtr->pss_freelist, RF_MAX_FREE_PSS,
    111 	    RF_PSS_INC, sizeof(RF_ReconParityStripeStatus_t));
    112 	if (raidPtr->pss_freelist == NULL)
    113 		return (ENOMEM);
    114 	rc = rf_ShutdownCreate(listp, rf_ShutdownPSStatus, raidPtr);
    115 	if (rc) {
    116 		rf_print_unable_to_add_shutdown(__FILE__, __LINE__, rc);
    117 		rf_ShutdownPSStatus(raidPtr);
    118 		return (rc);
    119 	}
    120 	RF_FREELIST_PRIME_INIT_ARG(raidPtr->pss_freelist, RF_PSS_INITIAL, next,
    121 	    (RF_ReconParityStripeStatus_t *), init_pss, raidPtr);
    122 	return (0);
    123 }
    124 /*****************************************************************************************
    125  * sets up the pss table
    126  * We pre-allocate a bunch of entries to avoid as much as possible having to
    127  * malloc up hash chain entries.
    128  ****************************************************************************************/
    129 RF_PSStatusHeader_t *
    130 rf_MakeParityStripeStatusTable(raidPtr)
    131 	RF_Raid_t *raidPtr;
    132 {
    133 	RF_PSStatusHeader_t *pssTable;
    134 	int     i, j, rc;
    135 
    136 	RF_Calloc(pssTable, raidPtr->pssTableSize, sizeof(RF_PSStatusHeader_t), (RF_PSStatusHeader_t *));
    137 	for (i = 0; i < raidPtr->pssTableSize; i++) {
    138 		rc = rf_mutex_init(&pssTable[i].mutex);
    139 		if (rc) {
    140 			rf_print_unable_to_init_mutex(__FILE__, __LINE__, rc);
    141 			/* fail and deallocate */
    142 			for (j = 0; j < i; j++) {
    143 				rf_mutex_destroy(&pssTable[i].mutex);
    144 			}
    145 			RF_Free(pssTable, raidPtr->pssTableSize * sizeof(RF_PSStatusHeader_t));
    146 			return (NULL);
    147 		}
    148 	}
    149 	return (pssTable);
    150 }
    151 
    152 void
    153 rf_FreeParityStripeStatusTable(raidPtr, pssTable)
    154 	RF_Raid_t *raidPtr;
    155 	RF_PSStatusHeader_t *pssTable;
    156 {
    157 	int     i;
    158 
    159 #if RF_DEBUG_PSS
    160 	if (rf_pssDebug)
    161 		RealPrintPSStatusTable(raidPtr, pssTable);
    162 #endif
    163 	for (i = 0; i < raidPtr->pssTableSize; i++) {
    164 		if (pssTable[i].chain) {
    165 			printf("ERROR: pss hash chain not null at recon shutdown\n");
    166 		}
    167 		rf_mutex_destroy(&pssTable[i].mutex);
    168 	}
    169 	RF_Free(pssTable, raidPtr->pssTableSize * sizeof(RF_PSStatusHeader_t));
    170 }
    171 
    172 
    173 /* looks up the status structure for a parity stripe.
    174  * if the create_flag is on, creates and returns the status structure it it doesn't exist
    175  * otherwise returns NULL if the status structure does not exist
    176  *
    177  * ASSUMES THE PSS DESCRIPTOR IS LOCKED UPON ENTRY
    178  */
    179 RF_ReconParityStripeStatus_t *
    180 rf_LookupRUStatus(
    181     RF_Raid_t * raidPtr,
    182     RF_PSStatusHeader_t * pssTable,
    183     RF_StripeNum_t psID,
    184     RF_ReconUnitNum_t which_ru,
    185     RF_PSSFlags_t flags,	/* whether or not to create it if it doesn't
    186 				 * exist + what flags to set initially */
    187     int *created)
    188 {
    189 	RF_PSStatusHeader_t *hdr = &pssTable[RF_HASH_PSID(raidPtr, psID)];
    190 	RF_ReconParityStripeStatus_t *p, *pssPtr = hdr->chain;
    191 
    192 	*created = 0;
    193 	for (p = pssPtr; p; p = p->next) {
    194 		if (p->parityStripeID == psID && p->which_ru == which_ru)
    195 			break;
    196 	}
    197 
    198 	if (!p && (flags & RF_PSS_CREATE)) {
    199 		Dprintf2("PSS: creating pss for psid %ld ru %d\n", psID, which_ru);
    200 		p = rf_AllocPSStatus(raidPtr);
    201 		p->next = hdr->chain;
    202 		hdr->chain = p;
    203 
    204 		p->parityStripeID = psID;
    205 		p->which_ru = which_ru;
    206 		p->flags = flags;
    207 		p->rbuf = NULL;
    208 		p->writeRbuf = NULL;
    209 		p->blockCount = 0;
    210 		p->procWaitList = NULL;
    211 		p->blockWaitList = NULL;
    212 		p->bufWaitList = NULL;
    213 		*created = 1;
    214 	} else
    215 		if (p) {	/* we didn't create, but we want to specify
    216 				 * some new status */
    217 			p->flags |= flags;	/* add in whatever flags we're
    218 						 * specifying */
    219 		}
    220 	if (p && (flags & RF_PSS_RECON_BLOCKED)) {
    221 		p->blockCount++;/* if we're asking to block recon, bump the
    222 				 * count */
    223 		Dprintf3("raid%d: Blocked recon on psid %ld.  count now %d\n",
    224 			 raidPtr->raidid, psID, p->blockCount);
    225 	}
    226 	return (p);
    227 }
    228 /* deletes an entry from the parity stripe status table.  typically used
    229  * when an entry has been allocated solely to block reconstruction, and
    230  * no recon was requested while recon was blocked.  Assumes the hash
    231  * chain is ALREADY LOCKED.
    232  */
    233 void
    234 rf_PSStatusDelete(raidPtr, pssTable, pssPtr)
    235 	RF_Raid_t *raidPtr;
    236 	RF_PSStatusHeader_t *pssTable;
    237 	RF_ReconParityStripeStatus_t *pssPtr;
    238 {
    239 	RF_PSStatusHeader_t *hdr = &(pssTable[RF_HASH_PSID(raidPtr, pssPtr->parityStripeID)]);
    240 	RF_ReconParityStripeStatus_t *p = hdr->chain, *pt = NULL;
    241 
    242 	while (p) {
    243 		if (p == pssPtr) {
    244 			if (pt)
    245 				pt->next = p->next;
    246 			else
    247 				hdr->chain = p->next;
    248 			p->next = NULL;
    249 			rf_FreePSStatus(raidPtr, p);
    250 			return;
    251 		}
    252 		pt = p;
    253 		p = p->next;
    254 	}
    255 	RF_ASSERT(0);		/* we must find it here */
    256 }
    257 /* deletes an entry from the ps status table after reconstruction has completed */
    258 void
    259 rf_RemoveFromActiveReconTable(raidPtr, row, psid, which_ru)
    260 	RF_Raid_t *raidPtr;
    261 	RF_RowCol_t row;
    262 	RF_ReconUnitNum_t which_ru;
    263 	RF_StripeNum_t psid;
    264 {
    265 	RF_PSStatusHeader_t *hdr = &(raidPtr->reconControl[row]->pssTable[RF_HASH_PSID(raidPtr, psid)]);
    266 	RF_ReconParityStripeStatus_t *p, *pt;
    267 	RF_CallbackDesc_t *cb, *cb1;
    268 
    269 	RF_LOCK_MUTEX(hdr->mutex);
    270 	for (pt = NULL, p = hdr->chain; p; pt = p, p = p->next) {
    271 		if ((p->parityStripeID == psid) && (p->which_ru == which_ru))
    272 			break;
    273 	}
    274 	if (p == NULL) {
    275 		rf_PrintPSStatusTable(raidPtr, row);
    276 	}
    277 	RF_ASSERT(p);		/* it must be there */
    278 
    279 	Dprintf2("PSS: deleting pss for psid %ld ru %d\n", psid, which_ru);
    280 
    281 	/* delete this entry from the hash chain */
    282 	if (pt)
    283 		pt->next = p->next;
    284 	else
    285 		hdr->chain = p->next;
    286 	p->next = NULL;
    287 
    288 	RF_UNLOCK_MUTEX(hdr->mutex);
    289 
    290 	/* wakup anyone waiting on the parity stripe ID */
    291 	cb = p->procWaitList;
    292 	p->procWaitList = NULL;
    293 	while (cb) {
    294 		Dprintf1("Waking up access waiting on parity stripe ID %ld\n", p->parityStripeID);
    295 		cb1 = cb->next;
    296 		(cb->callbackFunc) (cb->callbackArg);
    297 
    298 		/* THIS IS WHAT THE ORIGINAL CODE HAD... the extra 0 is bogus,
    299 		 * IMHO */
    300 		/* (cb->callbackFunc)(cb->callbackArg, 0); */
    301 		rf_FreeCallbackDesc(cb);
    302 		cb = cb1;
    303 	}
    304 
    305 	rf_FreePSStatus(raidPtr, p);
    306 }
    307 
    308 RF_ReconParityStripeStatus_t *
    309 rf_AllocPSStatus(raidPtr)
    310 	RF_Raid_t *raidPtr;
    311 {
    312 	RF_ReconParityStripeStatus_t *p;
    313 
    314 	RF_FREELIST_GET_INIT_ARG(raidPtr->pss_freelist, p, next, (RF_ReconParityStripeStatus_t *), init_pss, raidPtr);
    315 	if (p) {
    316 		memset(p->issued, 0, raidPtr->numCol);
    317 	}
    318 	p->next = NULL;
    319 	/* no need to initialize here b/c the only place we're called from is
    320 	 * the above Lookup */
    321 	return (p);
    322 }
    323 
    324 void
    325 rf_FreePSStatus(raidPtr, p)
    326 	RF_Raid_t *raidPtr;
    327 	RF_ReconParityStripeStatus_t *p;
    328 {
    329 	RF_ASSERT(p->procWaitList == NULL);
    330 	RF_ASSERT(p->blockWaitList == NULL);
    331 	RF_ASSERT(p->bufWaitList == NULL);
    332 
    333 	RF_FREELIST_FREE_CLEAN_ARG(raidPtr->pss_freelist, p, next, clean_pss, raidPtr);
    334 }
    335 
    336 static void
    337 RealPrintPSStatusTable(raidPtr, pssTable)
    338 	RF_Raid_t *raidPtr;
    339 	RF_PSStatusHeader_t *pssTable;
    340 {
    341 	int     i, j, procsWaiting, blocksWaiting, bufsWaiting;
    342 	RF_ReconParityStripeStatus_t *p;
    343 	RF_CallbackDesc_t *cb;
    344 
    345 	printf("\nParity Stripe Status Table\n");
    346 	for (i = 0; i < raidPtr->pssTableSize; i++) {
    347 		for (p = pssTable[i].chain; p; p = p->next) {
    348 			procsWaiting = blocksWaiting = bufsWaiting = 0;
    349 			for (cb = p->procWaitList; cb; cb = cb->next)
    350 				procsWaiting++;
    351 			for (cb = p->blockWaitList; cb; cb = cb->next)
    352 				blocksWaiting++;
    353 			for (cb = p->bufWaitList; cb; cb = cb->next)
    354 				bufsWaiting++;
    355 			printf("PSID %ld RU %d : blockCount %d %d/%d/%d proc/block/buf waiting, issued ",
    356 			    (long) p->parityStripeID, p->which_ru, p->blockCount, procsWaiting, blocksWaiting, bufsWaiting);
    357 			for (j = 0; j < raidPtr->numCol; j++)
    358 				printf("%c", (p->issued[j]) ? '1' : '0');
    359 			if (!p->flags)
    360 				printf(" flags: (none)");
    361 			else {
    362 				if (p->flags & RF_PSS_UNDER_RECON)
    363 					printf(" under-recon");
    364 				if (p->flags & RF_PSS_FORCED_ON_WRITE)
    365 					printf(" forced-w");
    366 				if (p->flags & RF_PSS_FORCED_ON_READ)
    367 					printf(" forced-r");
    368 				if (p->flags & RF_PSS_RECON_BLOCKED)
    369 					printf(" blocked");
    370 				if (p->flags & RF_PSS_BUFFERWAIT)
    371 					printf(" bufwait");
    372 			}
    373 			printf("\n");
    374 		}
    375 	}
    376 }
    377 
    378 void
    379 rf_PrintPSStatusTable(raidPtr, row)
    380 	RF_Raid_t *raidPtr;
    381 	RF_RowCol_t row;
    382 {
    383 	RF_PSStatusHeader_t *pssTable = raidPtr->reconControl[row]->pssTable;
    384 	RealPrintPSStatusTable(raidPtr, pssTable);
    385 }
    386