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