Whamcloud - gitweb
Fixx gcc -Wall nitpicks.
[tools/e2fsprogs.git] / misc / badblocks.c
1 /*
2  * badblocks.c          - Bad blocks checker
3  *
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)
7  *
8  * Copyright 1995, 1996, 1997, 1998, 1999 by Theodore Ts'o
9  * Copyright 1999 by David Beattie
10  *
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>
13  * 
14  * %Begin-Header%
15  * This file may be redistributed under the terms of the GNU Public
16  * License.
17  * %End-Header%
18  */
19
20 /*
21  * History:
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)
30  */
31
32 #include <errno.h>
33 #include <fcntl.h>
34 #ifdef HAVE_GETOPT_H
35 #include <getopt.h>
36 #else
37 extern char *optarg;
38 extern int optind;
39 #endif
40 #include <signal.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <setjmp.h>
46 #include <time.h>
47
48 #include <sys/ioctl.h>
49 #include <sys/types.h>
50
51 #include "et/com_err.h"
52 #include "ext2fs/ext2_io.h"
53 #include "ext2fs/ext2_fs.h"
54 #include "ext2fs/ext2fs.h"
55 #include "nls-enable.h"
56
57 const char * program_name = "badblocks";
58 const char * done_string = N_("done                        \n");
59
60 static int v_flag = 0;                  /* verbose */
61 static int w_flag = 0;                  /* do r/w test: 0=no, 1=yes,
62                                          * 2=non-destructive */
63 static int s_flag = 0;                  /* show progress of test */
64 static int force = 0;                   /* force check of mounted device */
65 static int t_flag = 0;                  /* number of test patterns */
66 static int t_max = 0;                   /* allocated test patterns */
67 static unsigned long *t_patts = NULL;   /* test patterns */
68 #define T_INC 32
69
70 static void usage(void)
71 {
72         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"),
73                  program_name);
74         exit (1);
75 }
76
77 static unsigned long currently_testing = 0;
78 static unsigned long num_blocks = 0;
79 static ext2_badblocks_list bb_list = NULL;
80 static FILE *out;
81 static blk_t next_bad = 0;
82 static ext2_badblocks_iterate bb_iter = NULL;
83
84 /*
85  * This routine reports a new bad block.  If the bad block has already
86  * been seen before, then it returns 0; otherwise it returns 1.
87  */
88 static int bb_output (unsigned long bad)
89 {
90         errcode_t errcode;
91
92         if (ext2fs_badblocks_list_test(bb_list, bad))
93                 return 0;
94
95         fprintf(out, "%lu\n", bad);
96         fflush(out);
97
98         errcode = ext2fs_badblocks_list_add (bb_list, bad);
99         if (errcode) {
100                 com_err (program_name, errcode, "adding to in-memory bad block list");
101                 exit (1);
102         }
103
104         /* kludge:
105            increment the iteration through the bb_list if 
106            an element was just added before the current iteration
107            position.  This should not cause next_bad to change. */
108         if (bb_iter && bad < next_bad)
109                 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
110         return 1;
111 }
112
113 static void print_status(void)
114 {
115         fprintf(stderr, "%9ld/%9ld", currently_testing, num_blocks);
116         fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
117         fflush (stderr);
118 }
119
120 static void alarm_intr(int alnum)
121 {
122         signal (SIGALRM, alarm_intr);
123         alarm(1);
124         if (!num_blocks)
125                 return;
126         fprintf(stderr, "%9ld/%9ld", currently_testing, num_blocks);
127         fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
128         fflush (stderr);
129 }
130
131 static void *terminate_addr = NULL;
132
133 static void terminate_intr(int signo)
134 {
135         if (terminate_addr)
136                 longjmp(terminate_addr,1);
137         exit(1);
138 }
139
140 static void capture_terminate(jmp_buf term_addr)
141 {
142         terminate_addr = term_addr;
143         signal (SIGHUP, terminate_intr);
144         signal (SIGINT, terminate_intr);
145         signal (SIGPIPE, terminate_intr);
146         signal (SIGTERM, terminate_intr);
147         signal (SIGUSR1, terminate_intr);
148         signal (SIGUSR2, terminate_intr);
149 }
150
151 static void uncapture_terminate(void)
152 {
153         terminate_addr = NULL;
154         signal (SIGHUP, SIG_DFL);
155         signal (SIGINT, SIG_DFL);
156         signal (SIGPIPE, SIG_DFL);
157         signal (SIGTERM, SIG_DFL);
158         signal (SIGUSR1, SIG_DFL);
159         signal (SIGUSR2, SIG_DFL);
160 }
161
162 static void pattern_fill(unsigned char *buffer, unsigned long pattern,
163                          size_t n)
164 {
165         int     i, nb;
166         unsigned char   bpattern[sizeof(pattern)], *ptr;
167         
168         if (pattern == ~0) {
169                 for (ptr = buffer; ptr < buffer + n; ptr++) {
170                         (*ptr) = random() % (1 << (8 * sizeof(char)));
171                 }
172                 if (s_flag | v_flag)
173                         fprintf(stderr, _("Testing with random pattern: "));
174         } else {
175                 bpattern[0] = 0;
176                 for (i = 0; i < sizeof(bpattern); i++) {
177                         if (pattern == 0)
178                                 break;
179                         bpattern[i] = pattern & 0xFF;
180                         pattern = pattern >> 8;
181                 }
182                 nb = i ? (i-1) : 0;
183                 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
184                         *ptr = bpattern[i--];
185                         if (i < 0)
186                                 i = nb;
187                 }
188                 if (s_flag | v_flag) {
189                         fprintf(stderr, _("Testing with pattern 0x"));
190                         for (i = 0; i <= nb; i++)
191                                 fprintf(stderr, "%02x", buffer[i]);
192                         fprintf(stderr, ": ");
193                 }
194         }
195 }
196
197 /*
198  * Perform a read of a sequence of blocks; return the number of blocks
199  *    successfully sequentially read.
200  */
201 static long do_read (int dev, unsigned char * buffer, int try, int block_size,
202                      unsigned long current_block)
203 {
204         long got;
205
206         if (v_flag > 1)
207                 print_status();
208
209         /* Seek to the correct loc. */
210         if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
211                          SEEK_SET) != (ext2_loff_t) current_block * block_size)
212                 com_err (program_name, errno, _("during seek"));
213
214         /* Try the read */
215         got = read (dev, buffer, try * block_size);
216         if (got < 0)
217                 got = 0;        
218         if (got & 511)
219                 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
220         got /= block_size;
221         return got;
222 }
223
224 /*
225  * Perform a write of a sequence of blocks; return the number of blocks
226  *    successfully sequentially written.
227  */
228 static long do_write (int dev, unsigned char * buffer, int try, int block_size,
229                      unsigned long current_block)
230 {
231         long got;
232
233         if (v_flag > 1)
234                 print_status();
235
236         /* Seek to the correct loc. */
237         if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
238                          SEEK_SET) != (ext2_loff_t) current_block * block_size)
239                 com_err (program_name, errno, _("during seek"));
240
241         /* Try the write */
242         got = write (dev, buffer, try * block_size);
243         if (got < 0)
244                 got = 0;        
245         if (got & 511)
246                 fprintf (stderr,
247                          "Weird value (%ld) in do_write\n", got);
248         got /= block_size;
249         return got;
250 }
251
252 static int host_dev;
253
254 static void flush_bufs(void)
255 {
256         errcode_t       retval;
257
258         retval = ext2fs_sync_device(host_dev, 1);
259         if (retval)
260                 com_err(program_name, retval, _("during ext2fs_sync_device"));
261 }
262
263 static unsigned int test_ro (int dev, unsigned long last_block,
264                              int block_size, unsigned long from_count,
265                              unsigned long blocks_at_once)
266 {
267         unsigned char * blkbuf;
268         int try;
269         long got;
270         unsigned int bb_count = 0;
271         errcode_t errcode;
272
273         errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
274         if (errcode) {
275                 com_err (program_name, errcode,
276                          _("while beginning bad block list iteration"));
277                 exit (1);
278         }
279         do {
280                 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
281         } while (next_bad && next_bad < from_count);
282
283         if (t_flag) {
284                 blkbuf = malloc ((blocks_at_once + 1) * block_size);
285         } else {
286                 blkbuf = malloc (blocks_at_once * block_size);
287         }
288         if (!blkbuf)
289         {
290                 com_err (program_name, ENOMEM, _("while allocating buffers"));
291                 exit (1);
292         }
293         if (v_flag) {
294             fprintf (stderr, _("Checking blocks %lu to %lu\n"), from_count,
295                      last_block);
296         }
297         if (t_flag) {
298                 fprintf(stderr, _("Checking for bad blocks in read-only mode\n"));
299                 pattern_fill(blkbuf + blocks_at_once * block_size,
300                              t_patts[0], block_size);
301         }
302         flush_bufs();
303         try = blocks_at_once;
304         currently_testing = from_count;
305         num_blocks = last_block;
306         if (!t_flag && (s_flag || v_flag)) {
307                 fprintf(stderr,
308                         _("Checking for bad blocks (read-only test): "));
309                 if (v_flag <= 1)
310                         alarm_intr(SIGALRM);
311         }
312         while (currently_testing < last_block)
313         {
314                 if (next_bad) {
315                         if (currently_testing == next_bad) {
316                                 /* fprintf (out, "%lu\n", nextbad); */
317                                 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
318                                 currently_testing++;
319                                 continue;
320                         }
321                         else if (currently_testing + try > next_bad)
322                                 try = next_bad - currently_testing;
323                 }
324                 if (currently_testing + try > last_block)
325                         try = last_block - currently_testing;
326                 got = do_read (dev, blkbuf, try, block_size, currently_testing);
327                 if (t_flag) {
328                         /* test the comparison between all the
329                            blocks successfully read  */
330                         int i;
331                         for (i = 0; i < got; ++i)
332                                 if (memcmp (blkbuf+i*block_size,
333                                             blkbuf+blocks_at_once*block_size,
334                                             block_size))
335                                         bb_count += bb_output(currently_testing + i);
336                 }
337                 currently_testing += got;
338                 if (got == try) {
339                         try = blocks_at_once;
340                         continue;
341                 }
342                 else
343                         try = 1;
344                 if (got == 0) {
345                         bb_count += bb_output(currently_testing++);
346                 }
347         }
348         num_blocks = 0;
349         alarm(0);
350         if (s_flag || v_flag)
351                 fputs(done_string, stderr);
352
353         fflush (stderr);
354         free (blkbuf);
355
356         ext2fs_badblocks_list_iterate_end(bb_iter);
357
358         return bb_count;
359 }
360
361 static unsigned int test_rw (int dev, unsigned long last_block,
362                              int block_size, unsigned long from_count,
363                              unsigned long blocks_at_once)
364 {
365         unsigned char * buffer;
366         const unsigned long patterns[] = {0xaa, 0x55, 0xff, 0x00};
367         const unsigned long *pattern;
368         int nr_pattern, pat_idx;
369         unsigned int bb_count = 0;
370
371         buffer = malloc (2 * block_size);
372         if (!buffer)
373         {
374                 com_err (program_name, ENOMEM, _("while allocating buffers"));
375                 exit (1);
376         }
377
378         flush_bufs();
379
380         if (v_flag) {
381                 fprintf(stderr,
382                         _("Checking for bad blocks in read-write mode\n"));
383                 fprintf(stderr, _("From block %lu to %lu\n"),
384                          from_count, last_block);
385         }
386         if (t_flag) {
387                 pattern = t_patts;
388                 nr_pattern = t_flag;
389         } else {
390                 pattern = patterns;
391                 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
392         }
393         for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
394                 pattern_fill(buffer, pattern[pat_idx], block_size);
395                 num_blocks = last_block;
396                 currently_testing = from_count;
397                 if (s_flag && v_flag <= 1)
398                         alarm_intr(SIGALRM);
399                 for (;
400                      currently_testing < last_block;
401                      currently_testing++)
402                 {
403                         if (ext2fs_llseek (dev, (ext2_loff_t) currently_testing *
404                                          block_size, SEEK_SET) !=
405                             (ext2_loff_t) currently_testing * block_size)
406                                 com_err (program_name, errno,
407                                          _("during seek on block %d"),
408                                          currently_testing);
409                         if (v_flag > 1)
410                                 print_status();
411                         write (dev, buffer, block_size);
412                 }
413                 num_blocks = 0;
414                 alarm (0);
415                 if (s_flag | v_flag)
416                         fputs(done_string, stderr);
417                 flush_bufs();
418                 if (s_flag | v_flag)
419                         fprintf (stderr, _("Reading and comparing: "));
420                 num_blocks = last_block;
421                 currently_testing = from_count;
422                 if (s_flag && v_flag <= 1)
423                         alarm_intr(SIGALRM);
424                 for (;
425                      currently_testing < last_block;
426                      currently_testing++)
427                 {
428                         if (ext2fs_llseek (dev, (ext2_loff_t) currently_testing *
429                                          block_size, SEEK_SET) !=
430                             (ext2_loff_t) currently_testing * block_size)
431                                 com_err (program_name, errno,
432                                          _("during seek on block %d"),
433                                          currently_testing);
434                         if (v_flag > 1)
435                                 print_status();
436                         if ((read (dev, buffer + block_size, block_size) 
437                              != block_size) ||
438                             memcmp(buffer, buffer + block_size, block_size))
439                                 bb_count += bb_output(currently_testing);
440                 }
441                 num_blocks = 0;
442                 alarm (0);
443                 if (s_flag | v_flag)
444                         fputs(done_string, stderr);
445                 flush_bufs();
446         }
447         uncapture_terminate();
448         free(buffer);
449         return bb_count;
450 }
451
452 struct saved_blk_record {
453         blk_t   block;
454         int     num;
455 };
456
457 static unsigned int test_nd (int dev, unsigned long last_block,
458                              int block_size, unsigned long from_count,
459                              unsigned long blocks_at_once)
460 {
461         unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
462         int try, i;
463         const unsigned long patterns[] = { ~0 };
464         const unsigned long *pattern;
465         int nr_pattern, pat_idx;
466         long got, used2, written, save_currently_testing;
467         struct saved_blk_record *test_record;
468         /* This is static to prevent being clobbered by the longjmp */
469         static int num_saved;
470         jmp_buf terminate_env;
471         errcode_t errcode;
472         long buf_used;
473         unsigned int bb_count = 0;
474
475         errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
476         if (errcode) {
477                 com_err (program_name, errcode,
478                          _("while beginning bad block list iteration"));
479                 exit (1);
480         }
481         do {
482                 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
483         } while (next_bad && next_bad < from_count);
484
485         blkbuf = malloc (3 * blocks_at_once * block_size);
486         test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
487         if (!blkbuf || !test_record) {
488                 com_err(program_name, ENOMEM, _("while allocating buffers"));
489                 exit (1);
490         }
491         num_saved = 0;
492
493         flush_bufs();
494         if (v_flag) {
495             fprintf (stderr,
496                      _("Checking for bad blocks in non-destructive read-write mode\n"));
497             fprintf (stderr, _("From block %lu to %lu\n"), from_count, last_block);
498         }
499         if (s_flag || v_flag > 1) {
500                 fprintf(stderr, _("Checking for bad blocks (non-destructive read-write test)\n"));
501         }
502         if (setjmp(terminate_env)) {
503                 /*
504                  * Abnormal termination by a signal is handled here.
505                  */
506                 signal (SIGALRM, SIG_IGN);
507                 fprintf(stderr, _("\nInterrupt caught, cleaning up\n"));
508
509                 save_ptr = blkbuf;
510                 for (i=0; i < num_saved; i++) {
511                         do_write(dev, save_ptr, test_record[i].num,
512                                  block_size, test_record[i].block);
513                         save_ptr += test_record[i].num * block_size;
514                 }
515                 fflush (out);
516                 exit(1);
517         }
518         
519         /* set up abend handler */
520         capture_terminate(terminate_env);
521
522         if (t_flag) {
523                 pattern = t_patts;
524                 nr_pattern = t_flag;
525         } else {
526                 pattern = patterns;
527                 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
528         }
529         for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
530                 pattern_fill(blkbuf + blocks_at_once * block_size,
531                              pattern[pat_idx], blocks_at_once * block_size);
532
533                 buf_used = 0;
534                 bb_count = 0;
535                 save_ptr = blkbuf;
536                 test_ptr = blkbuf + (blocks_at_once * block_size);
537                 currently_testing = from_count;
538                 num_blocks = last_block;
539                 if (s_flag && v_flag <= 1)
540                         alarm_intr(SIGALRM);
541
542                 while (currently_testing < last_block) {
543                         try = blocks_at_once - buf_used;
544                         if (next_bad) {
545                                 if (currently_testing == next_bad) {
546                                         /* fprintf (out, "%lu\n", nextbad); */
547                                         ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
548                                         currently_testing++;
549                                         goto check_for_more;
550                                 }
551                                 else if (currently_testing + try > next_bad)
552                                         try = next_bad - currently_testing;
553                         }
554                         if (currently_testing + try > last_block)
555                                 try = last_block - currently_testing;
556                         got = do_read (dev, save_ptr, try, block_size,
557                                        currently_testing);
558                         if (got == 0) {
559                                 /* First block must have been bad. */
560                                 bb_count += bb_output(currently_testing++);
561                                 goto check_for_more;
562                         }
563
564                         /*
565                          * Note the fact that we've saved this much data
566                          * *before* we overwrite it with test data
567                          */
568                         test_record[num_saved].block = currently_testing;
569                         test_record[num_saved].num = got;
570                         num_saved++;
571
572                         /* Write the test data */
573                         written = do_write (dev, test_ptr, got, block_size,
574                                             currently_testing);
575                         if (written != got)
576                                 com_err (program_name, errno,
577                                          _("during test data write, block %lu"),
578                                          currently_testing + written);
579
580                         buf_used += got;
581                         save_ptr += got * block_size;
582                         test_ptr += got * block_size;
583                         currently_testing += got;
584                         if (got != try)
585                                 bb_count += bb_output(currently_testing++);
586
587                 check_for_more:
588                         /*
589                          * If there's room for more blocks to be tested this
590                          * around, and we're not done yet testing the disk, go
591                          * back and get some more blocks.
592                          */
593                         if ((buf_used != blocks_at_once) &&
594                             (currently_testing < last_block))
595                                 continue;
596
597                         flush_bufs();
598                         save_currently_testing = currently_testing;
599
600                         /*
601                          * for each contiguous block that we read into the
602                          * buffer (and wrote test data into afterwards), read
603                          * it back (looping if necessary, to get past newly
604                          * discovered unreadable blocks, of which there should
605                          * be none, but with a hard drive which is unreliable,
606                          * it has happened), and compare with the test data
607                          * that was written; output to the bad block list if
608                          * it doesn't match.
609                          */
610                         used2 = 0;
611                         save_ptr = blkbuf;
612                         test_ptr = blkbuf + (blocks_at_once * block_size);
613                         read_ptr = blkbuf + (2 * blocks_at_once * block_size);
614                         try = 0;
615
616                         while (1) {
617                                 if (try == 0) {
618                                         if (used2 >= num_saved)
619                                                 break;
620                                         currently_testing = test_record[used2].block;
621                                         try = test_record[used2].num;
622                                         used2++;
623                                 }
624                                 
625                                 got = do_read (dev, read_ptr, try,
626                                                block_size, currently_testing);
627
628                                 /* test the comparison between all the
629                                    blocks successfully read  */
630                                 for (i = 0; i < got; ++i)
631                                         if (memcmp (test_ptr+i*block_size,
632                                                     read_ptr+i*block_size, block_size))
633                                                 bb_count += bb_output(currently_testing + i);
634                                 if (got < try) {
635                                         bb_count += bb_output(currently_testing + got);
636                                         got++;
637                                 }
638                                         
639                                 /* when done, write back original data */
640                                 do_write (dev, save_ptr, got, block_size,
641                                           currently_testing);
642
643                                 currently_testing += got;
644                                 save_ptr += got * block_size;
645                                 test_ptr += got * block_size;
646                                 read_ptr += got * block_size;
647                                 try -= got;
648                         }
649
650                         /* empty the buffer so it can be reused */
651                         num_saved = 0;
652                         buf_used = 0;
653                         save_ptr = blkbuf;
654                         test_ptr = blkbuf + (blocks_at_once * block_size);
655                         currently_testing = save_currently_testing;
656                 }
657                 num_blocks = 0;
658                 alarm(0);
659                 if (s_flag || v_flag > 1)
660                         fputs(done_string, stderr);
661
662                 flush_bufs();
663         }
664         uncapture_terminate();
665         fflush(stderr);
666         free(blkbuf);
667         free(test_record);
668
669         ext2fs_badblocks_list_iterate_end(bb_iter);
670
671         return bb_count;
672 }
673
674 static void check_mount(char *device_name)
675 {
676         errcode_t       retval;
677         int             mount_flags;
678
679         retval = ext2fs_check_if_mounted(device_name, &mount_flags);
680         if (retval) {
681                 com_err("ext2fs_check_if_mount", retval,
682                         _("while determining whether %s is mounted."),
683                         device_name);
684                 return;
685         }
686         if (!(mount_flags & EXT2_MF_MOUNTED))
687                 return;
688
689         fprintf(stderr, _("%s is mounted; "), device_name);
690         if (force) {
691                 fprintf(stderr, _("badblocks forced anyway.  "
692                         "Hope /etc/mtab is incorrect.\n"));
693                 return;
694         }
695         fprintf(stderr, _("it's not safe to run badblocks!\n"));
696         exit(1);
697 }
698
699
700 int main (int argc, char ** argv)
701 {
702         int c;
703         char * tmp;
704         char * device_name;
705         char * host_device_name = NULL;
706         char * input_file = NULL;
707         char * output_file = NULL;
708         FILE * in = NULL;
709         int block_size = 1024;
710         unsigned long blocks_at_once = 16;
711         blk_t last_block, from_count;
712         int num_passes = 0;
713         int passes_clean = 0;
714         int dev;
715         errcode_t errcode;
716         unsigned long pattern;
717         unsigned int (*test_func)(int, unsigned long,
718                                   int, unsigned long,
719                                   unsigned long);
720
721         setbuf(stdout, NULL);
722         setbuf(stderr, NULL);
723 #ifdef ENABLE_NLS
724         setlocale(LC_MESSAGES, "");
725         setlocale(LC_CTYPE, "");
726         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
727         textdomain(NLS_CAT_NAME);
728 #endif
729         srandom((unsigned int)time(NULL));  /* simple randomness is enough */
730         test_func = test_ro;
731
732         if (argc && *argv)
733                 program_name = *argv;
734         while ((c = getopt (argc, argv, "b:fi:o:svwnc:p:h:t:")) != EOF) {
735                 switch (c) {
736                 case 'b':
737                         block_size = strtoul (optarg, &tmp, 0);
738                         if (*tmp || block_size > 4096) {
739                                 com_err (program_name, 0,
740                                          _("bad block size - %s"), optarg);
741                                 exit (1);
742                         }
743                         break;
744                 case 'f':
745                         force++;
746                         break;
747                 case 'i':
748                         input_file = optarg;
749                         break;
750                 case 'o':
751                         output_file = optarg;
752                         break;
753                 case 's':
754                         s_flag = 1;
755                         break;
756                 case 'v':
757                         v_flag++;
758                         break;
759                 case 'w':
760                         if (w_flag)
761                                 usage();
762                         test_func = test_rw;
763                         w_flag = 1;
764                         break;
765                 case 'n':
766                         if (w_flag)
767                                 usage();
768                         test_func = test_nd;
769                         w_flag = 2;
770                         break;
771                 case 'c':
772                         blocks_at_once = strtoul (optarg, &tmp, 0);
773                         if (*tmp) {
774                                 com_err (program_name, 0,
775                                          "bad simultaneous block count - %s", optarg);
776                                 exit (1);
777                         }
778                         break;
779                 case 'p':
780                         num_passes = strtoul (optarg, &tmp, 0);
781                         if (*tmp) {
782                                 com_err (program_name, 0,
783                                     "bad number of clean passes - %s", optarg);
784                                 exit (1);
785                         }
786                         break;
787                 case 'h':
788                         host_device_name = optarg;
789                         break;
790                 case 't':
791                         if (t_flag + 1 > t_max) {
792                                 unsigned long *t_patts_new;
793
794                                 t_patts_new = realloc(t_patts, t_max + T_INC);
795                                 if (!t_patts_new) {
796                                         com_err(program_name, ENOMEM,
797                                                 _("can't allocate memory for "
798                                                   "test_pattern - %s"),
799                                                 optarg);
800                                         exit(1);
801                                 }
802                                 t_patts = t_patts_new;
803                                 t_max += T_INC;
804                         }
805                         if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
806                                 t_patts[t_flag++] = ~0;
807                         } else {
808                                 pattern = strtoul(optarg, &tmp, 0);
809                                 if (*tmp) {
810                                         com_err(program_name, 0,
811                                         _("invalid test_pattern: %s\n"),
812                                                 optarg);
813                                         exit(1);
814                                 }
815                                 if (pattern == ~0)
816                                         pattern = 0xffff;
817                                 t_patts[t_flag++] = pattern;
818                         }
819                         break;
820                 default:
821                         usage();
822                 }
823         }
824         if (!w_flag) {
825                 if (t_flag > 1) {
826                         com_err(program_name, 0,
827                         _("Maximum of one test_pattern may be specified "
828                           "in read-only mode"));
829                         exit(1);
830                 }
831                 if (t_patts && (t_patts[0] == ~0)) {
832                         com_err(program_name, 0,
833                         _("Random test_pattern is not allowed "
834                           "in read-only mode"));
835                         exit(1);
836                 }
837         }
838         if (optind > argc - 1)
839                 usage();
840         device_name = argv[optind++];
841         if (optind > argc - 1) {
842                 errcode = ext2fs_get_device_size(device_name,
843                                                  block_size,
844                                                  &last_block);
845                 if (errcode == EXT2_ET_UNIMPLEMENTED) {
846                         com_err(program_name, 0,
847                                 _("Couldn't determine device size; you "
848                                   "must specify\nthe size manually\n"));
849                         exit(1);
850                 }
851                 if (errcode) {
852                         com_err(program_name, errcode,
853                                 _("while trying to determine device size"));
854                         exit(1);
855                 }
856         } else {
857                 last_block = strtoul (argv[optind], &tmp, 0);
858                 if (*tmp) {
859                         com_err (program_name, 0, _("bad blocks count - %s"),
860                                  argv[optind]);
861                         exit (1);
862                 }
863                 optind++;
864         }
865         if (optind <= argc-1) {
866                 from_count = strtoul (argv[optind], &tmp, 0);
867                 if (*tmp) {
868                         com_err (program_name, 0, _("bad starting block - %s"),
869                                  argv[optind]);
870                         exit (1);
871                 }
872         } else from_count = 0;
873         if (from_count >= last_block) {
874             com_err (program_name, 0, _("bad blocks range: %lu-%lu"),
875                      from_count, last_block);
876             exit (1);
877         }
878         if (w_flag)
879                 check_mount(device_name);
880         
881         dev = open (device_name, O_RDWR);
882         if ((dev == -1) && ((errno == EPERM) || (errno == EACCES) ||
883                             (errno == EROFS)) &&
884             (w_flag == 0))
885                 dev = open(device_name, O_RDONLY);
886         if (dev == -1) {
887                 com_err (program_name, errno, _("while trying to open %s"),
888                          device_name);
889                 exit (1);
890         }
891         if (host_device_name) {
892                 host_dev = open (host_device_name, O_RDWR);
893                 if ((host_dev == -1) &&
894                     ((errno == EPERM) || (errno == EACCES)))
895                         host_dev = open(host_device_name, O_RDONLY);
896                 if (host_dev == -1) {
897                         com_err (program_name, errno,
898                                  _("while trying to open %s"),
899                                  host_device_name);
900                         exit (1);
901                 }
902         } else
903                 host_dev = dev;
904         if (input_file) {
905                 if (strcmp (input_file, "-") == 0)
906                         in = stdin;
907                 else {
908                         in = fopen (input_file, "r");
909                         if (in == NULL)
910                         {
911                                 com_err (program_name, errno,
912                                          _("while trying to open %s"),
913                                          input_file);
914                                 exit (1);
915                         }
916                 }
917         }
918         if (output_file && strcmp (output_file, "-") != 0)
919         {
920                 out = fopen (output_file, "w");
921                 if (out == NULL)
922                 {
923                         com_err (program_name, errno,
924                                  _("while trying to open %s"),
925                                  output_file);
926                         exit (1);
927                 }
928         }
929         else
930                 out = stdout;
931
932         errcode = ext2fs_badblocks_list_create(&bb_list,0);
933         if (errcode) {
934                 com_err (program_name, errcode,
935                          _("creating in-memory bad blocks list"));
936                 exit (1);
937         }
938
939         if (in) {
940                 for(;;) {
941                         switch(fscanf (in, "%u\n", &next_bad)) {
942                                 case 0:
943                                         com_err (program_name, 0, "input file - bad format");
944                                         exit (1);
945                                 case EOF:
946                                         break;
947                                 default:
948                                         errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
949                                         if (errcode) {
950                                                 com_err (program_name, errcode, _("adding to in-memory bad block list"));
951                                                 exit (1);
952                                         }
953                                         continue;
954                         }
955                         break;
956                 }
957
958                 if (in != stdin)
959                         fclose (in);
960         }
961
962         do {
963                 unsigned int bb_count;
964
965                 bb_count = test_func(dev, last_block, block_size,
966                                      from_count, blocks_at_once);
967                 if (bb_count)
968                         passes_clean = 0;
969                 else
970                         ++passes_clean;
971                 
972                 if (v_flag)
973                         fprintf(stderr,
974                                 _("Pass completed, %u bad blocks found.\n"), 
975                                 bb_count);
976
977         } while (passes_clean < num_passes);
978
979         close (dev);
980         if (out != stdout)
981                 fclose (out);
982         if (t_patts)
983                 free(t_patts);
984         return 0;
985 }
986