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 unsigned long currently_testing = 0;
85 static unsigned long num_blocks = 0;
86 static ext2_badblocks_list bb_list = NULL;
88 static blk_t next_bad = 0;
89 static ext2_badblocks_iterate bb_iter = NULL;
91 static void *allocate_buffer(size_t size)
95 #ifdef HAVE_POSIX_MEMALIGN
96 if (posix_memalign(&ret, sys_page_size, size) < 0)
100 ret = memalign(sys_page_size, size);
104 #endif /* HAVE_VALLOC */
105 #endif /* HAVE_MEMALIGN */
106 #endif /* HAVE_POSIX_MEMALIGN */
115 * This routine reports a new bad block. If the bad block has already
116 * been seen before, then it returns 0; otherwise it returns 1.
118 static int bb_output (unsigned long bad)
122 if (ext2fs_badblocks_list_test(bb_list, bad))
125 fprintf(out, "%lu\n", bad);
128 errcode = ext2fs_badblocks_list_add (bb_list, bad);
130 com_err (program_name, errcode, "adding to in-memory bad block list");
135 increment the iteration through the bb_list if
136 an element was just added before the current iteration
137 position. This should not cause next_bad to change. */
138 if (bb_iter && bad < next_bad)
139 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
143 static void print_status(void)
145 fprintf(stderr, "%15ld/%15ld", currently_testing, num_blocks);
146 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);
150 static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
152 signal (SIGALRM, alarm_intr);
159 static void *terminate_addr = NULL;
161 static void terminate_intr(int signo EXT2FS_ATTR((unused)))
164 longjmp(terminate_addr,1);
168 static void capture_terminate(jmp_buf term_addr)
170 terminate_addr = term_addr;
171 signal (SIGHUP, terminate_intr);
172 signal (SIGINT, terminate_intr);
173 signal (SIGPIPE, terminate_intr);
174 signal (SIGTERM, terminate_intr);
175 signal (SIGUSR1, terminate_intr);
176 signal (SIGUSR2, terminate_intr);
179 static void uncapture_terminate(void)
181 terminate_addr = NULL;
182 signal (SIGHUP, SIG_DFL);
183 signal (SIGINT, SIG_DFL);
184 signal (SIGPIPE, SIG_DFL);
185 signal (SIGTERM, SIG_DFL);
186 signal (SIGUSR1, SIG_DFL);
187 signal (SIGUSR2, SIG_DFL);
190 static void set_o_direct(int dev, unsigned char *buffer, size_t size,
191 unsigned long current_block)
194 int new_flag = O_DIRECT;
197 if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
198 ((size & (sys_page_size - 1)) != 0) ||
199 ((current_block & ((sys_page_size >> 9)-1)) != 0))
202 if (new_flag != current_O_DIRECT) {
203 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
204 flag = fcntl(dev, F_GETFL);
206 flag = (flag & ~O_DIRECT) | new_flag;
207 fcntl(dev, F_SETFL, flag);
209 current_O_DIRECT = new_flag;
215 static void pattern_fill(unsigned char *buffer, unsigned long pattern,
219 unsigned char bpattern[sizeof(pattern)], *ptr;
221 if (pattern == (unsigned long) ~0) {
222 for (ptr = buffer; ptr < buffer + n; ptr++) {
223 (*ptr) = random() % (1 << (8 * sizeof(char)));
226 fputs(_("Testing with random pattern: "), stderr);
229 for (i = 0; i < sizeof(bpattern); i++) {
232 bpattern[i] = pattern & 0xFF;
233 pattern = pattern >> 8;
236 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
243 if (s_flag | v_flag) {
244 fputs(_("Testing with pattern 0x"), stderr);
245 for (i = 0; i <= nb; i++)
246 fprintf(stderr, "%02x", buffer[i]);
253 * Perform a read of a sequence of blocks; return the number of blocks
254 * successfully sequentially read.
256 static long do_read (int dev, unsigned char * buffer, int try, int block_size,
257 unsigned long current_block)
261 set_o_direct(dev, buffer, try * block_size, current_block);
266 /* Seek to the correct loc. */
267 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
268 SEEK_SET) != (ext2_loff_t) current_block * block_size)
269 com_err (program_name, errno, _("during seek"));
272 got = read (dev, buffer, try * block_size);
276 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
282 * Perform a write of a sequence of blocks; return the number of blocks
283 * successfully sequentially written.
285 static long do_write (int dev, unsigned char * buffer, int try, int block_size,
286 unsigned long current_block)
290 set_o_direct(dev, buffer, try * block_size, current_block);
295 /* Seek to the correct loc. */
296 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
297 SEEK_SET) != (ext2_loff_t) current_block * block_size)
298 com_err (program_name, errno, _("during seek"));
301 got = write (dev, buffer, try * block_size);
305 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
312 static void flush_bufs(void)
316 retval = ext2fs_sync_device(host_dev, 1);
318 com_err(program_name, retval, _("during ext2fs_sync_device"));
321 static unsigned int test_ro (int dev, unsigned long last_block,
322 int block_size, unsigned long from_count,
323 unsigned long blocks_at_once)
325 unsigned char * blkbuf;
328 unsigned int bb_count = 0;
331 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
333 com_err (program_name, errcode,
334 _("while beginning bad block list iteration"));
338 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
339 } while (next_bad && next_bad < from_count);
342 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
344 blkbuf = allocate_buffer(blocks_at_once * block_size);
348 com_err (program_name, ENOMEM, _("while allocating buffers"));
352 fprintf (stderr, _("Checking blocks %lu to %lu\n"), from_count,
356 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
357 pattern_fill(blkbuf + blocks_at_once * block_size,
358 t_patts[0], block_size);
361 try = blocks_at_once;
362 currently_testing = from_count;
363 num_blocks = last_block;
364 if (!t_flag && (s_flag || v_flag)) {
365 fputs(_("Checking for bad blocks (read-only test): "), stderr);
369 while (currently_testing < last_block)
372 if (currently_testing == next_bad) {
373 /* fprintf (out, "%lu\n", nextbad); */
374 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
378 else if (currently_testing + try > next_bad)
379 try = next_bad - currently_testing;
381 if (currently_testing + try > last_block)
382 try = last_block - currently_testing;
383 got = do_read (dev, blkbuf, try, block_size, currently_testing);
385 /* test the comparison between all the
386 blocks successfully read */
388 for (i = 0; i < got; ++i)
389 if (memcmp (blkbuf+i*block_size,
390 blkbuf+blocks_at_once*block_size,
392 bb_count += bb_output(currently_testing + i);
394 currently_testing += got;
396 try = blocks_at_once;
397 /* recover page-aligned offset for O_DIRECT */
398 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
399 && (currently_testing % (sys_page_size >> 9)!= 0))
400 try -= (sys_page_size >> 9)
402 % (sys_page_size >> 9));
408 bb_count += bb_output(currently_testing++);
413 if (s_flag || v_flag)
414 fputs(_(done_string), stderr);
419 ext2fs_badblocks_list_iterate_end(bb_iter);
424 static unsigned int test_rw (int dev, unsigned long last_block,
425 int block_size, unsigned long from_count,
426 unsigned long blocks_at_once)
428 unsigned char *buffer, *read_buffer;
429 const unsigned long patterns[] = {0xaa, 0x55, 0xff, 0x00};
430 const unsigned long *pattern;
431 int i, try, got, nr_pattern, pat_idx;
432 unsigned int bb_count = 0;
434 buffer = allocate_buffer(2 * blocks_at_once * block_size);
435 read_buffer = buffer + blocks_at_once * block_size;
438 com_err (program_name, ENOMEM, _("while allocating buffers"));
445 fputs(_("Checking for bad blocks in read-write mode\n"),
447 fprintf(stderr, _("From block %lu to %lu\n"),
448 from_count, last_block);
455 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
457 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
458 pattern_fill(buffer, pattern[pat_idx],
459 blocks_at_once * block_size);
460 num_blocks = last_block;
461 currently_testing = from_count;
462 if (s_flag && v_flag <= 1)
465 try = blocks_at_once;
466 while (currently_testing < last_block) {
467 if (currently_testing + try > last_block)
468 try = last_block - currently_testing;
469 got = do_write(dev, buffer, try, block_size,
474 currently_testing += got;
476 try = blocks_at_once;
477 /* recover page-aligned offset for O_DIRECT */
478 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
479 && (currently_testing %
480 (sys_page_size >> 9)!= 0))
481 try -= (sys_page_size >> 9)
483 % (sys_page_size >> 9));
488 bb_count += bb_output(currently_testing++);
495 fputs(_(done_string), stderr);
498 fputs(_("Reading and comparing: "), stderr);
499 num_blocks = last_block;
500 currently_testing = from_count;
501 if (s_flag && v_flag <= 1)
504 try = blocks_at_once;
505 while (currently_testing < last_block) {
506 if (currently_testing + try > last_block)
507 try = last_block - currently_testing;
508 got = do_read (dev, read_buffer, try, block_size,
511 bb_count += bb_output(currently_testing++);
514 for (i=0; i < got; i++) {
515 if (memcmp(read_buffer + i * block_size,
516 buffer + i * block_size,
518 bb_count += bb_output(currently_testing+i);
520 currently_testing += got;
521 /* recover page-aligned offset for O_DIRECT */
522 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
523 && (currently_testing % (sys_page_size >> 9)!= 0))
524 try = blocks_at_once - (sys_page_size >> 9)
526 % (sys_page_size >> 9));
528 try = blocks_at_once;
536 fputs(_(done_string), stderr);
539 uncapture_terminate();
544 struct saved_blk_record {
549 static unsigned int test_nd (int dev, unsigned long last_block,
550 int block_size, unsigned long from_count,
551 unsigned long blocks_at_once)
553 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
554 unsigned char *test_base, *save_base, *read_base;
556 const unsigned long patterns[] = { ~0 };
557 const unsigned long *pattern;
558 int nr_pattern, pat_idx;
559 long got, used2, written, save_currently_testing;
560 struct saved_blk_record *test_record;
561 /* This is static to prevent being clobbered by the longjmp */
562 static int num_saved;
563 jmp_buf terminate_env;
565 unsigned long buf_used;
566 static unsigned int bb_count;
569 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
571 com_err (program_name, errcode,
572 _("while beginning bad block list iteration"));
576 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
577 } while (next_bad && next_bad < from_count);
579 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
580 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
581 if (!blkbuf || !test_record) {
582 com_err(program_name, ENOMEM, _("while allocating buffers"));
587 test_base = blkbuf + (blocks_at_once * block_size);
588 read_base = blkbuf + (2 * blocks_at_once * block_size);
594 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
595 fprintf (stderr, _("From block %lu to %lu\n"), from_count, last_block);
597 if (s_flag || v_flag > 1) {
598 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
600 if (setjmp(terminate_env)) {
602 * Abnormal termination by a signal is handled here.
604 signal (SIGALRM, SIG_IGN);
605 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
607 save_ptr = save_base;
608 for (i=0; i < num_saved; i++) {
609 do_write(dev, save_ptr, test_record[i].num,
610 block_size, test_record[i].block);
611 save_ptr += test_record[i].num * block_size;
617 /* set up abend handler */
618 capture_terminate(terminate_env);
625 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
627 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
628 pattern_fill(test_base, pattern[pat_idx],
629 blocks_at_once * block_size);
633 save_ptr = save_base;
634 test_ptr = test_base;
635 currently_testing = from_count;
636 num_blocks = last_block;
637 if (s_flag && v_flag <= 1)
640 while (currently_testing < last_block) {
641 got = try = blocks_at_once - buf_used;
643 if (currently_testing == next_bad) {
644 /* fprintf (out, "%lu\n", nextbad); */
645 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
649 else if (currently_testing + try > next_bad)
650 try = next_bad - currently_testing;
652 if (currently_testing + try > last_block)
653 try = last_block - currently_testing;
654 got = do_read (dev, save_ptr, try, block_size,
657 /* First block must have been bad. */
658 bb_count += bb_output(currently_testing++);
663 * Note the fact that we've saved this much data
664 * *before* we overwrite it with test data
666 test_record[num_saved].block = currently_testing;
667 test_record[num_saved].num = got;
670 /* Write the test data */
671 written = do_write (dev, test_ptr, got, block_size,
674 com_err (program_name, errno,
675 _("during test data write, block %lu"),
676 currently_testing + written);
679 save_ptr += got * block_size;
680 test_ptr += got * block_size;
681 currently_testing += got;
683 bb_count += bb_output(currently_testing++);
687 * If there's room for more blocks to be tested this
688 * around, and we're not done yet testing the disk, go
689 * back and get some more blocks.
691 if ((buf_used != blocks_at_once) &&
692 (currently_testing < last_block))
696 save_currently_testing = currently_testing;
699 * for each contiguous block that we read into the
700 * buffer (and wrote test data into afterwards), read
701 * it back (looping if necessary, to get past newly
702 * discovered unreadable blocks, of which there should
703 * be none, but with a hard drive which is unreliable,
704 * it has happened), and compare with the test data
705 * that was written; output to the bad block list if
709 save_ptr = save_base;
710 test_ptr = test_base;
711 read_ptr = read_base;
716 if (used2 >= num_saved)
718 currently_testing = test_record[used2].block;
719 try = test_record[used2].num;
723 got = do_read (dev, read_ptr, try,
724 block_size, currently_testing);
726 /* test the comparison between all the
727 blocks successfully read */
728 for (i = 0; i < got; ++i)
729 if (memcmp (test_ptr+i*block_size,
730 read_ptr+i*block_size, block_size))
731 bb_count += bb_output(currently_testing + i);
733 bb_count += bb_output(currently_testing + got);
737 /* write back original data */
738 do_write (dev, save_ptr, got,
739 block_size, currently_testing);
740 save_ptr += got * block_size;
742 currently_testing += got;
743 test_ptr += got * block_size;
744 read_ptr += got * block_size;
748 /* empty the buffer so it can be reused */
751 save_ptr = save_base;
752 test_ptr = test_base;
753 currently_testing = save_currently_testing;
757 if (s_flag || v_flag > 1)
758 fputs(_(done_string), stderr);
762 uncapture_terminate();
767 ext2fs_badblocks_list_iterate_end(bb_iter);
772 static void check_mount(char *device_name)
777 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
779 com_err("ext2fs_check_if_mount", retval,
780 _("while determining whether %s is mounted."),
784 if (mount_flags & EXT2_MF_MOUNTED) {
785 fprintf(stderr, _("%s is mounted; "), device_name);
787 fputs(_("badblocks forced anyway. "
788 "Hope /etc/mtab is incorrect.\n"), stderr);
792 fputs(_("it's not safe to run badblocks!\n"), stderr);
796 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
797 fprintf(stderr, _("%s is apparently in use by the system; "),
800 fputs(_("badblocks forced anyway.\n"), stderr);
802 goto abort_badblocks;
808 int main (int argc, char ** argv)
813 char * host_device_name = NULL;
814 char * input_file = NULL;
815 char * output_file = NULL;
817 int block_size = 1024;
818 unsigned long blocks_at_once = 64;
819 blk_t last_block, from_count;
821 int passes_clean = 0;
824 unsigned long pattern;
825 unsigned int (*test_func)(int, unsigned long,
831 setbuf(stdout, NULL);
832 setbuf(stderr, NULL);
834 setlocale(LC_MESSAGES, "");
835 setlocale(LC_CTYPE, "");
836 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
837 textdomain(NLS_CAT_NAME);
839 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
842 /* Determine the system page size if possible */
844 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
845 #define _SC_PAGESIZE _SC_PAGE_SIZE
848 sysval = sysconf(_SC_PAGESIZE);
850 sys_page_size = sysval;
851 #endif /* _SC_PAGESIZE */
852 #endif /* HAVE_SYSCONF */
855 program_name = *argv;
856 while ((c = getopt (argc, argv, "b:fi:o:svwnc:p:h:t:X")) != EOF) {
859 block_size = strtoul (optarg, &tmp, 0);
860 if (*tmp || block_size > 4096) {
861 com_err (program_name, 0,
862 _("bad block size - %s"), optarg);
873 output_file = optarg;
894 blocks_at_once = strtoul (optarg, &tmp, 0);
896 com_err (program_name, 0,
897 "bad simultaneous block count - %s", optarg);
902 num_passes = strtoul (optarg, &tmp, 0);
904 com_err (program_name, 0,
905 "bad number of clean passes - %s", optarg);
910 host_device_name = optarg;
913 if (t_flag + 1 > t_max) {
914 unsigned long *t_patts_new;
916 t_patts_new = realloc(t_patts, t_max + T_INC);
918 com_err(program_name, ENOMEM,
919 _("can't allocate memory for "
920 "test_pattern - %s"),
924 t_patts = t_patts_new;
927 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
928 t_patts[t_flag++] = ~0;
930 pattern = strtoul(optarg, &tmp, 0);
932 com_err(program_name, 0,
933 _("invalid test_pattern: %s\n"),
937 if (pattern == (unsigned long) ~0)
939 t_patts[t_flag++] = pattern;
951 com_err(program_name, 0,
952 _("Maximum of one test_pattern may be specified "
953 "in read-only mode"));
956 if (t_patts && (t_patts[0] == (unsigned long) ~0)) {
957 com_err(program_name, 0,
958 _("Random test_pattern is not allowed "
959 "in read-only mode"));
963 if (optind > argc - 1)
965 device_name = argv[optind++];
966 if (optind > argc - 1) {
967 errcode = ext2fs_get_device_size(device_name,
970 if (errcode == EXT2_ET_UNIMPLEMENTED) {
971 com_err(program_name, 0,
972 _("Couldn't determine device size; you "
973 "must specify\nthe size manually\n"));
977 com_err(program_name, errcode,
978 _("while trying to determine device size"));
982 last_block = strtoul (argv[optind], &tmp, 0);
984 com_err (program_name, 0, _("invalid blocks count - %s"),
990 if (optind <= argc-1) {
991 from_count = strtoul (argv[optind], &tmp, 0);
993 com_err (program_name, 0, _("invalid starting block - %s"),
997 } else from_count = 0;
998 if (from_count >= last_block) {
999 com_err (program_name, 0, _("invalid blocks range: %lu-%lu"),
1000 (unsigned long) from_count, (unsigned long) last_block);
1004 check_mount(device_name);
1006 open_flag = w_flag ? O_RDWR : O_RDONLY;
1007 dev = open (device_name, open_flag);
1009 com_err (program_name, errno, _("while trying to open %s"),
1013 if (host_device_name) {
1014 host_dev = open (host_device_name, open_flag);
1015 if (host_dev == -1) {
1016 com_err (program_name, errno,
1017 _("while trying to open %s"),
1024 if (strcmp (input_file, "-") == 0)
1027 in = fopen (input_file, "r");
1030 com_err (program_name, errno,
1031 _("while trying to open %s"),
1037 if (output_file && strcmp (output_file, "-") != 0)
1039 out = fopen (output_file, "w");
1042 com_err (program_name, errno,
1043 _("while trying to open %s"),
1051 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1053 com_err (program_name, errcode,
1054 _("while creating in-memory bad blocks list"));
1060 switch(fscanf (in, "%u\n", &next_bad)) {
1062 com_err (program_name, 0, "input file - bad format");
1067 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1069 com_err (program_name, errcode, _("while adding to in-memory bad block list"));
1082 unsigned int bb_count;
1084 bb_count = test_func(dev, last_block, block_size,
1085 from_count, blocks_at_once);
1093 _("Pass completed, %u bad blocks found.\n"),
1096 } while (passes_clean < num_passes);