xref: /netbsd-src/sys/dev/raidframe/rf_disks.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: rf_disks.c,v 1.59 2005/12/11 12:23:37 christos Exp $	*/
2 /*-
3  * Copyright (c) 1999 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Greg Oster
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *        This product includes software developed by the NetBSD
20  *        Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Copyright (c) 1995 Carnegie-Mellon University.
40  * All rights reserved.
41  *
42  * Author: Mark Holland
43  *
44  * Permission to use, copy, modify and distribute this software and
45  * its documentation is hereby granted, provided that both the copyright
46  * notice and this permission notice appear in all copies of the
47  * software, derivative works or modified versions, and any portions
48  * thereof, and that both notices appear in supporting documentation.
49  *
50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53  *
54  * Carnegie Mellon requests users of this software to return to
55  *
56  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57  *  School of Computer Science
58  *  Carnegie Mellon University
59  *  Pittsburgh PA 15213-3890
60  *
61  * any improvements or extensions that they make and grant Carnegie the
62  * rights to redistribute these changes.
63  */
64 
65 /***************************************************************
66  * rf_disks.c -- code to perform operations on the actual disks
67  ***************************************************************/
68 
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: rf_disks.c,v 1.59 2005/12/11 12:23:37 christos Exp $");
71 
72 #include <dev/raidframe/raidframevar.h>
73 
74 #include "rf_raid.h"
75 #include "rf_alloclist.h"
76 #include "rf_utils.h"
77 #include "rf_general.h"
78 #include "rf_options.h"
79 #include "rf_kintf.h"
80 #include "rf_netbsd.h"
81 
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/proc.h>
85 #include <sys/ioctl.h>
86 #include <sys/fcntl.h>
87 #include <sys/vnode.h>
88 
89 static int rf_AllocDiskStructures(RF_Raid_t *, RF_Config_t *);
90 static void rf_print_label_status( RF_Raid_t *, int, char *,
91 				  RF_ComponentLabel_t *);
92 static int rf_check_label_vitals( RF_Raid_t *, int, int, char *,
93 				  RF_ComponentLabel_t *, int, int );
94 
95 #define DPRINTF6(a,b,c,d,e,f) if (rf_diskDebug) printf(a,b,c,d,e,f)
96 #define DPRINTF7(a,b,c,d,e,f,g) if (rf_diskDebug) printf(a,b,c,d,e,f,g)
97 
98 /**************************************************************************
99  *
100  * initialize the disks comprising the array
101  *
102  * We want the spare disks to have regular row,col numbers so that we can
103  * easily substitue a spare for a failed disk.  But, the driver code assumes
104  * throughout that the array contains numRow by numCol _non-spare_ disks, so
105  * it's not clear how to fit in the spares.  This is an unfortunate holdover
106  * from raidSim.  The quick and dirty fix is to make row zero bigger than the
107  * rest, and put all the spares in it.  This probably needs to get changed
108  * eventually.
109  *
110  **************************************************************************/
111 
112 int
113 rf_ConfigureDisks(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
114 		  RF_Config_t *cfgPtr)
115 {
116 	RF_RaidDisk_t *disks;
117 	RF_SectorCount_t min_numblks = (RF_SectorCount_t) 0x7FFFFFFFFFFFLL;
118 	RF_RowCol_t c;
119 	int bs, ret;
120 	unsigned i, count, foundone = 0, numFailuresThisRow;
121 	int force;
122 
123 	force = cfgPtr->force;
124 
125 	ret = rf_AllocDiskStructures(raidPtr, cfgPtr);
126 	if (ret)
127 		goto fail;
128 
129 	disks = raidPtr->Disks;
130 
131 	numFailuresThisRow = 0;
132 	for (c = 0; c < raidPtr->numCol; c++) {
133 		ret = rf_ConfigureDisk(raidPtr,
134 				       &cfgPtr->devnames[0][c][0],
135 				       &disks[c], c);
136 
137 		if (ret)
138 			goto fail;
139 
140 		if (disks[c].status == rf_ds_optimal) {
141 			raidread_component_label(
142 						 raidPtr->raid_cinfo[c].ci_dev,
143 						 raidPtr->raid_cinfo[c].ci_vp,
144 						 &raidPtr->raid_cinfo[c].ci_label);
145 		}
146 
147 		if (disks[c].status != rf_ds_optimal) {
148 			numFailuresThisRow++;
149 		} else {
150 			if (disks[c].numBlocks < min_numblks)
151 				min_numblks = disks[c].numBlocks;
152 			DPRINTF6("Disk at col %d: dev %s numBlocks %ld blockSize %d (%ld MB)\n",
153 				 c, disks[c].devname,
154 				 (long int) disks[c].numBlocks,
155 				 disks[c].blockSize,
156 				 (long int) disks[c].numBlocks *
157 				 disks[c].blockSize / 1024 / 1024);
158 		}
159 	}
160 	/* XXX fix for n-fault tolerant */
161 	/* XXX this should probably check to see how many failures
162 	   we can handle for this configuration! */
163 	if (numFailuresThisRow > 0)
164 		raidPtr->status = rf_rs_degraded;
165 
166 	/* all disks must be the same size & have the same block size, bs must
167 	 * be a power of 2 */
168 	bs = 0;
169 	foundone = 0;
170 	for (c = 0; c < raidPtr->numCol; c++) {
171 		if (disks[c].status == rf_ds_optimal) {
172 			bs = disks[c].blockSize;
173 			foundone = 1;
174 			break;
175 		}
176 	}
177 	if (!foundone) {
178 		RF_ERRORMSG("RAIDFRAME: Did not find any live disks in the array.\n");
179 		ret = EINVAL;
180 		goto fail;
181 	}
182 	for (count = 0, i = 1; i; i <<= 1)
183 		if (bs & i)
184 			count++;
185 	if (count != 1) {
186 		RF_ERRORMSG1("Error: block size on disks (%d) must be a power of 2\n", bs);
187 		ret = EINVAL;
188 		goto fail;
189 	}
190 
191 	if (rf_CheckLabels( raidPtr, cfgPtr )) {
192 		printf("raid%d: There were fatal errors\n", raidPtr->raidid);
193 		if (force != 0) {
194 			printf("raid%d: Fatal errors being ignored.\n",
195 			       raidPtr->raidid);
196 		} else {
197 			ret = EINVAL;
198 			goto fail;
199 		}
200 	}
201 
202 	for (c = 0; c < raidPtr->numCol; c++) {
203 		if (disks[c].status == rf_ds_optimal) {
204 			if (disks[c].blockSize != bs) {
205 				RF_ERRORMSG1("Error: block size of disk at c %d different from disk at c 0\n", c);
206 				ret = EINVAL;
207 				goto fail;
208 			}
209 			if (disks[c].numBlocks != min_numblks) {
210 				RF_ERRORMSG2("WARNING: truncating disk at c %d to %d blocks\n",
211 					     c, (int) min_numblks);
212 				disks[c].numBlocks = min_numblks;
213 			}
214 		}
215 	}
216 
217 	raidPtr->sectorsPerDisk = min_numblks;
218 	raidPtr->logBytesPerSector = ffs(bs) - 1;
219 	raidPtr->bytesPerSector = bs;
220 	raidPtr->sectorMask = bs - 1;
221 	return (0);
222 
223 fail:
224 
225 	rf_UnconfigureVnodes( raidPtr );
226 
227 	return (ret);
228 }
229 
230 
231 /****************************************************************************
232  * set up the data structures describing the spare disks in the array
233  * recall from the above comment that the spare disk descriptors are stored
234  * in row zero, which is specially expanded to hold them.
235  ****************************************************************************/
236 int
237 rf_ConfigureSpareDisks(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
238 		       RF_Config_t *cfgPtr)
239 {
240 	int     i, ret;
241 	unsigned int bs;
242 	RF_RaidDisk_t *disks;
243 	int     num_spares_done;
244 
245 	num_spares_done = 0;
246 
247 	/* The space for the spares should have already been allocated by
248 	 * ConfigureDisks() */
249 
250 	disks = &raidPtr->Disks[raidPtr->numCol];
251 	for (i = 0; i < raidPtr->numSpare; i++) {
252 		ret = rf_ConfigureDisk(raidPtr, &cfgPtr->spare_names[i][0],
253 				       &disks[i], raidPtr->numCol + i);
254 		if (ret)
255 			goto fail;
256 		if (disks[i].status != rf_ds_optimal) {
257 			RF_ERRORMSG1("Warning: spare disk %s failed TUR\n",
258 				     &cfgPtr->spare_names[i][0]);
259 		} else {
260 			disks[i].status = rf_ds_spare;	/* change status to
261 							 * spare */
262 			DPRINTF6("Spare Disk %d: dev %s numBlocks %ld blockSize %d (%ld MB)\n", i,
263 			    disks[i].devname,
264 			    (long int) disks[i].numBlocks, disks[i].blockSize,
265 			    (long int) disks[i].numBlocks *
266 				 disks[i].blockSize / 1024 / 1024);
267 		}
268 		num_spares_done++;
269 	}
270 
271 	/* check sizes and block sizes on spare disks */
272 	bs = 1 << raidPtr->logBytesPerSector;
273 	for (i = 0; i < raidPtr->numSpare; i++) {
274 		if (disks[i].blockSize != bs) {
275 			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 			ret = EINVAL;
277 			goto fail;
278 		}
279 		if (disks[i].numBlocks < raidPtr->sectorsPerDisk) {
280 			RF_ERRORMSG3("Spare disk %s (%d blocks) is too small to serve as a spare (need %ld blocks)\n",
281 				     disks[i].devname, disks[i].blockSize,
282 				     (long int) raidPtr->sectorsPerDisk);
283 			ret = EINVAL;
284 			goto fail;
285 		} else
286 			if (disks[i].numBlocks > raidPtr->sectorsPerDisk) {
287 				RF_ERRORMSG3("Warning: truncating spare disk %s to %ld blocks (from %ld)\n",
288 				    disks[i].devname,
289 				    (long int) raidPtr->sectorsPerDisk,
290 				    (long int) disks[i].numBlocks);
291 
292 				disks[i].numBlocks = raidPtr->sectorsPerDisk;
293 			}
294 	}
295 
296 	return (0);
297 
298 fail:
299 
300 	/* Release the hold on the main components.  We've failed to allocate
301 	 * a spare, and since we're failing, we need to free things..
302 
303 	 XXX failing to allocate a spare is *not* that big of a deal...
304 	 We *can* survive without it, if need be, esp. if we get hot
305 	 adding working.
306 
307 	 If we don't fail out here, then we need a way to remove this spare...
308 	 that should be easier to do here than if we are "live"...
309 
310 	 */
311 
312 	rf_UnconfigureVnodes( raidPtr );
313 
314 	return (ret);
315 }
316 
317 static int
318 rf_AllocDiskStructures(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr)
319 {
320 	int ret;
321 
322 	/* We allocate RF_MAXSPARE on the first row so that we
323 	   have room to do hot-swapping of spares */
324 	RF_MallocAndAdd(raidPtr->Disks, (raidPtr->numCol + RF_MAXSPARE) *
325 			sizeof(RF_RaidDisk_t), (RF_RaidDisk_t *),
326 			raidPtr->cleanupList);
327 	if (raidPtr->Disks == NULL) {
328 		ret = ENOMEM;
329 		goto fail;
330 	}
331 
332 	/* get space for device specific stuff.. */
333 	RF_MallocAndAdd(raidPtr->raid_cinfo,
334 			(raidPtr->numCol + RF_MAXSPARE) *
335 			sizeof(struct raidcinfo), (struct raidcinfo *),
336 			raidPtr->cleanupList);
337 
338 	if (raidPtr->raid_cinfo == NULL) {
339 		ret = ENOMEM;
340 		goto fail;
341 	}
342 
343 	return(0);
344 fail:
345 	rf_UnconfigureVnodes( raidPtr );
346 
347 	return(ret);
348 }
349 
350 
351 /* configure a single disk during auto-configuration at boot */
352 int
353 rf_AutoConfigureDisks(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr,
354 		      RF_AutoConfig_t *auto_config)
355 {
356 	RF_RaidDisk_t *disks;
357 	RF_RaidDisk_t *diskPtr;
358 	RF_RowCol_t c;
359 	RF_SectorCount_t min_numblks = (RF_SectorCount_t) 0x7FFFFFFFFFFFLL;
360 	int bs, ret;
361 	int numFailuresThisRow;
362 	RF_AutoConfig_t *ac;
363 	int parity_good;
364 	int mod_counter;
365 	int mod_counter_found;
366 
367 #if DEBUG
368 	printf("Starting autoconfiguration of RAID set...\n");
369 #endif
370 
371 	ret = rf_AllocDiskStructures(raidPtr, cfgPtr);
372 	if (ret)
373 		goto fail;
374 
375 	disks = raidPtr->Disks;
376 
377 	/* assume the parity will be fine.. */
378 	parity_good = RF_RAID_CLEAN;
379 
380 	/* Check for mod_counters that are too low */
381 	mod_counter_found = 0;
382 	mod_counter = 0;
383 	ac = auto_config;
384 	while(ac!=NULL) {
385 		if (mod_counter_found==0) {
386 			mod_counter = ac->clabel->mod_counter;
387 			mod_counter_found = 1;
388 		} else {
389 			if (ac->clabel->mod_counter > mod_counter) {
390 				mod_counter = ac->clabel->mod_counter;
391 			}
392 		}
393 		ac->flag = 0; /* clear the general purpose flag */
394 		ac = ac->next;
395 	}
396 
397 	bs = 0;
398 
399 	numFailuresThisRow = 0;
400 	for (c = 0; c < raidPtr->numCol; c++) {
401 		diskPtr = &disks[c];
402 
403 		/* find this row/col in the autoconfig */
404 #if DEBUG
405 		printf("Looking for %d in autoconfig\n",c);
406 #endif
407 		ac = auto_config;
408 		while(ac!=NULL) {
409 			if (ac->clabel==NULL) {
410 				/* big-time bad news. */
411 				goto fail;
412 			}
413 			if ((ac->clabel->column == c) &&
414 			    (ac->clabel->mod_counter == mod_counter)) {
415 				/* it's this one... */
416 				/* flag it as 'used', so we don't
417 				   free it later. */
418 				ac->flag = 1;
419 #if DEBUG
420 				printf("Found: %s at %d\n",
421 				       ac->devname,c);
422 #endif
423 
424 				break;
425 			}
426 			ac=ac->next;
427 		}
428 
429 		if (ac==NULL) {
430 			/* we didn't find an exact match with a
431 			   correct mod_counter above... can we find
432 			   one with an incorrect mod_counter to use
433 			   instead?  (this one, if we find it, will be
434 			   marked as failed once the set configures)
435 			*/
436 
437 			ac = auto_config;
438 			while(ac!=NULL) {
439 				if (ac->clabel==NULL) {
440 					/* big-time bad news. */
441 					goto fail;
442 				}
443 				if (ac->clabel->column == c) {
444 					/* it's this one...
445 					   flag it as 'used', so we
446 					   don't free it later. */
447 					ac->flag = 1;
448 #if DEBUG
449 					printf("Found(low mod_counter): %s at %d\n",
450 					       ac->devname,c);
451 #endif
452 
453 					break;
454 				}
455 				ac=ac->next;
456 			}
457 		}
458 
459 
460 
461 		if (ac!=NULL) {
462 			/* Found it.  Configure it.. */
463 			diskPtr->blockSize = ac->clabel->blockSize;
464 			diskPtr->numBlocks = ac->clabel->numBlocks;
465 			/* Note: rf_protectedSectors is already
466 			   factored into numBlocks here */
467 			raidPtr->raid_cinfo[c].ci_vp = ac->vp;
468 			raidPtr->raid_cinfo[c].ci_dev = ac->dev;
469 
470 			memcpy(&raidPtr->raid_cinfo[c].ci_label,
471 			    ac->clabel, sizeof(*ac->clabel));
472 			snprintf(diskPtr->devname, sizeof(diskPtr->devname),
473 			    "/dev/%s", ac->devname);
474 
475 			/* note the fact that this component was
476 			   autoconfigured.  You'll need this info
477 			   later.  Trust me :) */
478 			diskPtr->auto_configured = 1;
479 			diskPtr->dev = ac->dev;
480 
481 			/*
482 			 * we allow the user to specify that
483 			 * only a fraction of the disks should
484 			 * be used this is just for debug: it
485 			 * speeds up the parity scan
486 			 */
487 
488 			diskPtr->numBlocks = diskPtr->numBlocks *
489 				rf_sizePercentage / 100;
490 
491 			/* XXX these will get set multiple times,
492 			   but since we're autoconfiguring, they'd
493 			   better be always the same each time!
494 			   If not, this is the least of your worries */
495 
496 			bs = diskPtr->blockSize;
497 			min_numblks = diskPtr->numBlocks;
498 
499 			/* this gets done multiple times, but that's
500 			   fine -- the serial number will be the same
501 			   for all components, guaranteed */
502 			raidPtr->serial_number = ac->clabel->serial_number;
503 			/* check the last time the label was modified */
504 
505 			if (ac->clabel->mod_counter != mod_counter) {
506 				/* Even though we've filled in all of
507 				   the above, we don't trust this
508 				   component since it's modification
509 				   counter is not in sync with the
510 				   rest, and we really consider it to
511 				   be failed.  */
512 				disks[c].status = rf_ds_failed;
513 				numFailuresThisRow++;
514 			} else {
515 				if (ac->clabel->clean != RF_RAID_CLEAN) {
516 					parity_good = RF_RAID_DIRTY;
517 				}
518 			}
519 		} else {
520 			/* Didn't find it at all!!  Component must
521 			   really be dead */
522 			disks[c].status = rf_ds_failed;
523 			snprintf(disks[c].devname, sizeof(disks[c].devname),
524 			    "component%d", c);
525 			numFailuresThisRow++;
526 		}
527 	}
528 	/* XXX fix for n-fault tolerant */
529 	/* XXX this should probably check to see how many failures
530 	   we can handle for this configuration! */
531 	if (numFailuresThisRow > 0) {
532 		raidPtr->status = rf_rs_degraded;
533 		raidPtr->numFailures = numFailuresThisRow;
534 	}
535 
536 	/* close the device for the ones that didn't get used */
537 
538 	ac = auto_config;
539 	while(ac!=NULL) {
540 		if (ac->flag == 0) {
541 			vn_lock(ac->vp, LK_EXCLUSIVE | LK_RETRY);
542 			VOP_CLOSE(ac->vp, FREAD | FWRITE, NOCRED, 0);
543 			vput(ac->vp);
544 			ac->vp = NULL;
545 #if DEBUG
546 			printf("Released %s from auto-config set.\n",
547 			       ac->devname);
548 #endif
549 		}
550 		ac = ac->next;
551 	}
552 
553 	raidPtr->mod_counter = mod_counter;
554 
555 	/* note the state of the parity, if any */
556 	raidPtr->parity_good = parity_good;
557 	raidPtr->sectorsPerDisk = min_numblks;
558 	raidPtr->logBytesPerSector = ffs(bs) - 1;
559 	raidPtr->bytesPerSector = bs;
560 	raidPtr->sectorMask = bs - 1;
561 	return (0);
562 
563 fail:
564 
565 	rf_UnconfigureVnodes( raidPtr );
566 
567 	return (ret);
568 
569 }
570 
571 /* configure a single disk in the array */
572 int
573 rf_ConfigureDisk(RF_Raid_t *raidPtr, char *bf, RF_RaidDisk_t *diskPtr,
574 		 RF_RowCol_t col)
575 {
576 	char   *p;
577 	struct partinfo dpart;
578 	struct vnode *vp;
579 	struct vattr va;
580 	struct lwp *l;
581 	int     error;
582 
583 	p = rf_find_non_white(bf);
584 	if (p[strlen(p) - 1] == '\n') {
585 		/* strip off the newline */
586 		p[strlen(p) - 1] = '\0';
587 	}
588 	(void) strcpy(diskPtr->devname, p);
589 
590 	l = LIST_FIRST(&raidPtr->engine_thread->p_lwps);
591 
592 	/* Let's start by claiming the component is fine and well... */
593 	diskPtr->status = rf_ds_optimal;
594 
595 	raidPtr->raid_cinfo[col].ci_vp = NULL;
596 	raidPtr->raid_cinfo[col].ci_dev = 0;
597 
598 	if (!strcmp("absent", diskPtr->devname)) {
599 		printf("Ignoring missing component at column %d\n", col);
600 		sprintf(diskPtr->devname, "component%d", col);
601 		diskPtr->status = rf_ds_failed;
602 		return (0);
603 	}
604 
605 	error = raidlookup(diskPtr->devname, l, &vp);
606 	if (error) {
607 		printf("raidlookup 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 		} else {
612 			return (error);
613 		}
614 	}
615 	if (diskPtr->status == rf_ds_optimal) {
616 
617 		if ((error = VOP_GETATTR(vp, &va,
618 		    l->l_proc->p_ucred, l)) != 0) {
619 			return (error);
620 		}
621 		error = VOP_IOCTL(vp, DIOCGPART, &dpart,
622 				  FREAD, l->l_proc->p_ucred, l);
623 		if (error) {
624 			return (error);
625 		}
626 
627 		diskPtr->blockSize = dpart.disklab->d_secsize;
628 
629 		diskPtr->numBlocks = dpart.part->p_size - rf_protectedSectors;
630 		diskPtr->partitionSize = dpart.part->p_size;
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 modfication 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 = &raidPtr->raid_cinfo[c].ci_label;
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 = &raidPtr->raid_cinfo[c].ci_label;
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 = &raidPtr->raid_cinfo[c].ci_label;
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 = &raidPtr->raid_cinfo[c].ci_label;
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 %ld blockSize %d (%ld MB)\n", spare_number,
1004 			 disks[spare_number].devname,
1005 			 (long int) disks[spare_number].numBlocks,
1006 			 disks[spare_number].blockSize,
1007 			 (long int) disks[spare_number].numBlocks *
1008 			 disks[spare_number].blockSize / 1024 / 1024);
1009 	}
1010 
1011 
1012 	/* check sizes and block sizes on the spare disk */
1013 	bs = 1 << raidPtr->logBytesPerSector;
1014 	if (disks[spare_number].blockSize != bs) {
1015 		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);
1016 		rf_close_component(raidPtr, raidPtr->raid_cinfo[raidPtr->numCol+spare_number].ci_vp, 0);
1017 		ret = EINVAL;
1018 		goto fail;
1019 	}
1020 	if (disks[spare_number].numBlocks < raidPtr->sectorsPerDisk) {
1021 		RF_ERRORMSG3("Spare disk %s (%d blocks) is too small to serve as a spare (need %ld blocks)\n",
1022 			     disks[spare_number].devname,
1023 			     disks[spare_number].blockSize,
1024 			     (long int) raidPtr->sectorsPerDisk);
1025 		rf_close_component(raidPtr, raidPtr->raid_cinfo[raidPtr->numCol+spare_number].ci_vp, 0);
1026 		ret = EINVAL;
1027 		goto fail;
1028 	} else {
1029 		if (disks[spare_number].numBlocks >
1030 		    raidPtr->sectorsPerDisk) {
1031 			RF_ERRORMSG3("Warning: truncating spare disk %s to %ld blocks (from %ld)\n",
1032 			    disks[spare_number].devname,
1033 			    (long int) raidPtr->sectorsPerDisk,
1034 			    (long int) disks[spare_number].numBlocks);
1035 
1036 			disks[spare_number].numBlocks = raidPtr->sectorsPerDisk;
1037 		}
1038 	}
1039 
1040 	spareQueues = &raidPtr->Queues[raidPtr->numCol];
1041 	ret = rf_ConfigureDiskQueue( raidPtr, &spareQueues[spare_number],
1042 				 raidPtr->numCol + spare_number,
1043 				 raidPtr->qType,
1044 				 raidPtr->sectorsPerDisk,
1045 				 raidPtr->Disks[raidPtr->numCol +
1046 						  spare_number].dev,
1047 				 raidPtr->maxOutstanding,
1048 				 &raidPtr->shutdownList,
1049 				 raidPtr->cleanupList);
1050 
1051 	RF_LOCK_MUTEX(raidPtr->mutex);
1052 	raidPtr->numSpare++;
1053 	RF_UNLOCK_MUTEX(raidPtr->mutex);
1054 
1055 fail:
1056 	RF_LOCK_MUTEX(raidPtr->mutex);
1057 	raidPtr->adding_hot_spare=0;
1058 	wakeup(&(raidPtr->adding_hot_spare));
1059 	RF_UNLOCK_MUTEX(raidPtr->mutex);
1060 
1061 	return(ret);
1062 }
1063 
1064 int
1065 rf_remove_hot_spare(RF_Raid_t *raidPtr, RF_SingleComponent_t *sparePtr)
1066 {
1067 	int spare_number;
1068 
1069 
1070 	if (raidPtr->numSpare==0) {
1071 		printf("No spares to remove!\n");
1072 		return(EINVAL);
1073 	}
1074 
1075 	spare_number = sparePtr->column;
1076 
1077 	return(EINVAL); /* XXX not implemented yet */
1078 #if 0
1079 	if (spare_number < 0 || spare_number > raidPtr->numSpare) {
1080 		return(EINVAL);
1081 	}
1082 
1083 	/* verify that this spare isn't in use... */
1084 
1085 
1086 
1087 
1088 	/* it's gone.. */
1089 
1090 	raidPtr->numSpare--;
1091 
1092 	return(0);
1093 #endif
1094 }
1095 
1096 
1097 int
1098 rf_delete_component(RF_Raid_t *raidPtr, RF_SingleComponent_t *component)
1099 {
1100 	RF_RaidDisk_t *disks;
1101 
1102 	if ((component->column < 0) ||
1103 	    (component->column >= raidPtr->numCol)) {
1104 		return(EINVAL);
1105 	}
1106 
1107 	disks = &raidPtr->Disks[component->column];
1108 
1109 	/* 1. This component must be marked as 'failed' */
1110 
1111 	return(EINVAL); /* Not implemented yet. */
1112 }
1113 
1114 int
1115 rf_incorporate_hot_spare(RF_Raid_t *raidPtr, RF_SingleComponent_t *component)
1116 {
1117 
1118 	/* Issues here include how to 'move' this in if there is IO
1119 	   taking place (e.g. component queues and such) */
1120 
1121 	return(EINVAL); /* Not implemented yet. */
1122 }
1123