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 */
71 static int exclusive_ok = 0;
75 int sys_page_size = 4096;
77 static void usage(void)
79 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"),
84 static void exclusive_usage(void)
87 _("The -n and -w options are mutually exclusive.\n\n"));
91 static unsigned long currently_testing = 0;
92 static unsigned long num_blocks = 0;
93 static ext2_badblocks_list bb_list = NULL;
95 static blk_t next_bad = 0;
96 static ext2_badblocks_iterate bb_iter = NULL;
98 static void *allocate_buffer(size_t size)
102 #ifdef HAVE_POSIX_MEMALIGN
103 if (posix_memalign(&ret, sys_page_size, size) < 0)
107 ret = memalign(sys_page_size, size);
111 #endif /* HAVE_VALLOC */
112 #endif /* HAVE_MEMALIGN */
113 #endif /* HAVE_POSIX_MEMALIGN */
122 * This routine reports a new bad block. If the bad block has already
123 * been seen before, then it returns 0; otherwise it returns 1.
125 static int bb_output (unsigned long bad)
129 if (ext2fs_badblocks_list_test(bb_list, bad))
132 fprintf(out, "%lu\n", bad);
135 errcode = ext2fs_badblocks_list_add (bb_list, bad);
137 com_err (program_name, errcode, "adding to in-memory bad block list");
142 increment the iteration through the bb_list if
143 an element was just added before the current iteration
144 position. This should not cause next_bad to change. */
145 if (bb_iter && bad < next_bad)
146 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
150 static void print_status(void)
152 fprintf(stderr, "%15ld/%15ld", currently_testing, num_blocks);
153 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);
157 static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
159 signal (SIGALRM, alarm_intr);
166 static void *terminate_addr = NULL;
168 static void terminate_intr(int signo EXT2FS_ATTR((unused)))
171 longjmp(terminate_addr,1);
175 static void capture_terminate(jmp_buf term_addr)
177 terminate_addr = term_addr;
178 signal (SIGHUP, terminate_intr);
179 signal (SIGINT, terminate_intr);
180 signal (SIGPIPE, terminate_intr);
181 signal (SIGTERM, terminate_intr);
182 signal (SIGUSR1, terminate_intr);
183 signal (SIGUSR2, terminate_intr);
186 static void uncapture_terminate(void)
188 terminate_addr = NULL;
189 signal (SIGHUP, SIG_DFL);
190 signal (SIGINT, SIG_DFL);
191 signal (SIGPIPE, SIG_DFL);
192 signal (SIGTERM, SIG_DFL);
193 signal (SIGUSR1, SIG_DFL);
194 signal (SIGUSR2, SIG_DFL);
197 static void set_o_direct(int dev, unsigned char *buffer, size_t size,
198 unsigned long current_block)
201 int new_flag = O_DIRECT;
204 if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
205 ((size & (sys_page_size - 1)) != 0) ||
206 ((current_block & ((sys_page_size >> 9)-1)) != 0))
209 if (new_flag != current_O_DIRECT) {
210 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
211 flag = fcntl(dev, F_GETFL);
213 flag = (flag & ~O_DIRECT) | new_flag;
214 fcntl(dev, F_SETFL, flag);
216 current_O_DIRECT = new_flag;
222 static void pattern_fill(unsigned char *buffer, unsigned long pattern,
226 unsigned char bpattern[sizeof(pattern)], *ptr;
228 if (pattern == (unsigned long) ~0) {
229 for (ptr = buffer; ptr < buffer + n; ptr++) {
230 (*ptr) = random() % (1 << (8 * sizeof(char)));
233 fputs(_("Testing with random pattern: "), stderr);
236 for (i = 0; i < sizeof(bpattern); i++) {
239 bpattern[i] = pattern & 0xFF;
240 pattern = pattern >> 8;
243 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
250 if (s_flag | v_flag) {
251 fputs(_("Testing with pattern 0x"), stderr);
252 for (i = 0; i <= nb; i++)
253 fprintf(stderr, "%02x", buffer[i]);
260 * Perform a read of a sequence of blocks; return the number of blocks
261 * successfully sequentially read.
263 static long do_read (int dev, unsigned char * buffer, int try, int block_size,
264 unsigned long current_block)
268 set_o_direct(dev, buffer, try * block_size, current_block);
273 /* Seek to the correct loc. */
274 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
275 SEEK_SET) != (ext2_loff_t) current_block * block_size)
276 com_err (program_name, errno, _("during seek"));
279 got = read (dev, buffer, try * block_size);
283 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
289 * Perform a write of a sequence of blocks; return the number of blocks
290 * successfully sequentially written.
292 static long do_write (int dev, unsigned char * buffer, int try, int block_size,
293 unsigned long current_block)
297 set_o_direct(dev, buffer, try * block_size, current_block);
302 /* Seek to the correct loc. */
303 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
304 SEEK_SET) != (ext2_loff_t) current_block * block_size)
305 com_err (program_name, errno, _("during seek"));
308 got = write (dev, buffer, try * block_size);
312 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
319 static void flush_bufs(void)
323 retval = ext2fs_sync_device(host_dev, 1);
325 com_err(program_name, retval, _("during ext2fs_sync_device"));
328 static unsigned int test_ro (int dev, unsigned long last_block,
329 int block_size, unsigned long from_count,
330 unsigned long blocks_at_once)
332 unsigned char * blkbuf;
335 unsigned int bb_count = 0;
338 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
340 com_err (program_name, errcode,
341 _("while beginning bad block list iteration"));
345 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
346 } while (next_bad && next_bad < from_count);
349 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
351 blkbuf = allocate_buffer(blocks_at_once * block_size);
355 com_err (program_name, ENOMEM, _("while allocating buffers"));
359 fprintf (stderr, _("Checking blocks %lu to %lu\n"), from_count,
363 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
364 pattern_fill(blkbuf + blocks_at_once * block_size,
365 t_patts[0], block_size);
368 try = blocks_at_once;
369 currently_testing = from_count;
370 num_blocks = last_block - 1;
371 if (!t_flag && (s_flag || v_flag)) {
372 fputs(_("Checking for bad blocks (read-only test): "), stderr);
376 while (currently_testing < last_block)
379 if (currently_testing == next_bad) {
380 /* fprintf (out, "%lu\n", nextbad); */
381 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
385 else if (currently_testing + try > next_bad)
386 try = next_bad - currently_testing;
388 if (currently_testing + try > last_block)
389 try = last_block - currently_testing;
390 got = do_read (dev, blkbuf, try, block_size, currently_testing);
392 /* test the comparison between all the
393 blocks successfully read */
395 for (i = 0; i < got; ++i)
396 if (memcmp (blkbuf+i*block_size,
397 blkbuf+blocks_at_once*block_size,
399 bb_count += bb_output(currently_testing + i);
401 currently_testing += got;
403 try = blocks_at_once;
404 /* recover page-aligned offset for O_DIRECT */
405 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
406 && (currently_testing % (sys_page_size >> 9)!= 0))
407 try -= (sys_page_size >> 9)
409 % (sys_page_size >> 9));
415 bb_count += bb_output(currently_testing++);
420 if (s_flag || v_flag)
421 fputs(_(done_string), stderr);
426 ext2fs_badblocks_list_iterate_end(bb_iter);
431 static unsigned int test_rw (int dev, unsigned long last_block,
432 int block_size, unsigned long from_count,
433 unsigned long blocks_at_once)
435 unsigned char *buffer, *read_buffer;
436 const unsigned long patterns[] = {0xaa, 0x55, 0xff, 0x00};
437 const unsigned long *pattern;
438 int i, try, got, nr_pattern, pat_idx;
439 unsigned int bb_count = 0;
441 buffer = allocate_buffer(2 * blocks_at_once * block_size);
442 read_buffer = buffer + blocks_at_once * block_size;
445 com_err (program_name, ENOMEM, _("while allocating buffers"));
452 fputs(_("Checking for bad blocks in read-write mode\n"),
454 fprintf(stderr, _("From block %lu to %lu\n"),
455 from_count, last_block);
462 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
464 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
465 pattern_fill(buffer, pattern[pat_idx],
466 blocks_at_once * block_size);
467 num_blocks = last_block - 1;
468 currently_testing = from_count;
469 if (s_flag && v_flag <= 1)
472 try = blocks_at_once;
473 while (currently_testing < last_block) {
474 if (currently_testing + try > last_block)
475 try = last_block - currently_testing;
476 got = do_write(dev, buffer, try, block_size,
481 currently_testing += got;
483 try = blocks_at_once;
484 /* recover page-aligned offset for O_DIRECT */
485 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
486 && (currently_testing %
487 (sys_page_size >> 9)!= 0))
488 try -= (sys_page_size >> 9)
490 % (sys_page_size >> 9));
495 bb_count += bb_output(currently_testing++);
502 fputs(_(done_string), stderr);
505 fputs(_("Reading and comparing: "), stderr);
506 num_blocks = last_block;
507 currently_testing = from_count;
508 if (s_flag && v_flag <= 1)
511 try = blocks_at_once;
512 while (currently_testing < last_block) {
513 if (currently_testing + try > last_block)
514 try = last_block - currently_testing;
515 got = do_read (dev, read_buffer, try, block_size,
518 bb_count += bb_output(currently_testing++);
521 for (i=0; i < got; i++) {
522 if (memcmp(read_buffer + i * block_size,
523 buffer + i * block_size,
525 bb_count += bb_output(currently_testing+i);
527 currently_testing += got;
528 /* recover page-aligned offset for O_DIRECT */
529 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
530 && (currently_testing % (sys_page_size >> 9)!= 0))
531 try = blocks_at_once - (sys_page_size >> 9)
533 % (sys_page_size >> 9));
535 try = blocks_at_once;
543 fputs(_(done_string), stderr);
546 uncapture_terminate();
551 struct saved_blk_record {
556 static unsigned int test_nd (int dev, unsigned long last_block,
557 int block_size, unsigned long from_count,
558 unsigned long blocks_at_once)
560 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
561 unsigned char *test_base, *save_base, *read_base;
563 const unsigned long patterns[] = { ~0 };
564 const unsigned long *pattern;
565 int nr_pattern, pat_idx;
566 long got, used2, written, save_currently_testing;
567 struct saved_blk_record *test_record;
568 /* This is static to prevent being clobbered by the longjmp */
569 static int num_saved;
570 jmp_buf terminate_env;
572 unsigned long buf_used;
573 static unsigned int bb_count;
576 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
578 com_err (program_name, errcode,
579 _("while beginning bad block list iteration"));
583 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
584 } while (next_bad && next_bad < from_count);
586 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
587 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
588 if (!blkbuf || !test_record) {
589 com_err(program_name, ENOMEM, _("while allocating buffers"));
594 test_base = blkbuf + (blocks_at_once * block_size);
595 read_base = blkbuf + (2 * blocks_at_once * block_size);
601 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
602 fprintf (stderr, _("From block %lu to %lu\n"), from_count, last_block);
604 if (s_flag || v_flag > 1) {
605 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
607 if (setjmp(terminate_env)) {
609 * Abnormal termination by a signal is handled here.
611 signal (SIGALRM, SIG_IGN);
612 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
614 save_ptr = save_base;
615 for (i=0; i < num_saved; i++) {
616 do_write(dev, save_ptr, test_record[i].num,
617 block_size, test_record[i].block);
618 save_ptr += test_record[i].num * block_size;
624 /* set up abend handler */
625 capture_terminate(terminate_env);
632 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
634 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
635 pattern_fill(test_base, pattern[pat_idx],
636 blocks_at_once * block_size);
640 save_ptr = save_base;
641 test_ptr = test_base;
642 currently_testing = from_count;
643 num_blocks = last_block - 1;
644 if (s_flag && v_flag <= 1)
647 while (currently_testing < last_block) {
648 got = try = blocks_at_once - buf_used;
650 if (currently_testing == next_bad) {
651 /* fprintf (out, "%lu\n", nextbad); */
652 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
656 else if (currently_testing + try > next_bad)
657 try = next_bad - currently_testing;
659 if (currently_testing + try > last_block)
660 try = last_block - currently_testing;
661 got = do_read (dev, save_ptr, try, block_size,
664 /* First block must have been bad. */
665 bb_count += bb_output(currently_testing++);
670 * Note the fact that we've saved this much data
671 * *before* we overwrite it with test data
673 test_record[num_saved].block = currently_testing;
674 test_record[num_saved].num = got;
677 /* Write the test data */
678 written = do_write (dev, test_ptr, got, block_size,
681 com_err (program_name, errno,
682 _("during test data write, block %lu"),
683 currently_testing + written);
686 save_ptr += got * block_size;
687 test_ptr += got * block_size;
688 currently_testing += got;
690 bb_count += bb_output(currently_testing++);
694 * If there's room for more blocks to be tested this
695 * around, and we're not done yet testing the disk, go
696 * back and get some more blocks.
698 if ((buf_used != blocks_at_once) &&
699 (currently_testing < last_block))
703 save_currently_testing = currently_testing;
706 * for each contiguous block that we read into the
707 * buffer (and wrote test data into afterwards), read
708 * it back (looping if necessary, to get past newly
709 * discovered unreadable blocks, of which there should
710 * be none, but with a hard drive which is unreliable,
711 * it has happened), and compare with the test data
712 * that was written; output to the bad block list if
716 save_ptr = save_base;
717 test_ptr = test_base;
718 read_ptr = read_base;
723 if (used2 >= num_saved)
725 currently_testing = test_record[used2].block;
726 try = test_record[used2].num;
730 got = do_read (dev, read_ptr, try,
731 block_size, currently_testing);
733 /* test the comparison between all the
734 blocks successfully read */
735 for (i = 0; i < got; ++i)
736 if (memcmp (test_ptr+i*block_size,
737 read_ptr+i*block_size, block_size))
738 bb_count += bb_output(currently_testing + i);
740 bb_count += bb_output(currently_testing + got);
744 /* write back original data */
745 do_write (dev, save_ptr, got,
746 block_size, currently_testing);
747 save_ptr += got * block_size;
749 currently_testing += got;
750 test_ptr += got * block_size;
751 read_ptr += got * block_size;
755 /* empty the buffer so it can be reused */
758 save_ptr = save_base;
759 test_ptr = test_base;
760 currently_testing = save_currently_testing;
764 if (s_flag || v_flag > 1)
765 fputs(_(done_string), stderr);
769 uncapture_terminate();
774 ext2fs_badblocks_list_iterate_end(bb_iter);
779 static void check_mount(char *device_name)
784 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
786 com_err("ext2fs_check_if_mount", retval,
787 _("while determining whether %s is mounted."),
791 if (mount_flags & EXT2_MF_MOUNTED) {
792 fprintf(stderr, _("%s is mounted; "), device_name);
794 fputs(_("badblocks forced anyway. "
795 "Hope /etc/mtab is incorrect.\n"), stderr);
799 fputs(_("it's not safe to run badblocks!\n"), stderr);
803 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
804 fprintf(stderr, _("%s is apparently in use by the system; "),
807 fputs(_("badblocks forced anyway.\n"), stderr);
809 goto abort_badblocks;
815 int main (int argc, char ** argv)
820 char * host_device_name = NULL;
821 char * input_file = NULL;
822 char * output_file = NULL;
824 int block_size = 1024;
825 unsigned long blocks_at_once = 64;
826 blk_t last_block, from_count;
828 int passes_clean = 0;
831 unsigned long pattern;
832 unsigned int (*test_func)(int, unsigned long,
838 setbuf(stdout, NULL);
839 setbuf(stderr, NULL);
841 setlocale(LC_MESSAGES, "");
842 setlocale(LC_CTYPE, "");
843 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
844 textdomain(NLS_CAT_NAME);
846 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
849 /* Determine the system page size if possible */
851 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
852 #define _SC_PAGESIZE _SC_PAGE_SIZE
855 sysval = sysconf(_SC_PAGESIZE);
857 sys_page_size = sysval;
858 #endif /* _SC_PAGESIZE */
859 #endif /* HAVE_SYSCONF */
862 program_name = *argv;
863 while ((c = getopt (argc, argv, "b:fi:o:svwnc:p:h:t:X")) != EOF) {
866 block_size = strtoul (optarg, &tmp, 0);
867 if (*tmp || block_size > 4096) {
868 com_err (program_name, 0,
869 _("bad block size - %s"), optarg);
880 output_file = optarg;
901 blocks_at_once = strtoul (optarg, &tmp, 0);
903 com_err (program_name, 0,
904 "bad simultaneous block count - %s", optarg);
909 num_passes = strtoul (optarg, &tmp, 0);
911 com_err (program_name, 0,
912 "bad number of clean passes - %s", optarg);
917 host_device_name = optarg;
920 if (t_flag + 1 > t_max) {
921 unsigned long *t_patts_new;
923 t_patts_new = realloc(t_patts, t_max + T_INC);
925 com_err(program_name, ENOMEM,
926 _("can't allocate memory for "
927 "test_pattern - %s"),
931 t_patts = t_patts_new;
934 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
935 t_patts[t_flag++] = ~0;
937 pattern = strtoul(optarg, &tmp, 0);
939 com_err(program_name, 0,
940 _("invalid test_pattern: %s\n"),
944 if (pattern == (unsigned long) ~0)
946 t_patts[t_flag++] = pattern;
958 com_err(program_name, 0,
959 _("Maximum of one test_pattern may be specified "
960 "in read-only mode"));
963 if (t_patts && (t_patts[0] == (unsigned long) ~0)) {
964 com_err(program_name, 0,
965 _("Random test_pattern is not allowed "
966 "in read-only mode"));
970 if (optind > argc - 1)
972 device_name = argv[optind++];
973 if (optind > argc - 1) {
974 errcode = ext2fs_get_device_size(device_name,
977 if (errcode == EXT2_ET_UNIMPLEMENTED) {
978 com_err(program_name, 0,
979 _("Couldn't determine device size; you "
980 "must specify\nthe size manually\n"));
984 com_err(program_name, errcode,
985 _("while trying to determine device size"));
989 last_block = strtoul (argv[optind], &tmp, 0) + 1;
991 com_err (program_name, 0, _("invalid blocks count - %s"),
997 if (optind <= argc-1) {
998 from_count = strtoul (argv[optind], &tmp, 0);
1000 com_err (program_name, 0, _("invalid starting block - %s"),
1004 } else from_count = 0;
1005 if (from_count >= last_block) {
1006 com_err (program_name, 0, _("invalid blocks range: %lu-%lu"),
1007 (unsigned long) from_count, (unsigned long) last_block);
1011 check_mount(device_name);
1013 open_flag = w_flag ? O_RDWR : O_RDONLY;
1014 dev = open (device_name, open_flag);
1016 com_err (program_name, errno, _("while trying to open %s"),
1020 if (host_device_name) {
1021 host_dev = open (host_device_name, open_flag);
1022 if (host_dev == -1) {
1023 com_err (program_name, errno,
1024 _("while trying to open %s"),
1031 if (strcmp (input_file, "-") == 0)
1034 in = fopen (input_file, "r");
1037 com_err (program_name, errno,
1038 _("while trying to open %s"),
1044 if (output_file && strcmp (output_file, "-") != 0)
1046 out = fopen (output_file, "w");
1049 com_err (program_name, errno,
1050 _("while trying to open %s"),
1058 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1060 com_err (program_name, errcode,
1061 _("while creating in-memory bad blocks list"));
1067 switch(fscanf (in, "%u\n", &next_bad)) {
1069 com_err (program_name, 0, "input file - bad format");
1074 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1076 com_err (program_name, errcode, _("while adding to in-memory bad block list"));
1089 unsigned int bb_count;
1091 bb_count = test_func(dev, last_block, block_size,
1092 from_count, blocks_at_once);
1100 _("Pass completed, %u bad blocks found.\n"),
1103 } while (passes_clean < num_passes);