recoverdisk.c revision 189691
1158337Smaxim/*-
2135911Sphk * ----------------------------------------------------------------------------
3135911Sphk * "THE BEER-WARE LICENSE" (Revision 42):
4135911Sphk * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5135911Sphk * can do whatever you want with this stuff. If we meet some day, and you think
6135911Sphk * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7135911Sphk * ----------------------------------------------------------------------------
8135911Sphk *
9135911Sphk * $FreeBSD: head/sbin/recoverdisk/recoverdisk.c 189691 2009-03-11 10:37:02Z phk $
10135911Sphk */
11158337Smaxim#include <sys/param.h>
12158337Smaxim#include <sys/queue.h>
13158337Smaxim#include <sys/disk.h>
14158337Smaxim#include <sys/stat.h>
15158337Smaxim
16136257Sphk#include <err.h>
17135911Sphk#include <errno.h>
18135911Sphk#include <fcntl.h>
19158337Smaxim#include <signal.h>
20158337Smaxim#include <stdint.h>
21158337Smaxim#include <stdio.h>
22158337Smaxim#include <stdlib.h>
23158337Smaxim#include <string.h>
24136257Sphk#include <time.h>
25135911Sphk#include <unistd.h>
26135911Sphk
27158337Smaximvolatile sig_atomic_t aborting = 0;
28158337Smaximstatic size_t bigsize = 1024 * 1024;
29189691Sphkstatic size_t medsize;
30158337Smaximstatic size_t minsize = 512;
31135911Sphk
32135911Sphkstruct lump {
33135911Sphk	off_t			start;
34135911Sphk	off_t			len;
35135911Sphk	int			state;
36135911Sphk	TAILQ_ENTRY(lump)	list;
37135911Sphk};
38135911Sphk
39135911Sphkstatic TAILQ_HEAD(, lump) lumps = TAILQ_HEAD_INITIALIZER(lumps);
40135911Sphk
41135911Sphkstatic void
42135911Sphknew_lump(off_t start, off_t len, int state)
43135911Sphk{
44135911Sphk	struct lump *lp;
45135911Sphk
46135911Sphk	lp = malloc(sizeof *lp);
47135911Sphk	if (lp == NULL)
48135911Sphk		err(1, "Malloc failed");
49135911Sphk	lp->start = start;
50135911Sphk	lp->len = len;
51135911Sphk	lp->state = state;
52135911Sphk	TAILQ_INSERT_TAIL(&lumps, lp, list);
53135911Sphk}
54135911Sphk
55158337Smaximstatic struct lump *lp;
56158337Smaximstatic char *wworklist = NULL;
57158337Smaximstatic char *rworklist = NULL;
58158337Smaxim
59168972Sphk
60168972Sphk#define PRINT_HEADER \
61168972Sphk	printf("%13s %7s %13s %5s %13s %13s %9s\n", \
62168972Sphk		"start", "size", "block-len", "state", "done", "remaining", "% done")
63168972Sphk
64168972Sphk#define PRINT_STATUS(start, i, len, state, d, t) \
65168972Sphk	printf("\r%13jd %7zu %13jd %5d %13jd %13jd %9.5f", \
66168972Sphk		(intmax_t)start, \
67168972Sphk		i,  \
68168972Sphk		(intmax_t)len, \
69168972Sphk		state, \
70168972Sphk		(intmax_t)d, \
71168972Sphk		(intmax_t)(t - d), \
72168972Sphk		100*(double)d/(double)t)
73168972Sphk
74158337Smaxim/* Save the worklist if -w was given */
75158337Smaximstatic void
76158337Smaximsave_worklist(void)
77158337Smaxim{
78158337Smaxim	FILE *file;
79189691Sphk	struct lump *llp;
80158337Smaxim
81158337Smaxim	if (wworklist != NULL) {
82158337Smaxim		(void)fprintf(stderr, "\nSaving worklist ...");
83158337Smaxim		fflush(stderr);
84158337Smaxim
85158337Smaxim		file = fopen(wworklist, "w");
86158337Smaxim		if (file == NULL)
87158337Smaxim			err(1, "Error opening file %s", wworklist);
88158337Smaxim
89189691Sphk		TAILQ_FOREACH(llp, &lumps, list)
90158337Smaxim			fprintf(file, "%jd %jd %d\n",
91189691Sphk			    (intmax_t)llp->start, (intmax_t)llp->len,
92189691Sphk			    llp->state);
93189691Sphk		fclose(file);
94158337Smaxim		(void)fprintf(stderr, " done.\n");
95158337Smaxim	}
96158337Smaxim}
97158337Smaxim
98158337Smaxim/* Read the worklist if -r was given */
99158337Smaximstatic off_t
100158337Smaximread_worklist(off_t t)
101158337Smaxim{
102158337Smaxim	off_t s, l, d;
103158337Smaxim	int state, lines;
104158337Smaxim	FILE *file;
105158337Smaxim
106158337Smaxim	(void)fprintf(stderr, "Reading worklist ...");
107158337Smaxim	fflush(stderr);
108158337Smaxim	file = fopen(rworklist, "r");
109158337Smaxim	if (file == NULL)
110158337Smaxim		err(1, "Error opening file %s", rworklist);
111158337Smaxim
112158337Smaxim	lines = 0;
113158337Smaxim	d = t;
114158337Smaxim	for (;;) {
115158337Smaxim		++lines;
116158337Smaxim		if (3 != fscanf(file, "%jd %jd %d\n", &s, &l, &state)) {
117158337Smaxim			if (!feof(file))
118158337Smaxim				err(1, "Error parsing file %s at line %d",
119158337Smaxim				    rworklist, lines);
120158337Smaxim			else
121158337Smaxim				break;
122158337Smaxim		}
123158337Smaxim		new_lump(s, l, state);
124158337Smaxim		d -= l;
125158337Smaxim	}
126158337Smaxim	(void)fprintf(stderr, " done.\n");
127158337Smaxim	/*
128158337Smaxim	 * Return the number of bytes already read
129158337Smaxim	 * (at least not in worklist).
130158337Smaxim	 */
131158337Smaxim	return (d);
132158337Smaxim}
133158337Smaxim
134158337Smaximstatic void
135158337Smaximusage(void)
136158337Smaxim{
137158337Smaxim	(void)fprintf(stderr,
138158337Smaxim    "usage: recoverdisk [-r worklist] [-w worklist] source-drive [destination]\n");
139158337Smaxim	exit(1);
140158337Smaxim}
141158337Smaxim
142158337Smaximstatic void
143158337Smaximsighandler(__unused int sig)
144158337Smaxim{
145158337Smaxim
146158337Smaxim	aborting = 1;
147158337Smaxim}
148158337Smaxim
149135911Sphkint
150158337Smaximmain(int argc, char * const argv[])
151135911Sphk{
152158337Smaxim	int ch;
153135911Sphk	int fdr, fdw;
154168972Sphk	off_t t, d, start, len;
155135911Sphk	size_t i, j;
156168972Sphk	int error, flags, state;
157135911Sphk	u_char *buf;
158158337Smaxim	u_int sectorsize;
159136257Sphk	time_t t1, t2;
160149605Ssobomax	struct stat sb;
161189691Sphk	u_int n, snapshot = 60;
162135911Sphk
163189691Sphk	while ((ch = getopt(argc, argv, "b:r:w:s:")) != -1) {
164158337Smaxim		switch (ch) {
165189691Sphk		case 'b':
166189691Sphk			bigsize = strtoul(optarg, NULL, 0);
167189691Sphk			break;
168158337Smaxim		case 'r':
169158337Smaxim			rworklist = strdup(optarg);
170158337Smaxim			if (rworklist == NULL)
171158337Smaxim				err(1, "Cannot allocate enough memory");
172158337Smaxim			break;
173189691Sphk		case 's':
174189691Sphk			snapshot = strtoul(optarg, NULL, 0);
175189691Sphk			break;
176158337Smaxim		case 'w':
177158337Smaxim			wworklist = strdup(optarg);
178158337Smaxim			if (wworklist == NULL)
179158337Smaxim				err(1, "Cannot allocate enough memory");
180158337Smaxim			break;
181158337Smaxim		default:
182158337Smaxim			usage();
183158337Smaxim			/* NOTREACHED */
184158337Smaxim		}
185158337Smaxim	}
186158337Smaxim	argc -= optind;
187158337Smaxim	argv += optind;
188135911Sphk
189158337Smaxim	if (argc < 1 || argc > 2)
190158337Smaxim		usage();
191135911Sphk
192158337Smaxim	fdr = open(argv[0], O_RDONLY);
193135911Sphk	if (fdr < 0)
194158337Smaxim		err(1, "Cannot open read descriptor %s", argv[0]);
195149605Ssobomax
196149605Ssobomax	error = fstat(fdr, &sb);
197149605Ssobomax	if (error < 0)
198149605Ssobomax		err(1, "fstat failed");
199149605Ssobomax	flags = O_WRONLY;
200149605Ssobomax	if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
201149605Ssobomax		error = ioctl(fdr, DIOCGSECTORSIZE, &sectorsize);
202149605Ssobomax		if (error < 0)
203149605Ssobomax			err(1, "DIOCGSECTORSIZE failed");
204158337Smaxim
205149605Ssobomax		minsize = sectorsize;
206189691Sphk		bigsize = (bigsize / sectorsize) * sectorsize;
207149605Ssobomax
208149605Ssobomax		error = ioctl(fdr, DIOCGMEDIASIZE, &t);
209149605Ssobomax		if (error < 0)
210149605Ssobomax			err(1, "DIOCGMEDIASIZE failed");
211149605Ssobomax	} else {
212149605Ssobomax		t = sb.st_size;
213149605Ssobomax		flags |= O_CREAT | O_TRUNC;
214149605Ssobomax	}
215149605Ssobomax
216189691Sphk	if (bigsize < minsize)
217189691Sphk		bigsize = minsize;
218189691Sphk
219189691Sphk	for (ch = 0; (bigsize >> ch) > minsize; ch++)
220189691Sphk		continue;
221189691Sphk	medsize = bigsize >> (ch / 2);
222189691Sphk	medsize = (medsize / minsize) * minsize;
223189691Sphk
224189691Sphk	fprintf(stderr, "Bigsize = %u, medsize = %u, minsize = %u\n",
225189691Sphk	    bigsize, medsize, minsize);
226189691Sphk
227158337Smaxim	buf = malloc(bigsize);
228158337Smaxim	if (buf == NULL)
229158337Smaxim		err(1, "Cannot allocate %jd bytes buffer", (intmax_t)bigsize);
230158337Smaxim
231158337Smaxim	if (argc > 1) {
232158337Smaxim		fdw = open(argv[1], flags, DEFFILEMODE);
233135911Sphk		if (fdw < 0)
234158337Smaxim			err(1, "Cannot open write descriptor %s", argv[1]);
235158337Smaxim	} else
236158337Smaxim		fdw = -1;
237158337Smaxim
238158337Smaxim	if (rworklist != NULL) {
239158337Smaxim		d = read_worklist(t);
240135911Sphk	} else {
241158337Smaxim		new_lump(0, t, 0);
242158337Smaxim		d = 0;
243135911Sphk	}
244158337Smaxim	if (wworklist != NULL)
245158337Smaxim		signal(SIGINT, sighandler);
246135911Sphk
247136257Sphk	t1 = 0;
248168972Sphk	start = len = i = state = 0;
249168972Sphk	PRINT_HEADER;
250189691Sphk	n = 0;
251135911Sphk	for (;;) {
252135911Sphk		lp = TAILQ_FIRST(&lumps);
253135911Sphk		if (lp == NULL)
254135911Sphk			break;
255158337Smaxim		while (lp->len > 0 && !aborting) {
256168972Sphk			/* These are only copied for printing stats */
257168972Sphk			start = lp->start;
258168972Sphk			len = lp->len;
259168972Sphk			state = lp->state;
260168972Sphk
261159076Smatteo			i = MIN(lp->len, (off_t)bigsize);
262135911Sphk			if (lp->state == 1)
263159076Smatteo				i = MIN(lp->len, (off_t)medsize);
264135911Sphk			if (lp->state > 1)
265159076Smatteo				i = MIN(lp->len, (off_t)minsize);
266136257Sphk			time(&t2);
267159076Smatteo			if (t1 != t2 || lp->len < (off_t)bigsize) {
268168972Sphk				PRINT_STATUS(start, i, len, state, d, t);
269136257Sphk				t1 = t2;
270189691Sphk				if (++n == snapshot) {
271189691Sphk					save_worklist();
272189691Sphk					n = 0;
273189691Sphk				}
274136257Sphk			}
275135911Sphk			if (i == 0) {
276135911Sphk				errx(1, "BOGUS i %10jd", (intmax_t)i);
277135911Sphk			}
278135911Sphk			fflush(stdout);
279135911Sphk			j = pread(fdr, buf, i, lp->start);
280135911Sphk			if (j == i) {
281135911Sphk				d += i;
282135911Sphk				if (fdw >= 0)
283135911Sphk					j = pwrite(fdw, buf, i, lp->start);
284135911Sphk				else
285135911Sphk					j = i;
286135911Sphk				if (j != i)
287136815Sdes					printf("\nWrite error at %jd/%zu\n",
288136815Sdes					    lp->start, i);
289135911Sphk				lp->start += i;
290135911Sphk				lp->len -= i;
291135911Sphk				continue;
292135911Sphk			}
293187360Sphk			printf("\n%jd %zu failed (%s)\n",
294187360Sphk			    lp->start, i, strerror(errno));
295187360Sphk			if (errno == ENXIO)
296187360Sphk				aborting = 1;
297135911Sphk			new_lump(lp->start, i, lp->state + 1);
298135911Sphk			lp->start += i;
299135911Sphk			lp->len -= i;
300135911Sphk		}
301158337Smaxim		if (aborting) {
302158337Smaxim			save_worklist();
303158337Smaxim			return (0);
304158337Smaxim		}
305158337Smaxim		TAILQ_REMOVE(&lumps, lp, list);
306135911Sphk		free(lp);
307135911Sphk	}
308168972Sphk	PRINT_STATUS(start, i, len, state, d, t);
309135911Sphk	printf("\nCompleted\n");
310158337Smaxim	return (0);
311135911Sphk}
312