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