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