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