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 */
55 #include <sys/ioctl.h>
56 #include <sys/types.h>
58 #include "et/com_err.h"
59 #include "ext2fs/ext2_io.h"
60 #include "ext2fs/ext2_fs.h"
61 #include "ext2fs/ext2fs.h"
62 #include "support/nls-enable.h"
68 /* Maximum number of bad blocks we support */
69 #define MAX_BAD_BLOCKS (INT_MAX/2)
71 static const char * program_name = "badblocks";
72 static const char * done_string = N_("done \n");
74 static int v_flag; /* verbose */
75 static int w_flag; /* do r/w test: 0=no, 1=yes,
76 * 2=non-destructive */
77 static int s_flag; /* show progress of test */
78 static int force; /* force check of mounted device */
79 static int t_flag; /* number of test patterns */
80 static int t_max; /* allocated test patterns */
81 static unsigned int *t_patts; /* test patterns */
82 static int use_buffered_io;
83 static int exclusive_ok;
84 static unsigned int max_bb = MAX_BAD_BLOCKS; /* Abort test if more than this
85 * number of bad blocks has been
87 static unsigned int d_flag; /* delay factor between reads */
88 static struct timeval time_start;
92 static unsigned int sys_page_size = 4096;
94 static void usage(void)
97 "Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwnf]\n"
98 " [-c blocks_at_once] [-d delay_factor_between_reads] [-e max_bad_blocks]\n"
99 " [-p num_passes] [-t test_pattern [-t test_pattern [...]]]\n"
100 " device [last_block [first_block]]\n"),
105 static void exclusive_usage(void)
108 _("%s: The -n and -w options are mutually exclusive.\n\n"),
113 static blk_t currently_testing = 0;
114 static blk_t num_blocks = 0;
115 static blk_t num_read_errors = 0;
116 static blk_t num_write_errors = 0;
117 static blk_t num_corruption_errors = 0;
118 static ext2_badblocks_list bb_list = NULL;
120 static blk_t next_bad = 0;
121 static ext2_badblocks_iterate bb_iter = NULL;
123 enum error_types { READ_ERROR, WRITE_ERROR, CORRUPTION_ERROR };
125 static void *allocate_buffer(size_t size)
129 #ifdef HAVE_POSIX_MEMALIGN
130 if (posix_memalign(&ret, sys_page_size, size) < 0)
134 ret = memalign(sys_page_size, size);
138 #endif /* HAVE_VALLOC */
139 #endif /* HAVE_MEMALIGN */
140 #endif /* HAVE_POSIX_MEMALIGN */
149 * This routine reports a new bad block. If the bad block has already
150 * been seen before, then it returns 0; otherwise it returns 1.
152 static int bb_output (blk_t bad, enum error_types error_type)
156 if (ext2fs_badblocks_list_test(bb_list, bad))
159 fprintf(out, "%lu\n", (unsigned long) bad);
162 errcode = ext2fs_badblocks_list_add (bb_list, bad);
164 com_err (program_name, errcode, "adding to in-memory bad block list");
169 increment the iteration through the bb_list if
170 an element was just added before the current iteration
171 position. This should not cause next_bad to change. */
172 if (bb_iter && bad < next_bad)
173 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
175 if (error_type == READ_ERROR) {
177 } else if (error_type == WRITE_ERROR) {
179 } else if (error_type == CORRUPTION_ERROR) {
180 num_corruption_errors++;
185 static char *time_diff_format(struct timeval *tv1,
186 struct timeval *tv2, char *buf)
188 time_t diff = (tv1->tv_sec - tv2->tv_sec);
197 sprintf(buf, "%d:%02d:%02d", hr, min, sec);
199 sprintf(buf, "%d:%02d", min, sec);
203 static float calc_percent(unsigned long current, unsigned long total) {
207 if (current >= total) {
210 percent=(100.0*(float)current/(float)total);
215 static void print_status(void)
217 struct timeval time_end;
218 char diff_buf[32], line_buf[128];
221 gettimeofday(&time_end, 0);
222 len = snprintf(line_buf, sizeof(line_buf),
223 _("%6.2f%% done, %s elapsed. "
224 "(%d/%d/%d errors)"),
225 calc_percent((unsigned long) currently_testing,
226 (unsigned long) num_blocks),
227 time_diff_format(&time_end, &time_start, diff_buf),
230 num_corruption_errors);
232 len = mbstowcs(NULL, line_buf, sizeof(line_buf));
234 fputs(line_buf, stderr);
235 memset(line_buf, '\b', len);
237 fputs(line_buf, stderr);
241 static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
243 signal (SIGALRM, alarm_intr);
250 static void *terminate_addr = NULL;
252 static void terminate_intr(int signo EXT2FS_ATTR((unused)))
255 fprintf(stderr, "\n\nInterrupted at block %llu\n",
256 (unsigned long long) currently_testing);
259 longjmp(terminate_addr,1);
263 static void capture_terminate(jmp_buf term_addr)
265 terminate_addr = term_addr;
266 signal (SIGHUP, terminate_intr);
267 signal (SIGINT, terminate_intr);
268 signal (SIGPIPE, terminate_intr);
269 signal (SIGTERM, terminate_intr);
270 signal (SIGUSR1, terminate_intr);
271 signal (SIGUSR2, terminate_intr);
274 static void uncapture_terminate(void)
276 terminate_addr = NULL;
277 signal (SIGHUP, SIG_DFL);
278 signal (SIGINT, SIG_DFL);
279 signal (SIGPIPE, SIG_DFL);
280 signal (SIGTERM, SIG_DFL);
281 signal (SIGUSR1, SIG_DFL);
282 signal (SIGUSR2, SIG_DFL);
285 /* Linux requires that O_DIRECT I/Os be 512-byte sector aligned */
287 #define O_DIRECT_SIZE 512
289 static void set_o_direct(int dev, unsigned char *buffer, size_t size,
293 static int current_O_DIRECT; /* Current status of O_DIRECT flag */
294 int new_flag = O_DIRECT;
297 if ((use_buffered_io != 0) ||
298 (((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
299 ((size & (sys_page_size - 1)) != 0) ||
300 ((offset & (O_DIRECT_SIZE - 1)) != 0))
303 if (new_flag != current_O_DIRECT) {
304 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
305 flag = fcntl(dev, F_GETFL);
307 flag = (flag & ~O_DIRECT) | new_flag;
308 if (fcntl(dev, F_SETFL, flag) < 0)
309 perror("set_o_direct");
311 current_O_DIRECT = new_flag;
317 static void pattern_fill(unsigned char *buffer, unsigned int pattern,
321 unsigned char bpattern[sizeof(pattern)], *ptr;
323 if (pattern == (unsigned int) ~0) {
324 for (ptr = buffer; ptr < buffer + n; ptr++) {
325 (*ptr) = random() % (1 << (8 * sizeof(char)));
328 fputs(_("Testing with random pattern: "), stderr);
331 for (i = 0; i < sizeof(bpattern); i++) {
334 bpattern[i] = pattern & 0xFF;
335 pattern = pattern >> 8;
338 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
345 if (s_flag | v_flag) {
346 fputs(_("Testing with pattern 0x"), stderr);
347 for (i = 0; i <= nb; i++)
348 fprintf(stderr, "%02x", buffer[i]);
355 * Perform a read of a sequence of blocks; return the number of blocks
356 * successfully sequentially read.
358 static int do_read (int dev, unsigned char * buffer, int try, int block_size,
362 struct timeval tv1, tv2;
363 #define NANOSEC (1000000000L)
364 #define MILISEC (1000L)
367 printf("do_read: block %d, try %d\n", current_block, try);
369 set_o_direct(dev, buffer, try * block_size,
370 ((ext2_loff_t) current_block) * block_size);
375 /* Seek to the correct loc. */
376 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
377 SEEK_SET) != (ext2_loff_t) current_block * block_size)
378 com_err (program_name, errno, "%s", _("during seek"));
382 gettimeofday(&tv1, NULL);
383 got = read (dev, buffer, try * block_size);
385 gettimeofday(&tv2, NULL);
389 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
391 if (d_flag && got == try) {
392 #ifdef HAVE_NANOSLEEP
394 ts.tv_sec = tv2.tv_sec - tv1.tv_sec;
395 ts.tv_nsec = (tv2.tv_usec - tv1.tv_usec) * MILISEC;
396 if (ts.tv_nsec < 0) {
397 ts.tv_nsec += NANOSEC;
400 /* increase/decrease the sleep time based on d_flag value */
401 ts.tv_sec = ts.tv_sec * d_flag / 100;
402 ts.tv_nsec = ts.tv_nsec * d_flag / 100;
403 if (ts.tv_nsec > NANOSEC) {
404 ts.tv_sec += ts.tv_nsec / NANOSEC;
405 ts.tv_nsec %= NANOSEC;
407 if (ts.tv_sec || ts.tv_nsec)
408 nanosleep(&ts, NULL);
412 tv.tv_sec = tv2.tv_sec - tv1.tv_sec;
413 tv.tv_usec = tv2.tv_usec - tv1.tv_usec;
414 tv.tv_sec = tv.tv_sec * d_flag / 100;
415 tv.tv_usec = tv.tv_usec * d_flag / 100;
416 if (tv.tv_usec > 1000000) {
417 tv.tv_sec += tv.tv_usec / 1000000;
418 tv.tv_usec %= 1000000;
431 * Perform a write of a sequence of blocks; return the number of blocks
432 * successfully sequentially written.
434 static int do_write(int dev, unsigned char * buffer, int try, int block_size,
435 unsigned long current_block)
440 printf("do_write: block %lu, try %d\n", current_block, try);
442 set_o_direct(dev, buffer, try * block_size,
443 ((ext2_loff_t) current_block) * block_size);
448 /* Seek to the correct loc. */
449 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
450 SEEK_SET) != (ext2_loff_t) current_block * block_size)
451 com_err (program_name, errno, "%s", _("during seek"));
454 got = write (dev, buffer, try * block_size);
458 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
465 static void flush_bufs(void)
470 if (!use_buffered_io)
473 retval = ext2fs_sync_device(host_dev, 1);
475 com_err(program_name, retval, "%s",
476 _("during ext2fs_sync_device"));
479 static unsigned int test_ro (int dev, blk_t last_block,
480 int block_size, blk_t first_block,
481 unsigned int blocks_at_once)
483 unsigned char * blkbuf;
486 unsigned int bb_count = 0;
488 blk_t recover_block = ~0;
490 /* set up abend handler */
491 capture_terminate(NULL);
493 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
495 com_err(program_name, errcode, "%s",
496 _("while beginning bad block list iteration"));
500 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
501 } while (next_bad && next_bad < first_block);
504 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
506 blkbuf = allocate_buffer(blocks_at_once * block_size);
510 com_err(program_name, ENOMEM, "%s",
511 _("while allocating buffers"));
515 fprintf(stderr, _("Checking blocks %lu to %lu\n"),
516 (unsigned long)first_block,
517 (unsigned long)last_block - 1);
520 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
521 pattern_fill(blkbuf + blocks_at_once * block_size,
522 t_patts[0], block_size);
525 try = blocks_at_once;
526 currently_testing = first_block;
527 num_blocks = last_block - 1;
528 if (!t_flag && (s_flag || v_flag))
529 fputs(_("Checking for bad blocks (read-only test): "), stderr);
530 if (s_flag && v_flag <= 1)
532 while (currently_testing < last_block)
534 if (bb_count >= max_bb) {
535 if (s_flag || v_flag) {
536 fputs(_("Too many bad blocks, aborting test\n"), stderr);
541 if (currently_testing == next_bad) {
542 /* fprintf (out, "%lu\n", nextbad); */
543 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
547 else if (currently_testing + try > next_bad)
548 try = next_bad - currently_testing;
550 if (currently_testing + try > last_block)
551 try = last_block - currently_testing;
552 got = do_read (dev, blkbuf, try, block_size, currently_testing);
554 /* test the comparison between all the
555 blocks successfully read */
557 for (i = 0; i < got; ++i)
558 if (memcmp (blkbuf+i*block_size,
559 blkbuf+blocks_at_once*block_size,
561 bb_count += bb_output(currently_testing + i, CORRUPTION_ERROR);
563 if (got == 0 && try == 1)
564 bb_count += bb_output(currently_testing++, READ_ERROR);
565 currently_testing += got;
568 if (recover_block == ~0U)
569 recover_block = currently_testing - got +
572 } else if (currently_testing == recover_block) {
573 try = blocks_at_once;
579 if (s_flag || v_flag)
580 fputs(_(done_string), stderr);
585 ext2fs_badblocks_list_iterate_end(bb_iter);
587 uncapture_terminate();
592 static unsigned int test_rw (int dev, blk_t last_block,
593 int block_size, blk_t first_block,
594 unsigned int blocks_at_once)
596 unsigned char *buffer, *read_buffer;
597 const unsigned int patterns[] = {0xaa, 0x55, 0xff, 0x00};
598 const unsigned int *pattern;
599 int i, try, got, nr_pattern, pat_idx;
600 unsigned int bb_count = 0;
601 blk_t recover_block = ~0;
603 /* set up abend handler */
604 capture_terminate(NULL);
606 buffer = allocate_buffer(2 * blocks_at_once * block_size);
607 read_buffer = buffer + blocks_at_once * block_size;
610 com_err(program_name, ENOMEM, "%s",
611 _("while allocating buffers"));
618 fputs(_("Checking for bad blocks in read-write mode\n"),
620 fprintf(stderr, _("From block %lu to %lu\n"),
621 (unsigned long) first_block,
622 (unsigned long) last_block - 1);
629 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
631 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
632 pattern_fill(buffer, pattern[pat_idx],
633 blocks_at_once * block_size);
634 num_blocks = last_block - 1;
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 (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_write(dev, buffer, try, block_size,
654 if (got == 0 && try == 1)
655 bb_count += bb_output(currently_testing++, WRITE_ERROR);
656 currently_testing += got;
659 if (recover_block == ~0U)
660 recover_block = currently_testing -
661 got + blocks_at_once;
663 } else if (currently_testing == recover_block) {
664 try = blocks_at_once;
672 fputs(_(done_string), stderr);
675 fputs(_("Reading and comparing: "), stderr);
676 num_blocks = last_block;
677 currently_testing = first_block;
678 if (s_flag && v_flag <= 1)
681 try = blocks_at_once;
682 while (currently_testing < last_block) {
683 if (bb_count >= max_bb) {
684 if (s_flag || v_flag) {
685 fputs(_("Too many bad blocks, aborting test\n"), stderr);
689 if (currently_testing + try > last_block)
690 try = last_block - currently_testing;
691 got = do_read (dev, read_buffer, try, block_size,
693 if (got == 0 && try == 1)
694 bb_count += bb_output(currently_testing++, READ_ERROR);
695 currently_testing += got;
698 if (recover_block == ~0U)
699 recover_block = currently_testing -
700 got + blocks_at_once;
702 } else if (currently_testing == recover_block) {
703 try = blocks_at_once;
706 for (i=0; i < got; i++) {
707 if (memcmp(read_buffer + i * block_size,
708 buffer + i * block_size,
710 bb_count += bb_output(currently_testing+i, CORRUPTION_ERROR);
719 fputs(_(done_string), stderr);
722 uncapture_terminate();
727 struct saved_blk_record {
732 static unsigned int test_nd (int dev, blk_t last_block,
733 int block_size, blk_t first_block,
734 unsigned int blocks_at_once)
736 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
737 unsigned char *test_base, *save_base, *read_base;
739 const unsigned int patterns[] = { ~0 };
740 const unsigned int *pattern;
741 int nr_pattern, pat_idx;
742 int got, used2, written;
743 blk_t save_currently_testing;
744 struct saved_blk_record *test_record;
745 /* This is static to prevent being clobbered by the longjmp */
746 static int num_saved;
747 jmp_buf terminate_env;
749 unsigned long buf_used;
750 static unsigned int bb_count;
751 unsigned int granularity = blocks_at_once;
752 blk_t recover_block = ~0U;
755 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
757 com_err(program_name, errcode, "%s",
758 _("while beginning bad block list iteration"));
762 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
763 } while (next_bad && next_bad < first_block);
765 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
766 test_record = malloc(blocks_at_once * sizeof(struct saved_blk_record));
767 if (!blkbuf || !test_record) {
768 com_err(program_name, ENOMEM, "%s",
769 _("while allocating buffers"));
774 test_base = blkbuf + (blocks_at_once * block_size);
775 read_base = blkbuf + (2 * blocks_at_once * block_size);
781 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
782 fprintf (stderr, _("From block %lu to %lu\n"),
783 (unsigned long) first_block,
784 (unsigned long) last_block - 1);
786 if (s_flag || v_flag > 1) {
787 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
789 if (setjmp(terminate_env)) {
791 * Abnormal termination by a signal is handled here.
793 signal (SIGALRM, SIG_IGN);
794 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
796 save_ptr = save_base;
797 for (i=0; i < num_saved; i++) {
798 do_write(dev, save_ptr, test_record[i].num,
799 block_size, test_record[i].block);
800 save_ptr += test_record[i].num * block_size;
806 /* set up abend handler */
807 capture_terminate(terminate_env);
814 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
816 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
817 pattern_fill(test_base, pattern[pat_idx],
818 blocks_at_once * block_size);
822 save_ptr = save_base;
823 test_ptr = test_base;
824 currently_testing = first_block;
825 num_blocks = last_block - 1;
826 if (s_flag && v_flag <= 1)
829 while (currently_testing < last_block) {
830 if (bb_count >= max_bb) {
831 if (s_flag || v_flag) {
832 fputs(_("Too many bad blocks, aborting test\n"), stderr);
836 got = try = granularity - buf_used;
838 if (currently_testing == next_bad) {
839 /* fprintf (out, "%lu\n", nextbad); */
840 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
844 else if (currently_testing + try > next_bad)
845 try = next_bad - currently_testing;
847 if (currently_testing + try > last_block)
848 try = last_block - currently_testing;
849 got = do_read (dev, save_ptr, try, block_size,
852 if (recover_block == ~0U)
853 recover_block = currently_testing +
855 if (granularity != 1) {
859 /* First block must have been bad. */
860 bb_count += bb_output(currently_testing++, READ_ERROR);
865 * Note the fact that we've saved this much data
866 * *before* we overwrite it with test data
868 test_record[num_saved].block = currently_testing;
869 test_record[num_saved].num = got;
872 /* Write the test data */
873 written = do_write (dev, test_ptr, got, block_size,
876 com_err (program_name, errno,
877 _("during test data write, block %lu"),
878 (unsigned long) currently_testing +
882 save_ptr += got * block_size;
883 test_ptr += got * block_size;
884 currently_testing += got;
887 if (recover_block == ~0U)
888 recover_block = currently_testing -
889 got + blocks_at_once;
895 * If there's room for more blocks to be tested this
896 * around, and we're not done yet testing the disk, go
897 * back and get some more blocks.
899 if ((buf_used != granularity) &&
900 (currently_testing < last_block))
903 if (currently_testing >= recover_block) {
904 granularity = blocks_at_once;
909 save_currently_testing = currently_testing;
912 * for each contiguous block that we read into the
913 * buffer (and wrote test data into afterwards), read
914 * it back (looping if necessary, to get past newly
915 * discovered unreadable blocks, of which there should
916 * be none, but with a hard drive which is unreliable,
917 * it has happened), and compare with the test data
918 * that was written; output to the bad block list if
922 save_ptr = save_base;
923 test_ptr = test_base;
924 read_ptr = read_base;
929 if (used2 >= num_saved)
931 currently_testing = test_record[used2].block;
932 try = test_record[used2].num;
936 got = do_read (dev, read_ptr, try,
937 block_size, currently_testing);
939 /* test the comparison between all the
940 blocks successfully read */
941 for (i = 0; i < got; ++i)
942 if (memcmp (test_ptr+i*block_size,
943 read_ptr+i*block_size, block_size))
944 bb_count += bb_output(currently_testing + i, CORRUPTION_ERROR);
946 bb_count += bb_output(currently_testing + got, READ_ERROR);
950 /* write back original data */
951 do_write (dev, save_ptr, got,
952 block_size, currently_testing);
953 save_ptr += got * block_size;
955 currently_testing += got;
956 test_ptr += got * block_size;
957 read_ptr += got * block_size;
961 /* empty the buffer so it can be reused */
964 save_ptr = save_base;
965 test_ptr = test_base;
966 currently_testing = save_currently_testing;
970 if (s_flag || v_flag > 1)
971 fputs(_(done_string), stderr);
975 uncapture_terminate();
980 ext2fs_badblocks_list_iterate_end(bb_iter);
985 static void check_mount(char *device_name)
990 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
992 com_err("ext2fs_check_if_mount", retval,
993 _("while determining whether %s is mounted."),
997 if (mount_flags & EXT2_MF_MOUNTED) {
998 fprintf(stderr, _("%s is mounted; "), device_name);
1000 fputs(_("badblocks forced anyway. "
1001 "Hope /etc/mtab is incorrect.\n"), stderr);
1005 fputs(_("it's not safe to run badblocks!\n"), stderr);
1009 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
1010 fprintf(stderr, _("%s is apparently in use by the system; "),
1013 fputs(_("badblocks forced anyway.\n"), stderr);
1015 goto abort_badblocks;
1021 * This function will convert a string to an unsigned long, printing
1022 * an error message if it fails, and returning success or failure in err.
1024 static unsigned int parse_uint(const char *str, const char *descr)
1030 ret = strtoul(str, &tmp, 0);
1031 if (*tmp || errno || (ret > UINT_MAX) ||
1032 (ret == ULONG_MAX && errno == ERANGE)) {
1033 com_err (program_name, 0, _("invalid %s - %s"), descr, str);
1039 int main (int argc, char ** argv)
1043 char * host_device_name = NULL;
1044 char * input_file = NULL;
1045 char * output_file = NULL;
1047 int block_size = 1024;
1048 unsigned int blocks_at_once = 64;
1049 blk64_t last_block, first_block;
1051 int passes_clean = 0;
1054 unsigned int pattern;
1055 unsigned int (*test_func)(int, blk_t,
1062 setbuf(stdout, NULL);
1063 setbuf(stderr, NULL);
1065 setlocale(LC_MESSAGES, "");
1066 setlocale(LC_CTYPE, "");
1067 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1068 textdomain(NLS_CAT_NAME);
1069 set_com_err_gettext(gettext);
1071 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
1072 test_func = test_ro;
1074 /* Determine the system page size if possible */
1076 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1077 #define _SC_PAGESIZE _SC_PAGE_SIZE
1080 sysval = sysconf(_SC_PAGESIZE);
1082 sys_page_size = sysval;
1083 #endif /* _SC_PAGESIZE */
1084 #endif /* HAVE_SYSCONF */
1087 program_name = *argv;
1088 while ((c = getopt (argc, argv, "b:d:e:fi:o:svwnc:p:h:t:BX")) != EOF) {
1091 block_size = parse_uint(optarg, "block size");
1097 input_file = optarg;
1100 output_file = optarg;
1111 test_func = test_rw;
1117 test_func = test_nd;
1121 blocks_at_once = parse_uint(optarg, "blocks at once");
1124 max_bb = parse_uint(optarg, "max bad block count");
1125 if (max_bb > MAX_BAD_BLOCKS) {
1126 com_err (program_name, 0,
1127 _("Too big max bad blocks count %u - "
1128 "maximum is %u"), max_bb,
1132 /* 0 really means unlimited but we cannot do that much... */
1134 max_bb = MAX_BAD_BLOCKS;
1137 d_flag = parse_uint(optarg, "read delay factor");
1140 num_passes = parse_uint(optarg,
1141 "number of clean passes");
1144 host_device_name = optarg;
1147 if (t_flag + 1 > t_max) {
1148 unsigned int *t_patts_new;
1150 t_patts_new = realloc(t_patts, sizeof(int) *
1153 com_err(program_name, ENOMEM,
1154 _("can't allocate memory for "
1155 "test_pattern - %s"),
1159 t_patts = t_patts_new;
1162 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
1163 t_patts[t_flag++] = ~0;
1165 pattern = parse_uint(optarg, "test pattern");
1166 if (pattern == (unsigned int) ~0)
1168 t_patts[t_flag++] = pattern;
1172 use_buffered_io = 1;
1183 com_err(program_name, 0, "%s",
1184 _("Maximum of one test_pattern may be "
1185 "specified in read-only mode"));
1188 if (t_patts && (t_patts[0] == (unsigned int) ~0)) {
1189 com_err(program_name, 0, "%s",
1190 _("Random test_pattern is not allowed "
1191 "in read-only mode"));
1195 if (optind > argc - 1)
1197 device_name = argv[optind++];
1198 if (optind > argc - 1) {
1199 errcode = ext2fs_get_device_size2(device_name,
1202 if (errcode == EXT2_ET_UNIMPLEMENTED) {
1203 com_err(program_name, 0, "%s",
1204 _("Couldn't determine device size; you "
1205 "must specify\nthe size manually\n"));
1209 com_err(program_name, errcode, "%s",
1210 _("while trying to determine device size"));
1215 last_block = parse_uint(argv[optind], _("last block"));
1219 if (optind <= argc-1) {
1221 first_block = parse_uint(argv[optind], _("first block"));
1222 } else first_block = 0;
1223 if (first_block >= last_block) {
1224 com_err (program_name, 0, _("invalid starting block (%llu): must be less than %llu"),
1225 first_block, last_block);
1228 /* ext2 badblocks file can't handle large values */
1229 if (last_block >> 32) {
1230 com_err(program_name, EOVERFLOW,
1231 _("invalid end block (%llu): must be 32-bit value"),
1236 check_mount(device_name);
1238 gettimeofday(&time_start, 0);
1239 open_flag = O_LARGEFILE | (w_flag ? O_RDWR : O_RDONLY);
1240 dev = open (device_name, open_flag);
1242 com_err (program_name, errno, _("while trying to open %s"),
1246 if (host_device_name) {
1247 host_dev = open (host_device_name, open_flag);
1248 if (host_dev == -1) {
1249 com_err (program_name, errno,
1250 _("while trying to open %s"),
1257 if (strcmp (input_file, "-") == 0)
1260 in = fopen (input_file, "r");
1263 com_err (program_name, errno,
1264 _("while trying to open %s"),
1270 if (output_file && strcmp (output_file, "-") != 0)
1272 out = fopen (output_file, "w");
1275 com_err (program_name, errno,
1276 _("while trying to open %s"),
1284 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1286 com_err(program_name, errcode, "%s",
1287 _("while creating in-memory bad blocks list"));
1293 switch (fscanf(in, "%llu\n", &inblk)) {
1295 com_err(program_name, 0, "%s",
1296 _("input file - bad format"));
1302 com_err(program_name,
1304 _("while adding to in-memory "
1309 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1311 com_err(program_name, errcode,
1313 _("while adding to in-memory "
1327 unsigned int bb_count;
1329 bb_count = test_func(dev, last_block, block_size,
1330 first_block, blocks_at_once);
1338 _("Pass completed, %u bad blocks found. (%d/%d/%d errors)\n"),
1339 bb_count, num_read_errors, num_write_errors, num_corruption_errors);
1341 } while (passes_clean < num_passes);