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