xref: /titanic_41/usr/src/cmd/format/analyze.c (revision ff3124eff995e6cd8ebd8c6543648e0670920034)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * This file contains routines to analyze the surface of a disk.
30  */
31 #include "global.h"
32 #include "analyze.h"
33 #include <stdlib.h>
34 #include <errno.h>
35 #include "misc.h"
36 #include "defect.h"
37 #include "label.h"
38 #include "param.h"
39 #include "checkdev.h"
40 
41 
42 /*
43  * These global variables control the surface analysis process.  They
44  * are set from a command in the defect menu.
45  */
46 int	scan_entire = 1;		/* scan whole disk flag */
47 diskaddr_t	scan_lower = 0;			/* lower bound */
48 diskaddr_t	scan_upper = 0;			/* upper bound */
49 int	scan_correct = 1;		/* correct errors flag */
50 int	scan_stop = 0;			/* stop after error flag */
51 int	scan_loop = 0;			/* loop forever flag */
52 int	scan_passes = 2;		/* number of passes */
53 int	scan_random = 0;		/* random patterns flag */
54 int	scan_size = 0;			/* sectors/scan operation */
55 int	scan_auto = 1;			/* scan after format flag */
56 int	scan_restore_defects = 1;	/* restore defect list after writing */
57 int	scan_restore_label = 1;		/* restore label after writing */
58 
59 /*
60  * These are summary variables to print out info after analysis.
61  * Values less than 0 imply they are invalid.
62  */
63 offset_t	scan_cur_block = -1;		/* current block */
64 int64_t		scan_blocks_fixed = -1;		/* # blocks repaired */
65 
66 /*
67  * This variable is used to tell whether the most recent surface
68  * analysis error was caused by a media defect or some other problem.
69  */
70 int	media_error;			/* error was caused by defect */
71 
72 int	disk_error;			/* disk errors during analysis */
73 
74 /*
75  * These are the data patterns used if random patterns are not chosen.
76  * They are designed to show pattern dependent errors.
77  */
78 static unsigned int	scan_patterns[] = {
79 	0xc6dec6de,
80 	0x6db6db6d,
81 	0x00000000,
82 	0xffffffff,
83 	0xaaaaaaaa,
84 };
85 #define	NPATTERNS	5		/* number of predefined patterns */
86 
87 /*
88  * These are the data patterns from the SunFed requirements document.
89  */
90 static unsigned int purge_patterns[] = {	/* patterns to be written */
91 	0xaaaaaaaa,		/* 10101010... */
92 	0x55555555,		/* 01010101...  == UUUU... */
93 	0xaaaaaaaa,		/* 10101010... */
94 	0xaaaaaaaa,		/* 10101010... */
95 };
96 
97 static unsigned int alpha_pattern =  0x40404040;   /* 10000000...  == @@@@... */
98 
99 /* Function prototypes */
100 #ifdef	__STDC__
101 
102 static int	scan_repair(diskaddr_t bn, int mode);
103 static int	analyze_blocks(int flags, diskaddr_t blkno, int blkcnt,
104 		unsigned data, int init, int driver_flags, int *xfercntp);
105 static int	handle_error_conditions(void);
106 static int	verify_blocks(int flags, diskaddr_t blkno, int blkcnt,
107 		unsigned data, int driver_flags, int *xfercntp);
108 #else	/* __STDC__ */
109 
110 static int	scan_repair();
111 static int	analyze_blocks();
112 static int	handle_error_conditions();
113 static int	verify_blocks();
114 
115 #endif	/* __STDC__ */
116 
117 /*
118  * This routine performs a surface analysis based upon the global
119  * parameters.  It is called from several commands in the defect menu,
120  * and from the format command in the command menu (if post-format
121  * analysis is enable).
122  */
123 int
124 do_scan(flags, mode)
125 	int	flags, mode;
126 {
127 	diskaddr_t	start, end, curnt;
128 	int	pass, size, needinit, data;
129 	int	status, founderr, i, j;
130 	int	error = 0;
131 	int	pattern = 0;
132 	int	xfercnt;
133 
134 	/*
135 	 * Check to be sure we aren't correcting without a defect list
136 	 * if the controller can correct the defect.
137 	 */
138 	if (scan_correct && !EMBEDDED_SCSI && (cur_ops->op_repair != NULL) &&
139 			(cur_list.list == NULL)) {
140 		err_print("Current Defect List must be initialized ");
141 		err_print("to do automatic repair.\n");
142 		return (-1);
143 	}
144 	/*
145 	 * Define the bounds of the scan.
146 	 */
147 	if (scan_entire) {
148 		start = 0;
149 	    if (cur_label == L_TYPE_SOLARIS) {
150 		if (cur_ctype->ctype_flags & CF_SCSI)
151 			end = datasects() - 1;
152 		else
153 			end = physsects() - 1;
154 	    } else if (cur_label == L_TYPE_EFI) {
155 		end = cur_parts->etoc->efi_last_lba;
156 	    }
157 	} else {
158 		start = scan_lower;
159 		end = scan_upper;
160 	}
161 	/*
162 	 * Make sure the user knows if we are scanning over a mounted
163 	 * partition.
164 	 */
165 	if ((flags & (SCAN_PATTERN | SCAN_WRITE)) &&
166 	    (checkmount(start, end))) {
167 		err_print("Cannot do analysis on a mounted partition.\n");
168 		return (-1);
169 	}
170 
171 	/*
172 	 * Make sure the user knows if we are scanning over a
173 	 * partition being used for swapping.
174 	 */
175 	if ((flags & (SCAN_PATTERN | SCAN_WRITE)) &&
176 	    (checkswap(start, end))) {
177 		err_print("Cannot do analysis on a partition \
178 		    which is currently being used for swapping.\n");
179 		return (-1);
180 	}
181 
182 	/*
183 	 * Check to see if any partitions used for svm, vxvm, ZFS zpool
184 	 * or live upgrade are on the disk.
185 	 */
186 	if ((flags & (SCAN_PATTERN | SCAN_WRITE)) &&
187 	    (checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
188 	    (diskaddr_t)-1, 0, 0))) {
189 		err_print("Cannot do analysis on a partition "
190 		    "while it in use as described above.\n");
191 		return (-1);
192 	}
193 
194 	/*
195 	 * If we are scanning destructively over certain sectors,
196 	 * we mark the defect list and/or label dirty so it will get rewritten.
197 	 */
198 	if (flags & (SCAN_PATTERN | SCAN_WRITE)) {
199 	    if (cur_label == L_TYPE_SOLARIS) {
200 		if (start < (daddr_t)totalsects() &&
201 				end >= (daddr_t)datasects()) {
202 			if (!EMBEDDED_SCSI) {
203 				cur_list.flags |= LIST_DIRTY;
204 			}
205 			if (cur_disk->disk_flags & DSK_LABEL)
206 				cur_flags |= LABEL_DIRTY;
207 		}
208 	    }
209 	    if (start == 0) {
210 		if (cur_disk->disk_flags & DSK_LABEL)
211 			cur_flags |= LABEL_DIRTY;
212 	    }
213 	}
214 	/*
215 	 * Initialize the summary info on sectors repaired.
216 	 */
217 	scan_blocks_fixed = 0;
218 	/*
219 	 * Loop through the passes of the scan. If required, loop forever.
220 	 */
221 	for (pass = 0; pass < scan_passes || scan_loop; pass++) {
222 		/*
223 		 * Determine the data pattern to use if pattern testing
224 		 * is to be done.
225 		 */
226 		if (flags & SCAN_PATTERN) {
227 			if (scan_random)
228 				data = (int)mrand48();
229 			else
230 				data = scan_patterns[pass % NPPATTERNS];
231 
232 			if (flags & SCAN_PURGE) {
233 				flags &= ~(SCAN_PURGE_READ_PASS
234 						| SCAN_PURGE_ALPHA_PASS);
235 				switch (pattern % (NPPATTERNS + 1)) {
236 				case NPPATTERNS:
237 					pattern = 0;
238 					if (!error) {
239 					    fmt_print(
240 "\nThe last %d passes were successful, running alpha pattern pass", NPPATTERNS);
241 					    flags |= SCAN_PURGE_ALPHA_PASS;
242 					    data = alpha_pattern;
243 					} else {
244 					    data = purge_patterns[pattern];
245 					    pattern++;
246 					};
247 					break;
248 				case READPATTERN:
249 					flags |=  SCAN_PURGE_READ_PASS;
250 				default:
251 					data = purge_patterns[pattern];
252 					pattern++;
253 					break;
254 				}
255 			}
256 			fmt_print("\n        pass %d", pass);
257 			fmt_print(" - pattern = 0x%x", data);
258 		} else
259 			fmt_print("\n        pass %d", pass);
260 
261 		fmt_print("\n");
262 		/*
263 		 * Mark the pattern buffer as corrupt, since it
264 		 * hasn't been initialized.
265 		 */
266 		needinit = 1;
267 		/*
268 		 * Print the first block number to the log file if
269 		 * logging is on so there is some record of what
270 		 * analysis was performed.
271 		 */
272 		if (log_file) {
273 			pr_dblock(log_print, start);
274 			log_print("\n");
275 		}
276 		/*
277 		 * Loop through this pass, each time analyzing an amount
278 		 * specified by the global parameters.
279 		 */
280 		xfercnt = 0;
281 		for (curnt = start; curnt <= end; curnt += size) {
282 			if ((end - curnt) < scan_size)
283 				size = end - curnt + 1;
284 			else
285 				size = scan_size;
286 			/*
287 			 * Print out where we are, so we don't look dead.
288 			 * Also store it in summary info for logging.
289 			 */
290 			scan_cur_block = curnt;
291 			nolog_print("   ");
292 			pr_dblock(nolog_print, curnt);
293 			nolog_print("  \015");
294 			(void) fflush(stdout);
295 			disk_error = 0;
296 			/*
297 			 * Do the actual analysis.
298 			 */
299 			status = analyze_blocks(flags, curnt, size,
300 			    (unsigned)data, needinit, (F_ALLERRS | F_SILENT),
301 			    &xfercnt);
302 			/*
303 			 * If there were no errors, the pattern buffer is
304 			 * still initialized, and we just loop to next chunk.
305 			 */
306 			needinit = 0;
307 			if (!status)
308 				continue;
309 			/*
310 			 * There was an error. Check if surface analysis
311 			 * can be continued.
312 			 */
313 			if (handle_error_conditions()) {
314 				scan_blocks_fixed = scan_cur_block = -1;
315 				return (-1);
316 			}
317 			/*
318 			 * There was an error. Mark the pattern buffer
319 			 * corrupt so it will get reinitialized.
320 			 */
321 			needinit = 1;
322 			/*
323 			 * If it was not a media error, ignore it.
324 			 */
325 			if (!media_error)
326 				continue;
327 			/*
328 			 * Loop 5 times through each sector of the chunk,
329 			 * analyzing them individually.
330 			 */
331 			nolog_print("   ");
332 			pr_dblock(nolog_print, curnt);
333 			nolog_print("  \015");
334 			(void) fflush(stdout);
335 			founderr = 0;
336 			for (j = 0; j < size * 5; j++) {
337 				i = j % size;
338 				disk_error = 0;
339 				status = analyze_blocks(flags, (curnt + i), 1,
340 				    (unsigned)data, needinit, F_ALLERRS, NULL);
341 				needinit = 0;
342 				if (!status)
343 					continue;
344 				/*
345 				 * There was an error. Check if surface analysis
346 				 * can be continued.
347 				 */
348 				if (handle_error_conditions()) {
349 					scan_blocks_fixed = scan_cur_block = -1;
350 					return (-1);
351 				}
352 				/*
353 				 * An error occurred.  Mark the buffer
354 				 * corrupt and see if it was media
355 				 * related.
356 				 */
357 				needinit = 1;
358 				if (!media_error)
359 					continue;
360 				/*
361 				 * We found a bad sector. Print out a message
362 				 * and fix it if required.
363 				 */
364 				founderr = 1;
365 				if (scan_correct && (flags != SCAN_VALID)) {
366 					if (scan_repair(curnt+i, mode)) {
367 						error = -1;
368 					}
369 				} else
370 					err_print("\n");
371 				/*
372 				 * Stop after the error if required.
373 				 */
374 				if (scan_stop)
375 					goto out;
376 			}
377 			/*
378 			 * Mark the pattern buffer corrupt to be safe.
379 			 */
380 			needinit = 1;
381 			/*
382 			 * We didn't find an individual sector that was bad.
383 			 * Print out a warning.
384 			 */
385 			if (!founderr) {
386 				err_print("Warning: unable to pinpoint ");
387 				err_print("defective block.\n");
388 			}
389 		}
390 		/*
391 		 * Print the end of each pass to the log file.
392 		 */
393 		enter_critical();
394 		if (log_file) {
395 			pr_dblock(log_print, scan_cur_block);
396 			log_print("\n");
397 		}
398 		scan_cur_block = -1;
399 		exit_critical();
400 		fmt_print("\n");
401 
402 		/*
403 		 * alternate the read and write for SCAN_VERIFY test
404 		 */
405 		if (flags & SCAN_VERIFY) {
406 			flags ^= SCAN_VERIFY_READ_PASS;
407 		}
408 	}
409 out:
410 	/*
411 	 * We got here either by giving up after an error or falling
412 	 * through after all passes were completed.
413 	 */
414 	fmt_print("\n");
415 	enter_critical();
416 	/*
417 	 * If the defect list is dirty, write it to disk,
418 	 * if scan_restore_defects (the default) is true.
419 	 */
420 	if (!EMBEDDED_SCSI && (cur_list.flags & LIST_DIRTY) &&
421 				(scan_restore_defects)) {
422 		cur_list.flags = 0;
423 		write_deflist(&cur_list);
424 		}
425 	/*
426 	 * If the label is dirty, write it to disk.
427 	 * if scan_restore_label (the default) is true.
428 	 */
429 	if ((cur_flags & LABEL_DIRTY) && (scan_restore_label)) {
430 		cur_flags &= ~LABEL_DIRTY;
431 		(void) write_label();
432 	}
433 	/*
434 	 * If we dropped down to here after an error, we need to write
435 	 * the final block number to the log file for record keeping.
436 	 */
437 	if (log_file && scan_cur_block >= 0) {
438 		pr_dblock(log_print, scan_cur_block);
439 		log_print("\n");
440 	}
441 	fmt_print("Total of %lld defective blocks repaired.\n",
442 		scan_blocks_fixed);
443 	/*
444 	 * Reinitialize the logging variables so they don't get used
445 	 * when they are not really valid.
446 	 */
447 	scan_blocks_fixed = scan_cur_block = -1;
448 	exit_critical();
449 	return (error);
450 }
451 
452 
453 /*
454  * This routine is called to repair a bad block discovered
455  * during a scan operation.  Return 0 for success, 1 for failure.
456  * (This has been extracted out of do_scan(), to simplify it.)
457  */
458 static int
459 scan_repair(bn, mode)
460 	diskaddr_t	bn;
461 	int	mode;
462 {
463 	int	status;
464 	int	result = 1;
465 	char	buf[SECSIZE];
466 	int	buf_is_good;
467 	int	i;
468 
469 	if (cur_ops->op_repair == NULL) {
470 		err_print("Warning: Controller does ");
471 		err_print("not support repairing.\n\n");
472 		return (result);
473 	}
474 
475 	enter_critical();
476 
477 	/*
478 	 * Determine if the error appears to be hard or soft.  We
479 	 * already assume there's an error.  If we can get any
480 	 * good data out of the sector, write that data back
481 	 * after the repair.
482 	 */
483 	buf_is_good = 0;
484 	for (i = 0; i < 5; i++) {
485 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn, 1,
486 				buf, F_SILENT, NULL);
487 		if (status == 0) {
488 			buf_is_good = 1;
489 			break;
490 		}
491 	}
492 
493 	fmt_print("Repairing %s error on %llu (",
494 				buf_is_good ? "soft" : "hard", bn);
495 	pr_dblock(fmt_print, bn);
496 	fmt_print(")...");
497 
498 	status = (*cur_ops->op_repair)(bn, mode);
499 	if (status) {
500 		/*
501 		 * If the repair failed, we note it and will return the
502 		 * failure. However, the analysis goes on.
503 		 */
504 		fmt_print("failed.\n\n");
505 	} else {
506 		/*
507 		 * The repair worked.  Write the good data we could
508 		 * recover from the failed block, if possible.
509 		 * If not, zero the block.  In doing so, try to
510 		 * determine if the new block appears ok.
511 		 */
512 		if (!buf_is_good) {
513 			bzero(buf, SECSIZE);
514 			fmt_print("Warning: Block %llu zero-filled.\n", bn);
515 		} else {
516 			fmt_print("ok.\n");
517 		}
518 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, bn,
519 					1, buf, (F_SILENT | F_ALLERRS), NULL);
520 		if (status == 0) {
521 			status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn,
522 					1, buf, (F_SILENT | F_ALLERRS), NULL);
523 		}
524 		if (status) {
525 			fmt_print("The new block also appears defective.\n");
526 		}
527 		fmt_print("\n");
528 		/*
529 		 * add the defect to the list and write the list out.
530 		 * Also, kill the working list so it will get resynced
531 		 * with the current list.
532 		 *
533 		 * For embedded scsi, we don't require a defect list.
534 		 * However, if we have one, add the defect if the
535 		 * list includes the grown list.  If not, kill it
536 		 * to force a resync if we need the list later.
537 		 */
538 		if (EMBEDDED_SCSI) {
539 			if (cur_list.list != NULL) {
540 				if (cur_list.flags & LIST_PGLIST) {
541 					add_ldef(bn, &cur_list);
542 				} else {
543 					kill_deflist(&cur_list);
544 				}
545 			}
546 		/*
547 		 * The next "if" statement reflects the fix for
548 		 * bug id 1026096 where format keeps adding the
549 		 * same defect to the defect list.
550 		 */
551 		} else if (cur_ctype->ctype_flags & CF_WLIST) {
552 			kill_deflist(&cur_list);
553 			(*cur_ops->op_ex_cur)(&cur_list);
554 			fmt_print("Current list updated\n");
555 		} else {
556 			add_ldef(bn, &cur_list);
557 			write_deflist(&cur_list);
558 		}
559 		kill_deflist(&work_list);
560 
561 		/* Log the repair.  */
562 		scan_blocks_fixed++;
563 
564 		/* return ok */
565 		result = 0;
566 	}
567 
568 	exit_critical();
569 
570 	return (result);
571 }
572 
573 
574 /*
575  * This routine analyzes a set of sectors on the disk.  It simply returns
576  * an error if a defect is found.  It is called by do_scan().
577  */
578 static int
579 analyze_blocks(flags, blkno, blkcnt, data, init, driver_flags, xfercntp)
580 	int	flags, driver_flags, blkcnt, init;
581 	register unsigned data;
582 	diskaddr_t	blkno;
583 	int	*xfercntp;
584 {
585 	int	corrupt = 0;
586 	register int	status, i, nints;
587 	register unsigned *ptr = (uint_t *)pattern_buf;
588 
589 	media_error = 0;
590 	if (flags & SCAN_VERIFY) {
591 		return (verify_blocks(flags, blkno, blkcnt, data,
592 		    driver_flags, xfercntp));
593 	}
594 
595 	/*
596 	 * Initialize the pattern buffer if necessary.
597 	 */
598 	nints = blkcnt * SECSIZE / sizeof (int);
599 	if ((flags & SCAN_PATTERN) && init) {
600 		for (i = 0; i < nints; i++)
601 			*((int *)((int *)pattern_buf + i)) = data;
602 	}
603 	/*
604 	 * Lock out interrupts so we can insure valid data will get
605 	 * restored. This is necessary because there are modes
606 	 * of scanning that corrupt the disk data then restore it at
607 	 * the end of the analysis.
608 	 */
609 	enter_critical();
610 	/*
611 	 * If the disk data is valid, read it into the data buffer.
612 	 */
613 	if (flags & SCAN_VALID) {
614 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
615 		    blkcnt, (caddr_t)cur_buf, driver_flags, xfercntp);
616 		if (status)
617 			goto bad;
618 	}
619 	/*
620 	 * If we are doing pattern testing, write and read the pattern
621 	 * from the pattern buffer.
622 	 */
623 	if (flags & SCAN_PATTERN) {
624 		/*
625 		 * If the disk data was valid, mark it corrupt so we know
626 		 * to restore it later.
627 		 */
628 		if (flags & SCAN_VALID)
629 			corrupt++;
630 		/*
631 		 * Only write if we're not on the read pass of SCAN_PURGE.
632 		 */
633 		if (!(flags & SCAN_PURGE_READ_PASS)) {
634 			status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
635 			    blkcnt, (caddr_t)pattern_buf, driver_flags,
636 			    xfercntp);
637 			if (status)
638 			    goto bad;
639 		}
640 		/*
641 		 * Only read if we are on the read pass of SCAN_PURGE, if we
642 		 * are purging.
643 		 */
644 		if ((!(flags & SCAN_PURGE)) || (flags & SCAN_PURGE_READ_PASS)) {
645 			status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
646 			    blkcnt, (caddr_t)pattern_buf, driver_flags,
647 			    xfercntp);
648 			if (status)
649 			    goto bad;
650 		}
651 	}
652 	/*
653 	 * If we are doing a data compare, make sure the pattern
654 	 * came back intact.
655 	 * Only compare if we are on the read pass of SCAN_PURGE, or
656 	 * we wrote random data instead of the expected data pattern.
657 	 */
658 	if ((flags & SCAN_COMPARE) || (flags & SCAN_PURGE_READ_PASS)) {
659 		for (i = nints, ptr = (uint_t *)pattern_buf; i; i--)
660 			if (*ptr++ != data) {
661 				err_print("Data miscompare error (expecting ");
662 				err_print("0x%x, got 0x%x) at ", data,
663 					*((int *)((int *)pattern_buf +
664 					(nints - i))));
665 				pr_dblock(err_print, blkno);
666 				err_print(", offset = 0x%x.\n",
667 					(nints - i) * sizeof (int));
668 				goto bad;
669 			}
670 	}
671 	/*
672 	 * If we are supposed to write data out, do so.
673 	 */
674 	if (flags & SCAN_WRITE) {
675 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
676 		    blkcnt, (caddr_t)cur_buf, driver_flags, xfercntp);
677 		if (status)
678 			goto bad;
679 	}
680 	exit_critical();
681 	/*
682 	 * No errors occurred, return ok.
683 	 */
684 	return (0);
685 bad:
686 	/*
687 	 * There was an error.  If the data was corrupted, we write it
688 	 * out from the data buffer to restore it.
689 	 */
690 	if (corrupt) {
691 		if ((*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
692 				blkcnt, (caddr_t)cur_buf, F_NORMAL, xfercntp))
693 		err_print("Warning: unable to restore original data.\n");
694 	}
695 	exit_critical();
696 	/*
697 	 * Return the error.
698 	 */
699 	return (-1);
700 }
701 
702 
703 /*
704  * This routine analyzes a set of sectors on the disk. It simply returns
705  * an error if a defect is found.  It is called by analyze_blocks().
706  * For simplicity, this is done as a separate function instead of
707  * making the analyze_block routine complex.
708  *
709  * This routine implements the 'verify' command.  It writes the disk
710  * by writing unique data for each block; after the write pass, it
711  * reads the data and verifies for correctness. Note that the entire
712  * disk (or the range of disk) is fully written first and then read.
713  * This should eliminate any caching effect on the drives.
714  */
715 static int
716 verify_blocks(int flags,
717 		diskaddr_t blkno,
718 		int blkcnt,
719 		unsigned data,
720 		int driver_flags,
721 		int *xfercntp)
722 {
723 	int		status, i, nints;
724 	unsigned	*ptr = (uint_t *)pattern_buf;
725 
726 	nints = SECSIZE / sizeof (int);
727 
728 	/*
729 	 * Initialize the pattern buffer if we are in write pass.
730 	 * Use the block number itself as data, each block has unique
731 	 * buffer data that way.
732 	 */
733 	if (!(flags & SCAN_VERIFY_READ_PASS)) {
734 		for (data = blkno; data < blkno + blkcnt; data++) {
735 			for (i = 0; i < nints; i++) {
736 				*ptr++ = data;
737 			}
738 		}
739 		ptr = (uint_t *)pattern_buf;
740 	}
741 
742 	/*
743 	 * Only write if we're not on the read pass of SCAN_VERIFY.
744 	 */
745 	if (!(flags & SCAN_VERIFY_READ_PASS)) {
746 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
747 		    blkcnt, (caddr_t)pattern_buf, driver_flags, xfercntp);
748 		if (status)
749 			goto bad;
750 	} else {
751 		/*
752 		 * Only read if we are on the read pass of SCAN_VERIFY
753 		 */
754 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
755 		    blkcnt, (caddr_t)pattern_buf, driver_flags, xfercntp);
756 		if (status)
757 			goto bad;
758 		/*
759 		 * compare and make sure the pattern came back intact.
760 		 */
761 		for (data = blkno; data < blkno + blkcnt; data++) {
762 		    for (i = 0; i < nints; i++) {
763 			if (*ptr++ != data) {
764 			    ptr--;
765 			    err_print("Data miscompare error (expecting "
766 				"0x%x, got 0x%x) at ", data, *ptr);
767 			    pr_dblock(err_print, blkno);
768 			    err_print(", offset = 0x%x.\n", (ptr -
769 				(uint_t *)pattern_buf) * sizeof (int));
770 			    goto bad;
771 			}
772 		    }
773 		}
774 	}
775 	/*
776 	 * No errors occurred, return ok.
777 	 */
778 	return (0);
779 bad:
780 	return (-1);
781 }
782 
783 
784 static int
785 handle_error_conditions()
786 {
787 
788 	/*
789 	 * Check if the errno is ENXIO.
790 	 */
791 	if (errno == ENXIO) {
792 		fmt_print("\n\nWarning:Cannot access drive, ");
793 		fmt_print("aborting surface analysis.\n");
794 		return (-1);
795 	}
796 	/*
797 	 * check for disk errors
798 	 */
799 	switch (disk_error) {
800 	case DISK_STAT_RESERVED:
801 	case DISK_STAT_UNAVAILABLE:
802 		fmt_print("\n\nWarning:Drive may be reserved ");
803 		fmt_print("or has been removed, ");
804 		fmt_print("aborting surface analysis.\n");
805 		return (-1);
806 	case DISK_STAT_NOTREADY:
807 		fmt_print("\n\nWarning: Drive not ready, ");
808 		fmt_print("aborting surface analysis.\n");
809 		return (-1);
810 	case DISK_STAT_DATA_PROTECT:
811 		fmt_print("\n\nWarning: Drive is write protected, ");
812 		fmt_print("aborting surface analysis.\n");
813 		return (-1);
814 	default:
815 		break;
816 	}
817 	return (0);
818 }
819