rf_reconutil.c revision 1.15 1 /* $NetBSD: rf_reconutil.c,v 1.15 2003/12/29 02:38:18 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 (at) 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 * rf_reconutil.c -- reconstruction utilities
31 ********************************************/
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: rf_reconutil.c,v 1.15 2003/12/29 02:38:18 oster Exp $");
35
36 #include <dev/raidframe/raidframevar.h>
37
38 #include "rf_raid.h"
39 #include "rf_desc.h"
40 #include "rf_reconutil.h"
41 #include "rf_reconbuffer.h"
42 #include "rf_general.h"
43 #include "rf_decluster.h"
44 #include "rf_raid5_rotatedspare.h"
45 #include "rf_interdecluster.h"
46 #include "rf_chaindecluster.h"
47
48 /*******************************************************************
49 * allocates/frees the reconstruction control information structures
50 *******************************************************************/
51 RF_ReconCtrl_t *
52 rf_MakeReconControl(reconDesc, fcol, scol)
53 RF_RaidReconDesc_t *reconDesc;
54 RF_RowCol_t fcol; /* failed column */
55 RF_RowCol_t scol; /* identifies which spare we're using */
56 {
57 RF_Raid_t *raidPtr = reconDesc->raidPtr;
58 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
59 RF_ReconUnitCount_t RUsPerPU = layoutPtr->SUsPerPU / layoutPtr->SUsPerRU;
60 RF_ReconUnitCount_t numSpareRUs;
61 RF_ReconCtrl_t *reconCtrlPtr;
62 RF_ReconBuffer_t *rbuf;
63 const RF_LayoutSW_t *lp;
64 #if (RF_INCLUDE_PARITY_DECLUSTERING_DS > 0)
65 int retcode;
66 #endif
67 int rc;
68 RF_RowCol_t i;
69
70 lp = raidPtr->Layout.map;
71
72 /* make and zero the global reconstruction structure and the per-disk
73 * structure */
74 RF_Calloc(reconCtrlPtr, 1, sizeof(RF_ReconCtrl_t), (RF_ReconCtrl_t *));
75
76 /* note: this zeros the perDiskInfo */
77 RF_Calloc(reconCtrlPtr->perDiskInfo, raidPtr->numCol,
78 sizeof(RF_PerDiskReconCtrl_t), (RF_PerDiskReconCtrl_t *));
79 reconCtrlPtr->reconDesc = reconDesc;
80 reconCtrlPtr->fcol = fcol;
81 reconCtrlPtr->spareCol = scol;
82 reconCtrlPtr->lastPSID = layoutPtr->numStripe / layoutPtr->SUsPerPU;
83 reconCtrlPtr->percentComplete = 0;
84
85 /* initialize each per-disk recon information structure */
86 for (i = 0; i < raidPtr->numCol; i++) {
87 reconCtrlPtr->perDiskInfo[i].reconCtrl = reconCtrlPtr;
88 reconCtrlPtr->perDiskInfo[i].col = i;
89 /* make it appear as if we just finished an RU */
90 reconCtrlPtr->perDiskInfo[i].curPSID = -1;
91 reconCtrlPtr->perDiskInfo[i].ru_count = RUsPerPU - 1;
92 }
93
94 /* Get the number of spare units per disk and the sparemap in case
95 * spare is distributed */
96
97 if (lp->GetNumSpareRUs) {
98 numSpareRUs = lp->GetNumSpareRUs(raidPtr);
99 } else {
100 numSpareRUs = 0;
101 }
102
103 #if (RF_INCLUDE_PARITY_DECLUSTERING_DS > 0)
104 /*
105 * Not all distributed sparing archs need dynamic mappings
106 */
107 if (lp->InstallSpareTable) {
108 retcode = rf_InstallSpareTable(raidPtr, fcol);
109 if (retcode) {
110 RF_PANIC(); /* XXX fix this */
111 }
112 }
113 #endif
114 /* make the reconstruction map */
115 reconCtrlPtr->reconMap = rf_MakeReconMap(raidPtr, (int) (layoutPtr->SUsPerRU * layoutPtr->sectorsPerStripeUnit),
116 raidPtr->sectorsPerDisk, numSpareRUs);
117
118 /* make the per-disk reconstruction buffers */
119 for (i = 0; i < raidPtr->numCol; i++) {
120 reconCtrlPtr->perDiskInfo[i].rbuf = (i == fcol) ? NULL : rf_MakeReconBuffer(raidPtr, i, RF_RBUF_TYPE_EXCLUSIVE);
121 }
122
123 /* initialize the event queue */
124 rc = rf_mutex_init(&reconCtrlPtr->eq_mutex);
125 if (rc) {
126 /* XXX deallocate, cleanup */
127 rf_print_unable_to_init_mutex(__FILE__, __LINE__, rc);
128 return (NULL);
129 }
130 rc = rf_cond_init(&reconCtrlPtr->eq_cond);
131 if (rc) {
132 /* XXX deallocate, cleanup */
133 rf_print_unable_to_init_cond(__FILE__, __LINE__, rc);
134 return (NULL);
135 }
136 reconCtrlPtr->eventQueue = NULL;
137 reconCtrlPtr->eq_count = 0;
138
139 /* make the floating recon buffers and append them to the free list */
140 rc = rf_mutex_init(&reconCtrlPtr->rb_mutex);
141 if (rc) {
142 /* XXX deallocate, cleanup */
143 rf_print_unable_to_init_mutex(__FILE__, __LINE__, rc);
144 return (NULL);
145 }
146 reconCtrlPtr->fullBufferList = NULL;
147 reconCtrlPtr->floatingRbufs = NULL;
148 reconCtrlPtr->committedRbufs = NULL;
149 for (i = 0; i < raidPtr->numFloatingReconBufs; i++) {
150 rbuf = rf_MakeReconBuffer(raidPtr, fcol,
151 RF_RBUF_TYPE_FLOATING);
152 rbuf->next = reconCtrlPtr->floatingRbufs;
153 reconCtrlPtr->floatingRbufs = rbuf;
154 }
155
156 /* create the parity stripe status table */
157 reconCtrlPtr->pssTable = rf_MakeParityStripeStatusTable(raidPtr);
158
159 /* set the initial min head sep counter val */
160 reconCtrlPtr->minHeadSepCounter = 0;
161
162 return (reconCtrlPtr);
163 }
164
165 void
166 rf_FreeReconControl(raidPtr)
167 RF_Raid_t *raidPtr;
168 {
169 RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl;
170 RF_ReconBuffer_t *t;
171 RF_ReconUnitNum_t i;
172
173 RF_ASSERT(reconCtrlPtr);
174 for (i = 0; i < raidPtr->numCol; i++)
175 if (reconCtrlPtr->perDiskInfo[i].rbuf)
176 rf_FreeReconBuffer(reconCtrlPtr->perDiskInfo[i].rbuf);
177 for (i = 0; i < raidPtr->numFloatingReconBufs; i++) {
178 t = reconCtrlPtr->floatingRbufs;
179 RF_ASSERT(t);
180 reconCtrlPtr->floatingRbufs = t->next;
181 rf_FreeReconBuffer(t);
182 }
183 rf_mutex_destroy(&reconCtrlPtr->rb_mutex);
184 rf_mutex_destroy(&reconCtrlPtr->eq_mutex);
185 rf_cond_destroy(&reconCtrlPtr->eq_cond);
186 rf_FreeReconMap(reconCtrlPtr->reconMap);
187 rf_FreeParityStripeStatusTable(raidPtr, reconCtrlPtr->pssTable);
188 RF_Free(reconCtrlPtr->perDiskInfo,
189 raidPtr->numCol * sizeof(RF_PerDiskReconCtrl_t));
190 RF_Free(reconCtrlPtr, sizeof(*reconCtrlPtr));
191 }
192
193
194 /******************************************************************************
195 * computes the default head separation limit
196 *****************************************************************************/
197 RF_HeadSepLimit_t
198 rf_GetDefaultHeadSepLimit(raidPtr)
199 RF_Raid_t *raidPtr;
200 {
201 RF_HeadSepLimit_t hsl;
202 const RF_LayoutSW_t *lp;
203
204 lp = raidPtr->Layout.map;
205 if (lp->GetDefaultHeadSepLimit == NULL)
206 return (-1);
207 hsl = lp->GetDefaultHeadSepLimit(raidPtr);
208 return (hsl);
209 }
210
211
212 /******************************************************************************
213 * computes the default number of floating recon buffers
214 *****************************************************************************/
215 int
216 rf_GetDefaultNumFloatingReconBuffers(raidPtr)
217 RF_Raid_t *raidPtr;
218 {
219 const RF_LayoutSW_t *lp;
220 int nrb;
221
222 lp = raidPtr->Layout.map;
223 if (lp->GetDefaultNumFloatingReconBuffers == NULL)
224 return (3 * raidPtr->numCol);
225 nrb = lp->GetDefaultNumFloatingReconBuffers(raidPtr);
226 return (nrb);
227 }
228
229
230 /******************************************************************************
231 * creates and initializes a reconstruction buffer
232 *****************************************************************************/
233 RF_ReconBuffer_t *
234 rf_MakeReconBuffer(
235 RF_Raid_t * raidPtr,
236 RF_RowCol_t col,
237 RF_RbufType_t type)
238 {
239 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
240 RF_ReconBuffer_t *t;
241 u_int recon_buffer_size = rf_RaidAddressToByte(raidPtr, layoutPtr->SUsPerRU * layoutPtr->sectorsPerStripeUnit);
242
243 RF_Malloc(t, sizeof(RF_ReconBuffer_t), (RF_ReconBuffer_t *));
244 RF_Malloc(t->buffer, recon_buffer_size, (caddr_t));
245 t->raidPtr = raidPtr;
246 t->col = col;
247 t->priority = RF_IO_RECON_PRIORITY;
248 t->type = type;
249 t->pssPtr = NULL;
250 t->next = NULL;
251 return (t);
252 }
253 /******************************************************************************
254 * frees a reconstruction buffer
255 *****************************************************************************/
256 void
257 rf_FreeReconBuffer(rbuf)
258 RF_ReconBuffer_t *rbuf;
259 {
260 RF_Raid_t *raidPtr = rbuf->raidPtr;
261 u_int recon_buffer_size;
262
263 recon_buffer_size = rf_RaidAddressToByte(raidPtr, raidPtr->Layout.SUsPerRU * raidPtr->Layout.sectorsPerStripeUnit);
264
265 RF_Free(rbuf->buffer, recon_buffer_size);
266 RF_Free(rbuf, sizeof(*rbuf));
267 }
268
269 #if RF_DEBUG_RECON
270 /******************************************************************************
271 * debug only: sanity check the number of floating recon bufs in use
272 *****************************************************************************/
273 void
274 rf_CheckFloatingRbufCount(raidPtr, dolock)
275 RF_Raid_t *raidPtr;
276 int dolock;
277 {
278 RF_ReconParityStripeStatus_t *p;
279 RF_PSStatusHeader_t *pssTable;
280 RF_ReconBuffer_t *rbuf;
281 int i, j, sum = 0;
282
283 if (dolock)
284 RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
285 pssTable = raidPtr->reconControl->pssTable;
286
287 for (i = 0; i < raidPtr->pssTableSize; i++) {
288 RF_LOCK_MUTEX(pssTable[i].mutex);
289 for (p = pssTable[i].chain; p; p = p->next) {
290 rbuf = (RF_ReconBuffer_t *) p->rbuf;
291 if (rbuf && rbuf->type == RF_RBUF_TYPE_FLOATING)
292 sum++;
293
294 rbuf = (RF_ReconBuffer_t *) p->writeRbuf;
295 if (rbuf && rbuf->type == RF_RBUF_TYPE_FLOATING)
296 sum++;
297
298 for (j = 0; j < p->xorBufCount; j++) {
299 rbuf = (RF_ReconBuffer_t *) p->rbufsForXor[j];
300 RF_ASSERT(rbuf);
301 if (rbuf->type == RF_RBUF_TYPE_FLOATING)
302 sum++;
303 }
304 }
305 RF_UNLOCK_MUTEX(pssTable[i].mutex);
306 }
307
308 for (rbuf = raidPtr->reconControl->floatingRbufs; rbuf;
309 rbuf = rbuf->next) {
310 if (rbuf->type == RF_RBUF_TYPE_FLOATING)
311 sum++;
312 }
313 for (rbuf = raidPtr->reconControl->committedRbufs; rbuf;
314 rbuf = rbuf->next) {
315 if (rbuf->type == RF_RBUF_TYPE_FLOATING)
316 sum++;
317 }
318 for (rbuf = raidPtr->reconControl->fullBufferList; rbuf;
319 rbuf = rbuf->next) {
320 if (rbuf->type == RF_RBUF_TYPE_FLOATING)
321 sum++;
322 }
323 RF_ASSERT(sum == raidPtr->numFloatingReconBufs);
324
325 if (dolock)
326 RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
327 }
328 #endif
329
330