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