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