+ fputs(_(done_string), stderr);
+ flush_bufs();
+ }
+ uncapture_terminate();
+ free(buffer);
+ return bb_count;
+}
+
+struct saved_blk_record {
+ blk_t block;
+ int num;
+};
+
+static unsigned int test_nd (int dev, blk_t last_block,
+ int block_size, blk_t first_block,
+ unsigned int blocks_at_once)
+{
+ unsigned char *blkbuf, *save_ptr, *test_ptr, *read_ptr;
+ unsigned char *test_base, *save_base, *read_base;
+ int try, i;
+ const unsigned int patterns[] = { ~0 };
+ const unsigned int *pattern;
+ int nr_pattern, pat_idx;
+ int got, used2, written;
+ blk_t 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;
+ unsigned long buf_used;
+ static unsigned int bb_count;
+ unsigned int granularity = blocks_at_once;
+ blk_t recover_block = ~0U;
+
+ bb_count = 0;
+ errcode = ext2fs_badblocks_list_iterate_begin(bb_list,&bb_iter);
+ if (errcode) {
+ com_err(program_name, errcode, "%s",
+ _("while beginning bad block list iteration"));
+ exit (1);
+ }
+ do {
+ ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
+ } while (next_bad && next_bad < first_block);
+
+ blkbuf = allocate_buffer(3 * blocks_at_once * block_size);
+ test_record = malloc(blocks_at_once * sizeof(struct saved_blk_record));
+ if (!blkbuf || !test_record) {
+ com_err(program_name, ENOMEM, "%s",
+ _("while allocating buffers"));
+ exit (1);
+ }
+
+ save_base = blkbuf;
+ test_base = blkbuf + (blocks_at_once * block_size);
+ read_base = blkbuf + (2 * blocks_at_once * block_size);
+
+ num_saved = 0;
+
+ flush_bufs();
+ if (v_flag) {
+ fputs(_("Checking for bad blocks in non-destructive read-write mode\n"), stderr);
+ fprintf (stderr, _("From block %lu to %lu\n"),
+ (unsigned long) first_block,
+ (unsigned long) last_block - 1);
+ }
+ if (s_flag || v_flag > 1) {
+ 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);
+ fputs(_("\nInterrupt caught, cleaning up\n"), stderr);
+
+ save_ptr = save_base;
+ 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);
+ }
+
+ /* set up abend handler */
+ capture_terminate(terminate_env);
+
+ if (t_flag) {
+ pattern = t_patts;
+ nr_pattern = t_flag;
+ } else {
+ pattern = patterns;
+ nr_pattern = sizeof(patterns) / sizeof(patterns[0]);
+ }
+ for (pat_idx = 0; pat_idx < nr_pattern; pat_idx++) {
+ pattern_fill(test_base, pattern[pat_idx],
+ blocks_at_once * block_size);
+
+ buf_used = 0;
+ bb_count = 0;
+ save_ptr = save_base;
+ test_ptr = test_base;
+ currently_testing = first_block;
+ num_blocks = last_block - 1;
+ if (s_flag && v_flag <= 1)
+ alarm_intr(SIGALRM);
+
+ while (currently_testing < last_block) {
+ if (bb_count >= max_bb) {
+ if (s_flag || v_flag) {
+ fputs(_("Too many bad blocks, aborting test\n"), stderr);
+ }
+ break;
+ }
+ got = try = granularity - buf_used;
+ if (next_bad) {
+ if (currently_testing == next_bad) {
+ /* fprintf (out, "%lu\n", nextbad); */
+ ext2fs_badblocks_list_iterate (bb_iter, &next_bad);
+ currently_testing++;
+ goto check_for_more;
+ }
+ else if (currently_testing + try > next_bad)
+ try = next_bad - 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) {
+ if (recover_block == ~0U)
+ recover_block = currently_testing +
+ blocks_at_once;
+ if (granularity != 1) {
+ granularity = 1;
+ continue;
+ }
+ /* First block must have been bad. */
+ bb_count += bb_output(currently_testing++, READ_ERROR);
+ goto check_for_more;
+ }
+
+ /*
+ * 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"),
+ (unsigned long) currently_testing +
+ written);
+
+ buf_used += got;
+ save_ptr += got * block_size;
+ test_ptr += got * block_size;
+ currently_testing += got;
+ if (got != try) {
+ try = 1;
+ if (recover_block == ~0U)
+ recover_block = currently_testing -
+ got + blocks_at_once;
+ continue;
+ }
+
+ 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 != granularity) &&
+ (currently_testing < last_block))
+ continue;
+
+ if (currently_testing >= recover_block) {
+ granularity = blocks_at_once;
+ recover_block = ~0;
+ }
+
+ flush_bufs();
+ save_currently_testing = currently_testing;
+
+ /*
+ * for each contiguous block that we read into the
+ * buffer (and wrote test data into afterwards), read
+ * it back (looping if necessary, to get past newly
+ * discovered unreadable blocks, of which there should
+ * be none, but with a hard drive which is unreliable,
+ * it has happened), and compare with the test data
+ * that was written; output to the bad block list if
+ * it doesn't match.
+ */
+ used2 = 0;
+ save_ptr = save_base;
+ test_ptr = test_base;
+ read_ptr = read_base;
+ try = 0;
+
+ 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);
+
+ /* test the comparison between all the
+ blocks successfully read */
+ for (i = 0; i < got; ++i)
+ if (memcmp (test_ptr+i*block_size,
+ read_ptr+i*block_size, block_size))
+ bb_count += bb_output(currently_testing + i, CORRUPTION_ERROR);
+ if (got < try) {
+ bb_count += bb_output(currently_testing + got, READ_ERROR);
+ got++;
+ }
+
+ /* write back original data */
+ do_write (dev, save_ptr, got,
+ block_size, currently_testing);
+ save_ptr += got * block_size;
+
+ currently_testing += got;
+ test_ptr += got * block_size;
+ read_ptr += got * block_size;
+ try -= got;
+ }
+
+ /* empty the buffer so it can be reused */
+ num_saved = 0;
+ buf_used = 0;
+ save_ptr = save_base;
+ test_ptr = test_base;
+ currently_testing = save_currently_testing;
+ }
+ num_blocks = 0;
+ alarm(0);
+ if (s_flag || v_flag > 1)
+ fputs(_(done_string), stderr);
+
+ flush_bufs();