xref: /illumos-gate/usr/src/cmd/format/analyze.c (revision 602ca9ea)
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 2007 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, (daddr_t)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, (daddr_t)
340 				    (curnt + i), 1, (unsigned)data, needinit,
341 				    F_ALLERRS, NULL);
342 				needinit = 0;
343 				if (!status)
344 					continue;
345 				/*
346 				 * There was an error. Check if surface analysis
347 				 * can be continued.
348 				 */
349 				if (handle_error_conditions()) {
350 					scan_blocks_fixed = scan_cur_block = -1;
351 					return (-1);
352 				}
353 				/*
354 				 * An error occurred.  Mark the buffer
355 				 * corrupt and see if it was media
356 				 * related.
357 				 */
358 				needinit = 1;
359 				if (!media_error)
360 					continue;
361 				/*
362 				 * We found a bad sector. Print out a message
363 				 * and fix it if required.
364 				 */
365 				founderr = 1;
366 				if (scan_correct && (flags != SCAN_VALID)) {
367 					if (scan_repair(curnt+i, mode)) {
368 						error = -1;
369 					}
370 				} else
371 					err_print("\n");
372 				/*
373 				 * Stop after the error if required.
374 				 */
375 				if (scan_stop)
376 					goto out;
377 			}
378 			/*
379 			 * Mark the pattern buffer corrupt to be safe.
380 			 */
381 			needinit = 1;
382 			/*
383 			 * We didn't find an individual sector that was bad.
384 			 * Print out a warning.
385 			 */
386 			if (!founderr) {
387 				err_print("Warning: unable to pinpoint ");
388 				err_print("defective block.\n");
389 			}
390 		}
391 		/*
392 		 * Print the end of each pass to the log file.
393 		 */
394 		enter_critical();
395 		if (log_file) {
396 			pr_dblock(log_print, scan_cur_block);
397 			log_print("\n");
398 		}
399 		scan_cur_block = -1;
400 		exit_critical();
401 		fmt_print("\n");
402 
403 		/*
404 		 * alternate the read and write for SCAN_VERIFY test
405 		 */
406 		if (flags & SCAN_VERIFY) {
407 			flags ^= SCAN_VERIFY_READ_PASS;
408 		}
409 	}
410 out:
411 	/*
412 	 * We got here either by giving up after an error or falling
413 	 * through after all passes were completed.
414 	 */
415 	fmt_print("\n");
416 	enter_critical();
417 	/*
418 	 * If the defect list is dirty, write it to disk,
419 	 * if scan_restore_defects (the default) is true.
420 	 */
421 	if (!EMBEDDED_SCSI && (cur_list.flags & LIST_DIRTY) &&
422 				(scan_restore_defects)) {
423 		cur_list.flags = 0;
424 		write_deflist(&cur_list);
425 		}
426 	/*
427 	 * If the label is dirty, write it to disk.
428 	 * if scan_restore_label (the default) is true.
429 	 */
430 	if ((cur_flags & LABEL_DIRTY) && (scan_restore_label)) {
431 		cur_flags &= ~LABEL_DIRTY;
432 		(void) write_label();
433 	}
434 	/*
435 	 * If we dropped down to here after an error, we need to write
436 	 * the final block number to the log file for record keeping.
437 	 */
438 	if (log_file && scan_cur_block >= 0) {
439 		pr_dblock(log_print, scan_cur_block);
440 		log_print("\n");
441 	}
442 	fmt_print("Total of %lld defective blocks repaired.\n",
443 		scan_blocks_fixed);
444 	/*
445 	 * Reinitialize the logging variables so they don't get used
446 	 * when they are not really valid.
447 	 */
448 	scan_blocks_fixed = scan_cur_block = -1;
449 	exit_critical();
450 	return (error);
451 }
452 
453 
454 /*
455  * This routine is called to repair a bad block discovered
456  * during a scan operation.  Return 0 for success, 1 for failure.
457  * (This has been extracted out of do_scan(), to simplify it.)
458  */
459 static int
460 scan_repair(bn, mode)
461 	diskaddr_t	bn;
462 	int	mode;
463 {
464 	int	status;
465 	int	result = 1;
466 	char	buf[SECSIZE];
467 	int	buf_is_good;
468 	int	i;
469 
470 	if (cur_ops->op_repair == NULL) {
471 		err_print("Warning: Controller does ");
472 		err_print("not support repairing.\n\n");
473 		return (result);
474 	}
475 
476 	enter_critical();
477 
478 	/*
479 	 * Determine if the error appears to be hard or soft.  We
480 	 * already assume there's an error.  If we can get any
481 	 * good data out of the sector, write that data back
482 	 * after the repair.
483 	 */
484 	buf_is_good = 0;
485 	for (i = 0; i < 5; i++) {
486 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn, 1,
487 				buf, F_SILENT, NULL);
488 		if (status == 0) {
489 			buf_is_good = 1;
490 			break;
491 		}
492 	}
493 
494 	fmt_print("Repairing %s error on %llu (",
495 				buf_is_good ? "soft" : "hard", bn);
496 	pr_dblock(fmt_print, bn);
497 	fmt_print(")...");
498 
499 	status = (*cur_ops->op_repair)(bn, mode);
500 	if (status) {
501 		/*
502 		 * If the repair failed, we note it and will return the
503 		 * failure. However, the analysis goes on.
504 		 */
505 		fmt_print("failed.\n\n");
506 	} else {
507 		/*
508 		 * The repair worked.  Write the good data we could
509 		 * recover from the failed block, if possible.
510 		 * If not, zero the block.  In doing so, try to
511 		 * determine if the new block appears ok.
512 		 */
513 		if (!buf_is_good) {
514 			bzero(buf, SECSIZE);
515 			fmt_print("Warning: Block %llu zero-filled.\n", bn);
516 		} else {
517 			fmt_print("ok.\n");
518 		}
519 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, bn,
520 					1, buf, (F_SILENT | F_ALLERRS), NULL);
521 		if (status == 0) {
522 			status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn,
523 					1, buf, (F_SILENT | F_ALLERRS), NULL);
524 		}
525 		if (status) {
526 			fmt_print("The new block also appears defective.\n");
527 		}
528 		fmt_print("\n");
529 		/*
530 		 * add the defect to the list and write the list out.
531 		 * Also, kill the working list so it will get resynced
532 		 * with the current list.
533 		 *
534 		 * For embedded scsi, we don't require a defect list.
535 		 * However, if we have one, add the defect if the
536 		 * list includes the grown list.  If not, kill it
537 		 * to force a resync if we need the list later.
538 		 */
539 		if (EMBEDDED_SCSI) {
540 			if (cur_list.list != NULL) {
541 				if (cur_list.flags & LIST_PGLIST) {
542 					add_ldef(bn, &cur_list);
543 				} else {
544 					kill_deflist(&cur_list);
545 				}
546 			}
547 		/*
548 		 * The next "if" statement reflects the fix for
549 		 * bug id 1026096 where format keeps adding the
550 		 * same defect to the defect list.
551 		 */
552 		} else if (cur_ctype->ctype_flags & CF_WLIST) {
553 			kill_deflist(&cur_list);
554 			(*cur_ops->op_ex_cur)(&cur_list);
555 			fmt_print("Current list updated\n");
556 		} else {
557 			add_ldef(bn, &cur_list);
558 			write_deflist(&cur_list);
559 		}
560 		kill_deflist(&work_list);
561 
562 		/* Log the repair.  */
563 		scan_blocks_fixed++;
564 
565 		/* return ok */
566 		result = 0;
567 	}
568 
569 	exit_critical();
570 
571 	return (result);
572 }
573 
574 
575 /*
576  * This routine analyzes a set of sectors on the disk.  It simply returns
577  * an error if a defect is found.  It is called by do_scan().
578  */
579 static int
580 analyze_blocks(flags, blkno, blkcnt, data, init, driver_flags, xfercntp)
581 	int	flags, driver_flags, blkcnt, init;
582 	register unsigned data;
583 	diskaddr_t	blkno;
584 	int	*xfercntp;
585 {
586 	int	corrupt = 0;
587 	register int	status, 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 = 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%x.\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 		int 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 (expecting "
767 				"0x%x, got 0x%x) at ", data, *ptr);
768 			    pr_dblock(err_print, blkno);
769 			    err_print(", offset = 0x%x.\n", (ptr -
770 				(uint_t *)pattern_buf) * sizeof (int));
771 			    goto bad;
772 			}
773 		    }
774 		}
775 	}
776 	/*
777 	 * No errors occurred, return ok.
778 	 */
779 	return (0);
780 bad:
781 	return (-1);
782 }
783 
784 
785 static int
786 handle_error_conditions()
787 {
788 
789 	/*
790 	 * Check if the errno is ENXIO.
791 	 */
792 	if (errno == ENXIO) {
793 		fmt_print("\n\nWarning:Cannot access drive, ");
794 		fmt_print("aborting surface analysis.\n");
795 		return (-1);
796 	}
797 	/*
798 	 * check for disk errors
799 	 */
800 	switch (disk_error) {
801 	case DISK_STAT_RESERVED:
802 	case DISK_STAT_UNAVAILABLE:
803 		fmt_print("\n\nWarning:Drive may be reserved ");
804 		fmt_print("or has been removed, ");
805 		fmt_print("aborting surface analysis.\n");
806 		return (-1);
807 	case DISK_STAT_NOTREADY:
808 		fmt_print("\n\nWarning: Drive not ready, ");
809 		fmt_print("aborting surface analysis.\n");
810 		return (-1);
811 	case DISK_STAT_DATA_PROTECT:
812 		fmt_print("\n\nWarning: Drive is write protected, ");
813 		fmt_print("aborting surface analysis.\n");
814 		return (-1);
815 	default:
816 		break;
817 	}
818 	return (0);
819 }
820