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)
33 #define _GNU_SOURCE /* for O_DIRECT */
59 #include <sys/ioctl.h>
60 #include <sys/types.h>
62 #include "et/com_err.h"
63 #include "ext2fs/ext2_io.h"
64 #include "ext2fs/ext2_fs.h"
65 #include "ext2fs/ext2fs.h"
66 #include "nls-enable.h"
68 const char * program_name = "badblocks";
69 const char * done_string = N_("done \n");
71 static int v_flag = 0; /* verbose */
72 static int w_flag = 0; /* do r/w test: 0=no, 1=yes,
73 * 2=non-destructive */
74 static int s_flag = 0; /* show progress of test */
75 static int force = 0; /* force check of mounted device */
76 static int t_flag = 0; /* number of test patterns */
77 static int t_max = 0; /* allocated test patterns */
78 static unsigned int *t_patts = NULL; /* test patterns */
79 static int current_O_DIRECT = 0; /* Current status of O_DIRECT flag */
80 static int use_buffered_io = 0;
81 static int exclusive_ok = 0;
82 static unsigned int max_bb = 0; /* Abort test if more than this number of bad blocks has been encountered */
83 static unsigned int d_flag = 0; /* delay factor between reads */
84 static struct timeval time_start;
88 unsigned int sys_page_size = 4096;
90 static void usage(void)
93 "Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwnf]\n"
94 " [-c blocks_at_once] [-d delay_factor_between_reads] [-e max_bad_blocks]\n"
95 " [-p num_passes] [-t test_pattern [-t test_pattern [...]]]\n"
96 " device [last_block [first_block]]\n"),
101 static void exclusive_usage(void)
104 _("%s: The -n and -w options are mutually exclusive.\n\n"),
109 static blk_t currently_testing = 0;
110 static blk_t num_blocks = 0;
111 static blk_t num_read_errors = 0;
112 static blk_t num_write_errors = 0;
113 static blk_t num_corruption_errors = 0;
114 static ext2_badblocks_list bb_list = NULL;
116 static blk_t next_bad = 0;
117 static ext2_badblocks_iterate bb_iter = NULL;
119 enum error_types { READ_ERROR, WRITE_ERROR, CORRUPTION_ERROR };
121 static void *allocate_buffer(size_t size)
125 #ifdef HAVE_POSIX_MEMALIGN
126 if (posix_memalign(&ret, sys_page_size, size) < 0)
130 ret = memalign(sys_page_size, size);
134 #endif /* HAVE_VALLOC */
135 #endif /* HAVE_MEMALIGN */
136 #endif /* HAVE_POSIX_MEMALIGN */
145 * This routine reports a new bad block. If the bad block has already
146 * been seen before, then it returns 0; otherwise it returns 1.
148 static int bb_output (blk_t bad, enum error_types error_type)
152 if (ext2fs_badblocks_list_test(bb_list, bad))
155 fprintf(out, "%lu\n", (unsigned long) bad);
158 errcode = ext2fs_badblocks_list_add (bb_list, bad);
160 com_err (program_name, errcode, "adding to in-memory bad block list");
165 increment the iteration through the bb_list if
166 an element was just added before the current iteration
167 position. This should not cause next_bad to change. */
168 if (bb_iter && bad < next_bad)
169 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
171 if (error_type == READ_ERROR) {
173 } else if (error_type == WRITE_ERROR) {
175 } else if (error_type == CORRUPTION_ERROR) {
176 num_corruption_errors++;
181 static char *time_diff_format(struct timeval *tv1,
182 struct timeval *tv2, char *buf)
184 time_t diff = (tv1->tv_sec - tv2->tv_sec);
193 sprintf(buf, "%d:%02d:%02d", hr, min, sec);
195 sprintf(buf, "%d:%02d", min, sec);
199 static float calc_percent(unsigned long current, unsigned long total) {
203 if (current >= total) {
206 percent=(100.0*(float)current/(float)total);
211 static void print_status(void)
213 struct timeval time_end;
214 char diff_buf[32], line_buf[128];
217 gettimeofday(&time_end, 0);
218 len = snprintf(line_buf, sizeof(line_buf),
219 _("%6.2f%% done, %s elapsed. "
220 "(%d/%d/%d errors)"),
221 calc_percent((unsigned long) currently_testing,
222 (unsigned long) num_blocks),
223 time_diff_format(&time_end, &time_start, diff_buf),
226 num_corruption_errors);
228 len = mbstowcs(NULL, line_buf, sizeof(line_buf));
230 fputs(line_buf, stderr);
231 memset(line_buf, '\b', len);
233 fputs(line_buf, stderr);
237 static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
239 signal (SIGALRM, alarm_intr);
246 static void *terminate_addr = NULL;
248 static void terminate_intr(int signo EXT2FS_ATTR((unused)))
251 fprintf(stderr, "\n\nInterrupted at block %llu\n",
252 (unsigned long long) currently_testing);
255 longjmp(terminate_addr,1);
259 static void capture_terminate(jmp_buf term_addr)
261 terminate_addr = term_addr;
262 signal (SIGHUP, terminate_intr);
263 signal (SIGINT, terminate_intr);
264 signal (SIGPIPE, terminate_intr);
265 signal (SIGTERM, terminate_intr);
266 signal (SIGUSR1, terminate_intr);
267 signal (SIGUSR2, terminate_intr);
270 static void uncapture_terminate(void)
272 terminate_addr = NULL;
273 signal (SIGHUP, SIG_DFL);
274 signal (SIGINT, SIG_DFL);
275 signal (SIGPIPE, SIG_DFL);
276 signal (SIGTERM, SIG_DFL);
277 signal (SIGUSR1, SIG_DFL);
278 signal (SIGUSR2, SIG_DFL);
281 /* Linux requires that O_DIRECT I/Os be 512-byte sector aligned */
283 #define O_DIRECT_SIZE 512
285 static void set_o_direct(int dev, unsigned char *buffer, size_t size,
289 int new_flag = O_DIRECT;
292 if ((use_buffered_io != 0) ||
293 (((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
294 ((size & (sys_page_size - 1)) != 0) ||
295 ((offset & (O_DIRECT_SIZE - 1)) != 0))
298 if (new_flag != current_O_DIRECT) {
299 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
300 flag = fcntl(dev, F_GETFL);
302 flag = (flag & ~O_DIRECT) | new_flag;
303 fcntl(dev, F_SETFL, flag);
305 current_O_DIRECT = new_flag;
311 static void pattern_fill(unsigned char *buffer, unsigned int pattern,
315 unsigned char bpattern[sizeof(pattern)], *ptr;
317 if (pattern == (unsigned int) ~0) {
318 for (ptr = buffer; ptr < buffer + n; ptr++) {
319 (*ptr) = random() % (1 << (8 * sizeof(char)));
322 fputs(_("Testing with random pattern: "), stderr);
325 for (i = 0; i < sizeof(bpattern); i++) {
328 bpattern[i] = pattern & 0xFF;
329 pattern = pattern >> 8;
332 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
339 if (s_flag | v_flag) {
340 fputs(_("Testing with pattern 0x"), stderr);
341 for (i = 0; i <= nb; i++)
342 fprintf(stderr, "%02x", buffer[i]);
349 * Perform a read of a sequence of blocks; return the number of blocks
350 * successfully sequentially read.
352 static int do_read (int dev, unsigned char * buffer, int try, int block_size,
356 struct timeval tv1, tv2;
357 #define NANOSEC (1000000000L)
358 #define MILISEC (1000L)
361 printf("do_read: block %d, try %d\n", current_block, try);
363 set_o_direct(dev, buffer, try * block_size,
364 ((ext2_loff_t) current_block) * block_size);
369 /* Seek to the correct loc. */
370 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
371 SEEK_SET) != (ext2_loff_t) current_block * block_size)
372 com_err (program_name, errno, _("during seek"));
376 gettimeofday(&tv1, NULL);
377 got = read (dev, buffer, try * block_size);
379 gettimeofday(&tv2, NULL);
383 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
385 if (d_flag && got == try) {
386 #ifdef HAVE_NANOSLEEP
388 ts.tv_sec = tv2.tv_sec - tv1.tv_sec;
389 ts.tv_nsec = (tv2.tv_usec - tv1.tv_usec) * MILISEC;
390 if (ts.tv_nsec < 0) {
391 ts.tv_nsec += NANOSEC;
394 /* increase/decrease the sleep time based on d_flag value */
395 ts.tv_sec = ts.tv_sec * d_flag / 100;
396 ts.tv_nsec = ts.tv_nsec * d_flag / 100;
397 if (ts.tv_nsec > NANOSEC) {
398 ts.tv_sec += ts.tv_nsec / NANOSEC;
399 ts.tv_nsec %= NANOSEC;
401 if (ts.tv_sec || ts.tv_nsec)
402 nanosleep(&ts, NULL);
406 tv.tv_sec = tv2.tv_sec - tv1.tv_sec;
407 tv.tv_usec = tv2.tv_usec - tv1.tv_usec;
408 tv.tv_sec = tv.tv_sec * d_flag / 100;
409 tv.tv_usec = tv.tv_usec * d_flag / 100;
410 if (tv.tv_usec > 1000000) {
411 tv.tv_sec += tv.tv_usec / 1000000;
412 tv.tv_usec %= 1000000;
425 * Perform a write of a sequence of blocks; return the number of blocks
426 * successfully sequentially written.
428 static int do_write(int dev, unsigned char * buffer, int try, int block_size,
429 unsigned long current_block)
434 printf("do_write: block %lu, try %d\n", current_block, try);
436 set_o_direct(dev, buffer, try * block_size,
437 ((ext2_loff_t) current_block) * block_size);
442 /* Seek to the correct loc. */
443 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
444 SEEK_SET) != (ext2_loff_t) current_block * block_size)
445 com_err (program_name, errno, _("during seek"));
448 got = write (dev, buffer, try * block_size);
452 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
459 static void flush_bufs(void)
464 if (!use_buffered_io)
467 retval = ext2fs_sync_device(host_dev, 1);
469 com_err(program_name, retval, _("during ext2fs_sync_device"));
472 static unsigned int test_ro (int dev, blk_t last_block,
473 int block_size, blk_t first_block,
474 unsigned int blocks_at_once)
476 unsigned char * blkbuf;
479 unsigned int bb_count = 0;
481 blk_t recover_block = ~0;
483 /* set up abend handler */
484 capture_terminate(NULL);
486 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
488 com_err (program_name, errcode,
489 _("while beginning bad block list iteration"));
493 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
494 } while (next_bad && next_bad < first_block);
497 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
499 blkbuf = allocate_buffer(blocks_at_once * block_size);
503 com_err (program_name, ENOMEM, _("while allocating buffers"));
507 fprintf (stderr, _("Checking blocks %lu to %lu\n"),
508 (unsigned long) first_block,
509 (unsigned long) last_block - 1);
512 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
513 pattern_fill(blkbuf + blocks_at_once * block_size,
514 t_patts[0], block_size);
517 try = blocks_at_once;
518 currently_testing = first_block;
519 num_blocks = last_block - 1;
520 if (!t_flag && (s_flag || v_flag)) {
521 fputs(_("Checking for bad blocks (read-only test): "), stderr);
525 while (currently_testing < last_block)
527 if (max_bb && bb_count >= max_bb) {
528 if (s_flag || v_flag) {
529 fputs(_("Too many bad blocks, aborting test\n"), stderr);
534 if (currently_testing == next_bad) {
535 /* fprintf (out, "%lu\n", nextbad); */
536 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
540 else if (currently_testing + try > next_bad)
541 try = next_bad - currently_testing;
543 if (currently_testing + try > last_block)
544 try = last_block - currently_testing;
545 got = do_read (dev, blkbuf, try, block_size, currently_testing);
547 /* test the comparison between all the
548 blocks successfully read */
550 for (i = 0; i < got; ++i)
551 if (memcmp (blkbuf+i*block_size,
552 blkbuf+blocks_at_once*block_size,
554 bb_count += bb_output(currently_testing + i, CORRUPTION_ERROR);
556 if (got == 0 && try == 1)
557 bb_count += bb_output(currently_testing++, READ_ERROR);
558 currently_testing += got;
561 if (recover_block == ~0)
562 recover_block = currently_testing - got +
565 } else if (currently_testing == recover_block) {
566 try = blocks_at_once;
572 if (s_flag || v_flag)
573 fputs(_(done_string), stderr);
578 ext2fs_badblocks_list_iterate_end(bb_iter);
580 uncapture_terminate();
585 static unsigned int test_rw (int dev, blk_t last_block,
586 int block_size, blk_t first_block,
587 unsigned int blocks_at_once)
589 unsigned char *buffer, *read_buffer;
590 const unsigned int patterns[] = {0xaa, 0x55, 0xff, 0x00};
591 const unsigned int *pattern;
592 int i, try, got, nr_pattern, pat_idx;
593 unsigned int bb_count = 0;
594 blk_t recover_block = ~0;
596 /* set up abend handler */
597 capture_terminate(NULL);
599 buffer = allocate_buffer(2 * blocks_at_once * block_size);
600 read_buffer = buffer + blocks_at_once * block_size;
603 com_err (program_name, ENOMEM, _("while allocating buffers"));
610 fputs(_("Checking for bad blocks in read-write mode\n"),
612 fprintf(stderr, _("From block %lu to %lu\n"),
613 (unsigned long) first_block,
614 (unsigned long) last_block - 1);
621 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
623 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
624 pattern_fill(buffer, pattern[pat_idx],
625 blocks_at_once * block_size);
626 num_blocks = last_block - 1;
627 currently_testing = first_block;
628 if (s_flag && v_flag <= 1)
631 try = blocks_at_once;
632 while (currently_testing < last_block) {
633 if (max_bb && bb_count >= max_bb) {
634 if (s_flag || v_flag) {
635 fputs(_("Too many bad blocks, aborting test\n"), stderr);
639 if (currently_testing + try > last_block)
640 try = last_block - currently_testing;
641 got = do_write(dev, buffer, try, block_size,
646 if (got == 0 && try == 1)
647 bb_count += bb_output(currently_testing++, WRITE_ERROR);
648 currently_testing += got;
651 if (recover_block == ~0)
652 recover_block = currently_testing -
653 got + blocks_at_once;
655 } else if (currently_testing == recover_block) {
656 try = blocks_at_once;
664 fputs(_(done_string), stderr);
667 fputs(_("Reading and comparing: "), stderr);
668 num_blocks = last_block;
669 currently_testing = first_block;
670 if (s_flag && v_flag <= 1)
673 try = blocks_at_once;
674 while (currently_testing < last_block) {
675 if (max_bb && bb_count >= max_bb) {
676 if (s_flag || v_flag) {
677 fputs(_("Too many bad blocks, aborting test\n"), stderr);
681 if (currently_testing + try > last_block)
682 try = last_block - currently_testing;
683 got = do_read (dev, read_buffer, try, block_size,
685 if (got == 0 && try == 1)
686 bb_count += bb_output(currently_testing++, READ_ERROR);
687 currently_testing += got;
690 if (recover_block == ~0)
691 recover_block = currently_testing -
692 got + blocks_at_once;
694 } else if (currently_testing == recover_block) {
695 try = blocks_at_once;
698 for (i=0; i < got; i++) {
699 if (memcmp(read_buffer + i * block_size,
700 buffer + i * block_size,
702 bb_count += bb_output(currently_testing+i, CORRUPTION_ERROR);
711 fputs(_(done_string), stderr);
714 uncapture_terminate();
719 struct saved_blk_record {
724 static unsigned int test_nd (int dev, blk_t last_block,
725 int block_size, blk_t first_block,
726 unsigned int blocks_at_once)
728 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
729 unsigned char *test_base, *save_base, *read_base;
731 const unsigned int patterns[] = { ~0 };
732 const unsigned int *pattern;
733 int nr_pattern, pat_idx;
734 int got, used2, written;
735 blk_t save_currently_testing;
736 struct saved_blk_record *test_record;
737 /* This is static to prevent being clobbered by the longjmp */
738 static int num_saved;
739 jmp_buf terminate_env;
741 unsigned long buf_used;
742 static unsigned int bb_count;
743 int granularity = blocks_at_once;
744 blk_t recover_block = ~0;
747 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
749 com_err (program_name, errcode,
750 _("while beginning bad block list iteration"));
754 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
755 } while (next_bad && next_bad < first_block);
757 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
758 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
759 if (!blkbuf || !test_record) {
760 com_err(program_name, ENOMEM, _("while allocating buffers"));
765 test_base = blkbuf + (blocks_at_once * block_size);
766 read_base = blkbuf + (2 * blocks_at_once * block_size);
772 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
773 fprintf (stderr, _("From block %lu to %lu\n"),
774 (unsigned long) first_block,
775 (unsigned long) last_block - 1);
777 if (s_flag || v_flag > 1) {
778 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
780 if (setjmp(terminate_env)) {
782 * Abnormal termination by a signal is handled here.
784 signal (SIGALRM, SIG_IGN);
785 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
787 save_ptr = save_base;
788 for (i=0; i < num_saved; i++) {
789 do_write(dev, save_ptr, test_record[i].num,
790 block_size, test_record[i].block);
791 save_ptr += test_record[i].num * block_size;
797 /* set up abend handler */
798 capture_terminate(terminate_env);
805 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
807 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
808 pattern_fill(test_base, pattern[pat_idx],
809 blocks_at_once * block_size);
813 save_ptr = save_base;
814 test_ptr = test_base;
815 currently_testing = first_block;
816 num_blocks = last_block - 1;
817 if (s_flag && v_flag <= 1)
820 while (currently_testing < last_block) {
821 if (max_bb && bb_count >= max_bb) {
822 if (s_flag || v_flag) {
823 fputs(_("Too many bad blocks, aborting test\n"), stderr);
827 got = try = granularity - buf_used;
829 if (currently_testing == next_bad) {
830 /* fprintf (out, "%lu\n", nextbad); */
831 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
835 else if (currently_testing + try > next_bad)
836 try = next_bad - currently_testing;
838 if (currently_testing + try > last_block)
839 try = last_block - currently_testing;
840 got = do_read (dev, save_ptr, try, block_size,
843 if (recover_block == ~0)
844 recover_block = currently_testing +
846 if (granularity != 1) {
850 /* First block must have been bad. */
851 bb_count += bb_output(currently_testing++, READ_ERROR);
856 * Note the fact that we've saved this much data
857 * *before* we overwrite it with test data
859 test_record[num_saved].block = currently_testing;
860 test_record[num_saved].num = got;
863 /* Write the test data */
864 written = do_write (dev, test_ptr, got, block_size,
867 com_err (program_name, errno,
868 _("during test data write, block %lu"),
869 (unsigned long) currently_testing +
873 save_ptr += got * block_size;
874 test_ptr += got * block_size;
875 currently_testing += got;
878 if (recover_block == ~0)
879 recover_block = currently_testing -
880 got + blocks_at_once;
886 * If there's room for more blocks to be tested this
887 * around, and we're not done yet testing the disk, go
888 * back and get some more blocks.
890 if ((buf_used != granularity) &&
891 (currently_testing < last_block))
894 if (currently_testing >= recover_block) {
895 granularity = blocks_at_once;
900 save_currently_testing = currently_testing;
903 * for each contiguous block that we read into the
904 * buffer (and wrote test data into afterwards), read
905 * it back (looping if necessary, to get past newly
906 * discovered unreadable blocks, of which there should
907 * be none, but with a hard drive which is unreliable,
908 * it has happened), and compare with the test data
909 * that was written; output to the bad block list if
913 save_ptr = save_base;
914 test_ptr = test_base;
915 read_ptr = read_base;
920 if (used2 >= num_saved)
922 currently_testing = test_record[used2].block;
923 try = test_record[used2].num;
927 got = do_read (dev, read_ptr, try,
928 block_size, currently_testing);
930 /* test the comparison between all the
931 blocks successfully read */
932 for (i = 0; i < got; ++i)
933 if (memcmp (test_ptr+i*block_size,
934 read_ptr+i*block_size, block_size))
935 bb_count += bb_output(currently_testing + i, CORRUPTION_ERROR);
937 bb_count += bb_output(currently_testing + got, READ_ERROR);
941 /* write back original data */
942 do_write (dev, save_ptr, got,
943 block_size, currently_testing);
944 save_ptr += got * block_size;
946 currently_testing += got;
947 test_ptr += got * block_size;
948 read_ptr += got * block_size;
952 /* empty the buffer so it can be reused */
955 save_ptr = save_base;
956 test_ptr = test_base;
957 currently_testing = save_currently_testing;
961 if (s_flag || v_flag > 1)
962 fputs(_(done_string), stderr);
966 uncapture_terminate();
971 ext2fs_badblocks_list_iterate_end(bb_iter);
976 static void check_mount(char *device_name)
981 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
983 com_err("ext2fs_check_if_mount", retval,
984 _("while determining whether %s is mounted."),
988 if (mount_flags & EXT2_MF_MOUNTED) {
989 fprintf(stderr, _("%s is mounted; "), device_name);
991 fputs(_("badblocks forced anyway. "
992 "Hope /etc/mtab is incorrect.\n"), stderr);
996 fputs(_("it's not safe to run badblocks!\n"), stderr);
1000 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
1001 fprintf(stderr, _("%s is apparently in use by the system; "),
1004 fputs(_("badblocks forced anyway.\n"), stderr);
1006 goto abort_badblocks;
1012 * This function will convert a string to an unsigned long, printing
1013 * an error message if it fails, and returning success or failure in err.
1015 static unsigned int parse_uint(const char *str, const char *descr)
1021 ret = strtoul(str, &tmp, 0);
1022 if (*tmp || errno || (ret > UINT_MAX) ||
1023 (ret == ULONG_MAX && errno == ERANGE)) {
1024 com_err (program_name, 0, _("invalid %s - %s"), descr, str);
1030 int main (int argc, char ** argv)
1034 char * host_device_name = NULL;
1035 char * input_file = NULL;
1036 char * output_file = NULL;
1038 int block_size = 1024;
1039 unsigned int blocks_at_once = 64;
1040 blk64_t last_block, first_block;
1042 int passes_clean = 0;
1045 unsigned int pattern;
1046 unsigned int (*test_func)(int, blk_t,
1052 setbuf(stdout, NULL);
1053 setbuf(stderr, NULL);
1055 setlocale(LC_MESSAGES, "");
1056 setlocale(LC_CTYPE, "");
1057 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1058 textdomain(NLS_CAT_NAME);
1060 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
1061 test_func = test_ro;
1063 /* Determine the system page size if possible */
1065 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1066 #define _SC_PAGESIZE _SC_PAGE_SIZE
1069 sysval = sysconf(_SC_PAGESIZE);
1071 sys_page_size = sysval;
1072 #endif /* _SC_PAGESIZE */
1073 #endif /* HAVE_SYSCONF */
1076 program_name = *argv;
1077 while ((c = getopt (argc, argv, "b:d:e:fi:o:svwnc:p:h:t:BX")) != EOF) {
1080 block_size = parse_uint(optarg, "block size");
1086 input_file = optarg;
1089 output_file = optarg;
1100 test_func = test_rw;
1106 test_func = test_nd;
1110 blocks_at_once = parse_uint(optarg, "blocks at once");
1113 max_bb = parse_uint(optarg, "max bad block count");
1116 d_flag = parse_uint(optarg, "read delay factor");
1119 num_passes = parse_uint(optarg,
1120 "number of clean passes");
1123 host_device_name = optarg;
1126 if (t_flag + 1 > t_max) {
1127 unsigned int *t_patts_new;
1129 t_patts_new = realloc(t_patts, sizeof(int) *
1132 com_err(program_name, ENOMEM,
1133 _("can't allocate memory for "
1134 "test_pattern - %s"),
1138 t_patts = t_patts_new;
1141 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
1142 t_patts[t_flag++] = ~0;
1144 pattern = parse_uint(optarg, "test pattern");
1145 if (pattern == (unsigned int) ~0)
1147 t_patts[t_flag++] = pattern;
1151 use_buffered_io = 1;
1162 com_err(program_name, 0,
1163 _("Maximum of one test_pattern may be specified "
1164 "in read-only mode"));
1167 if (t_patts && (t_patts[0] == (unsigned int) ~0)) {
1168 com_err(program_name, 0,
1169 _("Random test_pattern is not allowed "
1170 "in read-only mode"));
1174 if (optind > argc - 1)
1176 device_name = argv[optind++];
1177 if (optind > argc - 1) {
1178 errcode = ext2fs_get_device_size2(device_name,
1181 if (errcode == EXT2_ET_UNIMPLEMENTED) {
1182 com_err(program_name, 0,
1183 _("Couldn't determine device size; you "
1184 "must specify\nthe size manually\n"));
1188 com_err(program_name, errcode,
1189 _("while trying to determine device size"));
1194 last_block = parse_uint(argv[optind], _("last block"));
1198 if (optind <= argc-1) {
1200 first_block = parse_uint(argv[optind], _("first block"));
1201 } else first_block = 0;
1202 if (first_block >= last_block) {
1203 com_err (program_name, 0, _("invalid starting block (%lu): must be less than %lu"),
1204 (unsigned long) first_block, (unsigned long) last_block);
1208 check_mount(device_name);
1210 gettimeofday(&time_start, 0);
1211 open_flag = O_LARGEFILE | (w_flag ? O_RDWR : O_RDONLY);
1212 dev = open (device_name, open_flag);
1214 com_err (program_name, errno, _("while trying to open %s"),
1218 if (host_device_name) {
1219 host_dev = open (host_device_name, open_flag);
1220 if (host_dev == -1) {
1221 com_err (program_name, errno,
1222 _("while trying to open %s"),
1229 if (strcmp (input_file, "-") == 0)
1232 in = fopen (input_file, "r");
1235 com_err (program_name, errno,
1236 _("while trying to open %s"),
1242 if (output_file && strcmp (output_file, "-") != 0)
1244 out = fopen (output_file, "w");
1247 com_err (program_name, errno,
1248 _("while trying to open %s"),
1256 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1258 com_err (program_name, errcode,
1259 _("while creating in-memory bad blocks list"));
1265 switch(fscanf (in, "%u\n", &next_bad)) {
1267 com_err (program_name, 0, "input file - bad format");
1272 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1274 com_err (program_name, errcode, _("while adding to in-memory bad block list"));
1287 unsigned int bb_count;
1289 bb_count = test_func(dev, last_block, block_size,
1290 first_block, blocks_at_once);
1298 _("Pass completed, %u bad blocks found. (%d/%d/%d errors)\n"),
1299 bb_count, num_read_errors, num_write_errors, num_corruption_errors);
1301 } while (passes_clean < num_passes);