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 "nls-enable.h"
68 static const char * program_name = "badblocks";
69 static const char * done_string = N_("done \n");
71 static int v_flag; /* verbose */
72 static int w_flag; /* do r/w test: 0=no, 1=yes,
73 * 2=non-destructive */
74 static int s_flag; /* show progress of test */
75 static int force; /* force check of mounted device */
76 static int t_flag; /* number of test patterns */
77 static int t_max; /* allocated test patterns */
78 static unsigned int *t_patts; /* test patterns */
79 static int use_buffered_io;
80 static int exclusive_ok;
81 static unsigned int max_bb; /* Abort test if more than this number of bad blocks has been encountered */
82 static unsigned int d_flag; /* delay factor between reads */
83 static struct timeval time_start;
87 static unsigned int sys_page_size = 4096;
89 static void usage(void)
92 "Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwnf]\n"
93 " [-c blocks_at_once] [-d delay_factor_between_reads] [-e max_bad_blocks]\n"
94 " [-p num_passes] [-t test_pattern [-t test_pattern [...]]]\n"
95 " device [last_block [first_block]]\n"),
100 static void exclusive_usage(void)
103 _("%s: The -n and -w options are mutually exclusive.\n\n"),
108 static blk_t currently_testing = 0;
109 static blk_t num_blocks = 0;
110 static blk_t num_read_errors = 0;
111 static blk_t num_write_errors = 0;
112 static blk_t num_corruption_errors = 0;
113 static ext2_badblocks_list bb_list = NULL;
115 static blk_t next_bad = 0;
116 static ext2_badblocks_iterate bb_iter = NULL;
118 enum error_types { READ_ERROR, WRITE_ERROR, CORRUPTION_ERROR };
120 static void *allocate_buffer(size_t size)
124 #ifdef HAVE_POSIX_MEMALIGN
125 if (posix_memalign(&ret, sys_page_size, size) < 0)
129 ret = memalign(sys_page_size, size);
133 #endif /* HAVE_VALLOC */
134 #endif /* HAVE_MEMALIGN */
135 #endif /* HAVE_POSIX_MEMALIGN */
144 * This routine reports a new bad block. If the bad block has already
145 * been seen before, then it returns 0; otherwise it returns 1.
147 static int bb_output (blk_t bad, enum error_types error_type)
151 if (ext2fs_badblocks_list_test(bb_list, bad))
154 fprintf(out, "%lu\n", (unsigned long) bad);
157 errcode = ext2fs_badblocks_list_add (bb_list, bad);
159 com_err (program_name, errcode, "adding to in-memory bad block list");
164 increment the iteration through the bb_list if
165 an element was just added before the current iteration
166 position. This should not cause next_bad to change. */
167 if (bb_iter && bad < next_bad)
168 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
170 if (error_type == READ_ERROR) {
172 } else if (error_type == WRITE_ERROR) {
174 } else if (error_type == CORRUPTION_ERROR) {
175 num_corruption_errors++;
180 static char *time_diff_format(struct timeval *tv1,
181 struct timeval *tv2, char *buf)
183 time_t diff = (tv1->tv_sec - tv2->tv_sec);
192 sprintf(buf, "%d:%02d:%02d", hr, min, sec);
194 sprintf(buf, "%d:%02d", min, sec);
198 static float calc_percent(unsigned long current, unsigned long total) {
202 if (current >= total) {
205 percent=(100.0*(float)current/(float)total);
210 static void print_status(void)
212 struct timeval time_end;
213 char diff_buf[32], line_buf[128];
216 gettimeofday(&time_end, 0);
217 len = snprintf(line_buf, sizeof(line_buf),
218 _("%6.2f%% done, %s elapsed. "
219 "(%d/%d/%d errors)"),
220 calc_percent((unsigned long) currently_testing,
221 (unsigned long) num_blocks),
222 time_diff_format(&time_end, &time_start, diff_buf),
225 num_corruption_errors);
227 len = mbstowcs(NULL, line_buf, sizeof(line_buf));
229 fputs(line_buf, stderr);
230 memset(line_buf, '\b', len);
232 fputs(line_buf, stderr);
236 static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
238 signal (SIGALRM, alarm_intr);
245 static void *terminate_addr = NULL;
247 static void terminate_intr(int signo EXT2FS_ATTR((unused)))
250 fprintf(stderr, "\n\nInterrupted at block %llu\n",
251 (unsigned long long) currently_testing);
254 longjmp(terminate_addr,1);
258 static void capture_terminate(jmp_buf term_addr)
260 terminate_addr = term_addr;
261 signal (SIGHUP, terminate_intr);
262 signal (SIGINT, terminate_intr);
263 signal (SIGPIPE, terminate_intr);
264 signal (SIGTERM, terminate_intr);
265 signal (SIGUSR1, terminate_intr);
266 signal (SIGUSR2, terminate_intr);
269 static void uncapture_terminate(void)
271 terminate_addr = NULL;
272 signal (SIGHUP, SIG_DFL);
273 signal (SIGINT, SIG_DFL);
274 signal (SIGPIPE, SIG_DFL);
275 signal (SIGTERM, SIG_DFL);
276 signal (SIGUSR1, SIG_DFL);
277 signal (SIGUSR2, SIG_DFL);
280 /* Linux requires that O_DIRECT I/Os be 512-byte sector aligned */
282 #define O_DIRECT_SIZE 512
284 static void set_o_direct(int dev, unsigned char *buffer, size_t size,
288 static int current_O_DIRECT; /* Current status of O_DIRECT flag */
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, "%s", _("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, "%s", _("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, "%s",
470 _("during ext2fs_sync_device"));
473 static unsigned int test_ro (int dev, blk_t last_block,
474 int block_size, blk_t first_block,
475 unsigned int blocks_at_once)
477 unsigned char * blkbuf;
480 unsigned int bb_count = 0;
482 blk_t recover_block = ~0;
484 /* set up abend handler */
485 capture_terminate(NULL);
487 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
489 com_err(program_name, errcode, "%s",
490 _("while beginning bad block list iteration"));
494 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
495 } while (next_bad && next_bad < first_block);
498 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
500 blkbuf = allocate_buffer(blocks_at_once * block_size);
504 com_err(program_name, ENOMEM, "%s",
505 _("while allocating buffers"));
509 fprintf(stderr, _("Checking blocks %lu to %lu\n"),
510 (unsigned long)first_block,
511 (unsigned long)last_block - 1);
514 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
515 pattern_fill(blkbuf + blocks_at_once * block_size,
516 t_patts[0], block_size);
519 try = blocks_at_once;
520 currently_testing = first_block;
521 num_blocks = last_block - 1;
522 if (!t_flag && (s_flag || v_flag))
523 fputs(_("Checking for bad blocks (read-only test): "), stderr);
524 if (s_flag && v_flag <= 1)
526 while (currently_testing < last_block)
528 if (max_bb && bb_count >= max_bb) {
529 if (s_flag || v_flag) {
530 fputs(_("Too many bad blocks, aborting test\n"), stderr);
535 if (currently_testing == next_bad) {
536 /* fprintf (out, "%lu\n", nextbad); */
537 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
541 else if (currently_testing + try > next_bad)
542 try = next_bad - currently_testing;
544 if (currently_testing + try > last_block)
545 try = last_block - currently_testing;
546 got = do_read (dev, blkbuf, try, block_size, currently_testing);
548 /* test the comparison between all the
549 blocks successfully read */
551 for (i = 0; i < got; ++i)
552 if (memcmp (blkbuf+i*block_size,
553 blkbuf+blocks_at_once*block_size,
555 bb_count += bb_output(currently_testing + i, CORRUPTION_ERROR);
557 if (got == 0 && try == 1)
558 bb_count += bb_output(currently_testing++, READ_ERROR);
559 currently_testing += got;
562 if (recover_block == ~0U)
563 recover_block = currently_testing - got +
566 } else if (currently_testing == recover_block) {
567 try = blocks_at_once;
573 if (s_flag || v_flag)
574 fputs(_(done_string), stderr);
579 ext2fs_badblocks_list_iterate_end(bb_iter);
581 uncapture_terminate();
586 static unsigned int test_rw (int dev, blk_t last_block,
587 int block_size, blk_t first_block,
588 unsigned int blocks_at_once)
590 unsigned char *buffer, *read_buffer;
591 const unsigned int patterns[] = {0xaa, 0x55, 0xff, 0x00};
592 const unsigned int *pattern;
593 int i, try, got, nr_pattern, pat_idx;
594 unsigned int bb_count = 0;
595 blk_t recover_block = ~0;
597 /* set up abend handler */
598 capture_terminate(NULL);
600 buffer = allocate_buffer(2 * blocks_at_once * block_size);
601 read_buffer = buffer + blocks_at_once * block_size;
604 com_err(program_name, ENOMEM, "%s",
605 _("while allocating buffers"));
612 fputs(_("Checking for bad blocks in read-write mode\n"),
614 fprintf(stderr, _("From block %lu to %lu\n"),
615 (unsigned long) first_block,
616 (unsigned long) last_block - 1);
623 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
625 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
626 pattern_fill(buffer, pattern[pat_idx],
627 blocks_at_once * block_size);
628 num_blocks = last_block - 1;
629 currently_testing = first_block;
630 if (s_flag && v_flag <= 1)
633 try = blocks_at_once;
634 while (currently_testing < last_block) {
635 if (max_bb && bb_count >= max_bb) {
636 if (s_flag || v_flag) {
637 fputs(_("Too many bad blocks, aborting test\n"), stderr);
641 if (currently_testing + try > last_block)
642 try = last_block - currently_testing;
643 got = do_write(dev, buffer, try, block_size,
648 if (got == 0 && try == 1)
649 bb_count += bb_output(currently_testing++, WRITE_ERROR);
650 currently_testing += got;
653 if (recover_block == ~0U)
654 recover_block = currently_testing -
655 got + blocks_at_once;
657 } else if (currently_testing == recover_block) {
658 try = blocks_at_once;
666 fputs(_(done_string), stderr);
669 fputs(_("Reading and comparing: "), stderr);
670 num_blocks = last_block;
671 currently_testing = first_block;
672 if (s_flag && v_flag <= 1)
675 try = blocks_at_once;
676 while (currently_testing < last_block) {
677 if (max_bb && bb_count >= max_bb) {
678 if (s_flag || v_flag) {
679 fputs(_("Too many bad blocks, aborting test\n"), stderr);
683 if (currently_testing + try > last_block)
684 try = last_block - currently_testing;
685 got = do_read (dev, read_buffer, try, block_size,
687 if (got == 0 && try == 1)
688 bb_count += bb_output(currently_testing++, READ_ERROR);
689 currently_testing += got;
692 if (recover_block == ~0U)
693 recover_block = currently_testing -
694 got + blocks_at_once;
696 } else if (currently_testing == recover_block) {
697 try = blocks_at_once;
700 for (i=0; i < got; i++) {
701 if (memcmp(read_buffer + i * block_size,
702 buffer + i * block_size,
704 bb_count += bb_output(currently_testing+i, CORRUPTION_ERROR);
713 fputs(_(done_string), stderr);
716 uncapture_terminate();
721 struct saved_blk_record {
726 static unsigned int test_nd (int dev, blk_t last_block,
727 int block_size, blk_t first_block,
728 unsigned int blocks_at_once)
730 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
731 unsigned char *test_base, *save_base, *read_base;
733 const unsigned int patterns[] = { ~0 };
734 const unsigned int *pattern;
735 int nr_pattern, pat_idx;
736 int got, used2, written;
737 blk_t save_currently_testing;
738 struct saved_blk_record *test_record;
739 /* This is static to prevent being clobbered by the longjmp */
740 static int num_saved;
741 jmp_buf terminate_env;
743 unsigned long buf_used;
744 static unsigned int bb_count;
745 unsigned int granularity = blocks_at_once;
746 blk_t recover_block = ~0U;
749 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
751 com_err(program_name, errcode, "%s",
752 _("while beginning bad block list iteration"));
756 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
757 } while (next_bad && next_bad < first_block);
759 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
760 test_record = malloc(blocks_at_once * sizeof(struct saved_blk_record));
761 if (!blkbuf || !test_record) {
762 com_err(program_name, ENOMEM, "%s",
763 _("while allocating buffers"));
768 test_base = blkbuf + (blocks_at_once * block_size);
769 read_base = blkbuf + (2 * blocks_at_once * block_size);
775 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
776 fprintf (stderr, _("From block %lu to %lu\n"),
777 (unsigned long) first_block,
778 (unsigned long) last_block - 1);
780 if (s_flag || v_flag > 1) {
781 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
783 if (setjmp(terminate_env)) {
785 * Abnormal termination by a signal is handled here.
787 signal (SIGALRM, SIG_IGN);
788 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
790 save_ptr = save_base;
791 for (i=0; i < num_saved; i++) {
792 do_write(dev, save_ptr, test_record[i].num,
793 block_size, test_record[i].block);
794 save_ptr += test_record[i].num * block_size;
800 /* set up abend handler */
801 capture_terminate(terminate_env);
808 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
810 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
811 pattern_fill(test_base, pattern[pat_idx],
812 blocks_at_once * block_size);
816 save_ptr = save_base;
817 test_ptr = test_base;
818 currently_testing = first_block;
819 num_blocks = last_block - 1;
820 if (s_flag && v_flag <= 1)
823 while (currently_testing < last_block) {
824 if (max_bb && bb_count >= max_bb) {
825 if (s_flag || v_flag) {
826 fputs(_("Too many bad blocks, aborting test\n"), stderr);
830 got = try = granularity - buf_used;
832 if (currently_testing == next_bad) {
833 /* fprintf (out, "%lu\n", nextbad); */
834 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
838 else if (currently_testing + try > next_bad)
839 try = next_bad - currently_testing;
841 if (currently_testing + try > last_block)
842 try = last_block - currently_testing;
843 got = do_read (dev, save_ptr, try, block_size,
846 if (recover_block == ~0U)
847 recover_block = currently_testing +
849 if (granularity != 1) {
853 /* First block must have been bad. */
854 bb_count += bb_output(currently_testing++, READ_ERROR);
859 * Note the fact that we've saved this much data
860 * *before* we overwrite it with test data
862 test_record[num_saved].block = currently_testing;
863 test_record[num_saved].num = got;
866 /* Write the test data */
867 written = do_write (dev, test_ptr, got, block_size,
870 com_err (program_name, errno,
871 _("during test data write, block %lu"),
872 (unsigned long) currently_testing +
876 save_ptr += got * block_size;
877 test_ptr += got * block_size;
878 currently_testing += got;
881 if (recover_block == ~0U)
882 recover_block = currently_testing -
883 got + blocks_at_once;
889 * If there's room for more blocks to be tested this
890 * around, and we're not done yet testing the disk, go
891 * back and get some more blocks.
893 if ((buf_used != granularity) &&
894 (currently_testing < last_block))
897 if (currently_testing >= recover_block) {
898 granularity = blocks_at_once;
903 save_currently_testing = currently_testing;
906 * for each contiguous block that we read into the
907 * buffer (and wrote test data into afterwards), read
908 * it back (looping if necessary, to get past newly
909 * discovered unreadable blocks, of which there should
910 * be none, but with a hard drive which is unreliable,
911 * it has happened), and compare with the test data
912 * that was written; output to the bad block list if
916 save_ptr = save_base;
917 test_ptr = test_base;
918 read_ptr = read_base;
923 if (used2 >= num_saved)
925 currently_testing = test_record[used2].block;
926 try = test_record[used2].num;
930 got = do_read (dev, read_ptr, try,
931 block_size, currently_testing);
933 /* test the comparison between all the
934 blocks successfully read */
935 for (i = 0; i < got; ++i)
936 if (memcmp (test_ptr+i*block_size,
937 read_ptr+i*block_size, block_size))
938 bb_count += bb_output(currently_testing + i, CORRUPTION_ERROR);
940 bb_count += bb_output(currently_testing + got, READ_ERROR);
944 /* write back original data */
945 do_write (dev, save_ptr, got,
946 block_size, currently_testing);
947 save_ptr += got * block_size;
949 currently_testing += got;
950 test_ptr += got * block_size;
951 read_ptr += got * block_size;
955 /* empty the buffer so it can be reused */
958 save_ptr = save_base;
959 test_ptr = test_base;
960 currently_testing = save_currently_testing;
964 if (s_flag || v_flag > 1)
965 fputs(_(done_string), stderr);
969 uncapture_terminate();
974 ext2fs_badblocks_list_iterate_end(bb_iter);
979 static void check_mount(char *device_name)
984 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
986 com_err("ext2fs_check_if_mount", retval,
987 _("while determining whether %s is mounted."),
991 if (mount_flags & EXT2_MF_MOUNTED) {
992 fprintf(stderr, _("%s is mounted; "), device_name);
994 fputs(_("badblocks forced anyway. "
995 "Hope /etc/mtab is incorrect.\n"), stderr);
999 fputs(_("it's not safe to run badblocks!\n"), stderr);
1003 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
1004 fprintf(stderr, _("%s is apparently in use by the system; "),
1007 fputs(_("badblocks forced anyway.\n"), stderr);
1009 goto abort_badblocks;
1015 * This function will convert a string to an unsigned long, printing
1016 * an error message if it fails, and returning success or failure in err.
1018 static unsigned int parse_uint(const char *str, const char *descr)
1024 ret = strtoul(str, &tmp, 0);
1025 if (*tmp || errno || (ret > UINT_MAX) ||
1026 (ret == ULONG_MAX && errno == ERANGE)) {
1027 com_err (program_name, 0, _("invalid %s - %s"), descr, str);
1033 int main (int argc, char ** argv)
1037 char * host_device_name = NULL;
1038 char * input_file = NULL;
1039 char * output_file = NULL;
1041 int block_size = 1024;
1042 unsigned int blocks_at_once = 64;
1043 blk64_t last_block, first_block;
1045 int passes_clean = 0;
1048 unsigned int pattern;
1049 unsigned int (*test_func)(int, blk_t,
1056 setbuf(stdout, NULL);
1057 setbuf(stderr, NULL);
1059 setlocale(LC_MESSAGES, "");
1060 setlocale(LC_CTYPE, "");
1061 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1062 textdomain(NLS_CAT_NAME);
1063 set_com_err_gettext(gettext);
1065 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
1066 test_func = test_ro;
1068 /* Determine the system page size if possible */
1070 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1071 #define _SC_PAGESIZE _SC_PAGE_SIZE
1074 sysval = sysconf(_SC_PAGESIZE);
1076 sys_page_size = sysval;
1077 #endif /* _SC_PAGESIZE */
1078 #endif /* HAVE_SYSCONF */
1081 program_name = *argv;
1082 while ((c = getopt (argc, argv, "b:d:e:fi:o:svwnc:p:h:t:BX")) != EOF) {
1085 block_size = parse_uint(optarg, "block size");
1091 input_file = optarg;
1094 output_file = optarg;
1105 test_func = test_rw;
1111 test_func = test_nd;
1115 blocks_at_once = parse_uint(optarg, "blocks at once");
1118 max_bb = parse_uint(optarg, "max bad block count");
1121 d_flag = parse_uint(optarg, "read delay factor");
1124 num_passes = parse_uint(optarg,
1125 "number of clean passes");
1128 host_device_name = optarg;
1131 if (t_flag + 1 > t_max) {
1132 unsigned int *t_patts_new;
1134 t_patts_new = realloc(t_patts, sizeof(int) *
1137 com_err(program_name, ENOMEM,
1138 _("can't allocate memory for "
1139 "test_pattern - %s"),
1143 t_patts = t_patts_new;
1146 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
1147 t_patts[t_flag++] = ~0;
1149 pattern = parse_uint(optarg, "test pattern");
1150 if (pattern == (unsigned int) ~0)
1152 t_patts[t_flag++] = pattern;
1156 use_buffered_io = 1;
1167 com_err(program_name, 0, "%s",
1168 _("Maximum of one test_pattern may be "
1169 "specified in read-only mode"));
1172 if (t_patts && (t_patts[0] == (unsigned int) ~0)) {
1173 com_err(program_name, 0, "%s",
1174 _("Random test_pattern is not allowed "
1175 "in read-only mode"));
1179 if (optind > argc - 1)
1181 device_name = argv[optind++];
1182 if (optind > argc - 1) {
1183 errcode = ext2fs_get_device_size2(device_name,
1186 if (errcode == EXT2_ET_UNIMPLEMENTED) {
1187 com_err(program_name, 0, "%s",
1188 _("Couldn't determine device size; you "
1189 "must specify\nthe size manually\n"));
1193 com_err(program_name, errcode, "%s",
1194 _("while trying to determine device size"));
1199 last_block = parse_uint(argv[optind], _("last block"));
1203 if (optind <= argc-1) {
1205 first_block = parse_uint(argv[optind], _("first block"));
1206 } else first_block = 0;
1207 if (first_block >= last_block) {
1208 com_err (program_name, 0, _("invalid starting block (%llu): must be less than %llu"),
1209 first_block, last_block);
1212 /* ext2 badblocks file can't handle large values */
1213 if (last_block >> 32) {
1214 com_err(program_name, EOVERFLOW,
1215 _("invalid end block (%llu): must be 32-bit value"),
1220 check_mount(device_name);
1222 gettimeofday(&time_start, 0);
1223 open_flag = O_LARGEFILE | (w_flag ? O_RDWR : O_RDONLY);
1224 dev = open (device_name, open_flag);
1226 com_err (program_name, errno, _("while trying to open %s"),
1230 if (host_device_name) {
1231 host_dev = open (host_device_name, open_flag);
1232 if (host_dev == -1) {
1233 com_err (program_name, errno,
1234 _("while trying to open %s"),
1241 if (strcmp (input_file, "-") == 0)
1244 in = fopen (input_file, "r");
1247 com_err (program_name, errno,
1248 _("while trying to open %s"),
1254 if (output_file && strcmp (output_file, "-") != 0)
1256 out = fopen (output_file, "w");
1259 com_err (program_name, errno,
1260 _("while trying to open %s"),
1268 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1270 com_err(program_name, errcode, "%s",
1271 _("while creating in-memory bad blocks list"));
1277 switch (fscanf(in, "%llu\n", &inblk)) {
1279 com_err(program_name, 0, "%s",
1280 _("input file - bad format"));
1286 com_err(program_name,
1288 _("while adding to in-memory "
1293 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1295 com_err(program_name, errcode,
1297 _("while adding to in-memory "
1311 unsigned int bb_count;
1313 bb_count = test_func(dev, last_block, block_size,
1314 first_block, blocks_at_once);
1322 _("Pass completed, %u bad blocks found. (%d/%d/%d errors)\n"),
1323 bb_count, num_read_errors, num_write_errors, num_corruption_errors);
1325 } while (passes_clean < num_passes);