1 /*	$NetBSD: rf_disks.c,v 1.87 2014/10/18 08:33:28 snj 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.87 2014/10/18 08:33:28 snj 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 #include <miscfs/specfs/specdev.h> /* for v_rdev */
84 
85 static int rf_AllocDiskStructures(RF_Raid_t *, RF_Config_t *);
86 static void rf_print_label_status( RF_Raid_t *, int, char *,
87 				  RF_ComponentLabel_t *);
88 static int rf_check_label_vitals( RF_Raid_t *, int, int, char *,
89 				  RF_ComponentLabel_t *, int, int );
90 
91 #define DPRINTF6(a,b,c,d,e,f) if (rf_diskDebug) printf(a,b,c,d,e,f)
92 #define DPRINTF7(a,b,c,d,e,f,g) if (rf_diskDebug) printf(a,b,c,d,e,f,g)
93 
94 /**************************************************************************
95  *
96  * initialize the disks comprising the array
97  *
98  * We want the spare disks to have regular row,col numbers so that we can
99  * easily substitue a spare for a failed disk.  But, the driver code assumes
100  * throughout that the array contains numRow by numCol _non-spare_ disks, so
101  * it's not clear how to fit in the spares.  This is an unfortunate holdover
102  * from raidSim.  The quick and dirty fix is to make row zero bigger than the
103  * rest, and put all the spares in it.  This probably needs to get changed
104  * eventually.
105  *
106  **************************************************************************/
107 
108 int
rf_ConfigureDisks(RF_ShutdownList_t ** listp,RF_Raid_t * raidPtr,RF_Config_t * cfgPtr)109 rf_ConfigureDisks(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
110 		  RF_Config_t *cfgPtr)
111 {
112 	RF_RaidDisk_t *disks;
113 	RF_SectorCount_t min_numblks = (RF_SectorCount_t) 0x7FFFFFFFFFFFLL;
114 	RF_RowCol_t c;
115 	int bs, ret;
116 	unsigned i, count, foundone = 0, numFailuresThisRow;
117 	int force;
118 
119 	force = cfgPtr->force;
120 
121 	ret = rf_AllocDiskStructures(raidPtr, cfgPtr);
122 	if (ret)
123 		goto fail;
124 
125 	disks = raidPtr->Disks;
126 
127 	numFailuresThisRow = 0;
128 	for (c = 0; c < raidPtr->numCol; c++) {
129 		ret = rf_ConfigureDisk(raidPtr,
130 				       &cfgPtr->devnames[0][c][0],
131 				       &disks[c], c);
132 
133 		if (ret)
134 			goto fail;
135 
136 		if (disks[c].status == rf_ds_optimal) {
137 			ret = raidfetch_component_label(raidPtr, c);
138 			if (ret)
139 				goto fail;
140 
141 			/* mark it as failed if the label looks bogus... */
142 			if (!rf_reasonable_label(&raidPtr->raid_cinfo[c].ci_label,0) && !force) {
143 				disks[c].status = rf_ds_failed;
144 			}
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 %" PRIu64 " blockSize %d (%ld MB)\n",
153 				 c, disks[c].devname,
154 				 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
rf_ConfigureSpareDisks(RF_ShutdownList_t ** listp,RF_Raid_t * raidPtr,RF_Config_t * cfgPtr)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 %" PRIu64 " blockSize %d (%ld MB)\n", i,
263 			    disks[i].devname,
264 			    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 %" PRIu64 " blocks)\n",
281 				     disks[i].devname, disks[i].blockSize,
282 				     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 %" PRIu64 " blocks (from %" PRIu64 ")\n",
288 				    disks[i].devname,
289 				    raidPtr->sectorsPerDisk,
290 				    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
rf_AllocDiskStructures(RF_Raid_t * raidPtr,RF_Config_t * cfgPtr)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
rf_AutoConfigureDisks(RF_Raid_t * raidPtr,RF_Config_t * cfgPtr,RF_AutoConfig_t * auto_config)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 =
465 			    rf_component_label_numblocks(ac->clabel);
466 			/* Note: rf_protectedSectors is already
467 			   factored into numBlocks here */
468 			raidPtr->raid_cinfo[c].ci_vp = ac->vp;
469 			raidPtr->raid_cinfo[c].ci_dev = ac->dev;
470 
471 			memcpy(raidget_component_label(raidPtr, c),
472 			    ac->clabel, sizeof(*ac->clabel));
473 			snprintf(diskPtr->devname, sizeof(diskPtr->devname),
474 			    "/dev/%s", ac->devname);
475 
476 			/* note the fact that this component was
477 			   autoconfigured.  You'll need this info
478 			   later.  Trust me :) */
479 			diskPtr->auto_configured = 1;
480 			diskPtr->dev = ac->dev;
481 
482 			/*
483 			 * we allow the user to specify that
484 			 * only a fraction of the disks should
485 			 * be used this is just for debug: it
486 			 * speeds up the parity scan
487 			 */
488 
489 			diskPtr->numBlocks = diskPtr->numBlocks *
490 				rf_sizePercentage / 100;
491 
492 			/* XXX these will get set multiple times,
493 			   but since we're autoconfiguring, they'd
494 			   better be always the same each time!
495 			   If not, this is the least of your worries */
496 
497 			bs = diskPtr->blockSize;
498 			min_numblks = diskPtr->numBlocks;
499 
500 			/* this gets done multiple times, but that's
501 			   fine -- the serial number will be the same
502 			   for all components, guaranteed */
503 			raidPtr->serial_number = ac->clabel->serial_number;
504 			/* check the last time the label was modified */
505 
506 			if (ac->clabel->mod_counter != mod_counter) {
507 				/* Even though we've filled in all of
508 				   the above, we don't trust this
509 				   component since its modification
510 				   counter is not in sync with the
511 				   rest, and we really consider it to
512 				   be failed.  */
513 				disks[c].status = rf_ds_failed;
514 				numFailuresThisRow++;
515 			} else {
516 				if (ac->clabel->clean != RF_RAID_CLEAN) {
517 					parity_good = RF_RAID_DIRTY;
518 				}
519 			}
520 		} else {
521 			/* Didn't find it at all!!  Component must
522 			   really be dead */
523 			disks[c].status = rf_ds_failed;
524 			snprintf(disks[c].devname, sizeof(disks[c].devname),
525 			    "component%d", c);
526 			numFailuresThisRow++;
527 		}
528 	}
529 	/* XXX fix for n-fault tolerant */
530 	/* XXX this should probably check to see how many failures
531 	   we can handle for this configuration! */
532 	if (numFailuresThisRow > 0) {
533 		raidPtr->status = rf_rs_degraded;
534 		raidPtr->numFailures = numFailuresThisRow;
535 	}
536 
537 	/* close the device for the ones that didn't get used */
538 
539 	ac = auto_config;
540 	while(ac!=NULL) {
541 		if (ac->flag == 0) {
542 			vn_lock(ac->vp, LK_EXCLUSIVE | LK_RETRY);
543 			VOP_CLOSE(ac->vp, FREAD | FWRITE, NOCRED);
544 			vput(ac->vp);
545 			ac->vp = NULL;
546 #if DEBUG
547 			printf("Released %s from auto-config set.\n",
548 			       ac->devname);
549 #endif
550 		}
551 		ac = ac->next;
552 	}
553 
554 	raidPtr->mod_counter = mod_counter;
555 
556 	/* note the state of the parity, if any */
557 	raidPtr->parity_good = parity_good;
558 	raidPtr->sectorsPerDisk = min_numblks;
559 	raidPtr->logBytesPerSector = ffs(bs) - 1;
560 	raidPtr->bytesPerSector = bs;
561 	raidPtr->sectorMask = bs - 1;
562 	return (0);
563 
564 fail:
565 
566 	rf_UnconfigureVnodes( raidPtr );
567 
568 	return (ret);
569 
570 }
571 
572 /* configure a single disk in the array */
573 int
rf_ConfigureDisk(RF_Raid_t * raidPtr,char * bf,RF_RaidDisk_t * diskPtr,RF_RowCol_t col)574 rf_ConfigureDisk(RF_Raid_t *raidPtr, char *bf, RF_RaidDisk_t *diskPtr,
575 		 RF_RowCol_t col)
576 {
577 	char   *p;
578 	struct pathbuf *pb;
579 	struct vnode *vp;
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 		raidPtr->raid_cinfo[col].ci_vp = vp;
635 		raidPtr->raid_cinfo[col].ci_dev = vp->v_rdev;
636 
637 		/* This component was not automatically configured */
638 		diskPtr->auto_configured = 0;
639 		diskPtr->dev = vp->v_rdev;
640 
641 		/* we allow the user to specify that only a fraction of the
642 		 * disks should be used this is just for debug:  it speeds up
643 		 * the parity scan */
644 		diskPtr->numBlocks = diskPtr->numBlocks *
645 			rf_sizePercentage / 100;
646 	}
647 	return (0);
648 }
649 
650 static void
rf_print_label_status(RF_Raid_t * raidPtr,int column,char * dev_name,RF_ComponentLabel_t * ci_label)651 rf_print_label_status(RF_Raid_t *raidPtr, int column, char *dev_name,
652 		      RF_ComponentLabel_t *ci_label)
653 {
654 
655 	printf("raid%d: Component %s being configured at col: %d\n",
656 	       raidPtr->raidid, dev_name, column );
657 	printf("         Column: %d Num Columns: %d\n",
658 	       ci_label->column,
659 	       ci_label->num_columns);
660 	printf("         Version: %d Serial Number: %d Mod Counter: %d\n",
661 	       ci_label->version, ci_label->serial_number,
662 	       ci_label->mod_counter);
663 	printf("         Clean: %s Status: %d\n",
664 	       ci_label->clean ? "Yes" : "No", ci_label->status );
665 }
666 
rf_check_label_vitals(RF_Raid_t * raidPtr,int row,int column,char * dev_name,RF_ComponentLabel_t * ci_label,int serial_number,int mod_counter)667 static int rf_check_label_vitals(RF_Raid_t *raidPtr, int row, int column,
668 				 char *dev_name, RF_ComponentLabel_t *ci_label,
669 				 int serial_number, int mod_counter)
670 {
671 	int fatal_error = 0;
672 
673 	if (serial_number != ci_label->serial_number) {
674 		printf("%s has a different serial number: %d %d\n",
675 		       dev_name, serial_number, ci_label->serial_number);
676 		fatal_error = 1;
677 	}
678 	if (mod_counter != ci_label->mod_counter) {
679 		printf("%s has a different modification count: %d %d\n",
680 		       dev_name, mod_counter, ci_label->mod_counter);
681 	}
682 
683 	if (row != ci_label->row) {
684 		printf("Row out of alignment for: %s\n", dev_name);
685 		fatal_error = 1;
686 	}
687 	if (column != ci_label->column) {
688 		printf("Column out of alignment for: %s\n", dev_name);
689 		fatal_error = 1;
690 	}
691 	if (raidPtr->numCol != ci_label->num_columns) {
692 		printf("Number of columns do not match for: %s\n", dev_name);
693 		fatal_error = 1;
694 	}
695 	if (ci_label->clean == 0) {
696 		/* it's not clean, but that's not fatal */
697 		printf("%s is not clean!\n", dev_name);
698 	}
699 	return(fatal_error);
700 }
701 
702 
703 /*
704 
705    rf_CheckLabels() - check all the component labels for consistency.
706    Return an error if there is anything major amiss.
707 
708  */
709 
710 int
rf_CheckLabels(RF_Raid_t * raidPtr,RF_Config_t * cfgPtr)711 rf_CheckLabels(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr)
712 {
713 	int c;
714 	char *dev_name;
715 	RF_ComponentLabel_t *ci_label;
716 	int serial_number = 0;
717 	int mod_number = 0;
718 	int fatal_error = 0;
719 	int mod_values[4];
720 	int mod_count[4];
721 	int ser_values[4];
722 	int ser_count[4];
723 	int num_ser;
724 	int num_mod;
725 	int i;
726 	int found;
727 	int hosed_column;
728 	int too_fatal;
729 	int parity_good;
730 	int force;
731 
732 	hosed_column = -1;
733 	too_fatal = 0;
734 	force = cfgPtr->force;
735 
736 	/*
737 	   We're going to try to be a little intelligent here.  If one
738 	   component's label is bogus, and we can identify that it's the
739 	   *only* one that's gone, we'll mark it as "failed" and allow
740 	   the configuration to proceed.  This will be the *only* case
741 	   that we'll proceed if there would be (otherwise) fatal errors.
742 
743 	   Basically we simply keep a count of how many components had
744 	   what serial number.  If all but one agree, we simply mark
745 	   the disagreeing component as being failed, and allow
746 	   things to come up "normally".
747 
748 	   We do this first for serial numbers, and then for "mod_counter".
749 
750 	 */
751 
752 	num_ser = 0;
753 	num_mod = 0;
754 
755 	ser_values[0] = ser_values[1] = ser_values[2] = ser_values[3] = 0;
756 	ser_count[0] = ser_count[1] = ser_count[2] = ser_count[3] = 0;
757 	mod_values[0] = mod_values[1] = mod_values[2] = mod_values[3] = 0;
758 	mod_count[0] = mod_count[1] = mod_count[2] = mod_count[3] = 0;
759 
760 	for (c = 0; c < raidPtr->numCol; c++) {
761 		if (raidPtr->Disks[c].status != rf_ds_optimal)
762 			continue;
763 		ci_label = raidget_component_label(raidPtr, c);
764 		found=0;
765 		for(i=0;i<num_ser;i++) {
766 			if (ser_values[i] == ci_label->serial_number) {
767 				ser_count[i]++;
768 				found=1;
769 				break;
770 			}
771 		}
772 		if (!found) {
773 			ser_values[num_ser] = ci_label->serial_number;
774 			ser_count[num_ser] = 1;
775 			num_ser++;
776 			if (num_ser>2) {
777 				fatal_error = 1;
778 				break;
779 			}
780 		}
781 		found=0;
782 		for(i=0;i<num_mod;i++) {
783 			if (mod_values[i] == ci_label->mod_counter) {
784 				mod_count[i]++;
785 				found=1;
786 				break;
787 			}
788 		}
789 		if (!found) {
790 			mod_values[num_mod] = ci_label->mod_counter;
791 			mod_count[num_mod] = 1;
792 			num_mod++;
793 			if (num_mod>2) {
794 				fatal_error = 1;
795 				break;
796 			}
797 		}
798 	}
799 #if DEBUG
800 	printf("raid%d: Summary of serial numbers:\n", raidPtr->raidid);
801 	for(i=0;i<num_ser;i++) {
802 		printf("%d %d\n", ser_values[i], ser_count[i]);
803 	}
804 	printf("raid%d: Summary of mod counters:\n", raidPtr->raidid);
805 	for(i=0;i<num_mod;i++) {
806 		printf("%d %d\n", mod_values[i], mod_count[i]);
807 	}
808 #endif
809 	serial_number = ser_values[0];
810 	if (num_ser == 2) {
811 		if ((ser_count[0] == 1) || (ser_count[1] == 1)) {
812 			/* Locate the maverick component */
813 			if (ser_count[1] > ser_count[0]) {
814 				serial_number = ser_values[1];
815 			}
816 
817 			for (c = 0; c < raidPtr->numCol; c++) {
818 				if (raidPtr->Disks[c].status != rf_ds_optimal)
819 					continue;
820 				ci_label = raidget_component_label(raidPtr, c);
821 				if (serial_number != ci_label->serial_number) {
822 					hosed_column = c;
823 					break;
824 				}
825 			}
826 			printf("Hosed component: %s\n",
827 			       &cfgPtr->devnames[0][hosed_column][0]);
828 			if (!force) {
829 				/* we'll fail this component, as if there are
830 				   other major errors, we arn't forcing things
831 				   and we'll abort the config anyways */
832 				raidPtr->Disks[hosed_column].status
833 					= rf_ds_failed;
834 				raidPtr->numFailures++;
835 				raidPtr->status = rf_rs_degraded;
836 			}
837 		} else {
838 			too_fatal = 1;
839 		}
840 		if (cfgPtr->parityConfig == '0') {
841 			/* We've identified two different serial numbers.
842 			   RAID 0 can't cope with that, so we'll punt */
843 			too_fatal = 1;
844 		}
845 
846 	}
847 
848 	/* record the serial number for later.  If we bail later, setting
849 	   this doesn't matter, otherwise we've got the best guess at the
850 	   correct serial number */
851 	raidPtr->serial_number = serial_number;
852 
853 	mod_number = mod_values[0];
854 	if (num_mod == 2) {
855 		if ((mod_count[0] == 1) || (mod_count[1] == 1)) {
856 			/* Locate the maverick component */
857 			if (mod_count[1] > mod_count[0]) {
858 				mod_number = mod_values[1];
859 			} else if (mod_count[1] < mod_count[0]) {
860 				mod_number = mod_values[0];
861 			} else {
862 				/* counts of different modification values
863 				   are the same.   Assume greater value is
864 				   the correct one, all other things
865 				   considered */
866 				if (mod_values[0] > mod_values[1]) {
867 					mod_number = mod_values[0];
868 				} else {
869 					mod_number = mod_values[1];
870 				}
871 
872 			}
873 
874 			for (c = 0; c < raidPtr->numCol; c++) {
875 				if (raidPtr->Disks[c].status != rf_ds_optimal)
876 					continue;
877 
878 				ci_label = raidget_component_label(raidPtr, c);
879 				if (mod_number != ci_label->mod_counter) {
880 					if (hosed_column == c) {
881 						/* same one.  Can
882 						   deal with it.  */
883 					} else {
884 						hosed_column = c;
885 						if (num_ser != 1) {
886 							too_fatal = 1;
887 							break;
888 						}
889 					}
890 				}
891 			}
892 			printf("Hosed component: %s\n",
893 			       &cfgPtr->devnames[0][hosed_column][0]);
894 			if (!force) {
895 				/* we'll fail this component, as if there are
896 				   other major errors, we arn't forcing things
897 				   and we'll abort the config anyways */
898 				if (raidPtr->Disks[hosed_column].status != rf_ds_failed) {
899 					raidPtr->Disks[hosed_column].status
900 						= rf_ds_failed;
901 					raidPtr->numFailures++;
902 					raidPtr->status = rf_rs_degraded;
903 				}
904 			}
905 		} else {
906 			too_fatal = 1;
907 		}
908 		if (cfgPtr->parityConfig == '0') {
909 			/* We've identified two different mod counters.
910 			   RAID 0 can't cope with that, so we'll punt */
911 			too_fatal = 1;
912 		}
913 	}
914 
915 	raidPtr->mod_counter = mod_number;
916 
917 	if (too_fatal) {
918 		/* we've had both a serial number mismatch, and a mod_counter
919 		   mismatch -- and they involved two different components!!
920 		   Bail -- make things fail so that the user must force
921 		   the issue... */
922 		hosed_column = -1;
923 		fatal_error = 1;
924 	}
925 
926 	if (num_ser > 2) {
927 		printf("raid%d: Too many different serial numbers!\n",
928 		       raidPtr->raidid);
929 		fatal_error = 1;
930 	}
931 
932 	if (num_mod > 2) {
933 		printf("raid%d: Too many different mod counters!\n",
934 		       raidPtr->raidid);
935 		fatal_error = 1;
936 	}
937 
938         for (c = 0; c < raidPtr->numCol; c++) {
939 		if (raidPtr->Disks[c].status != rf_ds_optimal) {
940 			hosed_column = c;
941 			break;
942 		}
943 	}
944 
945 	/* we start by assuming the parity will be good, and flee from
946 	   that notion at the slightest sign of trouble */
947 
948 	parity_good = RF_RAID_CLEAN;
949 
950 	for (c = 0; c < raidPtr->numCol; c++) {
951 		dev_name = &cfgPtr->devnames[0][c][0];
952 		ci_label = raidget_component_label(raidPtr, c);
953 
954 		if (c == hosed_column) {
955 			printf("raid%d: Ignoring %s\n",
956 			       raidPtr->raidid, dev_name);
957 		} else {
958 			rf_print_label_status( raidPtr, c, dev_name, ci_label);
959 			if (rf_check_label_vitals( raidPtr, 0, c,
960 						   dev_name, ci_label,
961 						   serial_number,
962 						   mod_number )) {
963 				fatal_error = 1;
964 			}
965 			if (ci_label->clean != RF_RAID_CLEAN) {
966 				parity_good = RF_RAID_DIRTY;
967 			}
968 		}
969 	}
970 
971 	if (fatal_error) {
972 		parity_good = RF_RAID_DIRTY;
973 	}
974 
975 	/* we note the state of the parity */
976 	raidPtr->parity_good = parity_good;
977 
978 	return(fatal_error);
979 }
980 
981 int
rf_add_hot_spare(RF_Raid_t * raidPtr,RF_SingleComponent_t * sparePtr)982 rf_add_hot_spare(RF_Raid_t *raidPtr, RF_SingleComponent_t *sparePtr)
983 {
984 	RF_RaidDisk_t *disks;
985 	RF_DiskQueue_t *spareQueues;
986 	int ret;
987 	unsigned int bs;
988 	int spare_number;
989 
990 	ret=0;
991 
992 	if (raidPtr->numSpare >= RF_MAXSPARE) {
993 		RF_ERRORMSG1("Too many spares: %d\n", raidPtr->numSpare);
994 		return(EINVAL);
995 	}
996 
997 	rf_lock_mutex2(raidPtr->mutex);
998 	while (raidPtr->adding_hot_spare == 1) {
999 		rf_wait_cond2(raidPtr->adding_hot_spare_cv, raidPtr->mutex);
1000 	}
1001 	raidPtr->adding_hot_spare = 1;
1002 	rf_unlock_mutex2(raidPtr->mutex);
1003 
1004 	/* the beginning of the spares... */
1005 	disks = &raidPtr->Disks[raidPtr->numCol];
1006 
1007 	spare_number = raidPtr->numSpare;
1008 
1009 	ret = rf_ConfigureDisk(raidPtr, sparePtr->component_name,
1010 			       &disks[spare_number],
1011 			       raidPtr->numCol + spare_number);
1012 
1013 	if (ret)
1014 		goto fail;
1015 	if (disks[spare_number].status != rf_ds_optimal) {
1016 		RF_ERRORMSG1("Warning: spare disk %s failed TUR\n",
1017 			     sparePtr->component_name);
1018 		rf_close_component(raidPtr, raidPtr->raid_cinfo[raidPtr->numCol+spare_number].ci_vp, 0);
1019 		ret=EINVAL;
1020 		goto fail;
1021 	} else {
1022 		disks[spare_number].status = rf_ds_spare;
1023 		DPRINTF6("Spare Disk %d: dev %s numBlocks %" PRIu64 " blockSize %d (%ld MB)\n",
1024 			 spare_number,
1025 			 disks[spare_number].devname,
1026 			 disks[spare_number].numBlocks,
1027 			 disks[spare_number].blockSize,
1028 			 (long int) disks[spare_number].numBlocks *
1029 			 disks[spare_number].blockSize / 1024 / 1024);
1030 	}
1031 
1032 
1033 	/* check sizes and block sizes on the spare disk */
1034 	bs = 1 << raidPtr->logBytesPerSector;
1035 	if (disks[spare_number].blockSize != bs) {
1036 		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);
1037 		rf_close_component(raidPtr, raidPtr->raid_cinfo[raidPtr->numCol+spare_number].ci_vp, 0);
1038 		ret = EINVAL;
1039 		goto fail;
1040 	}
1041 	if (disks[spare_number].numBlocks < raidPtr->sectorsPerDisk) {
1042 		RF_ERRORMSG3("Spare disk %s (%d blocks) is too small to serve as a spare (need %" PRIu64 " blocks)\n",
1043 			     disks[spare_number].devname,
1044 			     disks[spare_number].blockSize,
1045 			     raidPtr->sectorsPerDisk);
1046 		rf_close_component(raidPtr, raidPtr->raid_cinfo[raidPtr->numCol+spare_number].ci_vp, 0);
1047 		ret = EINVAL;
1048 		goto fail;
1049 	} else {
1050 		if (disks[spare_number].numBlocks >
1051 		    raidPtr->sectorsPerDisk) {
1052 			RF_ERRORMSG3("Warning: truncating spare disk %s to %" PRIu64 " blocks (from %" PRIu64 ")\n",
1053 			    disks[spare_number].devname,
1054 			    raidPtr->sectorsPerDisk,
1055 			    disks[spare_number].numBlocks);
1056 
1057 			disks[spare_number].numBlocks = raidPtr->sectorsPerDisk;
1058 		}
1059 	}
1060 
1061 	spareQueues = &raidPtr->Queues[raidPtr->numCol];
1062 	ret = rf_ConfigureDiskQueue( raidPtr, &spareQueues[spare_number],
1063 				 raidPtr->numCol + spare_number,
1064 				 raidPtr->qType,
1065 				 raidPtr->sectorsPerDisk,
1066 				 raidPtr->Disks[raidPtr->numCol +
1067 						  spare_number].dev,
1068 				 raidPtr->maxOutstanding,
1069 				 &raidPtr->shutdownList,
1070 				 raidPtr->cleanupList);
1071 
1072 	rf_lock_mutex2(raidPtr->mutex);
1073 	raidPtr->numSpare++;
1074 	rf_unlock_mutex2(raidPtr->mutex);
1075 
1076 fail:
1077 	rf_lock_mutex2(raidPtr->mutex);
1078 	raidPtr->adding_hot_spare = 0;
1079 	rf_signal_cond2(raidPtr->adding_hot_spare_cv);
1080 	rf_unlock_mutex2(raidPtr->mutex);
1081 
1082 	return(ret);
1083 }
1084 
1085 int
rf_remove_hot_spare(RF_Raid_t * raidPtr,RF_SingleComponent_t * sparePtr)1086 rf_remove_hot_spare(RF_Raid_t *raidPtr, RF_SingleComponent_t *sparePtr)
1087 {
1088 #if 0
1089 	int spare_number;
1090 #endif
1091 
1092 	if (raidPtr->numSpare==0) {
1093 		printf("No spares to remove!\n");
1094 		return(EINVAL);
1095 	}
1096 
1097 	return(EINVAL); /* XXX not implemented yet */
1098 #if 0
1099 	spare_number = sparePtr->column;
1100 
1101 	if (spare_number < 0 || spare_number > raidPtr->numSpare) {
1102 		return(EINVAL);
1103 	}
1104 
1105 	/* verify that this spare isn't in use... */
1106 
1107 
1108 
1109 
1110 	/* it's gone.. */
1111 
1112 	raidPtr->numSpare--;
1113 
1114 	return(0);
1115 #endif
1116 }
1117 
1118 
1119 int
rf_delete_component(RF_Raid_t * raidPtr,RF_SingleComponent_t * component)1120 rf_delete_component(RF_Raid_t *raidPtr, RF_SingleComponent_t *component)
1121 {
1122 #if 0
1123 	RF_RaidDisk_t *disks;
1124 #endif
1125 
1126 	if ((component->column < 0) ||
1127 	    (component->column >= raidPtr->numCol)) {
1128 		return(EINVAL);
1129 	}
1130 
1131 #if 0
1132 	disks = &raidPtr->Disks[component->column];
1133 #endif
1134 
1135 	/* 1. This component must be marked as 'failed' */
1136 
1137 	return(EINVAL); /* Not implemented yet. */
1138 }
1139 
1140 int
rf_incorporate_hot_spare(RF_Raid_t * raidPtr,RF_SingleComponent_t * component)1141 rf_incorporate_hot_spare(RF_Raid_t *raidPtr,
1142     RF_SingleComponent_t *component)
1143 {
1144 
1145 	/* Issues here include how to 'move' this in if there is IO
1146 	   taking place (e.g. component queues and such) */
1147 
1148 	return(EINVAL); /* Not implemented yet. */
1149 }
1150