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