2 * Archiver. In charge of sending blocks to Venti.
10 #include "9.h" /* for consPrint */
14 static void archThread(void*);
31 archInit(Cache *c, Disk *disk, Fs *fs, VtConn *z)
35 a = vtmallocz(sizeof(Arch));
40 a->blockSize = diskBlockSize(disk);
44 proccreate(archThread, a, STACK);
63 ventiSend(Arch *a, Block *b, uchar *data)
66 uchar score[VtScoreSize];
69 fprint(2, "ventiSend: sending %#ux %L to venti\n", b->addr, &b->l);
70 n = vtzerotruncate(vtType[b->l.type], data, a->blockSize);
72 fprint(2, "ventiSend: truncate %d to %d\n", a->blockSize, n);
73 if(vtwrite(a->z, score, vtType[b->l.type], data, n) < 0){
74 fprint(2, "ventiSend: vtwrite block %#ux failed: %r\n", b->addr);
77 if(vtsha1check(score, data, n) < 0){
78 uchar score2[VtScoreSize];
79 vtsha1(score2, data, n);
80 fprint(2, "ventiSend: vtwrite block %#ux failed vtsha1check %V %V\n",
81 b->addr, score, score2);
90 * parameters for recursion; there are so many,
91 * and some only change occasionally. this is
92 * easier than spelling things out at each call.
94 typedef struct Param Param;
97 /* these never change */
98 uint snapEpoch; /* epoch for snapshot being archived */
103 /* changes on every call */
116 /* these occasionally change (must save old values and put back) */
120 /* return value; avoids using stack space */
122 uchar score[VtScoreSize];
126 shaBlock(uchar score[VtScoreSize], Block *b, uchar *data, uint bsize)
128 vtsha1(score, data, vtzerotruncate(vtType[b->l.type], data, bsize));
132 copyBlock(Block *b, u32int blockSize)
136 data = vtmalloc(blockSize);
139 memmove(data, b->data, blockSize);
144 * Walk over the block tree, archiving it to Venti.
146 * We don't archive the snapshots. Instead we zero the
147 * entries in a temporary copy of the block and archive that.
151 * ArchFailure some error occurred
152 * ArchSuccess block and all children archived
153 * ArchFaked success, but block or children got copied
162 archWalk(Param *p, u32int addr, uchar type, u32int tag)
164 int ret, i, x, psize, dsize;
165 uchar *data, score[VtScoreSize];
174 b = cacheLocalData(p->c, addr, type, tag, OReadWrite,0);
176 fprint(2, "archive(%ud, %#ux): cannot find block: %r\n", p->snapEpoch, addr);
177 rerrstr(err, sizeof err);
178 if(strcmp(err, ELabelMismatch) == 0){
179 /* might as well plod on so we write _something_ to Venti */
180 memmove(p->score, vtzeroscore, VtScoreSize);
186 if(DEBUG) fprint(2, "%*sarchive(%ud, %#ux): block label %L\n",
187 p->depth*2, "", p->snapEpoch, b->addr, &b->l);
189 if(p->depth > p->maxdepth)
190 p->maxdepth = p->depth;
193 if((b->l.state&BsVenti) == 0){
194 initWalk(&w, b, b->l.type==BtDir ? p->dsize : p->psize);
195 for(i=0; nextWalk(&w, score, &type, &tag, &e); i++){
197 if(!(e->flags&VtEntryActive))
199 if((e->snap && !e->archive)
200 || (e->flags&VtEntryNoArchive)){
201 if(0) fprint(2, "snap; faking %#ux\n", b->addr);
203 data = copyBlock(b, p->blockSize);
210 memmove(e->score, vtzeroscore, VtScoreSize);
214 e->flags &= ~VtEntryLocal;
215 entryPack(e, data, w.n-1);
219 addr = globalToLocal(score);
229 x = archWalk(p, addr, type, tag);
235 while(b->iostate != BioClean && b->iostate != BioDirty)
239 fprint(2, "archWalk %#ux failed; ptr is in %#ux offset %d\n",
245 * When we're writing the entry for an archive directory
246 * (like /archive/2003/1215) then even if we've faked
247 * any data, record the score unconditionally.
248 * This way, we will always record the Venti score here.
249 * Otherwise, temporary data or corrupted file system
250 * would cause us to keep holding onto the on-disk
251 * copy of the archive.
253 if(e==nil || !e->archive)
255 if(0) fprint(2, "faked %#ux, faking %#ux (%V)\n", addr, b->addr, p->score);
256 data = copyBlock(b, p->blockSize);
264 if(0) fprint(2, "falling\n");
267 memmove(e->score, p->score, VtScoreSize);
268 e->flags &= ~VtEntryLocal;
269 entryPack(e, data, w.n-1);
271 memmove(data+(w.n-1)*VtScoreSize, p->score, VtScoreSize);
275 * If b is in the active tree, then we need to note that we've
276 * just removed addr from the active tree (replacing it with the
277 * copy we just stored to Venti). If addr is in other snapshots,
278 * this will close addr but not free it, since it has a non-empty
281 * If b is in the active tree but has been copied (this can happen
282 * if we get killed at just the right moment), then we will
283 * mistakenly leak its kids.
285 * The children of an archive directory (e.g., /archive/2004/0604)
286 * are not treated as in the active tree.
288 if((b->l.state&BsCopied)==0 && (e==nil || e->snap==0))
289 blockRemoveLink(b, addr, p->l.type, p->l.tag, 0);
295 if(!ventiSend(p->a, b, data)){
303 if(data == b->data){ /* not faking it, so update state */
307 if(!blockSetLabel(b, &l, 0)){
314 shaBlock(p->score, b, data, p->blockSize);
315 if(0) fprint(2, "ventisend %V %p %p %p\n", p->score, data, b->data, w.data);
316 ret = data!=b->data ? ArchFaked : ArchSuccess;
335 uchar rbuf[VtRootSize];
338 threadsetname("arch");
343 b = superGet(a->c, &super);
345 wunlock(&a->fs->elk);
346 fprint(2, "archThread: superGet: %r\n");
351 if(addr != NilBlock && super.current == NilBlock){
352 super.current = addr;
353 super.next = NilBlock;
354 superPack(&super, b->data);
357 addr = super.current;
359 wunlock(&a->fs->elk);
361 if(addr == NilBlock){
371 sleep(10*1000); /* window of opportunity to provoke races */
374 memset(&p, 0, sizeof p);
375 p.blockSize = a->blockSize;
376 p.dsize = 3*VtEntrySize; /* root has three Entries */
380 ret = archWalk(&p, addr, BtDir, RootTag);
385 fprint(2, "archiveBlock %#ux: %r\n", addr);
393 if(0) fprint(2, "archiveSnapshot 0x%#ux: maxdepth %ud nfixed %ud"
394 " send %ud nfailsend %ud nvisit %ud"
395 " nreclaim %ud nfake %ud nreal %ud\n",
396 addr, p.maxdepth, p.nfixed,
397 p.nsend, p.nfailsend, p.nvisit,
398 p.nreclaim, p.nfake, p.nreal);
399 if(0) fprint(2, "archiveBlock %V (%ud)\n", p.score, p.blockSize);
401 /* tie up vac root */
402 memset(&root, 0, sizeof root);
403 strecpy(root.type, root.type+sizeof root.type, "vac");
404 strecpy(root.name, root.name+sizeof root.name, "fossil");
405 memmove(root.score, p.score, VtScoreSize);
406 memmove(root.prev, super.last, VtScoreSize);
407 root.blocksize = a->blockSize;
408 vtrootpack(&root, rbuf);
409 if(vtwrite(a->z, p.score, VtRootType, rbuf, VtRootSize) < 0
410 || vtsha1check(p.score, rbuf, VtRootSize) < 0){
411 fprint(2, "vtWriteBlock %#ux: %r\n", addr);
418 b = superGet(a->c, &super);
420 wunlock(&a->fs->elk);
421 fprint(2, "archThread: superGet: %r\n");
425 super.current = NilBlock;
426 memmove(super.last, p.score, VtScoreSize);
427 superPack(&super, b->data);
430 wunlock(&a->fs->elk);
432 consPrint("archive vac:%V\n", p.score);
445 fprint(2, "warning: archKick nil\n");