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