Whamcloud - gitweb
ChangeLog, badblocks.8.in, badblocks.c:
[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 #endif
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <setjmp.h>
43
44 #include <sys/ioctl.h>
45 #include <sys/types.h>
46
47 #if HAVE_LINUX_FS_H
48 #include <linux/fd.h>
49 #endif
50
51 #include "et/com_err.h"
52 #include "ext2fs/ext2_io.h"
53 #include <linux/ext2_fs.h>
54 #include "ext2fs/ext2fs.h"
55
56 const char * program_name = "badblocks";
57 const char * done_string = "done                        \n";
58
59 int v_flag = 0;                 /* verbose */
60 int w_flag = 0;                 /* do r/w test: 0=no, 1=yes, 2=non-destructive */
61 int s_flag = 0;                 /* show progress of test */
62
63 static void usage(void)
64 {
65         fprintf (stderr, "Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwn]\n [-c blocks_at_once] [-p num_passes] device blocks_count [start_count]\n",
66                  program_name);
67         exit (1);
68 }
69
70 static unsigned long currently_testing = 0;
71 static unsigned long num_blocks = 0;
72 static ext2_badblocks_list bb_list = NULL;
73 static FILE *out;
74 static blk_t next_bad = 0;
75 static ext2_badblocks_iterate bb_iter = NULL;
76
77 /*
78  * This routine reports a new bad block.  If the bad block has already
79  * been seen before, then it returns 0; otherwise it returns 1.
80  */
81 static int bb_output (unsigned long bad)
82 {
83         errcode_t errcode;
84
85         if (ext2fs_badblocks_list_test(bb_list, bad))
86                 return 0;
87
88         fprintf (out, "%lu\n", bad);
89
90         errcode = ext2fs_badblocks_list_add (bb_list, bad);
91         if (errcode) {
92                 com_err (program_name, errcode, "adding to in-memory bad block list");
93                 exit (1);
94         }
95
96         /* kludge:
97            increment the iteration through the bb_list if 
98            an element was just added before the current iteration
99            position.  This should not cause next_bad to change. */
100         if (bb_iter && bad < next_bad)
101                 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
102         return 1;
103 }
104
105 static void print_status (void)
106 {
107         fprintf(stderr, "%9ld/%9ld", currently_testing, num_blocks);
108         fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
109         fflush (stderr);
110 }
111
112 static void alarm_intr (int alnum)
113 {
114         signal (SIGALRM, alarm_intr);
115         alarm(1);
116         if (!num_blocks)
117                 return;
118         fprintf(stderr, "%9ld/%9ld", currently_testing, num_blocks);
119         fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
120         fflush (stderr);
121 }
122
123 static void *terminate_addr = NULL;
124
125 static void terminate_intr (int signo)
126 {
127         if (terminate_addr)
128                 longjmp(terminate_addr,1);
129         exit(1);
130 }
131
132 static void capture_terminate (jmp_buf term_addr)
133 {
134         terminate_addr = term_addr;
135         signal (SIGHUP, terminate_intr);
136         signal (SIGINT, terminate_intr);
137         signal (SIGPIPE, terminate_intr);
138         signal (SIGTERM, terminate_intr);
139         signal (SIGUSR1, terminate_intr);
140         signal (SIGUSR2, terminate_intr);
141 }
142
143 /*
144  * Perform a read of a sequence of blocks; return the number of blocks
145  *    successfully sequentially read.
146  */
147 static long do_read (int dev, char * buffer, int try, int block_size,
148                      unsigned long current_block)
149 {
150         long got;
151
152         if (v_flag > 1)
153                 print_status();
154
155         /* Seek to the correct loc. */
156         if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
157                          SEEK_SET) != (ext2_loff_t) current_block * block_size)
158                 com_err (program_name, errno, "during seek");
159
160         /* Try the read */
161         got = read (dev, buffer, try * block_size);
162         if (got < 0)
163                 got = 0;        
164         if (got & 511)
165                 fprintf (stderr,
166                          "Weird value (%ld) in do_read\n", got);
167         got /= block_size;
168         return got;
169 }
170
171 /*
172  * Perform a write of a sequence of blocks; return the number of blocks
173  *    successfully sequentially written.
174  */
175 static long do_write (int dev, char * buffer, int try, int block_size,
176                      unsigned long current_block)
177 {
178         long got;
179
180         if (v_flag > 1)
181                 print_status();
182
183         /* Seek to the correct loc. */
184         if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
185                          SEEK_SET) != (ext2_loff_t) current_block * block_size)
186                 com_err (program_name, errno, "during seek");
187
188         /* Try the write */
189         got = write (dev, buffer, try * block_size);
190         if (got < 0)
191                 got = 0;        
192         if (got & 511)
193                 fprintf (stderr,
194                          "Weird value (%ld) in do_write\n", got);
195         got /= block_size;
196         return got;
197 }
198
199 static int host_dev;
200
201 static void flush_bufs (int dev, int sync)
202 {
203   if (v_flag
204 #if !defined (BLKFLSBUF) && !defined (FDFLUSH)
205       && sync
206 #endif
207       )
208     fprintf (stderr, "Flushing buffers\n");
209
210   if (sync && fdatasync (dev) == -1)
211     com_err (program_name, errno, "during fsync");
212
213 #ifdef BLKFLSBUF
214   ioctl (host_dev, BLKFLSBUF, 0);   /* In case this is a HD */
215 #endif
216 #ifdef FDFLUSH
217   ioctl (host_dev, FDFLUSH, 0);   /* In case this is floppy */
218 #endif
219 }
220
221 static unsigned int test_ro (int dev, unsigned long blocks_count,
222                              int block_size, unsigned long from_count,
223                              unsigned long blocks_at_once)
224 {
225         char * blkbuf;
226         int try;
227         long got;
228         unsigned int bb_count = 0;
229         errcode_t errcode;
230
231         errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
232         if (errcode) {
233                 com_err (program_name, errcode, "while beginning bad block list iteration");
234                 exit (1);
235         }
236         do {
237                 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
238         } while (next_bad && next_bad < from_count);
239
240         blkbuf = malloc (blocks_at_once * block_size);
241         if (!blkbuf)
242         {
243                 com_err (program_name, ENOMEM, "while allocating buffers");
244                 exit (1);
245         }
246         flush_bufs (dev, 0);
247         if (v_flag) {
248             fprintf (stderr,
249                      "Checking for bad blocks in read-only mode\n");
250             fprintf (stderr, "From block %lu to %lu\n", from_count, blocks_count);
251         }
252         try = blocks_at_once;
253         currently_testing = from_count;
254         num_blocks = blocks_count;
255         if (s_flag || v_flag > 1) {
256                 fprintf(stderr, "Checking for bad blocks (read-only test): ");
257                 if (v_flag <= 1)
258                         alarm_intr(SIGALRM);
259         }
260         while (currently_testing < blocks_count)
261         {
262                 if (next_bad) {
263                         if (currently_testing == next_bad) {
264                                 /* fprintf (out, "%lu\n", nextbad); */
265                                 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
266                                 currently_testing++;
267                                 continue;
268                         }
269                         else if (currently_testing + try > next_bad)
270                                 try = next_bad - currently_testing;
271                 }
272                 if (currently_testing + try > blocks_count)
273                         try = blocks_count - currently_testing;
274                 got = do_read (dev, blkbuf, try, block_size, currently_testing);
275                 currently_testing += got;
276                 if (got == try) {
277                         try = blocks_at_once;
278                         continue;
279                 }
280                 else
281                         try = 1;
282                 if (got == 0) {
283                         bb_count += bb_output(currently_testing++);
284                 }
285         }
286         num_blocks = 0;
287         alarm(0);
288         if (s_flag || v_flag > 1)
289                 fprintf(stderr, done_string);
290
291         fflush (stderr);
292         free (blkbuf);
293
294         ext2fs_badblocks_list_iterate_end(bb_iter);
295
296         return bb_count;
297 }
298
299 static unsigned int test_rw (int dev, unsigned long blocks_count,
300                              int block_size, unsigned long from_count,
301                              unsigned long blocks_at_once)
302 {
303         int i;
304         char * buffer;
305         unsigned char pattern[] = {0xaa, 0x55, 0xff, 0x00};
306         unsigned int bb_count = 0;
307
308         buffer = malloc (2 * block_size);
309         if (!buffer)
310         {
311                 com_err (program_name, ENOMEM, "while allocating buffers");
312                 exit (1);
313         }
314
315         flush_bufs (dev, 0);
316
317         if (v_flag) {
318                 fprintf(stderr,
319                         "Checking for bad blocks in read-write mode\n");
320                 fprintf(stderr, "From block %lu to %lu\n",
321                          from_count, blocks_count);
322         }
323         for (i = 0; i < sizeof (pattern); i++) {
324                 memset (buffer, pattern[i], block_size);
325                 if (s_flag | v_flag)
326                         fprintf (stderr, "Writing pattern 0x%08x: ",
327                                  *((int *) buffer));
328                 num_blocks = blocks_count;
329                 currently_testing = from_count;
330                 if (s_flag && v_flag <= 1)
331                         alarm_intr(SIGALRM);
332                 for (;
333                      currently_testing < blocks_count;
334                      currently_testing++)
335                 {
336                         if (ext2fs_llseek (dev, (ext2_loff_t) currently_testing *
337                                          block_size, SEEK_SET) !=
338                             (ext2_loff_t) currently_testing * block_size)
339                                 com_err (program_name, errno,
340                                          "during seek on block %d",
341                                          currently_testing);
342                         if (v_flag > 1)
343                                 print_status();
344                         write (dev, buffer, block_size);
345                 }
346                 num_blocks = 0;
347                 alarm (0);
348                 if (s_flag | v_flag)
349                         fprintf(stderr, done_string);
350                 flush_bufs (dev, 1);
351                 if (s_flag | v_flag)
352                         fprintf (stderr, "Reading and comparing: ");
353                 num_blocks = blocks_count;
354                 currently_testing = from_count;
355                 if (s_flag && v_flag <= 1)
356                         alarm_intr(SIGALRM);
357                 for (;
358                      currently_testing < blocks_count;
359                      currently_testing++)
360                 {
361                         if (ext2fs_llseek (dev, (ext2_loff_t) currently_testing *
362                                          block_size, SEEK_SET) !=
363                             (ext2_loff_t) currently_testing * block_size)
364                                 com_err (program_name, errno,
365                                          "during seek on block %d",
366                                          currently_testing);
367                         if (v_flag > 1)
368                                 print_status();
369                         if ((read (dev, buffer + block_size, block_size) 
370                              < block_size) ||
371                             memcmp(buffer, buffer + block_size, block_size))
372                                 bb_count = bb_output(currently_testing);
373                 }
374                 num_blocks = 0;
375                 alarm (0);
376                 if (s_flag | v_flag)
377                         fprintf(stderr, done_string);
378                 flush_bufs (dev, 0);
379         }
380
381         return bb_count;
382 }
383
384 static unsigned int test_nd (int dev, unsigned long blocks_count,
385                              int block_size, unsigned long from_count,
386                              unsigned long blocks_at_once)
387 {
388         char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
389         char * ptr;
390         int try, i;
391         long got, used2;
392         unsigned long *bufblk;
393         unsigned long *bufblks;
394         jmp_buf terminate_env;
395         errcode_t errcode;
396         /* These are static to prevent being clobbered by the longjmp */
397         static long buf_used;
398         static unsigned int bb_count;
399
400         bb_count = 0;
401         buf_used = 0;
402
403         errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
404         if (errcode) {
405                 com_err (program_name, errcode,
406                          "while beginning bad block list iteration");
407                 exit (1);
408         }
409         do {
410                 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
411         } while (next_bad && next_bad < from_count);
412
413         blkbuf = malloc (3 * blocks_at_once * block_size);
414         bufblk = malloc (blocks_at_once * sizeof(unsigned long));
415         bufblks = malloc (blocks_at_once * sizeof(unsigned long));
416         if (!blkbuf || !bufblk || !bufblks) {
417                 com_err (program_name, ENOMEM, "while allocating buffers");
418                 exit (1);
419         }
420
421         /* inititalize the test data randomly: */
422         if (v_flag) {
423                 fprintf (stderr, "Initializing random test data\n");
424         }
425         for(ptr = blkbuf + blocks_at_once * block_size;
426             ptr < blkbuf + 2 * blocks_at_once * block_size;
427             ++ptr) {
428                 (*ptr) = random() % (1 << sizeof(char));
429         }
430
431         flush_bufs (dev, 0);
432         if (v_flag) {
433             fprintf (stderr,
434                      "Checking for bad blocks in non-destructive read-write mode\n");
435             fprintf (stderr, "From block %lu to %lu\n", from_count, blocks_count);
436         }
437         if (s_flag || v_flag > 1) {
438                 fprintf (stderr, "Checking for bad blocks (non-destructive read-write test): ");
439                 if (v_flag <= 1)
440                         alarm_intr(SIGALRM);
441         }
442         if (! setjmp(terminate_env)) {
443                 /* set up abend handler */
444                 capture_terminate(terminate_env);
445
446                 buf_used = 0; save_ptr = blkbuf;
447                 test_ptr = blkbuf + (blocks_at_once * block_size);
448                 currently_testing = from_count;
449                 num_blocks = blocks_count;
450
451                 while (currently_testing < blocks_count) {
452                         try = blocks_at_once - buf_used;
453                         if (next_bad) {
454                                 if (currently_testing == next_bad) {
455                                         /* fprintf (out, "%lu\n", nextbad); */
456                                         ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
457                                         bufblk[buf_used] = currently_testing++;
458                                         goto test_full_buf;
459                                 }
460                                 else if (currently_testing + try > next_bad)
461                                         try = next_bad - currently_testing;
462                         }
463                         if (currently_testing + try > blocks_count)
464                                 try = blocks_count - currently_testing;
465                         got = do_read (dev, save_ptr, try, block_size,
466                                        currently_testing);
467
468                         /* if reading succeeded, write the test data */
469                         if (got) {
470                                 long written;
471
472                                 written = do_write (dev, test_ptr, got, 
473                                                     block_size,
474                                                     currently_testing);
475                                 if (written != got)
476                                         com_err (program_name, errno,
477                                  "during test data write, block %lu",
478                                                  currently_testing + written);
479                         }
480
481                         bufblk[buf_used] = currently_testing;
482                         bufblks[buf_used] = got;
483                         buf_used += got;
484                         save_ptr += got * block_size;
485                         test_ptr += got * block_size;
486                         currently_testing += got;
487                         if (got != try)
488                                 bb_count += bb_output(currently_testing++);
489
490                         test_full_buf:
491                         /*
492                          * If there's room for more blocks to be
493                          * tested this around, and we're not done yet
494                          * testing the disk, go back and get some
495                          * more blocks.
496                          */
497                         if ((buf_used != blocks_at_once) &&
498                             (currently_testing != blocks_count))
499                                 continue;
500
501                         flush_bufs (dev, 1);
502
503                         /*
504                          * for each contiguous block that we read into
505                          * the buffer (and wrote test data into
506                          * afterwards), read it back (looping if
507                          * necessary, to get past newly discovered
508                          * unreadable blocks, of which there should be
509                          * none, but with a hard drive which is
510                          * unreliable, it has happened), and compare
511                          * with the test data that was written; output
512                          * to the bad block list if it doesn't match.
513                          */
514                         used2 = 0;
515                         save_ptr = blkbuf;
516                         test_ptr = blkbuf + (blocks_at_once * block_size);
517                         read_ptr = blkbuf + (2 * blocks_at_once * block_size);
518                         currently_testing = bufblk[0];
519                         try = bufblks[0];
520                         while (currently_testing < blocks_count) {
521
522                                 got = do_read (dev, read_ptr, try,
523                                                block_size, currently_testing);
524
525                                 /* test the comparison between all the
526                                    blocks successfully read  */
527                                 for (i = 0; i < got; ++i)
528                                         if (memcmp (test_ptr, read_ptr,
529                                                     block_size))
530                                                 bb_count += bb_output(currently_testing + i);
531                                 if (got == 0) {
532                                         bb_count += bb_output(currently_testing);
533                                         got = 1;
534                                 }
535                                         
536                                 /* when done, write back original data */
537                                 do_write (dev, save_ptr, got, block_size, 
538                                           currently_testing);
539
540                                 currently_testing += got;
541                                 save_ptr += bufblks[got] * block_size;
542                                 test_ptr += bufblks[got] * block_size;
543                                 read_ptr += bufblks[got] * block_size;
544                                 try -= got;
545
546                                 if (try == 0) {
547                                         used2 += bufblks[used2];
548                                         if (used2 >= blocks_at_once)
549                                                 break;
550                                         currently_testing = bufblk[used2];
551                                         try = bufblks[used2];
552                                 }
553                         }
554
555                         /* empty the buffer so it can be reused */
556                         buf_used = 0;
557                 }
558                 num_blocks = 0;
559                 alarm(0);
560                 if (s_flag || v_flag > 1)
561                         fprintf(stderr, "done               \n");
562
563         } else {
564                 /* abnormal termination by a signal is handled here */
565                 /* logic is: write back the data that is in the buffer,
566                    so that we can maintain data integrity on disk.  Then
567                    abort. */
568                 long buf_written;
569
570                 fprintf(stderr, "Interrupt caught, cleaning up\n");
571
572                 for (buf_written = 0;
573                         buf_written < buf_used;
574                            buf_written += bufblks[buf_written])
575                         do_write (dev, blkbuf + buf_written * block_size,
576                                   bufblks[buf_written], block_size, 
577                                   bufblk[buf_written]);
578
579                 fflush (out);
580                 exit(1);
581         }
582
583         fflush(stderr);
584         free(blkbuf);
585         free(bufblk);
586         free(bufblks);
587
588         ext2fs_badblocks_list_iterate_end(bb_iter);
589
590         return bb_count;
591 }
592
593 int main (int argc, char ** argv)
594 {
595         int c;
596         char * tmp;
597         char * device_name;
598         char * host_device_name = NULL;
599         char * input_file = NULL;
600         char * output_file = NULL;
601         FILE * in = NULL;
602         int block_size = 1024;
603         unsigned long blocks_at_once = 16;
604         unsigned long blocks_count, from_count;
605         int num_passes = 0;
606         int passes_clean = 0;
607         int dev;
608         errcode_t errcode;
609
610         setbuf(stdout, NULL);
611         setbuf(stderr, NULL);
612         if (argc && *argv)
613                 program_name = *argv;
614         while ((c = getopt (argc, argv, "b:i:o:svwnc:p:h:")) != EOF) {
615                 switch (c) {
616                 case 'b':
617                         block_size = strtoul (optarg, &tmp, 0);
618                         if (*tmp || block_size > 4096) {
619                                 com_err (program_name, 0,
620                                          "bad block size - %s", optarg);
621                                 exit (1);
622                         }
623                         break;
624                 case 'i':
625                         input_file = optarg;
626                         break;
627                 case 'o':
628                         output_file = optarg;
629                         break;
630                 case 's':
631                         s_flag = 1;
632                         break;
633                 case 'v':
634                         v_flag++;
635                         break;
636                 case 'w':
637                         if (! w_flag)
638                                 w_flag = 1;
639                         break;
640                 case 'n':
641                         w_flag = 2;
642                         break;
643                 case 'c':
644                         blocks_at_once = strtoul (optarg, &tmp, 0);
645                         if (*tmp) {
646                                 com_err (program_name, 0,
647                                          "bad simultaneous block count - %s", optarg);
648                                 exit (1);
649                         }
650                         break;
651                 case 'p':
652                         num_passes = strtoul (optarg, &tmp, 0);
653                         if (*tmp) {
654                                 com_err (program_name, 0,
655                                     "bad number of clean passes - %s", optarg);
656                                 exit (1);
657                         }
658                         break;
659                 case 'h':
660                         host_device_name = optarg;
661                         break;
662                 default:
663                         usage();
664                 }
665         }
666         if (optind > argc - 1)
667                 usage();
668         device_name = argv[optind++];
669         if (optind > argc - 1)
670                 usage();
671         blocks_count = strtoul (argv[optind], &tmp, 0);
672         if (*tmp)
673         {
674                 com_err (program_name, 0, "bad blocks count - %s", argv[optind]);
675                 exit (1);
676         }
677         if (++optind <= argc-1) {
678                 from_count = strtoul (argv[optind], &tmp, 0);
679         } else from_count = 0;
680         if (from_count >= blocks_count) {
681             com_err (program_name, 0, "bad blocks range: %lu-%lu",
682                      from_count, blocks_count);
683             exit (1);
684         }
685         dev = open (device_name, w_flag ? O_RDWR : O_RDONLY);
686         if (dev == -1)
687         {
688                 com_err (program_name, errno, "while trying to open %s",
689                          device_name);
690                 exit (1);
691         }
692         if (host_device_name) {
693                 host_dev = open (host_device_name, O_RDONLY);
694                 if (host_dev == -1)
695                 {
696                         com_err (program_name, errno, "while trying to open %s",
697                             host_device_name);
698                         exit (1);
699                 }
700         } else
701                 host_dev = dev;
702         if (input_file)
703                 if (strcmp (input_file, "-") == 0)
704                         in = stdin;
705                 else {
706                         in = fopen (input_file, "r");
707                         if (in == NULL)
708                         {
709                                 com_err (program_name, errno,"while trying to open %s",
710                                          input_file);
711                                 exit (1);
712                         }
713                 }
714         if (output_file && strcmp (output_file, "-") != 0)
715         {
716                 out = fopen (output_file, "w");
717                 if (out == NULL)
718                 {
719                         com_err (program_name, errno,"while trying to open %s",
720                                  output_file);
721                         exit (1);
722                 }
723         }
724         else
725                 out = stdout;
726
727         errcode = ext2fs_badblocks_list_create(&bb_list,0);
728         if (errcode) {
729                 com_err (program_name, errcode, "creating in-memory bad blocks list");
730                 exit (1);
731         }
732
733         if (in) {
734                 for(;;) {
735                         switch(fscanf (in, "%lu\n", &next_bad)) {
736                                 case 0:
737                                         com_err (program_name, 0, "input file - bad format");
738                                         exit (1);
739                                 case EOF:
740                                         break;
741                                 default:
742                                         errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
743                                         if (errcode) {
744                                                 com_err (program_name, errcode, "adding to in-memory bad block list");
745                                                 exit (1);
746                                         }
747                                         continue;
748                         }
749                         break;
750                 }
751
752                 if (in != stdin)
753                         fclose (in);
754         }
755
756         do {
757                 unsigned int bb_count;
758
759                 (bb_count =
760
761                 (w_flag == 2 ? test_nd
762                     : w_flag ? test_rw
763                              : test_ro)
764                                         (dev, blocks_count, block_size, from_count,
765                                               blocks_at_once)
766                 )       ? passes_clean = 0 : ++passes_clean;
767
768                 if (v_flag)
769                         fprintf(stderr,"Pass completed, %u bad blocks found.\n", bb_count);
770
771         } while (passes_clean < num_passes);
772
773         close (dev);
774         if (out != stdout)
775                 fclose (out);
776         return 0;
777 }