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