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)
47 #include <sys/ioctl.h>
48 #include <sys/types.h>
54 #include "et/com_err.h"
55 #include "ext2fs/ext2_io.h"
56 #include <linux/ext2_fs.h>
57 #include "ext2fs/ext2fs.h"
58 #include "nls-enable.h"
60 const char * program_name = "badblocks";
61 const char * done_string = N_("done \n");
63 static int v_flag = 0; /* verbose */
64 static int w_flag = 0; /* do r/w test: 0=no, 1=yes,
65 * 2=non-destructive */
66 static int s_flag = 0; /* show progress of test */
68 static char *blkbuf; /* Allocation array for bad block testing */
71 static void usage(void)
73 fprintf(stderr, _("Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwn]\n [-c blocks_at_once] [-p num_passes] device [blocks_count] [start_count]\n"),
78 static unsigned long currently_testing = 0;
79 static unsigned long num_blocks = 0;
80 static ext2_badblocks_list bb_list = NULL;
82 static blk_t next_bad = 0;
83 static ext2_badblocks_iterate bb_iter = NULL;
86 * This routine reports a new bad block. If the bad block has already
87 * been seen before, then it returns 0; otherwise it returns 1.
89 static int bb_output (unsigned long bad)
93 if (ext2fs_badblocks_list_test(bb_list, bad))
96 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()
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);
163 * Perform a read of a sequence of blocks; return the number of blocks
164 * successfully sequentially read.
166 static long do_read (int dev, char * buffer, int try, int block_size,
167 unsigned long current_block)
174 /* Seek to the correct loc. */
175 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
176 SEEK_SET) != (ext2_loff_t) current_block * block_size)
177 com_err (program_name, errno, _("during seek"));
180 got = read (dev, buffer, try * block_size);
184 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
190 * Perform a write of a sequence of blocks; return the number of blocks
191 * successfully sequentially written.
193 static long do_write (int dev, char * buffer, int try, int block_size,
194 unsigned long current_block)
201 /* Seek to the correct loc. */
202 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
203 SEEK_SET) != (ext2_loff_t) current_block * block_size)
204 com_err (program_name, errno, _("during seek"));
207 got = write (dev, buffer, try * block_size);
212 "Weird value (%ld) in do_write\n", got);
219 static void flush_bufs (int dev)
221 #ifdef HAVE_FDATASYNC
222 if (sync && fdatasync (dev) == -1)
223 com_err (program_name, errno, _("during fdatasync"));
225 if (sync && fsync (dev) == -1)
226 com_err (program_name, errno, _("during fsync"));
230 ioctl (host_dev, BLKFLSBUF, 0); /* In case this is a HD */
233 ioctl (host_dev, FDFLUSH, 0); /* In case this is floppy */
237 static unsigned int test_ro (int dev, unsigned long blocks_count,
238 int block_size, unsigned long from_count,
239 unsigned long blocks_at_once)
244 unsigned int bb_count = 0;
247 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
249 com_err (program_name, errcode,
250 _("while beginning bad block list iteration"));
254 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
255 } while (next_bad && next_bad < from_count);
257 blkbuf = malloc (blocks_at_once * block_size);
260 com_err (program_name, ENOMEM, _("while allocating buffers"));
265 fprintf(stderr, _("Checking for bad blocks in read-only mode\n"));
266 fprintf (stderr, _("From block %lu to %lu\n"), from_count,
269 try = blocks_at_once;
270 currently_testing = from_count;
271 num_blocks = blocks_count;
272 if (s_flag || v_flag > 1) {
274 _("Checking for bad blocks (read-only test): "));
278 while (currently_testing < blocks_count)
281 if (currently_testing == next_bad) {
282 /* fprintf (out, "%lu\n", nextbad); */
283 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
287 else if (currently_testing + try > next_bad)
288 try = next_bad - currently_testing;
290 if (currently_testing + try > blocks_count)
291 try = blocks_count - currently_testing;
292 got = do_read (dev, blkbuf, try, block_size, currently_testing);
293 currently_testing += got;
295 try = blocks_at_once;
301 bb_count += bb_output(currently_testing++);
306 if (s_flag || v_flag > 1)
307 fprintf(stderr, _(done_string));
312 ext2fs_badblocks_list_iterate_end(bb_iter);
317 static unsigned int test_rw (int dev, unsigned long blocks_count,
318 int block_size, unsigned long from_count,
319 unsigned long blocks_at_once)
323 unsigned char pattern[] = {0xaa, 0x55, 0xff, 0x00};
324 unsigned int bb_count = 0;
326 buffer = malloc (2 * block_size);
329 com_err (program_name, ENOMEM, _("while allocating buffers"));
337 _("Checking for bad blocks in read-write mode\n"));
338 fprintf(stderr, _("From block %lu to %lu\n"),
339 from_count, blocks_count);
341 for (i = 0; i < sizeof (pattern); i++) {
342 memset (buffer, pattern[i], block_size);
344 fprintf (stderr, _("Writing pattern 0x%08x: "),
346 num_blocks = blocks_count;
347 currently_testing = from_count;
348 if (s_flag && v_flag <= 1)
351 currently_testing < blocks_count;
354 if (ext2fs_llseek (dev, (ext2_loff_t) currently_testing *
355 block_size, SEEK_SET) !=
356 (ext2_loff_t) currently_testing * block_size)
357 com_err (program_name, errno,
358 _("during seek on block %d"),
362 write (dev, buffer, block_size);
367 fprintf(stderr, _(done_string));
370 fprintf (stderr, _("Reading and comparing: "));
371 num_blocks = blocks_count;
372 currently_testing = from_count;
373 if (s_flag && v_flag <= 1)
376 currently_testing < blocks_count;
379 if (ext2fs_llseek (dev, (ext2_loff_t) currently_testing *
380 block_size, SEEK_SET) !=
381 (ext2_loff_t) currently_testing * block_size)
382 com_err (program_name, errno,
383 _("during seek on block %d"),
387 if ((read (dev, buffer + block_size, block_size)
389 memcmp(buffer, buffer + block_size, block_size))
390 bb_count += bb_output(currently_testing);
395 fprintf(stderr, _(done_string));
402 struct saved_blk_record {
407 static unsigned int test_nd (int dev, unsigned long blocks_count,
408 int block_size, unsigned long from_count,
409 unsigned long blocks_at_once)
411 char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
414 long got, used2, written;
415 struct saved_blk_record *test_record;
417 jmp_buf terminate_env;
419 /* These are static to prevent being clobbered by the longjmp */
420 static long buf_used = 0;
421 static unsigned int bb_count = 0;
423 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
425 com_err (program_name, errcode,
426 _("while beginning bad block list iteration"));
430 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
431 } while (next_bad && next_bad < from_count);
433 blkbuf = malloc (3 * blocks_at_once * block_size);
434 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
435 if (!blkbuf || !test_record) {
436 com_err(program_name, ENOMEM, _("while allocating buffers"));
441 /* inititalize the test data randomly: */
443 fprintf (stderr, _("Initializing random test data\n"));
445 for(ptr = blkbuf + blocks_at_once * block_size;
446 ptr < blkbuf + 2 * blocks_at_once * block_size;
448 (*ptr) = random() % (1 << sizeof(char));
454 _("Checking for bad blocks in non-destructive read-write mode\n"));
455 fprintf (stderr, _("From block %lu to %lu\n"), from_count, blocks_count);
457 if (s_flag || v_flag > 1) {
458 fprintf(stderr, _("Checking for bad blocks (non-destructive read-write test): "));
462 if (setjmp(terminate_env)) {
464 * Abnormal termination by a signal is handled here.
467 fprintf(stderr, _("Interrupt caught, cleaning up\n"));
470 for (i=0; i < num_saved; i++) {
471 do_write(dev, save_ptr, test_record[i].num,
472 block_size, test_record[i].block);
473 save_ptr += test_record[i].num * block_size;
479 /* set up abend handler */
480 capture_terminate(terminate_env);
484 test_ptr = blkbuf + (blocks_at_once * block_size);
485 currently_testing = from_count;
486 num_blocks = blocks_count;
488 while (currently_testing < blocks_count) {
489 try = blocks_at_once - buf_used;
491 if (currently_testing == next_bad) {
492 /* fprintf (out, "%lu\n", nextbad); */
493 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
497 else if (currently_testing + try > next_bad)
498 try = next_bad - currently_testing;
500 if (currently_testing + try > blocks_count)
501 try = blocks_count - currently_testing;
502 got = do_read (dev, save_ptr, try, block_size,
505 /* First block must have been bad. */
506 bb_count += bb_output(currently_testing++);
511 * Note the fact that we've saved this much data
512 * *before* we overwrite it with test data
514 test_record[num_saved].block = currently_testing;
515 test_record[num_saved].num = got;
518 /* Write the test data */
519 written = do_write (dev, test_ptr, got, block_size,
522 com_err (program_name, errno,
523 _("during test data write, block %lu"),
524 currently_testing + written);
527 save_ptr += got * block_size;
528 test_ptr += got * block_size;
529 currently_testing += got;
531 bb_count += bb_output(currently_testing++);
535 * If there's room for more blocks to be tested this
536 * around, and we're not done yet testing the disk, go
537 * back and get some more blocks.
539 if ((buf_used != blocks_at_once) &&
540 (currently_testing < blocks_count))
546 * for each contiguous block that we read into the
547 * buffer (and wrote test data into afterwards), read
548 * it back (looping if necessary, to get past newly
549 * discovered unreadable blocks, of which there should
550 * be none, but with a hard drive which is unreliable,
551 * it has happened), and compare with the test data
552 * that was written; output to the bad block list if
557 test_ptr = blkbuf + (blocks_at_once * block_size);
558 read_ptr = blkbuf + (2 * blocks_at_once * block_size);
563 if (used2 >= num_saved)
565 currently_testing = test_record[used2].block;
566 try = test_record[used2].num;
570 got = do_read (dev, read_ptr, try,
571 block_size, currently_testing);
573 /* test the comparison between all the
574 blocks successfully read */
575 for (i = 0; i < got; ++i)
576 if (memcmp (test_ptr+i*block_size,
577 read_ptr+i*block_size, block_size))
578 bb_count += bb_output(currently_testing + i);
580 bb_count += bb_output(currently_testing + got);
584 /* when done, write back original data */
585 do_write (dev, save_ptr, got, block_size,
588 currently_testing += got;
589 save_ptr += got * block_size;
590 test_ptr += got * block_size;
591 read_ptr += got * block_size;
595 /* empty the buffer so it can be reused */
599 test_ptr = blkbuf + (blocks_at_once * block_size);
603 uncapture_terminate();
604 if (s_flag || v_flag > 1)
605 fprintf(stderr, _(done_string));
611 ext2fs_badblocks_list_iterate_end(bb_iter);
616 int main (int argc, char ** argv)
621 char * host_device_name = NULL;
622 char * input_file = NULL;
623 char * output_file = NULL;
625 int block_size = 1024;
626 unsigned long blocks_at_once = 16;
627 blk_t blocks_count, from_count;
629 int passes_clean = 0;
632 unsigned int (*test_func)(int dev, unsigned long blocks_count,
633 int block_size, unsigned long from_count,
634 unsigned long blocks_at_once);
637 setbuf(stdout, NULL);
638 setbuf(stderr, NULL);
640 setlocale(LC_MESSAGES, "");
641 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
642 textdomain(NLS_CAT_NAME);
647 program_name = *argv;
648 while ((c = getopt (argc, argv, "b:i:o:svwnc:p:h:")) != EOF) {
651 block_size = strtoul (optarg, &tmp, 0);
652 if (*tmp || block_size > 4096) {
653 com_err (program_name, 0,
654 _("bad block size - %s"), optarg);
662 output_file = optarg;
683 blocks_at_once = strtoul (optarg, &tmp, 0);
685 com_err (program_name, 0,
686 "bad simultaneous block count - %s", optarg);
691 num_passes = strtoul (optarg, &tmp, 0);
693 com_err (program_name, 0,
694 "bad number of clean passes - %s", optarg);
699 host_device_name = optarg;
705 if (optind > argc - 1)
707 device_name = argv[optind++];
708 if (optind > argc - 1) {
709 errcode = ext2fs_get_device_size(device_name,
712 if (errcode == EXT2_ET_UNIMPLEMENTED) {
713 com_err(program_name, 0,
714 _("Couldn't determine device size; you "
715 "must specify\nthe size manually\n"));
719 com_err(program_name, errcode,
720 _("while trying to determine device size"));
724 blocks_count = strtoul (argv[optind], &tmp, 0);
726 com_err (program_name, 0, _("bad blocks count - %s"),
732 if (optind <= argc-1) {
733 from_count = strtoul (argv[optind], &tmp, 0);
734 } else from_count = 0;
735 if (from_count >= blocks_count) {
736 com_err (program_name, 0, _("bad blocks range: %lu-%lu"),
737 from_count, blocks_count);
740 dev = open (device_name, w_flag ? O_RDWR : O_RDONLY);
743 com_err (program_name, errno, _("while trying to open %s"),
747 if (host_device_name) {
748 host_dev = open (host_device_name, O_RDONLY);
751 com_err (program_name, errno,
752 _("while trying to open %s"),
759 if (strcmp (input_file, "-") == 0)
762 in = fopen (input_file, "r");
765 com_err (program_name, errno,
766 _("while trying to open %s"),
771 if (output_file && strcmp (output_file, "-") != 0)
773 out = fopen (output_file, "w");
776 com_err (program_name, errno,
777 _("while trying to open %s"),
785 errcode = ext2fs_badblocks_list_create(&bb_list,0);
787 com_err (program_name, errcode,
788 _("creating in-memory bad blocks list"));
794 switch(fscanf (in, "%lu\n", &next_bad)) {
796 com_err (program_name, 0, "input file - bad format");
801 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
803 com_err (program_name, errcode, _("adding to in-memory bad block list"));
816 unsigned int bb_count;
818 bb_count = test_func(dev, blocks_count, block_size,
819 from_count, blocks_at_once);
827 _("Pass completed, %u bad blocks found.\n"),
830 } while (passes_clean < num_passes);