#include <unistd.h>
#include <setjmp.h>
#include <time.h>
+#include <limits.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include "nls-enable.h"
const char * program_name = "badblocks";
-const char * done_string = N_("done \n");
+const char * done_string = N_("done \n");
static int v_flag = 0; /* verbose */
static int w_flag = 0; /* do r/w test: 0=no, 1=yes,
static int t_max = 0; /* allocated test patterns */
static unsigned long *t_patts = NULL; /* test patterns */
static int current_O_DIRECT = 0; /* Current status of O_DIRECT flag */
+static int exclusive_ok = 0;
#define T_INC 32
exit (1);
}
+static void exclusive_usage(void)
+{
+ fprintf(stderr,
+ _("%s: The -n and -w options are mutually exclusive.\n\n"),
+ program_name);
+ exit(1);
+}
+
static unsigned long currently_testing = 0;
static unsigned long num_blocks = 0;
static ext2_badblocks_list bb_list = NULL;
static void print_status(void)
{
- fprintf(stderr, "%9ld/%9ld", currently_testing, num_blocks);
- fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
+ fprintf(stderr, "%15ld/%15ld", currently_testing, num_blocks);
+ 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);
fflush (stderr);
}
-static void alarm_intr(int alnum)
+static void alarm_intr(int alnum EXT2FS_ATTR((unused)))
{
signal (SIGALRM, alarm_intr);
alarm(1);
if (!num_blocks)
return;
- fprintf(stderr, "%9ld/%9ld", currently_testing, num_blocks);
- fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
- fflush (stderr);
+ print_status();
}
static void *terminate_addr = NULL;
-static void terminate_intr(int signo)
+static void terminate_intr(int signo EXT2FS_ATTR((unused)))
{
if (terminate_addr)
longjmp(terminate_addr,1);
signal (SIGUSR2, SIG_DFL);
}
-static void set_o_direct(int dev, unsigned char *buffer, size_t size)
+static void set_o_direct(int dev, unsigned char *buffer, size_t size,
+ unsigned long current_block)
{
#ifdef O_DIRECT
int new_flag = O_DIRECT;
int flag;
if ((((unsigned long) buffer & (sys_page_size - 1)) != 0) ||
- ((size & (sys_page_size - 1)) != 0))
+ ((size & (sys_page_size - 1)) != 0) ||
+ ((current_block & ((sys_page_size >> 9)-1)) != 0))
new_flag = 0;
if (new_flag != current_O_DIRECT) {
- printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing");
+ /* printf("%s O_DIRECT\n", new_flag ? "Setting" : "Clearing"); */
flag = fcntl(dev, F_GETFL);
if (flag > 0) {
flag = (flag & ~O_DIRECT) | new_flag;
static void pattern_fill(unsigned char *buffer, unsigned long pattern,
size_t n)
{
- int i, nb;
+ unsigned int i, nb;
unsigned char bpattern[sizeof(pattern)], *ptr;
- if (pattern == ~0) {
+ if (pattern == (unsigned long) ~0) {
for (ptr = buffer; ptr < buffer + n; ptr++) {
(*ptr) = random() % (1 << (8 * sizeof(char)));
}
if (s_flag | v_flag)
- fprintf(stderr, _("Testing with random pattern: "));
+ fputs(_("Testing with random pattern: "), stderr);
} else {
bpattern[0] = 0;
for (i = 0; i < sizeof(bpattern); i++) {
}
nb = i ? (i-1) : 0;
for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
- *ptr = bpattern[i--];
- if (i < 0)
+ *ptr = bpattern[i];
+ if (i == 0)
i = nb;
+ else
+ i--;
}
if (s_flag | v_flag) {
- fprintf(stderr, _("Testing with pattern 0x"));
+ fputs(_("Testing with pattern 0x"), stderr);
for (i = 0; i <= nb; i++)
fprintf(stderr, "%02x", buffer[i]);
- fprintf(stderr, ": ");
+ fputs(": ", stderr);
}
}
}
{
long got;
- set_o_direct(dev, buffer, try * block_size);
+ set_o_direct(dev, buffer, try * block_size, current_block);
if (v_flag > 1)
print_status();
{
long got;
- set_o_direct(dev, buffer, try * block_size);
+ set_o_direct(dev, buffer, try * block_size, current_block);
if (v_flag > 1)
print_status();
if (got < 0)
got = 0;
if (got & 511)
- fprintf (stderr,
- "Weird value (%ld) in do_write\n", got);
+ fprintf(stderr, "Weird value (%ld) in do_write\n", got);
got /= block_size;
return got;
}
}
if (v_flag) {
fprintf (stderr, _("Checking blocks %lu to %lu\n"), from_count,
- last_block);
+ last_block - 1);
}
if (t_flag) {
- fprintf(stderr, _("Checking for bad blocks in read-only mode\n"));
+ fputs(_("Checking for bad blocks in read-only mode\n"), stderr);
pattern_fill(blkbuf + blocks_at_once * block_size,
t_patts[0], block_size);
}
flush_bufs();
try = blocks_at_once;
currently_testing = from_count;
- num_blocks = last_block;
+ num_blocks = last_block - 1;
if (!t_flag && (s_flag || v_flag)) {
- fprintf(stderr,
- _("Checking for bad blocks (read-only test): "));
+ fputs(_("Checking for bad blocks (read-only test): "), stderr);
if (v_flag <= 1)
alarm_intr(SIGALRM);
}
currently_testing += got;
if (got == try) {
try = blocks_at_once;
+ /* recover page-aligned offset for O_DIRECT */
+ if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
+ && (currently_testing % (sys_page_size >> 9)!= 0))
+ try -= (sys_page_size >> 9)
+ - (currently_testing
+ % (sys_page_size >> 9));
continue;
}
else
num_blocks = 0;
alarm(0);
if (s_flag || v_flag)
- fputs(done_string, stderr);
+ fputs(_(done_string), stderr);
fflush (stderr);
free (blkbuf);
flush_bufs();
if (v_flag) {
- fprintf(stderr,
- _("Checking for bad blocks in read-write mode\n"));
+ fputs(_("Checking for bad blocks in read-write mode\n"),
+ stderr);
fprintf(stderr, _("From block %lu to %lu\n"),
from_count, last_block);
}
for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
pattern_fill(buffer, pattern[pat_idx],
blocks_at_once * block_size);
- num_blocks = last_block;
+ num_blocks = last_block - 1;
currently_testing = from_count;
if (s_flag && v_flag <= 1)
alarm_intr(SIGALRM);
currently_testing += got;
if (got == try) {
try = blocks_at_once;
+ /* recover page-aligned offset for O_DIRECT */
+ if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
+ && (currently_testing %
+ (sys_page_size >> 9)!= 0))
+ try -= (sys_page_size >> 9)
+ - (currently_testing
+ % (sys_page_size >> 9));
continue;
} else
try = 1;
num_blocks = 0;
alarm (0);
if (s_flag | v_flag)
- fputs(done_string, stderr);
+ fputs(_(done_string), stderr);
flush_bufs();
if (s_flag | v_flag)
- fprintf (stderr, _("Reading and comparing: "));
+ fputs(_("Reading and comparing: "), stderr);
num_blocks = last_block;
currently_testing = from_count;
if (s_flag && v_flag <= 1)
bb_count += bb_output(currently_testing+i);
}
currently_testing += got;
+ /* recover page-aligned offset for O_DIRECT */
+ if ( blocks_at_once >= (unsigned long) (sys_page_size >> 9)
+ && (currently_testing % (sys_page_size >> 9)!= 0))
+ try = blocks_at_once - (sys_page_size >> 9)
+ - (currently_testing
+ % (sys_page_size >> 9));
+ else
+ try = blocks_at_once;
if (v_flag > 1)
print_status();
}
num_blocks = 0;
alarm (0);
if (s_flag | v_flag)
- fputs(done_string, stderr);
+ fputs(_(done_string), stderr);
flush_bufs();
}
uncapture_terminate();
static int num_saved;
jmp_buf terminate_env;
errcode_t errcode;
- long buf_used;
- unsigned int bb_count = 0;
+ unsigned long buf_used;
+ static unsigned int bb_count;
+ bb_count = 0;
errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
if (errcode) {
com_err (program_name, errcode,
flush_bufs();
if (v_flag) {
- fprintf (stderr,
- _("Checking for bad blocks in non-destructive read-write mode\n"));
+ fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
fprintf (stderr, _("From block %lu to %lu\n"), from_count, last_block);
}
if (s_flag || v_flag > 1) {
- fprintf(stderr, _("Checking for bad blocks (non-destructive read-write test)\n"));
+ fputs(_("Checking for bad blocks (non-destructive read-write test)\n"), stderr);
}
if (setjmp(terminate_env)) {
/*
* Abnormal termination by a signal is handled here.
*/
signal (SIGALRM, SIG_IGN);
- fprintf(stderr, _("\nInterrupt caught, cleaning up\n"));
+ fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
save_ptr = save_base;
for (i=0; i < num_saved; i++) {
save_ptr = save_base;
test_ptr = test_base;
currently_testing = from_count;
- num_blocks = last_block;
+ num_blocks = last_block - 1;
if (s_flag && v_flag <= 1)
alarm_intr(SIGALRM);
num_blocks = 0;
alarm(0);
if (s_flag || v_flag > 1)
- fputs(done_string, stderr);
+ fputs(_(done_string), stderr);
flush_bufs();
}
device_name);
return;
}
- if (!(mount_flags & EXT2_MF_MOUNTED))
- return;
+ if (mount_flags & EXT2_MF_MOUNTED) {
+ fprintf(stderr, _("%s is mounted; "), device_name);
+ if (force) {
+ fputs(_("badblocks forced anyway. "
+ "Hope /etc/mtab is incorrect.\n"), stderr);
+ return;
+ }
+ abort_badblocks:
+ fputs(_("it's not safe to run badblocks!\n"), stderr);
+ exit(1);
+ }
- fprintf(stderr, _("%s is mounted; "), device_name);
- if (force) {
- fprintf(stderr, _("badblocks forced anyway. "
- "Hope /etc/mtab is incorrect.\n"));
- return;
+ if ((mount_flags & EXT2_MF_BUSY) && !exclusive_ok) {
+ fprintf(stderr, _("%s is apparently in use by the system; "),
+ device_name);
+ if (force)
+ fputs(_("badblocks forced anyway.\n"), stderr);
+ else
+ goto abort_badblocks;
}
- fprintf(stderr, _("it's not safe to run badblocks!\n"));
- exit(1);
+
}
char * output_file = NULL;
FILE * in = NULL;
int block_size = 1024;
- unsigned long blocks_at_once = 16;
+ unsigned long blocks_at_once = 64;
blk_t last_block, from_count;
int num_passes = 0;
int passes_clean = 0;
if (argc && *argv)
program_name = *argv;
- while ((c = getopt (argc, argv, "b:fi:o:svwnc:p:h:t:")) != EOF) {
+ while ((c = getopt (argc, argv, "b:fi:o:svwnc:p:h:t:X")) != EOF) {
switch (c) {
case 'b':
block_size = strtoul (optarg, &tmp, 0);
break;
case 'w':
if (w_flag)
- usage();
+ exclusive_usage();
test_func = test_rw;
w_flag = 1;
break;
case 'n':
if (w_flag)
- usage();
+ exclusive_usage();
test_func = test_nd;
w_flag = 2;
break;
optarg);
exit(1);
}
- if (pattern == ~0)
+ if (pattern == (unsigned long) ~0)
pattern = 0xffff;
t_patts[t_flag++] = pattern;
}
break;
+ case 'X':
+ exclusive_ok++;
+ break;
default:
usage();
}
"in read-only mode"));
exit(1);
}
- if (t_patts && (t_patts[0] == ~0)) {
+ if (t_patts && (t_patts[0] == (unsigned long) ~0)) {
com_err(program_name, 0,
_("Random test_pattern is not allowed "
"in read-only mode"));
exit(1);
}
} else {
+ errno = 0;
last_block = strtoul (argv[optind], &tmp, 0);
- if (*tmp) {
- com_err (program_name, 0, _("bad blocks count - %s"),
+ printf("last_block = %d (%s)\n", last_block, argv[optind]);
+ if (*tmp || errno ||
+ (last_block == ULONG_MAX && errno == ERANGE)) {
+ com_err (program_name, 0, _("invalid blocks count - %s"),
argv[optind]);
exit (1);
}
+ last_block++;
optind++;
}
if (optind <= argc-1) {
+ errno = 0;
from_count = strtoul (argv[optind], &tmp, 0);
- if (*tmp) {
- com_err (program_name, 0, _("bad starting block - %s"),
+ printf("from_count = %d\n", from_count);
+ if (*tmp || errno ||
+ (from_count == ULONG_MAX && errno == ERANGE)) {
+ com_err (program_name, 0, _("invalid starting block - %s"),
argv[optind]);
exit (1);
}
} else from_count = 0;
if (from_count >= last_block) {
- com_err (program_name, 0, _("bad blocks range: %lu-%lu"),
- from_count, last_block);
+ com_err (program_name, 0, _("invalid starting block (%d): must be less than %lu"),
+ (unsigned long) from_count, (unsigned long) last_block);
exit (1);
}
if (w_flag)
errcode = ext2fs_badblocks_list_create(&bb_list,0);
if (errcode) {
com_err (program_name, errcode,
- _("creating in-memory bad blocks list"));
+ _("while creating in-memory bad blocks list"));
exit (1);
}
default:
errcode = ext2fs_badblocks_list_add(bb_list,next_bad);
if (errcode) {
- com_err (program_name, errcode, _("adding to in-memory bad block list"));
+ com_err (program_name, errcode, _("while adding to in-memory bad block list"));
exit (1);
}
continue;