Whamcloud - gitweb
In mke2fs and e2fsck, specifying the -c option twice will now do
[tools/e2fsprogs.git] / misc / badblocks.c
index 475c9c3..8de08f7 100644 (file)
@@ -33,6 +33,9 @@
 #include <fcntl.h>
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
+#else
+extern char *optarg;
+extern int optind;
 #endif
 #include <signal.h>
 #include <stdio.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
 
-#if HAVE_LINUX_FS_H
-#include <linux/fd.h>
-#endif
-
 #include "et/com_err.h"
 #include "ext2fs/ext2_io.h"
-#include <linux/ext2_fs.h>
+#include "ext2fs/ext2_fs.h"
 #include "ext2fs/ext2fs.h"
 #include "nls-enable.h"
 
@@ -61,13 +60,11 @@ static int v_flag = 0;                      /* verbose */
 static int w_flag = 0;                 /* do r/w test: 0=no, 1=yes,
                                         * 2=non-destructive */
 static int s_flag = 0;                 /* show progress of test */
-
-static char *blkbuf;           /* Allocation array for bad block testing */
-
+static int force = 0;                  /* force check of mounted device */
 
 static void usage(void)
 {
-       fprintf(stderr, _("Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwn]\n [-c blocks_at_once] [-p num_passes] device blocks_count [start_count]\n"),
+       fprintf(stderr, _("Usage: %s [-b block_size] [-i input_file] [-o output_file] [-svwnf]\n [-c blocks_at_once] [-p num_passes] device [last_block [start_count]]\n"),
                 program_name);
        exit (1);
 }
@@ -107,14 +104,14 @@ static int bb_output (unsigned long bad)
        return 1;
 }
 
-static void print_status (void)
+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");
        fflush (stderr);
 }
 
