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)
33 #define _GNU_SOURCE /* for O_DIRECT */
58 #include <sys/ioctl.h>
59 #include <sys/types.h>
61 #include "et/com_err.h"
62 #include "ext2fs/ext2_io.h"
63 #include "ext2fs/ext2_fs.h"
64 #include "ext2fs/ext2fs.h"
65 #include "support/nls-enable.h"
71 /* Maximum number of bad blocks we support */
72 #define MAX_BAD_BLOCKS (INT_MAX/2)
74 static const char * program_name
= "badblocks";
75 static const char * done_string
= N_("done \n");
77 static int v_flag
; /* verbose */
78 static int w_flag
; /* do r/w test: 0=no, 1=yes,
79 * 2=non-destructive */
80 static int s_flag
; /* show progress of test */
81 static int force
; /* force check of mounted device */
82 static int t_flag
; /* number of test patterns */
83 static int t_max
; /* allocated test patterns */
84 static unsigned int *t_patts
; /* test patterns */
85 static int use_buffered_io
;
86 static int exclusive_ok
;
87 static unsigned int max_bb
= MAX_BAD_BLOCKS
; /* Abort test if more than this
88 * number of bad blocks has been
90 static unsigned int d_flag
; /* delay factor between reads */
91 static struct timeval time_start
;
95 static unsigned int sys_page_size
= 4096;
97 static void usage(void)
100 "Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwnfBX]\n"
101 " [-c blocks_at_once] [-d delay_factor_between_reads] [-e max_bad_blocks]\n"
102 " [-p num_passes] [-t test_pattern [-t test_pattern [...]]]\n"
103 " device [last_block [first_block]]\n"),
108 static void exclusive_usage(void)
111 _("%s: The -n and -w options are mutually exclusive.\n\n"),
116 static blk_t currently_testing
= 0;
117 static blk_t num_blocks
= 0;
118 static blk_t num_read_errors
= 0;
119 static blk_t num_write_errors
= 0;
120 static blk_t num_corruption_errors
= 0;
121 static ext2_badblocks_list bb_list
= NULL
;
123 static blk_t next_bad
= 0;
124 static ext2_badblocks_iterate bb_iter
= NULL
;
126 enum error_types
{ READ_ERROR
, WRITE_ERROR
, CORRUPTION_ERROR
};
128 static void *allocate_buffer(size_t size
)
132 #ifdef HAVE_POSIX_MEMALIGN
133 if (posix_memalign(&ret
, sys_page_size
, size
) < 0)
137 ret
= memalign(sys_page_size
, size
);
141 #endif /* HAVE_VALLOC */
142 #endif /* HAVE_MEMALIGN */
143 #endif /* HAVE_POSIX_MEMALIGN */
152 * This routine reports a new bad block. If the bad block has already
153 * been seen before, then it returns 0; otherwise it returns 1.
155 static int bb_output (blk_t bad
, enum error_types error_type
)
159 if (ext2fs_badblocks_list_test(bb_list
, bad
))
162 fprintf(out
, "%lu\n", (unsigned long) bad
);
165 errcode
= ext2fs_badblocks_list_add (bb_list
, bad
);
167 com_err (program_name
, errcode
, "adding to in-memory bad block list");
172 increment the iteration through the bb_list if
173 an element was just added before the current iteration
174 position. This should not cause next_bad to change. */
175 if (bb_iter
&& bad
< next_bad
)
176 ext2fs_badblocks_list_iterate (bb_iter
, &next_bad
);
178 if (error_type
== READ_ERROR
) {
180 } else if (error_type
== WRITE_ERROR
) {
182 } else if (error_type
== CORRUPTION_ERROR
) {
183 num_corruption_errors
++;
188 static char *time_diff_format(struct timeval
*tv1
,
189 struct timeval
*tv2
, char *buf
)
191 time_t diff
= (tv1
->tv_sec
- tv2
->tv_sec
);
200 sprintf(buf
, "%d:%02d:%02d", hr
, min
, sec
);
202 sprintf(buf
, "%d:%02d", min
, sec
);
206 static float calc_percent(unsigned long current
, unsigned long total
) {
210 if (current
>= total
) {
213 percent
=(100.0*(float)current
/(float)total
);
218 static void print_status(void)
220 struct timeval time_end
;
221 char diff_buf
[32], line_buf
[128];
223 wchar_t wline_buf
[128];
227 gettimeofday(&time_end
, 0);
228 len
= snprintf(line_buf
, sizeof(line_buf
),
229 _("%6.2f%% done, %s elapsed. "
230 "(%d/%d/%d errors)"),
231 calc_percent((unsigned long) currently_testing
,
232 (unsigned long) num_blocks
),
233 time_diff_format(&time_end
, &time_start
, diff_buf
),
236 num_corruption_errors
);
238 mbstowcs(wline_buf
, line_buf
, sizeof(line_buf
));
239 len
= wcswidth(wline_buf
, sizeof(line_buf
));
241 len
= strlen(line_buf
); /* Should never happen... */
243 fputs(line_buf
, stderr
);
244 memset(line_buf
, '\b', len
);
246 fputs(line_buf
, stderr
);
250 static void alarm_intr(int alnum
EXT2FS_ATTR((unused
)))
252 signal (SIGALRM
, alarm_intr
);
259 static void *terminate_addr
= NULL
;
261 static void terminate_intr(int signo
EXT2FS_ATTR((unused
)))
264 fprintf(stderr
, "\n\nInterrupted at block %llu\n",
265 (unsigned long long) currently_testing
);
268 longjmp(terminate_addr
,1);
272 static void capture_terminate(jmp_buf term_addr
)
274 terminate_addr
= term_addr
;
275 signal (SIGHUP
, terminate_intr
);
276 signal (SIGINT
, terminate_intr
);
277 signal (SIGPIPE
, terminate_intr
);
278 signal (SIGTERM
, terminate_intr
);
279 signal (SIGUSR1
, terminate_intr
);
280 signal (SIGUSR2
, terminate_intr
);
283 static void uncapture_terminate(void)
285 terminate_addr
= NULL
;
286 signal (SIGHUP
, SIG_DFL
);
287 signal (SIGINT
, SIG_DFL
);
288 signal (SIGPIPE
, SIG_DFL
);
289 signal (SIGTERM
, SIG_DFL
);
290 signal (SIGUSR1
, SIG_DFL
);
291 signal (SIGUSR2
, SIG_DFL
);
294 /* Linux requires that O_DIRECT I/Os be 512-byte sector aligned */
296 #define O_DIRECT_SIZE 512
298 static void set_o_direct(int dev
, unsigned char *buffer
, size_t size
,
302 static int current_O_DIRECT
; /* Current status of O_DIRECT flag */
303 int new_flag
= O_DIRECT
;
306 if ((use_buffered_io
!= 0) ||
307 (((unsigned long) buffer
& (sys_page_size
- 1)) != 0) ||
308 ((size
& (sys_page_size
- 1)) != 0) ||
309 ((offset
& (O_DIRECT_SIZE
- 1)) != 0))
312 if (new_flag
!= current_O_DIRECT
) {
313 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
314 flag
= fcntl(dev
, F_GETFL
);
316 flag
= (flag
& ~O_DIRECT
) | new_flag
;
317 if (fcntl(dev
, F_SETFL
, flag
) < 0)
318 perror("set_o_direct");
320 current_O_DIRECT
= new_flag
;
326 static void pattern_fill(unsigned char *buffer
, unsigned int pattern
,
330 unsigned char bpattern
[sizeof(pattern
)], *ptr
;
332 if (pattern
== (unsigned int) ~0) {
333 for (ptr
= buffer
; ptr
< buffer
+ n
; ptr
++) {
334 (*ptr
) = random() % (1 << (8 * sizeof(char)));
337 fputs(_("Testing with random pattern: "), stderr
);
340 for (i
= 0; i
< sizeof(bpattern
); i
++) {
343 bpattern
[i
] = pattern
& 0xFF;
344 pattern
= pattern
>> 8;
347 for (ptr
= buffer
, i
= nb
; ptr
< buffer
+ n
; ptr
++) {
354 if (s_flag
| v_flag
) {
355 fputs(_("Testing with pattern 0x"), stderr
);
356 for (i
= 0; i
<= nb
; i
++)
357 fprintf(stderr
, "%02x", buffer
[i
]);
364 * Perform a read of a sequence of blocks; return the number of blocks
365 * successfully sequentially read.
367 static int do_read (int dev
, unsigned char * buffer
, int try, int block_size
,
371 struct timeval tv1
, tv2
;
372 #define NANOSEC (1000000000L)
373 #define MILISEC (1000L)
376 printf("do_read: block %d, try %d\n", current_block
, try);
378 set_o_direct(dev
, buffer
, try * block_size
,
379 ((ext2_loff_t
) current_block
) * block_size
);
384 /* Seek to the correct loc. */
385 if (ext2fs_llseek (dev
, (ext2_loff_t
) current_block
* block_size
,
386 SEEK_SET
) != (ext2_loff_t
) current_block
* block_size
)
387 com_err (program_name
, errno
, "%s", _("during seek"));
391 gettimeofday(&tv1
, NULL
);
392 got
= read (dev
, buffer
, try * block_size
);
394 gettimeofday(&tv2
, NULL
);
398 fprintf(stderr
, _("Weird value (%ld) in do_read\n"), got
);
400 if (d_flag
&& got
== try) {
401 #ifdef HAVE_NANOSLEEP
403 ts
.tv_sec
= tv2
.tv_sec
- tv1
.tv_sec
;
404 ts
.tv_nsec
= (tv2
.tv_usec
- tv1
.tv_usec
) * MILISEC
;
405 if (ts
.tv_nsec
< 0) {
406 ts
.tv_nsec
+= NANOSEC
;
409 /* increase/decrease the sleep time based on d_flag value */
410 ts
.tv_sec
= ts
.tv_sec
* d_flag
/ 100;
411 ts
.tv_nsec
= ts
.tv_nsec
* d_flag
/ 100;
412 if (ts
.tv_nsec
> NANOSEC
) {
413 ts
.tv_sec
+= ts
.tv_nsec
/ NANOSEC
;
414 ts
.tv_nsec
%= NANOSEC
;
416 if (ts
.tv_sec
|| ts
.tv_nsec
)
417 nanosleep(&ts
, NULL
);
421 tv
.tv_sec
= tv2
.tv_sec
- tv1
.tv_sec
;
422 tv
.tv_usec
= tv2
.tv_usec
- tv1
.tv_usec
;
423 tv
.tv_sec
= tv
.tv_sec
* d_flag
/ 100;
424 tv
.tv_usec
= tv
.tv_usec
* d_flag
/ 100;
425 if (tv
.tv_usec
> 1000000) {
426 tv
.tv_sec
+= tv
.tv_usec
/ 1000000;
427 tv
.tv_usec
%= 1000000;
440 * Perform a write of a sequence of blocks; return the number of blocks
441 * successfully sequentially written.
443 static int do_write(int dev
, unsigned char * buffer
, int try, int block_size
,
444 unsigned long current_block
)
449 printf("do_write: block %lu, try %d\n", current_block
, try);
451 set_o_direct(dev
, buffer
, try * block_size
,
452 ((ext2_loff_t
) current_block
) * block_size
);
457 /* Seek to the correct loc. */
458 if (ext2fs_llseek (dev
, (ext2_loff_t
) current_block
* block_size
,
459 SEEK_SET
) != (ext2_loff_t
) current_block
* block_size
)
460 com_err (program_name
, errno
, "%s", _("during seek"));
463 got
= write (dev
, buffer
, try * block_size
);
467 fprintf(stderr
, "Weird value (%ld) in do_write\n", got
);
474 static void flush_bufs(void)
479 if (!use_buffered_io
)
482 retval
= ext2fs_sync_device(host_dev
, 1);
484 com_err(program_name
, retval
, "%s",
485 _("during ext2fs_sync_device"));
488 static unsigned int test_ro (int dev
, blk_t last_block
,
489 int block_size
, blk_t first_block
,
490 unsigned int blocks_at_once
)
492 unsigned char * blkbuf
;
495 unsigned int bb_count
= 0;
497 blk_t recover_block
= ~0;
499 /* set up abend handler */
500 capture_terminate(NULL
);
502 errcode
= ext2fs_badblocks_list_iterate_begin(bb_list
,&bb_iter
);
504 com_err(program_name
, errcode
, "%s",
505 _("while beginning bad block list iteration"));
509 ext2fs_badblocks_list_iterate (bb_iter
, &next_bad
);
510 } while (next_bad
&& next_bad
< first_block
);
513 blkbuf
= allocate_buffer((blocks_at_once
+ 1) * block_size
);
515 blkbuf
= allocate_buffer(blocks_at_once
* block_size
);
519 com_err(program_name
, ENOMEM
, "%s",
520 _("while allocating buffers"));
524 fprintf(stderr
, _("Checking blocks %lu to %lu\n"),
525 (unsigned long)first_block
,
526 (unsigned long)last_block
- 1);
529 fputs(_("Checking for bad blocks in read-only mode\n"), stderr
);
530 pattern_fill(blkbuf
+ blocks_at_once
* block_size
,
531 t_patts
[0], block_size
);
534 try = blocks_at_once
;
535 currently_testing
= first_block
;
536 num_blocks
= last_block
- 1;
537 if (!t_flag
&& (s_flag
|| v_flag
))
538 fputs(_("Checking for bad blocks (read-only test): "), stderr
);
539 if (s_flag
&& v_flag
<= 1)
541 while (currently_testing
< last_block
)
543 if (bb_count
>= max_bb
) {
544 if (s_flag
|| v_flag
) {
545 fputs(_("Too many bad blocks, aborting test\n"), stderr
);
550 if (currently_testing
== next_bad
) {
551 /* fprintf (out, "%lu\n", nextbad); */
552 ext2fs_badblocks_list_iterate (bb_iter
, &next_bad
);
556 else if (currently_testing
+ try > next_bad
)
557 try = next_bad
- currently_testing
;
559 if (currently_testing
+ try > last_block
)
560 try = last_block
- currently_testing
;
561 got
= do_read (dev
, blkbuf
, try, block_size
, currently_testing
);
563 /* test the comparison between all the
564 blocks successfully read */
566 for (i
= 0; i
< got
; ++i
)
567 if (memcmp (blkbuf
+i
*block_size
,
568 blkbuf
+blocks_at_once
*block_size
,
570 bb_count
+= bb_output(currently_testing
+ i
, CORRUPTION_ERROR
);
572 if (got
== 0 && try == 1)
573 bb_count
+= bb_output(currently_testing
++, READ_ERROR
);
574 currently_testing
+= got
;
577 if (recover_block
== ~0U)
578 recover_block
= currently_testing
- got
+
581 } else if (currently_testing
== recover_block
) {
582 try = blocks_at_once
;
588 if (s_flag
|| v_flag
)
589 fputs(_(done_string
), stderr
);
594 ext2fs_badblocks_list_iterate_end(bb_iter
);
596 uncapture_terminate();
601 static unsigned int test_rw (int dev
, blk_t last_block
,
602 int block_size
, blk_t first_block
,
603 unsigned int blocks_at_once
)
605 unsigned char *buffer
, *read_buffer
;
606 const unsigned int patterns
[] = {0xaa, 0x55, 0xff, 0x00};
607 const unsigned int *pattern
;
608 int i
, try, got
, nr_pattern
, pat_idx
;
609 unsigned int bb_count
= 0;
610 blk_t recover_block
= ~0;
612 /* set up abend handler */
613 capture_terminate(NULL
);
615 buffer
= allocate_buffer(2 * blocks_at_once
* block_size
);
616 read_buffer
= buffer
+ blocks_at_once
* block_size
;
619 com_err(program_name
, ENOMEM
, "%s",
620 _("while allocating buffers"));
627 fputs(_("Checking for bad blocks in read-write mode\n"),
629 fprintf(stderr
, _("From block %lu to %lu\n"),
630 (unsigned long) first_block
,
631 (unsigned long) last_block
- 1);
638 nr_pattern
= sizeof(patterns
) / sizeof(patterns
[0]);
640 for (pat_idx
= 0; pat_idx
< nr_pattern
; pat_idx
++) {
641 pattern_fill(buffer
, pattern
[pat_idx
],
642 blocks_at_once
* block_size
);
643 num_blocks
= last_block
- 1;
644 currently_testing
= first_block
;
645 if (s_flag
&& v_flag
<= 1)
648 try = blocks_at_once
;
649 while (currently_testing
< last_block
) {
650 if (bb_count
>= max_bb
) {
651 if (s_flag
|| v_flag
) {
652 fputs(_("Too many bad blocks, aborting test\n"), stderr
);
656 if (currently_testing
+ try > last_block
)
657 try = last_block
- currently_testing
;
658 got
= do_write(dev
, buffer
, try, block_size
,
663 if (got
== 0 && try == 1)
664 bb_count
+= bb_output(currently_testing
++, WRITE_ERROR
);
665 currently_testing
+= got
;
668 if (recover_block
== ~0U)
669 recover_block
= currently_testing
-
670 got
+ blocks_at_once
;
672 } else if (currently_testing
== recover_block
) {
673 try = blocks_at_once
;
681 fputs(_(done_string
), stderr
);
684 fputs(_("Reading and comparing: "), stderr
);
685 num_blocks
= last_block
;
686 currently_testing
= first_block
;
687 if (s_flag
&& v_flag
<= 1)
690 try = blocks_at_once
;
691 while (currently_testing
< last_block
) {
692 if (bb_count
>= max_bb
) {
693 if (s_flag
|| v_flag
) {
694 fputs(_("Too many bad blocks, aborting test\n"), stderr
);
698 if (currently_testing
+ try > last_block
)
699 try = last_block
- currently_testing
;
700 got
= do_read (dev
, read_buffer
, try, block_size
,
702 if (got
== 0 && try == 1)
703 bb_count
+= bb_output(currently_testing
++, READ_ERROR
);
704 currently_testing
+= got
;
707 if (recover_block
== ~0U)
708 recover_block
= currently_testing
-
709 got
+ blocks_at_once
;
711 } else if (currently_testing
== recover_block
) {
712 try = blocks_at_once
;
715 for (i
=0; i
< got
; i
++) {
716 if (memcmp(read_buffer
+ i
* block_size
,
717 buffer
+ i
* block_size
,
719 bb_count
+= bb_output(currently_testing
+i
, CORRUPTION_ERROR
);
728 fputs(_(done_string
), stderr
);
731 uncapture_terminate();
736 struct saved_blk_record
{
741 static unsigned int test_nd (int dev
, blk_t last_block
,
742 int block_size
, blk_t first_block
,
743 unsigned int blocks_at_once
)
745 unsigned char *blkbuf
, *save_ptr
, *test_ptr
, *read_ptr
;
746 unsigned char *test_base
, *save_base
, *read_base
;
748 const unsigned int patterns
[] = { ~0 };
749 const unsigned int *pattern
;
750 int nr_pattern
, pat_idx
;
751 int got
, used2
, written
;
752 blk_t save_currently_testing
;
753 struct saved_blk_record
*test_record
;
754 /* This is static to prevent being clobbered by the longjmp */
755 static int num_saved
;
756 jmp_buf terminate_env
;
758 unsigned long buf_used
;
759 static unsigned int bb_count
;
760 unsigned int granularity
= blocks_at_once
;
761 blk_t recover_block
= ~0U;
764 errcode
= ext2fs_badblocks_list_iterate_begin(bb_list
,&bb_iter
);
766 com_err(program_name
, errcode
, "%s",
767 _("while beginning bad block list iteration"));
771 ext2fs_badblocks_list_iterate (bb_iter
, &next_bad
);
772 } while (next_bad
&& next_bad
< first_block
);
774 blkbuf
= allocate_buffer(3 * blocks_at_once
* block_size
);
775 test_record
= malloc(blocks_at_once
* sizeof(struct saved_blk_record
));
776 if (!blkbuf
|| !test_record
) {
777 com_err(program_name
, ENOMEM
, "%s",
778 _("while allocating buffers"));
783 test_base
= blkbuf
+ (blocks_at_once
* block_size
);
784 read_base
= blkbuf
+ (2 * blocks_at_once
* block_size
);
790 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr
);
791 fprintf (stderr
, _("From block %lu to %lu\n"),
792 (unsigned long) first_block
,
793 (unsigned long) last_block
- 1);
795 if (s_flag
|| v_flag
> 1) {
796 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr
);
798 if (setjmp(terminate_env
)) {
800 * Abnormal termination by a signal is handled here.
802 signal (SIGALRM
, SIG_IGN
);
803 fputs(_("\nInterrupt caught, cleaning up\n"), stderr
);
805 save_ptr
= save_base
;
806 for (i
=0; i
< num_saved
; i
++) {
807 do_write(dev
, save_ptr
, test_record
[i
].num
,
808 block_size
, test_record
[i
].block
);
809 save_ptr
+= test_record
[i
].num
* block_size
;
815 /* set up abend handler */
816 capture_terminate(terminate_env
);
823 nr_pattern
= sizeof(patterns
) / sizeof(patterns
[0]);
825 for (pat_idx
= 0; pat_idx
< nr_pattern
; pat_idx
++) {
826 pattern_fill(test_base
, pattern
[pat_idx
],
827 blocks_at_once
* block_size
);
831 save_ptr
= save_base
;
832 test_ptr
= test_base
;
833 currently_testing
= first_block
;
834 num_blocks
= last_block
- 1;
835 if (s_flag
&& v_flag
<= 1)
838 while (currently_testing
< last_block
) {
839 if (bb_count
>= max_bb
) {
840 if (s_flag
|| v_flag
) {
841 fputs(_("Too many bad blocks, aborting test\n"), stderr
);
845 got
= try = granularity
- buf_used
;
847 if (currently_testing
== next_bad
) {
848 /* fprintf (out, "%lu\n", nextbad); */
849 ext2fs_badblocks_list_iterate (bb_iter
, &next_bad
);
853 else if (currently_testing
+ try > next_bad
)
854 try = next_bad
- currently_testing
;
856 if (currently_testing
+ try > last_block
)
857 try = last_block
- currently_testing
;
858 got
= do_read (dev
, save_ptr
, try, block_size
,
861 if (recover_block
== ~0U)
862 recover_block
= currently_testing
+
864 if (granularity
!= 1) {
868 /* First block must have been bad. */
869 bb_count
+= bb_output(currently_testing
++, READ_ERROR
);
874 * Note the fact that we've saved this much data
875 * *before* we overwrite it with test data
877 test_record
[num_saved
].block
= currently_testing
;
878 test_record
[num_saved
].num
= got
;
881 /* Write the test data */
882 written
= do_write (dev
, test_ptr
, got
, block_size
,
885 com_err (program_name
, errno
,
886 _("during test data write, block %lu"),
887 (unsigned long) currently_testing
+
891 save_ptr
+= got
* block_size
;
892 test_ptr
+= got
* block_size
;
893 currently_testing
+= got
;
896 if (recover_block
== ~0U)
897 recover_block
= currently_testing
-
898 got
+ blocks_at_once
;
904 * If there's room for more blocks to be tested this
905 * around, and we're not done yet testing the disk, go
906 * back and get some more blocks.
908 if ((buf_used
!= granularity
) &&
909 (currently_testing
< last_block
))
912 if (currently_testing
>= recover_block
) {
913 granularity
= blocks_at_once
;
918 save_currently_testing
= currently_testing
;
921 * for each contiguous block that we read into the
922 * buffer (and wrote test data into afterwards), read
923 * it back (looping if necessary, to get past newly
924 * discovered unreadable blocks, of which there should
925 * be none, but with a hard drive which is unreliable,
926 * it has happened), and compare with the test data
927 * that was written; output to the bad block list if
931 save_ptr
= save_base
;
932 test_ptr
= test_base
;
933 read_ptr
= read_base
;
938 if (used2
>= num_saved
)
940 currently_testing
= test_record
[used2
].block
;
941 try = test_record
[used2
].num
;
945 got
= do_read (dev
, read_ptr
, try,
946 block_size
, currently_testing
);
948 /* test the comparison between all the
949 blocks successfully read */
950 for (i
= 0; i
< got
; ++i
)
951 if (memcmp (test_ptr
+i
*block_size
,
952 read_ptr
+i
*block_size
, block_size
))
953 bb_count
+= bb_output(currently_testing
+ i
, CORRUPTION_ERROR
);
955 bb_count
+= bb_output(currently_testing
+ got
, READ_ERROR
);
959 /* write back original data */
960 do_write (dev
, save_ptr
, got
,
961 block_size
, currently_testing
);
962 save_ptr
+= got
* block_size
;
964 currently_testing
+= got
;
965 test_ptr
+= got
* block_size
;
966 read_ptr
+= got
* block_size
;
970 /* empty the buffer so it can be reused */
973 save_ptr
= save_base
;
974 test_ptr
= test_base
;
975 currently_testing
= save_currently_testing
;
979 if (s_flag
|| v_flag
> 1)
980 fputs(_(done_string
), stderr
);
984 uncapture_terminate();
989 ext2fs_badblocks_list_iterate_end(bb_iter
);
994 static void check_mount(char *device_name
)
999 retval
= ext2fs_check_if_mounted(device_name
, &mount_flags
);
1001 com_err("ext2fs_check_if_mount", retval
,
1002 _("while determining whether %s is mounted."),
1006 if (mount_flags
& EXT2_MF_MOUNTED
) {
1007 fprintf(stderr
, _("%s is mounted; "), device_name
);
1009 fputs(_("badblocks forced anyway. "
1010 "Hope /etc/mtab is incorrect.\n"), stderr
);
1014 fputs(_("it's not safe to run badblocks!\n"), stderr
);
1018 if ((mount_flags
& EXT2_MF_BUSY
) && !exclusive_ok
) {
1019 fprintf(stderr
, _("%s is apparently in use by the system; "),
1022 fputs(_("badblocks forced anyway.\n"), stderr
);
1024 goto abort_badblocks
;
1030 * This function will convert a string to an unsigned long, printing
1031 * an error message if it fails, and returning success or failure in err.
1033 static unsigned int parse_uint(const char *str
, const char *descr
)
1039 ret
= strtoul(str
, &tmp
, 0);
1040 if (*tmp
|| errno
|| (ret
> UINT_MAX
) ||
1041 (ret
== ULONG_MAX
&& errno
== ERANGE
)) {
1042 com_err (program_name
, 0, _("invalid %s - %s"), descr
, str
);
1048 int main (int argc
, char ** argv
)
1052 char * host_device_name
= NULL
;
1053 char * input_file
= NULL
;
1054 char * output_file
= NULL
;
1056 int block_size
= 1024;
1057 unsigned int blocks_at_once
= 64;
1058 blk64_t last_block
, first_block
;
1060 int passes_clean
= 0;
1063 unsigned int pattern
;
1064 unsigned int (*test_func
)(int, blk_t
,
1071 setbuf(stdout
, NULL
);
1072 setbuf(stderr
, NULL
);
1074 setlocale(LC_MESSAGES
, "");
1075 setlocale(LC_CTYPE
, "");
1076 bindtextdomain(NLS_CAT_NAME
, LOCALEDIR
);
1077 textdomain(NLS_CAT_NAME
);
1078 set_com_err_gettext(gettext
);
1080 srandom((unsigned int)time(NULL
)); /* simple randomness is enough */
1081 test_func
= test_ro
;
1083 /* Determine the system page size if possible */
1085 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1086 #define _SC_PAGESIZE _SC_PAGE_SIZE
1089 sysval
= sysconf(_SC_PAGESIZE
);
1091 sys_page_size
= sysval
;
1092 #endif /* _SC_PAGESIZE */
1093 #endif /* HAVE_SYSCONF */
1096 program_name
= *argv
;
1097 while ((c
= getopt (argc
, argv
, "b:d:e:fi:o:svwnc:p:h:t:BX")) != EOF
) {
1100 block_size
= parse_uint(optarg
, "block size");
1106 input_file
= optarg
;
1109 output_file
= optarg
;
1120 test_func
= test_rw
;
1126 test_func
= test_nd
;
1130 blocks_at_once
= parse_uint(optarg
, "blocks at once");
1133 max_bb
= parse_uint(optarg
, "max bad block count");
1134 if (max_bb
> MAX_BAD_BLOCKS
) {
1135 com_err (program_name
, 0,
1136 _("Too big max bad blocks count %u - "
1137 "maximum is %u"), max_bb
,
1141 /* 0 really means unlimited but we cannot do that much... */
1143 max_bb
= MAX_BAD_BLOCKS
;
1146 d_flag
= parse_uint(optarg
, "read delay factor");
1149 num_passes
= parse_uint(optarg
,
1150 "number of clean passes");
1153 host_device_name
= optarg
;
1156 if (t_flag
+ 1 > t_max
) {
1157 unsigned int *t_patts_new
;
1159 t_patts_new
= realloc(t_patts
, sizeof(int) *
1162 com_err(program_name
, ENOMEM
,
1163 _("can't allocate memory for "
1164 "test_pattern - %s"),
1168 t_patts
= t_patts_new
;
1171 if (!strcmp(optarg
, "r") || !strcmp(optarg
,"random")) {
1172 t_patts
[t_flag
++] = ~0;
1174 pattern
= parse_uint(optarg
, "test pattern");
1175 if (pattern
== (unsigned int) ~0)
1177 t_patts
[t_flag
++] = pattern
;
1181 use_buffered_io
= 1;
1192 com_err(program_name
, 0, "%s",
1193 _("Maximum of one test_pattern may be "
1194 "specified in read-only mode"));
1197 if (t_patts
&& (t_patts
[0] == (unsigned int) ~0)) {
1198 com_err(program_name
, 0, "%s",
1199 _("Random test_pattern is not allowed "
1200 "in read-only mode"));
1204 if (optind
> argc
- 1)
1206 device_name
= argv
[optind
++];
1207 if (optind
> argc
- 1) {
1208 errcode
= ext2fs_get_device_size2(device_name
,
1211 if (errcode
== EXT2_ET_UNIMPLEMENTED
) {
1212 com_err(program_name
, 0, "%s",
1213 _("Couldn't determine device size; you "
1214 "must specify\nthe size manually\n"));
1218 com_err(program_name
, errcode
, "%s",
1219 _("while trying to determine device size"));
1224 last_block
= parse_uint(argv
[optind
], _("last block"));
1228 if (optind
<= argc
-1) {
1230 first_block
= parse_uint(argv
[optind
], _("first block"));
1231 } else first_block
= 0;
1232 if (first_block
>= last_block
) {
1233 com_err (program_name
, 0, _("invalid starting block (%llu): must be less than %llu"),
1234 first_block
, last_block
);
1237 /* ext2 badblocks file can't handle large values */
1238 if (last_block
>> 32) {
1239 com_err(program_name
, EOVERFLOW
,
1240 _("invalid end block (%llu): must be 32-bit value"),
1245 check_mount(device_name
);
1247 gettimeofday(&time_start
, 0);
1248 open_flag
= O_LARGEFILE
| (w_flag
? O_RDWR
: O_RDONLY
);
1249 dev
= open (device_name
, open_flag
);
1251 com_err (program_name
, errno
, _("while trying to open %s"),
1255 if (host_device_name
) {
1256 host_dev
= open (host_device_name
, open_flag
);
1257 if (host_dev
== -1) {
1258 com_err (program_name
, errno
,
1259 _("while trying to open %s"),
1266 if (strcmp (input_file
, "-") == 0)
1269 in
= fopen (input_file
, "r");
1272 com_err (program_name
, errno
,
1273 _("while trying to open %s"),
1279 if (output_file
&& strcmp (output_file
, "-") != 0)
1281 out
= fopen (output_file
, "w");
1284 com_err (program_name
, errno
,
1285 _("while trying to open %s"),
1293 errcode
= ext2fs_badblocks_list_create(&bb_list
,0);
1295 com_err(program_name
, errcode
, "%s",
1296 _("while creating in-memory bad blocks list"));
1302 switch (fscanf(in
, "%llu\n", &inblk
)) {
1304 com_err(program_name
, 0, "%s",
1305 _("input file - bad format"));
1311 com_err(program_name
,
1313 _("while adding to in-memory "
1318 errcode
= ext2fs_badblocks_list_add(bb_list
,next_bad
);
1320 com_err(program_name
, errcode
,
1322 _("while adding to in-memory "
1336 unsigned int bb_count
;
1338 bb_count
= test_func(dev
, last_block
, block_size
,
1339 first_block
, blocks_at_once
);
1347 _("Pass completed, %u bad blocks found. (%d/%d/%d errors)\n"),
1348 bb_count
, num_read_errors
, num_write_errors
, num_corruption_errors
);
1350 } while (passes_clean
< num_passes
);