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 */
56 #include <sys/ioctl.h>
57 #include <sys/types.h>
60 #include "et/com_err.h"
61 #include "ext2fs/ext2_io.h"
62 #include "ext2fs/ext2_fs.h"
63 #include "ext2fs/ext2fs.h"
64 #include "nls-enable.h"
66 const char * program_name = "badblocks";
67 const char * done_string = N_("done \n");
69 static int v_flag = 0; /* verbose */
70 static int w_flag = 0; /* do r/w test: 0=no, 1=yes,
71 * 2=non-destructive */
72 static int s_flag = 0; /* show progress of test */
73 static int force = 0; /* force check of mounted device */
74 static int t_flag = 0; /* number of test patterns */
75 static int t_max = 0; /* allocated test patterns */
76 static unsigned int *t_patts = NULL; /* test patterns */
77 static int current_O_DIRECT = 0; /* Current status of O_DIRECT flag */
78 static int exclusive_ok = 0;
79 static unsigned int max_bb = 0; /* Abort test if more than this number of bad blocks has been encountered */
80 static unsigned int d_flag = 0; /* delay factor between reads */
84 unsigned int sys_page_size = 4096;
86 static void usage(void)
89 "Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwnf]\n"
90 " [-c blocks_at_once] [-d delay_factor_between_reads] [-e max_bad_blocks]\n"
91 " [-p num_passes] [-t test_pattern [-t test_pattern [...]]]\n"
92 " device [last_block [first_block]]\n"),
97 static void exclusive_usage(void)
100 _("%s: The -n and -w options are mutually exclusive.\n\n"),
105 static blk_t currently_testing = 0;
106 static blk_t num_blocks = 0;
107 static ext2_badblocks_list bb_list = NULL;
109 static blk_t next_bad = 0;
110 static ext2_badblocks_iterate bb_iter = NULL;
112 static void *allocate_buffer(size_t size)
116 #ifdef HAVE_POSIX_MEMALIGN
117 if (posix_memalign(&ret, sys_page_size, size) < 0)
121 ret = memalign(sys_page_size, size);
125 #endif /* HAVE_VALLOC */
126 #endif /* HAVE_MEMALIGN */
127 #endif /* HAVE_POSIX_MEMALIGN */
136 * This routine reports a new bad block. If the bad block has already
137 * been seen before, then it returns 0; otherwise it returns 1.
139 static int bb_output (blk_t bad)
143 if (ext2fs_badblocks_list_test(bb_list, bad))
146 fprintf(out, "%lu\n", (unsigned long) bad);
149 errcode = ext2fs_badblocks_list_add (bb_list, bad);
151 com_err (program_name, errcode, "adding to in-memory bad block list");
156 increment the iteration through the bb_list if
157 an element was just added before the current iteration
158 position. This should not cause next_bad to change. */
159 if (bb_iter && bad < next_bad)
160 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
164 static void print_status(void)
166 fprintf(stderr, "%15lu/%15lu", (unsigned long) currently_testing,
167 (unsigned long) num_blocks);
168 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);
172 static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
174 signal (SIGALRM, alarm_intr);
181 static void *terminate_addr = NULL;
183 static void terminate_intr(int signo EXT2FS_ATTR((unused)))
186 longjmp(terminate_addr,1);
190 static void capture_terminate(jmp_buf term_addr)
192 terminate_addr = term_addr;
193 signal (SIGHUP, terminate_intr);
194 signal (SIGINT, terminate_intr);
195 signal (SIGPIPE, terminate_intr);
196 signal (SIGTERM, terminate_intr);
197 signal (SIGUSR1, terminate_intr);
198 signal (SIGUSR2, terminate_intr);
201 static void uncapture_terminate(void)
203 terminate_addr = NULL;
204 signal (SIGHUP, SIG_DFL);
205 signal (SIGINT, SIG_DFL);
206 signal (SIGPIPE, SIG_DFL);
207 signal (SIGTERM, SIG_DFL);
208 signal (SIGUSR1, SIG_DFL);
209 signal (SIGUSR2, SIG_DFL);
212 static void set_o_direct(int dev, unsigned char *buffer, size_t size,
216 int new_flag = O_DIRECT;
219 if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
220 ((size & (sys_page_size - 1)) != 0) ||
221 ((current_block & ((sys_page_size >> 9)-1)) != 0))
224 if (new_flag != current_O_DIRECT) {
225 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
226 flag = fcntl(dev, F_GETFL);
228 flag = (flag & ~O_DIRECT) | new_flag;
229 fcntl(dev, F_SETFL, flag);
231 current_O_DIRECT = new_flag;
237 static void pattern_fill(unsigned char *buffer, unsigned int pattern,
241 unsigned char bpattern[sizeof(pattern)], *ptr;
243 if (pattern == (unsigned int) ~0) {
244 for (ptr = buffer; ptr < buffer + n; ptr++) {
245 (*ptr) = random() % (1 << (8 * sizeof(char)));
248 fputs(_("Testing with random pattern: "), stderr);
251 for (i = 0; i < sizeof(bpattern); i++) {
254 bpattern[i] = pattern & 0xFF;
255 pattern = pattern >> 8;
258 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
265 if (s_flag | v_flag) {
266 fputs(_("Testing with pattern 0x"), stderr);
267 for (i = 0; i <= nb; i++)
268 fprintf(stderr, "%02x", buffer[i]);
275 * Perform a read of a sequence of blocks; return the number of blocks
276 * successfully sequentially read.
278 static int do_read (int dev, unsigned char * buffer, int try, int block_size,
282 struct timeval tv1, tv2;
283 #define NANOSEC (1000000000L)
284 #define MILISEC (1000L)
286 set_o_direct(dev, buffer, try * block_size, current_block);
291 /* Seek to the correct loc. */
292 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
293 SEEK_SET) != (ext2_loff_t) current_block * block_size)
294 com_err (program_name, errno, _("during seek"));
298 gettimeofday(&tv1, NULL);
299 got = read (dev, buffer, try * block_size);
301 gettimeofday(&tv2, NULL);
305 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
307 if (d_flag && got == try) {
308 #ifdef HAVE_NANOSLEEP
310 ts.tv_sec = tv2.tv_sec - tv1.tv_sec;
311 ts.tv_nsec = (tv2.tv_usec - tv1.tv_usec) * MILISEC;
312 if (ts.tv_nsec < 0) {
313 ts.tv_nsec += NANOSEC;
316 /* increase/decrease the sleep time based on d_flag value */
317 ts.tv_sec = ts.tv_sec * d_flag / 100;
318 ts.tv_nsec = ts.tv_nsec * d_flag / 100;
319 if (ts.tv_nsec > NANOSEC) {
320 ts.tv_sec += ts.tv_nsec / NANOSEC;
321 ts.tv_nsec %= NANOSEC;
323 if (ts.tv_sec || ts.tv_nsec)
324 nanosleep(&ts, NULL);
328 tv.tv_sec = tv2.tv_sec - tv1.tv_sec;
329 tv.tv_usec = tv2.tv_usec - tv1.tv_usec;
330 tv.tv_sec = tv.tv_sec * d_flag / 100;
331 tv.tv_usec = tv.tv_usec * d_flag / 100;
332 if (tv.tv_usec > 1000000) {
333 tv.tv_sec += tv.tv_usec / 1000000;
334 tv.tv_usec %= 1000000;
347 * Perform a write of a sequence of blocks; return the number of blocks
348 * successfully sequentially written.
350 static int do_write(int dev, unsigned char * buffer, int try, int block_size,
351 unsigned long current_block)
355 set_o_direct(dev, buffer, try * block_size, current_block);
360 /* Seek to the correct loc. */
361 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
362 SEEK_SET) != (ext2_loff_t) current_block * block_size)
363 com_err (program_name, errno, _("during seek"));
366 got = write (dev, buffer, try * block_size);
370 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
377 static void flush_bufs(void)
381 retval = ext2fs_sync_device(host_dev, 1);
383 com_err(program_name, retval, _("during ext2fs_sync_device"));
386 static unsigned int test_ro (int dev, blk_t last_block,
387 int block_size, blk_t first_block,
388 unsigned int blocks_at_once)
390 unsigned char * blkbuf;
393 unsigned int bb_count = 0;
396 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
398 com_err (program_name, errcode,
399 _("while beginning bad block list iteration"));
403 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
404 } while (next_bad && next_bad < first_block);
407 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
409 blkbuf = allocate_buffer(blocks_at_once * block_size);
413 com_err (program_name, ENOMEM, _("while allocating buffers"));
417 fprintf (stderr, _("Checking blocks %lu to %lu\n"),
418 (unsigned long) first_block,
419 (unsigned long) last_block - 1);
422 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
423 pattern_fill(blkbuf + blocks_at_once * block_size,
424 t_patts[0], block_size);
427 try = blocks_at_once;
428 currently_testing = first_block;
429 num_blocks = last_block - 1;
430 if (!t_flag && (s_flag || v_flag)) {
431 fputs(_("Checking for bad blocks (read-only test): "), stderr);
435 while (currently_testing < last_block)
437 if (max_bb && bb_count >= max_bb) {
438 if (s_flag || v_flag) {
439 fputs(_("Too many bad blocks, aborting test\n"), stderr);
444 if (currently_testing == next_bad) {
445 /* fprintf (out, "%lu\n", nextbad); */
446 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
450 else if (currently_testing + try > next_bad)
451 try = next_bad - currently_testing;
453 if (currently_testing + try > last_block)
454 try = last_block - currently_testing;
455 got = do_read (dev, blkbuf, try, block_size, currently_testing);
457 /* test the comparison between all the
458 blocks successfully read */
460 for (i = 0; i < got; ++i)
461 if (memcmp (blkbuf+i*block_size,
462 blkbuf+blocks_at_once*block_size,
464 bb_count += bb_output(currently_testing + i);
466 currently_testing += got;
468 try = blocks_at_once;
469 /* recover page-aligned offset for O_DIRECT */
470 if ( (blocks_at_once >= sys_page_size >> 9)
471 && (currently_testing % (sys_page_size >> 9)!= 0))
472 try -= (sys_page_size >> 9)
474 % (sys_page_size >> 9));
480 bb_count += bb_output(currently_testing++);
485 if (s_flag || v_flag)
486 fputs(_(done_string), stderr);
491 ext2fs_badblocks_list_iterate_end(bb_iter);
496 static unsigned int test_rw (int dev, blk_t last_block,
497 int block_size, blk_t first_block,
498 unsigned int blocks_at_once)
500 unsigned char *buffer, *read_buffer;
501 const unsigned int patterns[] = {0xaa, 0x55, 0xff, 0x00};
502 const unsigned int *pattern;
503 int i, try, got, nr_pattern, pat_idx;
504 unsigned int bb_count = 0;
506 buffer = allocate_buffer(2 * blocks_at_once * block_size);
507 read_buffer = buffer + blocks_at_once * block_size;
510 com_err (program_name, ENOMEM, _("while allocating buffers"));
517 fputs(_("Checking for bad blocks in read-write mode\n"),
519 fprintf(stderr, _("From block %lu to %lu\n"),
520 (unsigned long) first_block,
521 (unsigned long) last_block - 1);
528 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
530 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
531 pattern_fill(buffer, pattern[pat_idx],
532 blocks_at_once * block_size);
533 num_blocks = last_block - 1;
534 currently_testing = first_block;
535 if (s_flag && v_flag <= 1)
538 try = blocks_at_once;
539 while (currently_testing < last_block) {
540 if (max_bb && bb_count >= max_bb) {
541 if (s_flag || v_flag) {
542 fputs(_("Too many bad blocks, aborting test\n"), stderr);
546 if (currently_testing + try > last_block)
547 try = last_block - currently_testing;
548 got = do_write(dev, buffer, try, block_size,
553 currently_testing += got;
555 try = blocks_at_once;
556 /* recover page-aligned offset for O_DIRECT */
557 if ( (blocks_at_once >= sys_page_size >> 9)
558 && (currently_testing %
559 (sys_page_size >> 9)!= 0))
560 try -= (sys_page_size >> 9)
562 % (sys_page_size >> 9));
567 bb_count += bb_output(currently_testing++);
574 fputs(_(done_string), stderr);
577 fputs(_("Reading and comparing: "), stderr);
578 num_blocks = last_block;
579 currently_testing = first_block;
580 if (s_flag && v_flag <= 1)
583 try = blocks_at_once;
584 while (currently_testing < last_block) {
585 if (max_bb && bb_count >= max_bb) {
586 if (s_flag || v_flag) {
587 fputs(_("Too many bad blocks, aborting test\n"), stderr);
591 if (currently_testing + try > last_block)
592 try = last_block - currently_testing;
593 got = do_read (dev, read_buffer, try, block_size,
596 bb_count += bb_output(currently_testing++);
599 for (i=0; i < got; i++) {
600 if (memcmp(read_buffer + i * block_size,
601 buffer + i * block_size,
603 bb_count += bb_output(currently_testing+i);
605 currently_testing += got;
606 /* recover page-aligned offset for O_DIRECT */
607 if ( (blocks_at_once >= sys_page_size >> 9)
608 && (currently_testing % (sys_page_size >> 9)!= 0))
609 try = blocks_at_once - (sys_page_size >> 9)
611 % (sys_page_size >> 9));
613 try = blocks_at_once;
621 fputs(_(done_string), stderr);
624 uncapture_terminate();
629 struct saved_blk_record {
634 static unsigned int test_nd (int dev, blk_t last_block,
635 int block_size, blk_t first_block,
636 unsigned int blocks_at_once)
638 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
639 unsigned char *test_base, *save_base, *read_base;
641 const unsigned int patterns[] = { ~0 };
642 const unsigned int *pattern;
643 int nr_pattern, pat_idx;
644 int got, used2, written;
645 blk_t save_currently_testing;
646 struct saved_blk_record *test_record;
647 /* This is static to prevent being clobbered by the longjmp */
648 static int num_saved;
649 jmp_buf terminate_env;
651 unsigned long buf_used;
652 static unsigned int bb_count;
655 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
657 com_err (program_name, errcode,
658 _("while beginning bad block list iteration"));
662 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
663 } while (next_bad && next_bad < first_block);
665 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
666 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
667 if (!blkbuf || !test_record) {
668 com_err(program_name, ENOMEM, _("while allocating buffers"));
673 test_base = blkbuf + (blocks_at_once * block_size);
674 read_base = blkbuf + (2 * blocks_at_once * block_size);
680 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
681 fprintf (stderr, _("From block %lu to %lu\n"),
682 (unsigned long) first_block,
683 (unsigned long) last_block - 1);
685 if (s_flag || v_flag > 1) {
686 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
688 if (setjmp(terminate_env)) {
690 * Abnormal termination by a signal is handled here.
692 signal (SIGALRM, SIG_IGN);
693 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
695 save_ptr = save_base;
696 for (i=0; i < num_saved; i++) {
697 do_write(dev, save_ptr, test_record[i].num,
698 block_size, test_record[i].block);
699 save_ptr += test_record[i].num * block_size;
705 /* set up abend handler */
706 capture_terminate(terminate_env);
713 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
715 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
716 pattern_fill(test_base, pattern[pat_idx],
717 blocks_at_once * block_size);
721 save_ptr = save_base;
722 test_ptr = test_base;
723 currently_testing = first_block;
724 num_blocks = last_block - 1;
725 if (s_flag && v_flag <= 1)
728 while (currently_testing < last_block) {
729 if (max_bb && bb_count >= max_bb) {
730 if (s_flag || v_flag) {
731 fputs(_("Too many bad blocks, aborting test\n"), stderr);
735 got = try = blocks_at_once - buf_used;
737 if (currently_testing == next_bad) {
738 /* fprintf (out, "%lu\n", nextbad); */
739 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
743 else if (currently_testing + try > next_bad)
744 try = next_bad - currently_testing;
746 if (currently_testing + try > last_block)
747 try = last_block - currently_testing;
748 got = do_read (dev, save_ptr, try, block_size,
751 /* First block must have been bad. */
752 bb_count += bb_output(currently_testing++);
757 * Note the fact that we've saved this much data
758 * *before* we overwrite it with test data
760 test_record[num_saved].block = currently_testing;
761 test_record[num_saved].num = got;
764 /* Write the test data */
765 written = do_write (dev, test_ptr, got, block_size,
768 com_err (program_name, errno,
769 _("during test data write, block %lu"),
770 (unsigned long) currently_testing +
774 save_ptr += got * block_size;
775 test_ptr += got * block_size;
776 currently_testing += got;
778 bb_count += bb_output(currently_testing++);
782 * If there's room for more blocks to be tested this
783 * around, and we're not done yet testing the disk, go
784 * back and get some more blocks.
786 if ((buf_used != blocks_at_once) &&
787 (currently_testing < last_block))
791 save_currently_testing = currently_testing;
794 * for each contiguous block that we read into the
795 * buffer (and wrote test data into afterwards), read
796 * it back (looping if necessary, to get past newly
797 * discovered unreadable blocks, of which there should
798 * be none, but with a hard drive which is unreliable,
799 * it has happened), and compare with the test data
800 * that was written; output to the bad block list if
804 save_ptr = save_base;
805 test_ptr = test_base;
806 read_ptr = read_base;
811 if (used2 >= num_saved)
813 currently_testing = test_record[used2].block;
814 try = test_record[used2].num;
818 got = do_read (dev, read_ptr, try,
819 block_size, currently_testing);
821 /* test the comparison between all the
822 blocks successfully read */
823 for (i = 0; i < got; ++i)
824 if (memcmp (test_ptr+i*block_size,
825 read_ptr+i*block_size, block_size))
826 bb_count += bb_output(currently_testing + i);
828 bb_count += bb_output(currently_testing + got);
832 /* write back original data */
833 do_write (dev, save_ptr, got,
834 block_size, currently_testing);
835 save_ptr += got * block_size;
837 currently_testing += got;
838 test_ptr += got * block_size;
839 read_ptr += got * block_size;
843 /* empty the buffer so it can be reused */
846 save_ptr = save_base;
847 test_ptr = test_base;
848 currently_testing = save_currently_testing;
852 if (s_flag || v_flag > 1)
853 fputs(_(done_string), stderr);
857 uncapture_terminate();
862 ext2fs_badblocks_list_iterate_end(bb_iter);
867 static void check_mount(char *device_name)
872 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
874 com_err("ext2fs_check_if_mount", retval,
875 _("while determining whether %s is mounted."),
879 if (mount_flags & EXT2_MF_MOUNTED) {
880 fprintf(stderr, _("%s is mounted; "), device_name);
882 fputs(_("badblocks forced anyway. "
883 "Hope /etc/mtab is incorrect.\n"), stderr);
887 fputs(_("it's not safe to run badblocks!\n"), stderr);
891 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
892 fprintf(stderr, _("%s is apparently in use by the system; "),
895 fputs(_("badblocks forced anyway.\n"), stderr);
897 goto abort_badblocks;
903 * This function will convert a string to an unsigned long, printing
904 * an error message if it fails, and returning success or failure in err.
906 static unsigned int parse_uint(const char *str, const char *descr)
912 ret = strtoul(str, &tmp, 0);
913 if (*tmp || errno || (ret > UINT_MAX) ||
914 (ret == ULONG_MAX && errno == ERANGE)) {
915 com_err (program_name, 0, _("invalid %s - %s"), descr, str);
921 int main (int argc, char ** argv)
925 char * host_device_name = NULL;
926 char * input_file = NULL;
927 char * output_file = NULL;
929 int block_size = 1024;
930 unsigned int blocks_at_once = 64;
931 blk_t last_block, first_block;
933 int passes_clean = 0;
936 unsigned int pattern;
937 unsigned int (*test_func)(int, blk_t,
943 setbuf(stdout, NULL);
944 setbuf(stderr, NULL);
946 setlocale(LC_MESSAGES, "");
947 setlocale(LC_CTYPE, "");
948 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
949 textdomain(NLS_CAT_NAME);
951 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
954 /* Determine the system page size if possible */
956 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
957 #define _SC_PAGESIZE _SC_PAGE_SIZE
960 sysval = sysconf(_SC_PAGESIZE);
962 sys_page_size = sysval;
963 #endif /* _SC_PAGESIZE */
964 #endif /* HAVE_SYSCONF */
967 program_name = *argv;
968 while ((c = getopt (argc, argv, "b:d:e:fi:o:svwnc:p:h:t:X")) != EOF) {
971 block_size = parse_uint(optarg, "block size");
972 if (block_size > 4096) {
973 com_err (program_name, 0,
974 _("bad block size - %s"), optarg);
985 output_file = optarg;
1002 test_func = test_nd;
1006 blocks_at_once = parse_uint(optarg, "blocks at once");
1009 max_bb = parse_uint(optarg, "max bad block count");
1012 d_flag = parse_uint(optarg, "read delay factor");
1015 num_passes = parse_uint(optarg,
1016 "number of clean passes");
1019 host_device_name = optarg;
1022 if (t_flag + 1 > t_max) {
1023 unsigned int *t_patts_new;
1025 t_patts_new = realloc(t_patts, sizeof(int) *
1028 com_err(program_name, ENOMEM,
1029 _("can't allocate memory for "
1030 "test_pattern - %s"),
1034 t_patts = t_patts_new;
1037 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
1038 t_patts[t_flag++] = ~0;
1040 pattern = parse_uint(optarg, "test pattern");
1041 if (pattern == (unsigned int) ~0)
1043 t_patts[t_flag++] = pattern;
1055 com_err(program_name, 0,
1056 _("Maximum of one test_pattern may be specified "
1057 "in read-only mode"));
1060 if (t_patts && (t_patts[0] == (unsigned int) ~0)) {
1061 com_err(program_name, 0,
1062 _("Random test_pattern is not allowed "
1063 "in read-only mode"));
1067 if (optind > argc - 1)
1069 device_name = argv[optind++];
1070 if (optind > argc - 1) {
1071 errcode = ext2fs_get_device_size(device_name,
1074 if (errcode == EXT2_ET_UNIMPLEMENTED) {
1075 com_err(program_name, 0,
1076 _("Couldn't determine device size; you "
1077 "must specify\nthe size manually\n"));
1081 com_err(program_name, errcode,
1082 _("while trying to determine device size"));
1087 last_block = parse_uint(argv[optind], _("last block"));
1091 if (optind <= argc-1) {
1093 first_block = parse_uint(argv[optind], _("first block"));
1094 } else first_block = 0;
1095 if (first_block >= last_block) {
1096 com_err (program_name, 0, _("invalid starting block (%lu): must be less than %lu"),
1097 (unsigned long) first_block, (unsigned long) last_block);
1101 check_mount(device_name);
1103 open_flag = O_LARGEFILE | (w_flag ? O_RDWR : O_RDONLY);
1104 dev = open (device_name, open_flag);
1106 com_err (program_name, errno, _("while trying to open %s"),
1110 if (host_device_name) {
1111 host_dev = open (host_device_name, open_flag);
1112 if (host_dev == -1) {
1113 com_err (program_name, errno,
1114 _("while trying to open %s"),
1121 if (strcmp (input_file, "-") == 0)
1124 in = fopen (input_file, "r");
1127 com_err (program_name, errno,
1128 _("while trying to open %s"),
1134 if (output_file && strcmp (output_file, "-") != 0)
1136 out = fopen (output_file, "w");
1139 com_err (program_name, errno,
1140 _("while trying to open %s"),
1148 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1150 com_err (program_name, errcode,
1151 _("while creating in-memory bad blocks list"));
1157 switch(fscanf (in, "%u\n", &next_bad)) {
1159 com_err (program_name, 0, "input file - bad format");
1164 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1166 com_err (program_name, errcode, _("while adding to in-memory bad block list"));
1179 unsigned int bb_count;
1181 bb_count = test_func(dev, last_block, block_size,
1182 first_block, blocks_at_once);
1190 _("Pass completed, %u bad blocks found.\n"),
1193 } while (passes_clean < num_passes);