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