xref: /netbsd-src/sys/dev/raidframe/rf_reconutil.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: rf_reconutil.c,v 1.23 2004/03/18 16:54:54 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.23 2004/03/18 16:54:54 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->eventQueue = NULL;
129 	reconCtrlPtr->eq_count = 0;
130 
131 	/* make the floating recon buffers and append them to the free list */
132 	simple_lock_init(&reconCtrlPtr->rb_mutex);
133 
134 	reconCtrlPtr->fullBufferList = NULL;
135 	reconCtrlPtr->floatingRbufs = NULL;
136 	reconCtrlPtr->committedRbufs = NULL;
137 	for (i = 0; i < raidPtr->numFloatingReconBufs; i++) {
138 		rbuf = rf_MakeReconBuffer(raidPtr, fcol,
139 					  RF_RBUF_TYPE_FLOATING);
140 		rbuf->next = reconCtrlPtr->floatingRbufs;
141 		reconCtrlPtr->floatingRbufs = rbuf;
142 	}
143 
144 	/* create the parity stripe status table */
145 	reconCtrlPtr->pssTable = rf_MakeParityStripeStatusTable(raidPtr);
146 
147 	/* set the initial min head sep counter val */
148 	reconCtrlPtr->minHeadSepCounter = 0;
149 
150 	return (reconCtrlPtr);
151 }
152 
153 void
154 rf_FreeReconControl(RF_Raid_t *raidPtr)
155 {
156 	RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl;
157 	RF_ReconBuffer_t *t;
158 	RF_ReconUnitNum_t i;
159 
160 	RF_ASSERT(reconCtrlPtr);
161 	for (i = 0; i < raidPtr->numCol; i++)
162 		if (reconCtrlPtr->perDiskInfo[i].rbuf)
163 			rf_FreeReconBuffer(reconCtrlPtr->perDiskInfo[i].rbuf);
164 	for (i = 0; i < raidPtr->numFloatingReconBufs; i++) {
165 		t = reconCtrlPtr->floatingRbufs;
166 		RF_ASSERT(t);
167 		reconCtrlPtr->floatingRbufs = t->next;
168 		rf_FreeReconBuffer(t);
169 	}
170 	rf_FreeReconMap(reconCtrlPtr->reconMap);
171 	rf_FreeParityStripeStatusTable(raidPtr, reconCtrlPtr->pssTable);
172 	RF_Free(reconCtrlPtr->perDiskInfo,
173 		raidPtr->numCol * sizeof(RF_PerDiskReconCtrl_t));
174 	RF_Free(reconCtrlPtr, sizeof(*reconCtrlPtr));
175 }
176 
177 
178 /******************************************************************************
179  * computes the default head separation limit
180  *****************************************************************************/
181 RF_HeadSepLimit_t
182 rf_GetDefaultHeadSepLimit(RF_Raid_t *raidPtr)
183 {
184 	RF_HeadSepLimit_t hsl;
185 	const RF_LayoutSW_t *lp;
186 
187 	lp = raidPtr->Layout.map;
188 	if (lp->GetDefaultHeadSepLimit == NULL)
189 		return (-1);
190 	hsl = lp->GetDefaultHeadSepLimit(raidPtr);
191 	return (hsl);
192 }
193 
194 
195 /******************************************************************************
196  * computes the default number of floating recon buffers
197  *****************************************************************************/
198 int
199 rf_GetDefaultNumFloatingReconBuffers(RF_Raid_t *raidPtr)
200 {
201 	const RF_LayoutSW_t *lp;
202 	int     nrb;
203 
204 	lp = raidPtr->Layout.map;
205 	if (lp->GetDefaultNumFloatingReconBuffers == NULL)
206 		return (3 * raidPtr->numCol);
207 	nrb = lp->GetDefaultNumFloatingReconBuffers(raidPtr);
208 	return (nrb);
209 }
210 
211 
212 /******************************************************************************
213  * creates and initializes a reconstruction buffer
214  *****************************************************************************/
215 RF_ReconBuffer_t *
216 rf_MakeReconBuffer(RF_Raid_t *raidPtr, RF_RowCol_t col, RF_RbufType_t type)
217 {
218 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
219 	RF_ReconBuffer_t *t;
220 	u_int   recon_buffer_size = rf_RaidAddressToByte(raidPtr, layoutPtr->SUsPerRU * layoutPtr->sectorsPerStripeUnit);
221 
222 	t = pool_get(&rf_pools.reconbuffer, PR_WAITOK);
223 	RF_Malloc(t->buffer, recon_buffer_size, (caddr_t));
224 	t->raidPtr = raidPtr;
225 	t->col = col;
226 	t->priority = RF_IO_RECON_PRIORITY;
227 	t->type = type;
228 	t->pssPtr = NULL;
229 	t->next = NULL;
230 	return (t);
231 }
232 /******************************************************************************
233  * frees a reconstruction buffer
234  *****************************************************************************/
235 void
236 rf_FreeReconBuffer(RF_ReconBuffer_t *rbuf)
237 {
238 	RF_Raid_t *raidPtr = rbuf->raidPtr;
239 	u_int   recon_buffer_size;
240 
241 	recon_buffer_size = rf_RaidAddressToByte(raidPtr, raidPtr->Layout.SUsPerRU * raidPtr->Layout.sectorsPerStripeUnit);
242 
243 	RF_Free(rbuf->buffer, recon_buffer_size);
244 	pool_put(&rf_pools.reconbuffer, rbuf);
245 }
246 
247 #if RF_DEBUG_RECON
248 XXXX IF you use this, you really want to fix the locking in here.
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