2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
11 #include <sys/param.h>
12 #include <sys/queue.h>
27 static volatile sig_atomic_t aborting = 0;
28 static size_t bigsize = 1024 * 1024;
29 static size_t medsize;
30 static size_t minsize = 512;
36 TAILQ_ENTRY(lump) list;
39 static TAILQ_HEAD(, lump) lumps = TAILQ_HEAD_INITIALIZER(lumps);
42 new_lump(off_t start, off_t len, int state)
46 lp = malloc(sizeof *lp);
48 err(1, "Malloc failed");
52 TAILQ_INSERT_TAIL(&lumps, lp, list);
55 static struct lump *lp;
56 static char *wworklist = NULL;
57 static char *rworklist = NULL;
60 #define PRINT_HEADER \
61 printf("%13s %7s %13s %5s %13s %13s %9s\n", \
62 "start", "size", "block-len", "state", "done", "remaining", "% done")
64 #define PRINT_STATUS(start, i, len, state, d, t) \
65 printf("\r%13jd %7zu %13jd %5d %13jd %13jd %9.5f", \
72 100*(double)d/(double)t)
74 /* Save the worklist if -w was given */
81 if (wworklist != NULL) {
82 (void)fprintf(stderr, "\nSaving worklist ...");
85 file = fopen(wworklist, "w");
87 err(1, "Error opening file %s", wworklist);
89 TAILQ_FOREACH(llp, &lumps, list)
90 fprintf(file, "%jd %jd %d\n",
91 (intmax_t)llp->start, (intmax_t)llp->len,
94 (void)fprintf(stderr, " done.\n");
98 /* Read the worklist if -r was given */
100 read_worklist(off_t t)
106 (void)fprintf(stderr, "Reading worklist ...");
108 file = fopen(rworklist, "r");
110 err(1, "Error opening file %s", rworklist);
116 if (3 != fscanf(file, "%jd %jd %d\n", &s, &l, &state)) {
118 err(1, "Error parsing file %s at line %d",
123 new_lump(s, l, state);
127 (void)fprintf(stderr, " done.\n");
129 * Return the number of bytes already read
130 * (at least not in worklist).
138 (void)fprintf(stderr, "usage: recoverdisk [-b bigsize] [-r readlist] "
139 "[-s interval] [-w writelist] source [destination]\n");
144 sighandler(__unused int sig)
151 main(int argc, char * const argv[])
155 off_t t, d, start, len;
163 u_int n, snapshot = 60;
165 while ((ch = getopt(argc, argv, "b:r:w:s:")) != -1) {
168 bigsize = strtoul(optarg, NULL, 0);
171 rworklist = strdup(optarg);
172 if (rworklist == NULL)
173 err(1, "Cannot allocate enough memory");
176 snapshot = strtoul(optarg, NULL, 0);
179 wworklist = strdup(optarg);
180 if (wworklist == NULL)
181 err(1, "Cannot allocate enough memory");
191 if (argc < 1 || argc > 2)
194 fdr = open(argv[0], O_RDONLY);
196 err(1, "Cannot open read descriptor %s", argv[0]);
198 error = fstat(fdr, &sb);
200 err(1, "fstat failed");
201 if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
202 error = ioctl(fdr, DIOCGSECTORSIZE, §orsize);
204 err(1, "DIOCGSECTORSIZE failed");
206 error = ioctl(fdr, DIOCGSTRIPESIZE, &stripesize);
207 if (error == 0 && stripesize > sectorsize)
208 sectorsize = stripesize;
210 minsize = sectorsize;
211 bigsize = rounddown(bigsize, sectorsize);
213 error = ioctl(fdr, DIOCGMEDIASIZE, &t);
215 err(1, "DIOCGMEDIASIZE failed");
220 if (bigsize < minsize)
223 for (ch = 0; (bigsize >> ch) > minsize; ch++)
225 medsize = bigsize >> (ch / 2);
226 medsize = rounddown(medsize, minsize);
228 fprintf(stderr, "Bigsize = %zu, medsize = %zu, minsize = %zu\n",
229 bigsize, medsize, minsize);
231 buf = malloc(bigsize);
233 err(1, "Cannot allocate %zu bytes buffer", bigsize);
236 fdw = open(argv[1], O_WRONLY | O_CREAT, DEFFILEMODE);
238 err(1, "Cannot open write descriptor %s", argv[1]);
239 if (ftruncate(fdw, t) < 0)
240 err(1, "Cannot truncate output %s to %jd bytes",
241 argv[1], (intmax_t)t);
245 if (rworklist != NULL) {
246 d = read_worklist(t);
251 if (wworklist != NULL)
252 signal(SIGINT, sighandler);
255 start = len = i = state = 0;
259 lp = TAILQ_FIRST(&lumps);
262 while (lp->len > 0 && !aborting) {
263 /* These are only copied for printing stats */
268 i = MIN(lp->len, (off_t)bigsize);
270 i = MIN(lp->len, (off_t)medsize);
272 i = MIN(lp->len, (off_t)minsize);
274 if (t1 != t2 || lp->len < (off_t)bigsize) {
275 PRINT_STATUS(start, i, len, state, d, t);
277 if (++n == snapshot) {
283 errx(1, "BOGUS i %10jd", (intmax_t)i);
286 j = pread(fdr, buf, i, lp->start);
290 j = pwrite(fdw, buf, i, lp->start);
294 printf("\nWrite error at %jd/%zu\n",
300 printf("\n%jd %zu failed (%s)\n",
301 lp->start, i, strerror(errno));
302 if (errno == EINVAL) {
303 printf("read() size too big? Try with -b 131072");
308 new_lump(lp->start, i, lp->state + 1);
316 TAILQ_REMOVE(&lumps, lp, list);
319 PRINT_STATUS(start, i, len, state, d, t);
321 printf("\nCompleted\n");