blob: 5676a8486861dc081734cec5fabec4de0c89e910 [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
51#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000052#include <sys/types.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000053
Theodore Ts'o3839e651997-04-26 13:21:57 +000054#include "et/com_err.h"
Theodore Ts'od40259f1997-10-20 00:44:26 +000055#include "ext2fs/ext2_io.h"
Theodore Ts'o54c637d2001-05-14 11:45:38 +000056#include "ext2fs/ext2_fs.h"
Theodore Ts'o879ac922000-01-18 20:59:11 +000057#include "ext2fs/ext2fs.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000058#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000059
60const char * program_name = "badblocks";
Theodore Ts'of63978a2006-05-13 09:25:47 -040061const char * done_string = N_("done \n");
Theodore Ts'o3839e651997-04-26 13:21:57 +000062
Theodore Ts'o4d003982000-04-03 16:01:11 +000063static int v_flag = 0; /* verbose */
64static int w_flag = 0; /* do r/w test: 0=no, 1=yes,
65 * 2=non-destructive */
66static int s_flag = 0; /* show progress of test */
Theodore Ts'o981dc562000-07-06 14:13:29 +000067static int force = 0; /* force check of mounted device */
Theodore Ts'o849b6bc2003-05-07 09:52:14 -040068static int t_flag = 0; /* number of test patterns */
69static int t_max = 0; /* allocated test patterns */
Theodore Ts'oe9860ae2007-10-22 09:51:50 -040070static unsigned int *t_patts = NULL; /* test patterns */
Theodore Ts'o1c29b092003-07-12 16:01:45 -040071static int current_O_DIRECT = 0; /* Current status of O_DIRECT flag */
Theodore Ts'of63978a2006-05-13 09:25:47 -040072static int exclusive_ok = 0;
Iustin Pop931b0282008-06-11 13:12:17 +020073static unsigned int max_bb = 0; /* Abort test if more than this number of bad blocks has been encountered */
Theodore Ts'o1c29b092003-07-12 16:01:45 -040074
Theodore Ts'o849b6bc2003-05-07 09:52:14 -040075#define T_INC 32
Theodore Ts'o4d003982000-04-03 16:01:11 +000076
Theodore Ts'oacd77412007-10-22 10:09:05 -040077unsigned int sys_page_size = 4096;
Theodore Ts'o1c29b092003-07-12 16:01:45 -040078
Theodore Ts'o8820c792001-01-06 04:20:03 +000079static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000080{
Iustin Pop931b0282008-06-11 13:12:17 +020081 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] [-t test_pattern [-t test_pattern [...]]]\n device [last_block [start_block]]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000082 program_name);
83 exit (1);
84}
85
Theodore Ts'od8b5f772006-11-12 23:09:03 -050086static void exclusive_usage(void)
87{
88 fprintf(stderr,
Theodore Ts'o017a76e2006-11-17 23:00:19 -050089 _("%s: The -n and -w options are mutually exclusive.\n\n"),
90 program_name);
91 exit(1);
Theodore Ts'od8b5f772006-11-12 23:09:03 -050092}
93
Theodore Ts'oacd77412007-10-22 10:09:05 -040094static blk_t currently_testing = 0;
95static blk_t num_blocks = 0;
Theodore Ts'o879ac922000-01-18 20:59:11 +000096static ext2_badblocks_list bb_list = NULL;
97static FILE *out;
98static blk_t next_bad = 0;
99static ext2_badblocks_iterate bb_iter = NULL;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000100
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400101static void *allocate_buffer(size_t size)
102{
103 void *ret = 0;
104
105#ifdef HAVE_POSIX_MEMALIGN
106 if (posix_memalign(&ret, sys_page_size, size) < 0)
107 ret = 0;
108#else
109#ifdef HAVE_MEMALIGN
110 ret = memalign(sys_page_size, size);
111#else
112#ifdef HAVE_VALLOC
113 ret = valloc(size);
114#endif /* HAVE_VALLOC */
115#endif /* HAVE_MEMALIGN */
116#endif /* HAVE_POSIX_MEMALIGN */
117
118 if (!ret)
119 ret = malloc(size);
120
121 return ret;
122}
123
Theodore Ts'odd018f52000-02-06 23:57:07 +0000124/*
125 * This routine reports a new bad block. If the bad block has already
126 * been seen before, then it returns 0; otherwise it returns 1.
127 */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400128static int bb_output (blk_t bad)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000129{
130 errcode_t errcode;
131
Theodore Ts'odd018f52000-02-06 23:57:07 +0000132 if (ext2fs_badblocks_list_test(bb_list, bad))
133 return 0;
134
Theodore Ts'oacd77412007-10-22 10:09:05 -0400135 fprintf(out, "%lu\n", (unsigned long) bad);
Theodore Ts'occ4f98e2003-04-03 11:37:46 -0500136 fflush(out);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000137
138 errcode = ext2fs_badblocks_list_add (bb_list, bad);
139 if (errcode) {
140 com_err (program_name, errcode, "adding to in-memory bad block list");
141 exit (1);
142 }
143
144 /* kludge:
145 increment the iteration through the bb_list if
146 an element was just added before the current iteration
147 position. This should not cause next_bad to change. */
148 if (bb_iter && bad < next_bad)
149 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
Theodore Ts'odd018f52000-02-06 23:57:07 +0000150 return 1;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000151}
152
Theodore Ts'o8820c792001-01-06 04:20:03 +0000153static void print_status(void)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000154{
Theodore Ts'oacd77412007-10-22 10:09:05 -0400155 fprintf(stderr, "%15lu/%15lu", (unsigned long) currently_testing,
156 (unsigned long) num_blocks);
Theodore Ts'oc76564a2005-01-06 14:48:59 -0500157 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 +0000158 fflush (stderr);
159}
160
Theodore Ts'o54434922003-12-07 01:28:50 -0500161static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000162{
163 signal (SIGALRM, alarm_intr);
164 alarm(1);
165 if (!num_blocks)
166 return;
Theodore Ts'oc76564a2005-01-06 14:48:59 -0500167 print_status();
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000168}
169
Theodore Ts'o879ac922000-01-18 20:59:11 +0000170static void *terminate_addr = NULL;
171
Theodore Ts'o54434922003-12-07 01:28:50 -0500172static void terminate_intr(int signo EXT2FS_ATTR((unused)))
Theodore Ts'o879ac922000-01-18 20:59:11 +0000173{
174 if (terminate_addr)
175 longjmp(terminate_addr,1);
176 exit(1);
177}
178
Theodore Ts'o981dc562000-07-06 14:13:29 +0000179static void capture_terminate(jmp_buf term_addr)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000180{
181 terminate_addr = term_addr;
182 signal (SIGHUP, terminate_intr);
183 signal (SIGINT, terminate_intr);
184 signal (SIGPIPE, terminate_intr);
185 signal (SIGTERM, terminate_intr);
186 signal (SIGUSR1, terminate_intr);
187 signal (SIGUSR2, terminate_intr);
188}
189
Theodore Ts'o8820c792001-01-06 04:20:03 +0000190static void uncapture_terminate(void)
Theodore Ts'o4d003982000-04-03 16:01:11 +0000191{
192 terminate_addr = NULL;
193 signal (SIGHUP, SIG_DFL);
194 signal (SIGINT, SIG_DFL);
195 signal (SIGPIPE, SIG_DFL);
196 signal (SIGTERM, SIG_DFL);
197 signal (SIGUSR1, SIG_DFL);
198 signal (SIGUSR2, SIG_DFL);
199}
200
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400201static void set_o_direct(int dev, unsigned char *buffer, size_t size,
Theodore Ts'oacd77412007-10-22 10:09:05 -0400202 blk_t current_block)
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400203{
204#ifdef O_DIRECT
205 int new_flag = O_DIRECT;
206 int flag;
207
208 if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400209 ((size & (sys_page_size - 1)) != 0) ||
210 ((current_block & ((sys_page_size >> 9)-1)) != 0))
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400211 new_flag = 0;
212
213 if (new_flag != current_O_DIRECT) {
Theodore Ts'odc058712003-07-25 07:39:33 -0400214 /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400215 flag = fcntl(dev, F_GETFL);
216 if (flag > 0) {
217 flag = (flag & ~O_DIRECT) | new_flag;
218 fcntl(dev, F_SETFL, flag);
219 }
220 current_O_DIRECT = new_flag;
221 }
222#endif
223}
224
225
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400226static void pattern_fill(unsigned char *buffer, unsigned int pattern,
Theodore Ts'o84c05452003-05-18 01:11:52 -0400227 size_t n)
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400228{
Theodore Ts'o54434922003-12-07 01:28:50 -0500229 unsigned int i, nb;
Theodore Ts'o84c05452003-05-18 01:11:52 -0400230 unsigned char bpattern[sizeof(pattern)], *ptr;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400231
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400232 if (pattern == (unsigned int) ~0) {
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400233 for (ptr = buffer; ptr < buffer + n; ptr++) {
234 (*ptr) = random() % (1 << (8 * sizeof(char)));
235 }
236 if (s_flag | v_flag)
Theodore Ts'o54434922003-12-07 01:28:50 -0500237 fputs(_("Testing with random pattern: "), stderr);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400238 } else {
239 bpattern[0] = 0;
240 for (i = 0; i < sizeof(bpattern); i++) {
241 if (pattern == 0)
242 break;
243 bpattern[i] = pattern & 0xFF;
244 pattern = pattern >> 8;
245 }
246 nb = i ? (i-1) : 0;
247 for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500248 *ptr = bpattern[i];
249 if (i == 0)
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400250 i = nb;
Theodore Ts'o54434922003-12-07 01:28:50 -0500251 else
252 i--;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400253 }
Theodore Ts'o84c05452003-05-18 01:11:52 -0400254 if (s_flag | v_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500255 fputs(_("Testing with pattern 0x"), stderr);
Theodore Ts'o84c05452003-05-18 01:11:52 -0400256 for (i = 0; i <= nb; i++)
257 fprintf(stderr, "%02x", buffer[i]);
Theodore Ts'o54434922003-12-07 01:28:50 -0500258 fputs(": ", stderr);
Theodore Ts'o84c05452003-05-18 01:11:52 -0400259 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400260 }
261}
262
Theodore Ts'o3839e651997-04-26 13:21:57 +0000263/*
Theodore Ts'o879ac922000-01-18 20:59:11 +0000264 * Perform a read of a sequence of blocks; return the number of blocks
265 * successfully sequentially read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266 */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400267static int do_read (int dev, unsigned char * buffer, int try, int block_size,
268 blk_t current_block)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269{
270 long got;
271
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400272 set_o_direct(dev, buffer, try * block_size, current_block);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400273
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000274 if (v_flag > 1)
275 print_status();
276
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277 /* Seek to the correct loc. */
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000278 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000279 SEEK_SET) != (ext2_loff_t) current_block * block_size)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000280 com_err (program_name, errno, _("during seek"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000281
282 /* Try the read */
283 got = read (dev, buffer, try * block_size);
284 if (got < 0)
285 got = 0;
Theodore Ts'o9f10a7b1999-07-16 10:41:36 +0000286 if (got & 511)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000287 fprintf(stderr, _("Weird value (%ld) in do_read\n"), got);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000288 got /= block_size;
289 return got;
290}
291
Theodore Ts'o879ac922000-01-18 20:59:11 +0000292/*
293 * Perform a write of a sequence of blocks; return the number of blocks
294 * successfully sequentially written.
295 */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400296static int do_write(int dev, unsigned char * buffer, int try, int block_size,
297 unsigned long current_block)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000298{
299 long got;
300
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400301 set_o_direct(dev, buffer, try * block_size, current_block);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400302
Theodore Ts'o879ac922000-01-18 20:59:11 +0000303 if (v_flag > 1)
304 print_status();
305
306 /* Seek to the correct loc. */
307 if (ext2fs_llseek (dev, (ext2_loff_t) current_block * block_size,
308 SEEK_SET) != (ext2_loff_t) current_block * block_size)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000309 com_err (program_name, errno, _("during seek"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000310
311 /* Try the write */
312 got = write (dev, buffer, try * block_size);
313 if (got < 0)
314 got = 0;
315 if (got & 511)
Theodore Ts'o54434922003-12-07 01:28:50 -0500316 fprintf(stderr, "Weird value (%ld) in do_write\n", got);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000317 got /= block_size;
318 return got;
319}
320
321static int host_dev;
322
Theodore Ts'o4d404542001-01-11 16:04:59 +0000323static void flush_bufs(void)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000324{
Theodore Ts'o4d404542001-01-11 16:04:59 +0000325 errcode_t retval;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000326
Theodore Ts'o4d404542001-01-11 16:04:59 +0000327 retval = ext2fs_sync_device(host_dev, 1);
328 if (retval)
329 com_err(program_name, retval, _("during ext2fs_sync_device"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000330}
331
Theodore Ts'oacd77412007-10-22 10:09:05 -0400332static unsigned int test_ro (int dev, blk_t last_block,
333 int block_size, blk_t from_count,
334 unsigned int blocks_at_once)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000335{
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400336 unsigned char * blkbuf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000337 int try;
Theodore Ts'oacd77412007-10-22 10:09:05 -0400338 int got;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000339 unsigned int bb_count = 0;
340 errcode_t errcode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000341
Theodore Ts'o879ac922000-01-18 20:59:11 +0000342 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
343 if (errcode) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000344 com_err (program_name, errcode,
345 _("while beginning bad block list iteration"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000346 exit (1);
347 }
348 do {
349 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
350 } while (next_bad && next_bad < from_count);
351
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400352 if (t_flag) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400353 blkbuf = allocate_buffer((blocks_at_once + 1) * block_size);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400354 } else {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400355 blkbuf = allocate_buffer(blocks_at_once * block_size);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400356 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000357 if (!blkbuf)
358 {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000359 com_err (program_name, ENOMEM, _("while allocating buffers"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000360 exit (1);
361 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000362 if (v_flag) {
Theodore Ts'oacd77412007-10-22 10:09:05 -0400363 fprintf (stderr, _("Checking blocks %lu to %lu\n"),
364 (unsigned long) from_count,
365 (unsigned long) last_block - 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000366 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400367 if (t_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500368 fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400369 pattern_fill(blkbuf + blocks_at_once * block_size,
370 t_patts[0], block_size);
371 }
372 flush_bufs();
Theodore Ts'o879ac922000-01-18 20:59:11 +0000373 try = blocks_at_once;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000374 currently_testing = from_count;
Theodore Ts'o8938ce62006-10-03 23:35:57 -0400375 num_blocks = last_block - 1;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400376 if (!t_flag && (s_flag || v_flag)) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500377 fputs(_("Checking for bad blocks (read-only test): "), stderr);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000378 if (v_flag <= 1)
379 alarm_intr(SIGALRM);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000380 }
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000381 while (currently_testing < last_block)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000382 {
Iustin Pop931b0282008-06-11 13:12:17 +0200383 if (max_bb && bb_count >= max_bb) {
384 if (s_flag || v_flag) {
385 fputs(_("Too many bad blocks, aborting test\n"), stderr);
386 }
387 break;
388 }
Theodore Ts'o879ac922000-01-18 20:59:11 +0000389 if (next_bad) {
390 if (currently_testing == next_bad) {
391 /* fprintf (out, "%lu\n", nextbad); */
392 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
393 currently_testing++;
394 continue;
395 }
396 else if (currently_testing + try > next_bad)
397 try = next_bad - currently_testing;
398 }
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000399 if (currently_testing + try > last_block)
400 try = last_block - currently_testing;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000401 got = do_read (dev, blkbuf, try, block_size, currently_testing);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400402 if (t_flag) {
403 /* test the comparison between all the
404 blocks successfully read */
405 int i;
406 for (i = 0; i < got; ++i)
407 if (memcmp (blkbuf+i*block_size,
408 blkbuf+blocks_at_once*block_size,
409 block_size))
410 bb_count += bb_output(currently_testing + i);
411 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000412 currently_testing += got;
413 if (got == try) {
Theodore Ts'o879ac922000-01-18 20:59:11 +0000414 try = blocks_at_once;
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400415 /* recover page-aligned offset for O_DIRECT */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400416 if ( (blocks_at_once >= sys_page_size >> 9)
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400417 && (currently_testing % (sys_page_size >> 9)!= 0))
418 try -= (sys_page_size >> 9)
419 - (currently_testing
420 % (sys_page_size >> 9));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000421 continue;
422 }
423 else
424 try = 1;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000425 if (got == 0) {
Theodore Ts'odd018f52000-02-06 23:57:07 +0000426 bb_count += bb_output(currently_testing++);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000427 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000428 }
429 num_blocks = 0;
430 alarm(0);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400431 if (s_flag || v_flag)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400432 fputs(_(done_string), stderr);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000433
Theodore Ts'of3db3561997-04-26 13:34:30 +0000434 fflush (stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000435 free (blkbuf);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000436
437 ext2fs_badblocks_list_iterate_end(bb_iter);
438
439 return bb_count;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440}
441
Theodore Ts'oacd77412007-10-22 10:09:05 -0400442static unsigned int test_rw (int dev, blk_t last_block,
443 int block_size, blk_t from_count,
444 unsigned int blocks_at_once)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445{
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400446 unsigned char *buffer, *read_buffer;
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400447 const unsigned int patterns[] = {0xaa, 0x55, 0xff, 0x00};
448 const unsigned int *pattern;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400449 int i, try, got, nr_pattern, pat_idx;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000450 unsigned int bb_count = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000451
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400452 buffer = allocate_buffer(2 * blocks_at_once * block_size);
453 read_buffer = buffer + blocks_at_once * block_size;
454
455 if (!buffer) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000456 com_err (program_name, ENOMEM, _("while allocating buffers"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000457 exit (1);
458 }
459
Theodore Ts'o4d404542001-01-11 16:04:59 +0000460 flush_bufs();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000461
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000462 if (v_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500463 fputs(_("Checking for bad blocks in read-write mode\n"),
464 stderr);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000465 fprintf(stderr, _("From block %lu to %lu\n"),
Theodore Ts'oacd77412007-10-22 10:09:05 -0400466 (unsigned long) from_count,
467 (unsigned long) last_block);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000468 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400469 if (t_flag) {
470 pattern = t_patts;
471 nr_pattern = t_flag;
472 } else {
473 pattern = patterns;
474 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
475 }
476 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400477 pattern_fill(buffer, pattern[pat_idx],
478 blocks_at_once * block_size);
Theodore Ts'o8938ce62006-10-03 23:35:57 -0400479 num_blocks = last_block - 1;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000480 currently_testing = from_count;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000481 if (s_flag && v_flag <= 1)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000482 alarm_intr(SIGALRM);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400483
484 try = blocks_at_once;
485 while (currently_testing < last_block) {
Iustin Pop931b0282008-06-11 13:12:17 +0200486 if (max_bb && bb_count >= max_bb) {
487 if (s_flag || v_flag) {
488 fputs(_("Too many bad blocks, aborting test\n"), stderr);
489 }
490 break;
491 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400492 if (currently_testing + try > last_block)
493 try = last_block - currently_testing;
494 got = do_write(dev, buffer, try, block_size,
495 currently_testing);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000496 if (v_flag > 1)
497 print_status();
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400498
499 currently_testing += got;
500 if (got == try) {
501 try = blocks_at_once;
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400502 /* recover page-aligned offset for O_DIRECT */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400503 if ( (blocks_at_once >= sys_page_size >> 9)
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400504 && (currently_testing %
505 (sys_page_size >> 9)!= 0))
506 try -= (sys_page_size >> 9)
507 - (currently_testing
508 % (sys_page_size >> 9));
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400509 continue;
510 } else
511 try = 1;
512 if (got == 0) {
513 bb_count += bb_output(currently_testing++);
514 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400516
Theodore Ts'of3db3561997-04-26 13:34:30 +0000517 num_blocks = 0;
518 alarm (0);
519 if (s_flag | v_flag)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400520 fputs(_(done_string), stderr);
Theodore Ts'o4d404542001-01-11 16:04:59 +0000521 flush_bufs();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000522 if (s_flag | v_flag)
Theodore Ts'o54434922003-12-07 01:28:50 -0500523 fputs(_("Reading and comparing: "), stderr);
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000524 num_blocks = last_block;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000525 currently_testing = from_count;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000526 if (s_flag && v_flag <= 1)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000527 alarm_intr(SIGALRM);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400528
529 try = blocks_at_once;
530 while (currently_testing < last_block) {
Iustin Pop931b0282008-06-11 13:12:17 +0200531 if (max_bb && bb_count >= max_bb) {
532 if (s_flag || v_flag) {
533 fputs(_("Too many bad blocks, aborting test\n"), stderr);
534 }
535 break;
536 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400537 if (currently_testing + try > last_block)
538 try = last_block - currently_testing;
539 got = do_read (dev, read_buffer, try, block_size,
540 currently_testing);
541 if (got == 0) {
542 bb_count += bb_output(currently_testing++);
543 continue;
544 }
545 for (i=0; i < got; i++) {
546 if (memcmp(read_buffer + i * block_size,
547 buffer + i * block_size,
548 block_size))
549 bb_count += bb_output(currently_testing+i);
550 }
551 currently_testing += got;
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400552 /* recover page-aligned offset for O_DIRECT */
Theodore Ts'oacd77412007-10-22 10:09:05 -0400553 if ( (blocks_at_once >= sys_page_size >> 9)
Theodore Ts'o1f9a60c2003-08-01 00:58:00 -0400554 && (currently_testing % (sys_page_size >> 9)!= 0))
555 try = blocks_at_once - (sys_page_size >> 9)
556 - (currently_testing
557 % (sys_page_size >> 9));
558 else
559 try = blocks_at_once;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000560 if (v_flag > 1)
561 print_status();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000562 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400563
Theodore Ts'of3db3561997-04-26 13:34:30 +0000564 num_blocks = 0;
565 alarm (0);
566 if (s_flag | v_flag)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400567 fputs(_(done_string), stderr);
Theodore Ts'o4d404542001-01-11 16:04:59 +0000568 flush_bufs();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000569 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400570 uncapture_terminate();
Theodore Ts'o6d40f562003-05-07 08:35:38 -0400571 free(buffer);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000572 return bb_count;
573}
574
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000575struct saved_blk_record {
576 blk_t block;
577 int num;
578};
579
Theodore Ts'oacd77412007-10-22 10:09:05 -0400580static unsigned int test_nd (int dev, blk_t last_block,
581 int block_size, blk_t from_count,
582 unsigned int blocks_at_once)
Theodore Ts'o879ac922000-01-18 20:59:11 +0000583{
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400584 unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400585 unsigned char *test_base, *save_base, *read_base;
Theodore Ts'odd018f52000-02-06 23:57:07 +0000586 int try, i;
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400587 const unsigned int patterns[] = { ~0 };
588 const unsigned int *pattern;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400589 int nr_pattern, pat_idx;
Theodore Ts'oacd77412007-10-22 10:09:05 -0400590 int got, used2, written;
591 blk_t save_currently_testing;
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000592 struct saved_blk_record *test_record;
Theodore Ts'oa551b782000-07-13 22:05:31 +0000593 /* This is static to prevent being clobbered by the longjmp */
594 static int num_saved;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000595 jmp_buf terminate_env;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000596 errcode_t errcode;
Theodore Ts'o54434922003-12-07 01:28:50 -0500597 unsigned long buf_used;
598 static unsigned int bb_count;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000599
Theodore Ts'o54434922003-12-07 01:28:50 -0500600 bb_count = 0;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000601 errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
602 if (errcode) {
Theodore Ts'odd018f52000-02-06 23:57:07 +0000603 com_err (program_name, errcode,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000604 _("while beginning bad block list iteration"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000605 exit (1);
606 }
607 do {
608 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
609 } while (next_bad && next_bad < from_count);
610
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400611 blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000612 test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
613 if (!blkbuf || !test_record) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000614 com_err(program_name, ENOMEM, _("while allocating buffers"));
Theodore Ts'o879ac922000-01-18 20:59:11 +0000615 exit (1);
616 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400617
618 save_base = blkbuf;
619 test_base = blkbuf + (blocks_at_once * block_size);
620 read_base = blkbuf + (2 * blocks_at_once * block_size);
621
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000622 num_saved = 0;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000623
Theodore Ts'o4d404542001-01-11 16:04:59 +0000624 flush_bufs();
Theodore Ts'o879ac922000-01-18 20:59:11 +0000625 if (v_flag) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500626 fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
Theodore Ts'oacd77412007-10-22 10:09:05 -0400627 fprintf (stderr, _("From block %lu to %lu\n"),
628 (unsigned long) from_count, (unsigned long) last_block);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000629 }
Theodore Ts'o879ac922000-01-18 20:59:11 +0000630 if (s_flag || v_flag > 1) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500631 fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000632 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000633 if (setjmp(terminate_env)) {
634 /*
635 * Abnormal termination by a signal is handled here.
Theodore Ts'o4d003982000-04-03 16:01:11 +0000636 */
Theodore Ts'oa551b782000-07-13 22:05:31 +0000637 signal (SIGALRM, SIG_IGN);
Theodore Ts'o54434922003-12-07 01:28:50 -0500638 fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000639
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400640 save_ptr = save_base;
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000641 for (i=0; i < num_saved; i++) {
642 do_write(dev, save_ptr, test_record[i].num,
643 block_size, test_record[i].block);
644 save_ptr += test_record[i].num * block_size;
645 }
Theodore Ts'o879ac922000-01-18 20:59:11 +0000646 fflush (out);
Theodore Ts'odd018f52000-02-06 23:57:07 +0000647 exit(1);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000648 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000649
650 /* set up abend handler */
651 capture_terminate(terminate_env);
652
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400653 if (t_flag) {
654 pattern = t_patts;
655 nr_pattern = t_flag;
656 } else {
657 pattern = patterns;
658 nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
659 }
660 for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400661 pattern_fill(test_base, pattern[pat_idx],
662 blocks_at_once * block_size);
Theodore Ts'o4d003982000-04-03 16:01:11 +0000663
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400664 buf_used = 0;
665 bb_count = 0;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400666 save_ptr = save_base;
667 test_ptr = test_base;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400668 currently_testing = from_count;
Theodore Ts'o8938ce62006-10-03 23:35:57 -0400669 num_blocks = last_block - 1;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400670 if (s_flag && v_flag <= 1)
671 alarm_intr(SIGALRM);
672
673 while (currently_testing < last_block) {
Iustin Pop931b0282008-06-11 13:12:17 +0200674 if (max_bb && bb_count >= max_bb) {
675 if (s_flag || v_flag) {
676 fputs(_("Too many bad blocks, aborting test\n"), stderr);
677 }
678 break;
679 }
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400680 got = try = blocks_at_once - buf_used;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400681 if (next_bad) {
682 if (currently_testing == next_bad) {
683 /* fprintf (out, "%lu\n", nextbad); */
684 ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
685 currently_testing++;
686 goto check_for_more;
687 }
688 else if (currently_testing + try > next_bad)
689 try = next_bad - currently_testing;
690 }
691 if (currently_testing + try > last_block)
692 try = last_block - currently_testing;
693 got = do_read (dev, save_ptr, try, block_size,
694 currently_testing);
695 if (got == 0) {
696 /* First block must have been bad. */
697 bb_count += bb_output(currently_testing++);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000698 goto check_for_more;
Theodore Ts'o4d003982000-04-03 16:01:11 +0000699 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000700
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400701 /*
702 * Note the fact that we've saved this much data
703 * *before* we overwrite it with test data
704 */
705 test_record[num_saved].block = currently_testing;
706 test_record[num_saved].num = got;
707 num_saved++;
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000708
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400709 /* Write the test data */
710 written = do_write (dev, test_ptr, got, block_size,
711 currently_testing);
712 if (written != got)
713 com_err (program_name, errno,
714 _("during test data write, block %lu"),
Theodore Ts'oacd77412007-10-22 10:09:05 -0400715 (unsigned long) currently_testing +
716 written);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000717
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400718 buf_used += got;
Theodore Ts'o4d003982000-04-03 16:01:11 +0000719 save_ptr += got * block_size;
720 test_ptr += got * block_size;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400721 currently_testing += got;
722 if (got != try)
723 bb_count += bb_output(currently_testing++);
724
725 check_for_more:
726 /*
727 * If there's room for more blocks to be tested this
728 * around, and we're not done yet testing the disk, go
729 * back and get some more blocks.
730 */
731 if ((buf_used != blocks_at_once) &&
732 (currently_testing < last_block))
733 continue;
734
735 flush_bufs();
736 save_currently_testing = currently_testing;
737
738 /*
739 * for each contiguous block that we read into the
740 * buffer (and wrote test data into afterwards), read
741 * it back (looping if necessary, to get past newly
742 * discovered unreadable blocks, of which there should
743 * be none, but with a hard drive which is unreliable,
744 * it has happened), and compare with the test data
745 * that was written; output to the bad block list if
746 * it doesn't match.
747 */
748 used2 = 0;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400749 save_ptr = save_base;
750 test_ptr = test_base;
751 read_ptr = read_base;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400752 try = 0;
753
754 while (1) {
755 if (try == 0) {
756 if (used2 >= num_saved)
757 break;
758 currently_testing = test_record[used2].block;
759 try = test_record[used2].num;
760 used2++;
761 }
762
763 got = do_read (dev, read_ptr, try,
764 block_size, currently_testing);
765
766 /* test the comparison between all the
767 blocks successfully read */
768 for (i = 0; i < got; ++i)
769 if (memcmp (test_ptr+i*block_size,
770 read_ptr+i*block_size, block_size))
771 bb_count += bb_output(currently_testing + i);
772 if (got < try) {
773 bb_count += bb_output(currently_testing + got);
774 got++;
775 }
776
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400777 /* write back original data */
778 do_write (dev, save_ptr, got,
779 block_size, currently_testing);
780 save_ptr += got * block_size;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400781
782 currently_testing += got;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400783 test_ptr += got * block_size;
784 read_ptr += got * block_size;
785 try -= got;
786 }
787
788 /* empty the buffer so it can be reused */
789 num_saved = 0;
790 buf_used = 0;
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400791 save_ptr = save_base;
792 test_ptr = test_base;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400793 currently_testing = save_currently_testing;
Theodore Ts'o4d003982000-04-03 16:01:11 +0000794 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400795 num_blocks = 0;
796 alarm(0);
797 if (s_flag || v_flag > 1)
Theodore Ts'o3ef681c2004-09-19 08:04:44 -0400798 fputs(_(done_string), stderr);
Theodore Ts'o4d003982000-04-03 16:01:11 +0000799
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400800 flush_bufs();
Theodore Ts'o4d003982000-04-03 16:01:11 +0000801 }
Theodore Ts'o4d003982000-04-03 16:01:11 +0000802 uncapture_terminate();
Theodore Ts'odd018f52000-02-06 23:57:07 +0000803 fflush(stderr);
804 free(blkbuf);
Theodore Ts'od49a22b2000-07-06 00:31:27 +0000805 free(test_record);
Theodore Ts'o879ac922000-01-18 20:59:11 +0000806
807 ext2fs_badblocks_list_iterate_end(bb_iter);
808
809 return bb_count;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000810}
811
Theodore Ts'o981dc562000-07-06 14:13:29 +0000812static void check_mount(char *device_name)
813{
814 errcode_t retval;
815 int mount_flags;
816
817 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
818 if (retval) {
819 com_err("ext2fs_check_if_mount", retval,
820 _("while determining whether %s is mounted."),
821 device_name);
822 return;
823 }
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400824 if (mount_flags & EXT2_MF_MOUNTED) {
825 fprintf(stderr, _("%s is mounted; "), device_name);
826 if (force) {
827 fputs(_("badblocks forced anyway. "
828 "Hope /etc/mtab is incorrect.\n"), stderr);
829 return;
830 }
831 abort_badblocks:
832 fputs(_("it's not safe to run badblocks!\n"), stderr);
833 exit(1);
Theodore Ts'o981dc562000-07-06 14:13:29 +0000834 }
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400835
Theodore Ts'of63978a2006-05-13 09:25:47 -0400836 if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400837 fprintf(stderr, _("%s is apparently in use by the system; "),
838 device_name);
839 if (force)
840 fputs(_("badblocks forced anyway.\n"), stderr);
841 else
842 goto abort_badblocks;
843 }
844
Theodore Ts'o981dc562000-07-06 14:13:29 +0000845}
846
Theodore Ts'od4be9fa2007-10-22 10:19:20 -0400847/*
848 * This function will convert a string to an unsigned long, printing
849 * an error message if it fails, and returning success or failure in err.
850 */
851static unsigned int parse_uint(const char *str, const char *descr)
852{
853 char *tmp;
854 unsigned long ret;
855
Iustin Popeb594252008-06-11 17:55:18 +0200856 errno = 0;
Theodore Ts'od4be9fa2007-10-22 10:19:20 -0400857 ret = strtoul(str, &tmp, 0);
858 if (*tmp || errno || (ret > UINT_MAX) ||
859 (ret == ULONG_MAX && errno == ERANGE)) {
860 com_err (program_name, 0, _("invalid %s - %s"), descr, str);
861 exit (1);
862 }
863 return ret;
864}
Theodore Ts'o981dc562000-07-06 14:13:29 +0000865
Theodore Ts'o00e54331997-09-16 02:13:52 +0000866int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000867{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000868 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000869 char * device_name;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000870 char * host_device_name = NULL;
871 char * input_file = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000872 char * output_file = NULL;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000873 FILE * in = NULL;
Theodore Ts'odd018f52000-02-06 23:57:07 +0000874 int block_size = 1024;
Theodore Ts'oacd77412007-10-22 10:09:05 -0400875 unsigned int blocks_at_once = 64;
Theodore Ts'ocd130a02001-05-05 05:43:23 +0000876 blk_t last_block, from_count;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000877 int num_passes = 0;
878 int passes_clean = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000879 int dev;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000880 errcode_t errcode;
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400881 unsigned int pattern;
Theodore Ts'oacd77412007-10-22 10:09:05 -0400882 unsigned int (*test_func)(int, blk_t,
883 int, blk_t,
884 unsigned int);
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400885 int open_flag = 0;
886 long sysval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000887
888 setbuf(stdout, NULL);
889 setbuf(stderr, NULL);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000890#ifdef ENABLE_NLS
891 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500892 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000893 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
894 textdomain(NLS_CAT_NAME);
895#endif
Theodore Ts'o6d40f562003-05-07 08:35:38 -0400896 srandom((unsigned int)time(NULL)); /* simple randomness is enough */
Theodore Ts'o4d003982000-04-03 16:01:11 +0000897 test_func = test_ro;
Theodore Ts'o4d404542001-01-11 16:04:59 +0000898
Theodore Ts'o1c29b092003-07-12 16:01:45 -0400899 /* Determine the system page size if possible */
900#ifdef HAVE_SYSCONF
901#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
902#define _SC_PAGESIZE _SC_PAGE_SIZE
903#endif
904#ifdef _SC_PAGESIZE
905 sysval = sysconf(_SC_PAGESIZE);
906 if (sysval > 0)
907 sys_page_size = sysval;
908#endif /* _SC_PAGESIZE */
909#endif /* HAVE_SYSCONF */
910
Theodore Ts'o3839e651997-04-26 13:21:57 +0000911 if (argc && *argv)
912 program_name = *argv;
Iustin Pop931b0282008-06-11 13:12:17 +0200913 while ((c = getopt (argc, argv, "b:e:fi:o:svwnc:p:h:t:X")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000914 switch (c) {
915 case 'b':
Theodore Ts'od4be9fa2007-10-22 10:19:20 -0400916 block_size = parse_uint(optarg, "block size");
917 if (block_size > 4096) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000918 com_err (program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000919 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000920 exit (1);
921 }
922 break;
Theodore Ts'o981dc562000-07-06 14:13:29 +0000923 case 'f':
924 force++;
925 break;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000926 case 'i':
927 input_file = optarg;
928 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000929 case 'o':
930 output_file = optarg;
931 break;
932 case 's':
933 s_flag = 1;
934 break;
935 case 'v':
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000936 v_flag++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000937 break;
938 case 'w':
Theodore Ts'o4d003982000-04-03 16:01:11 +0000939 if (w_flag)
Theodore Ts'od8b5f772006-11-12 23:09:03 -0500940 exclusive_usage();
Theodore Ts'o4d003982000-04-03 16:01:11 +0000941 test_func = test_rw;
942 w_flag = 1;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000943 break;
944 case 'n':
Theodore Ts'o4d003982000-04-03 16:01:11 +0000945 if (w_flag)
Theodore Ts'od8b5f772006-11-12 23:09:03 -0500946 exclusive_usage();
Theodore Ts'o4d003982000-04-03 16:01:11 +0000947 test_func = test_nd;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000948 w_flag = 2;
949 break;
950 case 'c':
Theodore Ts'od4be9fa2007-10-22 10:19:20 -0400951 blocks_at_once = parse_uint(optarg, "blocks at once");
Theodore Ts'o879ac922000-01-18 20:59:11 +0000952 break;
Iustin Pop931b0282008-06-11 13:12:17 +0200953 case 'e':
954 max_bb = parse_uint(optarg, "max bad block count");
955 break;
Theodore Ts'o879ac922000-01-18 20:59:11 +0000956 case 'p':
Theodore Ts'od4be9fa2007-10-22 10:19:20 -0400957 num_passes = parse_uint(optarg,
958 "number of clean passes");
Theodore Ts'o879ac922000-01-18 20:59:11 +0000959 break;
960 case 'h':
961 host_device_name = optarg;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000962 break;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400963 case 't':
964 if (t_flag + 1 > t_max) {
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400965 unsigned int *t_patts_new;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400966
967 t_patts_new = realloc(t_patts, t_max + T_INC);
968 if (!t_patts_new) {
969 com_err(program_name, ENOMEM,
970 _("can't allocate memory for "
971 "test_pattern - %s"),
972 optarg);
973 exit(1);
974 }
975 t_patts = t_patts_new;
976 t_max += T_INC;
977 }
Theodore Ts'o84c05452003-05-18 01:11:52 -0400978 if (!strcmp(optarg, "r") || !strcmp(optarg,"random")) {
979 t_patts[t_flag++] = ~0;
980 } else {
Theodore Ts'od4be9fa2007-10-22 10:19:20 -0400981 pattern = parse_uint(optarg, "test pattern");
Theodore Ts'oe9860ae2007-10-22 09:51:50 -0400982 if (pattern == (unsigned int) ~0)
Theodore Ts'o84c05452003-05-18 01:11:52 -0400983 pattern = 0xffff;
984 t_patts[t_flag++] = pattern;
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400985 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400986 break;
Theodore Ts'of63978a2006-05-13 09:25:47 -0400987 case 'X':
988 exclusive_ok++;
989 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000990 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +0000991 usage();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000992 }
993 }
Theodore Ts'o849b6bc2003-05-07 09:52:14 -0400994 if (!w_flag) {
995 if (t_flag > 1) {
996 com_err(program_name, 0,
997 _("Maximum of one test_pattern may be specified "
998 "in read-only mode"));
999 exit(1);
1000 }
Theodore Ts'oe9860ae2007-10-22 09:51:50 -04001001 if (t_patts && (t_patts[0] == (unsigned int) ~0)) {
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001002 com_err(program_name, 0,
1003 _("Random test_pattern is not allowed "
1004 "in read-only mode"));
1005 exit(1);
1006 }
1007 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001008 if (optind > argc - 1)
Theodore Ts'o818180c1998-06-27 05:11:14 +00001009 usage();
Theodore Ts'o3839e651997-04-26 13:21:57 +00001010 device_name = argv[optind++];
Theodore Ts'o35964b52000-07-06 13:19:43 +00001011 if (optind > argc - 1) {
1012 errcode = ext2fs_get_device_size(device_name,
1013 block_size,
Theodore Ts'ocd130a02001-05-05 05:43:23 +00001014 &last_block);
Theodore Ts'o35964b52000-07-06 13:19:43 +00001015 if (errcode == EXT2_ET_UNIMPLEMENTED) {
1016 com_err(program_name, 0,
1017 _("Couldn't determine device size; you "
1018 "must specify\nthe size manually\n"));
1019 exit(1);
1020 }
1021 if (errcode) {
1022 com_err(program_name, errcode,
1023 _("while trying to determine device size"));
1024 exit(1);
1025 }
1026 } else {
Theodore Ts'o5267a522007-06-04 01:49:51 -04001027 errno = 0;
Theodore Ts'od4be9fa2007-10-22 10:19:20 -04001028 last_block = parse_uint(argv[optind], "last block");
Theodore Ts'o5267a522007-06-04 01:49:51 -04001029 printf("last_block = %d (%s)\n", last_block, argv[optind]);
Theodore Ts'o5267a522007-06-04 01:49:51 -04001030 last_block++;
Theodore Ts'o35964b52000-07-06 13:19:43 +00001031 optind++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001032 }
Theodore Ts'o35964b52000-07-06 13:19:43 +00001033 if (optind <= argc-1) {
Theodore Ts'o5267a522007-06-04 01:49:51 -04001034 errno = 0;
Theodore Ts'od4be9fa2007-10-22 10:19:20 -04001035 from_count = parse_uint(argv[optind], "start block");
Theodore Ts'o5267a522007-06-04 01:49:51 -04001036 printf("from_count = %d\n", from_count);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001037 } else from_count = 0;
Theodore Ts'ocd130a02001-05-05 05:43:23 +00001038 if (from_count >= last_block) {
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001039 com_err (program_name, 0, _("invalid starting block (%lu): must be less than %lu"),
Theodore Ts'o54434922003-12-07 01:28:50 -05001040 (unsigned long) from_count, (unsigned long) last_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001041 exit (1);
1042 }
Theodore Ts'o981dc562000-07-06 14:13:29 +00001043 if (w_flag)
1044 check_mount(device_name);
1045
Theodore Ts'o1c29b092003-07-12 16:01:45 -04001046 open_flag = w_flag ? O_RDWR : O_RDONLY;
1047 dev = open (device_name, open_flag);
Theodore Ts'o5493a272002-01-02 14:15:35 -05001048 if (dev == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001049 com_err (program_name, errno, _("while trying to open %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +00001050 device_name);
1051 exit (1);
1052 }
Theodore Ts'o879ac922000-01-18 20:59:11 +00001053 if (host_device_name) {
Theodore Ts'o1c29b092003-07-12 16:01:45 -04001054 host_dev = open (host_device_name, open_flag);
Theodore Ts'o5493a272002-01-02 14:15:35 -05001055 if (host_dev == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001056 com_err (program_name, errno,
1057 _("while trying to open %s"),
1058 host_device_name);
Theodore Ts'o879ac922000-01-18 20:59:11 +00001059 exit (1);
1060 }
1061 } else
1062 host_dev = dev;
Theodore Ts'o3e699062002-10-13 23:56:28 -04001063 if (input_file) {
Theodore Ts'o879ac922000-01-18 20:59:11 +00001064 if (strcmp (input_file, "-") == 0)
1065 in = stdin;
1066 else {
1067 in = fopen (input_file, "r");
1068 if (in == NULL)
1069 {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001070 com_err (program_name, errno,
1071 _("while trying to open %s"),
Theodore Ts'o879ac922000-01-18 20:59:11 +00001072 input_file);
1073 exit (1);
1074 }
1075 }
Theodore Ts'o3e699062002-10-13 23:56:28 -04001076 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001077 if (output_file && strcmp (output_file, "-") != 0)
1078 {
1079 out = fopen (output_file, "w");
1080 if (out == NULL)
1081 {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001082 com_err (program_name, errno,
1083 _("while trying to open %s"),
Theodore Ts'o879ac922000-01-18 20:59:11 +00001084 output_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001085 exit (1);
1086 }
1087 }
1088 else
1089 out = stdout;
Theodore Ts'o879ac922000-01-18 20:59:11 +00001090
1091 errcode = ext2fs_badblocks_list_create(&bb_list,0);
1092 if (errcode) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001093 com_err (program_name, errcode,
Theodore Ts'obb145b02005-06-20 08:35:27 -04001094 _("while creating in-memory bad blocks list"));
Theodore Ts'o879ac922000-01-18 20:59:11 +00001095 exit (1);
1096 }
1097
1098 if (in) {
1099 for(;;) {
Theodore Ts'oa551b782000-07-13 22:05:31 +00001100 switch(fscanf (in, "%u\n", &next_bad)) {
Theodore Ts'o879ac922000-01-18 20:59:11 +00001101 case 0:
1102 com_err (program_name, 0, "input file - bad format");
1103 exit (1);
1104 case EOF:
1105 break;
1106 default:
1107 errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
1108 if (errcode) {
Theodore Ts'obb145b02005-06-20 08:35:27 -04001109 com_err (program_name, errcode, _("while adding to in-memory bad block list"));
Theodore Ts'o879ac922000-01-18 20:59:11 +00001110 exit (1);
1111 }
1112 continue;
1113 }
1114 break;
1115 }
1116
1117 if (in != stdin)
1118 fclose (in);
1119 }
1120
1121 do {
1122 unsigned int bb_count;
1123
Theodore Ts'ocd130a02001-05-05 05:43:23 +00001124 bb_count = test_func(dev, last_block, block_size,
Theodore Ts'o4d003982000-04-03 16:01:11 +00001125 from_count, blocks_at_once);
1126 if (bb_count)
1127 passes_clean = 0;
1128 else
1129 ++passes_clean;
1130
Theodore Ts'o879ac922000-01-18 20:59:11 +00001131 if (v_flag)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001132 fprintf(stderr,
1133 _("Pass completed, %u bad blocks found.\n"),
1134 bb_count);
Theodore Ts'o879ac922000-01-18 20:59:11 +00001135
1136 } while (passes_clean < num_passes);
1137
Theodore Ts'o3839e651997-04-26 13:21:57 +00001138 close (dev);
1139 if (out != stdout)
1140 fclose (out);
Theodore Ts'o849b6bc2003-05-07 09:52:14 -04001141 if (t_patts)
1142 free(t_patts);
Theodore Ts'o879ac922000-01-18 20:59:11 +00001143 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001144}
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001145