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