rf_reconmap.c revision 1.12 1 /* $NetBSD: rf_reconmap.c,v 1.12 2002/10/04 23:49:35 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.12 2002/10/04 23:49:35 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 /* used to mark the end of the list */
55 #define RU_NIL ((RF_ReconMapListElem_t *) 0)
56
57
58 static void
59 compact_stat_entry(RF_Raid_t * raidPtr, RF_ReconMap_t * mapPtr,
60 int i);
61 static void crunch_list(RF_ReconMap_t * mapPtr, RF_ReconMapListElem_t * listPtr);
62 static RF_ReconMapListElem_t *
63 MakeReconMapListElem(RF_SectorNum_t startSector,
64 RF_SectorNum_t stopSector, RF_ReconMapListElem_t * next);
65 static void
66 FreeReconMapListElem(RF_ReconMap_t * mapPtr,
67 RF_ReconMapListElem_t * p);
68 static void update_size(RF_ReconMap_t * mapPtr, int size);
69 #if 0
70 static void PrintList(RF_ReconMapListElem_t * listPtr);
71 #endif
72
73 /*---------------------------------------------------------------------------
74 *
75 * Creates and initializes new Reconstruction map
76 *
77 *-------------------------------------------------------------------------*/
78
79 RF_ReconMap_t *
80 rf_MakeReconMap(raidPtr, ru_sectors, disk_sectors, spareUnitsPerDisk)
81 RF_Raid_t *raidPtr;
82 RF_SectorCount_t ru_sectors; /* size of reconstruction unit in
83 * sectors */
84 RF_SectorCount_t disk_sectors; /* size of disk in sectors */
85 RF_ReconUnitCount_t spareUnitsPerDisk; /* zero unless distributed
86 * sparing */
87 {
88 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
89 RF_ReconUnitCount_t num_rus = layoutPtr->stripeUnitsPerDisk / layoutPtr->SUsPerRU;
90 RF_ReconMap_t *p;
91 int rc;
92
93 RF_Malloc(p, sizeof(RF_ReconMap_t), (RF_ReconMap_t *));
94 p->sectorsPerReconUnit = ru_sectors;
95 p->sectorsInDisk = disk_sectors;
96
97 p->totalRUs = num_rus;
98 p->spareRUs = spareUnitsPerDisk;
99 p->unitsLeft = num_rus - spareUnitsPerDisk;
100
101 RF_Malloc(p->status, num_rus * sizeof(RF_ReconMapListElem_t *), (RF_ReconMapListElem_t **));
102 RF_ASSERT(p->status != (RF_ReconMapListElem_t **) NULL);
103
104 (void) memset((char *) p->status, 0,
105 num_rus * sizeof(RF_ReconMapListElem_t *));
106
107 p->size = sizeof(RF_ReconMap_t) + num_rus * sizeof(RF_ReconMapListElem_t *);
108 p->maxSize = p->size;
109
110 rc = rf_mutex_init(&p->mutex);
111 if (rc) {
112 rf_print_unable_to_init_mutex(__FILE__, __LINE__, rc);
113 RF_Free(p->status, num_rus * sizeof(RF_ReconMapListElem_t *));
114 RF_Free(p, sizeof(RF_ReconMap_t));
115 return (NULL);
116 }
117 return (p);
118 }
119
120
121 /*---------------------------------------------------------------------------
122 *
123 * marks a new set of sectors as reconstructed. All the possible
124 * mergings get complicated. To simplify matters, the approach I take
125 * is to just dump something into the list, and then clean it up
126 * (i.e. merge elements and eliminate redundant ones) in a second pass
127 * over the list (compact_stat_entry()). Not 100% efficient, since a
128 * structure can be allocated and then immediately freed, but it keeps
129 * this code from becoming (more of) a nightmare of special cases.
130 * The only thing that compact_stat_entry() assumes is that the list
131 * is sorted by startSector, and so this is the only condition I
132 * maintain here. (MCH)
133 *
134 *-------------------------------------------------------------------------*/
135
136 void
137 rf_ReconMapUpdate(raidPtr, mapPtr, startSector, stopSector)
138 RF_Raid_t *raidPtr;
139 RF_ReconMap_t *mapPtr;
140 RF_SectorNum_t startSector;
141 RF_SectorNum_t stopSector;
142 {
143 RF_SectorCount_t sectorsPerReconUnit = mapPtr->sectorsPerReconUnit;
144 RF_SectorNum_t i, first_in_RU, last_in_RU;
145 RF_ReconMapListElem_t *p, *pt;
146
147 RF_LOCK_MUTEX(mapPtr->mutex);
148 RF_ASSERT(startSector >= 0 && stopSector < mapPtr->sectorsInDisk &&
149 stopSector >= startSector);
150
151 while (startSector <= stopSector) {
152 i = startSector / mapPtr->sectorsPerReconUnit;
153 first_in_RU = i * sectorsPerReconUnit;
154 last_in_RU = first_in_RU + sectorsPerReconUnit - 1;
155 p = mapPtr->status[i];
156 if (p != RU_ALL) {
157 if (p == RU_NOTHING || p->startSector > startSector) {
158 /* insert at front of list */
159
160 mapPtr->status[i] = MakeReconMapListElem(startSector, RF_MIN(stopSector, last_in_RU), (p == RU_NOTHING) ? NULL : p);
161 update_size(mapPtr, sizeof(RF_ReconMapListElem_t));
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(startSector, RF_MIN(stopSector, last_in_RU), p);
169 update_size(mapPtr, sizeof(RF_ReconMapListElem_t));
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_SectorNum_t startSector,
250 RF_SectorNum_t stopSector,
251 RF_ReconMapListElem_t * next)
252 {
253 RF_ReconMapListElem_t *p;
254
255 RF_Malloc(p, sizeof(RF_ReconMapListElem_t), (RF_ReconMapListElem_t *));
256 if (p == NULL)
257 return (NULL);
258 p->startSector = startSector;
259 p->stopSector = stopSector;
260 p->next = next;
261 return (p);
262 }
263 /*---------------------------------------------------------------------------
264 *
265 * Free a list element
266 *
267 *-------------------------------------------------------------------------*/
268
269 static void
270 FreeReconMapListElem(mapPtr, p)
271 RF_ReconMap_t *mapPtr;
272 RF_ReconMapListElem_t *p;
273 {
274 int delta;
275
276 if (mapPtr) {
277 delta = 0 - (int) sizeof(RF_ReconMapListElem_t);
278 update_size(mapPtr, delta);
279 }
280 RF_Free(p, sizeof(*p));
281 }
282 /*---------------------------------------------------------------------------
283 *
284 * Free an entire status structure. Inefficient, but can be called at
285 * any time.
286 *
287 *-------------------------------------------------------------------------*/
288 void
289 rf_FreeReconMap(mapPtr)
290 RF_ReconMap_t *mapPtr;
291 {
292 RF_ReconMapListElem_t *p, *q;
293 RF_ReconUnitCount_t numRUs;
294 RF_ReconUnitNum_t i;
295
296 numRUs = mapPtr->sectorsInDisk / mapPtr->sectorsPerReconUnit;
297 if (mapPtr->sectorsInDisk % mapPtr->sectorsPerReconUnit)
298 numRUs++;
299
300 for (i = 0; i < numRUs; i++) {
301 p = mapPtr->status[i];
302 while (p != RU_NOTHING && p != RU_ALL) {
303 q = p;
304 p = p->next;
305 RF_Free(q, sizeof(*q));
306 }
307 }
308 rf_mutex_destroy(&mapPtr->mutex);
309 RF_Free(mapPtr->status, mapPtr->totalRUs *
310 sizeof(RF_ReconMapListElem_t *));
311 RF_Free(mapPtr, sizeof(RF_ReconMap_t));
312 }
313 /*---------------------------------------------------------------------------
314 *
315 * returns nonzero if the indicated RU has been reconstructed already
316 *
317 *-------------------------------------------------------------------------*/
318
319 int
320 rf_CheckRUReconstructed(mapPtr, startSector)
321 RF_ReconMap_t *mapPtr;
322 RF_SectorNum_t startSector;
323 {
324 RF_ReconMapListElem_t *l; /* used for searching */
325 RF_ReconUnitNum_t i;
326
327 i = startSector / mapPtr->sectorsPerReconUnit;
328 l = mapPtr->status[i];
329 return ((l == RU_ALL) ? 1 : 0);
330 }
331
332 RF_ReconUnitCount_t
333 rf_UnitsLeftToReconstruct(mapPtr)
334 RF_ReconMap_t *mapPtr;
335 {
336 RF_ASSERT(mapPtr != NULL);
337 return (mapPtr->unitsLeft);
338 }
339 /* updates the size fields of a status descriptor */
340 static void
341 update_size(mapPtr, size)
342 RF_ReconMap_t *mapPtr;
343 int size;
344 {
345 mapPtr->size += size;
346 mapPtr->maxSize = RF_MAX(mapPtr->size, mapPtr->maxSize);
347 }
348
349 #if 0
350 static void
351 PrintList(listPtr)
352 RF_ReconMapListElem_t *listPtr;
353 {
354 while (listPtr) {
355 printf("%d,%d -> ", (int) listPtr->startSector,
356 (int) listPtr->stopSector);
357 listPtr = listPtr->next;
358 }
359 printf("\n");
360 }
361
362 void
363 rf_PrintReconMap(raidPtr, mapPtr, frow, fcol)
364 RF_Raid_t *raidPtr;
365 RF_ReconMap_t *mapPtr;
366 RF_RowCol_t frow;
367 RF_RowCol_t fcol;
368 {
369 RF_ReconUnitCount_t numRUs;
370 RF_ReconMapListElem_t *p;
371 RF_ReconUnitNum_t i;
372
373 numRUs = mapPtr->totalRUs;
374 if (mapPtr->sectorsInDisk % mapPtr->sectorsPerReconUnit)
375 numRUs++;
376
377 for (i = 0; i < numRUs; i++) {
378 p = mapPtr->status[i];
379 if (p == RU_ALL)/* printf("[%d] ALL\n",i) */
380 ;
381 else
382 if (p == RU_NOTHING) {
383 printf("%d: Unreconstructed\n", i);
384 } else {
385 printf("%d: ", i);
386 PrintList(p);
387 }
388 }
389 }
390 #endif
391
392 #if RF_DEBUG_RECON
393 void
394 rf_PrintReconSchedule(mapPtr, starttime)
395 RF_ReconMap_t *mapPtr;
396 struct timeval *starttime;
397 {
398 static int old_pctg = -1;
399 struct timeval tv, diff;
400 int new_pctg;
401
402 new_pctg = 100 - (rf_UnitsLeftToReconstruct(mapPtr) *
403 100 / mapPtr->totalRUs);
404 if (new_pctg != old_pctg) {
405 RF_GETTIME(tv);
406 RF_TIMEVAL_DIFF(starttime, &tv, &diff);
407 printf("%d %d.%06d\n", (int) new_pctg, (int) diff.tv_sec,
408 (int) diff.tv_usec);
409 old_pctg = new_pctg;
410 }
411 }
412 #endif
413
414