Home | History | Annotate | Line # | Download | only in raidframe
rf_disks.c revision 1.60
      1 /*	$NetBSD: rf_disks.c,v 1.60 2006/05/14 21:45:00 elad Exp $	*/
      2 /*-
      3  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Greg Oster
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1995 Carnegie-Mellon University.
     40  * All rights reserved.
     41  *
     42  * Author: Mark Holland
     43  *
     44  * Permission to use, copy, modify and distribute this software and
     45  * its documentation is hereby granted, provided that both the copyright
     46  * notice and this permission notice appear in all copies of the
     47  * software, derivative works or modified versions, and any portions
     48  * thereof, and that both notices appear in supporting documentation.
     49  *
     50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     53  *
     54  * Carnegie Mellon requests users of this software to return to
     55  *
     56  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     57  *  School of Computer Science
     58  *  Carnegie Mellon University
     59  *  Pittsburgh PA 15213-3890
     60  *
     61  * any improvements or extensions that they make and grant Carnegie the
     62  * rights to redistribute these changes.
     63  */
     64 
     65 /***************************************************************
     66  * rf_disks.c -- code to perform operations on the actual disks
     67  ***************************************************************/
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: rf_disks.c,v 1.60 2006/05/14 21:45:00 elad Exp $");
     71 
     72 #include <dev/raidframe/raidframevar.h>
     73 
     74 #include "rf_raid.h"
     75 #include "rf_alloclist.h"
     76 #include "rf_utils.h"
     77 #include "rf_general.h"
     78 #include "rf_options.h"
     79 #include "rf_kintf.h"
     80 #include "rf_netbsd.h"
     81 
     82 #include <sys/param.h>
     83 #include <sys/systm.h>
     84 #include <sys/proc.h>
     85 #include <sys/ioctl.h>
     86 #include <sys/fcntl.h>
     87 #include <sys/vnode.h>
     88 #include <sys/kauth.h>
     89 
     90 static int rf_AllocDiskStructures(RF_Raid_t *, RF_Config_t *);
     91 static void rf_print_label_status( RF_Raid_t *, int, char *,
     92 				  RF_ComponentLabel_t *);
     93 static int rf_check_label_vitals( RF_Raid_t *, int, int, char *,
     94 				  RF_ComponentLabel_t *, int, int );
     95 
     96 #define DPRINTF6(a,b,c,d,e,f) if (rf_diskDebug) printf(a,b,c,d,e,f)
     97 #define DPRINTF7(a,b,c,d,e,f,g) if (rf_diskDebug) printf(a,b,c,d,e,f,g)
     98 
     99 /**************************************************************************
    100  *
    101  * initialize the disks comprising the array
    102  *
    103  * We want the spare disks to have regular row,col numbers so that we can
    104  * easily substitue a spare for a failed disk.  But, the driver code assumes
    105  * throughout that the array contains numRow by numCol _non-spare_ disks, so
    106  * it's not clear how to fit in the spares.  This is an unfortunate holdover
    107  * from raidSim.  The quick and dirty fix is to make row zero bigger than the
    108  * rest, and put all the spares in it.  This probably needs to get changed
    109  * eventually.
    110  *
    111  **************************************************************************/
    112 
    113 int
    114 rf_ConfigureDisks(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
    115 		  RF_Config_t *cfgPtr)
    116 {
    117 	RF_RaidDisk_t *disks;
    118 	RF_SectorCount_t min_numblks = (RF_SectorCount_t) 0x7FFFFFFFFFFFLL;
    119 	RF_RowCol_t c;
    120 	int bs, ret;
    121 	unsigned i, count, foundone = 0, numFailuresThisRow;
    122 	int force;
    123 
    124 	force = cfgPtr->force;
    125 
    126 	ret = rf_AllocDiskStructures(raidPtr, cfgPtr);
    127 	if (ret)
    128 		goto fail;
    129 
    130 	disks = raidPtr->Disks;
    131 
    132 	numFailuresThisRow = 0;
    133 	for (c = 0; c < raidPtr->numCol; c++) {
    134 		ret = rf_ConfigureDisk(raidPtr,
    135 				       &cfgPtr->devnames[0][c][0],
    136 				       &disks[c], c);
    137 
    138 		if (ret)
    139 			goto fail;
    140 
    141 		if (disks[c].status == rf_ds_optimal) {
    142 			raidread_component_label(
    143 						 raidPtr->raid_cinfo[c].ci_dev,
    144 						 raidPtr->raid_cinfo[c].ci_vp,
    145 						 &raidPtr->raid_cinfo[c].ci_label);
    146 		}
    147 
    148 		if (disks[c].status != rf_ds_optimal) {
    149 			numFailuresThisRow++;
    150 		} else {
    151 			if (disks[c].numBlocks < min_numblks)
    152 				min_numblks = disks[c].numBlocks;
    153 			DPRINTF6("Disk at col %d: dev %s numBlocks %ld blockSize %d (%ld MB)\n",
    154 				 c, disks[c].devname,
    155 				 (long int) disks[c].numBlocks,
    156 				 disks[c].blockSize,
    157 				 (long int) disks[c].numBlocks *
    158 				 disks[c].blockSize / 1024 / 1024);
    159 		}
    160 	}
    161 	/* XXX fix for n-fault tolerant */
    162 	/* XXX this should probably check to see how many failures
    163 	   we can handle for this configuration! */
    164 	if (numFailuresThisRow > 0)
    165 		raidPtr->status = rf_rs_degraded;
    166 
    167 	/* all disks must be the same size & have the same block size, bs must
    168 	 * be a power of 2 */
    169 	bs = 0;
    170 	foundone = 0;
    171 	for (c = 0; c < raidPtr->numCol; c++) {
    172 		if (disks[c].status == rf_ds_optimal) {
    173 			bs = disks[c].blockSize;
    174 			foundone = 1;
    175 			break;
    176 		}
    177 	}
    178 	if (!foundone) {
    179 		RF_ERRORMSG("RAIDFRAME: Did not find any live disks in the array.\n");
    180 		ret = EINVAL;
    181 		goto fail;
    182 	}
    183 	for (count = 0, i = 1; i; i <<= 1)
    184 		if (bs & i)
    185 			count++;
    186 	if (count != 1) {
    187 		RF_ERRORMSG1("Error: block size on disks (%d) must be a power of 2\n", bs);
    188 		ret = EINVAL;
    189 		goto fail;
    190 	}
    191 
    192 	if (rf_CheckLabels( raidPtr, cfgPtr )) {
    193 		printf("raid%d: There were fatal errors\n", raidPtr->raidid);
    194 		if (force != 0) {
    195 			printf("raid%d: Fatal errors being ignored.\n",
    196 			       raidPtr->raidid);
    197 		} else {
    198 			ret = EINVAL;
    199 			goto fail;
    200 		}
    201 	}
    202 
    203 	for (c = 0; c < raidPtr->numCol; c++) {
    204 		if (disks[c].status == rf_ds_optimal) {
    205 			if (disks[c].blockSize != bs) {
    206 				RF_ERRORMSG1("Error: block size of disk at c %d different from disk at c 0\n", c);
    207 				ret = EINVAL;
    208 				goto fail;
    209 			}
    210 			if (disks[c].numBlocks != min_numblks) {
    211 				RF_ERRORMSG2("WARNING: truncating disk at c %d to %d blocks\n",
    212 					     c, (int) min_numblks);
    213 				disks[c].numBlocks = min_numblks;
    214 			}
    215 		}
    216 	}
    217 
    218 	raidPtr->sectorsPerDisk = min_numblks;
    219 	raidPtr->logBytesPerSector = ffs(bs) - 1;
    220 	raidPtr->bytesPerSector = bs;
    221 	raidPtr->sectorMask = bs - 1;
    222 	return (0);
    223 
    224 fail:
    225 
    226 	rf_UnconfigureVnodes( raidPtr );
    227 
    228 	return (ret);
    229 }
    230 
    231 
    232 /****************************************************************************
    233  * set up the data structures describing the spare disks in the array
    234  * recall from the above comment that the spare disk descriptors are stored
    235  * in row zero, which is specially expanded to hold them.
    236  ****************************************************************************/
    237 int
    238 rf_ConfigureSpareDisks(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
    239 		       RF_Config_t *cfgPtr)
    240 {
    241 	int     i, ret;
    242 	unsigned int bs;
    243 	RF_RaidDisk_t *disks;
    244 	int     num_spares_done;
    245 
    246 	num_spares_done = 0;
    247 
    248 	/* The space for the spares should have already been allocated by
    249 	 * ConfigureDisks() */
    250 
    251 	disks = &raidPtr->Disks[raidPtr->numCol];
    252 	for (i = 0; i < raidPtr->numSpare; i++) {
    253 		ret = rf_ConfigureDisk(raidPtr, &cfgPtr->spare_names[i][0],
    254 				       &disks[i], raidPtr->numCol + i);
    255 		if (ret)
    256 			goto fail;
    257 		if (disks[i].status != rf_ds_optimal) {
    258 			RF_ERRORMSG1("Warning: spare disk %s failed TUR\n",
    259 				     &cfgPtr->spare_names[i][0]);
    260 		} else {
    261 			disks[i].status = rf_ds_spare;	/* change status to
    262 							 * spare */
    263 			DPRINTF6("Spare Disk %d: dev %s numBlocks %ld blockSize %d (%ld MB)\n", i,
    264 			    disks[i].devname,
    265 			    (long int) disks[i].numBlocks, disks[i].blockSize,
    266 			    (long int) disks[i].numBlocks *
    267 				 disks[i].blockSize / 1024 / 1024);
    268 		}
    269 		num_spares_done++;
    270 	}
    271 
    272 	/* check sizes and block sizes on spare disks */
    273 	bs = 1 << raidPtr->logBytesPerSector;
    274 	for (i = 0; i < raidPtr->numSpare; i++) {
    275 		if (disks[i].blockSize != bs) {
    276 			RF_ERRORMSG3("Block size of %d on spare disk %s is not the same as on other disks (%d)\n", disks[i].blockSize, disks[i].devname, bs);
    277 			ret = EINVAL;
    278 			goto fail;
    279 		}
    280 		if (disks[i].numBlocks < raidPtr->sectorsPerDisk) {
    281 			RF_ERRORMSG3("Spare disk %s (%d blocks) is too small to serve as a spare (need %ld blocks)\n",
    282 				     disks[i].devname, disks[i].blockSize,
    283 				     (long int) raidPtr->sectorsPerDisk);
    284 			ret = EINVAL;
    285 			goto fail;
    286 		} else
    287 			if (disks[i].numBlocks > raidPtr->sectorsPerDisk) {
    288 				RF_ERRORMSG3("Warning: truncating spare disk %s to %ld blocks (from %ld)\n",
    289 				    disks[i].devname,
    290 				    (long int) raidPtr->sectorsPerDisk,
    291 				    (long int) disks[i].numBlocks);
    292 
    293 				disks[i].numBlocks = raidPtr->sectorsPerDisk;
    294 			}
    295 	}
    296 
    297 	return (0);
    298 
    299 fail:
    300 
    301 	/* Release the hold on the main components.  We've failed to allocate
    302 	 * a spare, and since we're failing, we need to free things..
    303 
    304 	 XXX failing to allocate a spare is *not* that big of a deal...
    305 	 We *can* survive without it, if need be, esp. if we get hot
    306 	 adding working.
    307 
    308 	 If we don't fail out here, then we need a way to remove this spare...
    309 	 that should be easier to do here than if we are "live"...
    310 
    311 	 */
    312 
    313 	rf_UnconfigureVnodes( raidPtr );
    314 
    315 	return (ret);
    316 }
    317 
    318 static int
    319 rf_AllocDiskStructures(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr)
    320 {
    321 	int ret;
    322 
    323 	/* We allocate RF_MAXSPARE on the first row so that we
    324 	   have room to do hot-swapping of spares */
    325 	RF_MallocAndAdd(raidPtr->Disks, (raidPtr->numCol + RF_MAXSPARE) *
    326 			sizeof(RF_RaidDisk_t), (RF_RaidDisk_t *),
    327 			raidPtr->cleanupList);
    328 	if (raidPtr->Disks == NULL) {
    329 		ret = ENOMEM;
    330 		goto fail;
    331 	}
    332 
    333 	/* get space for device specific stuff.. */
    334 	RF_MallocAndAdd(raidPtr->raid_cinfo,
    335 			(raidPtr->numCol + RF_MAXSPARE) *
    336 			sizeof(struct raidcinfo), (struct raidcinfo *),
    337 			raidPtr->cleanupList);
    338 
    339 	if (raidPtr->raid_cinfo == NULL) {
    340 		ret = ENOMEM;
    341 		goto fail;
    342 	}
    343 
    344 	return(0);
    345 fail:
    346 	rf_UnconfigureVnodes( raidPtr );
    347 
    348 	return(ret);
    349 }
    350 
    351 
    352 /* configure a single disk during auto-configuration at boot */
    353 int
    354 rf_AutoConfigureDisks(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr,
    355 		      RF_AutoConfig_t *auto_config)
    356 {
    357 	RF_RaidDisk_t *disks;
    358 	RF_RaidDisk_t *diskPtr;
    359 	RF_RowCol_t c;
    360 	RF_SectorCount_t min_numblks = (RF_SectorCount_t) 0x7FFFFFFFFFFFLL;
    361 	int bs, ret;
    362 	int numFailuresThisRow;
    363 	RF_AutoConfig_t *ac;
    364 	int parity_good;
    365 	int mod_counter;
    366 	int mod_counter_found;
    367 
    368 #if DEBUG
    369 	printf("Starting autoconfiguration of RAID set...\n");
    370 #endif
    371 
    372 	ret = rf_AllocDiskStructures(raidPtr, cfgPtr);
    373 	if (ret)
    374 		goto fail;
    375 
    376 	disks = raidPtr->Disks;
    377 
    378 	/* assume the parity will be fine.. */
    379 	parity_good = RF_RAID_CLEAN;
    380 
    381 	/* Check for mod_counters that are too low */
    382 	mod_counter_found = 0;
    383 	mod_counter = 0;
    384 	ac = auto_config;
    385 	while(ac!=NULL) {
    386 		if (mod_counter_found==0) {
    387 			mod_counter = ac->clabel->mod_counter;
    388 			mod_counter_found = 1;
    389 		} else {
    390 			if (ac->clabel->mod_counter > mod_counter) {
    391 				mod_counter = ac->clabel->mod_counter;
    392 			}
    393 		}
    394 		ac->flag = 0; /* clear the general purpose flag */
    395 		ac = ac->next;
    396 	}
    397 
    398 	bs = 0;
    399 
    400 	numFailuresThisRow = 0;
    401 	for (c = 0; c < raidPtr->numCol; c++) {
    402 		diskPtr = &disks[c];
    403 
    404 		/* find this row/col in the autoconfig */
    405 #if DEBUG
    406 		printf("Looking for %d in autoconfig\n",c);
    407 #endif
    408 		ac = auto_config;
    409 		while(ac!=NULL) {
    410 			if (ac->clabel==NULL) {
    411 				/* big-time bad news. */
    412 				goto fail;
    413 			}
    414 			if ((ac->clabel->column == c) &&
    415 			    (ac->clabel->mod_counter == mod_counter)) {
    416 				/* it's this one... */
    417 				/* flag it as 'used', so we don't
    418 				   free it later. */
    419 				ac->flag = 1;
    420 #if DEBUG
    421 				printf("Found: %s at %d\n",
    422 				       ac->devname,c);
    423 #endif
    424 
    425 				break;
    426 			}
    427 			ac=ac->next;
    428 		}
    429 
    430 		if (ac==NULL) {
    431 			/* we didn't find an exact match with a
    432 			   correct mod_counter above... can we find
    433 			   one with an incorrect mod_counter to use
    434 			   instead?  (this one, if we find it, will be
    435 			   marked as failed once the set configures)
    436 			*/
    437 
    438 			ac = auto_config;
    439 			while(ac!=NULL) {
    440 				if (ac->clabel==NULL) {
    441 					/* big-time bad news. */
    442 					goto fail;
    443 				}
    444 				if (ac->clabel->column == c) {
    445 					/* it's this one...
    446 					   flag it as 'used', so we
    447 					   don't free it later. */
    448 					ac->flag = 1;
    449 #if DEBUG
    450 					printf("Found(low mod_counter): %s at %d\n",
    451 					       ac->devname,c);
    452 #endif
    453 
    454 					break;
    455 				}
    456 				ac=ac->next;
    457 			}
    458 		}
    459 
    460 
    461 
    462 		if (ac!=NULL) {
    463 			/* Found it.  Configure it.. */
    464 			diskPtr->blockSize = ac->clabel->blockSize;
    465 			diskPtr->numBlocks = ac->clabel->numBlocks;
    466 			/* Note: rf_protectedSectors is already
    467 			   factored into numBlocks here */
    468 			raidPtr->raid_cinfo[c].ci_vp = ac->vp;
    469 			raidPtr->raid_cinfo[c].ci_dev = ac->dev;
    470 
    471 			memcpy(&raidPtr->raid_cinfo[c].ci_label,
    472 			    ac->clabel, sizeof(*ac->clabel));
    473 			snprintf(diskPtr->devname, sizeof(diskPtr->devname),
    474 			    "/dev/%s", ac->devname);
    475 
    476 			/* note the fact that this component was
    477 			   autoconfigured.  You'll need this info
    478 			   later.  Trust me :) */
    479 			diskPtr->auto_configured = 1;
    480 			diskPtr->dev = ac->dev;
    481 
    482 			/*
    483 			 * we allow the user to specify that
    484 			 * only a fraction of the disks should
    485 			 * be used this is just for debug: it
    486 			 * speeds up the parity scan
    487 			 */
    488 
    489 			diskPtr->numBlocks = diskPtr->numBlocks *
    490 				rf_sizePercentage / 100;
    491 
    492 			/* XXX these will get set multiple times,
    493 			   but since we're autoconfiguring, they'd
    494 			   better be always the same each time!
    495 			   If not, this is the least of your worries */
    496 
    497 			bs = diskPtr->blockSize;
    498 			min_numblks = diskPtr->numBlocks;
    499 
    500 			/* this gets done multiple times, but that's
    501 			   fine -- the serial number will be the same
    502 			   for all components, guaranteed */
    503 			raidPtr->serial_number = ac->clabel->serial_number;
    504 			/* check the last time the label was modified */
    505 
    506 			if (ac->clabel->mod_counter != mod_counter) {
    507 				/* Even though we've filled in all of
    508 				   the above, we don't trust this
    509 				   component since it's modification
    510 				   counter is not in sync with the
    511 				   rest, and we really consider it to
    512 				   be failed.  */
    513 				disks[c].status = rf_ds_failed;
    514 				numFailuresThisRow++;
    515 			} else {
    516 				if (ac->clabel->clean != RF_RAID_CLEAN) {
    517 					parity_good = RF_RAID_DIRTY;
    518 				}
    519 			}
    520 		} else {
    521 			/* Didn't find it at all!!  Component must
    522 			   really be dead */
    523 			disks[c].status = rf_ds_failed;
    524 			snprintf(disks[c].devname, sizeof(disks[c].devname),
    525 			    "component%d", c);
    526 			numFailuresThisRow++;
    527 		}
    528 	}
    529 	/* XXX fix for n-fault tolerant */
    530 	/* XXX this should probably check to see how many failures
    531 	   we can handle for this configuration! */
    532 	if (numFailuresThisRow > 0) {
    533 		raidPtr->status = rf_rs_degraded;
    534 		raidPtr->numFailures = numFailuresThisRow;
    535 	}
    536 
    537 	/* close the device for the ones that didn't get used */
    538 
    539 	ac = auto_config;
    540 	while(ac!=NULL) {
    541 		if (ac->flag == 0) {
    542 			vn_lock(ac->vp, LK_EXCLUSIVE | LK_RETRY);
    543 			VOP_CLOSE(ac->vp, FREAD | FWRITE, NOCRED, 0);
    544 			vput(ac->vp);
    545 			ac->vp = NULL;
    546 #if DEBUG
    547 			printf("Released %s from auto-config set.\n",
    548 			       ac->devname);
    549 #endif
    550 		}
    551 		ac = ac->next;
    552 	}
    553 
    554 	raidPtr->mod_counter = mod_counter;
    555 
    556 	/* note the state of the parity, if any */
    557 	raidPtr->parity_good = parity_good;
    558 	raidPtr->sectorsPerDisk = min_numblks;
    559 	raidPtr->logBytesPerSector = ffs(bs) - 1;
    560 	raidPtr->bytesPerSector = bs;
    561 	raidPtr->sectorMask = bs - 1;
    562 	return (0);
    563 
    564 fail:
    565 
    566 	rf_UnconfigureVnodes( raidPtr );
    567 
    568 	return (ret);
    569 
    570 }
    571 
    572 /* configure a single disk in the array */
    573 int
    574 rf_ConfigureDisk(RF_Raid_t *raidPtr, char *bf, RF_RaidDisk_t *diskPtr,
    575 		 RF_RowCol_t col)
    576 {
    577 	char   *p;
    578 	struct partinfo dpart;
    579 	struct vnode *vp;
    580 	struct vattr va;
    581 	struct lwp *l;
    582 	int     error;
    583 
    584 	p = rf_find_non_white(bf);
    585 	if (p[strlen(p) - 1] == '\n') {
    586 		/* strip off the newline */
    587 		p[strlen(p) - 1] = '\0';
    588 	}
    589 	(void) strcpy(diskPtr->devname, p);
    590 
    591 	l = LIST_FIRST(&raidPtr->engine_thread->p_lwps);
    592 
    593 	/* Let's start by claiming the component is fine and well... */
    594 	diskPtr->status = rf_ds_optimal;
    595 
    596 	raidPtr->raid_cinfo[col].ci_vp = NULL;
    597 	raidPtr->raid_cinfo[col].ci_dev = 0;
    598 
    599 	if (!strcmp("absent", diskPtr->devname)) {
    600 		printf("Ignoring missing component at column %d\n", col);
    601 		sprintf(diskPtr->devname, "component%d", col);
    602 		diskPtr->status = rf_ds_failed;
    603 		return (0);
    604 	}
    605 
    606 	error = raidlookup(diskPtr->devname, l, &vp);
    607 	if (error) {
    608 		printf("raidlookup on device: %s failed!\n", diskPtr->devname);
    609 		if (error == ENXIO) {
    610 			/* the component isn't there... must be dead :-( */
    611 			diskPtr->status = rf_ds_failed;
    612 		} else {
    613 			return (error);
    614 		}
    615 	}
    616 	if (diskPtr->status == rf_ds_optimal) {
    617 
    618 		if ((error = VOP_GETATTR(vp, &va,
    619 		    l->l_proc->p_cred, l)) != 0) {
    620 			return (error);
    621 		}
    622 		error = VOP_IOCTL(vp, DIOCGPART, &dpart,
    623 				  FREAD, l->l_proc->p_cred, l);
    624 		if (error) {
    625 			return (error);
    626 		}
    627 
    628 		diskPtr->blockSize = dpart.disklab->d_secsize;
    629 
    630 		diskPtr->numBlocks = dpart.part->p_size - rf_protectedSectors;
    631 		diskPtr->partitionSize = dpart.part->p_size;
    632 
    633 		raidPtr->raid_cinfo[col].ci_vp = vp;
    634 		raidPtr->raid_cinfo[col].ci_dev = va.va_rdev;
    635 
    636 		/* This component was not automatically configured */
    637 		diskPtr->auto_configured = 0;
    638 		diskPtr->dev = va.va_rdev;
    639 
    640 		/* we allow the user to specify that only a fraction of the
    641 		 * disks should be used this is just for debug:  it speeds up
    642 		 * the parity scan */
    643 		diskPtr->numBlocks = diskPtr->numBlocks *
    644 			rf_sizePercentage / 100;
    645 	}
    646 	return (0);
    647 }
    648 
    649 static void
    650 rf_print_label_status(RF_Raid_t *raidPtr, int column, char *dev_name,
    651 		      RF_ComponentLabel_t *ci_label)
    652 {
    653 
    654 	printf("raid%d: Component %s being configured at col: %d\n",
    655 	       raidPtr->raidid, dev_name, column );
    656 	printf("         Column: %d Num Columns: %d\n",
    657 	       ci_label->column,
    658 	       ci_label->num_columns);
    659 	printf("         Version: %d Serial Number: %d Mod Counter: %d\n",
    660 	       ci_label->version, ci_label->serial_number,
    661 	       ci_label->mod_counter);
    662 	printf("         Clean: %s Status: %d\n",
    663 	       ci_label->clean ? "Yes" : "No", ci_label->status );
    664 }
    665 
    666 static int rf_check_label_vitals(RF_Raid_t *raidPtr, int row, int column,
    667 				 char *dev_name, RF_ComponentLabel_t *ci_label,
    668 				 int serial_number, int mod_counter)
    669 {
    670 	int fatal_error = 0;
    671 
    672 	if (serial_number != ci_label->serial_number) {
    673 		printf("%s has a different serial number: %d %d\n",
    674 		       dev_name, serial_number, ci_label->serial_number);
    675 		fatal_error = 1;
    676 	}
    677 	if (mod_counter != ci_label->mod_counter) {
    678 		printf("%s has a different modfication count: %d %d\n",
    679 		       dev_name, mod_counter, ci_label->mod_counter);
    680 	}
    681 
    682 	if (row != ci_label->row) {
    683 		printf("Row out of alignment for: %s\n", dev_name);
    684 		fatal_error = 1;
    685 	}
    686 	if (column != ci_label->column) {
    687 		printf("Column out of alignment for: %s\n", dev_name);
    688 		fatal_error = 1;
    689 	}
    690 	if (raidPtr->numCol != ci_label->num_columns) {
    691 		printf("Number of columns do not match for: %s\n", dev_name);
    692 		fatal_error = 1;
    693 	}
    694 	if (ci_label->clean == 0) {
    695 		/* it's not clean, but that's not fatal */
    696 		printf("%s is not clean!\n", dev_name);
    697 	}
    698 	return(fatal_error);
    699 }
    700 
    701 
    702 /*
    703 
    704    rf_CheckLabels() - check all the component labels for consistency.
    705    Return an error if there is anything major amiss.
    706 
    707  */
    708 
    709 int
    710 rf_CheckLabels(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr)
    711 {
    712 	int c;
    713 	char *dev_name;
    714 	RF_ComponentLabel_t *ci_label;
    715 	int serial_number = 0;
    716 	int mod_number = 0;
    717 	int fatal_error = 0;
    718 	int mod_values[4];
    719 	int mod_count[4];
    720 	int ser_values[4];
    721 	int ser_count[4];
    722 	int num_ser;
    723 	int num_mod;
    724 	int i;
    725 	int found;
    726 	int hosed_column;
    727 	int too_fatal;
    728 	int parity_good;
    729 	int force;
    730 
    731 	hosed_column = -1;
    732 	too_fatal = 0;
    733 	force = cfgPtr->force;
    734 
    735 	/*
    736 	   We're going to try to be a little intelligent here.  If one
    737 	   component's label is bogus, and we can identify that it's the
    738 	   *only* one that's gone, we'll mark it as "failed" and allow
    739 	   the configuration to proceed.  This will be the *only* case
    740 	   that we'll proceed if there would be (otherwise) fatal errors.
    741 
    742 	   Basically we simply keep a count of how many components had
    743 	   what serial number.  If all but one agree, we simply mark
    744 	   the disagreeing component as being failed, and allow
    745 	   things to come up "normally".
    746 
    747 	   We do this first for serial numbers, and then for "mod_counter".
    748 
    749 	 */
    750 
    751 	num_ser = 0;
    752 	num_mod = 0;
    753 
    754 	for (c = 0; c < raidPtr->numCol; c++) {
    755 		ci_label = &raidPtr->raid_cinfo[c].ci_label;
    756 		found=0;
    757 		for(i=0;i<num_ser;i++) {
    758 			if (ser_values[i] == ci_label->serial_number) {
    759 				ser_count[i]++;
    760 				found=1;
    761 				break;
    762 			}
    763 		}
    764 		if (!found) {
    765 			ser_values[num_ser] = ci_label->serial_number;
    766 			ser_count[num_ser] = 1;
    767 			num_ser++;
    768 			if (num_ser>2) {
    769 				fatal_error = 1;
    770 				break;
    771 			}
    772 		}
    773 		found=0;
    774 		for(i=0;i<num_mod;i++) {
    775 			if (mod_values[i] == ci_label->mod_counter) {
    776 				mod_count[i]++;
    777 				found=1;
    778 				break;
    779 			}
    780 		}
    781 		if (!found) {
    782 			mod_values[num_mod] = ci_label->mod_counter;
    783 			mod_count[num_mod] = 1;
    784 			num_mod++;
    785 			if (num_mod>2) {
    786 				fatal_error = 1;
    787 				break;
    788 			}
    789 		}
    790 	}
    791 #if DEBUG
    792 	printf("raid%d: Summary of serial numbers:\n", raidPtr->raidid);
    793 	for(i=0;i<num_ser;i++) {
    794 		printf("%d %d\n", ser_values[i], ser_count[i]);
    795 	}
    796 	printf("raid%d: Summary of mod counters:\n", raidPtr->raidid);
    797 	for(i=0;i<num_mod;i++) {
    798 		printf("%d %d\n", mod_values[i], mod_count[i]);
    799 	}
    800 #endif
    801 	serial_number = ser_values[0];
    802 	if (num_ser == 2) {
    803 		if ((ser_count[0] == 1) || (ser_count[1] == 1)) {
    804 			/* Locate the maverick component */
    805 			if (ser_count[1] > ser_count[0]) {
    806 				serial_number = ser_values[1];
    807 			}
    808 
    809 			for (c = 0; c < raidPtr->numCol; c++) {
    810 				ci_label = &raidPtr->raid_cinfo[c].ci_label;
    811 				if (serial_number != ci_label->serial_number) {
    812 					hosed_column = c;
    813 					break;
    814 				}
    815 			}
    816 			printf("Hosed component: %s\n",
    817 			       &cfgPtr->devnames[0][hosed_column][0]);
    818 			if (!force) {
    819 				/* we'll fail this component, as if there are
    820 				   other major errors, we arn't forcing things
    821 				   and we'll abort the config anyways */
    822 				raidPtr->Disks[hosed_column].status
    823 					= rf_ds_failed;
    824 				raidPtr->numFailures++;
    825 				raidPtr->status = rf_rs_degraded;
    826 			}
    827 		} else {
    828 			too_fatal = 1;
    829 		}
    830 		if (cfgPtr->parityConfig == '0') {
    831 			/* We've identified two different serial numbers.
    832 			   RAID 0 can't cope with that, so we'll punt */
    833 			too_fatal = 1;
    834 		}
    835 
    836 	}
    837 
    838 	/* record the serial number for later.  If we bail later, setting
    839 	   this doesn't matter, otherwise we've got the best guess at the
    840 	   correct serial number */
    841 	raidPtr->serial_number = serial_number;
    842 
    843 	mod_number = mod_values[0];
    844 	if (num_mod == 2) {
    845 		if ((mod_count[0] == 1) || (mod_count[1] == 1)) {
    846 			/* Locate the maverick component */
    847 			if (mod_count[1] > mod_count[0]) {
    848 				mod_number = mod_values[1];
    849 			} else if (mod_count[1] < mod_count[0]) {
    850 				mod_number = mod_values[0];
    851 			} else {
    852 				/* counts of different modification values
    853 				   are the same.   Assume greater value is
    854 				   the correct one, all other things
    855 				   considered */
    856 				if (mod_values[0] > mod_values[1]) {
    857 					mod_number = mod_values[0];
    858 				} else {
    859 					mod_number = mod_values[1];
    860 				}
    861 
    862 			}
    863 
    864 			for (c = 0; c < raidPtr->numCol; c++) {
    865 				ci_label = &raidPtr->raid_cinfo[c].ci_label;
    866 				if (mod_number != ci_label->mod_counter) {
    867 					if (hosed_column == c) {
    868 						/* same one.  Can
    869 						   deal with it.  */
    870 					} else {
    871 						hosed_column = c;
    872 						if (num_ser != 1) {
    873 							too_fatal = 1;
    874 							break;
    875 						}
    876 					}
    877 				}
    878 			}
    879 			printf("Hosed component: %s\n",
    880 			       &cfgPtr->devnames[0][hosed_column][0]);
    881 			if (!force) {
    882 				/* we'll fail this component, as if there are
    883 				   other major errors, we arn't forcing things
    884 				   and we'll abort the config anyways */
    885 				if (raidPtr->Disks[hosed_column].status != rf_ds_failed) {
    886 					raidPtr->Disks[hosed_column].status
    887 						= rf_ds_failed;
    888 					raidPtr->numFailures++;
    889 					raidPtr->status = rf_rs_degraded;
    890 				}
    891 			}
    892 		} else {
    893 			too_fatal = 1;
    894 		}
    895 		if (cfgPtr->parityConfig == '0') {
    896 			/* We've identified two different mod counters.
    897 			   RAID 0 can't cope with that, so we'll punt */
    898 			too_fatal = 1;
    899 		}
    900 	}
    901 
    902 	raidPtr->mod_counter = mod_number;
    903 
    904 	if (too_fatal) {
    905 		/* we've had both a serial number mismatch, and a mod_counter
    906 		   mismatch -- and they involved two different components!!
    907 		   Bail -- make things fail so that the user must force
    908 		   the issue... */
    909 		hosed_column = -1;
    910 		fatal_error = 1;
    911 	}
    912 
    913 	if (num_ser > 2) {
    914 		printf("raid%d: Too many different serial numbers!\n",
    915 		       raidPtr->raidid);
    916 		fatal_error = 1;
    917 	}
    918 
    919 	if (num_mod > 2) {
    920 		printf("raid%d: Too many different mod counters!\n",
    921 		       raidPtr->raidid);
    922 		fatal_error = 1;
    923 	}
    924 
    925 	/* we start by assuming the parity will be good, and flee from
    926 	   that notion at the slightest sign of trouble */
    927 
    928 	parity_good = RF_RAID_CLEAN;
    929 
    930 	for (c = 0; c < raidPtr->numCol; c++) {
    931 		dev_name = &cfgPtr->devnames[0][c][0];
    932 		ci_label = &raidPtr->raid_cinfo[c].ci_label;
    933 
    934 		if (c == hosed_column) {
    935 			printf("raid%d: Ignoring %s\n",
    936 			       raidPtr->raidid, dev_name);
    937 		} else {
    938 			rf_print_label_status( raidPtr, c, dev_name, ci_label);
    939 			if (rf_check_label_vitals( raidPtr, 0, c,
    940 						   dev_name, ci_label,
    941 						   serial_number,
    942 						   mod_number )) {
    943 				fatal_error = 1;
    944 			}
    945 			if (ci_label->clean != RF_RAID_CLEAN) {
    946 				parity_good = RF_RAID_DIRTY;
    947 			}
    948 		}
    949 	}
    950 
    951 	if (fatal_error) {
    952 		parity_good = RF_RAID_DIRTY;
    953 	}
    954 
    955 	/* we note the state of the parity */
    956 	raidPtr->parity_good = parity_good;
    957 
    958 	return(fatal_error);
    959 }
    960 
    961 int
    962 rf_add_hot_spare(RF_Raid_t *raidPtr, RF_SingleComponent_t *sparePtr)
    963 {
    964 	RF_RaidDisk_t *disks;
    965 	RF_DiskQueue_t *spareQueues;
    966 	int ret;
    967 	unsigned int bs;
    968 	int spare_number;
    969 
    970 	ret=0;
    971 
    972 	if (raidPtr->numSpare >= RF_MAXSPARE) {
    973 		RF_ERRORMSG1("Too many spares: %d\n", raidPtr->numSpare);
    974 		return(EINVAL);
    975 	}
    976 
    977 	RF_LOCK_MUTEX(raidPtr->mutex);
    978 	while (raidPtr->adding_hot_spare==1) {
    979 		ltsleep(&(raidPtr->adding_hot_spare), PRIBIO, "raidhs", 0,
    980 			&(raidPtr->mutex));
    981 	}
    982 	raidPtr->adding_hot_spare=1;
    983 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    984 
    985 	/* the beginning of the spares... */
    986 	disks = &raidPtr->Disks[raidPtr->numCol];
    987 
    988 	spare_number = raidPtr->numSpare;
    989 
    990 	ret = rf_ConfigureDisk(raidPtr, sparePtr->component_name,
    991 			       &disks[spare_number],
    992 			       raidPtr->numCol + spare_number);
    993 
    994 	if (ret)
    995 		goto fail;
    996 	if (disks[spare_number].status != rf_ds_optimal) {
    997 		RF_ERRORMSG1("Warning: spare disk %s failed TUR\n",
    998 			     sparePtr->component_name);
    999 		rf_close_component(raidPtr, raidPtr->raid_cinfo[raidPtr->numCol+spare_number].ci_vp, 0);
   1000 		ret=EINVAL;
   1001 		goto fail;
   1002 	} else {
   1003 		disks[spare_number].status = rf_ds_spare;
   1004 		DPRINTF6("Spare Disk %d: dev %s numBlocks %ld blockSize %d (%ld MB)\n", spare_number,
   1005 			 disks[spare_number].devname,
   1006 			 (long int) disks[spare_number].numBlocks,
   1007 			 disks[spare_number].blockSize,
   1008 			 (long int) disks[spare_number].numBlocks *
   1009 			 disks[spare_number].blockSize / 1024 / 1024);
   1010 	}
   1011 
   1012 
   1013 	/* check sizes and block sizes on the spare disk */
   1014 	bs = 1 << raidPtr->logBytesPerSector;
   1015 	if (disks[spare_number].blockSize != bs) {
   1016 		RF_ERRORMSG3("Block size of %d on spare disk %s is not the same as on other disks (%d)\n", disks[spare_number].blockSize, disks[spare_number].devname, bs);
   1017 		rf_close_component(raidPtr, raidPtr->raid_cinfo[raidPtr->numCol+spare_number].ci_vp, 0);
   1018 		ret = EINVAL;
   1019 		goto fail;
   1020 	}
   1021 	if (disks[spare_number].numBlocks < raidPtr->sectorsPerDisk) {
   1022 		RF_ERRORMSG3("Spare disk %s (%d blocks) is too small to serve as a spare (need %ld blocks)\n",
   1023 			     disks[spare_number].devname,
   1024 			     disks[spare_number].blockSize,
   1025 			     (long int) raidPtr->sectorsPerDisk);
   1026 		rf_close_component(raidPtr, raidPtr->raid_cinfo[raidPtr->numCol+spare_number].ci_vp, 0);
   1027 		ret = EINVAL;
   1028 		goto fail;
   1029 	} else {
   1030 		if (disks[spare_number].numBlocks >
   1031 		    raidPtr->sectorsPerDisk) {
   1032 			RF_ERRORMSG3("Warning: truncating spare disk %s to %ld blocks (from %ld)\n",
   1033 			    disks[spare_number].devname,
   1034 			    (long int) raidPtr->sectorsPerDisk,
   1035 			    (long int) disks[spare_number].numBlocks);
   1036 
   1037 			disks[spare_number].numBlocks = raidPtr->sectorsPerDisk;
   1038 		}
   1039 	}
   1040 
   1041 	spareQueues = &raidPtr->Queues[raidPtr->numCol];
   1042 	ret = rf_ConfigureDiskQueue( raidPtr, &spareQueues[spare_number],
   1043 				 raidPtr->numCol + spare_number,
   1044 				 raidPtr->qType,
   1045 				 raidPtr->sectorsPerDisk,
   1046 				 raidPtr->Disks[raidPtr->numCol +
   1047 						  spare_number].dev,
   1048 				 raidPtr->maxOutstanding,
   1049 				 &raidPtr->shutdownList,
   1050 				 raidPtr->cleanupList);
   1051 
   1052 	RF_LOCK_MUTEX(raidPtr->mutex);
   1053 	raidPtr->numSpare++;
   1054 	RF_UNLOCK_MUTEX(raidPtr->mutex);
   1055 
   1056 fail:
   1057 	RF_LOCK_MUTEX(raidPtr->mutex);
   1058 	raidPtr->adding_hot_spare=0;
   1059 	wakeup(&(raidPtr->adding_hot_spare));
   1060 	RF_UNLOCK_MUTEX(raidPtr->mutex);
   1061 
   1062 	return(ret);
   1063 }
   1064 
   1065 int
   1066 rf_remove_hot_spare(RF_Raid_t *raidPtr, RF_SingleComponent_t *sparePtr)
   1067 {
   1068 	int spare_number;
   1069 
   1070 
   1071 	if (raidPtr->numSpare==0) {
   1072 		printf("No spares to remove!\n");
   1073 		return(EINVAL);
   1074 	}
   1075 
   1076 	spare_number = sparePtr->column;
   1077 
   1078 	return(EINVAL); /* XXX not implemented yet */
   1079 #if 0
   1080 	if (spare_number < 0 || spare_number > raidPtr->numSpare) {
   1081 		return(EINVAL);
   1082 	}
   1083 
   1084 	/* verify that this spare isn't in use... */
   1085 
   1086 
   1087 
   1088 
   1089 	/* it's gone.. */
   1090 
   1091 	raidPtr->numSpare--;
   1092 
   1093 	return(0);
   1094 #endif
   1095 }
   1096 
   1097 
   1098 int
   1099 rf_delete_component(RF_Raid_t *raidPtr, RF_SingleComponent_t *component)
   1100 {
   1101 	RF_RaidDisk_t *disks;
   1102 
   1103 	if ((component->column < 0) ||
   1104 	    (component->column >= raidPtr->numCol)) {
   1105 		return(EINVAL);
   1106 	}
   1107 
   1108 	disks = &raidPtr->Disks[component->column];
   1109 
   1110 	/* 1. This component must be marked as 'failed' */
   1111 
   1112 	return(EINVAL); /* Not implemented yet. */
   1113 }
   1114 
   1115 int
   1116 rf_incorporate_hot_spare(RF_Raid_t *raidPtr, RF_SingleComponent_t *component)
   1117 {
   1118 
   1119 	/* Issues here include how to 'move' this in if there is IO
   1120 	   taking place (e.g. component queues and such) */
   1121 
   1122 	return(EINVAL); /* Not implemented yet. */
   1123 }
   1124