blob: 866144e191bcc60e3847837a603adb4e1bc47056 [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'o5267a522007-06-04 01:49:51 -040049#include <limits.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000050
Iustin Popedf261f2008-06-18 22:26:26 +020051#include <sys/time.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000052#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000053#include <sys/types.h>
Eric Sandeen79e62402008-07-06 18:36:56 -040054#include <sys/time.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000055
Theodore Ts'o3839e651997-04-26 13:21:57 +000056#include "et/com_err.h"
Theodore Ts'od40259f1997-10-20 00:44:26 +000057#include "ext2fs/ext2_io.h"
Theodore Ts'o54c637d2001-05-14 11:45:38 +000058#include "ext2fs/ext2_fs.h"
Theodore Ts'o879ac922000-01-18 20:59:11 +000059#include "ext2fs/ext2fs.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000060#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000061
62const char * program_name = "badblocks";
Theodore Ts'of63978a2006-05-13 09:25:47 -040063const char * done_string = N_("done \n");
Theodore Ts'o3839e651997-04-26 13:21:57 +000064
Theodore Ts'o4d003982000-04-03 16:01:11 +000065static int v_flag = 0; /* verbose */
66static int w_flag = 0; /* do r/w test: 0=no, 1=yes,
67 * 2=non-destructive */
68static int s_flag = 0; /* show progress of test */
Theodore Ts'o981dc562000-07-06 14:13:29 +000069static int force = 0; /* force check of mounted device */
Theodore Ts'o849b6bc2003-05-07 09:52:14 -040070static int t_flag = 0; /* number of test patterns */
71static int t_max = 0; /* allocated test patterns */
Theodore Ts'oe9860ae2007-10-22 09:51:50 -040072static unsigned int *t_patts = NULL; /* test patterns */
Theodore Ts'o1c29b092003-07-12 16:01:45 -040073static int current_O_DIRECT = 0; /* Current status of O_DIRECT flag */
Theodore Ts'of63978a2006-05-13 09:25:47 -040074static int exclusive_ok = 0;
Iustin Pop931b0282008-06-11 13:12:17 +020075static unsigned int max_bb = 0; /* Abort test if more than this number of bad blocks has been encountered */
Iustin Pop264f64a2008-06-12 09:30:04 +020076static unsigned int d_flag = 0; /* delay factor between reads */
Theodore Ts'o1c29b092003-07-12 16:01:45 -040077
Theodore Ts'o849b6bc2003-05-07 09:52:14 -040078#define T_INC 32
Theodore Ts'o4d003982000-04-03 16:01:11 +000079
Theodore Ts'oacd77412007-10-22 10:09:05 -040080unsigned int sys_page_size = 4096;
Theodore Ts'o1c29b092003-07-12 16:01:45 -040081
Theodore Ts'o8820c792001-01-06 04:20:03 +000082static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000083{
Iustin Pop264f64a2008-06-12 09:30:04 +020084 fprintf(stderr, _("Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwnf]\n [-c blocks_at_once] [-p num_passes] [-e max_bad_blocks] [-d delay_factor_between_reads] [-t test_pattern [-t test_pattern [...]]]\n device [last_block [start_block]]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000085 program_name);
86 exit (1);
87}
88
Theodore Ts'od8b5f772006-11-12 23:09:03 -050089static void exclusive_usage(void)
90{
91 fprintf(stderr,
Theodore Ts'o017a76e2006-11-17 23:00:19 -050092 _("%s: The -n and -w options are mutually exclusive.\n\n"),
93 program_name);
94 exit(1);
Theodore Ts'od8b5f772006-11-12 23:09:03 -050095}
96
Theodore Ts'oacd77412007-10-22 10:09:05 -040097static blk_t currently_testing = 0;
98static blk_t num_blocks = 0;
Theodore Ts'o879ac922000-01-18 20:59:11 +000099static ext2_badblocks_list bb_list = NULL;
100static FILE *out;
101static blk_t next_bad = 0;
102static ext2_badblocks_iterate bb_iter = NULL;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000103
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400104static void *allocate_buffer(size_t size)
105{
106 void *ret = 0;
107
108#ifdef HAVE_POSIX_MEMALIGN
109 if (posix_memalign(&ret, sys_page_size, size) < 0)
110 ret = 0;
111#else
112#ifdef HAVE_MEMALIGN
113 ret = memalign(sys_page_size, size);
114#else
115#ifdef HAVE_VALLOC
116 ret = valloc(size);
117#endif /* HAVE_VALLOC */
118#endif /* HAVE_MEMALIGN */
119#endif /* HAVE_POSIX_MEMALIGN */
120
121 if (!ret)
122 ret = malloc(size);
123
124 return ret;
125}
126
Theodore Ts'odd018f52000-02-06 23:57:07 +0000127/*
128 * This routine reports a new bad block. If the bad block has already
129 * been seen before, then it returns 0; otherwise it returns 1.
130 */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400131static int bb_output (blk_t bad)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000132{
133 errcode_t errcode;
134
Theodore Ts'odd018f52000-02-06 23:57:07 +0000135 if (ext2fs_badblocks_list_test(bb_list, bad))
136 return 0;
137
Theodore Ts'oacd77412007-10-22 10:09:05 -0400138 fprintf(out, "%lu\n", (unsigned long) bad);
Theodore Ts'occ4f98e2003-04-03 11:37:46 -0500139 fflush(out);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000140
141 errcode = ext2fs_badblocks_list_add (bb_list, bad);
142 if (errcode) {
143 com_err (program_name, errcode, "adding to in-memory bad block list");
144 exit (1);
145 }
146
147 /* kludge:
148 increment the iteration through the bb_list if
149 an element was just added before the current iteration
150 position. This should not cause next_bad to change. */
151 if (bb_iter && bad < next_bad)
152 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
Theodore Ts'odd018f52000-02-06 23:57:07 +0000153 return 1;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000154}
155
Theodore Ts'o8820c792001-01-06 04:20:03 +0000156static void print_status(void)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000157{
Theodore Ts'oacd77412007-10-22 10:09:05 -0400158 fprintf(stderr, "%15lu/%15lu", (unsigned long) currently_testing,
159 (unsigned long) num_blocks);
Theodore Ts'oc76564a2005-01-06 14:48:59 -0500160 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 +0000161 fflush (stderr);
162}
163
Theodore Ts'o54434922003-12-07 01:28:50 -0500164static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000165{
166 signal (SIGALRM, alarm_intr);
167 alarm(1);
168 if (!num_blocks)
169 return;
Theodore Ts'oc76564a2005-01-06 14:48:59 -0500170 print_status();
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000171}
172
Theodore Ts'o879ac922000-01-18 20:59:11 +0000173static void *terminate_addr = NULL;
174
Theodore Ts'o54434922003-12-07 01:28:50 -0500175static void terminate_intr(int signo EXT2FS_ATTR((unused)))
Theodore Ts'o879ac922000-01-18 20:59:11 +0000176{
177 if (terminate_addr)
178 longjmp(terminate_addr,1);
179 exit(1);
180}
181
Theodore Ts'o981dc562000-07-06 14:13:29 +0000182static void capture_terminate(jmp_buf term_addr)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000183{
184 terminate_addr = term_addr;
185 signal (SIGHUP, terminate_intr);
186 signal (SIGINT, terminate_intr);
187 signal (SIGPIPE, terminate_intr);
188 signal (SIGTERM, terminate_intr);
189 signal (SIGUSR1, terminate_intr);
190 signal (SIGUSR2, terminate_intr);
191}
192
Theodore Ts'o8820c792001-01-06 04:20:03 +0000193static void uncapture_terminate(void)
Theodore Ts'o4d003982000-04-03 16:01:11 +0000194{
195 terminate_addr = NULL;
196 signal (SIGHUP, SIG_DFL);
197 signal (SIGINT, SIG_DFL);
198 signal (SIGPIPE, SIG_DFL);
199 signal (SIGTERM, SIG_DFL);
200 signal (SIGUSR1, SIG_DFL);
201 signal (SIGUSR2, SIG_DFL);
202}
203
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400204static void set_o_direct(int dev, unsigned char *buffer, size_t size,
Theodore Ts'oacd77412007-10-22 10:09:05 -0400205 blk_t current_block)
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400206{
207#ifdef O_DIRECT
208 int new_flag = O_DIRECT;
209 int flag;
210
211 if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400212 ((size & (sys_page_size - 1)) != 0) ||
213 ((current_block & ((sys_page_size >> 9)-1)) != 0))
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400214 new_flag = 0;
215
216 if (new_flag != current_O_DIRECT) {
Theodore Ts'odc058712003-07-25 07:39:33 -0400217 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400218 flag = fcntl(dev, F_GETFL);
219 if (flag > 0) {
220 flag = (flag & ~O_DIRECT) | new_flag;
221 fcntl(dev, F_SETFL, flag);
222 }
223 current_O_DIRECT = new_flag;
224 }
225#endif
226}
227
228
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400229static void pattern_fill(unsigned char *buffer, unsigned int pattern,
Theodore Ts'o84c05452003-05-18 01:11:52 -0400230 size_t n)
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400231{
Theodore Ts'o54434922003-12-07 01:28:50 -0500232 unsigned int i, nb;
Theodore Ts'o84c05452003-05-18 01:11:52 -0400233 unsigned char bpattern[sizeof(pattern)], *ptr;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400234
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400235 if (pattern == (unsigned int) ~0) {
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400236 for (ptr = buffer; ptr < buffer + n; ptr++) {
237 (*ptr) = random() % (1 << (8 * sizeof(char)));
238 }
239 if (s_flag | v_flag)
Theodore Ts'o54434922003-12-07 01:28:50 -0500240 fputs(_("Testing with random pattern: "), stderr);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400241 } else {
242 bpattern[0] = 0;
243 for (i = 0; i < sizeof(bpattern); i++) {
244 if (pattern == 0)
245 break;
246 bpattern[i] = pattern & 0xFF;
247 pattern = pattern >> 8;
248 }
249 nb = i ? (i-1) : 0;
250 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500251 *ptr = bpattern[i];
252 if (i == 0)
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400253 i = nb;
Theodore Ts'o54434922003-12-07 01:28:50 -0500254 else
255 i--;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400256 }
Theodore Ts'o84c05452003-05-18 01:11:52 -0400257 if (s_flag | v_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500258 fputs(_("Testing with pattern 0x"), stderr);
Theodore Ts'o84c05452003-05-18 01:11:52 -0400259 for (i = 0; i <= nb; i++)
260 fprintf(stderr, "%02x", buffer[i]);
Theodore Ts'o54434922003-12-07 01:28:50 -0500261 fputs(": ", stderr);
Theodore Ts'o84c05452003-05-18 01:11:52 -0400262 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400263 }
264}
265
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266/*
Theodore Ts'o879ac922000-01-18 20:59:11 +0000267 * Perform a read of a sequence of blocks; return the number of blocks
268 * successfully sequentially read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269 */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400270static int do_read (int dev, unsigned char * buffer, int try, int block_size,
271 blk_t current_block)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000272{
273 long got;
Iustin Pop264f64a2008-06-12 09:30:04 +0200274 struct timeval tv1, tv2;
275#define NANOSEC (1000000000L)
276#define MILISEC (1000L)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400278 set_o_direct(dev, buffer, try * block_size, current_block);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400279
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000280 if (v_flag > 1)
281 print_status();
282
Theodore Ts'o3839e651997-04-26 13:21:57 +0000283 /* Seek to the correct loc. */
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000284 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000285 SEEK_SET) != (ext2_loff_t) current_block * block_size)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000286 com_err (program_name, errno, _("during seek"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000287
288 /* Try the read */
Iustin Pop264f64a2008-06-12 09:30:04 +0200289 if (d_flag)
Iustin Popedf261f2008-06-18 22:26:26 +0200290 gettimeofday(&tv1, NULL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291 got = read (dev, buffer, try * block_size);
Iustin Pop264f64a2008-06-12 09:30:04 +0200292 if (d_flag)
Iustin Popedf261f2008-06-18 22:26:26 +0200293 gettimeofday(&tv2, NULL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000294 if (got < 0)
295 got = 0;
Theodore Ts'o9f10a7b1999-07-16 10:41:36 +0000296 if (got & 511)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000297 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000298 got /= block_size;
Iustin Pop264f64a2008-06-12 09:30:04 +0200299 if (d_flag && got == try) {
300 struct timespec ts;
301 ts.tv_sec = tv2.tv_sec - tv1.tv_sec;
302 ts.tv_nsec = (tv2.tv_usec - tv1.tv_usec) * MILISEC;
303 if (ts.tv_nsec < 0) {
304 ts.tv_nsec += NANOSEC;
305 ts.tv_sec -= 1;
306 }
307 /* increase/decrease the sleep time based on d_flag value */
308 ts.tv_sec = ts.tv_sec * d_flag / 100;
309 ts.tv_nsec = ts.tv_nsec * d_flag / 100;
310 if (ts.tv_nsec > NANOSEC) {
311 ts.tv_sec += ts.tv_nsec / NANOSEC;
312 ts.tv_nsec %= NANOSEC;
313 }
314 if (ts.tv_sec || ts.tv_nsec)
315 nanosleep(&ts, NULL);
316 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000317 return got;
318}
319
Theodore Ts'o879ac922000-01-18 20:59:11 +0000320/*
321 * Perform a write of a sequence of blocks; return the number of blocks
322 * successfully sequentially written.
323 */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400324static int do_write(int dev, unsigned char * buffer, int try, int block_size,
325 unsigned long current_block)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000326{
327 long got;
328
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400329 set_o_direct(dev, buffer, try * block_size, current_block);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400330
Theodore Ts'o879ac922000-01-18 20:59:11 +0000331 if (v_flag > 1)
332 print_status();
333
334 /* Seek to the correct loc. */
335 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
336 SEEK_SET) != (ext2_loff_t) current_block * block_size)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000337 com_err (program_name, errno, _("during seek"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000338
339 /* Try the write */
340 got = write (dev, buffer, try * block_size);
341 if (got < 0)
342 got = 0;
343 if (got & 511)
Theodore Ts'o54434922003-12-07 01:28:50 -0500344 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000345 got /= block_size;
346 return got;
347}
348
349static int host_dev;
350
Theodore Ts'o4d404542001-01-11 16:04:59 +0000351static void flush_bufs(void)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000352{
Theodore Ts'o4d404542001-01-11 16:04:59 +0000353 errcode_t retval;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000354
Theodore Ts'o4d404542001-01-11 16:04:59 +0000355 retval = ext2fs_sync_device(host_dev, 1);
356 if (retval)
357 com_err(program_name, retval, _("during ext2fs_sync_device"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000358}
359
Theodore Ts'oacd77412007-10-22 10:09:05 -0400360static unsigned int test_ro (int dev, blk_t last_block,
361 int block_size, blk_t from_count,
362 unsigned int blocks_at_once)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000363{
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400364 unsigned char * blkbuf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000365 int try;
Theodore Ts'oacd77412007-10-22 10:09:05 -0400366 int got;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000367 unsigned int bb_count = 0;
368 errcode_t errcode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000369
Theodore Ts'o879ac922000-01-18 20:59:11 +0000370 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
371 if (errcode) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000372 com_err (program_name, errcode,
373 _("while beginning bad block list iteration"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000374 exit (1);
375 }
376 do {
377 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
378 } while (next_bad && next_bad < from_count);
379
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400380 if (t_flag) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400381 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400382 } else {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400383 blkbuf = allocate_buffer(blocks_at_once * block_size);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400384 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000385 if (!blkbuf)
386 {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000387 com_err (program_name, ENOMEM, _("while allocating buffers"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000388 exit (1);
389 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000390 if (v_flag) {
Theodore Ts'oacd77412007-10-22 10:09:05 -0400391 fprintf (stderr, _("Checking blocks %lu to %lu\n"),
392 (unsigned long) from_count,
393 (unsigned long) last_block - 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000394 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400395 if (t_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500396 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400397 pattern_fill(blkbuf + blocks_at_once * block_size,
398 t_patts[0], block_size);
399 }
400 flush_bufs();
Theodore Ts'o879ac922000-01-18 20:59:11 +0000401 try = blocks_at_once;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000402 currently_testing = from_count;
Theodore Ts'o8938ce62006-10-03 23:35:57 -0400403 num_blocks = last_block - 1;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400404 if (!t_flag && (s_flag || v_flag)) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500405 fputs(_("Checking for bad blocks (read-only test): "), stderr);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000406 if (v_flag <= 1)
407 alarm_intr(SIGALRM);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000408 }
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000409 while (currently_testing < last_block)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000410 {
Iustin Pop931b0282008-06-11 13:12:17 +0200411 if (max_bb && bb_count >= max_bb) {
412 if (s_flag || v_flag) {
413 fputs(_("Too many bad blocks, aborting test\n"), stderr);
414 }
415 break;
416 }
Theodore Ts'o879ac922000-01-18 20:59:11 +0000417 if (next_bad) {
418 if (currently_testing == next_bad) {
419 /* fprintf (out, "%lu\n", nextbad); */
420 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
421 currently_testing++;
422 continue;
423 }
424 else if (currently_testing + try > next_bad)
425 try = next_bad - currently_testing;
426 }
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000427 if (currently_testing + try > last_block)
428 try = last_block - currently_testing;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000429 got = do_read (dev, blkbuf, try, block_size, currently_testing);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400430 if (t_flag) {
431 /* test the comparison between all the
432 blocks successfully read */
433 int i;
434 for (i = 0; i < got; ++i)
435 if (memcmp (blkbuf+i*block_size,
436 blkbuf+blocks_at_once*block_size,
437 block_size))
438 bb_count += bb_output(currently_testing + i);
439 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440 currently_testing += got;
441 if (got == try) {
Theodore Ts'o879ac922000-01-18 20:59:11 +0000442 try = blocks_at_once;
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400443 /* recover page-aligned offset for O_DIRECT */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400444 if ( (blocks_at_once >= sys_page_size >> 9)
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400445 && (currently_testing % (sys_page_size >> 9)!= 0))
446 try -= (sys_page_size >> 9)
447 - (currently_testing
448 % (sys_page_size >> 9));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000449 continue;
450 }
451 else
452 try = 1;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000453 if (got == 0) {
Theodore Ts'odd018f52000-02-06 23:57:07 +0000454 bb_count += bb_output(currently_testing++);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000455 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000456 }
457 num_blocks = 0;
458 alarm(0);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400459 if (s_flag || v_flag)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400460 fputs(_(done_string), stderr);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000461
Theodore Ts'of3db3561997-04-26 13:34:30 +0000462 fflush (stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463 free (blkbuf);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000464
465 ext2fs_badblocks_list_iterate_end(bb_iter);
466
467 return bb_count;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000468}
469
Theodore Ts'oacd77412007-10-22 10:09:05 -0400470static unsigned int test_rw (int dev, blk_t last_block,
471 int block_size, blk_t from_count,
472 unsigned int blocks_at_once)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000473{
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400474 unsigned char *buffer, *read_buffer;
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400475 const unsigned int patterns[] = {0xaa, 0x55, 0xff, 0x00};
476 const unsigned int *pattern;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400477 int i, try, got, nr_pattern, pat_idx;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000478 unsigned int bb_count = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000479
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400480 buffer = allocate_buffer(2 * blocks_at_once * block_size);
481 read_buffer = buffer + blocks_at_once * block_size;
482
483 if (!buffer) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000484 com_err (program_name, ENOMEM, _("while allocating buffers"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485 exit (1);
486 }
487
Theodore Ts'o4d404542001-01-11 16:04:59 +0000488 flush_bufs();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000489
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000490 if (v_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500491 fputs(_("Checking for bad blocks in read-write mode\n"),
492 stderr);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000493 fprintf(stderr, _("From block %lu to %lu\n"),
Theodore Ts'oacd77412007-10-22 10:09:05 -0400494 (unsigned long) from_count,
495 (unsigned long) last_block);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000496 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400497 if (t_flag) {
498 pattern = t_patts;
499 nr_pattern = t_flag;
500 } else {
501 pattern = patterns;
502 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
503 }
504 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400505 pattern_fill(buffer, pattern[pat_idx],
506 blocks_at_once * block_size);
Theodore Ts'o8938ce62006-10-03 23:35:57 -0400507 num_blocks = last_block - 1;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000508 currently_testing = from_count;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000509 if (s_flag && v_flag <= 1)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000510 alarm_intr(SIGALRM);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400511
512 try = blocks_at_once;
513 while (currently_testing < last_block) {
Iustin Pop931b0282008-06-11 13:12:17 +0200514 if (max_bb && bb_count >= max_bb) {
515 if (s_flag || v_flag) {
516 fputs(_("Too many bad blocks, aborting test\n"), stderr);
517 }
518 break;
519 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400520 if (currently_testing + try > last_block)
521 try = last_block - currently_testing;
522 got = do_write(dev, buffer, try, block_size,
523 currently_testing);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000524 if (v_flag > 1)
525 print_status();
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400526
527 currently_testing += got;
528 if (got == try) {
529 try = blocks_at_once;
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400530 /* recover page-aligned offset for O_DIRECT */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400531 if ( (blocks_at_once >= sys_page_size >> 9)
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400532 && (currently_testing %
533 (sys_page_size >> 9)!= 0))
534 try -= (sys_page_size >> 9)
535 - (currently_testing
536 % (sys_page_size >> 9));
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400537 continue;
538 } else
539 try = 1;
540 if (got == 0) {
541 bb_count += bb_output(currently_testing++);
542 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000543 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400544
Theodore Ts'of3db3561997-04-26 13:34:30 +0000545 num_blocks = 0;
546 alarm (0);
547 if (s_flag | v_flag)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400548 fputs(_(done_string), stderr);
Theodore Ts'o4d404542001-01-11 16:04:59 +0000549 flush_bufs();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000550 if (s_flag | v_flag)
Theodore Ts'o54434922003-12-07 01:28:50 -0500551 fputs(_("Reading and comparing: "), stderr);
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000552 num_blocks = last_block;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000553 currently_testing = from_count;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000554 if (s_flag && v_flag <= 1)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000555 alarm_intr(SIGALRM);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400556
557 try = blocks_at_once;
558 while (currently_testing < last_block) {
Iustin Pop931b0282008-06-11 13:12:17 +0200559 if (max_bb && bb_count >= max_bb) {
560 if (s_flag || v_flag) {
561 fputs(_("Too many bad blocks, aborting test\n"), stderr);
562 }
563 break;
564 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400565 if (currently_testing + try > last_block)
566 try = last_block - currently_testing;
567 got = do_read (dev, read_buffer, try, block_size,
568 currently_testing);
569 if (got == 0) {
570 bb_count += bb_output(currently_testing++);
571 continue;
572 }
573 for (i=0; i < got; i++) {
574 if (memcmp(read_buffer + i * block_size,
575 buffer + i * block_size,
576 block_size))
577 bb_count += bb_output(currently_testing+i);
578 }
579 currently_testing += got;
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400580 /* recover page-aligned offset for O_DIRECT */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400581 if ( (blocks_at_once >= sys_page_size >> 9)
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400582 && (currently_testing % (sys_page_size >> 9)!= 0))
583 try = blocks_at_once - (sys_page_size >> 9)
584 - (currently_testing
585 % (sys_page_size >> 9));
586 else
587 try = blocks_at_once;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000588 if (v_flag > 1)
589 print_status();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000590 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400591
Theodore Ts'of3db3561997-04-26 13:34:30 +0000592 num_blocks = 0;
593 alarm (0);
594 if (s_flag | v_flag)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400595 fputs(_(done_string), stderr);
Theodore Ts'o4d404542001-01-11 16:04:59 +0000596 flush_bufs();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400598 uncapture_terminate();
Theodore Ts'o6d40f562003-05-07 08:35:38 -0400599 free(buffer);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000600 return bb_count;
601}
602
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000603struct saved_blk_record {
604 blk_t block;
605 int num;
606};
607
Theodore Ts'oacd77412007-10-22 10:09:05 -0400608static unsigned int test_nd (int dev, blk_t last_block,
609 int block_size, blk_t from_count,
610 unsigned int blocks_at_once)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000611{
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400612 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400613 unsigned char *test_base, *save_base, *read_base;
Theodore Ts'odd018f52000-02-06 23:57:07 +0000614 int try, i;
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400615 const unsigned int patterns[] = { ~0 };
616 const unsigned int *pattern;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400617 int nr_pattern, pat_idx;
Theodore Ts'oacd77412007-10-22 10:09:05 -0400618 int got, used2, written;
619 blk_t save_currently_testing;
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000620 struct saved_blk_record *test_record;
Theodore Ts'oa551b782000-07-13 22:05:31 +0000621 /* This is static to prevent being clobbered by the longjmp */
622 static int num_saved;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000623 jmp_buf terminate_env;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000624 errcode_t errcode;
Theodore Ts'o54434922003-12-07 01:28:50 -0500625 unsigned long buf_used;
626 static unsigned int bb_count;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000627
Theodore Ts'o54434922003-12-07 01:28:50 -0500628 bb_count = 0;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000629 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
630 if (errcode) {
Theodore Ts'odd018f52000-02-06 23:57:07 +0000631 com_err (program_name, errcode,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000632 _("while beginning bad block list iteration"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000633 exit (1);
634 }
635 do {
636 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
637 } while (next_bad && next_bad < from_count);
638
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400639 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000640 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
641 if (!blkbuf || !test_record) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000642 com_err(program_name, ENOMEM, _("while allocating buffers"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000643 exit (1);
644 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400645
646 save_base = blkbuf;
647 test_base = blkbuf + (blocks_at_once * block_size);
648 read_base = blkbuf + (2 * blocks_at_once * block_size);
649
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000650 num_saved = 0;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000651
Theodore Ts'o4d404542001-01-11 16:04:59 +0000652 flush_bufs();
Theodore Ts'o879ac922000-01-18 20:59:11 +0000653 if (v_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500654 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
Theodore Ts'oacd77412007-10-22 10:09:05 -0400655 fprintf (stderr, _("From block %lu to %lu\n"),
656 (unsigned long) from_count, (unsigned long) last_block);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000657 }
Theodore Ts'o879ac922000-01-18 20:59:11 +0000658 if (s_flag || v_flag > 1) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500659 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000660 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000661 if (setjmp(terminate_env)) {
662 /*
663 * Abnormal termination by a signal is handled here.
Theodore Ts'o4d003982000-04-03 16:01:11 +0000664 */
Theodore Ts'oa551b782000-07-13 22:05:31 +0000665 signal (SIGALRM, SIG_IGN);
Theodore Ts'o54434922003-12-07 01:28:50 -0500666 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000667
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400668 save_ptr = save_base;
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000669 for (i=0; i < num_saved; i++) {
670 do_write(dev, save_ptr, test_record[i].num,
671 block_size, test_record[i].block);
672 save_ptr += test_record[i].num * block_size;
673 }
Theodore Ts'o879ac922000-01-18 20:59:11 +0000674 fflush (out);
Theodore Ts'odd018f52000-02-06 23:57:07 +0000675 exit(1);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000676 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000677
678 /* set up abend handler */
679 capture_terminate(terminate_env);
680
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400681 if (t_flag) {
682 pattern = t_patts;
683 nr_pattern = t_flag;
684 } else {
685 pattern = patterns;
686 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
687 }
688 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400689 pattern_fill(test_base, pattern[pat_idx],
690 blocks_at_once * block_size);
Theodore Ts'o4d003982000-04-03 16:01:11 +0000691
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400692 buf_used = 0;
693 bb_count = 0;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400694 save_ptr = save_base;
695 test_ptr = test_base;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400696 currently_testing = from_count;
Theodore Ts'o8938ce62006-10-03 23:35:57 -0400697 num_blocks = last_block - 1;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400698 if (s_flag && v_flag <= 1)
699 alarm_intr(SIGALRM);
700
701 while (currently_testing < last_block) {
Iustin Pop931b0282008-06-11 13:12:17 +0200702 if (max_bb && bb_count >= max_bb) {
703 if (s_flag || v_flag) {
704 fputs(_("Too many bad blocks, aborting test\n"), stderr);
705 }
706 break;
707 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400708 got = try = blocks_at_once - buf_used;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400709 if (next_bad) {
710 if (currently_testing == next_bad) {
711 /* fprintf (out, "%lu\n", nextbad); */
712 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
713 currently_testing++;
714 goto check_for_more;
715 }
716 else if (currently_testing + try > next_bad)
717 try = next_bad - currently_testing;
718 }
719 if (currently_testing + try > last_block)
720 try = last_block - currently_testing;
721 got = do_read (dev, save_ptr, try, block_size,
722 currently_testing);
723 if (got == 0) {
724 /* First block must have been bad. */
725 bb_count += bb_output(currently_testing++);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000726 goto check_for_more;
Theodore Ts'o4d003982000-04-03 16:01:11 +0000727 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000728
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400729 /*
730 * Note the fact that we've saved this much data
731 * *before* we overwrite it with test data
732 */
733 test_record[num_saved].block = currently_testing;
734 test_record[num_saved].num = got;
735 num_saved++;
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000736
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400737 /* Write the test data */
738 written = do_write (dev, test_ptr, got, block_size,
739 currently_testing);
740 if (written != got)
741 com_err (program_name, errno,
742 _("during test data write, block %lu"),
Theodore Ts'oacd77412007-10-22 10:09:05 -0400743 (unsigned long) currently_testing +
744 written);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000745
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400746 buf_used += got;
Theodore Ts'o4d003982000-04-03 16:01:11 +0000747 save_ptr += got * block_size;
748 test_ptr += got * block_size;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400749 currently_testing += got;
750 if (got != try)
751 bb_count += bb_output(currently_testing++);
752
753 check_for_more:
754 /*
755 * If there's room for more blocks to be tested this
756 * around, and we're not done yet testing the disk, go
757 * back and get some more blocks.
758 */
759 if ((buf_used != blocks_at_once) &&
760 (currently_testing < last_block))
761 continue;
762
763 flush_bufs();
764 save_currently_testing = currently_testing;
765
766 /*
767 * for each contiguous block that we read into the
768 * buffer (and wrote test data into afterwards), read
769 * it back (looping if necessary, to get past newly
770 * discovered unreadable blocks, of which there should
771 * be none, but with a hard drive which is unreliable,
772 * it has happened), and compare with the test data
773 * that was written; output to the bad block list if
774 * it doesn't match.
775 */
776 used2 = 0;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400777 save_ptr = save_base;
778 test_ptr = test_base;
779 read_ptr = read_base;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400780 try = 0;
781
782 while (1) {
783 if (try == 0) {
784 if (used2 >= num_saved)
785 break;
786 currently_testing = test_record[used2].block;
787 try = test_record[used2].num;
788 used2++;
789 }
790
791 got = do_read (dev, read_ptr, try,
792 block_size, currently_testing);
793
794 /* test the comparison between all the
795 blocks successfully read */
796 for (i = 0; i < got; ++i)
797 if (memcmp (test_ptr+i*block_size,
798 read_ptr+i*block_size, block_size))
799 bb_count += bb_output(currently_testing + i);
800 if (got < try) {
801 bb_count += bb_output(currently_testing + got);
802 got++;
803 }
804
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400805 /* write back original data */
806 do_write (dev, save_ptr, got,
807 block_size, currently_testing);
808 save_ptr += got * block_size;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400809
810 currently_testing += got;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400811 test_ptr += got * block_size;
812 read_ptr += got * block_size;
813 try -= got;
814 }
815
816 /* empty the buffer so it can be reused */
817 num_saved = 0;
818 buf_used = 0;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400819 save_ptr = save_base;
820 test_ptr = test_base;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400821 currently_testing = save_currently_testing;
Theodore Ts'o4d003982000-04-03 16:01:11 +0000822 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400823 num_blocks = 0;
824 alarm(0);
825 if (s_flag || v_flag > 1)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400826 fputs(_(done_string), stderr);
Theodore Ts'o4d003982000-04-03 16:01:11 +0000827
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400828 flush_bufs();
Theodore Ts'o4d003982000-04-03 16:01:11 +0000829 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000830 uncapture_terminate();
Theodore Ts'odd018f52000-02-06 23:57:07 +0000831 fflush(stderr);
832 free(blkbuf);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000833 free(test_record);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000834
835 ext2fs_badblocks_list_iterate_end(bb_iter);
836
837 return bb_count;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000838}
839
Theodore Ts'o981dc562000-07-06 14:13:29 +0000840static void check_mount(char *device_name)
841{
842 errcode_t retval;
843 int mount_flags;
844
845 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
846 if (retval) {
847 com_err("ext2fs_check_if_mount", retval,
848 _("while determining whether %s is mounted."),
849 device_name);
850 return;
851 }
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400852 if (mount_flags & EXT2_MF_MOUNTED) {
853 fprintf(stderr, _("%s is mounted; "), device_name);
854 if (force) {
855 fputs(_("badblocks forced anyway. "
856 "Hope /etc/mtab is incorrect.\n"), stderr);
857 return;
858 }
859 abort_badblocks:
860 fputs(_("it's not safe to run badblocks!\n"), stderr);
861 exit(1);
Theodore Ts'o981dc562000-07-06 14:13:29 +0000862 }
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400863
Theodore Ts'of63978a2006-05-13 09:25:47 -0400864 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400865 fprintf(stderr, _("%s is apparently in use by the system; "),
866 device_name);
867 if (force)
868 fputs(_("badblocks forced anyway.\n"), stderr);
869 else
870 goto abort_badblocks;
871 }
872
Theodore Ts'o981dc562000-07-06 14:13:29 +0000873}
874
Theodore Ts'od4be9fa2007-10-22 10:19:20 -0400875/*
876 * This function will convert a string to an unsigned long, printing
877 * an error message if it fails, and returning success or failure in err.
878 */
879static unsigned int parse_uint(const char *str, const char *descr)
880{
881 char *tmp;
882 unsigned long ret;
883
Iustin Popeb594252008-06-11 17:55:18 +0200884 errno = 0;
Theodore Ts'od4be9fa2007-10-22 10:19:20 -0400885 ret = strtoul(str, &tmp, 0);
886 if (*tmp || errno || (ret > UINT_MAX) ||
887 (ret == ULONG_MAX && errno == ERANGE)) {
888 com_err (program_name, 0, _("invalid %s - %s"), descr, str);
889 exit (1);
890 }
891 return ret;
892}
Theodore Ts'o981dc562000-07-06 14:13:29 +0000893
Theodore Ts'o00e54331997-09-16 02:13:52 +0000894int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000895{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000896 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000897 char * device_name;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000898 char * host_device_name = NULL;
899 char * input_file = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000900 char * output_file = NULL;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000901 FILE * in = NULL;
Theodore Ts'odd018f52000-02-06 23:57:07 +0000902 int block_size = 1024;
Theodore Ts'oacd77412007-10-22 10:09:05 -0400903 unsigned int blocks_at_once = 64;
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000904 blk_t last_block, from_count;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000905 int num_passes = 0;
906 int passes_clean = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000907 int dev;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000908 errcode_t errcode;
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400909 unsigned int pattern;
Theodore Ts'oacd77412007-10-22 10:09:05 -0400910 unsigned int (*test_func)(int, blk_t,
911 int, blk_t,
912 unsigned int);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400913 int open_flag = 0;
914 long sysval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000915
916 setbuf(stdout, NULL);
917 setbuf(stderr, NULL);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000918#ifdef ENABLE_NLS
919 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500920 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000921 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
922 textdomain(NLS_CAT_NAME);
923#endif
Theodore Ts'o6d40f562003-05-07 08:35:38 -0400924 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
Theodore Ts'o4d003982000-04-03 16:01:11 +0000925 test_func = test_ro;
Theodore Ts'o4d404542001-01-11 16:04:59 +0000926
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400927 /* Determine the system page size if possible */
928#ifdef HAVE_SYSCONF
929#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
930#define _SC_PAGESIZE _SC_PAGE_SIZE
931#endif
932#ifdef _SC_PAGESIZE
933 sysval = sysconf(_SC_PAGESIZE);
934 if (sysval > 0)
935 sys_page_size = sysval;
936#endif /* _SC_PAGESIZE */
937#endif /* HAVE_SYSCONF */
938
Theodore Ts'o3839e651997-04-26 13:21:57 +0000939 if (argc && *argv)
940 program_name = *argv;
Iustin Pop264f64a2008-06-12 09:30:04 +0200941 while ((c = getopt (argc, argv, "b:d:e:fi:o:svwnc:p:h:t:X")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000942 switch (c) {
943 case 'b':
Theodore Ts'od4be9fa2007-10-22 10:19:20 -0400944 block_size = parse_uint(optarg, "block size");
945 if (block_size > 4096) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000946 com_err (program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000947 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000948 exit (1);
949 }
950 break;
Theodore Ts'o981dc562000-07-06 14:13:29 +0000951 case 'f':
952 force++;
953 break;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000954 case 'i':
955 input_file = optarg;
956 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000957 case 'o':
958 output_file = optarg;
959 break;
960 case 's':
961 s_flag = 1;
962 break;
963 case 'v':
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000964 v_flag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000965 break;
966 case 'w':
Theodore Ts'o4d003982000-04-03 16:01:11 +0000967 if (w_flag)
Theodore Ts'od8b5f772006-11-12 23:09:03 -0500968 exclusive_usage();
Theodore Ts'o4d003982000-04-03 16:01:11 +0000969 test_func = test_rw;
970 w_flag = 1;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000971 break;
972 case 'n':
Theodore Ts'o4d003982000-04-03 16:01:11 +0000973 if (w_flag)
Theodore Ts'od8b5f772006-11-12 23:09:03 -0500974 exclusive_usage();
Theodore Ts'o4d003982000-04-03 16:01:11 +0000975 test_func = test_nd;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000976 w_flag = 2;
977 break;
978 case 'c':
Theodore Ts'od4be9fa2007-10-22 10:19:20 -0400979 blocks_at_once = parse_uint(optarg, "blocks at once");
Theodore Ts'o879ac922000-01-18 20:59:11 +0000980 break;
Iustin Pop931b0282008-06-11 13:12:17 +0200981 case 'e':
982 max_bb = parse_uint(optarg, "max bad block count");
983 break;
Iustin Pop264f64a2008-06-12 09:30:04 +0200984 case 'd':
985 d_flag = parse_uint(optarg, "read delay factor");
986 break;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000987 case 'p':
Theodore Ts'od4be9fa2007-10-22 10:19:20 -0400988 num_passes = parse_uint(optarg,
989 "number of clean passes");
Theodore Ts'o879ac922000-01-18 20:59:11 +0000990 break;
991 case 'h':
992 host_device_name = optarg;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000993 break;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400994 case 't':
995 if (t_flag + 1 > t_max) {
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400996 unsigned int *t_patts_new;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400997
998 t_patts_new = realloc(t_patts, t_max + T_INC);
999 if (!t_patts_new) {
1000 com_err(program_name, ENOMEM,
1001 _("can't allocate memory for "
1002 "test_pattern - %s"),
1003 optarg);
1004 exit(1);
1005 }
1006 t_patts = t_patts_new;
1007 t_max += T_INC;
1008 }
Theodore Ts'o84c05452003-05-18 01:11:52 -04001009 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
1010 t_patts[t_flag++] = ~0;
1011 } else {
Theodore Ts'od4be9fa2007-10-22 10:19:20 -04001012 pattern = parse_uint(optarg, "test pattern");
Theodore Ts'oe9860ae2007-10-22 09:51:50 -04001013 if (pattern == (unsigned int) ~0)
Theodore Ts'o84c05452003-05-18 01:11:52 -04001014 pattern = 0xffff;
1015 t_patts[t_flag++] = pattern;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001016 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001017 break;
Theodore Ts'of63978a2006-05-13 09:25:47 -04001018 case 'X':
1019 exclusive_ok++;
1020 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001021 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +00001022 usage();
Theodore Ts'o3839e651997-04-26 13:21:57 +00001023 }
1024 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001025 if (!w_flag) {
1026 if (t_flag > 1) {
1027 com_err(program_name, 0,
1028 _("Maximum of one test_pattern may be specified "
1029 "in read-only mode"));
1030 exit(1);
1031 }
Theodore Ts'oe9860ae2007-10-22 09:51:50 -04001032 if (t_patts && (t_patts[0] == (unsigned int) ~0)) {
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001033 com_err(program_name, 0,
1034 _("Random test_pattern is not allowed "
1035 "in read-only mode"));
1036 exit(1);
1037 }
1038 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001039 if (optind > argc - 1)
Theodore Ts'o818180c1998-06-27 05:11:14 +00001040 usage();
Theodore Ts'o3839e651997-04-26 13:21:57 +00001041 device_name = argv[optind++];
Theodore Ts'o35964b52000-07-06 13:19:43 +00001042 if (optind > argc - 1) {
1043 errcode = ext2fs_get_device_size(device_name,
1044 block_size,
Theodore Ts'ocd130a02001-05-05 05:43:23 +00001045 &last_block);
Theodore Ts'o35964b52000-07-06 13:19:43 +00001046 if (errcode == EXT2_ET_UNIMPLEMENTED) {
1047 com_err(program_name, 0,
1048 _("Couldn't determine device size; you "
1049 "must specify\nthe size manually\n"));
1050 exit(1);
1051 }
1052 if (errcode) {
1053 com_err(program_name, errcode,
1054 _("while trying to determine device size"));
1055 exit(1);
1056 }
1057 } else {
Theodore Ts'o5267a522007-06-04 01:49:51 -04001058 errno = 0;
Theodore Ts'od4be9fa2007-10-22 10:19:20 -04001059 last_block = parse_uint(argv[optind], "last block");
Theodore Ts'o5267a522007-06-04 01:49:51 -04001060 printf("last_block = %d (%s)\n", last_block, argv[optind]);
Theodore Ts'o5267a522007-06-04 01:49:51 -04001061 last_block++;
Theodore Ts'o35964b52000-07-06 13:19:43 +00001062 optind++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001063 }
Theodore Ts'o35964b52000-07-06 13:19:43 +00001064 if (optind <= argc-1) {
Theodore Ts'o5267a522007-06-04 01:49:51 -04001065 errno = 0;
Theodore Ts'od4be9fa2007-10-22 10:19:20 -04001066 from_count = parse_uint(argv[optind], "start block");
Theodore Ts'o5267a522007-06-04 01:49:51 -04001067 printf("from_count = %d\n", from_count);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001068 } else from_count = 0;
Theodore Ts'ocd130a02001-05-05 05:43:23 +00001069 if (from_count >= last_block) {
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001070 com_err (program_name, 0, _("invalid starting block (%lu): must be less than %lu"),
Theodore Ts'o54434922003-12-07 01:28:50 -05001071 (unsigned long) from_count, (unsigned long) last_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001072 exit (1);
1073 }
Theodore Ts'o981dc562000-07-06 14:13:29 +00001074 if (w_flag)
1075 check_mount(device_name);
1076
Theodore Ts'o1c29b092003-07-12 16:01:45 -04001077 open_flag = w_flag ? O_RDWR : O_RDONLY;
1078 dev = open (device_name, open_flag);
Theodore Ts'o5493a272002-01-02 14:15:35 -05001079 if (dev == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001080 com_err (program_name, errno, _("while trying to open %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001081 device_name);
1082 exit (1);
1083 }
Theodore Ts'o879ac922000-01-18 20:59:11 +00001084 if (host_device_name) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -04001085 host_dev = open (host_device_name, open_flag);
Theodore Ts'o5493a272002-01-02 14:15:35 -05001086 if (host_dev == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001087 com_err (program_name, errno,
1088 _("while trying to open %s"),
1089 host_device_name);
Theodore Ts'o879ac922000-01-18 20:59:11 +00001090 exit (1);
1091 }
1092 } else
1093 host_dev = dev;
Theodore Ts'o3e699062002-10-13 23:56:28 -04001094 if (input_file) {
Theodore Ts'o879ac922000-01-18 20:59:11 +00001095 if (strcmp (input_file, "-") == 0)
1096 in = stdin;
1097 else {
1098 in = fopen (input_file, "r");
1099 if (in == NULL)
1100 {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001101 com_err (program_name, errno,
1102 _("while trying to open %s"),
Theodore Ts'o879ac922000-01-18 20:59:11 +00001103 input_file);
1104 exit (1);
1105 }
1106 }
Theodore Ts'o3e699062002-10-13 23:56:28 -04001107 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001108 if (output_file && strcmp (output_file, "-") != 0)
1109 {
1110 out = fopen (output_file, "w");
1111 if (out == NULL)
1112 {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001113 com_err (program_name, errno,
1114 _("while trying to open %s"),
Theodore Ts'o879ac922000-01-18 20:59:11 +00001115 output_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001116 exit (1);
1117 }
1118 }
1119 else
1120 out = stdout;
Theodore Ts'o879ac922000-01-18 20:59:11 +00001121
1122 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1123 if (errcode) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001124 com_err (program_name, errcode,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001125 _("while creating in-memory bad blocks list"));
Theodore Ts'o879ac922000-01-18 20:59:11 +00001126 exit (1);
1127 }
1128
1129 if (in) {
1130 for(;;) {
Theodore Ts'oa551b782000-07-13 22:05:31 +00001131 switch(fscanf (in, "%u\n", &next_bad)) {
Theodore Ts'o879ac922000-01-18 20:59:11 +00001132 case 0:
1133 com_err (program_name, 0, "input file - bad format");
1134 exit (1);
1135 case EOF:
1136 break;
1137 default:
1138 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1139 if (errcode) {
Theodore Ts'obb145b02005-06-20 08:35:27 -04001140 com_err (program_name, errcode, _("while adding to in-memory bad block list"));
Theodore Ts'o879ac922000-01-18 20:59:11 +00001141 exit (1);
1142 }
1143 continue;
1144 }
1145 break;
1146 }
1147
1148 if (in != stdin)
1149 fclose (in);
1150 }
1151
1152 do {
1153 unsigned int bb_count;
1154
Theodore Ts'ocd130a02001-05-05 05:43:23 +00001155 bb_count = test_func(dev, last_block, block_size,
Theodore Ts'o4d003982000-04-03 16:01:11 +00001156 from_count, blocks_at_once);
1157 if (bb_count)
1158 passes_clean = 0;
1159 else
1160 ++passes_clean;
1161
Theodore Ts'o879ac922000-01-18 20:59:11 +00001162 if (v_flag)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001163 fprintf(stderr,
1164 _("Pass completed, %u bad blocks found.\n"),
1165 bb_count);
Theodore Ts'o879ac922000-01-18 20:59:11 +00001166
1167 } while (passes_clean < num_passes);
1168
Theodore Ts'o3839e651997-04-26 13:21:57 +00001169 close (dev);
1170 if (out != stdout)
1171 fclose (out);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001172 if (t_patts)
1173 free(t_patts);
Theodore Ts'o879ac922000-01-18 20:59:11 +00001174 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001175}
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001176