xref: /netbsd-src/sys/dev/raidframe/rf_psstatus.c (revision 8a8f936f250a330d54f8a24ed0e92aadf9743a7b)
1 /*	$NetBSD: rf_psstatus.c,v 1.7 2001/10/04 15:58:55 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 <dev/raidframe/raidframevar.h>
40 
41 #include "rf_raid.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 		p->blockCount++;/* if we're asking to block recon, bump the
213 				 * count */
214 		Dprintf3("raid%d: Blocked recon on psid %ld.  count now %d\n",
215 			 raidPtr->raidid, psID, p->blockCount);
216 	}
217 	return (p);
218 }
219 /* deletes an entry from the parity stripe status table.  typically used
220  * when an entry has been allocated solely to block reconstruction, and
221  * no recon was requested while recon was blocked.  Assumes the hash
222  * chain is ALREADY LOCKED.
223  */
224 void
225 rf_PSStatusDelete(raidPtr, pssTable, pssPtr)
226 	RF_Raid_t *raidPtr;
227 	RF_PSStatusHeader_t *pssTable;
228 	RF_ReconParityStripeStatus_t *pssPtr;
229 {
230 	RF_PSStatusHeader_t *hdr = &(pssTable[RF_HASH_PSID(raidPtr, pssPtr->parityStripeID)]);
231 	RF_ReconParityStripeStatus_t *p = hdr->chain, *pt = NULL;
232 
233 	while (p) {
234 		if (p == pssPtr) {
235 			if (pt)
236 				pt->next = p->next;
237 			else
238 				hdr->chain = p->next;
239 			p->next = NULL;
240 			rf_FreePSStatus(raidPtr, p);
241 			return;
242 		}
243 		pt = p;
244 		p = p->next;
245 	}
246 	RF_ASSERT(0);		/* we must find it here */
247 }
248 /* deletes an entry from the ps status table after reconstruction has completed */
249 void
250 rf_RemoveFromActiveReconTable(raidPtr, row, psid, which_ru)
251 	RF_Raid_t *raidPtr;
252 	RF_RowCol_t row;
253 	RF_ReconUnitNum_t which_ru;
254 	RF_StripeNum_t psid;
255 {
256 	RF_PSStatusHeader_t *hdr = &(raidPtr->reconControl[row]->pssTable[RF_HASH_PSID(raidPtr, psid)]);
257 	RF_ReconParityStripeStatus_t *p, *pt;
258 	RF_CallbackDesc_t *cb, *cb1;
259 
260 	RF_LOCK_MUTEX(hdr->mutex);
261 	for (pt = NULL, p = hdr->chain; p; pt = p, p = p->next) {
262 		if ((p->parityStripeID == psid) && (p->which_ru == which_ru))
263 			break;
264 	}
265 	if (p == NULL) {
266 		rf_PrintPSStatusTable(raidPtr, row);
267 	}
268 	RF_ASSERT(p);		/* it must be there */
269 
270 	Dprintf2("PSS: deleting pss for psid %ld ru %d\n", psid, which_ru);
271 
272 	/* delete this entry from the hash chain */
273 	if (pt)
274 		pt->next = p->next;
275 	else
276 		hdr->chain = p->next;
277 	p->next = NULL;
278 
279 	RF_UNLOCK_MUTEX(hdr->mutex);
280 
281 	/* wakup anyone waiting on the parity stripe ID */
282 	cb = p->procWaitList;
283 	p->procWaitList = NULL;
284 	while (cb) {
285 		Dprintf1("Waking up access waiting on parity stripe ID %ld\n", p->parityStripeID);
286 		cb1 = cb->next;
287 		(cb->callbackFunc) (cb->callbackArg);
288 
289 		/* THIS IS WHAT THE ORIGINAL CODE HAD... the extra 0 is bogus,
290 		 * IMHO */
291 		/* (cb->callbackFunc)(cb->callbackArg, 0); */
292 		rf_FreeCallbackDesc(cb);
293 		cb = cb1;
294 	}
295 
296 	rf_FreePSStatus(raidPtr, p);
297 }
298 
299 RF_ReconParityStripeStatus_t *
300 rf_AllocPSStatus(raidPtr)
301 	RF_Raid_t *raidPtr;
302 {
303 	RF_ReconParityStripeStatus_t *p;
304 
305 	RF_FREELIST_GET_INIT_ARG(raidPtr->pss_freelist, p, next, (RF_ReconParityStripeStatus_t *), init_pss, raidPtr);
306 	if (p) {
307 		memset(p->issued, 0, raidPtr->numCol);
308 	}
309 	p->next = NULL;
310 	/* no need to initialize here b/c the only place we're called from is
311 	 * the above Lookup */
312 	return (p);
313 }
314 
315 void
316 rf_FreePSStatus(raidPtr, p)
317 	RF_Raid_t *raidPtr;
318 	RF_ReconParityStripeStatus_t *p;
319 {
320 	RF_ASSERT(p->procWaitList == NULL);
321 	RF_ASSERT(p->blockWaitList == NULL);
322 	RF_ASSERT(p->bufWaitList == NULL);
323 
324 	RF_FREELIST_FREE_CLEAN_ARG(raidPtr->pss_freelist, p, next, clean_pss, raidPtr);
325 }
326 
327 static void
328 RealPrintPSStatusTable(raidPtr, pssTable)
329 	RF_Raid_t *raidPtr;
330 	RF_PSStatusHeader_t *pssTable;
331 {
332 	int     i, j, procsWaiting, blocksWaiting, bufsWaiting;
333 	RF_ReconParityStripeStatus_t *p;
334 	RF_CallbackDesc_t *cb;
335 
336 	printf("\nParity Stripe Status Table\n");
337 	for (i = 0; i < raidPtr->pssTableSize; i++) {
338 		for (p = pssTable[i].chain; p; p = p->next) {
339 			procsWaiting = blocksWaiting = bufsWaiting = 0;
340 			for (cb = p->procWaitList; cb; cb = cb->next)
341 				procsWaiting++;
342 			for (cb = p->blockWaitList; cb; cb = cb->next)
343 				blocksWaiting++;
344 			for (cb = p->bufWaitList; cb; cb = cb->next)
345 				bufsWaiting++;
346 			printf("PSID %ld RU %d : blockCount %d %d/%d/%d proc/block/buf waiting, issued ",
347 			    (long) p->parityStripeID, p->which_ru, p->blockCount, procsWaiting, blocksWaiting, bufsWaiting);
348 			for (j = 0; j < raidPtr->numCol; j++)
349 				printf("%c", (p->issued[j]) ? '1' : '0');
350 			if (!p->flags)
351 				printf(" flags: (none)");
352 			else {
353 				if (p->flags & RF_PSS_UNDER_RECON)
354 					printf(" under-recon");
355 				if (p->flags & RF_PSS_FORCED_ON_WRITE)
356 					printf(" forced-w");
357 				if (p->flags & RF_PSS_FORCED_ON_READ)
358 					printf(" forced-r");
359 				if (p->flags & RF_PSS_RECON_BLOCKED)
360 					printf(" blocked");
361 				if (p->flags & RF_PSS_BUFFERWAIT)
362 					printf(" bufwait");
363 			}
364 			printf("\n");
365 		}
366 	}
367 }
368 
369 void
370 rf_PrintPSStatusTable(raidPtr, row)
371 	RF_Raid_t *raidPtr;
372 	RF_RowCol_t row;
373 {
374 	RF_PSStatusHeader_t *pssTable = raidPtr->reconControl[row]->pssTable;
375 	RealPrintPSStatusTable(raidPtr, pssTable);
376 }
377