-static void alarm_intr (int alnum)
+static void alarm_intr(int alnum)
 {
        signal (SIGALRM, alarm_intr);
        alarm(1);
@@ -127,14 +124,14 @@ static void alarm_intr (int alnum)
 
 static void *terminate_addr = NULL;
 
-static void terminate_intr (int signo)
+static void terminate_intr(int signo)
 {
        if (terminate_addr)
                longjmp(terminate_addr,1);
        exit(1);
 }
 
-static void capture_terminate (jmp_buf term_addr)
+static void capture_terminate(jmp_buf term_addr)
 {
        terminate_addr = term_addr;
        signal (SIGHUP, terminate_intr);
@@ -145,7 +142,7 @@ static void capture_terminate (jmp_buf term_addr)
        signal (SIGUSR2, terminate_intr);
 }
 
-static void uncapture_terminate()
+static void uncapture_terminate(void)
 {
        terminate_addr = NULL;
        signal (SIGHUP, SIG_DFL);
@@ -213,27 +210,16 @@ static long do_write (int dev, char * buffer, int try, int block_size,
 
 static int host_dev;
 
-static void flush_bufs (int dev, int sync)
+static void flush_bufs(void)
 {
-  if (v_flag
-#if !defined (BLKFLSBUF) && !defined (FDFLUSH)
-      && sync
-#endif
-      )
-    fprintf (stderr, _("Flushing buffers\n"));
-
-  if (sync && fdatasync (dev) == -1)
-    com_err (program_name, errno, _("during fsync"));
+       errcode_t       retval;
 
-#ifdef BLKFLSBUF
-  ioctl (host_dev, BLKFLSBUF, 0);   /* In case this is a HD */
-#endif
-#ifdef FDFLUSH
-  ioctl (host_dev, FDFLUSH, 0);   /* In case this is floppy */
-#endif
+       retval = ext2fs_sync_device(host_dev, 1);
+       if (retval)
+               com_err(program_name, retval, _("during ext2fs_sync_device"));
 }
 
-static unsigned int test_ro (int dev, unsigned long blocks_count,
+static unsigned int test_ro (int dev, unsigned long last_block,
                             int block_size, unsigned long from_count,
                             unsigned long blocks_at_once)
 {
@@ -259,22 +245,22 @@ static unsigned int test_ro (int dev, unsigned long blocks_count,
                com_err (program_name, ENOMEM, _("while allocating buffers"));
                exit (1);
        }
-       flush_bufs (dev, 0);
+       flush_bufs();
        if (v_flag) {
            fprintf(stderr, _("Checking for bad blocks in read-only mode\n"));
            fprintf (stderr, _("From block %lu to %lu\n"), from_count,
-                    blocks_count);
+                    last_block);
        }
        try = blocks_at_once;
        currently_testing = from_count;
-       num_blocks = blocks_count;
+       num_blocks = last_block;
        if (s_flag || v_flag > 1) {
                fprintf(stderr,
                        _("Checking for bad blocks (read-only test): "));
                if (v_flag <= 1)
                        alarm_intr(SIGALRM);
        }
-       while (currently_testing < blocks_count)
+       while (currently_testing < last_block)
        {
                if (next_bad) {
                        if (currently_testing == next_bad) {
@@ -286,8 +272,8 @@ static unsigned int test_ro (int dev, unsigned long blocks_count,
                        else if (currently_testing + try > next_bad)
                                try = next_bad - currently_testing;
                }
-               if (currently_testing + try > blocks_count)
-                       try = blocks_count - currently_testing;
+               if (currently_testing + try > last_block)
+                       try = last_block - currently_testing;
                got = do_read (dev, blkbuf, try, block_size, currently_testing);
                currently_testing += got;
                if (got == try) {
@@ -313,13 +299,13 @@ static unsigned int test_ro (int dev, unsigned long blocks_count,
        return bb_count;
 }
 
-static unsigned int test_rw (int dev, unsigned long blocks_count,
+static unsigned int test_rw (int dev, unsigned long last_block,
                             int block_size, unsigned long from_count,
                             unsigned long blocks_at_once)
 {
        int i;
        char * buffer;
-       unsigned char pattern[] = {0xaa, 0x55, 0xff, 0x00};
+       static unsigned char pattern[] = {0xaa, 0x55, 0xff, 0x00};
        unsigned int bb_count = 0;
 
        buffer = malloc (2 * block_size);
@@ -329,25 +315,25 @@ static unsigned int test_rw (int dev, unsigned long blocks_count,
                exit (1);
        }
 
-       flush_bufs (dev, 0);
+       flush_bufs();
 
        if (v_flag) {
                fprintf(stderr,
                        _("Checking for bad blocks in read-write mode\n"));
                fprintf(stderr, _("From block %lu to %lu\n"),
-                        from_count, blocks_count);
+                        from_count, last_block);
        }
        for (i = 0; i < sizeof (pattern); i++) {
                memset (buffer, pattern[i], block_size);
                if (s_flag | v_flag)
                        fprintf (stderr, _("Writing pattern 0x%08x: "),
                                 *((int *) buffer));
-               num_blocks = blocks_count;
+               num_blocks = last_block;
                currently_testing = from_count;
                if (s_flag && v_flag <= 1)
                        alarm_intr(SIGALRM);
                for (;
-                    currently_testing < blocks_count;
+                    currently_testing < last_block;
                     currently_testing++)
                {
                        if (ext2fs_llseek (dev, (ext2_loff_t) currently_testing *
@@ -364,15 +350,15 @@ static unsigned int test_rw (int dev, unsigned long blocks_count,
                alarm (0);
                if (s_flag | v_flag)
                        fprintf(stderr, _(done_string));
-               flush_bufs (dev, 1);
+               flush_bufs();
                if (s_flag | v_flag)
                        fprintf (stderr, _("Reading and comparing: "));
-               num_blocks = blocks_count;
+               num_blocks = last_block;
                currently_testing = from_count;
                if (s_flag && v_flag <= 1)
                        alarm_intr(SIGALRM);
                for (;
-                    currently_testing < blocks_count;
+                    currently_testing < last_block;
                     currently_testing++)
                {
                        if (ext2fs_llseek (dev, (ext2_loff_t) currently_testing *
@@ -392,27 +378,32 @@ static unsigned int test_rw (int dev, unsigned long blocks_count,
                alarm (0);
                if (s_flag | v_flag)
                        fprintf(stderr, _(done_string));
-               flush_bufs (dev, 0);
+               flush_bufs();
        }
 
        return bb_count;
 }
 
-static unsigned int test_nd (int dev, unsigned long blocks_count,
+struct saved_blk_record {
+       blk_t   block;
+       int     num;
+};
+
+static unsigned int test_nd (int dev, unsigned long last_block,
                             int block_size, unsigned long from_count,
                             unsigned long blocks_at_once)
 {
        char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
        char * ptr;
        int try, i;
-       long got, used2;
-       unsigned long *bufblk;
-       unsigned long *bufblks;
+       long got, used2, written, save_currently_testing;
+       struct saved_blk_record *test_record;
+       /* This is static to prevent being clobbered by the longjmp */
+       static int num_saved;
        jmp_buf terminate_env;
        errcode_t errcode;
-       /* These are static to prevent being clobbered by the longjmp */
-       static long buf_used = 0;
-       static unsigned int bb_count = 0;
+       long buf_used;
+       unsigned int bb_count;
 
        errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
        if (errcode) {
@@ -425,12 +416,12 @@ static unsigned int test_nd (int dev, unsigned long blocks_count,
        } while (next_bad && next_bad < from_count);
 
        blkbuf = malloc (3 * blocks_at_once * block_size);
-       bufblk = malloc (blocks_at_once * sizeof(unsigned long));
-       bufblks = malloc (blocks_at_once * sizeof(unsigned long));
-       if (!blkbuf || !bufblk || !bufblks) {
+       test_record = malloc (blocks_at_once*sizeof(struct saved_blk_record));
+       if (!blkbuf || !test_record) {
                com_err(program_name, ENOMEM, _("while allocating buffers"));
                exit (1);
        }
+       num_saved = 0;
 
        /* inititalize the test data randomly: */
        if (v_flag) {
@@ -442,11 +433,11 @@ static unsigned int test_nd (int dev, unsigned long blocks_count,
                (*ptr) = random() % (1 << sizeof(char));
        }
 
-       flush_bufs (dev, 0);
+       flush_bufs();
        if (v_flag) {
            fprintf (stderr,
                     _("Checking for bad blocks in non-destructive read-write mode\n"));
-           fprintf (stderr, _("From block %lu to %lu\n"), from_count, blocks_count);
+           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): "));
@@ -456,21 +447,16 @@ static unsigned int test_nd (int dev, unsigned long blocks_count,
        if (setjmp(terminate_env)) {
                /*
                 * Abnormal termination by a signal is handled here.
-                * buf_used will always contain the number of blocks
-                * saved in a non-destructive test, so they can be
-                * rewritten back to the disk.
                 */
-               long buf_written;
-
-               fprintf(stderr, _("Interrupt caught, cleaning up\n"));
-
-               for (buf_written = 0;
-                       buf_written < buf_used;
-                          buf_written += bufblks[buf_written])
-                       do_write (dev, blkbuf + buf_written * block_size,
-                                 bufblks[buf_written], block_size, 
-                                 bufblk[buf_written]);
+               signal (SIGALRM, SIG_IGN);
+               fprintf(stderr, _("\nInterrupt caught, cleaning up\n"));
 
+               save_ptr = blkbuf;
+               for (i=0; i < num_saved; i++) {
+                       do_write(dev, save_ptr, test_record[i].num,
+                                block_size, test_record[i].block);
+                       save_ptr += test_record[i].num * block_size;
+               }
                fflush (out);
                exit(1);
        }
@@ -478,42 +464,51 @@ static unsigned int test_nd (int dev, unsigned long blocks_count,
        /* set up abend handler */
        capture_terminate(terminate_env);
 
-       buf_used = 0; save_ptr = blkbuf;
+       buf_used = 0;
+       bb_count = 0;
+       save_ptr = blkbuf;
        test_ptr = blkbuf + (blocks_at_once * block_size);
        currently_testing = from_count;
-       num_blocks = blocks_count;
+       num_blocks = last_block;
 
-       while (currently_testing < blocks_count) {
+       while (currently_testing < last_block) {
                try = blocks_at_once - buf_used;
                if (next_bad) {
                        if (currently_testing == next_bad) {
                                /* fprintf (out, "%lu\n", nextbad); */
                                ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
-                               bufblk[buf_used] = currently_testing++;
-                               goto test_full_buf;
+                               currently_testing++;
+                               goto check_for_more;
                        }
                        else if (currently_testing + try > next_bad)
                                try = next_bad - currently_testing;
                }
-               if (currently_testing + try > blocks_count)
-                       try = blocks_count - currently_testing;
+               if (currently_testing + try > last_block)
+                       try = last_block - currently_testing;
                got = do_read (dev, save_ptr, try, block_size,
                               currently_testing);
+               if (got == 0) {
+                       /* First block must have been bad. */
+                       bb_count += bb_output(currently_testing++);
+                       goto check_for_more;
+               }
 
-               /* if reading succeeded, write the test data */
-               if (got) {
-                       long written;
-
-                       written = do_write (dev, test_ptr, got, block_size,
-                                           currently_testing);
-                       if (written != got)
-                               com_err (program_name, errno,
+               /*
+                * Note the fact that we've saved this much data
+                * *before* we overwrite it with test data
+                */
+               test_record[num_saved].block = currently_testing;
+               test_record[num_saved].num = got;
+               num_saved++;
+
+               /* Write the test data */
+               written = do_write (dev, test_ptr, got, block_size,
+                                   currently_testing);
+               if (written != got)
+                       com_err (program_name, errno,
                                 _("during test data write, block %lu"),
-                                        currently_testing + written);
-               }
+                                currently_testing + written);
 
-               bufblk[buf_used] = currently_testing;
-               bufblks[buf_used] = got;
                buf_used += got;
                save_ptr += got * block_size;
                test_ptr += got * block_size;
@@ -521,17 +516,18 @@ static unsigned int test_nd (int dev, unsigned long blocks_count,
                if (got != try)
                        bb_count += bb_output(currently_testing++);
 
-       test_full_buf:
+       check_for_more:
                /*
                 * If there's room for more blocks to be tested this
                 * around, and we're not done yet testing the disk, go
                 * back and get some more blocks.
                 */
                if ((buf_used != blocks_at_once) &&
-                   (currently_testing != blocks_count))
+                   (currently_testing < last_block))
                        continue;
 
-               flush_bufs (dev, 1);
+               flush_bufs();
+               save_currently_testing = currently_testing;
 
                /*
                 * for each contiguous block that we read into the
@@ -547,10 +543,17 @@ static unsigned int test_nd (int dev, unsigned long blocks_count,
                save_ptr = blkbuf;
                test_ptr = blkbuf + (blocks_at_once * block_size);
                read_ptr = blkbuf + (2 * blocks_at_once * block_size);
-               currently_testing = bufblk[0];
-               try = bufblks[0];
+               try = 0;
 
-               while (currently_testing < blocks_count) {
+               while (1) {
+                       if (try == 0) {
+                               if (used2 >= num_saved)
+                                       break;
+                               currently_testing = test_record[used2].block;
+                               try = test_record[used2].num;
+                               used2++;
+                       }
+                               
                        got = do_read (dev, read_ptr, try,
                                       block_size, currently_testing);
 
@@ -574,18 +577,14 @@ static unsigned int test_nd (int dev, unsigned long blocks_count,
                        test_ptr += got * block_size;
                        read_ptr += got * block_size;
                        try -= got;
-
-                       if (try == 0) {
-                               used2 += bufblks[used2];
-                               if (used2 >= blocks_at_once)
-                                       break;
-                               currently_testing = bufblk[used2];
-                               try = bufblks[used2];
-                       }
                }
 
                /* empty the buffer so it can be reused */
+               num_saved = 0;
                buf_used = 0;
+               save_ptr = blkbuf;
+               test_ptr = blkbuf + (blocks_at_once * block_size);
+               currently_testing = save_currently_testing;
        }
        num_blocks = 0;
        alarm(0);
@@ -595,14 +594,39 @@ static unsigned int test_nd (int dev, unsigned long blocks_count,
 
        fflush(stderr);
        free(blkbuf);
-       free(bufblk);
-       free(bufblks);
+       free(test_record);
 
        ext2fs_badblocks_list_iterate_end(bb_iter);
 
        return bb_count;
 }
 
+static void check_mount(char *device_name)
+{
+       errcode_t       retval;
+       int             mount_flags;
+
+       retval = ext2fs_check_if_mounted(device_name, &mount_flags);
+       if (retval) {
+               com_err("ext2fs_check_if_mount", retval,
+                       _("while determining whether %s is mounted."),
+                       device_name);
+               return;
+       }
+       if (!(mount_flags & EXT2_MF_MOUNTED))
+               return;
+
+       fprintf(stderr, _("%s is mounted; "), device_name);
+       if (force) {
+               fprintf(stderr, _("badblocks forced anyway.  "
+                       "Hope /etc/mtab is incorrect.\n"));
+               return;
+       }
+       fprintf(stderr, _("it's not safe to run badblocks!\n"));
+       exit(1);
+}
+
+
 int main (int argc, char ** argv)
 {
        int c;
@@ -614,15 +638,14 @@ int main (int argc, char ** argv)
        FILE * in = NULL;
        int block_size = 1024;
        unsigned long blocks_at_once = 16;
-       unsigned long blocks_count, from_count;
+       blk_t last_block, from_count;
        int num_passes = 0;
        int passes_clean = 0;
        int dev;
        errcode_t errcode;
-       unsigned int (*test_func)(int dev, unsigned long blocks_count,
-                                 int block_size, unsigned long from_count,
-                                 unsigned long blocks_at_once);
-       size_t  buf_size;
+       unsigned int (*test_func)(int, unsigned long,
+                                 int, unsigned long,
+                                 unsigned long);
 
        setbuf(stdout, NULL);
        setbuf(stderr, NULL);
@@ -632,10 +655,10 @@ int main (int argc, char ** argv)
        textdomain(NLS_CAT_NAME);
 #endif
        test_func = test_ro;
-       
+
        if (argc && *argv)
                program_name = *argv;
-       while ((c = getopt (argc, argv, "b:i:o:svwnc:p:h:")) != EOF) {
+       while ((c = getopt (argc, argv, "b:fi:o:svwnc:p:h:")) != EOF) {
                switch (c) {
                case 'b':
                        block_size = strtoul (optarg, &tmp, 0);
@@ -645,6 +668,9 @@ int main (int argc, char ** argv)
                                exit (1);
                        }
                        break;
+               case 'f':
+                       force++;
+                       break;
                case 'i':
                        input_file = optarg;
                        break;
@@ -695,23 +721,46 @@ int main (int argc, char ** argv)
        if (optind > argc - 1)
                usage();
        device_name = argv[optind++];
-       if (optind > argc - 1)
-               usage();
-       blocks_count = strtoul (argv[optind], &tmp, 0);
-       if (*tmp)
-       {
-               com_err (program_name, 0, _("bad blocks count - %s"),
-                        argv[optind]);
-               exit (1);
+       if (optind > argc - 1) {
+               errcode = ext2fs_get_device_size(device_name,
+                                                block_size,
+                                                &last_block);
+               if (errcode == EXT2_ET_UNIMPLEMENTED) {
+                       com_err(program_name, 0,
+                               _("Couldn't determine device size; you "
+                                 "must specify\nthe size manually\n"));
+                       exit(1);
+               }
+               if (errcode) {
+                       com_err(program_name, errcode,
+                               _("while trying to determine device size"));
+                       exit(1);
+               }
+       } else {
+               last_block = strtoul (argv[optind], &tmp, 0);
+               if (*tmp) {
+                       com_err (program_name, 0, _("bad blocks count - %s"),
+                                argv[optind]);
+                       exit (1);
+               }
+               optind++;
        }
-       if (++optind <= argc-1) {
+       if (optind <= argc-1) {
                from_count = strtoul (argv[optind], &tmp, 0);
+               if (*tmp) {
+                       com_err (program_name, 0, _("bad starting block - %s"),
+                                argv[optind]);
+                       exit (1);
+               }
        } else from_count = 0;
-       if (from_count >= blocks_count) {
+       if (from_count >= last_block) {
            com_err (program_name, 0, _("bad blocks range: %lu-%lu"),
-                    from_count, blocks_count);
+                    from_count, last_block);
            exit (1);
        }
+       if (w_flag)
+               check_mount(device_name);
+       
        dev = open (device_name, w_flag ? O_RDWR : O_RDONLY);
        if (dev == -1)
        {
@@ -766,7 +815,7 @@ int main (int argc, char ** argv)
 
        if (in) {
                for(;;) {
-                       switch(fscanf (in, "%lu\n", &next_bad)) {
+                       switch(fscanf (in, "%u\n", &next_bad)) {
                                case 0:
                                        com_err (program_name, 0, "input file - bad format");
                                        exit (1);
@@ -790,7 +839,7 @@ int main (int argc, char ** argv)
        do {
                unsigned int bb_count;
 
-               bb_count = test_func(dev, blocks_count, block_size,
+               bb_count = test_func(dev, last_block, block_size,
                                     from_count, blocks_at_once);
                if (bb_count)
                        passes_clean = 0;