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