rf_reconmap.c revision 1.19 1 /* $NetBSD: rf_reconmap.c,v 1.19 2003/12/29 03:33:48 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_reconmap.c
31 *
32 * code to maintain a map of what sectors have/have not been reconstructed
33 *
34 *************************************************************************/
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: rf_reconmap.c,v 1.19 2003/12/29 03:33:48 oster Exp $");
38
39 #include "rf_raid.h"
40 #include <sys/time.h>
41 #include "rf_general.h"
42 #include "rf_utils.h"
43
44 /* special pointer values indicating that a reconstruction unit
45 * has been either totally reconstructed or not at all. Both
46 * are illegal pointer values, so you have to be careful not to
47 * dereference through them. RU_NOTHING must be zero, since
48 * MakeReconMap uses memset to initialize the structure. These are used
49 * only at the head of the list.
50 */
51 #define RU_ALL ((RF_ReconMapListElem_t *) -1)
52 #define RU_NOTHING ((RF_ReconMapListElem_t *) 0)
53
54 /* For most reconstructs we need at most 3 RF_ReconMapListElem_t's.
55 * Bounding the number we need is quite difficult, as it depends on how
56 * badly the sectors to be reconstructed get divided up. In the current
57 * code, the reconstructed sectors appeared aligned on stripe boundaries,
58 * and are always presented in stripe width units, so we're probably
59 * allocating quite a bit more than we'll ever need.
60 */
61 #define RF_NUM_RECON_POOL_ELEM 100
62
63 static void
64 compact_stat_entry(RF_Raid_t * raidPtr, RF_ReconMap_t * mapPtr,
65 int i);
66 static void crunch_list(RF_ReconMap_t *mapPtr,
67 RF_ReconMapListElem_t * listPtr);
68 static RF_ReconMapListElem_t *
69 MakeReconMapListElem(RF_ReconMap_t *mapPtr, RF_SectorNum_t startSector,
70 RF_SectorNum_t stopSector, RF_ReconMapListElem_t * next);
71 static void
72 FreeReconMapListElem(RF_ReconMap_t *mapPtr, RF_ReconMapListElem_t * p);
73 #if 0
74 static void PrintList(RF_ReconMapListElem_t * listPtr);
75 #endif
76
77 /*---------------------------------------------------------------------------
78 *
79 * Creates and initializes new Reconstruction map
80 *
81 *-------------------------------------------------------------------------*/
82
83 RF_ReconMap_t *
84 rf_MakeReconMap(raidPtr, ru_sectors, disk_sectors, spareUnitsPerDisk)
85 RF_Raid_t *raidPtr;
86 RF_SectorCount_t ru_sectors; /* size of reconstruction unit in
87 * sectors */
88 RF_SectorCount_t disk_sectors; /* size of disk in sectors */
89 RF_ReconUnitCount_t spareUnitsPerDisk; /* zero unless distributed
90 * sparing */
91 {
92 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
93 RF_ReconUnitCount_t num_rus = layoutPtr->stripeUnitsPerDisk / layoutPtr->SUsPerRU;
94 RF_ReconMap_t *p;
95 int rc;
96
97 RF_Malloc(p, sizeof(RF_ReconMap_t), (RF_ReconMap_t *));
98 p->sectorsPerReconUnit = ru_sectors;
99 p->sectorsInDisk = disk_sectors;
100
101 p->totalRUs = num_rus;
102 p->spareRUs = spareUnitsPerDisk;
103 p->unitsLeft = num_rus - spareUnitsPerDisk;
104
105 RF_Malloc(p->status, num_rus * sizeof(RF_ReconMapListElem_t *), (RF_ReconMapListElem_t **));
106 RF_ASSERT(p->status != (RF_ReconMapListElem_t **) NULL);
107
108 (void) memset((char *) p->status, 0,
109 num_rus * sizeof(RF_ReconMapListElem_t *));
110
111
112 pool_init(&p->elem_pool, sizeof(RF_ReconMapListElem_t), 0,
113 0, 0, "raidreconpl", NULL);
114 pool_prime(&p->elem_pool, RF_NUM_RECON_POOL_ELEM);
115
116 rc = rf_mutex_init(&p->mutex);
117 if (rc) {
118 rf_print_unable_to_init_mutex(__FILE__, __LINE__, rc);
119 RF_Free(p->status, num_rus * sizeof(RF_ReconMapListElem_t *));
120 RF_Free(p, sizeof(RF_ReconMap_t));
121 return (NULL);
122 }
123 return (p);
124 }
125
126
127 /*---------------------------------------------------------------------------
128 *
129 * marks a new set of sectors as reconstructed. All the possible
130 * mergings get complicated. To simplify matters, the approach I take
131 * is to just dump something into the list, and then clean it up
132 * (i.e. merge elements and eliminate redundant ones) in a second pass
133 * over the list (compact_stat_entry()). Not 100% efficient, since a
134 * structure can be allocated and then immediately freed, but it keeps
135 * this code from becoming (more of) a nightmare of special cases.
136 * The only thing that compact_stat_entry() assumes is that the list
137 * is sorted by startSector, and so this is the only condition I
138 * maintain here. (MCH)
139 *
140 * This code now uses a pool instead of the previous malloc/free
141 * stuff.
142 *-------------------------------------------------------------------------*/
143
144 void
145 rf_ReconMapUpdate(raidPtr, mapPtr, startSector, stopSector)
146 RF_Raid_t *raidPtr;
147 RF_ReconMap_t *mapPtr;
148 RF_SectorNum_t startSector;
149 RF_SectorNum_t stopSector;
150 {
151 RF_SectorCount_t sectorsPerReconUnit = mapPtr->sectorsPerReconUnit;
152 RF_SectorNum_t i, first_in_RU, last_in_RU;
153 RF_ReconMapListElem_t *p, *pt;
154
155 RF_LOCK_MUTEX(mapPtr->mutex);
156 RF_ASSERT(startSector >= 0 && stopSector < mapPtr->sectorsInDisk &&
157 stopSector >= startSector);
158
159 while (startSector <= stopSector) {
160 i = startSector / mapPtr->sectorsPerReconUnit;
161 first_in_RU = i * sectorsPerReconUnit;
162 last_in_RU = first_in_RU + sectorsPerReconUnit - 1;
163 p = mapPtr->status[i];
164 if (p != RU_ALL) {
165 if (p == RU_NOTHING || p->startSector > startSector) {
166 /* insert at front of list */
167
168 mapPtr->status[i] = MakeReconMapListElem(mapPtr,startSector, RF_MIN(stopSector, last_in_RU), (p == RU_NOTHING) ? NULL : p);
169
170 } else {/* general case */
171 do { /* search for place to insert */
172 pt = p;
173 p = p->next;
174 } while (p && (p->startSector < startSector));
175 pt->next = MakeReconMapListElem(mapPtr,startSector, RF_MIN(stopSector, last_in_RU), p);
176
177 }
178 compact_stat_entry(raidPtr, mapPtr, i);
179 }
180 startSector = RF_MIN(stopSector, last_in_RU) + 1;
181 }
182 RF_UNLOCK_MUTEX(mapPtr->mutex);
183 }
184
185
186
187 /*---------------------------------------------------------------------------
188 *
189 * performs whatever list compactions can be done, and frees any space
190 * that is no longer necessary. Assumes only that the list is sorted
191 * by startSector. crunch_list() compacts a single list as much as
192 * possible, and the second block of code deletes the entire list if
193 * possible. crunch_list() is also called from
194 * MakeReconMapAccessList().
195 *
196 * When a recon unit is detected to be fully reconstructed, we set the
197 * corresponding bit in the parity stripe map so that the head follow
198 * code will not select this parity stripe again. This is redundant
199 * (but harmless) when compact_stat_entry is called from the
200 * reconstruction code, but necessary when called from the user-write
201 * code.
202 *
203 *-------------------------------------------------------------------------*/
204
205 static void
206 compact_stat_entry(raidPtr, mapPtr, i)
207 RF_Raid_t *raidPtr;
208 RF_ReconMap_t *mapPtr;
209 int i;
210 {
211 RF_SectorCount_t sectorsPerReconUnit = mapPtr->sectorsPerReconUnit;
212 RF_ReconMapListElem_t *p = mapPtr->status[i];
213
214 crunch_list(mapPtr, p);
215
216 if ((p->startSector == i * sectorsPerReconUnit) &&
217 (p->stopSector == i * sectorsPerReconUnit +
218 sectorsPerReconUnit - 1)) {
219 mapPtr->status[i] = RU_ALL;
220 mapPtr->unitsLeft--;
221 FreeReconMapListElem(mapPtr, p);
222 }
223 }
224
225 static void
226 crunch_list(mapPtr, listPtr)
227 RF_ReconMap_t *mapPtr;
228 RF_ReconMapListElem_t *listPtr;
229 {
230 RF_ReconMapListElem_t *pt, *p = listPtr;
231
232 if (!p)
233 return;
234 pt = p;
235 p = p->next;
236 while (p) {
237 if (pt->stopSector >= p->startSector - 1) {
238 pt->stopSector = RF_MAX(pt->stopSector, p->stopSector);
239 pt->next = p->next;
240 FreeReconMapListElem(mapPtr, p);
241 p = pt->next;
242 } else {
243 pt = p;
244 p = p->next;
245 }
246 }
247 }
248 /*---------------------------------------------------------------------------
249 *
250 * Allocate and fill a new list element
251 *
252 *-------------------------------------------------------------------------*/
253
254 static RF_ReconMapListElem_t *
255 MakeReconMapListElem(
256 RF_ReconMap_t *mapPtr,
257 RF_SectorNum_t startSector,
258 RF_SectorNum_t stopSector,
259 RF_ReconMapListElem_t * next)
260 {
261 RF_ReconMapListElem_t *p;
262
263 p = pool_get(&mapPtr->elem_pool, PR_WAITOK);
264
265 if (p == NULL)
266 return (NULL);
267
268 p->startSector = startSector;
269 p->stopSector = stopSector;
270 p->next = next;
271 return (p);
272 }
273 /*---------------------------------------------------------------------------
274 *
275 * Free a list element
276 *
277 *-------------------------------------------------------------------------*/
278
279 static void
280 FreeReconMapListElem(mapPtr, p)
281 RF_ReconMap_t *mapPtr;
282 RF_ReconMapListElem_t *p;
283 {
284 pool_put(&mapPtr->elem_pool, p);
285 }
286 /*---------------------------------------------------------------------------
287 *
288 * Free an entire status structure. Inefficient, but can be called at
289 * any time.
290 *
291 *-------------------------------------------------------------------------*/
292 void
293 rf_FreeReconMap(mapPtr)
294 RF_ReconMap_t *mapPtr;
295 {
296 RF_ReconMapListElem_t *p, *q;
297 RF_ReconUnitCount_t numRUs;
298 RF_ReconUnitNum_t i;
299
300 numRUs = mapPtr->sectorsInDisk / mapPtr->sectorsPerReconUnit;
301 if (mapPtr->sectorsInDisk % mapPtr->sectorsPerReconUnit)
302 numRUs++;
303
304 for (i = 0; i < numRUs; i++) {
305 p = mapPtr->status[i];
306 while (p != RU_NOTHING && p != RU_ALL) {
307 q = p;
308 p = p->next;
309 RF_Free(q, sizeof(*q));
310 }
311 }
312 pool_destroy(&mapPtr->elem_pool);
313 rf_mutex_destroy(&mapPtr->mutex);
314 RF_Free(mapPtr->status, mapPtr->totalRUs *
315 sizeof(RF_ReconMapListElem_t *));
316 RF_Free(mapPtr, sizeof(RF_ReconMap_t));
317 }
318 /*---------------------------------------------------------------------------
319 *
320 * returns nonzero if the indicated RU has been reconstructed already
321 *
322 *-------------------------------------------------------------------------*/
323
324 int
325 rf_CheckRUReconstructed(mapPtr, startSector)
326 RF_ReconMap_t *mapPtr;
327 RF_SectorNum_t startSector;
328 {
329 RF_ReconMapListElem_t *l; /* used for searching */
330 RF_ReconUnitNum_t i;
331
332 i = startSector / mapPtr->sectorsPerReconUnit;
333 l = mapPtr->status[i];
334 return ((l == RU_ALL) ? 1 : 0);
335 }
336
337 RF_ReconUnitCount_t
338 rf_UnitsLeftToReconstruct(mapPtr)
339 RF_ReconMap_t *mapPtr;
340 {
341 RF_ASSERT(mapPtr != NULL);
342 return (mapPtr->unitsLeft);
343 }
344
345 #if 0
346 static void
347 PrintList(listPtr)
348 RF_ReconMapListElem_t *listPtr;
349 {
350 while (listPtr) {
351 printf("%d,%d -> ", (int) listPtr->startSector,
352 (int) listPtr->stopSector);
353 listPtr = listPtr->next;
354 }
355 printf("\n");
356 }
357
358 void
359 rf_PrintReconMap(raidPtr, mapPtr, fcol)
360 RF_Raid_t *raidPtr;
361 RF_ReconMap_t *mapPtr;
362 RF_RowCol_t fcol;
363 {
364 RF_ReconUnitCount_t numRUs;
365 RF_ReconMapListElem_t *p;
366 RF_ReconUnitNum_t i;
367
368 numRUs = mapPtr->totalRUs;
369 if (mapPtr->sectorsInDisk % mapPtr->sectorsPerReconUnit)
370 numRUs++;
371
372 for (i = 0; i < numRUs; i++) {
373 p = mapPtr->status[i];
374 if (p == RU_ALL)/* printf("[%d] ALL\n",i) */
375 ;
376 else
377 if (p == RU_NOTHING) {
378 printf("%d: Unreconstructed\n", i);
379 } else {
380 printf("%d: ", i);
381 PrintList(p);
382 }
383 }
384 }
385 #endif
386
387 #if RF_DEBUG_RECON
388 void
389 rf_PrintReconSchedule(mapPtr, starttime)
390 RF_ReconMap_t *mapPtr;
391 struct timeval *starttime;
392 {
393 static int old_pctg = -1;
394 struct timeval tv, diff;
395 int new_pctg;
396
397 new_pctg = 100 - (rf_UnitsLeftToReconstruct(mapPtr) *
398 100 / mapPtr->totalRUs);
399 if (new_pctg != old_pctg) {
400 RF_GETTIME(tv);
401 RF_TIMEVAL_DIFF(starttime, &tv, &diff);
402 printf("%d %d.%06d\n", (int) new_pctg, (int) diff.tv_sec,
403 (int) diff.tv_usec);
404 old_pctg = new_pctg;
405 }
406 }
407 #endif
408
409