2 * SPDX-License-Identifier: Beerware
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
13 #include <sys/param.h>
14 #include <sys/queue.h>
29 static volatile sig_atomic_t aborting = 0;
30 static size_t bigsize = 1024 * 1024;
31 static size_t medsize;
32 static size_t minsize = 512;
38 TAILQ_ENTRY(lump) list;
41 static TAILQ_HEAD(, lump) lumps = TAILQ_HEAD_INITIALIZER(lumps);
44 new_lump(off_t start, off_t len, int state)
48 lp = malloc(sizeof *lp);
50 err(1, "Malloc failed");
54 TAILQ_INSERT_TAIL(&lumps, lp, list);
57 static struct lump *lp;
58 static char *wworklist = NULL;
59 static char *rworklist = NULL;
62 #define PRINT_HEADER \
63 printf("%13s %7s %13s %5s %13s %13s %9s\n", \
64 "start", "size", "block-len", "state", "done", "remaining", "% done")
66 #define PRINT_STATUS(start, i, len, state, d, t) \
67 printf("\r%13jd %7zu %13jd %5d %13jd %13jd %9.5f", \
74 100*(double)d/(double)t)
76 /* Save the worklist if -w was given */
83 if (wworklist != NULL) {
84 (void)fprintf(stderr, "\nSaving worklist ...");
87 file = fopen(wworklist, "w");
89 err(1, "Error opening file %s", wworklist);
91 TAILQ_FOREACH(llp, &lumps, list)
92 fprintf(file, "%jd %jd %d\n",
93 (intmax_t)llp->start, (intmax_t)llp->len,
96 (void)fprintf(stderr, " done.\n");
100 /* Read the worklist if -r was given */
102 read_worklist(off_t t)
108 (void)fprintf(stderr, "Reading worklist ...");
110 file = fopen(rworklist, "r");
112 err(1, "Error opening file %s", rworklist);
118 if (3 != fscanf(file, "%jd %jd %d\n", &s, &l, &state)) {
120 err(1, "Error parsing file %s at line %d",
125 new_lump(s, l, state);
129 (void)fprintf(stderr, " done.\n");
131 * Return the number of bytes already read
132 * (at least not in worklist).
140 (void)fprintf(stderr, "usage: recoverdisk [-b bigsize] [-r readlist] "
141 "[-s interval] [-w writelist] source [destination]\n");
146 sighandler(__unused int sig)
153 main(int argc, char * const argv[])
157 off_t t, d, start, len;
165 u_int n, snapshot = 60;
167 while ((ch = getopt(argc, argv, "b:r:w:s:")) != -1) {
170 bigsize = strtoul(optarg, NULL, 0);
173 rworklist = strdup(optarg);
174 if (rworklist == NULL)
175 err(1, "Cannot allocate enough memory");
178 snapshot = strtoul(optarg, NULL, 0);
181 wworklist = strdup(optarg);
182 if (wworklist == NULL)
183 err(1, "Cannot allocate enough memory");
193 if (argc < 1 || argc > 2)
196 fdr = open(argv[0], O_RDONLY);
198 err(1, "Cannot open read descriptor %s", argv[0]);
200 error = fstat(fdr, &sb);
202 err(1, "fstat failed");
203 if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
204 error = ioctl(fdr, DIOCGSECTORSIZE, §orsize);
206 err(1, "DIOCGSECTORSIZE failed");
208 error = ioctl(fdr, DIOCGSTRIPESIZE, &stripesize);
209 if (error == 0 && stripesize > sectorsize)
210 sectorsize = stripesize;
212 minsize = sectorsize;
213 bigsize = rounddown(bigsize, sectorsize);
215 error = ioctl(fdr, DIOCGMEDIASIZE, &t);
217 err(1, "DIOCGMEDIASIZE failed");
222 if (bigsize < minsize)
225 for (ch = 0; (bigsize >> ch) > minsize; ch++)
227 medsize = bigsize >> (ch / 2);
228 medsize = rounddown(medsize, minsize);
230 fprintf(stderr, "Bigsize = %zu, medsize = %zu, minsize = %zu\n",
231 bigsize, medsize, minsize);
233 buf = malloc(bigsize);
235 err(1, "Cannot allocate %zu bytes buffer", bigsize);
238 fdw = open(argv[1], O_WRONLY | O_CREAT, DEFFILEMODE);
240 err(1, "Cannot open write descriptor %s", argv[1]);
241 if (ftruncate(fdw, t) < 0)
242 err(1, "Cannot truncate output %s to %jd bytes",
243 argv[1], (intmax_t)t);
247 if (rworklist != NULL) {
248 d = read_worklist(t);
253 if (wworklist != NULL)
254 signal(SIGINT, sighandler);
257 start = len = i = state = 0;
261 lp = TAILQ_FIRST(&lumps);
264 while (lp->len > 0 && !aborting) {
265 /* These are only copied for printing stats */
270 i = MIN(lp->len, (off_t)bigsize);
272 i = MIN(lp->len, (off_t)medsize);
274 i = MIN(lp->len, (off_t)minsize);
276 if (t1 != t2 || lp->len < (off_t)bigsize) {
277 PRINT_STATUS(start, i, len, state, d, t);
279 if (++n == snapshot) {
285 errx(1, "BOGUS i %10jd", (intmax_t)i);
288 j = pread(fdr, buf, i, lp->start);
292 j = pwrite(fdw, buf, i, lp->start);
296 printf("\nWrite error at %jd/%zu\n",
302 printf("\n%jd %zu failed (%s)\n",
303 lp->start, i, strerror(errno));
304 if (errno == EINVAL) {
305 printf("read() size too big? Try with -b 131072");
310 new_lump(lp->start, i, lp->state + 1);
318 TAILQ_REMOVE(&lumps, lp, list);
321 PRINT_STATUS(start, i, len, state, d, t);
323 printf("\nCompleted\n");