xref: /netbsd-src/sys/dev/raidframe/rf_psstatus.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: rf_psstatus.c,v 1.9 2002/09/14 17:53:58 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@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.9 2002/09/14 17:53:58 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 #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_print_unable_to_add_shutdown(__FILE__, __LINE__, rc);
111 		rf_ShutdownPSStatus(raidPtr);
112 		return (rc);
113 	}
114 	RF_FREELIST_PRIME_INIT_ARG(raidPtr->pss_freelist, RF_PSS_INITIAL, next,
115 	    (RF_ReconParityStripeStatus_t *), init_pss, raidPtr);
116 	return (0);
117 }
118 /*****************************************************************************************
119  * sets up the pss table
120  * We pre-allocate a bunch of entries to avoid as much as possible having to
121  * malloc up hash chain entries.
122  ****************************************************************************************/
123 RF_PSStatusHeader_t *
124 rf_MakeParityStripeStatusTable(raidPtr)
125 	RF_Raid_t *raidPtr;
126 {
127 	RF_PSStatusHeader_t *pssTable;
128 	int     i, j, rc;
129 
130 	RF_Calloc(pssTable, raidPtr->pssTableSize, sizeof(RF_PSStatusHeader_t), (RF_PSStatusHeader_t *));
131 	for (i = 0; i < raidPtr->pssTableSize; i++) {
132 		rc = rf_mutex_init(&pssTable[i].mutex);
133 		if (rc) {
134 			rf_print_unable_to_init_mutex(__FILE__, __LINE__, rc);
135 			/* fail and deallocate */
136 			for (j = 0; j < i; j++) {
137 				rf_mutex_destroy(&pssTable[i].mutex);
138 			}
139 			RF_Free(pssTable, raidPtr->pssTableSize * sizeof(RF_PSStatusHeader_t));
140 			return (NULL);
141 		}
142 	}
143 	return (pssTable);
144 }
145 
146 void
147 rf_FreeParityStripeStatusTable(raidPtr, pssTable)
148 	RF_Raid_t *raidPtr;
149 	RF_PSStatusHeader_t *pssTable;
150 {
151 	int     i;
152 
153 	if (rf_pssDebug)
154 		RealPrintPSStatusTable(raidPtr, pssTable);
155 	for (i = 0; i < raidPtr->pssTableSize; i++) {
156 		if (pssTable[i].chain) {
157 			printf("ERROR: pss hash chain not null at recon shutdown\n");
158 		}
159 		rf_mutex_destroy(&pssTable[i].mutex);
160 	}
161 	RF_Free(pssTable, raidPtr->pssTableSize * sizeof(RF_PSStatusHeader_t));
162 }
163 
164 
165 /* looks up the status structure for a parity stripe.
166  * if the create_flag is on, creates and returns the status structure it it doesn't exist
167  * otherwise returns NULL if the status structure does not exist
168  *
169  * ASSUMES THE PSS DESCRIPTOR IS LOCKED UPON ENTRY
170  */
171 RF_ReconParityStripeStatus_t *
172 rf_LookupRUStatus(
173     RF_Raid_t * raidPtr,
174     RF_PSStatusHeader_t * pssTable,
175     RF_StripeNum_t psID,
176     RF_ReconUnitNum_t which_ru,
177     RF_PSSFlags_t flags,	/* whether or not to create it if it doesn't
178 				 * exist + what flags to set initially */
179     int *created)
180 {
181 	RF_PSStatusHeader_t *hdr = &pssTable[RF_HASH_PSID(raidPtr, psID)];
182 	RF_ReconParityStripeStatus_t *p, *pssPtr = hdr->chain;
183 
184 	*created = 0;
185 	for (p = pssPtr; p; p = p->next) {
186 		if (p->parityStripeID == psID && p->which_ru == which_ru)
187 			break;
188 	}
189 
190 	if (!p && (flags & RF_PSS_CREATE)) {
191 		Dprintf2("PSS: creating pss for psid %ld ru %d\n", psID, which_ru);
192 		p = rf_AllocPSStatus(raidPtr);
193 		p->next = hdr->chain;
194 		hdr->chain = p;
195 
196 		p->parityStripeID = psID;
197 		p->which_ru = which_ru;
198 		p->flags = flags;
199 		p->rbuf = NULL;
200 		p->writeRbuf = NULL;
201 		p->blockCount = 0;
202 		p->procWaitList = NULL;
203 		p->blockWaitList = NULL;
204 		p->bufWaitList = NULL;
205 		*created = 1;
206 	} else
207 		if (p) {	/* we didn't create, but we want to specify
208 				 * some new status */
209 			p->flags |= flags;	/* add in whatever flags we're
210 						 * specifying */
211 		}
212 	if (p && (flags & RF_PSS_RECON_BLOCKED)) {
213 		p->blockCount++;/* if we're asking to block recon, bump the
214 				 * count */
215 		Dprintf3("raid%d: Blocked recon on psid %ld.  count now %d\n",
216 			 raidPtr->raidid, 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 		memset(p->issued, 0, 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