5 static void checkDirs(Fsck*);
6 static void checkEpochs(Fsck*);
7 static void checkLeak(Fsck*);
8 static void closenop(Fsck*, Block*, u32int);
9 static void clrenop(Fsck*, Block*, int);
10 static void clrinop(Fsck*, char*, MetaBlock*, int, Block*);
11 static void error(Fsck*, char*, ...);
12 static int getBit(uchar*, u32int);
13 static int printnop(char*, ...);
14 static void setBit(uchar*, u32int);
15 static int walkEpoch(Fsck *chk, Block *b, uchar score[VtScoreSize],
16 int type, u32int tag, u32int epoch);
17 static void warn(Fsck*, char*, ...);
19 #pragma varargck argpos error 2
20 #pragma varargck argpos printnop 1
21 #pragma varargck argpos warn 2
26 chk->cache = chk->fs->cache;
27 chk->nblocks = cacheLocalSize(chk->cache, PartData);;
28 chk->bsize = chk->fs->blockSize;
31 chk->quantum = chk->nblocks/100;
35 chk->print = printnop;
39 chk->close = closenop;
46 * BUG: Should merge checkEpochs and checkDirs so that
47 * bad blocks are only reported once, and so that errors in checkEpochs
48 * can have the affected file names attached, and so that the file system
51 * Also should summarize the errors instead of printing for every one
52 * (e.g., XXX bad or unreachable blocks in /active/usr/rsc/foo).
62 b = superGet(chk->cache, &super);
64 chk->print("could not load super block: %r");
69 chk->hint = super.active;
72 chk->smap = vtmallocz(chk->nblocks/8+1);
77 static void checkEpoch(Fsck*, u32int);
80 * Walk through all the blocks in the write buffer.
81 * Then we can look for ones we missed -- those are leaks.
84 checkEpochs(Fsck *chk)
90 chk->amap = vtmallocz(nb/8+1);
91 chk->emap = vtmallocz(nb/8+1);
92 chk->xmap = vtmallocz(nb/8+1);
93 chk->errmap = vtmallocz(nb/8+1);
95 for(e = chk->fs->ehi; e >= chk->fs->elo; e--){
96 memset(chk->emap, 0, chk->nblocks/8+1);
97 memset(chk->xmap, 0, chk->nblocks/8+1);
108 checkEpoch(Fsck *chk, u32int epoch)
115 chk->print("checking epoch %ud...\n", epoch);
117 for(a=0; a<chk->nblocks; a++){
118 if(!readLabel(chk->cache, &l, (a+chk->hint)%chk->nblocks)){
119 error(chk, "could not read label for addr 0x%.8#ux", a);
122 if(l.tag == RootTag && l.epoch == epoch)
126 if(a == chk->nblocks){
127 chk->print("could not find root block for epoch %ud", epoch);
131 a = (a+chk->hint)%chk->nblocks;
132 b = cacheLocalData(chk->cache, a, BtDir, RootTag, OReadOnly, 0);
134 error(chk, "could not read root block 0x%.8#ux: %r", a);
138 /* no one should point at root blocks */
139 setBit(chk->amap, a);
140 setBit(chk->emap, a);
141 setBit(chk->xmap, a);
144 * First entry is the rest of the file system.
145 * Second entry is link to previous epoch root,
146 * just a convenience to help the search.
148 if(!entryUnpack(&e, b->data, 0)){
149 error(chk, "could not unpack root block 0x%.8#ux: %r", a);
153 walkEpoch(chk, b, e.score, BtDir, e.tag, epoch);
154 if(entryUnpack(&e, b->data, 1))
155 chk->hint = globalToLocal(e.score);
160 * When b points at bb, need to check:
162 * (i) b.e in [bb.e, bb.eClose)
163 * (ii) if b.e==bb.e, then no other b' in e points at bb.
164 * (iii) if !(b.state&Copied) and b.e==bb.e then no other b' points at bb.
165 * (iv) if b is active then no other active b' points at bb.
166 * (v) if b is a past life of b' then only one of b and b' is active
167 * (too hard to check)
170 walkEpoch(Fsck *chk, Block *b, uchar score[VtScoreSize], int type, u32int tag,
178 if(b && chk->walkdepth == 0 && chk->printblocks)
179 chk->print("%V %d %#.8ux %#.8ux\n", b->score, b->l.type,
180 b->l.tag, b->l.epoch);
182 if(!chk->useventi && globalToLocal(score) == NilBlock)
187 bb = cacheGlobal(chk->cache, score, type, tag, OReadOnly);
189 error(chk, "could not load block %V type %d tag %ux: %r",
195 chk->print("%*s%V %d %#.8ux %#.8ux\n", chk->walkdepth*2, "",
196 score, type, tag, bb->l.epoch);
199 addr = globalToLocal(score);
200 if(addr == NilBlock){
207 if(b->l.epoch < bb->l.epoch || bb->l.epochClose <= b->l.epoch){
208 error(chk, "walk: block %#ux [%ud, %ud) points at %#ux [%ud, %ud)",
209 b->addr, b->l.epoch, b->l.epochClose,
210 bb->addr, bb->l.epoch, bb->l.epochClose);
215 if(b->l.epoch == epoch && bb->l.epoch == epoch){
216 if(getBit(chk->emap, addr)){
217 error(chk, "walk: epoch join detected: addr %#ux %L",
221 setBit(chk->emap, addr);
225 if(!(b->l.state&BsCopied) && b->l.epoch == bb->l.epoch){
226 if(getBit(chk->xmap, addr)){
227 error(chk, "walk: copy join detected; addr %#ux %L",
231 setBit(chk->xmap, addr);
236 if(epoch == chk->fs->ehi){
238 * since epoch==fs->ehi is first, amap is same as
239 * ``have seen active''
241 if(getBit(chk->amap, addr)){
242 error(chk, "walk: active join detected: addr %#ux %L",
246 if(bb->l.state&BsClosed)
247 error(chk, "walk: addr %#ux: block is in active tree but is closed",
250 if(!getBit(chk->amap, addr))
251 if(!(bb->l.state&BsClosed)){
252 // error(chk, "walk: addr %#ux: block is not in active tree, not closed (%d)",
253 // addr, bb->l.epochClose);
254 chk->close(chk, bb, epoch+1);
258 if(getBit(chk->amap, addr)){
262 setBit(chk->amap, addr);
264 if(chk->nseen++%chk->quantum == 0)
265 chk->print("check: visited %d/%d blocks (%.0f%%)\n",
266 chk->nseen, chk->nblocks, chk->nseen*100./chk->nblocks);
268 b = nil; /* make sure no more refs to parent */
274 for(i = 0; i < chk->bsize/VtScoreSize; i++)
275 if(!walkEpoch(chk, bb, bb->data + i*VtScoreSize,
276 type-1, tag, epoch)){
277 setBit(chk->errmap, bb->addr);
278 chk->clrp(chk, bb, i);
285 for(i = 0; i < chk->bsize/VtEntrySize; i++){
286 if(!entryUnpack(&e, bb->data, i)){
287 // error(chk, "walk: could not unpack entry: %ux[%d]: %r",
289 setBit(chk->errmap, bb->addr);
290 chk->clre(chk, bb, i);
294 if(!(e.flags & VtEntryActive))
296 if(0) fprint(2, "%x[%d] tag=%x snap=%d score=%V\n",
297 addr, i, e.tag, e.snap, e.score);
301 // error(chk, "bad snap in entry: %ux[%d] snap = %ud: epoch = %ud",
302 // addr, i, e.snap, epoch);
303 setBit(chk->errmap, bb->addr);
304 chk->clre(chk, bb, i);
310 if(e.flags & VtEntryLocal){
312 if(e.tag != RootTag || tag != RootTag || i != 1){
313 // error(chk, "bad tag in entry: %ux[%d] tag = %ux",
315 setBit(chk->errmap, bb->addr);
316 chk->clre(chk, bb, i);
322 // error(chk, "bad tag in entry: %ux[%d] tag = %ux",
324 setBit(chk->errmap, bb->addr);
325 chk->clre(chk, bb, i);
329 if(!walkEpoch(chk, bb, e.score, entryType(&e),
331 setBit(chk->errmap, bb->addr);
332 chk->clre(chk, bb, i);
348 * We've just walked the whole write buffer. Notice blocks that
349 * aren't marked available but that we didn't visit. They are lost.
354 u32int a, nfree, nlost;
361 for(a = 0; a < chk->nblocks; a++){
362 if(!readLabel(chk->cache, &l, a)){
363 error(chk, "could not read label: addr 0x%ux %d %d: %r",
367 if(getBit(chk->amap, a))
369 if(l.state == BsFree || l.epochClose <= chk->fs->elo ||
370 l.epochClose == l.epoch){
372 setBit(chk->amap, a);
378 // warn(chk, "unreachable block: addr 0x%ux type %d tag 0x%ux "
379 // "state %s epoch %ud close %ud", a, l.type, l.tag,
380 // bsStr(l.state), l.epoch, l.epochClose);
381 b = cacheLocal(chk->cache, PartData, a, OReadOnly);
383 error(chk, "could not read block 0x%#.8ux", a);
386 chk->close(chk, b, 0);
388 setBit(chk->amap, a);
391 chk->print("fsys blocks: total=%ud used=%ud(%.1f%%) free=%ud(%.1f%%) lost=%ud(%.1f%%)\n",
393 chk->nblocks - nfree-nlost,
394 100.*(chk->nblocks - nfree - nlost)/chk->nblocks,
395 nfree, 100.*nfree/chk->nblocks,
396 nlost, 100.*nlost/chk->nblocks);
401 * Check that all sources in the tree are accessible.
404 openSource(Fsck *chk, Source *s, char *name, uchar *bm, u32int offset,
405 u32int gen, int dir, MetaBlock *mb, int i, Block *b)
410 if(getBit(bm, offset)){
411 warn(chk, "multiple references to source: %s -> %d",
417 r = sourceOpen(s, offset, OReadOnly, 0);
419 warn(chk, "could not open source: %s -> %d: %r", name, offset);
424 warn(chk, "source has been removed: %s -> %d", name, offset);
429 warn(chk, "dir mismatch: %s -> %d", name, offset);
434 chk->clri(chk, name, mb, i, b);
441 typedef struct MetaChunk MetaChunk;
449 offsetCmp(const void *s0, const void *s1)
451 MetaChunk *mc0, *mc1;
453 mc0 = (MetaChunk*)s0;
454 mc1 = (MetaChunk*)s1;
455 if(mc0->offset < mc1->offset)
457 if(mc0->offset > mc1->offset)
463 * Fsck that MetaBlock has reasonable header, sorted entries,
466 chkMetaBlock(MetaBlock *mb)
472 mc = vtmalloc(mb->nindex*sizeof(MetaChunk));
473 p = mb->buf + MetaHeaderSize;
474 for(i = 0; i < mb->nindex; i++){
475 mc[i].offset = p[0]<<8 | p[1];
476 mc[i].size = p[2]<<8 | p[3];
481 qsort(mc, mb->nindex, sizeof(MetaChunk), offsetCmp);
483 /* check block looks ok */
484 oo = MetaHeaderSize + mb->maxindex*MetaIndexSize;
487 for(i = 0; i < mb->nindex; i++){
494 if(o+n > mb->size || mb->size - oo != mb->free)
502 fprint(2, "metaChunks failed!\n");
503 oo = MetaHeaderSize + mb->maxindex*MetaIndexSize;
504 for(i=0; i<mb->nindex; i++){
505 fprint(2, "\t%d: %d %d\n", i, mc[i].offset,
506 mc[i].offset + mc[i].size);
509 fprint(2, "\tused=%d size=%d free=%d free2=%d\n",
510 oo, mb->size, mb->free, mb->size - oo);
517 scanSource(Fsck *chk, char *name, Source *r)
523 if(!chk->useventi && globalToLocal(r->score)==NilBlock)
525 if(!sourceGetEntry(r, &e)){
526 error(chk, "could not get entry for %s", name);
529 a = globalToLocal(e.score);
530 if(!chk->useventi && a==NilBlock)
532 if(getBit(chk->smap, a))
534 setBit(chk->smap, a);
536 nb = (sourceGetSize(r) + r->dsize-1) / r->dsize;
537 for(o = 0; o < nb; o++){
538 b = sourceBlock(r, o, OReadOnly);
540 error(chk, "could not read block in data file %s", name);
543 if(b->addr != NilBlock && getBit(chk->errmap, b->addr)){
544 warn(chk, "previously reported error in block %ux is in file %s",
552 * Walk the source tree making sure that the BtData
553 * sources containing directory entries are okay.
556 chkDir(Fsck *chk, char *name, Source *source, Source *meta)
559 u32int a1, a2, nb, o;
569 if(!chk->useventi && globalToLocal(source->score)==NilBlock &&
570 globalToLocal(meta->score)==NilBlock)
573 if(!sourceLock2(source, meta, OReadOnly)){
574 warn(chk, "could not lock sources for %s: %r", name);
577 if(!sourceGetEntry(source, &e1) || !sourceGetEntry(meta, &e2)){
578 warn(chk, "could not load entries for %s: %r", name);
581 a1 = globalToLocal(e1.score);
582 a2 = globalToLocal(e2.score);
583 if((!chk->useventi && a1==NilBlock && a2==NilBlock)
584 || (getBit(chk->smap, a1) && getBit(chk->smap, a2))){
585 sourceUnlock(source);
589 setBit(chk->smap, a1);
590 setBit(chk->smap, a2);
592 bm = vtmallocz(sourceGetDirSize(source)/8 + 1);
594 nb = (sourceGetSize(meta) + meta->dsize - 1)/meta->dsize;
595 for(o = 0; o < nb; o++){
596 b = sourceBlock(meta, o, OReadOnly);
598 error(chk, "could not read block in meta file: %s[%ud]: %r",
602 if(0) fprint(2, "source %V:%d block %d addr %d\n", source->score,
603 source->offset, o, b->addr);
604 if(b->addr != NilBlock && getBit(chk->errmap, b->addr))
605 warn(chk, "previously reported error in block %ux is in %s",
608 if(!mbUnpack(&mb, b->data, meta->dsize)){
609 error(chk, "could not unpack meta block: %s[%ud]: %r",
614 if(!chkMetaBlock(&mb)){
615 error(chk, "bad meta block: %s[%ud]: %r", name, o);
620 for(i=mb.nindex-1; i>=0; i--){
621 meUnpack(&me, &mb, i);
622 if(!deUnpack(&de, &me)){
624 "could not unpack dir entry: %s[%ud][%d]: %r",
628 if(s && strcmp(s, de.elem) <= 0)
630 "dir entry out of order: %s[%ud][%d] = %s last = %s",
631 name, o, i, de.elem, s);
633 s = vtstrdup(de.elem);
634 nn = smprint("%s/%s", name, de.elem);
636 error(chk, "out of memory");
641 chk->print("%s/\n", nn);
643 if(!(de.mode&ModeDir))
644 chk->print("%s\n", nn);
645 if(!(de.mode & ModeDir)){
646 r = openSource(chk, source, nn, bm, de.entry,
647 de.gen, 0, &mb, i, b);
649 if(sourceLock(r, OReadOnly)){
650 scanSource(chk, nn, r);
660 r = openSource(chk, source, nn, bm, de.entry,
661 de.gen, 1, &mb, i, b);
668 mr = openSource(chk, source, nn, bm, de.mentry,
669 de.mgen, 0, &mb, i, b);
677 if(!(de.mode&ModeSnapshot) || chk->walksnapshots)
678 chkDir(chk, nn, r, mr);
691 nb = sourceGetDirSize(source);
695 r = sourceOpen(source, o, OReadOnly, 0);
698 warn(chk, "non referenced entry in source %s[%d]", name, o);
699 if((bb = sourceBlock(source, o/(source->dsize/VtEntrySize),
701 if(bb->addr != NilBlock){
702 setBit(chk->errmap, bb->addr);
703 chk->clre(chk, bb, o%(source->dsize/VtEntrySize));
711 sourceUnlock(source);
721 sourceLock(chk->fs->source, OReadOnly);
722 r = sourceOpen(chk->fs->source, 0, OReadOnly, 0);
723 mr = sourceOpen(chk->fs->source, 1, OReadOnly, 0);
724 sourceUnlock(chk->fs->source);
725 chkDir(chk, "", r, mr);
732 setBit(uchar *bmap, u32int addr)
737 bmap[addr>>3] |= 1 << (addr & 7);
741 getBit(uchar *bmap, u32int addr)
746 return (bmap[addr>>3] >> (addr & 7)) & 1;
750 error(Fsck *chk, char *fmt, ...)
756 vseprint(buf, buf+sizeof buf, fmt, arg);
759 chk->print("error: %s\n", buf);
762 // sysfatal("too many errors");
766 warn(Fsck *chk, char *fmt, ...)
772 vseprint(buf, buf+sizeof buf, fmt, arg);
775 chk->print("error: %s\n", buf);
779 clrenop(Fsck *chk, Block *b, int i)
787 closenop(Fsck *chk, Block *b, u32int i)
795 clrinop(Fsck *chk, char *c, MetaBlock *mb, int i, Block *b)
805 printnop(char *c, ...)