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