archive.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Archiver. In charge of sending blocks to Venti.
  3. */
  4. #include "stdinc.h"
  5. #include "dat.h"
  6. #include "fns.h"
  7. #include "error.h"
  8. #include "9.h" /* for consPrint */
  9. #define DEBUG 0
  10. static void archThread(void*);
  11. struct Arch
  12. {
  13. int ref;
  14. uint blockSize;
  15. uint diskSize;
  16. Cache *c;
  17. Fs *fs;
  18. VtSession *z;
  19. VtLock *lk;
  20. VtRendez *starve;
  21. VtRendez *die;
  22. };
  23. Arch *
  24. archInit(Cache *c, Disk *disk, Fs *fs, VtSession *z)
  25. {
  26. Arch *a;
  27. a = vtMemAllocZ(sizeof(Arch));
  28. a->c = c;
  29. a->z = z;
  30. a->fs = fs;
  31. a->blockSize = diskBlockSize(disk);
  32. a->lk = vtLockAlloc();
  33. a->starve = vtRendezAlloc(a->lk);
  34. a->ref = 2;
  35. vtThread(archThread, a);
  36. return a;
  37. }
  38. void
  39. archFree(Arch *a)
  40. {
  41. /* kill slave */
  42. vtLock(a->lk);
  43. a->die = vtRendezAlloc(a->lk);
  44. vtWakeup(a->starve);
  45. while(a->ref > 1)
  46. vtSleep(a->die);
  47. vtUnlock(a->lk);
  48. vtRendezFree(a->starve);
  49. vtRendezFree(a->die);
  50. vtLockFree(a->lk);
  51. vtMemFree(a);
  52. }
  53. static int
  54. ventiSend(Arch *a, Block *b, uchar *data)
  55. {
  56. uint n;
  57. uchar score[VtScoreSize];
  58. if(DEBUG > 1)
  59. fprint(2, "ventiSend: sending %#ux %L to venti\n", b->addr, &b->l);
  60. n = vtZeroTruncate(vtType[b->l.type], data, a->blockSize);
  61. if(DEBUG > 1)
  62. fprint(2, "ventiSend: truncate %d to %d\n", a->blockSize, n);
  63. if(!vtWrite(a->z, score, vtType[b->l.type], data, n)){
  64. fprint(2, "ventiSend: vtWrite block %#ux failed: %R\n", b->addr);
  65. return 0;
  66. }
  67. if(!vtSha1Check(score, data, n)){
  68. uchar score2[VtScoreSize];
  69. vtSha1(score2, data, n);
  70. fprint(2, "ventiSend: vtWrite block %#ux failed vtSha1Check %V %V\n",
  71. b->addr, score, score2);
  72. return 0;
  73. }
  74. if(!vtSync(a->z))
  75. return 0;
  76. return 1;
  77. }
  78. /*
  79. * parameters for recursion; there are so many,
  80. * and some only change occasionally. this is
  81. * easier than spelling things out at each call.
  82. */
  83. typedef struct Param Param;
  84. struct Param
  85. {
  86. /* these never change */
  87. uint snapEpoch; /* epoch for snapshot being archived */
  88. uint blockSize;
  89. Cache *c;
  90. Arch *a;
  91. /* changes on every call */
  92. uint depth;
  93. /* statistics */
  94. uint nfixed;
  95. uint nsend;
  96. uint nvisit;
  97. uint nfailsend;
  98. uint maxdepth;
  99. uint nreclaim;
  100. uint nfake;
  101. uint nreal;
  102. /* these occasionally change (must save old values and put back) */
  103. uint dsize;
  104. uint psize;
  105. /* return value; avoids using stack space */
  106. Label l;
  107. uchar score[VtScoreSize];
  108. };
  109. static void
  110. shaBlock(uchar score[VtScoreSize], Block *b, uchar *data, uint bsize)
  111. {
  112. vtSha1(score, data, vtZeroTruncate(vtType[b->l.type], data, bsize));
  113. }
  114. static uint
  115. etype(Entry *e)
  116. {
  117. uint t;
  118. if(e->flags&VtEntryDir)
  119. t = BtDir;
  120. else
  121. t = BtData;
  122. return t+e->depth;
  123. }
  124. static uchar*
  125. copyBlock(Block *b, u32int blockSize)
  126. {
  127. uchar *data;
  128. data = vtMemAlloc(blockSize);
  129. if(data == nil)
  130. return nil;
  131. memmove(data, b->data, blockSize);
  132. return data;
  133. }
  134. /*
  135. * Walk over the block tree, archiving it to Venti.
  136. *
  137. * We don't archive the snapshots. Instead we zero the
  138. * entries in a temporary copy of the block and archive that.
  139. *
  140. * Return value is:
  141. *
  142. * ArchFailure some error occurred
  143. * ArchSuccess block and all children archived
  144. * ArchFaked success, but block or children got copied
  145. */
  146. enum
  147. {
  148. ArchFailure,
  149. ArchSuccess,
  150. ArchFaked,
  151. };
  152. static int
  153. archWalk(Param *p, u32int addr, uchar type, u32int tag)
  154. {
  155. int ret, i, x, psize, dsize;
  156. uchar *data, score[VtScoreSize];
  157. Block *b;
  158. Label l;
  159. Entry *e;
  160. WalkPtr w;
  161. p->nvisit++;
  162. b = cacheLocalData(p->c, addr, type, tag, OReadWrite,0);
  163. if(b == nil){
  164. fprint(2, "archive(%ud, %#ux): cannot find block: %R\n", p->snapEpoch, addr);
  165. if(strcmp(vtGetError(), ELabelMismatch) == 0){
  166. /* might as well plod on so we write _something_ to Venti */
  167. memmove(p->score, vtZeroScore, VtScoreSize);
  168. return ArchFaked;
  169. }
  170. return ArchFailure;
  171. }
  172. if(DEBUG) fprint(2, "%*sarchive(%ud, %#ux): block label %L\n",
  173. p->depth*2, "", p->snapEpoch, b->addr, &b->l);
  174. p->depth++;
  175. if(p->depth > p->maxdepth)
  176. p->maxdepth = p->depth;
  177. data = b->data;
  178. if((b->l.state&BsVenti) == 0){
  179. initWalk(&w, b, b->l.type==BtDir ? p->dsize : p->psize);
  180. for(i=0; nextWalk(&w, score, &type, &tag, &e); i++){
  181. if(e){
  182. if(!(e->flags&VtEntryActive))
  183. continue;
  184. if((e->snap && !e->archive)
  185. || (e->flags&VtEntryNoArchive)){
  186. if(0) fprint(2, "snap; faking %#ux\n", b->addr);
  187. if(data == b->data){
  188. data = copyBlock(b, p->blockSize);
  189. if(data == nil){
  190. ret = ArchFailure;
  191. goto Out;
  192. }
  193. w.data = data;
  194. }
  195. memmove(e->score, vtZeroScore, VtScoreSize);
  196. e->depth = 0;
  197. e->size = 0;
  198. e->tag = 0;
  199. e->flags &= ~VtEntryLocal;
  200. entryPack(e, data, w.n-1);
  201. continue;
  202. }
  203. }
  204. addr = globalToLocal(score);
  205. if(addr == NilBlock)
  206. continue;
  207. dsize = p->dsize;
  208. psize = p->psize;
  209. if(e){
  210. p->dsize= e->dsize;
  211. p->psize = e->psize;
  212. }
  213. vtUnlock(b->lk);
  214. x = archWalk(p, addr, type, tag);
  215. vtLock(b->lk);
  216. if(e){
  217. p->dsize = dsize;
  218. p->psize = psize;
  219. }
  220. while(b->iostate != BioClean && b->iostate != BioDirty)
  221. vtSleep(b->ioready);
  222. switch(x){
  223. case ArchFailure:
  224. fprint(2, "archWalk %#ux failed; ptr is in %#ux offset %d\n",
  225. addr, b->addr, i);
  226. ret = ArchFailure;
  227. goto Out;
  228. case ArchFaked:
  229. /*
  230. * When we're writing the entry for an archive directory
  231. * (like /archive/2003/1215) then even if we've faked
  232. * any data, record the score unconditionally.
  233. * This way, we will always record the Venti score here.
  234. * Otherwise, temporary data or corrupted file system
  235. * would cause us to keep holding onto the on-disk
  236. * copy of the archive.
  237. */
  238. if(e==nil || !e->archive)
  239. if(data == b->data){
  240. if(0) fprint(2, "faked %#ux, faking %#ux (%V)\n", addr, b->addr, p->score);
  241. data = copyBlock(b, p->blockSize);
  242. if(data == nil){
  243. ret = ArchFailure;
  244. goto Out;
  245. }
  246. w.data = data;
  247. }
  248. /* fall through */
  249. if(0) fprint(2, "falling\n");
  250. case ArchSuccess:
  251. if(e){
  252. memmove(e->score, p->score, VtScoreSize);
  253. e->flags &= ~VtEntryLocal;
  254. entryPack(e, data, w.n-1);
  255. }else
  256. memmove(data+(w.n-1)*VtScoreSize, p->score, VtScoreSize);
  257. if(data == b->data){
  258. blockDirty(b);
  259. /*
  260. * If b is in the active tree, then we need to note that we've
  261. * just removed addr from the active tree (replacing it with the
  262. * copy we just stored to Venti). If addr is in other snapshots,
  263. * this will close addr but not free it, since it has a non-empty
  264. * epoch range.
  265. *
  266. * If b is in the active tree but has been copied (this can happen
  267. * if we get killed at just the right moment), then we will
  268. * mistakenly leak its kids.
  269. *
  270. * The children of an archive directory (e.g., /archive/2004/0604)
  271. * are not treated as in the active tree.
  272. */
  273. if((b->l.state&BsCopied)==0 && (e==nil || e->snap==0))
  274. blockRemoveLink(b, addr, p->l.type, p->l.tag, 0);
  275. }
  276. break;
  277. }
  278. }
  279. if(!ventiSend(p->a, b, data)){
  280. p->nfailsend++;
  281. ret = ArchFailure;
  282. goto Out;
  283. }
  284. p->nsend++;
  285. if(data != b->data)
  286. p->nfake++;
  287. if(data == b->data){ /* not faking it, so update state */
  288. p->nreal++;
  289. l = b->l;
  290. l.state |= BsVenti;
  291. if(!blockSetLabel(b, &l, 0)){
  292. ret = ArchFailure;
  293. goto Out;
  294. }
  295. }
  296. }
  297. shaBlock(p->score, b, data, p->blockSize);
  298. if(0) fprint(2, "ventisend %V %p %p %p\n", p->score, data, b->data, w.data);
  299. ret = data!=b->data ? ArchFaked : ArchSuccess;
  300. p->l = b->l;
  301. Out:
  302. if(data != b->data)
  303. vtMemFree(data);
  304. p->depth--;
  305. blockPut(b);
  306. return ret;
  307. }
  308. static void
  309. archThread(void *v)
  310. {
  311. Arch *a = v;
  312. Block *b;
  313. Param p;
  314. Super super;
  315. int ret;
  316. u32int addr;
  317. uchar rbuf[VtRootSize];
  318. VtRoot root;
  319. vtThreadSetName("arch");
  320. for(;;){
  321. /* look for work */
  322. vtLock(a->fs->elk);
  323. b = superGet(a->c, &super);
  324. if(b == nil){
  325. vtUnlock(a->fs->elk);
  326. fprint(2, "archThread: superGet: %R\n");
  327. sleep(60*1000);
  328. continue;
  329. }
  330. addr = super.next;
  331. if(addr != NilBlock && super.current == NilBlock){
  332. super.current = addr;
  333. super.next = NilBlock;
  334. superPack(&super, b->data);
  335. blockDirty(b);
  336. }else
  337. addr = super.current;
  338. blockPut(b);
  339. vtUnlock(a->fs->elk);
  340. if(addr == NilBlock){
  341. /* wait for work */
  342. vtLock(a->lk);
  343. vtSleep(a->starve);
  344. if(a->die != nil)
  345. goto Done;
  346. vtUnlock(a->lk);
  347. continue;
  348. }
  349. sleep(10*1000); /* window of opportunity to provoke races */
  350. /* do work */
  351. memset(&p, 0, sizeof p);
  352. p.blockSize = a->blockSize;
  353. p.dsize = 3*VtEntrySize; /* root has three Entries */
  354. p.c = a->c;
  355. p.a = a;
  356. ret = archWalk(&p, addr, BtDir, RootTag);
  357. switch(ret){
  358. default:
  359. abort();
  360. case ArchFailure:
  361. fprint(2, "archiveBlock %#ux: %R\n", addr);
  362. sleep(60*1000);
  363. continue;
  364. case ArchSuccess:
  365. case ArchFaked:
  366. break;
  367. }
  368. if(0) fprint(2, "archiveSnapshot 0x%#ux: maxdepth %ud nfixed %ud"
  369. " send %ud nfailsend %ud nvisit %ud"
  370. " nreclaim %ud nfake %ud nreal %ud\n",
  371. addr, p.maxdepth, p.nfixed,
  372. p.nsend, p.nfailsend, p.nvisit,
  373. p.nreclaim, p.nfake, p.nreal);
  374. if(0) fprint(2, "archiveBlock %V (%ud)\n", p.score, p.blockSize);
  375. /* tie up vac root */
  376. memset(&root, 0, sizeof root);
  377. root.version = VtRootVersion;
  378. strecpy(root.type, root.type+sizeof root.type, "vac");
  379. strecpy(root.name, root.name+sizeof root.name, "fossil");
  380. memmove(root.score, p.score, VtScoreSize);
  381. memmove(root.prev, super.last, VtScoreSize);
  382. root.blockSize = a->blockSize;
  383. vtRootPack(&root, rbuf);
  384. if(!vtWrite(a->z, p.score, VtRootType, rbuf, VtRootSize)
  385. || !vtSha1Check(p.score, rbuf, VtRootSize)){
  386. fprint(2, "vtWriteBlock %#ux: %R\n", addr);
  387. sleep(60*1000);
  388. continue;
  389. }
  390. /* record success */
  391. vtLock(a->fs->elk);
  392. b = superGet(a->c, &super);
  393. if(b == nil){
  394. vtUnlock(a->fs->elk);
  395. fprint(2, "archThread: superGet: %R\n");
  396. sleep(60*1000);
  397. continue;
  398. }
  399. super.current = NilBlock;
  400. memmove(super.last, p.score, VtScoreSize);
  401. superPack(&super, b->data);
  402. blockDirty(b);
  403. blockPut(b);
  404. vtUnlock(a->fs->elk);
  405. consPrint("archive vac:%V\n", p.score);
  406. }
  407. Done:
  408. a->ref--;
  409. vtWakeup(a->die);
  410. vtUnlock(a->lk);
  411. }
  412. void
  413. archKick(Arch *a)
  414. {
  415. if(a == nil){
  416. fprint(2, "warning: archKick nil\n");
  417. return;
  418. }
  419. vtLock(a->lk);
  420. vtWakeup(a->starve);
  421. vtUnlock(a->lk);
  422. }