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