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)))
227 longjmp(terminate_addr,1);
231 static void capture_terminate(jmp_buf term_addr)
233 terminate_addr = term_addr;
234 signal (SIGHUP, terminate_intr);
235 signal (SIGINT, terminate_intr);
236 signal (SIGPIPE, terminate_intr);
237 signal (SIGTERM, terminate_intr);
238 signal (SIGUSR1, terminate_intr);
239 signal (SIGUSR2, terminate_intr);
242 static void uncapture_terminate(void)
244 terminate_addr = NULL;
245 signal (SIGHUP, SIG_DFL);
246 signal (SIGINT, SIG_DFL);
247 signal (SIGPIPE, SIG_DFL);
248 signal (SIGTERM, SIG_DFL);
249 signal (SIGUSR1, SIG_DFL);
250 signal (SIGUSR2, SIG_DFL);
253 static void set_o_direct(int dev, unsigned char *buffer, size_t size,
257 int new_flag = O_DIRECT;
260 if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
261 ((size & (sys_page_size - 1)) != 0) ||
262 ((current_block & ((sys_page_size >> 9)-1)) != 0))
265 if (new_flag != current_O_DIRECT) {
266 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
267 flag = fcntl(dev, F_GETFL);
269 flag = (flag & ~O_DIRECT) | new_flag;
270 fcntl(dev, F_SETFL, flag);
272 current_O_DIRECT = new_flag;
278 static void pattern_fill(unsigned char *buffer, unsigned int pattern,
282 unsigned char bpattern[sizeof(pattern)], *ptr;
284 if (pattern == (unsigned int) ~0) {
285 for (ptr = buffer; ptr < buffer + n; ptr++) {
286 (*ptr) = random() % (1 << (8 * sizeof(char)));
289 fputs(_("Testing with random pattern: "), stderr);
292 for (i = 0; i < sizeof(bpattern); i++) {
295 bpattern[i] = pattern & 0xFF;
296 pattern = pattern >> 8;
299 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
306 if (s_flag | v_flag) {
307 fputs(_("Testing with pattern 0x"), stderr);
308 for (i = 0; i <= nb; i++)
309 fprintf(stderr, "%02x", buffer[i]);
316 * Perform a read of a sequence of blocks; return the number of blocks
317 * successfully sequentially read.
319 static int do_read (int dev, unsigned char * buffer, int try, int block_size,
323 struct timeval tv1, tv2;
324 #define NANOSEC (1000000000L)
325 #define MILISEC (1000L)
327 set_o_direct(dev, buffer, try * block_size, current_block);
332 /* Seek to the correct loc. */
333 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
334 SEEK_SET) != (ext2_loff_t) current_block * block_size)
335 com_err (program_name, errno, _("during seek"));
339 gettimeofday(&tv1, NULL);
340 got = read (dev, buffer, try * block_size);
342 gettimeofday(&tv2, NULL);
346 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
348 if (d_flag && got == try) {
349 #ifdef HAVE_NANOSLEEP
351 ts.tv_sec = tv2.tv_sec - tv1.tv_sec;
352 ts.tv_nsec = (tv2.tv_usec - tv1.tv_usec) * MILISEC;
353 if (ts.tv_nsec < 0) {
354 ts.tv_nsec += NANOSEC;
357 /* increase/decrease the sleep time based on d_flag value */
358 ts.tv_sec = ts.tv_sec * d_flag / 100;
359 ts.tv_nsec = ts.tv_nsec * d_flag / 100;
360 if (ts.tv_nsec > NANOSEC) {
361 ts.tv_sec += ts.tv_nsec / NANOSEC;
362 ts.tv_nsec %= NANOSEC;
364 if (ts.tv_sec || ts.tv_nsec)
365 nanosleep(&ts, NULL);
369 tv.tv_sec = tv2.tv_sec - tv1.tv_sec;
370 tv.tv_usec = tv2.tv_usec - tv1.tv_usec;
371 tv.tv_sec = tv.tv_sec * d_flag / 100;
372 tv.tv_usec = tv.tv_usec * d_flag / 100;
373 if (tv.tv_usec > 1000000) {
374 tv.tv_sec += tv.tv_usec / 1000000;
375 tv.tv_usec %= 1000000;
388 * Perform a write of a sequence of blocks; return the number of blocks
389 * successfully sequentially written.
391 static int do_write(int dev, unsigned char * buffer, int try, int block_size,
392 unsigned long current_block)
396 set_o_direct(dev, buffer, try * block_size, current_block);
401 /* Seek to the correct loc. */
402 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
403 SEEK_SET) != (ext2_loff_t) current_block * block_size)
404 com_err (program_name, errno, _("during seek"));
407 got = write (dev, buffer, try * block_size);
411 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
418 static void flush_bufs(void)
422 retval = ext2fs_sync_device(host_dev, 1);
424 com_err(program_name, retval, _("during ext2fs_sync_device"));
427 static unsigned int test_ro (int dev, blk_t last_block,
428 int block_size, blk_t first_block,
429 unsigned int blocks_at_once)
431 unsigned char * blkbuf;
434 unsigned int bb_count = 0;
437 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
439 com_err (program_name, errcode,
440 _("while beginning bad block list iteration"));
444 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
445 } while (next_bad && next_bad < first_block);
448 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
450 blkbuf = allocate_buffer(blocks_at_once * block_size);
454 com_err (program_name, ENOMEM, _("while allocating buffers"));
458 fprintf (stderr, _("Checking blocks %lu to %lu\n"),
459 (unsigned long) first_block,
460 (unsigned long) last_block - 1);
463 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
464 pattern_fill(blkbuf + blocks_at_once * block_size,
465 t_patts[0], block_size);
468 try = blocks_at_once;
469 currently_testing = first_block;
470 num_blocks = last_block - 1;
471 if (!t_flag && (s_flag || v_flag)) {
472 fputs(_("Checking for bad blocks (read-only test): "), stderr);
476 while (currently_testing < last_block)
478 if (max_bb && bb_count >= max_bb) {
479 if (s_flag || v_flag) {
480 fputs(_("Too many bad blocks, aborting test\n"), stderr);
485 if (currently_testing == next_bad) {
486 /* fprintf (out, "%lu\n", nextbad); */
487 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
491 else if (currently_testing + try > next_bad)
492 try = next_bad - currently_testing;
494 if (currently_testing + try > last_block)
495 try = last_block - currently_testing;
496 got = do_read (dev, blkbuf, try, block_size, currently_testing);
498 /* test the comparison between all the
499 blocks successfully read */
501 for (i = 0; i < got; ++i)
502 if (memcmp (blkbuf+i*block_size,
503 blkbuf+blocks_at_once*block_size,
505 bb_count += bb_output(currently_testing + i);
507 currently_testing += got;
509 try = blocks_at_once;
510 /* recover page-aligned offset for O_DIRECT */
511 if ( (blocks_at_once >= sys_page_size >> 9)
512 && (currently_testing % (sys_page_size >> 9)!= 0))
513 try -= (sys_page_size >> 9)
515 % (sys_page_size >> 9));
521 bb_count += bb_output(currently_testing++);
526 if (s_flag || v_flag)
527 fputs(_(done_string), stderr);
532 ext2fs_badblocks_list_iterate_end(bb_iter);
537 static unsigned int test_rw (int dev, blk_t last_block,
538 int block_size, blk_t first_block,
539 unsigned int blocks_at_once)
541 unsigned char *buffer, *read_buffer;
542 const unsigned int patterns[] = {0xaa, 0x55, 0xff, 0x00};
543 const unsigned int *pattern;
544 int i, try, got, nr_pattern, pat_idx;
545 unsigned int bb_count = 0;
547 buffer = allocate_buffer(2 * blocks_at_once * block_size);
548 read_buffer = buffer + blocks_at_once * block_size;
551 com_err (program_name, ENOMEM, _("while allocating buffers"));
558 fputs(_("Checking for bad blocks in read-write mode\n"),
560 fprintf(stderr, _("From block %lu to %lu\n"),
561 (unsigned long) first_block,
562 (unsigned long) last_block - 1);
569 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
571 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
572 pattern_fill(buffer, pattern[pat_idx],
573 blocks_at_once * block_size);
574 num_blocks = last_block - 1;
575 currently_testing = first_block;
576 if (s_flag && v_flag <= 1)
579 try = blocks_at_once;
580 while (currently_testing < last_block) {
581 if (max_bb && bb_count >= max_bb) {
582 if (s_flag || v_flag) {
583 fputs(_("Too many bad blocks, aborting test\n"), stderr);
587 if (currently_testing + try > last_block)
588 try = last_block - currently_testing;
589 got = do_write(dev, buffer, try, block_size,
594 currently_testing += got;
596 try = blocks_at_once;
597 /* recover page-aligned offset for O_DIRECT */
598 if ( (blocks_at_once >= sys_page_size >> 9)
599 && (currently_testing %
600 (sys_page_size >> 9)!= 0))
601 try -= (sys_page_size >> 9)
603 % (sys_page_size >> 9));
608 bb_count += bb_output(currently_testing++);
615 fputs(_(done_string), stderr);
618 fputs(_("Reading and comparing: "), stderr);
619 num_blocks = last_block;
620 currently_testing = first_block;
621 if (s_flag && v_flag <= 1)
624 try = blocks_at_once;
625 while (currently_testing < last_block) {
626 if (max_bb && bb_count >= max_bb) {
627 if (s_flag || v_flag) {
628 fputs(_("Too many bad blocks, aborting test\n"), stderr);
632 if (currently_testing + try > last_block)
633 try = last_block - currently_testing;
634 got = do_read (dev, read_buffer, try, block_size,
637 bb_count += bb_output(currently_testing++);
640 for (i=0; i < got; i++) {
641 if (memcmp(read_buffer + i * block_size,
642 buffer + i * block_size,
644 bb_count += bb_output(currently_testing+i);
646 currently_testing += got;
647 /* recover page-aligned offset for O_DIRECT */
648 if ( (blocks_at_once >= sys_page_size >> 9)
649 && (currently_testing % (sys_page_size >> 9)!= 0))
650 try = blocks_at_once - (sys_page_size >> 9)
652 % (sys_page_size >> 9));
654 try = blocks_at_once;
662 fputs(_(done_string), stderr);
665 uncapture_terminate();
670 struct saved_blk_record {
675 static unsigned int test_nd (int dev, blk_t last_block,
676 int block_size, blk_t first_block,
677 unsigned int blocks_at_once)
679 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
680 unsigned char *test_base, *save_base, *read_base;
682 const unsigned int patterns[] = { ~0 };
683 const unsigned int *pattern;
684 int nr_pattern, pat_idx;
685 int got, used2, written;
686 blk_t save_currently_testing;
687 struct saved_blk_record *test_record;
688 /* This is static to prevent being clobbered by the longjmp */
689 static int num_saved;
690 jmp_buf terminate_env;
692 unsigned long buf_used;
693 static unsigned int bb_count;
696 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
698 com_err (program_name, errcode,
699 _("while beginning bad block list iteration"));
703 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
704 } while (next_bad && next_bad < first_block);
706 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
707 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
708 if (!blkbuf || !test_record) {
709 com_err(program_name, ENOMEM, _("while allocating buffers"));
714 test_base = blkbuf + (blocks_at_once * block_size);
715 read_base = blkbuf + (2 * blocks_at_once * block_size);
721 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
722 fprintf (stderr, _("From block %lu to %lu\n"),
723 (unsigned long) first_block,
724 (unsigned long) last_block - 1);
726 if (s_flag || v_flag > 1) {
727 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
729 if (setjmp(terminate_env)) {
731 * Abnormal termination by a signal is handled here.
733 signal (SIGALRM, SIG_IGN);
734 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
736 save_ptr = save_base;
737 for (i=0; i < num_saved; i++) {
738 do_write(dev, save_ptr, test_record[i].num,
739 block_size, test_record[i].block);
740 save_ptr += test_record[i].num * block_size;
746 /* set up abend handler */
747 capture_terminate(terminate_env);
754 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
756 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
757 pattern_fill(test_base, pattern[pat_idx],
758 blocks_at_once * block_size);
762 save_ptr = save_base;
763 test_ptr = test_base;
764 currently_testing = first_block;
765 num_blocks = last_block - 1;
766 if (s_flag && v_flag <= 1)
769 while (currently_testing < last_block) {
770 if (max_bb && bb_count >= max_bb) {
771 if (s_flag || v_flag) {
772 fputs(_("Too many bad blocks, aborting test\n"), stderr);
776 got = try = blocks_at_once - buf_used;
778 if (currently_testing == next_bad) {
779 /* fprintf (out, "%lu\n", nextbad); */
780 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
784 else if (currently_testing + try > next_bad)
785 try = next_bad - currently_testing;
787 if (currently_testing + try > last_block)
788 try = last_block - currently_testing;
789 got = do_read (dev, save_ptr, try, block_size,
792 /* First block must have been bad. */
793 bb_count += bb_output(currently_testing++);
798 * Note the fact that we've saved this much data
799 * *before* we overwrite it with test data
801 test_record[num_saved].block = currently_testing;
802 test_record[num_saved].num = got;
805 /* Write the test data */
806 written = do_write (dev, test_ptr, got, block_size,
809 com_err (program_name, errno,
810 _("during test data write, block %lu"),
811 (unsigned long) currently_testing +
815 save_ptr += got * block_size;
816 test_ptr += got * block_size;
817 currently_testing += got;
819 bb_count += bb_output(currently_testing++);
823 * If there's room for more blocks to be tested this
824 * around, and we're not done yet testing the disk, go
825 * back and get some more blocks.
827 if ((buf_used != blocks_at_once) &&
828 (currently_testing < last_block))
832 save_currently_testing = currently_testing;
835 * for each contiguous block that we read into the
836 * buffer (and wrote test data into afterwards), read
837 * it back (looping if necessary, to get past newly
838 * discovered unreadable blocks, of which there should
839 * be none, but with a hard drive which is unreliable,
840 * it has happened), and compare with the test data
841 * that was written; output to the bad block list if
845 save_ptr = save_base;
846 test_ptr = test_base;
847 read_ptr = read_base;
852 if (used2 >= num_saved)
854 currently_testing = test_record[used2].block;
855 try = test_record[used2].num;
859 got = do_read (dev, read_ptr, try,
860 block_size, currently_testing);
862 /* test the comparison between all the
863 blocks successfully read */
864 for (i = 0; i < got; ++i)
865 if (memcmp (test_ptr+i*block_size,
866 read_ptr+i*block_size, block_size))
867 bb_count += bb_output(currently_testing + i);
869 bb_count += bb_output(currently_testing + got);
873 /* write back original data */
874 do_write (dev, save_ptr, got,
875 block_size, currently_testing);
876 save_ptr += got * block_size;
878 currently_testing += got;
879 test_ptr += got * block_size;
880 read_ptr += got * block_size;
884 /* empty the buffer so it can be reused */
887 save_ptr = save_base;
888 test_ptr = test_base;
889 currently_testing = save_currently_testing;
893 if (s_flag || v_flag > 1)
894 fputs(_(done_string), stderr);
898 uncapture_terminate();
903 ext2fs_badblocks_list_iterate_end(bb_iter);
908 static void check_mount(char *device_name)
913 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
915 com_err("ext2fs_check_if_mount", retval,
916 _("while determining whether %s is mounted."),
920 if (mount_flags & EXT2_MF_MOUNTED) {
921 fprintf(stderr, _("%s is mounted; "), device_name);
923 fputs(_("badblocks forced anyway. "
924 "Hope /etc/mtab is incorrect.\n"), stderr);
928 fputs(_("it's not safe to run badblocks!\n"), stderr);
932 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
933 fprintf(stderr, _("%s is apparently in use by the system; "),
936 fputs(_("badblocks forced anyway.\n"), stderr);
938 goto abort_badblocks;
944 * This function will convert a string to an unsigned long, printing
945 * an error message if it fails, and returning success or failure in err.
947 static unsigned int parse_uint(const char *str, const char *descr)
953 ret = strtoul(str, &tmp, 0);
954 if (*tmp || errno || (ret > UINT_MAX) ||
955 (ret == ULONG_MAX && errno == ERANGE)) {
956 com_err (program_name, 0, _("invalid %s - %s"), descr, str);
962 int main (int argc, char ** argv)
966 char * host_device_name = NULL;
967 char * input_file = NULL;
968 char * output_file = NULL;
970 int block_size = 1024;
971 unsigned int blocks_at_once = 64;
972 blk_t last_block, first_block;
974 int passes_clean = 0;
977 unsigned int pattern;
978 unsigned int (*test_func)(int, blk_t,
984 setbuf(stdout, NULL);
985 setbuf(stderr, NULL);
987 setlocale(LC_MESSAGES, "");
988 setlocale(LC_CTYPE, "");
989 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
990 textdomain(NLS_CAT_NAME);
992 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
995 /* Determine the system page size if possible */
997 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
998 #define _SC_PAGESIZE _SC_PAGE_SIZE
1001 sysval = sysconf(_SC_PAGESIZE);
1003 sys_page_size = sysval;
1004 #endif /* _SC_PAGESIZE */
1005 #endif /* HAVE_SYSCONF */
1008 program_name = *argv;
1009 while ((c = getopt (argc, argv, "b:d:e:fi:o:svwnc:p:h:t:X")) != EOF) {
1012 block_size = parse_uint(optarg, "block size");
1018 input_file = optarg;
1021 output_file = optarg;
1032 test_func = test_rw;
1038 test_func = test_nd;
1042 blocks_at_once = parse_uint(optarg, "blocks at once");
1045 max_bb = parse_uint(optarg, "max bad block count");
1048 d_flag = parse_uint(optarg, "read delay factor");
1051 num_passes = parse_uint(optarg,
1052 "number of clean passes");
1055 host_device_name = optarg;
1058 if (t_flag + 1 > t_max) {
1059 unsigned int *t_patts_new;
1061 t_patts_new = realloc(t_patts, sizeof(int) *
1064 com_err(program_name, ENOMEM,
1065 _("can't allocate memory for "
1066 "test_pattern - %s"),
1070 t_patts = t_patts_new;
1073 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
1074 t_patts[t_flag++] = ~0;
1076 pattern = parse_uint(optarg, "test pattern");
1077 if (pattern == (unsigned int) ~0)
1079 t_patts[t_flag++] = pattern;
1091 com_err(program_name, 0,
1092 _("Maximum of one test_pattern may be specified "
1093 "in read-only mode"));
1096 if (t_patts && (t_patts[0] == (unsigned int) ~0)) {
1097 com_err(program_name, 0,
1098 _("Random test_pattern is not allowed "
1099 "in read-only mode"));
1103 if (optind > argc - 1)
1105 device_name = argv[optind++];
1106 if (optind > argc - 1) {
1107 errcode = ext2fs_get_device_size(device_name,
1110 if (errcode == EXT2_ET_UNIMPLEMENTED) {
1111 com_err(program_name, 0,
1112 _("Couldn't determine device size; you "
1113 "must specify\nthe size manually\n"));
1117 com_err(program_name, errcode,
1118 _("while trying to determine device size"));
1123 last_block = parse_uint(argv[optind], _("last block"));
1127 if (optind <= argc-1) {
1129 first_block = parse_uint(argv[optind], _("first block"));
1130 } else first_block = 0;
1131 if (first_block >= last_block) {
1132 com_err (program_name, 0, _("invalid starting block (%lu): must be less than %lu"),
1133 (unsigned long) first_block, (unsigned long) last_block);
1137 check_mount(device_name);
1139 gettimeofday(&time_start, 0);
1140 open_flag = O_LARGEFILE | (w_flag ? O_RDWR : O_RDONLY);
1141 dev = open (device_name, open_flag);
1143 com_err (program_name, errno, _("while trying to open %s"),
1147 if (host_device_name) {
1148 host_dev = open (host_device_name, open_flag);
1149 if (host_dev == -1) {
1150 com_err (program_name, errno,
1151 _("while trying to open %s"),
1158 if (strcmp (input_file, "-") == 0)
1161 in = fopen (input_file, "r");
1164 com_err (program_name, errno,
1165 _("while trying to open %s"),
1171 if (output_file && strcmp (output_file, "-") != 0)
1173 out = fopen (output_file, "w");
1176 com_err (program_name, errno,
1177 _("while trying to open %s"),
1185 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1187 com_err (program_name, errcode,
1188 _("while creating in-memory bad blocks list"));
1194 switch(fscanf (in, "%u\n", &next_bad)) {
1196 com_err (program_name, 0, "input file - bad format");
1201 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1203 com_err (program_name, errcode, _("while adding to in-memory bad block list"));
1216 unsigned int bb_count;
1218 bb_count = test_func(dev, last_block, block_size,
1219 first_block, blocks_at_once);
1227 _("Pass completed, %u bad blocks found.\n"),
1230 } while (passes_clean < num_passes);