From nobody@FreeBSD.org  Tue Jan  2 21:48:40 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 1036716A40F
	for <freebsd-gnats-submit@FreeBSD.org>; Tue,  2 Jan 2007 21:48:40 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [69.147.83.33])
	by mx1.freebsd.org (Postfix) with ESMTP id E9DFB13C43E
	for <freebsd-gnats-submit@FreeBSD.org>; Tue,  2 Jan 2007 21:48:39 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id l02LmdvM092891
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 2 Jan 2007 21:48:39 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id l02Lmdad092890;
	Tue, 2 Jan 2007 21:48:39 GMT
	(envelope-from nobody)
Message-Id: <200701022148.l02Lmdad092890@www.freebsd.org>
Date: Tue, 2 Jan 2007 21:48:39 GMT
From: James Risner<risner@stdio.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: dd fails to copy from disk to disk when bad sectors are involved
X-Send-Pr-Version: www-3.0

>Number:         107443
>Category:       kern
>Synopsis:       dd(1) fails to copy from disk to disk when bad sectors are involved
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Jan 02 21:50:33 GMT 2007
>Closed-Date:    Sat May 17 20:13:09 UTC 2008
>Last-Modified:  Sat May 17 20:13:09 UTC 2008
>Originator:     James Risner
>Release:        5.5 and 6.2
>Organization:
OpenWorld, Inc
>Environment:
FreeBSD akira.stdio.com 6.2-PRERELEASE FreeBSD 6.2-PRERELEASE #2: Mon Sep 11 10:55:57 EDT 2006     risner@akira.stdio.com:/usr/obj/usr/src/sys/AKIRA  i386

>Description:
I have had this problem multiple times over the years.

I am unwise because I run drives with no redundancy until they have
bad sectors, then copy the data from one drive to another (usually
with dd.)

I first ran into the "you can not correctly copy disk to disk" problem
with this ticket:

bin/22347: dd copies incorrect data after 2^32 bytes and I/O error

In that case, I solved the problem with a simple patch to dd source.

When that ticket was closed, I assumed the problem was fixed.  I recently had
another disk fail and copied the disk with dd:

Fdisk and partitioned the destination disk correctly.
dd if=/dev/ad0s1a bs=512 of=/dev/ad1s1a conv=noerror,sync
That partition was 8 gigabytes.

The approximately 2000 bad sectors cause the end result to be shifted
down 4096 bytes.  By this I mean if I examined (with hexdump) a region
of the partiton (/dev/ad1s1a) around 7 gb into the partition, it was not
aligned to the source (/dev/ad0s1a).  This alignment problem means that
fsck can not find the superblocks and all superblocks don't match (due
to the alignment shift.)

I solved this problem by writing my own tiny dd program:

#include <stdio.h>
#include <fcntl.h>

char *src = "/dev/ad0s1a";
int fsrc;
char *dst = "/dev/ad1s1a";
int fdst;
char buf[256];

main()
{
int flen = 16777216;  // size of partition
size_t i, ret;

fsrc = open(src, O_RDONLY);
if (fsrc < 0)
        {
        perror("open src");
        exit(1);
        }
fcntl(fsrc, F_SETFL, O_NONBLOCK);
fdst = open(dst, O_WRONLY);
if (fdst < 0)
        {
        perror("open dst");
        exit(1);
        }
fcntl(fdst, F_SETFL, O_NONBLOCK);

printf("fsrc = %d, fdst = %d\n", fsrc, fdst);

for (i=0; i<flen; i++)
        {
        printf("%d\n", i);
        bzero(buf, 512);
        ret = pread(fsrc, buf, (size_t)512, (off_t)512*i);
        if (ret == 0)
                {
                printf("%d blocks written\n", i);
                exit(1);
                }
        if (ret<0)
                {
                printf("%d");
                perror("read");
                }
        ret = write(fdst, buf, 512);
        if (ret == 0)
                {
                printf("eof on write (%d)\n", i);
                exit(1);
                }
        if (ret<0)
                {
                printf("write error (%d)\n", i);
                perror("write");
                }
        }
}

>How-To-Repeat:

Find a failed drive with many bad sectors larger than 2 gb.

Copy from the failed drive to a new drive using dd on FreeBSD.

>Fix:

I have no idea.  My current fix was to use my tiny rdd program that I
hand write on the spot when I identified the reason the new disk could
not fsck was that the data was not aligned properly in the destination.

It seems when there are disk errors in the src, dd gets confused about
the dst pointer and does not advance properly even with "sync" conversion?
>Release-Note:
>Audit-Trail:

From: "Poul-Henning Kamp" <phk@phk.freebsd.dk>
To: James Risner <risner@stdio.com>
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: misc/107443: dd fails to copy from disk to disk when bad sectors are involved 
Date: Tue, 02 Jan 2007 21:51:37 +0000

 In message <200701022148.l02Lmdad092890@www.freebsd.org>, James Risner writes:
 
 >I have had this problem multiple times over the years.
 
 Look in src/tools/tools/recoverdisk
 
 
 -- 
 Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
 phk@FreeBSD.ORG         | TCP/IP since RFC 956
 FreeBSD committer       | BSD since 4.3-tahoe    
 Never attribute to malice what can adequately be explained by incompetence.
State-Changed-From-To: open->feedback 
State-Changed-By: linimon 
State-Changed-When: Thu Jan 4 16:53:20 UTC 2007 
State-Changed-Why:  
From misfiled reply caught in the spam-trap: 

 From: Remko Lodder <remko@elvandar.org> 
 Date: Wed, 3 Jan 2007 07:25:31 +0100 

 Hello, would it be an idea to post this problem on the -hackers 
 mailinglist first? It will get more exposure then and is much 
 likely to be resolved quicker that way.... 

 You can find more information about the lists on 
 http://lists.freebsd.org/mailman/listinfo (/freebsd-hackers in this case). 

 Thanks, 
 remko 

http://www.freebsd.org/cgi/query-pr.cgi?pr=107443 
State-Changed-From-To: feedback->closed 
State-Changed-By: vwe 
State-Changed-When: Sat May 17 20:12:55 UTC 2008 
State-Changed-Why:  

We're sorry to not see any feedback received for quite some time. 
If you think this is still an issue that should be worked on, 
please provide the requested information and we'll be happy to 
re-open this ticket. 
Thank you for bringing this problem to attention! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=107443 
>Unformatted:
