ecp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * ecp - copy a file fast (in big blocks), cope with errors, optionally verify.
  11. *
  12. * Transfers a block at a time. On error, retries one sector at a time,
  13. * and reports all errors on the retry.
  14. * Unlike dd, ecp ignores EOF, since it is sometimes reported on error.
  15. * Also unlike `dd conv=noerror,sync', ecp doesn't get stuck nor give up.
  16. *
  17. * Written by Geoff Collyer, originally to run on RSX-11M(!) in 1979.
  18. * Later simplified for UNIX and ultimately Plan 9.
  19. */
  20. #include <u.h>
  21. #include <libc.h>
  22. #include <ctype.h>
  23. /* fundamental constants */
  24. enum {
  25. No = 0,
  26. Yes,
  27. Noseek = 0, /* need not seek, may seek on seekable files */
  28. Mustseek,
  29. Enone = 0,
  30. Eio,
  31. };
  32. /* tunable parameters */
  33. enum {
  34. Defsectsz = 512, /* default sector size */
  35. /* 10K is a good size for HP WORM drives */
  36. Defblksz = 16*1024, /* default block (big-transfer) size */
  37. Mingoodblks = 3, /* after this many, go back to fast mode */
  38. };
  39. #define TTY "/dev/cons" /* plan 9 */
  40. #define badsect(errno) ((errno) != Enone) /* was last transfer in error? */
  41. /* disk address (in bytes or sectors), also type of 2nd arg. to seek */
  42. typedef uint64_t Daddr;
  43. typedef int64_t Sdaddr; /* signed disk address */
  44. typedef int32_t Rdwrfn(int, void *, int32_t); /* plan 9 read or write */
  45. typedef struct {
  46. char *name;
  47. int fd;
  48. Daddr startsect;
  49. int fast;
  50. int seekable;
  51. uint32_t maxconerrs; /* maximum consecutive errors */
  52. uint32_t conerrs; /* current consecutive errors */
  53. Daddr congoodblks;
  54. Daddr harderrs;
  55. Daddr lasterr; /* sector #s */
  56. Daddr lastgood;
  57. } File;
  58. /* exports */
  59. char *argv0;
  60. /* privates */
  61. static int reblock = No, progress = No, swizzle = No;
  62. static int reverse = No;
  63. static uint32_t sectsz = Defsectsz;
  64. static uint32_t blocksize = Defblksz;
  65. static char *buf, *vfybuf;
  66. static int blksects;
  67. /*
  68. * warning - print best error message possible and clear errno
  69. */
  70. void
  71. warning(char *s1, char *s2)
  72. {
  73. char err[100], msg[256];
  74. char *np, *ep = msg + sizeof msg - 1;
  75. errstr(err, sizeof err); /* save error string */
  76. np = seprint(msg, ep, "%s: ", argv0);
  77. np = seprint(np, ep, s1, s2);
  78. errstr(err, sizeof err); /* restore error string */
  79. seprint(np, ep, ": %r\n");
  80. fprint(2, "%s", msg);
  81. }
  82. int
  83. eopen(char *file, int mode)
  84. {
  85. int fd = open(file, mode);
  86. if (fd < 0)
  87. sysfatal("can't open %s: %r", file);
  88. return fd;
  89. }
  90. static int /* boolean */
  91. confirm(File *src, File *dest)
  92. {
  93. int absent, n, tty = eopen(TTY, 2);
  94. char c, junk;
  95. Dir *stp;
  96. if ((stp = dirstat(src->name)) == nil)
  97. sysfatal("no input file %s: %r", src->name);
  98. free(stp);
  99. stp = dirstat(dest->name);
  100. absent = (stp == nil);
  101. free(stp);
  102. fprint(2, "%s: copy %s to %s%s? ", argv0, src->name, dest->name,
  103. (absent? " (missing)": ""));
  104. n = read(tty, &c, 1);
  105. junk = c;
  106. if (n < 1)
  107. c = 'n';
  108. while (n > 0 && junk != '\n')
  109. n = read(tty, &junk, 1);
  110. close(tty);
  111. if (isascii(c) && isupper(c))
  112. c = tolower(c);
  113. return c == 'y';
  114. }
  115. static char *
  116. sectid(File *fp, Daddr sect)
  117. {
  118. static char sectname[256];
  119. if (fp->startsect == 0)
  120. snprint(sectname, sizeof sectname, "%s sector %llud",
  121. fp->name, sect);
  122. else
  123. snprint(sectname, sizeof sectname,
  124. "%s sector %llud (relative %llud)",
  125. fp->name, sect + fp->startsect, sect);
  126. return sectname;
  127. }
  128. static void
  129. io_expl(File *fp, char *rw, Daddr sect) /* explain an i/o error */
  130. {
  131. /* print only first 2 bad sectors in a range, if going forward */
  132. if (reverse || fp->conerrs == 0) {
  133. char msg[128];
  134. snprint(msg, sizeof msg, "%s %s", rw, sectid(fp, sect));
  135. warning("%s", msg);
  136. } else if (fp->conerrs == 1)
  137. fprint(2, "%s: ...\n", argv0);
  138. }
  139. static void
  140. repos(File *fp, Daddr sect)
  141. {
  142. if (!fp->seekable)
  143. sysfatal("%s: trying to seek on unseekable file", fp->name);
  144. if (seek(fp->fd, (sect+fp->startsect)*sectsz, 0) == -1)
  145. sysfatal("can't seek on %s: %r", fp->name);
  146. }
  147. static void
  148. rewind(File *fp)
  149. {
  150. repos(fp, 0);
  151. }
  152. static char magic[] = "\235any old ☺ rubbish\173";
  153. static char uniq[sizeof magic + 2*sizeof(uint32_t)];
  154. static char *
  155. putbe(char *p, uint32_t ul)
  156. {
  157. *p++ = ul>>24;
  158. *p++ = ul>>16;
  159. *p++ = ul>>8;
  160. *p++ = ul;
  161. return p;
  162. }
  163. /*
  164. * generate magic + unique string, add to start & end of buff.
  165. * return tail pointer.
  166. */
  167. static char *
  168. addmagic(char *buff, int bytes)
  169. {
  170. char *p, *tail;
  171. static uint32_t seq;
  172. strcpy(uniq, magic);
  173. p = putbe(uniq + sizeof magic - 1, time(0));
  174. putbe(p, ++seq);
  175. memcpy(buff, uniq, sizeof uniq);
  176. tail = buff + bytes - sizeof uniq;
  177. memcpy(tail, uniq, sizeof uniq);
  178. return tail;
  179. }
  180. /* verify magic + unique strings in buff */
  181. static int
  182. ismagicok(char *buff, char *tail)
  183. {
  184. return memcmp(buff, uniq, sizeof uniq) == 0 ||
  185. memcmp(tail, uniq, sizeof uniq) == 0;
  186. }
  187. /*
  188. * transfer (many) sectors. reblock input as needed.
  189. * returns Enone if no failures, others on failure with errstr set.
  190. */
  191. static int
  192. bio(File *fp, Rdwrfn *rdwr, char *buff, Daddr stsect, int sects,
  193. int mustseek)
  194. {
  195. int xfered;
  196. char *tail;
  197. uint32_t toread, bytes = sects * sectsz;
  198. static int reblocked = 0;
  199. if (mustseek) {
  200. if (!fp->seekable)
  201. sysfatal("%s: need to seek on unseekable file",
  202. fp->name);
  203. repos(fp, stsect);
  204. }
  205. if ((int32_t)blocksize != blocksize || (int32_t)bytes != bytes)
  206. sysfatal("i/o count too big: %lud", bytes);
  207. SET(tail);
  208. if (rdwr == read)
  209. tail = addmagic(buff, bytes);
  210. werrstr("");
  211. xfered = (*rdwr)(fp->fd, buff, bytes);
  212. if (xfered == bytes) {
  213. /* don't trust the hardware; it may lie */
  214. if (rdwr == read && ismagicok(buff, tail))
  215. fprint(2, "%s: `good' read didn't change buffer\n",
  216. argv0);
  217. return Enone; /* did as we asked */
  218. }
  219. if (xfered < 0)
  220. return Eio; /* out-and-out i/o error */
  221. /*
  222. * Kernel transferred less than asked. Shouldn't happen;
  223. * probably indicates disk driver error or trying to
  224. * transfer past the end of a disk partition. Treat as an
  225. * I/O error that reads zeros past the point of error,
  226. * unless reblocking input and this is a read.
  227. */
  228. if (rdwr == write)
  229. return Eio;
  230. if (!reblock) {
  231. memset(buff+xfered, '\0', bytes-xfered);
  232. return Eio; /* short read */
  233. }
  234. /* for pipes that return less than asked */
  235. if (progress && !reblocked) {
  236. fprint(2, "%s: reblocking input\n", argv0);
  237. reblocked++;
  238. }
  239. for (toread = bytes - xfered; toread != 0; toread -= xfered) {
  240. xfered = (*rdwr)(fp->fd, buff+bytes-toread, toread);
  241. if (xfered <= 0)
  242. break;
  243. }
  244. if (xfered < 0)
  245. return Eio; /* out-and-out i/o error */
  246. if (toread != 0) /* early EOF? */
  247. memset(buff+bytes-toread, '\0', toread);
  248. return Enone;
  249. }
  250. /* called only after a single-sector transfer */
  251. static int
  252. toomanyerrs(File *fp, Daddr sect)
  253. {
  254. if (sect == fp->lasterr+1)
  255. fp->conerrs++;
  256. else
  257. fp->conerrs = 0;
  258. fp->lasterr = sect;
  259. return fp->maxconerrs != 0 && fp->conerrs >= fp->maxconerrs &&
  260. fp->lastgood == -1;
  261. }
  262. static void
  263. ckendrange(File *fp)
  264. {
  265. if (!reverse && fp->conerrs > 0)
  266. fprint(2, "%s: %lld: ... last bad sector in range\n",
  267. argv0, fp->lasterr);
  268. }
  269. static int
  270. transfer(File *fp, Rdwrfn *rdwr, char *buff, Daddr stsect, int sects,
  271. int mustseek)
  272. {
  273. int res = bio(fp, rdwr, buff, stsect, sects, mustseek);
  274. if (badsect(res)) {
  275. fp->fast = 0; /* read single sectors for a while */
  276. fp->congoodblks = 0;
  277. } else
  278. fp->lastgood = stsect + sects - 1;
  279. return res;
  280. }
  281. /*
  282. * Read or write many sectors at once.
  283. * If it fails, retry the individual sectors and report errors.
  284. */
  285. static void
  286. bigxfer(File *fp, Rdwrfn *rdwr, char *buff, Daddr stsect, int sects,
  287. int mustseek)
  288. {
  289. int i, badsects = 0, wasfast = fp->fast;
  290. char *rw = (rdwr == read? "read": "write");
  291. if (fp->fast) {
  292. if (!badsect(transfer(fp, rdwr, buff, stsect, sects, mustseek)))
  293. return;
  294. if (progress)
  295. fprint(2, "%s: breaking up big transfer on %s error "
  296. "`%r' on %s\n", argv0, rw, sectid(fp, stsect));
  297. }
  298. for (i = 0; i < sects; i++)
  299. if (badsect(transfer(fp, rdwr, buff+i*sectsz, stsect+i, 1,
  300. Mustseek))) {
  301. io_expl(fp, rw, stsect+i);
  302. badsects++;
  303. fp->harderrs++;
  304. if (toomanyerrs(fp, stsect+i))
  305. sysfatal("more than %lud consecutive I/O errors",
  306. fp->maxconerrs);
  307. } else {
  308. ckendrange(fp);
  309. fp->conerrs = 0;
  310. }
  311. if (badsects == 0) {
  312. ckendrange(fp);
  313. fp->conerrs = 0;
  314. if (wasfast)
  315. fprint(2, "%s: %s error on big transfer at %s but none "
  316. "on retries!\n", argv0, rw, sectid(fp, stsect));
  317. ++fp->congoodblks;
  318. if (fp->congoodblks >= Mingoodblks) {
  319. fprint(2, "%s: %s: back to big transfers\n", argv0,
  320. fp->name);
  321. fp->fast = 1;
  322. }
  323. } else
  324. /*
  325. * the last sector could have been in error, so the seek pointer
  326. * may need to be corrected.
  327. */
  328. repos(fp, stsect + sects);
  329. }
  330. static void
  331. vrfyfailed(File *src, File *dest, Daddr stsect)
  332. {
  333. char *srcsect = strdup(sectid(src, stsect));
  334. fprint(2, "%s: verify failed at %s (%s)\n", argv0, srcsect,
  335. sectid(dest, stsect));
  336. free(srcsect);
  337. }
  338. /*
  339. * I've seen SCSI read errors that the kernel printed but then didn't
  340. * report to the program doing the read, so if a big verify fails,
  341. * break it up and verify each sector separately to isolate the bad sector(s).
  342. */
  343. int /* error count */
  344. verify(File *src, File *dest, char *buff, char *buft, Daddr stsect,
  345. int sectors)
  346. {
  347. int i, errors = 0;
  348. for (i = 0; i < sectors; i++)
  349. if (memcmp(buff + i*sectsz, buft + i*sectsz, sectsz) != 0)
  350. errors++;
  351. if (errors == 0)
  352. return errors; /* normal case */
  353. if (sectors == 1) {
  354. vrfyfailed(src, dest, stsect);
  355. return errors;
  356. }
  357. /* re-read and verify each sector individually */
  358. errors = 0;
  359. for (i = 0; i < sectors; i++) {
  360. int thissect = stsect + i;
  361. if (badsect(bio(src, read, buff, thissect, 1, Mustseek)))
  362. io_expl(src, "read", thissect);
  363. if (badsect(bio(dest, read, buft, thissect, 1, Mustseek)))
  364. io_expl(dest, "write", thissect);
  365. if (memcmp(buff, buft, sectsz) != 0) {
  366. vrfyfailed(src, dest, thissect);
  367. ++errors;
  368. }
  369. }
  370. if (errors == 0) {
  371. char *srcsect = strdup(sectid(src, stsect));
  372. fprint(2, "%s: verification failed on big read at %s (%s) "
  373. "but not on retries!\n", argv0, srcsect,
  374. sectid(dest, stsect));
  375. free(srcsect);
  376. }
  377. /*
  378. * the last sector of each could have been in error, so the seek
  379. * pointers may need to be corrected.
  380. */
  381. repos(src, stsect + sectors);
  382. repos(dest, stsect + sectors);
  383. return errors;
  384. }
  385. /*
  386. * start is starting sector of proposed transfer;
  387. * nsects is the total number of sectors being copied;
  388. * maxxfr is the block size in sectors.
  389. */
  390. int
  391. sectsleft(Daddr start, Daddr nsects, int maxxfr)
  392. {
  393. /* nsects-start is sectors to the end */
  394. if (start + maxxfr <= nsects - 1)
  395. return maxxfr;
  396. else
  397. return nsects - start;
  398. }
  399. enum {
  400. Rotbits = 3,
  401. };
  402. void
  403. swizzlebits(char *buff, int sects)
  404. {
  405. uint8_t *bp, *endbp;
  406. endbp = (uint8_t *)(buff+sects*sectsz);
  407. for (bp = (uint8_t *)buff; bp < endbp; bp++)
  408. *bp = ~(*bp>>Rotbits | *bp<<(8-Rotbits));
  409. }
  410. /*
  411. * copy at most blksects sectors, with error retries.
  412. * stsect is relative to the start of the copy; 0 is the first sector.
  413. * to get actual sector numbers, add e.g. dest->startsect.
  414. */
  415. static int
  416. copysects(File *src, File *dest, Daddr stsect, Daddr nsects, int mustseek)
  417. {
  418. int xfrsects = sectsleft(stsect, nsects, blksects);
  419. if (xfrsects > blksects) {
  420. fprint(2, "%s: block size of %d is too big.\n", argv0, xfrsects);
  421. exits("block size too big");
  422. }
  423. bigxfer(src, read, buf, stsect, xfrsects, mustseek);
  424. if (swizzle)
  425. swizzlebits(buf, xfrsects);
  426. bigxfer(dest, write, buf, stsect, xfrsects, mustseek);
  427. /* give a few reassurances at the start, then every 10MB */
  428. if (progress &&
  429. (stsect < blksects*10 || stsect%(10*1024*1024/sectsz) == 0))
  430. fprint(2, "%s: copied%s to relative sector %llud\n", argv0,
  431. (swizzle? " swizzled": ""), stsect + xfrsects - 1);
  432. return 0;
  433. }
  434. /*
  435. * verify at most blksects sectors, with error retries.
  436. * return error count.
  437. */
  438. static int
  439. vrfysects(File *src, File *dest, Daddr stsect, Daddr nsects, int mustseek)
  440. {
  441. int xfrsects = sectsleft(stsect, nsects, blksects);
  442. if (xfrsects > blksects) {
  443. fprint(2, "%s: block size of %d is too big.\n", argv0, xfrsects);
  444. exits("block size too big");
  445. }
  446. bigxfer(src, read, buf, stsect, xfrsects, mustseek);
  447. bigxfer(dest, read, vfybuf, stsect, xfrsects, mustseek);
  448. return verify(src, dest, buf, vfybuf, stsect, xfrsects);
  449. }
  450. static void
  451. setupfile(File *fp, int mode)
  452. {
  453. fp->fd = open(fp->name, mode);
  454. if (fp->fd < 0)
  455. sysfatal("can't open %s: %r", fp->name);
  456. fp->seekable = (seek(fp->fd, 0, 1) >= 0);
  457. if (fp->startsect != 0)
  458. rewind(fp);
  459. }
  460. static Daddr
  461. copyfile(File *src, File *dest, Daddr nsects, int plsverify)
  462. {
  463. Sdaddr stsect, vererrs = 0;
  464. Dir *stp;
  465. setupfile(src, OREAD);
  466. if ((stp = dirstat(dest->name)) == nil) {
  467. int fd = create(dest->name, ORDWR, 0666);
  468. if (fd >= 0)
  469. close(fd);
  470. }
  471. free(stp);
  472. setupfile(dest, ORDWR);
  473. if (progress)
  474. fprint(2, "%s: copying first sectors\n", argv0);
  475. if (reverse)
  476. for (stsect = (nsects/blksects)*blksects; stsect >= 0;
  477. stsect -= blksects)
  478. vererrs += copysects(src, dest, stsect, nsects, Mustseek);
  479. else {
  480. for (stsect = 0; stsect < nsects; stsect += blksects)
  481. vererrs += copysects(src, dest, stsect, nsects, Noseek);
  482. ckendrange(src);
  483. ckendrange(dest);
  484. }
  485. /*
  486. * verification is done as a separate pass rather than immediately after
  487. * writing, in part to defeat caching in clever disk controllers.
  488. * we really want to see the bits that hit the disk.
  489. */
  490. if (plsverify) {
  491. fprint(2, "%s: copy done; verifying...\n", argv0);
  492. rewind(src);
  493. rewind(dest);
  494. for (stsect = 0; stsect < nsects; stsect += blksects) /* forward */
  495. vererrs += vrfysects(src, dest, stsect, nsects, Noseek);
  496. if (vererrs <= 0)
  497. fprint(2, "%s: no", argv0);
  498. else
  499. fprint(2, "%s: %llud", argv0, vererrs);
  500. fprint(2, " error%s during verification\n",
  501. (vererrs != 1? "s": ""));
  502. }
  503. close(src->fd);
  504. close(dest->fd);
  505. return vererrs;
  506. }
  507. static void
  508. usage(void)
  509. {
  510. fprint(2, "usage: %s [-bcprvZ][-B blocksz][-e errs][-s sectsz]"
  511. "[-i issect][-o ossect] sectors from to\n", argv0);
  512. exits("usage");
  513. }
  514. void
  515. initfile(File *fp)
  516. {
  517. memset(fp, 0, sizeof *fp);
  518. fp->fast = 1;
  519. fp->lasterr = -1;
  520. fp->lastgood = -1;
  521. }
  522. void
  523. main(int argc, char **argv)
  524. {
  525. int errflg = 0, plsconfirm = No, plsverify = No;
  526. int32_t lval;
  527. File src, dest;
  528. Sdaddr sect;
  529. initfile(&src);
  530. initfile(&dest);
  531. ARGBEGIN {
  532. case 'b':
  533. reblock = Yes;
  534. break;
  535. case 'B':
  536. lval = atol(EARGF(usage()));
  537. if (lval < 0)
  538. usage();
  539. blocksize = lval;
  540. break;
  541. case 'c':
  542. plsconfirm = Yes;
  543. break;
  544. case 'e':
  545. lval = atol(EARGF(usage()));
  546. if (lval < 0)
  547. usage();
  548. src.maxconerrs = lval;
  549. dest.maxconerrs = lval;
  550. break;
  551. case 'i':
  552. sect = atoll(EARGF(usage()));
  553. if (sect < 0)
  554. usage();
  555. src.startsect = sect;
  556. break;
  557. case 'o':
  558. sect = atoll(EARGF(usage()));
  559. if (sect < 0)
  560. usage();
  561. dest.startsect = sect;
  562. break;
  563. case 'p':
  564. progress = Yes;
  565. break;
  566. case 'r':
  567. reverse = Yes;
  568. break;
  569. case 's':
  570. sectsz = atol(EARGF(usage()));
  571. if (sectsz <= 0 || sectsz % 512 != 0)
  572. usage();
  573. break;
  574. case 'v':
  575. plsverify = Yes;
  576. break;
  577. case 'Z':
  578. swizzle = Yes;
  579. break;
  580. default:
  581. errflg++;
  582. break;
  583. } ARGEND
  584. if (errflg || argc != 3)
  585. usage();
  586. if (blocksize <= 0 || blocksize % sectsz != 0)
  587. sysfatal("block size not a multiple of sector size");
  588. if (!isascii(argv[0][0]) || !isdigit(argv[0][0])) {
  589. fprint(2, "%s: %s is not numeric\n", argv0, argv[0]);
  590. exits("non-numeric sector count");
  591. }
  592. src.name = argv[1];
  593. dest.name = argv[2];
  594. blksects = blocksize / sectsz;
  595. if (blksects < 1)
  596. blksects = 1;
  597. buf = malloc(blocksize);
  598. vfybuf = malloc(blocksize);
  599. if (buf == nil || vfybuf == nil)
  600. sysfatal("out of memory: %r");
  601. if (plsconfirm? confirm(&src, &dest): Yes)
  602. copyfile(&src, &dest, atoll(argv[0]), plsverify);
  603. exits(src.harderrs || dest.harderrs? "hard errors": 0);
  604. }