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