xref: /illumos-gate/usr/src/cmd/format/analyze.c (revision 915894ef19890baaed00080f85f6b69e225cda98)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * This file contains routines to analyze the surface of a disk.
28  */
29 #include "global.h"
30 #include "analyze.h"
31 #include <stdlib.h>
32 #include <errno.h>
33 #include "misc.h"
34 #include "defect.h"
35 #include "label.h"
36 #include "param.h"
37 #include "checkdev.h"
38 
39 
40 /*
41  * These global variables control the surface analysis process.  They
42  * are set from a command in the defect menu.
43  */
44 int	scan_entire = 1;		/* scan whole disk flag */
45 diskaddr_t	scan_lower = 0;			/* lower bound */
46 diskaddr_t	scan_upper = 0;			/* upper bound */
47 int	scan_correct = 1;		/* correct errors flag */
48 int	scan_stop = 0;			/* stop after error flag */
49 int	scan_loop = 0;			/* loop forever flag */
50 int	scan_passes = 2;		/* number of passes */
51 int	scan_random = 0;		/* random patterns flag */
52 uint_t	scan_size = 0;			/* sectors/scan operation */
53 int	scan_auto = 1;			/* scan after format flag */
54 int	scan_restore_defects = 1;	/* restore defect list after writing */
55 int	scan_restore_label = 1;		/* restore label after writing */
56 
57 /*
58  * These are summary variables to print out info after analysis.
59  * Values less than 0 imply they are invalid.
60  */
61 offset_t	scan_cur_block = -1;		/* current block */
62 int64_t		scan_blocks_fixed = -1;		/* # blocks repaired */
63 
64 /*
65  * This variable is used to tell whether the most recent surface
66  * analysis error was caused by a media defect or some other problem.
67  */
68 int	media_error;			/* error was caused by defect */
69 
70 int	disk_error;			/* disk errors during analysis */
71 
72 /*
73  * These are the data patterns used if random patterns are not chosen.
74  * They are designed to show pattern dependent errors.
75  */
76 static unsigned int	scan_patterns[] = {
77 	0xc6dec6de,
78 	0x6db6db6d,
79 	0x00000000,
80 	0xffffffff,
81 	0xaaaaaaaa,
82 };
83 #define	NPATTERNS	5		/* number of predefined patterns */
84 
85 /*
86  * These are the data patterns from the SunFed requirements document.
87  */
88 static unsigned int purge_patterns[] = {	/* patterns to be written */
89 	0xaaaaaaaa,		/* 10101010... */
90 	0x55555555,		/* 01010101...  == UUUU... */
91 	0xaaaaaaaa,		/* 10101010... */
92 	0xaaaaaaaa,		/* 10101010... */
93 };
94 
95 static unsigned int alpha_pattern =  0x40404040;   /* 10000000...  == @@@@... */
96 
97 /* Function prototypes */
98 #ifdef	__STDC__
99 
100 static int	scan_repair(diskaddr_t bn, int mode);
101 static int	analyze_blocks(int flags, diskaddr_t blkno, uint_t blkcnt,
102 		unsigned data, int init, int driver_flags, int *xfercntp);
103 static int	handle_error_conditions(void);
104 static int	verify_blocks(int flags, diskaddr_t blkno, uint_t blkcnt,
105 		unsigned data, int driver_flags, int *xfercntp);
106 #else	/* __STDC__ */
107 
108 static int	scan_repair();
109 static int	analyze_blocks();
110 static int	handle_error_conditions();
111 static int	verify_blocks();
112 
113 #endif	/* __STDC__ */
114 
115 /*
116  * This routine performs a surface analysis based upon the global
117  * parameters.  It is called from several commands in the defect menu,
118  * and from the format command in the command menu (if post-format
119  * analysis is enable).
120  */
121 int
122 do_scan(flags, mode)
123 	int	flags, mode;
124 {
125 	diskaddr_t	start, end, curnt;
126 	int	pass, needinit, data;
127 	uint_t	size;
128 	int	status, founderr, i, j;
129 	int	error = 0;
130 	int	pattern = 0;
131 	int	xfercnt;
132 
133 	/*
134 	 * Check to be sure we aren't correcting without a defect list
135 	 * if the controller can correct the defect.
136 	 */
137 	if (scan_correct && !EMBEDDED_SCSI && (cur_ops->op_repair != NULL) &&
138 			(cur_list.list == NULL)) {
139 		err_print("Current Defect List must be initialized ");
140 		err_print("to do automatic repair.\n");
141 		return (-1);
142 	}
143 	/*
144 	 * Define the bounds of the scan.
145 	 */
146 	if (scan_entire) {
147 		start = 0;
148 	    if (cur_label == L_TYPE_SOLARIS) {
149 		if (cur_ctype->ctype_flags & CF_SCSI)
150 			end = datasects() - 1;
151 		else
152 			end = physsects() - 1;
153 	    } else if (cur_label == L_TYPE_EFI) {
154 		end = cur_parts->etoc->efi_last_lba;
155 	    }
156 	} else {
157 		start = scan_lower;
158 		end = scan_upper;
159 	}
160 	/*
161 	 * Make sure the user knows if we are scanning over a mounted
162 	 * partition.
163 	 */
164 	if ((flags & (SCAN_PATTERN | SCAN_WRITE)) &&
165 	    (checkmount(start, end))) {
166 		err_print("Cannot do analysis on a mounted partition.\n");
167 		return (-1);
168 	}
169 
170 	/*
171 	 * Make sure the user knows if we are scanning over a
172 	 * partition being used for swapping.
173 	 */
174 	if ((flags & (SCAN_PATTERN | SCAN_WRITE)) &&
175 	    (checkswap(start, end))) {
176 		err_print("Cannot do analysis on a partition \
177 		    which is currently being used for swapping.\n");
178 		return (-1);
179 	}
180 
181 	/*
182 	 * Check to see if any partitions used for svm, vxvm, ZFS zpool
183 	 * or live upgrade are on the disk.
184 	 */
185 	if ((flags & (SCAN_PATTERN | SCAN_WRITE)) &&
186 	    (checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
187 	    (diskaddr_t)-1, 0, 0))) {
188 		err_print("Cannot do analysis on a partition "
189 		    "while it in use as described above.\n");
190 		return (-1);
191 	}
192 
193 	/*
194 	 * If we are scanning destructively over certain sectors,
195 	 * we mark the defect list and/or label dirty so it will get rewritten.
196 	 */
197 	if (flags & (SCAN_PATTERN | SCAN_WRITE)) {
198 	    if (cur_label == L_TYPE_SOLARIS) {
199 		if (start < (diskaddr_t)totalsects() &&
200 				end >= (diskaddr_t)datasects()) {
201 			if (!EMBEDDED_SCSI) {
202 				cur_list.flags |= LIST_DIRTY;
203 			}
204 			if (cur_disk->disk_flags & DSK_LABEL)
205 				cur_flags |= LABEL_DIRTY;
206 		}
207 	    }
208 	    if (start == 0) {
209 		if (cur_disk->disk_flags & DSK_LABEL)
210 			cur_flags |= LABEL_DIRTY;
211 	    }
212 	}
213 	/*
214 	 * Initialize the summary info on sectors repaired.
215 	 */
216 	scan_blocks_fixed = 0;
217 	/*
218 	 * Loop through the passes of the scan. If required, loop forever.
219 	 */
220 	for (pass = 0; pass < scan_passes || scan_loop; pass++) {
221 		/*
222 		 * Determine the data pattern to use if pattern testing
223 		 * is to be done.
224 		 */
225 		if (flags & SCAN_PATTERN) {
226 			if (scan_random)
227 				data = (int)mrand48();
228 			else
229 				data = scan_patterns[pass % NPPATTERNS];
230 
231 			if (flags & SCAN_PURGE) {
232 				flags &= ~(SCAN_PURGE_READ_PASS
233 						| SCAN_PURGE_ALPHA_PASS);
234 				switch (pattern % (NPPATTERNS + 1)) {
235 				case NPPATTERNS:
236 					pattern = 0;
237 					if (!error) {
238 					    fmt_print(
239 "\nThe last %d passes were successful, running alpha pattern pass", NPPATTERNS);
240 					    flags |= SCAN_PURGE_ALPHA_PASS;
241 					    data = alpha_pattern;
242 					} else {
243 					    data = purge_patterns[pattern];
244 					    pattern++;
245 					};
246 					break;
247 				case READPATTERN:
248 					flags |=  SCAN_PURGE_READ_PASS;
249 					/* FALLTHROUGH */
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;
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 	buf = malloc(cur_blksz);
476 	if (buf == NULL) {
477 		err_print("Warning: no memory.\n\n");
478 		return (result);
479 	}
480 	enter_critical();
481 
482 	/*
483 	 * Determine if the error appears to be hard or soft.  We
484 	 * already assume there's an error.  If we can get any
485 	 * good data out of the sector, write that data back
486 	 * after the repair.
487 	 */
488 	buf_is_good = 0;
489 	for (i = 0; i < 5; i++) {
490 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn, 1,
491 				buf, F_SILENT, NULL);
492 		if (status == 0) {
493 			buf_is_good = 1;
494 			break;
495 		}
496 	}
497 
498 	fmt_print("Repairing %s error on %llu (",
499 				buf_is_good ? "soft" : "hard", bn);
500 	pr_dblock(fmt_print, bn);
501 	fmt_print(")...");
502 
503 	status = (*cur_ops->op_repair)(bn, mode);
504 	if (status) {
505 		/*
506 		 * If the repair failed, we note it and will return the
507 		 * failure. However, the analysis goes on.
508 		 */
509 		fmt_print("failed.\n\n");
510 	} else {
511 		/*
512 		 * The repair worked.  Write the good data we could
513 		 * recover from the failed block, if possible.
514 		 * If not, zero the block.  In doing so, try to
515 		 * determine if the new block appears ok.
516 		 */
517 		if (!buf_is_good) {
518 			bzero(buf, cur_blksz);
519 			fmt_print("Warning: Block %llu zero-filled.\n", bn);
520 		} else {
521 			fmt_print("ok.\n");
522 		}
523 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, bn,
524 					1, buf, (F_SILENT | F_ALLERRS), NULL);
525 		if (status == 0) {
526 			status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn,
527 					1, buf, (F_SILENT | F_ALLERRS), NULL);
528 		}
529 		if (status) {
530 			fmt_print("The new block also appears defective.\n");
531 		}
532 		fmt_print("\n");
533 		/*
534 		 * add the defect to the list and write the list out.
535 		 * Also, kill the working list so it will get resynced
536 		 * with the current list.
537 		 *
538 		 * For embedded scsi, we don't require a defect list.
539 		 * However, if we have one, add the defect if the
540 		 * list includes the grown list.  If not, kill it
541 		 * to force a resync if we need the list later.
542 		 */
543 		if (EMBEDDED_SCSI) {
544 			if (cur_list.list != NULL) {
545 				if (cur_list.flags & LIST_PGLIST) {
546 					add_ldef(bn, &cur_list);
547 				} else {
548 					kill_deflist(&cur_list);
549 				}
550 			}
551 		/*
552 		 * The next "if" statement reflects the fix for
553 		 * bug id 1026096 where format keeps adding the
554 		 * same defect to the defect list.
555 		 */
556 		} else if (cur_ctype->ctype_flags & CF_WLIST) {
557 			kill_deflist(&cur_list);
558 			(*cur_ops->op_ex_cur)(&cur_list);
559 			fmt_print("Current list updated\n");
560 		} else {
561 			add_ldef(bn, &cur_list);
562 			write_deflist(&cur_list);
563 		}
564 		kill_deflist(&work_list);
565 
566 		/* Log the repair.  */
567 		scan_blocks_fixed++;
568 
569 		/* return ok */
570 		result = 0;
571 	}
572 
573 	exit_critical();
574 	free(buf);
575 	return (result);
576 }
577 
578 
579 /*
580  * This routine analyzes a set of sectors on the disk.  It simply returns
581  * an error if a defect is found.  It is called by do_scan().
582  */
583 static int
584 analyze_blocks(flags, blkno, blkcnt, data, init, driver_flags, xfercntp)
585 	int	flags, driver_flags, init;
586 	uint_t	blkcnt;
587 	register unsigned data;
588 	diskaddr_t	blkno;
589 	int	*xfercntp;
590 {
591 	int		corrupt = 0;
592 	int		status;
593 	register diskaddr_t	i, nints;
594 	register unsigned *ptr = (uint_t *)pattern_buf;
595 
596 	media_error = 0;
597 	if (flags & SCAN_VERIFY) {
598 		return (verify_blocks(flags, blkno, blkcnt, data,
599 		    driver_flags, xfercntp));
600 	}
601 
602 	/*
603 	 * Initialize the pattern buffer if necessary.
604 	 */
605 	nints = (diskaddr_t)blkcnt * cur_blksz / sizeof (int);
606 	if ((flags & SCAN_PATTERN) && init) {
607 		for (i = 0; i < nints; i++)
608 			*((int *)((int *)pattern_buf + i)) = data;
609 	}
610 	/*
611 	 * Lock out interrupts so we can insure valid data will get
612 	 * restored. This is necessary because there are modes
613 	 * of scanning that corrupt the disk data then restore it at
614 	 * the end of the analysis.
615 	 */
616 	enter_critical();
617 	/*
618 	 * If the disk data is valid, read it into the data buffer.
619 	 */
620 	if (flags & SCAN_VALID) {
621 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
622 		    blkcnt, (caddr_t)cur_buf, driver_flags, xfercntp);
623 		if (status)
624 			goto bad;
625 	}
626 	/*
627 	 * If we are doing pattern testing, write and read the pattern
628 	 * from the pattern buffer.
629 	 */
630 	if (flags & SCAN_PATTERN) {
631 		/*
632 		 * If the disk data was valid, mark it corrupt so we know
633 		 * to restore it later.
634 		 */
635 		if (flags & SCAN_VALID)
636 			corrupt++;
637 		/*
638 		 * Only write if we're not on the read pass of SCAN_PURGE.
639 		 */
640 		if (!(flags & SCAN_PURGE_READ_PASS)) {
641 			status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
642 			    blkcnt, (caddr_t)pattern_buf, driver_flags,
643 			    xfercntp);
644 			if (status)
645 			    goto bad;
646 		}
647 		/*
648 		 * Only read if we are on the read pass of SCAN_PURGE, if we
649 		 * are purging.
650 		 */
651 		if ((!(flags & SCAN_PURGE)) || (flags & SCAN_PURGE_READ_PASS)) {
652 			status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
653 			    blkcnt, (caddr_t)pattern_buf, driver_flags,
654 			    xfercntp);
655 			if (status)
656 			    goto bad;
657 		}
658 	}
659 	/*
660 	 * If we are doing a data compare, make sure the pattern
661 	 * came back intact.
662 	 * Only compare if we are on the read pass of SCAN_PURGE, or
663 	 * we wrote random data instead of the expected data pattern.
664 	 */
665 	if ((flags & SCAN_COMPARE) || (flags & SCAN_PURGE_READ_PASS)) {
666 		for (i = nints, ptr = (uint_t *)pattern_buf; i; i--)
667 			if (*ptr++ != data) {
668 				err_print("Data miscompare error (expecting ");
669 				err_print("0x%x, got 0x%x) at ", data,
670 					*((int *)((int *)pattern_buf +
671 					(nints - i))));
672 				pr_dblock(err_print, blkno);
673 				err_print(", offset = 0x%llx.\n",
674 					(nints - i) * sizeof (int));
675 				goto bad;
676 			}
677 	}
678 	/*
679 	 * If we are supposed to write data out, do so.
680 	 */
681 	if (flags & SCAN_WRITE) {
682 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
683 		    blkcnt, (caddr_t)cur_buf, driver_flags, xfercntp);
684 		if (status)
685 			goto bad;
686 	}
687 	exit_critical();
688 	/*
689 	 * No errors occurred, return ok.
690 	 */
691 	return (0);
692 bad:
693 	/*
694 	 * There was an error.  If the data was corrupted, we write it
695 	 * out from the data buffer to restore it.
696 	 */
697 	if (corrupt) {
698 		if ((*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
699 				blkcnt, (caddr_t)cur_buf, F_NORMAL, xfercntp))
700 		err_print("Warning: unable to restore original data.\n");
701 	}
702 	exit_critical();
703 	/*
704 	 * Return the error.
705 	 */
706 	return (-1);
707 }
708 
709 
710 /*
711  * This routine analyzes a set of sectors on the disk. It simply returns
712  * an error if a defect is found.  It is called by analyze_blocks().
713  * For simplicity, this is done as a separate function instead of
714  * making the analyze_block routine complex.
715  *
716  * This routine implements the 'verify' command.  It writes the disk
717  * by writing unique data for each block; after the write pass, it
718  * reads the data and verifies for correctness. Note that the entire
719  * disk (or the range of disk) is fully written first and then read.
720  * This should eliminate any caching effect on the drives.
721  */
722 static int
723 verify_blocks(int flags,
724 		diskaddr_t blkno,
725 		uint_t blkcnt,
726 		unsigned data,
727 		int driver_flags,
728 		int *xfercntp)
729 {
730 	int		status, i, nints;
731 	unsigned	*ptr = (uint_t *)pattern_buf;
732 
733 	nints = cur_blksz / sizeof (int);
734 
735 	/*
736 	 * Initialize the pattern buffer if we are in write pass.
737 	 * Use the block number itself as data, each block has unique
738 	 * buffer data that way.
739 	 */
740 	if (!(flags & SCAN_VERIFY_READ_PASS)) {
741 		for (data = blkno; data < blkno + blkcnt; data++) {
742 			for (i = 0; i < nints; i++) {
743 				*ptr++ = data;
744 			}
745 		}
746 		ptr = (uint_t *)pattern_buf;
747 	}
748 
749 	/*
750 	 * Only write if we're not on the read pass of SCAN_VERIFY.
751 	 */
752 	if (!(flags & SCAN_VERIFY_READ_PASS)) {
753 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
754 		    blkcnt, (caddr_t)pattern_buf, driver_flags, xfercntp);
755 		if (status)
756 			goto bad;
757 	} else {
758 		/*
759 		 * Only read if we are on the read pass of SCAN_VERIFY
760 		 */
761 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
762 		    blkcnt, (caddr_t)pattern_buf, driver_flags, xfercntp);
763 		if (status)
764 			goto bad;
765 		/*
766 		 * compare and make sure the pattern came back intact.
767 		 */
768 		for (data = blkno; data < blkno + blkcnt; data++) {
769 			for (i = 0; i < nints; i++) {
770 				if (*ptr++ != data) {
771 					ptr--;
772 					err_print("Data miscompare error "
773 					    "(expecting 0x%x, got 0x%x) at ",
774 					    data, *ptr);
775 					pr_dblock(err_print, blkno);
776 					err_print(", offset = 0x%x.\n",
777 					    (ptr - (uint_t *)pattern_buf) *
778 					    sizeof (int));
779 					goto bad;
780 				}
781 			}
782 		}
783 	}
784 	/*
785 	 * No errors occurred, return ok.
786 	 */
787 	return (0);
788 bad:
789 	return (-1);
790 }
791 
792 
793 static int
794 handle_error_conditions()
795 {
796 
797 	/*
798 	 * Check if the errno is ENXIO.
799 	 */
800 	if (errno == ENXIO) {
801 		fmt_print("\n\nWarning:Cannot access drive, ");
802 		fmt_print("aborting surface analysis.\n");
803 		return (-1);
804 	}
805 	/*
806 	 * check for disk errors
807 	 */
808 	switch (disk_error) {
809 	case DISK_STAT_RESERVED:
810 	case DISK_STAT_UNAVAILABLE:
811 		fmt_print("\n\nWarning:Drive may be reserved ");
812 		fmt_print("or has been removed, ");
813 		fmt_print("aborting surface analysis.\n");
814 		return (-1);
815 	case DISK_STAT_NOTREADY:
816 		fmt_print("\n\nWarning: Drive not ready, ");
817 		fmt_print("aborting surface analysis.\n");
818 		return (-1);
819 	case DISK_STAT_DATA_PROTECT:
820 		fmt_print("\n\nWarning: Drive is write protected, ");
821 		fmt_print("aborting surface analysis.\n");
822 		return (-1);
823 	default:
824 		break;
825 	}
826 	return (0);
827 }
828