2 * badblocks.c - Bad blocks checker
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)
8 * Copyright 1995, 1996, 1997, 1998, 1999 by Theodore Ts'o
9 * Copyright 1999 by David Beattie
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>
15 * This file may be redistributed under the terms of the GNU Public
22 * 93/05/26 - Creation from e2fsck
23 * 94/02/27 - Made a separate bad blocks checker
24 * 99/06/30...99/07/26 - Added non-destructive write-testing,
25 * configurable blocks-at-once parameter,
26 * loading of badblocks list to avoid testing
27 * blocks known to be bad, multiple passes to
28 * make sure that no new blocks are added to the
29 * list. (Work done by David Beattie)
32 #define _GNU_SOURCE /* for O_DIRECT */
56 #include <sys/ioctl.h>
57 #include <sys/types.h>
59 #include "et/com_err.h"
60 #include "ext2fs/ext2_io.h"
61 #include "ext2fs/ext2_fs.h"
62 #include "ext2fs/ext2fs.h"
63 #include "nls-enable.h"
65 const char * program_name = "badblocks";
66 const char * done_string = N_("done \n");
68 static int v_flag = 0; /* verbose */
69 static int w_flag = 0; /* do r/w test: 0=no, 1=yes,
70 * 2=non-destructive */
71 static int s_flag = 0; /* show progress of test */
72 static int force = 0; /* force check of mounted device */
73 static int t_flag = 0; /* number of test patterns */
74 static int t_max = 0; /* allocated test patterns */
75 static unsigned int *t_patts = NULL; /* test patterns */
76 static int current_O_DIRECT = 0; /* Current status of O_DIRECT flag */
77 static int exclusive_ok = 0;
78 static unsigned int max_bb = 0; /* Abort test if more than this number of bad blocks has been encountered */
79 static unsigned int d_flag = 0; /* delay factor between reads */
80 static struct timeval time_start;
84 unsigned int sys_page_size = 4096;
86 static void usage(void)
89 "Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwnf]\n"
90 " [-c blocks_at_once] [-d delay_factor_between_reads] [-e max_bad_blocks]\n"
91 " [-p num_passes] [-t test_pattern [-t test_pattern [...]]]\n"
92 " device [last_block [first_block]]\n"),
97 static void exclusive_usage(void)
100 _("%s: The -n and -w options are mutually exclusive.\n\n"),
105 static blk_t currently_testing = 0;
106 static blk_t num_blocks = 0;
107 static ext2_badblocks_list bb_list = NULL;
109 static blk_t next_bad = 0;
110 static ext2_badblocks_iterate bb_iter = NULL;
112 static void *allocate_buffer(size_t size)
116 #ifdef HAVE_POSIX_MEMALIGN
117 if (posix_memalign(&ret, sys_page_size, size) < 0)
121 ret = memalign(sys_page_size, size);
125 #endif /* HAVE_VALLOC */
126 #endif /* HAVE_MEMALIGN */
127 #endif /* HAVE_POSIX_MEMALIGN */
136 * This routine reports a new bad block. If the bad block has already
137 * been seen before, then it returns 0; otherwise it returns 1.
139 static int bb_output (blk_t bad)
143 if (ext2fs_badblocks_list_test(bb_list, bad))
146 fprintf(out, "%lu\n", (unsigned long) bad);
149 errcode = ext2fs_badblocks_list_add (bb_list, bad);
151 com_err (program_name, errcode, "adding to in-memory bad block list");
156 increment the iteration through the bb_list if
157 an element was just added before the current iteration
158 position. This should not cause next_bad to change. */
159 if (bb_iter && bad < next_bad)
160 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
164 static char *time_diff_format(struct timeval *tv1,
165 struct timeval *tv2, char *buf)
167 time_t diff = (tv1->tv_sec - tv2->tv_sec);
176 sprintf(buf, "%d:%02d:%02d", hr, min, sec);
178 sprintf(buf, "%d:%02d", min, sec);
182 static float calc_percent(unsigned long current, unsigned long total) {
186 if (current >= total) {
189 percent=(100.0*(float)current/(float)total);
194 static void print_status(void)
196 struct timeval time_end;
197 char diff_buf[32], line_buf[128];
200 gettimeofday(&time_end, 0);
201 len = snprintf(line_buf, sizeof(line_buf),
202 _("%6.2f%% done, %s elapsed"),
203 calc_percent((unsigned long) currently_testing,
204 (unsigned long) num_blocks),
205 time_diff_format(&time_end, &time_start, diff_buf));
206 fputs(line_buf, stderr);
207 memset(line_buf, '\b', len);
209 fputs(line_buf, stderr);
213 static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
215 signal (SIGALRM, alarm_intr);
222 static void *terminate_addr = NULL;
224 static void terminate_intr(int signo EXT2FS_ATTR((unused)))
226 fprintf(stderr, "\n\nInterrupted at block %llu\n",
227 (unsigned long long) currently_testing);
230 longjmp(terminate_addr,1);
234 static void capture_terminate(jmp_buf term_addr)
236 terminate_addr = term_addr;
237 signal (SIGHUP, terminate_intr);
238 signal (SIGINT, terminate_intr);
239 signal (SIGPIPE, terminate_intr);
240 signal (SIGTERM, terminate_intr);
241 signal (SIGUSR1, terminate_intr);
242 signal (SIGUSR2, terminate_intr);
245 static void uncapture_terminate(void)
247 terminate_addr = NULL;
248 signal (SIGHUP, SIG_DFL);
249 signal (SIGINT, SIG_DFL);
250 signal (SIGPIPE, SIG_DFL);
251 signal (SIGTERM, SIG_DFL);
252 signal (SIGUSR1, SIG_DFL);
253 signal (SIGUSR2, SIG_DFL);
256 static void set_o_direct(int dev, unsigned char *buffer, size_t size,
260 int new_flag = O_DIRECT;
263 if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
264 ((size & (sys_page_size - 1)) != 0) ||
265 ((current_block & ((sys_page_size >> 9)-1)) != 0))
268 if (new_flag != current_O_DIRECT) {
269 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
270 flag = fcntl(dev, F_GETFL);
272 flag = (flag & ~O_DIRECT) | new_flag;
273 fcntl(dev, F_SETFL, flag);
275 current_O_DIRECT = new_flag;
281 static void pattern_fill(unsigned char *buffer, unsigned int pattern,
285 unsigned char bpattern[sizeof(pattern)], *ptr;
287 if (pattern == (unsigned int) ~0) {
288 for (ptr = buffer; ptr < buffer + n; ptr++) {
289 (*ptr) = random() % (1 << (8 * sizeof(char)));
292 fputs(_("Testing with random pattern: "), stderr);
295 for (i = 0; i < sizeof(bpattern); i++) {
298 bpattern[i] = pattern & 0xFF;
299 pattern = pattern >> 8;
302 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
309 if (s_flag | v_flag) {
310 fputs(_("Testing with pattern 0x"), stderr);
311 for (i = 0; i <= nb; i++)
312 fprintf(stderr, "%02x", buffer[i]);
319 * Perform a read of a sequence of blocks; return the number of blocks
320 * successfully sequentially read.
322 static int do_read (int dev, unsigned char * buffer, int try, int block_size,
326 struct timeval tv1, tv2;
327 #define NANOSEC (1000000000L)
328 #define MILISEC (1000L)
330 set_o_direct(dev, buffer, try * block_size, current_block);
335 /* Seek to the correct loc. */
336 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
337 SEEK_SET) != (ext2_loff_t) current_block * block_size)
338 com_err (program_name, errno, _("during seek"));
342 gettimeofday(&tv1, NULL);
343 got = read (dev, buffer, try * block_size);
345 gettimeofday(&tv2, NULL);
349 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
351 if (d_flag && got == try) {
352 #ifdef HAVE_NANOSLEEP
354 ts.tv_sec = tv2.tv_sec - tv1.tv_sec;
355 ts.tv_nsec = (tv2.tv_usec - tv1.tv_usec) * MILISEC;
356 if (ts.tv_nsec < 0) {
357 ts.tv_nsec += NANOSEC;
360 /* increase/decrease the sleep time based on d_flag value */
361 ts.tv_sec = ts.tv_sec * d_flag / 100;
362 ts.tv_nsec = ts.tv_nsec * d_flag / 100;
363 if (ts.tv_nsec > NANOSEC) {
364 ts.tv_sec += ts.tv_nsec / NANOSEC;
365 ts.tv_nsec %= NANOSEC;
367 if (ts.tv_sec || ts.tv_nsec)
368 nanosleep(&ts, NULL);
372 tv.tv_sec = tv2.tv_sec - tv1.tv_sec;
373 tv.tv_usec = tv2.tv_usec - tv1.tv_usec;
374 tv.tv_sec = tv.tv_sec * d_flag / 100;
375 tv.tv_usec = tv.tv_usec * d_flag / 100;
376 if (tv.tv_usec > 1000000) {
377 tv.tv_sec += tv.tv_usec / 1000000;
378 tv.tv_usec %= 1000000;
391 * Perform a write of a sequence of blocks; return the number of blocks
392 * successfully sequentially written.
394 static int do_write(int dev, unsigned char * buffer, int try, int block_size,
395 unsigned long current_block)
399 set_o_direct(dev, buffer, try * block_size, current_block);
404 /* Seek to the correct loc. */
405 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
406 SEEK_SET) != (ext2_loff_t) current_block * block_size)
407 com_err (program_name, errno, _("during seek"));
410 got = write (dev, buffer, try * block_size);
414 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
421 static void flush_bufs(void)
425 retval = ext2fs_sync_device(host_dev, 1);
427 com_err(program_name, retval, _("during ext2fs_sync_device"));
430 static unsigned int test_ro (int dev, blk_t last_block,
431 int block_size, blk_t first_block,
432 unsigned int blocks_at_once)
434 unsigned char * blkbuf;
437 unsigned int bb_count = 0;
440 /* set up abend handler */
441 capture_terminate(NULL);
443 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
445 com_err (program_name, errcode,
446 _("while beginning bad block list iteration"));
450 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
451 } while (next_bad && next_bad < first_block);
454 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
456 blkbuf = allocate_buffer(blocks_at_once * block_size);
460 com_err (program_name, ENOMEM, _("while allocating buffers"));
464 fprintf (stderr, _("Checking blocks %lu to %lu\n"),
465 (unsigned long) first_block,
466 (unsigned long) last_block - 1);
469 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
470 pattern_fill(blkbuf + blocks_at_once * block_size,
471 t_patts[0], block_size);
474 try = blocks_at_once;
475 currently_testing = first_block;
476 num_blocks = last_block - 1;
477 if (!t_flag && (s_flag || v_flag)) {
478 fputs(_("Checking for bad blocks (read-only test): "), stderr);
482 while (currently_testing < last_block)
484 if (max_bb && bb_count >= max_bb) {
485 if (s_flag || v_flag) {
486 fputs(_("Too many bad blocks, aborting test\n"), stderr);
491 if (currently_testing == next_bad) {
492 /* fprintf (out, "%lu\n", nextbad); */
493 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
497 else if (currently_testing + try > next_bad)
498 try = next_bad - currently_testing;
500 if (currently_testing + try > last_block)
501 try = last_block - currently_testing;
502 got = do_read (dev, blkbuf, try, block_size, currently_testing);
504 /* test the comparison between all the
505 blocks successfully read */
507 for (i = 0; i < got; ++i)
508 if (memcmp (blkbuf+i*block_size,
509 blkbuf+blocks_at_once*block_size,
511 bb_count += bb_output(currently_testing + i);
513 currently_testing += got;
515 try = blocks_at_once;
516 /* recover page-aligned offset for O_DIRECT */
517 if ( (blocks_at_once >= sys_page_size >> 9)
518 && (currently_testing % (sys_page_size >> 9)!= 0))
519 try -= (sys_page_size >> 9)
521 % (sys_page_size >> 9));
527 bb_count += bb_output(currently_testing++);
532 if (s_flag || v_flag)
533 fputs(_(done_string), stderr);
538 ext2fs_badblocks_list_iterate_end(bb_iter);
540 uncapture_terminate();
545 static unsigned int test_rw (int dev, blk_t last_block,
546 int block_size, blk_t first_block,
547 unsigned int blocks_at_once)
549 unsigned char *buffer, *read_buffer;
550 const unsigned int patterns[] = {0xaa, 0x55, 0xff, 0x00};
551 const unsigned int *pattern;
552 int i, try, got, nr_pattern, pat_idx;
553 unsigned int bb_count = 0;
555 /* set up abend handler */
556 capture_terminate(NULL);
558 buffer = allocate_buffer(2 * blocks_at_once * block_size);
559 read_buffer = buffer + blocks_at_once * block_size;
562 com_err (program_name, ENOMEM, _("while allocating buffers"));
569 fputs(_("Checking for bad blocks in read-write mode\n"),
571 fprintf(stderr, _("From block %lu to %lu\n"),
572 (unsigned long) first_block,
573 (unsigned long) last_block - 1);
580 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
582 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
583 pattern_fill(buffer, pattern[pat_idx],
584 blocks_at_once * block_size);
585 num_blocks = last_block - 1;
586 currently_testing = first_block;
587 if (s_flag && v_flag <= 1)
590 try = blocks_at_once;
591 while (currently_testing < last_block) {
592 if (max_bb && bb_count >= max_bb) {
593 if (s_flag || v_flag) {
594 fputs(_("Too many bad blocks, aborting test\n"), stderr);
598 if (currently_testing + try > last_block)
599 try = last_block - currently_testing;
600 got = do_write(dev, buffer, try, block_size,
605 currently_testing += got;
607 try = blocks_at_once;
608 /* recover page-aligned offset for O_DIRECT */
609 if ( (blocks_at_once >= sys_page_size >> 9)
610 && (currently_testing %
611 (sys_page_size >> 9)!= 0))
612 try -= (sys_page_size >> 9)
614 % (sys_page_size >> 9));
619 bb_count += bb_output(currently_testing++);
626 fputs(_(done_string), stderr);
629 fputs(_("Reading and comparing: "), stderr);
630 num_blocks = last_block;
631 currently_testing = first_block;
632 if (s_flag && v_flag <= 1)
635 try = blocks_at_once;
636 while (currently_testing < last_block) {
637 if (max_bb && bb_count >= max_bb) {
638 if (s_flag || v_flag) {
639 fputs(_("Too many bad blocks, aborting test\n"), stderr);
643 if (currently_testing + try > last_block)
644 try = last_block - currently_testing;
645 got = do_read (dev, read_buffer, try, block_size,
648 bb_count += bb_output(currently_testing++);
651 for (i=0; i < got; i++) {
652 if (memcmp(read_buffer + i * block_size,
653 buffer + i * block_size,
655 bb_count += bb_output(currently_testing+i);
657 currently_testing += got;
658 /* recover page-aligned offset for O_DIRECT */
659 if ( (blocks_at_once >= sys_page_size >> 9)
660 && (currently_testing % (sys_page_size >> 9)!= 0))
661 try = blocks_at_once - (sys_page_size >> 9)
663 % (sys_page_size >> 9));
665 try = blocks_at_once;
673 fputs(_(done_string), stderr);
676 uncapture_terminate();
681 struct saved_blk_record {
686 static unsigned int test_nd (int dev, blk_t last_block,
687 int block_size, blk_t first_block,
688 unsigned int blocks_at_once)
690 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
691 unsigned char *test_base, *save_base, *read_base;
693 const unsigned int patterns[] = { ~0 };
694 const unsigned int *pattern;
695 int nr_pattern, pat_idx;
696 int got, used2, written;
697 blk_t save_currently_testing;
698 struct saved_blk_record *test_record;
699 /* This is static to prevent being clobbered by the longjmp */
700 static int num_saved;
701 jmp_buf terminate_env;
703 unsigned long buf_used;
704 static unsigned int bb_count;
707 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
709 com_err (program_name, errcode,
710 _("while beginning bad block list iteration"));
714 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
715 } while (next_bad && next_bad < first_block);
717 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
718 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
719 if (!blkbuf || !test_record) {
720 com_err(program_name, ENOMEM, _("while allocating buffers"));
725 test_base = blkbuf + (blocks_at_once * block_size);
726 read_base = blkbuf + (2 * blocks_at_once * block_size);
732 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
733 fprintf (stderr, _("From block %lu to %lu\n"),
734 (unsigned long) first_block,
735 (unsigned long) last_block - 1);
737 if (s_flag || v_flag > 1) {
738 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
740 if (setjmp(terminate_env)) {
742 * Abnormal termination by a signal is handled here.
744 signal (SIGALRM, SIG_IGN);
745 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
747 save_ptr = save_base;
748 for (i=0; i < num_saved; i++) {
749 do_write(dev, save_ptr, test_record[i].num,
750 block_size, test_record[i].block);
751 save_ptr += test_record[i].num * block_size;
757 /* set up abend handler */
758 capture_terminate(terminate_env);
765 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
767 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
768 pattern_fill(test_base, pattern[pat_idx],
769 blocks_at_once * block_size);
773 save_ptr = save_base;
774 test_ptr = test_base;
775 currently_testing = first_block;
776 num_blocks = last_block - 1;
777 if (s_flag && v_flag <= 1)
780 while (currently_testing < last_block) {
781 if (max_bb && bb_count >= max_bb) {
782 if (s_flag || v_flag) {
783 fputs(_("Too many bad blocks, aborting test\n"), stderr);
787 got = try = blocks_at_once - buf_used;
789 if (currently_testing == next_bad) {
790 /* fprintf (out, "%lu\n", nextbad); */
791 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
795 else if (currently_testing + try > next_bad)
796 try = next_bad - currently_testing;
798 if (currently_testing + try > last_block)
799 try = last_block - currently_testing;
800 got = do_read (dev, save_ptr, try, block_size,
803 /* First block must have been bad. */
804 bb_count += bb_output(currently_testing++);
809 * Note the fact that we've saved this much data
810 * *before* we overwrite it with test data
812 test_record[num_saved].block = currently_testing;
813 test_record[num_saved].num = got;
816 /* Write the test data */
817 written = do_write (dev, test_ptr, got, block_size,
820 com_err (program_name, errno,
821 _("during test data write, block %lu"),
822 (unsigned long) currently_testing +
826 save_ptr += got * block_size;
827 test_ptr += got * block_size;
828 currently_testing += got;
830 bb_count += bb_output(currently_testing++);
834 * If there's room for more blocks to be tested this
835 * around, and we're not done yet testing the disk, go
836 * back and get some more blocks.
838 if ((buf_used != blocks_at_once) &&
839 (currently_testing < last_block))
843 save_currently_testing = currently_testing;
846 * for each contiguous block that we read into the
847 * buffer (and wrote test data into afterwards), read
848 * it back (looping if necessary, to get past newly
849 * discovered unreadable blocks, of which there should
850 * be none, but with a hard drive which is unreliable,
851 * it has happened), and compare with the test data
852 * that was written; output to the bad block list if
856 save_ptr = save_base;
857 test_ptr = test_base;
858 read_ptr = read_base;
863 if (used2 >= num_saved)
865 currently_testing = test_record[used2].block;
866 try = test_record[used2].num;
870 got = do_read (dev, read_ptr, try,
871 block_size, currently_testing);
873 /* test the comparison between all the
874 blocks successfully read */
875 for (i = 0; i < got; ++i)
876 if (memcmp (test_ptr+i*block_size,
877 read_ptr+i*block_size, block_size))
878 bb_count += bb_output(currently_testing + i);
880 bb_count += bb_output(currently_testing + got);
884 /* write back original data */
885 do_write (dev, save_ptr, got,
886 block_size, currently_testing);
887 save_ptr += got * block_size;
889 currently_testing += got;
890 test_ptr += got * block_size;
891 read_ptr += got * block_size;
895 /* empty the buffer so it can be reused */
898 save_ptr = save_base;
899 test_ptr = test_base;
900 currently_testing = save_currently_testing;
904 if (s_flag || v_flag > 1)
905 fputs(_(done_string), stderr);
909 uncapture_terminate();
914 ext2fs_badblocks_list_iterate_end(bb_iter);
919 static void check_mount(char *device_name)
924 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
926 com_err("ext2fs_check_if_mount", retval,
927 _("while determining whether %s is mounted."),
931 if (mount_flags & EXT2_MF_MOUNTED) {
932 fprintf(stderr, _("%s is mounted; "), device_name);
934 fputs(_("badblocks forced anyway. "
935 "Hope /etc/mtab is incorrect.\n"), stderr);
939 fputs(_("it's not safe to run badblocks!\n"), stderr);
943 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
944 fprintf(stderr, _("%s is apparently in use by the system; "),
947 fputs(_("badblocks forced anyway.\n"), stderr);
949 goto abort_badblocks;
955 * This function will convert a string to an unsigned long, printing
956 * an error message if it fails, and returning success or failure in err.
958 static unsigned int parse_uint(const char *str, const char *descr)
964 ret = strtoul(str, &tmp, 0);
965 if (*tmp || errno || (ret > UINT_MAX) ||
966 (ret == ULONG_MAX && errno == ERANGE)) {
967 com_err (program_name, 0, _("invalid %s - %s"), descr, str);
973 int main (int argc, char ** argv)
977 char * host_device_name = NULL;
978 char * input_file = NULL;
979 char * output_file = NULL;
981 int block_size = 1024;
982 unsigned int blocks_at_once = 64;
983 blk_t last_block, first_block;
985 int passes_clean = 0;
988 unsigned int pattern;
989 unsigned int (*test_func)(int, blk_t,
995 setbuf(stdout, NULL);
996 setbuf(stderr, NULL);
998 setlocale(LC_MESSAGES, "");
999 setlocale(LC_CTYPE, "");
1000 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1001 textdomain(NLS_CAT_NAME);
1003 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
1004 test_func = test_ro;
1006 /* Determine the system page size if possible */
1008 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1009 #define _SC_PAGESIZE _SC_PAGE_SIZE
1012 sysval = sysconf(_SC_PAGESIZE);
1014 sys_page_size = sysval;
1015 #endif /* _SC_PAGESIZE */
1016 #endif /* HAVE_SYSCONF */
1019 program_name = *argv;
1020 while ((c = getopt (argc, argv, "b:d:e:fi:o:svwnc:p:h:t:X")) != EOF) {
1023 block_size = parse_uint(optarg, "block size");
1029 input_file = optarg;
1032 output_file = optarg;
1043 test_func = test_rw;
1049 test_func = test_nd;
1053 blocks_at_once = parse_uint(optarg, "blocks at once");
1056 max_bb = parse_uint(optarg, "max bad block count");
1059 d_flag = parse_uint(optarg, "read delay factor");
1062 num_passes = parse_uint(optarg,
1063 "number of clean passes");
1066 host_device_name = optarg;
1069 if (t_flag + 1 > t_max) {
1070 unsigned int *t_patts_new;
1072 t_patts_new = realloc(t_patts, sizeof(int) *
1075 com_err(program_name, ENOMEM,
1076 _("can't allocate memory for "
1077 "test_pattern - %s"),
1081 t_patts = t_patts_new;
1084 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
1085 t_patts[t_flag++] = ~0;
1087 pattern = parse_uint(optarg, "test pattern");
1088 if (pattern == (unsigned int) ~0)
1090 t_patts[t_flag++] = pattern;
1102 com_err(program_name, 0,
1103 _("Maximum of one test_pattern may be specified "
1104 "in read-only mode"));
1107 if (t_patts && (t_patts[0] == (unsigned int) ~0)) {
1108 com_err(program_name, 0,
1109 _("Random test_pattern is not allowed "
1110 "in read-only mode"));
1114 if (optind > argc - 1)
1116 device_name = argv[optind++];
1117 if (optind > argc - 1) {
1118 errcode = ext2fs_get_device_size(device_name,
1121 if (errcode == EXT2_ET_UNIMPLEMENTED) {
1122 com_err(program_name, 0,
1123 _("Couldn't determine device size; you "
1124 "must specify\nthe size manually\n"));
1128 com_err(program_name, errcode,
1129 _("while trying to determine device size"));
1134 last_block = parse_uint(argv[optind], _("last block"));
1138 if (optind <= argc-1) {
1140 first_block = parse_uint(argv[optind], _("first block"));
1141 } else first_block = 0;
1142 if (first_block >= last_block) {
1143 com_err (program_name, 0, _("invalid starting block (%lu): must be less than %lu"),
1144 (unsigned long) first_block, (unsigned long) last_block);
1148 check_mount(device_name);
1150 gettimeofday(&time_start, 0);
1151 open_flag = O_LARGEFILE | (w_flag ? O_RDWR : O_RDONLY);
1152 dev = open (device_name, open_flag);
1154 com_err (program_name, errno, _("while trying to open %s"),
1158 if (host_device_name) {
1159 host_dev = open (host_device_name, open_flag);
1160 if (host_dev == -1) {
1161 com_err (program_name, errno,
1162 _("while trying to open %s"),
1169 if (strcmp (input_file, "-") == 0)
1172 in = fopen (input_file, "r");
1175 com_err (program_name, errno,
1176 _("while trying to open %s"),
1182 if (output_file && strcmp (output_file, "-") != 0)
1184 out = fopen (output_file, "w");
1187 com_err (program_name, errno,
1188 _("while trying to open %s"),
1196 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1198 com_err (program_name, errcode,
1199 _("while creating in-memory bad blocks list"));
1205 switch(fscanf (in, "%u\n", &next_bad)) {
1207 com_err (program_name, 0, "input file - bad format");
1212 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1214 com_err (program_name, errcode, _("while adding to in-memory bad block list"));
1227 unsigned int bb_count;
1229 bb_count = test_func(dev, last_block, block_size,
1230 first_block, blocks_at_once);
1238 _("Pass completed, %u bad blocks found.\n"),
1241 } while (passes_clean < num_passes);