recoverdisk.c revision 187360
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 187360 2009-01-17 11:57:32Z 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; 29158337Smaximstatic size_t medsize = 64 * 1024; 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; 79158337Smaxim 80158337Smaxim if (wworklist != NULL) { 81158337Smaxim (void)fprintf(stderr, "\nSaving worklist ..."); 82158337Smaxim fflush(stderr); 83158337Smaxim 84158337Smaxim file = fopen(wworklist, "w"); 85158337Smaxim if (file == NULL) 86158337Smaxim err(1, "Error opening file %s", wworklist); 87158337Smaxim 88158337Smaxim for (;;) { 89158337Smaxim lp = TAILQ_FIRST(&lumps); 90158337Smaxim if (lp == NULL) 91158337Smaxim break; 92158337Smaxim fprintf(file, "%jd %jd %d\n", 93158337Smaxim (intmax_t)lp->start, (intmax_t)lp->len, lp->state); 94158337Smaxim TAILQ_REMOVE(&lumps, lp, list); 95158337Smaxim } 96158337Smaxim (void)fprintf(stderr, " done.\n"); 97158337Smaxim } 98158337Smaxim} 99158337Smaxim 100158337Smaxim/* Read the worklist if -r was given */ 101158337Smaximstatic off_t 102158337Smaximread_worklist(off_t t) 103158337Smaxim{ 104158337Smaxim off_t s, l, d; 105158337Smaxim int state, lines; 106158337Smaxim FILE *file; 107158337Smaxim 108158337Smaxim (void)fprintf(stderr, "Reading worklist ..."); 109158337Smaxim fflush(stderr); 110158337Smaxim file = fopen(rworklist, "r"); 111158337Smaxim if (file == NULL) 112158337Smaxim err(1, "Error opening file %s", rworklist); 113158337Smaxim 114158337Smaxim lines = 0; 115158337Smaxim d = t; 116158337Smaxim for (;;) { 117158337Smaxim ++lines; 118158337Smaxim if (3 != fscanf(file, "%jd %jd %d\n", &s, &l, &state)) { 119158337Smaxim if (!feof(file)) 120158337Smaxim err(1, "Error parsing file %s at line %d", 121158337Smaxim rworklist, lines); 122158337Smaxim else 123158337Smaxim break; 124158337Smaxim } 125158337Smaxim new_lump(s, l, state); 126158337Smaxim d -= l; 127158337Smaxim } 128158337Smaxim (void)fprintf(stderr, " done.\n"); 129158337Smaxim /* 130158337Smaxim * Return the number of bytes already read 131158337Smaxim * (at least not in worklist). 132158337Smaxim */ 133158337Smaxim return (d); 134158337Smaxim} 135158337Smaxim 136158337Smaximstatic void 137158337Smaximusage(void) 138158337Smaxim{ 139158337Smaxim (void)fprintf(stderr, 140158337Smaxim "usage: recoverdisk [-r worklist] [-w worklist] source-drive [destination]\n"); 141158337Smaxim exit(1); 142158337Smaxim} 143158337Smaxim 144158337Smaximstatic void 145158337Smaximsighandler(__unused int sig) 146158337Smaxim{ 147158337Smaxim 148158337Smaxim aborting = 1; 149158337Smaxim} 150158337Smaxim 151135911Sphkint 152158337Smaximmain(int argc, char * const argv[]) 153135911Sphk{ 154158337Smaxim int ch; 155135911Sphk int fdr, fdw; 156168972Sphk off_t t, d, start, len; 157135911Sphk size_t i, j; 158168972Sphk int error, flags, state; 159135911Sphk u_char *buf; 160158337Smaxim u_int sectorsize; 161136257Sphk time_t t1, t2; 162149605Ssobomax struct stat sb; 163135911Sphk 164158337Smaxim while ((ch = getopt(argc, argv, "r:w:")) != -1) { 165158337Smaxim switch (ch) { 166158337Smaxim case 'r': 167158337Smaxim rworklist = strdup(optarg); 168158337Smaxim if (rworklist == NULL) 169158337Smaxim err(1, "Cannot allocate enough memory"); 170158337Smaxim break; 171158337Smaxim case 'w': 172158337Smaxim wworklist = strdup(optarg); 173158337Smaxim if (wworklist == NULL) 174158337Smaxim err(1, "Cannot allocate enough memory"); 175158337Smaxim break; 176158337Smaxim default: 177158337Smaxim usage(); 178158337Smaxim /* NOTREACHED */ 179158337Smaxim } 180158337Smaxim } 181158337Smaxim argc -= optind; 182158337Smaxim argv += optind; 183135911Sphk 184158337Smaxim if (argc < 1 || argc > 2) 185158337Smaxim usage(); 186135911Sphk 187158337Smaxim fdr = open(argv[0], O_RDONLY); 188135911Sphk if (fdr < 0) 189158337Smaxim err(1, "Cannot open read descriptor %s", argv[0]); 190149605Ssobomax 191149605Ssobomax error = fstat(fdr, &sb); 192149605Ssobomax if (error < 0) 193149605Ssobomax err(1, "fstat failed"); 194149605Ssobomax flags = O_WRONLY; 195149605Ssobomax if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) { 196149605Ssobomax error = ioctl(fdr, DIOCGSECTORSIZE, §orsize); 197149605Ssobomax if (error < 0) 198149605Ssobomax err(1, "DIOCGSECTORSIZE failed"); 199158337Smaxim 200158337Smaxim /* 201158337Smaxim * Make medsize roughly 64kB, depending on native sector 202158337Smaxim * size. bigsize has to be a multiple of medsize. 203158337Smaxim * For media with 2352 sectors, this will 204158337Smaxim * result in 2352, 63504, and 1016064 bytes. 205158337Smaxim */ 206149605Ssobomax minsize = sectorsize; 207158337Smaxim medsize = (medsize / sectorsize) * sectorsize; 208158337Smaxim bigsize = medsize * 16; 209149605Ssobomax 210149605Ssobomax error = ioctl(fdr, DIOCGMEDIASIZE, &t); 211149605Ssobomax if (error < 0) 212149605Ssobomax err(1, "DIOCGMEDIASIZE failed"); 213149605Ssobomax } else { 214149605Ssobomax t = sb.st_size; 215149605Ssobomax flags |= O_CREAT | O_TRUNC; 216149605Ssobomax } 217149605Ssobomax 218158337Smaxim buf = malloc(bigsize); 219158337Smaxim if (buf == NULL) 220158337Smaxim err(1, "Cannot allocate %jd bytes buffer", (intmax_t)bigsize); 221158337Smaxim 222158337Smaxim if (argc > 1) { 223158337Smaxim fdw = open(argv[1], flags, DEFFILEMODE); 224135911Sphk if (fdw < 0) 225158337Smaxim err(1, "Cannot open write descriptor %s", argv[1]); 226158337Smaxim } else 227158337Smaxim fdw = -1; 228158337Smaxim 229158337Smaxim if (rworklist != NULL) { 230158337Smaxim d = read_worklist(t); 231135911Sphk } else { 232158337Smaxim new_lump(0, t, 0); 233158337Smaxim d = 0; 234135911Sphk } 235158337Smaxim if (wworklist != NULL) 236158337Smaxim signal(SIGINT, sighandler); 237135911Sphk 238136257Sphk t1 = 0; 239168972Sphk start = len = i = state = 0; 240168972Sphk PRINT_HEADER; 241135911Sphk for (;;) { 242135911Sphk lp = TAILQ_FIRST(&lumps); 243135911Sphk if (lp == NULL) 244135911Sphk break; 245158337Smaxim while (lp->len > 0 && !aborting) { 246168972Sphk /* These are only copied for printing stats */ 247168972Sphk start = lp->start; 248168972Sphk len = lp->len; 249168972Sphk state = lp->state; 250168972Sphk 251159076Smatteo i = MIN(lp->len, (off_t)bigsize); 252135911Sphk if (lp->state == 1) 253159076Smatteo i = MIN(lp->len, (off_t)medsize); 254135911Sphk if (lp->state > 1) 255159076Smatteo i = MIN(lp->len, (off_t)minsize); 256136257Sphk time(&t2); 257159076Smatteo if (t1 != t2 || lp->len < (off_t)bigsize) { 258168972Sphk PRINT_STATUS(start, i, len, state, d, t); 259136257Sphk t1 = t2; 260136257Sphk } 261135911Sphk if (i == 0) { 262135911Sphk errx(1, "BOGUS i %10jd", (intmax_t)i); 263135911Sphk } 264135911Sphk fflush(stdout); 265135911Sphk j = pread(fdr, buf, i, lp->start); 266135911Sphk if (j == i) { 267135911Sphk d += i; 268135911Sphk if (fdw >= 0) 269135911Sphk j = pwrite(fdw, buf, i, lp->start); 270135911Sphk else 271135911Sphk j = i; 272135911Sphk if (j != i) 273136815Sdes printf("\nWrite error at %jd/%zu\n", 274136815Sdes lp->start, i); 275135911Sphk lp->start += i; 276135911Sphk lp->len -= i; 277135911Sphk continue; 278135911Sphk } 279187360Sphk printf("\n%jd %zu failed (%s)\n", 280187360Sphk lp->start, i, strerror(errno)); 281187360Sphk if (errno == ENXIO) 282187360Sphk aborting = 1; 283135911Sphk new_lump(lp->start, i, lp->state + 1); 284135911Sphk lp->start += i; 285135911Sphk lp->len -= i; 286135911Sphk } 287158337Smaxim if (aborting) { 288158337Smaxim save_worklist(); 289158337Smaxim return (0); 290158337Smaxim } 291158337Smaxim TAILQ_REMOVE(&lumps, lp, list); 292135911Sphk free(lp); 293135911Sphk } 294168972Sphk PRINT_STATUS(start, i, len, state, d, t); 295135911Sphk printf("\nCompleted\n"); 296158337Smaxim return (0); 297135911Sphk} 298