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)
48 #include <sys/ioctl.h>
49 #include <sys/types.h>
51 #include "et/com_err.h"
52 #include "ext2fs/ext2_io.h"
53 #include "ext2fs/ext2_fs.h"
54 #include "ext2fs/ext2fs.h"
55 #include "nls-enable.h"
57 const char * program_name = "badblocks";
58 const char * done_string = N_("done \n");
60 static int v_flag = 0; /* verbose */
61 static int w_flag = 0; /* do r/w test: 0=no, 1=yes,
62 * 2=non-destructive */
63 static int s_flag = 0; /* show progress of test */
64 static int force = 0; /* force check of mounted device */
65 static int t_flag = 0; /* number of test patterns */
66 static int t_max = 0; /* allocated test patterns */
67 static unsigned long *t_patts = NULL; /* test patterns */
70 static void usage(void)
72 fprintf(stderr, _("Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwnf]\n [-c blocks_at_once] [-p num_passes] [-t test_pattern [-t test_pattern [...]]]\n device [last_block [start_block]]\n"),
77 static unsigned long currently_testing = 0;
78 static unsigned long num_blocks = 0;
79 static ext2_badblocks_list bb_list = NULL;
81 static blk_t next_bad = 0;
82 static ext2_badblocks_iterate bb_iter = NULL;
85 * This routine reports a new bad block. If the bad block has already
86 * been seen before, then it returns 0; otherwise it returns 1.
88 static int bb_output (unsigned long bad)
92 if (ext2fs_badblocks_list_test(bb_list, bad))
95 fprintf(out, "%lu\n", bad);
98 errcode = ext2fs_badblocks_list_add (bb_list, bad);
100 com_err (program_name, errcode, "adding to in-memory bad block list");
105 increment the iteration through the bb_list if
106 an element was just added before the current iteration
107 position. This should not cause next_bad to change. */
108 if (bb_iter && bad < next_bad)
109 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
113 static void print_status(void)
115 fprintf(stderr, "%9ld/%9ld", currently_testing, num_blocks);
116 fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
120 static void alarm_intr(int alnum)
122 signal (SIGALRM, alarm_intr);
126 fprintf(stderr, "%9ld/%9ld", currently_testing, num_blocks);
127 fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
131 static void *terminate_addr = NULL;
133 static void terminate_intr(int signo)
136 longjmp(terminate_addr,1);
140 static void capture_terminate(jmp_buf term_addr)
142 terminate_addr = term_addr;
143 signal (SIGHUP, terminate_intr);
144 signal (SIGINT, terminate_intr);
145 signal (SIGPIPE, terminate_intr);
146 signal (SIGTERM, terminate_intr);
147 signal (SIGUSR1, terminate_intr);
148 signal (SIGUSR2, terminate_intr);
151 static void uncapture_terminate(void)
153 terminate_addr = NULL;
154 signal (SIGHUP, SIG_DFL);
155 signal (SIGINT, SIG_DFL);
156 signal (SIGPIPE, SIG_DFL);
157 signal (SIGTERM, SIG_DFL);
158 signal (SIGUSR1, SIG_DFL);
159 signal (SIGUSR2, SIG_DFL);
162 static void pattern_fill(unsigned char *buffer, unsigned long pattern,
166 unsigned char bpattern[sizeof(pattern)], *ptr;
169 for (ptr = buffer; ptr < buffer + n; ptr++) {
170 (*ptr) = random() % (1 << (8 * sizeof(char)));
173 fprintf(stderr, _("Testing with random pattern: "));
176 for (i = 0; i < sizeof(bpattern); i++) {
179 bpattern[i] = pattern & 0xFF;
180 pattern = pattern >> 8;
183 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
184 *ptr = bpattern[i--];
188 if (s_flag | v_flag) {
189 fprintf(stderr, _("Testing with pattern 0x"));
190 for (i = 0; i <= nb; i++)
191 fprintf(stderr, "%02x", buffer[i]);
192 fprintf(stderr, ": ");
198 * Perform a read of a sequence of blocks; return the number of blocks
199 * successfully sequentially read.
201 static long do_read (int dev, unsigned char * buffer, int try, int block_size,
202 unsigned long current_block)
209 /* Seek to the correct loc. */
210 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
211 SEEK_SET) != (ext2_loff_t) current_block * block_size)
212 com_err (program_name, errno, _("during seek"));
215 got = read (dev, buffer, try * block_size);
219 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
225 * Perform a write of a sequence of blocks; return the number of blocks
226 * successfully sequentially written.
228 static long do_write (int dev, unsigned char * buffer, int try, int block_size,
229 unsigned long current_block)
236 /* Seek to the correct loc. */
237 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
238 SEEK_SET) != (ext2_loff_t) current_block * block_size)
239 com_err (program_name, errno, _("during seek"));
242 got = write (dev, buffer, try * block_size);
247 "Weird value (%ld) in do_write\n", got);
254 static void flush_bufs(void)
258 retval = ext2fs_sync_device(host_dev, 1);
260 com_err(program_name, retval, _("during ext2fs_sync_device"));
263 static unsigned int test_ro (int dev, unsigned long last_block,
264 int block_size, unsigned long from_count,
265 unsigned long blocks_at_once)
267 unsigned char * blkbuf;
270 unsigned int bb_count = 0;
273 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
275 com_err (program_name, errcode,
276 _("while beginning bad block list iteration"));
280 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
281 } while (next_bad && next_bad < from_count);
284 blkbuf = malloc ((blocks_at_once + 1) * block_size);
286 blkbuf = malloc (blocks_at_once * block_size);
290 com_err (program_name, ENOMEM, _("while allocating buffers"));
294 fprintf (stderr, _("Checking blocks %lu to %lu\n"), from_count,
298 fprintf(stderr, _("Checking for bad blocks in read-only mode\n"));
299 pattern_fill(blkbuf + blocks_at_once * block_size,
300 t_patts[0], block_size);
303 try = blocks_at_once;
304 currently_testing = from_count;
305 num_blocks = last_block;
306 if (!t_flag && (s_flag || v_flag)) {
308 _("Checking for bad blocks (read-only test): "));
312 while (currently_testing < last_block)
315 if (currently_testing == next_bad) {
316 /* fprintf (out, "%lu\n", nextbad); */
317 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
321 else if (currently_testing + try > next_bad)
322 try = next_bad - currently_testing;
324 if (currently_testing + try > last_block)
325 try = last_block - currently_testing;
326 got = do_read (dev, blkbuf, try, block_size, currently_testing);
328 /* test the comparison between all the
329 blocks successfully read */
331 for (i = 0; i < got; ++i)
332 if (memcmp (blkbuf+i*block_size,
333 blkbuf+blocks_at_once*block_size,
335 bb_count += bb_output(currently_testing + i);
337 currently_testing += got;
339 try = blocks_at_once;
345 bb_count += bb_output(currently_testing++);
350 if (s_flag || v_flag)
351 fputs(done_string, stderr);
356 ext2fs_badblocks_list_iterate_end(bb_iter);
361 static unsigned int test_rw (int dev, unsigned long last_block,
362 int block_size, unsigned long from_count,
363 unsigned long blocks_at_once)
365 unsigned char * buffer;
366 const unsigned long patterns[] = {0xaa, 0x55, 0xff, 0x00};
367 const unsigned long *pattern;
368 int nr_pattern, pat_idx;
369 unsigned int bb_count = 0;
371 buffer = malloc (2 * block_size);
374 com_err (program_name, ENOMEM, _("while allocating buffers"));
382 _("Checking for bad blocks in read-write mode\n"));
383 fprintf(stderr, _("From block %lu to %lu\n"),
384 from_count, last_block);
391 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
393 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
394 pattern_fill(buffer, pattern[pat_idx], block_size);
395 num_blocks = last_block;
396 currently_testing = from_count;
397 if (s_flag && v_flag <= 1)
400 currently_testing < last_block;
403 if (ext2fs_llseek (dev, (ext2_loff_t) currently_testing *
404 block_size, SEEK_SET) !=
405 (ext2_loff_t) currently_testing * block_size)
406 com_err (program_name, errno,
407 _("during seek on block %d"),
411 write (dev, buffer, block_size);
416 fputs(done_string, stderr);
419 fprintf (stderr, _("Reading and comparing: "));
420 num_blocks = last_block;
421 currently_testing = from_count;
422 if (s_flag && v_flag <= 1)
425 currently_testing < last_block;
428 if (ext2fs_llseek (dev, (ext2_loff_t) currently_testing *
429 block_size, SEEK_SET) !=
430 (ext2_loff_t) currently_testing * block_size)
431 com_err (program_name, errno,
432 _("during seek on block %d"),
436 if ((read (dev, buffer + block_size, block_size)
438 memcmp(buffer, buffer + block_size, block_size))
439 bb_count += bb_output(currently_testing);
444 fputs(done_string, stderr);
447 uncapture_terminate();
452 struct saved_blk_record {
457 static unsigned int test_nd (int dev, unsigned long last_block,
458 int block_size, unsigned long from_count,
459 unsigned long blocks_at_once)
461 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
463 const unsigned long patterns[] = { ~0 };
464 const unsigned long *pattern;
465 int nr_pattern, pat_idx;
466 long got, used2, written, save_currently_testing;
467 struct saved_blk_record *test_record;
468 /* This is static to prevent being clobbered by the longjmp */
469 static int num_saved;
470 jmp_buf terminate_env;
473 unsigned int bb_count = 0;
475 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
477 com_err (program_name, errcode,
478 _("while beginning bad block list iteration"));
482 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
483 } while (next_bad && next_bad < from_count);
485 blkbuf = malloc (3 * blocks_at_once * block_size);
486 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
487 if (!blkbuf || !test_record) {
488 com_err(program_name, ENOMEM, _("while allocating buffers"));
496 _("Checking for bad blocks in non-destructive read-write mode\n"));
497 fprintf (stderr, _("From block %lu to %lu\n"), from_count, last_block);
499 if (s_flag || v_flag > 1) {
500 fprintf(stderr, _("Checking for bad blocks (non-destructive read-write test)\n"));
502 if (setjmp(terminate_env)) {
504 * Abnormal termination by a signal is handled here.
506 signal (SIGALRM, SIG_IGN);
507 fprintf(stderr, _("\nInterrupt caught, cleaning up\n"));
510 for (i=0; i < num_saved; i++) {
511 do_write(dev, save_ptr, test_record[i].num,
512 block_size, test_record[i].block);
513 save_ptr += test_record[i].num * block_size;
519 /* set up abend handler */
520 capture_terminate(terminate_env);
527 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
529 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
530 pattern_fill(blkbuf + blocks_at_once * block_size,
531 pattern[pat_idx], blocks_at_once * block_size);
536 test_ptr = blkbuf + (blocks_at_once * block_size);
537 currently_testing = from_count;
538 num_blocks = last_block;
539 if (s_flag && v_flag <= 1)
542 while (currently_testing < last_block) {
543 try = blocks_at_once - buf_used;
545 if (currently_testing == next_bad) {
546 /* fprintf (out, "%lu\n", nextbad); */
547 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
551 else if (currently_testing + try > next_bad)
552 try = next_bad - currently_testing;
554 if (currently_testing + try > last_block)
555 try = last_block - currently_testing;
556 got = do_read (dev, save_ptr, try, block_size,
559 /* First block must have been bad. */
560 bb_count += bb_output(currently_testing++);
565 * Note the fact that we've saved this much data
566 * *before* we overwrite it with test data
568 test_record[num_saved].block = currently_testing;
569 test_record[num_saved].num = got;
572 /* Write the test data */
573 written = do_write (dev, test_ptr, got, block_size,
576 com_err (program_name, errno,
577 _("during test data write, block %lu"),
578 currently_testing + written);
581 save_ptr += got * block_size;
582 test_ptr += got * block_size;
583 currently_testing += got;
585 bb_count += bb_output(currently_testing++);
589 * If there's room for more blocks to be tested this
590 * around, and we're not done yet testing the disk, go
591 * back and get some more blocks.
593 if ((buf_used != blocks_at_once) &&
594 (currently_testing < last_block))
598 save_currently_testing = currently_testing;
601 * for each contiguous block that we read into the
602 * buffer (and wrote test data into afterwards), read
603 * it back (looping if necessary, to get past newly
604 * discovered unreadable blocks, of which there should
605 * be none, but with a hard drive which is unreliable,
606 * it has happened), and compare with the test data
607 * that was written; output to the bad block list if
612 test_ptr = blkbuf + (blocks_at_once * block_size);
613 read_ptr = blkbuf + (2 * blocks_at_once * block_size);
618 if (used2 >= num_saved)
620 currently_testing = test_record[used2].block;
621 try = test_record[used2].num;
625 got = do_read (dev, read_ptr, try,
626 block_size, currently_testing);
628 /* test the comparison between all the
629 blocks successfully read */
630 for (i = 0; i < got; ++i)
631 if (memcmp (test_ptr+i*block_size,
632 read_ptr+i*block_size, block_size))
633 bb_count += bb_output(currently_testing + i);
635 bb_count += bb_output(currently_testing + got);
639 /* when done, write back original data */
640 do_write (dev, save_ptr, got, block_size,
643 currently_testing += got;
644 save_ptr += got * block_size;
645 test_ptr += got * block_size;
646 read_ptr += got * block_size;
650 /* empty the buffer so it can be reused */
654 test_ptr = blkbuf + (blocks_at_once * block_size);
655 currently_testing = save_currently_testing;
659 if (s_flag || v_flag > 1)
660 fputs(done_string, stderr);
664 uncapture_terminate();
669 ext2fs_badblocks_list_iterate_end(bb_iter);
674 static void check_mount(char *device_name)
679 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
681 com_err("ext2fs_check_if_mount", retval,
682 _("while determining whether %s is mounted."),
686 if (!(mount_flags & EXT2_MF_MOUNTED))
689 fprintf(stderr, _("%s is mounted; "), device_name);
691 fprintf(stderr, _("badblocks forced anyway. "
692 "Hope /etc/mtab is incorrect.\n"));
695 fprintf(stderr, _("it's not safe to run badblocks!\n"));
700 int main (int argc, char ** argv)
705 char * host_device_name = NULL;
706 char * input_file = NULL;
707 char * output_file = NULL;
709 int block_size = 1024;
710 unsigned long blocks_at_once = 16;
711 blk_t last_block, from_count;
713 int passes_clean = 0;
716 unsigned long pattern;
717 unsigned int (*test_func)(int, unsigned long,
721 setbuf(stdout, NULL);
722 setbuf(stderr, NULL);
724 setlocale(LC_MESSAGES, "");
725 setlocale(LC_CTYPE, "");
726 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
727 textdomain(NLS_CAT_NAME);
729 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
733 program_name = *argv;
734 while ((c = getopt (argc, argv, "b:fi:o:svwnc:p:h:t:")) != EOF) {
737 block_size = strtoul (optarg, &tmp, 0);
738 if (*tmp || block_size > 4096) {
739 com_err (program_name, 0,
740 _("bad block size - %s"), optarg);
751 output_file = optarg;
772 blocks_at_once = strtoul (optarg, &tmp, 0);
774 com_err (program_name, 0,
775 "bad simultaneous block count - %s", optarg);
780 num_passes = strtoul (optarg, &tmp, 0);
782 com_err (program_name, 0,
783 "bad number of clean passes - %s", optarg);
788 host_device_name = optarg;
791 if (t_flag + 1 > t_max) {
792 unsigned long *t_patts_new;
794 t_patts_new = realloc(t_patts, t_max + T_INC);
796 com_err(program_name, ENOMEM,
797 _("can't allocate memory for "
798 "test_pattern - %s"),
802 t_patts = t_patts_new;
805 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
806 t_patts[t_flag++] = ~0;
808 pattern = strtoul(optarg, &tmp, 0);
810 com_err(program_name, 0,
811 _("invalid test_pattern: %s\n"),
817 t_patts[t_flag++] = pattern;
826 com_err(program_name, 0,
827 _("Maximum of one test_pattern may be specified "
828 "in read-only mode"));
831 if (t_patts && (t_patts[0] == ~0)) {
832 com_err(program_name, 0,
833 _("Random test_pattern is not allowed "
834 "in read-only mode"));
838 if (optind > argc - 1)
840 device_name = argv[optind++];
841 if (optind > argc - 1) {
842 errcode = ext2fs_get_device_size(device_name,
845 if (errcode == EXT2_ET_UNIMPLEMENTED) {
846 com_err(program_name, 0,
847 _("Couldn't determine device size; you "
848 "must specify\nthe size manually\n"));
852 com_err(program_name, errcode,
853 _("while trying to determine device size"));
857 last_block = strtoul (argv[optind], &tmp, 0);
859 com_err (program_name, 0, _("bad blocks count - %s"),
865 if (optind <= argc-1) {
866 from_count = strtoul (argv[optind], &tmp, 0);
868 com_err (program_name, 0, _("bad starting block - %s"),
872 } else from_count = 0;
873 if (from_count >= last_block) {
874 com_err (program_name, 0, _("bad blocks range: %lu-%lu"),
875 from_count, last_block);
879 check_mount(device_name);
881 dev = open (device_name, O_RDWR);
882 if ((dev == -1) && ((errno == EPERM) || (errno == EACCES) ||
885 dev = open(device_name, O_RDONLY);
887 com_err (program_name, errno, _("while trying to open %s"),
891 if (host_device_name) {
892 host_dev = open (host_device_name, O_RDWR);
893 if ((host_dev == -1) &&
894 ((errno == EPERM) || (errno == EACCES)))
895 host_dev = open(host_device_name, O_RDONLY);
896 if (host_dev == -1) {
897 com_err (program_name, errno,
898 _("while trying to open %s"),
905 if (strcmp (input_file, "-") == 0)
908 in = fopen (input_file, "r");
911 com_err (program_name, errno,
912 _("while trying to open %s"),
918 if (output_file && strcmp (output_file, "-") != 0)
920 out = fopen (output_file, "w");
923 com_err (program_name, errno,
924 _("while trying to open %s"),
932 errcode = ext2fs_badblocks_list_create(&bb_list,0);
934 com_err (program_name, errcode,
935 _("creating in-memory bad blocks list"));
941 switch(fscanf (in, "%u\n", &next_bad)) {
943 com_err (program_name, 0, "input file - bad format");
948 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
950 com_err (program_name, errcode, _("adding to in-memory bad block list"));
963 unsigned int bb_count;
965 bb_count = test_func(dev, last_block, block_size,
966 from_count, blocks_at_once);
974 _("Pass completed, %u bad blocks found.\n"),
977 } while (passes_clean < num_passes);