blob: 9a6acdfaacd6b889a811a9d87584d4435cf6e48b [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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 *
Theodore Ts'odd018f52000-02-06 23:57:07 +00008 * Copyright 1995, 1996, 1997, 1998, 1999 by Theodore Ts'o
Theodore Ts'o879ac922000-01-18 20:59:11 +00009 * Copyright 1999 by David Beattie
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000010 *
Theodore Ts'o3839e651997-04-26 13:21:57 +000011 * 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>
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000013 *
14 * %Begin-Header%
15 * This file may be redistributed under the terms of the GNU Public
16 * License.
17 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000018 */
19
20/*
21 * History:
22 * 93/05/26 - Creation from e2fsck
23 * 94/02/27 - Made a separate bad blocks checker
Theodore Ts'o879ac922000-01-18 20:59:11 +000024 * 99/06/30...99/07/26 - Added non-destructive write-testing,
Theodore Ts'odd018f52000-02-06 23:57:07 +000025 * 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)
Theodore Ts'o3839e651997-04-26 13:21:57 +000030 */
31
Theodore Ts'o1c29b092003-07-12 16:01:45 -040032#define _GNU_SOURCE /* for O_DIRECT */
33
Theodore Ts'o3839e651997-04-26 13:21:57 +000034#include <errno.h>
35#include <fcntl.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000036#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000037#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000038#else
39extern char *optarg;
40extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000041#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include <signal.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
Theodore Ts'o879ac922000-01-18 20:59:11 +000047#include <setjmp.h>
Theodore Ts'o6d40f562003-05-07 08:35:38 -040048#include <time.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000049
50#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000051#include <sys/types.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000052
Theodore Ts'o3839e651997-04-26 13:21:57 +000053#include "et/com_err.h"
Theodore Ts'od40259f1997-10-20 00:44:26 +000054#include "ext2fs/ext2_io.h"
Theodore Ts'o54c637d2001-05-14 11:45:38 +000055#include "ext2fs/ext2_fs.h"
Theodore Ts'o879ac922000-01-18 20:59:11 +000056#include "ext2fs/ext2fs.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000057#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000058
59const char * program_name = "badblocks";
Theodore Ts'o8f5c0f62000-02-08 20:29:44 +000060const char * done_string = N_("done \n");
Theodore Ts'o3839e651997-04-26 13:21:57 +000061
Theodore Ts'o4d003982000-04-03 16:01:11 +000062static 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 */
Theodore Ts'o981dc562000-07-06 14:13:29 +000066static int force = 0; /* force check of mounted device */
Theodore Ts'o849b6bc2003-05-07 09:52:14 -040067static int t_flag = 0; /* number of test patterns */
68static int t_max = 0; /* allocated test patterns */
Theodore Ts'o84c05452003-05-18 01:11:52 -040069static unsigned long *t_patts = NULL; /* test patterns */
Theodore Ts'o1c29b092003-07-12 16:01:45 -040070static int current_O_DIRECT = 0; /* Current status of O_DIRECT flag */
71
Theodore Ts'o849b6bc2003-05-07 09:52:14 -040072#define T_INC 32
Theodore Ts'o4d003982000-04-03 16:01:11 +000073
Theodore Ts'o1c29b092003-07-12 16:01:45 -040074int sys_page_size = 4096;
75
Theodore Ts'o8820c792001-01-06 04:20:03 +000076static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000077{
Theodore Ts'o849b6bc2003-05-07 09:52:14 -040078 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"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000079 program_name);
80 exit (1);
81}
82
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000083static unsigned long currently_testing = 0;
84static unsigned long num_blocks = 0;
Theodore Ts'o879ac922000-01-18 20:59:11 +000085static ext2_badblocks_list bb_list = NULL;
86static FILE *out;
87static blk_t next_bad = 0;
88static ext2_badblocks_iterate bb_iter = NULL;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000089
Theodore Ts'o1c29b092003-07-12 16:01:45 -040090static 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
Theodore Ts'odd018f52000-02-06 23:57:07 +0000113/*
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)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000118{
119 errcode_t errcode;
120
Theodore Ts'odd018f52000-02-06 23:57:07 +0000121 if (ext2fs_badblocks_list_test(bb_list, bad))
122 return 0;
123
Theodore Ts'occ4f98e2003-04-03 11:37:46 -0500124 fprintf(out, "%lu\n", bad);
125 fflush(out);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000126
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);
Theodore Ts'odd018f52000-02-06 23:57:07 +0000139 return 1;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000140}
141
Theodore Ts'o8820c792001-01-06 04:20:03 +0000142static void print_status(void)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000143{
Theodore Ts'oc76564a2005-01-06 14:48:59 -0500144 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);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000146 fflush (stderr);
147}
148
Theodore Ts'o54434922003-12-07 01:28:50 -0500149static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000150{
151 signal (SIGALRM, alarm_intr);
152 alarm(1);
153 if (!num_blocks)
154 return;
Theodore Ts'oc76564a2005-01-06 14:48:59 -0500155 print_status();
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000156}
157
Theodore Ts'o879ac922000-01-18 20:59:11 +0000158static void *terminate_addr = NULL;
159
Theodore Ts'o54434922003-12-07 01:28:50 -0500160static void terminate_intr(int signo EXT2FS_ATTR((unused)))
Theodore Ts'o879ac922000-01-18 20:59:11 +0000161{
162 if (terminate_addr)
163 longjmp(terminate_addr,1);
164 exit(1);
165}
166
Theodore Ts'o981dc562000-07-06 14:13:29 +0000167static void capture_terminate(jmp_buf term_addr)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000168{
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
Theodore Ts'o8820c792001-01-06 04:20:03 +0000178static void uncapture_terminate(void)
Theodore Ts'o4d003982000-04-03 16:01:11 +0000179{
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
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400189static void set_o_direct(int dev, unsigned char *buffer, size_t size,
190 unsigned long current_block)
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400191{
192#ifdef O_DIRECT
193 int new_flag = O_DIRECT;
194 int flag;
195
196 if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400197 ((size & (sys_page_size - 1)) != 0) ||
198 ((current_block & ((sys_page_size >> 9)-1)) != 0))
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400199 new_flag = 0;
200
201 if (new_flag != current_O_DIRECT) {
Theodore Ts'odc058712003-07-25 07:39:33 -0400202 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400203 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
Theodore Ts'o84c05452003-05-18 01:11:52 -0400214static void pattern_fill(unsigned char *buffer, unsigned long pattern,
215 size_t n)
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400216{
Theodore Ts'o54434922003-12-07 01:28:50 -0500217 unsigned int i, nb;
Theodore Ts'o84c05452003-05-18 01:11:52 -0400218 unsigned char bpattern[sizeof(pattern)], *ptr;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400219
Theodore Ts'o023cbb32004-02-26 21:06:58 -0500220 if (pattern == (unsigned long) ~0) {
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400221 for (ptr = buffer; ptr < buffer + n; ptr++) {
222 (*ptr) = random() % (1 << (8 * sizeof(char)));
223 }
224 if (s_flag | v_flag)
Theodore Ts'o54434922003-12-07 01:28:50 -0500225 fputs(_("Testing with random pattern: "), stderr);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400226 } 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++) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500236 *ptr = bpattern[i];
237 if (i == 0)
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400238 i = nb;
Theodore Ts'o54434922003-12-07 01:28:50 -0500239 else
240 i--;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400241 }
Theodore Ts'o84c05452003-05-18 01:11:52 -0400242 if (s_flag | v_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500243 fputs(_("Testing with pattern 0x"), stderr);
Theodore Ts'o84c05452003-05-18 01:11:52 -0400244 for (i = 0; i <= nb; i++)
245 fprintf(stderr, "%02x", buffer[i]);
Theodore Ts'o54434922003-12-07 01:28:50 -0500246 fputs(": ", stderr);
Theodore Ts'o84c05452003-05-18 01:11:52 -0400247 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400248 }
249}
250
Theodore Ts'o3839e651997-04-26 13:21:57 +0000251/*
Theodore Ts'o879ac922000-01-18 20:59:11 +0000252 * Perform a read of a sequence of blocks; return the number of blocks
253 * successfully sequentially read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254 */
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400255static long do_read (int dev, unsigned char * buffer, int try, int block_size,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000256 unsigned long current_block)
257{
258 long got;
259
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400260 set_o_direct(dev, buffer, try * block_size, current_block);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400261
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000262 if (v_flag > 1)
263 print_status();
264
Theodore Ts'o3839e651997-04-26 13:21:57 +0000265 /* Seek to the correct loc. */
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000266 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000267 SEEK_SET) != (ext2_loff_t) current_block * block_size)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000268 com_err (program_name, errno, _("during seek"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269
270 /* Try the read */
271 got = read (dev, buffer, try * block_size);
272 if (got < 0)
273 got = 0;
Theodore Ts'o9f10a7b1999-07-16 10:41:36 +0000274 if (got & 511)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000275 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276 got /= block_size;
277 return got;
278}
279
Theodore Ts'o879ac922000-01-18 20:59:11 +0000280/*
281 * Perform a write of a sequence of blocks; return the number of blocks
282 * successfully sequentially written.
283 */
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400284static long do_write (int dev, unsigned char * buffer, int try, int block_size,
Theodore Ts'o879ac922000-01-18 20:59:11 +0000285 unsigned long current_block)
286{
287 long got;
288
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400289 set_o_direct(dev, buffer, try * block_size, current_block);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400290
Theodore Ts'o879ac922000-01-18 20:59:11 +0000291 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)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000297 com_err (program_name, errno, _("during seek"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000298
299 /* Try the write */
300 got = write (dev, buffer, try * block_size);
301 if (got < 0)
302 got = 0;
303 if (got & 511)
Theodore Ts'o54434922003-12-07 01:28:50 -0500304 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000305 got /= block_size;
306 return got;
307}
308
309static int host_dev;
310
Theodore Ts'o4d404542001-01-11 16:04:59 +0000311static void flush_bufs(void)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000312{
Theodore Ts'o4d404542001-01-11 16:04:59 +0000313 errcode_t retval;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000314
Theodore Ts'o4d404542001-01-11 16:04:59 +0000315 retval = ext2fs_sync_device(host_dev, 1);
316 if (retval)
317 com_err(program_name, retval, _("during ext2fs_sync_device"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000318}
319
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000320static unsigned int test_ro (int dev, unsigned long last_block,
Theodore Ts'odd018f52000-02-06 23:57:07 +0000321 int block_size, unsigned long from_count,
322 unsigned long blocks_at_once)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000323{
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400324 unsigned char * blkbuf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000325 int try;
326 long got;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000327 unsigned int bb_count = 0;
328 errcode_t errcode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000329
Theodore Ts'o879ac922000-01-18 20:59:11 +0000330 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
331 if (errcode) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000332 com_err (program_name, errcode,
333 _("while beginning bad block list iteration"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000334 exit (1);
335 }
336 do {
337 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
338 } while (next_bad && next_bad < from_count);
339
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400340 if (t_flag) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400341 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400342 } else {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400343 blkbuf = allocate_buffer(blocks_at_once * block_size);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400344 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000345 if (!blkbuf)
346 {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000347 com_err (program_name, ENOMEM, _("while allocating buffers"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000348 exit (1);
349 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000350 if (v_flag) {
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400351 fprintf (stderr, _("Checking blocks %lu to %lu\n"), from_count,
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000352 last_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000353 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400354 if (t_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500355 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400356 pattern_fill(blkbuf + blocks_at_once * block_size,
357 t_patts[0], block_size);
358 }
359 flush_bufs();
Theodore Ts'o879ac922000-01-18 20:59:11 +0000360 try = blocks_at_once;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000361 currently_testing = from_count;
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000362 num_blocks = last_block;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400363 if (!t_flag && (s_flag || v_flag)) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500364 fputs(_("Checking for bad blocks (read-only test): "), stderr);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000365 if (v_flag <= 1)
366 alarm_intr(SIGALRM);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000367 }
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000368 while (currently_testing < last_block)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000369 {
Theodore Ts'o879ac922000-01-18 20:59:11 +0000370 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 }
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000380 if (currently_testing + try > last_block)
381 try = last_block - currently_testing;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000382 got = do_read (dev, blkbuf, try, block_size, currently_testing);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400383 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 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000393 currently_testing += got;
394 if (got == try) {
Theodore Ts'o879ac922000-01-18 20:59:11 +0000395 try = blocks_at_once;
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400396 /* recover page-aligned offset for O_DIRECT */
Theodore Ts'o54434922003-12-07 01:28:50 -0500397 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400398 && (currently_testing % (sys_page_size >> 9)!= 0))
399 try -= (sys_page_size >> 9)
400 - (currently_testing
401 % (sys_page_size >> 9));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000402 continue;
403 }
404 else
405 try = 1;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000406 if (got == 0) {
Theodore Ts'odd018f52000-02-06 23:57:07 +0000407 bb_count += bb_output(currently_testing++);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000408 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000409 }
410 num_blocks = 0;
411 alarm(0);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400412 if (s_flag || v_flag)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400413 fputs(_(done_string), stderr);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000414
Theodore Ts'of3db3561997-04-26 13:34:30 +0000415 fflush (stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000416 free (blkbuf);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000417
418 ext2fs_badblocks_list_iterate_end(bb_iter);
419
420 return bb_count;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000421}
422
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000423static unsigned int test_rw (int dev, unsigned long last_block,
Theodore Ts'odd018f52000-02-06 23:57:07 +0000424 int block_size, unsigned long from_count,
425 unsigned long blocks_at_once)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000426{
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400427 unsigned char *buffer, *read_buffer;
Theodore Ts'o84c05452003-05-18 01:11:52 -0400428 const unsigned long patterns[] = {0xaa, 0x55, 0xff, 0x00};
429 const unsigned long *pattern;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400430 int i, try, got, nr_pattern, pat_idx;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000431 unsigned int bb_count = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000432
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400433 buffer = allocate_buffer(2 * blocks_at_once * block_size);
434 read_buffer = buffer + blocks_at_once * block_size;
435
436 if (!buffer) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000437 com_err (program_name, ENOMEM, _("while allocating buffers"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438 exit (1);
439 }
440
Theodore Ts'o4d404542001-01-11 16:04:59 +0000441 flush_bufs();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000442
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000443 if (v_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500444 fputs(_("Checking for bad blocks in read-write mode\n"),
445 stderr);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000446 fprintf(stderr, _("From block %lu to %lu\n"),
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000447 from_count, last_block);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000448 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400449 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++) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400457 pattern_fill(buffer, pattern[pat_idx],
458 blocks_at_once * block_size);
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000459 num_blocks = last_block;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000460 currently_testing = from_count;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000461 if (s_flag && v_flag <= 1)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000462 alarm_intr(SIGALRM);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400463
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);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000470 if (v_flag > 1)
471 print_status();
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400472
473 currently_testing += got;
474 if (got == try) {
475 try = blocks_at_once;
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400476 /* recover page-aligned offset for O_DIRECT */
Theodore Ts'o54434922003-12-07 01:28:50 -0500477 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400478 && (currently_testing %
479 (sys_page_size >> 9)!= 0))
480 try -= (sys_page_size >> 9)
481 - (currently_testing
482 % (sys_page_size >> 9));
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400483 continue;
484 } else
485 try = 1;
486 if (got == 0) {
487 bb_count += bb_output(currently_testing++);
488 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000489 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400490
Theodore Ts'of3db3561997-04-26 13:34:30 +0000491 num_blocks = 0;
492 alarm (0);
493 if (s_flag | v_flag)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400494 fputs(_(done_string), stderr);
Theodore Ts'o4d404542001-01-11 16:04:59 +0000495 flush_bufs();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000496 if (s_flag | v_flag)
Theodore Ts'o54434922003-12-07 01:28:50 -0500497 fputs(_("Reading and comparing: "), stderr);
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000498 num_blocks = last_block;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000499 currently_testing = from_count;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000500 if (s_flag && v_flag <= 1)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000501 alarm_intr(SIGALRM);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400502
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;
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400520 /* recover page-aligned offset for O_DIRECT */
Theodore Ts'o54434922003-12-07 01:28:50 -0500521 if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400522 && (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;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000528 if (v_flag > 1)
529 print_status();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400531
Theodore Ts'of3db3561997-04-26 13:34:30 +0000532 num_blocks = 0;
533 alarm (0);
534 if (s_flag | v_flag)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400535 fputs(_(done_string), stderr);
Theodore Ts'o4d404542001-01-11 16:04:59 +0000536 flush_bufs();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000537 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400538 uncapture_terminate();
Theodore Ts'o6d40f562003-05-07 08:35:38 -0400539 free(buffer);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000540 return bb_count;
541}
542
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000543struct saved_blk_record {
544 blk_t block;
545 int num;
546};
547
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000548static unsigned int test_nd (int dev, unsigned long last_block,
Theodore Ts'odd018f52000-02-06 23:57:07 +0000549 int block_size, unsigned long from_count,
550 unsigned long blocks_at_once)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000551{
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400552 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400553 unsigned char *test_base, *save_base, *read_base;
Theodore Ts'odd018f52000-02-06 23:57:07 +0000554 int try, i;
Theodore Ts'o84c05452003-05-18 01:11:52 -0400555 const unsigned long patterns[] = { ~0 };
556 const unsigned long *pattern;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400557 int nr_pattern, pat_idx;
Theodore Ts'oa551b782000-07-13 22:05:31 +0000558 long got, used2, written, save_currently_testing;
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000559 struct saved_blk_record *test_record;
Theodore Ts'oa551b782000-07-13 22:05:31 +0000560 /* This is static to prevent being clobbered by the longjmp */
561 static int num_saved;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000562 jmp_buf terminate_env;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000563 errcode_t errcode;
Theodore Ts'o54434922003-12-07 01:28:50 -0500564 unsigned long buf_used;
565 static unsigned int bb_count;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000566
Theodore Ts'o54434922003-12-07 01:28:50 -0500567 bb_count = 0;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000568 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
569 if (errcode) {
Theodore Ts'odd018f52000-02-06 23:57:07 +0000570 com_err (program_name, errcode,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000571 _("while beginning bad block list iteration"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000572 exit (1);
573 }
574 do {
575 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
576 } while (next_bad && next_bad < from_count);
577
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400578 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000579 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
580 if (!blkbuf || !test_record) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000581 com_err(program_name, ENOMEM, _("while allocating buffers"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000582 exit (1);
583 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400584
585 save_base = blkbuf;
586 test_base = blkbuf + (blocks_at_once * block_size);
587 read_base = blkbuf + (2 * blocks_at_once * block_size);
588
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000589 num_saved = 0;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000590
Theodore Ts'o4d404542001-01-11 16:04:59 +0000591 flush_bufs();
Theodore Ts'o879ac922000-01-18 20:59:11 +0000592 if (v_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500593 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000594 fprintf (stderr, _("From block %lu to %lu\n"), from_count, last_block);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000595 }
Theodore Ts'o879ac922000-01-18 20:59:11 +0000596 if (s_flag || v_flag > 1) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500597 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000598 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000599 if (setjmp(terminate_env)) {
600 /*
601 * Abnormal termination by a signal is handled here.
Theodore Ts'o4d003982000-04-03 16:01:11 +0000602 */
Theodore Ts'oa551b782000-07-13 22:05:31 +0000603 signal (SIGALRM, SIG_IGN);
Theodore Ts'o54434922003-12-07 01:28:50 -0500604 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000605
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400606 save_ptr = save_base;
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000607 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 }
Theodore Ts'o879ac922000-01-18 20:59:11 +0000612 fflush (out);
Theodore Ts'odd018f52000-02-06 23:57:07 +0000613 exit(1);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000614 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000615
616 /* set up abend handler */
617 capture_terminate(terminate_env);
618
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400619 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++) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400627 pattern_fill(test_base, pattern[pat_idx],
628 blocks_at_once * block_size);
Theodore Ts'o4d003982000-04-03 16:01:11 +0000629
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400630 buf_used = 0;
631 bb_count = 0;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400632 save_ptr = save_base;
633 test_ptr = test_base;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400634 currently_testing = from_count;
635 num_blocks = last_block;
636 if (s_flag && v_flag <= 1)
637 alarm_intr(SIGALRM);
638
639 while (currently_testing < last_block) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400640 got = try = blocks_at_once - buf_used;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400641 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;
650 }
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++);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000658 goto check_for_more;
Theodore Ts'o4d003982000-04-03 16:01:11 +0000659 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000660
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400661 /*
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++;
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000668
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400669 /* 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);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000676
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400677 buf_used += got;
Theodore Ts'o4d003982000-04-03 16:01:11 +0000678 save_ptr += got * block_size;
679 test_ptr += got * block_size;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400680 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;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400708 save_ptr = save_base;
709 test_ptr = test_base;
710 read_ptr = read_base;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400711 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
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400736 /* write back original data */
737 do_write (dev, save_ptr, got,
738 block_size, currently_testing);
739 save_ptr += got * block_size;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400740
741 currently_testing += got;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400742 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;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400750 save_ptr = save_base;
751 test_ptr = test_base;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400752 currently_testing = save_currently_testing;
Theodore Ts'o4d003982000-04-03 16:01:11 +0000753 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400754 num_blocks = 0;
755 alarm(0);
756 if (s_flag || v_flag > 1)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400757 fputs(_(done_string), stderr);
Theodore Ts'o4d003982000-04-03 16:01:11 +0000758
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400759 flush_bufs();
Theodore Ts'o4d003982000-04-03 16:01:11 +0000760 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000761 uncapture_terminate();
Theodore Ts'odd018f52000-02-06 23:57:07 +0000762 fflush(stderr);
763 free(blkbuf);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000764 free(test_record);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000765
766 ext2fs_badblocks_list_iterate_end(bb_iter);
767
768 return bb_count;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000769}
770
Theodore Ts'o981dc562000-07-06 14:13:29 +0000771static 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 }
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400783 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);
Theodore Ts'o981dc562000-07-06 14:13:29 +0000793 }
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400794
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;
802 }
803
Theodore Ts'o981dc562000-07-06 14:13:29 +0000804}
805
806
Theodore Ts'o00e54331997-09-16 02:13:52 +0000807int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000808{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000809 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000810 char * tmp;
811 char * device_name;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000812 char * host_device_name = NULL;
813 char * input_file = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000814 char * output_file = NULL;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000815 FILE * in = NULL;
Theodore Ts'odd018f52000-02-06 23:57:07 +0000816 int block_size = 1024;
Theodore Ts'o167af992004-02-28 08:20:41 -0500817 unsigned long blocks_at_once = 64;
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000818 blk_t last_block, from_count;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000819 int num_passes = 0;
820 int passes_clean = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000821 int dev;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000822 errcode_t errcode;
Theodore Ts'o84c05452003-05-18 01:11:52 -0400823 unsigned long pattern;
Theodore Ts'o8820c792001-01-06 04:20:03 +0000824 unsigned int (*test_func)(int, unsigned long,
825 int, unsigned long,
826 unsigned long);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400827 int open_flag = 0;
828 long sysval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000829
830 setbuf(stdout, NULL);
831 setbuf(stderr, NULL);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000832#ifdef ENABLE_NLS
833 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500834 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000835 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
836 textdomain(NLS_CAT_NAME);
837#endif
Theodore Ts'o6d40f562003-05-07 08:35:38 -0400838 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
Theodore Ts'o4d003982000-04-03 16:01:11 +0000839 test_func = test_ro;
Theodore Ts'o4d404542001-01-11 16:04:59 +0000840
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400841 /* 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
Theodore Ts'o3839e651997-04-26 13:21:57 +0000853 if (argc && *argv)
854 program_name = *argv;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400855 while ((c = getopt (argc, argv, "b:fi:o:svwnc:p:h:t:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000856 switch (c) {
857 case 'b':
858 block_size = strtoul (optarg, &tmp, 0);
859 if (*tmp || block_size > 4096) {
860 com_err (program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000861 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000862 exit (1);
863 }
864 break;
Theodore Ts'o981dc562000-07-06 14:13:29 +0000865 case 'f':
866 force++;
867 break;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000868 case 'i':
869 input_file = optarg;
870 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000871 case 'o':
872 output_file = optarg;
873 break;
874 case 's':
875 s_flag = 1;
876 break;
877 case 'v':
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000878 v_flag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000879 break;
880 case 'w':
Theodore Ts'o4d003982000-04-03 16:01:11 +0000881 if (w_flag)
882 usage();
883 test_func = test_rw;
884 w_flag = 1;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000885 break;
886 case 'n':
Theodore Ts'o4d003982000-04-03 16:01:11 +0000887 if (w_flag)
888 usage();
889 test_func = test_nd;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000890 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;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000910 break;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400911 case 't':
912 if (t_flag + 1 > t_max) {
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400913 unsigned long *t_patts_new;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400914
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 }
Theodore Ts'o84c05452003-05-18 01:11:52 -0400926 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,
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400932 _("invalid test_pattern: %s\n"),
Theodore Ts'o84c05452003-05-18 01:11:52 -0400933 optarg);
934 exit(1);
935 }
Theodore Ts'o54434922003-12-07 01:28:50 -0500936 if (pattern == (unsigned long) ~0)
Theodore Ts'o84c05452003-05-18 01:11:52 -0400937 pattern = 0xffff;
938 t_patts[t_flag++] = pattern;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400939 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400940 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000941 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +0000942 usage();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000943 }
944 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400945 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 }
Theodore Ts'o54434922003-12-07 01:28:50 -0500952 if (t_patts && (t_patts[0] == (unsigned long) ~0)) {
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400953 com_err(program_name, 0,
954 _("Random test_pattern is not allowed "
955 "in read-only mode"));
956 exit(1);
957 }
958 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000959 if (optind > argc - 1)
Theodore Ts'o818180c1998-06-27 05:11:14 +0000960 usage();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000961 device_name = argv[optind++];
Theodore Ts'o35964b52000-07-06 13:19:43 +0000962 if (optind > argc - 1) {
963 errcode = ext2fs_get_device_size(device_name,
964 block_size,
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000965 &last_block);
Theodore Ts'o35964b52000-07-06 13:19:43 +0000966 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 {
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000978 last_block = strtoul (argv[optind], &tmp, 0);
Theodore Ts'o35964b52000-07-06 13:19:43 +0000979 if (*tmp) {
Theodore Ts'of37ab682005-05-05 23:15:55 -0400980 com_err (program_name, 0, _("invalid blocks count - %s"),
Theodore Ts'o35964b52000-07-06 13:19:43 +0000981 argv[optind]);
982 exit (1);
983 }
984 optind++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000985 }
Theodore Ts'o35964b52000-07-06 13:19:43 +0000986 if (optind <= argc-1) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000987 from_count = strtoul (argv[optind], &tmp, 0);
Theodore Ts'oa551b782000-07-13 22:05:31 +0000988 if (*tmp) {
Theodore Ts'of37ab682005-05-05 23:15:55 -0400989 com_err (program_name, 0, _("invalid starting block - %s"),
Theodore Ts'oa551b782000-07-13 22:05:31 +0000990 argv[optind]);
991 exit (1);
992 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000993 } else from_count = 0;
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000994 if (from_count >= last_block) {
Theodore Ts'of37ab682005-05-05 23:15:55 -0400995 com_err (program_name, 0, _("invalid blocks range: %lu-%lu"),
Theodore Ts'o54434922003-12-07 01:28:50 -0500996 (unsigned long) from_count, (unsigned long) last_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000997 exit (1);
998 }
Theodore Ts'o981dc562000-07-06 14:13:29 +0000999 if (w_flag)
1000 check_mount(device_name);
1001
Theodore Ts'o1c29b092003-07-12 16:01:45 -04001002 open_flag = w_flag ? O_RDWR : O_RDONLY;
1003 dev = open (device_name, open_flag);
Theodore Ts'o5493a272002-01-02 14:15:35 -05001004 if (dev == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001005 com_err (program_name, errno, _("while trying to open %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001006 device_name);
1007 exit (1);
1008 }
Theodore Ts'o879ac922000-01-18 20:59:11 +00001009 if (host_device_name) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -04001010 host_dev = open (host_device_name, open_flag);
Theodore Ts'o5493a272002-01-02 14:15:35 -05001011 if (host_dev == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001012 com_err (program_name, errno,
1013 _("while trying to open %s"),
1014 host_device_name);
Theodore Ts'o879ac922000-01-18 20:59:11 +00001015 exit (1);
1016 }
1017 } else
1018 host_dev = dev;
Theodore Ts'o3e699062002-10-13 23:56:28 -04001019 if (input_file) {
Theodore Ts'o879ac922000-01-18 20:59:11 +00001020 if (strcmp (input_file, "-") == 0)
1021 in = stdin;
1022 else {
1023 in = fopen (input_file, "r");
1024 if (in == NULL)
1025 {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001026 com_err (program_name, errno,
1027 _("while trying to open %s"),
Theodore Ts'o879ac922000-01-18 20:59:11 +00001028 input_file);
1029 exit (1);
1030 }
1031 }
Theodore Ts'o3e699062002-10-13 23:56:28 -04001032 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001033 if (output_file && strcmp (output_file, "-") != 0)
1034 {
1035 out = fopen (output_file, "w");
1036 if (out == NULL)
1037 {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001038 com_err (program_name, errno,
1039 _("while trying to open %s"),
Theodore Ts'o879ac922000-01-18 20:59:11 +00001040 output_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001041 exit (1);
1042 }
1043 }
1044 else
1045 out = stdout;
Theodore Ts'o879ac922000-01-18 20:59:11 +00001046
1047 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1048 if (errcode) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001049 com_err (program_name, errcode,
1050 _("creating in-memory bad blocks list"));
Theodore Ts'o879ac922000-01-18 20:59:11 +00001051 exit (1);
1052 }
1053
1054 if (in) {
1055 for(;;) {
Theodore Ts'oa551b782000-07-13 22:05:31 +00001056 switch(fscanf (in, "%u\n", &next_bad)) {
Theodore Ts'o879ac922000-01-18 20:59:11 +00001057 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) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001065 com_err (program_name, errcode, _("adding to in-memory bad block list"));
Theodore Ts'o879ac922000-01-18 20:59:11 +00001066 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
Theodore Ts'ocd130a02001-05-05 05:43:23 +00001080 bb_count = test_func(dev, last_block, block_size,
Theodore Ts'o4d003982000-04-03 16:01:11 +00001081 from_count, blocks_at_once);
1082 if (bb_count)
1083 passes_clean = 0;
1084 else
1085 ++passes_clean;
1086
Theodore Ts'o879ac922000-01-18 20:59:11 +00001087 if (v_flag)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001088 fprintf(stderr,
1089 _("Pass completed, %u bad blocks found.\n"),
1090 bb_count);
Theodore Ts'o879ac922000-01-18 20:59:11 +00001091
1092 } while (passes_clean < num_passes);
1093
Theodore Ts'o3839e651997-04-26 13:21:57 +00001094 close (dev);
1095 if (out != stdout)
1096 fclose (out);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001097 if (t_patts)
1098 free(t_patts);
Theodore Ts'o879ac922000-01-18 20:59:11 +00001099 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001100}
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001101