Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * badblocks.c - Bad blocks checker |
| 3 | * |
| 4 | * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr> |
| 5 | * Laboratoire MASI, Institut Blaise Pascal |
| 6 | * Universite Pierre et Marie Curie (Paris VI) |
| 7 | * |
Theodore Ts'o | dd018f5 | 2000-02-06 23:57:07 +0000 | [diff] [blame] | 8 | * Copyright 1995, 1996, 1997, 1998, 1999 by Theodore Ts'o |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 9 | * Copyright 1999 by David Beattie |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 10 | * |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 11 | * This file is based on the minix file system programs fsck and mkfs |
| 12 | * written and copyrighted by Linus Torvalds <Linus.Torvalds@cs.helsinki.fi> |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 13 | * |
| 14 | * %Begin-Header% |
| 15 | * This file may be redistributed under the terms of the GNU Public |
| 16 | * License. |
| 17 | * %End-Header% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * History: |
| 22 | * 93/05/26 - Creation from e2fsck |
| 23 | * 94/02/27 - Made a separate bad blocks checker |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 24 | * 99/06/30...99/07/26 - Added non-destructive write-testing, |
Theodore Ts'o | dd018f5 | 2000-02-06 23:57:07 +0000 | [diff] [blame] | 25 | * configurable blocks-at-once parameter, |
| 26 | * loading of badblocks list to avoid testing |
| 27 | * blocks known to be bad, multiple passes to |
| 28 | * make sure that no new blocks are added to the |
| 29 | * list. (Work done by David Beattie) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 32 | #define _GNU_SOURCE /* for O_DIRECT */ |
| 33 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 34 | #include <errno.h> |
| 35 | #include <fcntl.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 36 | #ifdef HAVE_GETOPT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 37 | #include <getopt.h> |
Theodore Ts'o | 373b833 | 2000-04-03 16:22:35 +0000 | [diff] [blame] | 38 | #else |
| 39 | extern char *optarg; |
| 40 | extern int optind; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 41 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 42 | #include <signal.h> |
| 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> |
| 45 | #include <string.h> |
| 46 | #include <unistd.h> |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 47 | #include <setjmp.h> |
Theodore Ts'o | 6d40f56 | 2003-05-07 08:35:38 -0400 | [diff] [blame] | 48 | #include <time.h> |
Theodore Ts'o | 5267a52 | 2007-06-04 01:49:51 -0400 | [diff] [blame] | 49 | #include <limits.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 50 | |
| 51 | #include <sys/ioctl.h> |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 52 | #include <sys/types.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 53 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 54 | #include "et/com_err.h" |
Theodore Ts'o | d40259f | 1997-10-20 00:44:26 +0000 | [diff] [blame] | 55 | #include "ext2fs/ext2_io.h" |
Theodore Ts'o | 54c637d | 2001-05-14 11:45:38 +0000 | [diff] [blame] | 56 | #include "ext2fs/ext2_fs.h" |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 57 | #include "ext2fs/ext2fs.h" |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 58 | #include "nls-enable.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 59 | |
| 60 | const char * program_name = "badblocks"; |
Theodore Ts'o | f63978a | 2006-05-13 09:25:47 -0400 | [diff] [blame] | 61 | const char * done_string = N_("done \n"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 62 | |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 63 | static int v_flag = 0; /* verbose */ |
| 64 | static int w_flag = 0; /* do r/w test: 0=no, 1=yes, |
| 65 | * 2=non-destructive */ |
| 66 | static int s_flag = 0; /* show progress of test */ |
Theodore Ts'o | 981dc56 | 2000-07-06 14:13:29 +0000 | [diff] [blame] | 67 | static int force = 0; /* force check of mounted device */ |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 68 | static int t_flag = 0; /* number of test patterns */ |
| 69 | static int t_max = 0; /* allocated test patterns */ |
Theodore Ts'o | e9860ae | 2007-10-22 09:51:50 -0400 | [diff] [blame] | 70 | static unsigned int *t_patts = NULL; /* test patterns */ |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 71 | static int current_O_DIRECT = 0; /* Current status of O_DIRECT flag */ |
Theodore Ts'o | f63978a | 2006-05-13 09:25:47 -0400 | [diff] [blame] | 72 | static int exclusive_ok = 0; |
Iustin Pop | 931b028 | 2008-06-11 13:12:17 +0200 | [diff] [blame^] | 73 | static unsigned int max_bb = 0; /* Abort test if more than this number of bad blocks has been encountered */ |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 74 | |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 75 | #define T_INC 32 |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 76 | |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 77 | unsigned int sys_page_size = 4096; |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 78 | |
Theodore Ts'o | 8820c79 | 2001-01-06 04:20:03 +0000 | [diff] [blame] | 79 | static void usage(void) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 80 | { |
Iustin Pop | 931b028 | 2008-06-11 13:12:17 +0200 | [diff] [blame^] | 81 | fprintf(stderr, _("Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwnf]\n [-c blocks_at_once] [-p num_passes] [-e max_bad_blocks] [-t test_pattern [-t test_pattern [...]]]\n device [last_block [start_block]]\n"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 82 | program_name); |
| 83 | exit (1); |
| 84 | } |
| 85 | |
Theodore Ts'o | d8b5f77 | 2006-11-12 23:09:03 -0500 | [diff] [blame] | 86 | static void exclusive_usage(void) |
| 87 | { |
| 88 | fprintf(stderr, |
Theodore Ts'o | 017a76e | 2006-11-17 23:00:19 -0500 | [diff] [blame] | 89 | _("%s: The -n and -w options are mutually exclusive.\n\n"), |
| 90 | program_name); |
| 91 | exit(1); |
Theodore Ts'o | d8b5f77 | 2006-11-12 23:09:03 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 94 | static blk_t currently_testing = 0; |
| 95 | static blk_t num_blocks = 0; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 96 | static ext2_badblocks_list bb_list = NULL; |
| 97 | static FILE *out; |
| 98 | static blk_t next_bad = 0; |
| 99 | static ext2_badblocks_iterate bb_iter = NULL; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 100 | |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 101 | static void *allocate_buffer(size_t size) |
| 102 | { |
| 103 | void *ret = 0; |
| 104 | |
| 105 | #ifdef HAVE_POSIX_MEMALIGN |
| 106 | if (posix_memalign(&ret, sys_page_size, size) < 0) |
| 107 | ret = 0; |
| 108 | #else |
| 109 | #ifdef HAVE_MEMALIGN |
| 110 | ret = memalign(sys_page_size, size); |
| 111 | #else |
| 112 | #ifdef HAVE_VALLOC |
| 113 | ret = valloc(size); |
| 114 | #endif /* HAVE_VALLOC */ |
| 115 | #endif /* HAVE_MEMALIGN */ |
| 116 | #endif /* HAVE_POSIX_MEMALIGN */ |
| 117 | |
| 118 | if (!ret) |
| 119 | ret = malloc(size); |
| 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
Theodore Ts'o | dd018f5 | 2000-02-06 23:57:07 +0000 | [diff] [blame] | 124 | /* |
| 125 | * This routine reports a new bad block. If the bad block has already |
| 126 | * been seen before, then it returns 0; otherwise it returns 1. |
| 127 | */ |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 128 | static int bb_output (blk_t bad) |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 129 | { |
| 130 | errcode_t errcode; |
| 131 | |
Theodore Ts'o | dd018f5 | 2000-02-06 23:57:07 +0000 | [diff] [blame] | 132 | if (ext2fs_badblocks_list_test(bb_list, bad)) |
| 133 | return 0; |
| 134 | |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 135 | fprintf(out, "%lu\n", (unsigned long) bad); |
Theodore Ts'o | cc4f98e | 2003-04-03 11:37:46 -0500 | [diff] [blame] | 136 | fflush(out); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 137 | |
| 138 | errcode = ext2fs_badblocks_list_add (bb_list, bad); |
| 139 | if (errcode) { |
| 140 | com_err (program_name, errcode, "adding to in-memory bad block list"); |
| 141 | exit (1); |
| 142 | } |
| 143 | |
| 144 | /* kludge: |
| 145 | increment the iteration through the bb_list if |
| 146 | an element was just added before the current iteration |
| 147 | position. This should not cause next_bad to change. */ |
| 148 | if (bb_iter && bad < next_bad) |
| 149 | ext2fs_badblocks_list_iterate (bb_iter, &next_bad); |
Theodore Ts'o | dd018f5 | 2000-02-06 23:57:07 +0000 | [diff] [blame] | 150 | return 1; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Theodore Ts'o | 8820c79 | 2001-01-06 04:20:03 +0000 | [diff] [blame] | 153 | static void print_status(void) |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 154 | { |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 155 | fprintf(stderr, "%15lu/%15lu", (unsigned long) currently_testing, |
| 156 | (unsigned long) num_blocks); |
Theodore Ts'o | c76564a | 2005-01-06 14:48:59 -0500 | [diff] [blame] | 157 | fputs("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", stderr); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 158 | fflush (stderr); |
| 159 | } |
| 160 | |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 161 | static void alarm_intr(int alnum EXT2FS_ATTR((unused))) |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 162 | { |
| 163 | signal (SIGALRM, alarm_intr); |
| 164 | alarm(1); |
| 165 | if (!num_blocks) |
| 166 | return; |
Theodore Ts'o | c76564a | 2005-01-06 14:48:59 -0500 | [diff] [blame] | 167 | print_status(); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 170 | static void *terminate_addr = NULL; |
| 171 | |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 172 | static void terminate_intr(int signo EXT2FS_ATTR((unused))) |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 173 | { |
| 174 | if (terminate_addr) |
| 175 | longjmp(terminate_addr,1); |
| 176 | exit(1); |
| 177 | } |
| 178 | |
Theodore Ts'o | 981dc56 | 2000-07-06 14:13:29 +0000 | [diff] [blame] | 179 | static void capture_terminate(jmp_buf term_addr) |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 180 | { |
| 181 | terminate_addr = term_addr; |
| 182 | signal (SIGHUP, terminate_intr); |
| 183 | signal (SIGINT, terminate_intr); |
| 184 | signal (SIGPIPE, terminate_intr); |
| 185 | signal (SIGTERM, terminate_intr); |
| 186 | signal (SIGUSR1, terminate_intr); |
| 187 | signal (SIGUSR2, terminate_intr); |
| 188 | } |
| 189 | |
Theodore Ts'o | 8820c79 | 2001-01-06 04:20:03 +0000 | [diff] [blame] | 190 | static void uncapture_terminate(void) |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 191 | { |
| 192 | terminate_addr = NULL; |
| 193 | signal (SIGHUP, SIG_DFL); |
| 194 | signal (SIGINT, SIG_DFL); |
| 195 | signal (SIGPIPE, SIG_DFL); |
| 196 | signal (SIGTERM, SIG_DFL); |
| 197 | signal (SIGUSR1, SIG_DFL); |
| 198 | signal (SIGUSR2, SIG_DFL); |
| 199 | } |
| 200 | |
Theodore Ts'o | 1f9a60c | 2003-08-01 00:58:00 -0400 | [diff] [blame] | 201 | static void set_o_direct(int dev, unsigned char *buffer, size_t size, |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 202 | blk_t current_block) |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 203 | { |
| 204 | #ifdef O_DIRECT |
| 205 | int new_flag = O_DIRECT; |
| 206 | int flag; |
| 207 | |
| 208 | if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) || |
Theodore Ts'o | 1f9a60c | 2003-08-01 00:58:00 -0400 | [diff] [blame] | 209 | ((size & (sys_page_size - 1)) != 0) || |
| 210 | ((current_block & ((sys_page_size >> 9)-1)) != 0)) |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 211 | new_flag = 0; |
| 212 | |
| 213 | if (new_flag != current_O_DIRECT) { |
Theodore Ts'o | dc05871 | 2003-07-25 07:39:33 -0400 | [diff] [blame] | 214 | /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */ |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 215 | flag = fcntl(dev, F_GETFL); |
| 216 | if (flag > 0) { |
| 217 | flag = (flag & ~O_DIRECT) | new_flag; |
| 218 | fcntl(dev, F_SETFL, flag); |
| 219 | } |
| 220 | current_O_DIRECT = new_flag; |
| 221 | } |
| 222 | #endif |
| 223 | } |
| 224 | |
| 225 | |
Theodore Ts'o | e9860ae | 2007-10-22 09:51:50 -0400 | [diff] [blame] | 226 | static void pattern_fill(unsigned char *buffer, unsigned int pattern, |
Theodore Ts'o | 84c0545 | 2003-05-18 01:11:52 -0400 | [diff] [blame] | 227 | size_t n) |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 228 | { |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 229 | unsigned int i, nb; |
Theodore Ts'o | 84c0545 | 2003-05-18 01:11:52 -0400 | [diff] [blame] | 230 | unsigned char bpattern[sizeof(pattern)], *ptr; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 231 | |
Theodore Ts'o | e9860ae | 2007-10-22 09:51:50 -0400 | [diff] [blame] | 232 | if (pattern == (unsigned int) ~0) { |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 233 | for (ptr = buffer; ptr < buffer + n; ptr++) { |
| 234 | (*ptr) = random() % (1 << (8 * sizeof(char))); |
| 235 | } |
| 236 | if (s_flag | v_flag) |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 237 | fputs(_("Testing with random pattern: "), stderr); |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 238 | } else { |
| 239 | bpattern[0] = 0; |
| 240 | for (i = 0; i < sizeof(bpattern); i++) { |
| 241 | if (pattern == 0) |
| 242 | break; |
| 243 | bpattern[i] = pattern & 0xFF; |
| 244 | pattern = pattern >> 8; |
| 245 | } |
| 246 | nb = i ? (i-1) : 0; |
| 247 | for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) { |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 248 | *ptr = bpattern[i]; |
| 249 | if (i == 0) |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 250 | i = nb; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 251 | else |
| 252 | i--; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 253 | } |
Theodore Ts'o | 84c0545 | 2003-05-18 01:11:52 -0400 | [diff] [blame] | 254 | if (s_flag | v_flag) { |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 255 | fputs(_("Testing with pattern 0x"), stderr); |
Theodore Ts'o | 84c0545 | 2003-05-18 01:11:52 -0400 | [diff] [blame] | 256 | for (i = 0; i <= nb; i++) |
| 257 | fprintf(stderr, "%02x", buffer[i]); |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 258 | fputs(": ", stderr); |
Theodore Ts'o | 84c0545 | 2003-05-18 01:11:52 -0400 | [diff] [blame] | 259 | } |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 263 | /* |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 264 | * Perform a read of a sequence of blocks; return the number of blocks |
| 265 | * successfully sequentially read. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 266 | */ |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 267 | static int do_read (int dev, unsigned char * buffer, int try, int block_size, |
| 268 | blk_t current_block) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 269 | { |
| 270 | long got; |
| 271 | |
Theodore Ts'o | 1f9a60c | 2003-08-01 00:58:00 -0400 | [diff] [blame] | 272 | set_o_direct(dev, buffer, try * block_size, current_block); |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 273 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 274 | if (v_flag > 1) |
| 275 | print_status(); |
| 276 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 277 | /* Seek to the correct loc. */ |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 278 | if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size, |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 279 | SEEK_SET) != (ext2_loff_t) current_block * block_size) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 280 | com_err (program_name, errno, _("during seek")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 281 | |
| 282 | /* Try the read */ |
| 283 | got = read (dev, buffer, try * block_size); |
| 284 | if (got < 0) |
| 285 | got = 0; |
Theodore Ts'o | 9f10a7b | 1999-07-16 10:41:36 +0000 | [diff] [blame] | 286 | if (got & 511) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 287 | fprintf(stderr, _("Weird value (%ld) in do_read\n"), got); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 288 | got /= block_size; |
| 289 | return got; |
| 290 | } |
| 291 | |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 292 | /* |
| 293 | * Perform a write of a sequence of blocks; return the number of blocks |
| 294 | * successfully sequentially written. |
| 295 | */ |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 296 | static int do_write(int dev, unsigned char * buffer, int try, int block_size, |
| 297 | unsigned long current_block) |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 298 | { |
| 299 | long got; |
| 300 | |
Theodore Ts'o | 1f9a60c | 2003-08-01 00:58:00 -0400 | [diff] [blame] | 301 | set_o_direct(dev, buffer, try * block_size, current_block); |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 302 | |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 303 | if (v_flag > 1) |
| 304 | print_status(); |
| 305 | |
| 306 | /* Seek to the correct loc. */ |
| 307 | if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size, |
| 308 | SEEK_SET) != (ext2_loff_t) current_block * block_size) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 309 | com_err (program_name, errno, _("during seek")); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 310 | |
| 311 | /* Try the write */ |
| 312 | got = write (dev, buffer, try * block_size); |
| 313 | if (got < 0) |
| 314 | got = 0; |
| 315 | if (got & 511) |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 316 | fprintf(stderr, "Weird value (%ld) in do_write\n", got); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 317 | got /= block_size; |
| 318 | return got; |
| 319 | } |
| 320 | |
| 321 | static int host_dev; |
| 322 | |
Theodore Ts'o | 4d40454 | 2001-01-11 16:04:59 +0000 | [diff] [blame] | 323 | static void flush_bufs(void) |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 324 | { |
Theodore Ts'o | 4d40454 | 2001-01-11 16:04:59 +0000 | [diff] [blame] | 325 | errcode_t retval; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 326 | |
Theodore Ts'o | 4d40454 | 2001-01-11 16:04:59 +0000 | [diff] [blame] | 327 | retval = ext2fs_sync_device(host_dev, 1); |
| 328 | if (retval) |
| 329 | com_err(program_name, retval, _("during ext2fs_sync_device")); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 332 | static unsigned int test_ro (int dev, blk_t last_block, |
| 333 | int block_size, blk_t from_count, |
| 334 | unsigned int blocks_at_once) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 335 | { |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 336 | unsigned char * blkbuf; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 337 | int try; |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 338 | int got; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 339 | unsigned int bb_count = 0; |
| 340 | errcode_t errcode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 341 | |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 342 | errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter); |
| 343 | if (errcode) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 344 | com_err (program_name, errcode, |
| 345 | _("while beginning bad block list iteration")); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 346 | exit (1); |
| 347 | } |
| 348 | do { |
| 349 | ext2fs_badblocks_list_iterate (bb_iter, &next_bad); |
| 350 | } while (next_bad && next_bad < from_count); |
| 351 | |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 352 | if (t_flag) { |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 353 | blkbuf = allocate_buffer((blocks_at_once + 1) * block_size); |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 354 | } else { |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 355 | blkbuf = allocate_buffer(blocks_at_once * block_size); |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 356 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 357 | if (!blkbuf) |
| 358 | { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 359 | com_err (program_name, ENOMEM, _("while allocating buffers")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 360 | exit (1); |
| 361 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 362 | if (v_flag) { |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 363 | fprintf (stderr, _("Checking blocks %lu to %lu\n"), |
| 364 | (unsigned long) from_count, |
| 365 | (unsigned long) last_block - 1); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 366 | } |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 367 | if (t_flag) { |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 368 | fputs(_("Checking for bad blocks in read-only mode\n"), stderr); |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 369 | pattern_fill(blkbuf + blocks_at_once * block_size, |
| 370 | t_patts[0], block_size); |
| 371 | } |
| 372 | flush_bufs(); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 373 | try = blocks_at_once; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 374 | currently_testing = from_count; |
Theodore Ts'o | 8938ce6 | 2006-10-03 23:35:57 -0400 | [diff] [blame] | 375 | num_blocks = last_block - 1; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 376 | if (!t_flag && (s_flag || v_flag)) { |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 377 | fputs(_("Checking for bad blocks (read-only test): "), stderr); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 378 | if (v_flag <= 1) |
| 379 | alarm_intr(SIGALRM); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 380 | } |
Theodore Ts'o | cd130a0 | 2001-05-05 05:43:23 +0000 | [diff] [blame] | 381 | while (currently_testing < last_block) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 382 | { |
Iustin Pop | 931b028 | 2008-06-11 13:12:17 +0200 | [diff] [blame^] | 383 | if (max_bb && bb_count >= max_bb) { |
| 384 | if (s_flag || v_flag) { |
| 385 | fputs(_("Too many bad blocks, aborting test\n"), stderr); |
| 386 | } |
| 387 | break; |
| 388 | } |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 389 | if (next_bad) { |
| 390 | if (currently_testing == next_bad) { |
| 391 | /* fprintf (out, "%lu\n", nextbad); */ |
| 392 | ext2fs_badblocks_list_iterate (bb_iter, &next_bad); |
| 393 | currently_testing++; |
| 394 | continue; |
| 395 | } |
| 396 | else if (currently_testing + try > next_bad) |
| 397 | try = next_bad - currently_testing; |
| 398 | } |
Theodore Ts'o | cd130a0 | 2001-05-05 05:43:23 +0000 | [diff] [blame] | 399 | if (currently_testing + try > last_block) |
| 400 | try = last_block - currently_testing; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 401 | got = do_read (dev, blkbuf, try, block_size, currently_testing); |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 402 | if (t_flag) { |
| 403 | /* test the comparison between all the |
| 404 | blocks successfully read */ |
| 405 | int i; |
| 406 | for (i = 0; i < got; ++i) |
| 407 | if (memcmp (blkbuf+i*block_size, |
| 408 | blkbuf+blocks_at_once*block_size, |
| 409 | block_size)) |
| 410 | bb_count += bb_output(currently_testing + i); |
| 411 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 412 | currently_testing += got; |
| 413 | if (got == try) { |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 414 | try = blocks_at_once; |
Theodore Ts'o | 1f9a60c | 2003-08-01 00:58:00 -0400 | [diff] [blame] | 415 | /* recover page-aligned offset for O_DIRECT */ |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 416 | if ( (blocks_at_once >= sys_page_size >> 9) |
Theodore Ts'o | 1f9a60c | 2003-08-01 00:58:00 -0400 | [diff] [blame] | 417 | && (currently_testing % (sys_page_size >> 9)!= 0)) |
| 418 | try -= (sys_page_size >> 9) |
| 419 | - (currently_testing |
| 420 | % (sys_page_size >> 9)); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 421 | continue; |
| 422 | } |
| 423 | else |
| 424 | try = 1; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 425 | if (got == 0) { |
Theodore Ts'o | dd018f5 | 2000-02-06 23:57:07 +0000 | [diff] [blame] | 426 | bb_count += bb_output(currently_testing++); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 427 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 428 | } |
| 429 | num_blocks = 0; |
| 430 | alarm(0); |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 431 | if (s_flag || v_flag) |
Theodore Ts'o | 3ef681c | 2004-09-19 08:04:44 -0400 | [diff] [blame] | 432 | fputs(_(done_string), stderr); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 433 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 434 | fflush (stderr); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 435 | free (blkbuf); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 436 | |
| 437 | ext2fs_badblocks_list_iterate_end(bb_iter); |
| 438 | |
| 439 | return bb_count; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 442 | static unsigned int test_rw (int dev, blk_t last_block, |
| 443 | int block_size, blk_t from_count, |
| 444 | unsigned int blocks_at_once) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 445 | { |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 446 | unsigned char *buffer, *read_buffer; |
Theodore Ts'o | e9860ae | 2007-10-22 09:51:50 -0400 | [diff] [blame] | 447 | const unsigned int patterns[] = {0xaa, 0x55, 0xff, 0x00}; |
| 448 | const unsigned int *pattern; |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 449 | int i, try, got, nr_pattern, pat_idx; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 450 | unsigned int bb_count = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 451 | |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 452 | buffer = allocate_buffer(2 * blocks_at_once * block_size); |
| 453 | read_buffer = buffer + blocks_at_once * block_size; |
| 454 | |
| 455 | if (!buffer) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 456 | com_err (program_name, ENOMEM, _("while allocating buffers")); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 457 | exit (1); |
| 458 | } |
| 459 | |
Theodore Ts'o | 4d40454 | 2001-01-11 16:04:59 +0000 | [diff] [blame] | 460 | flush_bufs(); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 461 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 462 | if (v_flag) { |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 463 | fputs(_("Checking for bad blocks in read-write mode\n"), |
| 464 | stderr); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 465 | fprintf(stderr, _("From block %lu to %lu\n"), |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 466 | (unsigned long) from_count, |
| 467 | (unsigned long) last_block); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 468 | } |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 469 | if (t_flag) { |
| 470 | pattern = t_patts; |
| 471 | nr_pattern = t_flag; |
| 472 | } else { |
| 473 | pattern = patterns; |
| 474 | nr_pattern = sizeof(patterns) / sizeof(patterns[0]); |
| 475 | } |
| 476 | for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) { |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 477 | pattern_fill(buffer, pattern[pat_idx], |
| 478 | blocks_at_once * block_size); |
Theodore Ts'o | 8938ce6 | 2006-10-03 23:35:57 -0400 | [diff] [blame] | 479 | num_blocks = last_block - 1; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 480 | currently_testing = from_count; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 481 | if (s_flag && v_flag <= 1) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 482 | alarm_intr(SIGALRM); |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 483 | |
| 484 | try = blocks_at_once; |
| 485 | while (currently_testing < last_block) { |
Iustin Pop | 931b028 | 2008-06-11 13:12:17 +0200 | [diff] [blame^] | 486 | if (max_bb && bb_count >= max_bb) { |
| 487 | if (s_flag || v_flag) { |
| 488 | fputs(_("Too many bad blocks, aborting test\n"), stderr); |
| 489 | } |
| 490 | break; |
| 491 | } |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 492 | if (currently_testing + try > last_block) |
| 493 | try = last_block - currently_testing; |
| 494 | got = do_write(dev, buffer, try, block_size, |
| 495 | currently_testing); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 496 | if (v_flag > 1) |
| 497 | print_status(); |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 498 | |
| 499 | currently_testing += got; |
| 500 | if (got == try) { |
| 501 | try = blocks_at_once; |
Theodore Ts'o | 1f9a60c | 2003-08-01 00:58:00 -0400 | [diff] [blame] | 502 | /* recover page-aligned offset for O_DIRECT */ |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 503 | if ( (blocks_at_once >= sys_page_size >> 9) |
Theodore Ts'o | 1f9a60c | 2003-08-01 00:58:00 -0400 | [diff] [blame] | 504 | && (currently_testing % |
| 505 | (sys_page_size >> 9)!= 0)) |
| 506 | try -= (sys_page_size >> 9) |
| 507 | - (currently_testing |
| 508 | % (sys_page_size >> 9)); |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 509 | continue; |
| 510 | } else |
| 511 | try = 1; |
| 512 | if (got == 0) { |
| 513 | bb_count += bb_output(currently_testing++); |
| 514 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 515 | } |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 516 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 517 | num_blocks = 0; |
| 518 | alarm (0); |
| 519 | if (s_flag | v_flag) |
Theodore Ts'o | 3ef681c | 2004-09-19 08:04:44 -0400 | [diff] [blame] | 520 | fputs(_(done_string), stderr); |
Theodore Ts'o | 4d40454 | 2001-01-11 16:04:59 +0000 | [diff] [blame] | 521 | flush_bufs(); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 522 | if (s_flag | v_flag) |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 523 | fputs(_("Reading and comparing: "), stderr); |
Theodore Ts'o | cd130a0 | 2001-05-05 05:43:23 +0000 | [diff] [blame] | 524 | num_blocks = last_block; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 525 | currently_testing = from_count; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 526 | if (s_flag && v_flag <= 1) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 527 | alarm_intr(SIGALRM); |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 528 | |
| 529 | try = blocks_at_once; |
| 530 | while (currently_testing < last_block) { |
Iustin Pop | 931b028 | 2008-06-11 13:12:17 +0200 | [diff] [blame^] | 531 | if (max_bb && bb_count >= max_bb) { |
| 532 | if (s_flag || v_flag) { |
| 533 | fputs(_("Too many bad blocks, aborting test\n"), stderr); |
| 534 | } |
| 535 | break; |
| 536 | } |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 537 | if (currently_testing + try > last_block) |
| 538 | try = last_block - currently_testing; |
| 539 | got = do_read (dev, read_buffer, try, block_size, |
| 540 | currently_testing); |
| 541 | if (got == 0) { |
| 542 | bb_count += bb_output(currently_testing++); |
| 543 | continue; |
| 544 | } |
| 545 | for (i=0; i < got; i++) { |
| 546 | if (memcmp(read_buffer + i * block_size, |
| 547 | buffer + i * block_size, |
| 548 | block_size)) |
| 549 | bb_count += bb_output(currently_testing+i); |
| 550 | } |
| 551 | currently_testing += got; |
Theodore Ts'o | 1f9a60c | 2003-08-01 00:58:00 -0400 | [diff] [blame] | 552 | /* recover page-aligned offset for O_DIRECT */ |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 553 | if ( (blocks_at_once >= sys_page_size >> 9) |
Theodore Ts'o | 1f9a60c | 2003-08-01 00:58:00 -0400 | [diff] [blame] | 554 | && (currently_testing % (sys_page_size >> 9)!= 0)) |
| 555 | try = blocks_at_once - (sys_page_size >> 9) |
| 556 | - (currently_testing |
| 557 | % (sys_page_size >> 9)); |
| 558 | else |
| 559 | try = blocks_at_once; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 560 | if (v_flag > 1) |
| 561 | print_status(); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 562 | } |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 563 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 564 | num_blocks = 0; |
| 565 | alarm (0); |
| 566 | if (s_flag | v_flag) |
Theodore Ts'o | 3ef681c | 2004-09-19 08:04:44 -0400 | [diff] [blame] | 567 | fputs(_(done_string), stderr); |
Theodore Ts'o | 4d40454 | 2001-01-11 16:04:59 +0000 | [diff] [blame] | 568 | flush_bufs(); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 569 | } |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 570 | uncapture_terminate(); |
Theodore Ts'o | 6d40f56 | 2003-05-07 08:35:38 -0400 | [diff] [blame] | 571 | free(buffer); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 572 | return bb_count; |
| 573 | } |
| 574 | |
Theodore Ts'o | d49a22b | 2000-07-06 00:31:27 +0000 | [diff] [blame] | 575 | struct saved_blk_record { |
| 576 | blk_t block; |
| 577 | int num; |
| 578 | }; |
| 579 | |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 580 | static unsigned int test_nd (int dev, blk_t last_block, |
| 581 | int block_size, blk_t from_count, |
| 582 | unsigned int blocks_at_once) |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 583 | { |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 584 | unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr; |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 585 | unsigned char *test_base, *save_base, *read_base; |
Theodore Ts'o | dd018f5 | 2000-02-06 23:57:07 +0000 | [diff] [blame] | 586 | int try, i; |
Theodore Ts'o | e9860ae | 2007-10-22 09:51:50 -0400 | [diff] [blame] | 587 | const unsigned int patterns[] = { ~0 }; |
| 588 | const unsigned int *pattern; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 589 | int nr_pattern, pat_idx; |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 590 | int got, used2, written; |
| 591 | blk_t save_currently_testing; |
Theodore Ts'o | d49a22b | 2000-07-06 00:31:27 +0000 | [diff] [blame] | 592 | struct saved_blk_record *test_record; |
Theodore Ts'o | a551b78 | 2000-07-13 22:05:31 +0000 | [diff] [blame] | 593 | /* This is static to prevent being clobbered by the longjmp */ |
| 594 | static int num_saved; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 595 | jmp_buf terminate_env; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 596 | errcode_t errcode; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 597 | unsigned long buf_used; |
| 598 | static unsigned int bb_count; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 599 | |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 600 | bb_count = 0; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 601 | errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter); |
| 602 | if (errcode) { |
Theodore Ts'o | dd018f5 | 2000-02-06 23:57:07 +0000 | [diff] [blame] | 603 | com_err (program_name, errcode, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 604 | _("while beginning bad block list iteration")); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 605 | exit (1); |
| 606 | } |
| 607 | do { |
| 608 | ext2fs_badblocks_list_iterate (bb_iter, &next_bad); |
| 609 | } while (next_bad && next_bad < from_count); |
| 610 | |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 611 | blkbuf = allocate_buffer(3 * blocks_at_once * block_size); |
Theodore Ts'o | d49a22b | 2000-07-06 00:31:27 +0000 | [diff] [blame] | 612 | test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record)); |
| 613 | if (!blkbuf || !test_record) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 614 | com_err(program_name, ENOMEM, _("while allocating buffers")); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 615 | exit (1); |
| 616 | } |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 617 | |
| 618 | save_base = blkbuf; |
| 619 | test_base = blkbuf + (blocks_at_once * block_size); |
| 620 | read_base = blkbuf + (2 * blocks_at_once * block_size); |
| 621 | |
Theodore Ts'o | d49a22b | 2000-07-06 00:31:27 +0000 | [diff] [blame] | 622 | num_saved = 0; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 623 | |
Theodore Ts'o | 4d40454 | 2001-01-11 16:04:59 +0000 | [diff] [blame] | 624 | flush_bufs(); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 625 | if (v_flag) { |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 626 | fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr); |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 627 | fprintf (stderr, _("From block %lu to %lu\n"), |
| 628 | (unsigned long) from_count, (unsigned long) last_block); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 629 | } |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 630 | if (s_flag || v_flag > 1) { |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 631 | fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 632 | } |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 633 | if (setjmp(terminate_env)) { |
| 634 | /* |
| 635 | * Abnormal termination by a signal is handled here. |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 636 | */ |
Theodore Ts'o | a551b78 | 2000-07-13 22:05:31 +0000 | [diff] [blame] | 637 | signal (SIGALRM, SIG_IGN); |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 638 | fputs(_("\nInterrupt caught, cleaning up\n"), stderr); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 639 | |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 640 | save_ptr = save_base; |
Theodore Ts'o | d49a22b | 2000-07-06 00:31:27 +0000 | [diff] [blame] | 641 | for (i=0; i < num_saved; i++) { |
| 642 | do_write(dev, save_ptr, test_record[i].num, |
| 643 | block_size, test_record[i].block); |
| 644 | save_ptr += test_record[i].num * block_size; |
| 645 | } |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 646 | fflush (out); |
Theodore Ts'o | dd018f5 | 2000-02-06 23:57:07 +0000 | [diff] [blame] | 647 | exit(1); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 648 | } |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 649 | |
| 650 | /* set up abend handler */ |
| 651 | capture_terminate(terminate_env); |
| 652 | |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 653 | if (t_flag) { |
| 654 | pattern = t_patts; |
| 655 | nr_pattern = t_flag; |
| 656 | } else { |
| 657 | pattern = patterns; |
| 658 | nr_pattern = sizeof(patterns) / sizeof(patterns[0]); |
| 659 | } |
| 660 | for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) { |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 661 | pattern_fill(test_base, pattern[pat_idx], |
| 662 | blocks_at_once * block_size); |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 663 | |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 664 | buf_used = 0; |
| 665 | bb_count = 0; |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 666 | save_ptr = save_base; |
| 667 | test_ptr = test_base; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 668 | currently_testing = from_count; |
Theodore Ts'o | 8938ce6 | 2006-10-03 23:35:57 -0400 | [diff] [blame] | 669 | num_blocks = last_block - 1; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 670 | if (s_flag && v_flag <= 1) |
| 671 | alarm_intr(SIGALRM); |
| 672 | |
| 673 | while (currently_testing < last_block) { |
Iustin Pop | 931b028 | 2008-06-11 13:12:17 +0200 | [diff] [blame^] | 674 | if (max_bb && bb_count >= max_bb) { |
| 675 | if (s_flag || v_flag) { |
| 676 | fputs(_("Too many bad blocks, aborting test\n"), stderr); |
| 677 | } |
| 678 | break; |
| 679 | } |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 680 | got = try = blocks_at_once - buf_used; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 681 | if (next_bad) { |
| 682 | if (currently_testing == next_bad) { |
| 683 | /* fprintf (out, "%lu\n", nextbad); */ |
| 684 | ext2fs_badblocks_list_iterate (bb_iter, &next_bad); |
| 685 | currently_testing++; |
| 686 | goto check_for_more; |
| 687 | } |
| 688 | else if (currently_testing + try > next_bad) |
| 689 | try = next_bad - currently_testing; |
| 690 | } |
| 691 | if (currently_testing + try > last_block) |
| 692 | try = last_block - currently_testing; |
| 693 | got = do_read (dev, save_ptr, try, block_size, |
| 694 | currently_testing); |
| 695 | if (got == 0) { |
| 696 | /* First block must have been bad. */ |
| 697 | bb_count += bb_output(currently_testing++); |
Theodore Ts'o | d49a22b | 2000-07-06 00:31:27 +0000 | [diff] [blame] | 698 | goto check_for_more; |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 699 | } |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 700 | |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 701 | /* |
| 702 | * Note the fact that we've saved this much data |
| 703 | * *before* we overwrite it with test data |
| 704 | */ |
| 705 | test_record[num_saved].block = currently_testing; |
| 706 | test_record[num_saved].num = got; |
| 707 | num_saved++; |
Theodore Ts'o | d49a22b | 2000-07-06 00:31:27 +0000 | [diff] [blame] | 708 | |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 709 | /* Write the test data */ |
| 710 | written = do_write (dev, test_ptr, got, block_size, |
| 711 | currently_testing); |
| 712 | if (written != got) |
| 713 | com_err (program_name, errno, |
| 714 | _("during test data write, block %lu"), |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 715 | (unsigned long) currently_testing + |
| 716 | written); |
Theodore Ts'o | d49a22b | 2000-07-06 00:31:27 +0000 | [diff] [blame] | 717 | |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 718 | buf_used += got; |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 719 | save_ptr += got * block_size; |
| 720 | test_ptr += got * block_size; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 721 | currently_testing += got; |
| 722 | if (got != try) |
| 723 | bb_count += bb_output(currently_testing++); |
| 724 | |
| 725 | check_for_more: |
| 726 | /* |
| 727 | * If there's room for more blocks to be tested this |
| 728 | * around, and we're not done yet testing the disk, go |
| 729 | * back and get some more blocks. |
| 730 | */ |
| 731 | if ((buf_used != blocks_at_once) && |
| 732 | (currently_testing < last_block)) |
| 733 | continue; |
| 734 | |
| 735 | flush_bufs(); |
| 736 | save_currently_testing = currently_testing; |
| 737 | |
| 738 | /* |
| 739 | * for each contiguous block that we read into the |
| 740 | * buffer (and wrote test data into afterwards), read |
| 741 | * it back (looping if necessary, to get past newly |
| 742 | * discovered unreadable blocks, of which there should |
| 743 | * be none, but with a hard drive which is unreliable, |
| 744 | * it has happened), and compare with the test data |
| 745 | * that was written; output to the bad block list if |
| 746 | * it doesn't match. |
| 747 | */ |
| 748 | used2 = 0; |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 749 | save_ptr = save_base; |
| 750 | test_ptr = test_base; |
| 751 | read_ptr = read_base; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 752 | try = 0; |
| 753 | |
| 754 | while (1) { |
| 755 | if (try == 0) { |
| 756 | if (used2 >= num_saved) |
| 757 | break; |
| 758 | currently_testing = test_record[used2].block; |
| 759 | try = test_record[used2].num; |
| 760 | used2++; |
| 761 | } |
| 762 | |
| 763 | got = do_read (dev, read_ptr, try, |
| 764 | block_size, currently_testing); |
| 765 | |
| 766 | /* test the comparison between all the |
| 767 | blocks successfully read */ |
| 768 | for (i = 0; i < got; ++i) |
| 769 | if (memcmp (test_ptr+i*block_size, |
| 770 | read_ptr+i*block_size, block_size)) |
| 771 | bb_count += bb_output(currently_testing + i); |
| 772 | if (got < try) { |
| 773 | bb_count += bb_output(currently_testing + got); |
| 774 | got++; |
| 775 | } |
| 776 | |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 777 | /* write back original data */ |
| 778 | do_write (dev, save_ptr, got, |
| 779 | block_size, currently_testing); |
| 780 | save_ptr += got * block_size; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 781 | |
| 782 | currently_testing += got; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 783 | test_ptr += got * block_size; |
| 784 | read_ptr += got * block_size; |
| 785 | try -= got; |
| 786 | } |
| 787 | |
| 788 | /* empty the buffer so it can be reused */ |
| 789 | num_saved = 0; |
| 790 | buf_used = 0; |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 791 | save_ptr = save_base; |
| 792 | test_ptr = test_base; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 793 | currently_testing = save_currently_testing; |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 794 | } |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 795 | num_blocks = 0; |
| 796 | alarm(0); |
| 797 | if (s_flag || v_flag > 1) |
Theodore Ts'o | 3ef681c | 2004-09-19 08:04:44 -0400 | [diff] [blame] | 798 | fputs(_(done_string), stderr); |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 799 | |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 800 | flush_bufs(); |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 801 | } |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 802 | uncapture_terminate(); |
Theodore Ts'o | dd018f5 | 2000-02-06 23:57:07 +0000 | [diff] [blame] | 803 | fflush(stderr); |
| 804 | free(blkbuf); |
Theodore Ts'o | d49a22b | 2000-07-06 00:31:27 +0000 | [diff] [blame] | 805 | free(test_record); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 806 | |
| 807 | ext2fs_badblocks_list_iterate_end(bb_iter); |
| 808 | |
| 809 | return bb_count; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Theodore Ts'o | 981dc56 | 2000-07-06 14:13:29 +0000 | [diff] [blame] | 812 | static void check_mount(char *device_name) |
| 813 | { |
| 814 | errcode_t retval; |
| 815 | int mount_flags; |
| 816 | |
| 817 | retval = ext2fs_check_if_mounted(device_name, &mount_flags); |
| 818 | if (retval) { |
| 819 | com_err("ext2fs_check_if_mount", retval, |
| 820 | _("while determining whether %s is mounted."), |
| 821 | device_name); |
| 822 | return; |
| 823 | } |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 824 | if (mount_flags & EXT2_MF_MOUNTED) { |
| 825 | fprintf(stderr, _("%s is mounted; "), device_name); |
| 826 | if (force) { |
| 827 | fputs(_("badblocks forced anyway. " |
| 828 | "Hope /etc/mtab is incorrect.\n"), stderr); |
| 829 | return; |
| 830 | } |
| 831 | abort_badblocks: |
| 832 | fputs(_("it's not safe to run badblocks!\n"), stderr); |
| 833 | exit(1); |
Theodore Ts'o | 981dc56 | 2000-07-06 14:13:29 +0000 | [diff] [blame] | 834 | } |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 835 | |
Theodore Ts'o | f63978a | 2006-05-13 09:25:47 -0400 | [diff] [blame] | 836 | if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) { |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 837 | fprintf(stderr, _("%s is apparently in use by the system; "), |
| 838 | device_name); |
| 839 | if (force) |
| 840 | fputs(_("badblocks forced anyway.\n"), stderr); |
| 841 | else |
| 842 | goto abort_badblocks; |
| 843 | } |
| 844 | |
Theodore Ts'o | 981dc56 | 2000-07-06 14:13:29 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Theodore Ts'o | d4be9fa | 2007-10-22 10:19:20 -0400 | [diff] [blame] | 847 | /* |
| 848 | * This function will convert a string to an unsigned long, printing |
| 849 | * an error message if it fails, and returning success or failure in err. |
| 850 | */ |
| 851 | static unsigned int parse_uint(const char *str, const char *descr) |
| 852 | { |
| 853 | char *tmp; |
| 854 | unsigned long ret; |
| 855 | |
| 856 | ret = strtoul(str, &tmp, 0); |
| 857 | if (*tmp || errno || (ret > UINT_MAX) || |
| 858 | (ret == ULONG_MAX && errno == ERANGE)) { |
| 859 | com_err (program_name, 0, _("invalid %s - %s"), descr, str); |
| 860 | exit (1); |
| 861 | } |
| 862 | return ret; |
| 863 | } |
Theodore Ts'o | 981dc56 | 2000-07-06 14:13:29 +0000 | [diff] [blame] | 864 | |
Theodore Ts'o | 00e5433 | 1997-09-16 02:13:52 +0000 | [diff] [blame] | 865 | int main (int argc, char ** argv) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 866 | { |
Theodore Ts'o | 519149f | 1997-10-25 03:49:49 +0000 | [diff] [blame] | 867 | int c; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 868 | char * device_name; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 869 | char * host_device_name = NULL; |
| 870 | char * input_file = NULL; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 871 | char * output_file = NULL; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 872 | FILE * in = NULL; |
Theodore Ts'o | dd018f5 | 2000-02-06 23:57:07 +0000 | [diff] [blame] | 873 | int block_size = 1024; |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 874 | unsigned int blocks_at_once = 64; |
Theodore Ts'o | cd130a0 | 2001-05-05 05:43:23 +0000 | [diff] [blame] | 875 | blk_t last_block, from_count; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 876 | int num_passes = 0; |
| 877 | int passes_clean = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 878 | int dev; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 879 | errcode_t errcode; |
Theodore Ts'o | e9860ae | 2007-10-22 09:51:50 -0400 | [diff] [blame] | 880 | unsigned int pattern; |
Theodore Ts'o | acd7741 | 2007-10-22 10:09:05 -0400 | [diff] [blame] | 881 | unsigned int (*test_func)(int, blk_t, |
| 882 | int, blk_t, |
| 883 | unsigned int); |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 884 | int open_flag = 0; |
| 885 | long sysval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 886 | |
| 887 | setbuf(stdout, NULL); |
| 888 | setbuf(stderr, NULL); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 889 | #ifdef ENABLE_NLS |
| 890 | setlocale(LC_MESSAGES, ""); |
Theodore Ts'o | 14308a5 | 2002-03-05 03:26:52 -0500 | [diff] [blame] | 891 | setlocale(LC_CTYPE, ""); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 892 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
| 893 | textdomain(NLS_CAT_NAME); |
| 894 | #endif |
Theodore Ts'o | 6d40f56 | 2003-05-07 08:35:38 -0400 | [diff] [blame] | 895 | srandom((unsigned int)time(NULL)); /* simple randomness is enough */ |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 896 | test_func = test_ro; |
Theodore Ts'o | 4d40454 | 2001-01-11 16:04:59 +0000 | [diff] [blame] | 897 | |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 898 | /* Determine the system page size if possible */ |
| 899 | #ifdef HAVE_SYSCONF |
| 900 | #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE)) |
| 901 | #define _SC_PAGESIZE _SC_PAGE_SIZE |
| 902 | #endif |
| 903 | #ifdef _SC_PAGESIZE |
| 904 | sysval = sysconf(_SC_PAGESIZE); |
| 905 | if (sysval > 0) |
| 906 | sys_page_size = sysval; |
| 907 | #endif /* _SC_PAGESIZE */ |
| 908 | #endif /* HAVE_SYSCONF */ |
| 909 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 910 | if (argc && *argv) |
| 911 | program_name = *argv; |
Iustin Pop | 931b028 | 2008-06-11 13:12:17 +0200 | [diff] [blame^] | 912 | while ((c = getopt (argc, argv, "b:e:fi:o:svwnc:p:h:t:X")) != EOF) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 913 | switch (c) { |
| 914 | case 'b': |
Theodore Ts'o | d4be9fa | 2007-10-22 10:19:20 -0400 | [diff] [blame] | 915 | block_size = parse_uint(optarg, "block size"); |
| 916 | if (block_size > 4096) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 917 | com_err (program_name, 0, |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 918 | _("bad block size - %s"), optarg); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 919 | exit (1); |
| 920 | } |
| 921 | break; |
Theodore Ts'o | 981dc56 | 2000-07-06 14:13:29 +0000 | [diff] [blame] | 922 | case 'f': |
| 923 | force++; |
| 924 | break; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 925 | case 'i': |
| 926 | input_file = optarg; |
| 927 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 928 | case 'o': |
| 929 | output_file = optarg; |
| 930 | break; |
| 931 | case 's': |
| 932 | s_flag = 1; |
| 933 | break; |
| 934 | case 'v': |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 935 | v_flag++; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 936 | break; |
| 937 | case 'w': |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 938 | if (w_flag) |
Theodore Ts'o | d8b5f77 | 2006-11-12 23:09:03 -0500 | [diff] [blame] | 939 | exclusive_usage(); |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 940 | test_func = test_rw; |
| 941 | w_flag = 1; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 942 | break; |
| 943 | case 'n': |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 944 | if (w_flag) |
Theodore Ts'o | d8b5f77 | 2006-11-12 23:09:03 -0500 | [diff] [blame] | 945 | exclusive_usage(); |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 946 | test_func = test_nd; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 947 | w_flag = 2; |
| 948 | break; |
| 949 | case 'c': |
Theodore Ts'o | d4be9fa | 2007-10-22 10:19:20 -0400 | [diff] [blame] | 950 | blocks_at_once = parse_uint(optarg, "blocks at once"); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 951 | break; |
Iustin Pop | 931b028 | 2008-06-11 13:12:17 +0200 | [diff] [blame^] | 952 | case 'e': |
| 953 | max_bb = parse_uint(optarg, "max bad block count"); |
| 954 | break; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 955 | case 'p': |
Theodore Ts'o | d4be9fa | 2007-10-22 10:19:20 -0400 | [diff] [blame] | 956 | num_passes = parse_uint(optarg, |
| 957 | "number of clean passes"); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 958 | break; |
| 959 | case 'h': |
| 960 | host_device_name = optarg; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 961 | break; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 962 | case 't': |
| 963 | if (t_flag + 1 > t_max) { |
Theodore Ts'o | e9860ae | 2007-10-22 09:51:50 -0400 | [diff] [blame] | 964 | unsigned int *t_patts_new; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 965 | |
| 966 | t_patts_new = realloc(t_patts, t_max + T_INC); |
| 967 | if (!t_patts_new) { |
| 968 | com_err(program_name, ENOMEM, |
| 969 | _("can't allocate memory for " |
| 970 | "test_pattern - %s"), |
| 971 | optarg); |
| 972 | exit(1); |
| 973 | } |
| 974 | t_patts = t_patts_new; |
| 975 | t_max += T_INC; |
| 976 | } |
Theodore Ts'o | 84c0545 | 2003-05-18 01:11:52 -0400 | [diff] [blame] | 977 | if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) { |
| 978 | t_patts[t_flag++] = ~0; |
| 979 | } else { |
Theodore Ts'o | d4be9fa | 2007-10-22 10:19:20 -0400 | [diff] [blame] | 980 | pattern = parse_uint(optarg, "test pattern"); |
Theodore Ts'o | e9860ae | 2007-10-22 09:51:50 -0400 | [diff] [blame] | 981 | if (pattern == (unsigned int) ~0) |
Theodore Ts'o | 84c0545 | 2003-05-18 01:11:52 -0400 | [diff] [blame] | 982 | pattern = 0xffff; |
| 983 | t_patts[t_flag++] = pattern; |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 984 | } |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 985 | break; |
Theodore Ts'o | f63978a | 2006-05-13 09:25:47 -0400 | [diff] [blame] | 986 | case 'X': |
| 987 | exclusive_ok++; |
| 988 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 989 | default: |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 990 | usage(); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 993 | if (!w_flag) { |
| 994 | if (t_flag > 1) { |
| 995 | com_err(program_name, 0, |
| 996 | _("Maximum of one test_pattern may be specified " |
| 997 | "in read-only mode")); |
| 998 | exit(1); |
| 999 | } |
Theodore Ts'o | e9860ae | 2007-10-22 09:51:50 -0400 | [diff] [blame] | 1000 | if (t_patts && (t_patts[0] == (unsigned int) ~0)) { |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 1001 | com_err(program_name, 0, |
| 1002 | _("Random test_pattern is not allowed " |
| 1003 | "in read-only mode")); |
| 1004 | exit(1); |
| 1005 | } |
| 1006 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1007 | if (optind > argc - 1) |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 1008 | usage(); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1009 | device_name = argv[optind++]; |
Theodore Ts'o | 35964b5 | 2000-07-06 13:19:43 +0000 | [diff] [blame] | 1010 | if (optind > argc - 1) { |
| 1011 | errcode = ext2fs_get_device_size(device_name, |
| 1012 | block_size, |
Theodore Ts'o | cd130a0 | 2001-05-05 05:43:23 +0000 | [diff] [blame] | 1013 | &last_block); |
Theodore Ts'o | 35964b5 | 2000-07-06 13:19:43 +0000 | [diff] [blame] | 1014 | if (errcode == EXT2_ET_UNIMPLEMENTED) { |
| 1015 | com_err(program_name, 0, |
| 1016 | _("Couldn't determine device size; you " |
| 1017 | "must specify\nthe size manually\n")); |
| 1018 | exit(1); |
| 1019 | } |
| 1020 | if (errcode) { |
| 1021 | com_err(program_name, errcode, |
| 1022 | _("while trying to determine device size")); |
| 1023 | exit(1); |
| 1024 | } |
| 1025 | } else { |
Theodore Ts'o | 5267a52 | 2007-06-04 01:49:51 -0400 | [diff] [blame] | 1026 | errno = 0; |
Theodore Ts'o | d4be9fa | 2007-10-22 10:19:20 -0400 | [diff] [blame] | 1027 | last_block = parse_uint(argv[optind], "last block"); |
Theodore Ts'o | 5267a52 | 2007-06-04 01:49:51 -0400 | [diff] [blame] | 1028 | printf("last_block = %d (%s)\n", last_block, argv[optind]); |
Theodore Ts'o | 5267a52 | 2007-06-04 01:49:51 -0400 | [diff] [blame] | 1029 | last_block++; |
Theodore Ts'o | 35964b5 | 2000-07-06 13:19:43 +0000 | [diff] [blame] | 1030 | optind++; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1031 | } |
Theodore Ts'o | 35964b5 | 2000-07-06 13:19:43 +0000 | [diff] [blame] | 1032 | if (optind <= argc-1) { |
Theodore Ts'o | 5267a52 | 2007-06-04 01:49:51 -0400 | [diff] [blame] | 1033 | errno = 0; |
Theodore Ts'o | d4be9fa | 2007-10-22 10:19:20 -0400 | [diff] [blame] | 1034 | from_count = parse_uint(argv[optind], "start block"); |
Theodore Ts'o | 5267a52 | 2007-06-04 01:49:51 -0400 | [diff] [blame] | 1035 | printf("from_count = %d\n", from_count); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 1036 | } else from_count = 0; |
Theodore Ts'o | cd130a0 | 2001-05-05 05:43:23 +0000 | [diff] [blame] | 1037 | if (from_count >= last_block) { |
Theodore Ts'o | d4e0b1c | 2007-08-03 18:56:01 -0400 | [diff] [blame] | 1038 | com_err (program_name, 0, _("invalid starting block (%lu): must be less than %lu"), |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 1039 | (unsigned long) from_count, (unsigned long) last_block); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 1040 | exit (1); |
| 1041 | } |
Theodore Ts'o | 981dc56 | 2000-07-06 14:13:29 +0000 | [diff] [blame] | 1042 | if (w_flag) |
| 1043 | check_mount(device_name); |
| 1044 | |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 1045 | open_flag = w_flag ? O_RDWR : O_RDONLY; |
| 1046 | dev = open (device_name, open_flag); |
Theodore Ts'o | 5493a27 | 2002-01-02 14:15:35 -0500 | [diff] [blame] | 1047 | if (dev == -1) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1048 | com_err (program_name, errno, _("while trying to open %s"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1049 | device_name); |
| 1050 | exit (1); |
| 1051 | } |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 1052 | if (host_device_name) { |
Theodore Ts'o | 1c29b09 | 2003-07-12 16:01:45 -0400 | [diff] [blame] | 1053 | host_dev = open (host_device_name, open_flag); |
Theodore Ts'o | 5493a27 | 2002-01-02 14:15:35 -0500 | [diff] [blame] | 1054 | if (host_dev == -1) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1055 | com_err (program_name, errno, |
| 1056 | _("while trying to open %s"), |
| 1057 | host_device_name); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 1058 | exit (1); |
| 1059 | } |
| 1060 | } else |
| 1061 | host_dev = dev; |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 1062 | if (input_file) { |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 1063 | if (strcmp (input_file, "-") == 0) |
| 1064 | in = stdin; |
| 1065 | else { |
| 1066 | in = fopen (input_file, "r"); |
| 1067 | if (in == NULL) |
| 1068 | { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1069 | com_err (program_name, errno, |
| 1070 | _("while trying to open %s"), |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 1071 | input_file); |
| 1072 | exit (1); |
| 1073 | } |
| 1074 | } |
Theodore Ts'o | 3e69906 | 2002-10-13 23:56:28 -0400 | [diff] [blame] | 1075 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1076 | if (output_file && strcmp (output_file, "-") != 0) |
| 1077 | { |
| 1078 | out = fopen (output_file, "w"); |
| 1079 | if (out == NULL) |
| 1080 | { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1081 | com_err (program_name, errno, |
| 1082 | _("while trying to open %s"), |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 1083 | output_file); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1084 | exit (1); |
| 1085 | } |
| 1086 | } |
| 1087 | else |
| 1088 | out = stdout; |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 1089 | |
| 1090 | errcode = ext2fs_badblocks_list_create(&bb_list,0); |
| 1091 | if (errcode) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1092 | com_err (program_name, errcode, |
Theodore Ts'o | bb145b0 | 2005-06-20 08:35:27 -0400 | [diff] [blame] | 1093 | _("while creating in-memory bad blocks list")); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 1094 | exit (1); |
| 1095 | } |
| 1096 | |
| 1097 | if (in) { |
| 1098 | for(;;) { |
Theodore Ts'o | a551b78 | 2000-07-13 22:05:31 +0000 | [diff] [blame] | 1099 | switch(fscanf (in, "%u\n", &next_bad)) { |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 1100 | case 0: |
| 1101 | com_err (program_name, 0, "input file - bad format"); |
| 1102 | exit (1); |
| 1103 | case EOF: |
| 1104 | break; |
| 1105 | default: |
| 1106 | errcode = ext2fs_badblocks_list_add(bb_list,next_bad); |
| 1107 | if (errcode) { |
Theodore Ts'o | bb145b0 | 2005-06-20 08:35:27 -0400 | [diff] [blame] | 1108 | com_err (program_name, errcode, _("while adding to in-memory bad block list")); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 1109 | exit (1); |
| 1110 | } |
| 1111 | continue; |
| 1112 | } |
| 1113 | break; |
| 1114 | } |
| 1115 | |
| 1116 | if (in != stdin) |
| 1117 | fclose (in); |
| 1118 | } |
| 1119 | |
| 1120 | do { |
| 1121 | unsigned int bb_count; |
| 1122 | |
Theodore Ts'o | cd130a0 | 2001-05-05 05:43:23 +0000 | [diff] [blame] | 1123 | bb_count = test_func(dev, last_block, block_size, |
Theodore Ts'o | 4d00398 | 2000-04-03 16:01:11 +0000 | [diff] [blame] | 1124 | from_count, blocks_at_once); |
| 1125 | if (bb_count) |
| 1126 | passes_clean = 0; |
| 1127 | else |
| 1128 | ++passes_clean; |
| 1129 | |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 1130 | if (v_flag) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1131 | fprintf(stderr, |
| 1132 | _("Pass completed, %u bad blocks found.\n"), |
| 1133 | bb_count); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 1134 | |
| 1135 | } while (passes_clean < num_passes); |
| 1136 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1137 | close (dev); |
| 1138 | if (out != stdout) |
| 1139 | fclose (out); |
Theodore Ts'o | 849b6bc | 2003-05-07 09:52:14 -0400 | [diff] [blame] | 1140 | if (t_patts) |
| 1141 | free(t_patts); |
Theodore Ts'o | 879ac92 | 2000-01-18 20:59:11 +0000 | [diff] [blame] | 1142 | return 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1143 | } |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 1144 | |