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, "%9ld/%9ld", currently_testing, num_blocks);
145 fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
149 static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
151 signal (SIGALRM, alarm_intr);
155 fprintf(stderr, "%9ld/%9ld", currently_testing, num_blocks);
156 fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
160 static void *terminate_addr = NULL;
162 static void terminate_intr(int signo EXT2FS_ATTR((unused)))
165 longjmp(terminate_addr,1);
169 static void capture_terminate(jmp_buf term_addr)
171 terminate_addr = term_addr;
172 signal (SIGHUP, terminate_intr);
173 signal (SIGINT, terminate_intr);
174 signal (SIGPIPE, terminate_intr);
175 signal (SIGTERM, terminate_intr);
176 signal (SIGUSR1, terminate_intr);
177 signal (SIGUSR2, terminate_intr);
180 static void uncapture_terminate(void)
182 terminate_addr = NULL;
183 signal (SIGHUP, SIG_DFL);
184 signal (SIGINT, SIG_DFL);
185 signal (SIGPIPE, SIG_DFL);
186 signal (SIGTERM, SIG_DFL);
187 signal (SIGUSR1, SIG_DFL);
188 signal (SIGUSR2, SIG_DFL);
191 static void set_o_direct(int dev, unsigned char *buffer, size_t size,
192 unsigned long current_block)
195 int new_flag = O_DIRECT;
198 if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
199 ((size & (sys_page_size - 1)) != 0) ||
200 ((current_block & ((sys_page_size >> 9)-1)) != 0))
203 if (new_flag != current_O_DIRECT) {
204 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
205 flag = fcntl(dev, F_GETFL);
207 flag = (flag & ~O_DIRECT) | new_flag;
208 fcntl(dev, F_SETFL, flag);
210 current_O_DIRECT = new_flag;
216 static void pattern_fill(unsigned char *buffer, unsigned long pattern,
220 unsigned char bpattern[sizeof(pattern)], *ptr;
222 if (pattern == (unsigned long) ~0) {
223 for (ptr = buffer; ptr < buffer + n; ptr++) {
224 (*ptr) = random() % (1 << (8 * sizeof(char)));
227 fputs(_("Testing with random pattern: "), stderr);
230 for (i = 0; i < sizeof(bpattern); i++) {
233 bpattern[i] = pattern & 0xFF;
234 pattern = pattern >> 8;
237 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
244 if (s_flag | v_flag) {
245 fputs(_("Testing with pattern 0x"), stderr);
246 for (i = 0; i <= nb; i++)
247 fprintf(stderr, "%02x", buffer[i]);
254 * Perform a read of a sequence of blocks; return the number of blocks
255 * successfully sequentially read.
257 static long do_read (int dev, unsigned char * buffer, int try, int block_size,
258 unsigned long current_block)
262 set_o_direct(dev, buffer, try * block_size, current_block);
267 /* Seek to the correct loc. */
268 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
269 SEEK_SET) != (ext2_loff_t) current_block * block_size)
270 com_err (program_name, errno, _("during seek"));
273 got = read (dev, buffer, try * block_size);
277 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
283 * Perform a write of a sequence of blocks; return the number of blocks
284 * successfully sequentially written.
286 static long do_write (int dev, unsigned char * buffer, int try, int block_size,
287 unsigned long current_block)
291 set_o_direct(dev, buffer, try * block_size, current_block);
296 /* Seek to the correct loc. */
297 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
298 SEEK_SET) != (ext2_loff_t) current_block * block_size)
299 com_err (program_name, errno, _("during seek"));
302 got = write (dev, buffer, try * block_size);
306 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
313 static void flush_bufs(void)
317 retval = ext2fs_sync_device(host_dev, 1);
319 com_err(program_name, retval, _("during ext2fs_sync_device"));
322 static unsigned int test_ro (int dev, unsigned long last_block,
323 int block_size, unsigned long from_count,
324 unsigned long blocks_at_once)
326 unsigned char * blkbuf;
329 unsigned int bb_count = 0;
332 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
334 com_err (program_name, errcode,
335 _("while beginning bad block list iteration"));
339 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
340 } while (next_bad && next_bad < from_count);
343 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
345 blkbuf = allocate_buffer(blocks_at_once * block_size);
349 com_err (program_name, ENOMEM, _("while allocating buffers"));
353 fprintf (stderr, _("Checking blocks %lu to %lu\n"), from_count,
357 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
358 pattern_fill(blkbuf + blocks_at_once * block_size,
359 t_patts[0], block_size);
362 try = blocks_at_once;
363 currently_testing = from_count;
364 num_blocks = last_block;
365 if (!t_flag && (s_flag || v_flag)) {
366 fputs(_("Checking for bad blocks (read-only test): "), stderr);
370 while (currently_testing < last_block)
373 if (currently_testing == next_bad) {
374 /* fprintf (out, "%lu\n", nextbad); */
375 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
379 else if (currently_testing + try > next_bad)
380 try = next_bad - currently_testing;
382 if (currently_testing + try > last_block)
383 try = last_block - currently_testing;
384 got = do_read (dev, blkbuf, try, block_size, currently_testing);
386 /* test the comparison between all the
387 blocks successfully read */
389 for (i = 0; i < got; ++i)
390 if (memcmp (blkbuf+i*block_size,
391 blkbuf+blocks_at_once*block_size,
393 bb_count += bb_output(currently_testing + i);
395 currently_testing += got;
397 try = blocks_at_once;
398 /* recover page-aligned offset for O_DIRECT */
399 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
400 && (currently_testing % (sys_page_size >> 9)!= 0))
401 try -= (sys_page_size >> 9)
403 % (sys_page_size >> 9));
409 bb_count += bb_output(currently_testing++);
414 if (s_flag || v_flag)
415 fputs(_(done_string), stderr);
420 ext2fs_badblocks_list_iterate_end(bb_iter);
425 static unsigned int test_rw (int dev, unsigned long last_block,
426 int block_size, unsigned long from_count,
427 unsigned long blocks_at_once)
429 unsigned char *buffer, *read_buffer;
430 const unsigned long patterns[] = {0xaa, 0x55, 0xff, 0x00};
431 const unsigned long *pattern;
432 int i, try, got, nr_pattern, pat_idx;
433 unsigned int bb_count = 0;
435 buffer = allocate_buffer(2 * blocks_at_once * block_size);
436 read_buffer = buffer + blocks_at_once * block_size;
439 com_err (program_name, ENOMEM, _("while allocating buffers"));
446 fputs(_("Checking for bad blocks in read-write mode\n"),
448 fprintf(stderr, _("From block %lu to %lu\n"),
449 from_count, last_block);
456 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
458 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
459 pattern_fill(buffer, pattern[pat_idx],
460 blocks_at_once * block_size);
461 num_blocks = last_block;
462 currently_testing = from_count;
463 if (s_flag && v_flag <= 1)
466 try = blocks_at_once;
467 while (currently_testing < last_block) {
468 if (currently_testing + try > last_block)
469 try = last_block - currently_testing;
470 got = do_write(dev, buffer, try, block_size,
475 currently_testing += got;
477 try = blocks_at_once;
478 /* recover page-aligned offset for O_DIRECT */
479 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
480 && (currently_testing %
481 (sys_page_size >> 9)!= 0))
482 try -= (sys_page_size >> 9)
484 % (sys_page_size >> 9));
489 bb_count += bb_output(currently_testing++);
496 fputs(_(done_string), stderr);
499 fputs(_("Reading and comparing: "), stderr);
500 num_blocks = last_block;
501 currently_testing = from_count;
502 if (s_flag && v_flag <= 1)
505 try = blocks_at_once;
506 while (currently_testing < last_block) {
507 if (currently_testing + try > last_block)
508 try = last_block - currently_testing;
509 got = do_read (dev, read_buffer, try, block_size,
512 bb_count += bb_output(currently_testing++);
515 for (i=0; i < got; i++) {
516 if (memcmp(read_buffer + i * block_size,
517 buffer + i * block_size,
519 bb_count += bb_output(currently_testing+i);
521 currently_testing += got;
522 /* recover page-aligned offset for O_DIRECT */
523 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
524 && (currently_testing % (sys_page_size >> 9)!= 0))
525 try = blocks_at_once - (sys_page_size >> 9)
527 % (sys_page_size >> 9));
529 try = blocks_at_once;
537 fputs(_(done_string), stderr);
540 uncapture_terminate();
545 struct saved_blk_record {
550 static unsigned int test_nd (int dev, unsigned long last_block,
551 int block_size, unsigned long from_count,
552 unsigned long blocks_at_once)
554 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
555 unsigned char *test_base, *save_base, *read_base;
557 const unsigned long patterns[] = { ~0 };
558 const unsigned long *pattern;
559 int nr_pattern, pat_idx;
560 long got, used2, written, save_currently_testing;
561 struct saved_blk_record *test_record;
562 /* This is static to prevent being clobbered by the longjmp */
563 static int num_saved;
564 jmp_buf terminate_env;
566 unsigned long buf_used;
567 static unsigned int bb_count;
570 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
572 com_err (program_name, errcode,
573 _("while beginning bad block list iteration"));
577 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
578 } while (next_bad && next_bad < from_count);
580 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
581 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
582 if (!blkbuf || !test_record) {
583 com_err(program_name, ENOMEM, _("while allocating buffers"));
588 test_base = blkbuf + (blocks_at_once * block_size);
589 read_base = blkbuf + (2 * blocks_at_once * block_size);
595 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
596 fprintf (stderr, _("From block %lu to %lu\n"), from_count, last_block);
598 if (s_flag || v_flag > 1) {
599 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
601 if (setjmp(terminate_env)) {
603 * Abnormal termination by a signal is handled here.
605 signal (SIGALRM, SIG_IGN);
606 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
608 save_ptr = save_base;
609 for (i=0; i < num_saved; i++) {
610 do_write(dev, save_ptr, test_record[i].num,
611 block_size, test_record[i].block);
612 save_ptr += test_record[i].num * block_size;
618 /* set up abend handler */
619 capture_terminate(terminate_env);
626 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
628 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
629 pattern_fill(test_base, pattern[pat_idx],
630 blocks_at_once * block_size);
634 save_ptr = save_base;
635 test_ptr = test_base;
636 currently_testing = from_count;
637 num_blocks = last_block;
638 if (s_flag && v_flag <= 1)
641 while (currently_testing < last_block) {
642 got = try = blocks_at_once - buf_used;
644 if (currently_testing == next_bad) {
645 /* fprintf (out, "%lu\n", nextbad); */
646 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
650 else if (currently_testing + try > next_bad)
651 try = next_bad - currently_testing;
653 if (currently_testing + try > last_block)
654 try = last_block - currently_testing;
655 got = do_read (dev, save_ptr, try, block_size,
658 /* First block must have been bad. */
659 bb_count += bb_output(currently_testing++);
664 * Note the fact that we've saved this much data
665 * *before* we overwrite it with test data
667 test_record[num_saved].block = currently_testing;
668 test_record[num_saved].num = got;
671 /* Write the test data */
672 written = do_write (dev, test_ptr, got, block_size,
675 com_err (program_name, errno,
676 _("during test data write, block %lu"),
677 currently_testing + written);
680 save_ptr += got * block_size;
681 test_ptr += got * block_size;
682 currently_testing += got;
684 bb_count += bb_output(currently_testing++);
688 * If there's room for more blocks to be tested this
689 * around, and we're not done yet testing the disk, go
690 * back and get some more blocks.
692 if ((buf_used != blocks_at_once) &&
693 (currently_testing < last_block))
697 save_currently_testing = currently_testing;
700 * for each contiguous block that we read into the
701 * buffer (and wrote test data into afterwards), read
702 * it back (looping if necessary, to get past newly
703 * discovered unreadable blocks, of which there should
704 * be none, but with a hard drive which is unreliable,
705 * it has happened), and compare with the test data
706 * that was written; output to the bad block list if
710 save_ptr = save_base;
711 test_ptr = test_base;
712 read_ptr = read_base;
717 if (used2 >= num_saved)
719 currently_testing = test_record[used2].block;
720 try = test_record[used2].num;
724 got = do_read (dev, read_ptr, try,
725 block_size, currently_testing);
727 /* test the comparison between all the
728 blocks successfully read */
729 for (i = 0; i < got; ++i)
730 if (memcmp (test_ptr+i*block_size,
731 read_ptr+i*block_size, block_size))
732 bb_count += bb_output(currently_testing + i);
734 bb_count += bb_output(currently_testing + got);
738 /* write back original data */
739 do_write (dev, save_ptr, got,
740 block_size, currently_testing);
741 save_ptr += got * block_size;
743 currently_testing += got;
744 test_ptr += got * block_size;
745 read_ptr += got * block_size;
749 /* empty the buffer so it can be reused */
752 save_ptr = save_base;
753 test_ptr = test_base;
754 currently_testing = save_currently_testing;
758 if (s_flag || v_flag > 1)
759 fputs(_(done_string), stderr);
763 uncapture_terminate();
768 ext2fs_badblocks_list_iterate_end(bb_iter);
773 static void check_mount(char *device_name)
778 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
780 com_err("ext2fs_check_if_mount", retval,
781 _("while determining whether %s is mounted."),
785 if (!(mount_flags & EXT2_MF_MOUNTED))
788 fprintf(stderr, _("%s is mounted; "), device_name);
790 fputs(_("badblocks forced anyway. "
791 "Hope /etc/mtab is incorrect.\n"), stderr);
794 fputs(_("it's not safe to run badblocks!\n"), stderr);
799 int main (int argc, char ** argv)
804 char * host_device_name = NULL;
805 char * input_file = NULL;
806 char * output_file = NULL;
808 int block_size = 1024;
809 unsigned long blocks_at_once = 64;
810 blk_t last_block, from_count;
812 int passes_clean = 0;
815 unsigned long pattern;
816 unsigned int (*test_func)(int, unsigned long,
822 setbuf(stdout, NULL);
823 setbuf(stderr, NULL);
825 setlocale(LC_MESSAGES, "");
826 setlocale(LC_CTYPE, "");
827 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
828 textdomain(NLS_CAT_NAME);
830 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
833 /* Determine the system page size if possible */
835 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
836 #define _SC_PAGESIZE _SC_PAGE_SIZE
839 sysval = sysconf(_SC_PAGESIZE);
841 sys_page_size = sysval;
842 #endif /* _SC_PAGESIZE */
843 #endif /* HAVE_SYSCONF */
846 program_name = *argv;
847 while ((c = getopt (argc, argv, "b:fi:o:svwnc:p:h:t:")) != EOF) {
850 block_size = strtoul (optarg, &tmp, 0);
851 if (*tmp || block_size > 4096) {
852 com_err (program_name, 0,
853 _("bad block size - %s"), optarg);
864 output_file = optarg;
885 blocks_at_once = strtoul (optarg, &tmp, 0);
887 com_err (program_name, 0,
888 "bad simultaneous block count - %s", optarg);
893 num_passes = strtoul (optarg, &tmp, 0);
895 com_err (program_name, 0,
896 "bad number of clean passes - %s", optarg);
901 host_device_name = optarg;
904 if (t_flag + 1 > t_max) {
905 unsigned long *t_patts_new;
907 t_patts_new = realloc(t_patts, t_max + T_INC);
909 com_err(program_name, ENOMEM,
910 _("can't allocate memory for "
911 "test_pattern - %s"),
915 t_patts = t_patts_new;
918 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
919 t_patts[t_flag++] = ~0;
921 pattern = strtoul(optarg, &tmp, 0);
923 com_err(program_name, 0,
924 _("invalid test_pattern: %s\n"),
928 if (pattern == (unsigned long) ~0)
930 t_patts[t_flag++] = pattern;
939 com_err(program_name, 0,
940 _("Maximum of one test_pattern may be specified "
941 "in read-only mode"));
944 if (t_patts && (t_patts[0] == (unsigned long) ~0)) {
945 com_err(program_name, 0,
946 _("Random test_pattern is not allowed "
947 "in read-only mode"));
951 if (optind > argc - 1)
953 device_name = argv[optind++];
954 if (optind > argc - 1) {
955 errcode = ext2fs_get_device_size(device_name,
958 if (errcode == EXT2_ET_UNIMPLEMENTED) {
959 com_err(program_name, 0,
960 _("Couldn't determine device size; you "
961 "must specify\nthe size manually\n"));
965 com_err(program_name, errcode,
966 _("while trying to determine device size"));
970 last_block = strtoul (argv[optind], &tmp, 0);
972 com_err (program_name, 0, _("bad blocks count - %s"),
978 if (optind <= argc-1) {
979 from_count = strtoul (argv[optind], &tmp, 0);
981 com_err (program_name, 0, _("bad starting block - %s"),
985 } else from_count = 0;
986 if (from_count >= last_block) {
987 com_err (program_name, 0, _("bad blocks range: %lu-%lu"),
988 (unsigned long) from_count, (unsigned long) last_block);
992 check_mount(device_name);
994 open_flag = w_flag ? O_RDWR : O_RDONLY;
995 dev = open (device_name, open_flag);
997 com_err (program_name, errno, _("while trying to open %s"),
1001 if (host_device_name) {
1002 host_dev = open (host_device_name, open_flag);
1003 if (host_dev == -1) {
1004 com_err (program_name, errno,
1005 _("while trying to open %s"),
1012 if (strcmp (input_file, "-") == 0)
1015 in = fopen (input_file, "r");
1018 com_err (program_name, errno,
1019 _("while trying to open %s"),
1025 if (output_file && strcmp (output_file, "-") != 0)
1027 out = fopen (output_file, "w");
1030 com_err (program_name, errno,
1031 _("while trying to open %s"),
1039 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1041 com_err (program_name, errcode,
1042 _("creating in-memory bad blocks list"));
1048 switch(fscanf (in, "%u\n", &next_bad)) {
1050 com_err (program_name, 0, "input file - bad format");
1055 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1057 com_err (program_name, errcode, _("adding to in-memory bad block list"));
1070 unsigned int bb_count;
1072 bb_count = test_func(dev, last_block, block_size,
1073 from_count, blocks_at_once);
1081 _("Pass completed, %u bad blocks found.\n"),
1084 } while (passes_clean < num_passes);