5 typedef struct ASum ASum;
13 static void sealarena(Arena *arena);
14 static int okarena(Arena *arena);
15 static int loadarena(Arena *arena);
16 static CIBlock *getcib(Arena *arena, int clump, int writing, CIBlock *rock);
17 static void putcib(Arena *arena, CIBlock *cib);
18 static void sumproc(void *);
21 static Rendez sumwait;
23 static uchar zero[8192];
25 int arenasumsleeptime;
32 if(vtproc(sumproc, nil) < 0){
33 seterr(EOk, "can't start arena checksum slave: %r");
40 * make an Arena, and initialize it based upon the disk header and trailer.
43 initarena(Part *part, u64int base, u64int size, u32int blocksize)
49 arena->blocksize = blocksize;
50 arena->clumpmax = arena->blocksize / ClumpInfoSize;
51 arena->base = base + blocksize;
52 arena->size = size - 2 * blocksize;
54 if(loadarena(arena) < 0){
55 seterr(ECorrupt, "arena header or trailer corrupted");
59 if(okarena(arena) < 0){
64 if(arena->diskstats.sealed && scorecmp(zeroscore, arena->score)==0)
71 freearena(Arena *arena)
79 newarena(Part *part, u32int vers, char *name, u64int base, u64int size, u32int blocksize)
85 seterr(EOk, "illegal arena name", name);
90 arena->version = vers;
91 if(vers == ArenaVersion4)
92 arena->clumpmagic = _ClumpMagic;
95 arena->clumpmagic = fastrand();
96 while(arena->clumpmagic==_ClumpMagic || arena->clumpmagic==0);
98 arena->blocksize = blocksize;
99 arena->clumpmax = arena->blocksize / ClumpInfoSize;
100 arena->base = base + blocksize;
101 arena->size = size - 2 * blocksize;
103 namecp(arena->name, name);
106 if(bsize > arena->blocksize)
107 bsize = arena->blocksize;
109 if(wbarena(arena)<0 || wbarenahead(arena)<0
110 || writepart(arena->part, arena->base, zero, bsize)<0){
119 readclumpinfo(Arena *arena, int clump, ClumpInfo *ci)
123 cib = getcib(arena, clump, 0, &r);
126 unpackclumpinfo(ci, &cib->data->data[cib->offset]);
132 readclumpinfos(Arena *arena, int clump, ClumpInfo *cis, int n)
137 for(i = 0; i < n; i++){
138 cib = getcib(arena, clump + i, 0, &r);
141 unpackclumpinfo(&cis[i], &cib->data->data[cib->offset]);
148 * write directory information for one clump
149 * must be called the arena locked
152 writeclumpinfo(Arena *arena, int clump, ClumpInfo *ci)
156 cib = getcib(arena, clump, 1, &r);
159 dirtydblock(cib->data, DirtyArenaCib);
160 packclumpinfo(ci, &cib->data->data[cib->offset]);
166 arenadirsize(Arena *arena, u32int clumps)
168 return ((clumps / arena->clumpmax) + 1) * arena->blocksize;
172 * read a clump of data
173 * n is a hint of the size of the data, not including the header
174 * make sure it won't run off the end, then return the number of bytes actually read
177 readarena(Arena *arena, u64int aa, u8int *buf, long n)
181 u32int blocksize, off, m;
188 a = arena->size - arenadirsize(arena, arena->memstats.clumps);
189 qunlock(&arena->lock);
191 seterr(EOk, "reading beyond arena clump storage: clumps=%d aa=%lld a=%lld -1 clumps=%lld\n", arena->memstats.clumps, aa, a, arena->size - arenadirsize(arena, arena->memstats.clumps - 1));
197 blocksize = arena->blocksize;
198 a = arena->base + aa;
199 off = a & (blocksize - 1);
203 b = getdblock(arena->part, a, OREAD);
209 memmove(&buf[nn], &b->data[off], m);
221 * write some data to the clump section at a given offset
222 * used to fix up corrupted arenas.
225 writearena(Arena *arena, u64int aa, u8int *clbuf, u32int n)
229 u32int blocksize, off, m;
237 a = arena->size - arenadirsize(arena, arena->memstats.clumps);
238 if(aa >= a || aa + n > a){
239 qunlock(&arena->lock);
240 seterr(EOk, "writing beyond arena clump storage");
244 blocksize = arena->blocksize;
245 a = arena->base + aa;
246 off = a & (blocksize - 1);
250 b = getdblock(arena->part, a, off != 0 || off + n < blocksize ? ORDWR : OWRITE);
252 qunlock(&arena->lock);
255 dirtydblock(b, DirtyArena);
259 memmove(&b->data[off], &clbuf[nn], m);
260 // ok = writepart(arena->part, a, b->data, blocksize);
264 qunlock(&arena->lock);
273 qunlock(&arena->lock);
278 * allocate space for the clump and write it,
279 * updating the arena directory
280 ZZZ question: should this distinguish between an arena
281 filling up and real errors writing the clump?
284 writeaclump(Arena *arena, Clump *c, u8int *clbuf, u64int start, u64int *pa)
288 u32int clump, n, nn, m, off, blocksize;
292 n = c->info.size + ClumpSize + U32Size;
294 aa = arena->memstats.used;
295 if(arena->memstats.sealed
296 || aa + n + U32Size + arenadirsize(arena, arena->memstats.clumps + 1) > arena->size){
297 if(!arena->memstats.sealed){
298 trace(0, "seal memstats %s", arena->name);
299 arena->memstats.sealed = 1;
302 as.stats = arena->memstats;
305 qunlock(&arena->lock);
308 if(packclump(c, &clbuf[0], arena->clumpmagic) < 0){
309 qunlock(&arena->lock);
314 * write the data out one block at a time
316 blocksize = arena->blocksize;
317 a = arena->base + aa;
318 off = a & (blocksize - 1);
322 b = getdblock(arena->part, a, off != 0 ? ORDWR : OWRITE);
324 qunlock(&arena->lock);
327 dirtydblock(b, DirtyArena);
331 memmove(&b->data[off], &clbuf[nn], m);
332 // ok = writepart(arena->part, a, b->data, blocksize);
336 qunlock(&arena->lock);
346 arena->memstats.used += c->info.size + ClumpSize;
347 arena->memstats.uncsize += c->info.uncsize;
348 if(c->info.size < c->info.uncsize)
349 arena->memstats.cclumps++;
351 clump = arena->memstats.clumps++;
352 if(arena->memstats.clumps == 0)
353 sysfatal("clumps wrapped");
354 arena->wtime = now();
355 if(arena->ctime == 0)
356 arena->ctime = arena->wtime;
358 writeclumpinfo(arena, clump, &c->info);
360 /* set up for call to setdcachestate */
362 as.aa = start+arena->memstats.used;
363 as.stats = arena->memstats;
365 /* update this before calling setdcachestate so it cannot be behind dcache.diskstate */
368 qunlock(&arena->lock);
374 atailcmp(ATailStats *a, ATailStats *b)
377 if(a->used < b->used)
379 if(a->used > b->used)
382 /* suspect tests - why order this way? (no one cares) */
383 if(a->clumps < b->clumps)
385 if(a->clumps > b->clumps)
387 if(a->cclumps < b->cclumps)
389 if(a->cclumps > b->cclumps)
391 if(a->uncsize < b->uncsize)
393 if(a->uncsize > b->uncsize)
395 if(a->sealed < b->sealed)
397 if(a->sealed > b->sealed)
400 /* everything matches */
405 setatailstate(AState *as)
411 trace(0, "setatailstate %s 0x%llux clumps %d", as->arena->name, as->aa, as->stats.clumps);
414 for(i=0; i<ix->narenas; i++)
415 if(ix->arenas[i] == as->arena)
417 if(i==ix->narenas || as->aa < ix->amap[i].start || as->aa >= ix->amap[i].stop || as->arena != ix->arenas[i]){
418 fprint(2, "funny settailstate 0x%llux\n", as->aa);
424 if(atailcmp(&a->diskstats, &a->memstats) == 0)
430 osealed = a->diskstats.sealed;
432 a->diskstats = as->stats;
434 a->diskstats = a->memstats;
436 if(a->diskstats.sealed != osealed && !a->inqueue)
443 * once sealed, an arena never has any data added to it.
444 * it should only be changed to fix errors.
445 * this also syncs the clump directory.
448 sealarena(Arena *arena)
455 backsumarena(Arena *arena)
474 sumproc(void *unused)
496 sumarena(Arena *arena)
502 u8int score[VtScoreSize];
505 if(bs < arena->blocksize)
506 bs = arena->blocksize;
509 * read & sum all blocks except the last one
511 memset(&s, 0, sizeof s);
512 b = alloczblock(bs, 0, arena->part->blocksize);
513 e = arena->base + arena->size;
514 for(a = arena->base - arena->blocksize; a + arena->blocksize <= e; a += bs){
515 sleep(arenasumsleeptime);
517 bs = arena->blocksize;
518 if(readpart(arena->part, a, b->data, bs) < 0)
520 addstat(StatSumRead, 1);
521 addstat(StatSumReadBytes, bs);
522 sha1(b->data, bs, nil, &s);
526 * the last one is special, since it may already have the checksum included
528 bs = arena->blocksize;
529 if(readpart(arena->part, e, b->data, bs) < 0){
531 logerr(EOk, "sumarena can't sum %s, read at %lld failed: %r", arena->name, a);
535 addstat(StatSumRead, 1);
536 addstat(StatSumReadBytes, bs);
538 sha1(b->data, bs-VtScoreSize, nil, &s);
539 sha1(zeroscore, VtScoreSize, nil, &s);
540 sha1(nil, 0, score, &s);
543 * check for no checksum or the same
545 * the writepart is okay because we flushed the dcache in sealarena
547 if(scorecmp(score, &b->data[bs - VtScoreSize]) != 0){
548 if(scorecmp(zeroscore, &b->data[bs - VtScoreSize]) != 0)
549 logerr(EOk, "overwriting mismatched checksums for arena=%s, found=%V calculated=%V",
550 arena->name, &b->data[bs - VtScoreSize], score);
551 scorecp(&b->data[bs - VtScoreSize], score);
552 if(writepart(arena->part, e, b->data, bs) < 0)
553 logerr(EOk, "sumarena can't write sum for %s: %r", arena->name);
558 scorecp(arena->score, score);
559 qunlock(&arena->lock);
563 * write the arena trailer block to the partition
566 wbarena(Arena *arena)
571 if((b = getdblock(arena->part, arena->base + arena->size, OWRITE)) == nil){
572 logerr(EAdmin, "can't write arena trailer: %r");
575 dirtydblock(b, DirtyArenaTrailer);
576 bad = okarena(arena)<0 || packarena(arena, b->data)<0;
584 wbarenahead(Arena *arena)
590 namecp(head.name, arena->name);
591 head.version = arena->version;
592 head.size = arena->size + 2 * arena->blocksize;
593 head.blocksize = arena->blocksize;
594 head.clumpmagic = arena->clumpmagic;
595 b = alloczblock(arena->blocksize, 1, arena->part->blocksize);
597 logerr(EAdmin, "can't write arena header: %r");
598 ///ZZZ add error message?
602 * this writepart is okay because it only happens
603 * during initialization.
605 bad = packarenahead(&head, b->data)<0 ||
606 writepart(arena->part, arena->base - arena->blocksize, b->data, arena->blocksize)<0;
614 * read the arena header and trailer blocks from disk
617 loadarena(Arena *arena)
622 b = alloczblock(arena->blocksize, 0, arena->part->blocksize);
625 if(readpart(arena->part, arena->base + arena->size, b->data, arena->blocksize) < 0){
629 if(unpackarena(arena, b->data) < 0){
633 if(arena->version != ArenaVersion4 && arena->version != ArenaVersion5){
634 seterr(EAdmin, "unknown arena version %d", arena->version);
638 scorecp(arena->score, &b->data[arena->blocksize - VtScoreSize]);
640 if(readpart(arena->part, arena->base - arena->blocksize, b->data, arena->blocksize) < 0){
641 logerr(EAdmin, "can't read arena header: %r");
645 if(unpackarenahead(&head, b->data) < 0)
646 logerr(ECorrupt, "corrupted arena header: %r");
647 else if(namecmp(arena->name, head.name)!=0
648 || arena->clumpmagic != head.clumpmagic
649 || arena->version != head.version
650 || arena->blocksize != head.blocksize
651 || arena->size + 2 * arena->blocksize != head.size){
652 if(namecmp(arena->name, head.name)!=0)
653 logerr(ECorrupt, "arena tail name %s head %s",
654 arena->name, head.name);
655 else if(arena->clumpmagic != head.clumpmagic)
656 logerr(ECorrupt, "arena tail clumpmagic 0x%lux head 0x%lux",
657 (ulong)arena->clumpmagic, (ulong)head.clumpmagic);
658 else if(arena->version != head.version)
659 logerr(ECorrupt, "arena tail version %d head version %d",
660 arena->version, head.version);
661 else if(arena->blocksize != head.blocksize)
662 logerr(ECorrupt, "arena tail block size %d head %d",
663 arena->blocksize, head.blocksize);
664 else if(arena->size+2*arena->blocksize != head.size)
665 logerr(ECorrupt, "arena tail size %lud head %lud",
666 (ulong)arena->size+2*arena->blocksize, head.size);
668 logerr(ECorrupt, "arena header inconsistent with arena data");
676 okarena(Arena *arena)
682 dsize = arenadirsize(arena, arena->diskstats.clumps);
683 if(arena->diskstats.used + dsize > arena->size){
684 seterr(ECorrupt, "arena used > size");
688 if(arena->diskstats.cclumps > arena->diskstats.clumps)
689 logerr(ECorrupt, "arena has more compressed clumps than total clumps");
691 if(arena->diskstats.uncsize + arena->diskstats.clumps * ClumpSize + arena->blocksize < arena->diskstats.used)
692 logerr(ECorrupt, "arena uncompressed size inconsistent with used space %lld %d %lld", arena->diskstats.uncsize, arena->diskstats.clumps, arena->diskstats.used);
694 if(arena->ctime > arena->wtime)
695 logerr(ECorrupt, "arena creation time after last write time");
701 getcib(Arena *arena, int clump, int writing, CIBlock *rock)
707 if(clump >= arena->memstats.clumps){
708 seterr(EOk, "clump directory access out of range");
711 block = clump / arena->clumpmax;
712 off = (clump - block * arena->clumpmax) * ClumpInfoSize;
718 if(off == 0 && clump == arena->memstats.clumps-1)
725 cib->data = getdblock(arena->part,
726 arena->base + arena->size - (block + 1) * arena->blocksize, mode);
733 putcib(Arena *arena, CIBlock *cib)
737 putdblock(cib->data);