blob: 87c87f63b42d2a140053eedda325eba1e6b0b8be [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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'odd018f52000-02-06 23:57:07 +00008 * Copyright 1995, 1996, 1997, 1998, 1999 by Theodore Ts'o
Theodore Ts'o879ac922000-01-18 20:59:11 +00009 * Copyright 1999 by David Beattie
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000010 *
Theodore Ts'o3839e651997-04-26 13:21:57 +000011 * 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'oefc6f622008-08-27 23:07:54 -040013 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000014 * %Begin-Header%
15 * This file may be redistributed under the terms of the GNU Public
16 * License.
17 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000018 */
19
20/*
21 * History:
22 * 93/05/26 - Creation from e2fsck
23 * 94/02/27 - Made a separate bad blocks checker
Theodore Ts'o879ac922000-01-18 20:59:11 +000024 * 99/06/30...99/07/26 - Added non-destructive write-testing,
Theodore Ts'odd018f52000-02-06 23:57:07 +000025 * configurable blocks-at-once parameter,
26 * loading of badblocks list to avoid testing
Theodore Ts'oefc6f622008-08-27 23:07:54 -040027 * blocks known to be bad, multiple passes to
Theodore Ts'odd018f52000-02-06 23:57:07 +000028 * make sure that no new blocks are added to the
29 * list. (Work done by David Beattie)
Theodore Ts'o3839e651997-04-26 13:21:57 +000030 */
31
JP Abgralle0ed7402014-03-19 19:08:39 -070032#ifndef _GNU_SOURCE
Theodore Ts'o1c29b092003-07-12 16:01:45 -040033#define _GNU_SOURCE /* for O_DIRECT */
Andreas Dilgercf5301d2011-06-11 10:58:25 -040034#endif
Theodore Ts'o1c29b092003-07-12 16:01:45 -040035
Theodore Ts'o3839e651997-04-26 13:21:57 +000036#include <errno.h>
37#include <fcntl.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000038#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000039#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000040#else
41extern char *optarg;
42extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000043#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000044#include <signal.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
Theodore Ts'o879ac922000-01-18 20:59:11 +000049#include <setjmp.h>
Theodore Ts'o6d40f562003-05-07 08:35:38 -040050#include <time.h>
Theodore Ts'o5267a522007-06-04 01:49:51 -040051#include <limits.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000052
Iustin Popedf261f2008-06-18 22:26:26 +020053#include <sys/time.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000054#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000055#include <sys/types.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000056
Theodore Ts'o3839e651997-04-26 13:21:57 +000057#include "et/com_err.h"
Theodore Ts'od40259f1997-10-20 00:44:26 +000058#include "ext2fs/ext2_io.h"
Theodore Ts'o54c637d2001-05-14 11:45:38 +000059#include "ext2fs/ext2_fs.h"
Theodore Ts'o879ac922000-01-18 20:59:11 +000060#include "ext2fs/ext2fs.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000061#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000062
JP Abgralle0ed7402014-03-19 19:08:39 -070063#ifndef O_LARGEFILE
64#define O_LARGEFILE 0
65#endif
Theodore Ts'of4041672013-12-16 18:56:36 -050066
JP Abgralle0ed7402014-03-19 19:08:39 -070067static const char * program_name = "badblocks";
68static const char * done_string = N_("done \n");
69
70static int v_flag; /* verbose */
71static int w_flag; /* do r/w test: 0=no, 1=yes,
Theodore Ts'o4d003982000-04-03 16:01:11 +000072 * 2=non-destructive */
JP Abgralle0ed7402014-03-19 19:08:39 -070073static int s_flag; /* show progress of test */
74static int force; /* force check of mounted device */
75static int t_flag; /* number of test patterns */
76static int t_max; /* allocated test patterns */
77static unsigned int *t_patts; /* test patterns */
78static int use_buffered_io;
79static int exclusive_ok;
80static unsigned int max_bb; /* Abort test if more than this number of bad blocks has been encountered */
81static unsigned int d_flag; /* delay factor between reads */
Manish Katiyar504f7a22008-09-04 14:44:38 +053082static struct timeval time_start;
Theodore Ts'o1c29b092003-07-12 16:01:45 -040083
Theodore Ts'o849b6bc2003-05-07 09:52:14 -040084#define T_INC 32
Theodore Ts'o4d003982000-04-03 16:01:11 +000085
JP Abgralle0ed7402014-03-19 19:08:39 -070086static unsigned int sys_page_size = 4096;
Theodore Ts'o1c29b092003-07-12 16:01:45 -040087
Theodore Ts'o8820c792001-01-06 04:20:03 +000088static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000089{
Benno Schulenbergad39bcd2008-07-17 23:32:02 +020090 fprintf(stderr, _(
91"Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwnf]\n"
92" [-c blocks_at_once] [-d delay_factor_between_reads] [-e max_bad_blocks]\n"
93" [-p num_passes] [-t test_pattern [-t test_pattern [...]]]\n"
94" device [last_block [first_block]]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000095 program_name);
96 exit (1);
97}
98
Theodore Ts'od8b5f772006-11-12 23:09:03 -050099static void exclusive_usage(void)
100{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400101 fprintf(stderr,
102 _("%s: The -n and -w options are mutually exclusive.\n\n"),
Theodore Ts'o017a76e2006-11-17 23:00:19 -0500103 program_name);
104 exit(1);
Theodore Ts'od8b5f772006-11-12 23:09:03 -0500105}
106
Theodore Ts'oacd77412007-10-22 10:09:05 -0400107static blk_t currently_testing = 0;
108static blk_t num_blocks = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700109static blk_t num_read_errors = 0;
110static blk_t num_write_errors = 0;
111static blk_t num_corruption_errors = 0;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000112static ext2_badblocks_list bb_list = NULL;
113static FILE *out;
114static blk_t next_bad = 0;
115static ext2_badblocks_iterate bb_iter = NULL;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000116
JP Abgralle0ed7402014-03-19 19:08:39 -0700117enum error_types { READ_ERROR, WRITE_ERROR, CORRUPTION_ERROR };
118
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400119static void *allocate_buffer(size_t size)
120{
121 void *ret = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400122
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400123#ifdef HAVE_POSIX_MEMALIGN
124 if (posix_memalign(&ret, sys_page_size, size) < 0)
125 ret = 0;
126#else
127#ifdef HAVE_MEMALIGN
128 ret = memalign(sys_page_size, size);
129#else
130#ifdef HAVE_VALLOC
131 ret = valloc(size);
132#endif /* HAVE_VALLOC */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400133#endif /* HAVE_MEMALIGN */
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400134#endif /* HAVE_POSIX_MEMALIGN */
135
136 if (!ret)
137 ret = malloc(size);
138
139 return ret;
140}
141
Theodore Ts'odd018f52000-02-06 23:57:07 +0000142/*
143 * This routine reports a new bad block. If the bad block has already
144 * been seen before, then it returns 0; otherwise it returns 1.
145 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700146static int bb_output (blk_t bad, enum error_types error_type)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000147{
148 errcode_t errcode;
149
Theodore Ts'odd018f52000-02-06 23:57:07 +0000150 if (ext2fs_badblocks_list_test(bb_list, bad))
151 return 0;
152
Theodore Ts'oacd77412007-10-22 10:09:05 -0400153 fprintf(out, "%lu\n", (unsigned long) bad);
Theodore Ts'occ4f98e2003-04-03 11:37:46 -0500154 fflush(out);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000155
156 errcode = ext2fs_badblocks_list_add (bb_list, bad);
157 if (errcode) {
158 com_err (program_name, errcode, "adding to in-memory bad block list");
159 exit (1);
160 }
161
162 /* kludge:
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400163 increment the iteration through the bb_list if
Theodore Ts'o879ac922000-01-18 20:59:11 +0000164 an element was just added before the current iteration
165 position. This should not cause next_bad to change. */
166 if (bb_iter && bad < next_bad)
167 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
JP Abgralle0ed7402014-03-19 19:08:39 -0700168
169 if (error_type == READ_ERROR) {
170 num_read_errors++;
171 } else if (error_type == WRITE_ERROR) {
172 num_write_errors++;
173 } else if (error_type == CORRUPTION_ERROR) {
174 num_corruption_errors++;
175 }
Theodore Ts'odd018f52000-02-06 23:57:07 +0000176 return 1;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000177}
178
Manish Katiyar504f7a22008-09-04 14:44:38 +0530179static char *time_diff_format(struct timeval *tv1,
180 struct timeval *tv2, char *buf)
181{
182 time_t diff = (tv1->tv_sec - tv2->tv_sec);
183 int hr,min,sec;
184
185 sec = diff % 60;
186 diff /= 60;
187 min = diff % 60;
188 hr = diff / 60;
189
190 if (hr)
191 sprintf(buf, "%d:%02d:%02d", hr, min, sec);
192 else
193 sprintf(buf, "%d:%02d", min, sec);
194 return buf;
195}
196
197static float calc_percent(unsigned long current, unsigned long total) {
198 float percent = 0.0;
199 if (total <= 0)
200 return percent;
201 if (current >= total) {
202 percent = 100.0;
203 } else {
204 percent=(100.0*(float)current/(float)total);
205 }
206 return percent;
207}
208
Theodore Ts'o8820c792001-01-06 04:20:03 +0000209static void print_status(void)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000210{
Manish Katiyar504f7a22008-09-04 14:44:38 +0530211 struct timeval time_end;
212 char diff_buf[32], line_buf[128];
213 int len;
214
215 gettimeofday(&time_end, 0);
216 len = snprintf(line_buf, sizeof(line_buf),
JP Abgralle0ed7402014-03-19 19:08:39 -0700217 _("%6.2f%% done, %s elapsed. "
218 "(%d/%d/%d errors)"),
Manish Katiyar504f7a22008-09-04 14:44:38 +0530219 calc_percent((unsigned long) currently_testing,
220 (unsigned long) num_blocks),
JP Abgralle0ed7402014-03-19 19:08:39 -0700221 time_diff_format(&time_end, &time_start, diff_buf),
222 num_read_errors,
223 num_write_errors,
224 num_corruption_errors);
Theodore Ts'o61ef2472010-08-01 22:30:33 -0400225#ifdef HAVE_MBSTOWCS
226 len = mbstowcs(NULL, line_buf, sizeof(line_buf));
227#endif
Manish Katiyar504f7a22008-09-04 14:44:38 +0530228 fputs(line_buf, stderr);
229 memset(line_buf, '\b', len);
230 line_buf[len] = 0;
231 fputs(line_buf, stderr);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000232 fflush (stderr);
233}
234
Theodore Ts'o54434922003-12-07 01:28:50 -0500235static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000236{
237 signal (SIGALRM, alarm_intr);
238 alarm(1);
239 if (!num_blocks)
240 return;
Theodore Ts'oc76564a2005-01-06 14:48:59 -0500241 print_status();
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000242}
243
Theodore Ts'o879ac922000-01-18 20:59:11 +0000244static void *terminate_addr = NULL;
245
Theodore Ts'o54434922003-12-07 01:28:50 -0500246static void terminate_intr(int signo EXT2FS_ATTR((unused)))
Theodore Ts'o879ac922000-01-18 20:59:11 +0000247{
Theodore Ts'oc510d6f2009-06-29 01:04:02 -0400248 fflush(out);
Theodore Ts'o83bfa272009-06-29 00:52:27 -0400249 fprintf(stderr, "\n\nInterrupted at block %llu\n",
250 (unsigned long long) currently_testing);
251 fflush(stderr);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000252 if (terminate_addr)
253 longjmp(terminate_addr,1);
254 exit(1);
255}
256
Theodore Ts'o981dc562000-07-06 14:13:29 +0000257static void capture_terminate(jmp_buf term_addr)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000258{
259 terminate_addr = term_addr;
260 signal (SIGHUP, terminate_intr);
261 signal (SIGINT, terminate_intr);
262 signal (SIGPIPE, terminate_intr);
263 signal (SIGTERM, terminate_intr);
264 signal (SIGUSR1, terminate_intr);
265 signal (SIGUSR2, terminate_intr);
266}
267
Theodore Ts'o8820c792001-01-06 04:20:03 +0000268static void uncapture_terminate(void)
Theodore Ts'o4d003982000-04-03 16:01:11 +0000269{
270 terminate_addr = NULL;
271 signal (SIGHUP, SIG_DFL);
272 signal (SIGINT, SIG_DFL);
273 signal (SIGPIPE, SIG_DFL);
274 signal (SIGTERM, SIG_DFL);
275 signal (SIGUSR1, SIG_DFL);
276 signal (SIGUSR2, SIG_DFL);
277}
278
JP Abgralle0ed7402014-03-19 19:08:39 -0700279/* Linux requires that O_DIRECT I/Os be 512-byte sector aligned */
280
281#define O_DIRECT_SIZE 512
282
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400283static void set_o_direct(int dev, unsigned char *buffer, size_t size,
JP Abgralle0ed7402014-03-19 19:08:39 -0700284 ext2_loff_t offset)
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400285{
286#ifdef O_DIRECT
JP Abgralle0ed7402014-03-19 19:08:39 -0700287 static int current_O_DIRECT; /* Current status of O_DIRECT flag */
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400288 int new_flag = O_DIRECT;
289 int flag;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400290
JP Abgralle0ed7402014-03-19 19:08:39 -0700291 if ((use_buffered_io != 0) ||
292 (((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400293 ((size & (sys_page_size - 1)) != 0) ||
JP Abgralle0ed7402014-03-19 19:08:39 -0700294 ((offset & (O_DIRECT_SIZE - 1)) != 0))
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400295 new_flag = 0;
296
297 if (new_flag != current_O_DIRECT) {
Theodore Ts'odc058712003-07-25 07:39:33 -0400298 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400299 flag = fcntl(dev, F_GETFL);
300 if (flag > 0) {
301 flag = (flag & ~O_DIRECT) | new_flag;
302 fcntl(dev, F_SETFL, flag);
303 }
304 current_O_DIRECT = new_flag;
305 }
306#endif
307}
308
309
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400310static void pattern_fill(unsigned char *buffer, unsigned int pattern,
Theodore Ts'o84c05452003-05-18 01:11:52 -0400311 size_t n)
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400312{
Theodore Ts'o54434922003-12-07 01:28:50 -0500313 unsigned int i, nb;
Theodore Ts'o84c05452003-05-18 01:11:52 -0400314 unsigned char bpattern[sizeof(pattern)], *ptr;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400315
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400316 if (pattern == (unsigned int) ~0) {
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400317 for (ptr = buffer; ptr < buffer + n; ptr++) {
318 (*ptr) = random() % (1 << (8 * sizeof(char)));
319 }
320 if (s_flag | v_flag)
Theodore Ts'o54434922003-12-07 01:28:50 -0500321 fputs(_("Testing with random pattern: "), stderr);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400322 } else {
323 bpattern[0] = 0;
324 for (i = 0; i < sizeof(bpattern); i++) {
325 if (pattern == 0)
326 break;
327 bpattern[i] = pattern & 0xFF;
328 pattern = pattern >> 8;
329 }
330 nb = i ? (i-1) : 0;
331 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500332 *ptr = bpattern[i];
333 if (i == 0)
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400334 i = nb;
Theodore Ts'o54434922003-12-07 01:28:50 -0500335 else
336 i--;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400337 }
Theodore Ts'o84c05452003-05-18 01:11:52 -0400338 if (s_flag | v_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500339 fputs(_("Testing with pattern 0x"), stderr);
Theodore Ts'o84c05452003-05-18 01:11:52 -0400340 for (i = 0; i <= nb; i++)
341 fprintf(stderr, "%02x", buffer[i]);
Theodore Ts'o54434922003-12-07 01:28:50 -0500342 fputs(": ", stderr);
Theodore Ts'o84c05452003-05-18 01:11:52 -0400343 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400344 }
345}
346
Theodore Ts'o3839e651997-04-26 13:21:57 +0000347/*
Theodore Ts'o879ac922000-01-18 20:59:11 +0000348 * Perform a read of a sequence of blocks; return the number of blocks
349 * successfully sequentially read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000350 */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400351static int do_read (int dev, unsigned char * buffer, int try, int block_size,
352 blk_t current_block)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000353{
354 long got;
Iustin Pop264f64a2008-06-12 09:30:04 +0200355 struct timeval tv1, tv2;
356#define NANOSEC (1000000000L)
357#define MILISEC (1000L)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000358
JP Abgralle0ed7402014-03-19 19:08:39 -0700359#if 0
360 printf("do_read: block %d, try %d\n", current_block, try);
361#endif
362 set_o_direct(dev, buffer, try * block_size,
363 ((ext2_loff_t) current_block) * block_size);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400364
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000365 if (v_flag > 1)
366 print_status();
367
Theodore Ts'o3839e651997-04-26 13:21:57 +0000368 /* Seek to the correct loc. */
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000369 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000370 SEEK_SET) != (ext2_loff_t) current_block * block_size)
JP Abgralle0ed7402014-03-19 19:08:39 -0700371 com_err (program_name, errno, "%s", _("during seek"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000372
373 /* Try the read */
Iustin Pop264f64a2008-06-12 09:30:04 +0200374 if (d_flag)
Iustin Popedf261f2008-06-18 22:26:26 +0200375 gettimeofday(&tv1, NULL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000376 got = read (dev, buffer, try * block_size);
Iustin Pop264f64a2008-06-12 09:30:04 +0200377 if (d_flag)
Iustin Popedf261f2008-06-18 22:26:26 +0200378 gettimeofday(&tv2, NULL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000379 if (got < 0)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400380 got = 0;
Theodore Ts'o9f10a7b1999-07-16 10:41:36 +0000381 if (got & 511)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000382 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000383 got /= block_size;
Iustin Pop264f64a2008-06-12 09:30:04 +0200384 if (d_flag && got == try) {
Theodore Ts'oc13ab4f2008-07-13 16:17:57 -0400385#ifdef HAVE_NANOSLEEP
Iustin Pop264f64a2008-06-12 09:30:04 +0200386 struct timespec ts;
387 ts.tv_sec = tv2.tv_sec - tv1.tv_sec;
388 ts.tv_nsec = (tv2.tv_usec - tv1.tv_usec) * MILISEC;
389 if (ts.tv_nsec < 0) {
390 ts.tv_nsec += NANOSEC;
391 ts.tv_sec -= 1;
392 }
393 /* increase/decrease the sleep time based on d_flag value */
394 ts.tv_sec = ts.tv_sec * d_flag / 100;
395 ts.tv_nsec = ts.tv_nsec * d_flag / 100;
396 if (ts.tv_nsec > NANOSEC) {
397 ts.tv_sec += ts.tv_nsec / NANOSEC;
398 ts.tv_nsec %= NANOSEC;
399 }
400 if (ts.tv_sec || ts.tv_nsec)
401 nanosleep(&ts, NULL);
Theodore Ts'oc13ab4f2008-07-13 16:17:57 -0400402#else
403#ifdef HAVE_USLEEP
404 struct timeval tv;
405 tv.tv_sec = tv2.tv_sec - tv1.tv_sec;
406 tv.tv_usec = tv2.tv_usec - tv1.tv_usec;
407 tv.tv_sec = tv.tv_sec * d_flag / 100;
408 tv.tv_usec = tv.tv_usec * d_flag / 100;
409 if (tv.tv_usec > 1000000) {
410 tv.tv_sec += tv.tv_usec / 1000000;
411 tv.tv_usec %= 1000000;
412 }
413 if (tv.tv_sec)
414 sleep(tv.tv_sec);
415 if (tv.tv_usec)
416 usleep(tv.tv_usec);
417#endif
418#endif
Iustin Pop264f64a2008-06-12 09:30:04 +0200419 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000420 return got;
421}
422
Theodore Ts'o879ac922000-01-18 20:59:11 +0000423/*
424 * Perform a write of a sequence of blocks; return the number of blocks
425 * successfully sequentially written.
426 */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400427static int do_write(int dev, unsigned char * buffer, int try, int block_size,
428 unsigned long current_block)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000429{
430 long got;
431
JP Abgralle0ed7402014-03-19 19:08:39 -0700432#if 0
433 printf("do_write: block %lu, try %d\n", current_block, try);
434#endif
435 set_o_direct(dev, buffer, try * block_size,
436 ((ext2_loff_t) current_block) * block_size);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400437
Theodore Ts'o879ac922000-01-18 20:59:11 +0000438 if (v_flag > 1)
439 print_status();
440
441 /* Seek to the correct loc. */
442 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
443 SEEK_SET) != (ext2_loff_t) current_block * block_size)
JP Abgralle0ed7402014-03-19 19:08:39 -0700444 com_err (program_name, errno, "%s", _("during seek"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000445
446 /* Try the write */
447 got = write (dev, buffer, try * block_size);
448 if (got < 0)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400449 got = 0;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000450 if (got & 511)
Theodore Ts'o54434922003-12-07 01:28:50 -0500451 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000452 got /= block_size;
453 return got;
454}
455
456static int host_dev;
457
Theodore Ts'o4d404542001-01-11 16:04:59 +0000458static void flush_bufs(void)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000459{
Theodore Ts'o4d404542001-01-11 16:04:59 +0000460 errcode_t retval;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000461
JP Abgralle0ed7402014-03-19 19:08:39 -0700462#ifdef O_DIRECT
463 if (!use_buffered_io)
464 return;
465#endif
Theodore Ts'o4d404542001-01-11 16:04:59 +0000466 retval = ext2fs_sync_device(host_dev, 1);
467 if (retval)
JP Abgralle0ed7402014-03-19 19:08:39 -0700468 com_err(program_name, retval, "%s",
469 _("during ext2fs_sync_device"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000470}
471
Theodore Ts'oacd77412007-10-22 10:09:05 -0400472static unsigned int test_ro (int dev, blk_t last_block,
Theodore Ts'of56f32b2008-07-10 10:08:40 -0400473 int block_size, blk_t first_block,
Theodore Ts'oacd77412007-10-22 10:09:05 -0400474 unsigned int blocks_at_once)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475{
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400476 unsigned char * blkbuf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477 int try;
Theodore Ts'oacd77412007-10-22 10:09:05 -0400478 int got;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000479 unsigned int bb_count = 0;
480 errcode_t errcode;
JP Abgralle0ed7402014-03-19 19:08:39 -0700481 blk_t recover_block = ~0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000482
Theodore Ts'o83bfa272009-06-29 00:52:27 -0400483 /* set up abend handler */
484 capture_terminate(NULL);
485
Theodore Ts'o879ac922000-01-18 20:59:11 +0000486 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
487 if (errcode) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700488 com_err(program_name, errcode, "%s",
489 _("while beginning bad block list iteration"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000490 exit (1);
491 }
492 do {
493 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
Theodore Ts'of56f32b2008-07-10 10:08:40 -0400494 } while (next_bad && next_bad < first_block);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000495
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400496 if (t_flag) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400497 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400498 } else {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400499 blkbuf = allocate_buffer(blocks_at_once * block_size);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400500 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501 if (!blkbuf)
502 {
JP Abgralle0ed7402014-03-19 19:08:39 -0700503 com_err(program_name, ENOMEM, "%s",
504 _("while allocating buffers"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505 exit (1);
506 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000507 if (v_flag) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700508 fprintf(stderr, _("Checking blocks %lu to %lu\n"),
509 (unsigned long)first_block,
510 (unsigned long)last_block - 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000511 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400512 if (t_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500513 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400514 pattern_fill(blkbuf + blocks_at_once * block_size,
515 t_patts[0], block_size);
516 }
517 flush_bufs();
Theodore Ts'o879ac922000-01-18 20:59:11 +0000518 try = blocks_at_once;
Theodore Ts'of56f32b2008-07-10 10:08:40 -0400519 currently_testing = first_block;
Theodore Ts'o8938ce62006-10-03 23:35:57 -0400520 num_blocks = last_block - 1;
JP Abgralle0ed7402014-03-19 19:08:39 -0700521 if (!t_flag && (s_flag || v_flag))
Theodore Ts'o54434922003-12-07 01:28:50 -0500522 fputs(_("Checking for bad blocks (read-only test): "), stderr);
JP Abgralle0ed7402014-03-19 19:08:39 -0700523 if (s_flag && v_flag <= 1)
524 alarm_intr(SIGALRM);
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000525 while (currently_testing < last_block)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 {
Iustin Pop931b0282008-06-11 13:12:17 +0200527 if (max_bb && bb_count >= max_bb) {
528 if (s_flag || v_flag) {
529 fputs(_("Too many bad blocks, aborting test\n"), stderr);
530 }
531 break;
532 }
Theodore Ts'o879ac922000-01-18 20:59:11 +0000533 if (next_bad) {
534 if (currently_testing == next_bad) {
535 /* fprintf (out, "%lu\n", nextbad); */
536 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
537 currently_testing++;
538 continue;
539 }
540 else if (currently_testing + try > next_bad)
541 try = next_bad - currently_testing;
542 }
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000543 if (currently_testing + try > last_block)
544 try = last_block - currently_testing;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000545 got = do_read (dev, blkbuf, try, block_size, currently_testing);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400546 if (t_flag) {
547 /* test the comparison between all the
548 blocks successfully read */
549 int i;
550 for (i = 0; i < got; ++i)
551 if (memcmp (blkbuf+i*block_size,
552 blkbuf+blocks_at_once*block_size,
553 block_size))
JP Abgralle0ed7402014-03-19 19:08:39 -0700554 bb_count += bb_output(currently_testing + i, CORRUPTION_ERROR);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400555 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700556 if (got == 0 && try == 1)
557 bb_count += bb_output(currently_testing++, READ_ERROR);
Theodore Ts'o468d82f2011-02-17 22:58:21 -0500558 currently_testing += got;
JP Abgralle0ed7402014-03-19 19:08:39 -0700559 if (got != try) {
JP Abgrall65f0aab2014-03-06 13:50:20 -0800560 try = 1;
JP Abgralle0ed7402014-03-19 19:08:39 -0700561 if (recover_block == ~0U)
562 recover_block = currently_testing - got +
563 blocks_at_once;
564 continue;
565 } else if (currently_testing == recover_block) {
566 try = blocks_at_once;
567 recover_block = ~0;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000568 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000569 }
570 num_blocks = 0;
571 alarm(0);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400572 if (s_flag || v_flag)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400573 fputs(_(done_string), stderr);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000574
Theodore Ts'of3db3561997-04-26 13:34:30 +0000575 fflush (stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 free (blkbuf);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000577
578 ext2fs_badblocks_list_iterate_end(bb_iter);
579
Theodore Ts'o83bfa272009-06-29 00:52:27 -0400580 uncapture_terminate();
581
Theodore Ts'o879ac922000-01-18 20:59:11 +0000582 return bb_count;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000583}
584
Theodore Ts'oacd77412007-10-22 10:09:05 -0400585static unsigned int test_rw (int dev, blk_t last_block,
Theodore Ts'of56f32b2008-07-10 10:08:40 -0400586 int block_size, blk_t first_block,
Theodore Ts'oacd77412007-10-22 10:09:05 -0400587 unsigned int blocks_at_once)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588{
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400589 unsigned char *buffer, *read_buffer;
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400590 const unsigned int patterns[] = {0xaa, 0x55, 0xff, 0x00};
591 const unsigned int *pattern;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400592 int i, try, got, nr_pattern, pat_idx;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000593 unsigned int bb_count = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700594 blk_t recover_block = ~0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000595
Theodore Ts'o83bfa272009-06-29 00:52:27 -0400596 /* set up abend handler */
597 capture_terminate(NULL);
598
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400599 buffer = allocate_buffer(2 * blocks_at_once * block_size);
600 read_buffer = buffer + blocks_at_once * block_size;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400601
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400602 if (!buffer) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700603 com_err(program_name, ENOMEM, "%s",
604 _("while allocating buffers"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000605 exit (1);
606 }
607
Theodore Ts'o4d404542001-01-11 16:04:59 +0000608 flush_bufs();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000609
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000610 if (v_flag) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400611 fputs(_("Checking for bad blocks in read-write mode\n"),
Theodore Ts'o54434922003-12-07 01:28:50 -0500612 stderr);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000613 fprintf(stderr, _("From block %lu to %lu\n"),
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400614 (unsigned long) first_block,
Theodore Ts'of56f32b2008-07-10 10:08:40 -0400615 (unsigned long) last_block - 1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000616 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400617 if (t_flag) {
618 pattern = t_patts;
619 nr_pattern = t_flag;
620 } else {
621 pattern = patterns;
622 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
623 }
624 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400625 pattern_fill(buffer, pattern[pat_idx],
626 blocks_at_once * block_size);
Theodore Ts'o8938ce62006-10-03 23:35:57 -0400627 num_blocks = last_block - 1;
Theodore Ts'of56f32b2008-07-10 10:08:40 -0400628 currently_testing = first_block;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000629 if (s_flag && v_flag <= 1)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000630 alarm_intr(SIGALRM);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400631
632 try = blocks_at_once;
633 while (currently_testing < last_block) {
Iustin Pop931b0282008-06-11 13:12:17 +0200634 if (max_bb && bb_count >= max_bb) {
635 if (s_flag || v_flag) {
636 fputs(_("Too many bad blocks, aborting test\n"), stderr);
637 }
638 break;
639 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400640 if (currently_testing + try > last_block)
641 try = last_block - currently_testing;
642 got = do_write(dev, buffer, try, block_size,
643 currently_testing);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000644 if (v_flag > 1)
645 print_status();
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400646
JP Abgralle0ed7402014-03-19 19:08:39 -0700647 if (got == 0 && try == 1)
648 bb_count += bb_output(currently_testing++, WRITE_ERROR);
Theodore Ts'o468d82f2011-02-17 22:58:21 -0500649 currently_testing += got;
JP Abgralle0ed7402014-03-19 19:08:39 -0700650 if (got != try) {
JP Abgrall65f0aab2014-03-06 13:50:20 -0800651 try = 1;
JP Abgralle0ed7402014-03-19 19:08:39 -0700652 if (recover_block == ~0U)
653 recover_block = currently_testing -
654 got + blocks_at_once;
655 continue;
656 } else if (currently_testing == recover_block) {
657 try = blocks_at_once;
658 recover_block = ~0;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400659 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000660 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400661
Theodore Ts'of3db3561997-04-26 13:34:30 +0000662 num_blocks = 0;
663 alarm (0);
664 if (s_flag | v_flag)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400665 fputs(_(done_string), stderr);
Theodore Ts'o4d404542001-01-11 16:04:59 +0000666 flush_bufs();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000667 if (s_flag | v_flag)
Theodore Ts'o54434922003-12-07 01:28:50 -0500668 fputs(_("Reading and comparing: "), stderr);
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000669 num_blocks = last_block;
Theodore Ts'of56f32b2008-07-10 10:08:40 -0400670 currently_testing = first_block;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000671 if (s_flag && v_flag <= 1)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000672 alarm_intr(SIGALRM);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400673
674 try = blocks_at_once;
675 while (currently_testing < last_block) {
Iustin Pop931b0282008-06-11 13:12:17 +0200676 if (max_bb && bb_count >= max_bb) {
677 if (s_flag || v_flag) {
678 fputs(_("Too many bad blocks, aborting test\n"), stderr);
679 }
680 break;
681 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400682 if (currently_testing + try > last_block)
683 try = last_block - currently_testing;
684 got = do_read (dev, read_buffer, try, block_size,
685 currently_testing);
JP Abgralle0ed7402014-03-19 19:08:39 -0700686 if (got == 0 && try == 1)
687 bb_count += bb_output(currently_testing++, READ_ERROR);
688 currently_testing += got;
689 if (got != try) {
690 try = 1;
691 if (recover_block == ~0U)
692 recover_block = currently_testing -
693 got + blocks_at_once;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400694 continue;
JP Abgralle0ed7402014-03-19 19:08:39 -0700695 } else if (currently_testing == recover_block) {
696 try = blocks_at_once;
697 recover_block = ~0U;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400698 }
699 for (i=0; i < got; i++) {
700 if (memcmp(read_buffer + i * block_size,
701 buffer + i * block_size,
702 block_size))
JP Abgralle0ed7402014-03-19 19:08:39 -0700703 bb_count += bb_output(currently_testing+i, CORRUPTION_ERROR);
Theodore Ts'o468d82f2011-02-17 22:58:21 -0500704 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000705 if (v_flag > 1)
706 print_status();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000707 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400708
Theodore Ts'of3db3561997-04-26 13:34:30 +0000709 num_blocks = 0;
710 alarm (0);
711 if (s_flag | v_flag)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400712 fputs(_(done_string), stderr);
Theodore Ts'o4d404542001-01-11 16:04:59 +0000713 flush_bufs();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000714 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400715 uncapture_terminate();
Theodore Ts'o6d40f562003-05-07 08:35:38 -0400716 free(buffer);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000717 return bb_count;
718}
719
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000720struct saved_blk_record {
721 blk_t block;
722 int num;
723};
724
Theodore Ts'oacd77412007-10-22 10:09:05 -0400725static unsigned int test_nd (int dev, blk_t last_block,
Theodore Ts'of56f32b2008-07-10 10:08:40 -0400726 int block_size, blk_t first_block,
Theodore Ts'oacd77412007-10-22 10:09:05 -0400727 unsigned int blocks_at_once)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000728{
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400729 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400730 unsigned char *test_base, *save_base, *read_base;
Theodore Ts'odd018f52000-02-06 23:57:07 +0000731 int try, i;
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400732 const unsigned int patterns[] = { ~0 };
733 const unsigned int *pattern;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400734 int nr_pattern, pat_idx;
Theodore Ts'oacd77412007-10-22 10:09:05 -0400735 int got, used2, written;
736 blk_t save_currently_testing;
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000737 struct saved_blk_record *test_record;
Theodore Ts'oa551b782000-07-13 22:05:31 +0000738 /* This is static to prevent being clobbered by the longjmp */
739 static int num_saved;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000740 jmp_buf terminate_env;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000741 errcode_t errcode;
Theodore Ts'o54434922003-12-07 01:28:50 -0500742 unsigned long buf_used;
743 static unsigned int bb_count;
JP Abgralle0ed7402014-03-19 19:08:39 -0700744 unsigned int granularity = blocks_at_once;
745 blk_t recover_block = ~0U;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000746
Theodore Ts'o54434922003-12-07 01:28:50 -0500747 bb_count = 0;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000748 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
749 if (errcode) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700750 com_err(program_name, errcode, "%s",
751 _("while beginning bad block list iteration"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000752 exit (1);
753 }
754 do {
755 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
Theodore Ts'of56f32b2008-07-10 10:08:40 -0400756 } while (next_bad && next_bad < first_block);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000757
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400758 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
JP Abgralle0ed7402014-03-19 19:08:39 -0700759 test_record = malloc(blocks_at_once * sizeof(struct saved_blk_record));
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000760 if (!blkbuf || !test_record) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700761 com_err(program_name, ENOMEM, "%s",
762 _("while allocating buffers"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000763 exit (1);
764 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400765
766 save_base = blkbuf;
767 test_base = blkbuf + (blocks_at_once * block_size);
768 read_base = blkbuf + (2 * blocks_at_once * block_size);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400769
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000770 num_saved = 0;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000771
Theodore Ts'o4d404542001-01-11 16:04:59 +0000772 flush_bufs();
Theodore Ts'o879ac922000-01-18 20:59:11 +0000773 if (v_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500774 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400775 fprintf (stderr, _("From block %lu to %lu\n"),
Theodore Ts'of56f32b2008-07-10 10:08:40 -0400776 (unsigned long) first_block,
777 (unsigned long) last_block - 1);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000778 }
Theodore Ts'o879ac922000-01-18 20:59:11 +0000779 if (s_flag || v_flag > 1) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500780 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000781 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000782 if (setjmp(terminate_env)) {
783 /*
784 * Abnormal termination by a signal is handled here.
Theodore Ts'o4d003982000-04-03 16:01:11 +0000785 */
Theodore Ts'oa551b782000-07-13 22:05:31 +0000786 signal (SIGALRM, SIG_IGN);
Theodore Ts'o54434922003-12-07 01:28:50 -0500787 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000788
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400789 save_ptr = save_base;
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000790 for (i=0; i < num_saved; i++) {
791 do_write(dev, save_ptr, test_record[i].num,
792 block_size, test_record[i].block);
793 save_ptr += test_record[i].num * block_size;
794 }
Theodore Ts'o879ac922000-01-18 20:59:11 +0000795 fflush (out);
Theodore Ts'odd018f52000-02-06 23:57:07 +0000796 exit(1);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000797 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400798
Theodore Ts'o4d003982000-04-03 16:01:11 +0000799 /* set up abend handler */
800 capture_terminate(terminate_env);
801
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400802 if (t_flag) {
803 pattern = t_patts;
804 nr_pattern = t_flag;
805 } else {
806 pattern = patterns;
807 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
808 }
809 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400810 pattern_fill(test_base, pattern[pat_idx],
811 blocks_at_once * block_size);
Theodore Ts'o4d003982000-04-03 16:01:11 +0000812
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400813 buf_used = 0;
814 bb_count = 0;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400815 save_ptr = save_base;
816 test_ptr = test_base;
Theodore Ts'of56f32b2008-07-10 10:08:40 -0400817 currently_testing = first_block;
Theodore Ts'o8938ce62006-10-03 23:35:57 -0400818 num_blocks = last_block - 1;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400819 if (s_flag && v_flag <= 1)
820 alarm_intr(SIGALRM);
821
822 while (currently_testing < last_block) {
Iustin Pop931b0282008-06-11 13:12:17 +0200823 if (max_bb && bb_count >= max_bb) {
824 if (s_flag || v_flag) {
825 fputs(_("Too many bad blocks, aborting test\n"), stderr);
826 }
827 break;
828 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700829 got = try = granularity - buf_used;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400830 if (next_bad) {
831 if (currently_testing == next_bad) {
832 /* fprintf (out, "%lu\n", nextbad); */
833 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
834 currently_testing++;
835 goto check_for_more;
836 }
837 else if (currently_testing + try > next_bad)
838 try = next_bad - currently_testing;
839 }
840 if (currently_testing + try > last_block)
841 try = last_block - currently_testing;
842 got = do_read (dev, save_ptr, try, block_size,
843 currently_testing);
844 if (got == 0) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700845 if (recover_block == ~0U)
846 recover_block = currently_testing +
847 blocks_at_once;
848 if (granularity != 1) {
849 granularity = 1;
850 continue;
851 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400852 /* First block must have been bad. */
JP Abgralle0ed7402014-03-19 19:08:39 -0700853 bb_count += bb_output(currently_testing++, READ_ERROR);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000854 goto check_for_more;
Theodore Ts'o4d003982000-04-03 16:01:11 +0000855 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000856
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400857 /*
858 * Note the fact that we've saved this much data
859 * *before* we overwrite it with test data
860 */
861 test_record[num_saved].block = currently_testing;
862 test_record[num_saved].num = got;
863 num_saved++;
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000864
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400865 /* Write the test data */
866 written = do_write (dev, test_ptr, got, block_size,
867 currently_testing);
868 if (written != got)
869 com_err (program_name, errno,
870 _("during test data write, block %lu"),
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400871 (unsigned long) currently_testing +
Theodore Ts'oacd77412007-10-22 10:09:05 -0400872 written);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000873
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400874 buf_used += got;
Theodore Ts'o4d003982000-04-03 16:01:11 +0000875 save_ptr += got * block_size;
876 test_ptr += got * block_size;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400877 currently_testing += got;
JP Abgralle0ed7402014-03-19 19:08:39 -0700878 if (got != try) {
879 try = 1;
880 if (recover_block == ~0U)
881 recover_block = currently_testing -
882 got + blocks_at_once;
883 continue;
884 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400885
886 check_for_more:
887 /*
888 * If there's room for more blocks to be tested this
889 * around, and we're not done yet testing the disk, go
890 * back and get some more blocks.
891 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700892 if ((buf_used != granularity) &&
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400893 (currently_testing < last_block))
894 continue;
895
JP Abgralle0ed7402014-03-19 19:08:39 -0700896 if (currently_testing >= recover_block) {
897 granularity = blocks_at_once;
898 recover_block = ~0;
899 }
900
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400901 flush_bufs();
902 save_currently_testing = currently_testing;
903
904 /*
905 * for each contiguous block that we read into the
906 * buffer (and wrote test data into afterwards), read
907 * it back (looping if necessary, to get past newly
908 * discovered unreadable blocks, of which there should
909 * be none, but with a hard drive which is unreliable,
910 * it has happened), and compare with the test data
911 * that was written; output to the bad block list if
912 * it doesn't match.
913 */
914 used2 = 0;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400915 save_ptr = save_base;
916 test_ptr = test_base;
917 read_ptr = read_base;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400918 try = 0;
919
920 while (1) {
921 if (try == 0) {
922 if (used2 >= num_saved)
923 break;
924 currently_testing = test_record[used2].block;
925 try = test_record[used2].num;
926 used2++;
927 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400928
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400929 got = do_read (dev, read_ptr, try,
930 block_size, currently_testing);
931
932 /* test the comparison between all the
933 blocks successfully read */
934 for (i = 0; i < got; ++i)
935 if (memcmp (test_ptr+i*block_size,
936 read_ptr+i*block_size, block_size))
JP Abgralle0ed7402014-03-19 19:08:39 -0700937 bb_count += bb_output(currently_testing + i, CORRUPTION_ERROR);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400938 if (got < try) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700939 bb_count += bb_output(currently_testing + got, READ_ERROR);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400940 got++;
941 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400942
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400943 /* write back original data */
944 do_write (dev, save_ptr, got,
945 block_size, currently_testing);
946 save_ptr += got * block_size;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400947
948 currently_testing += got;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400949 test_ptr += got * block_size;
950 read_ptr += got * block_size;
951 try -= got;
952 }
953
954 /* empty the buffer so it can be reused */
955 num_saved = 0;
956 buf_used = 0;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400957 save_ptr = save_base;
958 test_ptr = test_base;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400959 currently_testing = save_currently_testing;
Theodore Ts'o4d003982000-04-03 16:01:11 +0000960 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400961 num_blocks = 0;
962 alarm(0);
963 if (s_flag || v_flag > 1)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400964 fputs(_(done_string), stderr);
Theodore Ts'o4d003982000-04-03 16:01:11 +0000965
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400966 flush_bufs();
Theodore Ts'o4d003982000-04-03 16:01:11 +0000967 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000968 uncapture_terminate();
Theodore Ts'odd018f52000-02-06 23:57:07 +0000969 fflush(stderr);
970 free(blkbuf);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000971 free(test_record);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000972
973 ext2fs_badblocks_list_iterate_end(bb_iter);
974
975 return bb_count;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000976}
977
Theodore Ts'o981dc562000-07-06 14:13:29 +0000978static void check_mount(char *device_name)
979{
980 errcode_t retval;
981 int mount_flags;
982
983 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
984 if (retval) {
985 com_err("ext2fs_check_if_mount", retval,
986 _("while determining whether %s is mounted."),
987 device_name);
988 return;
989 }
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400990 if (mount_flags & EXT2_MF_MOUNTED) {
991 fprintf(stderr, _("%s is mounted; "), device_name);
992 if (force) {
993 fputs(_("badblocks forced anyway. "
994 "Hope /etc/mtab is incorrect.\n"), stderr);
995 return;
996 }
997 abort_badblocks:
998 fputs(_("it's not safe to run badblocks!\n"), stderr);
999 exit(1);
Theodore Ts'o981dc562000-07-06 14:13:29 +00001000 }
Theodore Ts'o2fa8f372005-06-05 16:05:22 -04001001
Theodore Ts'of63978a2006-05-13 09:25:47 -04001002 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
Theodore Ts'o2fa8f372005-06-05 16:05:22 -04001003 fprintf(stderr, _("%s is apparently in use by the system; "),
1004 device_name);
1005 if (force)
1006 fputs(_("badblocks forced anyway.\n"), stderr);
1007 else
1008 goto abort_badblocks;
1009 }
1010
Theodore Ts'o981dc562000-07-06 14:13:29 +00001011}
1012
Theodore Ts'od4be9fa2007-10-22 10:19:20 -04001013/*
1014 * This function will convert a string to an unsigned long, printing
1015 * an error message if it fails, and returning success or failure in err.
1016 */
1017static unsigned int parse_uint(const char *str, const char *descr)
1018{
1019 char *tmp;
1020 unsigned long ret;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001021
Iustin Popeb594252008-06-11 17:55:18 +02001022 errno = 0;
Theodore Ts'od4be9fa2007-10-22 10:19:20 -04001023 ret = strtoul(str, &tmp, 0);
1024 if (*tmp || errno || (ret > UINT_MAX) ||
1025 (ret == ULONG_MAX && errno == ERANGE)) {
1026 com_err (program_name, 0, _("invalid %s - %s"), descr, str);
1027 exit (1);
1028 }
1029 return ret;
1030}
Theodore Ts'o981dc562000-07-06 14:13:29 +00001031
Theodore Ts'o00e54331997-09-16 02:13:52 +00001032int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001033{
Theodore Ts'o519149f1997-10-25 03:49:49 +00001034 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001035 char * device_name;
Theodore Ts'o879ac922000-01-18 20:59:11 +00001036 char * host_device_name = NULL;
1037 char * input_file = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001038 char * output_file = NULL;
Theodore Ts'o879ac922000-01-18 20:59:11 +00001039 FILE * in = NULL;
Theodore Ts'odd018f52000-02-06 23:57:07 +00001040 int block_size = 1024;
Theodore Ts'oacd77412007-10-22 10:09:05 -04001041 unsigned int blocks_at_once = 64;
JP Abgralle0ed7402014-03-19 19:08:39 -07001042 blk64_t last_block, first_block;
Theodore Ts'o879ac922000-01-18 20:59:11 +00001043 int num_passes = 0;
1044 int passes_clean = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001045 int dev;
Theodore Ts'o879ac922000-01-18 20:59:11 +00001046 errcode_t errcode;
Theodore Ts'oe9860ae2007-10-22 09:51:50 -04001047 unsigned int pattern;
Theodore Ts'oacd77412007-10-22 10:09:05 -04001048 unsigned int (*test_func)(int, blk_t,
1049 int, blk_t,
1050 unsigned int);
Theodore Ts'o22301af2008-09-02 08:29:20 -04001051 int open_flag;
Theodore Ts'o1c29b092003-07-12 16:01:45 -04001052 long sysval;
JP Abgralle0ed7402014-03-19 19:08:39 -07001053 blk64_t inblk;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001054
1055 setbuf(stdout, NULL);
1056 setbuf(stderr, NULL);
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001057#ifdef ENABLE_NLS
1058 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001059 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001060 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1061 textdomain(NLS_CAT_NAME);
JP Abgralle0ed7402014-03-19 19:08:39 -07001062 set_com_err_gettext(gettext);
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001063#endif
Theodore Ts'o6d40f562003-05-07 08:35:38 -04001064 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
Theodore Ts'o4d003982000-04-03 16:01:11 +00001065 test_func = test_ro;
Theodore Ts'o4d404542001-01-11 16:04:59 +00001066
Theodore Ts'o1c29b092003-07-12 16:01:45 -04001067 /* Determine the system page size if possible */
1068#ifdef HAVE_SYSCONF
1069#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1070#define _SC_PAGESIZE _SC_PAGE_SIZE
1071#endif
1072#ifdef _SC_PAGESIZE
1073 sysval = sysconf(_SC_PAGESIZE);
1074 if (sysval > 0)
1075 sys_page_size = sysval;
1076#endif /* _SC_PAGESIZE */
1077#endif /* HAVE_SYSCONF */
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001078
Theodore Ts'o3839e651997-04-26 13:21:57 +00001079 if (argc && *argv)
1080 program_name = *argv;
JP Abgralle0ed7402014-03-19 19:08:39 -07001081 while ((c = getopt (argc, argv, "b:d:e:fi:o:svwnc:p:h:t:BX")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001082 switch (c) {
1083 case 'b':
Theodore Ts'od4be9fa2007-10-22 10:19:20 -04001084 block_size = parse_uint(optarg, "block size");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001085 break;
Theodore Ts'o981dc562000-07-06 14:13:29 +00001086 case 'f':
1087 force++;
1088 break;
Theodore Ts'o879ac922000-01-18 20:59:11 +00001089 case 'i':
1090 input_file = optarg;
1091 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001092 case 'o':
1093 output_file = optarg;
1094 break;
1095 case 's':
1096 s_flag = 1;
1097 break;
1098 case 'v':
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001099 v_flag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001100 break;
1101 case 'w':
Theodore Ts'o4d003982000-04-03 16:01:11 +00001102 if (w_flag)
Theodore Ts'od8b5f772006-11-12 23:09:03 -05001103 exclusive_usage();
Theodore Ts'o4d003982000-04-03 16:01:11 +00001104 test_func = test_rw;
1105 w_flag = 1;
Theodore Ts'o879ac922000-01-18 20:59:11 +00001106 break;
1107 case 'n':
Theodore Ts'o4d003982000-04-03 16:01:11 +00001108 if (w_flag)
Theodore Ts'od8b5f772006-11-12 23:09:03 -05001109 exclusive_usage();
Theodore Ts'o4d003982000-04-03 16:01:11 +00001110 test_func = test_nd;
Theodore Ts'o879ac922000-01-18 20:59:11 +00001111 w_flag = 2;
1112 break;
1113 case 'c':
Theodore Ts'od4be9fa2007-10-22 10:19:20 -04001114 blocks_at_once = parse_uint(optarg, "blocks at once");
Theodore Ts'o879ac922000-01-18 20:59:11 +00001115 break;
Iustin Pop931b0282008-06-11 13:12:17 +02001116 case 'e':
1117 max_bb = parse_uint(optarg, "max bad block count");
1118 break;
Iustin Pop264f64a2008-06-12 09:30:04 +02001119 case 'd':
1120 d_flag = parse_uint(optarg, "read delay factor");
1121 break;
Theodore Ts'o879ac922000-01-18 20:59:11 +00001122 case 'p':
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001123 num_passes = parse_uint(optarg,
Theodore Ts'od4be9fa2007-10-22 10:19:20 -04001124 "number of clean passes");
Theodore Ts'o879ac922000-01-18 20:59:11 +00001125 break;
1126 case 'h':
1127 host_device_name = optarg;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001128 break;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001129 case 't':
1130 if (t_flag + 1 > t_max) {
Theodore Ts'oe9860ae2007-10-22 09:51:50 -04001131 unsigned int *t_patts_new;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001132
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001133 t_patts_new = realloc(t_patts, sizeof(int) *
Theodore Ts'o69d81352008-07-06 18:50:44 -04001134 (t_max + T_INC));
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001135 if (!t_patts_new) {
1136 com_err(program_name, ENOMEM,
1137 _("can't allocate memory for "
1138 "test_pattern - %s"),
1139 optarg);
1140 exit(1);
1141 }
1142 t_patts = t_patts_new;
1143 t_max += T_INC;
1144 }
Theodore Ts'o84c05452003-05-18 01:11:52 -04001145 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
1146 t_patts[t_flag++] = ~0;
1147 } else {
Theodore Ts'od4be9fa2007-10-22 10:19:20 -04001148 pattern = parse_uint(optarg, "test pattern");
Theodore Ts'oe9860ae2007-10-22 09:51:50 -04001149 if (pattern == (unsigned int) ~0)
Theodore Ts'o84c05452003-05-18 01:11:52 -04001150 pattern = 0xffff;
1151 t_patts[t_flag++] = pattern;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001152 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001153 break;
JP Abgralle0ed7402014-03-19 19:08:39 -07001154 case 'B':
1155 use_buffered_io = 1;
1156 break;
Theodore Ts'of63978a2006-05-13 09:25:47 -04001157 case 'X':
1158 exclusive_ok++;
1159 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001160 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +00001161 usage();
Theodore Ts'o3839e651997-04-26 13:21:57 +00001162 }
1163 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001164 if (!w_flag) {
1165 if (t_flag > 1) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001166 com_err(program_name, 0, "%s",
1167 _("Maximum of one test_pattern may be "
1168 "specified in read-only mode"));
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001169 exit(1);
1170 }
Theodore Ts'oe9860ae2007-10-22 09:51:50 -04001171 if (t_patts && (t_patts[0] == (unsigned int) ~0)) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001172 com_err(program_name, 0, "%s",
1173 _("Random test_pattern is not allowed "
1174 "in read-only mode"));
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001175 exit(1);
1176 }
1177 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001178 if (optind > argc - 1)
Theodore Ts'o818180c1998-06-27 05:11:14 +00001179 usage();
Theodore Ts'o3839e651997-04-26 13:21:57 +00001180 device_name = argv[optind++];
Theodore Ts'o35964b52000-07-06 13:19:43 +00001181 if (optind > argc - 1) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001182 errcode = ext2fs_get_device_size2(device_name,
Theodore Ts'o35964b52000-07-06 13:19:43 +00001183 block_size,
Theodore Ts'ocd130a02001-05-05 05:43:23 +00001184 &last_block);
Theodore Ts'o35964b52000-07-06 13:19:43 +00001185 if (errcode == EXT2_ET_UNIMPLEMENTED) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001186 com_err(program_name, 0, "%s",
Theodore Ts'o35964b52000-07-06 13:19:43 +00001187 _("Couldn't determine device size; you "
1188 "must specify\nthe size manually\n"));
1189 exit(1);
1190 }
1191 if (errcode) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001192 com_err(program_name, errcode, "%s",
Theodore Ts'o35964b52000-07-06 13:19:43 +00001193 _("while trying to determine device size"));
1194 exit(1);
1195 }
1196 } else {
Theodore Ts'o5267a522007-06-04 01:49:51 -04001197 errno = 0;
Theodore Ts'of56f32b2008-07-10 10:08:40 -04001198 last_block = parse_uint(argv[optind], _("last block"));
Theodore Ts'o5267a522007-06-04 01:49:51 -04001199 last_block++;
Theodore Ts'o35964b52000-07-06 13:19:43 +00001200 optind++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001201 }
Theodore Ts'o35964b52000-07-06 13:19:43 +00001202 if (optind <= argc-1) {
Theodore Ts'o5267a522007-06-04 01:49:51 -04001203 errno = 0;
Theodore Ts'of56f32b2008-07-10 10:08:40 -04001204 first_block = parse_uint(argv[optind], _("first block"));
1205 } else first_block = 0;
1206 if (first_block >= last_block) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001207 com_err (program_name, 0, _("invalid starting block (%llu): must be less than %llu"),
1208 first_block, last_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001209 exit (1);
1210 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001211 /* ext2 badblocks file can't handle large values */
1212 if (last_block >> 32) {
1213 com_err(program_name, EOVERFLOW,
1214 _("invalid end block (%llu): must be 32-bit value"),
1215 last_block);
1216 exit(1);
1217 }
Theodore Ts'o981dc562000-07-06 14:13:29 +00001218 if (w_flag)
1219 check_mount(device_name);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001220
Theodore Ts'o42e572b2008-10-01 20:00:54 -04001221 gettimeofday(&time_start, 0);
Theodore Ts'o22301af2008-09-02 08:29:20 -04001222 open_flag = O_LARGEFILE | (w_flag ? O_RDWR : O_RDONLY);
Theodore Ts'o1c29b092003-07-12 16:01:45 -04001223 dev = open (device_name, open_flag);
Theodore Ts'o5493a272002-01-02 14:15:35 -05001224 if (dev == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001225 com_err (program_name, errno, _("while trying to open %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001226 device_name);
1227 exit (1);
1228 }
Theodore Ts'o879ac922000-01-18 20:59:11 +00001229 if (host_device_name) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -04001230 host_dev = open (host_device_name, open_flag);
Theodore Ts'o5493a272002-01-02 14:15:35 -05001231 if (host_dev == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001232 com_err (program_name, errno,
1233 _("while trying to open %s"),
1234 host_device_name);
Theodore Ts'o879ac922000-01-18 20:59:11 +00001235 exit (1);
1236 }
1237 } else
1238 host_dev = dev;
Theodore Ts'o3e699062002-10-13 23:56:28 -04001239 if (input_file) {
Theodore Ts'o879ac922000-01-18 20:59:11 +00001240 if (strcmp (input_file, "-") == 0)
1241 in = stdin;
1242 else {
1243 in = fopen (input_file, "r");
1244 if (in == NULL)
1245 {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001246 com_err (program_name, errno,
1247 _("while trying to open %s"),
Theodore Ts'o879ac922000-01-18 20:59:11 +00001248 input_file);
1249 exit (1);
1250 }
1251 }
Theodore Ts'o3e699062002-10-13 23:56:28 -04001252 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001253 if (output_file && strcmp (output_file, "-") != 0)
1254 {
1255 out = fopen (output_file, "w");
1256 if (out == NULL)
1257 {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001258 com_err (program_name, errno,
1259 _("while trying to open %s"),
Theodore Ts'o879ac922000-01-18 20:59:11 +00001260 output_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001261 exit (1);
1262 }
1263 }
1264 else
1265 out = stdout;
Theodore Ts'o879ac922000-01-18 20:59:11 +00001266
1267 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1268 if (errcode) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001269 com_err(program_name, errcode, "%s",
1270 _("while creating in-memory bad blocks list"));
Theodore Ts'o879ac922000-01-18 20:59:11 +00001271 exit (1);
1272 }
1273
1274 if (in) {
1275 for(;;) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001276 switch (fscanf(in, "%llu\n", &inblk)) {
Theodore Ts'o879ac922000-01-18 20:59:11 +00001277 case 0:
JP Abgralle0ed7402014-03-19 19:08:39 -07001278 com_err(program_name, 0, "%s",
1279 _("input file - bad format"));
Theodore Ts'o879ac922000-01-18 20:59:11 +00001280 exit (1);
1281 case EOF:
1282 break;
1283 default:
JP Abgralle0ed7402014-03-19 19:08:39 -07001284 if (inblk >> 32) {
1285 com_err(program_name,
1286 EOVERFLOW, "%s",
1287 _("while adding to in-memory "
1288 "bad block list"));
1289 exit(1);
1290 }
1291 next_bad = inblk;
Theodore Ts'o879ac922000-01-18 20:59:11 +00001292 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1293 if (errcode) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001294 com_err(program_name, errcode,
1295 "%s",
1296 _("while adding to in-memory "
1297 "bad block list"));
Theodore Ts'o879ac922000-01-18 20:59:11 +00001298 exit (1);
1299 }
1300 continue;
1301 }
1302 break;
1303 }
1304
1305 if (in != stdin)
1306 fclose (in);
1307 }
1308
1309 do {
1310 unsigned int bb_count;
1311
Theodore Ts'ocd130a02001-05-05 05:43:23 +00001312 bb_count = test_func(dev, last_block, block_size,
Theodore Ts'of56f32b2008-07-10 10:08:40 -04001313 first_block, blocks_at_once);
Theodore Ts'o4d003982000-04-03 16:01:11 +00001314 if (bb_count)
1315 passes_clean = 0;
1316 else
1317 ++passes_clean;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001318
Theodore Ts'o879ac922000-01-18 20:59:11 +00001319 if (v_flag)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001320 fprintf(stderr,
JP Abgralle0ed7402014-03-19 19:08:39 -07001321 _("Pass completed, %u bad blocks found. (%d/%d/%d errors)\n"),
1322 bb_count, num_read_errors, num_write_errors, num_corruption_errors);
Theodore Ts'o879ac922000-01-18 20:59:11 +00001323
1324 } while (passes_clean < num_passes);
1325
Theodore Ts'o3839e651997-04-26 13:21:57 +00001326 close (dev);
1327 if (out != stdout)
1328 fclose (out);
Jim Meyering45e338f2009-02-23 18:07:50 +01001329 free(t_patts);
Theodore Ts'o879ac922000-01-18 20:59:11 +00001330 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001331}