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 * ----------------------------------------------------------------------------
11 #include <sys/param.h>
12 #include <sys/queue.h>
30 /* Safe printf into a fixed-size buffer */
31 #define bprintf(buf, fmt, ...) \
34 ibprintf = snprintf(buf, sizeof buf, fmt, __VA_ARGS__); \
35 assert(ibprintf >= 0 && ibprintf < (int)sizeof buf); \
42 TAILQ_ENTRY(lump) list;
50 TAILQ_ENTRY(period) list;
52 TAILQ_HEAD(period_head, period);
54 static volatile sig_atomic_t aborting = 0;
55 static int verbose = 0;
56 static size_t bigsize = 1024 * 1024;
57 static size_t medsize;
58 static size_t minsize = 512;
59 static off_t tot_size;
60 static off_t done_size;
62 static char *wworklist = NULL;
63 static char *rworklist = NULL;
64 static const char *unreadable_pattern = "_UNREAD_";
65 static const int write_errors_are_fatal = 1;
68 static TAILQ_HEAD(, lump) lumps = TAILQ_HEAD_INITIALIZER(lumps);
69 static struct period_head minute = TAILQ_HEAD_INITIALIZER(minute);
70 static struct period_head quarter = TAILQ_HEAD_INITIALIZER(quarter);
71 static struct period_head hour = TAILQ_HEAD_INITIALIZER(quarter);
72 static struct period_head day = TAILQ_HEAD_INITIALIZER(quarter);
74 /**********************************************************************/
77 report_good_read2(time_t now, size_t bytes, struct period_head *ph, time_t dt)
84 if (pp == NULL || pp->t1 < now) {
85 pp = calloc(sizeof *pp, 1L);
87 pp->t0 = (now / dt) * dt;
88 pp->t1 = (now / dt + 1) * dt;
89 assert(localtime_r(&pp->t0, &tm1) != NULL);
94 assert(strftime(pp->str, sizeof pp->str, fmt, &tm1) != 0);
95 TAILQ_INSERT_HEAD(ph, pp, list);
97 pp->bytes_read += bytes;
101 report_good_read(time_t now, size_t bytes)
104 report_good_read2(now, bytes, &minute, 60L);
105 report_good_read2(now, bytes, &quarter, 900L);
106 report_good_read2(now, bytes, &hour, 3600L);
107 report_good_read2(now, bytes, &day, 86400L);
111 report_one_period(const char *period, struct period_head *ph)
117 printf("%s \xe2\x94\x82", period);
118 TAILQ_FOREACH(pp, ph, list) {
120 TAILQ_REMOVE(ph, pp, list);
125 printf(" \xe2\x94\x82");
126 printf(" %s %14jd", pp->str, pp->bytes_read);
129 printf(" \xe2\x94\x82");
130 printf(" %5s %14s", "", "");
138 report_one_period("1m ", &minute);
139 report_one_period("15m", &quarter);
140 report_one_period("1h ", &hour);
141 report_one_period("1d ", &day);
144 /**********************************************************************/
151 if (!isatty(STDIN_FILENO) || ioctl(STDIN_FILENO, TIOCGWINSZ, &wsz))
157 report_header(int eol)
159 printf("%13s %7s %13s %5s %13s %13s %9s",
175 report_hline(const char *how)
179 for (j = 0; j < REPORTWID; j++) {
180 if (how && (j == 4 || j == 29 || j == 54)) {
183 printf("\xe2\x94\x80");
189 static off_t hist[REPORTWID];
190 static off_t last_done = -1;
193 report_histogram(const struct lump *lp)
195 off_t j, bucket, fp, fe, k, now;
199 bucket = tot_size / REPORTWID;
200 if (tot_size > bucket * REPORTWID)
202 if (done_size != last_done) {
203 memset(hist, 0, sizeof hist);
204 TAILQ_FOREACH(lp2, &lumps, list) {
206 fe = lp2->start + lp2->len;
207 for (j = fp / bucket; fp < fe; j++) {
208 k = (j + 1) * bucket;
216 last_done = done_size;
218 now = lp->start / bucket;
219 for (j = 0; j < REPORTWID; j++) {
220 a = round(8 * (double)hist[j] / bucket);
221 assert (a >= 0 && a < 9);
222 if (a == 0 && hist[j])
231 putchar(0x80 + (int)a);
240 report(const struct lump *lp, size_t sz)
248 printf("\x1b[H%s\x1b[K\n", input);
254 printf("%13jd %7zu %13jd %5d %13jd %13jd %9.4f",
260 (intmax_t)(tot_size - done_size),
261 100*(double)done_size/(double)tot_size
267 report_histogram(lp);
268 if (TAILQ_EMPTY(&minute)) {
271 report_hline("\xe2\x94\xac");
273 report_hline("\xe2\x94\xb4");
275 j = ioctl(STDIN_FILENO, TIOCGWINSZ, &wsz);
277 printf("\x1b[%d;1H", wsz.ws_row);
282 /**********************************************************************/
285 new_lump(off_t start, off_t len, int state)
289 lp = malloc(sizeof *lp);
291 err(1, "Malloc failed");
295 TAILQ_INSERT_TAIL(&lumps, lp, list);
298 /**********************************************************************
299 * Save the worklist if -w was given
309 if (fdw >= 0 && fdatasync(fdw))
310 err(1, "Write error, probably disk full");
312 if (wworklist != NULL) {
313 bprintf(buf, "%s.tmp", wworklist);
314 (void)fprintf(stderr, "\nSaving worklist ...");
315 (void)fflush(stderr);
317 file = fopen(buf, "w");
319 err(1, "Error opening file %s", buf);
321 TAILQ_FOREACH(llp, &lumps, list)
322 fprintf(file, "%jd %jd %d\n",
323 (intmax_t)llp->start, (intmax_t)llp->len,
326 if (ferror(file) || fdatasync(fileno(file)) || fclose(file))
327 err(1, "Error writing file %s", buf);
328 if (rename(buf, wworklist))
329 err(1, "Error renaming %s to %s", buf, wworklist);
330 (void)fprintf(stderr, " done.\n");
334 /* Read the worklist if -r was given */
336 read_worklist(off_t t)
342 (void)fprintf(stderr, "Reading worklist ...");
343 (void)fflush(stderr);
344 file = fopen(rworklist, "r");
346 err(1, "Error opening file %s", rworklist);
352 if (3 != fscanf(file, "%jd %jd %d\n", &s, &l, &state)) {
354 err(1, "Error parsing file %s at line %d",
359 new_lump(s, l, state);
363 err(1, "Error closing file %s", rworklist);
364 (void)fprintf(stderr, " done.\n");
366 * Return the number of bytes already read
367 * (at least not in worklist).
372 /**********************************************************************/
375 write_buf(int fd, const void *buf, ssize_t len, off_t where)
379 i = pwrite(fd, buf, len, where);
383 printf("\nWrite error at %jd/%zu\n\t%s\n",
384 where, i, strerror(errno));
386 if (write_errors_are_fatal)
391 fill_buf(char *buf, ssize_t len, const char *pattern)
393 ssize_t sz = strlen(pattern);
396 for (i = 0; i < len; i += sz) {
400 memcpy(buf + i, pattern, j);
404 /**********************************************************************/
409 (void)fprintf(stderr, "usage: recoverdisk [-b bigsize] [-r readlist] "
410 "[-s interval] [-w writelist] source [destination]\n");
416 sighandler(__unused int sig)
423 main(int argc, char * const argv[])
433 u_int n, snapshot = 60;
434 static struct lump *lp;
436 while ((ch = getopt(argc, argv, "b:r:w:s:u:v")) != -1) {
439 bigsize = strtoul(optarg, NULL, 0);
442 rworklist = strdup(optarg);
443 if (rworklist == NULL)
444 err(1, "Cannot allocate enough memory");
447 snapshot = strtoul(optarg, NULL, 0);
450 unreadable_pattern = optarg;
456 wworklist = strdup(optarg);
457 if (wworklist == NULL)
458 err(1, "Cannot allocate enough memory");
468 if (argc < 1 || argc > 2)
472 fdr = open(argv[0], O_RDONLY);
474 err(1, "Cannot open read descriptor %s", argv[0]);
476 error = fstat(fdr, &sb);
478 err(1, "fstat failed");
479 if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
480 error = ioctl(fdr, DIOCGSECTORSIZE, §orsize);
482 err(1, "DIOCGSECTORSIZE failed");
484 error = ioctl(fdr, DIOCGSTRIPESIZE, &stripesize);
485 if (error == 0 && stripesize > sectorsize)
486 sectorsize = stripesize;
488 minsize = sectorsize;
489 bigsize = rounddown(bigsize, sectorsize);
491 error = ioctl(fdr, DIOCGMEDIASIZE, &tot_size);
493 err(1, "DIOCGMEDIASIZE failed");
495 tot_size = sb.st_size;
498 if (bigsize < minsize)
501 for (ch = 0; (bigsize >> ch) > minsize; ch++)
503 medsize = bigsize >> (ch / 2);
504 medsize = rounddown(medsize, minsize);
506 fprintf(stderr, "Bigsize = %zu, medsize = %zu, minsize = %zu\n",
507 bigsize, medsize, minsize);
509 buf = malloc(bigsize);
511 err(1, "Cannot allocate %zu bytes buffer", bigsize);
514 fdw = open(argv[1], O_WRONLY | O_CREAT, DEFFILEMODE);
516 err(1, "Cannot open write descriptor %s", argv[1]);
517 if (ftruncate(fdw, tot_size) < 0)
518 err(1, "Cannot truncate output %s to %jd bytes",
519 argv[1], (intmax_t)tot_size);
523 if (rworklist != NULL) {
524 done_size = read_worklist(tot_size);
526 new_lump(0, tot_size, 0);
529 if (wworklist != NULL)
530 signal(SIGINT, sighandler);
540 lp = TAILQ_FIRST(&lumps);
543 while (lp->len > 0) {
546 sz = MIN(lp->len, (off_t)bigsize);
547 else if (lp->state == 1)
548 sz = MIN(lp->len, (off_t)medsize);
550 sz = MIN(lp->len, (off_t)minsize);
554 if (t1 != t2 || lp->len < (off_t)bigsize) {
556 if (++n == snapshot) {
563 j = pread(fdr, buf, sz, lp->start);
565 if (!(random() & 0xf)) {
573 write_buf(fdw, buf, sz, lp->start);
576 if (verbose && lp->state > 2)
577 report_good_read(t2, sz);
582 printf("%jd %zu %d read error (%s)\n",
583 lp->start, sz, lp->state, strerror(error));
586 if (fdw >= 0 && strlen(unreadable_pattern)) {
587 fill_buf(buf, sz, unreadable_pattern);
588 write_buf(fdw, buf, sz, lp->start);
590 new_lump(lp->start, sz, lp->state + 1);
593 if (error == EINVAL) {
594 printf("Try with -b 131072 or lower ?\n");
598 if (error == ENXIO) {
599 printf("Input device probably detached...\n");
606 if (aborting || !TAILQ_NEXT(lp, list))
610 assert(lp->len == 0);
611 TAILQ_REMOVE(&lumps, lp, list);
614 printf("%s", aborting ? "Aborted\n" : "Completed\n");