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