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