]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - misc/badblocks.c
ext2fs: rename "s_overhead_blocks" to "s_overhead_clusters"
[thirdparty/e2fsprogs.git] / misc / badblocks.c
CommitLineData
3839e657
TT
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 *
dd018f5a 8 * Copyright 1995, 1996, 1997, 1998, 1999 by Theodore Ts'o
879ac920 9 * Copyright 1999 by David Beattie
19c78dc0 10 *
3839e657
TT
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>
efc6f628 13 *
19c78dc0
TT
14 * %Begin-Header%
15 * This file may be redistributed under the terms of the GNU Public
16 * License.
17 * %End-Header%
3839e657
TT
18 */
19
20/*
21 * History:
22 * 93/05/26 - Creation from e2fsck
23 * 94/02/27 - Made a separate bad blocks checker
879ac920 24 * 99/06/30...99/07/26 - Added non-destructive write-testing,
dd018f5a
TT
25 * configurable blocks-at-once parameter,
26 * loading of badblocks list to avoid testing
efc6f628 27 * blocks known to be bad, multiple passes to
dd018f5a
TT
28 * make sure that no new blocks are added to the
29 * list. (Work done by David Beattie)
3839e657
TT
30 */
31
cf5301d7 32#ifndef _GNU_SOURCE
1c29b097 33#define _GNU_SOURCE /* for O_DIRECT */
cf5301d7 34#endif
1c29b097 35
d1154eb4 36#include "config.h"
3839e657
TT
37#include <errno.h>
38#include <fcntl.h>
a418d3ad 39#ifdef HAVE_GETOPT_H
3839e657 40#include <getopt.h>
373b8337
TT
41#else
42extern char *optarg;
43extern int optind;
a418d3ad 44#endif
3839e657
TT
45#include <signal.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <unistd.h>
879ac920 50#include <setjmp.h>
6d40f568 51#include <time.h>
5267a520 52#include <limits.h>
0c574883
TT
53#ifdef HAVE_MBSTOWCS
54#include <wchar.h>
55#endif
3839e657 56
edf261f6 57#include <sys/time.h>
3839e657 58#include <sys/ioctl.h>
f3db3566 59#include <sys/types.h>
3839e657 60
3839e657 61#include "et/com_err.h"
d40259fd 62#include "ext2fs/ext2_io.h"
54c637d4 63#include "ext2fs/ext2_fs.h"
879ac920 64#include "ext2fs/ext2fs.h"
99ceb8ec 65#include "support/nls-enable.h"
3839e657 66
f404167d
TT
67#ifndef O_LARGEFILE
68#define O_LARGEFILE 0
69#endif
70
fcc19b4a
JK
71/* Maximum number of bad blocks we support */
72#define MAX_BAD_BLOCKS (INT_MAX/2)
73
f404167d
TT
74static const char * program_name = "badblocks";
75static const char * done_string = N_("done \n");
3839e657 76
1d6fd6d0
AD
77static int v_flag; /* verbose */
78static int w_flag; /* do r/w test: 0=no, 1=yes,
4d003982 79 * 2=non-destructive */
1d6fd6d0
AD
80static int s_flag; /* show progress of test */
81static int force; /* force check of mounted device */
82static int t_flag; /* number of test patterns */
83static int t_max; /* allocated test patterns */
84static unsigned int *t_patts; /* test patterns */
85static int use_buffered_io;
86static int exclusive_ok;
fcc19b4a
JK
87static unsigned int max_bb = MAX_BAD_BLOCKS; /* Abort test if more than this
88 * number of bad blocks has been
89 * encountered */
1d6fd6d0 90static unsigned int d_flag; /* delay factor between reads */
504f7a29 91static struct timeval time_start;
1c29b097 92
849b6bc8 93#define T_INC 32
4d003982 94
f404167d 95static unsigned int sys_page_size = 4096;
1c29b097 96
8820c79f 97static void usage(void)
3839e657 98{
ad39bcd9 99 fprintf(stderr, _(
75dd3c47 100"Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwnfBX]\n"
ad39bcd9
BS
101" [-c blocks_at_once] [-d delay_factor_between_reads] [-e max_bad_blocks]\n"
102" [-p num_passes] [-t test_pattern [-t test_pattern [...]]]\n"
103" device [last_block [first_block]]\n"),
3839e657
TT
104 program_name);
105 exit (1);
106}
107
d8b5f777
TT
108static void exclusive_usage(void)
109{
efc6f628
TT
110 fprintf(stderr,
111 _("%s: The -n and -w options are mutually exclusive.\n\n"),
017a76ee
TT
112 program_name);
113 exit(1);
d8b5f777
TT
114}
115
acd77415
TT
116static blk_t currently_testing = 0;
117static blk_t num_blocks = 0;
89d45975
TT
118static blk_t num_read_errors = 0;
119static blk_t num_write_errors = 0;
120static blk_t num_corruption_errors = 0;
879ac920
TT
121static ext2_badblocks_list bb_list = NULL;
122static FILE *out;
123static blk_t next_bad = 0;
124static ext2_badblocks_iterate bb_iter = NULL;
19c78dc0 125
89d45975
TT
126enum error_types { READ_ERROR, WRITE_ERROR, CORRUPTION_ERROR };
127
1c29b097
TT
128static void *allocate_buffer(size_t size)
129{
130 void *ret = 0;
efc6f628 131
1c29b097 132#ifdef HAVE_POSIX_MEMALIGN
c2eedf43 133 if (posix_memalign(&ret, sys_page_size, size) != 0)
1c29b097
TT
134 ret = 0;
135#else
136#ifdef HAVE_MEMALIGN
137 ret = memalign(sys_page_size, size);
138#else
139#ifdef HAVE_VALLOC
140 ret = valloc(size);
141#endif /* HAVE_VALLOC */
efc6f628 142#endif /* HAVE_MEMALIGN */
1c29b097
TT
143#endif /* HAVE_POSIX_MEMALIGN */
144
145 if (!ret)
146 ret = malloc(size);
147
148 return ret;
149}
150
dd018f5a
TT
151/*
152 * This routine reports a new bad block. If the bad block has already
153 * been seen before, then it returns 0; otherwise it returns 1.
154 */
89d45975 155static int bb_output (blk_t bad, enum error_types error_type)
879ac920
TT
156{
157 errcode_t errcode;
158
dd018f5a
TT
159 if (ext2fs_badblocks_list_test(bb_list, bad))
160 return 0;
161
acd77415 162 fprintf(out, "%lu\n", (unsigned long) bad);
cc4f98ed 163 fflush(out);
879ac920
TT
164
165 errcode = ext2fs_badblocks_list_add (bb_list, bad);
166 if (errcode) {
167 com_err (program_name, errcode, "adding to in-memory bad block list");
168 exit (1);
169 }
170
171 /* kludge:
efc6f628 172 increment the iteration through the bb_list if
879ac920
TT
173 an element was just added before the current iteration
174 position. This should not cause next_bad to change. */
175 if (bb_iter && bad < next_bad)
176 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
89d45975
TT
177
178 if (error_type == READ_ERROR) {
179 num_read_errors++;
180 } else if (error_type == WRITE_ERROR) {
181 num_write_errors++;
182 } else if (error_type == CORRUPTION_ERROR) {
183 num_corruption_errors++;
184 }
dd018f5a 185 return 1;
879ac920
TT
186}
187
504f7a29
MK
188static char *time_diff_format(struct timeval *tv1,
189 struct timeval *tv2, char *buf)
190{
191 time_t diff = (tv1->tv_sec - tv2->tv_sec);
192 int hr,min,sec;
193
194 sec = diff % 60;
195 diff /= 60;
196 min = diff % 60;
197 hr = diff / 60;
198
199 if (hr)
200 sprintf(buf, "%d:%02d:%02d", hr, min, sec);
201 else
202 sprintf(buf, "%d:%02d", min, sec);
203 return buf;
204}
205
206static float calc_percent(unsigned long current, unsigned long total) {
207 float percent = 0.0;
208 if (total <= 0)
209 return percent;
210 if (current >= total) {
211 percent = 100.0;
212 } else {
213 percent=(100.0*(float)current/(float)total);
214 }
215 return percent;
216}
217
8820c79f 218static void print_status(void)
19c78dc0 219{
504f7a29
MK
220 struct timeval time_end;
221 char diff_buf[32], line_buf[128];
0c574883
TT
222#ifdef HAVE_MBSTOWCS
223 wchar_t wline_buf[128];
224#endif
504f7a29
MK
225 int len;
226
227 gettimeofday(&time_end, 0);
228 len = snprintf(line_buf, sizeof(line_buf),
89d45975
TT
229 _("%6.2f%% done, %s elapsed. "
230 "(%d/%d/%d errors)"),
504f7a29
MK
231 calc_percent((unsigned long) currently_testing,
232 (unsigned long) num_blocks),
89d45975
TT
233 time_diff_format(&time_end, &time_start, diff_buf),
234 num_read_errors,
235 num_write_errors,
236 num_corruption_errors);
61ef2474 237#ifdef HAVE_MBSTOWCS
0c574883
TT
238 mbstowcs(wline_buf, line_buf, sizeof(line_buf));
239 len = wcswidth(wline_buf, sizeof(line_buf));
240 if (len < 0)
241 len = strlen(line_buf); /* Should never happen... */
61ef2474 242#endif
504f7a29
MK
243 fputs(line_buf, stderr);
244 memset(line_buf, '\b', len);
245 line_buf[len] = 0;
246 fputs(line_buf, stderr);
19c78dc0
TT
247 fflush (stderr);
248}
249
54434927 250static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
19c78dc0
TT
251{
252 signal (SIGALRM, alarm_intr);
253 alarm(1);
254 if (!num_blocks)
255 return;
c76564a8 256 print_status();
19c78dc0
TT
257}
258
879ac920
TT
259static void *terminate_addr = NULL;
260
54434927 261static void terminate_intr(int signo EXT2FS_ATTR((unused)))
879ac920 262{
c510d6f2 263 fflush(out);
83bfa27d
TT
264 fprintf(stderr, "\n\nInterrupted at block %llu\n",
265 (unsigned long long) currently_testing);
266 fflush(stderr);
879ac920
TT
267 if (terminate_addr)
268 longjmp(terminate_addr,1);
269 exit(1);
270}
271
981dc56a 272static void capture_terminate(jmp_buf term_addr)
879ac920
TT
273{
274 terminate_addr = term_addr;
275 signal (SIGHUP, terminate_intr);
276 signal (SIGINT, terminate_intr);
277 signal (SIGPIPE, terminate_intr);
278 signal (SIGTERM, terminate_intr);
279 signal (SIGUSR1, terminate_intr);
280 signal (SIGUSR2, terminate_intr);
281}
282
8820c79f 283static void uncapture_terminate(void)
4d003982
TT
284{
285 terminate_addr = NULL;
286 signal (SIGHUP, SIG_DFL);
287 signal (SIGINT, SIG_DFL);
288 signal (SIGPIPE, SIG_DFL);
289 signal (SIGTERM, SIG_DFL);
290 signal (SIGUSR1, SIG_DFL);
291 signal (SIGUSR2, SIG_DFL);
292}
293
7d684035
TT
294/* Linux requires that O_DIRECT I/Os be 512-byte sector aligned */
295
296#define O_DIRECT_SIZE 512
297
1f9a60c2 298static void set_o_direct(int dev, unsigned char *buffer, size_t size,
7d684035 299 ext2_loff_t offset)
1c29b097
TT
300{
301#ifdef O_DIRECT
1d6fd6d0 302 static int current_O_DIRECT; /* Current status of O_DIRECT flag */
1c29b097
TT
303 int new_flag = O_DIRECT;
304 int flag;
efc6f628 305
e53e8fb0
TT
306 if ((use_buffered_io != 0) ||
307 (((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
1f9a60c2 308 ((size & (sys_page_size - 1)) != 0) ||
7d684035 309 ((offset & (O_DIRECT_SIZE - 1)) != 0))
1c29b097
TT
310 new_flag = 0;
311
312 if (new_flag != current_O_DIRECT) {
dc058719 313 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
1c29b097
TT
314 flag = fcntl(dev, F_GETFL);
315 if (flag > 0) {
316 flag = (flag & ~O_DIRECT) | new_flag;
56087556
TT
317 if (fcntl(dev, F_SETFL, flag) < 0)
318 perror("set_o_direct");
1c29b097
TT
319 }
320 current_O_DIRECT = new_flag;
321 }
322#endif
323}
324
325
e9860ae9 326static void pattern_fill(unsigned char *buffer, unsigned int pattern,
84c05457 327 size_t n)
849b6bc8 328{
54434927 329 unsigned int i, nb;
84c05457 330 unsigned char bpattern[sizeof(pattern)], *ptr;
efc6f628 331
e9860ae9 332 if (pattern == (unsigned int) ~0) {
849b6bc8
TT
333 for (ptr = buffer; ptr < buffer + n; ptr++) {
334 (*ptr) = random() % (1 << (8 * sizeof(char)));
335 }
336 if (s_flag | v_flag)
54434927 337 fputs(_("Testing with random pattern: "), stderr);
849b6bc8
TT
338 } else {
339 bpattern[0] = 0;
340 for (i = 0; i < sizeof(bpattern); i++) {
341 if (pattern == 0)
342 break;
343 bpattern[i] = pattern & 0xFF;
344 pattern = pattern >> 8;
345 }
346 nb = i ? (i-1) : 0;
347 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
54434927
TT
348 *ptr = bpattern[i];
349 if (i == 0)
849b6bc8 350 i = nb;
54434927
TT
351 else
352 i--;
849b6bc8 353 }
84c05457 354 if (s_flag | v_flag) {
54434927 355 fputs(_("Testing with pattern 0x"), stderr);
84c05457
TT
356 for (i = 0; i <= nb; i++)
357 fprintf(stderr, "%02x", buffer[i]);
54434927 358 fputs(": ", stderr);
84c05457 359 }
849b6bc8
TT
360 }
361}
362
3839e657 363/*
879ac920
TT
364 * Perform a read of a sequence of blocks; return the number of blocks
365 * successfully sequentially read.
3839e657 366 */
acd77415
TT
367static int do_read (int dev, unsigned char * buffer, int try, int block_size,
368 blk_t current_block)
3839e657
TT
369{
370 long got;
264f64a5
IP
371 struct timeval tv1, tv2;
372#define NANOSEC (1000000000L)
373#define MILISEC (1000L)
3839e657 374
468d82f4
TT
375#if 0
376 printf("do_read: block %d, try %d\n", current_block, try);
377#endif
7d684035
TT
378 set_o_direct(dev, buffer, try * block_size,
379 ((ext2_loff_t) current_block) * block_size);
1c29b097 380
19c78dc0
TT
381 if (v_flag > 1)
382 print_status();
383
3839e657 384 /* Seek to the correct loc. */
19c78dc0 385 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
f3db3566 386 SEEK_SET) != (ext2_loff_t) current_block * block_size)
45ff69ff 387 com_err (program_name, errno, "%s", _("during seek"));
3839e657
TT
388
389 /* Try the read */
264f64a5 390 if (d_flag)
edf261f6 391 gettimeofday(&tv1, NULL);
3839e657 392 got = read (dev, buffer, try * block_size);
264f64a5 393 if (d_flag)
edf261f6 394 gettimeofday(&tv2, NULL);
3839e657 395 if (got < 0)
efc6f628 396 got = 0;
9f10a7b3 397 if (got & 511)
d9c56d3c 398 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
879ac920 399 got /= block_size;
264f64a5 400 if (d_flag && got == try) {
c13ab4fa 401#ifdef HAVE_NANOSLEEP
264f64a5
IP
402 struct timespec ts;
403 ts.tv_sec = tv2.tv_sec - tv1.tv_sec;
404 ts.tv_nsec = (tv2.tv_usec - tv1.tv_usec) * MILISEC;
405 if (ts.tv_nsec < 0) {
406 ts.tv_nsec += NANOSEC;
407 ts.tv_sec -= 1;
408 }
409 /* increase/decrease the sleep time based on d_flag value */
410 ts.tv_sec = ts.tv_sec * d_flag / 100;
411 ts.tv_nsec = ts.tv_nsec * d_flag / 100;
412 if (ts.tv_nsec > NANOSEC) {
413 ts.tv_sec += ts.tv_nsec / NANOSEC;
414 ts.tv_nsec %= NANOSEC;
415 }
416 if (ts.tv_sec || ts.tv_nsec)
417 nanosleep(&ts, NULL);
c13ab4fa
TT
418#else
419#ifdef HAVE_USLEEP
420 struct timeval tv;
421 tv.tv_sec = tv2.tv_sec - tv1.tv_sec;
422 tv.tv_usec = tv2.tv_usec - tv1.tv_usec;
423 tv.tv_sec = tv.tv_sec * d_flag / 100;
424 tv.tv_usec = tv.tv_usec * d_flag / 100;
425 if (tv.tv_usec > 1000000) {
426 tv.tv_sec += tv.tv_usec / 1000000;
427 tv.tv_usec %= 1000000;
428 }
429 if (tv.tv_sec)
430 sleep(tv.tv_sec);
431 if (tv.tv_usec)
432 usleep(tv.tv_usec);
433#endif
434#endif
264f64a5 435 }
879ac920
TT
436 return got;
437}
438
439/*
440 * Perform a write of a sequence of blocks; return the number of blocks
441 * successfully sequentially written.
442 */
acd77415
TT
443static int do_write(int dev, unsigned char * buffer, int try, int block_size,
444 unsigned long current_block)
879ac920
TT
445{
446 long got;
447
468d82f4
TT
448#if 0
449 printf("do_write: block %lu, try %d\n", current_block, try);
450#endif
7d684035
TT
451 set_o_direct(dev, buffer, try * block_size,
452 ((ext2_loff_t) current_block) * block_size);
1c29b097 453
879ac920
TT
454 if (v_flag > 1)
455 print_status();
456
457 /* Seek to the correct loc. */
458 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
459 SEEK_SET) != (ext2_loff_t) current_block * block_size)
45ff69ff 460 com_err (program_name, errno, "%s", _("during seek"));
879ac920
TT
461
462 /* Try the write */
463 got = write (dev, buffer, try * block_size);
464 if (got < 0)
efc6f628 465 got = 0;
879ac920 466 if (got & 511)
54434927 467 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
3839e657
TT
468 got /= block_size;
469 return got;
470}
471
879ac920
TT
472static int host_dev;
473
4d404547 474static void flush_bufs(void)
a418d3ad 475{
4d404547 476 errcode_t retval;
a418d3ad 477
e53e8fb0
TT
478#ifdef O_DIRECT
479 if (!use_buffered_io)
480 return;
481#endif
4d404547
TT
482 retval = ext2fs_sync_device(host_dev, 1);
483 if (retval)
45ff69ff
AD
484 com_err(program_name, retval, "%s",
485 _("during ext2fs_sync_device"));
a418d3ad
TT
486}
487
acd77415 488static unsigned int test_ro (int dev, blk_t last_block,
f56f32b0 489 int block_size, blk_t first_block,
acd77415 490 unsigned int blocks_at_once)
3839e657 491{
48e6e813 492 unsigned char * blkbuf;
3839e657 493 int try;
acd77415 494 int got;
879ac920
TT
495 unsigned int bb_count = 0;
496 errcode_t errcode;
468d82f4 497 blk_t recover_block = ~0;
3839e657 498
83bfa27d
TT
499 /* set up abend handler */
500 capture_terminate(NULL);
501
879ac920
TT
502 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
503 if (errcode) {
45ff69ff
AD
504 com_err(program_name, errcode, "%s",
505 _("while beginning bad block list iteration"));
879ac920
TT
506 exit (1);
507 }
508 do {
509 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
f56f32b0 510 } while (next_bad && next_bad < first_block);
879ac920 511
849b6bc8 512 if (t_flag) {
1c29b097 513 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
849b6bc8 514 } else {
1c29b097 515 blkbuf = allocate_buffer(blocks_at_once * block_size);
849b6bc8 516 }
3839e657
TT
517 if (!blkbuf)
518 {
45ff69ff
AD
519 com_err(program_name, ENOMEM, "%s",
520 _("while allocating buffers"));
3839e657
TT
521 exit (1);
522 }
f3db3566 523 if (v_flag) {
45ff69ff
AD
524 fprintf(stderr, _("Checking blocks %lu to %lu\n"),
525 (unsigned long)first_block,
526 (unsigned long)last_block - 1);
f3db3566 527 }
849b6bc8 528 if (t_flag) {
54434927 529 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
849b6bc8
TT
530 pattern_fill(blkbuf + blocks_at_once * block_size,
531 t_patts[0], block_size);
532 }
533 flush_bufs();
879ac920 534 try = blocks_at_once;
f56f32b0 535 currently_testing = first_block;
8938ce64 536 num_blocks = last_block - 1;
4a4a44d7 537 if (!t_flag && (s_flag || v_flag))
54434927 538 fputs(_("Checking for bad blocks (read-only test): "), stderr);
4a4a44d7
TT
539 if (s_flag && v_flag <= 1)
540 alarm_intr(SIGALRM);
cd130a08 541 while (currently_testing < last_block)
3839e657 542 {
fcc19b4a 543 if (bb_count >= max_bb) {
931b0289
IP
544 if (s_flag || v_flag) {
545 fputs(_("Too many bad blocks, aborting test\n"), stderr);
546 }
547 break;
548 }
879ac920
TT
549 if (next_bad) {
550 if (currently_testing == next_bad) {
551 /* fprintf (out, "%lu\n", nextbad); */
552 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
553 currently_testing++;
554 continue;
555 }
556 else if (currently_testing + try > next_bad)
557 try = next_bad - currently_testing;
558 }
cd130a08
TT
559 if (currently_testing + try > last_block)
560 try = last_block - currently_testing;
879ac920 561 got = do_read (dev, blkbuf, try, block_size, currently_testing);
849b6bc8
TT
562 if (t_flag) {
563 /* test the comparison between all the
564 blocks successfully read */
565 int i;
566 for (i = 0; i < got; ++i)
567 if (memcmp (blkbuf+i*block_size,
568 blkbuf+blocks_at_once*block_size,
569 block_size))
89d45975 570 bb_count += bb_output(currently_testing + i, CORRUPTION_ERROR);
849b6bc8 571 }
468d82f4 572 if (got == 0 && try == 1)
89d45975 573 bb_count += bb_output(currently_testing++, READ_ERROR);
3839e657 574 currently_testing += got;
468d82f4 575 if (got != try) {
3839e657 576 try = 1;
577c773a 577 if (recover_block == ~0U)
468d82f4
TT
578 recover_block = currently_testing - got +
579 blocks_at_once;
580 continue;
581 } else if (currently_testing == recover_block) {
582 try = blocks_at_once;
583 recover_block = ~0;
879ac920 584 }
3839e657
TT
585 }
586 num_blocks = 0;
587 alarm(0);
849b6bc8 588 if (s_flag || v_flag)
3ef681c5 589 fputs(_(done_string), stderr);
879ac920 590
f3db3566 591 fflush (stderr);
3839e657 592 free (blkbuf);
879ac920
TT
593
594 ext2fs_badblocks_list_iterate_end(bb_iter);
595
83bfa27d
TT
596 uncapture_terminate();
597
879ac920 598 return bb_count;
3839e657
TT
599}
600
acd77415 601static unsigned int test_rw (int dev, blk_t last_block,
f56f32b0 602 int block_size, blk_t first_block,
acd77415 603 unsigned int blocks_at_once)
3839e657 604{
1c29b097 605 unsigned char *buffer, *read_buffer;
e9860ae9
TT
606 const unsigned int patterns[] = {0xaa, 0x55, 0xff, 0x00};
607 const unsigned int *pattern;
1c29b097 608 int i, try, got, nr_pattern, pat_idx;
879ac920 609 unsigned int bb_count = 0;
39791dc0 610 blk_t recover_block = ~0;
3839e657 611
83bfa27d
TT
612 /* set up abend handler */
613 capture_terminate(NULL);
614
1c29b097
TT
615 buffer = allocate_buffer(2 * blocks_at_once * block_size);
616 read_buffer = buffer + blocks_at_once * block_size;
efc6f628 617
1c29b097 618 if (!buffer) {
45ff69ff
AD
619 com_err(program_name, ENOMEM, "%s",
620 _("while allocating buffers"));
3839e657
TT
621 exit (1);
622 }
623
4d404547 624 flush_bufs();
a418d3ad 625
19c78dc0 626 if (v_flag) {
efc6f628 627 fputs(_("Checking for bad blocks in read-write mode\n"),
54434927 628 stderr);
d9c56d3c 629 fprintf(stderr, _("From block %lu to %lu\n"),
efc6f628 630 (unsigned long) first_block,
f56f32b0 631 (unsigned long) last_block - 1);
19c78dc0 632 }
849b6bc8
TT
633 if (t_flag) {
634 pattern = t_patts;
635 nr_pattern = t_flag;
636 } else {
637 pattern = patterns;
638 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
639 }
640 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
1c29b097
TT
641 pattern_fill(buffer, pattern[pat_idx],
642 blocks_at_once * block_size);
8938ce64 643 num_blocks = last_block - 1;
f56f32b0 644 currently_testing = first_block;
19c78dc0 645 if (s_flag && v_flag <= 1)
f3db3566 646 alarm_intr(SIGALRM);
1c29b097
TT
647
648 try = blocks_at_once;
649 while (currently_testing < last_block) {
fcc19b4a 650 if (bb_count >= max_bb) {
931b0289
IP
651 if (s_flag || v_flag) {
652 fputs(_("Too many bad blocks, aborting test\n"), stderr);
653 }
654 break;
655 }
1c29b097
TT
656 if (currently_testing + try > last_block)
657 try = last_block - currently_testing;
658 got = do_write(dev, buffer, try, block_size,
659 currently_testing);
19c78dc0
TT
660 if (v_flag > 1)
661 print_status();
1c29b097 662
468d82f4 663 if (got == 0 && try == 1)
89d45975 664 bb_count += bb_output(currently_testing++, WRITE_ERROR);
1c29b097 665 currently_testing += got;
468d82f4 666 if (got != try) {
1c29b097 667 try = 1;
577c773a 668 if (recover_block == ~0U)
468d82f4
TT
669 recover_block = currently_testing -
670 got + blocks_at_once;
671 continue;
672 } else if (currently_testing == recover_block) {
673 try = blocks_at_once;
674 recover_block = ~0;
1c29b097 675 }
3839e657 676 }
efc6f628 677
f3db3566
TT
678 num_blocks = 0;
679 alarm (0);
680 if (s_flag | v_flag)
3ef681c5 681 fputs(_(done_string), stderr);
4d404547 682 flush_bufs();
f3db3566 683 if (s_flag | v_flag)
54434927 684 fputs(_("Reading and comparing: "), stderr);
cd130a08 685 num_blocks = last_block;
f56f32b0 686 currently_testing = first_block;
19c78dc0 687 if (s_flag && v_flag <= 1)
f3db3566 688 alarm_intr(SIGALRM);
1c29b097
TT
689
690 try = blocks_at_once;
691 while (currently_testing < last_block) {
fcc19b4a 692 if (bb_count >= max_bb) {
931b0289
IP
693 if (s_flag || v_flag) {
694 fputs(_("Too many bad blocks, aborting test\n"), stderr);
695 }
696 break;
697 }
1c29b097
TT
698 if (currently_testing + try > last_block)
699 try = last_block - currently_testing;
700 got = do_read (dev, read_buffer, try, block_size,
701 currently_testing);
468d82f4 702 if (got == 0 && try == 1)
89d45975 703 bb_count += bb_output(currently_testing++, READ_ERROR);
468d82f4
TT
704 currently_testing += got;
705 if (got != try) {
706 try = 1;
577c773a 707 if (recover_block == ~0U)
468d82f4
TT
708 recover_block = currently_testing -
709 got + blocks_at_once;
1c29b097 710 continue;
468d82f4
TT
711 } else if (currently_testing == recover_block) {
712 try = blocks_at_once;
577c773a 713 recover_block = ~0U;
1c29b097
TT
714 }
715 for (i=0; i < got; i++) {
716 if (memcmp(read_buffer + i * block_size,
717 buffer + i * block_size,
718 block_size))
89d45975 719 bb_count += bb_output(currently_testing+i, CORRUPTION_ERROR);
468d82f4 720 }
19c78dc0
TT
721 if (v_flag > 1)
722 print_status();
3839e657 723 }
efc6f628 724
f3db3566
TT
725 num_blocks = 0;
726 alarm (0);
727 if (s_flag | v_flag)
3ef681c5 728 fputs(_(done_string), stderr);
4d404547 729 flush_bufs();
3839e657 730 }
849b6bc8 731 uncapture_terminate();
6d40f568 732 free(buffer);
879ac920
TT
733 return bb_count;
734}
735
d49a22b7
TT
736struct saved_blk_record {
737 blk_t block;
738 int num;
739};
740
acd77415 741static unsigned int test_nd (int dev, blk_t last_block,
f56f32b0 742 int block_size, blk_t first_block,
acd77415 743 unsigned int blocks_at_once)
879ac920 744{
48e6e813 745 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
1c29b097 746 unsigned char *test_base, *save_base, *read_base;
dd018f5a 747 int try, i;
e9860ae9
TT
748 const unsigned int patterns[] = { ~0 };
749 const unsigned int *pattern;
849b6bc8 750 int nr_pattern, pat_idx;
acd77415
TT
751 int got, used2, written;
752 blk_t save_currently_testing;
d49a22b7 753 struct saved_blk_record *test_record;
a551b783
TT
754 /* This is static to prevent being clobbered by the longjmp */
755 static int num_saved;
879ac920 756 jmp_buf terminate_env;
879ac920 757 errcode_t errcode;
54434927
TT
758 unsigned long buf_used;
759 static unsigned int bb_count;
577c773a
TT
760 unsigned int granularity = blocks_at_once;
761 blk_t recover_block = ~0U;
879ac920 762
54434927 763 bb_count = 0;
879ac920
TT
764 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
765 if (errcode) {
45ff69ff
AD
766 com_err(program_name, errcode, "%s",
767 _("while beginning bad block list iteration"));
879ac920
TT
768 exit (1);
769 }
770 do {
771 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
f56f32b0 772 } while (next_bad && next_bad < first_block);
879ac920 773
1c29b097 774 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
45ff69ff 775 test_record = malloc(blocks_at_once * sizeof(struct saved_blk_record));
d49a22b7 776 if (!blkbuf || !test_record) {
45ff69ff
AD
777 com_err(program_name, ENOMEM, "%s",
778 _("while allocating buffers"));
879ac920
TT
779 exit (1);
780 }
1c29b097
TT
781
782 save_base = blkbuf;
783 test_base = blkbuf + (blocks_at_once * block_size);
784 read_base = blkbuf + (2 * blocks_at_once * block_size);
efc6f628 785
d49a22b7 786 num_saved = 0;
879ac920 787
4d404547 788 flush_bufs();
879ac920 789 if (v_flag) {
54434927 790 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
efc6f628 791 fprintf (stderr, _("From block %lu to %lu\n"),
f56f32b0
TT
792 (unsigned long) first_block,
793 (unsigned long) last_block - 1);
879ac920 794 }
879ac920 795 if (s_flag || v_flag > 1) {
54434927 796 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
879ac920 797 }
4d003982
TT
798 if (setjmp(terminate_env)) {
799 /*
800 * Abnormal termination by a signal is handled here.
4d003982 801 */
a551b783 802 signal (SIGALRM, SIG_IGN);
54434927 803 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
879ac920 804
1c29b097 805 save_ptr = save_base;
d49a22b7
TT
806 for (i=0; i < num_saved; i++) {
807 do_write(dev, save_ptr, test_record[i].num,
808 block_size, test_record[i].block);
809 save_ptr += test_record[i].num * block_size;
810 }
879ac920 811 fflush (out);
dd018f5a 812 exit(1);
879ac920 813 }
efc6f628 814
4d003982
TT
815 /* set up abend handler */
816 capture_terminate(terminate_env);
817
849b6bc8
TT
818 if (t_flag) {
819 pattern = t_patts;
820 nr_pattern = t_flag;
821 } else {
822 pattern = patterns;
823 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
824 }
825 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
1c29b097
TT
826 pattern_fill(test_base, pattern[pat_idx],
827 blocks_at_once * block_size);
4d003982 828
849b6bc8
TT
829 buf_used = 0;
830 bb_count = 0;
1c29b097
TT
831 save_ptr = save_base;
832 test_ptr = test_base;
f56f32b0 833 currently_testing = first_block;
8938ce64 834 num_blocks = last_block - 1;
849b6bc8
TT
835 if (s_flag && v_flag <= 1)
836 alarm_intr(SIGALRM);
4d003982 837
849b6bc8 838 while (currently_testing < last_block) {
fcc19b4a 839 if (bb_count >= max_bb) {
931b0289
IP
840 if (s_flag || v_flag) {
841 fputs(_("Too many bad blocks, aborting test\n"), stderr);
842 }
843 break;
844 }
468d82f4 845 got = try = granularity - buf_used;
849b6bc8
TT
846 if (next_bad) {
847 if (currently_testing == next_bad) {
848 /* fprintf (out, "%lu\n", nextbad); */
849 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
850 currently_testing++;
851 goto check_for_more;
852 }
853 else if (currently_testing + try > next_bad)
854 try = next_bad - currently_testing;
d49a22b7 855 }
849b6bc8
TT
856 if (currently_testing + try > last_block)
857 try = last_block - currently_testing;
858 got = do_read (dev, save_ptr, try, block_size,
859 currently_testing);
860 if (got == 0) {
577c773a 861 if (recover_block == ~0U)
468d82f4
TT
862 recover_block = currently_testing +
863 blocks_at_once;
864 if (granularity != 1) {
865 granularity = 1;
866 continue;
867 }
849b6bc8 868 /* First block must have been bad. */
89d45975 869 bb_count += bb_output(currently_testing++, READ_ERROR);
849b6bc8 870 goto check_for_more;
4d003982 871 }
4d003982 872
849b6bc8
TT
873 /*
874 * Note the fact that we've saved this much data
875 * *before* we overwrite it with test data
876 */
877 test_record[num_saved].block = currently_testing;
878 test_record[num_saved].num = got;
879 num_saved++;
880
881 /* Write the test data */
882 written = do_write (dev, test_ptr, got, block_size,
883 currently_testing);
884 if (written != got)
885 com_err (program_name, errno,
886 _("during test data write, block %lu"),
efc6f628 887 (unsigned long) currently_testing +
acd77415 888 written);
849b6bc8
TT
889
890 buf_used += got;
4d003982
TT
891 save_ptr += got * block_size;
892 test_ptr += got * block_size;
849b6bc8 893 currently_testing += got;
468d82f4
TT
894 if (got != try) {
895 try = 1;
577c773a 896 if (recover_block == ~0U)
468d82f4
TT
897 recover_block = currently_testing -
898 got + blocks_at_once;
899 continue;
900 }
849b6bc8
TT
901
902 check_for_more:
903 /*
904 * If there's room for more blocks to be tested this
905 * around, and we're not done yet testing the disk, go
906 * back and get some more blocks.
907 */
468d82f4 908 if ((buf_used != granularity) &&
849b6bc8
TT
909 (currently_testing < last_block))
910 continue;
911
468d82f4
TT
912 if (currently_testing >= recover_block) {
913 granularity = blocks_at_once;
39791dc0 914 recover_block = ~0;
468d82f4
TT
915 }
916
849b6bc8
TT
917 flush_bufs();
918 save_currently_testing = currently_testing;
919
920 /*
921 * for each contiguous block that we read into the
922 * buffer (and wrote test data into afterwards), read
923 * it back (looping if necessary, to get past newly
924 * discovered unreadable blocks, of which there should
925 * be none, but with a hard drive which is unreliable,
926 * it has happened), and compare with the test data
927 * that was written; output to the bad block list if
928 * it doesn't match.
929 */
930 used2 = 0;
1c29b097
TT
931 save_ptr = save_base;
932 test_ptr = test_base;
933 read_ptr = read_base;
849b6bc8
TT
934 try = 0;
935
936 while (1) {
937 if (try == 0) {
938 if (used2 >= num_saved)
939 break;
940 currently_testing = test_record[used2].block;
941 try = test_record[used2].num;
942 used2++;
943 }
efc6f628 944
849b6bc8
TT
945 got = do_read (dev, read_ptr, try,
946 block_size, currently_testing);
947
948 /* test the comparison between all the
949 blocks successfully read */
950 for (i = 0; i < got; ++i)
951 if (memcmp (test_ptr+i*block_size,
952 read_ptr+i*block_size, block_size))
89d45975 953 bb_count += bb_output(currently_testing + i, CORRUPTION_ERROR);
849b6bc8 954 if (got < try) {
89d45975 955 bb_count += bb_output(currently_testing + got, READ_ERROR);
849b6bc8
TT
956 got++;
957 }
efc6f628 958
1c29b097
TT
959 /* write back original data */
960 do_write (dev, save_ptr, got,
961 block_size, currently_testing);
962 save_ptr += got * block_size;
849b6bc8
TT
963
964 currently_testing += got;
849b6bc8
TT
965 test_ptr += got * block_size;
966 read_ptr += got * block_size;
967 try -= got;
968 }
969
970 /* empty the buffer so it can be reused */
971 num_saved = 0;
972 buf_used = 0;
1c29b097
TT
973 save_ptr = save_base;
974 test_ptr = test_base;
849b6bc8 975 currently_testing = save_currently_testing;
4d003982 976 }
849b6bc8
TT
977 num_blocks = 0;
978 alarm(0);
979 if (s_flag || v_flag > 1)
3ef681c5 980 fputs(_(done_string), stderr);
4d003982 981
849b6bc8 982 flush_bufs();
4d003982 983 }
4d003982 984 uncapture_terminate();
dd018f5a
TT
985 fflush(stderr);
986 free(blkbuf);
d49a22b7 987 free(test_record);
879ac920
TT
988
989 ext2fs_badblocks_list_iterate_end(bb_iter);
990
991 return bb_count;
3839e657
TT
992}
993
981dc56a
TT
994static void check_mount(char *device_name)
995{
996 errcode_t retval;
997 int mount_flags;
998
999 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
1000 if (retval) {
1001 com_err("ext2fs_check_if_mount", retval,
1002 _("while determining whether %s is mounted."),
1003 device_name);
1004 return;
1005 }
2fa8f37f
TT
1006 if (mount_flags & EXT2_MF_MOUNTED) {
1007 fprintf(stderr, _("%s is mounted; "), device_name);
1008 if (force) {
1009 fputs(_("badblocks forced anyway. "
1010 "Hope /etc/mtab is incorrect.\n"), stderr);
1011 return;
1012 }
1013 abort_badblocks:
1014 fputs(_("it's not safe to run badblocks!\n"), stderr);
1015 exit(1);
1016 }
981dc56a 1017
f63978af 1018 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
2fa8f37f
TT
1019 fprintf(stderr, _("%s is apparently in use by the system; "),
1020 device_name);
1021 if (force)
1022 fputs(_("badblocks forced anyway.\n"), stderr);
1023 else
1024 goto abort_badblocks;
981dc56a 1025 }
2fa8f37f 1026
981dc56a
TT
1027}
1028
d4be9fad
TT
1029/*
1030 * This function will convert a string to an unsigned long, printing
1031 * an error message if it fails, and returning success or failure in err.
1032 */
1033static unsigned int parse_uint(const char *str, const char *descr)
1034{
1035 char *tmp;
1036 unsigned long ret;
efc6f628 1037
eb594251 1038 errno = 0;
d4be9fad
TT
1039 ret = strtoul(str, &tmp, 0);
1040 if (*tmp || errno || (ret > UINT_MAX) ||
1041 (ret == ULONG_MAX && errno == ERANGE)) {
1042 com_err (program_name, 0, _("invalid %s - %s"), descr, str);
1043 exit (1);
1044 }
1045 return ret;
1046}
981dc56a 1047
00e5433e 1048int main (int argc, char ** argv)
3839e657 1049{
519149fb 1050 int c;
3839e657 1051 char * device_name;
879ac920
TT
1052 char * host_device_name = NULL;
1053 char * input_file = NULL;
3839e657 1054 char * output_file = NULL;
879ac920 1055 FILE * in = NULL;
dd018f5a 1056 int block_size = 1024;
acd77415 1057 unsigned int blocks_at_once = 64;
5648c35a 1058 blk64_t last_block, first_block;
879ac920
TT
1059 int num_passes = 0;
1060 int passes_clean = 0;
3839e657 1061 int dev;
879ac920 1062 errcode_t errcode;
e9860ae9 1063 unsigned int pattern;
acd77415
TT
1064 unsigned int (*test_func)(int, blk_t,
1065 int, blk_t,
1066 unsigned int);
22301afb 1067 int open_flag;
1c29b097 1068 long sysval;
d87f198c 1069 blk64_t inblk;
3839e657
TT
1070
1071 setbuf(stdout, NULL);
1072 setbuf(stderr, NULL);
d9c56d3c
TT
1073#ifdef ENABLE_NLS
1074 setlocale(LC_MESSAGES, "");
14308a53 1075 setlocale(LC_CTYPE, "");
d9c56d3c
TT
1076 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1077 textdomain(NLS_CAT_NAME);
9d4507c5 1078 set_com_err_gettext(gettext);
d9c56d3c 1079#endif
6d40f568 1080 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
4d003982 1081 test_func = test_ro;
4d404547 1082
1c29b097
TT
1083 /* Determine the system page size if possible */
1084#ifdef HAVE_SYSCONF
1085#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1086#define _SC_PAGESIZE _SC_PAGE_SIZE
1087#endif
1088#ifdef _SC_PAGESIZE
1089 sysval = sysconf(_SC_PAGESIZE);
1090 if (sysval > 0)
1091 sys_page_size = sysval;
1092#endif /* _SC_PAGESIZE */
1093#endif /* HAVE_SYSCONF */
efc6f628 1094
3839e657
TT
1095 if (argc && *argv)
1096 program_name = *argv;
e53e8fb0 1097 while ((c = getopt (argc, argv, "b:d:e:fi:o:svwnc:p:h:t:BX")) != EOF) {
3839e657
TT
1098 switch (c) {
1099 case 'b':
d4be9fad 1100 block_size = parse_uint(optarg, "block size");
3839e657 1101 break;
981dc56a
TT
1102 case 'f':
1103 force++;
1104 break;
879ac920
TT
1105 case 'i':
1106 input_file = optarg;
1107 break;
3839e657
TT
1108 case 'o':
1109 output_file = optarg;
1110 break;
1111 case 's':
1112 s_flag = 1;
1113 break;
1114 case 'v':
19c78dc0 1115 v_flag++;
3839e657
TT
1116 break;
1117 case 'w':
4d003982 1118 if (w_flag)
d8b5f777 1119 exclusive_usage();
4d003982
TT
1120 test_func = test_rw;
1121 w_flag = 1;
879ac920
TT
1122 break;
1123 case 'n':
4d003982 1124 if (w_flag)
d8b5f777 1125 exclusive_usage();
4d003982 1126 test_func = test_nd;
879ac920
TT
1127 w_flag = 2;
1128 break;
1129 case 'c':
d4be9fad 1130 blocks_at_once = parse_uint(optarg, "blocks at once");
879ac920 1131 break;
931b0289
IP
1132 case 'e':
1133 max_bb = parse_uint(optarg, "max bad block count");
fcc19b4a
JK
1134 if (max_bb > MAX_BAD_BLOCKS) {
1135 com_err (program_name, 0,
1136 _("Too big max bad blocks count %u - "
1137 "maximum is %u"), max_bb,
1138 MAX_BAD_BLOCKS);
1139 exit (1);
1140 }
1141 /* 0 really means unlimited but we cannot do that much... */
1142 if (max_bb == 0)
1143 max_bb = MAX_BAD_BLOCKS;
931b0289 1144 break;
264f64a5
IP
1145 case 'd':
1146 d_flag = parse_uint(optarg, "read delay factor");
1147 break;
879ac920 1148 case 'p':
efc6f628 1149 num_passes = parse_uint(optarg,
d4be9fad 1150 "number of clean passes");
879ac920
TT
1151 break;
1152 case 'h':
1153 host_device_name = optarg;
3839e657 1154 break;
849b6bc8
TT
1155 case 't':
1156 if (t_flag + 1 > t_max) {
e9860ae9 1157 unsigned int *t_patts_new;
849b6bc8 1158
efc6f628 1159 t_patts_new = realloc(t_patts, sizeof(int) *
69d81352 1160 (t_max + T_INC));
849b6bc8
TT
1161 if (!t_patts_new) {
1162 com_err(program_name, ENOMEM,
1163 _("can't allocate memory for "
1164 "test_pattern - %s"),
1165 optarg);
1166 exit(1);
1167 }
1168 t_patts = t_patts_new;
1169 t_max += T_INC;
1170 }
84c05457
TT
1171 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
1172 t_patts[t_flag++] = ~0;
1173 } else {
d4be9fad 1174 pattern = parse_uint(optarg, "test pattern");
e9860ae9 1175 if (pattern == (unsigned int) ~0)
84c05457
TT
1176 pattern = 0xffff;
1177 t_patts[t_flag++] = pattern;
849b6bc8 1178 }
849b6bc8 1179 break;
e53e8fb0
TT
1180 case 'B':
1181 use_buffered_io = 1;
1182 break;
f63978af
TT
1183 case 'X':
1184 exclusive_ok++;
1185 break;
3839e657 1186 default:
818180cd 1187 usage();
3839e657
TT
1188 }
1189 }
849b6bc8
TT
1190 if (!w_flag) {
1191 if (t_flag > 1) {
45ff69ff
AD
1192 com_err(program_name, 0, "%s",
1193 _("Maximum of one test_pattern may be "
1194 "specified in read-only mode"));
849b6bc8
TT
1195 exit(1);
1196 }
e9860ae9 1197 if (t_patts && (t_patts[0] == (unsigned int) ~0)) {
45ff69ff
AD
1198 com_err(program_name, 0, "%s",
1199 _("Random test_pattern is not allowed "
1200 "in read-only mode"));
849b6bc8
TT
1201 exit(1);
1202 }
1203 }
3839e657 1204 if (optind > argc - 1)
818180cd 1205 usage();
3839e657 1206 device_name = argv[optind++];
35964b5c 1207 if (optind > argc - 1) {
5648c35a 1208 errcode = ext2fs_get_device_size2(device_name,
35964b5c 1209 block_size,
cd130a08 1210 &last_block);
35964b5c 1211 if (errcode == EXT2_ET_UNIMPLEMENTED) {
45ff69ff 1212 com_err(program_name, 0, "%s",
35964b5c
TT
1213 _("Couldn't determine device size; you "
1214 "must specify\nthe size manually\n"));
1215 exit(1);
1216 }
1217 if (errcode) {
45ff69ff 1218 com_err(program_name, errcode, "%s",
35964b5c
TT
1219 _("while trying to determine device size"));
1220 exit(1);
1221 }
1222 } else {
5267a520 1223 errno = 0;
f56f32b0 1224 last_block = parse_uint(argv[optind], _("last block"));
5267a520 1225 last_block++;
35964b5c 1226 optind++;
3839e657 1227 }
35964b5c 1228 if (optind <= argc-1) {
5267a520 1229 errno = 0;
f56f32b0
TT
1230 first_block = parse_uint(argv[optind], _("first block"));
1231 } else first_block = 0;
1232 if (first_block >= last_block) {
d87f198c
DW
1233 com_err (program_name, 0, _("invalid starting block (%llu): must be less than %llu"),
1234 first_block, last_block);
f3db3566
TT
1235 exit (1);
1236 }
d87f198c
DW
1237 /* ext2 badblocks file can't handle large values */
1238 if (last_block >> 32) {
1239 com_err(program_name, EOVERFLOW,
1240 _("invalid end block (%llu): must be 32-bit value"),
1241 last_block);
1242 exit(1);
1243 }
981dc56a
TT
1244 if (w_flag)
1245 check_mount(device_name);
efc6f628 1246
42e572b5 1247 gettimeofday(&time_start, 0);
22301afb 1248 open_flag = O_LARGEFILE | (w_flag ? O_RDWR : O_RDONLY);
1c29b097 1249 dev = open (device_name, open_flag);
5493a27d 1250 if (dev == -1) {
d9c56d3c 1251 com_err (program_name, errno, _("while trying to open %s"),
3839e657
TT
1252 device_name);
1253 exit (1);
1254 }
879ac920 1255 if (host_device_name) {
1c29b097 1256 host_dev = open (host_device_name, open_flag);
5493a27d 1257 if (host_dev == -1) {
d9c56d3c
TT
1258 com_err (program_name, errno,
1259 _("while trying to open %s"),
1260 host_device_name);
879ac920
TT
1261 exit (1);
1262 }
1263 } else
1264 host_dev = dev;
3e699064 1265 if (input_file) {
879ac920
TT
1266 if (strcmp (input_file, "-") == 0)
1267 in = stdin;
1268 else {
1269 in = fopen (input_file, "r");
1270 if (in == NULL)
1271 {
d9c56d3c
TT
1272 com_err (program_name, errno,
1273 _("while trying to open %s"),
879ac920
TT
1274 input_file);
1275 exit (1);
1276 }
1277 }
3e699064 1278 }
3839e657
TT
1279 if (output_file && strcmp (output_file, "-") != 0)
1280 {
1281 out = fopen (output_file, "w");
1282 if (out == NULL)
1283 {
d9c56d3c
TT
1284 com_err (program_name, errno,
1285 _("while trying to open %s"),
879ac920 1286 output_file);
3839e657
TT
1287 exit (1);
1288 }
1289 }
1290 else
1291 out = stdout;
879ac920
TT
1292
1293 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1294 if (errcode) {
45ff69ff
AD
1295 com_err(program_name, errcode, "%s",
1296 _("while creating in-memory bad blocks list"));
879ac920
TT
1297 exit (1);
1298 }
1299
1300 if (in) {
1301 for(;;) {
d87f198c 1302 switch (fscanf(in, "%llu\n", &inblk)) {
879ac920 1303 case 0:
45ff69ff
AD
1304 com_err(program_name, 0, "%s",
1305 _("input file - bad format"));
879ac920
TT
1306 exit (1);
1307 case EOF:
1308 break;
1309 default:
d87f198c
DW
1310 if (inblk >> 32) {
1311 com_err(program_name,
45ff69ff
AD
1312 EOVERFLOW, "%s",
1313 _("while adding to in-memory "
1314 "bad block list"));
d87f198c
DW
1315 exit(1);
1316 }
1317 next_bad = inblk;
879ac920
TT
1318 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1319 if (errcode) {
45ff69ff
AD
1320 com_err(program_name, errcode,
1321 "%s",
1322 _("while adding to in-memory "
1323 "bad block list"));
879ac920
TT
1324 exit (1);
1325 }
1326 continue;
1327 }
1328 break;
1329 }
1330
1331 if (in != stdin)
1332 fclose (in);
1333 }
1334
1335 do {
1336 unsigned int bb_count;
1337
cd130a08 1338 bb_count = test_func(dev, last_block, block_size,
f56f32b0 1339 first_block, blocks_at_once);
4d003982
TT
1340 if (bb_count)
1341 passes_clean = 0;
1342 else
1343 ++passes_clean;
efc6f628 1344
879ac920 1345 if (v_flag)
d9c56d3c 1346 fprintf(stderr,
89d45975
TT
1347 _("Pass completed, %u bad blocks found. (%d/%d/%d errors)\n"),
1348 bb_count, num_read_errors, num_write_errors, num_corruption_errors);
879ac920
TT
1349
1350 } while (passes_clean < num_passes);
1351
3839e657
TT
1352 close (dev);
1353 if (out != stdout)
1354 fclose (out);
45e338f5 1355 free(t_patts);
879ac920 1356 return 0;
3839e657 1357}