Subject: Re: Scanning for bad sectors on a disk
To: Bri <simian@replic.net>
From: Manuel Bouyer <bouyer@antioche.lip6.fr>
List: port-i386
Date: 07/18/2001 21:05:46
--vtzGhvizbBRQ85DL
Content-Type: text/plain; charset=us-ascii

On Tue, Jul 17, 2001 at 04:26:11PM -0700, Bri wrote:
> > dd if=/dev/rwd0d of=/dev/null
> > will read the whole disk. Maybe you're looking for a better check (like
> > read/write/read) ?
> 
> 
>  Yeah, I was looking for something like a read/write/read check.
> Preferabley one that would keep the contents of the disk intact ;)

The attached program should work (I tested it with a ZIP drive and the
data were still here :)
usage: ./rw_test /dev/rsd0d

It will be slow; if you want you can increase MYBUFSIZE (multiple of
512 and <= 64k), it will be faster but give less accurate results, as it'll
try to read several sectors at once.

--
Manuel Bouyer <bouyer@antioche.eu.org>
--

--vtzGhvizbBRQ85DL
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="rw_test.c"

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

#define MYBUFSIZE 512

int
main(int argc, char **argv)
{
	static char buf[MYBUFSIZE], buf2[MYBUFSIZE];
	int fd;
	off_t p;
	int nchar=0;

	fd = open(argv[1], O_RDWR, 0);
	if (fd < 0) {
		perror("open");
		exit(1);
	}
	printf("sector: ");
	for (p = 0; ; p+= MYBUFSIZE) {
		while (nchar--)
			printf("\b");
		nchar = printf("%lu", (u_long)(p / MYBUFSIZE));
		fflush(stdout);
		if (lseek(fd, p, SEEK_SET) < 0) {
			perror("seek");
			exit(1);
		}
		if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
			perror("read");
			continue;
		}
		if (lseek(fd, p, SEEK_SET) < 0) {
			perror("seek");
			exit(1);
		}
		if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
			perror("write");
			continue;
		}
		if (lseek(fd, p, SEEK_SET) < 0) {
			perror("seek");
			exit(1);
		}
		if (read(fd, buf2, sizeof(buf2)) != sizeof(buf2)) {
			perror("read2");
			continue;
		}
		if (memcmp(buf, buf2, sizeof(buf2)) != 0)
			fprintf(stderr, "second read differ\n");
	}
	exit(0);
}

--vtzGhvizbBRQ85DL--