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));
207 len = mbstowcs(NULL, line_buf, sizeof(line_buf));
209 fputs(line_buf, stderr);
210 memset(line_buf, '\b', len);
212 fputs(line_buf, stderr);
216 static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
218 signal (SIGALRM, alarm_intr);
225 static void *terminate_addr = NULL;
227 static void terminate_intr(int signo EXT2FS_ATTR((unused)))
230 fprintf(stderr, "\n\nInterrupted at block %llu\n",
231 (unsigned long long) currently_testing);
234 longjmp(terminate_addr,1);
238 static void capture_terminate(jmp_buf term_addr)
240 terminate_addr = term_addr;
241 signal (SIGHUP, terminate_intr);
242 signal (SIGINT, terminate_intr);
243 signal (SIGPIPE, terminate_intr);
244 signal (SIGTERM, terminate_intr);
245 signal (SIGUSR1, terminate_intr);
246 signal (SIGUSR2, terminate_intr);
249 static void uncapture_terminate(void)
251 terminate_addr = NULL;
252 signal (SIGHUP, SIG_DFL);
253 signal (SIGINT, SIG_DFL);
254 signal (SIGPIPE, SIG_DFL);
255 signal (SIGTERM, SIG_DFL);
256 signal (SIGUSR1, SIG_DFL);
257 signal (SIGUSR2, SIG_DFL);
260 static void set_o_direct(int dev, unsigned char *buffer, size_t size,
264 int new_flag = O_DIRECT;
267 if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
268 ((size & (sys_page_size - 1)) != 0) ||
269 ((current_block & ((sys_page_size >> 9)-1)) != 0))
272 if (new_flag != current_O_DIRECT) {
273 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
274 flag = fcntl(dev, F_GETFL);
276 flag = (flag & ~O_DIRECT) | new_flag;
277 fcntl(dev, F_SETFL, flag);
279 current_O_DIRECT = new_flag;
285 static void pattern_fill(unsigned char *buffer, unsigned int pattern,
289 unsigned char bpattern[sizeof(pattern)], *ptr;
291 if (pattern == (unsigned int) ~0) {
292 for (ptr = buffer; ptr < buffer + n; ptr++) {
293 (*ptr) = random() % (1 << (8 * sizeof(char)));
296 fputs(_("Testing with random pattern: "), stderr);
299 for (i = 0; i < sizeof(bpattern); i++) {
302 bpattern[i] = pattern & 0xFF;
303 pattern = pattern >> 8;
306 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
313 if (s_flag | v_flag) {
314 fputs(_("Testing with pattern 0x"), stderr);
315 for (i = 0; i <= nb; i++)
316 fprintf(stderr, "%02x", buffer[i]);
323 * Perform a read of a sequence of blocks; return the number of blocks
324 * successfully sequentially read.
326 static int do_read (int dev, unsigned char * buffer, int try, int block_size,
330 struct timeval tv1, tv2;
331 #define NANOSEC (1000000000L)
332 #define MILISEC (1000L)
334 set_o_direct(dev, buffer, try * block_size, current_block);
339 /* Seek to the correct loc. */
340 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
341 SEEK_SET) != (ext2_loff_t) current_block * block_size)
342 com_err (program_name, errno, _("during seek"));
346 gettimeofday(&tv1, NULL);
347 got = read (dev, buffer, try * block_size);
349 gettimeofday(&tv2, NULL);
353 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
355 if (d_flag && got == try) {
356 #ifdef HAVE_NANOSLEEP
358 ts.tv_sec = tv2.tv_sec - tv1.tv_sec;
359 ts.tv_nsec = (tv2.tv_usec - tv1.tv_usec) * MILISEC;
360 if (ts.tv_nsec < 0) {
361 ts.tv_nsec += NANOSEC;
364 /* increase/decrease the sleep time based on d_flag value */
365 ts.tv_sec = ts.tv_sec * d_flag / 100;
366 ts.tv_nsec = ts.tv_nsec * d_flag / 100;
367 if (ts.tv_nsec > NANOSEC) {
368 ts.tv_sec += ts.tv_nsec / NANOSEC;
369 ts.tv_nsec %= NANOSEC;
371 if (ts.tv_sec || ts.tv_nsec)
372 nanosleep(&ts, NULL);
376 tv.tv_sec = tv2.tv_sec - tv1.tv_sec;
377 tv.tv_usec = tv2.tv_usec - tv1.tv_usec;
378 tv.tv_sec = tv.tv_sec * d_flag / 100;
379 tv.tv_usec = tv.tv_usec * d_flag / 100;
380 if (tv.tv_usec > 1000000) {
381 tv.tv_sec += tv.tv_usec / 1000000;
382 tv.tv_usec %= 1000000;
395 * Perform a write of a sequence of blocks; return the number of blocks
396 * successfully sequentially written.
398 static int do_write(int dev, unsigned char * buffer, int try, int block_size,
399 unsigned long current_block)
403 set_o_direct(dev, buffer, try * block_size, current_block);
408 /* Seek to the correct loc. */
409 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
410 SEEK_SET) != (ext2_loff_t) current_block * block_size)
411 com_err (program_name, errno, _("during seek"));
414 got = write (dev, buffer, try * block_size);
418 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
425 static void flush_bufs(void)
429 retval = ext2fs_sync_device(host_dev, 1);
431 com_err(program_name, retval, _("during ext2fs_sync_device"));
434 static unsigned int test_ro (int dev, blk_t last_block,
435 int block_size, blk_t first_block,
436 unsigned int blocks_at_once)
438 unsigned char * blkbuf;
441 unsigned int bb_count = 0;
444 /* set up abend handler */
445 capture_terminate(NULL);
447 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
449 com_err (program_name, errcode,
450 _("while beginning bad block list iteration"));
454 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
455 } while (next_bad && next_bad < first_block);
458 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
460 blkbuf = allocate_buffer(blocks_at_once * block_size);
464 com_err (program_name, ENOMEM, _("while allocating buffers"));
468 fprintf (stderr, _("Checking blocks %lu to %lu\n"),
469 (unsigned long) first_block,
470 (unsigned long) last_block - 1);
473 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
474 pattern_fill(blkbuf + blocks_at_once * block_size,
475 t_patts[0], block_size);
478 try = blocks_at_once;
479 currently_testing = first_block;
480 num_blocks = last_block - 1;
481 if (!t_flag && (s_flag || v_flag)) {
482 fputs(_("Checking for bad blocks (read-only test): "), stderr);
486 while (currently_testing < last_block)
488 if (max_bb && bb_count >= max_bb) {
489 if (s_flag || v_flag) {
490 fputs(_("Too many bad blocks, aborting test\n"), stderr);
495 if (currently_testing == next_bad) {
496 /* fprintf (out, "%lu\n", nextbad); */
497 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
501 else if (currently_testing + try > next_bad)
502 try = next_bad - currently_testing;
504 if (currently_testing + try > last_block)
505 try = last_block - currently_testing;
506 got = do_read (dev, blkbuf, try, block_size, currently_testing);
508 /* test the comparison between all the
509 blocks successfully read */
511 for (i = 0; i < got; ++i)
512 if (memcmp (blkbuf+i*block_size,
513 blkbuf+blocks_at_once*block_size,
515 bb_count += bb_output(currently_testing + i);
517 currently_testing += got;
519 try = blocks_at_once;
520 /* recover page-aligned offset for O_DIRECT */
521 if ( (blocks_at_once >= sys_page_size >> 9)
522 && (currently_testing % (sys_page_size >> 9)!= 0))
523 try -= (sys_page_size >> 9)
525 % (sys_page_size >> 9));
531 bb_count += bb_output(currently_testing++);
536 if (s_flag || v_flag)
537 fputs(_(done_string), stderr);
542 ext2fs_badblocks_list_iterate_end(bb_iter);
544 uncapture_terminate();
549 static unsigned int test_rw (int dev, blk_t last_block,
550 int block_size, blk_t first_block,
551 unsigned int blocks_at_once)
553 unsigned char *buffer, *read_buffer;
554 const unsigned int patterns[] = {0xaa, 0x55, 0xff, 0x00};
555 const unsigned int *pattern;
556 int i, try, got, nr_pattern, pat_idx;
557 unsigned int bb_count = 0;
559 /* set up abend handler */
560 capture_terminate(NULL);
562 buffer = allocate_buffer(2 * blocks_at_once * block_size);
563 read_buffer = buffer + blocks_at_once * block_size;
566 com_err (program_name, ENOMEM, _("while allocating buffers"));
573 fputs(_("Checking for bad blocks in read-write mode\n"),
575 fprintf(stderr, _("From block %lu to %lu\n"),
576 (unsigned long) first_block,
577 (unsigned long) last_block - 1);
584 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
586 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
587 pattern_fill(buffer, pattern[pat_idx],
588 blocks_at_once * block_size);
589 num_blocks = last_block - 1;
590 currently_testing = first_block;
591 if (s_flag && v_flag <= 1)
594 try = blocks_at_once;
595 while (currently_testing < last_block) {
596 if (max_bb && bb_count >= max_bb) {
597 if (s_flag || v_flag) {
598 fputs(_("Too many bad blocks, aborting test\n"), stderr);
602 if (currently_testing + try > last_block)
603 try = last_block - currently_testing;
604 got = do_write(dev, buffer, try, block_size,
609 currently_testing += got;
611 try = blocks_at_once;
612 /* recover page-aligned offset for O_DIRECT */
613 if ( (blocks_at_once >= sys_page_size >> 9)
614 && (currently_testing %
615 (sys_page_size >> 9)!= 0))
616 try -= (sys_page_size >> 9)
618 % (sys_page_size >> 9));
623 bb_count += bb_output(currently_testing++);
630 fputs(_(done_string), stderr);
633 fputs(_("Reading and comparing: "), stderr);
634 num_blocks = last_block;
635 currently_testing = first_block;
636 if (s_flag && v_flag <= 1)
639 try = blocks_at_once;
640 while (currently_testing < last_block) {
641 if (max_bb && bb_count >= max_bb) {
642 if (s_flag || v_flag) {
643 fputs(_("Too many bad blocks, aborting test\n"), stderr);
647 if (currently_testing + try > last_block)
648 try = last_block - currently_testing;
649 got = do_read (dev, read_buffer, try, block_size,
652 bb_count += bb_output(currently_testing++);
655 for (i=0; i < got; i++) {
656 if (memcmp(read_buffer + i * block_size,
657 buffer + i * block_size,
659 bb_count += bb_output(currently_testing+i);
661 currently_testing += got;
662 /* recover page-aligned offset for O_DIRECT */
663 if ( (blocks_at_once >= sys_page_size >> 9)
664 && (currently_testing % (sys_page_size >> 9)!= 0))
665 try = blocks_at_once - (sys_page_size >> 9)
667 % (sys_page_size >> 9));
669 try = blocks_at_once;
677 fputs(_(done_string), stderr);
680 uncapture_terminate();
685 struct saved_blk_record {
690 static unsigned int test_nd (int dev, blk_t last_block,
691 int block_size, blk_t first_block,
692 unsigned int blocks_at_once)
694 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
695 unsigned char *test_base, *save_base, *read_base;
697 const unsigned int patterns[] = { ~0 };
698 const unsigned int *pattern;
699 int nr_pattern, pat_idx;
700 int got, used2, written;
701 blk_t save_currently_testing;
702 struct saved_blk_record *test_record;
703 /* This is static to prevent being clobbered by the longjmp */
704 static int num_saved;
705 jmp_buf terminate_env;
707 unsigned long buf_used;
708 static unsigned int bb_count;
711 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
713 com_err (program_name, errcode,
714 _("while beginning bad block list iteration"));
718 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
719 } while (next_bad && next_bad < first_block);
721 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
722 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
723 if (!blkbuf || !test_record) {
724 com_err(program_name, ENOMEM, _("while allocating buffers"));
729 test_base = blkbuf + (blocks_at_once * block_size);
730 read_base = blkbuf + (2 * blocks_at_once * block_size);
736 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
737 fprintf (stderr, _("From block %lu to %lu\n"),
738 (unsigned long) first_block,
739 (unsigned long) last_block - 1);
741 if (s_flag || v_flag > 1) {
742 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
744 if (setjmp(terminate_env)) {
746 * Abnormal termination by a signal is handled here.
748 signal (SIGALRM, SIG_IGN);
749 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
751 save_ptr = save_base;
752 for (i=0; i < num_saved; i++) {
753 do_write(dev, save_ptr, test_record[i].num,
754 block_size, test_record[i].block);
755 save_ptr += test_record[i].num * block_size;
761 /* set up abend handler */
762 capture_terminate(terminate_env);
769 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
771 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
772 pattern_fill(test_base, pattern[pat_idx],
773 blocks_at_once * block_size);
777 save_ptr = save_base;
778 test_ptr = test_base;
779 currently_testing = first_block;
780 num_blocks = last_block - 1;
781 if (s_flag && v_flag <= 1)
784 while (currently_testing < last_block) {
785 if (max_bb && bb_count >= max_bb) {
786 if (s_flag || v_flag) {
787 fputs(_("Too many bad blocks, aborting test\n"), stderr);
791 got = try = blocks_at_once - buf_used;
793 if (currently_testing == next_bad) {
794 /* fprintf (out, "%lu\n", nextbad); */
795 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
799 else if (currently_testing + try > next_bad)
800 try = next_bad - currently_testing;
802 if (currently_testing + try > last_block)
803 try = last_block - currently_testing;
804 got = do_read (dev, save_ptr, try, block_size,
807 /* First block must have been bad. */
808 bb_count += bb_output(currently_testing++);
813 * Note the fact that we've saved this much data
814 * *before* we overwrite it with test data
816 test_record[num_saved].block = currently_testing;
817 test_record[num_saved].num = got;
820 /* Write the test data */
821 written = do_write (dev, test_ptr, got, block_size,
824 com_err (program_name, errno,
825 _("during test data write, block %lu"),
826 (unsigned long) currently_testing +
830 save_ptr += got * block_size;
831 test_ptr += got * block_size;
832 currently_testing += got;
834 bb_count += bb_output(currently_testing++);
838 * If there's room for more blocks to be tested this
839 * around, and we're not done yet testing the disk, go
840 * back and get some more blocks.
842 if ((buf_used != blocks_at_once) &&
843 (currently_testing < last_block))
847 save_currently_testing = currently_testing;
850 * for each contiguous block that we read into the
851 * buffer (and wrote test data into afterwards), read
852 * it back (looping if necessary, to get past newly
853 * discovered unreadable blocks, of which there should
854 * be none, but with a hard drive which is unreliable,
855 * it has happened), and compare with the test data
856 * that was written; output to the bad block list if
860 save_ptr = save_base;
861 test_ptr = test_base;
862 read_ptr = read_base;
867 if (used2 >= num_saved)
869 currently_testing = test_record[used2].block;
870 try = test_record[used2].num;
874 got = do_read (dev, read_ptr, try,
875 block_size, currently_testing);
877 /* test the comparison between all the
878 blocks successfully read */
879 for (i = 0; i < got; ++i)
880 if (memcmp (test_ptr+i*block_size,
881 read_ptr+i*block_size, block_size))
882 bb_count += bb_output(currently_testing + i);
884 bb_count += bb_output(currently_testing + got);
888 /* write back original data */
889 do_write (dev, save_ptr, got,
890 block_size, currently_testing);
891 save_ptr += got * block_size;
893 currently_testing += got;
894 test_ptr += got * block_size;
895 read_ptr += got * block_size;
899 /* empty the buffer so it can be reused */
902 save_ptr = save_base;
903 test_ptr = test_base;
904 currently_testing = save_currently_testing;
908 if (s_flag || v_flag > 1)
909 fputs(_(done_string), stderr);
913 uncapture_terminate();
918 ext2fs_badblocks_list_iterate_end(bb_iter);
923 static void check_mount(char *device_name)
928 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
930 com_err("ext2fs_check_if_mount", retval,
931 _("while determining whether %s is mounted."),
935 if (mount_flags & EXT2_MF_MOUNTED) {
936 fprintf(stderr, _("%s is mounted; "), device_name);
938 fputs(_("badblocks forced anyway. "
939 "Hope /etc/mtab is incorrect.\n"), stderr);
943 fputs(_("it's not safe to run badblocks!\n"), stderr);
947 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
948 fprintf(stderr, _("%s is apparently in use by the system; "),
951 fputs(_("badblocks forced anyway.\n"), stderr);
953 goto abort_badblocks;
959 * This function will convert a string to an unsigned long, printing
960 * an error message if it fails, and returning success or failure in err.
962 static unsigned int parse_uint(const char *str, const char *descr)
968 ret = strtoul(str, &tmp, 0);
969 if (*tmp || errno || (ret > UINT_MAX) ||
970 (ret == ULONG_MAX && errno == ERANGE)) {
971 com_err (program_name, 0, _("invalid %s - %s"), descr, str);
977 int main (int argc, char ** argv)
981 char * host_device_name = NULL;
982 char * input_file = NULL;
983 char * output_file = NULL;
985 int block_size = 1024;
986 unsigned int blocks_at_once = 64;
987 blk_t last_block, first_block;
989 int passes_clean = 0;
992 unsigned int pattern;
993 unsigned int (*test_func)(int, blk_t,
999 setbuf(stdout, NULL);
1000 setbuf(stderr, NULL);
1002 setlocale(LC_MESSAGES, "");
1003 setlocale(LC_CTYPE, "");
1004 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1005 textdomain(NLS_CAT_NAME);
1007 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
1008 test_func = test_ro;
1010 /* Determine the system page size if possible */
1012 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1013 #define _SC_PAGESIZE _SC_PAGE_SIZE
1016 sysval = sysconf(_SC_PAGESIZE);
1018 sys_page_size = sysval;
1019 #endif /* _SC_PAGESIZE */
1020 #endif /* HAVE_SYSCONF */
1023 program_name = *argv;
1024 while ((c = getopt (argc, argv, "b:d:e:fi:o:svwnc:p:h:t:X")) != EOF) {
1027 block_size = parse_uint(optarg, "block size");
1033 input_file = optarg;
1036 output_file = optarg;
1047 test_func = test_rw;
1053 test_func = test_nd;
1057 blocks_at_once = parse_uint(optarg, "blocks at once");
1060 max_bb = parse_uint(optarg, "max bad block count");
1063 d_flag = parse_uint(optarg, "read delay factor");
1066 num_passes = parse_uint(optarg,
1067 "number of clean passes");
1070 host_device_name = optarg;
1073 if (t_flag + 1 > t_max) {
1074 unsigned int *t_patts_new;
1076 t_patts_new = realloc(t_patts, sizeof(int) *
1079 com_err(program_name, ENOMEM,
1080 _("can't allocate memory for "
1081 "test_pattern - %s"),
1085 t_patts = t_patts_new;
1088 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
1089 t_patts[t_flag++] = ~0;
1091 pattern = parse_uint(optarg, "test pattern");
1092 if (pattern == (unsigned int) ~0)
1094 t_patts[t_flag++] = pattern;
1106 com_err(program_name, 0,
1107 _("Maximum of one test_pattern may be specified "
1108 "in read-only mode"));
1111 if (t_patts && (t_patts[0] == (unsigned int) ~0)) {
1112 com_err(program_name, 0,
1113 _("Random test_pattern is not allowed "
1114 "in read-only mode"));
1118 if (optind > argc - 1)
1120 device_name = argv[optind++];
1121 if (optind > argc - 1) {
1122 errcode = ext2fs_get_device_size(device_name,
1125 if (errcode == EXT2_ET_UNIMPLEMENTED) {
1126 com_err(program_name, 0,
1127 _("Couldn't determine device size; you "
1128 "must specify\nthe size manually\n"));
1132 com_err(program_name, errcode,
1133 _("while trying to determine device size"));
1138 last_block = parse_uint(argv[optind], _("last block"));
1142 if (optind <= argc-1) {
1144 first_block = parse_uint(argv[optind], _("first block"));
1145 } else first_block = 0;
1146 if (first_block >= last_block) {
1147 com_err (program_name, 0, _("invalid starting block (%lu): must be less than %lu"),
1148 (unsigned long) first_block, (unsigned long) last_block);
1152 check_mount(device_name);
1154 gettimeofday(&time_start, 0);
1155 open_flag = O_LARGEFILE | (w_flag ? O_RDWR : O_RDONLY);
1156 dev = open (device_name, open_flag);
1158 com_err (program_name, errno, _("while trying to open %s"),
1162 if (host_device_name) {
1163 host_dev = open (host_device_name, open_flag);
1164 if (host_dev == -1) {
1165 com_err (program_name, errno,
1166 _("while trying to open %s"),
1173 if (strcmp (input_file, "-") == 0)
1176 in = fopen (input_file, "r");
1179 com_err (program_name, errno,
1180 _("while trying to open %s"),
1186 if (output_file && strcmp (output_file, "-") != 0)
1188 out = fopen (output_file, "w");
1191 com_err (program_name, errno,
1192 _("while trying to open %s"),
1200 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1202 com_err (program_name, errcode,
1203 _("while creating in-memory bad blocks list"));
1209 switch(fscanf (in, "%u\n", &next_bad)) {
1211 com_err (program_name, 0, "input file - bad format");
1216 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1218 com_err (program_name, errcode, _("while adding to in-memory bad block list"));
1231 unsigned int bb_count;
1233 bb_count = test_func(dev, last_block, block_size,
1234 first_block, blocks_at_once);
1242 _("Pass completed, %u bad blocks found.\n"),
1245 } while (passes_clean < num_passes);