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);
126 (void)fprintf(stderr, " done.\n");
128 * Return the number of bytes already read
129 * (at least not in worklist).
137 (void)fprintf(stderr, "usage: recoverdisk [-b bigsize] [-r readlist] "
138 "[-s interval] [-w writelist] source [destination]\n");
143 sighandler(__unused int sig)
150 main(int argc, char * const argv[])
154 off_t t, d, start, len;
162 u_int n, snapshot = 60;
164 while ((ch = getopt(argc, argv, "b:r:w:s:")) != -1) {
167 bigsize = strtoul(optarg, NULL, 0);
170 rworklist = strdup(optarg);
171 if (rworklist == NULL)
172 err(1, "Cannot allocate enough memory");
175 snapshot = strtoul(optarg, NULL, 0);
178 wworklist = strdup(optarg);
179 if (wworklist == NULL)
180 err(1, "Cannot allocate enough memory");
190 if (argc < 1 || argc > 2)
193 fdr = open(argv[0], O_RDONLY);
195 err(1, "Cannot open read descriptor %s", argv[0]);
197 error = fstat(fdr, &sb);
199 err(1, "fstat failed");
200 if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
201 error = ioctl(fdr, DIOCGSECTORSIZE, §orsize);
203 err(1, "DIOCGSECTORSIZE failed");
205 error = ioctl(fdr, DIOCGSTRIPESIZE, &stripesize);
206 if (error == 0 && stripesize > sectorsize)
207 sectorsize = stripesize;
209 minsize = sectorsize;
210 bigsize = (bigsize / sectorsize) * sectorsize;
212 error = ioctl(fdr, DIOCGMEDIASIZE, &t);
214 err(1, "DIOCGMEDIASIZE failed");
219 if (bigsize < minsize)
222 for (ch = 0; (bigsize >> ch) > minsize; ch++)
224 medsize = bigsize >> (ch / 2);
225 medsize = (medsize / minsize) * minsize;
227 fprintf(stderr, "Bigsize = %zu, medsize = %zu, minsize = %zu\n",
228 bigsize, medsize, minsize);
230 buf = malloc(bigsize);
232 err(1, "Cannot allocate %zu bytes buffer", bigsize);
235 fdw = open(argv[1], O_WRONLY | O_CREAT, DEFFILEMODE);
237 err(1, "Cannot open write descriptor %s", argv[1]);
238 if (ftruncate(fdw, t) < 0)
239 err(1, "Cannot truncate output %s to %jd bytes",
240 argv[1], (intmax_t)t);
244 if (rworklist != NULL) {
245 d = read_worklist(t);
250 if (wworklist != NULL)
251 signal(SIGINT, sighandler);
254 start = len = i = state = 0;
258 lp = TAILQ_FIRST(&lumps);
261 while (lp->len > 0 && !aborting) {
262 /* These are only copied for printing stats */
267 i = MIN(lp->len, (off_t)bigsize);
269 i = MIN(lp->len, (off_t)medsize);
271 i = MIN(lp->len, (off_t)minsize);
273 if (t1 != t2 || lp->len < (off_t)bigsize) {
274 PRINT_STATUS(start, i, len, state, d, t);
276 if (++n == snapshot) {
282 errx(1, "BOGUS i %10jd", (intmax_t)i);
285 j = pread(fdr, buf, i, lp->start);
289 j = pwrite(fdw, buf, i, lp->start);
293 printf("\nWrite error at %jd/%zu\n",
299 printf("\n%jd %zu failed (%s)\n",
300 lp->start, i, strerror(errno));
301 if (errno == EINVAL) {
302 printf("read() size too big? Try with -b 131072");
307 new_lump(lp->start, i, lp->state + 1);
315 TAILQ_REMOVE(&lumps, lp, list);
318 PRINT_STATUS(start, i, len, state, d, t);
320 printf("\nCompleted\n");