archive.c 10 KB

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