rf_reconmap.c revision 1.20 1 /* $NetBSD: rf_reconmap.c,v 1.20 2003/12/29 04:56:26 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.20 2003/12/29 04:56:26 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
96 RF_Malloc(p, sizeof(RF_ReconMap_t), (RF_ReconMap_t *));
97 p->sectorsPerReconUnit = ru_sectors;
98 p->sectorsInDisk = disk_sectors;
99
100 p->totalRUs = num_rus;
101 p->spareRUs = spareUnitsPerDisk;
102 p->unitsLeft = num_rus - spareUnitsPerDisk;
103
104 RF_Malloc(p->status, num_rus * sizeof(RF_ReconMapListElem_t *), (RF_ReconMapListElem_t **));
105 RF_ASSERT(p->status != (RF_ReconMapListElem_t **) NULL);
106
107 (void) memset((char *) p->status, 0,
108 num_rus * sizeof(RF_ReconMapListElem_t *));
109
110
111 pool_init(&p->elem_pool, sizeof(RF_ReconMapListElem_t), 0,
112 0, 0, "raidreconpl", NULL);
113 pool_prime(&p->elem_pool, RF_NUM_RECON_POOL_ELEM);
114
115 rf_mutex_init(&p->mutex);
116 return (p);
117 }
118
119
120 /*---------------------------------------------------------------------------
121 *
122 * marks a new set of sectors as reconstructed. All the possible
123 * mergings get complicated. To simplify matters, the approach I take
124 * is to just dump something into the list, and then clean it up
125 * (i.e. merge elements and eliminate redundant ones) in a second pass
126 * over the list (compact_stat_entry()). Not 100% efficient, since a
127 * structure can be allocated and then immediately freed, but it keeps
128 * this code from becoming (more of) a nightmare of special cases.
129 * The only thing that compact_stat_entry() assumes is that the list
130 * is sorted by startSector, and so this is the only condition I
131 * maintain here. (MCH)
132 *
133 * This code now uses a pool instead of the previous malloc/free
134 * stuff.
135 *-------------------------------------------------------------------------*/
136
137 void
138 rf_ReconMapUpdate(raidPtr, mapPtr, startSector, stopSector)
139 RF_Raid_t *raidPtr;
140 RF_ReconMap_t *mapPtr;
141 RF_SectorNum_t startSector;
142 RF_SectorNum_t stopSector;
143 {
144 RF_SectorCount_t sectorsPerReconUnit = mapPtr->sectorsPerReconUnit;
145 RF_SectorNum_t i, first_in_RU, last_in_RU;
146 RF_ReconMapListElem_t *p, *pt;
147
148 RF_LOCK_MUTEX(mapPtr->mutex);
149 RF_ASSERT(startSector >= 0 && stopSector < mapPtr->sectorsInDisk &&
150 stopSector >= startSector);
151
152 while (startSector <= stopSector) {
153 i = startSector / mapPtr->sectorsPerReconUnit;
154 first_in_RU = i * sectorsPerReconUnit;
155 last_in_RU = first_in_RU + sectorsPerReconUnit - 1;
156 p = mapPtr->status[i];
157 if (p != RU_ALL) {
158 if (p == RU_NOTHING || p->startSector > startSector) {
159 /* insert at front of list */
160
161 mapPtr->status[i] = MakeReconMapListElem(mapPtr,startSector, RF_MIN(stopSector, last_in_RU), (p == RU_NOTHING) ? NULL : p);
162
163 } else {/* general case */
164 do { /* search for place to insert */
165 pt = p;
166 p = p->next;
167 } while (p && (p->startSector < startSector));
168 pt->next = MakeReconMapListElem(mapPtr,startSector, RF_MIN(stopSector, last_in_RU), p);
169
170 }
171 compact_stat_entry(raidPtr, mapPtr, i);
172 }
173 startSector = RF_MIN(stopSector, last_in_RU) + 1;
174 }
175 RF_UNLOCK_MUTEX(mapPtr->mutex);
176 }
177
178
179
180 /*---------------------------------------------------------------------------
181 *
182 * performs whatever list compactions can be done, and frees any space
183 * that is no longer necessary. Assumes only that the list is sorted
184 * by startSector. crunch_list() compacts a single list as much as
185 * possible, and the second block of code deletes the entire list if
186 * possible. crunch_list() is also called from
187 * MakeReconMapAccessList().
188 *
189 * When a recon unit is detected to be fully reconstructed, we set the
190 * corresponding bit in the parity stripe map so that the head follow
191 * code will not select this parity stripe again. This is redundant
192 * (but harmless) when compact_stat_entry is called from the
193 * reconstruction code, but necessary when called from the user-write
194 * code.
195 *
196 *-------------------------------------------------------------------------*/
197
198 static void
199 compact_stat_entry(raidPtr, mapPtr, i)
200 RF_Raid_t *raidPtr;
201 RF_ReconMap_t *mapPtr;
202 int i;
203 {
204 RF_SectorCount_t sectorsPerReconUnit = mapPtr->sectorsPerReconUnit;
205 RF_ReconMapListElem_t *p = mapPtr->status[i];
206
207 crunch_list(mapPtr, p);
208
209 if ((p->startSector == i * sectorsPerReconUnit) &&
210 (p->stopSector == i * sectorsPerReconUnit +
211 sectorsPerReconUnit - 1)) {
212 mapPtr->status[i] = RU_ALL;
213 mapPtr->unitsLeft--;
214 FreeReconMapListElem(mapPtr, p);
215 }
216 }
217
218 static void
219 crunch_list(mapPtr, listPtr)
220 RF_ReconMap_t *mapPtr;
221 RF_ReconMapListElem_t *listPtr;
222 {
223 RF_ReconMapListElem_t *pt, *p = listPtr;
224
225 if (!p)
226 return;
227 pt = p;
228 p = p->next;
229 while (p) {
230 if (pt->stopSector >= p->startSector - 1) {
231 pt->stopSector = RF_MAX(pt->stopSector, p->stopSector);
232 pt->next = p->next;
233 FreeReconMapListElem(mapPtr, p);
234 p = pt->next;
235 } else {
236 pt = p;
237 p = p->next;
238 }
239 }
240 }
241 /*---------------------------------------------------------------------------
242 *
243 * Allocate and fill a new list element
244 *
245 *-------------------------------------------------------------------------*/
246
247 static RF_ReconMapListElem_t *
248 MakeReconMapListElem(
249 RF_ReconMap_t *mapPtr,
250 RF_SectorNum_t startSector,
251 RF_SectorNum_t stopSector,
252 RF_ReconMapListElem_t * next)
253 {
254 RF_ReconMapListElem_t *p;
255
256 p = pool_get(&mapPtr->elem_pool, PR_WAITOK);
257
258 if (p == NULL)
259 return (NULL);
260
261 p->startSector = startSector;
262 p->stopSector = stopSector;
263 p->next = next;
264 return (p);
265 }
266 /*---------------------------------------------------------------------------
267 *
268 * Free a list element
269 *
270 *-------------------------------------------------------------------------*/
271
272 static void
273 FreeReconMapListElem(mapPtr, p)
274 RF_ReconMap_t *mapPtr;
275 RF_ReconMapListElem_t *p;
276 {
277 pool_put(&mapPtr->elem_pool, p);
278 }
279 /*---------------------------------------------------------------------------
280 *
281 * Free an entire status structure. Inefficient, but can be called at
282 * any time.
283 *
284 *-------------------------------------------------------------------------*/
285 void
286 rf_FreeReconMap(mapPtr)
287 RF_ReconMap_t *mapPtr;
288 {
289 RF_ReconMapListElem_t *p, *q;
290 RF_ReconUnitCount_t numRUs;
291 RF_ReconUnitNum_t i;
292
293 numRUs = mapPtr->sectorsInDisk / mapPtr->sectorsPerReconUnit;
294 if (mapPtr->sectorsInDisk % mapPtr->sectorsPerReconUnit)
295 numRUs++;
296
297 for (i = 0; i < numRUs; i++) {
298 p = mapPtr->status[i];
299 while (p != RU_NOTHING && p != RU_ALL) {
300 q = p;
301 p = p->next;
302 RF_Free(q, sizeof(*q));
303 }
304 }
305 pool_destroy(&mapPtr->elem_pool);
306 rf_mutex_destroy(&mapPtr->mutex);
307 RF_Free(mapPtr->status, mapPtr->totalRUs *
308 sizeof(RF_ReconMapListElem_t *));
309 RF_Free(mapPtr, sizeof(RF_ReconMap_t));
310 }
311 /*---------------------------------------------------------------------------
312 *
313 * returns nonzero if the indicated RU has been reconstructed already
314 *
315 *-------------------------------------------------------------------------*/
316
317 int
318 rf_CheckRUReconstructed(mapPtr, startSector)
319 RF_ReconMap_t *mapPtr;
320 RF_SectorNum_t startSector;
321 {
322 RF_ReconMapListElem_t *l; /* used for searching */
323 RF_ReconUnitNum_t i;
324
325 i = startSector / mapPtr->sectorsPerReconUnit;
326 l = mapPtr->status[i];
327 return ((l == RU_ALL) ? 1 : 0);
328 }
329
330 RF_ReconUnitCount_t
331 rf_UnitsLeftToReconstruct(mapPtr)
332 RF_ReconMap_t *mapPtr;
333 {
334 RF_ASSERT(mapPtr != NULL);
335 return (mapPtr->unitsLeft);
336 }
337
338 #if 0
339 static void
340 PrintList(listPtr)
341 RF_ReconMapListElem_t *listPtr;
342 {
343 while (listPtr) {
344 printf("%d,%d -> ", (int) listPtr->startSector,
345 (int) listPtr->stopSector);
346 listPtr = listPtr->next;
347 }
348 printf("\n");
349 }
350
351 void
352 rf_PrintReconMap(raidPtr, mapPtr, fcol)
353 RF_Raid_t *raidPtr;
354 RF_ReconMap_t *mapPtr;
355 RF_RowCol_t fcol;
356 {
357 RF_ReconUnitCount_t numRUs;
358 RF_ReconMapListElem_t *p;
359 RF_ReconUnitNum_t i;
360
361 numRUs = mapPtr->totalRUs;
362 if (mapPtr->sectorsInDisk % mapPtr->sectorsPerReconUnit)
363 numRUs++;
364
365 for (i = 0; i < numRUs; i++) {
366 p = mapPtr->status[i];
367 if (p == RU_ALL)/* printf("[%d] ALL\n",i) */
368 ;
369 else
370 if (p == RU_NOTHING) {
371 printf("%d: Unreconstructed\n", i);
372 } else {
373 printf("%d: ", i);
374 PrintList(p);
375 }
376 }
377 }
378 #endif
379
380 #if RF_DEBUG_RECON
381 void
382 rf_PrintReconSchedule(mapPtr, starttime)
383 RF_ReconMap_t *mapPtr;
384 struct timeval *starttime;
385 {
386 static int old_pctg = -1;
387 struct timeval tv, diff;
388 int new_pctg;
389
390 new_pctg = 100 - (rf_UnitsLeftToReconstruct(mapPtr) *
391 100 / mapPtr->totalRUs);
392 if (new_pctg != old_pctg) {
393 RF_GETTIME(tv);
394 RF_TIMEVAL_DIFF(starttime, &tv, &diff);
395 printf("%d %d.%06d\n", (int) new_pctg, (int) diff.tv_sec,
396 (int) diff.tv_usec);
397 old_pctg = new_pctg;
398 }
399 }
400 #endif
401
402