17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*3e1bd7a2Ssjelinek * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * This file contains routines to analyze the surface of a disk. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate #include "global.h" 337c478bd9Sstevel@tonic-gate #include "analyze.h" 347c478bd9Sstevel@tonic-gate #include <stdlib.h> 357c478bd9Sstevel@tonic-gate #include <errno.h> 367c478bd9Sstevel@tonic-gate #include "misc.h" 377c478bd9Sstevel@tonic-gate #include "defect.h" 387c478bd9Sstevel@tonic-gate #include "label.h" 397c478bd9Sstevel@tonic-gate #include "param.h" 40*3e1bd7a2Ssjelinek #include "checkdev.h" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate /* 447c478bd9Sstevel@tonic-gate * These global variables control the surface analysis process. They 457c478bd9Sstevel@tonic-gate * are set from a command in the defect menu. 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate int scan_entire = 1; /* scan whole disk flag */ 487c478bd9Sstevel@tonic-gate diskaddr_t scan_lower = 0; /* lower bound */ 497c478bd9Sstevel@tonic-gate diskaddr_t scan_upper = 0; /* upper bound */ 507c478bd9Sstevel@tonic-gate int scan_correct = 1; /* correct errors flag */ 517c478bd9Sstevel@tonic-gate int scan_stop = 0; /* stop after error flag */ 527c478bd9Sstevel@tonic-gate int scan_loop = 0; /* loop forever flag */ 537c478bd9Sstevel@tonic-gate int scan_passes = 2; /* number of passes */ 547c478bd9Sstevel@tonic-gate int scan_random = 0; /* random patterns flag */ 557c478bd9Sstevel@tonic-gate int scan_size = 0; /* sectors/scan operation */ 567c478bd9Sstevel@tonic-gate int scan_auto = 1; /* scan after format flag */ 577c478bd9Sstevel@tonic-gate int scan_restore_defects = 1; /* restore defect list after writing */ 587c478bd9Sstevel@tonic-gate int scan_restore_label = 1; /* restore label after writing */ 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* 617c478bd9Sstevel@tonic-gate * These are summary variables to print out info after analysis. 627c478bd9Sstevel@tonic-gate * Values less than 0 imply they are invalid. 637c478bd9Sstevel@tonic-gate */ 647c478bd9Sstevel@tonic-gate offset_t scan_cur_block = -1; /* current block */ 657c478bd9Sstevel@tonic-gate int64_t scan_blocks_fixed = -1; /* # blocks repaired */ 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate /* 687c478bd9Sstevel@tonic-gate * This variable is used to tell whether the most recent surface 697c478bd9Sstevel@tonic-gate * analysis error was caused by a media defect or some other problem. 707c478bd9Sstevel@tonic-gate */ 717c478bd9Sstevel@tonic-gate int media_error; /* error was caused by defect */ 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate int disk_error; /* disk errors during analysis */ 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate /* 767c478bd9Sstevel@tonic-gate * These are the data patterns used if random patterns are not chosen. 777c478bd9Sstevel@tonic-gate * They are designed to show pattern dependent errors. 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate static unsigned int scan_patterns[] = { 807c478bd9Sstevel@tonic-gate 0xc6dec6de, 817c478bd9Sstevel@tonic-gate 0x6db6db6d, 827c478bd9Sstevel@tonic-gate 0x00000000, 837c478bd9Sstevel@tonic-gate 0xffffffff, 847c478bd9Sstevel@tonic-gate 0xaaaaaaaa, 857c478bd9Sstevel@tonic-gate }; 867c478bd9Sstevel@tonic-gate #define NPATTERNS 5 /* number of predefined patterns */ 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* 897c478bd9Sstevel@tonic-gate * These are the data patterns from the SunFed requirements document. 907c478bd9Sstevel@tonic-gate */ 917c478bd9Sstevel@tonic-gate static unsigned int purge_patterns[] = { /* patterns to be written */ 927c478bd9Sstevel@tonic-gate 0xaaaaaaaa, /* 10101010... */ 937c478bd9Sstevel@tonic-gate 0x55555555, /* 01010101... == UUUU... */ 947c478bd9Sstevel@tonic-gate 0xaaaaaaaa, /* 10101010... */ 957c478bd9Sstevel@tonic-gate 0xaaaaaaaa, /* 10101010... */ 967c478bd9Sstevel@tonic-gate }; 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate static unsigned int alpha_pattern = 0x40404040; /* 10000000... == @@@@... */ 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* Function prototypes */ 1017c478bd9Sstevel@tonic-gate #ifdef __STDC__ 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate static int scan_repair(diskaddr_t bn, int mode); 1047c478bd9Sstevel@tonic-gate static int analyze_blocks(int flags, diskaddr_t blkno, int blkcnt, 1057c478bd9Sstevel@tonic-gate unsigned data, int init, int driver_flags, int *xfercntp); 1067c478bd9Sstevel@tonic-gate static int handle_error_conditions(void); 1077c478bd9Sstevel@tonic-gate static int verify_blocks(int flags, diskaddr_t blkno, int blkcnt, 1087c478bd9Sstevel@tonic-gate unsigned data, int driver_flags, int *xfercntp); 1097c478bd9Sstevel@tonic-gate #else /* __STDC__ */ 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate static int scan_repair(); 1127c478bd9Sstevel@tonic-gate static int analyze_blocks(); 1137c478bd9Sstevel@tonic-gate static int handle_error_conditions(); 1147c478bd9Sstevel@tonic-gate static int verify_blocks(); 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate #endif /* __STDC__ */ 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate /* 1197c478bd9Sstevel@tonic-gate * This routine performs a surface analysis based upon the global 1207c478bd9Sstevel@tonic-gate * parameters. It is called from several commands in the defect menu, 1217c478bd9Sstevel@tonic-gate * and from the format command in the command menu (if post-format 1227c478bd9Sstevel@tonic-gate * analysis is enable). 1237c478bd9Sstevel@tonic-gate */ 1247c478bd9Sstevel@tonic-gate int 1257c478bd9Sstevel@tonic-gate do_scan(flags, mode) 1267c478bd9Sstevel@tonic-gate int flags, mode; 1277c478bd9Sstevel@tonic-gate { 1287c478bd9Sstevel@tonic-gate diskaddr_t start, end, curnt; 1297c478bd9Sstevel@tonic-gate int pass, size, needinit, data; 1307c478bd9Sstevel@tonic-gate int status, founderr, i, j; 1317c478bd9Sstevel@tonic-gate int error = 0; 1327c478bd9Sstevel@tonic-gate int pattern = 0; 1337c478bd9Sstevel@tonic-gate int xfercnt; 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate /* 1367c478bd9Sstevel@tonic-gate * Check to be sure we aren't correcting without a defect list 1377c478bd9Sstevel@tonic-gate * if the controller can correct the defect. 1387c478bd9Sstevel@tonic-gate */ 1397c478bd9Sstevel@tonic-gate if (scan_correct && !EMBEDDED_SCSI && (cur_ops->op_repair != NULL) && 1407c478bd9Sstevel@tonic-gate (cur_list.list == NULL)) { 1417c478bd9Sstevel@tonic-gate err_print("Current Defect List must be initialized "); 1427c478bd9Sstevel@tonic-gate err_print("to do automatic repair.\n"); 1437c478bd9Sstevel@tonic-gate return (-1); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate /* 1467c478bd9Sstevel@tonic-gate * Define the bounds of the scan. 1477c478bd9Sstevel@tonic-gate */ 1487c478bd9Sstevel@tonic-gate if (scan_entire) { 1497c478bd9Sstevel@tonic-gate start = 0; 1507c478bd9Sstevel@tonic-gate if (cur_label == L_TYPE_SOLARIS) { 1517c478bd9Sstevel@tonic-gate if (cur_ctype->ctype_flags & CF_SCSI) 1527c478bd9Sstevel@tonic-gate end = datasects() - 1; 1537c478bd9Sstevel@tonic-gate else 1547c478bd9Sstevel@tonic-gate end = physsects() - 1; 1557c478bd9Sstevel@tonic-gate } else if (cur_label == L_TYPE_EFI) { 1567c478bd9Sstevel@tonic-gate end = cur_parts->etoc->efi_last_lba; 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate } else { 1597c478bd9Sstevel@tonic-gate start = scan_lower; 1607c478bd9Sstevel@tonic-gate end = scan_upper; 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate /* 1637c478bd9Sstevel@tonic-gate * Make sure the user knows if we are scanning over a mounted 1647c478bd9Sstevel@tonic-gate * partition. 1657c478bd9Sstevel@tonic-gate */ 1667c478bd9Sstevel@tonic-gate if ((flags & (SCAN_PATTERN | SCAN_WRITE)) && 1677c478bd9Sstevel@tonic-gate (checkmount(start, end))) { 1687c478bd9Sstevel@tonic-gate err_print("Cannot do analysis on a mounted partition.\n"); 1697c478bd9Sstevel@tonic-gate return (-1); 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate /* 1737c478bd9Sstevel@tonic-gate * Make sure the user knows if we are scanning over a 1747c478bd9Sstevel@tonic-gate * partition being used for swapping. 1757c478bd9Sstevel@tonic-gate */ 1767c478bd9Sstevel@tonic-gate if ((flags & (SCAN_PATTERN | SCAN_WRITE)) && 1777c478bd9Sstevel@tonic-gate (checkswap(start, end))) { 1787c478bd9Sstevel@tonic-gate err_print("Cannot do analysis on a partition \ 1797c478bd9Sstevel@tonic-gate which is currently being used for swapping.\n"); 1807c478bd9Sstevel@tonic-gate return (-1); 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate /* 1847c478bd9Sstevel@tonic-gate * If we are scanning destructively over certain sectors, 1857c478bd9Sstevel@tonic-gate * we mark the defect list and/or label dirty so it will get rewritten. 1867c478bd9Sstevel@tonic-gate */ 1877c478bd9Sstevel@tonic-gate if (flags & (SCAN_PATTERN | SCAN_WRITE)) { 1887c478bd9Sstevel@tonic-gate if (cur_label == L_TYPE_SOLARIS) { 1897c478bd9Sstevel@tonic-gate if (start < (daddr_t)totalsects() && 1907c478bd9Sstevel@tonic-gate end >= (daddr_t)datasects()) { 1917c478bd9Sstevel@tonic-gate if (!EMBEDDED_SCSI) { 1927c478bd9Sstevel@tonic-gate cur_list.flags |= LIST_DIRTY; 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate if (cur_disk->disk_flags & DSK_LABEL) 1957c478bd9Sstevel@tonic-gate cur_flags |= LABEL_DIRTY; 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate if (start == 0) { 1997c478bd9Sstevel@tonic-gate if (cur_disk->disk_flags & DSK_LABEL) 2007c478bd9Sstevel@tonic-gate cur_flags |= LABEL_DIRTY; 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate /* 2047c478bd9Sstevel@tonic-gate * Initialize the summary info on sectors repaired. 2057c478bd9Sstevel@tonic-gate */ 2067c478bd9Sstevel@tonic-gate scan_blocks_fixed = 0; 2077c478bd9Sstevel@tonic-gate /* 2087c478bd9Sstevel@tonic-gate * Loop through the passes of the scan. If required, loop forever. 2097c478bd9Sstevel@tonic-gate */ 2107c478bd9Sstevel@tonic-gate for (pass = 0; pass < scan_passes || scan_loop; pass++) { 2117c478bd9Sstevel@tonic-gate /* 2127c478bd9Sstevel@tonic-gate * Determine the data pattern to use if pattern testing 2137c478bd9Sstevel@tonic-gate * is to be done. 2147c478bd9Sstevel@tonic-gate */ 2157c478bd9Sstevel@tonic-gate if (flags & SCAN_PATTERN) { 2167c478bd9Sstevel@tonic-gate if (scan_random) 2177c478bd9Sstevel@tonic-gate data = (int)mrand48(); 2187c478bd9Sstevel@tonic-gate else 2197c478bd9Sstevel@tonic-gate data = scan_patterns[pass % NPPATTERNS]; 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate if (flags & SCAN_PURGE) { 2227c478bd9Sstevel@tonic-gate flags &= ~(SCAN_PURGE_READ_PASS 2237c478bd9Sstevel@tonic-gate | SCAN_PURGE_ALPHA_PASS); 2247c478bd9Sstevel@tonic-gate switch (pattern % (NPPATTERNS + 1)) { 2257c478bd9Sstevel@tonic-gate case NPPATTERNS: 2267c478bd9Sstevel@tonic-gate pattern = 0; 2277c478bd9Sstevel@tonic-gate if (!error) { 2287c478bd9Sstevel@tonic-gate fmt_print( 2297c478bd9Sstevel@tonic-gate "\nThe last %d passes were successful, running alpha pattern pass", NPPATTERNS); 2307c478bd9Sstevel@tonic-gate flags |= SCAN_PURGE_ALPHA_PASS; 2317c478bd9Sstevel@tonic-gate data = alpha_pattern; 2327c478bd9Sstevel@tonic-gate } else { 2337c478bd9Sstevel@tonic-gate data = purge_patterns[pattern]; 2347c478bd9Sstevel@tonic-gate pattern++; 2357c478bd9Sstevel@tonic-gate }; 2367c478bd9Sstevel@tonic-gate break; 2377c478bd9Sstevel@tonic-gate case READPATTERN: 2387c478bd9Sstevel@tonic-gate flags |= SCAN_PURGE_READ_PASS; 2397c478bd9Sstevel@tonic-gate default: 2407c478bd9Sstevel@tonic-gate data = purge_patterns[pattern]; 2417c478bd9Sstevel@tonic-gate pattern++; 2427c478bd9Sstevel@tonic-gate break; 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate fmt_print("\n pass %d", pass); 2467c478bd9Sstevel@tonic-gate fmt_print(" - pattern = 0x%x", data); 2477c478bd9Sstevel@tonic-gate } else 2487c478bd9Sstevel@tonic-gate fmt_print("\n pass %d", pass); 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate fmt_print("\n"); 2517c478bd9Sstevel@tonic-gate /* 2527c478bd9Sstevel@tonic-gate * Mark the pattern buffer as corrupt, since it 2537c478bd9Sstevel@tonic-gate * hasn't been initialized. 2547c478bd9Sstevel@tonic-gate */ 2557c478bd9Sstevel@tonic-gate needinit = 1; 2567c478bd9Sstevel@tonic-gate /* 2577c478bd9Sstevel@tonic-gate * Print the first block number to the log file if 2587c478bd9Sstevel@tonic-gate * logging is on so there is some record of what 2597c478bd9Sstevel@tonic-gate * analysis was performed. 2607c478bd9Sstevel@tonic-gate */ 2617c478bd9Sstevel@tonic-gate if (log_file) { 2627c478bd9Sstevel@tonic-gate pr_dblock(log_print, start); 2637c478bd9Sstevel@tonic-gate log_print("\n"); 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate /* 2667c478bd9Sstevel@tonic-gate * Loop through this pass, each time analyzing an amount 2677c478bd9Sstevel@tonic-gate * specified by the global parameters. 2687c478bd9Sstevel@tonic-gate */ 2697c478bd9Sstevel@tonic-gate xfercnt = 0; 2707c478bd9Sstevel@tonic-gate for (curnt = start; curnt <= end; curnt += size) { 2717c478bd9Sstevel@tonic-gate if ((end - curnt) < scan_size) 2727c478bd9Sstevel@tonic-gate size = end - curnt + 1; 2737c478bd9Sstevel@tonic-gate else 2747c478bd9Sstevel@tonic-gate size = scan_size; 2757c478bd9Sstevel@tonic-gate /* 2767c478bd9Sstevel@tonic-gate * Print out where we are, so we don't look dead. 2777c478bd9Sstevel@tonic-gate * Also store it in summary info for logging. 2787c478bd9Sstevel@tonic-gate */ 2797c478bd9Sstevel@tonic-gate scan_cur_block = curnt; 2807c478bd9Sstevel@tonic-gate nolog_print(" "); 2817c478bd9Sstevel@tonic-gate pr_dblock(nolog_print, curnt); 2827c478bd9Sstevel@tonic-gate nolog_print(" \015"); 2837c478bd9Sstevel@tonic-gate (void) fflush(stdout); 2847c478bd9Sstevel@tonic-gate disk_error = 0; 2857c478bd9Sstevel@tonic-gate /* 2867c478bd9Sstevel@tonic-gate * Do the actual analysis. 2877c478bd9Sstevel@tonic-gate */ 2887c478bd9Sstevel@tonic-gate status = analyze_blocks(flags, (daddr_t)curnt, size, 2897c478bd9Sstevel@tonic-gate (unsigned)data, needinit, (F_ALLERRS | F_SILENT), 2907c478bd9Sstevel@tonic-gate &xfercnt); 2917c478bd9Sstevel@tonic-gate /* 2927c478bd9Sstevel@tonic-gate * If there were no errors, the pattern buffer is 2937c478bd9Sstevel@tonic-gate * still initialized, and we just loop to next chunk. 2947c478bd9Sstevel@tonic-gate */ 2957c478bd9Sstevel@tonic-gate needinit = 0; 2967c478bd9Sstevel@tonic-gate if (!status) 2977c478bd9Sstevel@tonic-gate continue; 2987c478bd9Sstevel@tonic-gate /* 2997c478bd9Sstevel@tonic-gate * There was an error. Check if surface analysis 3007c478bd9Sstevel@tonic-gate * can be continued. 3017c478bd9Sstevel@tonic-gate */ 3027c478bd9Sstevel@tonic-gate if (handle_error_conditions()) { 3037c478bd9Sstevel@tonic-gate scan_blocks_fixed = scan_cur_block = -1; 3047c478bd9Sstevel@tonic-gate return (-1); 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate /* 3077c478bd9Sstevel@tonic-gate * There was an error. Mark the pattern buffer 3087c478bd9Sstevel@tonic-gate * corrupt so it will get reinitialized. 3097c478bd9Sstevel@tonic-gate */ 3107c478bd9Sstevel@tonic-gate needinit = 1; 3117c478bd9Sstevel@tonic-gate /* 3127c478bd9Sstevel@tonic-gate * If it was not a media error, ignore it. 3137c478bd9Sstevel@tonic-gate */ 3147c478bd9Sstevel@tonic-gate if (!media_error) 3157c478bd9Sstevel@tonic-gate continue; 3167c478bd9Sstevel@tonic-gate /* 3177c478bd9Sstevel@tonic-gate * Loop 5 times through each sector of the chunk, 3187c478bd9Sstevel@tonic-gate * analyzing them individually. 3197c478bd9Sstevel@tonic-gate */ 3207c478bd9Sstevel@tonic-gate nolog_print(" "); 3217c478bd9Sstevel@tonic-gate pr_dblock(nolog_print, curnt); 3227c478bd9Sstevel@tonic-gate nolog_print(" \015"); 3237c478bd9Sstevel@tonic-gate (void) fflush(stdout); 3247c478bd9Sstevel@tonic-gate founderr = 0; 3257c478bd9Sstevel@tonic-gate for (j = 0; j < size * 5; j++) { 3267c478bd9Sstevel@tonic-gate i = j % size; 3277c478bd9Sstevel@tonic-gate disk_error = 0; 3287c478bd9Sstevel@tonic-gate status = analyze_blocks(flags, (daddr_t) 3297c478bd9Sstevel@tonic-gate (curnt + i), 1, (unsigned)data, needinit, 3307c478bd9Sstevel@tonic-gate F_ALLERRS, NULL); 3317c478bd9Sstevel@tonic-gate needinit = 0; 3327c478bd9Sstevel@tonic-gate if (!status) 3337c478bd9Sstevel@tonic-gate continue; 3347c478bd9Sstevel@tonic-gate /* 3357c478bd9Sstevel@tonic-gate * There was an error. Check if surface analysis 3367c478bd9Sstevel@tonic-gate * can be continued. 3377c478bd9Sstevel@tonic-gate */ 3387c478bd9Sstevel@tonic-gate if (handle_error_conditions()) { 3397c478bd9Sstevel@tonic-gate scan_blocks_fixed = scan_cur_block = -1; 3407c478bd9Sstevel@tonic-gate return (-1); 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate /* 3437c478bd9Sstevel@tonic-gate * An error occurred. Mark the buffer 3447c478bd9Sstevel@tonic-gate * corrupt and see if it was media 3457c478bd9Sstevel@tonic-gate * related. 3467c478bd9Sstevel@tonic-gate */ 3477c478bd9Sstevel@tonic-gate needinit = 1; 3487c478bd9Sstevel@tonic-gate if (!media_error) 3497c478bd9Sstevel@tonic-gate continue; 3507c478bd9Sstevel@tonic-gate /* 3517c478bd9Sstevel@tonic-gate * We found a bad sector. Print out a message 3527c478bd9Sstevel@tonic-gate * and fix it if required. 3537c478bd9Sstevel@tonic-gate */ 3547c478bd9Sstevel@tonic-gate founderr = 1; 3557c478bd9Sstevel@tonic-gate if (scan_correct && (flags != SCAN_VALID)) { 3567c478bd9Sstevel@tonic-gate if (scan_repair(curnt+i, mode)) { 3577c478bd9Sstevel@tonic-gate error = -1; 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate } else 3607c478bd9Sstevel@tonic-gate err_print("\n"); 3617c478bd9Sstevel@tonic-gate /* 3627c478bd9Sstevel@tonic-gate * Stop after the error if required. 3637c478bd9Sstevel@tonic-gate */ 3647c478bd9Sstevel@tonic-gate if (scan_stop) 3657c478bd9Sstevel@tonic-gate goto out; 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate /* 3687c478bd9Sstevel@tonic-gate * Mark the pattern buffer corrupt to be safe. 3697c478bd9Sstevel@tonic-gate */ 3707c478bd9Sstevel@tonic-gate needinit = 1; 3717c478bd9Sstevel@tonic-gate /* 3727c478bd9Sstevel@tonic-gate * We didn't find an individual sector that was bad. 3737c478bd9Sstevel@tonic-gate * Print out a warning. 3747c478bd9Sstevel@tonic-gate */ 3757c478bd9Sstevel@tonic-gate if (!founderr) { 3767c478bd9Sstevel@tonic-gate err_print("Warning: unable to pinpoint "); 3777c478bd9Sstevel@tonic-gate err_print("defective block.\n"); 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate /* 3817c478bd9Sstevel@tonic-gate * Print the end of each pass to the log file. 3827c478bd9Sstevel@tonic-gate */ 3837c478bd9Sstevel@tonic-gate enter_critical(); 3847c478bd9Sstevel@tonic-gate if (log_file) { 3857c478bd9Sstevel@tonic-gate pr_dblock(log_print, scan_cur_block); 3867c478bd9Sstevel@tonic-gate log_print("\n"); 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate scan_cur_block = -1; 3897c478bd9Sstevel@tonic-gate exit_critical(); 3907c478bd9Sstevel@tonic-gate fmt_print("\n"); 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate /* 3937c478bd9Sstevel@tonic-gate * alternate the read and write for SCAN_VERIFY test 3947c478bd9Sstevel@tonic-gate */ 3957c478bd9Sstevel@tonic-gate if (flags & SCAN_VERIFY) { 3967c478bd9Sstevel@tonic-gate flags ^= SCAN_VERIFY_READ_PASS; 3977c478bd9Sstevel@tonic-gate } 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate out: 4007c478bd9Sstevel@tonic-gate /* 4017c478bd9Sstevel@tonic-gate * We got here either by giving up after an error or falling 4027c478bd9Sstevel@tonic-gate * through after all passes were completed. 4037c478bd9Sstevel@tonic-gate */ 4047c478bd9Sstevel@tonic-gate fmt_print("\n"); 4057c478bd9Sstevel@tonic-gate enter_critical(); 4067c478bd9Sstevel@tonic-gate /* 4077c478bd9Sstevel@tonic-gate * If the defect list is dirty, write it to disk, 4087c478bd9Sstevel@tonic-gate * if scan_restore_defects (the default) is true. 4097c478bd9Sstevel@tonic-gate */ 4107c478bd9Sstevel@tonic-gate if (!EMBEDDED_SCSI && (cur_list.flags & LIST_DIRTY) && 4117c478bd9Sstevel@tonic-gate (scan_restore_defects)) { 4127c478bd9Sstevel@tonic-gate cur_list.flags = 0; 4137c478bd9Sstevel@tonic-gate write_deflist(&cur_list); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate /* 4167c478bd9Sstevel@tonic-gate * If the label is dirty, write it to disk. 4177c478bd9Sstevel@tonic-gate * if scan_restore_label (the default) is true. 4187c478bd9Sstevel@tonic-gate */ 4197c478bd9Sstevel@tonic-gate if ((cur_flags & LABEL_DIRTY) && (scan_restore_label)) { 4207c478bd9Sstevel@tonic-gate cur_flags &= ~LABEL_DIRTY; 4217c478bd9Sstevel@tonic-gate (void) write_label(); 4227c478bd9Sstevel@tonic-gate } 4237c478bd9Sstevel@tonic-gate /* 4247c478bd9Sstevel@tonic-gate * If we dropped down to here after an error, we need to write 4257c478bd9Sstevel@tonic-gate * the final block number to the log file for record keeping. 4267c478bd9Sstevel@tonic-gate */ 4277c478bd9Sstevel@tonic-gate if (log_file && scan_cur_block >= 0) { 4287c478bd9Sstevel@tonic-gate pr_dblock(log_print, scan_cur_block); 4297c478bd9Sstevel@tonic-gate log_print("\n"); 4307c478bd9Sstevel@tonic-gate } 4317c478bd9Sstevel@tonic-gate fmt_print("Total of %lld defective blocks repaired.\n", 4327c478bd9Sstevel@tonic-gate scan_blocks_fixed); 4337c478bd9Sstevel@tonic-gate /* 4347c478bd9Sstevel@tonic-gate * Reinitialize the logging variables so they don't get used 4357c478bd9Sstevel@tonic-gate * when they are not really valid. 4367c478bd9Sstevel@tonic-gate */ 4377c478bd9Sstevel@tonic-gate scan_blocks_fixed = scan_cur_block = -1; 4387c478bd9Sstevel@tonic-gate exit_critical(); 4397c478bd9Sstevel@tonic-gate return (error); 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate /* 4447c478bd9Sstevel@tonic-gate * This routine is called to repair a bad block discovered 4457c478bd9Sstevel@tonic-gate * during a scan operation. Return 0 for success, 1 for failure. 4467c478bd9Sstevel@tonic-gate * (This has been extracted out of do_scan(), to simplify it.) 4477c478bd9Sstevel@tonic-gate */ 4487c478bd9Sstevel@tonic-gate static int 4497c478bd9Sstevel@tonic-gate scan_repair(bn, mode) 4507c478bd9Sstevel@tonic-gate diskaddr_t bn; 4517c478bd9Sstevel@tonic-gate int mode; 4527c478bd9Sstevel@tonic-gate { 4537c478bd9Sstevel@tonic-gate int status; 4547c478bd9Sstevel@tonic-gate int result = 1; 4557c478bd9Sstevel@tonic-gate char buf[SECSIZE]; 4567c478bd9Sstevel@tonic-gate int buf_is_good; 4577c478bd9Sstevel@tonic-gate int i; 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate if (cur_ops->op_repair == NULL) { 4607c478bd9Sstevel@tonic-gate err_print("Warning: Controller does "); 4617c478bd9Sstevel@tonic-gate err_print("not support repairing.\n\n"); 4627c478bd9Sstevel@tonic-gate return (result); 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate enter_critical(); 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate /* 4687c478bd9Sstevel@tonic-gate * Determine if the error appears to be hard or soft. We 4697c478bd9Sstevel@tonic-gate * already assume there's an error. If we can get any 4707c478bd9Sstevel@tonic-gate * good data out of the sector, write that data back 4717c478bd9Sstevel@tonic-gate * after the repair. 4727c478bd9Sstevel@tonic-gate */ 4737c478bd9Sstevel@tonic-gate buf_is_good = 0; 4747c478bd9Sstevel@tonic-gate for (i = 0; i < 5; i++) { 4757c478bd9Sstevel@tonic-gate status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn, 1, 4767c478bd9Sstevel@tonic-gate buf, F_SILENT, NULL); 4777c478bd9Sstevel@tonic-gate if (status == 0) { 4787c478bd9Sstevel@tonic-gate buf_is_good = 1; 4797c478bd9Sstevel@tonic-gate break; 4807c478bd9Sstevel@tonic-gate } 4817c478bd9Sstevel@tonic-gate } 4827c478bd9Sstevel@tonic-gate 4837c478bd9Sstevel@tonic-gate fmt_print("Repairing %s error on %llu (", 4847c478bd9Sstevel@tonic-gate buf_is_good ? "soft" : "hard", bn); 4857c478bd9Sstevel@tonic-gate pr_dblock(fmt_print, bn); 4867c478bd9Sstevel@tonic-gate fmt_print(")..."); 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate status = (*cur_ops->op_repair)(bn, mode); 4897c478bd9Sstevel@tonic-gate if (status) { 4907c478bd9Sstevel@tonic-gate /* 4917c478bd9Sstevel@tonic-gate * If the repair failed, we note it and will return the 4927c478bd9Sstevel@tonic-gate * failure. However, the analysis goes on. 4937c478bd9Sstevel@tonic-gate */ 4947c478bd9Sstevel@tonic-gate fmt_print("failed.\n\n"); 4957c478bd9Sstevel@tonic-gate } else { 4967c478bd9Sstevel@tonic-gate /* 4977c478bd9Sstevel@tonic-gate * The repair worked. Write the good data we could 4987c478bd9Sstevel@tonic-gate * recover from the failed block, if possible. 4997c478bd9Sstevel@tonic-gate * If not, zero the block. In doing so, try to 5007c478bd9Sstevel@tonic-gate * determine if the new block appears ok. 5017c478bd9Sstevel@tonic-gate */ 5027c478bd9Sstevel@tonic-gate if (!buf_is_good) { 5037c478bd9Sstevel@tonic-gate bzero(buf, SECSIZE); 5047c478bd9Sstevel@tonic-gate fmt_print("Warning: Block %llu zero-filled.\n", bn); 5057c478bd9Sstevel@tonic-gate } else { 5067c478bd9Sstevel@tonic-gate fmt_print("ok.\n"); 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, bn, 5097c478bd9Sstevel@tonic-gate 1, buf, (F_SILENT | F_ALLERRS), NULL); 5107c478bd9Sstevel@tonic-gate if (status == 0) { 5117c478bd9Sstevel@tonic-gate status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn, 5127c478bd9Sstevel@tonic-gate 1, buf, (F_SILENT | F_ALLERRS), NULL); 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate if (status) { 5157c478bd9Sstevel@tonic-gate fmt_print("The new block also appears defective.\n"); 5167c478bd9Sstevel@tonic-gate } 5177c478bd9Sstevel@tonic-gate fmt_print("\n"); 5187c478bd9Sstevel@tonic-gate /* 5197c478bd9Sstevel@tonic-gate * add the defect to the list and write the list out. 5207c478bd9Sstevel@tonic-gate * Also, kill the working list so it will get resynced 5217c478bd9Sstevel@tonic-gate * with the current list. 5227c478bd9Sstevel@tonic-gate * 5237c478bd9Sstevel@tonic-gate * For embedded scsi, we don't require a defect list. 5247c478bd9Sstevel@tonic-gate * However, if we have one, add the defect if the 5257c478bd9Sstevel@tonic-gate * list includes the grown list. If not, kill it 5267c478bd9Sstevel@tonic-gate * to force a resync if we need the list later. 5277c478bd9Sstevel@tonic-gate */ 5287c478bd9Sstevel@tonic-gate if (EMBEDDED_SCSI) { 5297c478bd9Sstevel@tonic-gate if (cur_list.list != NULL) { 5307c478bd9Sstevel@tonic-gate if (cur_list.flags & LIST_PGLIST) { 5317c478bd9Sstevel@tonic-gate add_ldef(bn, &cur_list); 5327c478bd9Sstevel@tonic-gate } else { 5337c478bd9Sstevel@tonic-gate kill_deflist(&cur_list); 5347c478bd9Sstevel@tonic-gate } 5357c478bd9Sstevel@tonic-gate } 5367c478bd9Sstevel@tonic-gate /* 5377c478bd9Sstevel@tonic-gate * The next "if" statement reflects the fix for 5387c478bd9Sstevel@tonic-gate * bug id 1026096 where format keeps adding the 5397c478bd9Sstevel@tonic-gate * same defect to the defect list. 5407c478bd9Sstevel@tonic-gate */ 5417c478bd9Sstevel@tonic-gate } else if (cur_ctype->ctype_flags & CF_WLIST) { 5427c478bd9Sstevel@tonic-gate kill_deflist(&cur_list); 5437c478bd9Sstevel@tonic-gate (*cur_ops->op_ex_cur)(&cur_list); 5447c478bd9Sstevel@tonic-gate fmt_print("Current list updated\n"); 5457c478bd9Sstevel@tonic-gate } else { 5467c478bd9Sstevel@tonic-gate add_ldef(bn, &cur_list); 5477c478bd9Sstevel@tonic-gate write_deflist(&cur_list); 5487c478bd9Sstevel@tonic-gate } 5497c478bd9Sstevel@tonic-gate kill_deflist(&work_list); 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate /* Log the repair. */ 5527c478bd9Sstevel@tonic-gate scan_blocks_fixed++; 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate /* return ok */ 5557c478bd9Sstevel@tonic-gate result = 0; 5567c478bd9Sstevel@tonic-gate } 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate exit_critical(); 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate return (result); 5617c478bd9Sstevel@tonic-gate } 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate /* 5657c478bd9Sstevel@tonic-gate * This routine analyzes a set of sectors on the disk. It simply returns 5667c478bd9Sstevel@tonic-gate * an error if a defect is found. It is called by do_scan(). 5677c478bd9Sstevel@tonic-gate */ 5687c478bd9Sstevel@tonic-gate static int 5697c478bd9Sstevel@tonic-gate analyze_blocks(flags, blkno, blkcnt, data, init, driver_flags, xfercntp) 5707c478bd9Sstevel@tonic-gate int flags, driver_flags, blkcnt, init; 5717c478bd9Sstevel@tonic-gate register unsigned data; 5727c478bd9Sstevel@tonic-gate diskaddr_t blkno; 5737c478bd9Sstevel@tonic-gate int *xfercntp; 5747c478bd9Sstevel@tonic-gate { 5757c478bd9Sstevel@tonic-gate int corrupt = 0; 5767c478bd9Sstevel@tonic-gate register int status, i, nints; 5777c478bd9Sstevel@tonic-gate register unsigned *ptr = (uint_t *)pattern_buf; 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate media_error = 0; 5807c478bd9Sstevel@tonic-gate if (flags & SCAN_VERIFY) { 5817c478bd9Sstevel@tonic-gate return (verify_blocks(flags, blkno, blkcnt, data, 5827c478bd9Sstevel@tonic-gate driver_flags, xfercntp)); 5837c478bd9Sstevel@tonic-gate } 5847c478bd9Sstevel@tonic-gate 5857c478bd9Sstevel@tonic-gate /* 5867c478bd9Sstevel@tonic-gate * Initialize the pattern buffer if necessary. 5877c478bd9Sstevel@tonic-gate */ 5887c478bd9Sstevel@tonic-gate nints = blkcnt * SECSIZE / sizeof (int); 5897c478bd9Sstevel@tonic-gate if ((flags & SCAN_PATTERN) && init) { 5907c478bd9Sstevel@tonic-gate for (i = 0; i < nints; i++) 5917c478bd9Sstevel@tonic-gate *((int *)((int *)pattern_buf + i)) = data; 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate /* 5947c478bd9Sstevel@tonic-gate * Lock out interrupts so we can insure valid data will get 5957c478bd9Sstevel@tonic-gate * restored. This is necessary because there are modes 5967c478bd9Sstevel@tonic-gate * of scanning that corrupt the disk data then restore it at 5977c478bd9Sstevel@tonic-gate * the end of the analysis. 5987c478bd9Sstevel@tonic-gate */ 5997c478bd9Sstevel@tonic-gate enter_critical(); 6007c478bd9Sstevel@tonic-gate /* 6017c478bd9Sstevel@tonic-gate * If the disk data is valid, read it into the data buffer. 6027c478bd9Sstevel@tonic-gate */ 6037c478bd9Sstevel@tonic-gate if (flags & SCAN_VALID) { 6047c478bd9Sstevel@tonic-gate status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno, 6057c478bd9Sstevel@tonic-gate blkcnt, (caddr_t)cur_buf, driver_flags, xfercntp); 6067c478bd9Sstevel@tonic-gate if (status) 6077c478bd9Sstevel@tonic-gate goto bad; 6087c478bd9Sstevel@tonic-gate } 6097c478bd9Sstevel@tonic-gate /* 6107c478bd9Sstevel@tonic-gate * If we are doing pattern testing, write and read the pattern 6117c478bd9Sstevel@tonic-gate * from the pattern buffer. 6127c478bd9Sstevel@tonic-gate */ 6137c478bd9Sstevel@tonic-gate if (flags & SCAN_PATTERN) { 6147c478bd9Sstevel@tonic-gate /* 6157c478bd9Sstevel@tonic-gate * If the disk data was valid, mark it corrupt so we know 6167c478bd9Sstevel@tonic-gate * to restore it later. 6177c478bd9Sstevel@tonic-gate */ 6187c478bd9Sstevel@tonic-gate if (flags & SCAN_VALID) 6197c478bd9Sstevel@tonic-gate corrupt++; 6207c478bd9Sstevel@tonic-gate /* 6217c478bd9Sstevel@tonic-gate * Only write if we're not on the read pass of SCAN_PURGE. 6227c478bd9Sstevel@tonic-gate */ 6237c478bd9Sstevel@tonic-gate if (!(flags & SCAN_PURGE_READ_PASS)) { 6247c478bd9Sstevel@tonic-gate status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno, 6257c478bd9Sstevel@tonic-gate blkcnt, (caddr_t)pattern_buf, driver_flags, 6267c478bd9Sstevel@tonic-gate xfercntp); 6277c478bd9Sstevel@tonic-gate if (status) 6287c478bd9Sstevel@tonic-gate goto bad; 6297c478bd9Sstevel@tonic-gate } 6307c478bd9Sstevel@tonic-gate /* 6317c478bd9Sstevel@tonic-gate * Only read if we are on the read pass of SCAN_PURGE, if we 6327c478bd9Sstevel@tonic-gate * are purging. 6337c478bd9Sstevel@tonic-gate */ 6347c478bd9Sstevel@tonic-gate if ((!(flags & SCAN_PURGE)) || (flags & SCAN_PURGE_READ_PASS)) { 6357c478bd9Sstevel@tonic-gate status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno, 6367c478bd9Sstevel@tonic-gate blkcnt, (caddr_t)pattern_buf, driver_flags, 6377c478bd9Sstevel@tonic-gate xfercntp); 6387c478bd9Sstevel@tonic-gate if (status) 6397c478bd9Sstevel@tonic-gate goto bad; 6407c478bd9Sstevel@tonic-gate } 6417c478bd9Sstevel@tonic-gate } 6427c478bd9Sstevel@tonic-gate /* 6437c478bd9Sstevel@tonic-gate * If we are doing a data compare, make sure the pattern 6447c478bd9Sstevel@tonic-gate * came back intact. 6457c478bd9Sstevel@tonic-gate * Only compare if we are on the read pass of SCAN_PURGE, or 6467c478bd9Sstevel@tonic-gate * we wrote random data instead of the expected data pattern. 6477c478bd9Sstevel@tonic-gate */ 6487c478bd9Sstevel@tonic-gate if ((flags & SCAN_COMPARE) || (flags & SCAN_PURGE_READ_PASS)) { 6497c478bd9Sstevel@tonic-gate for (i = nints, ptr = (uint_t *)pattern_buf; i; i--) 6507c478bd9Sstevel@tonic-gate if (*ptr++ != data) { 6517c478bd9Sstevel@tonic-gate err_print("Data miscompare error (expecting "); 6527c478bd9Sstevel@tonic-gate err_print("0x%x, got 0x%x) at ", data, 6537c478bd9Sstevel@tonic-gate *((int *)((int *)pattern_buf + 6547c478bd9Sstevel@tonic-gate (nints - i)))); 6557c478bd9Sstevel@tonic-gate pr_dblock(err_print, blkno); 6567c478bd9Sstevel@tonic-gate err_print(", offset = 0x%x.\n", 6577c478bd9Sstevel@tonic-gate (nints - i) * sizeof (int)); 6587c478bd9Sstevel@tonic-gate goto bad; 6597c478bd9Sstevel@tonic-gate } 6607c478bd9Sstevel@tonic-gate } 6617c478bd9Sstevel@tonic-gate /* 6627c478bd9Sstevel@tonic-gate * If we are supposed to write data out, do so. 6637c478bd9Sstevel@tonic-gate */ 6647c478bd9Sstevel@tonic-gate if (flags & SCAN_WRITE) { 6657c478bd9Sstevel@tonic-gate status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno, 6667c478bd9Sstevel@tonic-gate blkcnt, (caddr_t)cur_buf, driver_flags, xfercntp); 6677c478bd9Sstevel@tonic-gate if (status) 6687c478bd9Sstevel@tonic-gate goto bad; 6697c478bd9Sstevel@tonic-gate } 6707c478bd9Sstevel@tonic-gate exit_critical(); 6717c478bd9Sstevel@tonic-gate /* 6727c478bd9Sstevel@tonic-gate * No errors occurred, return ok. 6737c478bd9Sstevel@tonic-gate */ 6747c478bd9Sstevel@tonic-gate return (0); 6757c478bd9Sstevel@tonic-gate bad: 6767c478bd9Sstevel@tonic-gate /* 6777c478bd9Sstevel@tonic-gate * There was an error. If the data was corrupted, we write it 6787c478bd9Sstevel@tonic-gate * out from the data buffer to restore it. 6797c478bd9Sstevel@tonic-gate */ 6807c478bd9Sstevel@tonic-gate if (corrupt) { 6817c478bd9Sstevel@tonic-gate if ((*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno, 6827c478bd9Sstevel@tonic-gate blkcnt, (caddr_t)cur_buf, F_NORMAL, xfercntp)) 6837c478bd9Sstevel@tonic-gate err_print("Warning: unable to restore original data.\n"); 6847c478bd9Sstevel@tonic-gate } 6857c478bd9Sstevel@tonic-gate exit_critical(); 6867c478bd9Sstevel@tonic-gate /* 6877c478bd9Sstevel@tonic-gate * Return the error. 6887c478bd9Sstevel@tonic-gate */ 6897c478bd9Sstevel@tonic-gate return (-1); 6907c478bd9Sstevel@tonic-gate } 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate 6937c478bd9Sstevel@tonic-gate /* 6947c478bd9Sstevel@tonic-gate * This routine analyzes a set of sectors on the disk. It simply returns 6957c478bd9Sstevel@tonic-gate * an error if a defect is found. It is called by analyze_blocks(). 6967c478bd9Sstevel@tonic-gate * For simplicity, this is done as a separate function instead of 6977c478bd9Sstevel@tonic-gate * making the analyze_block routine complex. 6987c478bd9Sstevel@tonic-gate * 6997c478bd9Sstevel@tonic-gate * This routine implements the 'verify' command. It writes the disk 7007c478bd9Sstevel@tonic-gate * by writing unique data for each block; after the write pass, it 7017c478bd9Sstevel@tonic-gate * reads the data and verifies for correctness. Note that the entire 7027c478bd9Sstevel@tonic-gate * disk (or the range of disk) is fully written first and then read. 7037c478bd9Sstevel@tonic-gate * This should eliminate any caching effect on the drives. 7047c478bd9Sstevel@tonic-gate */ 7057c478bd9Sstevel@tonic-gate static int 7067c478bd9Sstevel@tonic-gate verify_blocks(int flags, 7077c478bd9Sstevel@tonic-gate diskaddr_t blkno, 7087c478bd9Sstevel@tonic-gate int blkcnt, 7097c478bd9Sstevel@tonic-gate unsigned data, 7107c478bd9Sstevel@tonic-gate int driver_flags, 7117c478bd9Sstevel@tonic-gate int *xfercntp) 7127c478bd9Sstevel@tonic-gate { 7137c478bd9Sstevel@tonic-gate int status, i, nints; 7147c478bd9Sstevel@tonic-gate unsigned *ptr = (uint_t *)pattern_buf; 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate nints = SECSIZE / sizeof (int); 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate /* 7197c478bd9Sstevel@tonic-gate * Initialize the pattern buffer if we are in write pass. 7207c478bd9Sstevel@tonic-gate * Use the block number itself as data, each block has unique 7217c478bd9Sstevel@tonic-gate * buffer data that way. 7227c478bd9Sstevel@tonic-gate */ 7237c478bd9Sstevel@tonic-gate if (!(flags & SCAN_VERIFY_READ_PASS)) { 7247c478bd9Sstevel@tonic-gate for (data = blkno; data < blkno + blkcnt; data++) { 7257c478bd9Sstevel@tonic-gate for (i = 0; i < nints; i++) { 7267c478bd9Sstevel@tonic-gate *ptr++ = data; 7277c478bd9Sstevel@tonic-gate } 7287c478bd9Sstevel@tonic-gate } 7297c478bd9Sstevel@tonic-gate ptr = (uint_t *)pattern_buf; 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate 7327c478bd9Sstevel@tonic-gate /* 7337c478bd9Sstevel@tonic-gate * Only write if we're not on the read pass of SCAN_VERIFY. 7347c478bd9Sstevel@tonic-gate */ 7357c478bd9Sstevel@tonic-gate if (!(flags & SCAN_VERIFY_READ_PASS)) { 7367c478bd9Sstevel@tonic-gate status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno, 7377c478bd9Sstevel@tonic-gate blkcnt, (caddr_t)pattern_buf, driver_flags, xfercntp); 7387c478bd9Sstevel@tonic-gate if (status) 7397c478bd9Sstevel@tonic-gate goto bad; 7407c478bd9Sstevel@tonic-gate } else { 7417c478bd9Sstevel@tonic-gate /* 7427c478bd9Sstevel@tonic-gate * Only read if we are on the read pass of SCAN_VERIFY 7437c478bd9Sstevel@tonic-gate */ 7447c478bd9Sstevel@tonic-gate status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno, 7457c478bd9Sstevel@tonic-gate blkcnt, (caddr_t)pattern_buf, driver_flags, xfercntp); 7467c478bd9Sstevel@tonic-gate if (status) 7477c478bd9Sstevel@tonic-gate goto bad; 7487c478bd9Sstevel@tonic-gate /* 7497c478bd9Sstevel@tonic-gate * compare and make sure the pattern came back intact. 7507c478bd9Sstevel@tonic-gate */ 7517c478bd9Sstevel@tonic-gate for (data = blkno; data < blkno + blkcnt; data++) { 7527c478bd9Sstevel@tonic-gate for (i = 0; i < nints; i++) { 7537c478bd9Sstevel@tonic-gate if (*ptr++ != data) { 7547c478bd9Sstevel@tonic-gate ptr--; 7557c478bd9Sstevel@tonic-gate err_print("Data miscompare error (expecting " 7567c478bd9Sstevel@tonic-gate "0x%x, got 0x%x) at ", data, *ptr); 7577c478bd9Sstevel@tonic-gate pr_dblock(err_print, blkno); 7587c478bd9Sstevel@tonic-gate err_print(", offset = 0x%x.\n", (ptr - 7597c478bd9Sstevel@tonic-gate (uint_t *)pattern_buf) * sizeof (int)); 7607c478bd9Sstevel@tonic-gate goto bad; 7617c478bd9Sstevel@tonic-gate } 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate } 7647c478bd9Sstevel@tonic-gate } 7657c478bd9Sstevel@tonic-gate /* 7667c478bd9Sstevel@tonic-gate * No errors occurred, return ok. 7677c478bd9Sstevel@tonic-gate */ 7687c478bd9Sstevel@tonic-gate return (0); 7697c478bd9Sstevel@tonic-gate bad: 7707c478bd9Sstevel@tonic-gate return (-1); 7717c478bd9Sstevel@tonic-gate } 7727c478bd9Sstevel@tonic-gate 7737c478bd9Sstevel@tonic-gate 7747c478bd9Sstevel@tonic-gate static int 7757c478bd9Sstevel@tonic-gate handle_error_conditions() 7767c478bd9Sstevel@tonic-gate { 7777c478bd9Sstevel@tonic-gate 7787c478bd9Sstevel@tonic-gate /* 7797c478bd9Sstevel@tonic-gate * Check if the errno is ENXIO. 7807c478bd9Sstevel@tonic-gate */ 7817c478bd9Sstevel@tonic-gate if (errno == ENXIO) { 7827c478bd9Sstevel@tonic-gate fmt_print("\n\nWarning:Cannot access drive, "); 7837c478bd9Sstevel@tonic-gate fmt_print("aborting surface analysis.\n"); 7847c478bd9Sstevel@tonic-gate return (-1); 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate /* 7877c478bd9Sstevel@tonic-gate * check for disk errors 7887c478bd9Sstevel@tonic-gate */ 7897c478bd9Sstevel@tonic-gate switch (disk_error) { 7907c478bd9Sstevel@tonic-gate case DISK_STAT_RESERVED: 7917c478bd9Sstevel@tonic-gate case DISK_STAT_UNAVAILABLE: 7927c478bd9Sstevel@tonic-gate fmt_print("\n\nWarning:Drive may be reserved "); 7937c478bd9Sstevel@tonic-gate fmt_print("or has been removed, "); 7947c478bd9Sstevel@tonic-gate fmt_print("aborting surface analysis.\n"); 7957c478bd9Sstevel@tonic-gate return (-1); 7967c478bd9Sstevel@tonic-gate case DISK_STAT_NOTREADY: 7977c478bd9Sstevel@tonic-gate fmt_print("\n\nWarning: Drive not ready, "); 7987c478bd9Sstevel@tonic-gate fmt_print("aborting surface analysis.\n"); 7997c478bd9Sstevel@tonic-gate return (-1); 8007c478bd9Sstevel@tonic-gate case DISK_STAT_DATA_PROTECT: 8017c478bd9Sstevel@tonic-gate fmt_print("\n\nWarning: Drive is write protected, "); 8027c478bd9Sstevel@tonic-gate fmt_print("aborting surface analysis.\n"); 8037c478bd9Sstevel@tonic-gate return (-1); 8047c478bd9Sstevel@tonic-gate default: 8057c478bd9Sstevel@tonic-gate break; 8067c478bd9Sstevel@tonic-gate } 8077c478bd9Sstevel@tonic-gate return (0); 8087c478bd9Sstevel@tonic-gate } 809