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)
32 #define _GNU_SOURCE /* for O_DIRECT */
50 #include <sys/ioctl.h>
51 #include <sys/types.h>
53 #include "et/com_err.h"
54 #include "ext2fs/ext2_io.h"
55 #include "ext2fs/ext2_fs.h"
56 #include "ext2fs/ext2fs.h"
57 #include "nls-enable.h"
59 const char * program_name = "badblocks";
60 const char * done_string = N_("done \n");
62 static int v_flag = 0; /* verbose */
63 static int w_flag = 0; /* do r/w test: 0=no, 1=yes,
64 * 2=non-destructive */
65 static int s_flag = 0; /* show progress of test */
66 static int force = 0; /* force check of mounted device */
67 static int t_flag = 0; /* number of test patterns */
68 static int t_max = 0; /* allocated test patterns */
69 static unsigned long *t_patts = NULL; /* test patterns */
70 static int current_O_DIRECT = 0; /* Current status of O_DIRECT flag */
74 int sys_page_size = 4096;
76 static void usage(void)
78 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"),
83 static unsigned long currently_testing = 0;
84 static unsigned long num_blocks = 0;
85 static ext2_badblocks_list bb_list = NULL;
87 static blk_t next_bad = 0;
88 static ext2_badblocks_iterate bb_iter = NULL;
90 static void *allocate_buffer(size_t size)
94 #ifdef HAVE_POSIX_MEMALIGN
95 if (posix_memalign(&ret, sys_page_size, size) < 0)
99 ret = memalign(sys_page_size, size);
103 #endif /* HAVE_VALLOC */
104 #endif /* HAVE_MEMALIGN */
105 #endif /* HAVE_POSIX_MEMALIGN */
114 * This routine reports a new bad block. If the bad block has already
115 * been seen before, then it returns 0; otherwise it returns 1.
117 static int bb_output (unsigned long bad)
121 if (ext2fs_badblocks_list_test(bb_list, bad))
124 fprintf(out, "%lu\n", bad);
127 errcode = ext2fs_badblocks_list_add (bb_list, bad);
129 com_err (program_name, errcode, "adding to in-memory bad block list");
134 increment the iteration through the bb_list if
135 an element was just added before the current iteration
136 position. This should not cause next_bad to change. */
137 if (bb_iter && bad < next_bad)
138 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
142 static void print_status(void)
144 fprintf(stderr, "%15ld/%15ld", currently_testing, num_blocks);
145 fputs("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", stderr);
149 static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
151 signal (SIGALRM, alarm_intr);
158 static void *terminate_addr = NULL;
160 static void terminate_intr(int signo EXT2FS_ATTR((unused)))
163 longjmp(terminate_addr,1);
167 static void capture_terminate(jmp_buf term_addr)
169 terminate_addr = term_addr;
170 signal (SIGHUP, terminate_intr);
171 signal (SIGINT, terminate_intr);
172 signal (SIGPIPE, terminate_intr);
173 signal (SIGTERM, terminate_intr);
174 signal (SIGUSR1, terminate_intr);
175 signal (SIGUSR2, terminate_intr);
178 static void uncapture_terminate(void)
180 terminate_addr = NULL;
181 signal (SIGHUP, SIG_DFL);
182 signal (SIGINT, SIG_DFL);
183 signal (SIGPIPE, SIG_DFL);
184 signal (SIGTERM, SIG_DFL);
185 signal (SIGUSR1, SIG_DFL);
186 signal (SIGUSR2, SIG_DFL);
189 static void set_o_direct(int dev, unsigned char *buffer, size_t size,
190 unsigned long current_block)
193 int new_flag = O_DIRECT;
196 if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
197 ((size & (sys_page_size - 1)) != 0) ||
198 ((current_block & ((sys_page_size >> 9)-1)) != 0))
201 if (new_flag != current_O_DIRECT) {
202 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
203 flag = fcntl(dev, F_GETFL);
205 flag = (flag & ~O_DIRECT) | new_flag;
206 fcntl(dev, F_SETFL, flag);
208 current_O_DIRECT = new_flag;
214 static void pattern_fill(unsigned char *buffer, unsigned long pattern,
218 unsigned char bpattern[sizeof(pattern)], *ptr;
220 if (pattern == (unsigned long) ~0) {
221 for (ptr = buffer; ptr < buffer + n; ptr++) {
222 (*ptr) = random() % (1 << (8 * sizeof(char)));
225 fputs(_("Testing with random pattern: "), stderr);
228 for (i = 0; i < sizeof(bpattern); i++) {
231 bpattern[i] = pattern & 0xFF;
232 pattern = pattern >> 8;
235 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
242 if (s_flag | v_flag) {
243 fputs(_("Testing with pattern 0x"), stderr);
244 for (i = 0; i <= nb; i++)
245 fprintf(stderr, "%02x", buffer[i]);
252 * Perform a read of a sequence of blocks; return the number of blocks
253 * successfully sequentially read.
255 static long do_read (int dev, unsigned char * buffer, int try, int block_size,
256 unsigned long current_block)
260 set_o_direct(dev, buffer, try * block_size, current_block);
265 /* Seek to the correct loc. */
266 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
267 SEEK_SET) != (ext2_loff_t) current_block * block_size)
268 com_err (program_name, errno, _("during seek"));
271 got = read (dev, buffer, try * block_size);
275 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
281 * Perform a write of a sequence of blocks; return the number of blocks
282 * successfully sequentially written.
284 static long do_write (int dev, unsigned char * buffer, int try, int block_size,
285 unsigned long current_block)
289 set_o_direct(dev, buffer, try * block_size, current_block);
294 /* Seek to the correct loc. */
295 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
296 SEEK_SET) != (ext2_loff_t) current_block * block_size)
297 com_err (program_name, errno, _("during seek"));
300 got = write (dev, buffer, try * block_size);
304 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
311 static void flush_bufs(void)
315 retval = ext2fs_sync_device(host_dev, 1);
317 com_err(program_name, retval, _("during ext2fs_sync_device"));
320 static unsigned int test_ro (int dev, unsigned long last_block,
321 int block_size, unsigned long from_count,
322 unsigned long blocks_at_once)
324 unsigned char * blkbuf;
327 unsigned int bb_count = 0;
330 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
332 com_err (program_name, errcode,
333 _("while beginning bad block list iteration"));
337 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
338 } while (next_bad && next_bad < from_count);
341 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
343 blkbuf = allocate_buffer(blocks_at_once * block_size);
347 com_err (program_name, ENOMEM, _("while allocating buffers"));
351 fprintf (stderr, _("Checking blocks %lu to %lu\n"), from_count,
355 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
356 pattern_fill(blkbuf + blocks_at_once * block_size,
357 t_patts[0], block_size);
360 try = blocks_at_once;
361 currently_testing = from_count;
362 num_blocks = last_block;
363 if (!t_flag && (s_flag || v_flag)) {
364 fputs(_("Checking for bad blocks (read-only test): "), stderr);
368 while (currently_testing < last_block)
371 if (currently_testing == next_bad) {
372 /* fprintf (out, "%lu\n", nextbad); */
373 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
377 else if (currently_testing + try > next_bad)
378 try = next_bad - currently_testing;
380 if (currently_testing + try > last_block)
381 try = last_block - currently_testing;
382 got = do_read (dev, blkbuf, try, block_size, currently_testing);
384 /* test the comparison between all the
385 blocks successfully read */
387 for (i = 0; i < got; ++i)
388 if (memcmp (blkbuf+i*block_size,
389 blkbuf+blocks_at_once*block_size,
391 bb_count += bb_output(currently_testing + i);
393 currently_testing += got;
395 try = blocks_at_once;
396 /* recover page-aligned offset for O_DIRECT */
397 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
398 && (currently_testing % (sys_page_size >> 9)!= 0))
399 try -= (sys_page_size >> 9)
401 % (sys_page_size >> 9));
407 bb_count += bb_output(currently_testing++);
412 if (s_flag || v_flag)
413 fputs(_(done_string), stderr);
418 ext2fs_badblocks_list_iterate_end(bb_iter);
423 static unsigned int test_rw (int dev, unsigned long last_block,
424 int block_size, unsigned long from_count,
425 unsigned long blocks_at_once)
427 unsigned char *buffer, *read_buffer;
428 const unsigned long patterns[] = {0xaa, 0x55, 0xff, 0x00};
429 const unsigned long *pattern;
430 int i, try, got, nr_pattern, pat_idx;
431 unsigned int bb_count = 0;
433 buffer = allocate_buffer(2 * blocks_at_once * block_size);
434 read_buffer = buffer + blocks_at_once * block_size;
437 com_err (program_name, ENOMEM, _("while allocating buffers"));
444 fputs(_("Checking for bad blocks in read-write mode\n"),
446 fprintf(stderr, _("From block %lu to %lu\n"),
447 from_count, last_block);
454 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
456 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
457 pattern_fill(buffer, pattern[pat_idx],
458 blocks_at_once * block_size);
459 num_blocks = last_block;
460 currently_testing = from_count;
461 if (s_flag && v_flag <= 1)
464 try = blocks_at_once;
465 while (currently_testing < last_block) {
466 if (currently_testing + try > last_block)
467 try = last_block - currently_testing;
468 got = do_write(dev, buffer, try, block_size,
473 currently_testing += got;
475 try = blocks_at_once;
476 /* recover page-aligned offset for O_DIRECT */
477 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
478 && (currently_testing %
479 (sys_page_size >> 9)!= 0))
480 try -= (sys_page_size >> 9)
482 % (sys_page_size >> 9));
487 bb_count += bb_output(currently_testing++);
494 fputs(_(done_string), stderr);
497 fputs(_("Reading and comparing: "), stderr);
498 num_blocks = last_block;
499 currently_testing = from_count;
500 if (s_flag && v_flag <= 1)
503 try = blocks_at_once;
504 while (currently_testing < last_block) {
505 if (currently_testing + try > last_block)
506 try = last_block - currently_testing;
507 got = do_read (dev, read_buffer, try, block_size,
510 bb_count += bb_output(currently_testing++);
513 for (i=0; i < got; i++) {
514 if (memcmp(read_buffer + i * block_size,
515 buffer + i * block_size,
517 bb_count += bb_output(currently_testing+i);
519 currently_testing += got;
520 /* recover page-aligned offset for O_DIRECT */
521 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
522 && (currently_testing % (sys_page_size >> 9)!= 0))
523 try = blocks_at_once - (sys_page_size >> 9)
525 % (sys_page_size >> 9));
527 try = blocks_at_once;
535 fputs(_(done_string), stderr);
538 uncapture_terminate();
543 struct saved_blk_record {
548 static unsigned int test_nd (int dev, unsigned long last_block,
549 int block_size, unsigned long from_count,
550 unsigned long blocks_at_once)
552 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
553 unsigned char *test_base, *save_base, *read_base;
555 const unsigned long patterns[] = { ~0 };
556 const unsigned long *pattern;
557 int nr_pattern, pat_idx;
558 long got, used2, written, save_currently_testing;
559 struct saved_blk_record *test_record;
560 /* This is static to prevent being clobbered by the longjmp */
561 static int num_saved;
562 jmp_buf terminate_env;
564 unsigned long buf_used;
565 static unsigned int bb_count;
568 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
570 com_err (program_name, errcode,
571 _("while beginning bad block list iteration"));
575 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
576 } while (next_bad && next_bad < from_count);
578 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
579 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
580 if (!blkbuf || !test_record) {
581 com_err(program_name, ENOMEM, _("while allocating buffers"));
586 test_base = blkbuf + (blocks_at_once * block_size);
587 read_base = blkbuf + (2 * blocks_at_once * block_size);
593 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
594 fprintf (stderr, _("From block %lu to %lu\n"), from_count, last_block);
596 if (s_flag || v_flag > 1) {
597 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
599 if (setjmp(terminate_env)) {
601 * Abnormal termination by a signal is handled here.
603 signal (SIGALRM, SIG_IGN);
604 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
606 save_ptr = save_base;
607 for (i=0; i < num_saved; i++) {
608 do_write(dev, save_ptr, test_record[i].num,
609 block_size, test_record[i].block);
610 save_ptr += test_record[i].num * block_size;
616 /* set up abend handler */
617 capture_terminate(terminate_env);
624 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
626 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
627 pattern_fill(test_base, pattern[pat_idx],
628 blocks_at_once * block_size);
632 save_ptr = save_base;
633 test_ptr = test_base;
634 currently_testing = from_count;
635 num_blocks = last_block;
636 if (s_flag && v_flag <= 1)
639 while (currently_testing < last_block) {
640 got = try = blocks_at_once - buf_used;
642 if (currently_testing == next_bad) {
643 /* fprintf (out, "%lu\n", nextbad); */
644 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
648 else if (currently_testing + try > next_bad)
649 try = next_bad - currently_testing;
651 if (currently_testing + try > last_block)
652 try = last_block - currently_testing;
653 got = do_read (dev, save_ptr, try, block_size,
656 /* First block must have been bad. */
657 bb_count += bb_output(currently_testing++);
662 * Note the fact that we've saved this much data
663 * *before* we overwrite it with test data
665 test_record[num_saved].block = currently_testing;
666 test_record[num_saved].num = got;
669 /* Write the test data */
670 written = do_write (dev, test_ptr, got, block_size,
673 com_err (program_name, errno,
674 _("during test data write, block %lu"),
675 currently_testing + written);
678 save_ptr += got * block_size;
679 test_ptr += got * block_size;
680 currently_testing += got;
682 bb_count += bb_output(currently_testing++);
686 * If there's room for more blocks to be tested this
687 * around, and we're not done yet testing the disk, go
688 * back and get some more blocks.
690 if ((buf_used != blocks_at_once) &&
691 (currently_testing < last_block))
695 save_currently_testing = currently_testing;
698 * for each contiguous block that we read into the
699 * buffer (and wrote test data into afterwards), read
700 * it back (looping if necessary, to get past newly
701 * discovered unreadable blocks, of which there should
702 * be none, but with a hard drive which is unreliable,
703 * it has happened), and compare with the test data
704 * that was written; output to the bad block list if
708 save_ptr = save_base;
709 test_ptr = test_base;
710 read_ptr = read_base;
715 if (used2 >= num_saved)
717 currently_testing = test_record[used2].block;
718 try = test_record[used2].num;
722 got = do_read (dev, read_ptr, try,
723 block_size, currently_testing);
725 /* test the comparison between all the
726 blocks successfully read */
727 for (i = 0; i < got; ++i)
728 if (memcmp (test_ptr+i*block_size,
729 read_ptr+i*block_size, block_size))
730 bb_count += bb_output(currently_testing + i);
732 bb_count += bb_output(currently_testing + got);
736 /* write back original data */
737 do_write (dev, save_ptr, got,
738 block_size, currently_testing);
739 save_ptr += got * block_size;
741 currently_testing += got;
742 test_ptr += got * block_size;
743 read_ptr += got * block_size;
747 /* empty the buffer so it can be reused */
750 save_ptr = save_base;
751 test_ptr = test_base;
752 currently_testing = save_currently_testing;
756 if (s_flag || v_flag > 1)
757 fputs(_(done_string), stderr);
761 uncapture_terminate();
766 ext2fs_badblocks_list_iterate_end(bb_iter);
771 static void check_mount(char *device_name)
776 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
778 com_err("ext2fs_check_if_mount", retval,
779 _("while determining whether %s is mounted."),
783 if (!(mount_flags & EXT2_MF_MOUNTED))
786 fprintf(stderr, _("%s is mounted; "), device_name);
788 fputs(_("badblocks forced anyway. "
789 "Hope /etc/mtab is incorrect.\n"), stderr);
792 fputs(_("it's not safe to run badblocks!\n"), stderr);
797 int main (int argc, char ** argv)
802 char * host_device_name = NULL;
803 char * input_file = NULL;
804 char * output_file = NULL;
806 int block_size = 1024;
807 unsigned long blocks_at_once = 64;
808 blk_t last_block, from_count;
810 int passes_clean = 0;
813 unsigned long pattern;
814 unsigned int (*test_func)(int, unsigned long,
820 setbuf(stdout, NULL);
821 setbuf(stderr, NULL);
823 setlocale(LC_MESSAGES, "");
824 setlocale(LC_CTYPE, "");
825 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
826 textdomain(NLS_CAT_NAME);
828 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
831 /* Determine the system page size if possible */
833 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
834 #define _SC_PAGESIZE _SC_PAGE_SIZE
837 sysval = sysconf(_SC_PAGESIZE);
839 sys_page_size = sysval;
840 #endif /* _SC_PAGESIZE */
841 #endif /* HAVE_SYSCONF */
844 program_name = *argv;
845 while ((c = getopt (argc, argv, "b:fi:o:svwnc:p:h:t:")) != EOF) {
848 block_size = strtoul (optarg, &tmp, 0);
849 if (*tmp || block_size > 4096) {
850 com_err (program_name, 0,
851 _("bad block size - %s"), optarg);
862 output_file = optarg;
883 blocks_at_once = strtoul (optarg, &tmp, 0);
885 com_err (program_name, 0,
886 "bad simultaneous block count - %s", optarg);
891 num_passes = strtoul (optarg, &tmp, 0);
893 com_err (program_name, 0,
894 "bad number of clean passes - %s", optarg);
899 host_device_name = optarg;
902 if (t_flag + 1 > t_max) {
903 unsigned long *t_patts_new;
905 t_patts_new = realloc(t_patts, t_max + T_INC);
907 com_err(program_name, ENOMEM,
908 _("can't allocate memory for "
909 "test_pattern - %s"),
913 t_patts = t_patts_new;
916 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
917 t_patts[t_flag++] = ~0;
919 pattern = strtoul(optarg, &tmp, 0);
921 com_err(program_name, 0,
922 _("invalid test_pattern: %s\n"),
926 if (pattern == (unsigned long) ~0)
928 t_patts[t_flag++] = pattern;
937 com_err(program_name, 0,
938 _("Maximum of one test_pattern may be specified "
939 "in read-only mode"));
942 if (t_patts && (t_patts[0] == (unsigned long) ~0)) {
943 com_err(program_name, 0,
944 _("Random test_pattern is not allowed "
945 "in read-only mode"));
949 if (optind > argc - 1)
951 device_name = argv[optind++];
952 if (optind > argc - 1) {
953 errcode = ext2fs_get_device_size(device_name,
956 if (errcode == EXT2_ET_UNIMPLEMENTED) {
957 com_err(program_name, 0,
958 _("Couldn't determine device size; you "
959 "must specify\nthe size manually\n"));
963 com_err(program_name, errcode,
964 _("while trying to determine device size"));
968 last_block = strtoul (argv[optind], &tmp, 0);
970 com_err (program_name, 0, _("invalid blocks count - %s"),
976 if (optind <= argc-1) {
977 from_count = strtoul (argv[optind], &tmp, 0);
979 com_err (program_name, 0, _("invalid starting block - %s"),
983 } else from_count = 0;
984 if (from_count >= last_block) {
985 com_err (program_name, 0, _("invalid blocks range: %lu-%lu"),
986 (unsigned long) from_count, (unsigned long) last_block);
990 check_mount(device_name);
992 open_flag = w_flag ? O_RDWR : O_RDONLY;
993 dev = open (device_name, open_flag);
995 com_err (program_name, errno, _("while trying to open %s"),
999 if (host_device_name) {
1000 host_dev = open (host_device_name, open_flag);
1001 if (host_dev == -1) {
1002 com_err (program_name, errno,
1003 _("while trying to open %s"),
1010 if (strcmp (input_file, "-") == 0)
1013 in = fopen (input_file, "r");
1016 com_err (program_name, errno,
1017 _("while trying to open %s"),
1023 if (output_file && strcmp (output_file, "-") != 0)
1025 out = fopen (output_file, "w");
1028 com_err (program_name, errno,
1029 _("while trying to open %s"),
1037 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1039 com_err (program_name, errcode,
1040 _("creating in-memory bad blocks list"));
1046 switch(fscanf (in, "%u\n", &next_bad)) {
1048 com_err (program_name, 0, "input file - bad format");
1053 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1055 com_err (program_name, errcode, _("adding to in-memory bad block list"));
1068 unsigned int bb_count;
1070 bb_count = test_func(dev, last_block, block_size,
1071 from_count, blocks_at_once);
1079 _("Pass completed, %u bad blocks found.\n"),
1082 } while (passes_clean < num_passes);