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