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 */
52 #include <sys/ioctl.h>
53 #include <sys/types.h>
56 #include "et/com_err.h"
57 #include "ext2fs/ext2_io.h"
58 #include "ext2fs/ext2_fs.h"
59 #include "ext2fs/ext2fs.h"
60 #include "nls-enable.h"
62 const char * program_name = "badblocks";
63 const char * done_string = N_("done \n");
65 static int v_flag = 0; /* verbose */
66 static int w_flag = 0; /* do r/w test: 0=no, 1=yes,
67 * 2=non-destructive */
68 static int s_flag = 0; /* show progress of test */
69 static int force = 0; /* force check of mounted device */
70 static int t_flag = 0; /* number of test patterns */
71 static int t_max = 0; /* allocated test patterns */
72 static unsigned int *t_patts = NULL; /* test patterns */
73 static int current_O_DIRECT = 0; /* Current status of O_DIRECT flag */
74 static int exclusive_ok = 0;
75 static unsigned int max_bb = 0; /* Abort test if more than this number of bad blocks has been encountered */
76 static unsigned int d_flag = 0; /* delay factor between reads */
80 unsigned int sys_page_size = 4096;
82 static void usage(void)
84 fprintf(stderr, _("Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwnf]\n [-c blocks_at_once] [-p num_passes] [-e max_bad_blocks] [-d delay_factor_between_reads] [-t test_pattern [-t test_pattern [...]]]\n device [last_block [start_block]]\n"),
89 static void exclusive_usage(void)
92 _("%s: The -n and -w options are mutually exclusive.\n\n"),
97 static blk_t currently_testing = 0;
98 static blk_t num_blocks = 0;
99 static ext2_badblocks_list bb_list = NULL;
101 static blk_t next_bad = 0;
102 static ext2_badblocks_iterate bb_iter = NULL;
104 static void *allocate_buffer(size_t size)
108 #ifdef HAVE_POSIX_MEMALIGN
109 if (posix_memalign(&ret, sys_page_size, size) < 0)
113 ret = memalign(sys_page_size, size);
117 #endif /* HAVE_VALLOC */
118 #endif /* HAVE_MEMALIGN */
119 #endif /* HAVE_POSIX_MEMALIGN */
128 * This routine reports a new bad block. If the bad block has already
129 * been seen before, then it returns 0; otherwise it returns 1.
131 static int bb_output (blk_t bad)
135 if (ext2fs_badblocks_list_test(bb_list, bad))
138 fprintf(out, "%lu\n", (unsigned long) bad);
141 errcode = ext2fs_badblocks_list_add (bb_list, bad);
143 com_err (program_name, errcode, "adding to in-memory bad block list");
148 increment the iteration through the bb_list if
149 an element was just added before the current iteration
150 position. This should not cause next_bad to change. */
151 if (bb_iter && bad < next_bad)
152 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
156 static void print_status(void)
158 fprintf(stderr, "%15lu/%15lu", (unsigned long) currently_testing,
159 (unsigned long) num_blocks);
160 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);
164 static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
166 signal (SIGALRM, alarm_intr);
173 static void *terminate_addr = NULL;
175 static void terminate_intr(int signo EXT2FS_ATTR((unused)))
178 longjmp(terminate_addr,1);
182 static void capture_terminate(jmp_buf term_addr)
184 terminate_addr = term_addr;
185 signal (SIGHUP, terminate_intr);
186 signal (SIGINT, terminate_intr);
187 signal (SIGPIPE, terminate_intr);
188 signal (SIGTERM, terminate_intr);
189 signal (SIGUSR1, terminate_intr);
190 signal (SIGUSR2, terminate_intr);
193 static void uncapture_terminate(void)
195 terminate_addr = NULL;
196 signal (SIGHUP, SIG_DFL);
197 signal (SIGINT, SIG_DFL);
198 signal (SIGPIPE, SIG_DFL);
199 signal (SIGTERM, SIG_DFL);
200 signal (SIGUSR1, SIG_DFL);
201 signal (SIGUSR2, SIG_DFL);
204 static void set_o_direct(int dev, unsigned char *buffer, size_t size,
208 int new_flag = O_DIRECT;
211 if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
212 ((size & (sys_page_size - 1)) != 0) ||
213 ((current_block & ((sys_page_size >> 9)-1)) != 0))
216 if (new_flag != current_O_DIRECT) {
217 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
218 flag = fcntl(dev, F_GETFL);
220 flag = (flag & ~O_DIRECT) | new_flag;
221 fcntl(dev, F_SETFL, flag);
223 current_O_DIRECT = new_flag;
229 static void pattern_fill(unsigned char *buffer, unsigned int pattern,
233 unsigned char bpattern[sizeof(pattern)], *ptr;
235 if (pattern == (unsigned int) ~0) {
236 for (ptr = buffer; ptr < buffer + n; ptr++) {
237 (*ptr) = random() % (1 << (8 * sizeof(char)));
240 fputs(_("Testing with random pattern: "), stderr);
243 for (i = 0; i < sizeof(bpattern); i++) {
246 bpattern[i] = pattern & 0xFF;
247 pattern = pattern >> 8;
250 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
257 if (s_flag | v_flag) {
258 fputs(_("Testing with pattern 0x"), stderr);
259 for (i = 0; i <= nb; i++)
260 fprintf(stderr, "%02x", buffer[i]);
267 * Perform a read of a sequence of blocks; return the number of blocks
268 * successfully sequentially read.
270 static int do_read (int dev, unsigned char * buffer, int try, int block_size,
274 struct timeval tv1, tv2;
275 #define NANOSEC (1000000000L)
276 #define MILISEC (1000L)
278 set_o_direct(dev, buffer, try * block_size, current_block);
283 /* Seek to the correct loc. */
284 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
285 SEEK_SET) != (ext2_loff_t) current_block * block_size)
286 com_err (program_name, errno, _("during seek"));
290 gettimeofday(&tv1, NULL);
291 got = read (dev, buffer, try * block_size);
293 gettimeofday(&tv2, NULL);
297 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
299 if (d_flag && got == try) {
301 ts.tv_sec = tv2.tv_sec - tv1.tv_sec;
302 ts.tv_nsec = (tv2.tv_usec - tv1.tv_usec) * MILISEC;
303 if (ts.tv_nsec < 0) {
304 ts.tv_nsec += NANOSEC;
307 /* increase/decrease the sleep time based on d_flag value */
308 ts.tv_sec = ts.tv_sec * d_flag / 100;
309 ts.tv_nsec = ts.tv_nsec * d_flag / 100;
310 if (ts.tv_nsec > NANOSEC) {
311 ts.tv_sec += ts.tv_nsec / NANOSEC;
312 ts.tv_nsec %= NANOSEC;
314 if (ts.tv_sec || ts.tv_nsec)
315 nanosleep(&ts, NULL);
321 * Perform a write of a sequence of blocks; return the number of blocks
322 * successfully sequentially written.
324 static int do_write(int dev, unsigned char * buffer, int try, int block_size,
325 unsigned long current_block)
329 set_o_direct(dev, buffer, try * block_size, current_block);
334 /* Seek to the correct loc. */
335 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
336 SEEK_SET) != (ext2_loff_t) current_block * block_size)
337 com_err (program_name, errno, _("during seek"));
340 got = write (dev, buffer, try * block_size);
344 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
351 static void flush_bufs(void)
355 retval = ext2fs_sync_device(host_dev, 1);
357 com_err(program_name, retval, _("during ext2fs_sync_device"));
360 static unsigned int test_ro (int dev, blk_t last_block,
361 int block_size, blk_t from_count,
362 unsigned int blocks_at_once)
364 unsigned char * blkbuf;
367 unsigned int bb_count = 0;
370 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
372 com_err (program_name, errcode,
373 _("while beginning bad block list iteration"));
377 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
378 } while (next_bad && next_bad < from_count);
381 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
383 blkbuf = allocate_buffer(blocks_at_once * block_size);
387 com_err (program_name, ENOMEM, _("while allocating buffers"));
391 fprintf (stderr, _("Checking blocks %lu to %lu\n"),
392 (unsigned long) from_count,
393 (unsigned long) last_block - 1);
396 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
397 pattern_fill(blkbuf + blocks_at_once * block_size,
398 t_patts[0], block_size);
401 try = blocks_at_once;
402 currently_testing = from_count;
403 num_blocks = last_block - 1;
404 if (!t_flag && (s_flag || v_flag)) {
405 fputs(_("Checking for bad blocks (read-only test): "), stderr);
409 while (currently_testing < last_block)
411 if (max_bb && bb_count >= max_bb) {
412 if (s_flag || v_flag) {
413 fputs(_("Too many bad blocks, aborting test\n"), stderr);
418 if (currently_testing == next_bad) {
419 /* fprintf (out, "%lu\n", nextbad); */
420 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
424 else if (currently_testing + try > next_bad)
425 try = next_bad - currently_testing;
427 if (currently_testing + try > last_block)
428 try = last_block - currently_testing;
429 got = do_read (dev, blkbuf, try, block_size, currently_testing);
431 /* test the comparison between all the
432 blocks successfully read */
434 for (i = 0; i < got; ++i)
435 if (memcmp (blkbuf+i*block_size,
436 blkbuf+blocks_at_once*block_size,
438 bb_count += bb_output(currently_testing + i);
440 currently_testing += got;
442 try = blocks_at_once;
443 /* recover page-aligned offset for O_DIRECT */
444 if ( (blocks_at_once >= sys_page_size >> 9)
445 && (currently_testing % (sys_page_size >> 9)!= 0))
446 try -= (sys_page_size >> 9)
448 % (sys_page_size >> 9));
454 bb_count += bb_output(currently_testing++);
459 if (s_flag || v_flag)
460 fputs(_(done_string), stderr);
465 ext2fs_badblocks_list_iterate_end(bb_iter);
470 static unsigned int test_rw (int dev, blk_t last_block,
471 int block_size, blk_t from_count,
472 unsigned int blocks_at_once)
474 unsigned char *buffer, *read_buffer;
475 const unsigned int patterns[] = {0xaa, 0x55, 0xff, 0x00};
476 const unsigned int *pattern;
477 int i, try, got, nr_pattern, pat_idx;
478 unsigned int bb_count = 0;
480 buffer = allocate_buffer(2 * blocks_at_once * block_size);
481 read_buffer = buffer + blocks_at_once * block_size;
484 com_err (program_name, ENOMEM, _("while allocating buffers"));
491 fputs(_("Checking for bad blocks in read-write mode\n"),
493 fprintf(stderr, _("From block %lu to %lu\n"),
494 (unsigned long) from_count,
495 (unsigned long) last_block);
502 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
504 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
505 pattern_fill(buffer, pattern[pat_idx],
506 blocks_at_once * block_size);
507 num_blocks = last_block - 1;
508 currently_testing = from_count;
509 if (s_flag && v_flag <= 1)
512 try = blocks_at_once;
513 while (currently_testing < last_block) {
514 if (max_bb && bb_count >= max_bb) {
515 if (s_flag || v_flag) {
516 fputs(_("Too many bad blocks, aborting test\n"), stderr);
520 if (currently_testing + try > last_block)
521 try = last_block - currently_testing;
522 got = do_write(dev, buffer, try, block_size,
527 currently_testing += got;
529 try = blocks_at_once;
530 /* recover page-aligned offset for O_DIRECT */
531 if ( (blocks_at_once >= sys_page_size >> 9)
532 && (currently_testing %
533 (sys_page_size >> 9)!= 0))
534 try -= (sys_page_size >> 9)
536 % (sys_page_size >> 9));
541 bb_count += bb_output(currently_testing++);
548 fputs(_(done_string), stderr);
551 fputs(_("Reading and comparing: "), stderr);
552 num_blocks = last_block;
553 currently_testing = from_count;
554 if (s_flag && v_flag <= 1)
557 try = blocks_at_once;
558 while (currently_testing < last_block) {
559 if (max_bb && bb_count >= max_bb) {
560 if (s_flag || v_flag) {
561 fputs(_("Too many bad blocks, aborting test\n"), stderr);
565 if (currently_testing + try > last_block)
566 try = last_block - currently_testing;
567 got = do_read (dev, read_buffer, try, block_size,
570 bb_count += bb_output(currently_testing++);
573 for (i=0; i < got; i++) {
574 if (memcmp(read_buffer + i * block_size,
575 buffer + i * block_size,
577 bb_count += bb_output(currently_testing+i);
579 currently_testing += got;
580 /* recover page-aligned offset for O_DIRECT */
581 if ( (blocks_at_once >= sys_page_size >> 9)
582 && (currently_testing % (sys_page_size >> 9)!= 0))
583 try = blocks_at_once - (sys_page_size >> 9)
585 % (sys_page_size >> 9));
587 try = blocks_at_once;
595 fputs(_(done_string), stderr);
598 uncapture_terminate();
603 struct saved_blk_record {
608 static unsigned int test_nd (int dev, blk_t last_block,
609 int block_size, blk_t from_count,
610 unsigned int blocks_at_once)
612 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
613 unsigned char *test_base, *save_base, *read_base;
615 const unsigned int patterns[] = { ~0 };
616 const unsigned int *pattern;
617 int nr_pattern, pat_idx;
618 int got, used2, written;
619 blk_t save_currently_testing;
620 struct saved_blk_record *test_record;
621 /* This is static to prevent being clobbered by the longjmp */
622 static int num_saved;
623 jmp_buf terminate_env;
625 unsigned long buf_used;
626 static unsigned int bb_count;
629 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
631 com_err (program_name, errcode,
632 _("while beginning bad block list iteration"));
636 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
637 } while (next_bad && next_bad < from_count);
639 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
640 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
641 if (!blkbuf || !test_record) {
642 com_err(program_name, ENOMEM, _("while allocating buffers"));
647 test_base = blkbuf + (blocks_at_once * block_size);
648 read_base = blkbuf + (2 * blocks_at_once * block_size);
654 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
655 fprintf (stderr, _("From block %lu to %lu\n"),
656 (unsigned long) from_count, (unsigned long) last_block);
658 if (s_flag || v_flag > 1) {
659 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
661 if (setjmp(terminate_env)) {
663 * Abnormal termination by a signal is handled here.
665 signal (SIGALRM, SIG_IGN);
666 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
668 save_ptr = save_base;
669 for (i=0; i < num_saved; i++) {
670 do_write(dev, save_ptr, test_record[i].num,
671 block_size, test_record[i].block);
672 save_ptr += test_record[i].num * block_size;
678 /* set up abend handler */
679 capture_terminate(terminate_env);
686 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
688 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
689 pattern_fill(test_base, pattern[pat_idx],
690 blocks_at_once * block_size);
694 save_ptr = save_base;
695 test_ptr = test_base;
696 currently_testing = from_count;
697 num_blocks = last_block - 1;
698 if (s_flag && v_flag <= 1)
701 while (currently_testing < last_block) {
702 if (max_bb && bb_count >= max_bb) {
703 if (s_flag || v_flag) {
704 fputs(_("Too many bad blocks, aborting test\n"), stderr);
708 got = try = blocks_at_once - buf_used;
710 if (currently_testing == next_bad) {
711 /* fprintf (out, "%lu\n", nextbad); */
712 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
716 else if (currently_testing + try > next_bad)
717 try = next_bad - currently_testing;
719 if (currently_testing + try > last_block)
720 try = last_block - currently_testing;
721 got = do_read (dev, save_ptr, try, block_size,
724 /* First block must have been bad. */
725 bb_count += bb_output(currently_testing++);
730 * Note the fact that we've saved this much data
731 * *before* we overwrite it with test data
733 test_record[num_saved].block = currently_testing;
734 test_record[num_saved].num = got;
737 /* Write the test data */
738 written = do_write (dev, test_ptr, got, block_size,
741 com_err (program_name, errno,
742 _("during test data write, block %lu"),
743 (unsigned long) currently_testing +
747 save_ptr += got * block_size;
748 test_ptr += got * block_size;
749 currently_testing += got;
751 bb_count += bb_output(currently_testing++);
755 * If there's room for more blocks to be tested this
756 * around, and we're not done yet testing the disk, go
757 * back and get some more blocks.
759 if ((buf_used != blocks_at_once) &&
760 (currently_testing < last_block))
764 save_currently_testing = currently_testing;
767 * for each contiguous block that we read into the
768 * buffer (and wrote test data into afterwards), read
769 * it back (looping if necessary, to get past newly
770 * discovered unreadable blocks, of which there should
771 * be none, but with a hard drive which is unreliable,
772 * it has happened), and compare with the test data
773 * that was written; output to the bad block list if
777 save_ptr = save_base;
778 test_ptr = test_base;
779 read_ptr = read_base;
784 if (used2 >= num_saved)
786 currently_testing = test_record[used2].block;
787 try = test_record[used2].num;
791 got = do_read (dev, read_ptr, try,
792 block_size, currently_testing);
794 /* test the comparison between all the
795 blocks successfully read */
796 for (i = 0; i < got; ++i)
797 if (memcmp (test_ptr+i*block_size,
798 read_ptr+i*block_size, block_size))
799 bb_count += bb_output(currently_testing + i);
801 bb_count += bb_output(currently_testing + got);
805 /* write back original data */
806 do_write (dev, save_ptr, got,
807 block_size, currently_testing);
808 save_ptr += got * block_size;
810 currently_testing += got;
811 test_ptr += got * block_size;
812 read_ptr += got * block_size;
816 /* empty the buffer so it can be reused */
819 save_ptr = save_base;
820 test_ptr = test_base;
821 currently_testing = save_currently_testing;
825 if (s_flag || v_flag > 1)
826 fputs(_(done_string), stderr);
830 uncapture_terminate();
835 ext2fs_badblocks_list_iterate_end(bb_iter);
840 static void check_mount(char *device_name)
845 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
847 com_err("ext2fs_check_if_mount", retval,
848 _("while determining whether %s is mounted."),
852 if (mount_flags & EXT2_MF_MOUNTED) {
853 fprintf(stderr, _("%s is mounted; "), device_name);
855 fputs(_("badblocks forced anyway. "
856 "Hope /etc/mtab is incorrect.\n"), stderr);
860 fputs(_("it's not safe to run badblocks!\n"), stderr);
864 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
865 fprintf(stderr, _("%s is apparently in use by the system; "),
868 fputs(_("badblocks forced anyway.\n"), stderr);
870 goto abort_badblocks;
876 * This function will convert a string to an unsigned long, printing
877 * an error message if it fails, and returning success or failure in err.
879 static unsigned int parse_uint(const char *str, const char *descr)
885 ret = strtoul(str, &tmp, 0);
886 if (*tmp || errno || (ret > UINT_MAX) ||
887 (ret == ULONG_MAX && errno == ERANGE)) {
888 com_err (program_name, 0, _("invalid %s - %s"), descr, str);
894 int main (int argc, char ** argv)
898 char * host_device_name = NULL;
899 char * input_file = NULL;
900 char * output_file = NULL;
902 int block_size = 1024;
903 unsigned int blocks_at_once = 64;
904 blk_t last_block, from_count;
906 int passes_clean = 0;
909 unsigned int pattern;
910 unsigned int (*test_func)(int, blk_t,
916 setbuf(stdout, NULL);
917 setbuf(stderr, NULL);
919 setlocale(LC_MESSAGES, "");
920 setlocale(LC_CTYPE, "");
921 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
922 textdomain(NLS_CAT_NAME);
924 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
927 /* Determine the system page size if possible */
929 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
930 #define _SC_PAGESIZE _SC_PAGE_SIZE
933 sysval = sysconf(_SC_PAGESIZE);
935 sys_page_size = sysval;
936 #endif /* _SC_PAGESIZE */
937 #endif /* HAVE_SYSCONF */
940 program_name = *argv;
941 while ((c = getopt (argc, argv, "b:d:e:fi:o:svwnc:p:h:t:X")) != EOF) {
944 block_size = parse_uint(optarg, "block size");
945 if (block_size > 4096) {
946 com_err (program_name, 0,
947 _("bad block size - %s"), optarg);
958 output_file = optarg;
979 blocks_at_once = parse_uint(optarg, "blocks at once");
982 max_bb = parse_uint(optarg, "max bad block count");
985 d_flag = parse_uint(optarg, "read delay factor");
988 num_passes = parse_uint(optarg,
989 "number of clean passes");
992 host_device_name = optarg;
995 if (t_flag + 1 > t_max) {
996 unsigned int *t_patts_new;
998 t_patts_new = realloc(t_patts, t_max + T_INC);
1000 com_err(program_name, ENOMEM,
1001 _("can't allocate memory for "
1002 "test_pattern - %s"),
1006 t_patts = t_patts_new;
1009 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
1010 t_patts[t_flag++] = ~0;
1012 pattern = parse_uint(optarg, "test pattern");
1013 if (pattern == (unsigned int) ~0)
1015 t_patts[t_flag++] = pattern;
1027 com_err(program_name, 0,
1028 _("Maximum of one test_pattern may be specified "
1029 "in read-only mode"));
1032 if (t_patts && (t_patts[0] == (unsigned int) ~0)) {
1033 com_err(program_name, 0,
1034 _("Random test_pattern is not allowed "
1035 "in read-only mode"));
1039 if (optind > argc - 1)
1041 device_name = argv[optind++];
1042 if (optind > argc - 1) {
1043 errcode = ext2fs_get_device_size(device_name,
1046 if (errcode == EXT2_ET_UNIMPLEMENTED) {
1047 com_err(program_name, 0,
1048 _("Couldn't determine device size; you "
1049 "must specify\nthe size manually\n"));
1053 com_err(program_name, errcode,
1054 _("while trying to determine device size"));
1059 last_block = parse_uint(argv[optind], "last block");
1060 printf("last_block = %d (%s)\n", last_block, argv[optind]);
1064 if (optind <= argc-1) {
1066 from_count = parse_uint(argv[optind], "start block");
1067 printf("from_count = %d\n", from_count);
1068 } else from_count = 0;
1069 if (from_count >= last_block) {
1070 com_err (program_name, 0, _("invalid starting block (%lu): must be less than %lu"),
1071 (unsigned long) from_count, (unsigned long) last_block);
1075 check_mount(device_name);
1077 open_flag = w_flag ? O_RDWR : O_RDONLY;
1078 dev = open (device_name, open_flag);
1080 com_err (program_name, errno, _("while trying to open %s"),
1084 if (host_device_name) {
1085 host_dev = open (host_device_name, open_flag);
1086 if (host_dev == -1) {
1087 com_err (program_name, errno,
1088 _("while trying to open %s"),
1095 if (strcmp (input_file, "-") == 0)
1098 in = fopen (input_file, "r");
1101 com_err (program_name, errno,
1102 _("while trying to open %s"),
1108 if (output_file && strcmp (output_file, "-") != 0)
1110 out = fopen (output_file, "w");
1113 com_err (program_name, errno,
1114 _("while trying to open %s"),
1122 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1124 com_err (program_name, errcode,
1125 _("while creating in-memory bad blocks list"));
1131 switch(fscanf (in, "%u\n", &next_bad)) {
1133 com_err (program_name, 0, "input file - bad format");
1138 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1140 com_err (program_name, errcode, _("while adding to in-memory bad block list"));
1153 unsigned int bb_count;
1155 bb_count = test_func(dev, last_block, block_size,
1156 from_count, blocks_at_once);
1164 _("Pass completed, %u bad blocks found.\n"),
1167 } while (passes_clean < num_passes);