2006-05-06 19:52:25 +00:00
|
|
|
/*-
|
2004-09-28 22:00:01 +00:00
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
2006-05-06 19:52:25 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/disk.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2004-10-08 10:50:40 +00:00
|
|
|
#include <err.h>
|
2004-09-28 22:00:01 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2006-05-06 19:52:25 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2004-10-08 10:50:40 +00:00
|
|
|
#include <time.h>
|
2004-09-28 22:00:01 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2006-05-06 19:52:25 +00:00
|
|
|
volatile sig_atomic_t aborting = 0;
|
|
|
|
static size_t bigsize = 1024 * 1024;
|
2009-03-11 10:37:02 +00:00
|
|
|
static size_t medsize;
|
2006-05-06 19:52:25 +00:00
|
|
|
static size_t minsize = 512;
|
2004-09-28 22:00:01 +00:00
|
|
|
|
|
|
|
struct lump {
|
|
|
|
off_t start;
|
|
|
|
off_t len;
|
|
|
|
int state;
|
|
|
|
TAILQ_ENTRY(lump) list;
|
|
|
|
};
|
|
|
|
|
|
|
|
static TAILQ_HEAD(, lump) lumps = TAILQ_HEAD_INITIALIZER(lumps);
|
|
|
|
|
|
|
|
static void
|
|
|
|
new_lump(off_t start, off_t len, int state)
|
|
|
|
{
|
|
|
|
struct lump *lp;
|
|
|
|
|
|
|
|
lp = malloc(sizeof *lp);
|
|
|
|
if (lp == NULL)
|
|
|
|
err(1, "Malloc failed");
|
|
|
|
lp->start = start;
|
|
|
|
lp->len = len;
|
|
|
|
lp->state = state;
|
|
|
|
TAILQ_INSERT_TAIL(&lumps, lp, list);
|
|
|
|
}
|
|
|
|
|
2006-05-06 19:52:25 +00:00
|
|
|
static struct lump *lp;
|
|
|
|
static char *wworklist = NULL;
|
|
|
|
static char *rworklist = NULL;
|
|
|
|
|
2007-04-23 12:17:27 +00:00
|
|
|
|
|
|
|
#define PRINT_HEADER \
|
|
|
|
printf("%13s %7s %13s %5s %13s %13s %9s\n", \
|
|
|
|
"start", "size", "block-len", "state", "done", "remaining", "% done")
|
|
|
|
|
|
|
|
#define PRINT_STATUS(start, i, len, state, d, t) \
|
|
|
|
printf("\r%13jd %7zu %13jd %5d %13jd %13jd %9.5f", \
|
|
|
|
(intmax_t)start, \
|
|
|
|
i, \
|
|
|
|
(intmax_t)len, \
|
|
|
|
state, \
|
|
|
|
(intmax_t)d, \
|
|
|
|
(intmax_t)(t - d), \
|
|
|
|
100*(double)d/(double)t)
|
|
|
|
|
2006-05-06 19:52:25 +00:00
|
|
|
/* Save the worklist if -w was given */
|
|
|
|
static void
|
|
|
|
save_worklist(void)
|
|
|
|
{
|
|
|
|
FILE *file;
|
2009-03-11 10:37:02 +00:00
|
|
|
struct lump *llp;
|
2006-05-06 19:52:25 +00:00
|
|
|
|
|
|
|
if (wworklist != NULL) {
|
|
|
|
(void)fprintf(stderr, "\nSaving worklist ...");
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
file = fopen(wworklist, "w");
|
|
|
|
if (file == NULL)
|
|
|
|
err(1, "Error opening file %s", wworklist);
|
|
|
|
|
2009-03-11 10:37:02 +00:00
|
|
|
TAILQ_FOREACH(llp, &lumps, list)
|
2006-05-06 19:52:25 +00:00
|
|
|
fprintf(file, "%jd %jd %d\n",
|
2009-03-11 10:37:02 +00:00
|
|
|
(intmax_t)llp->start, (intmax_t)llp->len,
|
|
|
|
llp->state);
|
|
|
|
fclose(file);
|
2006-05-06 19:52:25 +00:00
|
|
|
(void)fprintf(stderr, " done.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the worklist if -r was given */
|
|
|
|
static off_t
|
|
|
|
read_worklist(off_t t)
|
|
|
|
{
|
|
|
|
off_t s, l, d;
|
|
|
|
int state, lines;
|
|
|
|
FILE *file;
|
|
|
|
|
|
|
|
(void)fprintf(stderr, "Reading worklist ...");
|
|
|
|
fflush(stderr);
|
|
|
|
file = fopen(rworklist, "r");
|
|
|
|
if (file == NULL)
|
|
|
|
err(1, "Error opening file %s", rworklist);
|
|
|
|
|
|
|
|
lines = 0;
|
|
|
|
d = t;
|
|
|
|
for (;;) {
|
|
|
|
++lines;
|
|
|
|
if (3 != fscanf(file, "%jd %jd %d\n", &s, &l, &state)) {
|
|
|
|
if (!feof(file))
|
|
|
|
err(1, "Error parsing file %s at line %d",
|
|
|
|
rworklist, lines);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
new_lump(s, l, state);
|
|
|
|
d -= l;
|
|
|
|
}
|
|
|
|
(void)fprintf(stderr, " done.\n");
|
|
|
|
/*
|
|
|
|
* Return the number of bytes already read
|
|
|
|
* (at least not in worklist).
|
|
|
|
*/
|
|
|
|
return (d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"usage: recoverdisk [-r worklist] [-w worklist] source-drive [destination]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sighandler(__unused int sig)
|
|
|
|
{
|
|
|
|
|
|
|
|
aborting = 1;
|
|
|
|
}
|
|
|
|
|
2004-09-28 22:00:01 +00:00
|
|
|
int
|
2006-05-06 19:52:25 +00:00
|
|
|
main(int argc, char * const argv[])
|
2004-09-28 22:00:01 +00:00
|
|
|
{
|
2006-05-06 19:52:25 +00:00
|
|
|
int ch;
|
2004-09-28 22:00:01 +00:00
|
|
|
int fdr, fdw;
|
2007-04-23 12:17:27 +00:00
|
|
|
off_t t, d, start, len;
|
2004-09-28 22:00:01 +00:00
|
|
|
size_t i, j;
|
2007-04-23 12:17:27 +00:00
|
|
|
int error, flags, state;
|
2004-09-28 22:00:01 +00:00
|
|
|
u_char *buf;
|
2006-05-06 19:52:25 +00:00
|
|
|
u_int sectorsize;
|
2004-10-08 10:50:40 +00:00
|
|
|
time_t t1, t2;
|
2005-08-29 23:08:01 +00:00
|
|
|
struct stat sb;
|
2009-03-11 10:37:02 +00:00
|
|
|
u_int n, snapshot = 60;
|
2004-09-28 22:00:01 +00:00
|
|
|
|
2009-03-11 10:37:02 +00:00
|
|
|
while ((ch = getopt(argc, argv, "b:r:w:s:")) != -1) {
|
2006-05-06 19:52:25 +00:00
|
|
|
switch (ch) {
|
2009-03-11 10:37:02 +00:00
|
|
|
case 'b':
|
|
|
|
bigsize = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
2006-05-06 19:52:25 +00:00
|
|
|
case 'r':
|
|
|
|
rworklist = strdup(optarg);
|
|
|
|
if (rworklist == NULL)
|
|
|
|
err(1, "Cannot allocate enough memory");
|
|
|
|
break;
|
2009-03-11 10:37:02 +00:00
|
|
|
case 's':
|
|
|
|
snapshot = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
2006-05-06 19:52:25 +00:00
|
|
|
case 'w':
|
|
|
|
wworklist = strdup(optarg);
|
|
|
|
if (wworklist == NULL)
|
|
|
|
err(1, "Cannot allocate enough memory");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2004-09-28 22:00:01 +00:00
|
|
|
|
2006-05-06 19:52:25 +00:00
|
|
|
if (argc < 1 || argc > 2)
|
|
|
|
usage();
|
2004-09-28 22:00:01 +00:00
|
|
|
|
2006-05-06 19:52:25 +00:00
|
|
|
fdr = open(argv[0], O_RDONLY);
|
2004-09-28 22:00:01 +00:00
|
|
|
if (fdr < 0)
|
2006-05-06 19:52:25 +00:00
|
|
|
err(1, "Cannot open read descriptor %s", argv[0]);
|
2005-08-29 23:08:01 +00:00
|
|
|
|
|
|
|
error = fstat(fdr, &sb);
|
|
|
|
if (error < 0)
|
|
|
|
err(1, "fstat failed");
|
|
|
|
flags = O_WRONLY;
|
|
|
|
if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
|
|
|
|
error = ioctl(fdr, DIOCGSECTORSIZE, §orsize);
|
|
|
|
if (error < 0)
|
|
|
|
err(1, "DIOCGSECTORSIZE failed");
|
2006-05-06 19:52:25 +00:00
|
|
|
|
2005-08-29 23:08:01 +00:00
|
|
|
minsize = sectorsize;
|
2009-03-11 10:37:02 +00:00
|
|
|
bigsize = (bigsize / sectorsize) * sectorsize;
|
2005-08-29 23:08:01 +00:00
|
|
|
|
|
|
|
error = ioctl(fdr, DIOCGMEDIASIZE, &t);
|
|
|
|
if (error < 0)
|
|
|
|
err(1, "DIOCGMEDIASIZE failed");
|
|
|
|
} else {
|
|
|
|
t = sb.st_size;
|
|
|
|
flags |= O_CREAT | O_TRUNC;
|
|
|
|
}
|
|
|
|
|
2009-03-11 10:37:02 +00:00
|
|
|
if (bigsize < minsize)
|
|
|
|
bigsize = minsize;
|
|
|
|
|
|
|
|
for (ch = 0; (bigsize >> ch) > minsize; ch++)
|
|
|
|
continue;
|
|
|
|
medsize = bigsize >> (ch / 2);
|
|
|
|
medsize = (medsize / minsize) * minsize;
|
|
|
|
|
2009-03-11 16:32:00 +00:00
|
|
|
fprintf(stderr, "Bigsize = %zu, medsize = %zu, minsize = %zu\n",
|
2009-03-11 10:37:02 +00:00
|
|
|
bigsize, medsize, minsize);
|
|
|
|
|
2006-05-06 19:52:25 +00:00
|
|
|
buf = malloc(bigsize);
|
|
|
|
if (buf == NULL)
|
2009-03-11 17:40:39 +00:00
|
|
|
err(1, "Cannot allocate %zu bytes buffer", bigsize);
|
2006-05-06 19:52:25 +00:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
fdw = open(argv[1], flags, DEFFILEMODE);
|
2004-09-28 22:00:01 +00:00
|
|
|
if (fdw < 0)
|
2006-05-06 19:52:25 +00:00
|
|
|
err(1, "Cannot open write descriptor %s", argv[1]);
|
|
|
|
} else
|
2004-09-28 22:00:01 +00:00
|
|
|
fdw = -1;
|
|
|
|
|
2006-05-06 19:52:25 +00:00
|
|
|
if (rworklist != NULL) {
|
|
|
|
d = read_worklist(t);
|
|
|
|
} else {
|
|
|
|
new_lump(0, t, 0);
|
|
|
|
d = 0;
|
|
|
|
}
|
|
|
|
if (wworklist != NULL)
|
|
|
|
signal(SIGINT, sighandler);
|
2004-09-28 22:00:01 +00:00
|
|
|
|
2004-10-08 10:50:40 +00:00
|
|
|
t1 = 0;
|
2007-04-23 12:17:27 +00:00
|
|
|
start = len = i = state = 0;
|
|
|
|
PRINT_HEADER;
|
2009-03-11 10:37:02 +00:00
|
|
|
n = 0;
|
2004-09-28 22:00:01 +00:00
|
|
|
for (;;) {
|
|
|
|
lp = TAILQ_FIRST(&lumps);
|
|
|
|
if (lp == NULL)
|
|
|
|
break;
|
2006-05-06 19:52:25 +00:00
|
|
|
while (lp->len > 0 && !aborting) {
|
2007-04-23 12:17:27 +00:00
|
|
|
/* These are only copied for printing stats */
|
|
|
|
start = lp->start;
|
|
|
|
len = lp->len;
|
|
|
|
state = lp->state;
|
|
|
|
|
2006-05-30 19:10:18 +00:00
|
|
|
i = MIN(lp->len, (off_t)bigsize);
|
2004-09-28 22:00:01 +00:00
|
|
|
if (lp->state == 1)
|
2006-05-30 19:10:18 +00:00
|
|
|
i = MIN(lp->len, (off_t)medsize);
|
2004-09-28 22:00:01 +00:00
|
|
|
if (lp->state > 1)
|
2006-05-30 19:10:18 +00:00
|
|
|
i = MIN(lp->len, (off_t)minsize);
|
2004-10-08 10:50:40 +00:00
|
|
|
time(&t2);
|
2006-05-30 19:10:18 +00:00
|
|
|
if (t1 != t2 || lp->len < (off_t)bigsize) {
|
2007-04-23 12:17:27 +00:00
|
|
|
PRINT_STATUS(start, i, len, state, d, t);
|
2004-10-08 10:50:40 +00:00
|
|
|
t1 = t2;
|
2009-03-11 10:37:02 +00:00
|
|
|
if (++n == snapshot) {
|
|
|
|
save_worklist();
|
|
|
|
n = 0;
|
|
|
|
}
|
2004-10-08 10:50:40 +00:00
|
|
|
}
|
2004-09-28 22:00:01 +00:00
|
|
|
if (i == 0) {
|
|
|
|
errx(1, "BOGUS i %10jd", (intmax_t)i);
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
j = pread(fdr, buf, i, lp->start);
|
|
|
|
if (j == i) {
|
|
|
|
d += i;
|
|
|
|
if (fdw >= 0)
|
|
|
|
j = pwrite(fdw, buf, i, lp->start);
|
|
|
|
else
|
|
|
|
j = i;
|
|
|
|
if (j != i)
|
2004-10-23 15:58:50 +00:00
|
|
|
printf("\nWrite error at %jd/%zu\n",
|
|
|
|
lp->start, i);
|
2004-09-28 22:00:01 +00:00
|
|
|
lp->start += i;
|
|
|
|
lp->len -= i;
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-17 11:57:32 +00:00
|
|
|
printf("\n%jd %zu failed (%s)\n",
|
|
|
|
lp->start, i, strerror(errno));
|
|
|
|
if (errno == ENXIO)
|
|
|
|
aborting = 1;
|
2004-09-28 22:00:01 +00:00
|
|
|
new_lump(lp->start, i, lp->state + 1);
|
|
|
|
lp->start += i;
|
|
|
|
lp->len -= i;
|
|
|
|
}
|
2006-05-06 19:52:25 +00:00
|
|
|
if (aborting) {
|
|
|
|
save_worklist();
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&lumps, lp, list);
|
2004-09-28 22:00:01 +00:00
|
|
|
free(lp);
|
|
|
|
}
|
2007-04-23 12:17:27 +00:00
|
|
|
PRINT_STATUS(start, i, len, state, d, t);
|
2009-03-23 11:07:34 +00:00
|
|
|
save_worklist();
|
2004-09-28 22:00:01 +00:00
|
|
|
printf("\nCompleted\n");
|
2006-05-06 19:52:25 +00:00
|
|
|
return (0);
|
2004-09-28 22:00:01 +00:00
|
|
|
}
|