xref: /illumos-gate/usr/src/cmd/format/analyze.c (revision 8b80e8cb6855118d46f605e91b5ed4ce83417395)
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 /*
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 				default:
250 					data = purge_patterns[pattern];
251 					pattern++;
252 					break;
253 				}
254 			}
255 			fmt_print("\n        pass %d", pass);
256 			fmt_print(" - pattern = 0x%x", data);
257 		} else
258 			fmt_print("\n        pass %d", pass);
259 
260 		fmt_print("\n");
261 		/*
262 		 * Mark the pattern buffer as corrupt, since it
263 		 * hasn't been initialized.
264 		 */
265 		needinit = 1;
266 		/*
267 		 * Print the first block number to the log file if
268 		 * logging is on so there is some record of what
269 		 * analysis was performed.
270 		 */
271 		if (log_file) {
272 			pr_dblock(log_print, start);
273 			log_print("\n");
274 		}
275 		/*
276 		 * Loop through this pass, each time analyzing an amount
277 		 * specified by the global parameters.
278 		 */
279 		xfercnt = 0;
280 		for (curnt = start; curnt <= end; curnt += size) {
281 			if ((end - curnt) < scan_size)
282 				size = end - curnt + 1;
283 			else
284 				size = scan_size;
285 			/*
286 			 * Print out where we are, so we don't look dead.
287 			 * Also store it in summary info for logging.
288 			 */
289 			scan_cur_block = curnt;
290 			nolog_print("   ");
291 			pr_dblock(nolog_print, curnt);
292 			nolog_print("  \015");
293 			(void) fflush(stdout);
294 			disk_error = 0;
295 			/*
296 			 * Do the actual analysis.
297 			 */
298 			status = analyze_blocks(flags, curnt, size,
299 			    (unsigned)data, needinit, (F_ALLERRS | F_SILENT),
300 			    &xfercnt);
301 			/*
302 			 * If there were no errors, the pattern buffer is
303 			 * still initialized, and we just loop to next chunk.
304 			 */
305 			needinit = 0;
306 			if (!status)
307 				continue;
308 			/*
309 			 * There was an error. Check if surface analysis
310 			 * can be continued.
311 			 */
312 			if (handle_error_conditions()) {
313 				scan_blocks_fixed = scan_cur_block = -1;
314 				return (-1);
315 			}
316 			/*
317 			 * There was an error. Mark the pattern buffer
318 			 * corrupt so it will get reinitialized.
319 			 */
320 			needinit = 1;
321 			/*
322 			 * If it was not a media error, ignore it.
323 			 */
324 			if (!media_error)
325 				continue;
326 			/*
327 			 * Loop 5 times through each sector of the chunk,
328 			 * analyzing them individually.
329 			 */
330 			nolog_print("   ");
331 			pr_dblock(nolog_print, curnt);
332 			nolog_print("  \015");
333 			(void) fflush(stdout);
334 			founderr = 0;
335 			for (j = 0; j < size * 5; j++) {
336 				i = j % size;
337 				disk_error = 0;
338 				status = analyze_blocks(flags, (curnt + i), 1,
339 				    (unsigned)data, needinit, F_ALLERRS, NULL);
340 				needinit = 0;
341 				if (!status)
342 					continue;
343 				/*
344 				 * There was an error. Check if surface analysis
345 				 * can be continued.
346 				 */
347 				if (handle_error_conditions()) {
348 					scan_blocks_fixed = scan_cur_block = -1;
349 					return (-1);
350 				}
351 				/*
352 				 * An error occurred.  Mark the buffer
353 				 * corrupt and see if it was media
354 				 * related.
355 				 */
356 				needinit = 1;
357 				if (!media_error)
358 					continue;
359 				/*
360 				 * We found a bad sector. Print out a message
361 				 * and fix it if required.
362 				 */
363 				founderr = 1;
364 				if (scan_correct && (flags != SCAN_VALID)) {
365 					if (scan_repair(curnt+i, mode)) {
366 						error = -1;
367 					}
368 				} else
369 					err_print("\n");
370 				/*
371 				 * Stop after the error if required.
372 				 */
373 				if (scan_stop)
374 					goto out;
375 			}
376 			/*
377 			 * Mark the pattern buffer corrupt to be safe.
378 			 */
379 			needinit = 1;
380 			/*
381 			 * We didn't find an individual sector that was bad.
382 			 * Print out a warning.
383 			 */
384 			if (!founderr) {
385 				err_print("Warning: unable to pinpoint ");
386 				err_print("defective block.\n");
387 			}
388 		}
389 		/*
390 		 * Print the end of each pass to the log file.
391 		 */
392 		enter_critical();
393 		if (log_file) {
394 			pr_dblock(log_print, scan_cur_block);
395 			log_print("\n");
396 		}
397 		scan_cur_block = -1;
398 		exit_critical();
399 		fmt_print("\n");
400 
401 		/*
402 		 * alternate the read and write for SCAN_VERIFY test
403 		 */
404 		if (flags & SCAN_VERIFY) {
405 			flags ^= SCAN_VERIFY_READ_PASS;
406 		}
407 	}
408 out:
409 	/*
410 	 * We got here either by giving up after an error or falling
411 	 * through after all passes were completed.
412 	 */
413 	fmt_print("\n");
414 	enter_critical();
415 	/*
416 	 * If the defect list is dirty, write it to disk,
417 	 * if scan_restore_defects (the default) is true.
418 	 */
419 	if (!EMBEDDED_SCSI && (cur_list.flags & LIST_DIRTY) &&
420 				(scan_restore_defects)) {
421 		cur_list.flags = 0;
422 		write_deflist(&cur_list);
423 		}
424 	/*
425 	 * If the label is dirty, write it to disk.
426 	 * if scan_restore_label (the default) is true.
427 	 */
428 	if ((cur_flags & LABEL_DIRTY) && (scan_restore_label)) {
429 		cur_flags &= ~LABEL_DIRTY;
430 		(void) write_label();
431 	}
432 	/*
433 	 * If we dropped down to here after an error, we need to write
434 	 * the final block number to the log file for record keeping.
435 	 */
436 	if (log_file && scan_cur_block >= 0) {
437 		pr_dblock(log_print, scan_cur_block);
438 		log_print("\n");
439 	}
440 	fmt_print("Total of %lld defective blocks repaired.\n",
441 		scan_blocks_fixed);
442 	/*
443 	 * Reinitialize the logging variables so they don't get used
444 	 * when they are not really valid.
445 	 */
446 	scan_blocks_fixed = scan_cur_block = -1;
447 	exit_critical();
448 	return (error);
449 }
450 
451 
452 /*
453  * This routine is called to repair a bad block discovered
454  * during a scan operation.  Return 0 for success, 1 for failure.
455  * (This has been extracted out of do_scan(), to simplify it.)
456  */
457 static int
458 scan_repair(bn, mode)
459 	diskaddr_t	bn;
460 	int	mode;
461 {
462 	int	status;
463 	int	result = 1;
464 	char	buf[SECSIZE];
465 	int	buf_is_good;
466 	int	i;
467 
468 	if (cur_ops->op_repair == NULL) {
469 		err_print("Warning: Controller does ");
470 		err_print("not support repairing.\n\n");
471 		return (result);
472 	}
473 
474 	enter_critical();
475 
476 	/*
477 	 * Determine if the error appears to be hard or soft.  We
478 	 * already assume there's an error.  If we can get any
479 	 * good data out of the sector, write that data back
480 	 * after the repair.
481 	 */
482 	buf_is_good = 0;
483 	for (i = 0; i < 5; i++) {
484 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn, 1,
485 				buf, F_SILENT, NULL);
486 		if (status == 0) {
487 			buf_is_good = 1;
488 			break;
489 		}
490 	}
491 
492 	fmt_print("Repairing %s error on %llu (",
493 				buf_is_good ? "soft" : "hard", bn);
494 	pr_dblock(fmt_print, bn);
495 	fmt_print(")...");
496 
497 	status = (*cur_ops->op_repair)(bn, mode);
498 	if (status) {
499 		/*
500 		 * If the repair failed, we note it and will return the
501 		 * failure. However, the analysis goes on.
502 		 */
503 		fmt_print("failed.\n\n");
504 	} else {
505 		/*
506 		 * The repair worked.  Write the good data we could
507 		 * recover from the failed block, if possible.
508 		 * If not, zero the block.  In doing so, try to
509 		 * determine if the new block appears ok.
510 		 */
511 		if (!buf_is_good) {
512 			bzero(buf, SECSIZE);
513 			fmt_print("Warning: Block %llu zero-filled.\n", bn);
514 		} else {
515 			fmt_print("ok.\n");
516 		}
517 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, bn,
518 					1, buf, (F_SILENT | F_ALLERRS), NULL);
519 		if (status == 0) {
520 			status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn,
521 					1, buf, (F_SILENT | F_ALLERRS), NULL);
522 		}
523 		if (status) {
524 			fmt_print("The new block also appears defective.\n");
525 		}
526 		fmt_print("\n");
527 		/*
528 		 * add the defect to the list and write the list out.
529 		 * Also, kill the working list so it will get resynced
530 		 * with the current list.
531 		 *
532 		 * For embedded scsi, we don't require a defect list.
533 		 * However, if we have one, add the defect if the
534 		 * list includes the grown list.  If not, kill it
535 		 * to force a resync if we need the list later.
536 		 */
537 		if (EMBEDDED_SCSI) {
538 			if (cur_list.list != NULL) {
539 				if (cur_list.flags & LIST_PGLIST) {
540 					add_ldef(bn, &cur_list);
541 				} else {
542 					kill_deflist(&cur_list);
543 				}
544 			}
545 		/*
546 		 * The next "if" statement reflects the fix for
547 		 * bug id 1026096 where format keeps adding the
548 		 * same defect to the defect list.
549 		 */
550 		} else if (cur_ctype->ctype_flags & CF_WLIST) {
551 			kill_deflist(&cur_list);
552 			(*cur_ops->op_ex_cur)(&cur_list);
553 			fmt_print("Current list updated\n");
554 		} else {
555 			add_ldef(bn, &cur_list);
556 			write_deflist(&cur_list);
557 		}
558 		kill_deflist(&work_list);
559 
560 		/* Log the repair.  */
561 		scan_blocks_fixed++;
562 
563 		/* return ok */
564 		result = 0;
565 	}
566 
567 	exit_critical();
568 
569 	return (result);
570 }
571 
572 
573 /*
574  * This routine analyzes a set of sectors on the disk.  It simply returns
575  * an error if a defect is found.  It is called by do_scan().
576  */
577 static int
578 analyze_blocks(flags, blkno, blkcnt, data, init, driver_flags, xfercntp)
579 	int	flags, driver_flags, init;
580 	uint_t	blkcnt;
581 	register unsigned data;
582 	diskaddr_t	blkno;
583 	int	*xfercntp;
584 {
585 	int		corrupt = 0;
586 	int		status;
587 	register diskaddr_t	i, nints;
588 	register unsigned *ptr = (uint_t *)pattern_buf;
589 
590 	media_error = 0;
591 	if (flags & SCAN_VERIFY) {
592 		return (verify_blocks(flags, blkno, blkcnt, data,
593 		    driver_flags, xfercntp));
594 	}
595 
596 	/*
597 	 * Initialize the pattern buffer if necessary.
598 	 */
599 	nints = (diskaddr_t)blkcnt * SECSIZE / sizeof (int);
600 	if ((flags & SCAN_PATTERN) && init) {
601 		for (i = 0; i < nints; i++)
602 			*((int *)((int *)pattern_buf + i)) = data;
603 	}
604 	/*
605 	 * Lock out interrupts so we can insure valid data will get
606 	 * restored. This is necessary because there are modes
607 	 * of scanning that corrupt the disk data then restore it at
608 	 * the end of the analysis.
609 	 */
610 	enter_critical();
611 	/*
612 	 * If the disk data is valid, read it into the data buffer.
613 	 */
614 	if (flags & SCAN_VALID) {
615 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
616 		    blkcnt, (caddr_t)cur_buf, driver_flags, xfercntp);
617 		if (status)
618 			goto bad;
619 	}
620 	/*
621 	 * If we are doing pattern testing, write and read the pattern
622 	 * from the pattern buffer.
623 	 */
624 	if (flags & SCAN_PATTERN) {
625 		/*
626 		 * If the disk data was valid, mark it corrupt so we know
627 		 * to restore it later.
628 		 */
629 		if (flags & SCAN_VALID)
630 			corrupt++;
631 		/*
632 		 * Only write if we're not on the read pass of SCAN_PURGE.
633 		 */
634 		if (!(flags & SCAN_PURGE_READ_PASS)) {
635 			status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
636 			    blkcnt, (caddr_t)pattern_buf, driver_flags,
637 			    xfercntp);
638 			if (status)
639 			    goto bad;
640 		}
641 		/*
642 		 * Only read if we are on the read pass of SCAN_PURGE, if we
643 		 * are purging.
644 		 */
645 		if ((!(flags & SCAN_PURGE)) || (flags & SCAN_PURGE_READ_PASS)) {
646 			status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
647 			    blkcnt, (caddr_t)pattern_buf, driver_flags,
648 			    xfercntp);
649 			if (status)
650 			    goto bad;
651 		}
652 	}
653 	/*
654 	 * If we are doing a data compare, make sure the pattern
655 	 * came back intact.
656 	 * Only compare if we are on the read pass of SCAN_PURGE, or
657 	 * we wrote random data instead of the expected data pattern.
658 	 */
659 	if ((flags & SCAN_COMPARE) || (flags & SCAN_PURGE_READ_PASS)) {
660 		for (i = nints, ptr = (uint_t *)pattern_buf; i; i--)
661 			if (*ptr++ != data) {
662 				err_print("Data miscompare error (expecting ");
663 				err_print("0x%x, got 0x%x) at ", data,
664 					*((int *)((int *)pattern_buf +
665 					(nints - i))));
666 				pr_dblock(err_print, blkno);
667 				err_print(", offset = 0x%llx.\n",
668 					(nints - i) * sizeof (int));
669 				goto bad;
670 			}
671 	}
672 	/*
673 	 * If we are supposed to write data out, do so.
674 	 */
675 	if (flags & SCAN_WRITE) {
676 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
677 		    blkcnt, (caddr_t)cur_buf, driver_flags, xfercntp);
678 		if (status)
679 			goto bad;
680 	}
681 	exit_critical();
682 	/*
683 	 * No errors occurred, return ok.
684 	 */
685 	return (0);
686 bad:
687 	/*
688 	 * There was an error.  If the data was corrupted, we write it
689 	 * out from the data buffer to restore it.
690 	 */
691 	if (corrupt) {
692 		if ((*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
693 				blkcnt, (caddr_t)cur_buf, F_NORMAL, xfercntp))
694 		err_print("Warning: unable to restore original data.\n");
695 	}
696 	exit_critical();
697 	/*
698 	 * Return the error.
699 	 */
700 	return (-1);
701 }
702 
703 
704 /*
705  * This routine analyzes a set of sectors on the disk. It simply returns
706  * an error if a defect is found.  It is called by analyze_blocks().
707  * For simplicity, this is done as a separate function instead of
708  * making the analyze_block routine complex.
709  *
710  * This routine implements the 'verify' command.  It writes the disk
711  * by writing unique data for each block; after the write pass, it
712  * reads the data and verifies for correctness. Note that the entire
713  * disk (or the range of disk) is fully written first and then read.
714  * This should eliminate any caching effect on the drives.
715  */
716 static int
717 verify_blocks(int flags,
718 		diskaddr_t blkno,
719 		uint_t blkcnt,
720 		unsigned data,
721 		int driver_flags,
722 		int *xfercntp)
723 {
724 	int		status, i, nints;
725 	unsigned	*ptr = (uint_t *)pattern_buf;
726 
727 	nints = SECSIZE / sizeof (int);
728 
729 	/*
730 	 * Initialize the pattern buffer if we are in write pass.
731 	 * Use the block number itself as data, each block has unique
732 	 * buffer data that way.
733 	 */
734 	if (!(flags & SCAN_VERIFY_READ_PASS)) {
735 		for (data = blkno; data < blkno + blkcnt; data++) {
736 			for (i = 0; i < nints; i++) {
737 				*ptr++ = data;
738 			}
739 		}
740 		ptr = (uint_t *)pattern_buf;
741 	}
742 
743 	/*
744 	 * Only write if we're not on the read pass of SCAN_VERIFY.
745 	 */
746 	if (!(flags & SCAN_VERIFY_READ_PASS)) {
747 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
748 		    blkcnt, (caddr_t)pattern_buf, driver_flags, xfercntp);
749 		if (status)
750 			goto bad;
751 	} else {
752 		/*
753 		 * Only read if we are on the read pass of SCAN_VERIFY
754 		 */
755 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
756 		    blkcnt, (caddr_t)pattern_buf, driver_flags, xfercntp);
757 		if (status)
758 			goto bad;
759 		/*
760 		 * compare and make sure the pattern came back intact.
761 		 */
762 		for (data = blkno; data < blkno + blkcnt; data++) {
763 			for (i = 0; i < nints; i++) {
764 				if (*ptr++ != data) {
765 					ptr--;
766 					err_print("Data miscompare error "
767 					    "(expecting 0x%x, got 0x%x) at ",
768 					    data, *ptr);
769 					pr_dblock(err_print, blkno);
770 					err_print(", offset = 0x%x.\n",
771 					    (ptr - (uint_t *)pattern_buf) *
772 					    sizeof (int));
773 					goto bad;
774 				}
775 			}
776 		}
777 	}
778 	/*
779 	 * No errors occurred, return ok.
780 	 */
781 	return (0);
782 bad:
783 	return (-1);
784 }
785 
786 
787 static int
788 handle_error_conditions()
789 {
790 
791 	/*
792 	 * Check if the errno is ENXIO.
793 	 */
794 	if (errno == ENXIO) {
795 		fmt_print("\n\nWarning:Cannot access drive, ");
796 		fmt_print("aborting surface analysis.\n");
797 		return (-1);
798 	}
799 	/*
800 	 * check for disk errors
801 	 */
802 	switch (disk_error) {
803 	case DISK_STAT_RESERVED:
804 	case DISK_STAT_UNAVAILABLE:
805 		fmt_print("\n\nWarning:Drive may be reserved ");
806 		fmt_print("or has been removed, ");
807 		fmt_print("aborting surface analysis.\n");
808 		return (-1);
809 	case DISK_STAT_NOTREADY:
810 		fmt_print("\n\nWarning: Drive not ready, ");
811 		fmt_print("aborting surface analysis.\n");
812 		return (-1);
813 	case DISK_STAT_DATA_PROTECT:
814 		fmt_print("\n\nWarning: Drive is write protected, ");
815 		fmt_print("aborting surface analysis.\n");
816 		return (-1);
817 	default:
818 		break;
819 	}
820 	return (0);
821 }
822