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 */
51 #include <sys/ioctl.h>
52 #include <sys/types.h>
54 #include "et/com_err.h"
55 #include "ext2fs/ext2_io.h"
56 #include "ext2fs/ext2_fs.h"
57 #include "ext2fs/ext2fs.h"
58 #include "nls-enable.h"
60 const char * program_name
= "badblocks";
61 const char * done_string
= N_("done \n");
63 static int v_flag
= 0; /* verbose */
64 static int w_flag
= 0; /* do r/w test: 0=no, 1=yes,
65 * 2=non-destructive */
66 static int s_flag
= 0; /* show progress of test */
67 static int force
= 0; /* force check of mounted device */
68 static int t_flag
= 0; /* number of test patterns */
69 static int t_max
= 0; /* allocated test patterns */
70 static unsigned long *t_patts
= NULL
; /* test patterns */
71 static int current_O_DIRECT
= 0; /* Current status of O_DIRECT flag */
72 static int exclusive_ok
= 0;
76 int sys_page_size
= 4096;
78 static void usage(void)
80 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"),
85 static void exclusive_usage(void)
88 _("%s: The -n and -w options are mutually exclusive.\n\n"),
93 static unsigned long currently_testing
= 0;
94 static unsigned long num_blocks
= 0;
95 static ext2_badblocks_list bb_list
= NULL
;
97 static blk_t next_bad
= 0;
98 static ext2_badblocks_iterate bb_iter
= NULL
;
100 static void *allocate_buffer(size_t size
)
104 #ifdef HAVE_POSIX_MEMALIGN
105 if (posix_memalign(&ret
, sys_page_size
, size
) < 0)
109 ret
= memalign(sys_page_size
, size
);
113 #endif /* HAVE_VALLOC */
114 #endif /* HAVE_MEMALIGN */
115 #endif /* HAVE_POSIX_MEMALIGN */
124 * This routine reports a new bad block. If the bad block has already
125 * been seen before, then it returns 0; otherwise it returns 1.
127 static int bb_output (unsigned long bad
)
131 if (ext2fs_badblocks_list_test(bb_list
, bad
))
134 fprintf(out
, "%lu\n", bad
);
137 errcode
= ext2fs_badblocks_list_add (bb_list
, bad
);
139 com_err (program_name
, errcode
, "adding to in-memory bad block list");
144 increment the iteration through the bb_list if
145 an element was just added before the current iteration
146 position. This should not cause next_bad to change. */
147 if (bb_iter
&& bad
< next_bad
)
148 ext2fs_badblocks_list_iterate (bb_iter
, &next_bad
);
152 static void print_status(void)
154 fprintf(stderr
, "%15ld/%15ld", currently_testing
, num_blocks
);
155 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
);
159 static void alarm_intr(int alnum
EXT2FS_ATTR((unused
)))
161 signal (SIGALRM
, alarm_intr
);
168 static void *terminate_addr
= NULL
;
170 static void terminate_intr(int signo
EXT2FS_ATTR((unused
)))
173 longjmp(terminate_addr
,1);
177 static void capture_terminate(jmp_buf term_addr
)
179 terminate_addr
= term_addr
;
180 signal (SIGHUP
, terminate_intr
);
181 signal (SIGINT
, terminate_intr
);
182 signal (SIGPIPE
, terminate_intr
);
183 signal (SIGTERM
, terminate_intr
);
184 signal (SIGUSR1
, terminate_intr
);
185 signal (SIGUSR2
, terminate_intr
);
188 static void uncapture_terminate(void)
190 terminate_addr
= NULL
;
191 signal (SIGHUP
, SIG_DFL
);
192 signal (SIGINT
, SIG_DFL
);
193 signal (SIGPIPE
, SIG_DFL
);
194 signal (SIGTERM
, SIG_DFL
);
195 signal (SIGUSR1
, SIG_DFL
);
196 signal (SIGUSR2
, SIG_DFL
);
199 static void set_o_direct(int dev
, unsigned char *buffer
, size_t size
,
200 unsigned long current_block
)
203 int new_flag
= O_DIRECT
;
206 if ((((unsigned long) buffer
& (sys_page_size
- 1)) != 0) ||
207 ((size
& (sys_page_size
- 1)) != 0) ||
208 ((current_block
& ((sys_page_size
>> 9)-1)) != 0))
211 if (new_flag
!= current_O_DIRECT
) {
212 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
213 flag
= fcntl(dev
, F_GETFL
);
215 flag
= (flag
& ~O_DIRECT
) | new_flag
;
216 fcntl(dev
, F_SETFL
, flag
);
218 current_O_DIRECT
= new_flag
;
224 static void pattern_fill(unsigned char *buffer
, unsigned long pattern
,
228 unsigned char bpattern
[sizeof(pattern
)], *ptr
;
230 if (pattern
== (unsigned long) ~0) {
231 for (ptr
= buffer
; ptr
< buffer
+ n
; ptr
++) {
232 (*ptr
) = random() % (1 << (8 * sizeof(char)));
235 fputs(_("Testing with random pattern: "), stderr
);
238 for (i
= 0; i
< sizeof(bpattern
); i
++) {
241 bpattern
[i
] = pattern
& 0xFF;
242 pattern
= pattern
>> 8;
245 for (ptr
= buffer
, i
= nb
; ptr
< buffer
+ n
; ptr
++) {
252 if (s_flag
| v_flag
) {
253 fputs(_("Testing with pattern 0x"), stderr
);
254 for (i
= 0; i
<= nb
; i
++)
255 fprintf(stderr
, "%02x", buffer
[i
]);
262 * Perform a read of a sequence of blocks; return the number of blocks
263 * successfully sequentially read.
265 static long do_read (int dev
, unsigned char * buffer
, int try, int block_size
,
266 unsigned long current_block
)
270 set_o_direct(dev
, buffer
, try * block_size
, current_block
);
275 /* Seek to the correct loc. */
276 if (ext2fs_llseek (dev
, (ext2_loff_t
) current_block
* block_size
,
277 SEEK_SET
) != (ext2_loff_t
) current_block
* block_size
)
278 com_err (program_name
, errno
, _("during seek"));
281 got
= read (dev
, buffer
, try * block_size
);
285 fprintf(stderr
, _("Weird value (%ld) in do_read\n"), got
);
291 * Perform a write of a sequence of blocks; return the number of blocks
292 * successfully sequentially written.
294 static long do_write (int dev
, unsigned char * buffer
, int try, int block_size
,
295 unsigned long current_block
)
299 set_o_direct(dev
, buffer
, try * block_size
, current_block
);
304 /* Seek to the correct loc. */
305 if (ext2fs_llseek (dev
, (ext2_loff_t
) current_block
* block_size
,
306 SEEK_SET
) != (ext2_loff_t
) current_block
* block_size
)
307 com_err (program_name
, errno
, _("during seek"));
310 got
= write (dev
, buffer
, try * block_size
);
314 fprintf(stderr
, "Weird value (%ld) in do_write\n", got
);
321 static void flush_bufs(void)
325 retval
= ext2fs_sync_device(host_dev
, 1);
327 com_err(program_name
, retval
, _("during ext2fs_sync_device"));
330 static unsigned int test_ro (int dev
, unsigned long last_block
,
331 int block_size
, unsigned long from_count
,
332 unsigned long blocks_at_once
)
334 unsigned char * blkbuf
;
337 unsigned int bb_count
= 0;
340 errcode
= ext2fs_badblocks_list_iterate_begin(bb_list
,&bb_iter
);
342 com_err (program_name
, errcode
,
343 _("while beginning bad block list iteration"));
347 ext2fs_badblocks_list_iterate (bb_iter
, &next_bad
);
348 } while (next_bad
&& next_bad
< from_count
);
351 blkbuf
= allocate_buffer((blocks_at_once
+ 1) * block_size
);
353 blkbuf
= allocate_buffer(blocks_at_once
* block_size
);
357 com_err (program_name
, ENOMEM
, _("while allocating buffers"));
361 fprintf (stderr
, _("Checking blocks %lu to %lu\n"), from_count
,
365 fputs(_("Checking for bad blocks in read-only mode\n"), stderr
);
366 pattern_fill(blkbuf
+ blocks_at_once
* block_size
,
367 t_patts
[0], block_size
);
370 try = blocks_at_once
;
371 currently_testing
= from_count
;
372 num_blocks
= last_block
- 1;
373 if (!t_flag
&& (s_flag
|| v_flag
)) {
374 fputs(_("Checking for bad blocks (read-only test): "), stderr
);
378 while (currently_testing
< last_block
)
381 if (currently_testing
== next_bad
) {
382 /* fprintf (out, "%lu\n", nextbad); */
383 ext2fs_badblocks_list_iterate (bb_iter
, &next_bad
);
387 else if (currently_testing
+ try > next_bad
)
388 try = next_bad
- currently_testing
;
390 if (currently_testing
+ try > last_block
)
391 try = last_block
- currently_testing
;
392 got
= do_read (dev
, blkbuf
, try, block_size
, currently_testing
);
394 /* test the comparison between all the
395 blocks successfully read */
397 for (i
= 0; i
< got
; ++i
)
398 if (memcmp (blkbuf
+i
*block_size
,
399 blkbuf
+blocks_at_once
*block_size
,
401 bb_count
+= bb_output(currently_testing
+ i
);
403 currently_testing
+= got
;
405 try = blocks_at_once
;
406 /* recover page-aligned offset for O_DIRECT */
407 if ( blocks_at_once
>= (unsigned long) (sys_page_size
>> 9)
408 && (currently_testing
% (sys_page_size
>> 9)!= 0))
409 try -= (sys_page_size
>> 9)
411 % (sys_page_size
>> 9));
417 bb_count
+= bb_output(currently_testing
++);
422 if (s_flag
|| v_flag
)
423 fputs(_(done_string
), stderr
);
428 ext2fs_badblocks_list_iterate_end(bb_iter
);
433 static unsigned int test_rw (int dev
, unsigned long last_block
,
434 int block_size
, unsigned long from_count
,
435 unsigned long blocks_at_once
)
437 unsigned char *buffer
, *read_buffer
;
438 const unsigned long patterns
[] = {0xaa, 0x55, 0xff, 0x00};
439 const unsigned long *pattern
;
440 int i
, try, got
, nr_pattern
, pat_idx
;
441 unsigned int bb_count
= 0;
443 buffer
= allocate_buffer(2 * blocks_at_once
* block_size
);
444 read_buffer
= buffer
+ blocks_at_once
* block_size
;
447 com_err (program_name
, ENOMEM
, _("while allocating buffers"));
454 fputs(_("Checking for bad blocks in read-write mode\n"),
456 fprintf(stderr
, _("From block %lu to %lu\n"),
457 from_count
, last_block
);
464 nr_pattern
= sizeof(patterns
) / sizeof(patterns
[0]);
466 for (pat_idx
= 0; pat_idx
< nr_pattern
; pat_idx
++) {
467 pattern_fill(buffer
, pattern
[pat_idx
],
468 blocks_at_once
* block_size
);
469 num_blocks
= last_block
- 1;
470 currently_testing
= from_count
;
471 if (s_flag
&& v_flag
<= 1)
474 try = blocks_at_once
;
475 while (currently_testing
< last_block
) {
476 if (currently_testing
+ try > last_block
)
477 try = last_block
- currently_testing
;
478 got
= do_write(dev
, buffer
, try, block_size
,
483 currently_testing
+= got
;
485 try = blocks_at_once
;
486 /* recover page-aligned offset for O_DIRECT */
487 if ( blocks_at_once
>= (unsigned long) (sys_page_size
>> 9)
488 && (currently_testing
%
489 (sys_page_size
>> 9)!= 0))
490 try -= (sys_page_size
>> 9)
492 % (sys_page_size
>> 9));
497 bb_count
+= bb_output(currently_testing
++);
504 fputs(_(done_string
), stderr
);
507 fputs(_("Reading and comparing: "), stderr
);
508 num_blocks
= last_block
;
509 currently_testing
= from_count
;
510 if (s_flag
&& v_flag
<= 1)
513 try = blocks_at_once
;
514 while (currently_testing
< last_block
) {
515 if (currently_testing
+ try > last_block
)
516 try = last_block
- currently_testing
;
517 got
= do_read (dev
, read_buffer
, try, block_size
,
520 bb_count
+= bb_output(currently_testing
++);
523 for (i
=0; i
< got
; i
++) {
524 if (memcmp(read_buffer
+ i
* block_size
,
525 buffer
+ i
* block_size
,
527 bb_count
+= bb_output(currently_testing
+i
);
529 currently_testing
+= got
;
530 /* recover page-aligned offset for O_DIRECT */
531 if ( blocks_at_once
>= (unsigned long) (sys_page_size
>> 9)
532 && (currently_testing
% (sys_page_size
>> 9)!= 0))
533 try = blocks_at_once
- (sys_page_size
>> 9)
535 % (sys_page_size
>> 9));
537 try = blocks_at_once
;
545 fputs(_(done_string
), stderr
);
548 uncapture_terminate();
553 struct saved_blk_record
{
558 static unsigned int test_nd (int dev
, unsigned long last_block
,
559 int block_size
, unsigned long from_count
,
560 unsigned long blocks_at_once
)
562 unsigned char *blkbuf
, *save_ptr
, *test_ptr
, *read_ptr
;
563 unsigned char *test_base
, *save_base
, *read_base
;
565 const unsigned long patterns
[] = { ~0 };
566 const unsigned long *pattern
;
567 int nr_pattern
, pat_idx
;
568 long got
, used2
, written
, save_currently_testing
;
569 struct saved_blk_record
*test_record
;
570 /* This is static to prevent being clobbered by the longjmp */
571 static int num_saved
;
572 jmp_buf terminate_env
;
574 unsigned long buf_used
;
575 static unsigned int bb_count
;
578 errcode
= ext2fs_badblocks_list_iterate_begin(bb_list
,&bb_iter
);
580 com_err (program_name
, errcode
,
581 _("while beginning bad block list iteration"));
585 ext2fs_badblocks_list_iterate (bb_iter
, &next_bad
);
586 } while (next_bad
&& next_bad
< from_count
);
588 blkbuf
= allocate_buffer(3 * blocks_at_once
* block_size
);
589 test_record
= malloc (blocks_at_once
*sizeof(struct saved_blk_record
));
590 if (!blkbuf
|| !test_record
) {
591 com_err(program_name
, ENOMEM
, _("while allocating buffers"));
596 test_base
= blkbuf
+ (blocks_at_once
* block_size
);
597 read_base
= blkbuf
+ (2 * blocks_at_once
* block_size
);
603 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr
);
604 fprintf (stderr
, _("From block %lu to %lu\n"), from_count
, last_block
);
606 if (s_flag
|| v_flag
> 1) {
607 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr
);
609 if (setjmp(terminate_env
)) {
611 * Abnormal termination by a signal is handled here.
613 signal (SIGALRM
, SIG_IGN
);
614 fputs(_("\nInterrupt caught, cleaning up\n"), stderr
);
616 save_ptr
= save_base
;
617 for (i
=0; i
< num_saved
; i
++) {
618 do_write(dev
, save_ptr
, test_record
[i
].num
,
619 block_size
, test_record
[i
].block
);
620 save_ptr
+= test_record
[i
].num
* block_size
;
626 /* set up abend handler */
627 capture_terminate(terminate_env
);
634 nr_pattern
= sizeof(patterns
) / sizeof(patterns
[0]);
636 for (pat_idx
= 0; pat_idx
< nr_pattern
; pat_idx
++) {
637 pattern_fill(test_base
, pattern
[pat_idx
],
638 blocks_at_once
* block_size
);
642 save_ptr
= save_base
;
643 test_ptr
= test_base
;
644 currently_testing
= from_count
;
645 num_blocks
= last_block
- 1;
646 if (s_flag
&& v_flag
<= 1)
649 while (currently_testing
< last_block
) {
650 got
= try = blocks_at_once
- buf_used
;
652 if (currently_testing
== next_bad
) {
653 /* fprintf (out, "%lu\n", nextbad); */
654 ext2fs_badblocks_list_iterate (bb_iter
, &next_bad
);
658 else if (currently_testing
+ try > next_bad
)
659 try = next_bad
- currently_testing
;
661 if (currently_testing
+ try > last_block
)
662 try = last_block
- currently_testing
;
663 got
= do_read (dev
, save_ptr
, try, block_size
,
666 /* First block must have been bad. */
667 bb_count
+= bb_output(currently_testing
++);
672 * Note the fact that we've saved this much data
673 * *before* we overwrite it with test data
675 test_record
[num_saved
].block
= currently_testing
;
676 test_record
[num_saved
].num
= got
;
679 /* Write the test data */
680 written
= do_write (dev
, test_ptr
, got
, block_size
,
683 com_err (program_name
, errno
,
684 _("during test data write, block %lu"),
685 currently_testing
+ written
);
688 save_ptr
+= got
* block_size
;
689 test_ptr
+= got
* block_size
;
690 currently_testing
+= got
;
692 bb_count
+= bb_output(currently_testing
++);
696 * If there's room for more blocks to be tested this
697 * around, and we're not done yet testing the disk, go
698 * back and get some more blocks.
700 if ((buf_used
!= blocks_at_once
) &&
701 (currently_testing
< last_block
))
705 save_currently_testing
= currently_testing
;
708 * for each contiguous block that we read into the
709 * buffer (and wrote test data into afterwards), read
710 * it back (looping if necessary, to get past newly
711 * discovered unreadable blocks, of which there should
712 * be none, but with a hard drive which is unreliable,
713 * it has happened), and compare with the test data
714 * that was written; output to the bad block list if
718 save_ptr
= save_base
;
719 test_ptr
= test_base
;
720 read_ptr
= read_base
;
725 if (used2
>= num_saved
)
727 currently_testing
= test_record
[used2
].block
;
728 try = test_record
[used2
].num
;
732 got
= do_read (dev
, read_ptr
, try,
733 block_size
, currently_testing
);
735 /* test the comparison between all the
736 blocks successfully read */
737 for (i
= 0; i
< got
; ++i
)
738 if (memcmp (test_ptr
+i
*block_size
,
739 read_ptr
+i
*block_size
, block_size
))
740 bb_count
+= bb_output(currently_testing
+ i
);
742 bb_count
+= bb_output(currently_testing
+ got
);
746 /* write back original data */
747 do_write (dev
, save_ptr
, got
,
748 block_size
, currently_testing
);
749 save_ptr
+= got
* block_size
;
751 currently_testing
+= got
;
752 test_ptr
+= got
* block_size
;
753 read_ptr
+= got
* block_size
;
757 /* empty the buffer so it can be reused */
760 save_ptr
= save_base
;
761 test_ptr
= test_base
;
762 currently_testing
= save_currently_testing
;
766 if (s_flag
|| v_flag
> 1)
767 fputs(_(done_string
), stderr
);
771 uncapture_terminate();
776 ext2fs_badblocks_list_iterate_end(bb_iter
);
781 static void check_mount(char *device_name
)
786 retval
= ext2fs_check_if_mounted(device_name
, &mount_flags
);
788 com_err("ext2fs_check_if_mount", retval
,
789 _("while determining whether %s is mounted."),
793 if (mount_flags
& EXT2_MF_MOUNTED
) {
794 fprintf(stderr
, _("%s is mounted; "), device_name
);
796 fputs(_("badblocks forced anyway. "
797 "Hope /etc/mtab is incorrect.\n"), stderr
);
801 fputs(_("it's not safe to run badblocks!\n"), stderr
);
805 if ((mount_flags
& EXT2_MF_BUSY
) && !exclusive_ok
) {
806 fprintf(stderr
, _("%s is apparently in use by the system; "),
809 fputs(_("badblocks forced anyway.\n"), stderr
);
811 goto abort_badblocks
;
817 int main (int argc
, char ** argv
)
822 char * host_device_name
= NULL
;
823 char * input_file
= NULL
;
824 char * output_file
= NULL
;
826 int block_size
= 1024;
827 unsigned long blocks_at_once
= 64;
828 blk_t last_block
, from_count
;
830 int passes_clean
= 0;
833 unsigned long pattern
;
834 unsigned int (*test_func
)(int, unsigned long,
840 setbuf(stdout
, NULL
);
841 setbuf(stderr
, NULL
);
843 setlocale(LC_MESSAGES
, "");
844 setlocale(LC_CTYPE
, "");
845 bindtextdomain(NLS_CAT_NAME
, LOCALEDIR
);
846 textdomain(NLS_CAT_NAME
);
848 srandom((unsigned int)time(NULL
)); /* simple randomness is enough */
851 /* Determine the system page size if possible */
853 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
854 #define _SC_PAGESIZE _SC_PAGE_SIZE
857 sysval
= sysconf(_SC_PAGESIZE
);
859 sys_page_size
= sysval
;
860 #endif /* _SC_PAGESIZE */
861 #endif /* HAVE_SYSCONF */
864 program_name
= *argv
;
865 while ((c
= getopt (argc
, argv
, "b:fi:o:svwnc:p:h:t:X")) != EOF
) {
868 block_size
= strtoul (optarg
, &tmp
, 0);
869 if (*tmp
|| block_size
> 4096) {
870 com_err (program_name
, 0,
871 _("bad block size - %s"), optarg
);
882 output_file
= optarg
;
903 blocks_at_once
= strtoul (optarg
, &tmp
, 0);
905 com_err (program_name
, 0,
906 "bad simultaneous block count - %s", optarg
);
911 num_passes
= strtoul (optarg
, &tmp
, 0);
913 com_err (program_name
, 0,
914 "bad number of clean passes - %s", optarg
);
919 host_device_name
= optarg
;
922 if (t_flag
+ 1 > t_max
) {
923 unsigned long *t_patts_new
;
925 t_patts_new
= realloc(t_patts
, t_max
+ T_INC
);
927 com_err(program_name
, ENOMEM
,
928 _("can't allocate memory for "
929 "test_pattern - %s"),
933 t_patts
= t_patts_new
;
936 if (!strcmp(optarg
, "r") || !strcmp(optarg
,"random")) {
937 t_patts
[t_flag
++] = ~0;
939 pattern
= strtoul(optarg
, &tmp
, 0);
941 com_err(program_name
, 0,
942 _("invalid test_pattern: %s\n"),
946 if (pattern
== (unsigned long) ~0)
948 t_patts
[t_flag
++] = pattern
;
960 com_err(program_name
, 0,
961 _("Maximum of one test_pattern may be specified "
962 "in read-only mode"));
965 if (t_patts
&& (t_patts
[0] == (unsigned long) ~0)) {
966 com_err(program_name
, 0,
967 _("Random test_pattern is not allowed "
968 "in read-only mode"));
972 if (optind
> argc
- 1)
974 device_name
= argv
[optind
++];
975 if (optind
> argc
- 1) {
976 errcode
= ext2fs_get_device_size(device_name
,
979 if (errcode
== EXT2_ET_UNIMPLEMENTED
) {
980 com_err(program_name
, 0,
981 _("Couldn't determine device size; you "
982 "must specify\nthe size manually\n"));
986 com_err(program_name
, errcode
,
987 _("while trying to determine device size"));
992 last_block
= strtoul (argv
[optind
], &tmp
, 0);
993 printf("last_block = %d (%s)\n", last_block
, argv
[optind
]);
995 (last_block
== ULONG_MAX
&& errno
== ERANGE
)) {
996 com_err (program_name
, 0, _("invalid blocks count - %s"),
1003 if (optind
<= argc
-1) {
1005 from_count
= strtoul (argv
[optind
], &tmp
, 0);
1006 printf("from_count = %d\n", from_count
);
1007 if (*tmp
|| errno
||
1008 (from_count
== ULONG_MAX
&& errno
== ERANGE
)) {
1009 com_err (program_name
, 0, _("invalid starting block - %s"),
1013 } else from_count
= 0;
1014 if (from_count
>= last_block
) {
1015 com_err (program_name
, 0, _("invalid starting block (%lu): must be less than %lu"),
1016 (unsigned long) from_count
, (unsigned long) last_block
);
1020 check_mount(device_name
);
1022 open_flag
= w_flag
? O_RDWR
: O_RDONLY
;
1023 dev
= open (device_name
, open_flag
);
1025 com_err (program_name
, errno
, _("while trying to open %s"),
1029 if (host_device_name
) {
1030 host_dev
= open (host_device_name
, open_flag
);
1031 if (host_dev
== -1) {
1032 com_err (program_name
, errno
,
1033 _("while trying to open %s"),
1040 if (strcmp (input_file
, "-") == 0)
1043 in
= fopen (input_file
, "r");
1046 com_err (program_name
, errno
,
1047 _("while trying to open %s"),
1053 if (output_file
&& strcmp (output_file
, "-") != 0)
1055 out
= fopen (output_file
, "w");
1058 com_err (program_name
, errno
,
1059 _("while trying to open %s"),
1067 errcode
= ext2fs_badblocks_list_create(&bb_list
,0);
1069 com_err (program_name
, errcode
,
1070 _("while creating in-memory bad blocks list"));
1076 switch(fscanf (in
, "%u\n", &next_bad
)) {
1078 com_err (program_name
, 0, "input file - bad format");
1083 errcode
= ext2fs_badblocks_list_add(bb_list
,next_bad
);
1085 com_err (program_name
, errcode
, _("while adding to in-memory bad block list"));
1098 unsigned int bb_count
;
1100 bb_count
= test_func(dev
, last_block
, block_size
,
1101 from_count
, blocks_at_once
);
1109 _("Pass completed, %u bad blocks found.\n"),
1112 } while (passes_clean
< num_passes
);