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