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