xref: /netbsd-src/sys/dev/raidframe/rf_psstatus.c (revision bada23909e740596d0a3785a73bd3583a9807fb8)
1 /*	$NetBSD: rf_psstatus.c,v 1.3 1999/02/05 00:06:15 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 "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 #include "rf_sys.h"
48 
49 #define Dprintf1(s,a)         if (rf_pssDebug) rf_debug_printf(s,(void *)((unsigned long)a),NULL,NULL,NULL,NULL,NULL,NULL,NULL)
50 #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)
51 #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)
52 
53 static void
54 RealPrintPSStatusTable(RF_Raid_t * raidPtr,
55     RF_PSStatusHeader_t * pssTable);
56 
57 #define RF_MAX_FREE_PSS  32
58 #define RF_PSS_INC        8
59 #define RF_PSS_INITIAL    4
60 
61 static int init_pss(RF_ReconParityStripeStatus_t *, RF_Raid_t *);
62 static void clean_pss(RF_ReconParityStripeStatus_t *, RF_Raid_t *);
63 static void rf_ShutdownPSStatus(void *);
64 
65 static int
66 init_pss(p, raidPtr)
67 	RF_ReconParityStripeStatus_t *p;
68 	RF_Raid_t *raidPtr;
69 {
70 	RF_Calloc(p->issued, raidPtr->numCol, sizeof(char), (char *));
71 	if (p->issued == NULL)
72 		return (ENOMEM);
73 	return (0);
74 }
75 
76 static void
77 clean_pss(p, raidPtr)
78 	RF_ReconParityStripeStatus_t *p;
79 	RF_Raid_t *raidPtr;
80 {
81 	RF_Free(p->issued, raidPtr->numCol * sizeof(char));
82 }
83 
84 static void
85 rf_ShutdownPSStatus(arg)
86 	void   *arg;
87 {
88 	RF_Raid_t *raidPtr = (RF_Raid_t *) arg;
89 
90 	RF_FREELIST_DESTROY_CLEAN_ARG(raidPtr->pss_freelist, next, (RF_ReconParityStripeStatus_t *), clean_pss, raidPtr);
91 }
92 
93 int
94 rf_ConfigurePSStatus(
95     RF_ShutdownList_t ** listp,
96     RF_Raid_t * raidPtr,
97     RF_Config_t * cfgPtr)
98 {
99 	int     rc;
100 
101 	raidPtr->pssTableSize = RF_PSS_DEFAULT_TABLESIZE;
102 	RF_FREELIST_CREATE(raidPtr->pss_freelist, RF_MAX_FREE_PSS,
103 	    RF_PSS_INC, sizeof(RF_ReconParityStripeStatus_t));
104 	if (raidPtr->pss_freelist == NULL)
105 		return (ENOMEM);
106 	rc = rf_ShutdownCreate(listp, rf_ShutdownPSStatus, raidPtr);
107 	if (rc) {
108 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
109 		    __FILE__, __LINE__, rc);
110 		rf_ShutdownPSStatus(raidPtr);
111 		return (rc);
112 	}
113 	RF_FREELIST_PRIME_INIT_ARG(raidPtr->pss_freelist, RF_PSS_INITIAL, next,
114 	    (RF_ReconParityStripeStatus_t *), init_pss, raidPtr);
115 	return (0);
116 }
117 /*****************************************************************************************
118  * sets up the pss table
119  * We pre-allocate a bunch of entries to avoid as much as possible having to
120  * malloc up hash chain entries.
121  ****************************************************************************************/
122 RF_PSStatusHeader_t *
123 rf_MakeParityStripeStatusTable(raidPtr)
124 	RF_Raid_t *raidPtr;
125 {
126 	RF_PSStatusHeader_t *pssTable;
127 	int     i, j, rc;
128 
129 	RF_Calloc(pssTable, raidPtr->pssTableSize, sizeof(RF_PSStatusHeader_t), (RF_PSStatusHeader_t *));
130 	for (i = 0; i < raidPtr->pssTableSize; i++) {
131 		rc = rf_mutex_init(&pssTable[i].mutex);
132 		if (rc) {
133 			RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
134 			    __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 		int     tid;
214 		rf_get_threadid(tid);
215 		p->blockCount++;/* if we're asking to block recon, bump the
216 				 * count */
217 		Dprintf3("[%d] Blocked recon on psid %ld.  count now %d\n", tid, psID, p->blockCount);
218 	}
219 	return (p);
220 }
221 /* deletes an entry from the parity stripe status table.  typically used
222  * when an entry has been allocated solely to block reconstruction, and
223  * no recon was requested while recon was blocked.  Assumes the hash
224  * chain is ALREADY LOCKED.
225  */
226 void
227 rf_PSStatusDelete(raidPtr, pssTable, pssPtr)
228 	RF_Raid_t *raidPtr;
229 	RF_PSStatusHeader_t *pssTable;
230 	RF_ReconParityStripeStatus_t *pssPtr;
231 {
232 	RF_PSStatusHeader_t *hdr = &(pssTable[RF_HASH_PSID(raidPtr, pssPtr->parityStripeID)]);
233 	RF_ReconParityStripeStatus_t *p = hdr->chain, *pt = NULL;
234 
235 	while (p) {
236 		if (p == pssPtr) {
237 			if (pt)
238 				pt->next = p->next;
239 			else
240 				hdr->chain = p->next;
241 			p->next = NULL;
242 			rf_FreePSStatus(raidPtr, p);
243 			return;
244 		}
245 		pt = p;
246 		p = p->next;
247 	}
248 	RF_ASSERT(0);		/* we must find it here */
249 }
250 /* deletes an entry from the ps status table after reconstruction has completed */
251 void
252 rf_RemoveFromActiveReconTable(raidPtr, row, psid, which_ru)
253 	RF_Raid_t *raidPtr;
254 	RF_RowCol_t row;
255 	RF_ReconUnitNum_t which_ru;
256 	RF_StripeNum_t psid;
257 {
258 	RF_PSStatusHeader_t *hdr = &(raidPtr->reconControl[row]->pssTable[RF_HASH_PSID(raidPtr, psid)]);
259 	RF_ReconParityStripeStatus_t *p, *pt;
260 	RF_CallbackDesc_t *cb, *cb1;
261 
262 	RF_LOCK_MUTEX(hdr->mutex);
263 	for (pt = NULL, p = hdr->chain; p; pt = p, p = p->next) {
264 		if ((p->parityStripeID == psid) && (p->which_ru == which_ru))
265 			break;
266 	}
267 	if (p == NULL) {
268 		rf_PrintPSStatusTable(raidPtr, row);
269 	}
270 	RF_ASSERT(p);		/* it must be there */
271 
272 	Dprintf2("PSS: deleting pss for psid %ld ru %d\n", psid, which_ru);
273 
274 	/* delete this entry from the hash chain */
275 	if (pt)
276 		pt->next = p->next;
277 	else
278 		hdr->chain = p->next;
279 	p->next = NULL;
280 
281 	RF_UNLOCK_MUTEX(hdr->mutex);
282 
283 	/* wakup anyone waiting on the parity stripe ID */
284 	cb = p->procWaitList;
285 	p->procWaitList = NULL;
286 	while (cb) {
287 		Dprintf1("Waking up access waiting on parity stripe ID %ld\n", p->parityStripeID);
288 		cb1 = cb->next;
289 		(cb->callbackFunc) (cb->callbackArg);
290 
291 		/* THIS IS WHAT THE ORIGINAL CODE HAD... the extra 0 is bogus,
292 		 * IMHO */
293 		/* (cb->callbackFunc)(cb->callbackArg, 0); */
294 		rf_FreeCallbackDesc(cb);
295 		cb = cb1;
296 	}
297 
298 	rf_FreePSStatus(raidPtr, p);
299 }
300 
301 RF_ReconParityStripeStatus_t *
302 rf_AllocPSStatus(raidPtr)
303 	RF_Raid_t *raidPtr;
304 {
305 	RF_ReconParityStripeStatus_t *p;
306 
307 	RF_FREELIST_GET_INIT_ARG(raidPtr->pss_freelist, p, next, (RF_ReconParityStripeStatus_t *), init_pss, raidPtr);
308 	if (p) {
309 		bzero(p->issued, raidPtr->numCol);
310 	}
311 	p->next = NULL;
312 	/* no need to initialize here b/c the only place we're called from is
313 	 * the above Lookup */
314 	return (p);
315 }
316 
317 void
318 rf_FreePSStatus(raidPtr, p)
319 	RF_Raid_t *raidPtr;
320 	RF_ReconParityStripeStatus_t *p;
321 {
322 	RF_ASSERT(p->procWaitList == NULL);
323 	RF_ASSERT(p->blockWaitList == NULL);
324 	RF_ASSERT(p->bufWaitList == NULL);
325 
326 	RF_FREELIST_FREE_CLEAN_ARG(raidPtr->pss_freelist, p, next, clean_pss, raidPtr);
327 }
328 
329 static void
330 RealPrintPSStatusTable(raidPtr, pssTable)
331 	RF_Raid_t *raidPtr;
332 	RF_PSStatusHeader_t *pssTable;
333 {
334 	int     i, j, procsWaiting, blocksWaiting, bufsWaiting;
335 	RF_ReconParityStripeStatus_t *p;
336 	RF_CallbackDesc_t *cb;
337 
338 	printf("\nParity Stripe Status Table\n");
339 	for (i = 0; i < raidPtr->pssTableSize; i++) {
340 		for (p = pssTable[i].chain; p; p = p->next) {
341 			procsWaiting = blocksWaiting = bufsWaiting = 0;
342 			for (cb = p->procWaitList; cb; cb = cb->next)
343 				procsWaiting++;
344 			for (cb = p->blockWaitList; cb; cb = cb->next)
345 				blocksWaiting++;
346 			for (cb = p->bufWaitList; cb; cb = cb->next)
347 				bufsWaiting++;
348 			printf("PSID %ld RU %d : blockCount %d %d/%d/%d proc/block/buf waiting, issued ",
349 			    (long) p->parityStripeID, p->which_ru, p->blockCount, procsWaiting, blocksWaiting, bufsWaiting);
350 			for (j = 0; j < raidPtr->numCol; j++)
351 				printf("%c", (p->issued[j]) ? '1' : '0');
352 			if (!p->flags)
353 				printf(" flags: (none)");
354 			else {
355 				if (p->flags & RF_PSS_UNDER_RECON)
356 					printf(" under-recon");
357 				if (p->flags & RF_PSS_FORCED_ON_WRITE)
358 					printf(" forced-w");
359 				if (p->flags & RF_PSS_FORCED_ON_READ)
360 					printf(" forced-r");
361 				if (p->flags & RF_PSS_RECON_BLOCKED)
362 					printf(" blocked");
363 				if (p->flags & RF_PSS_BUFFERWAIT)
364 					printf(" bufwait");
365 			}
366 			printf("\n");
367 		}
368 	}
369 }
370 
371 void
372 rf_PrintPSStatusTable(raidPtr, row)
373 	RF_Raid_t *raidPtr;
374 	RF_RowCol_t row;
375 {
376 	RF_PSStatusHeader_t *pssTable = raidPtr->reconControl[row]->pssTable;
377 	RealPrintPSStatusTable(raidPtr, pssTable);
378 }
379