1 /*        $NetBSD: rf_psstatus.c,v 1.39 2024/02/10 09:21:53 andvar 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.39 2024/02/10 09:21:53 andvar 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_psstatus.h"
48 #include "rf_shutdown.h"
49 
50 #if RF_DEBUG_PSS
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 #else
55 #define Dprintf1(s,a)
56 #define Dprintf2(s,a,b)
57 #define Dprintf3(s,a,b,c)
58 #endif
59 
60 static void
61 RealPrintPSStatusTable(RF_Raid_t * raidPtr,
62     RF_PSStatusHeader_t * pssTable);
63 
64 #define RF_MAX_FREE_PSS  32
65 #define RF_MIN_FREE_PSS   8
66 
67 static void rf_ShutdownPSStatus(void *);
68 
69 static void
rf_ShutdownPSStatus(void * arg)70 rf_ShutdownPSStatus(void *arg)
71 {
72           RF_Raid_t *raidPtr;
73 
74           raidPtr = (RF_Raid_t *) arg;
75 
76           pool_destroy(&raidPtr->pools.pss);
77 }
78 
79 int
rf_ConfigurePSStatus(RF_ShutdownList_t ** listp,RF_Raid_t * raidPtr,RF_Config_t * cfgPtr)80 rf_ConfigurePSStatus(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
81                          RF_Config_t *cfgPtr)
82 {
83 
84           rf_pool_init(raidPtr, raidPtr->poolNames.pss, &raidPtr->pools.pss, sizeof(RF_ReconParityStripeStatus_t),
85                          "pss", RF_MIN_FREE_PSS, RF_MAX_FREE_PSS);
86           rf_ShutdownCreate(listp, rf_ShutdownPSStatus, raidPtr);
87 
88           return (0);
89 }
90 
91 void
rf_InitPSStatus(RF_Raid_t * raidPtr)92 rf_InitPSStatus(RF_Raid_t *raidPtr)
93 {
94           raidPtr->pssTableSize = RF_PSS_DEFAULT_TABLESIZE;
95 }
96 
97 
98 /*****************************************************************************************
99  * sets up the pss table
100  * We pre-allocate a bunch of entries to avoid as much as possible having to
101  * malloc up hash chain entries.
102  ****************************************************************************************/
103 RF_PSStatusHeader_t *
rf_MakeParityStripeStatusTable(RF_Raid_t * raidPtr)104 rf_MakeParityStripeStatusTable(RF_Raid_t *raidPtr)
105 {
106           RF_PSStatusHeader_t *pssTable;
107           int     i;
108 
109           pssTable = RF_Malloc(raidPtr->pssTableSize * sizeof(*pssTable));
110           for (i = 0; i < raidPtr->pssTableSize; i++) {
111                     rf_init_mutex2(pssTable[i].mutex, IPL_VM);
112                     rf_init_cond2(pssTable[i].cond, "rfpsslk");
113           }
114           return (pssTable);
115 }
116 
117 void
rf_FreeParityStripeStatusTable(RF_Raid_t * raidPtr,RF_PSStatusHeader_t * pssTable)118 rf_FreeParityStripeStatusTable(RF_Raid_t *raidPtr,
119                                      RF_PSStatusHeader_t *pssTable)
120 {
121           int     i;
122 
123 #if RF_DEBUG_PSS
124           if (rf_pssDebug)
125                     RealPrintPSStatusTable(raidPtr, pssTable);
126 
127           for (i = 0; i < raidPtr->pssTableSize; i++) {
128                     if (pssTable[i].chain) {
129                               printf("ERROR: pss hash chain not null at recon shutdown\n");
130                     }
131           }
132 #endif
133           for (i = 0; i < raidPtr->pssTableSize; i++) {
134                     rf_destroy_mutex2(pssTable[i].mutex);
135                     rf_destroy_cond2(pssTable[i].cond);
136           }
137           RF_Free(pssTable, raidPtr->pssTableSize * sizeof(RF_PSStatusHeader_t));
138 }
139 
140 
141 /* looks up the status structure for a parity stripe.
142  * if the create_flag is on, uses (and returns) newpssPtr if
143  * a parity status structure doesn't exist
144  * otherwise returns NULL if the status structure does not exist
145  *
146  * ASSUMES THE PSS DESCRIPTOR IS LOCKED UPON ENTRY
147  *
148  * flags - whether or not to use newpssPtr if the needed PSS
149  *         doesn't exist and what flags to set it to initially
150  */
151 RF_ReconParityStripeStatus_t *
rf_LookupRUStatus(RF_Raid_t * raidPtr,RF_PSStatusHeader_t * pssTable,RF_StripeNum_t psID,RF_ReconUnitNum_t which_ru,RF_PSSFlags_t flags,RF_ReconParityStripeStatus_t * newpssPtr)152 rf_LookupRUStatus(RF_Raid_t *raidPtr, RF_PSStatusHeader_t *pssTable,
153                       RF_StripeNum_t psID, RF_ReconUnitNum_t which_ru,
154                       RF_PSSFlags_t flags, RF_ReconParityStripeStatus_t *newpssPtr)
155 {
156           RF_PSStatusHeader_t *hdr = &pssTable[RF_HASH_PSID(raidPtr, psID)];
157           RF_ReconParityStripeStatus_t *p, *pssPtr = hdr->chain;
158 
159           for (p = pssPtr; p; p = p->next) {
160                     if (p->parityStripeID == psID && p->which_ru == which_ru)
161                               break;
162           }
163 
164           if (!p && (flags & RF_PSS_CREATE)) {
165                     p = newpssPtr;
166                     p->next = hdr->chain;
167                     hdr->chain = p;
168 
169                     p->parityStripeID = psID;
170                     p->which_ru = which_ru;
171                     p->flags = flags;
172                     p->rbuf = NULL;
173                     p->writeRbuf = NULL;
174                     p->xorBufCount = 0;
175                     p->blockCount = 0;
176                     p->procWaitList = NULL;
177                     p->blockWaitList = NULL;
178                     p->bufWaitList = NULL;
179           } else
180                     if (p) {  /* we didn't create, but we want to specify
181                                          * some new status */
182                               p->flags |= flags;  /* add in whatever flags we're
183                                                              * specifying */
184                     }
185           if (p && (flags & RF_PSS_RECON_BLOCKED)) {
186                     p->blockCount++;/* if we're asking to block recon, bump the
187                                          * count */
188                     Dprintf3("raid%d: Blocked recon on psid %ld.  count now %d\n",
189                                raidPtr->raidid, psID, p->blockCount);
190           }
191           return (p);
192 }
193 /* deletes an entry from the parity stripe status table.  typically used
194  * when an entry has been allocated solely to block reconstruction, and
195  * no recon was requested while recon was blocked.  Assumes the hash
196  * chain is ALREADY LOCKED.
197  */
198 void
rf_PSStatusDelete(RF_Raid_t * raidPtr,RF_PSStatusHeader_t * pssTable,RF_ReconParityStripeStatus_t * pssPtr)199 rf_PSStatusDelete(RF_Raid_t *raidPtr, RF_PSStatusHeader_t *pssTable,
200                       RF_ReconParityStripeStatus_t *pssPtr)
201 {
202           RF_PSStatusHeader_t *hdr = &(pssTable[RF_HASH_PSID(raidPtr, pssPtr->parityStripeID)]);
203           RF_ReconParityStripeStatus_t *p = hdr->chain, *pt = NULL;
204 
205           while (p) {
206                     if (p == pssPtr) {
207                               if (pt)
208                                         pt->next = p->next;
209                               else
210                                         hdr->chain = p->next;
211                               p->next = NULL;
212                               rf_FreePSStatus(raidPtr, p);
213                               return;
214                     }
215                     pt = p;
216                     p = p->next;
217           }
218           RF_ASSERT(0);                 /* we must find it here */
219 }
220 /* deletes an entry from the ps status table after reconstruction has completed */
221 void
rf_RemoveFromActiveReconTable(RF_Raid_t * raidPtr,RF_StripeNum_t psid,RF_ReconUnitNum_t which_ru)222 rf_RemoveFromActiveReconTable(RF_Raid_t *raidPtr, RF_StripeNum_t psid,
223                                     RF_ReconUnitNum_t which_ru)
224 {
225           RF_PSStatusHeader_t *hdr = &(raidPtr->reconControl->pssTable[RF_HASH_PSID(raidPtr, psid)]);
226           RF_ReconParityStripeStatus_t *p, *pt;
227           RF_CallbackFuncDesc_t *cb, *cb1;
228 
229           rf_lock_mutex2(hdr->mutex);
230           while(hdr->lock) {
231                     rf_wait_cond2(hdr->cond, hdr->mutex);
232           }
233           hdr->lock = 1;
234           rf_unlock_mutex2(hdr->mutex);
235           for (pt = NULL, p = hdr->chain; p; pt = p, p = p->next) {
236                     if ((p->parityStripeID == psid) && (p->which_ru == which_ru))
237                               break;
238           }
239           if (p == NULL) {
240                     rf_PrintPSStatusTable(raidPtr);
241           }
242           RF_ASSERT(p);                 /* it must be there */
243 
244           Dprintf2("PSS: deleting pss for psid %ld ru %d\n", psid, which_ru);
245 
246           /* delete this entry from the hash chain */
247           if (pt)
248                     pt->next = p->next;
249           else
250                     hdr->chain = p->next;
251           p->next = NULL;
252 
253           rf_lock_mutex2(hdr->mutex);
254           hdr->lock = 0;
255           rf_unlock_mutex2(hdr->mutex);
256 
257           /* wakeup anyone waiting on the parity stripe ID */
258           cb = p->procWaitList;
259           p->procWaitList = NULL;
260           while (cb) {
261                     Dprintf1("Waking up access waiting on parity stripe ID %ld\n", p->parityStripeID);
262                     cb1 = cb->next;
263                     (cb->callbackFunc) (cb->callbackArg);
264                     rf_FreeCallbackFuncDesc(raidPtr, cb);
265                     cb = cb1;
266           }
267 
268           rf_FreePSStatus(raidPtr, p);
269 }
270 
271 RF_ReconParityStripeStatus_t *
rf_AllocPSStatus(RF_Raid_t * raidPtr)272 rf_AllocPSStatus(RF_Raid_t *raidPtr)
273 {
274           return pool_get(&raidPtr->pools.pss, PR_WAITOK | PR_ZERO);
275 }
276 
277 void
rf_FreePSStatus(RF_Raid_t * raidPtr,RF_ReconParityStripeStatus_t * p)278 rf_FreePSStatus(RF_Raid_t *raidPtr, RF_ReconParityStripeStatus_t *p)
279 {
280           RF_ASSERT(p->procWaitList == NULL);
281           RF_ASSERT(p->blockWaitList == NULL);
282           RF_ASSERT(p->bufWaitList == NULL);
283 
284           pool_put(&raidPtr->pools.pss, p);
285 }
286 
287 static void
RealPrintPSStatusTable(RF_Raid_t * raidPtr,RF_PSStatusHeader_t * pssTable)288 RealPrintPSStatusTable(RF_Raid_t *raidPtr, RF_PSStatusHeader_t *pssTable)
289 {
290           int     i, j, procsWaiting, blocksWaiting, bufsWaiting;
291           RF_ReconParityStripeStatus_t *p;
292           RF_CallbackValueDesc_t *vb;
293           RF_CallbackFuncDesc_t *fb;
294 
295           printf("\nParity Stripe Status Table\n");
296           for (i = 0; i < raidPtr->pssTableSize; i++) {
297                     for (p = pssTable[i].chain; p; p = p->next) {
298                               procsWaiting = blocksWaiting = bufsWaiting = 0;
299                               for (fb = p->procWaitList; fb; fb = fb->next)
300                                         procsWaiting++;
301                               for (vb = p->blockWaitList; vb; vb = vb->next)
302                                         blocksWaiting++;
303                               for (vb = p->bufWaitList; vb; vb = vb->next)
304                                         bufsWaiting++;
305                               printf("PSID %ld RU %d : blockCount %d %d/%d/%d proc/block/buf waiting, issued ",
306                                   (long) p->parityStripeID, p->which_ru, p->blockCount, procsWaiting, blocksWaiting, bufsWaiting);
307                               for (j = 0; j < raidPtr->numCol; j++)
308                                         printf("%c", (p->issued[j]) ? '1' : '0');
309                               if (!p->flags)
310                                         printf(" flags: (none)");
311                               else {
312                                         if (p->flags & RF_PSS_UNDER_RECON)
313                                                   printf(" under-recon");
314                                         if (p->flags & RF_PSS_FORCED_ON_WRITE)
315                                                   printf(" forced-w");
316                                         if (p->flags & RF_PSS_FORCED_ON_READ)
317                                                   printf(" forced-r");
318                                         if (p->flags & RF_PSS_RECON_BLOCKED)
319                                                   printf(" blocked");
320                                         if (p->flags & RF_PSS_BUFFERWAIT)
321                                                   printf(" bufwait");
322                               }
323                               printf("\n");
324                     }
325           }
326 }
327 
328 void
rf_PrintPSStatusTable(RF_Raid_t * raidPtr)329 rf_PrintPSStatusTable(RF_Raid_t *raidPtr)
330 {
331           RF_PSStatusHeader_t *pssTable = raidPtr->reconControl->pssTable;
332           RealPrintPSStatusTable(raidPtr, pssTable);
333 }
334